Compare commits

...

2 Commits

Author SHA1 Message Date
43fe76b841
Use and require /dev/disk/by-id disk paths 2022-11-29 23:51:52 -05:00
ead7a9f24e
Fix mirror zpool installation 2022-11-29 02:02:14 -05:00
2 changed files with 60 additions and 28 deletions

View File

@ -45,7 +45,7 @@ build {
} }
provisioner "shell" { provisioner "shell" {
inline = ["sudo /tmp/debianzfs.sh -p changeme -P letmeinzfs! /dev/vda debianzfs"] inline = ["sudo /tmp/debianzfs.sh -ip changeme -P letmeinzfs! /dev/vda debianzfs"]
} }
} }

View File

@ -7,7 +7,14 @@
### Functions ### ### Functions ###
################# #################
function usage () { function usage () {
echo "Usage: ./$(basename "$0") [-mpPr] <DISK> <HOSTNAME>" echo "Usage: $(basename "$0") [-ghimpPr] <DISK> [HOSTNAME]"
echo -e "\t-g\n\t\tMirror GRUB after the installation. Requires: -m"
echo -e "\n\t-h\n\t\tThe help menu, i.e., the menu you're seeing now."
echo -e "\n\t-i\n\t\tIgnore the check for the /dev/disk/by-id/* format."
echo -e "\n\t-m <MIRROR>\n\t\tSet the MIRROR disk for a ZFS mirror installation."
echo -e "\n\t-p <PASSWORD>\n\t\tSet the password for root. Caution: saves to file temporarily."
echo -e "\n\t-P <PASSWWORD>\n\t\tSet the password for encrypting the root zpool."
echo -e "\n\t-r <ZFSROOT>\n\t\tSet the path for the new ZFS chroot. Defaults to /mnt"
} }
function disk_check () { function disk_check () {
@ -97,24 +104,38 @@ function create_root_pool () {
rpool $2 rpool $2
} }
function part_path () {
DISK="$1"
PART="$2"
[ "$(disk_check "$DISK")" == 1 ] && exit 1
if [ "${DISK:0:7}" == "/dev/sd" ]; then
DISK_PART="${DISK}${PART}"
elif [ "${DISK:0:9}" == "/dev/nvme" ]; then
DISK_PART="${DISK}p${PART}"
else
echo "ERROR: Disk not recognized"
exit 1
fi
[ "$(disk_check "$DISK_PART")" == 1 ] && exit 1
echo "$DISK_PART"
exit 0
}
function mirror_grub () { function mirror_grub () {
DISK1=$(disk_by_id "$1")
DISK2=$(disk_by_id "$2")
umount /boot/efi umount /boot/efi
dd if="${DISK1}-part2" of="${DISK2}-part2" dd if="$1" of="$2"
efibootmgr -c -g -d "$DISK2" -p 2 \ efibootmgr -c -g -d "$2" -p 2 \
-L "debian-${3}" -l '\EFI\debian\grubx64.efi' -L "debian-${3}" -l '\EFI\debian\grubx64.efi'
mount /boot/efi mount /boot/efi
} }
function disk_by_id () { function disk_byid_check () {
disk_check "$1" BYID="/dev/disk/by-id/"
OUTPUT=$(find /dev/disk/by-id -lname "../../$(basename "$1")" | tail -n1) if [ ! "${1:0:${#BYID}}" == "$BYID" ]; then
if [ -z "$OUTPUT" ]; then echo "ERROR: DISK needs to be ${BYID}* format"
echo "ERROR: No disk by-id label found for: $1"
exit 1 exit 1
fi fi
echo "$OUTPUT"
} }
################ ################
@ -125,9 +146,10 @@ export DEBIAN_FRONTEND=noninteractive
CODENAME="bullseye" CODENAME="bullseye"
# Options # Options
while getopts ':gm:p:P:r:' OPTION; do while getopts ':ghim:p:P:r:' OPTION; do
case "$OPTION" in case "$OPTION" in
g) GRUB_MIRROR="true";; g) GRUB_MIRROR="true";;
i) IGNORE_BYID="true";;
m) MIRROR="$OPTARG";; m) MIRROR="$OPTARG";;
p) ROOTPW="$OPTARG";; p) ROOTPW="$OPTARG";;
P) RPOOLPW="$OPTARG";; P) RPOOLPW="$OPTARG";;
@ -152,6 +174,8 @@ if [ "$GRUB_MIRROR" == "true" ]; then
[Yy]*) [Yy]*)
disk_check "$DISK" disk_check "$DISK"
disk_check "$MIRROR" disk_check "$MIRROR"
[ -z "$IGNORE_BYID" ] && disk_byid_check "$DISK"
[ -z "$IGNORE_BYID" ] && disk_byid_check "$MIRROR"
mirror_grub "$DISK" "$MIRROR" 2 mirror_grub "$DISK" "$MIRROR" 2
exit 0;; exit 0;;
?) ?)
@ -202,9 +226,11 @@ fi
# Are the DISK pathes empty devices? i.e., no filesystem signatures # Are the DISK pathes empty devices? i.e., no filesystem signatures
disk_check "$DISK" disk_check "$DISK"
disk_status "$DISK" disk_status "$DISK"
[ -z "$IGNORE_BYID" ] && disk_byid_check "$DISK"
if [ -n "$MIRROR" ]; then if [ -n "$MIRROR" ]; then
disk_check "$MIRROR" disk_check "$MIRROR"
disk_status "$MIRROR" disk_status "$MIRROR"
[ -z "$IGNORE_BYID" ] && disk_byid_check "$MIRROR"
fi fi
############################################### ###############################################
@ -212,7 +238,7 @@ fi
############################################### ###############################################
# Display commands # Display commands
set -x set -xe
# 1. Boot the Debian GNU/Linux Live CD... done # 1. Boot the Debian GNU/Linux Live CD... done
# 2. Setup and update the repositories # 2. Setup and update the repositories
@ -239,20 +265,27 @@ swapoff --all
# 3. Partition your disk(s) # 3. Partition your disk(s)
# UEFI booting + boot pool + ZFS native encryption # UEFI booting + boot pool + ZFS native encryption
disk_format "$DISK" disk_format "$DISK"
DISK_BYID=$(disk_by_id "$DISK") [ -n "$MIRROR" ] && disk_format "$MIRROR"
sleep 5
# Check for partitions 3 and 4
disk_check "$DISK-part3"
disk_check "$DISK-part4"
if [ -n "$MIRROR" ]; then if [ -n "$MIRROR" ]; then
disk_format "$MIRROR" disk_check "$MIRROR-part3"
MIRROR_BYID=$(disk_by_id "$MIRROR") disk_check "$MIRROR-part4"
fi fi
# 4. Create the boot pool # 4. Create the boot pool
# 5. Create the root pool # 5. Create the root pool
if [ -z "$MIRROR" ]; then if [ -z "$MIRROR" ]; then
create_boot_pool "$ZFSROOT" "${DISK_BYID}-part3" create_boot_pool "$ZFSROOT" "$DISK-part3"
create_root_pool "$ZFSROOT" "${DISK_BYID}-part4" "$RPOOLPW" create_root_pool "$ZFSROOT" "$DISK-part4" "$RPOOLPW"
else else
create_boot_pool "$ZFSROOT" "mirror ${DISK_BYID}-part3 ${MIRROR_BYID}-part3" create_boot_pool "$ZFSROOT" \
create_root_pool "$ZFSROOT" "mirror ${DISK_BYID}-part4 ${MIRROR_BYID}-part4" "$RPOOLPW" "mirror ${DISK}-part3 $MIRROR-part3"
create_root_pool "$ZFSROOT" \
"mirror ${DISK}-part4 $MIRROR-part4" "$RPOOLPW"
fi fi
################################### ###################################
@ -342,7 +375,7 @@ EOF
# 4. Bind the virtual filesystems from the LiveCD environment to the new system and chroot into it # 4. Bind the virtual filesystems from the LiveCD environment to the new system and chroot into it
# Copy DISK/MIRROR vars under ZFSROOT # Copy DISK/MIRROR vars under ZFSROOT
echo -e "DISK=\"${DISK}\"\nROOTPW=\"${ROOTPW}\"" > "$ZFSROOT/var/tmp/zfsenv" echo -e "DISK=\"$DISK\"\nROOTPW=\"${ROOTPW}\"" > "$ZFSROOT/var/tmp/zfsenv"
# Copy self and GRUB mirror helper script into chroot # Copy self and GRUB mirror helper script into chroot
if [ -n "$MIRROR" ]; then if [ -n "$MIRROR" ]; then
@ -353,8 +386,8 @@ if [ -n "$MIRROR" ]; then
#!/bin/bash #!/bin/bash
# Post-install GRUB mirror helper script # Post-install GRUB mirror helper script
/usr/local/bin/debianzfs \ /usr/local/bin/debianzfs \
-gm $(disk_by_id "$MIRROR") \ -gm $MIRROR-part2 \
$(disk_by_id "$DISK") $DISK-part2)
GRUBMIRROR GRUBMIRROR
fi fi
@ -367,10 +400,9 @@ mount --make-private --rbind /sys /mnt/sys
cat << CHROOT | chroot /mnt bash --login cat << CHROOT | chroot /mnt bash --login
# Setup # Setup
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
set -ex set -ex
. /var/tmp/zfsenv . /var/tmp/zfsenv
rm -f /var/tmp/zfsenv
unset CDPATH unset CDPATH
cd cd
@ -391,9 +423,9 @@ echo REMAKE_INITRD=yes > /etc/dkms/zfs.conf
# Install GRUB for UEFI booting # Install GRUB for UEFI booting
apt-get install -y dosfstools apt-get install -y dosfstools
mkdosfs -F 32 -s 1 -n EFI "\${DISK}2" mkdosfs -F 32 -s 1 -n EFI "\${DISK}-part2"
mkdir /boot/efi mkdir /boot/efi
BLKID_BOOT="/dev/disk/by-uuid/\$(blkid -s UUID -o value \${DISK}2)" BLKID_BOOT="/dev/disk/by-uuid/\$(blkid -s UUID -o value \${DISK}-part2)"
echo "\${BLKID_BOOT} /boot/efi vfat defaults 0 0" >> /etc/fstab echo "\${BLKID_BOOT} /boot/efi vfat defaults 0 0" >> /etc/fstab
mount /boot/efi mount /boot/efi
apt-get install -y grub-efi-amd64 shim-signed apt-get install -y grub-efi-amd64 shim-signed