Skip to content

Commit bc26d99

Browse files
Detail how to extract an initramfs / initrd gz cpio archive, view it's init script and repackage
1 parent c64027b commit bc26d99

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,39 @@ Goal is to have mimimal but realistic node with network capabilities build in (`
1616
- <strike>dropbear needs at least a `dropbear_rsa_host_key` key config or will not start see [gist](https://gist.github.com/mad4j/7983719) </strike>
1717
- Prefering openssh for end user compatability (statically built)
1818

19+
### How do I extract an `initramfs` / `initrd` gz cpio archive, view it's `init` script and repackage?
1920

21+
> Situation: How do I make a change to the `init` script without going through the whole process of re-building? I've built `initramfs` using [`build-all.sh`](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/build-all.sh#L1) but I want to make a quick change to the `init` script (generated in [`create-init.sh`](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/create-init.sh#L1), called by [create-scratch-space](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/create-scratch-space.sh#L71).
2022
21-
TODO: add [iproute2](https://github.com/iproute2/iproute2) for minimal routing.
23+
For a faster feedback loop, sometimes you want to speed up debugging your `rootfs.cpio.gz` init script which is inside your (initramfs / initrd) image- which is typically a gzipped, `cpio` archive. The bootloader extracts and loads this `initramfs` / `initrd` filesystem, calling your `init` script first. In this repo example, we [pass](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/run-qemu.sh#L5C144-L5C154) `init=/init` to the kernel args. During boot the `init` file
24+
(which is stored in the root (`/`) of the `initramfs` image, is ran first. Distributions can use this to perform initial setup, such as ensuring
25+
a minimal valid linux directory structure (`/dev`, `proc` etc) and handle other boot-time kernel arguments such as `ip=` to set boot time network ip addressing.
26+
27+
To play around quicky with an already *built* `initramfs` / `initrd` image (for reference, [here's where we _build_ the initramfs image](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/build-rootfs.sh#L7)). But building a brand new image etc takes time, for instance, we [compile `busybox` and `openssh`](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/build-all.sh#L26-L35) into our`initramfs`.
28+
29+
To extract the end product (`rootfs.cpio.gz`) you can do the following:
30+
Goal: Extract `rootfs.cpio.gz` `initramfs` file into a specified directory without clobbering my actual system's paths (remember `initramfs` contains a full system directory layout with `/usr`, `/dev` , `/bin` etc- you don't want to overrite your laptop's system files!):
31+
```
32+
mkdir out;
33+
zcat initramfs-lts | cpio -D ./out -idmv --no-absolute-filenames;
34+
ls -l rootfs.cpio.gz
35+
```
36+
Above, you'll not be able to inspect the contents of your built `initramfs`. Take a look at your init script!
37+
```
38+
less out/init
39+
``
40+
Perhaps you want to make a change (such as add an `echo` or debug statement to your `init` script.
41+
42+
#### How to I repack my `initramfs`?
43+
44+
We've come full circle, taking inspiration from [build-rootfs.sh](https://github.com/KarmaComputing/minimal-linux-boot-coreutils/blob/c64027b54b12d488f83cef75b5fbfee3d444e661/build-rootfs.sh#L7) we can re-backage our extracted `initramfs` using `cpio` and `gzip` over our `out` folder:
45+
46+
```
47+
cd out # make sure you're in your extracted initramfs folder
48+
find . | cpio -o -H newc | gzip > ../my-new-rootfs.cpio.gz
49+
```
50+
51+
<strike>TODO: add [iproute2](https://github.com/iproute2/iproute2) for minimal routing.</strike>
2252
2353
## Things you need to build
2454

0 commit comments

Comments
 (0)