Created
March 29, 2017 01:56
-
-
Save MelvinTo/1be7db2dad938d887a2eaee74604dfd9 to your computer and use it in GitHub Desktop.
Enlarge filesystem to use the entire device storage space after dd image to device
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 | |
# This script is doing exactly the opposite of what shrink_image.sh is, | |
# which will resize the filesystem to use maximum storage space of | |
# the given device | |
# | |
# This is typically done after dd a shrinked image file to this given device | |
# | |
DEVICE_FILE=$1 | |
function perr_and_exit() | |
{ | |
echo "$1" >&2 | |
exit 1 | |
} | |
function is_device_file() | |
{ | |
if [[ $(stat -c "%t" $1) == "8" ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function usage() | |
{ | |
cat << EOM | |
Usage: $0 device_file | |
This script is designed to enlarge filesystem to use all unallocated spaces. This is typically done after dd a shrinked image file to a device. Using it improperly may cause unrecoverable damage. Use at your own risk! | |
Example: $0 /dev/sda | |
EOM | |
} | |
if [[ ! $(whoami) =~ "root" ]]; then | |
echo "This script requires root privilege!" | |
exit 1 | |
fi | |
if ! is_device_file $DEVICE_FILE; then | |
echo "ERROR: $DEVICE_FILE is not an device file!" | |
exit 2 | |
fi | |
if mount | egrep -q "^$DEVICE_FILE"; then | |
perr_and_exit "ERROR: Device file $DEVICE_FILE should be umounted first" | |
fi | |
if [[ $(ls -1 ${DEVICE_FILE}?? | wc -l) != "1" ]]; then | |
perr_and_exit "ERROR: Only support image containing one partition." | |
fi | |
DEVICE_PARTITION_1=$(ls -1 ${DEVICE_FILE}??) | |
if ! parted $DEVICE_PARTITION_1 p | tail -n 2 | head -n 1 | grep ext4 &>/dev/null; then | |
perr_and_exit "ERROR: Only ext4 filesystem is supported" | |
fi | |
# Enlarge partition first | |
parted $DEVICE_FILE rm 1 mkpart primary ext4 0% 100% || perr_and_exit "ERROR: Failed to run parted on device $DEVICE_FILE" | |
# Check disk status | |
e2fsck -f $DEVICE_PARTITION_1 || perr_and_exit "ERROR: Failed to run e2fsck on partition $DEVICE_PATITION_1" | |
# Enlarge file system | |
resize2fs $DEVICE_PARTITION_1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment