2021-05-29 17:53:40 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-02-11 21:24:43 +00:00
|
|
|
if [ $# -ne 3 ]; then
|
2023-09-11 16:27:57 +00:00
|
|
|
echo "$0 disk ssh-keyfile [efi|bios|efinvram]"
|
2021-05-29 17:53:40 +00:00
|
|
|
echo " disk: which disk to install to"
|
|
|
|
echo " ssh-keyfile: ssh keys to add into the image"
|
2022-02-11 21:24:43 +00:00
|
|
|
echo " use efi or bios partitioning"
|
2024-01-06 14:23:46 +00:00
|
|
|
echo "pre install in OS: apk add hdparm sudo wget efibootmgr git sfdisk"
|
2021-05-29 17:53:40 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
|
|
|
set -x
|
|
|
|
|
|
|
|
DISK=$1; shift
|
|
|
|
SSH_KEYS=$1; shift
|
2022-02-11 21:24:43 +00:00
|
|
|
BOOT_VIA=$1; shift
|
2021-05-29 17:53:40 +00:00
|
|
|
|
2024-01-06 14:23:46 +00:00
|
|
|
MAJOR_VERSION=3.19
|
|
|
|
MINOR_VERSION=0
|
|
|
|
|
2021-05-29 17:53:40 +00:00
|
|
|
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"
|
|
|
|
|
2021-07-28 14:32:43 +00:00
|
|
|
case $DISK in
|
|
|
|
/dev/sd*)
|
2022-02-11 21:24:43 +00:00
|
|
|
partition1=${DISK}1
|
|
|
|
partition2=${DISK}2
|
2021-07-28 14:32:43 +00:00
|
|
|
;;
|
|
|
|
/dev/mmcblk*|/dev/nvme*)
|
2022-02-11 21:24:43 +00:00
|
|
|
partition1=${DISK}p1
|
|
|
|
partition2=${DISK}p2
|
2021-07-28 14:32:43 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unsupported disk - edit this script" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-05-29 17:53:40 +00:00
|
|
|
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 \
|
2021-05-29 17:53:40 +00:00
|
|
|
/bin/sh -c "$*"
|
|
|
|
}
|
|
|
|
|
|
|
|
wget -c "$rootfs_url" -O "$IMAGE"
|
|
|
|
|
2021-05-29 18:05:42 +00:00
|
|
|
# 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
|
|
|
|
|
2022-02-11 21:24:43 +00:00
|
|
|
case "$BOOT_VIA" in
|
|
|
|
bios)
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo sfdisk "$DISK" <<EOF
|
2021-05-29 17:53:40 +00:00
|
|
|
label: dos
|
|
|
|
,,L
|
|
|
|
EOF
|
2024-01-31 11:05:16 +00:00
|
|
|
sleep 10
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo hdparm -z $DISK
|
2022-02-11 21:24:43 +00:00
|
|
|
# For creation, if an existing filesystem is on the partitions
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo mkfs.ext4 -F ${partition1}
|
|
|
|
sudo mount -t ext4 ${partition1} $rootfs_tmpdir
|
2022-02-11 21:24:43 +00:00
|
|
|
;;
|
2023-09-11 16:27:57 +00:00
|
|
|
efi*)
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo sfdisk "$DISK" <<EOF
|
2022-02-11 21:24:43 +00:00
|
|
|
label: gpt
|
|
|
|
,500MiB,U
|
|
|
|
,,L
|
|
|
|
EOF
|
2023-07-18 12:17:12 +00:00
|
|
|
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"
|
2022-02-11 21:24:43 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown disk format, $BOOT_VIA" >&2
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
2021-05-29 17:53:40 +00:00
|
|
|
|
|
|
|
|
2023-07-18 12:17:12 +00:00
|
|
|
# keep right permissions, use sudo
|
|
|
|
sudo tar xf $IMAGE -C $rootfs_tmpdir
|
2021-05-29 17:53:40 +00:00
|
|
|
|
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
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo mount --bind /${dir} ${rootfs_tmpdir}/${dir}
|
2021-05-29 20:24:10 +00:00
|
|
|
done
|
|
|
|
|
2021-05-29 17:53:40 +00:00
|
|
|
# Add SSH keys
|
|
|
|
run_root mkdir -p root/.ssh
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo cp $SSH_KEYS $rootfs_tmpdir/root/.ssh/authorized_keys
|
2021-05-29 17:53:40 +00:00
|
|
|
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.
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo cp "$RESOLVCONF" $rootfs_tmpdir/etc/resolv.conf
|
2021-05-29 17:53:40 +00:00
|
|
|
|
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 }')
|
2022-08-05 21:29:24 +00:00
|
|
|
UUID_1=$UUID
|
|
|
|
|
2021-05-29 19:07:58 +00:00
|
|
|
|
2021-05-29 17:53:40 +00:00
|
|
|
run_root apk update
|
2021-05-29 20:44:43 +00:00
|
|
|
run_root apk add linux-lts openrc udev openssh e2fsprogs
|
2021-05-29 17:53:40 +00:00
|
|
|
|
2023-12-06 17:58:09 +00:00
|
|
|
# For ansible
|
|
|
|
run_root apk add python3
|
|
|
|
|
2021-05-29 17:53:40 +00:00
|
|
|
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
|
2021-05-29 17:53:40 +00:00
|
|
|
run_root sed -i 's/root:!::0:::::/root:*::0:::::/' /etc/shadow
|
|
|
|
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo tee "$rootfs_tmpdir/etc/network/interfaces" <<EOF
|
2021-05-29 17:53:40 +00:00
|
|
|
auto lo
|
|
|
|
iface lo inet loopback
|
|
|
|
|
|
|
|
auto eth0
|
|
|
|
iface eth0 inet6 manual
|
|
|
|
up /sbin/ip link set \$IFACE up
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo tee "$rootfs_tmpdir/etc/hostname" <<EOF
|
2021-05-29 17:53:40 +00:00
|
|
|
alpine-unconfigured
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Setup bootloader
|
|
|
|
|
2022-02-22 18:26:55 +00:00
|
|
|
run_root apk add grub-bios grub-efi
|
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
|
2022-02-22 18:26:55 +00:00
|
|
|
|
|
|
|
case "$BOOT_VIA" in
|
|
|
|
bios)
|
|
|
|
run_root grub-install --target=i386-pc ${DISK}
|
2022-08-05 21:29:24 +00:00
|
|
|
echo "UUID=$UUID_1 / ext4 defaults 0 1" >> ${rootfs_tmpdir}/etc/fstab
|
2022-02-22 18:26:55 +00:00
|
|
|
;;
|
2023-09-11 16:27:57 +00:00
|
|
|
efi*)
|
2022-08-05 21:29:24 +00:00
|
|
|
eval $(blkid | grep ^${DISK}2 | awk '{ print $2 }')
|
|
|
|
UUID_2=$UUID
|
|
|
|
|
|
|
|
echo "UUID=$UUID_2 / ext4 defaults 0 1" >> ${rootfs_tmpdir}/etc/fstab
|
2022-10-09 10:00:02 +00:00
|
|
|
echo "UUID=$UUID_1 /boot vfat defaults 0 2" >> ${rootfs_tmpdir}/etc/fstab
|
2022-08-05 21:29:24 +00:00
|
|
|
|
2024-01-06 15:58:36 +00:00
|
|
|
|
|
|
|
# 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
|
2024-01-06 16:11:37 +00:00
|
|
|
sudo umount ${rootfs_tmpdir}${dir}
|
2024-01-06 15:58:36 +00:00
|
|
|
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
|
|
|
|
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo umount ${rootfs_tmpdir}/boot
|
2022-02-22 18:26:55 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2023-09-11 16:27:57 +00:00
|
|
|
|
2022-10-07 18:35:09 +00:00
|
|
|
# Debug
|
|
|
|
run_root cat /etc/fstab
|
2021-05-29 17:53:40 +00:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
run_root rm -f /etc/resolv.conf
|
|
|
|
for dir in dev proc sys; do
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo umount ${rootfs_tmpdir}/${dir}
|
2021-05-29 17:53:40 +00:00
|
|
|
done
|
2023-07-18 12:17:12 +00:00
|
|
|
sudo umount $rootfs_tmpdir
|
2021-05-29 17:53:40 +00:00
|
|
|
|
|
|
|
sync
|
2021-05-29 18:10:46 +00:00
|
|
|
rmdir ${rootfs_tmpdir}
|
2021-05-29 17:53:40 +00:00
|
|
|
|
|
|
|
echo "${DISK} has been setup with Alpine Linux"
|
|
|
|
|
|
|
|
exit 0
|