Skip to content

Instantly share code, notes, and snippets.

@Valian
Last active July 3, 2018 11:02
Show Gist options
  • Save Valian/910b29ba815dcfd78c20451bacf8479f to your computer and use it in GitHub Desktop.
Save Valian/910b29ba815dcfd78c20451bacf8479f to your computer and use it in GitHub Desktop.
Ubuntu Google Cloud Platform server - mount new disk, install docker and docker compose and change docker disk to new location. CAUTION: Only tested on a fresh server!!
#!/bin/bash
set -e
DISK="$1"
MOUNTPOINT="$2"
if [ -z "$DISK" ] || [ -z "$MOUNTPOINT" ]; then
echo "Usage: run.sh DISK MOUNTPOINT"
exit 1
fi
echo "Found disk '$DISK', mounting it to $MOUNTPOINT. Is it correct? Stop script in 20s if not (Ctrl^C) "
( set -x; sleep 20 )
echo "Creating new partition and mouting it to $MOUNTPOINT..."
mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/$DISK
echo "Creating mount directory"
mkdir -p $MOUNTPOINT
echo "Mounting disk"
mount -o discard,defaults /dev/$DISK $MOUNTPOINT
chmod a+w $MOUNTPOINT
echo "Editing fstab file"
cp /etc/fstab /etc/fstab.backup
echo UUID=$(blkid -s UUID -o value /dev/$DISK) $MOUNTPOINT ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
echo "Installing Docker"
wget -qO- https://get.docker.com/ | sh
# Install docker-compose
COMPOSE_VERSION=`git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oP "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | tail -n 1`
sh -c "curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
chmod +x /usr/local/bin/docker-compose
sh -c "curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose"
sed -i "s#ExecStart=\(.*\)#ExecStart=\1 -g $MOUNTPOINT\/docker #g" /lib/systemd/system/docker.service
systemctl daemon-reload
service docker stop
echo "Waiting 20s for docker to stop..."
sleep 20
echo "Moving old directory to a new location..."
mv /var/lib/docker $MOUNTPOINT/docker
echo "Starting docker"
service docker start
echo "Done."
# example usage
wget https://gist.githubusercontent.com/Valian/910b29ba815dcfd78c20451bacf8479f/raw/c83d3b781617ea1d263724b87be06ea60a6f9f7f/run.sh
sudo run.sh sdb /hdd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment