Install Alpine armv7 under qemu

2021-07-31

base script is created by Carlo Landmeter

install using iso image

#!/bin/sh

TMPDIR="."
DISK=armv7.vm.img

cdromiso="alpine-virt-210730-armv7.iso"

if [ ! -f $cdromiso ]; then
	wget -P $TMPDIR "https://dev.alpinelinux.org/~mps/$cdromiso"
fi

# efi bios is taken from debian because alpine doesn't have it for arm32
debefi="qemu-efi-arm_2021.05-1_all.deb"
if [ ! -f "AAVMF32_CODE.fd" ]; then
  mkdir tmp
  cd tmp/
  wget http://ftp.de.debian.org/debian/pool/main/e/edk2/$debefi
  ar x $debefi
  tar xvf data.tar.xz ./usr/share/AAVMF/AAVMF32_CODE.fd
  tar xvf data.tar.xz ./usr/share/AAVMF/AAVMF32_VARS.fd
  mv ./usr/share/AAVMF/AAVMF32_CODE.fd ../
  mv ./usr/share/AAVMF/AAVMF32_VARS.fd ../
  cd ..
  rm -rf tmp
fi

: ${MEMORY:=2048}

QEMU="qemu-system-arm -accel tcg,thread=multi"
CPU="-cpu cortex-a15 -smp cores=4"
MACHINE="-machine virt"
DISKSIZE="4G"

if [ ! -f "$DISK" ]; then
  echo "Creating qemu qcow2 disk"
  qemu-img create -f qcow2 "$DISK" $DISKSIZE
fi

echo "Booting Alpine linux armv7 iso - qemu"

$QEMU \
  $MACHINE \
  $CPU \
  -m $MEMORY \
  -bios AAVMF32_CODE.fd \
  -cdrom $cdromiso \
  -drive format=qcow2,file=$DISK \
  -nographic \
  -nic user,model=virtio \
  -rtc base=utc,clock=host \
  $OPTS

boot take some time, about 60-120 on intel I3 (4 cores) with 4GB RAM and on SSD

when it boots login as root and run setup-alpine

select vdb device where to install (and sys mode imo)

when install is finished do the next steps

mount /dev/vdb3 /mnt/
vi /etc/apk/repositories
(remove comment to enable community repo)
apk update
apk fetch linux-edge-virt
apk --root /mnt add linux-edge-virt-5.13.6-r0.apk
### 'apk add --root /mnt linux-edge-virt' doesn't work for some strange reason
vi /mnt/boot/grub/grub.cfg
change linux-virt with linux-edge-virt and initramfs-virt with initramfs-edge-virt in menuentry section
umount /mnt/
poweroff
 

boot installed with next command (ofc, parameters can be changed according needs)

# these could be changed to tweak VM
QEMU="qemu-system-arm -accel tcg,thread=multi"
CPU="-cpu cortex-a15 -smp cores=4"
MACHINE="-machine virt"
MEMORY=2048

DISK=armv7.vm.img
VMDRIVE="-drive if=virtio,id=hd,format=qcow2,file=$DISK"
# uncomment next line if need for disk to appear as /dev/sda
#VMDRIVE="-device virtio-scsi-pci,id=scsi -device scsi-hd,drive=hd -drive if=none,id=hd,format=qcow2,file=$DISK"
# simple disk method (check is the virtio default with this)
#VMDRIVE="-drive format=qcow2,file=$DISK"

# simple network
NET="-nic user,model=virtio"
# or more complex one
# adduser $USER qemu
# edit /etc/qemu/bridge.conf - uncomment line which allows users in qemu group to config bridge
NET="-netdev bridge,id=net0 -device virtio-net-device,netdev=net0"

$QEMU $MACHINE $CPU \
  -m $MEMORY \
  -bios AAVMF32_CODE.fd \
  -nographic \
  $VMDRIVE \
  $NET \
  -device virtio-rng-pci \
  -rtc base=utc,clock=host