Last active
June 15, 2025 14:00
-
-
Save bouroo/90d64e3a22cc1f41294103e03e7a9ebc to your computer and use it in GitHub Desktop.
linux extend disk partition 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
#!/usr/bin/env bash | |
# Exit immediately if a command exits with a non-zero status. | |
set -e | |
# Find the device containing the root filesystem (e.g., /dev/sda3 or /dev/mapper/vg-root) | |
ROOT_FS_DEVICE=$(findmnt -n -o SOURCE /) | |
# Check if ROOT_FS_DEVICE was found | |
if [ -z "$ROOT_FS_DEVICE" ]; then | |
echo "Error: Could not determine root filesystem device." | |
exit 1 | |
fi | |
# Determine if the root filesystem is on an LVM Logical Volume | |
IS_LVM=false | |
if [[ "$ROOT_FS_DEVICE" == /dev/mapper/* || "$ROOT_FS_DEVICE" == /dev/*/vg-* ]]; then | |
# Check if it's truly an LVM LV by querying lvs | |
if lvs "$ROOT_FS_DEVICE" &>/dev/null; then | |
IS_LVM=true | |
fi | |
fi | |
ROOT_PARTITION_TO_GROW="" # This will be the /dev/sdXN partition to grow | |
ROOT_DISK_NAME="" # The parent disk name (e.g., sda) | |
ROOT_PARTITION_NUM="" # The partition number (e.g., 3) | |
if $IS_LVM; then | |
echo "Root filesystem is on an LVM Logical Volume: ${ROOT_FS_DEVICE}" | |
# Get the Physical Volume (PV) backing the root Logical Volume (LV) | |
# Example: /dev/mapper/vg-root -> /dev/sda3 | |
ROOT_PV_PATH=$(lvs -o pv_name --noheadings "$ROOT_FS_DEVICE" 2>/dev/null | xargs) | |
if [ -z "$ROOT_PV_PATH" ]; then | |
echo "Error: Could not determine Physical Volume for ${ROOT_FS_DEVICE}." | |
exit 1 | |
fi | |
echo "Root LV's Physical Volume: ${ROOT_PV_PATH}" | |
# The partition to grow is the PV itself | |
ROOT_PARTITION_TO_GROW="$ROOT_PV_PATH" | |
else | |
echo "Root filesystem is directly on a partition: ${ROOT_FS_DEVICE}" | |
# The partition to grow is the root filesystem device itself | |
ROOT_PARTITION_TO_GROW="$ROOT_FS_DEVICE" | |
fi | |
# Extract the parent disk name (e.g., sda) and partition number (e.g., 3) | |
# from the partition device using lsblk | |
read -r ROOT_DISK_NAME ROOT_PARTITION_NUM <<< $(lsblk -no PKNAME,PARTNO "${ROOT_PARTITION_TO_GROW}") | |
# Check if disk name and partition number were extracted | |
if [ -z "$ROOT_DISK_NAME" ] || [ -z "$ROOT_PARTITION_NUM" ]; then | |
echo "Error: Could not extract disk or partition number from ${ROOT_PARTITION_TO_GROW}." | |
echo "This script expects the root filesystem (or its underlying PV) to be a standard partition (e.g., /dev/sda3)." | |
exit 1 | |
fi | |
# Construct the full disk device path (e.g., /dev/sda) | |
ROOT_DISK_DEV="/dev/${ROOT_DISK_NAME}" | |
echo "Extending root filesystem on ${ROOT_FS_DEVICE}..." | |
echo "Target partition to grow: ${ROOT_PARTITION_TO_GROW}" | |
echo "Parent disk: ${ROOT_DISK_DEV}" | |
echo "Partition number: ${ROOT_PARTITION_NUM}" | |
# Rescan the disk to pick up potential size changes | |
# Note: This requires root privileges. | |
echo "Rescanning disk ${ROOT_DISK_DEV}..." | |
echo 1 | sudo tee "/sys/block/${ROOT_DISK_NAME}/device/rescan" > /dev/null | |
# Grow the partition that contains root (or is the PV for root LV) | |
# Note: This requires root privileges. | |
echo "Growing partition ${ROOT_PARTITION_TO_GROW}..." | |
sudo growpart "${ROOT_DISK_DEV}" "${ROOT_PARTITION_NUM}" | |
if $IS_LVM; then | |
# Extend the physical volume on the root partition | |
# Note: This requires root privileges. | |
echo "Resizing Physical Volume ${ROOT_PV_PATH}..." | |
sudo pvresize "${ROOT_PV_PATH}" | |
# Extend the logical volume corresponding to the root device | |
# Use -r to resize the filesystem automatically after extending the LV | |
# Use -l +100%FREE to use all available free space in the volume group | |
# Note: This requires root privileges. | |
echo "Extending Logical Volume ${ROOT_FS_DEVICE} and resizing filesystem..." | |
sudo lvextend -r -l +100%FREE "${ROOT_FS_DEVICE}" | |
else | |
# If not LVM, directly resize the filesystem on the partition | |
# Note: This requires root privileges. | |
echo "Resizing filesystem on ${ROOT_FS_DEVICE}..." | |
FSTYPE=$(findmnt -n -o FSTYPE "$ROOT_FS_DEVICE") | |
case "$FSTYPE" in | |
ext2|ext3|ext4) | |
sudo resize2fs "$ROOT_FS_DEVICE" | |
;; | |
xfs) | |
sudo xfs_growfs "$ROOT_FS_DEVICE" | |
;; | |
*) | |
echo "Warning: Unknown filesystem type '$FSTYPE' on ${ROOT_FS_DEVICE}. Manual resize may be needed." | |
;; | |
esac | |
fi | |
echo "Root filesystem extension complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment