gen2stage4/mkstage4.sh

260 lines
5.7 KiB
Bash
Raw Normal View History

2014-05-18 17:52:33 -04:00
#!/bin/bash
# checks if run as root:
if [ "$(whoami)" != "root" ]
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
echo "$(basename "$0"): must be root."
exit 1
2014-05-18 17:52:33 -04:00
fi
#set flag variables to null
EXCLUDE_BOOT=0
EXCLUDE_CONNMAN=0
2016-02-19 15:15:25 -05:00
EXCLUDE_LOST=0
QUIET=0
USER_EXCL=()
S_KERNEL=0
x86_64=0
PARALLEL=0
HAS_PORTAGEQ=0
if command -v portageq &>/dev/null
then
2019-11-03 15:13:54 -05:00
HAS_PORTAGEQ=1
fi
if [ "$(getconf LONG_BIT)" == "64" ]
then
2019-11-03 15:13:54 -05:00
x86_64=1
fi
2016-02-11 01:51:15 -05:00
USAGE="usage:\n\
2019-11-03 15:13:54 -05:00
$(basename "$0") [-q -c -b -l -k -p] [-s || -t <target-mountpoint>] [-e <additional excludes dir*>] <archive-filename> [custom-tar-options]\n\
-q: activates quiet mode (no confirmation).\n\
-c: excludes connman network lists.\n\
-b: excludes boot directory.\n\
-l: excludes lost+found directory.\n\
-p: compresses parallelly using pbzip2.\n\
-e: an additional excludes directory (one dir one -e, donot use it with *).\n\
-s: makes tarball of current system.\n\
-k: separately save current kernel modules and src (smaller & save decompression time).\n\
-t: makes tarball of system located at the <target-mountpoint>.\n\
-h: displays help message."
2014-05-18 17:52:33 -04:00
# reads options:
while getopts ":t:e:skqcblph" flag
do
2019-11-03 15:13:54 -05:00
case "$flag" in
t)
TARGET="$OPTARG"
;;
s)
TARGET="/"
;;
q)
QUIET=1
;;
k)
S_KERNEL=1
;;
c)
EXCLUDE_CONNMAN=1
;;
b)
EXCLUDE_BOOT=1
;;
l)
EXCLUDE_LOST=1
;;
e)
USER_EXCL+=("--exclude=${OPTARG}")
;;
p)
PARALLEL=1
;;
h)
echo -e "$USAGE"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
2014-05-18 17:52:33 -04:00
done
if [ -z "$TARGET" ]
2016-02-11 01:45:57 -05:00
then
2019-11-03 15:13:54 -05:00
echo "$(basename "$0"): no target specified."
echo -e "$USAGE"
exit 1
2016-02-11 01:45:57 -05:00
fi
2017-03-22 12:17:24 -04:00
# make sure TARGET path ends with slash
2019-11-03 15:13:54 -05:00
if [[ "$TARGET" != */ ]]
2017-03-22 12:17:24 -04:00
then
2019-11-03 15:13:54 -05:00
TARGET="${TARGET}/"
2017-03-22 12:17:24 -04:00
fi
2014-05-18 17:52:33 -04:00
# shifts pointer to read mandatory output file specification
shift $((OPTIND - 1))
2014-05-18 17:52:33 -04:00
ARCHIVE=$1
# checks for correct output file specification
if [ -z "$ARCHIVE" ]
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
echo "$(basename "$0"): no archive file name specified."
echo -e "$USAGE"
exit 1
2014-05-18 17:52:33 -04:00
fi
# checks for quiet mode (no confirmation)
if ((QUIET == 1))
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
AGREE="yes"
2014-05-18 17:52:33 -04:00
fi
# determines if filename was given with relative or absolute path
if (($(grep -c '^\/' <<< "$ARCHIVE") > 0));
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
STAGE4_FILENAME="${ARCHIVE}.tar.bz2"
2014-05-18 17:52:33 -04:00
else
2019-11-03 15:13:54 -05:00
STAGE4_FILENAME="$(pwd)/${ARCHIVE}.tar.bz2"
2014-05-18 17:52:33 -04:00
fi
#Shifts pointer to read custom tar options
shift
mapfile -t OPTIONS <<< "$@"
2014-05-18 17:52:33 -04:00
if ((S_KERNEL == 1))
then
2019-11-03 15:13:54 -05:00
USER_EXCL+=("--exclude=${TARGET}usr/src/*")
if ((x86_64 == 1))
then
USER_EXCL+=("--exclude=${TARGET}lib64/modules/*")
else
USER_EXCL+=("--exclude=${TARGET}lib/modules/*")
fi
fi
2014-05-18 17:52:33 -04:00
# Excludes:
EXCLUDES=(
2019-11-03 15:13:54 -05:00
"--exclude=${TARGET}home/*/.bash_history"
"--exclude=${TARGET}dev/*"
"--exclude=${TARGET}var/tmp/*"
"--exclude=${TARGET}media/*"
"--exclude=${TARGET}mnt/*/*"
"--exclude=${TARGET}proc/*"
"--exclude=${TARGET}run/*"
"--exclude=${TARGET}sys/*"
"--exclude=${TARGET}tmp/*"
"--exclude=${TARGET}var/lock/*"
"--exclude=${TARGET}var/log/*"
"--exclude=${TARGET}var/run/*"
"--exclude=${TARGET}var/lib/docker/*"
)
EXCLUDES_DEFAULT_PORTAGE=(
2019-11-03 15:13:54 -05:00
"--exclude=${TARGET}var/db/repos/gentoo/*"
"--exclude=${TARGET}var/cache/distfiles/*"
"--exclude=${TARGET}usr/portage/*"
)
2014-05-18 17:52:33 -04:00
EXCLUDES+=("${USER_EXCL[@]}")
if [ "$TARGET" == '/' ]
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
EXCLUDES+=("--exclude=${STAGE4_FILENAME#/}")
if ((HAS_PORTAGEQ == 1))
then
EXCLUDES+=("--exclude=$(portageq get_repo_path / gentoo)/*")
EXCLUDES+=("--exclude=$(portageq distdir)/*")
else
EXCLUDES+=("${EXCLUDES_DEFAULT_PORTAGE[@]}")
fi
else
2019-11-03 15:13:54 -05:00
EXCLUDES+=("${EXCLUDES_DEFAULT_PORTAGE[@]}")
2014-05-18 17:52:33 -04:00
fi
if ((EXCLUDE_CONNMAN == 1))
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
EXCLUDES+=("--exclude=${TARGET}var/lib/connman/*")
2014-05-18 17:52:33 -04:00
fi
if ((EXCLUDE_BOOT == 1))
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
EXCLUDES+=("--exclude=${TARGET}boot/*")
2014-05-18 17:52:33 -04:00
fi
if ((EXCLUDE_LOST == 1))
then
2019-11-03 15:13:54 -05:00
EXCLUDES+=("--exclude=lost+found")
fi
2014-05-18 17:52:33 -04:00
# Generic tar options:
TAR_OPTIONS=(-cpP --ignore-failed-read "--xattrs-include='*.*'" --numeric-owner)
if [ ${PARALLEL} -eq 1 ]
then
2019-11-03 15:13:54 -05:00
if command -v pbzip2 &>/dev/null; then
TAR_OPTIONS+=("--use-compress-prog=pbzip2")
else
echo "WARING: pbzip2 isn't installed, single-threaded compressing is used." >&2
TAR_OPTIONS+=("-j")
fi
else
2019-11-03 15:13:54 -05:00
TAR_OPTIONS+=("-j")
fi
2014-05-18 17:52:33 -04:00
# if not in quiet mode, this message will be displayed:
if [[ "x$AGREE" != 'xyes' ]]
2014-05-18 17:52:33 -04:00
then
2019-11-03 15:13:54 -05:00
echo "Are you sure that you want to make a stage 4 tarball of the system"
echo "located under the following directory?"
echo "$TARGET"
echo ""
echo "WARNING: since all data is saved by default the user should exclude all"
echo "security- or privacy-related files and directories, which are not"
echo "already excluded by mkstage4 options (such as -c), manually per cmdline."
echo "example: \$ $(basename "$0") -s /my-backup --exclude=/etc/ssh/ssh_host*"
echo ""
echo "COMMAND LINE PREVIEW:"
echo "tar ${TAR_OPTIONS[*]} ${EXCLUDES[*]} ${OPTIONS[*]} -f $STAGE4_FILENAME ${TARGET}*"
if ((S_KERNEL == 1))
then
echo ""
echo "tar ${TAR_OPTIONS[*]} -f $STAGE4_FILENAME.ksrc ${TARGET}usr/src/linux-$(uname -r)*"
if ((x86_64 == 1))
then
echo ""
echo "tar ${TAR_OPTIONS[*]} -f $STAGE4_FILENAME.kmod ${TARGET}lib64/modules/$(uname -r)*"
else
echo ""
echo "tar ${TAR_OPTIONS[*]} -f $STAGE4_FILENAME.kmod ${TARGET}lib/modules/$(uname -r)*"
fi
fi
echo ""
echo -n "Type \"yes\" to continue or anything else to quit: "
read -r AGREE
2014-05-18 17:52:33 -04:00
fi
# start stage4 creation:
if [ "$AGREE" == "yes" ]
then
2019-11-03 15:13:54 -05:00
tar "${TAR_OPTIONS[@]}" "${EXCLUDES[@]}" "${OPTIONS[@]}" -f "$STAGE4_FILENAME" ${TARGET}*
if [ ${S_KERNEL} -eq 1 ]
then
tar "${TAR_OPTIONS[@]}" -f "$STAGE4_FILENAME.ksrc" "${TARGET}usr/src/linux-$(uname -r)"*
if [ ${x86_64} -eq 1 ]
then
tar "${TAR_OPTIONS[@]}" -f "$STAGE4_FILENAME.kmod" "${TARGET}lib64/modules/$(uname -r)"*
else
tar "${TAR_OPTIONS[@]}" -f "$STAGE4_FILENAME.kmod" "${TARGET}lib/modules/$(uname -r)"*
fi
fi
2014-05-18 17:52:33 -04:00
fi