Symlink to backup dir until ephemeral storage available
Currently, when you call `register-state-path --leave-symlink /a`, a symlink is left at /a pointing to /mnt/state/a. However, /mnt/state doesn't point at ephemeral storage until the instance has booted. So if another element tries to write through the symlink, it goes wrong. This patch defers moving content (and leaving a symlink if required) to the backup dir until finalise.d runs. Change-Id: Id5bc25965db1fdf27f2bcb947c518f6f4566fd2f Co-Author: Nicholas Randon
This commit is contained in:
parent
cf1b37ac01
commit
ac5279a265
@ -18,11 +18,11 @@
|
|||||||
set -eux
|
set -eux
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
SCRIPT_NAME=$(basename $0)
|
SCRIPT_NAME=$(basename ${0})
|
||||||
SCRIPT_HOME=$(dirname $0)
|
SCRIPT_HOME=$(dirname ${0})
|
||||||
|
|
||||||
function show_options () {
|
function show_options () {
|
||||||
echo "Usage: $SCRIPT_NAME [options] <path>"
|
echo "Usage: ${SCRIPT_NAME} [options] <path>"
|
||||||
echo
|
echo
|
||||||
echo "Register <path> as a state path."
|
echo "Register <path> as a state path."
|
||||||
echo
|
echo
|
||||||
@ -35,40 +35,34 @@ function show_options () {
|
|||||||
echo "Options:"
|
echo "Options:"
|
||||||
echo " --leave-symlink -- create a symlink from <path> to /mnt/state/<path>."
|
echo " --leave-symlink -- create a symlink from <path> to /mnt/state/<path>."
|
||||||
echo
|
echo
|
||||||
exit $1
|
exit ${1}
|
||||||
}
|
}
|
||||||
|
|
||||||
LEAVE_SYMLINK=""
|
LEAVE_SYMLINK=""
|
||||||
|
|
||||||
TEMP=`getopt -o h -l help,leave-symlink -n $SCRIPT_NAME -- "$@"`
|
TEMP=`getopt -o h -l help,leave-symlink -n ${SCRIPT_NAME} -- "$@"`
|
||||||
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
||||||
|
|
||||||
# Note the quotes around `$TEMP': they are essential!
|
# Note the quotes around `${TEMP}': they are essential!
|
||||||
eval set -- "$TEMP"
|
eval set -- "${TEMP}"
|
||||||
|
|
||||||
while true ; do
|
while true ; do
|
||||||
case "$1" in
|
case "${1}" in
|
||||||
--leave-symlink) LEAVE_SYMLINK="true"; shift 1 ;;
|
--leave-symlink) LEAVE_SYMLINK="true"; shift 1 ;;
|
||||||
-h | --help) show_options 0;;
|
-h | --help) show_options 0;;
|
||||||
--) shift ; break ;;
|
--) shift ; break ;;
|
||||||
*) echo "Error: unsupported option $1." ; exit 1 ;;
|
*) echo "Error: unsupported option ${1}." ; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
STATE_PATH=${1:-""}
|
STATE_PATH=${1:-""}
|
||||||
EXTRA=${2:-""}
|
EXTRA=${2:-""}
|
||||||
|
|
||||||
if [ -z "$STATE_PATH" -o -n "$EXTRA" ]; then
|
if [ -z "${STATE_PATH}" -o -n "${EXTRA}" ]; then
|
||||||
show_options 1
|
show_options 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p /var/lib/use-ephemeral
|
backup="/var/lib/use-ephemeral"
|
||||||
echo $STATE_PATH >> /var/lib/use-ephemeral/stateful-paths
|
mkdir -p "${backup}"
|
||||||
if [ -e "$STATE_PATH" -o -L "$STATE_PATH" ]; then
|
|
||||||
backup_dir=/var/lib/use-ephemeral/$(dirname "$STATE_PATH")
|
echo "${STATE_PATH};${LEAVE_SYMLINK}" >> "${backup}/stateful-paths"
|
||||||
mkdir -p "$backup_dir"
|
|
||||||
mv "$STATE_PATH" "$backup_dir"
|
|
||||||
fi
|
|
||||||
if [ -n "$LEAVE_SYMLINK" ]; then
|
|
||||||
ln -s "/mnt/state/$STATE_PATH" "$STATE_PATH"
|
|
||||||
fi
|
|
||||||
|
40
elements/use-ephemeral/finalise.d/00-cleanup-stateful-paths
Executable file
40
elements/use-ephemeral/finalise.d/00-cleanup-stateful-paths
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
set -eux
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
backup="/var/lib/use-ephemeral"
|
||||||
|
paths="${backup}/stateful-paths"
|
||||||
|
state="/mnt/state"
|
||||||
|
|
||||||
|
[ -e "${paths}" ] || exit 0
|
||||||
|
|
||||||
|
while read -r line; do
|
||||||
|
path="${line%;*}"
|
||||||
|
|
||||||
|
if [ -e "${path}" ]; then
|
||||||
|
backup_dir="${backup}$(dirname "${path}")"
|
||||||
|
mkdir -p "${backup_dir}"
|
||||||
|
mv "${path}" "${backup_dir}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${line##*;}" ]; then
|
||||||
|
ln -s "${state}${path}" "${path}"
|
||||||
|
fi
|
||||||
|
done < "${paths}"
|
||||||
|
|
@ -26,11 +26,12 @@ state="/mnt/state"
|
|||||||
|
|
||||||
done="$(mktemp)"
|
done="$(mktemp)"
|
||||||
while read -r line; do
|
while read -r line; do
|
||||||
dst="${state}${line}"
|
path="${line%;*}"
|
||||||
|
dst="${state}${path}"
|
||||||
dst_dir=$(dirname "${dst}")
|
dst_dir=$(dirname "${dst}")
|
||||||
|
|
||||||
# stateful_paths may contain duplicates, don't copy any path twice
|
# stateful_paths may contain duplicates, don't copy any path twice
|
||||||
src="${backup}${line}"
|
src="${backup}${path}"
|
||||||
[ -L "${src}" ] || src="$(readlink -f "${src}")"
|
[ -L "${src}" ] || src="$(readlink -f "${src}")"
|
||||||
grep -qFx "${src}" "${done}" && continue
|
grep -qFx "${src}" "${done}" && continue
|
||||||
echo "${src}" >> "${done}"
|
echo "${src}" >> "${done}"
|
||||||
|
Loading…
Reference in New Issue
Block a user