Support #929
Updated by Daniel Curtis almost 3 years ago
This is a guide on virtualizing an ARM based operating system, in this case Raspbian for the Raspberry Pi 3, on an x86_64 Arch Linux system. h2. Prepare the environment * Make sure the system is up to date: <pre> sudo pacman -Syu </pre> * Install a few dependencies: <pre> sudo pacman -S git base-devel wget </pre> h2. Prepare Disk Image * Create the guest drive: <pre> dd if=/dev/zero of=~/raspi.img bs=1M count=16384 </pre> * Format the guest drive: <pre> fdisk ~/raspi.img </pre> #* Basic format: <pre> n p 1 <enter> +200M n p <enter> <enter> <enter> w </pre> * Download the latest raspberry pi OS image: <pre> mkdir ~/raspios && cd ~/raspios wget https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2022-01-28/2022-01-28-raspios-bullseye-armhf-lite.zip unzip 2022-01-28-raspios-bullseye-armhf-lite.zip </pre> * Populate the guest drive: <pre> mkdir ~/src_boot && mkdir ~/src_root && mkdir ~/dst_boot && mkdir dst_root sudo losetup -f --show ~/raspi.img sudo partx -a /dev/loop0 sudo mkfs.vfat /dev/loop0p1 sudo mkfs.ext4 /dev/loop0p2 sudo mount /dev/loop0p1 ~/dst_boot sudo mount /dev/loop0p2 ~/dst_root sudo losetup -f -o 4194304 --show ~/raspbian/2022-01-28-raspios-bullseye-armhf-lite.img sudo mount /dev/loop1 ~/src_boot sudo losetup -f -o 272629760 --show ~/raspios/2022-01-28-raspios-bullseye-armhf-lite.img sudo mount /dev/loop2 ~/src_root sudo cp -r ~/src_boot/*.* ~/dst_boot/ sudo cp -r ~/src_root/* ~/dst_root/ </pre> * Edit the guest drive fstab: <pre> sudo nano ~/root/etc/fstab </pre> #* And change the following: <pre> /dev/sda1 /boot vfat defaults 0 2 /dev/sda2 / ext4 defaults,noatime 0 1 </pre> * Unmount the guest drive: <pre> sudo umount ~/src_boot sudo umount ~/src_root sudo umount ~/dst_boot sudo umount ~/dst_root sudo losetup -d /dev/loop2 sudo losetup -d /dev/loop1 sudo losetup -d /dev/loop1 </pre> h2. Install Qemu * Install qemu and the extra architectures: <pre> pacman -S git qemu qemu-arch-extra wget </pre> * Download prebuilt kernels: <pre> mkdir ~/git && cd ~/git git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git </pre> h3. Run Qemu * Run the newly built kernel with the latest raspbian: <pre> qemu-system-arm -kernel ~/git/qemu-rpi-kernel/kernel-qemu-5.10.63-bullseye -cpu arm1176 1176 -m 2048 -M versatilepb -dtb ~/git/qemu-rpi-kernel/versatile-pb-bullseye-5.10.63.dtb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -drive format=raw,file=/media/winshare/raspi.img -net nic,macaddr=20:20:20:67:89:0a -net user,hostfwd=tcp::5022-:22 -hda ~/raspi.img -redir tcp:5022::22 -no-reboot </pre> * Log in as the user *pi* with the password *raspberry* * Configure the pi to your use case: <pre> sudo raspi-config </pre> #* Go to *Interfacing* and enable SSH. #* Change *Localization*. h2. Compiling Latest kernel h3. Install ARM toolchain: * Clone the toolchain repo (using @--depth 1@ will decrease the overall download size by 400MB): <pre> mkdir ~/git && cd ~/git git clone --depth 1 https://github.com/raspberrypi/tools.git </pre> * Add the tools to your path: <pre> echo 'export PATH=$PATH:~/git/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin' >> ~/.bash_profile </pre> * Source the @.bash_profile@: <pre> source ~/.bash_profile </pre> h3. Compile Kernel * Download the qemu-rpi-kernel repo: <pre> cd ~/git git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git </pre> * Clone the specific kernel version to use: <pre> cd qemu-rpi-kernel/tools git clone -b rpi-5.10.y --depth 1 https://github.com/raspberrypi/linux.git </pre> * Modify the kernel compile script to use Arch commands: <pre> sed -i.bak -e's/sudo\ apt-get\ update/sudo\ pacman\ -Syu/g' -e's/sudo\ apt-get\ install\ git\ libncurses5-dev\ gcc-arm-linux-gnueabihf/sudo\ pacman\ -S\ git/g' build-kernel-qemu </pre> * Edit the build-kernel-qemu script: <pre> vi build-kernel-qemu </pre> #* And comment out all @COMMIT@ versions except for the desired commit, and also comment out the @exit@ command before the @make@ commands: <pre> #COMMIT=6820d0cbec64cfee481b961833feffec8880111e #COMMIT=raspberrypi-kernel_1.20171029-1 #COMMIT="" COMMIT=rpi-5.10.y #exit </pre> * Run the build script: <pre> ./build-kernel-qemu </pre> h2. Resources * https://azeria-labs.com/emulate-raspberry-pi-with-qemu/ * http://graznik.de/posts/emulate-raspberry-pi-with-qemu/ * http://downloads.raspberrypi.org/raspbian/images/raspbian-2018-03-14/ * https://github.com/raspberrypi/linux/releases * https://github.com/dhruvvyas90/qemu-rpi-kernel