Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save swamibluedata/0cd9f996c0df9b7c28d5b120aa4f79b6 to your computer and use it in GitHub Desktop.
Save swamibluedata/0cd9f996c0df9b7c28d5b120aa4f79b6 to your computer and use it in GitHub Desktop.
# Launch the container from bd_mgmt and invoke the following through a bdconfig command. Two parameters NEW_SIZE and INSTANCE_NAME
# docker run -d --name test bluedata/centos6
INSTANCE_NAME="test"
NEW_SIZE_GB="30"
NEW_SIZE=$(( $NEW_SIZE_GB*1024*1024*1024 ))
CONT_ID=$(docker inspect -f {{.Id}} $INSTANCE_NAME)
DEVMAPPER_POOL=$(docker info | grep pool | awk '{print $3}')
# Get the actual device information of the container
DEV=$(dmsetup info | grep $CONT_ID | awk '{print $2}')
# The actual metadata files for this container. We will use this to fetch the current information
# as well as updating after rezize
METADATA_FILE="/var/lib/docker/devicemapper/metadata/$CONT_ID"
METADATA_INIT_FILE="/var/lib/docker/devicemapper/metadata/$CONT_ID-init"
# Fetch the current size and the actual device id from the metadata file
OLD_SIZE=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['size']")
DEVICE_ID=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);print obj['device_id']")
# We got all the information, we can stop the container now. This also deletes the actual
# dm device.
docker stop $INSTANCE_NAME
# Create the same device with the old size
dmsetup create "$DEV" --table "0 $(($OLD_SIZE / 512)) thin /dev/mapper/$DEVMAPPER_POOL $DEVICE_ID"
dmsetup suspend $DEV
# Adjust to new size
dmsetup table $DEV | sed "s/0 [0-9]* thin/0 $(($NEW_SIZE/512)) thin/" | dmsetup load $DEV
# resume. Is this good enough?
dmsetup resume $DEV
e2fsck -f /dev/mapper/$DEV
resize2fs /dev/mapper/$DEV
# Update metadata
NEW_METADATA_FILE=$(cat $METADATA_FILE | python -c "import json,sys;obj=json.load(sys.stdin);obj['size']=$NEW_SIZE;print json.dumps(obj)")
NEW_METADATA_INIT_FILE=$(cat $METADATA_INIT_FILE | python -c "import json,sys;obj=json.load(sys.stdin);obj['size']=$NEW_SIZE;print json.dumps(obj)")
if [ -n "$NEW_METADATA_FILE" ]
then
echo -n $NEW_METADATA_FILE > $METADATA_FILE
fi
if [ -n "$NEW_METADATA_INIT_FILE" ]
then
echo -n $NEW_METADATA_INIT_FILE > $METADATA_INIT_FILE
fi
# Start the container
docker start $INSTANCE_NAME
# To verify after increasing
#docker exec -it $INSTANCE_NAME bash -c "df -h /"
# TBD. Currently as it stands requires docker restart before doing any more docker stop and docker stop of this container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment