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`
# 2. Copy the keyfile somewhere safe, but don't delete the original,
# 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
# 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`
luks_getkey() {
luks_getkeyfile() {
case "$1" in
#'my_luks_partition') echo -n 'yourLuksPassword_MakeItLongAndStrong' ;;
*) cat "/path/to/luks-keyfile" ;;
#'my_luks_partition') echo -n '/path/to/my_luks_partition_keyfile' ;;
*) echo -n "/path/to/luks-keyfile" ;;
esac
}

View File

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

View File

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

View File

@ -107,17 +107,10 @@ install_kernel_efi() {
# Create boot entry
einfo "Creating efi boot entry"
local linuxdev
linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_ROOT")" \
|| 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 linuxdev="$(resolve_device_by_id "$DISK_ID_ROOT")"
local efipartdev="$(resolve_device_by_id "$DISK_ID_EFI")"
local efipartnum="${efipartdev: -1}"
local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_EFI]}"
local gptdev
gptdev="$(get_device_by_ptuuid "$gptuuid")" \
|| die "Could not resolve GPT UUID '$gptuuid'"
local gptdev="$(resolve_device_by_id "${DISK_ID_PART_TO_GPT_ID[$DISK_ID_EFI]}")"
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
einfo "Copying syslinux MBR record"
local biosdev
biosdev="$(get_device_by_partuuid "$PARTITION_UUID_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'"
local biosdev="$(resolve_device_by_id "$DISK_ID_BIOS")"
local gptdev="$(resolve_device_by_id "${DISK_ID_PART_TO_GPT_ID[$DISK_ID_BIOS]}")"
try dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of="$gptdev"
# Install syslinux
@ -190,11 +178,11 @@ main_install_gentoo_in_chroot() {
# Mount efi partition
mount_efivars
einfo "Mounting efi partition"
mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi"
mount_by_id "$DISK_ID_EFI" "/boot/efi"
else
# Mount boot partition
einfo "Mounting boot partition"
mount_by_partuuid "$PARTITION_UUID_BIOS" "/boot"
mount_by_id "$DISK_ID_BIOS" "/boot"
fi
# Sync portage
@ -230,17 +218,17 @@ main_install_gentoo_in_chroot() {
einfo "Generating fstab"
install -m0644 -o root -g root "$GENTOO_INSTALL_REPO_DIR/configs/fstab" /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"
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"
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"
fi
if [[ -v "PARTITION_UUID_SWAP" ]]; then
echo "PARTUUID=$PARTITION_UUID_SWAP none swap defaults,discard 0 0" >> /etc/fstab \
if [[ -v "DISK_ID_SWAP" ]]; then
echo "$(resolve_device_by_id "$DISK_ID_SWAP") none swap defaults,discard 0 0" >> /etc/fstab \
|| die "Could not append entry to fstab"
fi