mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-16 09:34:39 +00:00
192 lines
4.1 KiB
Bash
Executable File
192 lines
4.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Copyright (c) 2014-2018 Franco Fichtner <franco@opnsense.org>
|
|
# Copyright (c) 2004-2009 Scott Ullrich <sullrich@gmail.com>
|
|
#
|
|
# Redistribution and use in source and binary forms, with or without
|
|
# modification, are permitted provided that the following conditions are met:
|
|
#
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
# this list of conditions and the following disclaimer.
|
|
#
|
|
# 2. 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.
|
|
#
|
|
# THIS SOFTWARE IS PROVIDED ``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
|
|
# AUTHOR 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.
|
|
|
|
INSTALL="/.probe.for.install.media"
|
|
MNT="/tmp/hdrescue"
|
|
|
|
bootstrap_and_exit()
|
|
{
|
|
# ensure config directory structure
|
|
mkdir -p /conf/backup
|
|
mkdir -p /conf/sshd
|
|
|
|
# create initial config.xml if necessary
|
|
if [ ! -f /conf/config.xml ]; then
|
|
echo -n "Bootstrapping config.xml..."
|
|
cp /usr/local/etc/config.xml /conf/config.xml
|
|
echo "done."
|
|
fi
|
|
|
|
# clean up after a finished import
|
|
if [ -d ${MNT} ]; then
|
|
umount ${MNT} 2> /dev/null
|
|
rm -rf ${MNT}
|
|
fi
|
|
|
|
exit 0
|
|
}
|
|
|
|
|
|
trap bootstrap_and_exit 2
|
|
|
|
DO_BOOT=
|
|
|
|
while getopts b OPT; do
|
|
case ${OPT} in
|
|
b)
|
|
DO_BOOT="-b"
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown argument during import." >&2
|
|
bootstrap_and_exit
|
|
;;
|
|
esac
|
|
done
|
|
|
|
timeout_prompt()
|
|
{
|
|
OUTPUT=$(echo ${2} | sed 's/./& /g')
|
|
MESSAGE=${1}
|
|
RETURN=1
|
|
|
|
echo -n "${MESSAGE} "
|
|
|
|
stty cbreak -echo
|
|
for NEXT in ${OUTPUT}; do
|
|
echo -n ${NEXT}
|
|
if timeout 1 dd of=/dev/null count=1 status=none; then
|
|
RETURN=0
|
|
break
|
|
fi
|
|
done
|
|
stty -cbreak echo
|
|
|
|
echo
|
|
|
|
return ${RETURN}
|
|
}
|
|
|
|
probe_for_part()
|
|
{
|
|
DEV=${1}
|
|
|
|
if [ -e "/dev/${DEV}s1a" ]; then
|
|
# MBR layout found
|
|
export PART="/dev/${DEV}s1a"
|
|
return 0
|
|
elif [ -e "/dev/${DEV}p3" ]; then
|
|
# GPT layout found
|
|
export PART="/dev/${DEV}p3"
|
|
return 0
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
DEVS=
|
|
PART=
|
|
|
|
if [ -n "${1}" ]; then
|
|
if ! probe_for_part ${1}; then
|
|
bootstrap_and_exit
|
|
fi
|
|
else
|
|
if [ -n "${DO_BOOT}" ]; then
|
|
touch ${INSTALL} 2> /dev/null
|
|
if [ -f ${INSTALL} -a -f /conf/config.xml ]; then
|
|
bootstrap_and_exit
|
|
fi
|
|
|
|
if ! timeout_prompt \
|
|
'Press any key to start the configuration importer:' .......; then
|
|
bootstrap_and_exit
|
|
fi
|
|
fi
|
|
DEVS=$(camcontrol devlist)
|
|
fi
|
|
|
|
while [ -z "${PART}" ]; do
|
|
echo
|
|
echo "${DEVS}"
|
|
echo
|
|
read -p "Select device to import from (e.g. ada0) or leave blank to exit: " DEV
|
|
echo
|
|
|
|
if [ -z "${DEV}" ]; then
|
|
bootstrap_and_exit
|
|
fi
|
|
|
|
if ! probe_for_part ${DEV}; then
|
|
echo "No known partition layout was found for '${DEV}'."
|
|
fi
|
|
done
|
|
|
|
echo "Starting import for partition '${PART}'."
|
|
echo
|
|
|
|
mkdir -p ${MNT}
|
|
|
|
echo -n "Running fsck..."
|
|
fsck -t ufs -y ${PART} > /dev/null
|
|
echo "done."
|
|
|
|
if ! mount ${PART} ${MNT} 2> /dev/null; then
|
|
echo "The device could not be mounted."
|
|
bootstrap_and_exit
|
|
# XXX redo
|
|
fi
|
|
|
|
if [ -f "${MNT}/conf/config.xml" ]; then
|
|
rm -rf /conf/*
|
|
for FILE in captiveportal.sqlite config.xml dhcpleases.tgz dhcp6c_duid netflow.tgz rrd.tgz; do
|
|
if [ -f "${MNT}/conf/${FILE}" ]; then
|
|
echo -n "Restoring ${FILE}..."
|
|
cp "${MNT}/conf/${FILE}" /conf
|
|
echo "done."
|
|
fi
|
|
done
|
|
for DIR in backup sshd; do
|
|
if [ -d "${MNT}/conf/${DIR}" ]; then
|
|
echo -n "Restoring ${DIR}..."
|
|
cp -r "${MNT}/conf/${DIR}" /conf
|
|
echo "done."
|
|
else
|
|
mkdir -p "/conf/${DIR}"
|
|
fi
|
|
done
|
|
else
|
|
echo "No previous configuration was found on this device."
|
|
bootstrap_and_exit
|
|
# XXX redo
|
|
fi
|
|
|
|
if [ -z "${DO_BOOT}" ]; then
|
|
echo "Please reboot."
|
|
fi
|
|
|
|
bootstrap_and_exit
|