Skip to main content
Version: 3.0.0-rc.2

Build a CPIO initramfs

[dependencies.hadris]
version = "3.0.0-rc.2"
default-features = false
features = ["std", "sync", "cpio"]
use hadris::cpio::CpioOptions;
use hadris::fs::Owner;
use hadris::fs::host::{self, TreeOptions};
use hadris::io::StdIo;
use std::{fs::File, io::BufWriter};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = TreeOptions::new().with_owner(Owner::new(0, 0));
let (tree, _) = host::read_tree("./initramfs-root", &options)?;
let mut output = StdIo::new(BufWriter::new(File::create("initramfs.cpio")?));
hadris::cpio::sync::write(&mut output, &tree, &CpioOptions::default())?;
Ok(())
}

Hadris writes the newc/SVR4 format used by Linux initramfs images. Device nodes, symlinks and hard links in the source tree are stored; hard link groups carry their data on the last name, as GNU cpio writes them. with_owner makes every entry root-owned, as an unprivileged build needs. For a reproducible archive, pass host::source_date_epoch() to CpioOptions::with_time and TreeOptions::with_clamp. The reader iterates entries without an allocator for constrained consumers.

Inspect the result with an independent implementation before booting it:

cpio -itv < initramfs.cpio

See Read and create CPIO archives for streaming entry and payload handling.