mirror of
https://github.com/Antynea/grub-btrfs.git
synced 2026-10-20 02:42:03 +08:00
grub-btrfsd: Start subdaemon when --timeshift-auto
Fix #272 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-05T11:40:40+00:00
|
GRUB_BTRFS_VERSION=4.12-master-2023-04-05T15:09:30+00:00
|
||||||
|
|
||||||
# Disable grub-btrfs.
|
# Disable grub-btrfs.
|
||||||
# Default: "false"
|
# Default: "false"
|
||||||
|
|||||||
+61
-57
@@ -219,7 +219,7 @@ 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}"
|
||||||
@@ -238,73 +238,77 @@ set_snapshot_dir() {
|
|||||||
if [ "${timeshift_old}" = true ]; then
|
if [ "${timeshift_old}" = true ]; then
|
||||||
snapdir="/run/timeshift/backup/timeshift-btrfs/snapshots"
|
snapdir="/run/timeshift/backup/timeshift-btrfs/snapshots"
|
||||||
else
|
else
|
||||||
snapdir="/run/timeshift/${timeshift_pid}/backup/timeshift-btrfs/snapshots"
|
snapdir="/run/timeshift/${timeshift_pid}/backup/timeshift-btrfs/snapshots"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
daemon_function() {
|
daemon_function() {
|
||||||
|
trap 'vlog "$BASHPID: Received SIGINT/ SIGTERM"; exit 0' SIGINT SIGTERM
|
||||||
# start the actual daemon
|
# start the actual daemon
|
||||||
snapdir=$1
|
snapdir=$1
|
||||||
vlog "Snapshot dir watchtimeout: $watchtime"
|
vlog "Subdaemon function started, PID: $BASHPID" "${GREEN}"
|
||||||
vlog "Entering infinite while for $snapdir" "${GREEN}"
|
vlog "${BASHPID}: Entering infinite while for $snapdir" "${GREEN}"
|
||||||
|
vlog "${BASHPID}: Snapshot dir watchtimeout: $watchtime"
|
||||||
while true; do
|
while true; do
|
||||||
runs=false
|
runs=false
|
||||||
if [ ${timeshift_auto} = true ] && ! [ "${timeshift_pid}" -gt 0 ] ; then
|
if [ ${timeshift_auto} = true ] && ! [ "${timeshift_pid}" -gt 0 ] ; then
|
||||||
# watch the timeshift folder for a folder that is created when timeshift starts up
|
# watch the timeshift folder for a folder that is created when timeshift starts up
|
||||||
sleep 1 # for safety so the outer while is not going crazy
|
sleep 1 # for safety so the outer while is not going crazy
|
||||||
if [ "${timeshift_pid}" -eq -2 ]; then
|
if [ "${timeshift_pid}" -eq -2 ]; then
|
||||||
log "detected timeshift shutdown"
|
log "${BASHPID}: detected timeshift shutdown"
|
||||||
fi
|
fi
|
||||||
timeshift_pid=$(ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /timeshift/ {print $1}')
|
timeshift_pid=$(ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /timeshift/ {print $1}')
|
||||||
if [ "${#timeshift_pid}" -gt 0 ]; then
|
if [ "${#timeshift_pid}" -gt 0 ]; then
|
||||||
set_snapshot_dir
|
|
||||||
log "detected running Timeshift at daemon startup, PID is: $timeshift_pid"
|
|
||||||
vlog "new snapshots directory is $snapdir"
|
|
||||||
else
|
|
||||||
log "Watching /run/timeshift for timeshift to start"
|
|
||||||
inotifywait ${inotify_qiet_flag} -e create -e delete /run/timeshift && {
|
|
||||||
sleep 1
|
|
||||||
timeshift_pid=$(ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /timeshift/ {print $1}')
|
|
||||||
set_snapshot_dir
|
set_snapshot_dir
|
||||||
log "detected Timeshift startup, PID is: $timeshift_pid" "${CYAN}"
|
log "${BASHPID}: detected running Timeshift at daemon startup, PID is: $timeshift_pid"
|
||||||
vlog "new snapshots directory is $snapdir" "${CYAN}"
|
vlog "${BASHPID}: new snapshots directory is $snapdir"
|
||||||
(create_grub_menu) # create the grub menu once immidiatly in a forking process. Snapshots from commandline using timeshift --create need this
|
else
|
||||||
}
|
log "Watching /run/timeshift for timeshift to start"
|
||||||
fi
|
inotifywait ${inotify_qiet_flag} -e create -e delete /run/timeshift && {
|
||||||
runs=false
|
sleep 1
|
||||||
else
|
timeshift_pid=$(ps ax | awk '{sub(/.*\//, "", $5)} $5 ~ /timeshift/ {print $1}')
|
||||||
while [ -d "$snapdir" ]; do
|
set_snapshot_dir
|
||||||
# watch the actual snapshots folder for a new snapshot or a deletion of a snapshot
|
log "${BASHPID}: detected Timeshift startup, PID is: $timeshift_pid" "${CYAN}"
|
||||||
if [ ${runs} = false ] && [ ${verbose} = false ]; then
|
vlog "${BASHPID}: new snapshots directory is $snapdir" "${CYAN}"
|
||||||
log "Watching $snapdir for new snapshots..." "${CYAN}"
|
(create_grub_menu) # create the grub menu once immidiatly in a forking process. Snapshots from commandline using timeshift --create need this
|
||||||
else
|
}
|
||||||
vlog "Watching $snapdir for new snapshots..." "${CYAN}"
|
fi
|
||||||
fi
|
runs=false
|
||||||
runs=true
|
else
|
||||||
inotifywait ${inotify_qiet_flag} ${inotify_recursive_flag} -e create -e delete -e unmount -t "$watchtime" "$snapdir" && {
|
while [ -d "$snapdir" ]; do
|
||||||
log "Detected snapshot creation/ deletion, recreating Grub menu" "${CYAN}"
|
# watch the actual snapshots folder for a new snapshot or a deletion of a snapshot
|
||||||
sleep 5
|
if [ ${runs} = false ] && [ ${verbose} = false ]; then
|
||||||
create_grub_menu
|
log "${BASHPID}: Watching $snapdir for new snapshots..." "${CYAN}"
|
||||||
}
|
else
|
||||||
sleep 1
|
vlog "${BASHPID}: Watching $snapdir for new snapshots..." "${CYAN}"
|
||||||
done
|
fi
|
||||||
timeshift_pid=-2
|
runs=true
|
||||||
fi
|
inotifywait ${inotify_qiet_flag} ${inotify_recursive_flag} -e create -e delete -e unmount -t "$watchtime" "$snapdir" && {
|
||||||
if ! [ ${timeshift_auto} = true ] && ! [ -d "${snapdir}" ] ; then # in case someone deletes the snapshots folder (in snapper mode) to prevent the while loop from going wild
|
log "${BASHPID}: Detected snapshot creation/ deletion, recreating Grub menu" "${CYAN}"
|
||||||
break
|
sleep 5
|
||||||
fi
|
create_grub_menu
|
||||||
|
}
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
timeshift_pid=-2
|
||||||
|
fi
|
||||||
|
if ! [ ${timeshift_auto} = true ] && ! [ -d "${snapdir}" ] ; then # in case someone deletes the snapshots folder (in snapper mode) to prevent the while loop from going wild
|
||||||
|
break
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# for all dirs that got passed to the script, start a new fork with that dir
|
|
||||||
snapdirs=( "${@}" )
|
|
||||||
for snapdir in "${snapdirs[@]}"
|
|
||||||
do
|
|
||||||
vlog "starting daemon with watching $snapdir..."
|
|
||||||
daemon_function "${snapdir}" &
|
|
||||||
done
|
|
||||||
|
|
||||||
|
if [ ${timeshift_auto} = true ] ; then
|
||||||
|
daemon_function &
|
||||||
|
else
|
||||||
|
# for all dirs that got passed to the script, start a new fork with that dir
|
||||||
|
snapdirs=( "${@}" )
|
||||||
|
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
|
wait # wait for forks to finish, kill child forks if SIGTERM is sent
|
||||||
|
|
||||||
|
|
||||||
exit 0 # tradition is tradition
|
exit 0 # tradition is tradition
|
||||||
|
|||||||
Reference in New Issue
Block a user