more refactoring
This commit is contained in:
+89
-61
@@ -6,13 +6,23 @@ 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")
|
|
||||||
IMG_FILE="PBX.img3.lz4"
|
|
||||||
NO_WRITE=false
|
NO_WRITE=false
|
||||||
NO_RETABLE=false
|
NO_RETABLE=false
|
||||||
NO_USB=false
|
NO_USB=false
|
||||||
NO_CLEANUP=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
|
for arg in "$@"; do
|
||||||
case $arg in
|
case $arg in
|
||||||
--nowrite)
|
--nowrite)
|
||||||
@@ -50,14 +60,7 @@ main() {
|
|||||||
|
|
||||||
udevadm trigger
|
udevadm trigger
|
||||||
|
|
||||||
lsblk
|
dialogue
|
||||||
echo
|
|
||||||
|
|
||||||
read -rp "Enter the target disk (e.g., sda): " disk_suffix
|
|
||||||
disk_name="/dev/${disk_suffix}"
|
|
||||||
|
|
||||||
echo "Chose disk $disk_name"
|
|
||||||
echo
|
|
||||||
|
|
||||||
trap 'echo "Status Code: $?"; cleanup;' EXIT
|
trap 'echo "Status Code: $?"; cleanup;' EXIT
|
||||||
|
|
||||||
@@ -65,32 +68,39 @@ main() {
|
|||||||
echo -e "${YELLOW}--noretable is specified. Tables will not be erased and no partitions will be created${RESET}"
|
echo -e "${YELLOW}--noretable is specified. Tables will not be erased and no partitions will be created${RESET}"
|
||||||
else
|
else
|
||||||
rewrite_gpt_table
|
rewrite_gpt_table
|
||||||
make_filesystem
|
|
||||||
lvm_init
|
lvm_init
|
||||||
|
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
|
|
||||||
|
|
||||||
echo "Generating fstab..."
|
gen_fstab
|
||||||
boot_uuid=$()
|
|
||||||
|
|
||||||
cp ./fstab_template ${mount_path}/etc/fstab
|
install_grub
|
||||||
|
|
||||||
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
|
|
||||||
"
|
|
||||||
|
|
||||||
|
cleanup
|
||||||
echo -e "${GREEN}Restore and resize complete!${RESET}"
|
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() {
|
rewrite_gpt_table() {
|
||||||
sgdisk -Z $disk_name
|
sgdisk -Z $disk_name
|
||||||
|
|
||||||
@@ -103,44 +113,45 @@ rewrite_gpt_table() {
|
|||||||
partprobe || true
|
partprobe || true
|
||||||
}
|
}
|
||||||
|
|
||||||
write_img() {
|
|
||||||
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}"
|
|
||||||
else
|
|
||||||
img_file_dir="null"
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
lvm_init() {
|
lvm_init() {
|
||||||
pvcreate $(get_partition $disk_name "3")
|
pvcreate $(get_partition $disk_name "3")
|
||||||
|
|
||||||
vgcreate PBX-vg $(get_partition $disk_name "3")
|
vgcreate $VGROUP $(get_partition $disk_name "3")
|
||||||
|
|
||||||
vgchange -ay PBX-vg
|
vgchange -ay $VGROUP
|
||||||
|
|
||||||
lvcreate -L 4G PBX-vg -n swap_1
|
lvcreate -L 4G $VGROUP -n swap_1
|
||||||
lvcreate -l 100%FREE PBX-vg -n root
|
lvcreate -l 100%FREE $VGROUP -n root
|
||||||
}
|
}
|
||||||
|
|
||||||
make_filesystem() {
|
make_filesystem() {
|
||||||
mkfs.vfat -F 32 $(get_partition $disk_name "1")
|
mkfs.vfat -F 32 $(get_partition $disk_name "1")
|
||||||
mkfs.ext4 $(get_partition $disk_name "2")
|
mkfs.ext4 $(get_partition $disk_name "2")
|
||||||
|
mkswap "${VG_PATH}${SWAP_LV}"
|
||||||
}
|
}
|
||||||
|
|
||||||
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}--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
|
exit 100
|
||||||
fi
|
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() {
|
cleanup() {
|
||||||
@@ -158,6 +169,8 @@ cleanup() {
|
|||||||
umount -lf /mnt/debian/boot/efi 2>/dev/null || true
|
umount -lf /mnt/debian/boot/efi 2>/dev/null || true
|
||||||
umount -lf /mnt/debian/boot 2>/dev/null || true
|
umount -lf /mnt/debian/boot 2>/dev/null || true
|
||||||
umount -lf /mnt/debian 2>/dev/null || true
|
umount -lf /mnt/debian 2>/dev/null || true
|
||||||
|
|
||||||
|
vgchange -an $VGROUP
|
||||||
}
|
}
|
||||||
|
|
||||||
get_partition() {
|
get_partition() {
|
||||||
@@ -185,21 +198,36 @@ get_partuuid() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
mount /dev/mapper/PBX--vg-root $MOUNT_PATH
|
||||||
mount $(get_partition $disk_name "2") ${mount_path}/boot
|
mount $(get_partition $disk_name "2") ${MOUNT_PATH}/boot
|
||||||
|
|
||||||
mkdir -p $mount_path/boot/efi
|
mkdir -p $MOUNT_PATH/boot/efi
|
||||||
mount $(get_partition $disk_name "1") ${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
|
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
|
main
|
||||||
cleanup
|
|
||||||
|
|||||||
Reference in New Issue
Block a user