Skip to content

Instantly share code, notes, and snippets.

@scrouthtv
Created October 15, 2021 16:10
Show Gist options
  • Save scrouthtv/96aa561846a58d5a925da7d52b76c8a7 to your computer and use it in GitHub Desktop.
Save scrouthtv/96aa561846a58d5a925da7d52b76c8a7 to your computer and use it in GitHub Desktop.
Creating an aarch64 VM for qemuhost, with Arch Linux guest
/*
* This document is provided to the public domain under the
* terms of the WTFPL license.
*/
This is
This is a combination of
- [How to boot Arch Linux ARM in QEMU (patched for M1)](https://gist.github.com/thalamus/561d028ff5b66310fac1224f3d023c12) - thanks to Avatar
Will Tisdale
- [How to launch ARM aarch64 VM with QEMU from scratch. ](https://futurewei-cloud.github.io/ARM-Datacenter/qemu/how-to-launch-aarch64-vm/) - thanks to Rob Foley
slightly adapted to Arch Linux.
0) Prerequisites
* packages `qemu`, `qemu-arch-extra`, `multipath-tools` (for `kpartx`)
* aur package `edk2-avmf`, which provides the firmware for aarch64
* [ArchLinuxARM image for generic aarch64](https://archlinuxarm.org/platforms/armv8/generic)
1) Create the root image
* Create the two flash images:
```bash
~ dd if=/dev/zero of=flash1.img bs=1M count=64
~ dd if=/dev/zero of=flash0.img bs=1M count=64
~ dd if=/usr/share/qemu-efi-aarch64/QEMU_EFI.fd of=flash0.img conv=notrunc
```
* Create the root image, partition + format it (if you don't plan on installing GUI / ... 8G is more than enough):
```bash
~ qemu-img create arch.img 8G
~ sudo fdisk arch.img
```
Now this part is the same as in Will's post:
- g (to create a new GPT partition table)
- n (to create a new partition), then enter twice, then +200M and enter
- t (to change the type), then 1 for EFI System Partition
- n and enter three times
- w to write changes and exit
```
~ sudo kpartx arch.img
```
This will tell you the mapper devices for the image, mkfs the 200M one with vfat, the other one with ext4.
* Copy the base system
```
~ mkdir root
~ sudo mount <fs with ext4> root
~ mkdir root/boot
~ sudo mount <fs with vfat> root/boot
~ bsdtar -xpf ArchLinuxARM-aarch64-latest.tar.gz -C root (x = extract, p = permissions, f = file)
```
* Setup the base system:
```
~ blkid
```
will show you the UUID for the two mapped fs, enter them into root/etc/fstab:
```
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX / ext4 defaults 0 0
UUID=XXXX-XXXX /boot vfat defaults 0 0
```
Edit root/boot/startup.nsh to contain only this line:
```
Image root=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX rw initrd=\initramfs-linux.img loglevel=4
```
You can freely add/remove kernel parameters here.
* Unmount everything, (optionally) convert to qcow2:
```
sudo umount -r root
sudo kpartx -d arch.img
sync
qemu-img convert -O qcow2 arch.img arch.qcow2
```
* Create a `launch.sh` with this contents:
```
qemu-system-aarch64 \
-nographic -machine virt,gic-version=max -m 8G -cpu max -smp 4 \
-netdev user,id=vnet,hostfwd=:127.0.0.1:0-:22 \
-device virtio-net-pci,netdev=vnet \
-drive file=arch.qcow2,format=qcow2,if=none,id=drive0,cache=writeback \
-device virtio-blk,drive=drive0,bootindex=0 \
-drive file=flash0.img,format=raw,if=pflash \
-drive file=flash1.img,format=raw,if=pflash
```
And run it.
There's a lot of space for optimizations, as
- the system's performance is very poor
- lot of debug info spammeed at startup
More info:
- Arch wiki
- Arch Arm Forum
- ...
@gabrielmoura
Copy link

sudo kpartx -av arch.img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment