mirror of
https://github.com/Antynea/grub-btrfs.git
synced 2026-10-20 02:42:03 +08:00
grub-btrfsd: refactor whole script
Signed-off-by: Pascal Jäger <pascal.jaeger@leimstift.de>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
|
||||||
GRUB_BTRFS_VERSION=4.12-master-2023-04-05T15:26:58+00:00
|
GRUB_BTRFS_VERSION=4.12-master-2023-04-05T16:05:29+00:00
|
||||||
|
|
||||||
# Disable grub-btrfs.
|
# Disable grub-btrfs.
|
||||||
# Default: "false"
|
# Default: "false"
|
||||||
|
|||||||
+175
-163
@@ -3,19 +3,7 @@
|
|||||||
# Distributed under the terms of the GNU General Public License v3
|
# Distributed under the terms of the GNU General Public License v3
|
||||||
# Update GRUB when new BTRFS snapshots are created.
|
# Update GRUB when new BTRFS snapshots are created.
|
||||||
|
|
||||||
# init
|
sighandler() {
|
||||||
timeshift_pid=-1
|
|
||||||
watchtime=0
|
|
||||||
logfile=0
|
|
||||||
timeshift_auto=false
|
|
||||||
timeshift_old=false
|
|
||||||
verbose=false
|
|
||||||
syslog=false
|
|
||||||
recursive=false
|
|
||||||
|
|
||||||
trap sighandler SIGINT SIGTERM
|
|
||||||
sighandler()
|
|
||||||
{
|
|
||||||
trap '""' SIGINT SIGTERM
|
trap '""' SIGINT SIGTERM
|
||||||
vlog "Parent $$: Received signal SIGINT/ SIGTERM"
|
vlog "Parent $$: Received signal SIGINT/ SIGTERM"
|
||||||
kill 0
|
kill 0
|
||||||
@@ -23,7 +11,6 @@ sighandler()
|
|||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setcolors() {
|
setcolors() {
|
||||||
if [ "${1}" = true ]; then
|
if [ "${1}" = true ]; then
|
||||||
GREEN=$'\033[0;32m'
|
GREEN=$'\033[0;32m'
|
||||||
@@ -38,13 +25,6 @@ setcolors() {
|
|||||||
RESET=$'\033[0m'
|
RESET=$'\033[0m'
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
setcolors true # normally we want colors
|
|
||||||
|
|
||||||
sysconfdir="/etc"
|
|
||||||
grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"
|
|
||||||
# source config file
|
|
||||||
[ -f "$grub_btrfs_config" ] && . "$grub_btrfs_config"
|
|
||||||
[ -f "${sysconfdir}/default/grub" ] && . "${sysconfdir}/default/grub"
|
|
||||||
|
|
||||||
print_help() {
|
print_help() {
|
||||||
echo "${CYAN}[?] Usage:"
|
echo "${CYAN}[?] Usage:"
|
||||||
@@ -97,138 +77,131 @@ err() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# parse arguments
|
parse_arguments() {
|
||||||
while getopts :l:ctvrsh-: opt; do
|
while getopts :l:ctvrsh-: opt; do
|
||||||
case "$opt" in
|
case "$opt" in
|
||||||
-)
|
-)
|
||||||
case "${OPTARG}" in
|
case "${OPTARG}" in
|
||||||
no-color)
|
no-color)
|
||||||
setcolors false
|
setcolors false
|
||||||
;;
|
;;
|
||||||
log-file)
|
log-file)
|
||||||
logfile="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
|
logfile="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
|
||||||
;;
|
;;
|
||||||
timeshift-auto)
|
timeshift-auto)
|
||||||
timeshift_auto=true
|
timeshift_auto=true
|
||||||
;;
|
;;
|
||||||
timeshift-old)
|
timeshift-old)
|
||||||
timeshift_old=true
|
timeshift_old=true
|
||||||
;;
|
;;
|
||||||
verbose)
|
verbose)
|
||||||
verbose=true
|
verbose=true
|
||||||
;;
|
;;
|
||||||
recursive)
|
recursive)
|
||||||
recursive=true
|
recursive=true
|
||||||
;;
|
;;
|
||||||
syslog)
|
syslog)
|
||||||
syslog=true
|
syslog=true
|
||||||
;;
|
;;
|
||||||
help)
|
help)
|
||||||
print_help
|
print_help
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
|
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
|
||||||
err "[!] Unknown option --${OPTARG}" "${RED}" >&2
|
err "[!] Unknown option --${OPTARG}" "${RED}" >&2
|
||||||
echo
|
echo
|
||||||
fi
|
fi
|
||||||
print_help
|
print_help
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac;;
|
esac;;
|
||||||
c)
|
c)
|
||||||
setcolors false
|
setcolors false
|
||||||
;;
|
;;
|
||||||
l)
|
l)
|
||||||
logfile="${OPTARG}"
|
logfile="${OPTARG}"
|
||||||
;;
|
;;
|
||||||
t)
|
t)
|
||||||
timeshift_auto=true
|
timeshift_auto=true
|
||||||
;;
|
;;
|
||||||
o)
|
o)
|
||||||
timeshift_old=true
|
timeshift_old=true
|
||||||
;;
|
;;
|
||||||
v)
|
v)
|
||||||
verbose=true
|
verbose=true
|
||||||
;;
|
;;
|
||||||
r)
|
r)
|
||||||
recursive=true
|
recursive=true
|
||||||
;;
|
;;
|
||||||
s)
|
s)
|
||||||
syslog=true
|
syslog=true
|
||||||
;;
|
;;
|
||||||
h)
|
h)
|
||||||
print_help
|
print_help
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
if [ "$OPTERR" = 1 ] || [ "${optspec:0:1}" = ":" ]; then
|
if [ "$OPTERR" = 1 ] || [ "${optspec:0:1}" = ":" ]; then
|
||||||
err "[!] Non-option argument: '-${OPTARG}'" "${RED}" >&2
|
err "[!] Non-option argument: '-${OPTARG}'" "${RED}" >&2
|
||||||
echo
|
echo
|
||||||
fi
|
fi
|
||||||
print_help
|
print_help
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
shift $(( OPTIND - 1 ))
|
|
||||||
|
|
||||||
snapdirs=( "${@}" )
|
|
||||||
|
|
||||||
# check if inotify exists, see issue #227
|
|
||||||
if ! command -v inotifywait >/dev/null 2>&1; then
|
|
||||||
err "[!] inotifywait was not found, exiting. Is inotify-tools installed?" "${RED}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ${#logfile} -gt 1 ]; then
|
|
||||||
touch "${logfile}"
|
|
||||||
echo "GRUB-BTRFSD log $(date)" >> "${logfile}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
log "grub-btrfsd starting up..." "${GREEN}"
|
|
||||||
|
|
||||||
if [ ${verbose} = true ]; then
|
|
||||||
inotify_qiet_flag=""
|
|
||||||
else
|
|
||||||
inotify_qiet_flag=" -q -q "
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ${recursive} = true ]; then
|
|
||||||
inotify_recursive_flag=" -r "
|
|
||||||
else
|
|
||||||
inotify_recursive_flag=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then
|
|
||||||
err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
vlog "Arguments:"
|
|
||||||
vlog "Snapshot directories: ${snapdirs[*]}"
|
|
||||||
vlog "Timestift autodetection: $timeshift_auto"
|
|
||||||
vlog "Timeshift old: $timeshift_old"
|
|
||||||
vlog "Logfile: $logfile"
|
|
||||||
vlog "Recursive: $recursive"
|
|
||||||
|
|
||||||
if ! [ ${timeshift_auto} = true ]; then
|
|
||||||
for snapdir in "${snapdirs[@]}"
|
|
||||||
do
|
|
||||||
if ! [ -d ${snapdir} ]; then
|
|
||||||
err "[!] No directory found at ${snapdir}" "${RED}" >&2
|
|
||||||
err "[!] Please specify a valid snapshot directory" "${RED}" >&2
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
;;
|
||||||
done
|
esac
|
||||||
fi
|
done
|
||||||
|
}
|
||||||
|
|
||||||
if [ ${timeshift_auto} = true ]; then
|
checks() {
|
||||||
watchtime=15
|
# check if inotify exists, see issue #227
|
||||||
[ -d /run/timeshift ] || mkdir /run/timeshift
|
if ! command -v inotifywait >/dev/null 2>&1; then
|
||||||
else
|
err "[!] inotifywait was not found, exiting. Is inotify-tools installed?" "${RED}" >&2
|
||||||
watchtime=0
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then
|
||||||
|
err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! [ ${timeshift_auto} = true ]; then
|
||||||
|
for snapdir in "${snapdirs[@]}"
|
||||||
|
do
|
||||||
|
if ! [ -d ${snapdir} ]; then
|
||||||
|
err "[!] No directory found at ${snapdir}" "${RED}" >&2
|
||||||
|
err "[!] Please specify a valid snapshot directory" "${RED}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
if [ ${#logfile} -gt 1 ]; then
|
||||||
|
touch "${logfile}"
|
||||||
|
echo "GRUB-BTRFSD log $(date)" >> "${logfile}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ${verbose} = true ]; then
|
||||||
|
inotify_qiet_flag=""
|
||||||
|
else
|
||||||
|
inotify_qiet_flag=" -q -q "
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ${recursive} = true ]; then
|
||||||
|
inotify_recursive_flag=" -r "
|
||||||
|
else
|
||||||
|
inotify_recursive_flag=""
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ${timeshift_auto} = true ]; then
|
||||||
|
watchtime=15
|
||||||
|
[ -d /run/timeshift ] || mkdir /run/timeshift
|
||||||
|
else
|
||||||
|
watchtime=0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
create_grub_menu() {
|
create_grub_menu() {
|
||||||
# create the grub submenu of the whole grub menu, depending on wether the submenu already exists
|
# create the grub submenu of the whole grub menu, depending on wether the submenu already exists
|
||||||
@@ -313,16 +286,55 @@ daemon_function() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
# init
|
||||||
|
timeshift_pid=-1
|
||||||
|
watchtime=0
|
||||||
|
logfile=0
|
||||||
|
snapshots=-1
|
||||||
|
timeshift_auto=false
|
||||||
|
timeshift_old=false
|
||||||
|
verbose=false
|
||||||
|
syslog=false
|
||||||
|
recursive=false
|
||||||
|
trap sighandler SIGINT SIGTERM
|
||||||
|
|
||||||
if [ ${timeshift_auto} = true ] ; then
|
setcolors true # normally we want colors
|
||||||
daemon_function &
|
|
||||||
else
|
sysconfdir="/etc"
|
||||||
# for all dirs that got passed to the script, start a new fork with that dir
|
grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"
|
||||||
for snapdir in "${snapdirs[@]}"
|
# source config file
|
||||||
do
|
[ -f "$grub_btrfs_config" ] && . "$grub_btrfs_config"
|
||||||
vlog "starting daemon watching $snapdir..."
|
[ -f "${sysconfdir}/default/grub" ] && . "${sysconfdir}/default/grub"
|
||||||
daemon_function "${snapdir}" &
|
|
||||||
done
|
parse_arguments "${@}"
|
||||||
fi
|
shift $(( OPTIND - 1 ))
|
||||||
wait # wait for forks to finish, kill child forks if SIGTERM is sent
|
snapdirs=( "${@}" )
|
||||||
exit 0 # tradition is tradition
|
|
||||||
|
vlog "Arguments:"
|
||||||
|
vlog "Snapshot directories: ${snapdirs[*]}"
|
||||||
|
vlog "Timestift autodetection: $timeshift_auto"
|
||||||
|
vlog "Timeshift old: $timeshift_old"
|
||||||
|
vlog "Logfile: $logfile"
|
||||||
|
vlog "Recursive: $recursive"
|
||||||
|
|
||||||
|
checks
|
||||||
|
setup
|
||||||
|
|
||||||
|
log "grub-btrfsd starting up..." "${GREEN}"
|
||||||
|
|
||||||
|
if [ ${timeshift_auto} = true ] ; then
|
||||||
|
daemon_function "timeshift" &
|
||||||
|
else
|
||||||
|
# for all dirs that got passed to the script, start a new fork with that dir
|
||||||
|
for snapdir in "${snapdirs[@]}"
|
||||||
|
do
|
||||||
|
vlog "starting daemon watching $snapdir..."
|
||||||
|
daemon_function "${snapdir}" &
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
wait # wait for forks to finish, kill child forks if SIGTERM is sent
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
main "${@}"
|
||||||
|
|||||||
Reference in New Issue
Block a user