Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PauloMigAlmeida/c594f21c4e3a75158630782beba1996b to your computer and use it in GitHub Desktop.
Save PauloMigAlmeida/c594f21c4e3a75158630782beba1996b to your computer and use it in GitHub Desktop.
User Mode Linux (UML) Kernel With Alpine Linux OS
How to compile the linux kernel, launch it as a process and boot into Alpine linux
#Command + Shift + 5
#Host -> Docker GCC -> UML w/ Alpine
# create and mount the filesystem
sudo rm -rf ~/workspace/root_fs_um
mkdir -p ~/workspace/root_fs_um
cd ~/workspace/root_fs_um
dd if=/dev/zero of=root_fs bs=1M count=1024
mkfs.ext4 -L ALPINE_ROOT root_fs
mkdir tmp_mount
sudo su
mount -t ext4 -o loop root_fs tmp_mount
curl -LO https://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/apk-tools-static-2.10.6-r0.apk
tar -zxvf apk-tools-static-2.10.6-r0.apk -C tmp_mount
tmp_mount/sbin/apk.static --repository https://dl-cdn.alpinelinux.org/alpine/v3.9/main --update-cache --allow-untrusted --root tmp_mount --initdb add alpine-base
ls -lah tmp_mount
exit
# Build the linux kernel and add kernel modules to filesystem
# reproducible kernel stuff
KBF="KBUILD_BUILD_TIMESTAMP=1980-01-01 KBUILD_BUILD_USER=user KBUILD_BUILD_HOST=host KBUILD_BUILD_VERSION=1 ARCH=um SUBARCH=x86_64"
OUT=gcc_um
# Download and configure the linux kernel
cd worskpace/linux-linus-branch
make $KBF O=$OUT defconfig
# feel free to add SLIRP (nat through host to get to internet)
# make $KBF O=$OUT menuconfig
make $KBF O=$OUT -j$(nproc)
make $KBF O=$OUT modules
sudo make $KBF O=$OUT modules_install INSTALL_MOD_PATH=~/workspace/root_fs_um/tmp_mount
# update the filesystem table
foo@gcc:~/linux-5.12.4$ echo "LABEL=ALPINE_ROOT / ext4 defaults 0 0" > /mnt/uml/etc/fstab
foo@gcc:~/linux-5.12.4$ echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" > /mnt/uml/etc/apk/repositories
foo@gcc:~/linux-5.12.4$ echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /mnt/uml/etc/apk/repositories
foo@gcc:~/linux-5.12.4$ echo "nameserver 8.8.8.8" > /mnt/uml/etc/resolv.conf
# It is critical to unmount the filesystem before starting the kernel
umount ~/workspace/root_fs_um/tmp_mount
# open network proxy on host
foo@gcc:~/linux-5.12.4$ export TMPDIR=/tmp
foo@gcc:~/linux-5.12.4$ slirp > /dev/null 2>&1 &
foo@gcc:~/linux-5.12.4$ bg %1
# boot user mode linux kernel with alpine filesystem
$OUT/linux ubda=~/workspace/root_fs_um/root_fs rw mem=64M init=/bin/sh rootfstype=ext4 eth0=slirp,,/usr/bin/slirp TERM=linux quiet
# configure User Mode Linux networking etc
/ # PS1="[\u@uml:\w ] $ "
[root@uml:/ ] $ mount -t proc proc proc/
[root@uml:/ ] $ mount -t sysfs sys sys/
[root@uml:/ ] $ ifconfig eth0 10.0.2.14 netmask 255.255.255.240 broadcast 10.0.2.15
[root@uml:/ ] $ route add default gw 10.0.2.2
[root@uml:/ ] $ nslookup google.com
[root@uml:/ ] $ apk update
[root@uml:/ ] $ apk search curl
[root@uml:/ ] $ apk add curl
[root@uml:/ ] $ curl https://www.reddit.com/r/worldnews.json -H "User-agent: uml"
[root@uml:/ ] $ halt -f # exit gracefully
# close network proxy on host
foo@gcc:~/linux-5.12.4$ kill %1 # close slirp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment