Skip to content

Commit

Permalink
remove generation of systemd mount information
Browse files Browse the repository at this point in the history
Since abroot now handles mounting before systemd, this is not required anymore.
  • Loading branch information
taukakao authored and mirkobrombin committed Jul 12, 2024
1 parent 24c1c14 commit eeaad76
Showing 1 changed file with 14 additions and 198 deletions.
212 changes: 14 additions & 198 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ var (
lockFile string = filepath.Join("/tmp", "ABSystem.Upgrade.lock")
stageFile string = filepath.Join("/tmp", "ABSystem.Upgrade.stage")
userLockFile string = filepath.Join("/tmp", "ABSystem.Upgrade.user.lock")
mountUnitDir = "/etc/systemd/system"

// Errors
ErrNoUpdate error = errors.New("no update available")
Expand Down Expand Up @@ -135,166 +134,6 @@ func (s *ABSystem) CheckUpdate() (string, bool) {
return s.Registry.HasUpdate(s.CurImage.Digest)
}

// GenerateFstab generates a fstab file for the future root
func (s *ABSystem) GenerateFstab(rootPath string, root ABRootPartition) error {
PrintVerboseInfo("ABSystem.GenerateFstab", "generating fstab")

template := `# /etc/fstab: static file system information.
# Generated by ABRoot
#
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=%s / %s defaults 0 0
/.system/usr /.system/usr none bind,ro
%s /var auto defaults 0 0
`
varSource := ""
if s.RootM.VarPartition.IsDevMapper() {
varSource = fmt.Sprintf("/dev/mapper/%s", s.RootM.VarPartition.Device)
} else {
varSource = fmt.Sprintf("UUID=%s", s.RootM.VarPartition.Uuid)
}

fstab := fmt.Sprintf(
template,
root.Partition.Uuid,
root.Partition.FsType,
varSource,
)

err := os.WriteFile(filepath.Join(rootPath, "/etc/fstab"), []byte(fstab), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateFstab", 0, err)
return err
}

PrintVerboseInfo("ABSystem.GenerateFstab", "fstab generated")
return nil
}

// GenerateCrypttab identifies which devices are encrypted and generates
// the /etc/crypttab file for the specified root
func (s *ABSystem) GenerateCrypttab(rootPath string) error {
PrintVerboseInfo("ABSystem.GenerateCrypttab", "generating crypttab")

cryptEntries := [][]string{}

// Check for encrypted roots
for _, rootDevice := range s.RootM.Partitions {
if strings.HasPrefix(rootDevice.Partition.Device, "luks-") {
parent := rootDevice.Partition.Parent
PrintVerboseInfo("ABSystem.GenerateCrypttab", "Adding", parent.Device, "to crypttab")

cryptEntries = append(cryptEntries, []string{
fmt.Sprintf("luks-%s", parent.Uuid),
fmt.Sprintf("UUID=%s", parent.Uuid),
"none",
"luks,discard",
})
}
}

// Check for encrypted /var
if strings.HasPrefix(s.RootM.VarPartition.Device, "luks-") {
parent := s.RootM.VarPartition.Parent
PrintVerboseInfo("ABSystem.GenerateCrypttab", "Adding", parent.Device, "to crypttab")

cryptEntries = append(cryptEntries, []string{
fmt.Sprintf("luks-%s", parent.Uuid),
fmt.Sprintf("UUID=%s", parent.Uuid),
"none",
"luks,discard",
})
}

crypttabContent := ""
for _, entry := range cryptEntries {
fmtEntry := strings.Join(entry, " ")
crypttabContent += fmtEntry + "\n"
}

err := os.WriteFile(rootPath+"/etc/crypttab", []byte(crypttabContent), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateCrypttab", 3, err)
return err
}

return nil
}

// GenerateSystemdUnits generates systemd units that mount the mutable parts
// of the system to their respective mountpoints
func (s *ABSystem) GenerateSystemdUnits(rootPath string, root ABRootPartition) error {
PrintVerboseInfo("ABSystem.GenerateSystemdUnits", "generating units")

extraDepends := ""
if root.Partition.IsEncrypted() || s.RootM.VarPartition.IsEncrypted() {
extraDepends = "cryptsetup.target"
}

type varmount struct {
source string
destination string
fsType string
options string
}

mounts := []varmount{
{"/var/home", "/home", "none", "bind"},
{"/var/opt", "/opt", "none", "bind"},
{"overlay", "/.system/etc", "overlay", "lowerdir=/.system/etc,upperdir=/var/lib/abroot/etc/" + root.Label + ",workdir=/var/lib/abroot/etc/" + root.Label + "-work"},
}

afterVarTemplate := `[Unit]
Description=Mounts %s from var
After=local-fs-pre.target %s
Before=local-fs.target nss-user-lookup.target
RequiresMountsFor=/var
[Mount]
What=%s
Where=%s
Type=%s
Options=%s
`

for _, mount := range mounts {
PrintVerboseInfo("ABSystem.GenerateSystemdUnits", "generating unit for", mount.destination)

unit := fmt.Sprintf(afterVarTemplate, mount.destination, extraDepends, mount.source, mount.destination, mount.fsType, mount.options)

// the unit file needs to have the escaped mount point as its name
out, err := exec.Command("systemd-escape", "--path", mount.destination).Output()
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 0, "failed to determine escaped path", err)
return err
}
mountUnitFile := "/" + strings.ReplaceAll(string(out), "\n", "") + ".mount"

err = os.WriteFile(filepath.Join(rootPath, mountUnitDir, mountUnitFile), []byte(unit), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 1, err)
return err
}

const targetRequires string = "/local-fs-pre.target.requires"

err = os.MkdirAll(filepath.Join(rootPath, mountUnitDir, targetRequires), 0o755)
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 2, err)
return err
}

err = os.Symlink(filepath.Join("../", mountUnitFile), filepath.Join(rootPath, mountUnitDir, targetRequires, mountUnitFile))
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 3, "failed to create symlink", err)
return err
}
}

PrintVerboseInfo("ABSystem.GenerateSystemdUnits", "units generated")
return nil
}

func (s *ABSystem) CreateRootSymlinks(systemNewPath string) error {
PrintVerboseInfo("ABSystem.CreateRootSymlinks", "creating symlinks")
links := []string{"mnt", "proc", "run", "dev", "media", "root", "sys", "tmp", "var"}
Expand Down Expand Up @@ -602,29 +441,6 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return err
}

// Stage 6: Generate /etc/fstab, /etc/crypttab, and the
// SystemD units to setup mountpoints
// ------------------------------------------------
PrintVerboseSimple("[Stage 6] -------- ABSystemRunOperation")

err = s.GenerateFstab(systemNew, partFuture)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 6, err)
return err
}

err = s.GenerateCrypttab(systemNew)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 6.1, err)
return err
}

err = s.GenerateSystemdUnits(systemNew, partFuture)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 6.2, "Failed to Generate SystemdUnits", err)
return err
}

// Stage (dry): If dry-run, exit here before writing to disk
// ------------------------------------------------
switch operation {
Expand All @@ -633,9 +449,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return nil
}

// Stage 7: Update the bootloader
// Stage 6: Update the bootloader
// ------------------------------------------------
PrintVerboseSimple("[Stage 7] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 6] -------- ABSystemRunOperation")

partPresent, err := s.RootM.GetPresent()
if err != nil {
Expand Down Expand Up @@ -762,9 +578,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return err
}

// Stage 8: Sync /etc
// Stage 7: Sync /etc
// ------------------------------------------------
PrintVerboseSimple("[Stage 8] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 7] -------- ABSystemRunOperation")

oldEtc := "/.system/sysconf" // The current etc WITHOUT anything overlayed
presentEtc, err := s.RootM.GetPresent()
Expand All @@ -786,9 +602,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return err
}

// Stage 9: Mount boot partition
// Stage 8: Mount boot partition
// ------------------------------------------------
PrintVerboseSimple("[Stage 9] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 8] -------- ABSystemRunOperation")

uuid := uuid.New().String()
tmpBootMount := filepath.Join("/tmp", uuid)
Expand All @@ -808,9 +624,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return partBoot.Unmount()
}, nil, 100, &goodies.NoErrorHandler{}, false)

// Stage 10: Atomic swap the rootfs and abimage.abr
// Stage 9: Atomic swap the rootfs and abimage.abr
// ------------------------------------------------
PrintVerboseSimple("[Stage 10] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 9] -------- ABSystemRunOperation")

err = AtomicSwap(systemOld, systemNew)
if err != nil {
Expand Down Expand Up @@ -842,9 +658,9 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
return os.RemoveAll(newABImage)
}, nil, 30, &goodies.NoErrorHandler{}, false)

// Stage 11: Atomic swap the bootloader
// Stage 10: Atomic swap the bootloader
// ------------------------------------------------
PrintVerboseSimple("[Stage 11] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 10] -------- ABSystemRunOperation")

grub, err := NewGrub(partBoot)
if err != nil {
Expand All @@ -862,7 +678,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
grubCfgCurrent := filepath.Join(tmpBootMount, "grub/grub.cfg")
grubCfgFuture := filepath.Join(tmpBootMount, "grub/grub.cfg.future")

// Just like in Stage 10, tmpBootMount/grub/grub.cfg.future may not exist.
// Just like in Stage 9, tmpBootMount/grub/grub.cfg.future may not exist.
if _, err = os.Stat(grubCfgFuture); os.IsNotExist(err) {
PrintVerboseInfo("ABSystem.RunOperation", "Creating grub.cfg.future")

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

// Stage 12: Cleanup old kernel images
// Stage 11: Cleanup old kernel images
// ------------------------------------------------
// If Thin-Provisioning set, we have to remove the old kernel images
// from the init partition since it is too small to hold multiple kernels.
Expand All @@ -907,7 +723,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
switch operation {
case DRY_RUN_UPGRADE, DRY_RUN_APPLY, DRY_RUN_INITRAMFS:
default:
PrintVerboseSimple("[Stage 12] -------- ABSystemRunOperation")
PrintVerboseSimple("[Stage 11] -------- ABSystemRunOperation")

// since we did the swap, the init partition is now mounted in
// .system instead of .system.new, so we need to update the path
Expand Down Expand Up @@ -997,7 +813,7 @@ func (s *ABSystem) Rollback(checkOnly bool) (response ABRollbackResponse, err er
grubCfgCurrent := filepath.Join(tmpBootMount, "grub/grub.cfg")
grubCfgFuture := filepath.Join(tmpBootMount, "grub/grub.cfg.future")

// Just like in Stage 10, tmpBootMount/grub/grub.cfg.future may not exist.
// Just like in Stage 9, tmpBootMount/grub/grub.cfg.future may not exist.
if _, err = os.Stat(grubCfgFuture); os.IsNotExist(err) {
PrintVerboseInfo("ABSystem.Rollback", "Creating grub.cfg.future")

Expand Down

0 comments on commit eeaad76

Please sign in to comment.