Last active
June 14, 2020 06:50
-
-
Save janbaer/30027fe94d7d0daa671626525ba74730 to your computer and use it in GitHub Desktop.
Creates an extra Docker volume and mounts it to /va/lib/docker
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
#!/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