Antonio Murdaca 4a819c6
#!/bin/bash
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
# Copyright (C) 2016 Red Hat, Inc.
Antonio Murdaca 4a819c6
#
Antonio Murdaca 4a819c6
# This program is free software: you can redistribute it and/or modify
Antonio Murdaca 4a819c6
# it under the terms of the GNU General Public License as published by
Antonio Murdaca 4a819c6
# the Free Software Foundation, either version 3 of the License, or
Antonio Murdaca 4a819c6
# (at your option) any later version.
Antonio Murdaca 4a819c6
#
Antonio Murdaca 4a819c6
# This program is distributed in the hope that it will be useful,
Antonio Murdaca 4a819c6
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Antonio Murdaca 4a819c6
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Antonio Murdaca 4a819c6
# GNU General Public License for more details.
Antonio Murdaca 4a819c6
#
Antonio Murdaca 4a819c6
# You should have received a copy of the GNU General Public License
Antonio Murdaca 4a819c6
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
set -euo pipefail
Antonio Murdaca 4a819c6
IFS=$'\n\t'
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
# This is a small wrapper script that automatically fetches
Antonio Murdaca 4a819c6
# the storage options from the docker-storage sysconfig file
Antonio Murdaca 4a819c6
# and passes them to the migrator.
Antonio Murdaca 4a819c6
#
Antonio Murdaca 4a819c6
# The script supports both in-container runs and direct
Antonio Murdaca 4a819c6
# invocation.
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
MIGRATOR=/usr/bin/v1.10-migrator-local
Antonio Murdaca 4a819c6
STORAGE_FILE=/etc/sysconfig/docker-storage
Antonio Murdaca 4a819c6
GRAPH=/var/lib/docker
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
main() {
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
	# are we in a container?
Antonio Murdaca 4a819c6
	if [[ -n ${container-} ]]; then
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
		if [[ ! -d /host ]]; then
Antonio Murdaca 4a819c6
			echo "ERROR: Running inside a container, but /host not mounted." >&2
Antonio Murdaca 4a819c6
			exit 1
Antonio Murdaca 4a819c6
		fi
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
		cp "$MIGRATOR" /host/tmp
Antonio Murdaca 4a819c6
		MIGRATOR="chroot /host /tmp/$(basename $MIGRATOR)"
Antonio Murdaca 4a819c6
		STORAGE_FILE=/host${STORAGE_FILE}
Antonio Murdaca 4a819c6
	fi
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
	if [ ! -d "$GRAPH" ]; then
Antonio Murdaca 4a819c6
		echo "ERROR: Cannot find docker root dir at \"$GRAPH\"." >&2
Antonio Murdaca 4a819c6
		exit 1
Antonio Murdaca 4a819c6
	fi
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
	# load storage opts if we can find the file
Antonio Murdaca 4a819c6
	local storage_opts=
Antonio Murdaca 4a819c6
	if [ -r "$STORAGE_FILE" ] && grep -q -E '^DOCKER_STORAGE_OPTIONS\s*=' "$STORAGE_FILE"; then
Antonio Murdaca 4a819c6
		storage_opts=$(sed -n -e 's/^DOCKER_STORAGE_OPTIONS\s*=\s*// p' "$STORAGE_FILE")
Antonio Murdaca 4a819c6
		storage_opts=${storage_opts#\"}
Antonio Murdaca 4a819c6
		storage_opts=${storage_opts%\"}
Antonio Murdaca 4a819c6
	fi
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
	CMD="$MIGRATOR --graph $GRAPH $storage_opts"
Antonio Murdaca 4a819c6
	echo "RUNNING: $CMD"
Antonio Murdaca 4a819c6
	eval $CMD
Antonio Murdaca 4a819c6
}
Antonio Murdaca 4a819c6
Antonio Murdaca 4a819c6
main "$@"