203 lines
5 KiB
Bash
Executable file
203 lines
5 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
if [ $# -ne 3 ]; then
|
|
echo "$0 disk ssh-keyfile [efi|bios|efinvram]"
|
|
echo " disk: which disk to install to"
|
|
echo " ssh-keyfile: ssh keys to add into the image"
|
|
echo " use efi or bios partitioning"
|
|
echo "pre install in OS: apk add hdparm sudo wget efibootmgr git sfdisk"
|
|
exit 1
|
|
fi
|
|
|
|
set -e
|
|
set -x
|
|
|
|
DISK=$1; shift
|
|
SSH_KEYS=$1; shift
|
|
BOOT_VIA=$1; shift
|
|
|
|
MAJOR_VERSION=3.20
|
|
MINOR_VERSION=1
|
|
|
|
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*)
|
|
partition1=${DISK}1
|
|
partition2=${DISK}2
|
|
;;
|
|
/dev/mmcblk*|/dev/nvme*)
|
|
partition1=${DISK}p1
|
|
partition2=${DISK}p2
|
|
;;
|
|
*)
|
|
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
|
|
|
|
case "$BOOT_VIA" in
|
|
bios)
|
|
sudo sfdisk "$DISK" <<EOF
|
|
label: dos
|
|
,,L
|
|
EOF
|
|
sleep 10
|
|
sudo hdparm -z $DISK
|
|
# For creation, if an existing filesystem is on the partitions
|
|
sudo mkfs.ext4 -F ${partition1}
|
|
sudo mount -t ext4 ${partition1} $rootfs_tmpdir
|
|
;;
|
|
efi*)
|
|
sudo sfdisk "$DISK" <<EOF
|
|
label: gpt
|
|
,500MiB,U
|
|
,,L
|
|
EOF
|
|
sudo hdparm -z $DISK
|
|
sudo mkfs.vfat ${partition1}
|
|
sudo mkfs.ext4 -F ${partition2}
|
|
sudo mount -t ext4 ${partition2} "$rootfs_tmpdir"
|
|
sudo mkdir "${rootfs_tmpdir}/boot"
|
|
sudo mount -t vfat ${partition1} "${rootfs_tmpdir}/boot"
|
|
;;
|
|
*)
|
|
echo "Unknown disk format, $BOOT_VIA" >&2
|
|
exit
|
|
;;
|
|
esac
|
|
|
|
|
|
# keep right permissions, use sudo
|
|
sudo tar xf $IMAGE -C $rootfs_tmpdir
|
|
|
|
# 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
|
|
|
|
# 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 }')
|
|
UUID_1=$UUID
|
|
|
|
|
|
run_root apk update
|
|
run_root apk add linux-lts openrc udev openssh e2fsprogs
|
|
|
|
# For ansible
|
|
run_root apk add python3
|
|
|
|
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" <<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 grub-efi
|
|
echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet rootfstype=ext4"' >> ${rootfs_tmpdir}/etc/default/grub
|
|
run_root grub-mkconfig -o /boot/grub/grub.cfg
|
|
|
|
case "$BOOT_VIA" in
|
|
bios)
|
|
run_root grub-install --target=i386-pc ${DISK}
|
|
echo "UUID=$UUID_1 / ext4 defaults 0 1" >> ${rootfs_tmpdir}/etc/fstab
|
|
;;
|
|
efi*)
|
|
eval $(blkid | grep ^${DISK}2 | awk '{ print $2 }')
|
|
UUID_2=$UUID
|
|
|
|
echo "UUID=$UUID_2 / ext4 defaults 0 1" >> ${rootfs_tmpdir}/etc/fstab
|
|
echo "UUID=$UUID_1 /boot vfat defaults 0 2" >> ${rootfs_tmpdir}/etc/fstab
|
|
|
|
|
|
# Actually do add us to the bios
|
|
if [ $BOOT_VIA = "efinvram" ]; then
|
|
dir=/sys/firmware/efi/efivars/
|
|
sudo mount --bind ${dir} ${rootfs_tmpdir}${dir}
|
|
run_root apk add efibootmgr
|
|
run_root grub-install --efi-directory=/boot
|
|
sudo umount ${rootfs_tmpdir}${dir}
|
|
else
|
|
run_root grub-install --efi-directory=/boot --no-nvram
|
|
|
|
# FIX for some machines (?)
|
|
run_root mkdir /boot/EFI/boot
|
|
run_root cp /boot/EFI/alpine/grubx64.efi /boot/EFI/boot/bootx64.efi
|
|
run_root cp /boot/grub/grub.cfg /boot/EFI/boot/
|
|
fi
|
|
|
|
sudo umount ${rootfs_tmpdir}/boot
|
|
;;
|
|
esac
|
|
|
|
|
|
# Debug
|
|
run_root cat /etc/fstab
|
|
|
|
# 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
|