grub-btrfsd: Kill process group on SIGTERM/ SIGINT

Signed-off-by: Pascal Jäger <pascal.jaeger@leimstift.de>
This commit is contained in:
Pascal Jäger
2023-04-05 17:10:49 +02:00
parent 04958d6e0b
commit 35429e02a4
2 changed files with 141 additions and 130 deletions

2
config
View File

@@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
GRUB_BTRFS_VERSION=4.12-master-2023-04-05T15:09:30+00:00 GRUB_BTRFS_VERSION=4.12-master-2023-04-05T15:10:49+00:00
# Disable grub-btrfs. # Disable grub-btrfs.
# Default: "false" # Default: "false"

View File

@@ -14,6 +14,17 @@ verbose=false
syslog=false syslog=false
recursive=false recursive=false
trap sighandler SIGINT SIGTERM
sighandler()
{
trap '""' SIGINT SIGTERM
vlog "Parent $$: Received signal SIGINT/ SIGTERM"
kill 0
wait
exit 0
}
setcolors() { setcolors() {
if [ "${1}" = true ]; then if [ "${1}" = true ]; then
GREEN=$'\033[0;32m' GREEN=$'\033[0;32m'
@@ -22,10 +33,10 @@ setcolors() {
RESET=$'\033[0m' RESET=$'\033[0m'
fi fi
if [ "${1}" = false ]; then if [ "${1}" = false ]; then
GREEN=$'\033[0;0m' GREEN=$'\033[0;0m'
RED=$'\033[0;0m' RED=$'\033[0;0m'
CYAN=$'\033[;0m' CYAN=$'\033[;0m'
RESET=$'\033[0m' RESET=$'\033[0m'
fi fi
} }
setcolors true # normally we want colors setcolors true # normally we want colors
@@ -37,129 +48,129 @@ grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"
[ -f "${sysconfdir}/default/grub" ] && . "${sysconfdir}/default/grub" [ -f "${sysconfdir}/default/grub" ] && . "${sysconfdir}/default/grub"
print_help() { print_help() {
echo "${CYAN}[?] Usage:" echo "${CYAN}[?] Usage:"
echo "${0##*/} [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-r, --recursive] [-s, --syslog] [-t, --timeshift-auto] [-v, --verbose] SNAPSHOTS_DIRS" echo "${0##*/} [-h, --help] [-c, --no-color] [-l, --log-file LOG_FILE] [-r, --recursive] [-s, --syslog] [-t, --timeshift-auto] [-v, --verbose] SNAPSHOTS_DIRS"
echo echo
echo "SNAPSHOTS_DIRS Snapshot directories to watch, without effect when --timeshift-auto" echo "SNAPSHOTS_DIRS Snapshot directories to watch, without effect when --timeshift-auto"
echo echo
echo "Optional arguments:" echo "Optional arguments:"
echo "-c, --no-color Disable colors in output" echo "-c, --no-color Disable colors in output"
echo "-l, --log-file Specify a logfile to write to" echo "-l, --log-file Specify a logfile to write to"
echo "-r, --recursive Watch snapshots directory recursively" echo "-r, --recursive Watch snapshots directory recursively"
echo "-s, --syslog Write to syslog" echo "-s, --syslog Write to syslog"
echo "-o, --timeshift-old Look for snapshots in directory of Timeshift <v22.06 (requires --timeshift-auto)" echo "-o, --timeshift-old Look for snapshots in directory of Timeshift <v22.06 (requires --timeshift-auto)"
echo "-t, --timeshift-auto Automatically detect Timeshifts snapshot directory" echo "-t, --timeshift-auto Automatically detect Timeshifts snapshot directory"
echo "-v, --verbose Let the log of the daemon be more verbose" echo "-v, --verbose Let the log of the daemon be more verbose"
echo "-h, --help Display this message" echo "-h, --help Display this message"
echo echo
echo "Version ${GRUB_BTRFS_VERSION}${RESET}" echo "Version ${GRUB_BTRFS_VERSION}${RESET}"
} }
log() { log() {
echo "${2}"$1"${RESET}" echo "${2}"$1"${RESET}"
if [ ${syslog} = true ]; then if [ ${syslog} = true ]; then
logger -p user.notice -t ${0##*/}"["$$"]" "$1" logger -p user.notice -t ${0##*/}"["$$"]" "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) ${1}" >> "${logfile}" echo "$(date) ${1}" >> "${logfile}"
fi fi
} }
vlog() { vlog() {
if [ ${verbose} = true ]; then if [ ${verbose} = true ]; then
echo "${2}"$1"${RESET}" echo "${2}"$1"${RESET}"
if [ ${syslog} = true ]; then if [ ${syslog} = true ]; then
logger -p user.notice -t ${0##*/} "$1" logger -p user.notice -t ${0##*/} "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) ${1}" >> "${logfile}" echo "$(date) ${1}" >> "${logfile}"
fi fi
fi fi
} }
err() { err() {
echo "${2}"${1}"${RESET}" >&2 echo "${2}"${1}"${RESET}" >&2
if [ ${syslog} = true ]; then if [ ${syslog} = true ]; then
logger -p user.error -t ${0##*/} "$1" logger -p user.error -t ${0##*/} "$1"
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
echo "$(date) error: ${1}" >> "${logfile}" echo "$(date) error: ${1}" >> "${logfile}"
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 exit 1
;; ;;
esac esac
done done
shift $(( OPTIND - 1 )) shift $(( OPTIND - 1 ))
@@ -167,32 +178,32 @@ snapshots="${1}"
# check if inotify exists, see issue #227 # check if inotify exists, see issue #227
if ! command -v inotifywait >/dev/null 2>&1; then if ! command -v inotifywait >/dev/null 2>&1; then
err "[!] inotifywait was not found, exiting. Is inotify-tools installed?" "${RED}" >&2 err "[!] inotifywait was not found, exiting. Is inotify-tools installed?" "${RED}" >&2
exit 1 exit 1
fi fi
if [ ${#logfile} -gt 1 ]; then if [ ${#logfile} -gt 1 ]; then
touch "${logfile}" touch "${logfile}"
echo "GRUB-BTRFSD log $(date)" >> "${logfile}" echo "GRUB-BTRFSD log $(date)" >> "${logfile}"
fi fi
log "grub-btrfsd starting up..." "${GREEN}" log "grub-btrfsd starting up..." "${GREEN}"
if [ ${verbose} = true ]; then if [ ${verbose} = true ]; then
inotify_qiet_flag="" inotify_qiet_flag=""
else else
inotify_qiet_flag=" -q -q " inotify_qiet_flag=" -q -q "
fi fi
if [ ${recursive} = true ]; then if [ ${recursive} = true ]; then
inotify_recursive_flag=" -r " inotify_recursive_flag=" -r "
else else
inotify_recursive_flag="" inotify_recursive_flag=""
fi fi
if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then if [ ${timeshift_auto} = false ] && [ ${timeshift_old} = true ]; then
err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2 err "[!] Flag --timeshift-old requires flag --timeshift-auto" "${RED}" >&2
exit 1 exit 1
fi fi
vlog "Arguments:" vlog "Arguments:"
@@ -203,14 +214,14 @@ vlog "Logfile: $logfile"
vlog "Recursive: $recursive" vlog "Recursive: $recursive"
if ! [ -d "$snapshots" ] && ! [ ${timeshift_auto} = true ]; then if ! [ -d "$snapshots" ] && ! [ ${timeshift_auto} = true ]; then
err "[!] No directory found at ${snapshots}" "${RED}" >&2 err "[!] No directory found at ${snapshots}" "${RED}" >&2
err "[!] Please specify a valid snapshot directory" "${RED}" >&2 err "[!] Please specify a valid snapshot directory" "${RED}" >&2
exit 1 exit 1
fi fi
if [ ${timeshift_auto} = true ]; then if [ ${timeshift_auto} = true ]; then
watchtime=15 watchtime=15
[ -d /run/timeshift ] || mkdir /run/timeshift [ -d /run/timeshift ] || mkdir /run/timeshift
else else
watchtime=0 watchtime=0
fi fi
@@ -219,17 +230,17 @@ 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
# and gives feedback if it worked # and gives feedback if it worked
if grep "snapshots-btrfs" "{grub_directory}/grub.cfg"; then if grep "snapshots-btrfs" "{grub_directory}/grub.cfg"; then
if /etc/grub.d/41_snapshots-btrfs; then if /etc/grub.d/41_snapshots-btrfs; then
log "Grub submenu recreated" "${GREEN}" log "Grub submenu recreated" "${GREEN}"
else else
err "[!] Error during grub submenu creation (grub-btrfs error)" "${RED}" err "[!] Error during grub submenu creation (grub-btrfs error)" "${RED}"
fi fi
else else
if ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o "${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}"/grub.cfg; then if ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o "${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}"/grub.cfg; then
log "Grub menu recreated" "${GREEN}" log "Grub menu recreated" "${GREEN}"
else else
err "[!] Error during grub menu creation (grub/ grub-btrfs error)" "${RED}" err "[!] Error during grub menu creation (grub/ grub-btrfs error)" "${RED}"
fi fi
fi fi
} }