Compare commits

...

10 Commits

Author SHA1 Message Date
poslop 4af9cb4a30 Convert to functions 2026-04-27 15:00:42 -05:00
poslop 4133d22caf part3 and fstab temp 2026-01-26 10:37:48 -06:00
poslop 90e0344210 tset bump 2026-01-16 23:34:31 -06:00
poslop 524d4f0500 Update README.md 2026-01-16 23:30:41 -06:00
poslop 9e980fd07e Update README.md 2026-01-16 23:27:47 -06:00
poslop 5ac621db8a convert to p3 only writing 2026-01-16 23:12:25 -06:00
poslop 2bb5bd8223 extract partitions 2026-01-16 18:29:37 -06:00
poslop a92e5f05de fixed return 2026-01-16 16:19:40 -06:00
poslop 265839faae fixed parameters of get_partition 2026-01-16 16:00:59 -06:00
poslop ce05c833e7 get_partition 2026-01-16 15:55:04 -06:00
7 changed files with 216 additions and 93 deletions
+1
View File
@@ -1,3 +1,4 @@
PBX.img PBX.img
PBX.qcow2 PBX.qcow2
PBX-Test.qcow2 PBX-Test.qcow2
*.snapshot*
+129 -58
View File
@@ -1,29 +1,30 @@
#!/bin/bash #!/bin/bash
set -e set -e
# THE RAINBOW HAHAHAH
RED='\e[31m' RED='\e[31m'
GREEN='\e[32m' GREEN='\e[32m'
YELLOW='\e[33m' YELLOW='\e[33m'
RESET='\e[0m' RESET='\e[0m'
# root check MOUNT_POINTS=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run")
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}You must be root to do this." 1>&2
exit 100
fi
mount_points=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run")
NO_WRITE=false NO_WRITE=false
NO_RETABLE=false
NO_USB=false
# arguments
for arg in "$@"; do for arg in "$@"; do
case $arg in case $arg in
--nowrite) --nowrite)
NO_WRITE=true NO_WRITE=true
shift shift
;; ;;
--noretable)
NO_RETABLE=true
shift
;;
--nousb)
NO_USB=true
shift
;;
-h) -h)
printf "%-20s %s\n" "Flags:" "Description:" printf "%-20s %s\n" "Flags:" "Description:"
echo echo
@@ -38,36 +39,67 @@ for arg in "$@"; do
esac esac
done done
main() {
root_check
cleanup() {
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
}
trap 'echo "Status Code: $?"; cleanup;' EXIT
# Set your target disk here
udevadm trigger udevadm trigger
lsblk lsblk
echo echo
read -rp "Enter the target disk (e.g., sda): " disk_suffix read -rp "Enter the target disk (e.g., sda): " disk_suffix
disk_name="/dev/${disk_suffix}" disk_name="/dev/${disk_suffix}"
echo "Chose disk $disk_name" echo "Chose disk $disk_name"
echo echo
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
make_filesystem
fi
if [[ $NO_WRITE == true ]]; then if [[ $NO_WRITE == true ]]; then
echo -e "${YELLOW}--nowrite is specified no need for Img no disks will be written to${RESET}" echo -e "${YELLOW}--nowrite is specified. No need for Img no disks will be written to${RESET}"
fi
# Resize physical volume
echo "Resizing physical volume..."
pvresize $(get_partition $disk_name "3") || true
# 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
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
# Chroot and reinstall GRUB
echo "Entering chroot to reinstall GRUB..."
chroot $mount_path /bin/bash -c "
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian
update-grub
"
echo -e "${GREEN}Restore and resize complete!${RESET}"
}
write_image() {
if [[ $NO_USB == true ]]; then
echo "Writing image to $(get_partition $disk_name "3")..."
lz4 -dc ./Image/PBX.img3.lz4 | dd of=$(get_partition $disk_name "3") status=progress
partprobe || true
else else
read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix
@@ -87,51 +119,90 @@ else
mount $usb_name /mnt/ventoy mount $usb_name /mnt/ventoy
cd /mnt/ventoy/Images cd /mnt/ventoy/Images
echo "Writing image to $disk_name..." echo "Writing image to $(get_partition $disk_name "3")..."
lz4 -dc PBX.img.lz4 | dd of=$disk_name status=progress lz4 -dc PBX.img.lz4 | dd of=$(get_partition $disk_name "3") status=progress
partprobe || true
fi fi
}
# Repartition disk using sgdisk rewrite_gpt_table() {
echo "Repairing partition tables" sgdisk -Z $disk_name
sgdisk -e $disk_name
sgdisk -v $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 partprobe || true
}
echo "Expanding Partition 3 on $disk_name..." make_filesystem() {
sgdisk --delete=3 $disk_name mkfs.vfat -F 32 $(get_partition $disk_name "1")
sgdisk --new=3:0:0 --typecode=3:8e00 --change-name=3:"Linux LVM" $disk_name mkfs.ext4 $(get_partition $disk_name "2")
}
partprobe || true root_check() {
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}You must be root to do this." 1>&2
exit 100
fi
}
# Resize physical volume cleanup() {
echo "Resizing physical volume..." echo "Cleaning Mounts"
pvresize ${disk_name}3 || true
# Resize swap and root logical volumes umount -lf /mnt/ventoy 2>/dev/null || true
echo "Resizing swap and root volumes..."
lvresize -L +7G PBX-vg/swap_1
lvresize -l +100%FREE --resizefs PBX-vg/root
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
}
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 PBX-vg
mount_path="/mnt/debian"
# Mount restored system for chroot
echo "Mounting restored system..." echo "Mounting restored system..."
mkdir -p /mnt/debian
mount /dev/mapper/PBX--vg-root /mnt/debian/
mount ${disk_name}2 /mnt/debian/boot
mount ${disk_name}1 /mnt/debian/boot/efi
mkdir -p $mount_path/boot
mount /dev/mapper/PBX--vg-root $mount_path
mount $(get_partition $disk_name "2") ${mount_path}/boot
for i in "${mount_points[@]}"; do mount -B $i /mnt/debian$i; done mkdir -p $mount_path/boot/efi
mount $(get_partition $disk_name "1") ${mount_path}/boot/efi
# Chroot and reinstall GRUB for i in "${MOUNT_POINTS[@]}"; do mount -B $i ${mount_path}${i}; done
echo "Entering chroot to reinstall GRUB..." }
chroot /mnt/debian /bin/bash -c "
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian
update-grub
"
echo -e "${GREEN}Restore and resize complete!${RESET}"
main
cleanup cleanup
BIN
View File
Binary file not shown.
-6
View File
@@ -1,6 +0,0 @@
#!/bin/bash
IMAGE="PBX.img"
fdisk -l ./PBX.img | grep 'PBX\.img[0-9]'
+33
View File
@@ -0,0 +1,33 @@
#!/bin/bash
set -e
# THE RAINBOW HAHAHAH
RED='\e[31m'
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
exit 100
fi
echo "Converting to IMG"
qemu-img convert -f qcow2 -O raw PBX.qcow2 PBX.img
losetup -fP PBX.img
loop_device=$(losetup -l | grep "PBX.img" | cut -d ' ' -f1)
echo $loop_device
vgchange -a y PBX-vg
lvreduce -L 14G -r PBX-vg/root
lvreduce -L 976M PBX-vg/swap_1
sgdisk --delete=3 $loop_device
sgdisk --new=3:+0:+15G --typecode=3:8e00 --change-name=3:"Linux LVM" $loop_device
vgchange -a n PBX-vg
dd if=/dev/loop0p3 bs=64M conv=sync status=progress | lz4 -z > ./PBX3.img.lz4
losetup -D
+2
View File
@@ -0,0 +1,2 @@
## Used for writing a Debian FreePBX image to a new drive
WIP
+19
View File
@@ -0,0 +1,19 @@
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/PBX--vg-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/vda2 during installation
UUID=BOOT_UUID /boot ext2 defaults 0 2
# /boot/efi was on /dev/vda1 during installation
UUID=EFI_UUID /boot/efi vfat umask=0077 0 1
/dev/mapper/PBX--vg-swap_1 none swap sw 0 0