Skip to content

Instantly share code, notes, and snippets.

@xkou
Forked from tuxmartin/alpine-qemu-arm.sh
Last active January 11, 2022 10:08
Show Gist options
  • Save xkou/a5234c5cd8db7bd5418396f6ca62f900 to your computer and use it in GitHub Desktop.
Save xkou/a5234c5cd8db7bd5418396f6ca62f900 to your computer and use it in GitHub Desktop.
# https://wiki.ubuntu.com/ARM64/QEMU
# Install QEMU and the EFI image for QEMU:
$ sudo apt-get install qemu-system-arm qemu-efi
# Now you'll need to create pflash volumes for UEFI. Two volumes are required, one static one for the UEFI firmware, and another dynamic one to store variables. Both need to be exactly 64M in size.
$ dd if=/dev/zero of=flash0.img bs=1M count=64
$ dd if=/usr/share/qemu-efi/QEMU_EFI.fd of=flash0.img conv=notrunc
$ dd if=/dev/zero of=flash1.img bs=1M count=64
# Now you should be able to boot/ into a -arm64-uefi1.img type cloud image.
# For accelerated VMs (arm64 host w/ KVM support)
$ sudo qemu-system-aarch64 -enable-kvm -m 1024 -cpu host -M virt -nographic -pflash flash0.img -pflash flash1.img -drive if=none,file=vivid-server-cloudimg-arm64-uefi1.img,id=hd0 -device virtio-blk-device,drive=hd0 -netdev type=tap,id=net0 -device virtio-net-device,netdev=net0,mac=$randmac
# Note: For GICv3 systems, such as Cavium ThunderX, you must use QEMU from Ubuntu 16.04 or newer, and pass "-M virt,gic-version=3". Additionally, you must use a 15.10 (wily) or newer cloud image for guests.
For emulated VMs (e.g. x86 host)
$ sudo qemu-system-aarch64 -m 1024 -cpu cortex-a57 -M virt -nographic -pflash flash0.img -pflash flash1.img -drive if=none,file=vivid-server-cloudimg-arm64-uefi1.img,id=hd0 -device virtio-blk-device,drive=hd0 -netdev type=tap,id=net0 -device virtio-net-device,netdev=net0,mac=$randmac
References
# mkpasswd --method=SHA-512 --rounds=4096
profile(){
prikey=`cat ~/.ssh/id_rsa.pub`
cat > x.txt << EOF
#cloud-config
users:
- name: xuke
ssh-authorized-keys:
- $prikey
sudo: ['ALL=(ALL) NOPASSWD:ALL']
passwd: \$6\$rounds=4096\$CAf6H4TGAWvKV\$24SQh0zjDta.8dpDOlmiLqn87VSnkl5.APQFs.yh3rqqMAyWN1YDRd47Haa4uILfCjbUaOSf9399dPrO56cAd.
lock_passwd: false
ssh_pwauth: True
groups: sudo
shell: /bin/bash
apt:
primary:
- arches: [default]
uri: https://mirrors.aliyun.com/ubuntu/
EOF
rm cloud.img
cloud-localds --disk-format qcow2 cloud.img x.txt
cat x.txt
rm x.txt
}
download(){
wget https://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.fd -O QEMU_EFI.fd
wget https://cloud-images.ubuntu.com/focal/20211212/focal-server-cloudimg-arm64.img -O focal-server-cloudimg-arm64.img -c
}
create(){
apt install -y whois
download
profile
}
run(){
qemu-system-aarch64 \
-smp 2 \
-m 4096 \
-M virt \
-cpu cortex-a57 \
-bios QEMU_EFI.fd \
-nographic \
-device virtio-blk-device,drive=image \
-drive if=none,id=image,file=focal-server-cloudimg-arm64.img \
-device virtio-blk-device,drive=cloud \
-drive if=none,id=cloud,file=cloud.img \
-device virtio-net-device,netdev=user0 -netdev user,id=user0,hostfwd=tcp::2222-:22
# -device virtio-net-device,netdev=user0 -netdev user,id=user0,hostfwd=tcp::2222-:22
}
inittap(){
ip tuntap add tap0 mode tap
brctl addif br1 tap0
}
$1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment