Last active
March 23, 2026 15:02
-
-
Save Marshall-Hallenbeck/eebcd72fef1278763df519a7387c9e4f to your computer and use it in GitHub Desktop.
dynamic online disk extension
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 | |
| set -euo pipefail | |
| MOUNT=${1:?Usage: $0 <mountpoint>} | |
| LV=$(findmnt -no SOURCE "$MOUNT") || { echo "Error: '$MOUNT' is not a mountpoint" >&2; exit 1; } | |
| echo "LV: $LV" | |
| VG=$(sudo lvs --noheadings -o vg_name "$LV" | xargs) | |
| echo "VG: $VG" | |
| PV=$(sudo pvs --noheadings -o pv_name --select "vg_name=$VG" | xargs) | |
| echo "PV: $PV" | |
| DISK=/dev/$(lsblk -dno PKNAME "$PV") | |
| echo "DISK: $DISK" | |
| PARTNUM=$(cat "/sys/class/block/${PV##*/}/partition") | |
| echo "PARTNUM: $PARTNUM" | |
| sudo sh -c "echo 1 > /sys/class/block/${DISK##*/}/device/rescan" | |
| # if logical partition, resize the extended container first | |
| PART_TYPE=$(sudo parted -s "$DISK" print | awk -v pn="$PARTNUM" '$1 == pn {print $5}') | |
| if [[ "$PART_TYPE" == "logical" ]]; then | |
| EXT_PARTNUM=$(sudo parted -s "$DISK" print | awk '$5 == "extended" {print $1}') | |
| echo "Resizing extended partition $EXT_PARTNUM first" | |
| sudo parted ---pretend-input-tty "$DISK" resizepart "$EXT_PARTNUM" 100% | |
| fi | |
| sudo parted ---pretend-input-tty "$DISK" resizepart "$PARTNUM" 100% | |
| sudo pvresize "$PV" | |
| sudo lvextend -l +100%FREE -r "$LV" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment