Support #929
Emulate Raspberry Pi OS on x86_64 Arch Linux
Description
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.
Prepare the environment¶
- Make sure the system is up to date:
sudo pacman -Syu
- Install a few dependencies:
sudo pacman -S git base-devel wget
Prepare Disk Image¶
- Create the guest drive:
dd if=/dev/zero of=~/raspi.img bs=1M count=16384
- Format the guest drive:
fdisk ~/raspi.img
- Basic format:
n p 1 <enter> +200M n p <enter> <enter> <enter> w
- Basic format:
- Download the latest raspberry pi OS image:
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
- Populate the guest drive:
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/
- Edit the guest drive fstab:
sudo nano ~/root/etc/fstab
- And change the following:
/dev/sda1 /boot vfat defaults 0 2 /dev/sda2 / ext4 defaults,noatime 0 1
- And change the following:
- Unmount the guest drive:
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
Install Qemu¶
- Install qemu and the extra architectures:
pacman -S git qemu qemu-arch-extra wget
- Download prebuilt kernels:
mkdir ~/git && cd ~/git git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
Run Qemu¶
- Run the newly built kernel with the latest raspbian:
qemu-system-aarch64 -kernel ~/git/qemu-rpi-kernel/kernel-qemu-5.10.63-bullseye -cpu arm1176 -M raspi3b -m 1024 -dtb ~/git/qemu-rpi-kernel/native-emulation/dtbs/bcm2711-rpi-4-b.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 -no-reboot
- Log in as the user pi with the password raspberry
- Configure the pi to your use case:
sudo raspi-config
- Go to Interfacing and enable SSH.
- Change Localization.
Compiling Latest kernel¶
Install ARM toolchain:¶
- Clone the toolchain repo (using
--depth 1
will decrease the overall download size by 400MB):mkdir ~/git && cd ~/git git clone --depth 1 https://github.com/raspberrypi/tools.git
- Add the tools to your path:
echo 'export PATH=$PATH:~/git/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin' >> ~/.bash_profile
- Source the
.bash_profile
:source ~/.bash_profile
Compile Kernel¶
- Download the qemu-rpi-kernel repo:
cd ~/git git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
- Clone the specific kernel version to use:
cd qemu-rpi-kernel/tools git clone -b rpi-5.10.y --depth 1 https://github.com/raspberrypi/linux.git
- Modify the kernel compile script to use Arch commands:
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
- Edit the build-kernel-qemu script:
vi build-kernel-qemu
- And comment out all
COMMIT
versions except for the desired commit, and also comment out theexit
command before themake
commands:#COMMIT=6820d0cbec64cfee481b961833feffec8880111e #COMMIT=raspberrypi-kernel_1.20171029-1 #COMMIT="" COMMIT=rpi-5.10.y #exit
- And comment out all
- Run the build script:
./build-kernel-qemu