From 7d4603da210eb550f633610b2a3a8551f2ece1ff Mon Sep 17 00:00:00 2001 From: har0ke Date: Mon, 27 Nov 2023 14:13:09 +0100 Subject: [PATCH] Add some tools --- tools/arch_do_partition.sh | 77 ++++++++++++++++++++++++++++++++++++++ tools/arch_fix_boot.sh | 8 ++++ tools/mount.sh | 45 ++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100755 tools/arch_do_partition.sh create mode 100644 tools/arch_fix_boot.sh create mode 100644 tools/mount.sh diff --git a/tools/arch_do_partition.sh b/tools/arch_do_partition.sh new file mode 100755 index 0000000..395060b --- /dev/null +++ b/tools/arch_do_partition.sh @@ -0,0 +1,77 @@ +#!/usr/bin/bash + +set -euo pipefail + +DISK=NOTFOUND + +# read arguments +opts=$(getopt \ + --longoptions "disk:" \ + --options "d:" \ + -- "$@" +) + +eval set --$opts + +while [[ $# -gt 0 ]]; do + case "$1" in + --disk | -d) + DISK="$2" + shift 2 + ;; + *) + break + ;; + esac +done + +PART_BOOT="${DISK}p1" +PART_LVM="${DISK}p2" +VG_NAME="vg0" + +function assert() ( + set +euo pipefail + "${@:2}" &> /dev/null + if [[ "$?" -ne 0 ]]; then + echo "$1" + exit 1 + fi +) + +function clean() ( + set +euo pipefail + swapoff "/dev/${VG_NAME}/swap" + umount "/mnt/boot" + umount "/mnt/" + cryptsetup close "${VG_NAME}-root" + cryptsetup close "${VG_NAME}-swap" + cryptsetup close "${VG_NAME}" + cryptsetup close cryptlvm + true +) + +assert "EFI Variables not set: Are you booted into UEFI?" ls /sys/firmware/efi/efivars + +clean + +echo -e 'size=512M, type=U\n size=+, type=L\n' | sfdisk "${DISK}" +sync + +mkfs.fat -F32 "${PART_BOOT}" + +cryptsetup luksFormat "${PART_LVM}" +cryptsetup open "${PART_LVM}" cryptlvm + +pvcreate /dev/mapper/cryptlvm +pvcreate /dev/mapper/cryptlvm +vgcreate "${VG_NAME}" /dev/mapper/cryptlvm +lvcreate -L 8G "${VG_NAME}" -n swap +lvcreate -l 100%FREE "${VG_NAME}" -n root +lvreduce -L -256M "${VG_NAME}/root" + +mkfs.ext4 "/dev/${VG_NAME}/root" +mkswap "/dev/${VG_NAME}/swap" + +mount "/dev/${VG_NAME}/root" /mnt +mount --mkdir "${PART_BOOT}" /mnt/boot +swapon "/dev/${VG_NAME}/swap" diff --git a/tools/arch_fix_boot.sh b/tools/arch_fix_boot.sh new file mode 100644 index 0000000..5ee0678 --- /dev/null +++ b/tools/arch_fix_boot.sh @@ -0,0 +1,8 @@ +#!/usr/bin/bash +set -euo pipefail + +script_dir="$(dirname -- "${BASH_SOURCE[0]}" )" + +"${script_dir}/mount.sh" + +sudo arch-chroot /mnt pacman -Suy diff --git a/tools/mount.sh b/tools/mount.sh new file mode 100644 index 0000000..3cdb58a --- /dev/null +++ b/tools/mount.sh @@ -0,0 +1,45 @@ +#!/usr/bin/bash +set -euo pipefail + +reset=0 +disk=/dev/nvme0n1p2 +volume_group="OkesVG" + +reset() { + swapon -s | grep "$(realpath /dev/mapper/${volume_group}-swap)" &> /dev/null && \ + swapoff "$(realpath /dev/mapper/${volume_group}-swap)" + df | grep /mnt/boot &> /dev/null && umount /mnt/boot + df | grep /mnt &> /dev/null && umount /mnt + [[ -e /dev/mapper/${volume_group}-root ]] && cryptsetup close ${volume_group}-root + [[ -e /dev/mapper/${volume_group}-swap ]] && cryptsetup close ${volume_group}-swap + [[ -e /dev/mapper/root ]] && cryptsetup close root +} + +# read arguments +opts=$(getopt \ + --longoptions "reset" \ + --options "r" \ + -- "$@" +) +eval set --$opts + +while [[ $# -gt 0 ]]; do + case "$1" in + --reset | -r) + reset=1 + shift 2 + ;; + *) + break + ;; + esac +done + +[[ "${reset}" -eq 1 ]] && reset +[[ -e /dev/mapper/root ]] || cryptsetup open "${disk}" root +vgchange -ay ${volume_group} +# sleep 2 +df | grep /mnt &> /dev/null || mount /dev/mapper/${volume_group}-root /mnt +df | grep /mnt/boot &> /dev/null || mount /dev/nvme0n1p1 /mnt/boot +swapon -s | grep "$(realpath /dev/mapper/${volume_group}-swap)" &> /dev/null || \ + swapon /dev/mapper/${volume_group}-swap