Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid changing roots in grub if running abroot on previous partition #181

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/disk-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 18 additions & 4 deletions core/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type ABRootManager struct {

// ABRootPartition represents an ABRoot partition
type ABRootPartition struct {
Label string // a,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
Expand Down
14 changes: 10 additions & 4 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)",
Expand All @@ -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
}
}
Expand Down
Loading