Added persistent uuids across repeated runs

This commit is contained in:
oddlama 2020-04-21 18:38:46 +02:00
parent b62df43a72
commit 9b854df006
No known key found for this signature in database
GPG Key ID: 88EA325D51D53908
4 changed files with 81 additions and 130 deletions

View File

@ -23,33 +23,34 @@ create_default_disk_layout() {
DISK_ID_ROOT=part_luks DISK_ID_ROOT=part_luks
} }
create_default_disk_layout /dev/sdX
# Example 2: Multiple disks, with raid 0 and luks # Example 2: Multiple disks, with raid 0 and luks
# - efi: partition on all disks, but only first disk used # - efi: partition on all disks, but only first disk used
# - swap: raid 0 → fs # - swap: raid 0 → fs
# - root: raid 0 → luks → fs # - root: raid 0 → luks → fs
devices=(/dev/sd{X,Y}) create_raid0_luks_layout() {
for i in "${!devices[@]}"; do local devices=("$@")
for i in "${!devices[@]}"; do
create_gpt new_id="gpt_dev${i}" device="${devices[$i]}" create_gpt new_id="gpt_dev${i}" device="${devices[$i]}"
create_partition new_id="part_efi_dev${i}" id="gpt_dev${i}" size=128MiB type=efi create_partition new_id="part_efi_dev${i}" id="gpt_dev${i}" size=128MiB type=efi
create_partition new_id="part_swap_dev${i}" id="gpt_dev${i}" size=8GiB type=raid create_partition new_id="part_swap_dev${i}" id="gpt_dev${i}" size=8GiB type=raid
create_partition new_id="part_root_dev${i}" id="gpt_dev${i}" size=remaining type=raid create_partition new_id="part_root_dev${i}" id="gpt_dev${i}" size=remaining type=raid
done done
create_raid new_id=part_raid_swap level=0 ids="$(expand_ids '^part_swap_dev\d$')" create_raid new_id=part_raid_swap level=0 ids="$(expand_ids '^part_swap_dev\d$')"
create_raid new_id=part_raid_root level=0 ids="$(expand_ids '^part_root_dev\d$')" create_raid new_id=part_raid_root level=0 ids="$(expand_ids '^part_root_dev\d$')"
create_luks new_id=part_luks_root id=part_raid_root create_luks new_id=part_luks_root id=part_raid_root
format id=part_efi_dev0 type=efi label=efi format id=part_efi_dev0 type=efi label=efi
format id=part_raid_swap type=swap label=swap format id=part_raid_swap type=swap label=swap
format id=part_luks_root type=ext4 label=root format id=part_luks_root type=ext4 label=root
DISK_ID_EFI=part_efi_dev0 DISK_ID_EFI=part_efi_dev0
DISK_ID_SWAP=part_raid_swap DISK_ID_SWAP=part_raid_swap
DISK_ID_ROOT=part_luks_root DISK_ID_ROOT=part_luks_root
}
create_default_disk_layout /dev/sdX
#create_raid0_luks_layout /dev/sd{X,Y}
################################################ ################################################
# System configuration # System configuration
@ -121,3 +122,8 @@ ANSIBLE_SSH_AUTHORIZED_KEYS=""
# To prove that you have read and edited the config # To prove that you have read and edited the config
# properly, set the following value to true. # properly, set the following value to true.
I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=false I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=false
################################################
# DO NOT EDIT
preprocess_config

View File

@ -34,6 +34,15 @@ check_config() {
[[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BOOT ]] \ [[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BOOT ]] \
|| die "You must assign DISK_ID_EFI or DISK_ID_BOOT" || die "You must assign DISK_ID_EFI or DISK_ID_BOOT"
[[ -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_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]" ]] \
die "Missing uuid for DISK_ID_SWAP, have you made sure it is used?"
[[ -v "DISK_ID_ROOT" ]] && [[ -v "DISK_ID_TO_UUID[$DISK_ID_ROOT]" ]] \
die "Missing uuid for DISK_ID_ROOT, have you made sure it is used?"
if [[ $INSTALL_ANSIBLE == true ]]; then if [[ $INSTALL_ANSIBLE == true ]]; then
[[ $INSTALL_SSHD == true ]] \ [[ $INSTALL_SSHD == true ]] \
|| die "You must enable INSTALL_SSHD for ansible" || die "You must enable INSTALL_SSHD for ansible"
@ -42,11 +51,21 @@ check_config() {
fi fi
} }
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() { prepare_installation_environment() {
einfo "Preparing installation environment" einfo "Preparing installation environment"
check_config
check_has_program gpg check_has_program gpg
check_has_program hwclock check_has_program hwclock
check_has_program lsblk check_has_program lsblk
@ -126,13 +145,18 @@ disk_create_gpt() {
fi fi
local device local device
local device_desc=""
if [[ -v arguments[id] ]]; then if [[ -v arguments[id] ]]; then
device="$(resolve_id_to_device "${arguments[id]}")" device="$(resolve_id_to_device "${arguments[id]}")"
device_desc="$device ($id)"
else else
device="${arguments[device]}" device="${arguments[device]}"
device_desc="$device"
fi fi
disk_id_to_resolvable[$new_id]="raw:$device" disk_id_to_resolvable[$new_id]="raw:$device"
einfo "Creating new gpt partition table ($new_id) on $device_desc"
sgdisk -Z "$device" >/dev/null \ sgdisk -Z "$device" >/dev/null \
|| die "Could not create new gpt partition table ($new_id) on '$device'" || die "Could not create new gpt partition table ($new_id) on '$device'"
partprobe "$device" partprobe "$device"
@ -149,13 +173,13 @@ disk_create_partition() {
fi fi
if [[ $size == "remaining" ]]; then if [[ $size == "remaining" ]]; then
size=0 arg_size=0
else else
size="+$size" arg_size="+$size"
fi fi
local device="$(resolve_id_to_device "$id")" local device="$(resolve_id_to_device "$id")"
local partuuid="$(load_or_generate_uuid "$(base64 -w 0 <<< "$new_id")")" local partuuid="${DISK_ID_TO_UUID[$new_id]}"
case "$type" in case "$type" in
'boot') type='ef02' ;; 'boot') type='ef02' ;;
'efi') type='ef00' ;; 'efi') type='ef00' ;;
@ -167,7 +191,9 @@ disk_create_partition() {
esac esac
disk_id_to_resolvable[$new_id]="partuuid:$partuuid" disk_id_to_resolvable[$new_id]="partuuid:$partuuid"
sgdisk -n "0:0:$size" -t "0:$type" -u 0:"$partuuid" "$device" >/dev/null \
einfo "Creating partition ($new_id) with type=$type, size=$size on $device"
sgdisk -n "0:0:$arg_size" -t "0:$type" -u 0:"$partuuid" "$device" >/dev/null \
|| die "Could not create new gpt partition ($new_id) on '$device' ($id)" || die "Could not create new gpt partition ($new_id) on '$device' ($id)"
partprobe "$device" partprobe "$device"
} }
@ -188,16 +214,22 @@ disk_create_raid() {
return 0 return 0
fi fi
local devices_desc=""
local devices=() local devices=()
local id
# Splitting is intentional here # Splitting is intentional here
# shellcheck disable=SC2086 # shellcheck disable=SC2086
for id in ${ids//';'/ }; do for id in ${ids//';'/ }; do
devices+=("$(resolve_id_to_device "$id")") local dev="$(resolve_id_to_device "$id")"
devices+=("$dev")
devices_desc+="$dev ($id), "
done done
devices_desc="${devices_desc:0:-2}"
local uuid="$(load_or_generate_uuid "$(base64 -w 0 <<< "$new_id")")" local uuid="${DISK_ID_TO_UUID[$new_id]}"
disk_id_to_resolvable[$new_id]="uuid:$uuid" disk_id_to_resolvable[$new_id]="uuid:$uuid"
einfo "Creating raid$level ($new_id) on $devices_desc"
mdadm --create \ mdadm --create \
--uuid="$uuid" \ --uuid="$uuid" \
--level="$level" \ --level="$level" \
@ -214,9 +246,10 @@ disk_create_luks() {
fi fi
local device="$(resolve_id_to_device "$id")" local device="$(resolve_id_to_device "$id")"
local uuid="$(load_or_generate_uuid "$(base64 -w 0 <<< "$new_id")")" local uuid="${DISK_ID_TO_UUID[$new_id]}"
disk_id_to_resolvable[$new_id]="uuid:$uuid" disk_id_to_resolvable[$new_id]="uuid:$uuid"
einfo "Creating luks ($new_id) on $device ($id)"
cryptsetup luksFormat \ cryptsetup luksFormat \
--uuid="$uuid" \ --uuid="$uuid" \
--type=luks2 \ --type=luks2 \
@ -236,6 +269,7 @@ disk_format() {
return 0 return 0
fi fi
einfo "Formatting $device ($id) with $type"
case "$type" in case "$type" in
'boot'|'efi') 'boot'|'efi')
if [[ -v "arguments[label]" ]]; then if [[ -v "arguments[label]" ]]; then
@ -417,95 +451,6 @@ apply_disk_configuration() {
|| die "Error in lsblk") elog || die "Error in lsblk") elog
} }
#partition_device() {
# [[ $ENABLE_PARTITIONING == true ]] \
# || return 0
#
# einfo "Preparing partitioning of device '$PARTITION_DEVICE'"
#
# [[ -b $PARTITION_DEVICE ]] \
# || die "Selected device '$PARTITION_DEVICE' is not a block device"
#
# partition_device_print_config_summary
# ask "Do you really want to apply this partitioning?" \
# || die "For manual partitioning formatting please set ENABLE_PARTITIONING=false in config.sh"
# countdown "Partitioning in " 5
#
# einfo "Partitioning device '$PARTITION_DEVICE'"
#
# # Delete any existing partition table
# sgdisk -Z "$PARTITION_DEVICE" >/dev/null \
# || die "Could not delete existing partition table"
#
# # Create efi/boot partition
# sgdisk -n "0:0:+$PARTITION_EFI_SIZE" -t 0:ef00 -c 0:"efi" -u 0:"$PARTITION_UUID_EFI" "$PARTITION_DEVICE" >/dev/null \
# || die "Could not create efi partition"
#
# # Create swap partition
# if [[ $ENABLE_SWAP == true ]]; then
# sgdisk -n "0:0:+$PARTITION_SWAP_SIZE" -t 0:8200 -c 0:"swap" -u 0:"$PARTITION_UUID_SWAP" "$PARTITION_DEVICE" >/dev/null \
# || die "Could not create swap partition"
# fi
#
# # Create system partition
# sgdisk -n 0:0:0 -t 0:8300 -c 0:"linux" -u 0:"$PARTITION_UUID_LINUX" "$PARTITION_DEVICE" >/dev/null \
# || die "Could not create linux partition"
#
# # Print partition table
# einfo "Applied partition table"
# sgdisk -p "$PARTITION_DEVICE" \
# || die "Could not print partition table"
#
# # Inform kernel of partition table changes
# partprobe "$PARTITION_DEVICE" \
# || die "Could not probe partitions"
#}
#
#format_partitions() {
# [[ $ENABLE_FORMATTING == true ]] \
# || return 0
#
# if [[ $ENABLE_PARTITIONING != true ]]; then
# einfo "Preparing to format the following partitions:"
#
# blkid -t PARTUUID="$PARTITION_UUID_EFI" \
# || die "Error while listing efi partition"
# if [[ $ENABLE_SWAP == true ]]; then
# blkid -t PARTUUID="$PARTITION_UUID_SWAP" \
# || die "Error while listing swap partition"
# fi
# blkid -t PARTUUID="$PARTITION_UUID_LINUX" \
# || die "Error while listing linux partition"
#
# ask "Do you really want to format these partitions?" \
# || die "For manual formatting please set ENABLE_FORMATTING=false in config.sh"
# countdown "Formatting in " 5
# fi
#
# einfo "Formatting partitions"
#
# local dev
# dev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \
# || die "Could not resolve partition UUID '$PARTITION_UUID_EFI'"
# einfo " $dev (efi)"
# mkfs.fat -F 32 -n "efi" "$dev" \
# || die "Could not format EFI partition"
#
# if [[ $ENABLE_SWAP == true ]]; then
# dev="$(get_device_by_partuuid "$PARTITION_UUID_SWAP")" \
# || die "Could not resolve partition UUID '$PARTITION_UUID_SWAP'"
# einfo " $dev (swap)"
# mkswap -L "swap" "$dev" \
# || die "Could not create swap"
# fi
#
# dev="$(get_device_by_partuuid "$PARTITION_UUID_LINUX")" \
# || die "Could not resolve partition UUID '$PARTITION_UUID_LINUX'"
# einfo " $dev (linux)"
# mkfs.ext4 -q -L "linux" "$dev" \
# || die "Could not create ext4 filesystem"
#}
mount_efivars() { mount_efivars() {
# Skip if already mounted # Skip if already mounted
mountpoint -q -- "/sys/firmware/efi/efivars" \ mountpoint -q -- "/sys/firmware/efi/efivars" \
@ -531,13 +476,13 @@ mount_by_partuuid() {
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="$(get_device_by_partuuid "$partuuid")" \
|| die "Could not resolve partition UUID '$PARTITION_UUID_LINUX'" || die "Could not resolve partition UUID '$partuuid'"
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_LINUX" "$ROOT_MOUNTPOINT" mount_by_partuuid "$PARTITION_UUID_ROOT" "$ROOT_MOUNTPOINT"
} }
bind_repo_dir() { bind_repo_dir() {

View File

@ -24,8 +24,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 set to check for existing ids # An associative array to check for existing ids (maps to uuids)
declare -A DISK_KNOWN_IDS 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
declare -A DISK_GPT_HAD_SIZE_REMAINING declare -A DISK_GPT_HAD_SIZE_REMAINING
@ -47,14 +47,14 @@ create_new_id() {
local id="${arguments[$1]}" local id="${arguments[$1]}"
[[ $id == *';'* ]] \ [[ $id == *';'* ]] \
&& die_trace 2 "Identifier contains invalid character ';'" && die_trace 2 "Identifier contains invalid character ';'"
[[ ! -v DISK_KNOWN_IDS[$id] ]] \ [[ ! -v DISK_ID_TO_UUID[$id] ]] \
|| die_trace 2 "Identifier '$id' already exists" || die_trace 2 "Identifier '$id' already exists"
DISK_KNOWN_IDS[$id]=true DISK_ID_TO_UUID[$id]="$(load_or_generate_uuid "$(base64 -w 0 <<< "$id")")"
} }
verify_existing_id() { verify_existing_id() {
local id="${arguments[$1]}" local id="${arguments[$1]}"
[[ -v DISK_KNOWN_IDS[$id] ]] \ [[ -v DISK_ID_TO_UUID[$id] ]] \
|| die_trace 2 "Identifier $1='$id' not found" || die_trace 2 "Identifier $1='$id' not found"
} }
@ -73,7 +73,7 @@ verify_existing_unique_ids() {
# Splitting is intentional here # Splitting is intentional here
# shellcheck disable=SC2086 # shellcheck disable=SC2086
for id in ${ids//';'/ }; do for id in ${ids//';'/ }; do
[[ -v DISK_KNOWN_IDS[$id] ]] \ [[ -v DISK_ID_TO_UUID[$id] ]] \
|| die_trace 2 "$arg=... contains unknown identifier '$id'" || die_trace 2 "$arg=... contains unknown identifier '$id'"
done done
} }
@ -178,7 +178,7 @@ format() {
# Returns a comma separated list of all registered ids matching the given regex. # Returns a comma separated list of all registered ids matching the given regex.
expand_ids() { expand_ids() {
local regex="$1" local regex="$1"
for id in "${!DISK_KNOWN_IDS[@]}"; do for id in "${!DISK_ID_TO_UUID[@]}"; do
[[ $id =~ $regex ]] \ [[ $id =~ $regex ]] \
&& echo -n "$id;" && echo -n "$id;"
done done

View File

@ -108,8 +108,8 @@ install_kernel() {
# Create boot entry # Create boot entry
einfo "Creating efi boot entry" einfo "Creating efi boot entry"
local linuxdev local linuxdev
linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_LINUX")" \ linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_ROOT")" \
|| die "Could not resolve partition UUID '$PARTITION_UUID_LINUX'" || die "Could not resolve partition UUID '$PARTITION_UUID_ROOT'"
local efidev local efidev
efidev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \ efidev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \
|| die "Could not resolve partition UUID '$PARTITION_UUID_EFI'" || die "Could not resolve partition UUID '$PARTITION_UUID_EFI'"
@ -189,11 +189,11 @@ 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_LINUX / ext4 defaults,noatime,errors=remount-ro,discard 0 1" >> /etc/fstab \ echo "PARTUUID=$PARTITION_UUID_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"
echo "PARTUUID=$PARTITION_UUID_EFI /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \ echo "PARTUUID=$PARTITION_UUID_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"
if [[ $ENABLE_SWAP == true ]]; then if [[ -v "PARTITION_UUID_SWAP" ]]; then
echo "PARTUUID=$PARTITION_UUID_SWAP none swap defaults,discard 0 0" >> /etc/fstab \ echo "PARTUUID=$PARTITION_UUID_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