Replaced old PARTITION_UUID variables with new disk id system

This commit is contained in:
oddlama 2020-04-22 23:48:36 +02:00
parent 486d22db79
commit 88ad973072
No known key found for this signature in database
GPG Key ID: 88EA325D51D53908
4 changed files with 41 additions and 63 deletions

View File

@ -13,14 +13,14 @@ source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" || exit 1
# `dd if=/dev/urandom bs=1024 count=1 of=/path/to/keyfile` # `dd if=/dev/urandom bs=1024 count=1 of=/path/to/keyfile`
# 2. Copy the keyfile somewhere safe, but don't delete the original, # 2. Copy the keyfile somewhere safe, but don't delete the original,
# which we will use in the live environment. # which we will use in the live environment.
# 3. Use `cat /path/to/keyfile` below. # 3. Use `echo -n /path/to/keyfile` below.
# Example: GPG Smartcard # Example: GPG Smartcard
# Same as above, but do not store a copy of the keyfile and instead store a # Same as above, but do not store a copy of the keyfile and instead store a
# gpg encrypted copy: `cat /path/to/keyfile | gpg --symmetric --cipher-algo AES256 --s2k-digest-algo SHA512 --output /my/permanent/storage/luks-key.gpg` # gpg encrypted copy: `cat /path/to/keyfile | gpg --symmetric --cipher-algo AES256 --s2k-digest-algo SHA512 --output /my/permanent/storage/luks-key.gpg`
luks_getkey() { luks_getkeyfile() {
case "$1" in case "$1" in
#'my_luks_partition') echo -n 'yourLuksPassword_MakeItLongAndStrong' ;; #'my_luks_partition') echo -n '/path/to/my_luks_partition_keyfile' ;;
*) cat "/path/to/luks-keyfile" ;; *) echo -n "/path/to/luks-keyfile" ;;
esac esac
} }

View File

@ -31,11 +31,11 @@ check_config() {
[[ -n $DISK_ID_ROOT ]] \ [[ -n $DISK_ID_ROOT ]] \
|| die "You must assign DISK_ID_ROOT" || die "You must assign DISK_ID_ROOT"
[[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BOOT ]] \ [[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BIOS ]] \
|| die "You must assign DISK_ID_EFI or DISK_ID_BOOT" || die "You must assign DISK_ID_EFI or DISK_ID_BIOS"
[[ -v "DISK_ID_BOOT" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_BOOT]" ]] \ [[ -v "DISK_ID_BIOS" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_BIOS]" ]] \
&& die "Missing uuid for DISK_ID_BOOT, have you made sure it is used?" && die "Missing uuid for DISK_ID_BIOS, have you made sure it is used?"
[[ -v "DISK_ID_EFI" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_EFI]" ]] \ [[ -v "DISK_ID_EFI" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_EFI]" ]] \
&& die "Missing uuid for DISK_ID_EFI, have you made sure it is used?" && die "Missing uuid for DISK_ID_EFI, have you made sure it is used?"
[[ -v "DISK_ID_SWAP" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_SWAP]" ]] \ [[ -v "DISK_ID_SWAP" ]] && [[ ! -v "DISK_ID_TO_UUID[$DISK_ID_SWAP]" ]] \
@ -59,14 +59,6 @@ check_config() {
preprocess_config() { preprocess_config() {
check_config check_config
[[ -v "DISK_ID_TO_UUID[$DISK_ID_BOOT]" ]] \
&& PARTITION_UUID_BOOT="${DISK_ID_TO_UUID[$DISK_ID_BOOT]}"
[[ -v "DISK_ID_TO_UUID[$DISK_ID_EFI]" ]] \
&& PARTITION_UUID_EFI="${DISK_ID_TO_UUID[$DISK_ID_EFI]}"
[[ -v "DISK_ID_TO_UUID[$DISK_ID_SWAP]" ]] \
&& PARTITION_UUID_SWAP="${DISK_ID_TO_UUID[$DISK_ID_SWAP]}"
PARTITION_UUID_ROOT="${DISK_ID_TO_UUID[$DISK_ID_ROOT]}"
} }
prepare_installation_environment() { prepare_installation_environment() {
@ -100,7 +92,7 @@ add_summary_entry() {
local ptr local ptr
case "$id" in case "$id" in
"$DISK_ID_BOOT") ptr="← bios" ;; "$DISK_ID_BIOS") ptr="← bios" ;;
"$DISK_ID_EFI") ptr="← efi" ;; "$DISK_ID_EFI") ptr="← efi" ;;
"$DISK_ID_SWAP") ptr="← swap" ;; "$DISK_ID_SWAP") ptr="← swap" ;;
"$DISK_ID_ROOT") ptr="← root" ;; "$DISK_ID_ROOT") ptr="← root" ;;
@ -123,7 +115,7 @@ summary_color_args() {
done done
} }
resolve_id_to_device() { resolve_device_by_id() {
local id="$1" local id="$1"
[[ -v disk_id_to_resolvable[$id] ]] \ [[ -v disk_id_to_resolvable[$id] ]] \
|| die "Cannot resolve id='$id' to a block device (no table entry)" || die "Cannot resolve id='$id' to a block device (no table entry)"
@ -155,7 +147,7 @@ disk_create_gpt() {
local device local device
local device_desc="" local device_desc=""
if [[ -v arguments[id] ]]; then if [[ -v arguments[id] ]]; then
device="$(resolve_id_to_device "${arguments[id]}")" device="$(resolve_device_by_id "${arguments[id]}")"
device_desc="$device ($id)" device_desc="$device ($id)"
else else
device="${arguments[device]}" device="${arguments[device]}"
@ -188,7 +180,7 @@ disk_create_partition() {
arg_size="+$size" arg_size="+$size"
fi fi
local device="$(resolve_id_to_device "$id")" local device="$(resolve_device_by_id "$id")"
local partuuid="${DISK_ID_TO_UUID[$new_id]}" local partuuid="${DISK_ID_TO_UUID[$new_id]}"
local extra_args="" local extra_args=""
case "$type" in case "$type" in
@ -233,7 +225,7 @@ disk_create_raid() {
# Splitting is intentional here # Splitting is intentional here
# shellcheck disable=SC2086 # shellcheck disable=SC2086
for id in ${ids//';'/ }; do for id in ${ids//';'/ }; do
local dev="$(resolve_id_to_device "$id")" local dev="$(resolve_device_by_id "$id")"
devices+=("$dev") devices+=("$dev")
devices_desc+="$dev ($id), " devices_desc+="$dev ($id), "
done done
@ -265,25 +257,24 @@ disk_create_luks() {
return 0 return 0
fi fi
local device="$(resolve_id_to_device "$id")" local device="$(resolve_device_by_id "$id")"
local uuid="${DISK_ID_TO_UUID[$new_id]}" local uuid="${DISK_ID_TO_UUID[$new_id]}"
disk_id_to_resolvable[$new_id]="luks:$uuid" disk_id_to_resolvable[$new_id]="luks:$uuid"
einfo "Creating luks ($new_id) on $device ($id)" einfo "Creating luks ($new_id) on $device ($id)"
local luks_key local keyfile
luks_key="$(luks_getkey "$new_id")" \ keyfile="$(luks_getkeyfile "$new_id")" \
|| die "Error in luks_getkey for id=$id" || die "Error in luks_getkeyfile for id=$id"
cryptsetup luksFormat \ cryptsetup luksFormat \
--type luks2 \ --type luks2 \
--uuid "$uuid" \ --uuid "$uuid" \
--key-file '-' \ --key-file "$keyfile" \
--cipher aes-xts-plain64 \ --cipher aes-xts-plain64 \
--hash sha512 \ --hash sha512 \
--pbkdf argon2id \ --pbkdf argon2id \
--iter-time 4000 \ --iter-time 4000 \
--key-size 512 \ --key-size 512 \
"$device" \ "$device" \
<<< "$luks_key" \
|| die "Could not create luks on '$device' ($id)" || die "Could not create luks on '$device' ($id)"
mkdir -p "$LUKS_HEADER_BACKUP_DIR" \ mkdir -p "$LUKS_HEADER_BACKUP_DIR" \
|| die "Could not create luks header backup dir '$LUKS_HEADER_BACKUP_DIR'" || die "Could not create luks header backup dir '$LUKS_HEADER_BACKUP_DIR'"
@ -291,9 +282,8 @@ disk_create_luks() {
--header-backup-file "$LUKS_HEADER_BACKUP_DIR/luks-header-$id-${uuid,,}.img" \ --header-backup-file "$LUKS_HEADER_BACKUP_DIR/luks-header-$id-${uuid,,}.img" \
|| die "Could not backup luks header on '$device' ($id)" || die "Could not backup luks header on '$device' ($id)"
cryptsetup open --type luks2 \ cryptsetup open --type luks2 \
--key-file '-' \ --key-file "$keyfile" \
"$device" "${uuid,,}" \ "$device" "${uuid,,}" \
<<< "$luks_key" \
|| die "Could not open luks header on '$device' ($id)" || die "Could not open luks header on '$device' ($id)"
} }
@ -306,7 +296,7 @@ disk_format() {
return 0 return 0
fi fi
local device="$(resolve_id_to_device "$id")" local device="$(resolve_device_by_id "$id")"
einfo "Formatting $device ($id) with $type" einfo "Formatting $device ($id) with $type"
case "$type" in case "$type" in
'bios'|'efi') 'bios'|'efi')
@ -500,9 +490,9 @@ mount_efivars() {
|| die "Could not mount efivarfs" || die "Could not mount efivarfs"
} }
mount_by_partuuid() { mount_by_id() {
local dev local dev
local partuuid="$1" local id="$1"
local mountpoint="$2" local mountpoint="$2"
# Skip if already mounted # Skip if already mounted
@ -510,17 +500,17 @@ mount_by_partuuid() {
&& return && return
# Mount device # Mount device
einfo "Mounting device partuuid=$partuuid to '$mountpoint'" einfo "Mounting device with id=$id to '$mountpoint'"
mkdir -p "$mountpoint" \ mkdir -p "$mountpoint" \
|| die "Could not create mountpoint directory '$mountpoint'" || die "Could not create mountpoint directory '$mountpoint'"
dev="$(get_device_by_partuuid "$partuuid")" \ dev="$(resolve_device_by_id "$id")" \
|| die "Could not resolve partition UUID '$partuuid'" || die "Could not resolve device with id=$id"
mount "$dev" "$mountpoint" \ mount "$dev" "$mountpoint" \
|| die "Could not mount device '$dev'" || die "Could not mount device '$dev'"
} }
mount_root() { mount_root() {
mount_by_partuuid "$PARTITION_UUID_ROOT" "$ROOT_MOUNTPOINT" mount_by_id "$DISK_ID_ROOT" "$ROOT_MOUNTPOINT"
} }
bind_repo_dir() { bind_repo_dir() {

View File

@ -23,8 +23,8 @@ USED_LUKS=false
# An array of disk related actions to perform # An array of disk related actions to perform
DISK_ACTIONS=() DISK_ACTIONS=()
# An associative array from partuuid to disk uuid # An associative array from disk id to parent gpt disk id (only for partitions)
declare -A DISK_PARTUUID_TO_GPT_UUID declare -A DISK_ID_PART_TO_GPT_ID
# An associative array to check for existing ids (maps to uuids) # An associative array to check for existing ids (maps to uuids)
declare -A DISK_ID_TO_UUID declare -A DISK_ID_TO_UUID
# An associative set to check for correct usage of size=remaining in gpt tables # An associative set to check for correct usage of size=remaining in gpt tables
@ -133,7 +133,7 @@ create_partition() {
[[ ${arguments[size]} == "remaining" ]] \ [[ ${arguments[size]} == "remaining" ]] \
&& DISK_GPT_HAD_SIZE_REMAINING[${arguments[id]}]=true && DISK_GPT_HAD_SIZE_REMAINING[${arguments[id]}]=true
DISK_PARTUUID_TO_GPT_UUID[${DISK_ID_TO_UUID[${arguments[new_id]}]}]="${DISK_ID_TO_UUID[${arguments[id]}]}" DISK_ID_PART_TO_GPT_ID[${arguments[new_id]}]="${arguments[id]}"
DISK_ACTIONS+=("action=create_partition" "$@" ";") DISK_ACTIONS+=("action=create_partition" "$@" ";")
} }

View File

@ -107,17 +107,10 @@ install_kernel_efi() {
# Create boot entry # Create boot entry
einfo "Creating efi boot entry" einfo "Creating efi boot entry"
local linuxdev local linuxdev="$(resolve_device_by_id "$DISK_ID_ROOT")"
linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_ROOT")" \ local efipartdev="$(resolve_device_by_id "$DISK_ID_EFI")"
|| die "Could not resolve partition UUID '$PARTITION_UUID_ROOT'"
local efipartdev
efipartdev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \
|| die "Could not resolve partition UUID '$PARTITION_UUID_EFI'"
local efipartnum="${efipartdev: -1}" local efipartnum="${efipartdev: -1}"
local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_EFI]}" local gptdev="$(resolve_device_by_id "${DISK_ID_PART_TO_GPT_ID[$DISK_ID_EFI]}")"
local gptdev
gptdev="$(get_device_by_ptuuid "$gptuuid")" \
|| die "Could not resolve GPT UUID '$gptuuid'"
try efibootmgr --verbose --create --disk "$gptdev" --part "$efipartnum" --label "gentoo" --loader '\EFI\vmlinuz.efi' --unicode "root=$linuxdev initrd=\\EFI\\initramfs.img" try efibootmgr --verbose --create --disk "$gptdev" --part "$efipartnum" --label "gentoo" --loader '\EFI\vmlinuz.efi' --unicode "root=$linuxdev initrd=\\EFI\\initramfs.img"
} }
@ -126,13 +119,8 @@ install_kernel_bios() {
# Install syslinux MBR record # Install syslinux MBR record
einfo "Copying syslinux MBR record" einfo "Copying syslinux MBR record"
local biosdev local biosdev="$(resolve_device_by_id "$DISK_ID_BIOS")"
biosdev="$(get_device_by_partuuid "$PARTITION_UUID_BIOS")" \ local gptdev="$(resolve_device_by_id "${DISK_ID_PART_TO_GPT_ID[$DISK_ID_BIOS]}")"
|| die "Could not resolve partition UUID '$PARTITION_UUID_BIOS'"
local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_BIOS]}"
local gptdev
gptdev="$(get_device_by_ptuuid "$gptuuid")" \
|| die "Could not resolve GPT UUID '$gptuuid'"
try dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of="$gptdev" try dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of="$gptdev"
# Install syslinux # Install syslinux
@ -190,11 +178,11 @@ main_install_gentoo_in_chroot() {
# Mount efi partition # Mount efi partition
mount_efivars mount_efivars
einfo "Mounting efi partition" einfo "Mounting efi partition"
mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi" mount_by_id "$DISK_ID_EFI" "/boot/efi"
else else
# Mount boot partition # Mount boot partition
einfo "Mounting boot partition" einfo "Mounting boot partition"
mount_by_partuuid "$PARTITION_UUID_BIOS" "/boot" mount_by_id "$DISK_ID_BIOS" "/boot"
fi fi
# Sync portage # Sync portage
@ -230,17 +218,17 @@ main_install_gentoo_in_chroot() {
einfo "Generating fstab" einfo "Generating fstab"
install -m0644 -o root -g root "$GENTOO_INSTALL_REPO_DIR/configs/fstab" /etc/fstab \ install -m0644 -o root -g root "$GENTOO_INSTALL_REPO_DIR/configs/fstab" /etc/fstab \
|| die "Could not overwrite /etc/fstab" || die "Could not overwrite /etc/fstab"
echo "PARTUUID=$PARTITION_UUID_ROOT / ext4 defaults,noatime,errors=remount-ro,discard 0 1" >> /etc/fstab \ echo "$(resolve_device_by_id "$DISK_ID_ROOT") / ext4 defaults,noatime,errors=remount-ro,discard 0 1" >> /etc/fstab \
|| die "Could not append entry to fstab" || die "Could not append entry to fstab"
if [[ $IS_EFI == "true" ]]; then if [[ $IS_EFI == "true" ]]; then
echo "PARTUUID=$PARTITION_UUID_EFI /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ echo "$(resolve_device_by_id "$DISK_ID_EFI") /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \
|| die "Could not append entry to fstab" || die "Could not append entry to fstab"
else else
echo "PARTUUID=$PARTITION_UUID_BIOS /boot vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ echo "$(resolve_device_by_id "$DISK_ID_BIOS") /boot vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \
|| die "Could not append entry to fstab" || die "Could not append entry to fstab"
fi fi
if [[ -v "PARTITION_UUID_SWAP" ]]; then if [[ -v "DISK_ID_SWAP" ]]; then
echo "PARTUUID=$PARTITION_UUID_SWAP none swap defaults,discard 0 0" >> /etc/fstab \ echo "$(resolve_device_by_id "$DISK_ID_SWAP") none swap defaults,discard 0 0" >> /etc/fstab \
|| die "Could not append entry to fstab" || die "Could not append entry to fstab"
fi fi