ungleich-tools/alpine-install-on-disk.sh

124 lines
3 KiB
Bash
Raw Normal View History

#!/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 () {
2021-05-29 18:22:25 +00:00
sudo chroot $rootfs_tmpdir /usr/bin/env \
2021-05-29 18:10:46 +00:00
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" <<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
2021-05-29 20:24:10 +00:00
# These are required by grub-install
# And also for generating grub config that contains rootfstype
for dir in dev proc sys; do
sudo mount --bind /${dir} ${rootfs_tmpdir}/${dir}
done
# 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
2021-05-29 19:07:58 +00:00
# Generate fstab which is later included in the initramfs
# Add filesystem to fstab, because busybox mount does not work
# without -t ext4 for mounting and returns "No such file or directory"
# nb2:~# blkid| grep ^${DISK}1 | awk '{ print $2 }'
# UUID="fecf4182-f6dd-4d2c-9af7-8f36444ee25c"
eval $(blkid | grep ^${DISK}1 | awk '{ print $2 }')
2021-05-29 19:23:05 +00:00
echo "UUID=$UUID / ext4 defaults 0 1" >> ${rootfs_tmpdir}/etc/fstab
2021-05-29 19:07:58 +00:00
run_root apk update
2021-05-29 20:44:43 +00:00
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
2021-06-07 17:15:35 +00:00
run_root rc-update add sysctl
2021-07-11 12:05:50 +00:00
run_root rc-update add modules
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
run_root apk add grub-bios
2021-05-29 20:24:10 +00:00
echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet rootfstype=ext4"' >> ${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
2021-05-29 18:22:25 +00:00
sudo umount ${rootfs_tmpdir}/${dir}
done
sudo umount $rootfs_tmpdir
sync
2021-05-29 18:10:46 +00:00
rmdir ${rootfs_tmpdir}
echo "${DISK} has been setup with Alpine Linux"
exit 0