Skip to content

Instantly share code, notes, and snippets.

@rgl
Last active December 23, 2023 02:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rgl/b02c24f9eb1b4bdb4ac6f970d4bfc885 to your computer and use it in GitHub Desktop.
Save rgl/b02c24f9eb1b4bdb4ac6f970d4bfc885 to your computer and use it in GitHub Desktop.
run emulated arm under qemu

This will show how to run an emulated arm64 virtual machine under qemu.

It first shows how to launch a typical amd64 virtual machine to make sure we have cloud-init working.

Then it shows how to launch the arm64 (aka aarch64) virtual machine.

NB In my humble i3-3245 amd64 host this is way too slow to run anything useful as it takes about 6m to allow you to finally login, and after that, its slow too. You are really better off with a proper physical arm64 machine, like:

In a Ubuntu bash shell:

sudo apt-get install -y cloud-image-utils
wget http://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
wget http://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img

cat >cloud-init-user-data.yml <<EOF
#cloud-config
hostname: test
timezone: Europe/Lisbon
# NB this password is for the default ubuntu user.
password: HeyH0Password
chpasswd:
  expire: false
ssh_pwauth: True
# NB the runcmd output is written to journald and /var/log/cloud-init-output.log.
runcmd:
  - echo '************** DONE RUNNING CLOUD-INIT **************'
EOF
cloud-localds cloud-init-data.iso cloud-init-user-data.yml

#
# test with an amd64 virtual machine.
#
cp /usr/share/OVMF/OVMF_CODE.fd firmware-code-amd64.fd
cp /usr/share/OVMF/OVMF_VARS.fd firmware-vars-amd64.fd
cp focal-server-cloudimg-amd64.img disk-amd64.img
qemu-system-x86_64 \
  -name amd64 \
  -machine q35,accel=kvm \
  -cpu host \
  -smp 2 \
  -m 2g \
  -k pt \
  -nographic \
  -drive if=pflash,file=firmware-code-amd64.fd,format=raw,readonly \
  -drive if=pflash,file=firmware-vars-amd64.fd,format=raw \
  -drive if=virtio,file=disk-amd64.img,cache=unsafe,discard=unmap,id=hd0 \
  -drive if=virtio,file=cloud-init-data.iso,media=cdrom,cache=unsafe,readonly,id=cc \
  -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  -device virtio-net-pci,netdev=net0 \
  -device virtio-rng-pci,rng=rng0 \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -qmp unix:amd64.socket,server,nowait
echo info qtree | qmp-shell -H amd64.socket
ssh ubuntu@localhost -p 2222
sudo su -l
uname -a
cat /etc/os-release
cat /proc/cpuinfo
timedatectl
lsblk -x KNAME -o KNAME,SIZE,MOUNTPOINT,TRAN,SUBSYSTEMS,FSTYPE,UUID,LABEL,MODEL,SERIAL
mount | grep ^/dev
df -h
lspci -v
find /var/lib/cloud/instances -type f
less /var/log/cloud-init-output.log
less /var/log/cloud-init.log
cloud-init analyze blame | less


#
# test with an arm64/aarch64 virtual machine.
#
cp /usr/share/AAVMF/AAVMF_CODE.fd firmware-code-arm64.fd
cp /usr/share/AAVMF/AAVMF_VARS.fd firmware-vars-arm64.fd
cp focal-server-cloudimg-arm64.img disk-arm64.img
# NB in my humble machine (i3-3245) emulating arm64 is very slow
#    it takes several minutes to the login prompt AND then
#    it takes several minutes to apply the cloud-init settings
#    that set the ubuntu user password, only then (~6m) you are
#    able to login.
qemu-system-aarch64 \
  -name arm64 \
  -machine virt \
  --accel tcg,thread=multi \
  -cpu cortex-a57 \
  -smp 2 \
  -m 2g \
  -k pt \
  -nographic \
  -drive if=pflash,file=firmware-code-arm64.fd,format=raw,readonly \
  -drive if=pflash,file=firmware-vars-arm64.fd,format=raw \
  -drive if=virtio,file=disk-arm64.img,cache=unsafe,discard=unmap,id=hd0 \
  -drive if=virtio,file=cloud-init-data.iso,media=cdrom,cache=unsafe,readonly,id=cc \
  -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  -device virtio-net-pci,netdev=net0 \
  -device virtio-rng-pci,rng=rng0 \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -qmp unix:arm64.socket,server,nowait
# NB if you need USB add something like:
#       -device nec-usb-xhci,id=usb0 \
#       -device usb-kbd,bus=usb0.0 \
#       -device usb-tablet,bus=usb0.0 
echo info qtree | qmp-shell -H arm64.socket
ssh ubuntu@localhost -p 2222

NB qemu aarch64 does not have a display by default; it only has -device virtio-gpu-pci, but that is not supported by Ubuntu.

Reference

@carenas
Copy link

carenas commented Mar 18, 2022

-device ramfb if you want a display should also work with Ubuntu

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