From c39f0201dcd78ee7e80614967f875cdf20f07eb7 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Sat, 26 Nov 2022 04:51:44 -0500 Subject: [PATCH] testing --- debianzfs.sh | 208 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 152 insertions(+), 56 deletions(-) mode change 100644 => 100755 debianzfs.sh diff --git a/debianzfs.sh b/debianzfs.sh old mode 100644 new mode 100755 index 5775100..c573865 --- a/debianzfs.sh +++ b/debianzfs.sh @@ -3,23 +3,152 @@ # Script is based off official guide: see "Debian Bullseye Root on ZFS" # https://openzfs.github.io/openzfs-docs/Getting%20Started/Debian/Debian%20Bullseye%20Root%20on%20ZFS.html -# Settings +################# +### Functions ### +################# +function usage () { + echo "Usage: ./$(basename "$0") [-mpP] /dev/sdX" +} + +function disk_check () { + DISK_TYPE=$(file "$1" | awk '{ print $2$3 }') + if [ "$DISK_TYPE" != "blockspecial" ]; then + echo "ERROR: Disk '$1' is not a block device" + exit 1; + fi +} + +function password_prompt () { + unset PASSWORD_PROMPT_RESULT + while true; do + read -r -s -p "${1}: " password + echo '' + read -r -s -p "${1} (confirm): " password_confirm + echo '' + if [ "$password" == "$password_confirm" ]; then + if [ -z "$password" ]; then + echo "Password can not be empty, try again." + else + break + fi + else + echo "Passwords did not match, try again." + fi + done + PASSWORD_PROMPT_RESULT="$password" + export PASSWORD_PROMPT_RESULT +} + +function disk_format () { + sgdisk -n2:1M:+512M -t2:EF00 "$1" + sgdisk -n3:0:+1G -t3:BF01 "$1" + sgdisk -n4:0:0 -t4:BF00 "$1" +} + +function create_boot_pool () { + zpool create -f \ + -o ashift=12 \ + -o autotrim=on -d \ + -o cachefile=/etc/zfs/zpool.cache \ + -o feature@async_destroy=enabled \ + -o feature@bookmarks=enabled \ + -o feature@embedded_data=enabled \ + -o feature@empty_bpobj=enabled \ + -o feature@enabled_txg=enabled \ + -o feature@extensible_dataset=enabled \ + -o feature@filesystem_limits=enabled \ + -o feature@hole_birth=enabled \ + -o feature@large_blocks=enabled \ + -o feature@livelist=enabled \ + -o feature@lz4_compress=enabled \ + -o feature@spacemap_histogram=enabled \ + -o feature@zpool_checkpoint=enabled \ + -O devices=off \ + -O acltype=posixacl -O xattr=sa \ + -O compression=lz4 \ + -O normalization=formD \ + -O relatime=on \ + -O canmount=off -O mountpoint=/boot -R "$1" \ + bpool "$2" +} + +function create_root_pool () { + zpool create -f \ + -o ashift=12 \ + -o autotrim=on \ + -O encryption=on -O keylocation=prompt -O keyformat=passphrase \ + -O acltype=posixacl -O xattr=sa -O dnodesize=auto \ + -O compression=lz4 \ + -O normalization=formD \ + -O relatime=on \ + -O canmount=off -O mountpoint=/ -R "$1" \ + rpool "$2" +} + + +################ +### Settings ### +################ +# Static export DEBIAN_FRONTEND=noninteractive -DISK=$1 -ZFSHOST=$2 -[ -z "$ZFSHOST" ] && ZFSHOST="debianzfs" CODENAME="bullseye" ZFSROOT="/mnt" +# Options +while getopts ':m:p:P:d' OPTION; do + case "$OPTION" in + m) MIRROR="$OPTARG";; + p) ROOTPW="$OPTARG";; + P) RPOOLPW="$OPTARG";; + ?) + usage + exit 1 + ;; + esac +done +shift "$((OPTIND -1))" + +# Parameters +DISK=$1 +ZFSHOST=$2 + +# Verify variables +[ -z "$ZFSHOST" ] && ZFSHOST="debianzfs" + +if [ -z "$DISK" ]; then + echo "FATAL: DISK not set" + usage + exit 1 +fi + +if [ -z "$ROOTPW" ]; then + password_prompt "Root Passphrase" + ROOTPW="$PASSWORD_PROMPT_RESULT" + unset PASSWORD_PROMPT_RESULT +fi + +if [ -z "$RPOOLPW" ]; then + password_prompt "ZFS Encryption Passphrase" + RPOOLPW="$PASSWORD_PROMPT_RESULT" + unset PASSWORD_PROMPT_RESULT +fi + +if [ "$DEBUG" == "true" ]; then + echo "DISK=${DISK}" + echo "MIRROR=${MIRROR}" + echo "ZFSHOST=${ZFSHOST}" + echo "CODENAME=${CODENAME}" + echo "ZFSROOT=${ZFSROOT}" + echo "ROOTPW=${ROOTPW}" + echo "RPOOLPW=${RPOOLPW}" +fi + # Display commands set -x -# Is the DISK path a block device? -DISK_TYPE=$(file "${DISK}" | awk '{ print $2$3 }') -if [ "$DISK_TYPE" != "blockspecial" ]; then - echo "ERROR: Disk '${DISK}' is not a block device" - exit 1; -fi +# Are the DISK paths block devices? +disk_check "$DISK" +[ -n "$MIRROR" ] && disk_check "$MIRROR" ############################################### ### Step 1: Prepare The Install Environment ### @@ -48,56 +177,23 @@ apt-get install -y debootstrap gdisk pwgen zfsutils-linux swapoff --all # 3. Partition your disk(s) -# Run this for UEFI booting (for use now or in the future) -sgdisk -n2:1M:+512M -t2:EF00 "$DISK" - -# Run this for the boot pool -sgdisk -n3:0:+1G -t3:BF01 "$DISK" - -# Unencrypted or ZFS native encryption -sgdisk -n4:0:0 -t4:BF00 "$DISK" +# UEFI booting + boot pool + ZFS native encryption +disk_format "$DISK" +[ -n "$MIRROR" ] && disk_format "$MIRROR" # 4. Create the boot pool -zpool create -f \ - -o ashift=12 \ - -o autotrim=on -d \ - -o cachefile=/etc/zfs/zpool.cache \ - -o feature@async_destroy=enabled \ - -o feature@bookmarks=enabled \ - -o feature@embedded_data=enabled \ - -o feature@empty_bpobj=enabled \ - -o feature@enabled_txg=enabled \ - -o feature@extensible_dataset=enabled \ - -o feature@filesystem_limits=enabled \ - -o feature@hole_birth=enabled \ - -o feature@large_blocks=enabled \ - -o feature@livelist=enabled \ - -o feature@lz4_compress=enabled \ - -o feature@spacemap_histogram=enabled \ - -o feature@zpool_checkpoint=enabled \ - -O devices=off \ - -O acltype=posixacl -O xattr=sa \ - -O compression=lz4 \ - -O normalization=formD \ - -O relatime=on \ - -O canmount=off -O mountpoint=/boot -R "$ZFSROOT" \ - bpool "${DISK}3" +if [ -z "$MIRROR" ]; then + create_boot_pool "$ZFSROOT" "${DISK}3 ${MIRROR}3" +else + create_boot_pool "$ZFSROOT" "mirror ${DISK}3 ${MIRROR}3" +fi # 5. Create the root pool -# ZFS native encryption (with a random password) -RPOOLPW="$(pwgen -s 16 1)" -echo "$RPOOLPW" | \ -zpool create -f \ - -o ashift=12 \ - -o autotrim=on \ - -O encryption=on -O keylocation=prompt -O keyformat=passphrase \ - -O acltype=posixacl -O xattr=sa -O dnodesize=auto \ - -O compression=lz4 \ - -O normalization=formD \ - -O relatime=on \ - -O canmount=off -O mountpoint=/ -R "$ZFSROOT" \ - rpool "${DISK}4" -unset RPOOLPW +if [ -z "$MIRROR" ]; then + create_root_pool "$ZFSROOT" "${DISK}4" +else + create_root_pool "$ZFSROOT" "mirror ${DISK}4 ${MIRROR}4" +fi ################################### ### Step 3: System Installation ###