Files
BastilleBSD_bastille/usr/local/share/bastille/migrate.sh

236 lines
8.0 KiB
Bash
Raw Normal View History

#!/bin/sh
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2018-2025, Christer Edwards <christer.edwards@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. /usr/local/share/bastille/common.sh
usage() {
error_notify "Usage: bastille migrate [option(s)] TARGET HOST USER"
cat << EOF
Options:
-a | --auto Auto mode. Start/stop jail(s) if required.
-d | --destroy Destroy local jail after migration.
-x | --debug Enable debug mode.
EOF
exit 1
}
# Handle options.
AUTO=0
OPT_DESTROY=0
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
-a|--auto)
AUTO=1
shift
;;
-d|--destroy)
OPT_DESTROY=1
shift
;;
-x|--debug)
enable_debug
shift
;;
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
a) AUTO=1 ;;
d) OPT_DESTROY=1 ;;
x) enable_debug ;;
*) error_exit "[ERROR]: Unknown Option: \"${1}\"" ;;
esac
done
shift
;;
*)
break
;;
esac
done
if [ "$#" -ne 3 ]; then
usage
fi
TARGET="${1}"
HOST="${2}"
USER="${3}"
bastille_root_check
set_target_single "${TARGET}"
# Validate jail state
check_target_is_stopped "${TARGET}" || if [ "${AUTO}" -eq 1 ]; then
bastille stop "${TARGET}"
else
info "\n[${TARGET}]:"
error_notify "Jail is running."
error_exit "Use [-a|--auto] to auto-stop the jail."
fi
validate_host_status() {
local _host="${1}"
local _user="${2}"
info "\nChecking remote host status..."
if ! ping -c 1 ${_host} >/dev/null 2>/dev/null; then
error_exit "[ERROR]: Host appears to be down"
elif ! ssh ${_user}@${_host} exit >/dev/null 2>/dev/null; then
error_notify "[ERROR]: Could not establish ssh connection to host."
error_exit "Please make sure user '${_user}' has password-less access."
else
echo "Host check successful."
fi
}
migrate_create_export() {
local _jail="${1}"
info "\nPreparing jail for migration..."
# --xz for ZFS, otherwise --txz
if checkyesno bastille_zfs_enable; then
bastille export --xz ${_jail} ${bastille_migratedir}
else
bastille export --txz ${_jail} ${_bastille_migratedir}
fi
}
migrate_jail() {
local _jail="${1}"
local _host="${2}"
local _user="${3}"
local _remote_bastille_zfs_enable="$(ssh ${_user}@${_host} sysrc -f /usr/local/etc/bastille/bastille.conf -n bastille_zfs_enable)"
local _remote_bastille_jailsdir="$(ssh ${_user}@${_host} sysrc -f /usr/local/etc/bastille/bastille.conf -n bastille_jailsdir)"
local _remote_bastille_migratedir="$(ssh ${_user}@${_host} sysrc -f /usr/local/etc/bastille/bastille.conf -n bastille_migratedir)"
local _remote_jail_list="$(ssh ${_user}@${_host} bastille list jails)"
# Verify jail does not exist remotely
if echo ${_remote_jail_list} | grep -Eoq "^${TARGET}$"; then
error_exit "[ERROR]: Jail already exists on remote system: ${TARGET}"
fi
# Verify ZFS on both systems
if checkyesno bastille_zfs_enable; then
if ! checkyesno _remote_bastille_zfs_enable; then
error_notify "[ERROR]: ZFS is enabled locally, but not remotely."
error_exit "Enable ZFS remotely to continue."
else
info "\nAttempting to send jail to remote system..."
local _file="$(ls -a ${bastille_migratedir} | grep -Eo "^${_jail}_.*\.xz")"
local _file_sha256="$(ls -a ${bastille_migratedir} | grep -Eo "^${_jail}_.*\.sha256")"
# Send sha256
if ! scp ${bastille_migratedir}/${_file_sha256} ${_user}@${_host}:${_remote_bastille_migratedir}; then
rm -f "${bastille_migratedir}/${_file_sha256}"
error_exit "[ERROR]: Failed to send jail to remote system."
fi
# Send jail export
if ! scp ${bastille_migratedir}/${_file} ${_user}@${_host}:${_remote_bastille_migratedir}; then
rm -f "${bastille_migratedir}/${_file_sha256}"
rm -f "${bastille_migratedir}/${_file}"
error_exit "[ERROR]: Failed to send jail to remote system."
fi
fi
else
if checkyesno _remote_bastille_zfs_enable; then
error_notify "[ERROR]: ZFS is enabled remotely, but not locally."
error_exit "Enable ZFS locally to continue."
else
info "\nAttempting to send jail to remote system..."
local _file="$(ls -a ${bastille_migratedir} | grep -Eo "^${_jail}_.*\.txz")"
local _file_sha256="$(ls -a ${bastille_migratedir} | grep -Eo "^${_jail}_.*\.sha256")"
# Send sha256
if ! scp ${bastille_migratedir}/${_file_sha256} ${_user}@${_host}:${_remote_bastille_migratedir}; then
rm -f "${bastille_migratedir}/${_file_sha256}"
error_exit "[ERROR]: Failed to send jail to remote system."
fi
# Send jail export
if ! scp ${bastille_migratedir}/${_file} ${_user}@${_host}:${_remote_bastille_migratedir}; then
rm -f "${bastille_migratedir}/${_file_sha256}"
rm -f "${bastille_migratedir}/${_file}"
error_exit "[ERROR]: Failed to send jail to remote system."
fi
fi
fi
# Import the jail remotely
if ! ssh ${_user}@${_host} sudo bastille import ${_remote_bastille_migratedir}/${_file}; then
ssh ${_user}@${_host} sudo rm -f "${_remote_bastille_migratedir}/${_file_sha256}"
ssh ${_user}@${_host} sudo rm -f "${_remote_bastille_migratedir}/${_file}"
error_exit "[ERROR]: Failed to import jail on remote system."
fi
# Remove archive files from local and remote system
ssh ${_user}@${_host} sudo rm -f "${_remote_bastille_migratedir}/${_file_sha256}"
ssh ${_user}@${_host} sudo rm -f "${_remote_bastille_migratedir}/${_file}"
rm -f "${bastille_migratedir}/${_file_sha256}"
rm -f "${bastille_migratedir}/${_file}"
# Destroy old jail if FORCE=1
if [ "${OPT_DESTROY}" -eq 1 ]; then
bastille destroy -af "${_jail}"
fi
}
# Validate user
if [ -z "${USER}" ]; then
USER="root"
fi
info "\nAttempting to migrate '${TARGET}' to '${HOST}'..."
validate_host_status "${HOST}" "${USER}"
migrate_jail "${TARGET}" "${HOST}" "${USER}"
info "\nSuccessfully migrated '${_jail}' to '${_host}'.\n"