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
|
|
|
|
|
|
|
|
|
|
check_has_program() {
|
|
|
|
|
type "$1" &>/dev/null \
|
|
|
|
|
|| die "Missing program: '$1'"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync_time() {
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 11:09:29 -01:00
|
|
|
|
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-04-21 16:38:46 +00:00
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $INSTALL_ANSIBLE == true ]]; then
|
|
|
|
|
[[ $INSTALL_SSHD == true ]] \
|
2020-01-06 19:34:44 -01:00
|
|
|
|
|| die "You must enable INSTALL_SSHD for ansible"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ -n $ANSIBLE_SSH_AUTHORIZED_KEYS ]] \
|
2020-01-06 14:13:42 -01:00
|
|
|
|
|| die "Missing pubkey for ansible user"
|
|
|
|
|
fi
|
2020-01-04 11:09:29 -01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
preprocess_config() {
|
|
|
|
|
check_config
|
2020-04-23 15:32:36 +00:00
|
|
|
|
load_resolvable_entries
|
2020-04-21 16:38:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
prepare_installation_environment() {
|
|
|
|
|
einfo "Preparing installation environment"
|
|
|
|
|
|
|
|
|
|
check_has_program gpg
|
|
|
|
|
check_has_program hwclock
|
|
|
|
|
check_has_program lsblk
|
|
|
|
|
check_has_program ntpd
|
|
|
|
|
check_has_program partprobe
|
|
|
|
|
check_has_program python3
|
|
|
|
|
check_has_program rhash
|
|
|
|
|
check_has_program sgdisk
|
|
|
|
|
check_has_program uuidgen
|
|
|
|
|
check_has_program wget
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ $USED_RAID == true ]] \
|
|
|
|
|
&& check_has_program mdadm
|
|
|
|
|
[[ $USED_LUKS == true ]] \
|
|
|
|
|
&& check_has_program cryptsetup
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
sync_time
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
add_summary_entry() {
|
|
|
|
|
local parent="$1"
|
|
|
|
|
local id="$2"
|
|
|
|
|
local name="$3"
|
|
|
|
|
local hint="$4"
|
|
|
|
|
local desc="$5"
|
|
|
|
|
|
|
|
|
|
local ptr
|
|
|
|
|
case "$id" in
|
2020-04-22 21:48:36 +00:00
|
|
|
|
"$DISK_ID_BIOS") ptr="[1;32m← bios[m" ;;
|
2020-04-20 22:40:54 +00:00
|
|
|
|
"$DISK_ID_EFI") ptr="[1;32m← efi[m" ;;
|
|
|
|
|
"$DISK_ID_SWAP") ptr="[1;34m← swap[m" ;;
|
|
|
|
|
"$DISK_ID_ROOT") ptr="[1;33m← root[m" ;;
|
|
|
|
|
# \x1f characters compensate for printf byte count and unicode character count mismatch due to '←'
|
|
|
|
|
*) ptr="[1;32m[m$(echo -e "\x1f\x1f")" ;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
summary_tree[$parent]+=";$id"
|
|
|
|
|
summary_name[$id]="$name"
|
|
|
|
|
summary_hint[$id]="$hint"
|
|
|
|
|
summary_ptr[$id]="$ptr"
|
|
|
|
|
summary_desc[$id]="$desc"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
summary_color_args() {
|
|
|
|
|
for arg in "$@"; do
|
|
|
|
|
if [[ -v "arguments[$arg]" ]]; then
|
|
|
|
|
printf '%-28s ' "[1;34m$arg[2m=[m${arguments[$arg]}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disk_create_gpt() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $disk_action_summarize_only == true ]]; then
|
|
|
|
|
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-23 15:32:36 +00:00
|
|
|
|
create_resolve_entry "$new_id" ptuuid "$ptuuid" "$device"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $disk_action_summarize_only == 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
|
|
|
|
|
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local device="$(resolve_device_by_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-23 15:32:36 +00:00
|
|
|
|
create_resolve_entry "$new_id" partuuid "$partuuid"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $disk_action_summarize_only == true ]]; then
|
|
|
|
|
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
|
2020-04-21 12:33:42 +00:00
|
|
|
|
# Splitting is intentional here
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for id in ${ids//';'/ }; do
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local dev="$(resolve_device_by_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-23 15:32:36 +00:00
|
|
|
|
create_resolve_entry "$new_id" mdadm "$uuid" "$mddevice"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disk_create_luks() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local new_id="${arguments[new_id]}"
|
|
|
|
|
local id="${arguments[id]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $disk_action_summarize_only == true ]]; then
|
2020-04-21 12:33:42 +00:00
|
|
|
|
add_summary_entry "$id" "$new_id" "luks" "" ""
|
2020-04-20 22:40:54 +00:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local device="$(resolve_device_by_id "$id")"
|
2020-04-21 16:38:46 +00:00
|
|
|
|
local uuid="${DISK_ID_TO_UUID[$new_id]}"
|
2020-04-23 15:32:36 +00:00
|
|
|
|
create_resolve_entry "$new_id" luks "$uuid"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
|
2020-04-21 16:38:46 +00:00
|
|
|
|
einfo "Creating luks ($new_id) on $device ($id)"
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local keyfile
|
|
|
|
|
keyfile="$(luks_getkeyfile "$new_id")" \
|
|
|
|
|
|| die "Error in luks_getkeyfile for id=$id"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
cryptsetup luksFormat \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
--type luks2 \
|
|
|
|
|
--uuid "$uuid" \
|
2020-04-22 21:48:36 +00:00
|
|
|
|
--key-file "$keyfile" \
|
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-04-21 21:45:55 +00:00
|
|
|
|
|| die "Could not create luks on '$device' ($id)"
|
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-04-22 21:08:11 +00:00
|
|
|
|
|| die "Could not backup luks header on '$device' ($id)"
|
|
|
|
|
cryptsetup open --type luks2 \
|
2020-04-22 21:48:36 +00:00
|
|
|
|
--key-file "$keyfile" \
|
2020-04-22 21:08:11 +00:00
|
|
|
|
"$device" "${uuid,,}" \
|
|
|
|
|
|| die "Could not open luks header on '$device' ($id)"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disk_format() {
|
2020-04-21 12:33:42 +00:00
|
|
|
|
local id="${arguments[id]}"
|
|
|
|
|
local type="${arguments[type]}"
|
|
|
|
|
local label="${arguments[label]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $disk_action_summarize_only == true ]]; then
|
|
|
|
|
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
|
|
|
|
|
2020-04-22 21:48:36 +00:00
|
|
|
|
local device="$(resolve_device_by_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)"
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
*) die "Unknown filesystem type" ;;
|
|
|
|
|
esac
|
2020-04-20 22:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apply_disk_action() {
|
|
|
|
|
unset known_arguments
|
|
|
|
|
unset arguments; declare -A arguments; parse_arguments "$@"
|
|
|
|
|
case "${arguments[action]}" in
|
|
|
|
|
'create_gpt') disk_create_gpt ;;
|
|
|
|
|
'create_partition') disk_create_partition ;;
|
|
|
|
|
'create_raid') disk_create_raid ;;
|
|
|
|
|
'create_luks') disk_create_luks ;;
|
|
|
|
|
'format') disk_format ;;
|
|
|
|
|
*) echo "Ignoring invalid action: ${arguments[action]}" ;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_summary_tree_entry() {
|
|
|
|
|
local indent_chars=""
|
|
|
|
|
local indent="0"
|
|
|
|
|
local d="1"
|
|
|
|
|
local maxd="$((depth - 1))"
|
|
|
|
|
while [[ $d -lt $maxd ]]; do
|
|
|
|
|
if [[ ${summary_depth_continues[$d]} == true ]]; then
|
|
|
|
|
indent_chars+='│ '
|
|
|
|
|
else
|
|
|
|
|
indent_chars+=' '
|
|
|
|
|
fi
|
|
|
|
|
indent=$((indent + 2))
|
|
|
|
|
d="$((d + 1))"
|
|
|
|
|
done
|
|
|
|
|
if [[ $maxd -gt 0 ]]; then
|
|
|
|
|
if [[ ${summary_depth_continues[$maxd]} == true ]]; then
|
|
|
|
|
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")"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_summary_tree() {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
if [[ $has_children == true ]]; then
|
|
|
|
|
local count="$(tr ';' '\n' <<< "$children" | grep -c '\S')"
|
|
|
|
|
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
|
|
|
|
|
[[ ${summary_depth_continues[0]} == true ]] && [[ $depth == 1 ]] && [[ $idx == "$count" ]] \
|
|
|
|
|
&& elog
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
apply_disk_actions() {
|
2020-04-23 15:42:23 +00:00
|
|
|
|
# Clean old resolved ids
|
|
|
|
|
rm -rf "$RESOLVABLE_MAP_DIR" &>/dev/null
|
|
|
|
|
unset DISK_ID_TO_RESOLVABLE; declare -A -g DISK_ID_TO_RESOLVABLE
|
|
|
|
|
unset DISK_UUID_TO_DEVICE; declare -A -g DISK_UUID_TO_DEVICE
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
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 ────────────────────────────────────────────────────────────────────────────────
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:33:42 +00:00
|
|
|
|
apply_disk_configuration() {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 23:09:09 -01:00
|
|
|
|
mount_efivars() {
|
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 21:48:36 +00:00
|
|
|
|
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'"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mount_root() {
|
2020-04-22 21:48:36 +00:00
|
|
|
|
mount_by_id "$DISK_ID_ROOT" "$ROOT_MOUNTPOINT"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 16:21:01 -01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
download_stage3() {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extract_stage3() {
|
|
|
|
|
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'"
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 21:28:16 -01:00
|
|
|
|
gentoo_umount() {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 12:10:40 -01:00
|
|
|
|
init_bash() {
|
|
|
|
|
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
|
|
|
|
|
|
2020-01-03 21:48:49 -01:00
|
|
|
|
env_update() {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 10:55:31 -01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
gentoo_chroot() {
|
2020-01-02 21:56:36 -01:00
|
|
|
|
if [[ $# -eq 0 ]]; then
|
2020-01-04 12:10:40 -01:00
|
|
|
|
gentoo_chroot /bin/bash --init-file <(echo 'init_bash')
|
2020-01-02 21:56:36 -01:00
|
|
|
|
fi
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
2020-01-02 21:28:16 -01:00
|
|
|
|
[[ $EXECUTED_IN_CHROOT != true ]] \
|
|
|
|
|
|| die "Already in chroot"
|
|
|
|
|
|
|
|
|
|
gentoo_umount
|
2019-12-31 14:28:42 -01:00
|
|
|
|
mount_root
|
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"
|
2020-01-04 19:55:42 -01:00
|
|
|
|
install --mode=0644 /etc/resolv.conf "$ROOT_MOUNTPOINT/etc/resolv.conf" \
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|| die "Could not copy resolv.conf"
|
|
|
|
|
|
|
|
|
|
# Mount virtual filesystems
|
|
|
|
|
einfo "Mounting virtual filesystems"
|
|
|
|
|
(
|
|
|
|
|
mountpoint -q -- "$ROOT_MOUNTPOINT/proc" || mount -t proc /proc "$ROOT_MOUNTPOINT/proc" || exit 1
|
|
|
|
|
mountpoint -q -- "$ROOT_MOUNTPOINT/tmp" || mount --rbind /tmp "$ROOT_MOUNTPOINT/tmp" || exit 1
|
2020-01-04 16:33:41 -01:00
|
|
|
|
mountpoint -q -- "$ROOT_MOUNTPOINT/sys" || {
|
|
|
|
|
mount --rbind /sys "$ROOT_MOUNTPOINT/sys" &&
|
|
|
|
|
mount --make-rslave "$ROOT_MOUNTPOINT/sys"; } || exit 1
|
|
|
|
|
mountpoint -q -- "$ROOT_MOUNTPOINT/dev" || {
|
|
|
|
|
mount --rbind /dev "$ROOT_MOUNTPOINT/dev" &&
|
|
|
|
|
mount --make-rslave "$ROOT_MOUNTPOINT/dev"; } || exit 1
|
2019-12-31 14:28:42 -01:00
|
|
|
|
) || die "Could not mount virtual filesystems"
|
|
|
|
|
|
|
|
|
|
# Execute command
|
|
|
|
|
einfo "Chrooting..."
|
|
|
|
|
EXECUTED_IN_CHROOT=true \
|
|
|
|
|
TMP_DIR=$TMP_DIR \
|
2020-01-08 16:21:01 -01:00
|
|
|
|
exec chroot -- "$ROOT_MOUNTPOINT" "$GENTOO_INSTALL_REPO_DIR/scripts/main_chroot.sh" "$@" \
|
2020-04-16 21:19:33 +00:00
|
|
|
|
|| die "Failed to chroot into '$ROOT_MOUNTPOINT'"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|