2021-04-28 14:06:52 +00:00
|
|
|
|
# shellcheck source=./scripts/protection.sh
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
################################################
|
|
|
|
|
# Functions
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function sync_time() {
|
2019-12-31 14:28:42 -01:00
|
|
|
|
einfo "Syncing time"
|
2020-01-02 22:29:17 -01:00
|
|
|
|
ntpd -g -q \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|| die "Could not sync time with remote server"
|
|
|
|
|
|
|
|
|
|
einfo "Current date: $(LANG=C date)"
|
|
|
|
|
einfo "Writing time to hardware clock"
|
2020-01-02 22:29:17 -01:00
|
|
|
|
hwclock --systohc --utc \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|| die "Could not save time to hardware clock"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function check_config() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ $KEYMAP =~ ^[0-9A-Za-z-]*$ ]] \
|
2020-01-04 11:09:29 -01:00
|
|
|
|
|| die "KEYMAP contains invalid characters"
|
2020-01-06 14:13:42 -01:00
|
|
|
|
|
|
|
|
|
# Check hostname per RFC1123
|
2020-01-06 16:20:47 -01:00
|
|
|
|
local hostname_regex='^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ $HOSTNAME =~ $hostname_regex ]] \
|
2020-01-06 14:13:42 -01:00
|
|
|
|
|| die "'$HOSTNAME' is not a valid hostname"
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ -n $DISK_ID_ROOT ]] \
|
|
|
|
|
|| die "You must assign DISK_ID_ROOT"
|
2020-04-22 21:48:36 +00:00
|
|
|
|
[[ -n $DISK_ID_EFI ]] || [[ -n $DISK_ID_BIOS ]] \
|
|
|
|
|
|| die "You must assign DISK_ID_EFI or DISK_ID_BIOS"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
|
2020-04-22 21:48:36 +00:00
|
|
|
|
[[ -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?"
|
2020-04-21 18:48:23 +00:00
|
|
|
|
[[ -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 [[ -v "DISK_ID_EFI" ]]; then
|
|
|
|
|
IS_EFI=true
|
|
|
|
|
else
|
|
|
|
|
IS_EFI=false
|
|
|
|
|
fi
|
2020-01-04 11:09:29 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function preprocess_config() {
|
2021-05-31 15:11:39 +00:00
|
|
|
|
# Check encryption key if used
|
|
|
|
|
[[ $USED_ENCRYPTION == "true" ]] \
|
|
|
|
|
&& check_encryption_key
|
2021-05-29 20:18:39 +00:00
|
|
|
|
disk_configuration
|
2020-04-21 16:38:46 +00:00
|
|
|
|
check_config
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function prepare_installation_environment() {
|
2019-12-31 14:28:42 -01:00
|
|
|
|
einfo "Preparing installation environment"
|
|
|
|
|
|
2021-05-29 20:23:18 +00:00
|
|
|
|
local needed_programs=(
|
|
|
|
|
gpg
|
|
|
|
|
hwclock
|
|
|
|
|
lsblk
|
|
|
|
|
ntpd
|
|
|
|
|
partprobe
|
|
|
|
|
python3
|
|
|
|
|
rhash
|
|
|
|
|
sgdisk
|
|
|
|
|
uuidgen
|
|
|
|
|
wget
|
|
|
|
|
)
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2021-04-28 12:56:38 +00:00
|
|
|
|
[[ $USED_BTRFS == "true" ]] \
|
2021-05-29 20:14:46 +00:00
|
|
|
|
&& needed_programs+=(btrfs)
|
2021-04-28 12:56:38 +00:00
|
|
|
|
[[ $USED_ZFS == "true" ]] \
|
2021-05-29 20:14:46 +00:00
|
|
|
|
&& needed_programs+=(zfs)
|
2020-04-23 21:56:53 +00:00
|
|
|
|
[[ $USED_RAID == "true" ]] \
|
2021-05-29 20:14:46 +00:00
|
|
|
|
&& needed_programs+=(mdadm)
|
2020-04-23 21:56:53 +00:00
|
|
|
|
[[ $USED_LUKS == "true" ]] \
|
2021-05-29 20:14:46 +00:00
|
|
|
|
&& needed_programs+=(cryptsetup)
|
|
|
|
|
|
|
|
|
|
# Check for existence of required programs
|
|
|
|
|
check_has_programs "${needed_programs[@]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
|
2021-05-02 13:29:21 +00:00
|
|
|
|
# Sync time now to prevent issues later
|
2019-12-31 14:28:42 -01:00
|
|
|
|
sync_time
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 13:29:21 +00:00
|
|
|
|
function check_encryption_key() {
|
2021-05-31 15:11:39 +00:00
|
|
|
|
if [[ -n "${GENTOO_INSTALL_ENCRYPTION_KEY+set}" ]]; then
|
|
|
|
|
elog "You have enabled encryption, but haven't specified a key in the environment variable GENTOO_INSTALL_ENCRYPTION_KEY."
|
|
|
|
|
if ask "Do you want to enter an encryption key now?"; then
|
|
|
|
|
local encryption_key_1
|
|
|
|
|
local encryption_key_2
|
|
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
|
flush_stdin
|
|
|
|
|
read -s -r -p "Enter encryption key: " encryption_key_1 \
|
|
|
|
|
|| die "Error in read"
|
|
|
|
|
|
|
|
|
|
[[ ${#encryption_key_1} -ge 8 ]] \
|
|
|
|
|
|| { ewarn "Your encryption key must be at least 8 characters long."; continue; }
|
|
|
|
|
|
|
|
|
|
flush_stdin
|
|
|
|
|
read -s -r -p "Repeat encryption key: " encryption_key_2 \
|
|
|
|
|
|| die "Error in read"
|
|
|
|
|
|
|
|
|
|
[[ "$encryption_key_1" == "$encryption_key_2" ]] \
|
|
|
|
|
|| { ewarn "Encryption keys mismatch."; continue; }
|
|
|
|
|
done
|
|
|
|
|
export GENTOO_INSTALL_ENCRYPTION_KEY="$encryption_key_1"
|
|
|
|
|
else
|
|
|
|
|
die "Please export GENTOO_INSTALL_ENCRYPTION_KEY with the desired key."
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2021-05-02 13:29:21 +00:00
|
|
|
|
|
|
|
|
|
[[ ${#GENTOO_INSTALL_ENCRYPTION_KEY} -ge 8 ]] \
|
|
|
|
|
|| die "Your encryption key must be at least 8 characters long."
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function add_summary_entry() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
local parent="$1"
|
|
|
|
|
local id="$2"
|
|
|
|
|
local name="$3"
|
|
|
|
|
local hint="$4"
|
|
|
|
|
local desc="$5"
|
|
|
|
|
|
|
|
|
|
local ptr
|
|
|
|
|
case "$id" in
|
2021-05-30 00:33:45 +00:00
|
|
|
|
"${DISK_ID_BIOS-__unused__}") ptr="[1;32m← bios[m" ;;
|
|
|
|
|
"${DISK_ID_EFI-__unused__}") ptr="[1;32m← efi[m" ;;
|
|
|
|
|
"${DISK_ID_SWAP-__unused__}") ptr="[1;34m← swap[m" ;;
|
|
|
|
|
"${DISK_ID_ROOT-__unused__}") ptr="[1;33m← root[m" ;;
|
2020-04-20 22:40:54 +00:00
|
|
|
|
# \x1f characters compensate for printf byte count and unicode character count mismatch due to '←'
|
2021-05-30 00:33:45 +00:00
|
|
|
|
*) ptr="[1;32m[m$(echo -e "\x1f\x1f")" ;;
|
2020-04-20 22:40:54 +00:00
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
summary_tree[$parent]+=";$id"
|
|
|
|
|
summary_name[$id]="$name"
|
|
|
|
|
summary_hint[$id]="$hint"
|
|
|
|
|
summary_ptr[$id]="$ptr"
|
|
|
|
|
summary_desc[$id]="$desc"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function summary_color_args() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
for arg in "$@"; do
|
|
|
|
|
if [[ -v "arguments[$arg]" ]]; then
|
|
|
|
|
printf '%-28s ' "[1;34m$arg[2m=[m${arguments[$arg]}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_create_gpt() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ -v arguments[id] ]]; then
|
2020-04-21 12:33:42 +00:00
|
|
|
|
add_summary_entry "${arguments[id]}" "$new_id" "gpt" "" ""
|
2020-04-20 22:40:54 +00:00
|
|
|
|
else
|
2020-04-21 12:33:42 +00:00
|
|
|
|
add_summary_entry __root__ "$new_id" "${arguments[device]}" "(gpt)" ""
|
2020-04-20 22:40:54 +00:00
|
|
|
|
fi
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
|
|
|
|
local device
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local device_desc=""
|
2020-04-21 12:33:42 +00:00
|
|
|
|
if [[ -v arguments[id] ]]; then
|
2020-04-22 21:48:36 +00:00
|
|
|
|
device="$(resolve_device_by_id "${arguments[id]}")"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
device_desc="$device ($id)"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
else
|
|
|
|
|
device="${arguments[device]}"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
device_desc="$device"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-04-21 18:58:21 +00:00
|
|
|
|
local ptuuid="${DISK_ID_TO_UUID[$new_id]}"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
|
|
|
|
|
einfo "Creating new gpt partition table ($new_id) on $device_desc"
|
2020-04-21 18:58:21 +00:00
|
|
|
|
sgdisk -Z -U "$ptuuid" "$device" >/dev/null \
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|| die "Could not create new gpt partition table ($new_id) on '$device'"
|
|
|
|
|
partprobe "$device"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_create_partition() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
|
|
|
|
local id="${arguments[id]}"
|
|
|
|
|
local size="${arguments[size]}"
|
|
|
|
|
local type="${arguments[type]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-04-21 12:33:42 +00:00
|
|
|
|
add_summary_entry "$id" "$new_id" "part" "($type)" "$(summary_color_args size)"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
|
|
|
|
if [[ $size == "remaining" ]]; then
|
2020-04-21 16:38:46 +00:00
|
|
|
|
arg_size=0
|
2020-04-21 12:33:42 +00:00
|
|
|
|
else
|
2020-04-21 16:38:46 +00:00
|
|
|
|
arg_size="+$size"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local device
|
|
|
|
|
device="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local partuuid="${DISK_ID_TO_UUID[$new_id]}"
|
2020-04-21 18:48:23 +00:00
|
|
|
|
local extra_args=""
|
2020-04-21 12:33:42 +00:00
|
|
|
|
case "$type" in
|
2020-04-21 19:52:46 +00:00
|
|
|
|
'bios') type='ef02' extra_args='--attributes=0:set:2';;
|
2020-04-21 12:33:42 +00:00
|
|
|
|
'efi') type='ef00' ;;
|
|
|
|
|
'swap') type='8200' ;;
|
|
|
|
|
'raid') type='fd00' ;;
|
|
|
|
|
'luks') type='8309' ;;
|
|
|
|
|
'linux') type='8300' ;;
|
|
|
|
|
*) ;;
|
|
|
|
|
esac
|
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
einfo "Creating partition ($new_id) with type=$type, size=$size on $device"
|
2020-04-21 18:48:23 +00:00
|
|
|
|
# shellcheck disable=SC2086
|
2020-04-21 21:15:29 +00:00
|
|
|
|
sgdisk -n "0:0:$arg_size" -t "0:$type" -u "0:$partuuid" $extra_args "$device" >/dev/null \
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|| die "Could not create new gpt partition ($new_id) on '$device' ($id)"
|
|
|
|
|
partprobe "$device"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_create_raid() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
|
|
|
|
local level="${arguments[level]}"
|
2020-04-21 21:29:06 +00:00
|
|
|
|
local name="${arguments[name]}"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local ids="${arguments[ids]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-04-20 22:40:54 +00:00
|
|
|
|
local id
|
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
2020-04-21 12:33:42 +00:00
|
|
|
|
for id in ${ids//';'/ }; do
|
2020-04-21 21:29:06 +00:00
|
|
|
|
add_summary_entry "$id" "_$new_id" "raid$level" "" "$(summary_color_args name)"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
done
|
|
|
|
|
|
2020-04-21 21:29:06 +00:00
|
|
|
|
add_summary_entry __root__ "$new_id" "raid$level" "" "$(summary_color_args name)"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local devices_desc=""
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local devices=()
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local id
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local dev
|
2020-04-21 12:33:42 +00:00
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
2021-04-28 14:06:52 +00:00
|
|
|
|
dev="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
devices+=("$dev")
|
|
|
|
|
devices_desc+="$dev ($id), "
|
2020-04-21 12:33:42 +00:00
|
|
|
|
done
|
2020-04-21 16:38:46 +00:00
|
|
|
|
devices_desc="${devices_desc:0:-2}"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-04-21 21:41:27 +00:00
|
|
|
|
local mddevice="/dev/md/$name"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local uuid="${DISK_ID_TO_UUID[$new_id]}"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
einfo "Creating raid$level ($new_id) on $devices_desc"
|
2020-04-21 21:29:06 +00:00
|
|
|
|
mdadm \
|
2020-04-21 21:41:27 +00:00
|
|
|
|
--create "$mddevice" \
|
2020-04-21 21:29:06 +00:00
|
|
|
|
--verbose \
|
|
|
|
|
--homehost="$HOSTNAME" \
|
|
|
|
|
--metadata=1.2 \
|
|
|
|
|
--raid-devices="${#devices[@]}" \
|
2020-04-21 12:33:42 +00:00
|
|
|
|
--uuid="$uuid" \
|
|
|
|
|
--level="$level" \
|
|
|
|
|
"${devices[@]}" \
|
2020-04-21 21:45:55 +00:00
|
|
|
|
|| die "Could not create raid$level array '$mddevice' ($new_id) on $devices_desc"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_create_luks() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
2020-04-24 21:02:00 +00:00
|
|
|
|
local name="${arguments[name]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-10-03 14:24:48 +00:00
|
|
|
|
if [[ -v arguments[id] ]]; then
|
|
|
|
|
add_summary_entry "${arguments[id]}" "$new_id" "luks" "" ""
|
|
|
|
|
else
|
|
|
|
|
add_summary_entry __root__ "$new_id" "${arguments[device]}" "(luks)" ""
|
|
|
|
|
fi
|
2020-04-20 22:40:54 +00:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-10-03 14:24:48 +00:00
|
|
|
|
local device
|
|
|
|
|
local device_desc=""
|
|
|
|
|
if [[ -v arguments[id] ]]; then
|
|
|
|
|
device="$(resolve_device_by_id "${arguments[id]}")"
|
|
|
|
|
device_desc="$device ($id)"
|
|
|
|
|
else
|
|
|
|
|
device="${arguments[device]}"
|
|
|
|
|
device_desc="$device"
|
|
|
|
|
fi
|
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local uuid="${DISK_ID_TO_UUID[$new_id]}"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-10-03 17:10:54 +00:00
|
|
|
|
einfo "Creating luks ($new_id) on $device_desc"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
cryptsetup luksFormat \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
--type luks2 \
|
|
|
|
|
--uuid "$uuid" \
|
2021-05-02 13:29:21 +00:00
|
|
|
|
--key-file <(echo -n "$GENTOO_INSTALL_ENCRYPTION_KEY") \
|
2020-04-21 12:33:42 +00:00
|
|
|
|
--cipher aes-xts-plain64 \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
--hash sha512 \
|
2020-04-21 12:33:42 +00:00
|
|
|
|
--pbkdf argon2id \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
--iter-time 4000 \
|
|
|
|
|
--key-size 512 \
|
2020-04-23 14:25:34 +00:00
|
|
|
|
--batch-mode \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
"$device" \
|
2020-10-03 17:10:54 +00:00
|
|
|
|
|| die "Could not create luks on $device_desc"
|
2020-04-22 21:08:11 +00:00
|
|
|
|
mkdir -p "$LUKS_HEADER_BACKUP_DIR" \
|
|
|
|
|
|| die "Could not create luks header backup dir '$LUKS_HEADER_BACKUP_DIR'"
|
2020-04-23 14:25:34 +00:00
|
|
|
|
local header_file="$LUKS_HEADER_BACKUP_DIR/luks-header-$id-${uuid,,}.img"
|
|
|
|
|
[[ ! -e $header_file ]] \
|
|
|
|
|
|| rm "$header_file" \
|
|
|
|
|
|| die "Could not remove old luks header backup file '$header_file'"
|
2020-04-22 21:08:11 +00:00
|
|
|
|
cryptsetup luksHeaderBackup "$device" \
|
2020-04-23 14:25:34 +00:00
|
|
|
|
--header-backup-file "$header_file" \
|
2020-10-03 17:10:54 +00:00
|
|
|
|
|| die "Could not backup luks header on $device_desc"
|
2020-04-22 21:08:11 +00:00
|
|
|
|
cryptsetup open --type luks2 \
|
2021-05-02 13:35:20 +00:00
|
|
|
|
--key-file <(echo -n "$GENTOO_INSTALL_ENCRYPTION_KEY") \
|
2020-04-24 21:02:00 +00:00
|
|
|
|
"$device" "$name" \
|
2020-10-03 17:10:54 +00:00
|
|
|
|
|| die "Could not open luks encrypted device $device_desc"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_create_dummy() {
|
2020-10-03 15:59:45 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
|
|
|
|
local device="${arguments[device]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-10-03 15:59:45 +00:00
|
|
|
|
add_summary_entry __root__ "$new_id" "$device" "" ""
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function init_btrfs() {
|
2020-10-03 14:24:48 +00:00
|
|
|
|
local device="$1"
|
|
|
|
|
local desc="$2"
|
|
|
|
|
mkdir -p /btrfs \
|
|
|
|
|
|| die "Could not create /btrfs directory"
|
|
|
|
|
mount "$device" /btrfs \
|
|
|
|
|
|| die "Could not mount $desc to /btrfs"
|
|
|
|
|
btrfs subvolume create /btrfs/root \
|
|
|
|
|
|| die "Could not create btrfs subvolume /root on $desc"
|
|
|
|
|
btrfs subvolume set-default /btrfs/root \
|
|
|
|
|
|| die "Could not set default btrfs subvolume to /root on $desc"
|
|
|
|
|
umount /btrfs \
|
|
|
|
|
|| die "Could not unmount btrfs on $desc"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_format() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local id="${arguments[id]}"
|
|
|
|
|
local type="${arguments[type]}"
|
|
|
|
|
local label="${arguments[label]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-04-20 22:40:54 +00:00
|
|
|
|
add_summary_entry "${arguments[id]}" "__fs__${arguments[id]}" "${arguments[type]}" "(fs)" "$(summary_color_args label)"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local device
|
|
|
|
|
device="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
einfo "Formatting $device ($id) with $type"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
case "$type" in
|
2020-04-21 19:52:46 +00:00
|
|
|
|
'bios'|'efi')
|
2020-04-21 12:33:42 +00:00
|
|
|
|
if [[ -v "arguments[label]" ]]; then
|
|
|
|
|
mkfs.fat -F 32 -n "$label" "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
else
|
|
|
|
|
mkfs.fat -F 32 "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
'swap')
|
|
|
|
|
if [[ -v "arguments[label]" ]]; then
|
|
|
|
|
mkswap -L "$label" "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
else
|
|
|
|
|
mkswap "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
'ext4')
|
|
|
|
|
if [[ -v "arguments[label]" ]]; then
|
|
|
|
|
mkfs.ext4 -q -L "$label" "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
else
|
|
|
|
|
mkfs.ext4 -q "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
2020-10-01 13:46:51 +00:00
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
'btrfs')
|
|
|
|
|
if [[ -v "arguments[label]" ]]; then
|
|
|
|
|
mkfs.btrfs -q -L "$label" "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
|
|
|
|
else
|
|
|
|
|
mkfs.btrfs -q "$device" \
|
|
|
|
|
|| die "Could not format device '$device' ($id)"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
fi
|
2020-10-03 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
init_btrfs "$device" "'$device' ($id)"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
;;
|
|
|
|
|
*) die "Unknown filesystem type" ;;
|
|
|
|
|
esac
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 18:35:28 +00:00
|
|
|
|
# This function will be called when a custom zfs pool type has been chosen.
|
|
|
|
|
# $1: either 'true' or 'false' determining if the pool should be encrypted
|
|
|
|
|
# $2: a string describing all device paths (for error messages)
|
|
|
|
|
# $@: device paths
|
|
|
|
|
function format_zfs_standard() {
|
|
|
|
|
local encrypt="$1"
|
|
|
|
|
local device_desc="$2"
|
|
|
|
|
shift 2
|
|
|
|
|
local devices=("$@")
|
|
|
|
|
|
|
|
|
|
einfo "Creating zfs pool on $devices_desc"
|
|
|
|
|
local extra_args=()
|
|
|
|
|
if [[ "$encrypt" == true ]]; then
|
|
|
|
|
extra_args+=(
|
|
|
|
|
"-O" "encryption=aes-256-gcm"
|
|
|
|
|
"-O" "keyformat=passphrase"
|
|
|
|
|
"-O" "keylocation=prompt"
|
|
|
|
|
)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# dnodesize=legacy might be needed for GRUB2, but auto is preferred for xattr=sa.
|
|
|
|
|
zpool create \
|
2021-05-30 00:56:53 +00:00
|
|
|
|
-R "$ROOT_MOUNTPOINT" \
|
2021-05-27 18:35:28 +00:00
|
|
|
|
-o ashift=12 \
|
|
|
|
|
-O acltype=posix \
|
|
|
|
|
-O atime=off \
|
|
|
|
|
-O xattr=sa \
|
|
|
|
|
-O dnodesize=auto \
|
|
|
|
|
-O mountpoint=none \
|
|
|
|
|
-O canmount=noauto \
|
|
|
|
|
-O devices=off \
|
|
|
|
|
-O compression=zstd \
|
|
|
|
|
"${extra_args[@]}" \
|
|
|
|
|
rpool \
|
|
|
|
|
"${devices[@]}" \
|
2021-05-30 00:46:07 +00:00
|
|
|
|
<<< "$GENTOO_INSTALL_ENCRYPTION_KEY" \
|
2021-05-27 18:35:28 +00:00
|
|
|
|
|| die "Could not create zfs pool on $devices_desc"
|
|
|
|
|
|
2021-05-30 00:51:51 +00:00
|
|
|
|
zfs create rpool/ROOT \
|
2021-05-30 00:44:26 +00:00
|
|
|
|
|| die "Could not create zfs dataset 'rpool/ROOT'"
|
2021-05-27 18:35:28 +00:00
|
|
|
|
zfs create -o mountpoint=/ rpool/ROOT/default \
|
2021-05-30 00:44:26 +00:00
|
|
|
|
|| die "Could not create zfs dataset 'rpool/ROOT/default'"
|
2021-05-27 18:35:28 +00:00
|
|
|
|
zpool set bootfs=rpool/ROOT/default rpool \
|
|
|
|
|
|| die "Could not set zfs property bootfs on rpool"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_format_zfs() {
|
|
|
|
|
local ids="${arguments[ids]}"
|
|
|
|
|
local pool_type="${arguments[pool_type]}"
|
2021-05-27 18:35:28 +00:00
|
|
|
|
local encrypt="${arguments[encrypt]-false}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local id
|
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
|
|
|
|
add_summary_entry "$id" "__fs__$id" "zfs" "(fs)" "$(summary_color_args label)"
|
|
|
|
|
done
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
2021-05-27 18:35:28 +00:00
|
|
|
|
local devices_desc=""
|
|
|
|
|
local devices=()
|
|
|
|
|
local id
|
|
|
|
|
local dev
|
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
|
|
|
|
dev="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
|
|
|
|
devices+=("$dev")
|
|
|
|
|
devices_desc+="$dev ($id), "
|
|
|
|
|
done
|
|
|
|
|
devices_desc="${devices_desc:0:-2}"
|
|
|
|
|
|
|
|
|
|
if [[ "$pool_type" == "custom" ]]; then
|
|
|
|
|
format_zfs_custom "$encrypt" "$devices_desc" "${devices[@]}"
|
|
|
|
|
else
|
|
|
|
|
format_zfs_standard "$encrypt" "$devices_desc" "${devices[@]}"
|
|
|
|
|
fi
|
2021-04-28 12:56:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function disk_format_btrfs() {
|
2020-10-03 14:24:48 +00:00
|
|
|
|
local ids="${arguments[ids]}"
|
|
|
|
|
local label="${arguments[label]}"
|
2020-10-11 19:45:26 +00:00
|
|
|
|
local raid_type="${arguments[raid_type]}"
|
2021-05-30 00:37:18 +00:00
|
|
|
|
if [[ ${disk_action_summarize_only-false} == "true" ]]; then
|
2020-10-03 14:24:48 +00:00
|
|
|
|
local id
|
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
|
|
|
|
add_summary_entry "$id" "__fs__$id" "btrfs" "(fs)" "$(summary_color_args label)"
|
|
|
|
|
done
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local devices_desc=""
|
|
|
|
|
local devices=()
|
|
|
|
|
local id
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local dev
|
2020-10-03 14:24:48 +00:00
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
2021-04-28 14:06:52 +00:00
|
|
|
|
dev="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
2020-10-03 14:24:48 +00:00
|
|
|
|
devices+=("$dev")
|
|
|
|
|
devices_desc+="$dev ($id), "
|
|
|
|
|
done
|
|
|
|
|
devices_desc="${devices_desc:0:-2}"
|
|
|
|
|
|
2020-10-11 19:45:26 +00:00
|
|
|
|
# Collect extra arguments
|
|
|
|
|
extra_args=()
|
2020-10-28 23:19:45 -01:00
|
|
|
|
if [[ "${#devices}" -gt 1 ]] && [[ -v "arguments[raid_type]" ]]; then
|
2020-10-11 19:45:26 +00:00
|
|
|
|
extra_args+=("-d" "$raid_type")
|
|
|
|
|
fi
|
|
|
|
|
|
2020-10-03 14:24:48 +00:00
|
|
|
|
if [[ -v "arguments[label]" ]]; then
|
2020-10-11 19:45:26 +00:00
|
|
|
|
extra_args+=("-L" "$label")
|
2020-10-03 14:24:48 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-10-11 19:45:26 +00:00
|
|
|
|
einfo "Creating btrfs on $devices_desc"
|
|
|
|
|
mkfs.btrfs -q "${extra_args[@]}" "${devices[@]}" \
|
|
|
|
|
|| die "Could not create btrfs on $devices_desc"
|
|
|
|
|
|
2020-10-03 14:24:48 +00:00
|
|
|
|
init_btrfs "${devices[0]}" "btrfs array ($devices_desc)"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function apply_disk_action() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
unset known_arguments
|
|
|
|
|
unset arguments; declare -A arguments; parse_arguments "$@"
|
|
|
|
|
case "${arguments[action]}" in
|
2020-10-03 15:59:45 +00:00
|
|
|
|
'create_gpt') disk_create_gpt ;;
|
|
|
|
|
'create_partition') disk_create_partition ;;
|
|
|
|
|
'create_raid') disk_create_raid ;;
|
|
|
|
|
'create_luks') disk_create_luks ;;
|
|
|
|
|
'create_dummy') disk_create_dummy ;;
|
|
|
|
|
'format') disk_format ;;
|
2021-04-28 12:56:38 +00:00
|
|
|
|
'format_zfs') disk_format_zfs ;;
|
2020-10-03 15:59:45 +00:00
|
|
|
|
'format_btrfs') disk_format_btrfs ;;
|
2020-04-20 22:40:54 +00:00
|
|
|
|
*) echo "Ignoring invalid action: ${arguments[action]}" ;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function print_summary_tree_entry() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
local indent_chars=""
|
|
|
|
|
local indent="0"
|
|
|
|
|
local d="1"
|
|
|
|
|
local maxd="$((depth - 1))"
|
|
|
|
|
while [[ $d -lt $maxd ]]; do
|
2020-04-23 21:56:53 +00:00
|
|
|
|
if [[ ${summary_depth_continues[$d]} == "true" ]]; then
|
2020-04-20 22:40:54 +00:00
|
|
|
|
indent_chars+='│ '
|
|
|
|
|
else
|
|
|
|
|
indent_chars+=' '
|
|
|
|
|
fi
|
|
|
|
|
indent=$((indent + 2))
|
|
|
|
|
d="$((d + 1))"
|
|
|
|
|
done
|
|
|
|
|
if [[ $maxd -gt 0 ]]; then
|
2020-04-23 21:56:53 +00:00
|
|
|
|
if [[ ${summary_depth_continues[$maxd]} == "true" ]]; then
|
2020-04-20 22:40:54 +00:00
|
|
|
|
indent_chars+='├─'
|
|
|
|
|
else
|
|
|
|
|
indent_chars+='└─'
|
|
|
|
|
fi
|
|
|
|
|
indent=$((indent + 2))
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local name="${summary_name[$root]}"
|
|
|
|
|
local hint="${summary_hint[$root]}"
|
|
|
|
|
local desc="${summary_desc[$root]}"
|
|
|
|
|
local ptr="${summary_ptr[$root]}"
|
|
|
|
|
local id_name="[2m[m"
|
|
|
|
|
if [[ $root != __* ]]; then
|
|
|
|
|
if [[ $root == _* ]]; then
|
|
|
|
|
id_name="[2m${root:1}[m"
|
|
|
|
|
else
|
|
|
|
|
id_name="[2m${root}[m"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local align=0
|
|
|
|
|
if [[ $indent -lt 33 ]]; then
|
|
|
|
|
align="$((33 - indent))"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
elog "$indent_chars$(printf "%-${align}s %-47s %s" \
|
|
|
|
|
"$name [2m$hint[m" \
|
|
|
|
|
"$id_name $ptr" \
|
|
|
|
|
"$desc")"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function print_summary_tree() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
local root="$1"
|
|
|
|
|
local depth="$((depth + 1))"
|
|
|
|
|
local has_children=false
|
|
|
|
|
|
|
|
|
|
if [[ -v "summary_tree[$root]" ]]; then
|
|
|
|
|
local children="${summary_tree[$root]}"
|
|
|
|
|
has_children=true
|
|
|
|
|
summary_depth_continues[$depth]=true
|
|
|
|
|
else
|
|
|
|
|
summary_depth_continues[$depth]=false
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $root != __root__ ]]; then
|
|
|
|
|
print_summary_tree_entry "$root"
|
|
|
|
|
fi
|
|
|
|
|
|
2020-04-23 21:56:53 +00:00
|
|
|
|
if [[ $has_children == "true" ]]; then
|
2021-04-28 14:06:52 +00:00
|
|
|
|
local count
|
|
|
|
|
count="$(tr ';' '\n' <<< "$children" | grep -c '\S')" \
|
|
|
|
|
|| count=0
|
2020-04-20 22:40:54 +00:00
|
|
|
|
local idx=0
|
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${children//';'/ }; do
|
|
|
|
|
idx="$((idx + 1))"
|
|
|
|
|
[[ $idx == "$count" ]] \
|
|
|
|
|
&& summary_depth_continues[$depth]=false
|
|
|
|
|
print_summary_tree "$id"
|
|
|
|
|
# separate blocks by newline
|
2020-04-23 21:56:53 +00:00
|
|
|
|
[[ ${summary_depth_continues[0]} == "true" ]] && [[ $depth == 1 ]] && [[ $idx == "$count" ]] \
|
2020-04-20 22:40:54 +00:00
|
|
|
|
&& elog
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function apply_disk_actions() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local param
|
|
|
|
|
local current_params=()
|
|
|
|
|
for param in "${DISK_ACTIONS[@]}"; do
|
|
|
|
|
if [[ $param == ';' ]]; then
|
|
|
|
|
apply_disk_action "${current_params[@]}"
|
|
|
|
|
current_params=()
|
|
|
|
|
else
|
|
|
|
|
current_params+=("$param")
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function summarize_disk_actions() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
elog "[1mCurrent lsblk output:[m"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
for_line_in <(lsblk \
|
|
|
|
|
|| die "Error in lsblk") elog
|
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local disk_action_summarize_only=true
|
2020-04-20 22:40:54 +00:00
|
|
|
|
declare -A summary_tree
|
|
|
|
|
declare -A summary_name
|
|
|
|
|
declare -A summary_hint
|
|
|
|
|
declare -A summary_ptr
|
|
|
|
|
declare -A summary_desc
|
|
|
|
|
declare -A summary_depth_continues
|
|
|
|
|
apply_disk_actions
|
|
|
|
|
|
|
|
|
|
local depth=-1
|
|
|
|
|
elog
|
2020-04-21 12:33:42 +00:00
|
|
|
|
elog "[1mConfigured disk layout:[m"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
elog ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
elog "$(printf '%-26s %-28s %s' NODE ID OPTIONS)"
|
|
|
|
|
elog ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
print_summary_tree __root__
|
|
|
|
|
elog ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function apply_disk_configuration() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
summarize_disk_actions
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
ask "Do you really want to apply this disk configuration?" \
|
|
|
|
|
|| die "Aborted"
|
|
|
|
|
countdown "Applying in " 5
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
einfo "Applying disk configuration"
|
|
|
|
|
apply_disk_actions
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
einfo "Disk configuration was applied successfully"
|
|
|
|
|
elog "[1mNew lsblk output:[m"
|
|
|
|
|
for_line_in <(lsblk \
|
|
|
|
|
|| die "Error in lsblk") elog
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function mount_efivars() {
|
2020-01-05 23:09:09 -01:00
|
|
|
|
# Skip if already mounted
|
|
|
|
|
mountpoint -q -- "/sys/firmware/efi/efivars" \
|
|
|
|
|
&& return
|
|
|
|
|
|
|
|
|
|
# Mount efivars
|
|
|
|
|
einfo "Mounting efivars"
|
2020-01-06 14:13:42 -01:00
|
|
|
|
mount -t efivarfs efivarfs "/sys/firmware/efi/efivars" \
|
|
|
|
|
|| die "Could not mount efivarfs"
|
2020-01-05 23:09:09 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function mount_by_id() {
|
2020-01-02 21:28:16 -01:00
|
|
|
|
local dev
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local id="$1"
|
2020-01-02 21:28:16 -01:00
|
|
|
|
local mountpoint="$2"
|
|
|
|
|
|
|
|
|
|
# Skip if already mounted
|
|
|
|
|
mountpoint -q -- "$mountpoint" \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
&& return
|
|
|
|
|
|
2020-01-02 21:28:16 -01:00
|
|
|
|
# Mount device
|
2020-04-22 21:48:36 +00:00
|
|
|
|
einfo "Mounting device with id=$id to '$mountpoint'"
|
2020-01-02 21:28:16 -01:00
|
|
|
|
mkdir -p "$mountpoint" \
|
|
|
|
|
|| die "Could not create mountpoint directory '$mountpoint'"
|
2020-04-22 21:48:36 +00:00
|
|
|
|
dev="$(resolve_device_by_id "$id")" \
|
|
|
|
|
|| die "Could not resolve device with id=$id"
|
2020-01-02 21:28:16 -01:00
|
|
|
|
mount "$dev" "$mountpoint" \
|
|
|
|
|
|| die "Could not mount device '$dev'"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function mount_root() {
|
2021-05-27 18:35:28 +00:00
|
|
|
|
if [[ $USED_ZFS == "true" ]] && ! mountpoint -q -- "$ROOT_MOUNTPOINT"; then
|
|
|
|
|
die "Error: Expected zfs to be mounted under '$ROOT_MOUNTPOINT', but it isn't."
|
|
|
|
|
else
|
|
|
|
|
mount_by_id "$DISK_ID_ROOT" "$ROOT_MOUNTPOINT"
|
|
|
|
|
fi
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function bind_repo_dir() {
|
2020-01-02 21:28:16 -01:00
|
|
|
|
# Use new location by default
|
2020-01-08 16:21:01 -01:00
|
|
|
|
export GENTOO_INSTALL_REPO_DIR="$GENTOO_INSTALL_REPO_BIND"
|
2020-01-02 21:28:16 -01:00
|
|
|
|
|
2020-01-08 16:21:01 -01:00
|
|
|
|
# Bind the repo dir to a location in /tmp,
|
2019-12-31 14:28:42 -01:00
|
|
|
|
# so it can be accessed from within the chroot
|
2020-01-08 16:21:01 -01:00
|
|
|
|
mountpoint -q -- "$GENTOO_INSTALL_REPO_BIND" \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
&& return
|
|
|
|
|
|
|
|
|
|
# Mount root device
|
2020-01-08 16:21:01 -01:00
|
|
|
|
einfo "Bind mounting repo directory"
|
|
|
|
|
mkdir -p "$GENTOO_INSTALL_REPO_BIND" \
|
|
|
|
|
|| die "Could not create mountpoint directory '$GENTOO_INSTALL_REPO_BIND'"
|
|
|
|
|
mount --bind "$GENTOO_INSTALL_REPO_DIR_ORIGINAL" "$GENTOO_INSTALL_REPO_BIND" \
|
|
|
|
|
|| die "Could not bind mount '$GENTOO_INSTALL_REPO_DIR_ORIGINAL' to '$GENTOO_INSTALL_REPO_BIND'"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function download_stage3() {
|
2019-12-31 14:28:42 -01:00
|
|
|
|
cd "$TMP_DIR" \
|
|
|
|
|
|| die "Could not cd into '$TMP_DIR'"
|
|
|
|
|
|
|
|
|
|
local STAGE3_RELEASES="$GENTOO_MIRROR/releases/amd64/autobuilds/current-$STAGE3_BASENAME/"
|
|
|
|
|
|
|
|
|
|
# Download upstream list of files
|
|
|
|
|
CURRENT_STAGE3="$(download_stdout "$STAGE3_RELEASES")" \
|
|
|
|
|
|| die "Could not retrieve list of tarballs"
|
|
|
|
|
# Decode urlencoded strings
|
|
|
|
|
CURRENT_STAGE3=$(python3 -c 'import sys, urllib.parse; print(urllib.parse.unquote(sys.stdin.read()))' <<< "$CURRENT_STAGE3")
|
|
|
|
|
# Parse output for correct filename
|
|
|
|
|
CURRENT_STAGE3="$(grep -o "\"${STAGE3_BASENAME}-[0-9A-Z]*.tar.xz\"" <<< "$CURRENT_STAGE3" \
|
|
|
|
|
| sort -u | head -1)" \
|
|
|
|
|
|| die "Could not parse list of tarballs"
|
|
|
|
|
# Strip quotes
|
|
|
|
|
CURRENT_STAGE3="${CURRENT_STAGE3:1:-1}"
|
2020-01-02 22:29:17 -01:00
|
|
|
|
# File to indiciate successful verification
|
|
|
|
|
CURRENT_STAGE3_VERIFIED="${CURRENT_STAGE3}.verified"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
|
|
# Download file if not already downloaded
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ -e $CURRENT_STAGE3_VERIFIED ]]; then
|
2020-01-02 22:29:17 -01:00
|
|
|
|
einfo "$STAGE3_BASENAME tarball already downloaded and verified"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
else
|
|
|
|
|
einfo "Downloading $STAGE3_BASENAME tarball"
|
|
|
|
|
download "$STAGE3_RELEASES/${CURRENT_STAGE3}" "${CURRENT_STAGE3}"
|
|
|
|
|
download "$STAGE3_RELEASES/${CURRENT_STAGE3}.DIGESTS.asc" "${CURRENT_STAGE3}.DIGESTS.asc"
|
|
|
|
|
|
2020-01-02 22:29:17 -01:00
|
|
|
|
# Import gentoo keys
|
|
|
|
|
einfo "Importing gentoo gpg key"
|
|
|
|
|
local GENTOO_GPG_KEY="$TMP_DIR/gentoo-keys.gpg"
|
|
|
|
|
download "https://gentoo.org/.well-known/openpgpkey/hu/wtktzo4gyuhzu8a4z5fdj3fgmr1u6tob?l=releng" "$GENTOO_GPG_KEY" \
|
|
|
|
|
|| die "Could not retrieve gentoo gpg key"
|
|
|
|
|
gpg --quiet --import < "$GENTOO_GPG_KEY" \
|
|
|
|
|
|| die "Could not import gentoo gpg key"
|
|
|
|
|
|
|
|
|
|
# Verify DIGESTS signature
|
|
|
|
|
einfo "Verifying DIGEST.asc signature"
|
|
|
|
|
gpg --quiet --verify "${CURRENT_STAGE3}.DIGESTS.asc" \
|
|
|
|
|
|| die "Signature of '${CURRENT_STAGE3}.DIGESTS.asc' invalid!"
|
|
|
|
|
|
|
|
|
|
# Check hashes
|
|
|
|
|
einfo "Verifying tarball integrity"
|
|
|
|
|
rhash -P --check <(grep -B 1 'tar.xz$' "${CURRENT_STAGE3}.DIGESTS.asc") \
|
|
|
|
|
|| die "Checksum mismatch!"
|
|
|
|
|
|
|
|
|
|
# Create verification file in case the script is restarted
|
2020-01-04 16:33:41 -01:00
|
|
|
|
touch_or_die 0644 "$CURRENT_STAGE3_VERIFIED"
|
2020-01-02 22:29:17 -01:00
|
|
|
|
fi
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function extract_stage3() {
|
2019-12-31 14:28:42 -01:00
|
|
|
|
mount_root
|
|
|
|
|
|
|
|
|
|
[[ -n $CURRENT_STAGE3 ]] \
|
|
|
|
|
|| die "CURRENT_STAGE3 is not set"
|
|
|
|
|
[[ -e "$TMP_DIR/$CURRENT_STAGE3" ]] \
|
|
|
|
|
|| die "stage3 file does not exist"
|
|
|
|
|
|
|
|
|
|
# Go to root directory
|
|
|
|
|
cd "$ROOT_MOUNTPOINT" \
|
|
|
|
|
|| die "Could not move to '$ROOT_MOUNTPOINT'"
|
|
|
|
|
# Ensure the directory is empty
|
|
|
|
|
find . -mindepth 1 -maxdepth 1 -not -name 'lost+found' \
|
|
|
|
|
| grep -q . \
|
|
|
|
|
&& die "root directory '$ROOT_MOUNTPOINT' is not empty"
|
|
|
|
|
|
|
|
|
|
# Extract tarball
|
|
|
|
|
einfo "Extracting stage3 tarball"
|
|
|
|
|
tar xpf "$TMP_DIR/$CURRENT_STAGE3" --xattrs --numeric-owner \
|
|
|
|
|
|| die "Error while extracting tarball"
|
|
|
|
|
cd "$TMP_DIR" \
|
|
|
|
|
|| die "Could not cd into '$TMP_DIR'"
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function gentoo_umount() {
|
2020-01-02 21:28:16 -01:00
|
|
|
|
if mountpoint -q -- "$ROOT_MOUNTPOINT"; then
|
2020-01-02 22:29:17 -01:00
|
|
|
|
einfo "Unmounting root filesystem"
|
|
|
|
|
umount -R -l "$ROOT_MOUNTPOINT" \
|
2020-01-02 21:28:16 -01:00
|
|
|
|
|| die "Could not unmount filesystems"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function init_bash() {
|
2020-01-04 12:10:40 -01:00
|
|
|
|
source /etc/profile
|
|
|
|
|
umask 0077
|
2020-01-05 22:38:39 -01:00
|
|
|
|
export PS1='(chroot) \[[0;31m\]\u\[[1;31m\]@\h \[[1;34m\]\w \[[m\]\$ \[[m\]'
|
2020-01-04 12:10:40 -01:00
|
|
|
|
}; export -f init_bash
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function env_update() {
|
2020-01-03 21:48:49 -01:00
|
|
|
|
env-update \
|
|
|
|
|
|| die "Error in env-update"
|
|
|
|
|
source /etc/profile \
|
|
|
|
|
|| die "Could not source /etc/profile"
|
2020-01-04 16:33:41 -01:00
|
|
|
|
umask 0077
|
2020-01-02 22:42:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function mkdir_or_die() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
# shellcheck disable=SC2174
|
2020-01-04 16:33:41 -01:00
|
|
|
|
mkdir -m "$1" -p "$2" \
|
|
|
|
|
|| die "Could not create directory '$2'"
|
2020-01-04 10:55:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function touch_or_die() {
|
2020-01-04 16:33:41 -01:00
|
|
|
|
touch "$2" \
|
|
|
|
|
|| die "Could not touch '$2'"
|
|
|
|
|
chmod "$1" "$2"
|
2020-01-04 10:55:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 18:35:28 +00:00
|
|
|
|
# $1: root directory
|
|
|
|
|
# $@: command...
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function gentoo_chroot() {
|
2021-05-27 18:35:28 +00:00
|
|
|
|
if [[ $# -eq 1 ]]; then
|
2021-05-30 20:52:28 +00:00
|
|
|
|
einfo "To later unmount all virtual filesystems, simply use umount -l ${1@Q}"
|
2021-05-27 18:35:28 +00:00
|
|
|
|
gentoo_chroot "$1" /bin/bash --init-file <(echo 'init_bash')
|
2020-01-02 21:56:36 -01:00
|
|
|
|
fi
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2021-05-30 01:02:17 +00:00
|
|
|
|
[[ ${EXECUTED_IN_CHROOT-false} == "false" ]] \
|
2020-01-02 21:28:16 -01:00
|
|
|
|
|| die "Already in chroot"
|
|
|
|
|
|
2021-05-27 18:35:28 +00:00
|
|
|
|
local chroot_dir="$1"
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
|
|
# Bind repo directory to tmp
|
2020-01-08 16:21:01 -01:00
|
|
|
|
bind_repo_dir
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
|
|
# Copy resolv.conf
|
|
|
|
|
einfo "Preparing chroot environment"
|
2021-05-27 18:35:28 +00:00
|
|
|
|
install --mode=0644 /etc/resolv.conf "$chroot_dir/etc/resolv.conf" \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|| die "Could not copy resolv.conf"
|
|
|
|
|
|
|
|
|
|
# Mount virtual filesystems
|
|
|
|
|
einfo "Mounting virtual filesystems"
|
|
|
|
|
(
|
2021-05-27 18:35:28 +00:00
|
|
|
|
mountpoint -q -- "$chroot_dir/proc" || mount -t proc /proc "$chroot_dir/proc" || exit 1
|
|
|
|
|
mountpoint -q -- "$chroot_dir/tmp" || mount --rbind /tmp "$chroot_dir/tmp" || exit 1
|
|
|
|
|
mountpoint -q -- "$chroot_dir/sys" || {
|
|
|
|
|
mount --rbind /sys "$chroot_dir/sys" &&
|
|
|
|
|
mount --make-rslave "$chroot_dir/sys"; } || exit 1
|
|
|
|
|
mountpoint -q -- "$chroot_dir/dev" || {
|
|
|
|
|
mount --rbind /dev "$chroot_dir/dev" &&
|
|
|
|
|
mount --make-rslave "$chroot_dir/dev"; } || exit 1
|
2019-12-31 14:28:42 -01:00
|
|
|
|
) || die "Could not mount virtual filesystems"
|
|
|
|
|
|
2021-05-27 18:35:28 +00:00
|
|
|
|
# Cache lsblk output, because it doesn't work correctly in chroot (returns almost no info for devices, e.g. empty uuids)
|
2020-10-08 20:16:26 +00:00
|
|
|
|
cache_lsblk_output
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
# Execute command
|
|
|
|
|
einfo "Chrooting..."
|
|
|
|
|
EXECUTED_IN_CHROOT=true \
|
2020-10-08 20:16:26 +00:00
|
|
|
|
TMP_DIR="$TMP_DIR" \
|
|
|
|
|
CACHED_LSBLK_OUTPUT="$CACHED_LSBLK_OUTPUT" \
|
2021-05-30 20:52:28 +00:00
|
|
|
|
exec chroot -- "$chroot_dir" "$GENTOO_INSTALL_REPO_DIR/scripts/dispatch_chroot.sh" "$@" \
|
|
|
|
|
|| die "Failed to chroot into '$chroot_dir'."
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
2020-10-10 15:08:54 +00:00
|
|
|
|
|
2021-04-28 14:06:52 +00:00
|
|
|
|
function enable_service() {
|
2020-10-10 15:08:54 +00:00
|
|
|
|
if [[ $SYSTEMD == "true" ]]; then
|
|
|
|
|
systemctl enable "$1" \
|
|
|
|
|
|| die "Could not enable $1 service"
|
|
|
|
|
else
|
|
|
|
|
rc-update add "$1" default \
|
|
|
|
|
|| die "Could not add $1 to default services"
|
|
|
|
|
fi
|
|
|
|
|
}
|