diff --git a/CloneToDisk.sh b/CloneToDisk.sh index 9f861ab..442b1bf 100755 --- a/CloneToDisk.sh +++ b/CloneToDisk.sh @@ -8,6 +8,7 @@ GREEN='\e[32m' YELLOW='\e[33m' RESET='\e[0m' + # root check if [[ $EUID -ne 0 ]]; then echo -e "${RED}You must be root to do this." 1>&2 @@ -17,6 +18,7 @@ fi mount_points=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run") NO_WRITE=false + # arguments for arg in "$@"; do case $arg in @@ -24,6 +26,10 @@ for arg in "$@"; do NO_WRITE=true shift ;; + --noretable) + NO_RETABLE=true + shift + ;; -h) printf "%-20s %s\n" "Flags:" "Description:" echo @@ -52,6 +58,7 @@ cleanup() { } trap 'echo "Status Code: $?"; cleanup;' EXIT + get_partition() { local disk_name="$1" local part_num="$2" @@ -61,27 +68,54 @@ get_partition() { | 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" +} + + # Set your target disk here udevadm trigger lsblk echo - - read -rp "Enter the target disk (e.g., sda): " disk_suffix disk_name="/dev/${disk_suffix}" echo "Chose disk $disk_name" echo + +if [[ $NO_RETABLE == true ]]; then + echo -e "${YELLOW}--noretable is specified. Tables will not be erased and no partitions will be created${RESET}" + else + 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 +fi + if [[ $NO_WRITE == true ]]; then - echo -e "${YELLOW}--nowrite is specified no need for Img no disks will be written to${RESET}" -else - read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix + echo -e "${YELLOW}--nowrite is specified. No need for Img no disks will be written to${RESET}" + else + read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix usb_name="/dev/mapper/${disk_suffix}" @@ -99,27 +133,17 @@ else mount $usb_name /mnt/ventoy cd /mnt/ventoy/Images - echo "Writing image to $disk_name..." - lz4 -dc PBX.img.lz4 | dd of=$disk_name status=progress + echo "Writing image to $(get_partition $disk_name "3")..." + lz4 -dc PBX.img.lz4 | dd of=$(get_partition $disk_name "3") status=progress + + partprobe || true fi -# Repartition disk using sgdisk -echo "Repairing partition tables" -sgdisk -e $disk_name || true -sgdisk -v $disk_name - -partprobe || true - -echo "Expanding Partition 3 on $disk_name..." -sgdisk --delete=3 $disk_name -sgdisk --new=3:0:0 --typecode=3:8e00 --change-name=3:"Linux LVM" $disk_name - -partprobe || true - # Resize physical volume echo "Resizing physical volume..." -pvresize "$(get_partition $disk_name "3")" || true +pvresize $(get_partition $disk_name "3") || true + # Resize swap and root logical volumes echo "Resizing swap and root volumes..." @@ -128,18 +152,27 @@ lvresize -l +100%FREE --resizefs PBX-vg/root # Mount restored system for chroot +mount_path="/mnt/debian" echo "Mounting restored system..." -mkdir -p /mnt/debian -mount /dev/mapper/PBX--vg-root /mnt/debian/ -mount "$(get_partition $disk_name "2")" /mnt/debian/boot -mount "$(get_partition $disk_name "3")" /mnt/debian/boot/efi +mkdir -p $mount_path +mount /dev/mapper/PBX--vg-root $mount_path +mount $(get_partition $disk_name "2") ${mount_path}/boot +mount $(get_partition $disk_name "1") ${mount_path}/boot/efi +for i in "${mount_points[@]}"; do mount -B $i ${mount_path}${i}; done + +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 -for i in "${mount_points[@]}"; do mount -B $i /mnt/debian$i; done # Chroot and reinstall GRUB echo "Entering chroot to reinstall GRUB..." -chroot /mnt/debian /bin/bash -c " +chroot $mount_path /bin/bash -c " grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian update-grub "