M #-: Polish script to RECREATE_RUN

pull/219/head
Vlastimil Holer 4 years ago
parent c68deb36c2
commit 23fb8394e7

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# -------------------------------------------------------------------------- # # -------------------------------------------------------------------------- #
# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
# # # #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # # 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 # # not use this file except in compliance with the License. You may obtain #
@ -16,37 +16,40 @@
# limitations under the License. # # limitations under the License. #
#--------------------------------------------------------------------------- # #--------------------------------------------------------------------------- #
TMP_DIR=$(mktemp -d "/tmp/one-context.XXXXXX") set -e
# Umount the directory and remove it # Umount the directory and remove it
cleanup() cleanup()
{ {
[ "$MOUNT" = "yes" ] && umount "${TMP_DIR}" [ "$MOUNTED" = "yes" ] && umount -l "${ROOTFS_DIR}"
rm -rf "${TMP_DIR}" rm -r "${TMP_DIR}"
} }
# Get files that should be copy from if [ "${RECREATE_RUN^^}" = "YES" ] && [ "$(uname -s)" = 'Linux' ]; then
get_files() # Detection of real run-time variable data directory in case there
{ # would be images with (unusual) reverse symlink /run -> /var/run
diff -qr "$1" "$2" | grep "Only in $1" | cut -d ':' -f2 | awk '{$1=$1};1' if [ -d /run ] && ! [ -L /run ]; then
} RUN_DIR=/run
elif [ -d /var/run ] && ! [ -L /var/run ]; then
if [ "${RECREATE_TMPFS^^}" = "YES" ]; then RUN_DIR=/var/run
MOUNT=yes else
# this shouldn't happen
mount --bind -o ro / "${TMP_DIR}" echo 'ERROR: No suitable run-time data directory in image!' >&2
exit 1
IFS=$'\n' fi
# Copy only files that are in /run but not in /var/run ###
files=$(get_files "${TMP_DIR}"/run /var/run)
TMP_DIR=$(mktemp -d "/tmp/one-context.XXXXXX")
for file in $files; ROOTFS_DIR="${TMP_DIR}/rootfs"
do trap cleanup EXIT
cp -pr "${TMP_DIR}"/run/"${file}" /var/run &> /dev/null chmod 0700 "${TMP_DIR}"
done mkdir "${ROOTFS_DIR}"
mount --bind -o ro,nodev,noexec,nosuid / "${ROOTFS_DIR}"
MOUNTED=yes
# copy, but don't overwrite
# NOTE: using -i </dev/null is a workaround for silly cp in busybox without -n support
cp -aiv "${ROOTFS_DIR}/${RUN_DIR}" "$(dirname "${RUN_DIR}")" 2>/dev/null </dev/null
fi fi
trap cleanup EXIT
exit 0
Loading…
Cancel
Save