From 0b31ec7688b8131dab287a5ba26d88d2be4f49a8 Mon Sep 17 00:00:00 2001 From: Aaron Dewes Date: Mon, 12 Apr 2021 14:48:15 +0200 Subject: [PATCH] Move docker data to the SSD (#589) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lounès Ksouri Co-authored-by: Luke Childs --- scripts/umbrel-os/external-storage/mount | 22 +++++++++++ .../services/umbrel-external-storage.service | 1 + scripts/update/01-run.sh | 37 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/scripts/umbrel-os/external-storage/mount b/scripts/umbrel-os/external-storage/mount index 3da25cf..ff860e8 100755 --- a/scripts/umbrel-os/external-storage/mount +++ b/scripts/umbrel-os/external-storage/mount @@ -16,6 +16,8 @@ set -euo pipefail UMBREL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../../..)" MOUNT_POINT="/mnt/data" EXTERNAL_UMBREL_ROOT="${MOUNT_POINT}/umbrel" +DOCKER_DIR="/var/lib/docker" +EXTERNAL_DOCKER_DIR="${MOUNT_POINT}/docker" SWAP_DIR="/swap" SWAP_FILE="${SWAP_DIR}/swapfile" @@ -112,6 +114,15 @@ setup_new_device () { "${UMBREL_ROOT}" "${EXTERNAL_UMBREL_ROOT}" } +# Copy Docker data dir to external storage +copy_docker_to_external_storage () { + mkdir -p "${EXTERNAL_DOCKER_DIR}" + cp --recursive \ + --archive \ + --no-target-directory \ + "${DOCKER_DIR}" "${EXTERNAL_DOCKER_DIR}" +} + main () { echo "Running external storage mount script..." check_root @@ -174,9 +185,17 @@ main () { setup_new_device $block_device $partition_path fi + if [[ ! -d "${EXTERNAL_DOCKER_DIR}" ]]; then + echo "Copying Docker data directory to external storage..." + copy_docker_to_external_storage + fi + echo "Bind mounting external storage over local Umbrel installation..." mount --bind "${EXTERNAL_UMBREL_ROOT}" "${UMBREL_ROOT}" + echo "Bind mounting external storage over local Docker data dir..." + mount --bind "${EXTERNAL_DOCKER_DIR}" "${DOCKER_DIR}" + echo "Bind mounting external storage to ${SWAP_DIR}" mkdir -p "${MOUNT_POINT}/swap" "${SWAP_DIR}" mount --bind "${MOUNT_POINT}/swap" "${SWAP_DIR}" @@ -190,6 +209,9 @@ main () { sleep 1 df -h "${UMBREL_ROOT}" | grep --quiet '/dev/sd' + echo "Checking ${DOCKER_DIR} is now on external storage..." + df -h "${DOCKER_DIR}" | grep --quiet '/dev/sd' + echo "Checking ${SWAP_DIR} is now on external storage..." df -h "${SWAP_DIR}" | grep --quiet '/dev/sd' diff --git a/scripts/umbrel-os/services/umbrel-external-storage.service b/scripts/umbrel-os/services/umbrel-external-storage.service index 164b18a..1836062 100644 --- a/scripts/umbrel-os/services/umbrel-external-storage.service +++ b/scripts/umbrel-os/services/umbrel-external-storage.service @@ -3,6 +3,7 @@ [Unit] Description=External Storage Mounter +Before=docker.service [Service] Type=oneshot diff --git a/scripts/update/01-run.sh b/scripts/update/01-run.sh index 8ed3c5b..1645b5e 100755 --- a/scripts/update/01-run.sh +++ b/scripts/update/01-run.sh @@ -121,6 +121,43 @@ EOF cd "$UMBREL_ROOT" ./scripts/stop +# Move Docker data dir to external storage now if this is an old install. +# This is only needed temporarily until all users have transitioned Docker to SSD. +DOCKER_DIR="/var/lib/docker" +MOUNT_POINT="/mnt/data" +EXTERNAL_DOCKER_DIR="${MOUNT_POINT}/docker" +if [[ ! -z "${UMBREL_OS:-}" ]] && [[ ! -d "${EXTERNAL_DOCKER_DIR}" ]]; then + echo "Attempting to move Docker to external storage..." +cat < "$UMBREL_ROOT"/statuses/update-status.json +{"state": "installing", "progress": 72, "description": "Migrating Docker install to external storage", "updateTo": "$RELEASE"} +EOF + + echo "Stopping Docker service..." + systemctl stop docker + + # Copy Docker data dir to external storage + copy_docker_to_external_storage () { + mkdir -p "${EXTERNAL_DOCKER_DIR}" + cp --recursive \ + --archive \ + --no-target-directory \ + "${DOCKER_DIR}" "${EXTERNAL_DOCKER_DIR}" + } + + echo "Copying Docker data directory to external storage..." + copy_docker_to_external_storage + + echo "Bind mounting external storage over local Docker data dir..." + mount --bind "${EXTERNAL_DOCKER_DIR}" "${DOCKER_DIR}" + + # Ensure fs changes are registered + sync + sleep 1 + + echo "Starting Docker service..." + systemctl start docker +fi + # Overlay home dir structure with new dir tree echo "Overlaying $UMBREL_ROOT/ with new directory tree" rsync --archive \