Add script to install alpine to a usb disk

This commit is contained in:
Nico Schottelius 2021-05-29 19:53:40 +02:00
parent bc568d7151
commit 8dc4d6bacb
2 changed files with 103 additions and 1 deletions

102
alpine-install-on-disk.sh Executable file
View File

@ -0,0 +1,102 @@
#!/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.13
MINOR_VERSION=5
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"
run_root () {
sudo chroot $rootfs_tmpdir /usr/bin/env \
PATH=/bin:/sbin \
/bin/sh -c "$*"
}
wget -c "$rootfs_url" -O "$IMAGE"
# Partition disk with 1 Linux partition
sudo sfdisk "$DISK" <<EOF
label: dos
,,L
EOF
# For creation, if an existing filesystem is on the partitions
sudo mkfs.ext4 -F ${DISK}1
sudo mount ${DISK}1 $rootfs_tmpdir
# keep right permissions, use sudo
sudo tar xf $IMAGE -C $rootfs_tmpdir
# Add SSH keys
run_root mkdir -p root/.ssh
sudo cp $SSH_KEYS $rootfs_tmpdir/root/.ssh/authorized_keys
run_root chown root:root /root/.ssh/authorized_keys
run_root chmod 0600 /root/.ssh/authorized_keys
run_root chmod 0700 /root/.ssh
# Import local resolv.conf.
sudo cp "$RESOLVCONF" $rootfs_tmpdir/etc/resolv.conf
run_root apk update
run_root apk add linux-lts openrc udev openssh
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 sed -i 's/root:!::0:::::/root:*::0:::::/' /etc/shadow
sudo tee "$rootfs_tmpdir/etc/network/interfaces" <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet6 manual
up /sbin/ip link set \$IFACE up
EOF
sudo tee "$rootfs_tmpdir/etc/hostname" <<EOF
alpine-unconfigured
EOF
# Setup bootloader
for dir in dev proc sys; do
sudo mount --bind /${dir} ${rootfs_tmpdir}/${dir}
done
run_root apk add grub-bios
run_root grub-install ${DISK}
# Cleanup
run_root rm -f /etc/resolv.conf
for dir in dev proc sys; do
sudo mount --bind /${dir} ${rootfs_tmpdir}/${dir}
done
sudo umount $rootfs_tmpdir
sync
echo "${DISK} has been setup with Alpine Linux"
exit 0

View File

@ -53,7 +53,7 @@ sudo cp "$RESOLVCONF" $rootfs_tmpdir/etc/resolv.conf
run_root ln -sf /sbin/init /init
run_root apk update
run_root apk add linux-lts openrc udev openssh rdnssd bonding vlan
run_root apk add linux-lts openrc udev openssh rdnssd
# rdnssd
run_root rc-update add udev
run_root rc-update add udev-trigger