sh/bash/rescue-hetzner.sh

289 lines
5.8 KiB
Bash
Raw Normal View History

2024-11-11 15:57:37 +00:00
rescue_hetzner_install() {
2024-11-11 16:01:19 +00:00
local package
local release='bookworm'
local packages=(
2024-11-11 16:14:04 +00:00
# installed
'dmidecode' 'efibootmgr' 'pciutils' 'usbutils'
'parted' 'mdadm' 'cryptsetup-bin' 'lvm2'
'btrfs-progs' 'dosfstools'
'git' 'nano' 'python3' 'rsync' 'vim'
'file' 'htop' 'lsof' 'man-db' 'tree' 'uuid-runtime'
# install
'lshw'
'duperemove' 'squashfs-tools'
'grub-efi-amd64-bin' 'grub-pc-bin'
'libdigest-sha3-perl' 'micro'
'iotop'
'exa' 'ipcalc' 'lf' 'ncdu' 'nnn' 'ranger'
2024-11-11 16:01:19 +00:00
)
local backports=(
)
# update catalog
apt-get update
#
debian_disable_frontend
# upgrade packages
apt-get upgrade --assume-yes
#
apt_clean_cache
# install packages
2024-11-11 16:14:04 +00:00
for package in "${packages[@]}"; do
echo
echo "${package}"
2024-11-11 16:01:19 +00:00
apt-get install \
2024-11-11 16:14:04 +00:00
--assume-yes \
"${package}"
2024-11-11 16:01:19 +00:00
apt_clean_cache
done
# install backports
2024-11-11 16:14:04 +00:00
for package in "${backports[@]}"; do
echo
echo "${package}"
2024-11-11 16:01:19 +00:00
apt-get install \
2024-11-11 16:14:04 +00:00
--assume-yes \
--target-release "${release}-backports" \
"${package}"
2024-11-11 16:01:19 +00:00
apt_clean_cache
done
2023-10-02 15:30:04 +00:00
}
2024-11-11 15:57:37 +00:00
rescue_hetzner_upload() {
2024-11-11 16:02:08 +00:00
local host="${1}"
local hostname="${2}"
2024-11-11 16:14:04 +00:00
if [ "${hostname}" ]; then
2024-11-11 16:33:55 +00:00
local user="root"
2024-11-11 16:02:08 +00:00
#
local user_host="${user}@${host}"
# remove fingerprints
ssh-keygen -R "${host}"
# copy ssh id
ssh-copy-id \
2024-11-11 16:33:55 +00:00
-o "StrictHostKeyChecking=accept-new" \
2024-11-11 16:14:04 +00:00
"${user_host}"
2024-11-11 16:02:08 +00:00
# upload root
2024-11-11 16:33:55 +00:00
rsync --delete --recursive \
"${MAIN_BASH_ROOT}/" "${user_host}:/etc/bash/"
2024-11-11 16:02:08 +00:00
# call setup
# TODO variable
2024-11-11 16:33:55 +00:00
ssh "${user_host}" -- "\
source \"/etc/bash/main.sh\" ; rescue_configure \"${hostname}\""
2024-11-11 16:02:08 +00:00
# create session
ssh "${user_host}" -- byobu new-session -d
# send keys
2024-11-11 16:33:55 +00:00
ssh "${user_host}" -- byobu send-keys "rescue_hetzner_install" "C-m"
2024-11-11 16:02:08 +00:00
# attach session
mosh "${user_host}" -- byobu attach-session
else
2024-11-11 16:33:55 +00:00
echo "Host?"
2024-11-11 16:02:08 +00:00
return 1
fi
2023-10-02 15:30:04 +00:00
}
2023-10-03 02:18:00 +00:00
2024-11-11 15:57:37 +00:00
rescue_hetzner_wipe_8_8_0_init() {
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 15:57:37 +00:00
rescue_hetzner_wipe_8_8_1_zero() {
2024-11-11 16:01:19 +00:00
# wipe crypt
dd status='progress' \
2024-11-11 16:14:04 +00:00
if='/dev/zero' of='/dev/mapper/crypt' bs='8G'
2023-12-28 07:36:46 +00:00
}
2024-11-11 15:57:37 +00:00
rescue_hetzner_wipe_8_8_2_make() {
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
}
2024-11-11 15:57:37 +00:00
rescue_hetzner_wipe_8_8_3_close() {
2024-11-11 16:01:19 +00:00
umount '/media/boot'
#
2024-11-11 16:14:04 +00:00
umount '/media/crypt' &&
cryptsetup luksClose 'crypt'
2023-12-28 07:36:46 +00:00
}