| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Copyright 2019 Johannes 'josch' Schauer <[email protected]> |
| 4 |
# Copyright 2021 Guilhem Moulin <[email protected]> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in all |
| 14 |
# copies or substantial portions of the Software. |
| 15 |
|
| 16 |
PREREQ="" |
| 17 |
|
| 18 |
prereqs() { |
| 19 |
echo "$PREREQ" |
| 20 |
} |
| 21 |
|
| 22 |
case "$1" in |
| 23 |
prereqs) |
| 24 |
prereqs |
| 25 |
exit 0 |
| 26 |
;; |
| 27 |
esac |
| 28 |
|
| 29 |
. /scripts/functions |
| 30 |
set -eux |
| 31 |
|
| 32 |
# drives available to the setup system |
| 33 |
TARGET_DRIVE="/dev/vda" |
| 34 |
CHROOT_TARBALL="/dev/vdb" |
| 35 |
|
| 36 |
# we'll halt execution and hang until the timeout is reached (can't do better) |
| 37 |
trap "panic \"couldn't prepare testbed\"" EXIT |
| 38 |
|
| 39 |
# format the target disk with three partitions |
| 40 |
GUID_TYPE_BIOS_boot="21686148-6449-6E6F-744E-656564454649" # BIOS boot partition |
| 41 |
GUID_TYPE_Linux_FS="0FC63DAF-8483-4772-8E79-3D69D8477DE4" # Linux filesystem data |
| 42 |
GUID_TYPE_LUKS="CA7D7CCB-63ED-4C53-861C-1742536059CC" # LUKS partition |
| 43 |
sfdisk "$TARGET_DRIVE" <<-EOF |
| 44 |
label: gpt |
| 45 |
unit: sectors |
| 46 |
|
| 47 |
${TARGET_DRIVE}1 : start=$((1024*2)), size=$((1024*2)), type=${GUID_TYPE_BIOS_boot} |
| 48 |
${TARGET_DRIVE}2 : start=$(((1+1)*1024*2)), size=$((512*1024*2)), type=${GUID_TYPE_Linux_FS} |
| 49 |
${TARGET_DRIVE}3 : start=$(((1+1+512)*1024*2)), size=$((16*1024*2)) |
| 50 |
${TARGET_DRIVE}4 : start=$(((1+1+512+16)*1024*2)), type=${GUID_TYPE_LUKS} |
| 51 |
EOF |
| 52 |
|
| 53 |
BOOT_DEV="${TARGET_DRIVE}2" |
| 54 |
CRYPTSWAP_DEV="${TARGET_DRIVE}3" |
| 55 |
CRYPTROOT_DEV="${TARGET_DRIVE}4" |
| 56 |
CRYPTSWAP_DMNAME="swap_crypt" |
| 57 |
CRYPTROOT_DMNAME="root_crypt" |
| 58 |
HOSTNAME="debian" |
| 59 |
|
| 60 |
# initialize a new LUKS partition and open it |
| 61 |
cryptsetup luksFormat --batch-mode \ |
| 62 |
--key-file=/cryptroot/rootfs.key \ |
| 63 |
--type=luks2 \ |
| 64 |
--pbkdf=argon2id \ |
| 65 |
--pbkdf-force-iterations=4 \ |
| 66 |
--pbkdf-memory=32 \ |
| 67 |
-- "$CRYPTROOT_DEV" |
| 68 |
cryptsetup luksOpen --key-file=/cryptroot/rootfs.key --allow-discards \ |
| 69 |
-- "$CRYPTROOT_DEV" "$CRYPTROOT_DMNAME" |
| 70 |
|
| 71 |
# create ext2 resp. ext4 file system for /boot resp. the root FS |
| 72 |
mke2fs -t ext2 -m0 "$BOOT_DEV" |
| 73 |
mke2fs -t ext4 "/dev/mapper/$CRYPTROOT_DMNAME" |
| 74 |
|
| 75 |
# extract the chroot tarball into the target's root FS (excluding /boot/* and special files) |
| 76 |
mount -t ext4 "/dev/mapper/$CRYPTROOT_DMNAME" /root |
| 77 |
tar -C /root -xf- --xattrs --xattrs-include="*" \ |
| 78 |
--exclude="./lost+found" \ |
| 79 |
--exclude="./boot/*" \ |
| 80 |
--exclude="./dev/*" \ |
| 81 |
--exclude="./proc/*" \ |
| 82 |
--exclude="./run/*" \ |
| 83 |
--exclude="./sys/*" \ |
| 84 |
--exclude="./tmp/*" \ |
| 85 |
<"$CHROOT_TARBALL" |
| 86 |
|
| 87 |
# extract boot from the chroot tarball into the target's /boot partition |
| 88 |
mount -t ext2 "$BOOT_DEV" /root/boot |
| 89 |
tar -C /root -xf- --xattrs --xattrs-include="*" "./boot/" <"$CHROOT_TARBALL" |
| 90 |
|
| 91 |
|
| 92 |
# setup fstab(5) and crypttab(5) |
| 93 |
cat >/root/etc/fstab <<-EOF |
| 94 |
/dev/mapper/$CRYPTROOT_DMNAME / auto errors=remount-ro 0 1 |
| 95 |
/dev/mapper/$CRYPTSWAP_DMNAME none swap sw 0 0 |
| 96 |
UUID=$(blkid -s UUID -o value "$BOOT_DEV") /boot auto defaults 0 2 |
| 97 |
EOF |
| 98 |
cat >/root/etc/crypttab <<-EOF |
| 99 |
$CRYPTROOT_DMNAME UUID=$(blkid -s UUID -o value "$CRYPTROOT_DEV") none luks,discard |
| 100 |
$CRYPTSWAP_DMNAME $CRYPTSWAP_DEV /dev/urandom cipher=aes-xts-plain64,size=256,discard,swap |
| 101 |
EOF |
| 102 |
|
| 103 |
# setup the network |
| 104 |
IFACE="eth0" # we can rely on that name since we set net.ifnames=0 |
| 105 |
cat >/root/etc/network/interfaces <<-EOF |
| 106 |
auto lo |
| 107 |
iface lo inet loopback |
| 108 |
|
| 109 |
auto $IFACE |
| 110 |
iface $IFACE inet static |
| 111 |
address 10.0.2.129/24 |
| 112 |
EOF |
| 113 |
echo "$HOSTNAME" >/root/etc/hostname |
| 114 |
echo "127.0.0.1 localhost $HOSTNAME" >/root/etc/hosts |
| 115 |
ln -s /dev/null /root/etc/systemd/system/systemd-firstboot.service |
| 116 |
|
| 117 |
# instead of supplying the ip parameter to the kernel cmdline, we tell dropbear |
| 118 |
# about it to test this functionality; we use a dedicated IP at initramfs stage |
| 119 |
# to check that the network is re-configured as intended after pivoting |
| 120 |
echo "IP=\"10.0.2.130:::255.255.255.0::$IFACE:off\"" >/root/etc/initramfs-tools/conf.d/dropbear |
| 121 |
|
| 122 |
# start dropbear on a non-default port to also test this functionality |
| 123 |
echo "DROPBEAR_OPTIONS=\"-p 2222\"" >>/root/etc/dropbear/initramfs/dropbear.conf |
| 124 |
|
| 125 |
# copy dropbear's authorized_keys file to ~root/.ssh so we can login to the system's |
| 126 |
# SSHd once it finished booting |
| 127 |
mkdir -pm0700 /root/root/.ssh |
| 128 |
cp -T /root/etc/dropbear/initramfs/authorized_keys /root/root/.ssh/authorized_keys |
| 129 |
|
| 130 |
# add some restrictions to dropbear's authorized_keys file |
| 131 |
sed -ri '/^\s*(#|$)/b; s#^#no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="/bin/cryptroot-unlock" #' \ |
| 132 |
/root/etc/dropbear/initramfs/authorized_keys |
| 133 |
|
| 134 |
# mount pseudo- and temporary filesystems so we can chroot in the target system |
| 135 |
mount -t devtmpfs none /root/dev |
| 136 |
mount -t devpts none /root/dev/pts |
| 137 |
mount -t proc none /root/proc |
| 138 |
mount -t tmpfs none /root/run |
| 139 |
mount -t sysfs none /root/sys |
| 140 |
|
| 141 |
# update the initramfs image |
| 142 |
echo RESUME=none >/root/etc/initramfs-tools/conf.d/resume |
| 143 |
chroot /root update-initramfs -u |
| 144 |
|
| 145 |
|
| 146 |
# configure and install GRUB |
| 147 |
cat >/root/etc/default/grub <<-EOF |
| 148 |
GRUB_DEFAULT=0 |
| 149 |
GRUB_TIMEOUT=0 |
| 150 |
GRUB_CMDLINE_LINUX_DEFAULT="" |
| 151 |
GRUB_CMDLINE_LINUX="net.ifnames=0 console=ttyS0" |
| 152 |
GRUB_DISABLE_RECOVERY=true |
| 153 |
EOF |
| 154 |
chroot /root grub-install --no-floppy --modules=part_gpt "$TARGET_DRIVE" |
| 155 |
chroot /root update-grub |
| 156 |
|
| 157 |
|
| 158 |
# umount everything |
| 159 |
for d in /boot /dev/pts /dev /proc /run /sys ""; do |
| 160 |
umount "/root$d" |
| 161 |
done |
| 162 |
cryptsetup luksClose -- "$CRYPTROOT_DMNAME" |
| 163 |
|
| 164 |
# we're done! power the setup system down so we can reboot into the |
| 165 |
# target system |
| 166 |
poweroff -f |