Skip to content

Instantly share code, notes, and snippets.

@SamRH
Last active October 11, 2023 19:29
Show Gist options
  • Save SamRH/f016453af33eab9a80c39d072870bb6c to your computer and use it in GitHub Desktop.
Save SamRH/f016453af33eab9a80c39d072870bb6c to your computer and use it in GitHub Desktop.
AArch64 Alpine Docker to VM
# podman works too ;)
docker build --arch=arm64 --build-arg ARCH=arm64 -f Containerfile -t alpine-vm-base .
mkdir -p build
docker export -o build/alpine-vm-base-fs.tar `docker run -d alpine-vm-base:latest /bin/true`
docker run -v $(PWD)/build:/output:Z alpine-vm-base:latest cp /usr/share/u-boot/qemu_arm64/u-boot.bin /output/alpine-vm-bios-arm64.img
# from libguestfs / guestfs-tools
virt-make-fs --format=raw --partition --type=ext4 build/alpine-vm-base-fs.tar build/alpine-vm-arm64.img
setenv kernel_comp_addr_r 0x50000000
setenv kernel_comp_size 0x04000000
setenv bootargs 'ro root=/dev/vda1 rootfstype=ext4'
load virtio 0:1 ${kernel_addr_r} /boot/vmlinuz-virt
load virtio 0:1 ${ramdisk_addr_r} /boot/initramfs-virt
booti $kernel_addr_r $ramdisk_addr_r:$filesize $fdt_addr
FROM alpine:latest
ARG ARCH=arm64
# install required packages and enable ssh
RUN apk update
RUN apk add linux-virt linux-virt-dev gcc make openrc linux-virt u-boot-tools dropbear openssh-client
RUN echo 'rc_need="root net"' >> /etc/conf.d/dropbear
RUN rc-update add dropbear
# we'll want use u-boot.bin as our bios from u-boot-qemu if arm64
RUN if [ "$ARCH" = "arm64" ]; then apk add u-boot-qemu; fi
# add network interfaces
RUN echo "auto lo" > /etc/network/interfaces
RUN echo "iface lo inet loopback" >> /etc/network/interfaces
RUN echo "auto eth0" >> /etc/network/interfaces
RUN echo "iface eth0 inet dhcp" >> /etc/network/interfaces
# add some required stuff
RUN mkdir -p /build
COPY container /build/container
# build uboot boot command image for arm64
RUN if [ "$ARCH" = "arm64" ]; then mkimage -A "$ARCH" -O linux -T script -d /build/container/etc/boot.scr.txt /boot/boot.scr; fi
# build custom kernel module here
# set TTYs so can use QEMU stdio as terminal
RUN echo "ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100" >> /etc/inittab
RUN echo "ttyAMA0::respawn:/sbin/getty -L ttyAMA0 115200 vt100" >> /etc/inittab
# setup users
RUN adduser -D -h /home/user -s /bin/sh -u 1001 user
RUN echo "root:root" | chpasswd
RUN echo "user:user" | chpasswd
# run the vm with ssh available on localhost:2222
qemu-system-aarch64 -nographic -cpu max -M virt -m 512 -bios build/alpine-vm-bios-arm64.img -drive if=virtio,format=raw,file=build/alpine-vm-arm64.img -nic user,hostfwd=tcp::2222-:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment