sh/shell/rescue/hetzner.sh
2024-11-16 13:33:53 +01:00

129 lines
2.7 KiB
Bash

[ "$(cat /proc/$$/comm)" = "bash" ] || return
rescue_wipe_0_init_hetzner_8_8() {
local device
set \
"/dev/sda" \
"/dev/sdb"
local members
local number
local passphrase
# read passphrase
passphrase="$(read_passphrase)"
# warn
warn_wipe "${@}"
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}"
#
parted --script "${device}" \
mktable gpt \
unit "mib" \
mkpart "crypt-${number}" 33282 7630885 \
mkpart "boot-${number}" 514 33282 \
mkpart "esp-${number}" 2 514 \
set 3 esp on \
mkpart "bios-${number}" 1 2 \
set 4 bios_grub on
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}4"
# wipe bios
fs_wipe "${device}4"
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}3"
# format esp
fs_wipe "${device}3" "1M"
fs_make_fat "${device}3" "esp-${number}" "0000000${number}"
# mount esp
mkdir --parents "/media/esp/${number}"
mount "${device}3" "/media/esp/${number}"
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}2"
# wipe boot
fs_wipe "${device}2" "1G" 1
done
#
members=""
for device in "${@}"; do
members="${members} ${device}2"
done
# shellcheck disable=SC2086
fs_raid_create \
"boot" "00000000:00000000:00000000:00000002" ${members}
#
fs_make_btrfs "/dev/md/boot" "boot" \
"00000000-0000-0000-0000-00000000000b"
# mount boot
mkdir --parents "/media/boot"
mount \
--options "autodefrag,compress-force=zstd" \
"/dev/md/boot" "/media/boot"
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}1"
# wipe crypt head
fs_wipe "${device}1" "1G" 1
done
#
members=""
for device in "${@}"; do
members="${members} ${device}1"
done
# shellcheck disable=SC2086
fs_raid_create \
"crypt" "00000000:00000000:00000000:00000001" ${members}
# encrypt
luks_format "${passphrase}" "/dev/md/crypt"
# open
echo "${passphrase}" |
cryptsetup luksOpen "/dev/md/crypt" "crypt"
# passphrase
unset passphrase
}
rescue_wipe_2_make_hetzner_8_8() {
local passphrase
# close
cryptsetup luksClose "crypt"
# read passphrase
passphrase="$(read_passphrase)"
# encrypt
luks_format "${passphrase}" "/dev/md/crypt"
# open
echo "${passphrase}" |
cryptsetup luksOpen "/dev/md/crypt" "crypt"
# passphrase
unset passphrase
# format crypt
fs_make_btrfs "/dev/mapper/crypt" "crypt" \
"00000000-0000-0000-0000-00000000000c"
# mount crypt
mkdir --parents "/media/crypt"
mount \
--options "autodefrag,compress-force=zstd" \
"/dev/mapper/crypt" "/media/crypt"
# make swap file
fs_make_btrfs_swap "/media/crypt/swap" "64g" \
"00000000-0000-0000-0000-000000000005"
}