Last active
January 18, 2020 12:39
-
-
Save marrobHD/823329ef053e48243f3283ecd45d6957 to your computer and use it in GitHub Desktop.
Resize partition
This file contains 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 -e | |
if [[ $# -eq 0 ]] ; then | |
echo 'please tell me the device to resize as the first parameter, like /dev/sda' | |
exit 1 | |
fi | |
if [[ $# -eq 1 ]] ; then | |
echo 'please tell me the partition number to resize as the second parameter, like 1 in case you mean /dev/sda1 or 4, if you mean /dev/sda2' | |
exit 1 | |
fi | |
DEVICE=$1 | |
PARTNR=$2 | |
APPLY=$3 | |
echo PARTNR:${PARTNR} | |
echo DEVICE:${DEVICE} | |
echo APPLY:${APPLY} | |
fdisk -l $DEVICE$PARTNR >> /dev/null 2>&1 || (echo "could not find device $DEVICE$PARTNR - please check the name" && exit 1) | |
CURRENTSIZEB=`fdisk -l $DEVICE$PARTNR | grep "Disk $DEVICE$PARTNR" | cut -d' ' -f5` | |
CURRENTSIZE=`expr $CURRENTSIZEB / 1024 / 1024` | |
# So get the disk-informations of our device in question printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda we use printf %s\\n 'unit MB print list' to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yet | |
# then use the 3rd column of the output (disk size) cut -d' ' -f3 (divided by space) | |
# and finally cut off the unit 'MB' with blanc using tr -d MB | |
MAXSIZEMB=`printf %s\\n 'unit MB print list' | parted | grep "Disk ${DEVICE}" | cut -d' ' -f3 | tr -d MB` | |
echo "[ok] would/will resize to from ${CURRENTSIZE}MB to ${MAXSIZEMB}MB " | |
if [[ "$APPLY" == "apply" ]] ; then | |
echo "[ok] applying resize operation.." | |
sudo parted -a none ${DEVICE} ---pretend-input-tty resizepart ${PARTNR} ${MAXSIZEMB} | |
sudo resize2fs ${DEVICE}${PARTNR} | |
echo "[done]" | |
else | |
echo "[WARNING]!: Sandbox mode, I did not size!. Use 'apply' as the 3d parameter to apply the changes" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment