# Run [Alpine](https://alpinelinux.org/) on QEMU aarch64 Virtual Machine
###### tags: `Virtual Machine`, `QEMU`, `Alpine`
## Prepare
### Get Alpine NET Installer's Kernel and Initial RAM Disk
The **vmlinuz-lts** and **initramfs-lts** of Alpine's net installer https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/
### The Virtual Disk Going to be Installed
Prepare the virtual disk `qemu-img create -f qcow2 disk.qcow2 8G`
## Installation Steps
Use the net installer to install Alpine into the virtual disk
```shell=
$ qemu-system-aarch64 -smp 2 \
-M virt -cpu cortex-a57 -m 1G \
-initrd initramfs-lts \
-kernel vmlinuz-lts \
--append "console=ttyAMA0 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/ modloop=http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/modloop-lts" \
-hda disk.qcow2 \
-netdev user,id=unet -device virtio-net-device,netdev=unet -net user \
-nographic
```
* `setup-alpine`
* us and us-intl
* set hostname
* eth0
* dhcp, not manual network
* root password
* Timezone
* proxy no
* NTP client: busybox
* mirror site
* ssh server: openssh
* use disk: vda
* poweroff
PS. Get the `alpine_repo` and `modloop` with **HTTP**, instead of HTTPs. Because, the crypto of secured HTTPs is not supported during boot time.
## Boot the New System
### Extract the Kernel and Initial RAM Disk from the Virtual Disk
```shell=
$ sudo modprobe nbd max_part=8
$ sudo qemu-nbd --connect=/dev/nbd0 disk.qcow2
$ sudo mount /dev/nbd0p1 /mnt/
$ cp /mnt/vmlinuz-lts ./vmlinuz-lts.img
$ cp /mnt/initramfs-lts ./initramfs-lts.img
$ sudo umount /dev/nbd0p1
$ sudo nbd-client -d /dev/nbd0
$ sudo modprobe -r nbd
```
### Boot
```shell=
$ qemu-system-aarch64 -smp 2 \
-M virt -cpu cortex-a57 -m 1G \
-initrd initramfs-lts.img \
-kernel vmlinuz-lts.img \
--append "console=ttyAMA0 root=/dev/vda3 rw rootfstype=ext4" \
-hda disk.qcow2 \
-device e1000,netdev=net0 \
-net nic -netdev user,hostfwd=tcp:127.0.0.1:2222-:22,id=net0 \
-nographic
```
Note:
* Append the [`rootfstype=ext4`](https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html) to kernel's boot command. Otherwise, kernel does not know how to mount the loop.
* The usable ethernet interface is the **e1000**. It might be `eth1`, not `eth0`. So, check the ethernet interface before use internet.
## Reference
* **[Building a Debian Stretch QEMU image for AARCH64](https://blahcat.github.io/2018/01/07/building-a-debian-stretch-qemu-image-for-aarch64/)**
* Confirmed that this works and is useful
* Suggest to get the installer's kernel and initrd from `stable` for generic idea http://ftp.debian.org/debian/dists/stable/main/installer-arm64/current/images/netboot/debian-installer/arm64/
* [S390x/Installation](https://wiki.alpinelinux.org/wiki/S390x/Installation)
* [Linux: How to load a kernel module automatically at boot time](https://www.cyberciti.biz/faq/linux-how-to-load-a-kernel-module-automatically-at-boot-time/)
* [‘virt’ generic virtual platform (virt)](https://qemu.readthedocs.io/en/latest/system/arm/virt.html)