Support #929
Updated by Daniel Curtis over 6 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>
pacman -Syu
</pre>
* Install a few dependencies:
<pre>
pacman -S git base-devel
</pre>
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>
h2. Install Qemu
* Install qemu and the extra architectures:
<pre>
pacman -S git qemu qemu-arch-extra wget
</pre>
h3. Compile Latest 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 raspberrypi-kernel_1.20180313-1 --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=raspberrypi-kernel_1.20180313-1
#exit
</pre>
* Run the build script:
<pre>
./build-kernel-qemu
</pre>
h2. Prepare Raspbian
* Download the latest raspbian image (1.7G):
<pre>
mkdir ~/raspbian && cd ~/raspbian
wget http://downloads.raspberrypi.org/raspbian/images/raspbian-2018-03-14/2018-03-13-raspbian-stretch.zip
unzip 2018-03-13-raspbian-stretch.zip
</pre>
* Show the partition layout of
<pre>
fdisk -l 2018-03-13-raspbian-stretch.img
</pre>
#* _Example output_:
<pre>
Disk 2018-03-13-raspbian-stretch.img: 4.6 GiB, 4949278720 bytes, 9666560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x15ca46a5
Device Boot Start End Sectors Size Id Type
2018-03-13-raspbian-stretch.img1 8192 93802 85611 41.8M c W95 FAT32 (
2018-03-13-raspbian-stretch.img2 98304 9666559 9568256 4.6G 83 Linux
</pre>
* Find the sector where the filesystem, 2018-03-13-raspbian-stretch.img2, starts at (sector 98304). Multiply the sector start value by 512, in this case 98304 * 512 = 50331648 bytes.
* Mount the rasbian image at offset 50331648:
<pre>
mkdir -p /media/raspbian
mount -v -o offset=50331648 -t ext4 ~/raspbian/2018-03-13-raspbian-stretch.img /media/raspbian
</pre>
* Edit the ld.so.preload file and comment out all visible lines
<pre>
nano /media/raspbian/etc/ld.so.preload
</pre>
h3. Run Qemu
* Run the newly built kernel with the latest raspbian:
<pre>
qemu-system-arm -kernel ~/git/qemu-rpi-kernel/qemu-kernel-4.9.80 -cpu arm1176 -m 256 -M versatilepb -dtb /git/qemu-rpi-kernel/versatile-pb.dtb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -hda ~/raspbian/2018-03-13-raspbian-stretch.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. Resources
* https://azeria-labs.com/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