Skip to content

Commit 5694fec

Browse files
committed
removes need for init partition
1 parent 1e9c441 commit 5694fec

4 files changed

Lines changed: 122 additions & 174 deletions

File tree

core/grub.go

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,83 @@ type Grub struct {
3131
FutureRoot string
3232
}
3333

34-
// generateABGrubConf generates a new grub config with the given details
35-
func generateABGrubConf(kernelVersion string, rootPath string, rootUuid string, rootLabel string, generatedGrubConfigPath string) error {
36-
PrintVerboseInfo("generateABGrubConf", "generating grub config for ABRoot")
34+
// createABSpecificGrub creates a directory that contains the root specific root information
35+
func createABSpecificGrub(kernelVersion string, rootUuid string, rootLabel string, generatedGrubConfigPath string, bootMountpoint string, filesDir string) error {
36+
PrintVerboseInfo("createABSpecificGrub", "creating root specific grub info")
37+
38+
bootPrefix := filepath.Join("/abroot", rootLabel)
39+
configDir := filepath.Join(bootMountpoint, bootPrefix)
40+
41+
err := MoveFile(
42+
filepath.Join(filesDir, "vmlinuz-"+kernelVersion),
43+
filepath.Join(configDir, "vmlinuz-"+kernelVersion),
44+
)
45+
if err != nil {
46+
PrintVerboseErr("createABSpecificGrub", 1, err)
47+
return err
48+
}
49+
err = MoveFile(
50+
filepath.Join(filesDir, "initrd.img-"+kernelVersion),
51+
filepath.Join(configDir, "initrd.img-"+kernelVersion),
52+
)
53+
if err != nil {
54+
PrintVerboseErr("createABSpecificGrub", 2, err)
55+
return err
56+
}
57+
err = MoveFile(
58+
filepath.Join(filesDir, "config-"+kernelVersion),
59+
filepath.Join(configDir, "config-"+kernelVersion),
60+
)
61+
if err != nil {
62+
PrintVerboseErr("createABSpecificGrub", 3, err)
63+
return err
64+
}
65+
err = MoveFile(
66+
filepath.Join(filesDir, "System.map-"+kernelVersion),
67+
filepath.Join(configDir, "System.map-"+kernelVersion),
68+
)
69+
if err != nil {
70+
PrintVerboseErr("createABSpecificGrub", 4, err)
71+
return err
72+
}
3773

3874
kargs, err := KargsRead()
3975
if err != nil {
40-
PrintVerboseErr("generateABGrubConf", 0, err)
76+
PrintVerboseErr("createABSpecificGrub", 5, err)
4177
return err
4278
}
4379

44-
var grubPath, bootPrefix, systemRoot string
80+
var systemRoot string
4581
if settings.Cnf.ThinProvisioning {
46-
grubPath = filepath.Join(rootPath, "boot", "init", rootLabel)
47-
bootPrefix = "/" + rootLabel
48-
4982
diskM := NewDiskManager()
5083
sysRootPart, err := diskM.GetPartitionByLabel(rootLabel)
5184
if err != nil {
52-
PrintVerboseErr("generateABGrubConf", 3, err)
85+
PrintVerboseErr("createABSpecificGrub", 6, err)
5386
return err
5487
}
5588
systemRoot = "/dev/mapper/" + sysRootPart.Device
5689
} else {
57-
grubPath = filepath.Join(rootPath, "boot", "grub")
58-
bootPrefix = "/.system/boot"
5990
systemRoot = "UUID=" + rootUuid
6091
}
6192

62-
confPath := filepath.Join(grubPath, "abroot.cfg")
63-
template := ` search --no-floppy --fs-uuid --set=root %s
93+
confPath := filepath.Join(configDir, "abroot.cfg")
94+
template := `
6495
linux %s/vmlinuz-%s root=%s %s
6596
initrd %s/initrd.img-%s
6697
`
6798

68-
err = os.MkdirAll(grubPath, 0755)
99+
_ = os.RemoveAll(confPath)
100+
err = os.MkdirAll(configDir, 0755)
69101
if err != nil {
70-
PrintVerboseErr("generateABGrubConf", 2, err)
102+
PrintVerboseErr("createABSpecificGrub", 7, err)
71103
return err
72104
}
73105

74-
abrootBootConfig := fmt.Sprintf(template, rootUuid, bootPrefix, kernelVersion, systemRoot, kargs, bootPrefix, kernelVersion)
106+
abrootBootConfig := fmt.Sprintf(template, bootPrefix, kernelVersion, systemRoot, kargs, bootPrefix, kernelVersion)
75107

76-
generatedGrubConfigContents, err := os.ReadFile(filepath.Join(rootPath, generatedGrubConfigPath))
108+
generatedGrubConfigContents, err := os.ReadFile(generatedGrubConfigPath)
77109
if err != nil {
78-
PrintVerboseErr("generateABGrubConf", 3, "could not read grub config", err)
110+
PrintVerboseErr("createABSpecificGrub", 8, "could not read grub config", err)
79111
return err
80112
}
81113

@@ -84,18 +116,18 @@ func generateABGrubConf(kernelVersion string, rootPath string, rootUuid string,
84116
replacementString := "REPLACED_BY_ABROOT"
85117
if !strings.Contains(generatedGrubConfig, replacementString) {
86118
err := errors.New("could not find replacement string \"" + replacementString + "\", check /etc/grub.d configuration")
87-
PrintVerboseErr("generateABGrubConf", 3.1, err)
119+
PrintVerboseErr("createABSpecificGrub", 9, err)
88120
return err
89121
}
90122
grubConfigWithBootEntry := strings.Replace(generatedGrubConfig, "REPLACED_BY_ABROOT", abrootBootConfig, 1)
91123

92124
err = os.WriteFile(confPath, []byte(grubConfigWithBootEntry), 0644)
93125
if err != nil {
94-
PrintVerboseErr("generateABGrubConf", 4, "could not read grub config", err)
126+
PrintVerboseErr("createABSpecificGrub", 10, "could not read grub config", err)
95127
return err
96128
}
97129

98-
PrintVerboseInfo("generateABGrubConf", "done")
130+
PrintVerboseInfo("createABSpecificGrub", "done")
99131
return nil
100132
}
101133

core/root.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,25 +214,3 @@ func (a *ABRootManager) GetBoot() (partition Partition, err error) {
214214
PrintVerboseInfo("ABRootManager.GetBoot", "successfully got boot partition")
215215
return part, nil
216216
}
217-
218-
// GetInit gets the init volume when using LVM Thin-Provisioning
219-
func (a *ABRootManager) GetInit() (partition Partition, err error) {
220-
PrintVerboseInfo("ABRootManager.GetInit", "running...")
221-
222-
// Make sure Thin-Provisioning is properly configured
223-
if !settings.Cnf.ThinProvisioning || settings.Cnf.ThinInitVolume == "" {
224-
return Partition{}, errors.New("ABRootManager.GetInit: error: system is not configured for thin-provisioning")
225-
}
226-
227-
diskM := NewDiskManager()
228-
part, err := diskM.GetPartitionByLabel(settings.Cnf.ThinInitVolume)
229-
if err != nil {
230-
err = errors.New("init volume not found")
231-
PrintVerboseErr("ABRootManager.GetInit", 0, err)
232-
233-
return Partition{}, err
234-
}
235-
236-
PrintVerboseInfo("ABRootManager.GetInit", "successfully got init volume")
237-
return part, nil
238-
}

core/system.go

Lines changed: 68 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -518,92 +518,34 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, deleteBeforeCopy bo
518518
}
519519
}
520520

521-
var rootUuid string
522-
// If Thin-Provisioning set, mount init partition and move linux and initrd
523-
// images to it.
524-
var initMountpoint string
525-
if settings.Cnf.ThinProvisioning {
526-
initPartition, err := s.RootM.GetInit()
527-
if err != nil {
528-
PrintVerboseErr("ABSystem.RunOperation", 7.3, err)
529-
return err
530-
}
531-
532-
initMountpoint = filepath.Join(futureRoot, "boot", "init")
533-
err = initPartition.Mount(initMountpoint)
534-
if err != nil {
535-
PrintVerboseErr("ABSystem.RunOperation", 7.4, err)
536-
return err
537-
}
538-
539-
cq.Add(func(args ...interface{}) error {
540-
return initPartition.Unmount()
541-
}, nil, 80, &goodies.NoErrorHandler{}, false)
542-
543-
futureInitDir := filepath.Join(initMountpoint, partFuture.Label)
544-
545-
if !dryRun {
546-
err = os.RemoveAll(futureInitDir)
547-
if err != nil {
548-
PrintVerboseWarn("ABSystem.RunOperation", 7.44)
549-
}
550-
err = os.MkdirAll(futureInitDir, 0o755)
551-
if err != nil {
552-
PrintVerboseWarn("ABSystem.RunOperation", 7.47, err)
553-
}
554-
555-
err = MoveFile(
556-
filepath.Join(futureRoot, "boot", "vmlinuz-"+newKernelVer),
557-
filepath.Join(futureInitDir, "vmlinuz-"+newKernelVer),
558-
)
559-
if err != nil {
560-
PrintVerboseErr("ABSystem.RunOperation", 7.5, err)
561-
return err
562-
}
563-
err = MoveFile(
564-
filepath.Join(futureRoot, "boot", "initrd.img-"+newKernelVer),
565-
filepath.Join(futureInitDir, "initrd.img-"+newKernelVer),
566-
)
567-
if err != nil {
568-
PrintVerboseErr("ABSystem.RunOperation", 7.6, err)
569-
return err
570-
}
571-
err = MoveFile(
572-
filepath.Join(futureRoot, "boot", "config-"+newKernelVer),
573-
filepath.Join(futureInitDir, "config-"+newKernelVer),
574-
)
575-
if err != nil {
576-
PrintVerboseErr("ABSystem.RunOperation", 7.7, err)
577-
return err
578-
}
579-
err = MoveFile(
580-
filepath.Join(futureRoot, "boot", "System.map-"+newKernelVer),
581-
filepath.Join(futureInitDir, "System.map-"+newKernelVer),
582-
)
583-
if err != nil {
584-
PrintVerboseErr("ABSystem.RunOperation", 7.8, err)
585-
return err
586-
}
587-
588-
}
521+
tmpBootMount := "/run/abroot/tmp-boot-mount-1/"
522+
err = os.MkdirAll(tmpBootMount, 0o755)
523+
if err != nil {
524+
PrintVerboseErr("ABSystem.RunOperation", 9, err)
525+
return err
526+
}
589527

590-
rootUuid = initPartition.Uuid
591-
} else {
592-
rootUuid = partFuture.Partition.Uuid
528+
err = partBoot.Mount(tmpBootMount)
529+
if err != nil {
530+
PrintVerboseErr("ABSystem.RunOperation", 9.1, err)
531+
return err
593532
}
594533

595-
if !dryRun {
596-
err = generateABGrubConf(
597-
newKernelVer,
598-
futureRoot,
599-
rootUuid,
600-
partFuture.Label,
601-
generatedGrubConfigPath,
602-
)
603-
if err != nil {
604-
PrintVerboseErr("ABSystem.RunOperation", 7.9, err)
605-
return err
606-
}
534+
cq.Add(func(args ...interface{}) error {
535+
return partBoot.Unmount()
536+
}, nil, 100, &goodies.NoErrorHandler{}, false)
537+
538+
err = createABSpecificGrub(
539+
newKernelVer,
540+
partFuture.Partition.Uuid,
541+
partFuture.Label,
542+
filepath.Join(futureRoot, generatedGrubConfigPath),
543+
tmpBootMount,
544+
filepath.Join(futureRoot, "/boot/"),
545+
)
546+
if err != nil {
547+
PrintVerboseErr("ABSystem.RunOperation", 7.9, err)
548+
return err
607549
}
608550

609551
// Stage 7: Sync /etc
@@ -627,31 +569,10 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, deleteBeforeCopy bo
627569
}
628570
}
629571

630-
// Stage 8: Mount boot partition
572+
// Stage 8: Atomic swap the bootloader
631573
// ------------------------------------------------
632574
PrintVerboseSimple("[Stage 8] -------- ABSystemRunOperation")
633575

634-
tmpBootMount := "/run/abroot/tmp-boot-mount-1/"
635-
err = os.MkdirAll(tmpBootMount, 0o755)
636-
if err != nil {
637-
PrintVerboseErr("ABSystem.RunOperation", 9, err)
638-
return err
639-
}
640-
641-
err = partBoot.Mount(tmpBootMount)
642-
if err != nil {
643-
PrintVerboseErr("ABSystem.RunOperation", 9.1, err)
644-
return err
645-
}
646-
647-
cq.Add(func(args ...interface{}) error {
648-
return partBoot.Unmount()
649-
}, nil, 100, &goodies.NoErrorHandler{}, false)
650-
651-
// Stage 9: Atomic swap the bootloader
652-
// ------------------------------------------------
653-
PrintVerboseSimple("[Stage 9] -------- ABSystemRunOperation")
654-
655576
grub, err := NewGrub(partBoot)
656577
if err != nil {
657578
PrintVerboseErr("ABSystem.RunOperation", 11, err)
@@ -668,34 +589,53 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, deleteBeforeCopy bo
668589
grubCfgCurrent := filepath.Join(tmpBootMount, "grub/grub.cfg")
669590
grubCfgFuture := filepath.Join(tmpBootMount, "grub/grub.cfg.future")
670591

671-
// Just like in Stage 9, tmpBootMount/grub/grub.cfg.future may not exist.
672-
if _, err = os.Stat(grubCfgFuture); os.IsNotExist(err) {
673-
PrintVerboseInfo("ABSystem.RunOperation", "Creating grub.cfg.future")
592+
_ = os.Remove(grubCfgFuture)
674593

675-
grubCfgContents, err := os.ReadFile(grubCfgCurrent)
676-
if err != nil {
677-
PrintVerboseErr("ABSystem.RunOperation", 11.2, err)
678-
}
594+
const preamble string = `set timeout=5
679595
680-
var replacerPairs []string
681-
if grub.FutureRoot == "a" {
682-
replacerPairs = []string{
683-
"default=1", "default=0",
684-
"Previous State (A)", "Current State (A)",
685-
"Current State (B)", "Previous State (B)",
686-
}
687-
} else {
688-
replacerPairs = []string{
689-
"default=0", "default=1",
690-
"Current State (A)", "Previous State (A)",
691-
"Previous State (B)", "Current State (B)",
692-
}
693-
}
596+
# Load video support
597+
insmod gfxterm
598+
insmod all_video
599+
600+
# Set graphics mode
601+
set gfxmode=1024x768,auto
602+
terminal_output gfxterm
603+
604+
`
605+
606+
const state_a string = `
607+
set default=0
608+
609+
menuentry "Current State (A)" --class abroot-a {
610+
configfile "/abroot/%s/abroot.cfg"
611+
}
612+
613+
menuentry "Previous State (B)" --class abroot-b {
614+
configfile "/abroot/%s/abroot.cfg"
615+
}
616+
`
694617

695-
replacer := strings.NewReplacer(replacerPairs...)
696-
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0o644)
618+
const state_b string = `
619+
set default=1
620+
menuentry "Previous State (A)" --class abroot-a {
621+
configfile "/abroot/%s/abroot.cfg"
622+
}
623+
624+
menuentry "Current State (B)" --class abroot-b {
625+
configfile "/abroot/%s/abroot.cfg"
626+
}
627+
`
628+
629+
configFile := ""
630+
631+
if grub.FutureRoot == "a" {
632+
configFile = preamble + fmt.Sprintf(state_a, partFuture.Label, partPresent.Label)
633+
} else {
634+
configFile = preamble + fmt.Sprintf(state_b, partFuture.Label, partPresent.Label)
697635
}
698636

637+
os.WriteFile(grubCfgFuture, []byte(configFile), 0o644)
638+
699639
err = AtomicSwap(grubCfgCurrent, grubCfgFuture)
700640
if err != nil {
701641
PrintVerboseErr("ABSystem.RunOperation", 11.3, err)

settings/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ type Config struct {
5757
PartCryptVar string `json:"PartCryptVar"`
5858

5959
// Structure
60-
ThinProvisioning bool `json:"thinProvisioning"`
61-
ThinInitVolume string `json:"thinInitVolume"`
60+
ThinProvisioning bool `json:"thinProvisioning"`
6261
}
6362

6463
var Cnf *Config
@@ -150,7 +149,6 @@ func init() {
150149

151150
// Structure
152151
ThinProvisioning: viper.GetBool("thinProvisioning"),
153-
ThinInitVolume: viper.GetString("thinInitVolume"),
154152
}
155153
}
156154

0 commit comments

Comments
 (0)