Skip to content

Instantly share code, notes, and snippets.

@janbaer
Last active June 14, 2020 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janbaer/30027fe94d7d0daa671626525ba74730 to your computer and use it in GitHub Desktop.
Save janbaer/30027fe94d7d0daa671626525ba74730 to your computer and use it in GitHub Desktop.
Creates an extra Docker volume and mounts it to /va/lib/docker
#!/bin/bash
# Check disksize with `parted /dev/sda print` before
STARTING_SIZE=$1
DISK_SIZE=$2
RESTART_DOCKER=false
if [ -z "${STARTING_SIZE}" ]; then
echo "Please enter the starting size in GB from where the new partition should be created"
parted /dev/sda print
exit 1
fi
if [ -z "${DISK_SIZE}" ]; then
echo "Please enter the total size of the disk /dev/sda in GB as argument"
parted /dev/sda print
exit 1
fi
echo "Creating new Docker volume starting from ${STARTING_SIZE}GB to ${DISK_SIZE}GB"
parted /dev/sda mkpart extended ${STARTING_SIZE}GB ${DISK_SIZE}GB
parted /dev/sda mkpart logical ${STARTING_SIZE}GB ${DISK_SIZE}GB
vgextend host-vg /dev/sda5
lvcreate --name docker --extents 100%FREE host-vg
mkfs.ext4 /dev/host-vg/docker -N 6500000
if [ -d "/var/lib/docker" ]; then
echo "Stop service Docker and move all folders and files under /var/lib/docker to the new volume"
mount /dev/mapper/host--vg-docker /mnt
systemctl stop docker
echo "Now move all files from /var/lib/docker to /mnt. This could take a while, be patient..."
cp -rp /var/lib/docker/* /mnt/ && rm /var/lib/docker/* -rf
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "Copying data to the new volume failed, please check why. Script will stop now..."
exit 1
fi
umount /mnt
RESTART_DOCKER=true
fi
mkdir -p /var/lib/docker
echo "/dev/mapper/host--vg-docker /var/lib/docker ext4 defaults 0 2" >> /etc/fstab
echo "Now the new volume will be mounted as /var/lib/docker"
mount -a
if [ $RESTART_DOCKER ]; then
echo "Restarting service Docker..."
systemctl start docker
systemctl status docker
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment