Skip to content

Commit

Permalink
Feat: [close #225] Add EtcBuilder
Browse files Browse the repository at this point in the history
Replace the custom Etc merge functions with EtcBuilder
  • Loading branch information
axtloss authored and mirkobrombin committed Mar 15, 2024
1 parent 621a2a3 commit e026c64
Show file tree
Hide file tree
Showing 21 changed files with 3,346 additions and 130 deletions.
112 changes: 4 additions & 108 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/google/uuid"
EtcBuilder "github.com/linux-immutability-tools/EtcBuilder/cmd"
"github.com/vanilla-os/abroot/settings"
)

Expand Down Expand Up @@ -152,92 +153,6 @@ func (s *ABSystem) CheckUpdate() (string, bool) {
return s.Registry.HasUpdate(s.CurImage.Digest)
}

// MergeUserEtcFiles merges user-related files from the new lower etc (/.system/etc)
// with the old upper etc, if present, saving the result in the new upper etc.
func (s *ABSystem) MergeUserEtcFiles(oldUpperEtc, newLowerEtc, newUpperEtc string) error {
PrintVerboseInfo("ABSystem.SyncLowerEtc", "syncing /.system/etc ->", newLowerEtc)

etcFiles := []string{
"passwd",
"group",
"shells",
"shadow",
"subuid",
"subgid",
}

etcDir := "/.system/etc"
if _, err := os.Stat(etcDir); os.IsNotExist(err) {
PrintVerboseErr("ABSystem.SyncLowerEtc", 0, err)
return err
}

for _, file := range etcFiles {
// Use file present in the immutable /etc if it exists. Otherwise, use the immutable one.
_, err := os.Stat(oldUpperEtc + "/" + file)
if err != nil {
if os.IsNotExist(err) { // No changes were made to the file from its image base, skip merge
continue
} else {
PrintVerboseErr("ABSystem.SyncLowerEtc", 1, err)
return err
}
} else {
firstFile := oldUpperEtc + "/" + file
secondFile := newLowerEtc + "/" + file
destination := newUpperEtc + "/" + file

// write the diff to the destination
err = MergeDiff(firstFile, secondFile, destination)
if err != nil {
PrintVerboseErr("ABSystem.SyncLowerEtc", 2, err)
return err
}
}
}

PrintVerboseInfo("ABSystem.SyncLowerEtc", "sync completed")
return nil
}

// SyncUpperEtc syncs the mutable etc directories from /var/lib/abroot/etc
func (s *ABSystem) SyncUpperEtc(newEtc string) error {
PrintVerboseInfo("ABSystem.SyncUpperEtc", "Starting")

current_part, err := s.RootM.GetPresent()
if err != nil {
PrintVerboseErr("ABSystem.SyncUpperEtc", 0, err)
return err
}

etcDir := fmt.Sprintf("/var/lib/abroot/etc/%s", current_part.Label)
if _, err := os.Stat(etcDir); os.IsNotExist(err) {
PrintVerboseErr("ABSystem.SyncEtc", 1, err)
return err
}

PrintVerboseInfo("ABSystem.SyncUpperEtc: syncing /var/lib/abroot/etc/%s -> %s", current_part.Label, newEtc)

opts := []string{
"--exclude=passwd",
"--exclude=group",
"--exclude=shells",
"--exclude=shadow",
"--exclude=subuid",
"--exclude=subgid",
"--exclude=fstab",
"--exclude=crypttab",
}
err = rsyncCmd(etcDir+"/", newEtc, opts, false)
if err != nil {
PrintVerboseErr("ABSystem.SyncUpperEtc", 2, err)
return err
}

PrintVerboseInfo("ABSystem.SyncUpperEtc", "sync completed")
return nil
}

// RunCleanUpQueue runs the functions in the queue or only the specified one
func (s *ABSystem) RunCleanUpQueue(fnName string) error {
PrintVerboseInfo("ABSystem.RunCleanUpQueue", "running...")
Expand Down Expand Up @@ -937,6 +852,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
// ------------------------------------------------
PrintVerboseSimple("[Stage 8] -------- ABSystemRunOperation")

oldEtc := "/.system/sysconf" // The current etc WITHOUT anything overlayed
presentEtc, err := s.RootM.GetPresent()
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 8, err)
Expand All @@ -950,29 +866,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
oldUpperEtc := fmt.Sprintf("/var/lib/abroot/etc/%s", presentEtc.Label)
newUpperEtc := fmt.Sprintf("/var/lib/abroot/etc/%s", futureEtc.Label)

// Clean new upper etc to prevent deleted files from persisting
err = os.RemoveAll(newUpperEtc)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 8.2, err)
return err
}
err = os.Mkdir(newUpperEtc, 0755)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 8.3, err)
return err
}

err = s.MergeUserEtcFiles(oldUpperEtc, filepath.Join(systemNew, "/etc"), newUpperEtc)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 8.4, err)
return err
}

s.RunCleanUpQueue("clearUnstagedPackages")

err = s.SyncUpperEtc(newUpperEtc)
err = EtcBuilder.ExtBuildCommand(oldEtc, systemNew+"/sysconf", oldUpperEtc, newUpperEtc)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 8.6, err)
PrintVerboseErr("AbSystem.RunOperation", 8.2, err)
return err
}

Expand Down
22 changes: 0 additions & 22 deletions docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@
* [func (s *ABSystem) GenerateFstab(rootPath string, root ABRootPartition) error](#ABSystem.GenerateFstab)
* [func (s *ABSystem) GenerateSystemdUnits(rootPath string, root ABRootPartition) error](#ABSystem.GenerateSystemdUnits)
* [func (s *ABSystem) LockUpgrade() error](#ABSystem.LockUpgrade)
* [func (s *ABSystem) MergeUserEtcFiles(oldUpperEtc, newLowerEtc, newUpperEtc string) error](#ABSystem.MergeUserEtcFiles)
* [func (s *ABSystem) RemoveFromCleanUpQueue(name string)](#ABSystem.RemoveFromCleanUpQueue)
* [func (s *ABSystem) RemoveStageFile() error](#ABSystem.RemoveStageFile)
* [func (s *ABSystem) ResetQueue()](#ABSystem.ResetQueue)
* [func (s *ABSystem) Rollback() (response ABRollbackResponse, err error)](#ABSystem.Rollback)
* [func (s *ABSystem) RunCleanUpQueue(fnName string) error](#ABSystem.RunCleanUpQueue)
* [func (s *ABSystem) RunOperation(operation ABSystemOperation) error](#ABSystem.RunOperation)
* [func (s *ABSystem) SyncUpperEtc(newEtc string) error](#ABSystem.SyncUpperEtc)
* [func (s *ABSystem) UnlockUpgrade() error](#ABSystem.UnlockUpgrade)
* [func (s *ABSystem) UpgradeLockExists() bool](#ABSystem.UpgradeLockExists)
* [func (s *ABSystem) UserLockRequested() bool](#ABSystem.UserLockRequested)
Expand Down Expand Up @@ -837,16 +835,6 @@ LockUpgrade creates a lock file, preventing upgrades from proceeding



### <a name="ABSystem.MergeUserEtcFiles">func</a> (\*ABSystem) [MergeUserEtcFiles](/src/target/system.go?s=4344:4432#L149)
``` go
func (s *ABSystem) MergeUserEtcFiles(oldUpperEtc, newLowerEtc, newUpperEtc string) error
```
MergeUserEtcFiles merges user-related files from the new lower etc (/.system/etc)
with the old upper etc, if present, saving the result in the new upper etc.




### <a name="ABSystem.RemoveFromCleanUpQueue">func</a> (\*ABSystem) [RemoveFromCleanUpQueue](/src/target/system.go?s=9681:9735#L332)
``` go
func (s *ABSystem) RemoveFromCleanUpQueue(name string)
Expand Down Expand Up @@ -911,16 +899,6 @@ RunOperation executes a root-switching operation from the options below:




### <a name="ABSystem.SyncUpperEtc">func</a> (\*ABSystem) [SyncUpperEtc](/src/target/system.go?s=5629:5681#L196)
``` go
func (s *ABSystem) SyncUpperEtc(newEtc string) error
```
SyncUpperEtc syncs the mutable etc directories from /var/lib/abroot/etc




### <a name="ABSystem.UnlockUpgrade">func</a> (\*ABSystem) [UnlockUpgrade](/src/target/system.go?s=33993:34033#L1175)
``` go
func (s *ABSystem) UnlockUpgrade() error
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/letsencrypt/boulder v0.0.0-20240104140712-c1f7de06e9f8 // indirect
github.com/linux-immutability-tools/EtcBuilder v0.0.0-20240221201646-c038f3d3418d // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/letsencrypt/boulder v0.0.0-20240104140712-c1f7de06e9f8 h1:VhZp18bDTiwcMExEJnuajfgtjMx03bh/NH7qIplYfdc=
github.com/letsencrypt/boulder v0.0.0-20240104140712-c1f7de06e9f8/go.mod h1:rS3/odUsNpJzEb3gCFNAL32rFXDS4kLeS28vd2x8NfQ=
github.com/linux-immutability-tools/EtcBuilder v0.0.0-20240221201646-c038f3d3418d h1:i0z+Mrudwrnup7/0tBrJhYE2eKIfcjfVPmmMoUfUrkg=
github.com/linux-immutability-tools/EtcBuilder v0.0.0-20240221201646-c038f3d3418d/go.mod h1:zOvDqvS9ojo8mr6dMYsk8L8c6aY9S785WgVIzgGwm8c=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
Loading

0 comments on commit e026c64

Please sign in to comment.