Last active
March 27, 2023 18:29
-
-
Save lucasmaurice/e3c777709236619e9265f35a692ef9ca to your computer and use it in GitHub Desktop.
Bash script that extend the root filesystem non-interactively and without rebooting the system.
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
# Get the root filesystem | |
FS_TYPE=$(df -T / | grep -v Filesystem | awk '{print $2}') | |
FS_DEVICE=$(df -T / | grep -v Filesystem | awk '{print $1}') | |
# Get VG and LV from the root filesystem | |
VG=$(lvdisplay $FS_DEVICE | grep "VG Name" | awk '{print $3}') | |
# Get the disk and partition number | |
DISK=$(pvs | grep $VG | awk '{print $1}' | rev | cut -d/ -f1 | rev | sed 's/[0-9]*//g') | |
PART=$(pvs | grep $VG | awk '{print $1}' | rev | cut -d/ -f1 | rev | sed 's/^[^0-9]*//') | |
# Print summary | |
echo -e "\e[34m --> Summary:\e[0m" | |
echo -e "\e[34m --> Filesystem: $FS_TYPE\e[0m" | |
echo -e "\e[34m --> Device: $FS_DEVICE\e[0m" | |
echo -e "\e[34m --> Volume Group: $VG\e[0m" | |
echo -e "\e[34m --> Disk: $DISK\e[0m" | |
echo -e "\e[34m --> Partition: $PART\e[0m" | |
echo -e "\e[34m --> Make a rescan of the connected disks.\e[0m" | |
echo 1 | tee /sys/block/$DISK/device/rescan | |
echo -e "\e[34m --> Clean partition table - Use espect as there is only an interactive cli.\e[0m" | |
/usr/bin/expect<<EOF | |
spawn fdisk /dev/$DISK | |
match_max 100000 | |
expect "Command (m for help): " | |
send -- "w\r" | |
expect eof | |
EOF | |
echo -e "\e[34m --> Extend the last partition - Use espect as there is only an interactive cli.\e[0m" | |
/usr/bin/expect<<EOF | |
spawn fdisk /dev/$DISK | |
match_max 100000 | |
expect "Command (m for help): " | |
send -- "d\r" | |
expect "Partition number *: " | |
send -- "\r" | |
expect "Command (m for help): " | |
send -- "ne\r" | |
expect "Partition number *: " | |
send -- "\r" | |
expect "First sector *: " | |
send -- "\r" | |
expect "Last sector, *: " | |
send -- "\r" | |
expect "Do you want to remove the signature? *: " | |
send -- "n\r" | |
expect "Command (m for help): " | |
send -- "t\r" | |
expect "Partition number *: " | |
send -- "\r" | |
expect "Partition type (type L to list all types): " | |
send -- "lvm\r" | |
expect "Command (m for help): " | |
send -- "w\r" | |
expect eof | |
EOF | |
echo -e "\e[34m --> Extend physical volume.\e[0m" | |
pvresize /dev/$DISK$PART | |
echo -e "\e[34m --> Extend logical volume.\e[0m" | |
lvextend -l +100%FREE $FS_DEVICE | |
echo -e "\e[34m --> Extend filesystem.\e[0m" | |
if [ $FS_TYPE == "xfs" ]; then | |
xfs_growfs $FS_DEVICE | |
elif [ $FS_TYPE == "ext4" ]; then | |
resize2fs $FS_DEVICE | |
else | |
echo -e "\e[31m --> Filesystem $FS_TYPE not supported.\e[0m" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment