diff --git a/tools/json/config.system.json b/tools/json/config.system.json index b4d19331c..788edbfc5 100644 --- a/tools/json/config.system.json +++ b/tools/json/config.system.json @@ -179,14 +179,24 @@ "description": "Network filesystem (NFS) Client", "sub": [ { - "id": "NFS06", - "description": "Show network filesystem (NFS) servers in subnet", + "id": "NFS12", + "description": "Enable network filesystem (NFS) client support", "command": [ - "module_nfsd servers" + "module_nfs install" ], "status": "Stable", "author": "@igorpecovnik", - "condition": "" + "condition": "check_if_installed nfs-common" + } + { + "id": "NFS13", + "description": "Find and mount network filesystem (NFS) servers from subnet", + "command": [ + "module_nfs servers" + ], + "status": "Stable", + "author": "@igorpecovnik", + "condition": "check_if_installed nfs-common" } ] } diff --git a/tools/modules/system/module_nfs.sh b/tools/modules/system/module_nfs.sh new file mode 100644 index 000000000..9d85f1a86 --- /dev/null +++ b/tools/modules/system/module_nfs.sh @@ -0,0 +1,103 @@ +module_options+=( + ["module_nfs,author"]="@igorpecovnik" + ["module_nfs,feature"]="module_nfs" + ["module_nfs,desc"]="Install nfs client" + ["module_nfs,example"]="install remove servers help" + ["module_nfs,port"]="" + ["module_nfs,status"]="Active" + ["module_nfs,arch"]="" +) +# +# Module nfs client +# +function module_nfs () { + local title="nfs" + local condition=$(which "$title" 2>/dev/null)? + + local package_name=nfs-common + + local commands + IFS=' ' read -r -a commands <<< "${module_options["module_nfs,example"]}" + + nfs_BASE="${SOFTWARE_FOLDER}/nfs" + + case "$1" in + "${commands[0]}") + apt_install_wrapper apt-get -y install $package_name + ;; + "${commands[1]}") + apt_install_wrapper apt-get -y autopurge $package_name + ;; + "${commands[3]}") + + if ! check_if_installed nmap; then + apt_install_wrapper apt-get -y install nmap + fi + + LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2 | grep -v "${LOCALIPADD}")) + LIST_LENGTH=$((${#LIST[@]})) + if nfs_server=$(dialog --no-items \ + --title "Network filesystem (NFS) servers in subnet" \ + --menu "" \ + $((${LIST_LENGTH} + 6)) \ + 80 \ + $((${LIST_LENGTH})) \ + ${LIST[@]} 3>&1 1>&2 2>&3); then + # verify if we can connect there + LIST=($(showmount -e "${nfs_server}" | tail -n +2 | cut -d" " -f1 | sort)) + VERIFIED_LIST=() + local tempfolder=$(mktemp -d) + local alreadymounted=$(df | grep $nfs_server | cut -d" " -f1 | xargs) + for i in "${LIST[@]}"; do + mount -n -t nfs $nfs_server:$i ${tempfolder} 2>/dev/null + if [[ $? -eq 0 ]]; then + if echo "${alreadymounted}" | grep -vq $i; then + VERIFIED_LIST+=($i) + fi + umount ${tempfolder} + fi + done + VERIFIED_LIST_LENGTH=$((${#VERIFIED_LIST[@]})) + if shares=$(dialog --no-items \ + --title "Network filesystem (NFS) shares on ${nfs_server}" \ + --menu "" \ + $((${VERIFIED_LIST_LENGTH} + 6)) \ + 80 \ + $((${VERIFIED_LIST_LENGTH})) \ + ${VERIFIED_LIST[@]} 3>&1 1>&2 2>&3) + then + if mount_folder=$(dialog --title \ + "Where do you want to mount $shares ?" \ + --inputbox "" \ + 6 80 "/armbian" 3>&1 1>&2 2>&3); then + if mount_options=$(dialog --title \ + "Which mount options do you want to use?" \ + --inputbox "" \ + 6 80 "auto,noatime 0 0" 3>&1 1>&2 2>&3); then + mkdir -p ${mount_folder} + read + sed -i '\?^'$nfs_server:$shares'?d' /etc/fstab + echo "${nfs_server}:${shares} ${mount_folder} nfs ${mount_options}" >> /etc/fstab + systemctl daemon-reload + mount ${mount_options} + fi + fi + fi + fi + ;; + "${commands[4]}") + echo -e "\nUsage: ${module_options["module_nfs,feature"]} " + echo -e "Commands: ${module_options["module_nfs,example"]}" + echo "Available commands:" + echo -e "\tinstall\t- Install $title." + echo -e "\tmanage\t- Edit exports in $title." + echo -e "\tadd\t- Add exports to $title." + echo -e "\tstatus\t- Installation status $title." + echo -e "\tremove\t- Remove $title." + echo + ;; + *) + ${module_options["module_nfs,feature"]} ${commands[4]} + ;; + esac +} diff --git a/tools/modules/system/module_nfsd.sh b/tools/modules/system/module_nfsd.sh index 7bd700df1..6d09f904e 100644 --- a/tools/modules/system/module_nfsd.sh +++ b/tools/modules/system/module_nfsd.sh @@ -105,7 +105,12 @@ function module_nfsd () { show_message <<< $(printf '%s\n' "${NFS_CLIENTS_CONNECTED[@]}") ;; "${commands[6]}") - LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2)) + + if ! check_if_installed nmap; then + apt_install_wrapper apt-get -y install nmap + fi + + LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2 | grep -v "${LOCALIPADD}")) LIST_LENGTH=$((${#LIST[@]})) if nfs_server=$(dialog --no-items \ --title "Network filesystem (NFS) servers in subnet" \