Skip to content

Commit 7ed103a

Browse files
Apply several fixes to NFS service (#313)
* NFSD: show connected clients * Add container-aware service wrapper * nfsd: work inside container * nfsd: fix uninstall command * Replace check_if_installed --------- Co-authored-by: Dimitry Ishenko <dimitry.ishenko@gmail.com>
1 parent cd8419d commit 7ed103a

File tree

5 files changed

+253
-23
lines changed

5 files changed

+253
-23
lines changed

tools/json/config.system.json

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,87 @@
130130
},
131131
{
132132
"id": "NFS01",
133-
"description": "Install network filesystem (NFS) daemon",
133+
"description": "Enable Network filesystem (NFS) support",
134134
"command": [
135-
"module_nfsd install"
135+
"module_nfs install"
136136
],
137137
"status": "Stable",
138138
"author": "@igorpecovnik",
139-
"condition": "! module_nfsd status"
139+
"condition": "! pkg_installed nfs-common"
140140
},
141141
{
142142
"id": "NFS02",
143-
"description": "Configure network filesystem (NFS) daemon",
143+
"description": "Disable Network filesystem (NFS) support",
144144
"command": [
145-
"module_nfsd manage"
145+
"module_nfs remove"
146146
],
147147
"status": "Stable",
148148
"author": "@igorpecovnik",
149-
"condition": "module_nfsd status"
149+
"condition": "pkg_installed nfs-common"
150150
},
151151
{
152-
"id": "NFS03",
153-
"description": "Remove network filesystem (NFS) daemon",
154-
"command": [
155-
"module_nfsd uninstall"
156-
],
157-
"status": "Stable",
158-
"author": "@igorpecovnik",
159-
"condition": "module_nfsd status"
152+
"id": "NFS05",
153+
"description": "Manage NFS Server",
154+
"condition": "pkg_installed nfs-common",
155+
"sub": [
156+
{
157+
"id": "NFS06",
158+
"description": "Enable network filesystem (NFS) daemon",
159+
"command": [
160+
"module_nfsd install"
161+
],
162+
"status": "Stable",
163+
"author": "@igorpecovnik",
164+
"condition": "! module_nfsd status"
165+
},
166+
{
167+
"id": "NFS07",
168+
"description": "Configure network filesystem (NFS) daemon",
169+
"command": [
170+
"module_nfsd manage"
171+
],
172+
"status": "Stable",
173+
"author": "@igorpecovnik",
174+
"condition": "module_nfsd status"
175+
},
176+
{
177+
"id": "NFS08",
178+
"description": "Remove network filesystem (NFS) daemon",
179+
"command": [
180+
"module_nfsd remove"
181+
],
182+
"status": "Stable",
183+
"author": "@igorpecovnik",
184+
"condition": "module_nfsd status"
185+
},
186+
{
187+
"id": "NFS09",
188+
"description": "Show network filesystem (NFS) daemon clients",
189+
"command": [
190+
"module_nfsd clients"
191+
],
192+
"status": "Stable",
193+
"author": "@igorpecovnik",
194+
"condition": "module_nfsd status && [[ ${NFS_CLIENTS_NUMBER} -gt 0 ]]"
195+
}
196+
]
197+
},
198+
{
199+
"id": "NFS20",
200+
"description": "Manage NFS Client",
201+
"condition": "pkg_installed nfs-common",
202+
"sub": [
203+
{
204+
"id": "NFS21",
205+
"description": "Find NFS servers in subnet and mount shares",
206+
"command": [
207+
"module_nfs servers"
208+
],
209+
"status": "Stable",
210+
"author": "@igorpecovnik",
211+
"condition": "pkg_installed nfs-common"
212+
}
213+
]
160214
}
161215
]
162216
},

tools/modules/functions/service.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# service.sh
2+
3+
declare -A module_options
4+
module_options+=(
5+
["service,author"]="@dimitry-ishenko"
6+
["service,desc"]="Wrapper for service manipulation"
7+
["service,example"]="service install some.service"
8+
["service,feature"]="service"
9+
["service,status"]="active"
10+
)
11+
12+
function service()
13+
{
14+
# ignore these commands, if running inside container
15+
[[ "$1" =~ ^(reload|restart|start|status|stop)$ ]] && systemd-detect-virt -qc && return 0
16+
systemctl daemon-reload
17+
systemctl "$@"
18+
}

tools/modules/runtime/config.runtime.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ fi
9696

9797
update_sub_submenu_data "System" "Storage" "SY220" "$(module_zfs zfs_version)"
9898
update_sub_submenu_data "System" "Storage" "SY221" "$(module_zfs zfs_installed_version)"
99+
update_sub_submenu_data "System" "Storage" "NFS04" "$NFS_CLIENTS_NUMBER"
99100
update_sub_submenu_data "Software" "Database" "DAT002" "Server: $LOCALIPADD"
100101
update_sub_submenu_data "Software" "Database" "DAT006" "http://$LOCALIPADD:${module_options["module_phpmyadmin,port"]}"
101102
update_sub_submenu_data "Software" "Media" "MED006" "http://$LOCALIPADD:${module_options["module_stirling,port"]}"

tools/modules/system/module_nfs.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
module_options+=(
2+
["module_nfs,author"]="@igorpecovnik"
3+
["module_nfs,feature"]="module_nfs"
4+
["module_nfs,desc"]="Install nfs client"
5+
["module_nfs,example"]="install remove servers help"
6+
["module_nfs,port"]=""
7+
["module_nfs,status"]="Active"
8+
["module_nfs,arch"]=""
9+
)
10+
#
11+
# Module nfs client
12+
#
13+
function module_nfs () {
14+
local title="nfs"
15+
local condition=$(which "$title" 2>/dev/null)?
16+
17+
local commands
18+
IFS=' ' read -r -a commands <<< "${module_options["module_nfs,example"]}"
19+
20+
nfs_BASE="${SOFTWARE_FOLDER}/nfs"
21+
22+
case "$1" in
23+
"${commands[0]}")
24+
pkg_install nfs-common
25+
;;
26+
"${commands[1]}")
27+
pkg_remove nfs-common
28+
;;
29+
"${commands[2]}")
30+
31+
if ! pkg_installed nmap; then
32+
pkg_install nmap
33+
fi
34+
35+
LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2 | grep -v "${LOCALIPADD}"))
36+
LIST_LENGTH=$((${#LIST[@]}))
37+
if nfs_server=$(dialog --no-items \
38+
--title "Network filesystem (NFS) servers in subnet" \
39+
--menu "" \
40+
$((${LIST_LENGTH} + 6)) \
41+
80 \
42+
$((${LIST_LENGTH})) \
43+
${LIST[@]} 3>&1 1>&2 2>&3); then
44+
# verify if we can connect there
45+
LIST=($(showmount -e "${nfs_server}" | tail -n +2 | cut -d" " -f1 | sort))
46+
VERIFIED_LIST=()
47+
local tempfolder=$(mktemp -d)
48+
local alreadymounted=$(df | grep $nfs_server | cut -d" " -f1 | xargs)
49+
for i in "${LIST[@]}"; do
50+
mount -n -t nfs $nfs_server:$i ${tempfolder} 2>/dev/null
51+
if [[ $? -eq 0 ]]; then
52+
if echo "${alreadymounted}" | grep -vq $i; then
53+
VERIFIED_LIST+=($i)
54+
fi
55+
umount ${tempfolder}
56+
fi
57+
done
58+
VERIFIED_LIST_LENGTH=$((${#VERIFIED_LIST[@]}))
59+
if shares=$(dialog --no-items \
60+
--title "Network filesystem (NFS) shares on ${nfs_server}" \
61+
--menu "" \
62+
$((${VERIFIED_LIST_LENGTH} + 6)) \
63+
80 \
64+
$((${VERIFIED_LIST_LENGTH})) \
65+
${VERIFIED_LIST[@]} 3>&1 1>&2 2>&3)
66+
then
67+
if mount_folder=$(dialog --title \
68+
"Where do you want to mount $shares ?" \
69+
--inputbox "" \
70+
6 80 "/armbian" 3>&1 1>&2 2>&3); then
71+
if mount_options=$(dialog --title \
72+
"Which mount options do you want to use?" \
73+
--inputbox "" \
74+
6 80 "auto,noatime 0 0" 3>&1 1>&2 2>&3); then
75+
mkdir -p ${mount_folder}
76+
sed -i '\?^'$nfs_server:$shares'?d' /etc/fstab
77+
echo "${nfs_server}:${shares} ${mount_folder} nfs ${mount_options}" >> /etc/fstab
78+
systemctl daemon-reload
79+
mount ${mount_folder}
80+
show_message <<< $(mount -t nfs4 | cut -d" " -f1)
81+
fi
82+
fi
83+
fi
84+
fi
85+
;;
86+
"${commands[3]}")
87+
echo -e "\nUsage: ${module_options["module_nfs,feature"]} <command>"
88+
echo -e "Commands: ${module_options["module_nfs,example"]}"
89+
echo "Available commands:"
90+
echo -e "\tinstall\t- Install $title."
91+
echo -e "\tremove\t- Remove $title."
92+
echo -e "\tservers\t- Find and mount shares $title."
93+
echo
94+
;;
95+
*)
96+
${module_options["module_nfs,feature"]} ${commands[3]}
97+
;;
98+
esac
99+
}
Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module_options+=(
22
["module_nfsd,author"]="@igorpecovnik"
33
["module_nfsd,feature"]="module_nfsd"
44
["module_nfsd,desc"]="Install nfsd server"
5-
["module_nfsd,example"]="install remove manage add status help"
5+
["module_nfsd,example"]="install remove manage add status clients servers help"
66
["module_nfsd,port"]=""
77
["module_nfsd,status"]="Active"
88
["module_nfsd,arch"]=""
@@ -14,6 +14,8 @@ function module_nfsd () {
1414
local title="nfsd"
1515
local condition=$(which "$title" 2>/dev/null)?
1616

17+
local service_name=nfs-server.service
18+
1719
# we will store our config in subfolder
1820
mkdir -p /etc/exports.d/
1921

@@ -27,7 +29,7 @@ function module_nfsd () {
2729
pkg_install nfs-common nfs-kernel-server
2830
# add some exports
2931
${module_options["module_nfsd,feature"]} ${commands[2]}
30-
systemctl restart nfs-server.service
32+
service restart $service_name
3133
;;
3234
"${commands[1]}")
3335
pkg_remove nfs-kernel-server
@@ -65,7 +67,7 @@ function module_nfsd () {
6567
break
6668
fi
6769
done
68-
systemctl restart nfs-server.service
70+
service restart $service_name
6971
;;
7072
"${commands[3]}")
7173
# choose between most common options
@@ -96,13 +98,69 @@ function module_nfsd () {
9698
fi
9799
;;
98100
"${commands[4]}")
99-
if systemctl is-active --quiet nfs-server.service; then
100-
return 0
101-
else
102-
return 1
103-
fi
101+
pkg_installed nfs-kernel-server
104102
;;
105103
"${commands[5]}")
104+
show_message <<< $(printf '%s\n' "${NFS_CLIENTS_CONNECTED[@]}")
105+
;;
106+
"${commands[6]}")
107+
108+
if ! pkg_installed nmap; then
109+
pkg_install nmap
110+
fi
111+
112+
LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2 | grep -v "${LOCALIPADD}"))
113+
LIST_LENGTH=$((${#LIST[@]}))
114+
if nfs_server=$(dialog --no-items \
115+
--title "Network filesystem (NFS) servers in subnet" \
116+
--menu "" \
117+
$((${LIST_LENGTH} + 6)) \
118+
80 \
119+
$((${LIST_LENGTH})) \
120+
${LIST[@]} 3>&1 1>&2 2>&3); then
121+
# verify if we can connect there
122+
LIST=($(showmount -e "${nfs_server}" | tail -n +2 | cut -d" " -f1 | sort))
123+
VERIFIED_LIST=()
124+
local tempfolder=$(mktemp -d)
125+
local alreadymounted=$(df | grep $nfs_server | cut -d" " -f1 | xargs)
126+
for i in "${LIST[@]}"; do
127+
mount -n -t nfs $nfs_server:$i ${tempfolder} 2>/dev/null
128+
if [[ $? -eq 0 ]]; then
129+
if echo "${alreadymounted}" | grep -vq $i; then
130+
VERIFIED_LIST+=($i)
131+
fi
132+
umount ${tempfolder}
133+
fi
134+
done
135+
VERIFIED_LIST_LENGTH=$((${#VERIFIED_LIST[@]}))
136+
if shares=$(dialog --no-items \
137+
--title "Network filesystem (NFS) shares on ${nfs_server}" \
138+
--menu "" \
139+
$((${VERIFIED_LIST_LENGTH} + 6)) \
140+
80 \
141+
$((${VERIFIED_LIST_LENGTH})) \
142+
${VERIFIED_LIST[@]} 3>&1 1>&2 2>&3)
143+
then
144+
if mount_folder=$(dialog --title \
145+
"Where do you want to mount $shares ?" \
146+
--inputbox "" \
147+
6 80 "/armbian" 3>&1 1>&2 2>&3); then
148+
if mount_options=$(dialog --title \
149+
"Which mount options do you want to use?" \
150+
--inputbox "" \
151+
6 80 "auto,noatime 0 0" 3>&1 1>&2 2>&3); then
152+
mkdir -p ${mount_folder}
153+
read
154+
sed -i '\?^'$nfs_server:$shares'?d' /etc/fstab
155+
echo "${nfs_server}:${shares} ${mount_folder} nfs ${mount_options}" >> /etc/fstab
156+
systemctl daemon-reload
157+
mount ${mount_options}
158+
fi
159+
fi
160+
fi
161+
fi
162+
;;
163+
"${commands[7]}")
106164
echo -e "\nUsage: ${module_options["module_nfsd,feature"]} <command>"
107165
echo -e "Commands: ${module_options["module_nfsd,example"]}"
108166
echo "Available commands:"
@@ -114,7 +172,7 @@ function module_nfsd () {
114172
echo
115173
;;
116174
*)
117-
${module_options["module_nfsd,feature"]} ${commands[5]}
175+
${module_options["module_nfsd,feature"]} ${commands[7]}
118176
;;
119177
esac
120178
}

0 commit comments

Comments
 (0)