Last active
May 1, 2024 12:23
-
-
Save davlgd/a34d07c767ea4ca923964b31c6d83096 to your computer and use it in GitHub Desktop.
Minimal Linux script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# For this script you'll need gcc, gzip make, qemu, tar, wget | |
# Learn more on my blog: https://labs.davlgd.fr/posts/2024-05-whats-a-minimal-linux/ | |
# Get and compile the kernel | |
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.8.8.tar.xz | |
tar xf linux-6.8.8.tar.xz | |
cd linux-6.8.8/ | |
make defconfig | |
make -j$(nproc) | |
# Get, decompress and compile BusyBox | |
wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2 | |
tar xf busybox-1.36.1.tar.bz2 | |
cd busybox-1.36.1/ | |
# Configure and set CONFIG_STATIC=y to get a standalone binary | |
make defconfig | |
sed -i 's/# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config | |
make -j$(nproc) | |
# Create initramfs folder, install BusyBox in it | |
mkdir ../initramfs | |
make install CONFIG_PREFIX=../initramfs | |
# Create needed folders and init script | |
cd ../initramfs/ | |
mkdir {dev,proc,sys} | |
cat > init <<EOF | |
#!/bin/sh | |
mount -t devtmpfs none /dev | |
mount -t proc none /proc | |
mount -t sysfs none /sys | |
echo "Welcome to my minimal Linux system!" | |
uname -a | |
# Understand this command: https://busybox.net/FAQ.html#job_control | |
exec setsid cttyhack /bin/sh | |
EOF | |
chmod +x init | |
# Create initramfs compressed archive | |
find . | cpio -H newc -o | gzip > ../initramfs.cpio.gz | |
cd .. | |
# Launch the emulated system with minimal Linux | |
qemu-system-x86_64 --enable-kvm -cpu host -kernel arch/x86/boot/bzImage -initrd initramfs.cpio.gz -append "console=ttyS0 panic=1" -nographic -no-reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment