diff --git a/grub-btrfsd b/grub-btrfsd index abf2b04..ae471f8 100755 --- a/grub-btrfsd +++ b/grub-btrfsd @@ -9,6 +9,8 @@ watchtime=0 logfile=0 snapshots=-1 timeshift_auto=false +verbose=false +syslog=false # helper functions GREEN=$'\033[0;32m' @@ -18,26 +20,52 @@ RESET=$'\033[0m' print_help() { echo "${CYAN}[?] Usage:" - echo "${0##*/} [-h, --help] [-t, --timeshift-auto] [-l, --log-file LOG_FILE] SNAPSHOTS_DIR" + echo "${0##*/} [-h, --help] [-t, --timeshift-auto] [-l, --log-file LOG_FILE] [-v, --verbose] [-s, --syslog] SNAPSHOTS_DIR" echo echo "SNAPSHOTS_DIR Snapshot directory to watch, without effect when --timeshift-auto" echo echo "Optional arguments:" echo "-t, --timeshift-auto Automatically detect Timeshifts snapshot directory" echo "-l, --log-file Specify a logfile to write to" + echo "-v, --verbose Let the log of the daemon be more verbose" + echo "-s, --syslog Write to syslog" echo "-h, --help Display this message${RESET}" } log() { + echo "${2}"$1"${RESET}" + if [ ${syslog} = true ]; then + logger -p user.notice -t ${0##*/}"["$$"]" "$1" + fi if [ ${#logfile} -gt 1 ]; then - echo "$(date) ${1}" | tee -a ${logfile} - else - echo "$(date) ${1}" + echo "$(date) ${1}" >> ${logfile} fi } +vlog() { + if [ ${verbose} = true ]; then + echo "${2}"$1"${RESET}" + if [ ${syslog} = true ]; then + logger -p user.notice -t ${0##*/} "$1" + fi + if [ ${#logfile} -gt 1 ]; then + echo "$(date) ${1}" >> ${logfile} + fi + fi +} + +err() { + echo "${2}"${1}"${RESET}" >&2 + if [ ${syslog} = true ]; then + logger -p user.error -t ${0##*/} "$1" + fi + if [ ${#logfile} -gt 1 ]; then + echo "$(date) error: ${1}" >> ${logfile} + fi +} + # parse arguments -while getopts :l:th-: opt; do +while getopts :l:tvsh-: opt; do case "$opt" in -) case "${OPTARG}" in @@ -47,9 +75,15 @@ while getopts :l:th-: opt; do timeshift-auto) timeshift_auto=true ;; + verbose) + verbose=true + ;; + syslog) + syslog=true + ;; *) if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then - echo "${RED}[!] Unknown option --${OPTARG} ${RESET}" >&2 + err "[!] Unknown option --${OPTARG}" "${RED}" >&2 echo fi print_help @@ -62,13 +96,19 @@ while getopts :l:th-: opt; do t) timeshift_auto=true ;; + v) + verbose=true + ;; + s) + syslog=true + ;; h) print_help exit 0 ;; *) if [ "$OPTERR" = 1 ] || [ "${optspec:0:1}" = ":" ]; then - echo "${RED}[!] Non-option argument: '-${OPTARG}'${RESET}" >&2 + err "[!] Non-option argument: '-${OPTARG}'" "${RED}" >&2 echo fi print_help @@ -81,19 +121,26 @@ shift $(( OPTIND - 1 )) snapshots="${1}" if [ ${#logfile} -gt 1 ]; then - echo "${GREEN}$(date) grub-btrfsd starting up...${RESET}" | tee ${logfile} -else - echo "${GREEN}$(date) grub-btrfsd starting up...${RESET}" + touch "${logfile}" + echo "GRUB-BTRFSD log $(date)" >> "${logfile}" fi -log "Arguments:" -log "Snapshot directory: $snapshots" -log "Timestift autodetection: $timeshift_auto" -log "Logfile: $logfile" +log "grub-btrfsd starting up..." "${GREEN}" + +if [ ${verbose} = true ]; then + inotify_qiet_flag="" +else + inotify_qiet_flag=" -q -q " +fi + +vlog "Arguments:" +vlog "Snapshot directory: $snapshots" +vlog "Timestift autodetection: $timeshift_auto" +vlog "Logfile: $logfile" if ! [ -d "$snapshots" ] && ! [ ${timeshift_auto} = true ]; then - log "${RED}[!] No directory found at ${snapshots} ${RESET}" >&2 - log "${RED}[!] Please specify a valid snapshot directory ${RESET}" >&2 + err "[!] No directory found at ${snapshots}" "${RED}" >&2 + err "[!] Please specify a valid snapshot directory" "${RED}" >&2 exit 1 fi @@ -104,9 +151,10 @@ else fi # start the actual daemon -log "Snapshot dir watchtimeout: $watchtime" -log "${GREEN}Entering infinite while${RESET}" +vlog "Snapshot dir watchtimeout: $watchtime" +vlog "Entering infinite while" "${GREEN}" while true; do + runs=false if [ ${timeshift_auto} = true ] && ! [ "${timeshift_pid}" -gt 0 ] ; then # watch the timeshift folder for a folder that is created when timeshift starts up if [ "${timeshift_pid}" -eq -2 ]; then @@ -116,32 +164,39 @@ while true; do if [ "${#timeshift_pid}" -gt 0 ]; then snapshots="/run/timeshift/${timeshift_pid}/backup/timeshift-btrfs/snapshots" log "detected running Timeshift at daemon startup, PID is: $timeshift_pid" - log "new snapshots directory is $snapshots" + vlog "new snapshots directory is $snapshots" else log "Watching /run/timeshift for timeshift to start" - inotifywait -q -q -e create -e delete /run/timeshift && { - sleep 1 + inotifywait ${inotify_qiet_flag} -e create -e delete /run/timeshift && { + sleep 3 timeshift_pid=$(ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /timeshift/ {print $1}') snapshots="/run/timeshift/${timeshift_pid}/backup/timeshift-btrfs/snapshots" - log "${CYAN}detected Timeshift startup, PID is: $timeshift_pid ${RESET}" - log "${CYAN}new snapshots directory is $snapshots ${RESET}" + log "detected Timeshift startup, PID is: $timeshift_pid" "${CYAN}" + vlog "new snapshots directory is $snapshots" "${CYAN}" } fi + runs=false else while [ -d "$snapshots" ]; do # watch the actual snapshots folder for a new snapshot or a deletion of a snapshot - log "Watching $snapshots for new snapshots..." - sleep 1 - inotifywait -q -q -e create -e delete -e unmount -t "$watchtime" "$snapshots" && { - log "${CYAN}Detected snapshot creation/ deletion, (re)creating grub menu${RESET}" + if [ ${runs} = false ] && [ ${verbose} = false ]; then + log "Watching $snapshots for new snapshots..." "${CYAN}" + else + vlog "Watching $snapshots for new snapshots..." "${CYAN}" + fi + runs=true + inotifywait ${inotify_qiet_flag} -e create -e delete -e unmount -t "$watchtime" "$snapshots" && { + log "Detected snapshot creation/ deletion, recreating Grub menu" "${CYAN}" sleep 5 if [ -s "${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}/grub-btrfs.cfg" ]; then /etc/grub.d/41_snapshots-btrfs + log "Grub submenu recreated" "${GREEN}" else ${GRUB_BTRFS_MKCONFIG:-grub-mkconfig} -o ${GRUB_BTRFS_GRUB_DIRNAME:-/boot/grub}/grub.cfg + log "Grub menu recreated" "${GREEN}" fi - log "${GREEN}Grub menu created${RESET}" } + sleep 1 done timeshift_pid=-2 fi diff --git a/grub-btrfsd.confd b/grub-btrfsd.confd index d58ff77..7d12ca1 100644 --- a/grub-btrfsd.confd +++ b/grub-btrfsd.confd @@ -9,5 +9,10 @@ snapshots="/.snapshots" # Snapper in the root directory # Possible options are: # -t, --timeshift-auto Automatically detect Timeshifts snapshot directory for timeshift >= 22.06 # -l, --log-file Specify a logfile to write to +# -v, --verbose Let the log of the daemon be more verbose +# -s, --syslog Write to syslog +# Uncomment the line to activate the option +optional_args+="--syslog " # write to syslog by default #optional_args+="--timeshift-auto " #optional_args+="--log-file /var/log/grub-btrfsd.log " +#optional_args+="--verbose " diff --git a/grub-btrfsd.service b/grub-btrfsd.service index 4fe7ee7..b567c00 100644 --- a/grub-btrfsd.service +++ b/grub-btrfsd.service @@ -13,6 +13,9 @@ EnvironmentFile=/etc/default/grub-btrfs/config # Optional arguments: # -t, --timeshift-auto Automatically detect Timeshifts snapshot directory # -l, --log-file Specify a logfile to write to +# -v, --verbose Let the log of the daemon be more verbose +# -s, --syslog Write to syslog, this usually not needed for systemd systems, because systemd +# is logging service output to the syslog anyway ExecStart=/usr/bin/grub-btrfsd /.snapshots [Install] diff --git a/manpages/grub-btrfsd.8.man b/manpages/grub-btrfsd.8.man index d1bf4cf..9158d62 100644 --- a/manpages/grub-btrfsd.8.man +++ b/manpages/grub-btrfsd.8.man @@ -9,7 +9,7 @@ when a new btrfs snapshot is created. .SH "SYNOPSIS" .PP -\fCgrub\-btrfsd [\-h, \-\-help] [\-t, \-\-timeshift\-auto] [\-l, \-\-log\-file LOG_FILE] SNAPSHOTS_DIR\fP +\fCgrub\-btrfsd [\-h, \-\-help] [\-t, \-\-timeshift\-auto] [\-l, \-\-log\-file LOG_FILE] [\-v, \-\-verbose] [\-s, \-\-syslog] SNAPSHOTS_DIR\fP .SH "DESCRIPTION" .PP @@ -26,7 +26,7 @@ E.g. for Snapper this would be \fC/.snapshots\fP .PP This is a flag to activate the auto detection of the path where Timeshift stores snapshots. Newer versions (>=22.06) of Timeshift mount their snapshots to \fC/run/timeshift/$PID/backup/timeshift\-btrfs\fP. Where \fC$PID\fP is the process ID of the currently running Timeshift session. The PID is changing every time Timeshift is opened. grub-btrfsd can automatically take care of the detection of the correct PID and directory if this flag is set. In this case the argument \fCSNAPSHOTS_DIR\fP has no effect. -.SS "\fC\-l /\-\-log\-file\fP" +.SS "\fC\-l / \-\-log\-file\fP" .PP This arguments specifies a file where grub-btrfsd should write log messages. @@ -34,6 +34,14 @@ This arguments specifies a file where grub-btrfsd should write log messages. .PP Displays a short help message. +.SS "\fC\-v / \-\-verbose\fP" +.PP +Let the log of the daemon be more verbose + +.SS "\fC\-s / \-\-syslog\fP" +.PP +Write to syslog + .SH "CONFIGURATION" .PP The daemon is usually configured via the file \fC/etc/conf.d/grub\-btrfsd\fP on openrc-init systems and \fCsudo systemctl edit \-\-full grub\-btrfsd\fP on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured. diff --git a/manpages/grub-btrfsd.8.org b/manpages/grub-btrfsd.8.org index 94776b2..c6521fa 100644 --- a/manpages/grub-btrfsd.8.org +++ b/manpages/grub-btrfsd.8.org @@ -10,7 +10,7 @@ grub-btrfsd - An OpenRC daemon to automatically update the grub menu with when a new btrfs snapshot is created. * SYNOPSIS -~grub-btrfsd [-h, --help] [-t, --timeshift-auto] [-l, --log-file LOG_FILE] SNAPSHOTS_DIR~ +~grub-btrfsd [-h, --help] [-t, --timeshift-auto] [-l, --log-file LOG_FILE] [-v, --verbose] [-s, --syslog] SNAPSHOTS_DIR~ * DESCRIPTION Grub-btrfs-openrc is a shell script which is meant to be run as a daemon. @@ -24,12 +24,18 @@ E.g. for Snapper this would be ~/.snapshots~ ** ~-t / --timeshift-auto~ This is a flag to activate the auto detection of the path where Timeshift stores snapshots. Newer versions (>=22.06) of Timeshift mount their snapshots to ~/run/timeshift/$PID/backup/timeshift-btrfs~. Where ~$PID~ is the process ID of the currently running Timeshift session. The PID is changing every time Timeshift is opened. grub-btrfsd can automatically take care of the detection of the correct PID and directory if this flag is set. In this case the argument ~SNAPSHOTS_DIR~ has no effect. -** ~-l /--log-file~ +** ~-l / --log-file~ This arguments specifies a file where grub-btrfsd should write log messages. ** ~-h / --help~ Displays a short help message. +** ~-v / --verbose~ +Let the log of the daemon be more verbose + +** ~-s / --syslog~ +Write to syslog + * CONFIGURATION The daemon is usually configured via the file ~/etc/conf.d/grub-btrfsd~ on openrc-init systems and ~sudo systemctl edit --full grub-btrfsd~ on systemd systems. In this file the arguments (See OPTIONS), that OpenRC passes to the daemon when it is started, can be configured.