2020-01-08 16:21:01 -01:00
|
|
|
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
|
|
|
|
################################################
|
|
|
|
# Script internal configuration
|
|
|
|
|
|
|
|
# The temporary directory for this script,
|
|
|
|
# must reside in /tmp to allow the chrooted system to access the files
|
2020-01-08 16:21:01 -01:00
|
|
|
TMP_DIR="/tmp/gentoo-install"
|
2019-12-31 14:28:42 -01:00
|
|
|
# Mountpoint for the new system
|
|
|
|
ROOT_MOUNTPOINT="$TMP_DIR/root"
|
|
|
|
# Mountpoint for the script files for access from chroot
|
2020-01-08 16:21:01 -01:00
|
|
|
GENTOO_INSTALL_REPO_BIND="$TMP_DIR/bind"
|
2019-12-31 14:28:42 -01:00
|
|
|
# Mountpoint for the script files for access from chroot
|
|
|
|
UUID_STORAGE_DIR="$TMP_DIR/uuids"
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
# Flag to track usage of raid (needed to check for mdadm existence)
|
|
|
|
USED_RAID=false
|
|
|
|
# Flag to track usage of luks (needed to check for cryptsetup existence)
|
|
|
|
USED_LUKS=false
|
|
|
|
|
2020-04-19 19:34:36 +00:00
|
|
|
# An array of disk related actions to perform
|
|
|
|
DISK_ACTIONS=()
|
2020-04-21 18:48:23 +00:00
|
|
|
# An associative array from partuuid to disk uuid
|
|
|
|
declare -A DISK_PARTUUID_TO_GPT_UUID
|
2020-04-21 16:38:46 +00:00
|
|
|
# An associative array to check for existing ids (maps to uuids)
|
|
|
|
declare -A DISK_ID_TO_UUID
|
2020-04-21 12:33:42 +00:00
|
|
|
# An associative set to check for correct usage of size=remaining in gpt tables
|
|
|
|
declare -A DISK_GPT_HAD_SIZE_REMAINING
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
only_one_of() {
|
|
|
|
local previous=""
|
|
|
|
local a
|
|
|
|
for a in "$@"; do
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ -v arguments[$a] ]]; then
|
|
|
|
if [[ -z $previous ]]; then
|
2020-04-19 19:34:36 +00:00
|
|
|
previous="$a"
|
|
|
|
else
|
|
|
|
die_trace 2 "Only one of the arguments ($*) can be given"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
create_new_id() {
|
|
|
|
local id="${arguments[$1]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
[[ $id == *';'* ]] \
|
|
|
|
&& die_trace 2 "Identifier contains invalid character ';'"
|
2020-04-21 16:38:46 +00:00
|
|
|
[[ ! -v DISK_ID_TO_UUID[$id] ]] \
|
2020-04-19 19:34:36 +00:00
|
|
|
|| die_trace 2 "Identifier '$id' already exists"
|
2020-04-21 16:38:46 +00:00
|
|
|
DISK_ID_TO_UUID[$id]="$(load_or_generate_uuid "$(base64 -w 0 <<< "$id")")"
|
2020-04-19 19:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
verify_existing_id() {
|
|
|
|
local id="${arguments[$1]}"
|
2020-04-21 16:38:46 +00:00
|
|
|
[[ -v DISK_ID_TO_UUID[$id] ]] \
|
2020-04-19 19:34:36 +00:00
|
|
|
|| die_trace 2 "Identifier $1='$id' not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
verify_existing_unique_ids() {
|
2020-04-20 22:40:54 +00:00
|
|
|
local arg="$1"
|
|
|
|
local ids="${arguments[$arg]}"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
count_orig="$(tr ';' '\n' <<< "$ids" | grep -c '\S')"
|
|
|
|
count_uniq="$(tr ';' '\n' <<< "$ids" | grep '\S' | sort -u | wc -l)"
|
|
|
|
[[ $count_orig -gt 0 ]] \
|
|
|
|
|| die_trace 2 "$arg=... must contain at least one entry"
|
|
|
|
[[ $count_orig -eq $count_uniq ]] \
|
|
|
|
|| die_trace 2 "$arg=... contains duplicate identifiers"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
local id
|
2020-04-19 19:34:36 +00:00
|
|
|
# Splitting is intentional here
|
2020-04-20 22:40:54 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
for id in ${ids//';'/ }; do
|
2020-04-21 16:38:46 +00:00
|
|
|
[[ -v DISK_ID_TO_UUID[$id] ]] \
|
2020-04-20 22:40:54 +00:00
|
|
|
|| die_trace 2 "$arg=... contains unknown identifier '$id'"
|
2020-04-19 19:34:36 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
verify_option() {
|
|
|
|
local opt="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
local arg="${arguments[$opt]}"
|
|
|
|
local i
|
|
|
|
for i in "$@"; do
|
2020-04-20 22:40:54 +00:00
|
|
|
[[ $i == "$arg" ]] \
|
2020-04-19 19:34:36 +00:00
|
|
|
&& return 0
|
|
|
|
done
|
|
|
|
|
|
|
|
die_trace 2 "Invalid option $opt='$arg', must be one of ($*)"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Named arguments:
|
2020-04-20 22:40:54 +00:00
|
|
|
# new_id: Id for the new gpt table
|
|
|
|
# device: The operand block device
|
|
|
|
create_gpt() {
|
|
|
|
local known_arguments=('+new_id' '+device|id')
|
2020-04-21 19:52:46 +00:00
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
only_one_of device id
|
|
|
|
create_new_id new_id
|
2020-04-20 22:40:54 +00:00
|
|
|
[[ -v arguments[id] ]] \
|
2020-04-19 19:34:36 +00:00
|
|
|
&& verify_existing_id id
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
DISK_ACTIONS+=("action=create_gpt" "$@" ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Named arguments:
|
|
|
|
# new_id: Id for the new partition
|
2020-04-21 12:33:42 +00:00
|
|
|
# size: Size for the new partition, or 'remaining' to allocate the rest
|
2020-04-21 19:52:46 +00:00
|
|
|
# type: The parition type, either (bios, efi, swap, raid, luks, linux) (or a 4 digit hex-code for gdisk).
|
2020-04-20 22:40:54 +00:00
|
|
|
# id: The operand device id
|
|
|
|
create_partition() {
|
|
|
|
local known_arguments=('+new_id' '+id' '+size' '+type')
|
2020-04-21 19:52:46 +00:00
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
|
|
|
create_new_id new_id
|
|
|
|
verify_existing_id id
|
2020-04-21 19:52:46 +00:00
|
|
|
verify_option type bios efi swap raid luks linux
|
2020-04-20 22:40:54 +00:00
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
[[ -v "DISK_GPT_HAD_SIZE_REMAINING[${arguments[id]}]" ]] \
|
|
|
|
&& die_trace 1 "Cannot add another partition to table (${arguments[id]}) after size=remaining was used"
|
|
|
|
|
|
|
|
[[ ${arguments[size]} == "remaining" ]] \
|
|
|
|
&& DISK_GPT_HAD_SIZE_REMAINING[${arguments[id]}]=true
|
|
|
|
|
2020-04-21 18:51:11 +00:00
|
|
|
DISK_PARTUUID_TO_GPT_UUID[${DISK_ID_TO_UUID[${arguments[new_id]}]}]="${DISK_ID_TO_UUID[${arguments[id]}]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
DISK_ACTIONS+=("action=create_partition" "$@" ";")
|
2020-04-19 19:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Named arguments:
|
|
|
|
# new_id: Id for the new raid
|
|
|
|
# level: Raid level
|
2020-04-20 22:40:54 +00:00
|
|
|
# ids: Comma separated list of all member ids
|
2020-04-19 19:34:36 +00:00
|
|
|
create_raid() {
|
2020-04-20 22:40:54 +00:00
|
|
|
USED_RAID=true
|
|
|
|
|
2020-04-19 19:34:36 +00:00
|
|
|
local known_arguments=('+new_id' '+level' '+ids')
|
2020-04-21 19:52:46 +00:00
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
create_new_id new_id
|
|
|
|
verify_option level 0 1 5 6
|
|
|
|
verify_existing_unique_ids ids
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
DISK_ACTIONS+=("action=create_raid" "$@" ";")
|
2020-04-19 19:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Named arguments:
|
|
|
|
# new_id: Id for the new luks
|
2020-04-20 22:40:54 +00:00
|
|
|
# id: The operand device id
|
2020-04-19 19:34:36 +00:00
|
|
|
create_luks() {
|
2020-04-20 22:40:54 +00:00
|
|
|
USED_LUKS=true
|
|
|
|
|
|
|
|
local known_arguments=('+new_id' '+id')
|
2020-04-21 19:52:46 +00:00
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
create_new_id new_id
|
2020-04-20 22:40:54 +00:00
|
|
|
verify_existing_id id
|
2020-04-19 19:34:36 +00:00
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
DISK_ACTIONS+=("action=create_luks" "$@" ";")
|
2020-04-19 19:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Named arguments:
|
|
|
|
# id: Id of the device / partition created earlier
|
2020-04-21 19:52:46 +00:00
|
|
|
# type: One of (bios, efi, swap, ext4)
|
2020-04-19 19:34:36 +00:00
|
|
|
# label: The label for the formatted disk
|
|
|
|
format() {
|
2020-04-20 22:40:54 +00:00
|
|
|
local known_arguments=('+id' '+type' '?label')
|
2020-04-21 19:52:46 +00:00
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
verify_existing_id id
|
2020-04-21 19:52:46 +00:00
|
|
|
verify_option type bios efi swap ext4
|
2020-04-19 19:34:36 +00:00
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
DISK_ACTIONS+=("action=format" "$@" ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Returns a comma separated list of all registered ids matching the given regex.
|
|
|
|
expand_ids() {
|
|
|
|
local regex="$1"
|
2020-04-21 16:38:46 +00:00
|
|
|
for id in "${!DISK_ID_TO_UUID[@]}"; do
|
2020-04-20 22:40:54 +00:00
|
|
|
[[ $id =~ $regex ]] \
|
|
|
|
&& echo -n "$id;"
|
|
|
|
done
|
2020-04-19 19:34:36 +00:00
|
|
|
}
|
2020-04-21 16:43:17 +00:00
|
|
|
|
|
|
|
# Example 1: Single disk, 3 partitions (efi, swap, root)
|
2020-04-21 19:52:46 +00:00
|
|
|
# Parameters:
|
|
|
|
# swap=<size> create a swap partition with given size, or no swap if set to false
|
|
|
|
# type=[efi|bios] defaults to efi. Selects the boot type.
|
2020-04-21 16:43:17 +00:00
|
|
|
create_default_disk_layout() {
|
2020-04-21 19:52:46 +00:00
|
|
|
local known_arguments=('+device' '+swap')
|
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
|
|
|
|
|
|
|
[[ ${#extra_arguments[@]} -eq 1 ]] \
|
|
|
|
|| die_trace 1 "Expected exactly one positional argument (the device)"
|
|
|
|
local device="${extra_arguments[0]}"
|
|
|
|
local size_swap="${arguments[swap]}"
|
|
|
|
local type="${arguments[type]}"
|
|
|
|
local efi=true
|
|
|
|
case "$type" in
|
|
|
|
'bios') efi=false ;;
|
|
|
|
'efi') efi=true ;;
|
|
|
|
*) die_trace 1 "Invalid argument type=$type, must be one of (bios, efi)" ;;
|
|
|
|
esac
|
2020-04-21 16:43:17 +00:00
|
|
|
|
|
|
|
create_gpt new_id=gpt device="$device"
|
2020-04-21 19:52:46 +00:00
|
|
|
create_partition new_id="part_$type" id=gpt size=128MiB type="$type"
|
|
|
|
[[ $size_swap != "false" ]] && \
|
|
|
|
create_partition new_id=part_swap id=gpt size="$size_swap" type=swap
|
|
|
|
create_partition new_id=part_root id=gpt size=remaining type=linux
|
2020-04-21 16:43:17 +00:00
|
|
|
|
2020-04-21 19:52:46 +00:00
|
|
|
format id="part_$type" type="$type" label="$type"
|
|
|
|
[[ $size_swap != "false" ]] && \
|
2020-04-21 16:43:17 +00:00
|
|
|
format id=part_swap type=swap label=swap
|
|
|
|
format id=part_root type=ext4 label=root
|
|
|
|
|
2020-04-21 19:52:46 +00:00
|
|
|
if [[ $type == "efi" ]]; then
|
|
|
|
DISK_ID_EFI="part_$type"
|
|
|
|
else
|
|
|
|
DISK_ID_BIOS="part_$type"
|
|
|
|
fi
|
2020-04-21 16:43:17 +00:00
|
|
|
DISK_ID_SWAP=part_raid
|
|
|
|
DISK_ID_ROOT=part_luks
|
|
|
|
}
|
|
|
|
|
|
|
|
# Example 2: Multiple disks, with raid 0 and luks
|
|
|
|
# - efi: partition on all disks, but only first disk used
|
|
|
|
# - swap: raid 0 → fs
|
|
|
|
# - root: raid 0 → luks → fs
|
2020-04-21 19:52:46 +00:00
|
|
|
# Parameters:
|
|
|
|
# swap=<size> create a swap partition with given size, or no swap if set to false
|
|
|
|
# type=[efi|bios] defaults to efi. Selects the boot type.
|
2020-04-21 16:43:17 +00:00
|
|
|
create_raid0_luks_layout() {
|
2020-04-21 19:52:46 +00:00
|
|
|
local known_arguments=('+device' '+swap')
|
|
|
|
local extra_arguments=()
|
|
|
|
declare -A arguments; parse_arguments "$@"
|
|
|
|
|
|
|
|
[[ ${#extra_arguments[@]} -gt 0 ]] \
|
|
|
|
|| die_trace 1 "Expected at least one positional argument (the devices)"
|
|
|
|
local size_swap="${arguments[swap]}"
|
|
|
|
local type="${arguments[type]}"
|
|
|
|
local efi=true
|
|
|
|
case "$type" in
|
|
|
|
'bios') efi=false ;;
|
|
|
|
'efi') efi=true ;;
|
|
|
|
*) die_trace 1 "Invalid argument type=$type, must be one of (bios, efi)" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
for i in "${!extra_arguments[@]}"; do
|
|
|
|
create_gpt new_id="gpt_dev${i}" device="${extra_arguments[$i]}"
|
|
|
|
create_partition new_id="part_${type}_dev${i}" id="gpt_dev${i}" size=128MiB type="$type"
|
|
|
|
[[ $size_swap != "false" ]] && \
|
|
|
|
create_partition new_id="part_swap_dev${i}" id="gpt_dev${i}" size="$size_swap" type=raid
|
|
|
|
create_partition new_id="part_root_dev${i}" id="gpt_dev${i}" size=remaining type=raid
|
2020-04-21 16:43:17 +00:00
|
|
|
done
|
|
|
|
|
2020-04-21 19:52:46 +00:00
|
|
|
[[ $size_swap != "false" ]] && \
|
2020-04-21 16:43:17 +00:00
|
|
|
create_raid new_id=part_raid_swap level=0 ids="$(expand_ids '^part_swap_dev[[:digit:]]$')"
|
|
|
|
create_raid new_id=part_raid_root level=0 ids="$(expand_ids '^part_root_dev[[:digit:]]$')"
|
|
|
|
create_luks new_id=part_luks_root id=part_raid_root
|
|
|
|
|
2020-04-21 19:52:46 +00:00
|
|
|
format id="part_${type}_dev0" type="$type" label="$type"
|
|
|
|
[[ $size_swap != "false" ]] && \
|
2020-04-21 16:43:17 +00:00
|
|
|
format id=part_raid_swap type=swap label=swap
|
|
|
|
format id=part_luks_root type=ext4 label=root
|
|
|
|
|
2020-04-21 19:52:46 +00:00
|
|
|
if [[ $type == "efi" ]]; then
|
|
|
|
DISK_ID_EFI="part_${type}_dev0"
|
|
|
|
else
|
|
|
|
DISK_ID_BIOS="part_${type}_dev0"
|
|
|
|
fi
|
|
|
|
[[ $size_swap != "false" ]] && \
|
2020-04-21 16:43:17 +00:00
|
|
|
DISK_ID_SWAP=part_raid_swap
|
|
|
|
DISK_ID_ROOT=part_luks_root
|
|
|
|
}
|