Compare commits

..

12 Commits

Author SHA1 Message Date
poslop 7d3d6c2216 fstab edits 2026-05-01 11:00:37 -05:00
poslop 89e7f17af6 apt install linux fuckery 2026-04-30 14:09:06 -05:00
poslop 45de18b162 mounts 2026-04-30 11:19:03 -05:00
poslop 0664dde3cb rewrite chroot 2026-04-30 10:32:47 -05:00
poslop 91f41fbfba lvm changes 2026-04-30 10:04:00 -05:00
poslop 27b5bf9aa5 fixed awk and sed 2026-04-29 16:16:36 -05:00
poslop 794ca09dfb AHHHHHHHHHHHHHHh I HATE DEBIUAB U AHTE DEBIAN 2026-04-29 15:37:40 -05:00
poslop f06573af1c echo debugs 2026-04-28 13:21:48 -05:00
poslop 8577cd1176 more refactoring 2026-04-28 12:00:42 -05:00
poslop 31c7e91b91 lvm_init 2026-04-28 08:46:33 -05:00
poslop 1e608196a5 partclone in extract script 2026-04-27 16:01:27 -05:00
poslop 4e3f5a158e Remove USB support 2026-04-27 15:26:47 -05:00
4 changed files with 164 additions and 246 deletions
+148 -88
View File
@@ -6,10 +6,24 @@ GREEN='\e[32m'
YELLOW='\e[33m' YELLOW='\e[33m'
RESET='\e[0m' RESET='\e[0m'
MOUNT_POINTS=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run")
NO_WRITE=false NO_WRITE=false
NO_RETABLE=false NO_RETABLE=false
NO_USB=false NO_USB=false
NO_CLEANUP=false
IMG_FILE="PBX-root.img.lz4"
MOUNT_PATH="/mnt/debian"
MOUNT_POINTS=("/dev" "/dev/pts" "/proc" "/sys" "/run")
BOOT_PART="NULL"
EFI_PART="NULL"
ROOT_PART="NULL"
VGROUP="SangomaPBX-vg"
ROOT_LV="root"
SWAP_LV="swap_1"
VG_PATH="/dev/mapper/SangomaPBX--vg-"
for arg in "$@"; do for arg in "$@"; do
case $arg in case $arg in
@@ -21,8 +35,8 @@ for arg in "$@"; do
NO_RETABLE=true NO_RETABLE=true
shift shift
;; ;;
--nousb) --nocleanup)
NO_USB=true NO_CLEANUP=true
shift shift
;; ;;
-h) -h)
@@ -44,87 +58,50 @@ main() {
udevadm trigger udevadm trigger
lsblk dialogue
echo
read -rp "Enter the target disk (e.g., sda): " disk_suffix assign_part_var
disk_name="/dev/${disk_suffix}"
echo "Chose disk $disk_name"
echo
trap 'echo "Status Code: $?"; cleanup;' EXIT trap 'echo "Status Code: $?"; cleanup;' EXIT
if [[ $NO_RETABLE == true ]]; then if [[ $NO_RETABLE == true ]]; then
echo -e "${YELLOW}--noretable is specified. Tables will not be erased and no partitions will be created${RESET}" echo -e "${YELLOW}Option --noretable is specified. Tables will not be erased and no partitions will be created${RESET}"
else else
rewrite_gpt_table rewrite_gpt_table
assign_part_var
lvm_init
make_filesystem make_filesystem
fi fi
if [[ $NO_WRITE == true ]]; then write_img
echo -e "${YELLOW}--nowrite is specified. No need for Img no disks will be written to${RESET}"
fi
# Resize physical volume mount_chroot
echo "Resizing physical volume..."
pvresize $(get_partition $disk_name "3") || true
# Resize swap and root logical volumes gen_fstab
echo "Resizing swap and root volumes..."
#lvresize -L +7G PBX-vg/swap_1
lvresize -l +100%FREE --resizefs PBX-vg/root
echo "Generating fstab..." install_grub
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}" echo -e "${GREEN}Restore and resize complete!${RESET}"
exit 0
} }
write_image() { root_check() {
if [[ $NO_USB == true ]]; then if [[ $EUID -ne 0 ]]; then
echo "Writing image to $(get_partition $disk_name "3")..." echo -e "${RED}You must be root to do this." 1>&2
lz4 -dc ./Image/PBX.img3.lz4 | dd of=$(get_partition $disk_name "3") status=progress exit 100
partprobe || true
else
read -rp "Enter the name of the usb partition in /dev/mapper (e.g., sda1): " disk_suffix
usb_name="/dev/mapper/${disk_suffix}"
user_response="n"
read -rp "$(echo -e "${RED}You are about to WRITE and ERASE disk ${disk_name} | Are you sure? y/N: ${RESET}")" user_response
if [[ $user_response != y ]]; then
echo "Exiting script"
exit
fi fi
}
echo "Mounting Ventoy..." dialogue() {
mkdir -p /mnt/ventoy lsblk
mount $usb_name /mnt/ventoy
cd /mnt/ventoy/Images
echo "Writing image to $(get_partition $disk_name "3")..." echo
lz4 -dc PBX.img.lz4 | dd of=$(get_partition $disk_name "3") status=progress read -rp "Enter the target disk (e.g., sda): " disk_suffix
partprobe || true disk_name="/dev/${disk_suffix}"
fi
echo "Chose disk $disk_name"
echo
} }
rewrite_gpt_table() { rewrite_gpt_table() {
@@ -137,30 +114,62 @@ rewrite_gpt_table() {
sgdisk -n 3:0:0 -t 3:8e00 -c 3:"Linux LVM" $disk_name sgdisk -n 3:0:0 -t 3:8e00 -c 3:"Linux LVM" $disk_name
partprobe || true partprobe || true
lsblk $disk_name
echo -e "${GREEN}GPT table created. ${RESET}"
}
assign_part_var() {
EFI_PART=$(get_partition $disk_name "1")
BOOT_PART=$(get_partition $disk_name "2")
ROOT_PART=$(get_partition $disk_name "3")
}
lvm_init() {
pvcreate $ROOT_PART -ff
vgcreate $VGROUP $ROOT_PART -f
vgchange -ay $VGROUP
lvcreate -L 4G $VGROUP -n swap_1
lvcreate -l 100%FREE $VGROUP -n root
echo -e "${GREEN}LVM initialized ${RESET}"
} }
make_filesystem() { make_filesystem() {
mkfs.vfat -F 32 $(get_partition $disk_name "1") mkfs.vfat -F 32 $EFI_PART
mkfs.ext4 $(get_partition $disk_name "2") mkfs.ext4 $BOOT_PART
mkswap "${VG_PATH}${SWAP_LV}"
echo -e "${GREEN}Make filesystems finished.${RESET}"
} }
root_check() { write_img() {
if [[ $EUID -ne 0 ]]; then if [[ $NO_WRITE == true ]]; then
echo -e "${RED}You must be root to do this." 1>&2 echo -e "${YELLOW}Option --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 ]]; then
echo -e "${RED}NO IMG FILE FOUND${RESET}"
exit 100 exit 100
fi fi
}
cleanup() { echo "Writing image to ${VG_PATH}${ROOT_LV}..."
echo "Cleaning Mounts"
umount -lf /mnt/ventoy 2>/dev/null || true lz4 -dc ${img_file_dir} | partclone.ext4 -r -o "${VG_PATH}${ROOT_LV}"
for i in "${MOUNT_POINTS[@]}"; do umount -lf "/mnt/debian$i" 2>/dev/null || true; done partprobe || true
umount -lf /mnt/debian/boot/efi 2>/dev/null || true echo -e "${GREEN}Finished writing image to disk${RESET}"
umount -lf /mnt/debian/boot 2>/dev/null || true
umount -lf /mnt/debian 2>/dev/null || true
} }
get_partition() { get_partition() {
@@ -177,32 +186,83 @@ get_partition() {
} }
get_partuuid() { get_partuuid() {
local disk_name="$1" local target_part="$1"
local part_num="$2"
local part_uuid="$( local part_uuid="$(
lsblk -ln -o PATH,PARTUUID $disk_name | awk '$1==$(get_partition $disk_name $part_num) {print $2}' lsblk -ln -o PATH,PARTUUID $disk_name | awk -v target_part="$target_part" '$1==target_part {print $2}'
)" )"
printf "$part_uuid" printf "$part_uuid"
} }
mount_chroot() { mount_chroot() {
vgchange -ay PBX-vg vgchange -ay $VGROUP
mount_path="/mnt/debian"
echo "Mounting restored system..." echo "Mounting restored system..."
mkdir -p $mount_path/boot mkdir -p "${MOUNT_PATH}/boot"
mount /dev/mapper/PBX--vg-root $mount_path mkdir -p "${MOUNT_PATH}/boot/efi"
mount $(get_partition $disk_name "2") ${mount_path}/boot
mkdir -p $mount_path/boot/efi mount "${VG_PATH}${ROOT_LV}" $MOUNT_PATH
mount $(get_partition $disk_name "1") ${mount_path}/boot/efi mount $BOOT_PART "${MOUNT_PATH}/boot"
mount $EFI_PART "${MOUNT_PATH}/boot/efi"
for i in "${MOUNT_POINTS[@]}"; do mount -B $i ${mount_path}${i}; done for i in "${MOUNT_POINTS[@]}"; do mount --bind -m "$i" "${MOUNT_PATH}${i}"; done
echo -e "${GREEN}Mounts prepared for chroot.${RESET}"
}
gen_fstab() {
echo "Generating fstab..."
boot_uuid=$(get_partuuid $BOOT_PART)
efi_uuid=$(get_partuuid $EFI_PART)
cp ./fstab_template ${MOUNT_PATH}/etc/fstab
sed --in-place "s/BOOT_UUID/${boot_uuid}/g" ${MOUNT_PATH}/etc/fstab
sed --in-place "s/EFI_UUID/${efi_uuid}/g" ${MOUNT_PATH}/etc/fstab
echo -e "${GREEN}fstab write complete!${RESET}"
}
install_grub() {
echo "Entering chroot to reinstall GRUB..."
CHROOT_LINUX_INSTALL="apt install --reinstall -y linux-image-6.1.0-44-amd64"
CHROOT_INITRAMFS="update-initramfs -u -k all"
CHROOT_GRUB_INSTALL="grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian"
CHROOT_GRUB_UPDATE="update-grub"
chroot_cmd "$CHROOT_LINUX_INSTALL"
chroot_cmd "$CHROOT_INITRAMFS"
chroot_cmd "$CHROOT_GRUB_INSTALL"
chroot_cmd "$CHROOT_GRUB_UPDATE"
echo -e "${GREEN}Grub installed!${RESET}"
}
chroot_cmd() {
CHROOT_PATH="export PATH=/usr/sbin:/usr/bin:/sbin:/bin"
chroot $MOUNT_PATH /bin/bash -c "${CHROOT_PATH} && ${1}"
}
cleanup() {
if [[ $NO_CLEANUP = true ]]; then
echo -e "${YELLOW}No cleanup specified. Make sure to manually clean mounts.${RESET}" 1>&2
exit 100
fi
echo "Cleaning Mounts"
for i in "${MOUNT_POINTS[@]}"; do umount -lf "${MOUNT_PATH}${i}" 2>/dev/null || true; done
umount -lf "${MOUNT_PATH}/boot/efi" 2>/dev/null || true
umount -lf "${MOUNT_PATH}/boot" 2>/dev/null || true
umount -lf "${MOUNT_PATH}" 2>/dev/null || true
vgchange -an $VGROUP
echo -e "${GREEN}Clean.${RESET}"
} }
main main
cleanup
-137
View File
@@ -1,137 +0,0 @@
#!/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
mount_points=("/dev" "/dev/pts" "/proc" "/sys" "/sys/firmware/efi/efivars" "/run")
NO_WRITE=false
# arguments
for arg in "$@"; do
case $arg in
--nowrite)
NO_WRITE=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
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
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_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
usb_name="/dev/mapper/${disk_suffix}"
user_response="n"
read -rp "$(echo -e "${RED}You are about to WRITE and ERASE disk ${disk_name} | Are you sure? y/N: ${RESET}")" user_response
if [[ $user_response != y ]]; then
echo "Exiting script"
exit
fi
echo "Mounting Ventoy..."
mkdir -p /mnt/ventoy
mount $usb_name /mnt/ventoy
cd /mnt/ventoy/Images
echo "Writing image to $disk_name..."
lz4 -dc PBX-LVM.img.lz4 | dd of=$disk_name status=progress
fi
# Repartition disk using sgdisk
echo "Repairing partition tables"
sgdisk -e $disk_name
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 ${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
# Mount restored system for chroot
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
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 "
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian
update-grub
"
echo -e "${GREEN}Restore and resize complete!${RESET}"
cleanup
+8 -13
View File
@@ -1,33 +1,28 @@
#!/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 img_file="PBX-Test.img"
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo -e "${RED}You must be root to do this." 1>&2 echo -e "${RED}You must be root to do this." 1>&2
exit 100 exit 100
fi fi
echo "Converting to IMG" losetup -fP $img_file
qemu-img convert -f qcow2 -O raw PBX.qcow2 PBX.img loop_device=$(losetup -l | grep $img_file | cut -d ' ' -f1)
losetup -fP PBX.img
loop_device=$(losetup -l | grep "PBX.img" | cut -d ' ' -f1)
echo $loop_device echo $loop_device
vgchange -a y PBX-vg vgchange -a y PBX-vg
lvreduce -L 14G -r PBX-vg/root
lvreduce -L 976M PBX-vg/swap_1
sgdisk --delete=3 $loop_device lvreduce -L 12G -r PBX-vg/root
sgdisk --new=3:+0:+15G --typecode=3:8e00 --change-name=3:"Linux LVM" $loop_device
partclone.ext4 -c -s /dev/mapper/PBX--vg-root | lz4 -z >./PBX-root.img.lz4
vgchange -a n PBX-vg vgchange -a n PBX-vg
dd if=/dev/loop0p3 bs=64M conv=sync status=progress | lz4 -z > ./PBX3.img.lz4
losetup -D losetup -D
+3 -3
View File
@@ -8,12 +8,12 @@
# Please run 'systemctl daemon-reload' after making changes here. # Please run 'systemctl daemon-reload' after making changes here.
# <file system> <mount point> <type> <options> <dump> <pass> # <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/PBX--vg-root / ext4 errors=remount-ro 0 1 /dev/mapper/SangomaPBX--vg-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/vda2 during installation # /boot was on /dev/vda2 during installation
UUID=BOOT_UUID /boot ext2 defaults 0 2 UUID=BOOT_UUID /boot ext4 defaults 0 2
# /boot/efi was on /dev/vda1 during installation # /boot/efi was on /dev/vda1 during installation
UUID=EFI_UUID /boot/efi vfat umask=0077 0 1 UUID=EFI_UUID /boot/efi vfat umask=0077 0 1
/dev/mapper/PBX--vg-swap_1 none swap sw 0 0 /dev/mapper/SangomaPBX--vg-swap_1 none swap sw 0 0