From f61ba60bbc092943c6f91d8c799bdd2ce910befb Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 7 Aug 2025 15:16:38 -0400 Subject: [PATCH] Add initramfs infrastructure This adds scaffolding to install a stub binary which can optionally be added into the initramfs; prep for us doing real work during setup as we aim to move to the native composefs backend. The binary is *built* but is only installed by a new `Makefile` target, so existing build system users won't pick it up. Our development-only `Dockerfile` gains a build option to use it (and also ensures the initramfs is regenerated). However previously we also discussed moving the fstab logic into the initramfs: https://github.com/bootc-dev/bootc/pull/1113 I might try doing that once this lands. One notable thing is that even this trivial nearly-no-op binary is still 4MB which I think is mostly due to linking in a whole copy of prebuilt rust `std`. In theory we could try going to `#[no_std]` but I don't think it'll be viable once we start doing more here. Probably most practical thing re size is `-Z build-std` + LTO. Signed-off-by: Colin Walters --- Cargo.lock | 7 ++++++ Dockerfile | 16 +++++++++++-- Makefile | 12 +++++++++- crates/initramfs/Cargo.toml | 12 ++++++++++ crates/initramfs/bootc-root-setup.service | 22 +++++++++++++++++ crates/initramfs/dracut/module-setup.sh | 18 ++++++++++++++ crates/initramfs/src/main.rs | 24 +++++++++++++++++++ .../booted/readonly/051-test-initramfs.nu | 13 ++++++++++ 8 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 crates/initramfs/Cargo.toml create mode 100644 crates/initramfs/bootc-root-setup.service create mode 100755 crates/initramfs/dracut/module-setup.sh create mode 100644 crates/initramfs/src/main.rs create mode 100644 tmt/tests/booted/readonly/051-test-initramfs.nu diff --git a/Cargo.lock b/Cargo.lock index 4582f2a26..f25b71eaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,6 +185,13 @@ dependencies = [ "tracing", ] +[[package]] +name = "bootc-initramfs-setup" +version = "0.1.0" +dependencies = [ + "anyhow", +] + [[package]] name = "bootc-internal-blockdev" version = "0.0.0" diff --git a/Dockerfile b/Dockerfile index 3515f73e3..09d3dec67 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,6 +39,8 @@ EORUN # bootc binaries in /out. The intention is that the target rootfs is extracted from /out # back into a final stae (without the build deps etc) below. FROM base as build +# Flip this on to enable initramfs code +ARG initramfs=0 # This installs our package dependencies, and we want to cache it independently of the rest. # Basically we don't want changing a .rs file to blow out the cache of packages. So we only # copy files necessary @@ -59,8 +61,14 @@ COPY --from=src /src /src WORKDIR /src # See https://www.reddit.com/r/rust/comments/126xeyx/exploring_the_problem_of_faster_cargo_docker/ # We aren't using the full recommendations there, just the simple bits. -RUN --mount=type=cache,target=/build/target --mount=type=cache,target=/var/roothome \ - make && make install-all DESTDIR=/out +RUN --mount=type=cache,target=/build/target --mount=type=cache,target=/var/roothome < Result<()> { + let _ = std::fs::metadata("/sysroot/usr")?; + println!("setup OK"); + Ok(()) +} + +fn main() -> Result<()> { + let v = std::env::args().collect::>(); + let args = match v.as_slice() { + [] => anyhow::bail!("Missing argument".to_string()), + [_, rest @ ..] => rest, + }; + match args { + [] => anyhow::bail!("Missing argument".to_string()), + [s] if s == "setup-root" => setup_root(), + [o, ..] => anyhow::bail!(format!("Unknown command {o}")), + } +} diff --git a/tmt/tests/booted/readonly/051-test-initramfs.nu b/tmt/tests/booted/readonly/051-test-initramfs.nu new file mode 100644 index 000000000..150054eee --- /dev/null +++ b/tmt/tests/booted/readonly/051-test-initramfs.nu @@ -0,0 +1,13 @@ +use std assert +use tap.nu + +tap begin "initramfs" + +if (not ("/usr/lib/bootc/initramfs-setup" | path exists)) { + print "No initramfs support" + exit 0 +} + +journalctl -b -t bootc-root-setup.service --grep=OK + +tap ok