Bash script to expand a disk partition

From Cramsession
Revision as of 16:59, 23 February 2026 by Mflavell (talk | contribs) (Created page with "#!/bin/bash # Ensure root privileges if $EUID -ne 0 ; then echo "Error: This script must be run as root." exit 1 fi # Ensure growpart is installed if ! command -v growpart &> /dev/null; then echo "Installing required tools (cloud-guest-utils)..." apt-get update -qq && apt-get install -y cloud-guest-utils > /dev/null fi echo "---------------------------------------------------" echo " Available Partitions (Filesystems only)" echo "------------------...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
✍️ Verified Author: MflavellClick to view professional profile & credentials
  1. !/bin/bash
  1. Ensure root privileges

if $EUID -ne 0 ; then

  echo "Error: This script must be run as root."
  exit 1

fi

  1. Ensure growpart is installed

if ! command -v growpart &> /dev/null; then

   echo "Installing required tools (cloud-guest-utils)..."
   apt-get update -qq && apt-get install -y cloud-guest-utils > /dev/null

fi

echo "---------------------------------------------------" echo " Available Partitions (Filesystems only)" echo "---------------------------------------------------"

  1. Create an array of partitions using lsblk
  2. Format: NAME,SIZE,TYPE,MOUNTPOINT

mapfile -t PARTS < <(lsblk -lnpo NAME,SIZE,TYPE,MOUNTPOINT | grep 'part')

  1. Display menu

for i in "${!PARTS[@]}"; do

   echo "[$i] ${PARTS[$i]}"

done

echo "---------------------------------------------------" read -p "Enter the number of the partition to extend: " CHOICE

  1. Validate input

if [[ -z "${PARTS[$CHOICE]}" ]]; then

   echo "Invalid selection. Exiting."
   exit 1

fi

  1. Extract full path (e.g., /dev/sda1)

SELECTED_PART=$(echo "${PARTS[$CHOICE]}" | awk '{print $1}')

  1. Logic to split /dev/sda1 into /dev/sda and 1
  2. This works for /dev/sda1 and /dev/nvme0n1p1 styles

if [[ $SELECTED_PART =~ ^(.*[^0-9])([0-9]+)$ ]]; then

   DISK_PATH="${BASH_REMATCH[1]}"
   PART_NUM="${BASH_REMATCH[2]}"
   
   # Handle NVMe edge case (the 'p' separator)
   DISK_PATH="${DISK_PATH%p}"

else

   echo "Could not parse disk and partition number from $SELECTED_PART"
   exit 1

fi

echo "Targeting Disk: $DISK_PATH | Partition: $PART_NUM" read -p "Are you sure you want to proceed? (y/N): " CONFIRM "$CONFIRM" != "y" && echo "Aborted." && exit 0

---

      1. Executing the Resizing
  1. 1. Grow the partition

echo "Step 1: Growing partition table..." growpart "$DISK_PATH" "$PART_NUM"

  1. 2. Grow the filesystem

echo "Step 2: Resizing filesystem..."

  1. resize2fs handles ext2/3/4; xfs_growfs is needed for XFS

FS_TYPE=$(lsblk -no FSTYPE "$SELECTED_PART")

if "$FS_TYPE" == "xfs" ; then

   # XFS requires the mount point, not the device path
   MOUNT_POINT=$(lsblk -no MOUNTPOINT "$SELECTED_PART")
   xfs_growfs "$MOUNT_POINT"

else

   resize2fs "$SELECTED_PART"

fi

echo "--- Process Complete ---" df -h "$SELECTED_PART"