-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpistopstart.in
379 lines (366 loc) · 9.2 KB
/
pistopstart.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/sh
#
# $Id: pistopstart.in,v 1.20 2024-09-12 08:55:42+05:30 Cprogrammer Exp mbhangui $
#
systemctl=$(which systemctl)
suicide()
{
echo "ARGH!!! Committing suicide. Going down on SIGTERM"
exit 1
}
get_mpd_conf_value()
{
if [ ! -f @sysconfdir@/mpd.conf ] ; then
return 1
fi
grep "^$1" @sysconfdir@/mpd.conf|awk '{print $2}' | \
sed -e 's{"{{g'
}
get_mdrive()
{
d1=$(grep -v '^#' @sysconfdir@/auto.master.d/mpd.autofs|awk '{print $1}')
d2=$(grep -v '^#' @sysconfdir@/auto.master.d/mpd.autofs|awk '{print $2}')
if [ -n "$d2" ] ; then
d3=$(grep -v '^#' $d2|awk '{print $1}')
echo "$d1/$d3"
fi
}
stop()
{
if [ -n "$POWER_OFF" ] ; then
echo "Shutting down $(uname -n) localtime $(date)" 1>&7
else
echo "stopping $(uname -n) localtime $(date)" 1>&7
fi
if [ $mpd_present -eq 1 ] ; then
count=1
while true
do
echo "Stopping mpd.service"
if [ -n $systemctl ] ; then
echo "$systemctl stop mpd.service mpd.socket"
$systemctl stop mpd.service mpd.socket
elif [ -d $servicedir/mpd ] ; then
echo "svc -d $servicedir/mpd"
svc -d $servicedir/mpd
else
break
fi
[ $? -eq 0 ] && break
printf "stop mpd: %2d: failed\n" $count
count=`expr $count + 1`
sleep 1
done
fi
if [ -n "$MDRIVE" ] ; then
count=1
if [ -x $HOME/.pistop/pre_unmount ] ; then
echo "Executing user command pre_unmount"
$HOME/.pistop/pre_unmount
fi
while true
do
if [ -n "$POWER_OFF" -a -n "$FORCE_UMOUNT" ] ; then
if [ $count -eq 1 ] ; then
echo "Force Unmounting MDrive with lazy option"
fi
[ $count -eq 1 ] && echo "umount -f -l $MDRIVE"
umount -f -l $MDRIVE >/dev/null 2>&1
else
if [ $count -eq 1 ] ; then
echo "Force Unmounting MDrive"
fi
[ $count -eq 1 ] && echo "umount -f -l $MDRIVE"
umount -f $MDRIVE >/dev/null 2>&1
fi
if [ $? -eq 0 -o $count -eq 60 ] ; then
if [ $? -eq 0 ] ; then
printf "umount: %2d: succeeded\n" $count
else
printf "WARNING!!! umount: %2d: threshold exceeded. Quitting without umount\n" $count
fi
break
fi
drive_mounted=$(df -k |grep MDrive)
if [ -z "$drive_mounted" ] ; then
echo "unmounted MDrive"
break
fi
printf "umount: %2d: failed\n" $count
count=`expr $count + 1`
sleep 1
done
if [ -x $HOME/.pistop/post_unmount ] ; then
echo "Executing user command post_unmount"
$HOME/.pistop/post_unmount
fi
fi
if [ -x $systemctl ] ; then
while true
do
echo "$systemctl stop autofs.service"
$systemctl stop autofs.service
if [ $? -eq 0 ] ; then
echo "stopped autofs"
break
fi
done
elif [ -x /bin/launchctl ] ; then
while true
do
echo "Stopping automountd"
launchctl stop automountd
if [ $? -eq 0 ] ; then
break
fi
done
fi
if [ -n "$POWER_OFF" ] ; then
@pistoplibexecdir@/host_status PowerOff
else
@pistoplibexecdir@/host_status MusicOff
fi
if [ -n "$POWER_OFF" ] ; then
echo "shutdown $(uname -n) localtime $(date)" 1>&7
echo "Completed STOP Service and shutdown in progress..."
shutdown -h now
else
echo "stopped $(uname -n) localtime $(date)" 1>&7
echo "Completed STOP Service"
fi
}
start_mpd()
{
mpd_up=0
if [ -x $systemctl ] ; then
echo "$systemctl status mpd.service"
$systemctl status mpd.service >/dev/null 2>&1
if [ $? -ne 0 ] ; then
# trigger autofs to mount MDrive
mpd_was_down=1
echo "$systemctl start mpd.service"
$systemctl start mpd.service
else
mpd_up=1
if [ $mpd_was_down -eq 1 ] ; then
echo "started music player daemon"
mpd_was_down=0
fi
fi
else
svstat $servicedir/mpd >/dev/null 2>&1
if [ $? -ne 0 ] ; then
# trigger autofs to mount MDrive
mpd_was_down=1
echo "starting music player daemon"
svc -u $servicedir/mpd
else
mpd_up=1
if [ $mpd_was_down -eq 1 ] ; then
echo "started music player daemon"
mpd_was_down=0
fi
fi
fi
[ $mpd_up -eq 1 ] && return 0 || return 1
}
give_up()
{
echo "Giving up..."
echo "svc -dx /service/fclient"
svc -dx /service/fclient
echo "$systemctl stop autofs"
$systemctl stop autofs
echo "umount $MDRIVE"
umount $MDRIVE
}
start()
{
mpd_up=0
autofs_up=0
count=1
mpd_was_down=0
autofs_was_down=0
mdrive_mounted=0
echo "Initializing $(uname -n) MDRIVE [$MDRIVE] localtime $(date)" 1>&7 # running under tcpclient
while true
do
# 1. we assume we are running on Linux mostly
# 2. If on mac/freebsd, we assume autofs has been setup
if [ -x $systemctl ] ; then
$systemctl status autofs.service >/dev/null 2>&1
if [ $? -ne 0 ] ; then
if [ $count -eq 60 ] ; then
give_up
break
fi
if [ $count -eq 0 -a -x $HOME/.pistop/pre_mount ] ; then
echo "Executing user command pre_mount"
$HOME/.pistop/pre_mount
fi
count=`expr $count + 1`
autofs_was_down=1
echo "$systemctl start autofs.service"
$systemctl start autofs.service
sleep 1
continue
else
autofs_up=1
if [ $autofs_was_down -eq 1 ] ; then
echo "started automount filesystems"
autofs_was_down=0
fi
fi
fi
if [ ! -f $MDRIVE/data/stats.db ] ; then
if [ $count -eq 60 ] ; then
give_up
break
fi
count=`expr $count + 1`
echo "MDrive [$MDRIVE] not yet mounted"
sleep 5
continue
fi
if [ $mdrive_mounted -eq 0 -a -x $HOME/.pistop/post_mount ] ; then
echo "Executing user command post_mount"
$HOME/.pistop/post_mount
fi
[ $mdrive_mounted -eq 0 ] && mdrive_mounted=1 && echo "MDrive mounted"
if [ $mpd_present -eq 1 ] ; then
start_mpd
[ $? -eq 0 ] && break
else
break
fi
[ $count -eq 60 ] && give_up && break
sleep 20
count=`expr $count + 1`
done
@pistoplibexecdir@/host_status load
[ -x $MDRIVE/bin/boot_sound ] && $MDRIVE/bin/boot_sound 2>/dev/null
if [ -n "DEST" -a -n "$BACKUP" -a -d $MDRIVE/backup ] ; then
echo "backing up configuraton"
@pistoplibexecdir@/backup
else
echo "Skipping backup of configuration"
fi
$systemctl list-unit-files sound.service >/dev/null 2>&1
if [ $? -eq 0 ] ; then
line=$(systemctl list-units --all sound.service|sed -n 2p)
if [ "$2" != "loaded" ] ; then
echo "$systemctl start sound.service"
$systemctl start sound.service
fi
else
echo "sound.service not needed to be loaded"
fi
echo "Initialized $(uname -n) localtime $(date)" 1>&7 # running under tcpclient
echo "Completed START Service"
}
## Main ###
trap suicide TERM INT
trap '' PIPE
if [ -n "$TCPREMOTEIP" ] ; then
if [ $# -gt 0 ] ; then
echo "Connected to $TCPREMOTEIP, executing $0 $*"
else
echo "Connected to $TCPREMOTEIP, executing $0"
fi
else
if [ $# -gt 0 ] ; then
echo "Connected to remote, executing $0 $*"
else
echo "Connected to remote, executing $0"
fi
fi
servicedir=@servicedir@
[ -f @sysconfdir@/mpd.conf -o -L @sysconfdir@/mpd.conf ] && mpd_present=1 || mpd_present=0
[ $mpd_present -eq 1 ] && MDRIVE=$(get_mdrive) || MDRIVE=""
if [ -z "$MDRIVE" ] ; then
if [ -d /MDrive ] ; then
MDRIVE=/MDrive
elif [ -d /autofs/MDrive ] ; then
MDRIVE=/autofs/MDrive
elif [ -d /var/lib/mpd/MDrive/backup ] ; then
MDRIVE=/var/lib/mpd/MDrive
elif [ -d /home/pi/MDrive/backup ] ; then
MDRIVE=/home/pi/MDrive
fi
fi
[ -n "$MDRIVE" ] && echo "MDrive is $MDRIVE" || echo "MDrive not found"
case "$1" in
start)
start
;;
stop)
stop
;;
sleep)
sleep 20
;;
esac
#
# $Log: pistopstart.in,v $
# Revision 1.20 2024-09-12 08:55:42+05:30 Cprogrammer
# added pre_unmount, post_unmount, pre_mount, post_mount feature
#
# Revision 1.19 2024-07-29 08:40:41+05:30 Cprogrammer
# get MDRIVE from /etc/auto.master.d/mpd.autofs and /etc/autofs.mounts
#
# Revision 1.18 2024-04-28 12:32:14+05:30 Cprogrammer
# added more info statements
#
# Revision 1.17 2024-04-21 23:12:32+05:30 Cprogrammer
# send PowerOff, MusicOff notification as desktop notification
#
# Revision 1.16 2024-03-27 09:44:56+05:30 Cprogrammer
# ignore sigpipe
# umount MDrive without triggering autof
#
# Revision 1.15 2024-03-21 23:40:03+05:30 Cprogrammer
# display umount method in logs
#
# Revision 1.14 2024-03-15 12:11:01+05:30 Cprogrammer
# modified for config without mpd
#
# Revision 1.13 2023-07-04 16:12:54+05:30 Cprogrammer
# use umount -f -l to prevent umount hanging
#
# Revision 1.12 2023-06-30 10:08:09+05:30 Cprogrammer
# send text to LCD display using host_status script
#
# Revision 1.11 2023-06-29 22:45:10+05:30 Cprogrammer
# updated text message sent to lcdhost
#
# Revision 1.10 2023-06-29 10:30:07+05:30 Cprogrammer
# use LCD_HOST if defined instead of lcdhost in /etc/hosts
#
# Revision 1.9 2023-06-28 23:23:18+05:30 Cprogrammer
# use timeout for nc command
#
# Revision 1.8 2023-06-28 21:35:40+05:30 Cprogrammer
# use /proc/loadavg instead of uptime command for consistent output
#
# Revision 1.7 2023-06-28 12:34:27+05:30 Cprogrammer
# send startup/shutdown text to lcdhost
#
# Revision 1.6 2023-06-26 19:53:16+05:30 Cprogrammer
# Use MDRIVE from variables if not from /etc/mpd.conf
#
# Revision 1.5 2023-05-18 08:16:54+05:30 Cprogrammer
# increased duration of sleep if disk mount fails
#
# Revision 1.4 2023-05-03 16:36:46+05:30 Cprogrammer
# removed hardcoded systemctl path
#
# Revision 1.3 2022-12-27 10:36:13+05:30 Cprogrammer
# added debug log
#
# Revision 1.2 2022-07-13 17:16:29+05:30 Cprogrammer
# fixed power off when fserver service is shutdown
#
# Revision 1.1 2022-07-12 17:32:59+05:30 Cprogrammer
# Initial revision
#
#