Compare commits
8 Commits
2b07753419
...
ead7a9f24e
Author | SHA1 | Date | |
---|---|---|---|
ead7a9f24e | |||
5e6dc73ce5 | |||
c4cf616f7b | |||
aaa74697a5 | |||
babc015294 | |||
087070a3d4 | |||
0d99930fa7 | |||
e96a557d9a |
118
debianzfs.sh
118
debianzfs.sh
@ -7,7 +7,7 @@
|
|||||||
### Functions ###
|
### Functions ###
|
||||||
#################
|
#################
|
||||||
function usage () {
|
function usage () {
|
||||||
echo "Usage: ./$(basename "$0") [-mpPr] <DISK> [hostname]"
|
echo "Usage: ./$(basename "$0") [-mpPr] <DISK> <HOSTNAME>"
|
||||||
}
|
}
|
||||||
|
|
||||||
function disk_check () {
|
function disk_check () {
|
||||||
@ -56,6 +56,7 @@ function disk_format () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function create_boot_pool () {
|
function create_boot_pool () {
|
||||||
|
# shellcheck disable=SC2086
|
||||||
zpool create -f \
|
zpool create -f \
|
||||||
-o ashift=12 \
|
-o ashift=12 \
|
||||||
-o autotrim=on -d \
|
-o autotrim=on -d \
|
||||||
@ -79,10 +80,11 @@ function create_boot_pool () {
|
|||||||
-O normalization=formD \
|
-O normalization=formD \
|
||||||
-O relatime=on \
|
-O relatime=on \
|
||||||
-O canmount=off -O mountpoint=/boot -R "$1" \
|
-O canmount=off -O mountpoint=/boot -R "$1" \
|
||||||
bpool "$2"
|
bpool $2
|
||||||
}
|
}
|
||||||
|
|
||||||
function create_root_pool () {
|
function create_root_pool () {
|
||||||
|
# shellcheck disable=SC2086
|
||||||
echo "$3" | zpool create -f \
|
echo "$3" | zpool create -f \
|
||||||
-o ashift=12 \
|
-o ashift=12 \
|
||||||
-o autotrim=on \
|
-o autotrim=on \
|
||||||
@ -92,7 +94,48 @@ function create_root_pool () {
|
|||||||
-O normalization=formD \
|
-O normalization=formD \
|
||||||
-O relatime=on \
|
-O relatime=on \
|
||||||
-O canmount=off -O mountpoint=/ -R "$1" \
|
-O canmount=off -O mountpoint=/ -R "$1" \
|
||||||
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 part_by_uuid () {
|
||||||
|
OUTPUT=$(
|
||||||
|
blkid -s UUID | grep -e "^${1}.*${2}: UUID=" | \
|
||||||
|
awk '{ print substr($2, 7, length($2)-7) }'
|
||||||
|
)
|
||||||
|
|
||||||
|
if [ -z "$OUTPUT" ]; then
|
||||||
|
echo "ERROR: No disk by-uuid label found for: ${1}, partition ${2}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "/dev/disk/by-uuid/$OUTPUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
function mirror_grub () {
|
||||||
|
DISK1_PART2="$(part_by_uuid "$1" 2)"
|
||||||
|
DISK2_PART2="$(part_by_uuid "$2" 2)"
|
||||||
|
umount /boot/efi
|
||||||
|
dd if="$DISK1_PART2" of="$DISK2_PART2"
|
||||||
|
efibootmgr -c -g -d "$DISK2" -p 2 \
|
||||||
|
-L "debian-${3}" -l '\EFI\debian\grubx64.efi'
|
||||||
|
mount /boot/efi
|
||||||
}
|
}
|
||||||
|
|
||||||
################
|
################
|
||||||
@ -103,8 +146,9 @@ export DEBIAN_FRONTEND=noninteractive
|
|||||||
CODENAME="bullseye"
|
CODENAME="bullseye"
|
||||||
|
|
||||||
# Options
|
# Options
|
||||||
while getopts ':m:p:P:r:' OPTION; do
|
while getopts ':gm:p:P:r:' OPTION; do
|
||||||
case "$OPTION" in
|
case "$OPTION" in
|
||||||
|
g) GRUB_MIRROR="true";;
|
||||||
m) MIRROR="$OPTARG";;
|
m) MIRROR="$OPTARG";;
|
||||||
p) ROOTPW="$OPTARG";;
|
p) ROOTPW="$OPTARG";;
|
||||||
P) RPOOLPW="$OPTARG";;
|
P) RPOOLPW="$OPTARG";;
|
||||||
@ -120,6 +164,24 @@ shift "$((OPTIND -1))"
|
|||||||
DISK=$1
|
DISK=$1
|
||||||
ZFSHOST=$2
|
ZFSHOST=$2
|
||||||
|
|
||||||
|
# Post-boot grub mirror?
|
||||||
|
if [ "$GRUB_MIRROR" == "true" ]; then
|
||||||
|
while true; do
|
||||||
|
echo -e "ORIGINAL GRUB: $DISK\nMIRROR TO: $MIRROR"
|
||||||
|
read -r -p "Would you like to mirror GRUB? [y/N]: " yn
|
||||||
|
case $yn in
|
||||||
|
[Yy]*)
|
||||||
|
disk_check "$DISK"
|
||||||
|
disk_check "$MIRROR"
|
||||||
|
mirror_grub "$DISK" "$MIRROR" 2
|
||||||
|
exit 0;;
|
||||||
|
?)
|
||||||
|
echo "ABORTED: User did not confirm mirroring"
|
||||||
|
exit 1;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
# Verify variables
|
# Verify variables
|
||||||
[ -z "$ZFSROOT" ] && ZFSROOT="/mnt"
|
[ -z "$ZFSROOT" ] && ZFSROOT="/mnt"
|
||||||
|
|
||||||
@ -157,9 +219,6 @@ if [ "$DEBUG" == "true" ]; then
|
|||||||
echo "RPOOLPW=${RPOOLPW}"
|
echo "RPOOLPW=${RPOOLPW}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Display commands
|
|
||||||
set -x
|
|
||||||
|
|
||||||
# Are the DISK paths block devices? AND
|
# Are the DISK paths block devices? AND
|
||||||
# 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"
|
||||||
@ -173,6 +232,9 @@ fi
|
|||||||
### Step 1: Prepare The Install Environment ###
|
### Step 1: Prepare The Install Environment ###
|
||||||
###############################################
|
###############################################
|
||||||
|
|
||||||
|
# Display commands
|
||||||
|
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
|
||||||
SOURCES_LIST="/etc/apt/sources.list"
|
SOURCES_LIST="/etc/apt/sources.list"
|
||||||
@ -198,20 +260,21 @@ 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"
|
||||||
[ -n "$MIRROR" ] && disk_format "$MIRROR"
|
if [ -n "$MIRROR" ]; then
|
||||||
|
disk_format "$MIRROR"
|
||||||
|
fi
|
||||||
|
sleep 5
|
||||||
|
|
||||||
# 4. Create the boot pool
|
# 4. Create the boot pool
|
||||||
if [ -z "$MIRROR" ]; then
|
|
||||||
create_boot_pool "$ZFSROOT" "${DISK}3"
|
|
||||||
else
|
|
||||||
create_boot_pool "$ZFSROOT" "mirror ${DISK}3 ${MIRROR}3"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 5. Create the root pool
|
# 5. Create the root pool
|
||||||
if [ -z "$MIRROR" ]; then
|
if [ -z "$MIRROR" ]; then
|
||||||
create_root_pool "$ZFSROOT" "${DISK}4" "$RPOOLPW"
|
create_boot_pool "$ZFSROOT" "$(part_path "$DISK" 3)"
|
||||||
|
create_root_pool "$ZFSROOT" "$(part_path "$DISK" 4)" "$RPOOLPW"
|
||||||
else
|
else
|
||||||
create_root_pool "$ZFSROOT" "mirror ${DISK}4 ${MIRROR}4" "$RPOOLPW"
|
create_boot_pool "$ZFSROOT" \
|
||||||
|
"mirror $(part_path "$DISK" 3) $(part_path "$MIRROR" 3)"
|
||||||
|
create_root_pool "$ZFSROOT" \
|
||||||
|
"mirror $(part_path "$DISK" 4) $(part_path "$MIRROR" 4)" "$RPOOLPW"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
###################################
|
###################################
|
||||||
@ -301,7 +364,21 @@ 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=\"$(part_path "$DISK" 2)\"\nROOTPW=\"${ROOTPW}\"" > "$ZFSROOT/var/tmp/zfsenv"
|
||||||
|
|
||||||
|
# Copy self and GRUB mirror helper script into chroot
|
||||||
|
if [ -n "$MIRROR" ]; then
|
||||||
|
cp "$0" "$ZFSROOT/usr/local/bin/debianzfs"
|
||||||
|
chmod u+x "$ZFSROOT/usr/local/bin/debianzfs"
|
||||||
|
HELPER_SCRIPT="/root/MIRROR_GRUB_POSTINSTALL.sh"
|
||||||
|
cat <<-GRUBMIRROR > "${ZFSROOT}${HELPER_SCRIPT}"
|
||||||
|
#!/bin/bash
|
||||||
|
# Post-install GRUB mirror helper script
|
||||||
|
/usr/local/bin/debianzfs \
|
||||||
|
-gm $(disk_by_id "$MIRROR") \
|
||||||
|
$(disk_by_id "$DISK")
|
||||||
|
GRUBMIRROR
|
||||||
|
fi
|
||||||
|
|
||||||
# Bind
|
# Bind
|
||||||
mount --make-private --rbind /dev /mnt/dev
|
mount --make-private --rbind /dev /mnt/dev
|
||||||
@ -316,6 +393,7 @@ export LC_CTYPE=en_US.UTF-8
|
|||||||
export LC_ALL=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
|
||||||
|
|
||||||
@ -336,9 +414,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}"
|
||||||
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})"
|
||||||
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
|
||||||
@ -437,4 +515,6 @@ mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | \
|
|||||||
# 4. If this fails for rpool, mounting it on boot will fail and you will need to
|
# 4. If this fails for rpool, mounting it on boot will fail and you will need to
|
||||||
# zpool import -f rpool, then exit in the initamfs prompt
|
# zpool import -f rpool, then exit in the initamfs prompt
|
||||||
zpool export -a || exit 0
|
zpool export -a || exit 0
|
||||||
|
[ -n "$HELPER_SCRIPT" ] && \
|
||||||
|
echo "NOTICE: A GRUB mirror helper script was placed at $HELPER_SCRIPT"
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user