sh/bash/rescue-hetzner.sh
2024-11-12 22:59:49 +01:00

164 lines
3.3 KiB
Bash

rescue_wipe_0_init_hetzner_8_8() {
local device
local devices=(
"/dev/sda"
"/dev/sdb"
)
local members
local number
local passphrase
# read passphrase
passphrase="$(read_passphrase)"
#
lsblk
echo -n "WIPE" "${devices[@]}" "/?\\ OR CANCEL /!\\"
read -r
#
number=0
for device in "${devices[@]}"; do
((number++))
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 "${devices[@]}"; do
((number++))
echo
echo "#${number}: ${device}4"
# wipe bios
fs_wipe "${device}4"
done
#
number=0
for device in "${devices[@]}"; do
((number++))
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 "${devices[@]}"; do
((number++))
echo
echo "#${number}: ${device}2"
# wipe boot
fs_wipe "${device}2" "1G" 1
done
#
members=()
for device in "${devices[@]}"; do
members+=("${device}2")
done
mdadm \
--create "/dev/md/boot" \
--level 0 \
--metadata 1 \
--name "md:boot" \
--raid-devices ${#devices[@]} \
--uuid "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 "${devices[@]}"; do
((number++))
echo
echo "#${number}: ${device}1"
# wipe crypt head
fs_wipe "${device}1" "1G" 1
done
#
members=()
for device in "${devices[@]}"; do
members+=("${device}1")
done
mdadm \
--create "/dev/md/crypt" \
--level 0 \
--metadata 1 \
--name "md:crypt" \
--raid-devices ${#devices[@]} \
--uuid "00000000:00000000:00000000:00000001" \
"${members[@]}"
# encrypt
echo "${passphrase}" |
cryptsetup \
--verbose \
--batch-mode \
--type "luks2" \
--pbkdf "argon2id" \
--cipher "aes-xts-plain64" \
--iter-time 8192 \
--key-size 512 \
--hash "sha512" \
--use-random \
luksFormat \
"/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
echo "${passphrase}" |
cryptsetup \
--verbose \
--batch-mode \
--type "luks2" \
--pbkdf "argon2id" \
--cipher "aes-xts-plain64" \
--iter-time 8192 \
--key-size 512 \
--hash "sha512" \
--use-random \
luksFormat \
"/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"
}