install Alpine Linux riscv64 under qemu

2021-08-12

usual disclaimer: no one is responsible for any damage/loss by using this

script bellow will install Alpine Linux riscv64 to run in qemu on x86_64 host


#!/bin/sh
instdir="/mnt"
# qemu-img create rv64-qemu.img 2G
img="rv64-qemu.img"
umount -q $instdir

mkfs.ext4 -L rv64.rfs $img
mount -o loop $img $instdir
sync
echo "making dirs and mounting"
apk --root $instdir --arch riscv64 --allow-untrusted --initdb \
  -X http://dl-cdn.alpinelinux.org/alpine/edge/main \
  -X http://dl-cdn.alpinelinux.org/alpine/edge/community \
  add alpine-base alpine-baselayout alpine-conf kmod openrc \
  sysfsutils ssl_client ca-certificates-bundle linux-edge-virt \
  util-linux

cp $instdir/boot/vmlinuz-edge-virt linux-edge-virt.gz
cp $instdir/boot/initramfs-edge-virt .
gunzip linux-edge-virt.gz
sync

ln -s /etc/init.d/bootmisc $instdir/etc/runlevels/boot/
ln -s /etc/init.d/hostname $instdir/etc/runlevels/boot
ln -s /etc/init.d/modules $instdir/etc/runlevels/boot
ln -s /etc/init.d/sysctl $instdir/etc/runlevels/boot
ln -s /etc/init.d/urandom $instdir/etc/runlevels/boot
ln -s /etc/init.d/devfs $instdir/etc/runlevels/sysinit
ln -s /etc/init.d/hwdrivers $instdir/etc/runlevels/sysinit
ln -s /etc/init.d/mdev $instdir/etc/runlevels/sysinit
ln -s /etc/init.d/modules $instdir/etc/runlevels/sysinit
ln -s /etc/init.d/mount-ro $instdir/etc/runlevels/shutdown
ln -s /etc/init.d/killprocs $instdir/etc/runlevels/shutdown
ln -s /etc/init.d/iwd $instdir/etc/runlevels/default

sed -i 's/^#ttyS0/ttyS0/' $instdir/etc/inittab
sync
umount $instdir
echo
echo "install completed."

exit 0

run this VM with next command


qemu-system-riscv64 -M virt -smp 5 -m 2G \
	-kernel linux-edge-virt \
	-drive file=rv64-qemu.img,id=hd0 -device virtio-blk,drive=hd0 \
	-append "root=/dev/vda rw rootfstype=ext4 console=ttyS0" \
	-nographic \
	-netdev user,id=n1 -device virtio-net-pci,netdev=n1