#!/bin/sh if [ $# -ne 2 ]; then echo "$0 disk ssh-keyfile" echo " disk: which disk to install to" echo " ssh-keyfile: ssh keys to add into the image" exit 1 fi set -e set -x DISK=$1; shift SSH_KEYS=$1; shift MAJOR_VERSION=3.14 MINOR_VERSION=0 IMAGE=alpine-minirootfs-$MAJOR_VERSION.$MINOR_VERSION-x86_64.tar.gz RESOLVCONF=/etc/resolv.conf working_directory=$(pwd -P) rootfs_tmpdir=$(mktemp -d) rootfs_url="http://dl-cdn.alpinelinux.org/alpine/v$MAJOR_VERSION/releases/x86_64/$IMAGE" case $DISK in /dev/sd*) partition=${DISK}1 ;; /dev/mmcblk*|/dev/nvme*) partition=${DISK}p1 ;; *) echo "Unsupported disk - edit this script" >&2 exit 1 ;; esac run_root () { sudo chroot $rootfs_tmpdir /usr/bin/env \ PATH=/sbin:/bin:/usr/sbin:/usr/bin \ /bin/sh -c "$*" } wget -c "$rootfs_url" -O "$IMAGE" # Clean the first 2M - getting rid of old things # in the gap and also the paritition table dd if=/dev/zero of=${DISK} bs=1M count=2 # Partition disk with 1 Linux partition sudo sfdisk "$DISK" <> ${rootfs_tmpdir}/etc/fstab run_root apk update run_root apk add linux-lts openrc udev openssh e2fsprogs run_root rc-update add udev run_root rc-update add udev-trigger run_root rc-update add sshd run_root rc-update add networking run_root rc-update add hostname run_root rc-update add sysctl run_root rc-update add modules run_root sed -i 's/root:!::0:::::/root:*::0:::::/' /etc/shadow sudo tee "$rootfs_tmpdir/etc/network/interfaces" <> ${rootfs_tmpdir}/etc/default/grub run_root grub-mkconfig -o /boot/grub/grub.cfg run_root grub-install --target=i386-pc ${DISK} # Cleanup run_root rm -f /etc/resolv.conf for dir in dev proc sys; do sudo umount ${rootfs_tmpdir}/${dir} done sudo umount $rootfs_tmpdir sync rmdir ${rootfs_tmpdir} echo "${DISK} has been setup with Alpine Linux" exit 0