Skip to content

Commit

Permalink
feat[close #183]: Implement a update-initramfs command
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Jan 1, 2024
1 parent 6652eac commit d6b09fd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 13 deletions.
74 changes: 74 additions & 0 deletions cmd/update-initramfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

/* License: GPLv3
Authors:
Mirko Brombin <mirko@fabricators.ltd>
Vanilla OS Contributors <https://github.com/vanilla-os/>
Copyright: 2023
Description:
ABRoot is utility which provides full immutability and
atomicity to a Linux system, by transacting between
two root filesystems. Updates are performed using OCI
images, to ensure that the system is always in a
consistent state.
*/

import (
"github.com/spf13/cobra"

"github.com/vanilla-os/abroot/core"
"github.com/vanilla-os/orchid/cmdr"
)

func NewUpdateInitfsCommand() *cmdr.Command {
cmd := cmdr.NewCommand(
"update-initramfs",
abroot.Trans("updateInitramfs.long"),
abroot.Trans("updateInitramfs.short"),
updateInitramfs,
)

cmd.WithBoolFlag(
cmdr.NewBoolFlag(
"dry-run",
"d",
abroot.Trans("updateInitramfs.dryRunFlag"),
false))

cmd.Example = "abroot update-initramfs"

return cmd
}

func updateInitramfs(cmd *cobra.Command, args []string) error {
if !core.RootCheck(false) {
cmdr.Error.Println(abroot.Trans("updateInitramfs.rootRequired"))
return nil
}

dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
cmdr.Error.Println(err)
return err
}

aBsys, err := core.NewABSystem()
if err != nil {
cmdr.Error.Println(err)
return err
}

if dryRun {
err = aBsys.RunOperation(core.DRY_RUN_INITRAMFS)
} else {
err = aBsys.RunOperation(core.INITRAMFS)
}
if err != nil {
cmdr.Error.Printf(abroot.Trans("updateInitramfs.updateFailed"), err)
return err
}

cmdr.Info.Println(abroot.Trans("updateInitramfs.updateSuccess"))

return nil
}
34 changes: 25 additions & 9 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ type QueuedFunction struct {
}

const (
UPGRADE = "upgrade"
FORCE_UPGRADE = "force-upgrade"
DRY_RUN_UPGRADE = "dry-run-upgrade"
APPLY = "package-apply"
DRY_RUN_APPLY = "dry-run-package-apply"
UPGRADE = "upgrade"
FORCE_UPGRADE = "force-upgrade"
DRY_RUN_UPGRADE = "dry-run-upgrade"
APPLY = "package-apply"
DRY_RUN_APPLY = "dry-run-package-apply"
INITRAMFS = "initramfs"
DRY_RUN_INITRAMFS = "dry-run-initramfs"
)

const (
Expand Down Expand Up @@ -471,6 +473,7 @@ Options=%s
// UPGRADE: Upgrades to a new image, if available,
// FORCE_UPGRADE: Forces the upgrade operation, even if no new image is available,
// APPLY: Applies package changes, but doesn't update the system.
// INITRAMFS: Updates the initramfs for the future root, but doesn't update the system.
func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
PrintVerbose("ABSystem.RunOperation: starting %s", operation)

Expand Down Expand Up @@ -518,7 +521,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
}

var imageDigest string
if operation != APPLY {
if operation != APPLY && operation != INITRAMFS {
var res bool
imageDigest, res = s.CheckUpdate()
if !res {
Expand Down Expand Up @@ -609,7 +612,8 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
content := `RUN ` + pkgsFinal

var imageName string
if operation == APPLY || operation == DRY_RUN_APPLY {
switch operation {
case APPLY, DRY_RUN_APPLY, INITRAMFS, DRY_RUN_INITRAMFS:
presentPartition, err := s.RootM.GetPresent()
if err != nil {
PrintVerbose("ABSystemRunOperation:err(3.2): %s", err)
Expand All @@ -624,7 +628,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
if imageName == "" {
imageName = settings.Cnf.FullImageName
}
} else {
default:
imageName = settings.Cnf.FullImageName + "@" + imageDigest
labels["ABRoot.BaseImageDigest"] = imageDigest
}
Expand Down Expand Up @@ -734,7 +738,8 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {

// Stage (dry): If dry-run, exit here before writing to disk
// ------------------------------------------------
if operation == DRY_RUN_UPGRADE || operation == DRY_RUN_APPLY {
switch operation {
case DRY_RUN_UPGRADE, DRY_RUN_APPLY, DRY_RUN_INITRAMFS:
PrintVerbose("ABSystem.RunOperation: dry-run completed")
return nil
}
Expand Down Expand Up @@ -773,6 +778,17 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return err
}

err = chroot.ExecuteCmds( // ensure initramfs is updated
[]string{
"update-initramfs -u",
"exit",
},
)
if err != nil {
PrintVerbose("ABSystem.RunOperation:err(7.1.1): %s", err)
return err
}

s.RunCleanUpQueue("closeChroot")

var rootUuid string
Expand Down
16 changes: 12 additions & 4 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ status:

upgrade:
use: "upgrade"
long: "Update the boot partition for maintenance purposes (for advanced users only)"
short: "Update the boot partition"
forceFlag: "force update the boot partition without asking for confirmation"
long: "Check for a new system image and apply it."
short: "Upgrade the system"
forceFlag: "force update even if the system is up to date"
rootRequired: "You must be root to run this command."
checkingSystemUpdate: "Checking for system updates..."
checkingPackageUpdate: "Checking for package updates..."
Expand All @@ -83,4 +83,12 @@ upgrade:
added: "Added"
upgraded: "Upgraded"
downgraded: "Downgraded"
removed: "Removed"
removed: "Removed"

updateInitramfs:
use: "update-initramfs"
long: "Update the initramfs of the future root."
short: "Update the initramfs"
rootRequired: "You must be root to run this command."
updateSuccess: "Updated initramfs of future root."
updateFailed: "Failed to update initramfs of future root.\n"
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func main() {
status := cmd.NewStatusCommand()
root.AddCommand(status)

updateInitramfs := cmd.NewUpdateInitfsCommand()
root.AddCommand(updateInitramfs)

// run the app
err := abroot.Run()
if err != nil {
Expand Down

0 comments on commit d6b09fd

Please sign in to comment.