summaryrefslogtreecommitdiff
path: root/experiments/03_20251130_leaving_initramfs
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/03_20251130_leaving_initramfs')
-rw-r--r--experiments/03_20251130_leaving_initramfs/Makefile52
-rw-r--r--experiments/03_20251130_leaving_initramfs/README32
-rw-r--r--experiments/03_20251130_leaving_initramfs/init23
-rwxr-xr-xexperiments/03_20251130_leaving_initramfs/run_qemu.sh3
4 files changed, 110 insertions, 0 deletions
diff --git a/experiments/03_20251130_leaving_initramfs/Makefile b/experiments/03_20251130_leaving_initramfs/Makefile
new file mode 100644
index 0000000..c55cc00
--- /dev/null
+++ b/experiments/03_20251130_leaving_initramfs/Makefile
@@ -0,0 +1,52 @@
+special_dirs := proc sys
+_special_dirs := $(patsubst %,initramfs/%,$(special_dirs))
+
+pkgs := $(wildcard pkgs/*.deb)
+pkgs_dirs := $(patsubst %.deb,%,$(pkgs))
+pkgs_content := $(patsubst %.deb,%/data.tar.xz,$(pkgs))
+pkgs_unpacked := $(patsubst pkgs/%/data.tar.xz,initramfs/%.unpack,$(pkgs_content))
+
+all : initrd.cpio.gz
+
+initrd.cpio.gz : clean_initramfs initramfs unpack $(_special_dirs) links initramfs/init
+ cd initramfs && find . | cpio -o -H newc | gzip > ../initrd.cpio.gz
+
+initramfs :
+ mkdir initramfs
+
+initramfs/init : init
+ cp $< $@
+ chmod 0755 $@
+
+unpack : $(pkgs_unpacked)
+
+initramfs/%.unpack : pkgs/%/data.tar.xz initramfs
+ tar xf $< -C initramfs
+
+$(_special_dirs) :
+ mkdir -p $@
+
+%/data.tar.xz : %.deb %
+ ar x $< --output "$(@D)"
+
+pkgs/% : pkgs/%.deb
+ mkdir $@
+
+links :
+ ln -s usr/bin initramfs/bin
+ ln -s usr/sbin initramfs/sbin
+ ln -s usr/lib initramfs/lib
+ ln -s usr/lib64 initramfs/lib64
+# ln -s bin/sh initramfs/init
+ ln -s /proc/mounts initramfs/etc/mtab
+
+clean_initramfs :
+ rm -rf initramfs
+
+clean : clean_initramfs
+ rm -rf $(pkgs_dirs)
+ rm initrd.cpio.gz
+
+.PHONY : all clean clean_initramfs unpack links
+.SECONDARY :
+.DEFAULT : all
diff --git a/experiments/03_20251130_leaving_initramfs/README b/experiments/03_20251130_leaving_initramfs/README
new file mode 100644
index 0000000..a458ac2
--- /dev/null
+++ b/experiments/03_20251130_leaving_initramfs/README
@@ -0,0 +1,32 @@
+Goal: boot into initramfs and switch to real system
+Status: success
+
+Added busybox to the mix, incredibely useful here
+
+init script will create new_root, mount tmpfs there and copy all the binaries
+from initramfs to it so we have something to work with. It'll also mount /proc,
+/dev and /sys. After that's done it'll switch root and run busybox shell as new
+PID 1.
+
+there's a /new_root/cake file created by the script to confirm that we indeed
+changed the root
+
+Useful resources:
+- https://github.com/torvalds/linux/blob/v4.17/Documentation/filesystems/ramfs-rootfs-initramfs.txt
+- https://www.kernel.org/doc/html/latest/admin-guide/initrd.html (obsolete but useful)
+
+Package list:
+busybox-static_1.37.0-7_amd64.deb
+dash_0.5.12-12_amd64.deb
+gcc-14-base_14.2.0-19_amd64.deb
+libblkid1_2.41.2-4_amd64.deb
+libc6_2.41-12_amd64.deb
+libc-bin_2.41-12_amd64.deb
+libgcc-s1_14.2.0-19_amd64.deb
+libmount1_2.41.2-4_amd64.deb
+libpcre2-8-0_10.47-2_amd64.deb
+libselinux1_3.9-4_amd64.deb
+libsmartcols1_2.41.2-4_amd64.deb
+mount_2.41.2-4_amd64.deb
+util-linux_2.41.2-4_amd64.deb
+
diff --git a/experiments/03_20251130_leaving_initramfs/init b/experiments/03_20251130_leaving_initramfs/init
new file mode 100644
index 0000000..27fcc61
--- /dev/null
+++ b/experiments/03_20251130_leaving_initramfs/init
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+echo "creating new root..."
+busybox mkdir /new_root
+busybox mount -t tmpfs tmpfs /new_root
+
+busybox cp -r bin etc init lib lib64 sbin usr var /new_root
+
+busybox mkdir /new_root/dev
+busybox mkdir /new_root/proc
+busybox mkdir /new_root/sys
+
+echo "mounting special filesystems..."
+busybox mount -t devtmpfs udev /new_root/dev
+busybox mount -t proc proc /new_root/proc
+busybox mount -t sysfs sysfs /new_root/sys
+
+echo "the cake is a lie!" > /new_root/cake
+
+echo "it's time to try 'switch_root' command and enter new_root"
+echo "look for /cake to verify it worked"
+
+exec busybox switch_root /new_root /bin/busybox sh
diff --git a/experiments/03_20251130_leaving_initramfs/run_qemu.sh b/experiments/03_20251130_leaving_initramfs/run_qemu.sh
new file mode 100755
index 0000000..211b281
--- /dev/null
+++ b/experiments/03_20251130_leaving_initramfs/run_qemu.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env sh
+
+qemu-system-x86_64 -kernel /boot/vmlinuz-$(uname -r) -initrd initrd.cpio.gz -nographic -append "root=/dev/sda console=ttyS0"