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