Skip to content

Commit

Permalink
Add elemental-setup services to elemental-rootfs
Browse files Browse the repository at this point in the history
The elemental-setup and rootfs features are heavily dependent on each
other.

This commit copies the elemental-setup feature into elemental-rootfs,
and changes the mount-command to actually start the
elemental-setup-rootfs service during mount to not run the yip rootfs
stage twice.

Signed-off-by: Fredrik Lönnegren <fredrik.lonnegren@suse.com>
  • Loading branch information
frelon committed Oct 11, 2023
1 parent 4bc02ba commit 66bd2a8
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GO_VERSION=1.20
ARG GO_VERSION=1.21
ARG LEAP_VERSION=15.5

FROM golang:${GO_VERSION}-alpine as elemental-bin
Expand Down
2 changes: 1 addition & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ mount:
read-kernel-cmdline: true # read and parse /proc/cmdline for arguments
sysroot: /sysroot # Path to mount system to
write-fstab: true # Write fstab into sysroot/etc/fstab
run-cloud-init: true # Run rootfs stage
run-rootfs-service: true # Run rootfs stage
run-fsck: true # Run fsck on all partitions before mounting
overlay:
type: tmpfs # tmpfs|block
Expand Down
2 changes: 1 addition & 1 deletion examples/green/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ RUN systemctl enable NetworkManager.service
RUN cp /usr/share/systemd/tmp.mount /etc/systemd/system

# Generate initrd with required elemental services
RUN elemental init -f elemental-rootfs,grub-config,grub-default-bootargs,elemental-setup,dracut-config,cloud-config-defaults,cloud-config-essentials && \
RUN elemental init -f elemental-rootfs,grub-config,grub-default-bootargs,dracut-config,cloud-config-defaults,cloud-config-essentials && \
kernel=$(ls /boot/Image-* | head -n1) && \
if [ -e "$kernel" ]; then ln -sf "${kernel#/boot/}" /boot/vmlinuz; fi && \
rm -rf /var/log/update* && \
Expand Down
2 changes: 1 addition & 1 deletion examples/tumbleweed/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ RUN systemctl enable NetworkManager.service
RUN echo "PermitRootLogin yes" > /etc/ssh/sshd_config.d/rootlogin.conf

# Generate initrd with required elemental services
RUN elemental init -f elemental-rootfs,grub-config,grub-default-bootargs,elemental-setup,dracut-config,cloud-config-defaults,cloud-config-essentials && \
RUN elemental init -f elemental-rootfs,grub-config,grub-default-bootargs,dracut-config,cloud-config-defaults,cloud-config-essentials && \
kernel=$(ls /boot/Image-* | head -n1) && \
if [ -e "$kernel" ]; then ln -sf "${kernel#/boot/}" /boot/vmlinuz; fi && \
rm -rf /var/log/update* && \
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rancher/elemental-toolkit

go 1.20
go 1.21

require (
github.com/canonical/go-efilib v0.3.1-0.20220324150059-04e254148b45
Expand Down
9 changes: 5 additions & 4 deletions pkg/action/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/rancher/elemental-toolkit/pkg/constants"
"github.com/rancher/elemental-toolkit/pkg/elemental"
"github.com/rancher/elemental-toolkit/pkg/systemd"
v1 "github.com/rancher/elemental-toolkit/pkg/types/v1"
"github.com/rancher/elemental-toolkit/pkg/utils"
)
Expand Down Expand Up @@ -52,15 +53,15 @@ func RunMount(cfg *v1.RunConfig, spec *v1.MountSpec) error {
return err
}

if spec.RunCloudInit {
cfg.Logger.Debug("Running rootfs cloud-init stage")
err := utils.RunStage(&cfg.Config, "rootfs", cfg.Strict, cfg.CloudInitPaths...)
if spec.RunRootfsService {
cfg.Logger.Debug("Running elemental-setup-rootfs service")
err := systemd.Start(&cfg.Config.Runner, systemd.NewUnit("elemental-setup-rootfs"))

Check failure on line 58 in pkg/action/mount.go

View workflow job for this annotation

GitHub Actions / build

cannot use &cfg.Config.Runner (value of type *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner) as "github.com/rancher/elemental-toolkit/pkg/types/v1".Runner value in argument to systemd.Start: *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner does not implement "github.com/rancher/elemental-toolkit/pkg/types/v1".Runner (type *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner is pointer to interface, not interface) (typecheck)

Check failure on line 58 in pkg/action/mount.go

View workflow job for this annotation

GitHub Actions / build

cannot use &cfg.Config.Runner (value of type *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner) as "github.com/rancher/elemental-toolkit/pkg/types/v1".Runner value in argument to systemd.Start: *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner does not implement "github.com/rancher/elemental-toolkit/pkg/types/v1".Runner (type *"github.com/rancher/elemental-toolkit/pkg/types/v1".Runner is pointer to interface, not interface)) (typecheck)
if err != nil {
cfg.Logger.Errorf("Error running rootfs stage: %s", err.Error())
return err
}
} else {
cfg.Logger.Debug("Skipping cloud-init rootfs stage")
cfg.Logger.Debug("Skipping elemental-setup-rootfs")
}

if err := applyLayoutConfig(cfg, spec); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func NewMountSpec() *v1.MountSpec {
ReadKernelCmdline: true,
Sysroot: "/sysroot",
WriteFstab: true,
RunCloudInit: true,
RunRootfsService: true,
RunFsck: true,
Image: &v1.Image{
FS: constants.LinuxImgFs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
install_items+=" /usr/lib/systemd/system/elemental-setup-initramfs.service /etc/systemd/system/initrd.target.requires/elemental-setup-initramfs.service "
install_items+=" /usr/lib/systemd/system/elemental-setup-rootfs.service /etc/systemd/system/initrd-fs.target.requires/elemental-setup-rootfs.service "
install_items+=" /etc/hosts "
add_dracutmodules+=" network "
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Elemental system configuration
Before=getty.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/elemental run-stage boot

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Elemental system after FS setup
DefaultDependencies=no
After=local-fs.target
Wants=local-fs.target
Before=sysinit.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/elemental run-stage fs

[Install]
WantedBy=sysinit.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Elemental system initramfs setup before switch root
DefaultDependencies=no
After=initrd-fs.target
Requires=initrd-fs.target
Before=initrd.target

[Service]
RootDirectory=/sysroot
BindPaths=/proc /sys /dev /run /tmp
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/elemental run-stage initramfs

[Install]
RequiredBy=initrd.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Elemental setup after network
After=network-online.target
Wants=network-online.target

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Type=oneshot
ExecStart=/usr/bin/elemental run-stage network
TimeoutStopSec=180
KillMode=process
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Elemental setup reconciler

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Type=oneshot
ExecStart=/bin/bash -c "systemd-inhibit /usr/bin/elemental run-stage reconcile"
TimeoutStopSec=180
KillMode=process
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Elemental setup reconciler

[Timer]
OnBootSec=5min
OnUnitActiveSec=60min
Unit=elemental-setup-reconcile.service

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Elemental system early rootfs setup
DefaultDependencies=no
After=initrd-root-fs.target
Requires=initrd-root-fs.target
Conflicts=initrd-switch-root.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/usr/bin/ln -sf -t / /sysroot/system
ExecStart=/usr/bin/elemental run-stage rootfs

[Install]
RequiredBy=initrd-fs.target
20 changes: 19 additions & 1 deletion pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/fs"
"path/filepath"
"slices"
"strings"

"github.com/rancher/elemental-toolkit/pkg/constants"
Expand Down Expand Up @@ -134,16 +135,33 @@ func Get(names []string) ([]*Feature, error) {
case FeatureCloudConfigEssentials:
features = append(features, New(name, nil))
case FeatureImmutableRootfs:
if slices.Contains(names, FeatureElementalRootfs) {
return features, fmt.Errorf("Conflicting features: %s and %s", FeatureImmutableRootfs, FeatureElementalRootfs)
}

features = append(features, New(name, nil))
case FeatureElementalRootfs:
features = append(features, New(name, nil))
units := []*systemd.Unit{
systemd.NewUnit("elemental-setup-reconcile.service"),
systemd.NewUnit("elemental-setup-reconcile.timer"),
systemd.NewUnit("elemental-setup-boot.service"),
systemd.NewUnit("elemental-setup-rootfs.service"),
systemd.NewUnit("elemental-setup-network.service"),
systemd.NewUnit("elemental-setup-initramfs.service"),
systemd.NewUnit("elemental-setup-fs.service"),
}
features = append(features, New(name, units))
case FeatureDracutConfig:
features = append(features, New(name, nil))
case FeatureGrubConfig:
features = append(features, New(name, nil))
case FeatureGrubDefaultBootargs:
features = append(features, New(name, nil))
case FeatureElementalSetup:
if slices.Contains(names, FeatureElementalRootfs) {
return features, fmt.Errorf("Conflicting features: %s and %s", FeatureElementalSetup, FeatureElementalRootfs)
}

units := []*systemd.Unit{
systemd.NewUnit("elemental-setup-reconcile.service"),
systemd.NewUnit("elemental-setup-reconcile.timer"),
Expand Down
5 changes: 5 additions & 0 deletions pkg/systemd/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ func Enable(runner v1.Runner, unit *Unit) error {
_, err := runner.Run("systemctl", "enable", unit.Name)
return err
}

func Start(runner v1.Runner, unit *Unit) error {
_, err := runner.Run("systemctl", "start", unit.Name)
return err
}
2 changes: 1 addition & 1 deletion pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ type InitSpec struct {
type MountSpec struct {
ReadKernelCmdline bool `yaml:"read-kernel-cmdline,omitempty" mapstructure:"read-kernel-cmdline"`
WriteFstab bool `yaml:"write-fstab,omitempty" mapstructure:"write-fstab"`
RunCloudInit bool `yaml:"run-cloud-init,omitempty" mapstructure:"run-cloud-init"`
RunRootfsService bool `yaml:"run-rootfs-service,omitempty" mapstructure:"run-rootfs-service"`
RunFsck bool `yaml:"run-fsck,omitempty" mapstructure:"run-fsck"`
Disable bool `yaml:"disable,omitempty" mapstructure:"disable"`
Sysroot string `yaml:"sysroot,omitempty" mapstructure:"sysroot"`
Expand Down

0 comments on commit 66bd2a8

Please sign in to comment.