Refactor ignition support during dracut

- Control root device using the node/profile root field
- Add support for `/warewulf/wwinit.d` to run scripts during first stage
- Move first-stage ignition support to `/warewulf/wwinit.d/`
- Add `wwctl <node|profile> set --parttype`
- Add overlay template functions `SystemdEscape` and `SystemdEscapePath`
- Support configuring ignition with resources

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-06-30 23:09:49 -06:00
parent 608ef89d40
commit b98eae4b01
29 changed files with 787 additions and 354 deletions

View File

@@ -11,66 +11,36 @@ import (
)
func Test_ignitionOverlay(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll()
env.ImportFile("etc/warewulf/nodes.conf", "nodes.conf")
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/etc/systemd/system/ww4-disks.target.ww", "../rootfs/etc/systemd/system/ww4-disks.target.ww")
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/etc/systemd/system/ww4-mounts.ww", "../rootfs/etc/systemd/system/ww4-mounts.ww")
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/warewulf/ignition.json.ww", "../rootfs/warewulf/ignition.json.ww")
tests := []struct {
name string
args []string
log string
json bool
tests := map[string]struct {
args []string
nodesConf string
output string
isJson bool
}{
{
name: "ignition:ww4-disks.target",
args: []string{"--render", "node1", "ignition", "etc/systemd/system/ww4-disks.target.ww"},
log: ignition_disks,
json: false,
},
{
name: "ignition:ww4-mounts",
args: []string{"--render", "node1", "ignition", "etc/systemd/system/ww4-mounts.ww"},
log: ignition_mounts,
json: false,
},
{
name: "ignition:ignition.json",
args: []string{"--quiet", "--render", "node1", "ignition", "warewulf/ignition.json.ww"},
log: ignition_json,
json: true,
},
}
"ignition:ww4-disks.target": {
args: []string{"--quiet", "--render", "node1", "ignition", "etc/systemd/system/ww4-disks.target.ww"},
nodesConf: `
nodes:
node1:
disks:
/dev/vda:
wipe_table: true
partitions:
scratch:
should_exist: true
swap:
number: "1"
size_mib: "1024"
filesystems:
/dev/disk/by-partlabel/scratch:
format: btrfs
path: /scratch
wipe_filesystem: true
/dev/disk/by-partlabel/swap:
format: swap
path: swap`,
output: `# This file is autogenerated by warewulf
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := show.GetCommand()
cmd.SetArgs(tt.args)
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
logbuf := bytes.NewBufferString("")
cmd.SetOut(stdout)
cmd.SetErr(stderr)
wwlog.SetLogWriter(logbuf)
err := cmd.Execute()
assert.NoError(t, err)
assert.Empty(t, stdout.String())
assert.Empty(t, stderr.String())
if tt.json {
assert.JSONEq(t, tt.log, logbuf.String())
} else {
assert.Equal(t, tt.log, logbuf.String())
}
})
}
}
const ignition_disks string = `backupFile: true
writeFile: true
Filename: etc/systemd/system/ww4-disks.target
# This file is autogenerated by warewulf
[Unit]
Description=mount ww4 disks
# make sure that the disks are available
@@ -80,14 +50,76 @@ Requisite=ignition-ww4-disks.service
# Get the mounts
Wants=scratch.mount
Wants=dev-disk-by\x2dpartlabel-swap.swap
`
`,
isJson: false,
},
"ignition:ww4-disks.target (resources)": {
args: []string{"--quiet", "--render", "node1", "ignition", "etc/systemd/system/ww4-disks.target.ww"},
nodesConf: `
nodes:
node1:
resources:
ignition:
storage:
disks:
- device: /dev/vda
wipeTable: true
partitions:
- label: scratch
shouldExist: true
- label: swap
number: 1
sizeMiB: 1024
filesystems:
- device: /dev/disk/by-partlabel/scratch
format: btrfs
path: /scratch
wipeFilesystem: true
- device: /dev/disk/by-partlabel/swap
format: swap
path: swap`,
output: `# This file is autogenerated by warewulf
const ignition_mounts string = `backupFile: true
[Unit]
Description=mount ww4 disks
# make sure that the disks are available
Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
Requisite=ignition-ww4-disks.service
# Get the mounts
Wants=scratch.mount
Wants=dev-disk-by\x2dpartlabel-swap.swap
`,
isJson: false,
},
"ignition:ww4-mounts": {
args: []string{"--quiet=false", "--render", "node1", "ignition", "etc/systemd/system/ww4-mounts.ww"},
nodesConf: `
nodes:
node1:
disks:
/dev/vda:
wipe_table: true
partitions:
scratch:
should_exist: true
swap:
number: "1"
size_mib: "1024"
filesystems:
/dev/disk/by-partlabel/scratch:
format: btrfs
path: /scratch
wipe_filesystem: true
/dev/disk/by-partlabel/swap:
format: swap
path: swap`,
output: `backupFile: true
writeFile: true
Filename: scratch.mount
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Before=local-fs.target
@@ -99,11 +131,11 @@ What=/dev/disk/by-partlabel/scratch
Type=btrfs
[Install]
RequiredBy=local-fs.target
backupFile: true
writeFile: true
Filename: dev-disk-by\x2dpartlabel-swap.swap
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Requires=ignition-ww4-disks.service
@@ -113,9 +145,91 @@ Before=swap.target
What=/dev/disk/by-partlabel/swap
[Install]
RequiredBy=swap.target
`
`,
isJson: false,
},
"ignition:ww4-mounts (resources)": {
args: []string{"--quiet=false", "--render", "node1", "ignition", "etc/systemd/system/ww4-mounts.ww"},
nodesConf: `
nodes:
node1:
resources:
ignition:
storage:
disks:
- device: /dev/vda
wipeTable: true
partitions:
scratch:
shouldExist: true
swap:
number: 1
sizeMiB: 1024
filesystems:
- device: /dev/disk/by-partlabel/scratch
format: btrfs
path: /scratch
wipeFilesystem: true
- device: /dev/disk/by-partlabel/swap
format: swap
path: swap`,
output: `backupFile: true
writeFile: true
Filename: scratch.mount
const ignition_json string = `{
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Before=local-fs.target
Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
[Mount]
Where=/scratch
What=/dev/disk/by-partlabel/scratch
Type=btrfs
[Install]
RequiredBy=local-fs.target
backupFile: true
writeFile: true
Filename: dev-disk-by\x2dpartlabel-swap.swap
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
Before=swap.target
[Swap]
What=/dev/disk/by-partlabel/swap
[Install]
RequiredBy=swap.target
`,
isJson: false,
},
"ignition:ignition.json": {
args: []string{"--quiet", "--render", "node1", "ignition", "warewulf/ignition.json.ww"},
nodesConf: `
nodes:
node1:
disks:
/dev/vda:
wipe_table: true
partitions:
scratch:
should_exist: true
swap:
number: "1"
size_mib: "1024"
filesystems:
/dev/disk/by-partlabel/scratch:
format: btrfs
path: /scratch
wipe_filesystem: true
/dev/disk/by-partlabel/swap:
format: swap
path: swap`,
output: `{
"ignition": {
"version": "3.1.0"
},
@@ -155,4 +269,85 @@ const ignition_json string = `{
}
]
}
}`
}`,
isJson: true,
},
"ignition:ignition.json (resources)": {
args: []string{"--quiet", "--render", "node1", "ignition", "warewulf/ignition.json.ww"},
nodesConf: `
nodes:
node1:
resources:
ignition:
storage:
disks:
- device: /dev/vda
partitions:
- label: rootfs
number: 1
shouldExist: true
wipePartitionEntry: false
wipeTable: true
filesystems:
- device: /dev/disk/by-partlabel/rootfs
format: ext4
path: /
wipeFilesystem: false`,
output: `{
"storage": {
"disks": [
{
"device": "/dev/vda",
"partitions": [
{
"label": "rootfs",
"number": 1,
"shouldExist": true,
"wipePartitionEntry": false
}
],
"wipeTable": true
}
],
"filesystems": [
{
"device": "/dev/disk/by-partlabel/rootfs",
"format": "ext4",
"path": "/",
"wipeFilesystem": false
}
]
}
}`,
isJson: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
env := testenv.New(t)
defer env.RemoveAll()
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/etc/systemd/system/ww4-disks.target.ww", "../rootfs/etc/systemd/system/ww4-disks.target.ww")
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/etc/systemd/system/ww4-mounts.ww", "../rootfs/etc/systemd/system/ww4-mounts.ww")
env.ImportFile("var/lib/warewulf/overlays/ignition/rootfs/warewulf/ignition.json.ww", "../rootfs/warewulf/ignition.json.ww")
env.WriteFile("etc/warewulf/nodes.conf", tt.nodesConf)
cmd := show.GetCommand()
cmd.SetArgs(tt.args)
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
logbuf := bytes.NewBufferString("")
cmd.SetOut(stdout)
cmd.SetErr(stderr)
wwlog.SetLogWriter(logbuf)
err := cmd.Execute()
assert.NoError(t, err)
assert.Empty(t, stdout.String())
assert.Empty(t, stderr.String())
if tt.isJson {
assert.JSONEq(t, tt.output, logbuf.String())
} else {
assert.Equal(t, tt.output, logbuf.String())
}
})
}
}

View File

@@ -1,19 +0,0 @@
nodes:
node1:
disks:
/dev/vda:
wipe_table: true
partitions:
scratch:
should_exist: true
swap:
number: "1"
size_mib: "1024"
filesystems:
/dev/disk/by-partlabel/scratch:
format: btrfs
path: /scratch
wipe_filesystem: true
/dev/disk/by-partlabel/swap:
format: swap
path: swap

View File

@@ -1,4 +1,5 @@
# This file is autogenerated by warewulf
[Unit]
Description=mount ww4 disks
# make sure that the disks are available
@@ -6,19 +7,20 @@ Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
Requisite=ignition-ww4-disks.service
# Get the mounts
{{- range $fsdevice,$fs := .FileSystems }}
{{- $prefix := $fsdevice }}
{{- $prefix = replace "-" "\\x2d" $fsdevice }}
{{- $prefix = replace "/" "-" $prefix }}
{{- $prefix = substr 1 -1 $prefix }}{{ $suffix := "mount" }}
{{- if eq $fs.Format "swap"}}
{{- $prefix = replace "/dev/disk/by-partlabel/" "dev-disk-by\\x2dpartlabel-" $fsdevice }}{{ $suffix = "swap"}}
{{- else }}
{{- if ne $fs.Path "" }}
{{- $prefix = replace "-" "\\x2d" $fs.Path }}
{{- $prefix = replace "/" "-" $prefix }}
{{- $prefix = substr 1 -1 $prefix }}
{{- if .FileSystems }}
{{- range $fsdevice, $fs := .FileSystems }}
{{- if eq $fs.Format "swap" }}
Wants={{ $fsdevice | SystemdEscapePath }}.swap
{{- else if ne $fs.Path "" }}
Wants={{ $fs.Path | SystemdEscapePath }}.mount
{{- end }}
{{- end }}
{{- else if .Resources.ignition.storage.filesystems }}
{{- range $fs := .Resources.ignition.storage.filesystems }}
{{- if eq $fs.format "swap" }}
Wants={{ $fs.device | SystemdEscapePath }}.swap
{{- else if ne $fs.path "" }}
Wants={{ $fs.path | SystemdEscapePath }}.mount
{{- end }}
{{- end }}
Wants={{ print $prefix "." $suffix }}
{{- end }}

View File

@@ -1,13 +1,9 @@
{{- range $fsdevice,$fs := .FileSystems }}
{{- $prefix := replace "-" "\\x2d" $fs.Path }}
{{- $prefix = replace "/" "-" $prefix }}
{{- $prefix = substr 1 -1 $prefix }}
{{ $suffix := "mount"}}
{{- if $fs.Label }}{{ $prefix = $fs.Label }}{{ end }}
{{- if eq $fs.Format "swap" }}{{ $prefix = replace "/dev/disk/by-partlabel/" "dev-disk-by\\x2dpartlabel-" $fsdevice }}{{ $suffix = "swap" }}{{ end }}
{{ file (print $prefix "." $suffix) }}
{{- if .FileSystems }}
{{- range $fsdevice, $fs := .FileSystems }}
{{- if eq $fs.Format "swap" }}
{{ file (print ($fsdevice | SystemdEscapePath) ".swap") }}
# This file is autogenerated by warewulf
{{- if eq $fs.Format "swap"}}
[Unit]
ConditionPathExists=/warewulf/ignition.json
Requires=ignition-ww4-disks.service
@@ -17,7 +13,10 @@ Before=swap.target
What={{ $fsdevice }}
[Install]
RequiredBy=swap.target
{{- else }}
{{- else if ne $fs.Path "" }}
{{ file (print ($fs.Path | SystemdEscapePath) ".mount") }}
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Before=local-fs.target
@@ -28,9 +27,49 @@ Where={{ $fs.Path }}
What={{ $fsdevice }}
Type={{ $fs.Format }}
{{- if $fs.MountOptions }}
Options={{range $index,$opt := $fs.MountOptions }}{{if $index }},{{ end }}{{ $opt }}{{ end }}
Options={{ $fs.MountOptions | join "." }}
{{- end }}
[Install]
RequiredBy=local-fs.target
{{- else }}
{{- continue }}
{{- end }}
{{- end }}
{{- else if .Resources.ignition.storage.filesystems }}
{{- range $fs := .Resources.ignition.storage.filesystems }}
{{- if eq $fs.format "swap" }}
{{ file (print ($fs.device | SystemdEscapePath) ".swap") }}
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
Before=swap.target
[Swap]
What={{ $fs.device }}
[Install]
RequiredBy=swap.target
{{- else if ne $fs.path "" }}
{{ file (print ($fs.path | SystemdEscapePath) ".mount") }}
# This file is autogenerated by warewulf
[Unit]
ConditionPathExists=/warewulf/ignition.json
Before=local-fs.target
Requires=ignition-ww4-disks.service
After=ignition-ww4-disks.service
[Mount]
Where={{ $fs.path }}
What={{ $fs.device }}
Type={{ $fs.format }}
{{- if $fs.mountOptions }}
Options={{ $fs.mountOptions | join "." }}
{{- end }}
[Install]
RequiredBy=local-fs.target
{{- else }}
{{- continue }}
{{- end }}
{{- end }}
{{- end }}

View File

@@ -1 +1 @@
{{ IgnitionJson }}
{{ coalesce IgnitionJson (.Resources.ignition | toJson) }}

View File

@@ -0,0 +1,23 @@
#!/bin/sh
PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
if ! command -v info >/dev/null; then
info() {
printf '%s\n' "$*"
}
fi
if ! command -v die >/dev/null; then
die() {
printf '%s\n' "$*" >&2
exit 1
}
fi
if command -v ignition >/dev/null; then :
info "warewulf: ignition: partition and format disks"
ignition --config-cache="${PREFIX}/warewulf/ignition.json" --platform=metal --stage=disks || die "warewulf: ignition: failed to partition/format disk"
else
info "warewulf: ignition not found"
fi

View File

@@ -1 +0,0 @@
{{ IgnitionJson }}

View File

@@ -1,53 +1,65 @@
#!/bin/sh
echo "Warewulf pre-init (/init)"
echo
echo "Mounting kernel file systems..."
if ! command -v info >/dev/null; then
info() {
printf '%s\n' "$*"
}
fi
if ! command -v die >/dev/null; then
die() {
printf '%s\n' "$*" >&2
echo
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
}
fi
info "warewulf: running pre-init wwinit process..."
info "warewulf: mounting kernel file systems..."
mountpoint -q /proc || (mkdir -p /proc && mount -t proc proc /proc && echo "Mounted /proc")
mountpoint -q /dev || (mkdir -p /dev && mount -t devtmpfs devtmpfs /dev && echo "Mounted /dev")
mountpoint -q /sys || (mkdir -p /sys && mount -t sysfs sysfs /sys && echo "Mounted /sys")
# https://systemd.io/INITRD_INTERFACE/
mountpoint -q /run || (mkdir -p /run && mount -t tmpfs -o mode=0755,nodev,nosuid,strictatime tmpfs /run && echo "Mounted /run")
echo
echo "Loading /warewulf/config..."
info "warewulf: loading /warewulf/config..."
if [ -f "/warewulf/config" ]; then
. /warewulf/config
else
echo "ERROR: Warewulf configuration file not found."
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
die "warewulf: ERROR: /warewulf/config not found"
fi
echo
echo "Configuring root file system..."
WWPRESCRIPTS=/warewulf/wwprescripts
chmod +rx "${WWPRESCRIPTS}"
if [ -x /warewulf/run-wwinit.d ]; then
/warewulf/run-wwinit.d
fi
NEXT_INIT=/warewulf/run-init
chmod +rx "${NEXT_INIT}"
info "warewulf: configuring root file system..."
WWROOT="${WWROOT:-initramfs}"
if [ "${WWROOT}" = "initramfs" ]; then
echo "WWROOT=${WWROOT}: using initial rootfs and invoking ${WWPRESCRIPTS}..."
exec "${WWPRESCRIPTS}"
elif [ "${WWROOT}" = "persistent" ] ; then
echo "WWROOT=${WWROOT}: booting from DISK"
exec "${WWPRESCRIPTS}"
elif [ "${WWROOT}" = "ramfs" -o "${WWROOT}" = "tmpfs" ]; then
echo "WWROOT=${WWROOT}: setting up new ${WWROOT} rootfs (/newroot)..."
mkdir /newroot
mount wwroot /newroot -t ${WWROOT} -o mpol=interleave # mpol ignored for ramfs
tar -cf - --exclude ./proc --exclude ./sys --exclude ./dev --exclude --exclude ./newroot . | tar -xf - -C /newroot
mkdir /newroot/proc /newroot/dev /newroot/sys 2>/dev/null
echo "Switching to new rootfs (/newroot) and invoking ${WWPRESCRIPTS}..."
exec /sbin/switch_root /newroot "${WWPRESCRIPTS}"
info "warewulf: using initial rootfs and invoking ${NEXT_INIT}..."
exec "${NEXT_INIT}"
else
echo "ERROR: Unrecognized rootfs type requested: ${WWROOT}"
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
mkdir /newroot
info "warewulf: mounting ${WWROOT} at /newroot"
if [ "${WWROOT}" = "ramfs" -o "${WWROOT}" = "tmpfs" ]; then
mount wwroot /newroot -t ${WWROOT} -o mpol=interleave # mpol ignored for ramfs
else
mount ${WWROOT} /newroot
fi
info "warewulf: copying image to /newroot..."
tar -cf - --exclude ./proc --exclude ./sys --exclude ./dev --exclude --exclude ./newroot . | tar -xf - -C /newroot
mkdir /newroot/proc /newroot/dev /newroot/sys 2>/dev/null
info "warewulf: switching to /newroot and invoking ${NEXT_INIT}..."
exec /sbin/switch_root /newroot "${NEXT_INIT}"
fi
echo
echo "ERROR: wwinit encountered a problem."
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
die "warewulf: ERROR: wwinit encountered a problem."

View File

@@ -0,0 +1,40 @@
#!/bin/sh
if ! command -v info >/dev/null; then
info() {
printf '%s\n' "$*"
}
fi
. /warewulf/config
scriptdir=/warewulf/init.d
echo "warewulf: running scripts in ${scriptdir}..."
ls -1 "${scriptdir}/" | while read -r name; do
info "warewulf: ${name}"
sh "${scriptdir}/${name}"
done
init="${WWINIT}"
if [ -z "${init}" ]
then
for candidate in /sbin/init /etc/init /bin/init
do
if [ -x "${candidate}" ]
then
init="${candidate}"
break
fi
done
fi
if [ -n "${init}" ]
then
info "warewulf: invoking ${init}..."
exec "${init}"
else
echo "ERROR: init not defined or not found."
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
fi

View File

@@ -0,0 +1,18 @@
#!/bin/sh
if ! command -v info >/dev/null; then
info() {
printf '%s\n' "$*"
}
fi
scriptdir="${PREFIX}/warewulf/wwinit.d"
if [ -d "${scriptdir}" ]; then
info "warewulf: running scripts in ${scriptdir}..."
ls -1 "${scriptdir}/" | while read -r name; do
info "warewulf: ${name}"
PREFIX=$PREFIX sh "${scriptdir}/${name}"
done
else
info "warewulf: ${scriptdir} not found"
fi

View File

@@ -1,36 +0,0 @@
#!/usr/bin/sh
. /warewulf/config
echo "Warewulf prescript runner (/warewulf/wwprescripts)"
scriptdir=/warewulf/init.d
echo "Looking for prescripts in /warewulf/init.d/..."
ls -1 "${scriptdir}/" | while read -r name; do
echo "Running prescript: ${name}..."
sh "${scriptdir}/${name}"
done
init="${WWINIT}"
if [ -z "${init}" ]
then
for candidate in /sbin/init /etc/init /bin/init
do
if [ -x "${candidate}" ]
then
init="${candidate}"
break
fi
done
fi
if [ -n "${init}" ]
then
echo
echo "Running ${init}..."
exec "${init}"
else
echo "ERROR: init not defined and not found."
echo "Rebooting in 1 minute..."
sleep 60
echo b > /proc/sysrq-trigger || /sbin/reboot -f
fi

View File

@@ -0,0 +1 @@
run-init