#!/bin/bash # Script to resize LVM partition after expanding disk in Proxmox # Must be run as root # Exit on any error set -e # Function to check if running as root check_root() { if [ "$(id -u)" != "0" ]; then echo "Error: This script must be run as root" >&2 exit 1 fi } # Function to check if required tools are installed check_requirements() { local required_tools=("parted" "pvresize" "lvresize" "resize2fs") for tool in "${required_tools[@]}"; do if ! command -v "$tool" >/dev/null 2>&1; then echo "Error: Required tool '$tool' is not installed" >&2 exit 1 fi done } # Function to verify LVM setup verify_lvm() { if ! vgs ubuntu-vg >/dev/null 2>&1; then echo "Error: Volume group 'ubuntu-vg' not found" >&2 exit 1 fi if ! lvs ubuntu-vg/ubuntu-lv >/dev/null 2>&1; then echo "Error: Logical volume 'ubuntu-lv' not found" >&2 exit 1 fi } # Function to backup partition table backup_partition_table() { local backup_file="/root/partition_backup_$(date +%Y%m%d_%H%M%S).bak" echo "Creating partition table backup at $backup_file" sfdisk -d /dev/sda > "$backup_file" } # Main resize function perform_resize() { echo "Starting disk resize process..." # Resize partition using parted echo "Resizing partition..." parted -s /dev/sda resizepart 3 100% # Give system time to register new partition size sleep 2 echo "Resizing physical volume..." pvresize /dev/sda3 echo "Resizing logical volume..." lvresize -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv echo "Resizing filesystem..." resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv } # Function to verify successful resize verify_resize() { echo "Verifying resize operation..." # Check if filesystem is clean if ! e2fsck -n /dev/mapper/ubuntu--vg-ubuntu--lv >/dev/null 2>&1; then echo "Warning: Filesystem check recommended after resize" >&2 fi # Display new size information echo -e "\nNew disk space information:" df -h / lvs ubuntu-vg/ubuntu-lv } # Main execution main() { echo "Starting LVM disk resize script..." # Run checks check_root check_requirements verify_lvm # Create backup backup_partition_table # Perform resize perform_resize # Verify results verify_resize echo -e "\nDisk resize completed successfully!" } # Execute main function with error handling if main; then exit 0 else echo "Error: Resize process failed" >&2 exit 1 fi