Skip to content

Commit 3960810

Browse files
committed
It works completely now
Small refactor to fix unmounting at shutdown. AutoMountNFS now mounts/unmounts nfs shares when the host system is available/unavailable and also does a check every minute. When the network is removed at shutwodn, the nfs mounts are unmounted before restart. This prevents a minute and a half delay.
1 parent 7a3eb57 commit 3960810

6 files changed

+102
-42
lines changed

Diff for: 10-AutoMountNFS

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if [ -e /sys/fs/cgroup/systemd ]; then
66
systemctl start AutoMountNFS.service
77
;;
88
down)
9-
systemctl start AutoMountNFS.service
9+
systemctl stop AutoMountNFS.service
1010
;;
1111
esac
1212
fi

Diff for: AutoMountNFS-check.service

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=Service for AutoMountNFS check timer
3+
After=network.target
4+
Requires=NetworkManager-dispatcher.service network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/bin/automountnfs check

Diff for: AutoMountNFS.timer renamed to AutoMountNFS-check.timer

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Description=Checks NFS mounts every minute.
55
OnCalendar=minutely
66

77
[Install]
8-
WantedBy=timers.target
8+
WantedBy=timers.target AutoMountNFS.service

Diff for: AutoMountNFS.service

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[Unit]
22
Description=Auto Mount NFS Shares
33
After=network.target
4-
Requires=NetworkManager-dispatcher.service
4+
Requires=NetworkManager-dispatcher.service network-online.target
55

66
[Service]
7-
Type=simple
8-
ExecStart=/usr/bin/automountnfs
9-
ExecStop=/usr/bin/automountnfs
7+
Type=oneshot
8+
RemainAfterExit=yes
9+
ExecStart=/usr/bin/automountnfs check
10+
ExecStop=/usr/bin/automountnfs unmount
1011

1112
[Install]
1213
WantedBy=multi-user.target
14+
Also=AutoMountNFS-check.timer

Diff for: PKGBUILD

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Maintainer: Jesus Alvarez <[email protected]>
22
pkgname=automountnfs
3-
pkgver=0.5.5
3+
pkgver=0.6.7
44
pkgrel=1
55
pkgdesc="Simple NFS mount handling for wireless hosts."
66
arch=('any')
@@ -10,16 +10,19 @@ depends=('networkmanager')
1010
source=('10-AutoMountNFS'
1111
'automountnfs'
1212
'AutoMountNFS.service'
13-
'AutoMountNFS.timer')
14-
md5sums=('3dbfe5d654075bc4a61d0870aad066f9'
15-
'c0ae6e0f7a3507b4b5b179bdd00d0b4f'
16-
'6a211a227fa5ba65ae966656586761b8'
17-
'160a396c51e46660abe7a636698909d4')
13+
'AutoMountNFS-check.service'
14+
'AutoMountNFS-check.timer')
15+
md5sums=('d24dcc16e952e079cce8fbc0194d0531'
16+
'2076fcf09820a50e26bb56ff960e9b24'
17+
'e7c165e85de84a9be7a982932ffc4132'
18+
'a8b53727cb514228d1738dd3d25ecfbe'
19+
'385306da45c700ea82e5390274088cf0')
1820

1921
package() {
2022
cd "${srcdir}"
2123
install -Dm755 automountnfs "${pkgdir}/usr/bin/automountnfs"
2224
install -Dm644 AutoMountNFS.service "${pkgdir}/usr/lib/systemd/system/AutoMountNFS.service"
23-
install -Dm644 AutoMountNFS.timer "${pkgdir}/usr/lib/systemd/system/AutoMountNFS.timer"
25+
install -Dm644 AutoMountNFS-check.service "${pkgdir}/usr/lib/systemd/system/AutoMountNFS-check.service"
26+
install -Dm644 AutoMountNFS-check.timer "${pkgdir}/usr/lib/systemd/system/AutoMountNFS-check.timer"
2427
install -Dm700 10-AutoMountNFS "${pkgdir}/etc/NetworkManager/dispatcher.d/10-AutoMountNFS"
2528
}

Diff for: automountnfs

+76-29
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/bin/bash
22

3-
SERVER_FQDN="lithium.alvaone.net"
4-
TEMP_LOG="/run/automountnfs"
5-
DATE_OPTS="%a %b %d %H:%M %Y"
3+
AMN_SERVER_FQDN="lithium.alvaone.net"
4+
AMN_TEMP_LOG="/var/log/automountnfs"
5+
AMN_DATE_OPTS="%a %b %d %H:%M %Y"
6+
AMN_CHECK=0
7+
AMN_UNMOUNT=0
68

79
trap 'trap_abort' INT QUIT TERM HUP
810
trap 'trap_exit' EXIT
911

1012
cleanup() {
11-
loge "Cleanup: $1"
12-
[[ $1 ]] && exit $1
13+
exit $1 || true
1314
}
1415

1516
abort() {
@@ -23,21 +24,21 @@ trap_abort() {
2324
}
2425

2526
trap_exit() {
26-
loge "Received exit..."
27+
log "Received exit..."
2728
trap - EXIT INT QUIT TERM HUP
2829
cleanup
2930
}
3031

3132
log() {
32-
echo -e `date +"$DATE_OPTS"`": $1" >> $TEMP_LOG
33+
echo -e `date +"$AMN_DATE_OPTS"`": $1" >> $AMN_TEMP_LOG
3334
}
3435

3536
loge() {
36-
echo -e `date +"$DATE_OPTS"`": ERROR: $1" >> $TEMP_LOG
37+
echo -e `date +"$AMN_DATE_OPTS"`": ERROR: $1" >> $AMN_TEMP_LOG
3738
}
3839

3940
logc() {
40-
echo -e `date +"$DATE_OPTS"`": Exec: $1" >> $TEMP_LOG
41+
echo -e `date +"$AMN_DATE_OPTS"`": Exec: $1" >> $AMN_TEMP_LOG
4142
}
4243

4344
run_cmd() {
@@ -48,38 +49,60 @@ run_cmd() {
4849
$cmd $2
4950
}
5051

52+
usage() {
53+
echo "${0} - Auto Mount or Unmount NFS shares"
54+
echo
55+
echo "Usage: ${0} [options] (mount|unmount)"
56+
echo
57+
echo "Options:"
58+
echo
59+
echo " -h: Show help information."
60+
echo
61+
echo "Commands:"
62+
echo
63+
echo " mount Mount fstab nfs mounts"
64+
echo " unmount Unmount all fstab nfs mounts"
65+
}
66+
67+
if [[ $# -lt 1 ]]; then
68+
usage;
69+
exit 0;
70+
fi
71+
5172
log "AutoMountNFS started: $0::$$"
5273

74+
ARGS=("$@")
75+
for (( a = 0; a < $#; a++ )); do
76+
if [[ ${ARGS[$a]} == "check" ]]; then
77+
log "Found check command argument"
78+
AMN_CHECK=1
79+
elif [[ ${ARGS[$a]} == "unmount" ]]; then
80+
log "Found unmount command argument"
81+
AMN_UNMOUNT=1
82+
elif [[ ${ARGS[$a]} == "-h" ]]; then
83+
usage;
84+
exit 0;
85+
fi
86+
done
87+
5388
MOUNT_POINTS=$(sed -e '/^.*#/d' -e '/^.*:\//!d' -e 's/\t/ /g' /etc/fstab \
5489
| tr -s " " | cut -f2 -d" ")
5590

56-
ping -c 1 "${SERVER_FQDN}" >> $TEMP_LOG
57-
58-
if [ $? -ne 0 ]; then
59-
log "The server could not be reached."
91+
unmount_all() {
6092
for umntpnt in ${MOUNT_POINTS}; do
6193
log "Unmounting $umntpnt"
6294
cmd="umount -l -f $umntpnt"
6395
logc "$cmd"
64-
$cmd 2>&1 >> $TEMP_LOG
96+
$cmd 2>&1 >> $AMN_TEMP_LOG
6597
if [ $? -ne 0 ]; then
6698
loge "umounting failed! ($?)"
6799
continue
68100
fi
69101
log "umounted $umntpnt"
70102
done
71-
else
72-
log "Asking the server for mountpoints"
73-
cmd="showmount -e $SERVER_FQDN"
74-
logc "$cmd"
75-
NFS_SHARES=`$cmd 2>>$TEMP_LOG | sed -e '/^\//!d' | cut -f1 -d" "; \
76-
test ${PIPESTATUS[0]} -eq 0`
77-
smount_ret=$?
78-
if [ $smount_ret -ne 0 ]; then
79-
loge "No shares available from host! ($smount_ret)"
80-
exit;
81-
fi
82-
log "Server mount points:\n$NFS_SHARES"
103+
}
104+
105+
mount_all() {
83106
for mount_point in ${MOUNT_POINTS}; do
84107
log "Processing point: $mount_point"
85108
if [[ $NFS_SHARES != *$mount_point* ]]; then
@@ -88,18 +111,42 @@ else
88111
fi
89112
cmd="mountpoint -q $mount_point"
90113
logc "$cmd"
91-
$cmd 2>&1 >> $TEMP_LOG
92-
114+
$cmd 2>&1 >> $AMN_TEMP_LOG
93115
if [ $? -ne 0 ]; then
94116
log "Mounting $mount_point"
95-
mount -v $mount_point >> $TEMP_LOG
117+
mount -v $mount_point >> $AMN_TEMP_LOG
96118
if [ $? -ne 0 ]; then
97119
loge "Mounting failed! ($?)"
98120
continue
99121
fi
100122
log "mounted $mount_point"
101123
fi
102124
done
125+
}
126+
127+
if [[ "${AMN_CHECK}" -eq 1 ]]; then
128+
ping -c 1 "${AMN_SERVER_FQDN}" >> $AMN_TEMP_LOG
129+
if [ $? -ne 0 ]; then
130+
log "The server could not be reached."
131+
unmount_all
132+
else
133+
log "Asking the server for mountpoints"
134+
cmd="showmount -e $AMN_SERVER_FQDN"
135+
logc "$cmd"
136+
NFS_SHARES=`$cmd 2>>$AMN_TEMP_LOG | sed -e '/^\//!d' | cut -f1 -d" "; \
137+
test ${PIPESTATUS[0]} -eq 0`
138+
smount_ret=$?
139+
if [ $smount_ret -ne 0 ]; then
140+
loge "No shares available from host! ($smount_ret)"
141+
exit;
142+
fi
143+
log "Server mount points:\n$NFS_SHARES"
144+
mount_all
145+
fi
146+
fi
147+
148+
if [[ "${AMN_UNMOUNT}" -eq 1 ]]; then
149+
unmount_all
103150
fi
104151

105152
# vim: ft=sh

0 commit comments

Comments
 (0)