Debian Codegraph @

btrfsmaintenance-refresh-cron.sh

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/bin/bash
2 #
3 # Copyright (c) 2017 SUSE LINUX GmbH, Nuernberg, Germany.
4
5 # Adjust symlinks of btrfs maintenance services according to the configs.
6 # Run with 'uninstall' to remove them again
7
8 umask 022
9 PATH=/sbin:/bin:/usr/sbin:/usr/bin
10 export PATH
11
12 SCRIPTS=/usr/share/btrfsmaintenance
13
14 if [ -f /etc/sysconfig/btrfsmaintenance ]; then
15 . /etc/sysconfig/btrfsmaintenance
16 fi
17
18 if [ -f /etc/default/btrfsmaintenance ]; then
19 . /etc/default/btrfsmaintenance
20 fi
21
22 case "$1" in
23 cron)
24 BTRFS_TIMER_IMPLEMENTATION="cron"
25 shift
26 ;;
27 systemd-timer|timer)
28 BTRFS_TIMER_IMPLEMENTATION="systemd-timer"
29 shift
30 ;;
31 esac
32
33 refresh_cron() {
34 local EXPECTED="$1"
35 local SCRIPT="$2"
36 local VALID=false
37 local PERIOD
38 local LINK
39 local FILE
40
41 echo "Refresh script $SCRIPT for $EXPECTED"
42
43 for PERIOD in daily weekly monthly none uninstall; do
44 if [ "$PERIOD" = "$EXPECTED" ]; then
45 VALID=true
46 fi
47 done
48
49 if ! $VALID; then
50 echo "$EXPECTED is not a valid period for cron. Not changing."
51 return
52 fi
53
54 for PERIOD in daily weekly monthly; do
55 # NOTE: debian does not allow filenames with dots in /etc/cron.*
56 LINK="${SCRIPT%.*}"
57 FILE="/etc/cron.$PERIOD/$LINK"
58 if [ "$PERIOD" = "$EXPECTED" ]; then
59 ln -sf "$SCRIPTS/$SCRIPT" "$FILE"
60 else
61 rm -f "$FILE"
62 fi
63 done
64 }
65
66 refresh_timer() {
67 local PERIOD="$1"
68 local SERVICE="$2"
69
70 echo "Refresh timer $SERVICE for $PERIOD"
71
72 case "$PERIOD" in
73 uninstall|none)
74 systemctl stop "$SERVICE".timer &> /dev/null
75 systemctl disable "$SERVICE".timer &> /dev/null
76 rm -rf /etc/systemd/system/"$SERVICE".timer.d
77 ;;
78 *)
79 mkdir -p /etc/systemd/system/"$SERVICE".timer.d/
80 cat << EOF > /etc/systemd/system/"$SERVICE".timer.d/schedule.conf
81 [Timer]
82 OnCalendar=
83 OnCalendar=$PERIOD
84 EOF
85 systemctl enable "$SERVICE".timer &> /dev/null
86 systemctl start "$SERVICE".timer &> /dev/null
87 ;;
88 esac
89 }
90
91 if [ "$1" = 'uninstall' ]; then
92 for SCRIPT in btrfs-scrub btrfs-defrag btrfs-balance btrfs-trim; do
93 case "$BTRFS_TIMER_IMPLEMENTATION" in
94 systemd-timer)
95 refresh_timer uninstall "${SCRIPT}"
96 ;;
97 *)
98 refresh_cron uninstall "${SCRIPT}.sh"
99 ;;
100 esac
101 done
102 exit 0
103 fi
104
105 case "$BTRFS_TIMER_IMPLEMENTATION" in
106 systemd-timer)
107 # Deinstall cron jobs, don't run it twice.
108 for SCRIPT in btrfs-scrub btrfs-defrag btrfs-balance btrfs-trim; do
109 refresh_cron uninstall "${SCRIPT}.sh"
110 done
111 refresh_timer "$BTRFS_SCRUB_PERIOD" btrfs-scrub
112 refresh_timer "$BTRFS_DEFRAG_PERIOD" btrfs-defrag
113 refresh_timer "$BTRFS_BALANCE_PERIOD" btrfs-balance
114 refresh_timer "$BTRFS_TRIM_PERIOD" btrfs-trim
115 ;;
116 *)
117 refresh_cron "$BTRFS_SCRUB_PERIOD" btrfs-scrub.sh
118 refresh_cron "$BTRFS_DEFRAG_PERIOD" btrfs-defrag.sh
119 refresh_cron "$BTRFS_BALANCE_PERIOD" btrfs-balance.sh
120 refresh_cron "$BTRFS_TRIM_PERIOD" btrfs-trim.sh
121 ;;
122 esac
123