lmde-expert/linuxmint-postsetup.sh

418 lines
9.7 KiB
Bash
Raw Normal View History

2024-07-02 16:10:17 -04:00
#!/bin/bash
if [[ "$(id -u)" -ne 0 ]]; then
echo "ERROR: This needs to be run as root"
exit 250
fi
declare -rA SUBVOLS_DEFAULT=(
["@root"]="root"
["@srv"]="srv"
["@opt"]="opt"
["@local"]="usr/local"
["@cache"]="var/cache"
["@containers"]="var/lib/containers"
["@libvirt"]="var/lib/libvirt/images"
["@machines"]="var/lib/machines"
["@portables"]="var/lib/portables"
["@log"]="var/log"
["@spool"]="var/spool"
["@tmp"]="var/tmp"
["@www"]="var/www"
["@snapshots"]=".snapshots"
)
function show_help() {
echo "Usage: $0 <root> <boot> <efi> [<options>...]"
echo ""
echo "Positional Arguments:"
echo "<root> Root Partition used for LUKS or BtrFS filesystem"
echo "<boot> Boot Partition or @boot for subvolume in BtrFS"
echo "<efi> ESP Partition for booting"
echo ""
echo "Options:"
echo "-h, --help Help on this tool."
2024-07-02 20:16:20 -04:00
echo "-e, --encryption Enable LUKS encryption."
2024-07-02 16:10:17 -04:00
echo "-s, --swap Enable Swap/Hibernation support."
echo "-d, --debug Enable DEBUG mode for testing."
exit 0
}
function create_subvolumes() {
local -a subvols
if [[ "$DEBUG" ]]; then
local cmd="echo"
else
local cmd=""
fi
subvols+=("${!SUBVOLS_DEFAULT[@]}")
2024-07-02 20:16:20 -04:00
if [[ "$ENCRYPTION" ]]; then
2024-07-02 21:05:16 -04:00
${cmd} mount -o noatime,space_cache=v2,ssd "/dev/mapper/$ENC_VOL" /mnt
2024-07-02 20:16:20 -04:00
else
${cmd} mount -o noatime,space_cache=v2,ssd "$RootPart" /mnt
fi
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /mnt"
exit 100
fi
2024-07-02 16:10:17 -04:00
2024-07-02 16:14:14 -04:00
if [[ "$BootPart" == "@boot" ]]; then
2024-07-02 16:15:57 -04:00
${cmd} btrfs subvolume create /mnt/@boot
2024-07-02 16:14:14 -04:00
${cmd} rsync -avhHi --delete-after "/mnt/@/boot/" "/mnt/@boot/"
fi
2024-07-02 16:10:17 -04:00
if [[ "$SWAP" ]]; then
${cmd} btrfs subvolume create /mnt/@swap
fi
for subvol in "${subvols[@]}"
do
${cmd} btrfs subvolume create /mnt/"$subvol"
2024-07-02 20:16:20 -04:00
if [[ -d "/mnt/@/${SUBVOLS_DEFAULT[$subvol]}" || "$DEBUG" ]]; then
${cmd} rsync -avhHi --delete-after "/mnt/@/${SUBVOLS_DEFAULT[$subvol]}/" "/mnt/$subvol/"
fi
2024-07-02 16:10:17 -04:00
done
${cmd} umount /mnt
}
2024-07-02 20:16:20 -04:00
function unmount_target() {
local subvol
if [[ "$DEBUG" ]]; then
local cmd="echo"
else
local cmd=""
fi
for subvol in "${!SUBVOLS_DEFAULT[@]}"
do
${cmd} umount /target/"${SUBVOLS_DEFAULT[$subvol]}"
done
if [[ "$SWAP" ]]; then
${cmd} umount /target/swap
fi
${cmd} umount /target/boot/efi
${cmd} umount /target/boot
${cmd} umount /target
if [[ "$ENCRYPTION" ]]; then
2024-07-02 21:05:16 -04:00
${cmd} cryptsetup luksClose "$ENC_VOL"
2024-07-02 20:16:20 -04:00
fi
}
2024-07-02 16:10:17 -04:00
function get_hibernate_size() {
free --giga | awk '/^Mem:/{print $2}'
}
function prepare_target() {
local subvol
local rootmount
if [[ "$DEBUG" ]]; then
local cmd="echo"
else
local cmd=""
fi
${cmd} mkdir /target
rootmount="$RootPart"
2024-07-02 20:16:20 -04:00
if [[ "$ENCRYPTION" ]]; then
2024-07-02 21:05:16 -04:00
rootmount="/dev/mapper/$ENC_VOL"
2024-07-02 20:16:20 -04:00
fi
2024-07-02 16:10:17 -04:00
${cmd} mount -o noatime,space_cache=v2,ssd,subvol=@ "$rootmount" /target
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /target"
exit 101
fi
2024-07-02 16:10:17 -04:00
for subvol in "${!SUBVOLS_DEFAULT[@]}"
do
2024-07-02 16:24:58 -04:00
if [[ ! -d "/target/${SUBVOLS_DEFAULT[$subvol]}" || "$DEBUG" ]]; then
${cmd} mkdir -p /target/"${SUBVOLS_DEFAULT[$subvol]}"
fi
2024-07-02 16:10:17 -04:00
done
#${cmd} mkdir -p /target/boot
if [[ "$BootPart" == "@boot" ]]; then
${cmd} mount -o noatime,space_cache=v2,ssd,subvol=@boot "$rootmount" /target/boot
else
${cmd} mount "$BootPart" /target/boot
fi
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /target/boot"
exit 102
fi
2024-07-02 16:10:17 -04:00
2024-07-02 16:33:00 -04:00
${cmd} mount -o noatime,space_cache=v2,ssd,subvol=@home "$rootmount" /target/home
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /target/home"
exit 103
fi
2024-07-02 16:33:00 -04:00
2024-07-02 16:10:17 -04:00
for subvol in "${!SUBVOLS_DEFAULT[@]}"
do
${cmd} mount -o noatime,space_cache=v2,ssd,subvol="$subvol" "$rootmount" /target/"${SUBVOLS_DEFAULT[$subvol]}"
done
2024-07-02 16:33:00 -04:00
if [[ ! -d "/target/boot/efi" ]]; then
${cmd} mkdir -p /target/boot/efi
fi
2024-07-02 16:10:17 -04:00
${cmd} mount "$EFIPart" /target/boot/efi
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /target/boot/efi"
exit 104
fi
2024-07-02 16:10:17 -04:00
if [[ "$SWAP" ]]; then
2024-07-02 16:24:58 -04:00
if [[ ! -d "/target/swap" ]]; then
${cmd} mkdir -p /target/swap
fi
2024-07-02 16:10:17 -04:00
${cmd} mount -o noatime,ssd,subvol=@swap "$rootmount" /target/swap
2024-07-02 21:05:16 -04:00
if [[ "$?" -ne 0 ]]; then
echo "FATAL: Could not mount /target/swap"
exit 105
fi
2024-07-02 16:10:17 -04:00
${cmd} btrfs filesystem mkswapfile --size "$(get_hibernate_size)g" --uuid clear /target/swap/hibernate.swp
fi
2024-07-02 16:48:15 -04:00
if [[ "$SWAP" || "$BootPart" == "@boot" ]]; then
${cmd} arch-chroot /target update-grub2
fi
2024-07-02 20:16:20 -04:00
${cmd} apt install -y arch-install-scripts
2024-07-02 16:48:15 -04:00
2024-07-02 20:16:20 -04:00
if [[ "$DEBUG" ]]; then
echo "genfstab -U /target > /target/etc/fstab"
echo
else
genfstab -U /target > /target/etc/fstab
fi
2024-07-02 16:48:15 -04:00
2024-07-02 20:16:20 -04:00
if [[ "$SWAP" ]]; then
if [[ "$DEBUG" ]]; then
echo "echo \"/swap/hibernate.swp none swap defaults 0 0\" >> /target/etc/fstab"
else
echo "/swap/hibernate.swp none swap defaults 0 0" >> /target/etc/fstab
fi
SwapUUID=$(grep btrfs /target/etc/fstab | head -n1 | cut -f1)
SwapOffset=$(btrfs inspect-internal map-swapfile -r /target/swap/hibernate.swp)
${cmd} sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/\(\"[^\"]*\)$/ resume=${SwapUUID} resume_offset=${SwapOffset}&/" /target/etc/default/grub
fi
2024-07-02 21:05:16 -04:00
#if [[ "$ENCRYPTION" ]]; then
#
#fi
2024-07-02 16:10:17 -04:00
}
function expert_step() {
local UUID PART_ENTRY_UUID
local SwapUUID SwapOffset
if [[ "$DEBUG" ]]; then
local cmd="echo"
else
local cmd=""
fi
${cmd} apt install -y arch-install-scripts
if [[ "$DEBUG" ]]; then
2024-07-02 16:42:24 -04:00
echo "genfstab -U /target > /target/etc/fstab"
2024-07-02 16:24:58 -04:00
echo "==== genfstab result : begin ===="
genfstab -U /target
echo "==== genfstab result : end ===="
echo
2024-07-02 16:10:17 -04:00
else
2024-07-02 16:42:24 -04:00
genfstab -U /target > /target/etc/fstab
2024-07-02 16:10:17 -04:00
fi
if [[ "$SWAP" ]]; then
if [[ "$DEBUG" ]]; then
echo "echo \"/swap/hibernate.swp none swap defaults 0 0\" >> /target/etc/fstab"
else
echo "/swap/hibernate.swp none swap defaults 0 0" >> /target/etc/fstab
fi
SwapUUID=$(grep btrfs /target/etc/fstab | head -n1 | cut -f1)
SwapOffset=$(btrfs inspect-internal map-swapfile -r /target/swap/hibernate.swp)
2024-07-02 16:33:00 -04:00
${cmd} sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/\(\"[^\"]*\)$/ resume=${SwapUUID} resume_offset=${SwapOffset}&/" /target/etc/default/grub
2024-07-02 16:10:17 -04:00
fi
}
function show_options() {
echo "Root Partition: $RootPart"
echo "Boot Partition: $BootPart"
echo "EFI Partition: $EFIPart"
2024-07-02 20:16:20 -04:00
if [[ "$ENCRYPTION" ]]; then
echo "Encryption: Enabled"
else
echo "Encryption: Disabled"
fi
2024-07-02 16:10:17 -04:00
if [[ "$SWAP" ]]; then
echo "Swap: Enabled"
else
echo "Swap: Disabled"
fi
if [[ "$DEBUG" ]]; then
echo "Debug: Enabled"
else
echo "Debug: Disabled"
fi
echo
}
function install_normal() {
show_options
read -rsn1 -p"Pre-Installation: To proceed, press enter to continue." proceed
echo
if [[ "$proceed" != "" ]]; then
echo "Aborting."
exit 1
fi
echo
echo "Preparing Subvolumes..."
create_subvolumes
echo
echo "Preparing Installation Target..."
prepare_target
echo
2024-07-02 20:16:20 -04:00
echo "Post-Setup Preparations Complete. You can verify things in /target or you can"
echo "re-run this script with --unmount to unmount and reboot."
2024-07-02 16:10:17 -04:00
}
2024-07-02 20:16:20 -04:00
function install_unmount() {
2024-07-02 16:10:17 -04:00
show_options
2024-07-02 20:16:20 -04:00
read -rsn1 -p"Post-Install Unmount and Reboot: To proceed, press enter to continue." proceed
2024-07-02 16:10:17 -04:00
echo
if [[ "$proceed" != "" ]]; then
echo "Aborting."
exit 1
fi
echo
echo "Running Expert-Mode Installation Steps..."
2024-07-02 20:16:20 -04:00
unmount_target
reboot
2024-07-02 16:10:17 -04:00
}
function install_cleanup() {
if [[ "$DEBUG" ]]; then
local cmd="echo"
else
local cmd=""
fi
for subvol in "${!SUBVOLS_DEFAULT[@]}"
do
${cmd} umount /target/"${SUBVOLS_DEFAULT[$subvol]}"
done
if [[ "$SWAP" ]]; then
${cmd} umount /target/swap
fi
${cmd} umount /target/boot/efi
${cmd} umount /target/boot
${cmd} umount /target
if [[ "$ENCRYPTION" ]]; then
2024-07-02 21:05:16 -04:00
${cmd} cryptsetup luksClose "$ENC_VOL"
2024-07-02 16:10:17 -04:00
fi
${cmd} dd if=/dev/zero of="${RootPart}" bs=1024 count=10
${cmd} dd if=/dev/zero of="${EFIPart}" bs=1024 count=10
if [[ "$BootPart" != "@boot" ]]; then
${cmd} dd if=/dev/zero of="${BootPart}" bs=1024 count=10
fi
}
declare -a POSITIONAL_ARGS=()
declare INSTALL_MODE="normal"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
;;
2024-07-02 20:16:20 -04:00
-e|--encryption)
ENCRYPTION=true
2024-07-02 21:05:16 -04:00
ENC_VOL="$2"
2024-07-02 20:16:20 -04:00
shift
;;
2024-07-02 16:10:17 -04:00
-s|--swap)
SWAP=true
shift
;;
-d|--debug)
DEBUG=true
shift
;;
2024-07-02 20:16:20 -04:00
--unmount)
INSTALL_MODE=unmount
2024-07-02 16:10:17 -04:00
shift
;;
--clean)
INSTALL_MODE=clean
shift
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
RootPart="$1"
BootPart="$2"
EFIPart="$3"
if [[ -z "$RootPart" || -z "$BootPart" || -z "$EFIPart" ]]; then
echo "ERROR: Invalid parameters. See --help for help"
exit 3
else
if [[ "$ENCRYPTION" && "$BootPart" == "@boot" ]]; then
2024-07-02 21:05:16 -04:00
if [[ -z "$ENC_VOL" ]]; then
echo "Encryption volume needs to be set. Reliably determining this is"
echo "best done by human eyes. To find your current volume, you can"
echo "run:"
echo " dmsetup ls --target crypt"
echo "If it's not active for whatever reason, you can open it with:"
echo " cryptsetup luksOpen "$RootPart" <volume_name>"
exit 4
elif [[ "$BootPart" == "@boot" ]]; then
echo "While encryption is enabled, using @boot subvolume will cause issues."
echo "Please prepare and set boot volume for /boot while using encryption."
exit 5
fi
2024-07-02 16:10:17 -04:00
fi
case "$INSTALL_MODE" in
2024-07-02 20:16:20 -04:00
normal) install_normal;;
unmount) install_unmount;;
clean) install_cleanup;;
2024-07-02 16:10:17 -04:00
*)
echo "Error, unknown installation mode detected."
exit 3
;;
esac
fi