Created
January 22, 2026 15:00
-
-
Save sunapi386/634491ea3e951968f02a62305109f510 to your computer and use it in GitHub Desktop.
Disk Resize Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ============================================================================== | |
| # resize_disk.sh (v6 - Based on Your Working Commands) | |
| # | |
| # Uses the exact command sequence that you verified works manually. | |
| # | |
| # ============================================================================== | |
| set -e | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| RED='\033[0;31m' | |
| NC='\033[0m' | |
| if [ "$EUID" -ne 0 ]; then | |
| echo -e "${RED}Please run this script as root or with sudo.${NC}" | |
| exit 1 | |
| fi | |
| echo -e "${GREEN}--- Starting Disk Resize Script ---${NC}" | |
| if ! command -v growpart &> /dev/null; then | |
| echo -e "${YELLOW}'growpart' command not found. Installing 'cloud-guest-utils'...${NC}" | |
| apt-get update | |
| apt-get install -y cloud-guest-utils | |
| echo -e "${GREEN}'cloud-guest-utils' installed successfully.${NC}" | |
| fi | |
| ROOT_FS=$(df --output=source / | tail -n 1) | |
| echo "Detected root filesystem: ${ROOT_FS}" | |
| if echo "${ROOT_FS}" | grep -q "mapper"; then | |
| IS_LVM=true | |
| echo "LVM setup detected." | |
| else | |
| IS_LVM=false | |
| echo "Standard partition setup detected." | |
| fi | |
| if [ "$IS_LVM" = true ]; then | |
| VG_NAME=$(lvs --noheadings -o vg_name "${ROOT_FS}" | xargs) | |
| echo "Found Volume Group: ${VG_NAME}" | |
| PV_PATH=$(vgs --noheadings -o pv_name "${VG_NAME}" | xargs) | |
| echo "LVM is on Physical Volume: ${PV_PATH}" | |
| # Extract disk and partition number from PV_PATH (e.g., /dev/sda3) | |
| # Use basename to get just "sda3", then strip the number to get "sda" | |
| PV_DEVICE=$(basename "${PV_PATH}") | |
| PART_NUM=$(echo "${PV_DEVICE}" | grep -o '[0-9]*$') | |
| DISK_NAME=$(echo "${PV_DEVICE}" | sed 's/[0-9]*$//') | |
| DISK_PATH="/dev/${DISK_NAME}" | |
| echo "Disk: ${DISK_PATH}, Partition: ${PART_NUM}" | |
| echo "Step 1: Growing partition ${PART_NUM} on disk ${DISK_PATH}..." | |
| growpart "${DISK_PATH}" "${PART_NUM}" | |
| echo "Step 2: Resizing LVM Physical Volume on ${PV_PATH}..." | |
| pvresize "${PV_PATH}" | |
| echo "Step 3: Extending LVM Logical Volume ${ROOT_FS} to use all free space..." | |
| lvextend -l +100%FREE "${ROOT_FS}" | |
| echo "Step 4: Resizing the filesystem on ${ROOT_FS}..." | |
| resize2fs "${ROOT_FS}" | |
| else | |
| PART_PATH="${ROOT_FS}" | |
| PART_DEVICE=$(basename "${PART_PATH}") | |
| PART_NUM=$(echo "${PART_DEVICE}" | grep -o '[0-9]*$') | |
| DISK_NAME=$(echo "${PART_DEVICE}" | sed 's/[0-9]*$//') | |
| DISK_PATH="/dev/${DISK_NAME}" | |
| echo "Step 1: Growing partition ${PART_NUM} on disk ${DISK_PATH}..." | |
| growpart "${DISK_PATH}" "${PART_NUM}" | |
| echo "Step 2: Resizing the filesystem on ${PART_PATH}..." | |
| resize2fs "${PART_PATH}" | |
| fi | |
| echo -e "\n${GREEN}--- Resize Complete! ---${NC}" | |
| echo "Verifying the new size of the root filesystem:" | |
| df -h / | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment