Files
PBX-Image-Script/CloneToDisk.sh
T
2026-04-28 12:00:42 -05:00

234 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
set -e
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
RESET='\e[0m'
NO_WRITE=false
NO_RETABLE=false
NO_USB=false
NO_CLEANUP=false
IMG_FILE="PBX.img3.lz4"
MOUNT_PATH="/mnt/debian"
MOUNT_POINTS=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run")
VGROUP="PBX-vg"
ROOT_LV="root"
SWAP_LV="swap_1"
VG_PATH="/dev/mapper/PBX--vg-"
GRUB_CMD="grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian && update-grub"
for arg in "$@"; do
case $arg in
--nowrite)
NO_WRITE=true
shift
;;
--noretable)
NO_RETABLE=true
shift
;;
--nousb)
NO_USB=true
shift
;;
--nocleanup)
NO_CLEANUP=true
shift
;;
-h)
printf "%-20s %s\n" "Flags:" "Description:"
echo
printf "%-20s %s\n" "--nowrite" "Disables Writing to Disk only modifys Existing Partitions"
exit 1
;;
*)
echo "Unknown option: $arg"
echo "Try -h"
exit 1
;;
esac
done
main() {
root_check
udevadm trigger
dialogue
trap 'echo "Status Code: $?"; cleanup;' EXIT
if [[ $NO_RETABLE == true ]]; then
echo -e "${YELLOW}--noretable is specified. Tables will not be erased and no partitions will be created${RESET}"
else
rewrite_gpt_table
lvm_init
make_filesystem
fi
write_img
gen_fstab
install_grub
cleanup
echo -e "${GREEN}Restore and resize complete!${RESET}"
}
root_check() {
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}You must be root to do this." 1>&2
exit 100
fi
}
dialogue() {
lsblk
echo
read -rp "Enter the target disk (e.g., sda): " disk_suffix
disk_name="/dev/${disk_suffix}"
echo "Chose disk $disk_name"
echo
}
rewrite_gpt_table() {
sgdisk -Z $disk_name
sgdisk -c $disk_name
sgdisk -n 1:1MiB:+512M -t 1:ef00 -c 1:"EFI System" $disk_name
sgdisk -n 2:0:+1G -t 2:8300 -c 2:"boot" $disk_name
sgdisk -n 3:0:0 -t 3:8e00 -c 3:"Linux LVM" $disk_name
partprobe || true
}
lvm_init() {
pvcreate $(get_partition $disk_name "3")
vgcreate $VGROUP $(get_partition $disk_name "3")
vgchange -ay $VGROUP
lvcreate -L 4G $VGROUP -n swap_1
lvcreate -l 100%FREE $VGROUP -n root
}
make_filesystem() {
mkfs.vfat -F 32 $(get_partition $disk_name "1")
mkfs.ext4 $(get_partition $disk_name "2")
mkswap "${VG_PATH}${SWAP_LV}"
}
write_img() {
if [[ $NO_WRITE == true ]]; then
echo -e "${YELLOW}--nowrite is specified. No need for Img no disks will be written to${RESET}"
return 0
fi
img_file_dir="null"
if [[ -f "./${IMG_FILE}" ]]; then
img_file_dir="./${IMG_FILE}"
elif [[ -f "./Image/${IMG_FILE}" ]]; then
img_file_dir="./Image/${IMG_FILE}"
elif [[ ! -f $img_file_dir ]]
echo "${RED}NO IMG FILE FOUND${RESET}"
exit 100
fi
echo "Writing image to $(get_partition $disk_name "3")..."
lz4 -dc ${img_file_dir} | partclone.ext4 -r -o $(get_partition $disk_name "3")
partprobe || true
}
cleanup() {
if [[ $NO_CLEANUP = true ]]; then
echo -e "${YELLOW}No cleanup specified. Make sure to manually clean mounts." 1>&2
exit 100
fi
echo "Cleaning Mounts"
umount -lf /mnt/ventoy 2>/dev/null || true
for i in "${MOUNT_POINTS[@]}"; do umount -lf "/mnt/debian$i" 2>/dev/null || true; done
umount -lf /mnt/debian/boot/efi 2>/dev/null || true
umount -lf /mnt/debian/boot 2>/dev/null || true
umount -lf /mnt/debian 2>/dev/null || true
vgchange -an $VGROUP
}
get_partition() {
local disk_name="$1"
local part_num="$2"
local part_path="$(
lsblk -lnp -o NAME,TYPE "$disk_name" |
awk '$2=="part"{print $1}' |
sed -n "${part_num}p"
)"
printf "$part_path"
}
get_partuuid() {
local disk_name="$1"
local part_num="$2"
local part_uuid="$(
lsblk -ln -o PATH,PARTUUID $disk_name | awk '$1==$(get_partition $disk_name $part_num) {print $2}'
)"
printf "$part_uuid"
}
mount_chroot() {
vgchange -ay $VGROUP
echo "Mounting restored system..."
mkdir -p $MOUNT_PATH/boot
mount /dev/mapper/PBX--vg-root $MOUNT_PATH
mount $(get_partition $disk_name "2") ${MOUNT_PATH}/boot
mkdir -p $MOUNT_PATH/boot/efi
mount $(get_partition $disk_name "1") ${MOUNT_PATH}/boot/efi
for i in "${MOUNT_POINTS[@]}"; do mount -B $i ${MOUNT_PATH}${i}; done
}
gen_fstab() {
echo "Generating fstab..."
boot_uuid=$()
cp ./fstab_template ${MOUNT_PATH}/etc/fstab
sed --in-place "s/boot_uuid/$(get_partuuid $disk_name "2")/g" ${MOUNT_PATH}/etc/fstab
sed --in-place "s/efi_uuid/$(get_partuuid $disk_name "1")/g" ${MOUNT_PATH}/etc/fstab
echo "${GREEN}fstab write complete!${RESET}"
}
install_grub() {
echo "Entering chroot to reinstall GRUB..."
chroot $MOUNT_PATH /bin/bash -c $GRUB_CMD
echo "${GREEN}Grub installed!${RESET}"
}
main