Skip to main content

Build a CPIO initramfs

[dependencies]
hadris-cpio = "2.1.0"
use hadris_cpio::{CpioArchiveWriter, CpioWriteOptions, FileTree};
use std::{fs::File, io::BufWriter, path::Path};

fn main() -> hadris_cpio::Result<()> {
let tree = FileTree::from_fs(Path::new("./initramfs-root"))?;
let output = BufWriter::new(File::create("initramfs.cpio")?);
CpioArchiveWriter::new(output, CpioWriteOptions::default()).finish(&tree)?;
Ok(())
}

Hadris writes the newc/SVR4 format used by Linux initramfs images. The reader also supports allocation-free entry iteration 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.