Skip to content

Commit c2e1f1d

Browse files
committed
image: add IMDS support in initrd
This makes built images work in cloud environments for passing credentials/configuration via IMDS, and keeps initrds consistent between all output formats. systemd's IMDS generator enables the imds and networkd units only on a hwdb-matched cloud, so there is no runtime impact in other environments. It still grows the disk footprint by a bit, measurements for the uncompressed boot-demo initrd: Fedora: 93.5 MB → 101.4 MB (+8.5%) Debian: 103.2 MB → 111.7 MB (+8.2%) Arch: unchanged, its systemd already ships both Signed-off-by: Martin Pitt <martin@amutable.com>
1 parent e0bd4df commit c2e1f1d

7 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎distribution/arch.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ _ARCH_PACKAGE_SETS = {
4848
],
4949
# `base-devel` is a metapackage, so makepkg's assumed build environment is one name.
5050
"buildroot": ["base-devel"],
51-
# Arch ships udev inside systemd and veritysetup inside cryptsetup.
51+
# Arch ships udev, networkd and IMDS support inside systemd, and veritysetup inside cryptsetup.
5252
"initrd": ["bash", "cryptsetup", "kmod", "systemd"],
5353
}
5454

‎distribution/debian.bzl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _DEBIAN_PACKAGE_SETS = {
7575
"mount",
7676
"systemd",
7777
"systemd-cryptsetup",
78+
"systemd-imds",
7879
"udev",
7980
"util-linux",
8081
],

‎distribution/fedora.bzl‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ _FEDORA_PACKAGE_SETS = {
4242
"util-linux",
4343
],
4444
"buildroot": ["@buildsys-build"],
45-
"initrd": ["bash", "kmod", "systemd", "systemd-udev", "veritysetup"],
45+
"initrd": [
46+
"bash",
47+
"kmod",
48+
# avoid full libcurl, not necessary for imds
49+
"libcurl-minimal",
50+
"systemd",
51+
"systemd-imds",
52+
"systemd-networkd",
53+
"systemd-udev",
54+
"veritysetup",
55+
],
4656
}
4757

4858
def _merge_package_sets(

‎docs/user/images.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ initrd never reads) apply either way. `compression` picks how the cpio the UKI c
242242
it from the tree. `install_docs` and `install_langs` default the other way round from an OS image, since
243243
documentation and translations only cost an initrd boot memory.
244244

245+
Every initrd can import system credentials, such as an SSH key, from a cloud's instance metadata service
246+
(IMDS). systemd's IMDS generator enables that (together with networkd to reach the service) on a cloud
247+
instance which it recognizes from the hardware database; `systemd.imds=` on the kernel command line
248+
overrides it.
249+
245250
The target is an ordinary image: it builds on its own, publishes `[pkgdb]` and `[sbom]`, and one initrd can
246251
be shared by several disk images.
247252

‎examples/image/BUCK‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ tests.vm_smoke(
262262
"test -s /usr/lib/udev/hwdb.bin",
263263
# The runner's cmdline_extra reached the kernel the UKI booted.
264264
"grep -q tine.smoke=1 /proc/cmdline",
265+
# The initrd carries networkd for IMDS, which is inert on a non-cloud VM.
266+
'[ -z "$(journalctl -b -q _RUNTIME_SCOPE=initrd _SYSTEMD_UNIT=systemd-networkd.service)" ]',
265267
],
266268
)
267269

‎examples/image/expected.json‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@
3636
},
3737
"x86_64": {
3838
"boot-demo.fedora": {
39-
"sha256": "6a7b4796d2216d9bf9d0db587d08e9c8d44fdc1ebc817c2c4ba3f428aa6f3195"
39+
"sha256": "e8559e1abcfc7c831acd49abca86c64724e2622ce267cec8eea82a87546d95e5"
4040
},
4141
"boot-demo.fedora[boot][initrd]": {
42-
"sha256": "d0081aa88c2729fb5363d30eb815e3cdc563eb9c61a5c19e93413ca80a3a0933"
42+
"sha256": "90cbd50c4bb7041e8af9031cfbf4bcaab81b7de5d1795200a8a8af739660d212"
4343
},
4444
"boot-demo.fedora[boot][kernel]": {
4545
"sha256": "50896468d533671538dea76a150529455c2712075a927665339ed755065913c6"
4646
},
4747
"boot-demo.fedora[boot][uki]": {
48-
"sha256": "445a85f5425a11d8af6094491c44854b746c6ee6fb0c029134348c148a2adc28"
48+
"sha256": "43df3056444a89663d57b97c96a597b357811b1b384a97ea7a761ac67f7a1173"
4949
},
5050
"boot-demo.fedora[disk-manifest]": {
51-
"sha256": "ece373a06978f121266654bb894cff92fd038e2f5dc5b4bd2db04edfcb250e29"
51+
"sha256": "e959738f81352350e37f16535cafb2aae2759da49ea23c27df0b0c5924e6d7a4"
5252
},
5353
"boot-demo.fedora[manifests][esp]": {
54-
"sha256": "370ca1974ee8a13d3f0e0fa4bd18df38b936d82ae5c101dc6261dd7f983b07b9"
54+
"sha256": "707c4c10d26529a8e5c3577e635bd69522f3747c1cd5bf282c023f0efa285a8c"
5555
},
5656
"boot-demo.fedora[manifests][usr]": {
5757
"sha256": "5ba6f1c6690e971d62b97e17d2131cc28186c4992d1cfa27a1ccca6343590654"

‎image/compose.bzl‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ load(
5555
"remove",
5656
"sign_systemd_boot",
5757
"symlink",
58+
"write_file",
5859
)
5960
load(":publish.bzl", "PublishedInfo", "published_metadata_subtargets")
6061
load(
@@ -80,6 +81,14 @@ InitrdInfo = provider(
8081
},
8182
)
8283

84+
# As of systemd 262, systemd-imds-early-network.service only writes a .network file, but doesn't start
85+
# networkd. The IMDS generator pulls the unit in only on a cloud it detects in the hardware database, so
86+
# networkd runs nowhere else, and `systemd.imds.network=off` opts out. mkosi does that via a preset call.
87+
_IMDS_NETWORKD = write_file(
88+
"/usr/lib/systemd/system/systemd-imds-early-network.service.d/tine-networkd.conf",
89+
"[Unit]\nWants=systemd-networkd.service\n",
90+
)
91+
8392
def _initrd_image_impl(ctx: AnalysisContext) -> list[Provider]:
8493
tools = ctx.attrs._tools[ImageToolsInfo]
8594
image = declare_image(
@@ -91,6 +100,7 @@ def _initrd_image_impl(ctx: AnalysisContext) -> list[Provider]:
91100
[
92101
symlink("/usr/lib/systemd/systemd", "/init"),
93102
symlink("/etc/os-release", "/etc/initrd-release"),
103+
_IMDS_NETWORKD,
94104
]
95105
+ ctx.attrs.ops,
96106
["initrd"],

0 commit comments

Comments
 (0)