From fea18cb421e90da106c384fab809394b0c9ca752 Mon Sep 17 00:00:00 2001 From: Mateus Melchiades Date: Sun, 24 Dec 2023 16:15:47 +0000 Subject: [PATCH 1/2] Avoid changing roots in grub if running abroot on previous partition --- core/grub.go | 22 ++++++++++++++++++---- core/root.go | 2 +- core/system.go | 14 ++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/grub.go b/core/grub.go index a4a4e9e1..8a045c5f 100644 --- a/core/grub.go +++ b/core/grub.go @@ -24,8 +24,8 @@ import ( ) type Grub struct { - presentRoot string - futureRoot string + PresentRoot string + FutureRoot string } // generateABGrubConf generates a new grub config with the given details @@ -160,7 +160,21 @@ func NewGrub(bootPart Partition) (*Grub, error) { } return &Grub{ - presentRoot: presentRoot, - futureRoot: futureRoot, + PresentRoot: presentRoot, + FutureRoot: futureRoot, }, nil } + +func (g *Grub) IsBootedIntoPresentRoot() (bool, error) { + a := NewABRootManager() + future, err := a.GetFuture() + if err != nil { + return false, err + } + + if g.FutureRoot == "a" { + return future.Label == settings.Cnf.PartLabelA, nil + } else { + return future.Label == settings.Cnf.PartLabelB, nil + } +} diff --git a/core/root.go b/core/root.go index 5c84ec0e..96386242 100644 --- a/core/root.go +++ b/core/root.go @@ -27,7 +27,7 @@ type ABRootManager struct { // ABRootPartition represents an ABRoot partition type ABRootPartition struct { - Label string // a,b + Label string // vos-a, vos-b IdentifiedAs string // present,future Partition Partition MountPoint string diff --git a/core/system.go b/core/system.go index 5b8e73a5..79198ebc 100644 --- a/core/system.go +++ b/core/system.go @@ -908,7 +908,13 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error { return err } - if grub.futureRoot != partFuture.Label { + // Only swap grub entries if we're booted into the present partition + isPresent, err := grub.IsBootedIntoPresentRoot() + if err != nil { + PrintVerbose("ABSystem.RunOperation:err(11.1): %s", err) + return err + } + if isPresent { grubCfgCurrent := filepath.Join(tmpBootMount, "grub/grub.cfg") grubCfgFuture := filepath.Join(tmpBootMount, "grub/grub.cfg.future") @@ -918,11 +924,11 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error { grubCfgContents, err := os.ReadFile(grubCfgCurrent) if err != nil { - PrintVerbose("ABSystem.RunOperation:err(11.1): %s", err) + PrintVerbose("ABSystem.RunOperation:err(11.2): %s", err) } var replacerPairs []string - if grub.futureRoot == "a" { + if grub.FutureRoot == "a" { replacerPairs = []string{ "default=1", "default=0", "A (previous)", "A (current)", @@ -942,7 +948,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error { err = AtomicSwap(grubCfgCurrent, grubCfgFuture) if err != nil { - PrintVerbose("ABSystem.RunOperation:err(11.2): %s", err) + PrintVerbose("ABSystem.RunOperation:err(11.3): %s", err) return err } } From cd072fb2b09d110cb5be09884ada5aefe519480e Mon Sep 17 00:00:00 2001 From: Mateus Melchiades Date: Sun, 24 Dec 2023 17:38:21 +0000 Subject: [PATCH 2/2] Fix some stirngs --- core/disk-manager.go | 4 ++-- core/root.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/disk-manager.go b/core/disk-manager.go index a073783b..82e0b5ac 100644 --- a/core/disk-manager.go +++ b/core/disk-manager.go @@ -39,9 +39,9 @@ type Partition struct { Device string // If the partition is LUKS-encrypted or an LVM volume, the logical volume - // opened in /dev/mapper will be a child of the physical partiiton in /dev. + // opened in /dev/mapper will be a child of the physical partition in /dev. // Otherwise, the partition will be a direct child of the block device, and - // its Parent will be nil. + // Parent will be nil. // // The same logic applies for encrypted LVM volumes. When this is the case, // the filesystem hirearchy is as follows: diff --git a/core/root.go b/core/root.go index 96386242..0630507f 100644 --- a/core/root.go +++ b/core/root.go @@ -27,8 +27,8 @@ type ABRootManager struct { // ABRootPartition represents an ABRoot partition type ABRootPartition struct { - Label string // vos-a, vos-b - IdentifiedAs string // present,future + Label string // Matches `partLabelA` and `partLabelB` settings entries + IdentifiedAs string // Either `present` or `future` Partition Partition MountPoint string MountOptions string