Files
PBX-Image-Script/CloneToDisk.sh
2025-06-25 13:22:56 -05:00

65 lines
1.6 KiB
Bash

#!/bin/bash
set -e
# Set your target disk here
udevadm trigger
lsblk
read -rp "Enter the target disk (e.g., sda): " disk_suffix
disk_name="/dev/${disk_suffix}"
read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix
usb_name="/dev/mapper/${disk_suffix}"
# Mount Ventoy
echo "Mounting Ventoy..."
mount /dev/mapper/sdb1 /mnt
cd /mnt/Images
# Decompress and write image
echo "Writing image to $disk_name..."
lz4 -dc PBX-LVM.img.lz4 | dd of=$disk_name status=progress
# Repartition disk using sgdisk
echo "Expanding Partition 3 on $disk_name..."
sgdisk -v $disk_name
sgdisk --delete=3 $disk_name
sgdisk --new=3:0:0 --typecode=3:8e00 --change-name=3:"Linux LVM" $disk_name
# Inform the OS of partition table changes
partprobe $disk_name
sleep 2 # Give the system a moment to recognize changes
# Resize physical volume
echo "Resizing physical volume..."
pvresize ${disk_name}3
# Resize swap and root logical volumes
echo "Resizing swap and root volumes..."
lvresize -L +7G PBX-vg/swap_1
lvresize -l +100%FREE --resizefs PBX-vg/root
# Mount restored system for chroot
echo "Mounting restored system..."
mount /dev/mapper/PBX--vg-root /mnt/
mount ${DISKNAME}2 /mnt/boot
mount ${DISKNAME}1 /mnt/boot/efi
for i in /dev /dev/pts /proc /sys /sys/firmware/efi/efivars /run; do mount -B $i /mnt/$i; done
# Chroot and reinstall GRUB
echo "Entering chroot to reinstall GRUB..."
chroot /mnt /bin/bash -c "
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian
update-grub
"
# Exit chroot and unmount
echo "Cleaning up..."
umount -R /mnt
echo "Restore and resize complete!"