sh/bash/rescue-hetzner.sh

195 lines
3.8 KiB
Bash
Raw Normal View History

2024-11-11 17:06:27 +00:00
rescue_wipe_0_init_hetzner_8_8() {
2024-11-11 16:01:19 +00:00
local device
local devices=(
'/dev/sda'
'/dev/sdb'
)
local members
local number
local passphrase
local unit='mib'
# read passphrase
echo -n 'PassPhrase: '
read -r -s passphrase
#
lsblk
echo -n 'WIPE' "${devices[@]}" '/?\ OR CANCEL /!\'
read
#
number=0
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
((number++))
2024-11-11 16:14:04 +00:00
echo
echo "#${number}: ${device}"
2024-11-11 16:01:19 +00:00
#
2024-11-11 16:19:05 +00:00
parted "${device}" \
--script \
2024-11-11 16:14:04 +00:00
mktable gpt
2024-11-11 16:01:19 +00:00
#
2024-11-11 16:14:04 +00:00
parted "${device}" \
2024-11-11 16:19:05 +00:00
unit "${unit}" \
mkpart "crypt-${number}" 33282 7630885
2024-11-11 16:01:19 +00:00
#
2024-11-11 16:14:04 +00:00
parted "${device}" \
2024-11-11 16:19:05 +00:00
unit "${unit}" \
mkpart "boot-${number}" 514 33282
2024-11-11 16:01:19 +00:00
#
2024-11-11 16:14:04 +00:00
parted "${device}" \
2024-11-11 16:19:05 +00:00
unit "${unit}" \
mkpart "esp-${number}" 2 514
2024-11-11 16:14:04 +00:00
parted "${device}" \
set 3 esp on
2024-11-11 16:01:19 +00:00
#
2024-11-11 16:14:04 +00:00
parted "${device}" \
2024-11-11 16:19:05 +00:00
unit "${unit}" \
mkpart "bios-${number}" 1 2
2024-11-11 16:14:04 +00:00
parted "${device}" \
set 4 bios_grub on
2024-11-11 16:01:19 +00:00
done
#
number=0
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
((number++))
2024-11-11 16:14:04 +00:00
echo
echo "#${number}: ${device}4"
2024-11-11 16:01:19 +00:00
# wipe bios
dd \
2024-11-11 16:14:04 +00:00
if='/dev/zero' of="${device}4"
2024-11-11 16:01:19 +00:00
done
#
number=0
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
((number++))
2024-11-11 16:14:04 +00:00
echo
echo "#${number}: ${device}3"
2024-11-11 16:01:19 +00:00
# format esp
dd \
2024-11-11 16:14:04 +00:00
if='/dev/zero' of="${device}3" bs='1M'
2024-11-11 16:01:19 +00:00
mkfs.vfat \
2024-11-11 16:14:04 +00:00
-F 32 \
-S 4096 \
-i "0000000${number}" \
-n "esp-${number}" \
"${device}3"
2024-11-11 16:01:19 +00:00
# mount esp
mkdir --parents "/media/esp/${number}"
mount "${device}3" "/media/esp/${number}"
done
#
number=0
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
((number++))
2024-11-11 16:14:04 +00:00
echo
echo "#${number}: ${device}2"
2024-11-11 16:01:19 +00:00
# wipe boot
dd status='progress' \
2024-11-11 16:14:04 +00:00
if='/dev/zero' of="${device}2" bs='1G' count=1
2024-11-11 16:01:19 +00:00
done
#
members=()
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
members+=("${device}2")
done
mdadm \
2024-11-11 16:14:04 +00:00
--create '/dev/md/boot' \
--level 0 \
--metadata 1 \
--name 'md:boot' \
--raid-devices ${#devices[@]} \
--uuid '00000000:00000000:00000000:00000002' \
"${members[@]}"
2024-11-11 16:01:19 +00:00
#
mkfs.btrfs --force \
2024-11-11 16:14:04 +00:00
--checksum 'sha256' \
--label 'boot' \
--uuid '00000000-0000-0000-0000-00000000000b' \
'/dev/md/boot'
2024-11-11 16:01:19 +00:00
# mount boot
mkdir --parents '/media/boot'
mount \
2024-11-11 16:14:04 +00:00
--options 'autodefrag,compress-force=zstd' \
'/dev/md/boot' '/media/boot'
2024-11-11 16:01:19 +00:00
#
number=0
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
((number++))
2024-11-11 16:14:04 +00:00
echo
echo "#${number}: ${device}1"
2024-11-11 16:01:19 +00:00
# wipe crypt head
dd status='progress' \
2024-11-11 16:14:04 +00:00
if='/dev/zero' of="${device}1" bs='1G' count=1
2024-11-11 16:01:19 +00:00
done
#
members=()
2024-11-11 16:14:04 +00:00
for device in "${devices[@]}"; do
2024-11-11 16:01:19 +00:00
members+=("${device}1")
done
mdadm \
2024-11-11 16:14:04 +00:00
--create '/dev/md/crypt' \
--level 0 \
--metadata 1 \
--name 'md:crypt' \
--raid-devices ${#devices[@]} \
--uuid '00000000:00000000:00000000:00000001' \
"${members[@]}"
2024-11-11 16:01:19 +00:00
# encrypt
2024-11-11 16:14:04 +00:00
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'
2024-11-11 16:01:19 +00:00
# open
2024-11-11 16:14:04 +00:00
echo "${passphrase}" |
cryptsetup luksOpen '/dev/md/crypt' 'crypt'
2023-12-28 11:00:25 +00:00
}
2024-11-11 17:06:27 +00:00
rescue_wipe_2_make_hetzner_8_8() {
2024-11-11 16:01:19 +00:00
local passphrase
# close
cryptsetup luksClose 'crypt'
# read passphrase
echo -n 'PassPhrase: '
read -r -s passphrase
# encrypt
2024-11-11 16:14:04 +00:00
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'
2024-11-11 16:01:19 +00:00
# open
2024-11-11 16:14:04 +00:00
echo "${passphrase}" |
cryptsetup luksOpen '/dev/md/crypt' 'crypt'
2024-11-11 16:01:19 +00:00
# format crypt
mkfs.btrfs --force \
2024-11-11 16:14:04 +00:00
--checksum 'sha256' \
--label 'crypt' \
--uuid '00000000-0000-0000-0000-00000000000c' \
'/dev/mapper/crypt'
2024-11-11 16:01:19 +00:00
# mount crypt
mkdir --parents '/media/crypt'
mount \
2024-11-11 16:14:04 +00:00
--options 'autodefrag,compress-force=zstd' \
'/dev/mapper/crypt' '/media/crypt'
2024-11-11 16:01:19 +00:00
# make swap file
btrfs filesystem mkswapfile \
2024-11-11 16:14:04 +00:00
--size '64g' \
--uuid '00000000-0000-0000-0000-000000000005' \
'/media/crypt/swap'
2023-12-28 12:00:10 +00:00
}