linux kernel + custom initramfs + init script

prologue

im gonna document a random idea i just had out of boredom. ive been recently making a package manager, so that got me thinking: "how am i even gonna test or use it?". you see, the problem is, that im currently on NixOS :D. if you did not know, on this distro, you cannot run arbitrary binaries, nor does it even have the common linux filesystem structure of /usr/bin and so on.

so i tried to find a distro that does not come with a package manager, a "bring-your-own-package-manager" distro. ideally it would have OpenRC and glibc. nothing really caught my eye, though admittedly i did not search for long, before i got this idea.

do i know what im doing?

no

1. get the linux kernel

im going with vanilla flavour, stable 7.2.8 version

1.1. download the kernel source

  • current working directory: ... (any)
  • you will need: git
git clone --depth 1 --branch v7.2.8 git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git linux-7.2.8
cd linux-7.2.8/

1.2. configure the kernel

  • current working directory: .../linux-7.2.8/
  • you will need: gnumake, bash, binutils, gcc, flex, bison
make tinyconfig

scripts/config --enable 64BIT

scripts/config --enable BLK_DEV_INITRD
scripts/config --enable RD_GZIP
scripts/config --enable BINFMT_ELF
scripts/config --enable BINFMT_SCRIPT

scripts/config --enable TTY
scripts/config --enable SERIAL_8250
scripts/config --enable SERIAL_8250_CONSOLE

make olddefconfig

1.3. compile the kernel

  • current working directory: .../linux-7.2.8/
  • you will need: gnumake, binutils, gcc, bc, elfutils OR libelf
make --jobs=$(nproc --all)

2. prepare the filesystem

this will be what the kernel boots into

2.1. create the filesystem

  • current working directory: .../linux-7.2.8/
  • you will need: coreutils, wget
mkdir --parents rootfs/bin rootfs/dev
cd rootfs/

wget --output-document=bin/busybox https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox
chmod 0755 bin/busybox

ln --symbolic busybox bin/sh
ln --symbolic busybox bin/sleep

sudo mknod --mode=0622 dev/console c 5 1
cat > init << 'EOF'
#!/bin/sh
echo 'it works!'
sleep 1000000000
EOF
chmod 0755 init

2.2. archive the filesystem

  • current working directory: .../linux-7.2.8/rootfs/
  • you will need: find, cpio, gzip
find . -print0                          \
    | cpio --null --create --format=newc \
    | gzip --best > ../initramfs.cpio.gz
cd ..

3. run it in a virtual machine

  • current working directory: .../linux-7.2.8/
  • you will need: qemu
qemu-system-x86_64               \
    -kernel arch/x86/boot/bzImage \
    -initrd initramfs.cpio.gz      \
    -append 'console=ttyS0'         \
    -nographic

you should see it works! in your terminal. press Ctrl+A and then X to exit qemu

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论