sh/bash/ovh-rescue.sh

176 lines
4.3 KiB
Bash
Raw Normal View History

2024-11-11 11:17:52 +00:00
#! /usr/bin/env sh
2024-11-11 10:57:19 +00:00
ovh_rescue_configure() {
2024-11-11 11:29:48 +00:00
local host="${1}"
2023-05-13 01:05:57 +00:00
# apt / conf
2024-11-11 11:17:52 +00:00
printf "\
2023-05-12 22:49:39 +00:00
Acquire::AllowInsecureRepositories False;
Acquire::AllowWeakRepositories False;
Acquire::AllowDowngradeToInsecureRepositories False;
Acquire::Check-Valid-Until True;
APT::Install-Recommends False;
APT::Install-Suggests False;
APT::Get::Show-Versions True;
Dir::Etc::SourceParts '';
Dpkg::Progress True;
2024-11-11 11:17:52 +00:00
" > "/etc/apt/apt.conf"
2023-05-13 01:05:57 +00:00
# apt / sources
2024-11-11 11:17:52 +00:00
printf "\
2023-05-12 22:49:39 +00:00
deb https://deb.debian.org/debian buster main contrib non-free
deb https://deb.debian.org/debian buster-backports main contrib non-free
deb https://deb.debian.org/debian buster-updates main contrib non-free
deb https://deb.debian.org/debian-security buster/updates main contrib non-free
2024-11-11 11:17:52 +00:00
" > "/etc/apt/sources.list"
2023-05-13 01:05:57 +00:00
# bash / rc
2023-05-15 10:07:16 +00:00
main_link_bashrc
2023-05-13 01:05:57 +00:00
# host name
2023-05-19 18:14:13 +00:00
hostname "${host}"
2023-05-13 01:05:57 +00:00
# locales
2024-11-11 11:17:52 +00:00
printf "\
2023-05-13 01:05:57 +00:00
en_US.UTF-8 UTF-8
fr_FR.UTF-8 UTF-8
" > '/etc/locale.gen'
# fix alias
2024-11-11 11:17:52 +00:00
rm --force "/usr/share/locale/locale.alias"
ln --symbolic "/etc/locale.alias" "/usr/share/locale/locale.alias"
2023-05-13 01:05:57 +00:00
# generate locales
locale-gen
2023-05-13 02:08:37 +00:00
# update catalog
apt-get update
2023-05-14 09:51:45 +00:00
#
debian_disable_frontend
2023-05-13 02:08:37 +00:00
# install packages
2024-11-11 11:29:48 +00:00
apt-get install --assume-yes \
"byobu" \
"mosh"
2023-05-15 19:23:36 +00:00
#
apt_clean_cache
2023-05-13 00:54:16 +00:00
}
2024-11-11 11:17:52 +00:00
ovh_rescue_install() {
local release="buster"
2023-05-13 01:05:57 +00:00
# update catalog
2023-05-12 22:49:39 +00:00
apt-get update
#
2023-05-14 09:51:45 +00:00
debian_disable_frontend
2023-05-13 01:05:57 +00:00
# upgrade packages
2023-05-12 22:49:39 +00:00
apt-get upgrade --assume-yes
2023-05-15 19:23:36 +00:00
#
apt_clean_cache
2023-05-13 01:05:57 +00:00
# install packages
2024-11-11 11:29:48 +00:00
apt-get install --assume-yes \
"parted" "mdadm" "lvm2" \
\
"lshw" \
"file" "micro" \
"grub-efi-amd64-bin" "grub-pc-bin" \
"htop" "iotop" "lsof" \
"exa" "ncdu" "nnn" "ranger" "tree" \
"squashfs-tools" \
"uuid-runtime"
2023-05-15 19:23:36 +00:00
#
apt_clean_cache
2023-05-13 01:05:57 +00:00
# install backports
2023-05-12 22:49:39 +00:00
apt-get install --assume-yes \
2024-11-11 11:29:48 +00:00
--target-release "${release}-backports" \
"cryptsetup-bin" "rsync" \
\
"git"
2023-05-15 19:23:36 +00:00
#
apt_clean_cache
2023-05-12 22:49:39 +00:00
}
2024-11-11 11:17:52 +00:00
ovh_rescue_upload() {
2023-05-12 22:49:39 +00:00
local host="${1}"
if [ "${host}" ] ; then
2024-11-11 11:17:52 +00:00
local user="root"
2023-05-12 22:49:39 +00:00
#
local user_host="${user}@${host}"
2023-05-13 01:05:57 +00:00
# remove fingerprints
2023-05-12 22:49:39 +00:00
ssh-keygen -R "${host}"
2023-05-13 01:05:57 +00:00
# copy ssh id
2023-05-12 22:49:39 +00:00
ssh-copy-id \
2024-11-11 11:17:52 +00:00
-o "StrictHostKeyChecking=accept-new" \
2023-05-12 22:49:39 +00:00
"${user_host}"
# upload root
2023-05-15 07:19:43 +00:00
rsync --delete --recursive "${MAIN_BASH_ROOT}/" "${user_host}:/etc/bash/"
2023-05-12 22:49:39 +00:00
# call setup
2023-05-14 09:55:08 +00:00
# TODO variable
2024-11-11 11:17:52 +00:00
ssh "${user_host}" -- \
"source \"/etc/bash/main.sh\" ; ovh-rescue-configure \"${host}\""
2023-05-12 22:49:39 +00:00
# create session
ssh "${user_host}" -- byobu new-session -d
2023-05-13 09:30:41 +00:00
# send keys
2024-11-11 11:17:52 +00:00
ssh "${user_host}" -- byobu send-keys "ovh-rescue-install" "C-m"
2023-05-12 22:49:39 +00:00
# attach session
mosh "${user_host}" -- byobu attach-session
else
echo 'Host?'
return 1
fi
}
2023-05-13 00:26:42 +00:00
2024-11-11 11:17:52 +00:00
ovh_rescue_wipe_1_2TB() {
local device="/dev/sda"
local unit="mib"
2023-05-13 00:26:42 +00:00
#
2023-05-13 01:28:08 +00:00
lsblk
2024-11-11 11:17:52 +00:00
printf "%s" "WIPE ${device} /?\\ OR CANCEL /!\\"
2024-11-11 10:57:19 +00:00
read -r
2023-05-13 00:26:42 +00:00
#
parted "${device}" --script mktable gpt
#
2024-11-11 11:17:52 +00:00
parted "${device}" unit "${unit}" mkpart "crypt" 65795 1907729
2023-05-13 00:26:42 +00:00
#
2024-11-11 11:17:52 +00:00
parted "${device}" unit "${unit}" mkpart "boot" 259 65795
2023-05-13 00:26:42 +00:00
#
2024-11-11 11:17:52 +00:00
parted "${device}" unit "${unit}" mkpart "esp" 2 259
2023-05-13 00:26:42 +00:00
parted "${device}" set 3 esp on
#
parted "${device}" unit "${unit}" mkpart bios 1 2
parted "${device}" set 4 bios_grub on
# wipe bios
2024-11-11 11:17:52 +00:00
dd if="/dev/zero" of="/dev/sda4"
2023-05-13 00:26:42 +00:00
# format esp
2024-11-11 11:17:52 +00:00
mkfs.vfat -F 32 -n "esp" "/dev/sda3"
2023-05-13 00:26:42 +00:00
# format boot
2024-11-11 11:17:52 +00:00
mkfs.ext4 -F -L "boot" "/dev/sda2"
2023-05-13 01:18:59 +00:00
# read passphrase
local passphrase
2024-11-11 11:17:52 +00:00
printf "PassPhrase: "
2023-05-13 01:23:05 +00:00
read -r -s passphrase
2023-05-13 00:26:42 +00:00
# encrypt
2023-05-13 01:18:59 +00:00
echo "${passphrase}" \
| cryptsetup \
2023-05-13 00:26:42 +00:00
--verbose \
2023-05-13 01:18:59 +00:00
--batch-mode \
2024-11-11 11:17:52 +00:00
--type "luks2" \
--pbkdf "argon2id" \
--cipher "aes-xts-plain64" \
2023-05-13 00:26:42 +00:00
--iter-time 8192 \
--key-size 512 \
2024-11-11 11:17:52 +00:00
--hash "sha512" \
2023-05-13 00:26:42 +00:00
--use-random \
luksFormat \
2024-11-11 11:17:52 +00:00
"/dev/sda1"
2023-05-13 00:26:42 +00:00
# open
2023-05-13 01:18:59 +00:00
echo "${passphrase}" \
2024-11-11 11:17:52 +00:00
| cryptsetup luksOpen "/dev/sda1" "crypt"
2023-05-13 00:26:42 +00:00
# pv
2024-11-11 11:17:52 +00:00
pvcreate "/dev/mapper/crypt"
2023-05-13 00:26:42 +00:00
# vg
2024-11-11 11:17:52 +00:00
vgcreate "crypt" "/dev/mapper/crypt"
2023-05-13 00:26:42 +00:00
# lv swap
2024-11-11 11:17:52 +00:00
lvcreate --name "swap" --size "68719476736b" "crypt"
2023-05-13 00:26:42 +00:00
# lv data
2024-11-11 11:17:52 +00:00
lvcreate --name "data" --extents "100%FREE" "crypt"
2023-05-13 00:26:42 +00:00
# format swap
2024-11-11 11:17:52 +00:00
mkswap --label "swap" "/dev/mapper/crypt-swap"
2023-05-13 00:26:42 +00:00
# format data
2024-11-11 11:17:52 +00:00
mkfs.ext4 -L "data" "/dev/mapper/crypt-data"
2023-05-13 00:26:42 +00:00
# vg off
2024-11-11 11:17:52 +00:00
vgchange --activate "n" "crypt"
2023-05-13 00:26:42 +00:00
# close
2024-11-11 11:17:52 +00:00
cryptsetup luksClose "crypt"
2023-05-13 00:26:42 +00:00
}