Skip to content

Commit 379b08a

Browse files
committed
removes need for init partition
1 parent 8b383a8 commit 379b08a

File tree

4 files changed

+118
-165
lines changed

4 files changed

+118
-165
lines changed

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: 64 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -531,83 +531,30 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, freeSpace bool) err
531531
return err
532532
}
533533

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

600-
rootUuid = initPartition.Uuid
601-
} else {
602-
rootUuid = partFuture.Partition.Uuid
541+
err = partBoot.Mount(tmpBootMount)
542+
if err != nil {
543+
PrintVerboseErr("ABSystem.RunOperation", 9.1, err)
544+
return err
603545
}
604546

605-
err = generateABGrubConf(
547+
cq.Add(func(args ...interface{}) error {
548+
return partBoot.Unmount()
549+
}, nil, 100, &goodies.NoErrorHandler{}, false)
550+
551+
err = createABSpecificGrub(
606552
newKernelVer,
607-
systemNew,
608-
rootUuid,
553+
partFuture.Partition.Uuid,
609554
partFuture.Label,
610-
generatedGrubConfigPath,
555+
filepath.Join(systemNew, generatedGrubConfigPath),
556+
tmpBootMount,
557+
filepath.Join(systemNew, "/boot/"),
611558
)
612559
if err != nil {
613560
PrintVerboseErr("ABSystem.RunOperation", 7.9, err)
@@ -633,31 +580,10 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, freeSpace bool) err
633580
return err
634581
}
635582

636-
// Stage 8: Mount boot partition
583+
// Stage 8: Apply the new rootfs
637584
// ------------------------------------------------
638585
PrintVerboseSimple("[Stage 8] -------- ABSystemRunOperation")
639586

640-
tmpBootMount := "/run/abroot/tmp-boot-mount-1/"
641-
err = os.MkdirAll(tmpBootMount, 0o755)
642-
if err != nil {
643-
PrintVerboseErr("ABSystem.RunOperation", 9, err)
644-
return err
645-
}
646-
647-
err = partBoot.Mount(tmpBootMount)
648-
if err != nil {
649-
PrintVerboseErr("ABSystem.RunOperation", 9.1, err)
650-
return err
651-
}
652-
653-
cq.Add(func(args ...interface{}) error {
654-
return partBoot.Unmount()
655-
}, nil, 100, &goodies.NoErrorHandler{}, false)
656-
657-
// Stage 9: Apply the new rootfs
658-
// ------------------------------------------------
659-
PrintVerboseSimple("[Stage 9] -------- ABSystemRunOperation")
660-
661587
err = ClearDirectory(partFuture.Partition.MountPoint, []string{"new"})
662588
if err != nil {
663589
PrintVerboseErr("ABSystem.RunOperation", 10.1, err)
@@ -687,9 +613,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, freeSpace bool) err
687613
return err
688614
}
689615

690-
// Stage 10: Atomic swap the bootloader
616+
// Stage 9: Atomic swap the bootloader
691617
// ------------------------------------------------
692-
PrintVerboseSimple("[Stage 10] -------- ABSystemRunOperation")
618+
PrintVerboseSimple("[Stage 9] -------- ABSystemRunOperation")
693619

694620
grub, err := NewGrub(partBoot)
695621
if err != nil {
@@ -707,34 +633,53 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, freeSpace bool) err
707633
grubCfgCurrent := filepath.Join(tmpBootMount, "grub/grub.cfg")
708634
grubCfgFuture := filepath.Join(tmpBootMount, "grub/grub.cfg.future")
709635

710-
// Just like in Stage 9, tmpBootMount/grub/grub.cfg.future may not exist.
711-
if _, err = os.Stat(grubCfgFuture); os.IsNotExist(err) {
712-
PrintVerboseInfo("ABSystem.RunOperation", "Creating grub.cfg.future")
636+
_ = os.Remove(grubCfgFuture)
713637

714-
grubCfgContents, err := os.ReadFile(grubCfgCurrent)
715-
if err != nil {
716-
PrintVerboseErr("ABSystem.RunOperation", 11.2, err)
717-
}
638+
const preamble string = `set timeout=5
718639
719-
var replacerPairs []string
720-
if grub.FutureRoot == "a" {
721-
replacerPairs = []string{
722-
"default=1", "default=0",
723-
"Previous State (A)", "Current State (A)",
724-
"Current State (B)", "Previous State (B)",
725-
}
726-
} else {
727-
replacerPairs = []string{
728-
"default=0", "default=1",
729-
"Current State (A)", "Previous State (A)",
730-
"Previous State (B)", "Current State (B)",
731-
}
732-
}
640+
# Load video support
641+
insmod gfxterm
642+
insmod all_video
643+
644+
# Set graphics mode
645+
set gfxmode=1024x768,auto
646+
terminal_output gfxterm
647+
648+
`
649+
650+
const state_a string = `
651+
set default=0
652+
653+
menuentry "Current State (A)" --class abroot-a {
654+
configfile "/abroot/%s/abroot.cfg"
655+
}
733656
734-
replacer := strings.NewReplacer(replacerPairs...)
735-
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0o644)
657+
menuentry "Previous State (B)" --class abroot-b {
658+
configfile "/abroot/%s/abroot.cfg"
659+
}
660+
`
661+
662+
const state_b string = `
663+
set default=1
664+
menuentry "Previous State (A)" --class abroot-a {
665+
configfile "/abroot/%s/abroot.cfg"
666+
}
667+
668+
menuentry "Current State (B)" --class abroot-b {
669+
configfile "/abroot/%s/abroot.cfg"
670+
}
671+
`
672+
673+
configFile := ""
674+
675+
if grub.FutureRoot == "a" {
676+
configFile = preamble + fmt.Sprintf(state_a, partFuture.Label, partPresent.Label)
677+
} else {
678+
configFile = preamble + fmt.Sprintf(state_b, partFuture.Label, partPresent.Label)
736679
}
737680

681+
os.WriteFile(grubCfgFuture, []byte(configFile), 0o644)
682+
738683
err = AtomicSwap(grubCfgCurrent, grubCfgFuture)
739684
if err != nil {
740685
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)