# Run Clang/LLVM Compiled Arm64 Linux Kernel on QEMU Virt with Debian Rootfs [toc] ## Enviroment Setup 1. Initramfs, Linux Kernel, external modules and QEMU Please see [Run Clang/LLVM Compiled Arm64 Linux Kernel on QEMU Virt with BusyBox Initramfs](https://hackmd.io/@Ming-Jun/SJTBZOA0A). 2. Install packages ```shell! $ sudo apt-get install binfmt-support debootstrap qemu-user-static ``` 3. Install qemu-binfmt for WSL2 according to ref[1] ```shell! $ wget https://github.com/qemu/qemu/raw/master/scripts/qemu-binfmt-conf.sh $ chmod +x qemu-binfmt-conf.sh $ sudo ./qemu-binfmt-conf.sh --qemu-path /usr/bin --qemu-suffix -static --debian $ sudo update-binfmts --import qemu-aarch64 $ cat /proc/sys/fs/binfmt_misc/qemu-aarch64 enabled interpreter /usr/bin/qemu-aarch64-static flags: offset 0 magic 7f454c460201010000000000000000000200b700 mask ffffffffffffff00fffffffffffffffffeffffff ``` ## Arm64 Debian Rootfs 1. Create rootfs ```shell! $ export MY_ROOTFS_DIR=/path/to/your/directory $ echo ${MY_ROOTFS_DIR} /path/to/your/directory $ sudo debootstrap \ --arch=arm64 \ --foreign \ bookworm \ ${MY_ROOTFS_DIR} \ http://ftp.debian.org/debian/ $ sudo cp /usr/bin/qemu-aarch64-static ${MY_ROOTFS_DIR}/usr/bin $ sudo chroot ${MY_ROOTFS_DIR} ./debootstrap/debootstrap --second-stage I: Installing core packages... I: Unpacking required packages... ... I: Configuring required packages... ... I: Unpacking the base system... ... I: Configuring the base system... ... I: Base system installed successfully. ``` 2. Post process ```shell! $ sudo chroot ${MY_ROOTFS_DIR} passwd -d root $ sudo chroot ${MY_ROOTFS_DIR} apt clean $ sudo rm -f ${MY_ROOTFS_DIR}/usr/bin/qemu-aarch64-static ``` 3. Create rootfs image with `-L my_label` ```shell! $ sudo mke2fs -t ext4 -L my_label -d ${MY_ROOTFS_DIR} debian_rootfs.ext4 512M $ sudo chmod 666 debian_rootfs.ext4 ``` ## Modify Busybox Initramfs 1. Create `/your/path/to/busybox-1.36.1/_install/init_switch_root` with the following content ```shell!= #!/bin/sh # Busybox init script for switching root to another rootfs /bin/echo "Mounting /proc" /bin/mkdir -p /proc /bin/mount -t proc proc /proc /bin/echo "Mounting /sys" /bin/mkdir -p /sys /bin/mount -t sysfs sysfs /sys /bin/echo "Mounting /dev" /bin/mkdir -p /dev /bin/mount -t tmpfs tmpfs /dev /bin/echo "Mounting /tmp" /bin/mkdir -p /tmp /bin/mount -t tmpfs tmpfs /tmp # Follow busybox/doc/mdev.txt to set up mdev /bin/echo /sbin/mdev > /proc/sys/kernel/hotplug /sbin/mdev -s # Search the first block device with LABEL="my_label" NEW_ROOT_DEV=$(/sbin/blkid | /bin/grep 'LABEL="my_label"' | /bin/sed 's/:.*//') if [ -n "${NEW_ROOT_DEV}" ]; then NEW_ROOT="/mnt/new_root" NEW_INIT="/sbin/init" /bin/echo "Mounting new rootfs" /bin/mkdir -p ${NEW_ROOT} /bin/mount "${NEW_ROOT_DEV}" "${NEW_ROOT}" /bin/echo "Switching root to ${NEW_ROOT_DEV}" /bin/echo "" > /proc/sys/kernel/hotplug exec /sbin/switch_root -c /dev/console "${NEW_ROOT}" "${NEW_INIT}" "$@" fi exec /bin/sh ``` 2. Update initramfs image ```shell! $ cd /your/path/to/busybox-1.36.1/_install $ rm -f init $ ln -s init_switch_root init $ find . | cpio --quiet -H newc -o | gzip -9 -n > ../initramfs.cpio.gz ``` ## QEMU 1. Run with `-drive file` option ```shell! $ qemu-system-aarch64 \ -machine virt \ -m size=1024M \ -cpu cortex-a57 \ -smp 2 \ -nographic \ -initrd /your/path/to/busybox-1.36.1/initramfs.cpio.gz \ -drive file=/your/path/to/debian_rootfs.ext4,format=raw,if=virtio \ -kernel /your/path/to/linux/arch/arm64/boot/Image \ --append "console=ttyAMA0" ``` 2. Boot to debian rootfs  ## Reference 1. [Install qemu-binfmt on WSL2](https://forums.developer.nvidia.com/t/installation-fails-on-wsl/274008/23?page=2)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up