#!/bin/bash

set -ex

dd if=/dev/zero of=/tmp/alpine-minirootfs-aarch64.ext4 bs=1M count={{ fs_size_mib }}
mkfs.ext4 /tmp/alpine-minirootfs-aarch64.ext4
mkdir -p /tmp/rootfs
mount -o loop /tmp/alpine-minirootfs-aarch64.ext4 /tmp/rootfs
tar xzf /tmp/alpine-minirootfs-3.14.9-aarch64.tar.gz -C /tmp/rootfs

cp -r /tmp/overlay/* /tmp/rootfs/

cat > /tmp/rootfs/prepare.sh <<EOF
passwd root -d root
apk update
apk add -u openrc ca-certificates
exit
EOF
chroot /tmp/rootfs/ /bin/sh /prepare.sh
rm /tmp/rootfs/prepare.sh

umount /tmp/rootfs
rm -rf /tmp/rootfs

mkdir -p /opt/code || :
cp /tmp/alpine-minirootfs-aarch64.ext4 /opt/code
