|
| 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 package_name=nfs-common |
| 18 | + |
| 19 | + local commands |
| 20 | + IFS=' ' read -r -a commands <<< "${module_options["module_nfs,example"]}" |
| 21 | + |
| 22 | + nfs_BASE="${SOFTWARE_FOLDER}/nfs" |
| 23 | + |
| 24 | + case "$1" in |
| 25 | + "${commands[0]}") |
| 26 | + apt_install_wrapper apt-get -y install $package_name |
| 27 | + ;; |
| 28 | + "${commands[1]}") |
| 29 | + apt_install_wrapper apt-get -y autopurge $package_name |
| 30 | + ;; |
| 31 | + "${commands[3]}") |
| 32 | + |
| 33 | + if ! check_if_installed nmap; then |
| 34 | + apt_install_wrapper apt-get -y install nmap |
| 35 | + fi |
| 36 | + |
| 37 | + LIST=($(nmap -oG - -p2049 ${LOCALSUBNET} | grep '/open/' | cut -d' ' -f2 | grep -v "${LOCALIPADD}")) |
| 38 | + LIST_LENGTH=$((${#LIST[@]})) |
| 39 | + if nfs_server=$(dialog --no-items \ |
| 40 | + --title "Network filesystem (NFS) servers in subnet" \ |
| 41 | + --menu "" \ |
| 42 | + $((${LIST_LENGTH} + 6)) \ |
| 43 | + 80 \ |
| 44 | + $((${LIST_LENGTH})) \ |
| 45 | + ${LIST[@]} 3>&1 1>&2 2>&3); then |
| 46 | + # verify if we can connect there |
| 47 | + LIST=($(showmount -e "${nfs_server}" | tail -n +2 | cut -d" " -f1 | sort)) |
| 48 | + VERIFIED_LIST=() |
| 49 | + local tempfolder=$(mktemp -d) |
| 50 | + local alreadymounted=$(df | grep $nfs_server | cut -d" " -f1 | xargs) |
| 51 | + for i in "${LIST[@]}"; do |
| 52 | + mount -n -t nfs $nfs_server:$i ${tempfolder} 2>/dev/null |
| 53 | + if [[ $? -eq 0 ]]; then |
| 54 | + if echo "${alreadymounted}" | grep -vq $i; then |
| 55 | + VERIFIED_LIST+=($i) |
| 56 | + fi |
| 57 | + umount ${tempfolder} |
| 58 | + fi |
| 59 | + done |
| 60 | + VERIFIED_LIST_LENGTH=$((${#VERIFIED_LIST[@]})) |
| 61 | + if shares=$(dialog --no-items \ |
| 62 | + --title "Network filesystem (NFS) shares on ${nfs_server}" \ |
| 63 | + --menu "" \ |
| 64 | + $((${VERIFIED_LIST_LENGTH} + 6)) \ |
| 65 | + 80 \ |
| 66 | + $((${VERIFIED_LIST_LENGTH})) \ |
| 67 | + ${VERIFIED_LIST[@]} 3>&1 1>&2 2>&3) |
| 68 | + then |
| 69 | + if mount_folder=$(dialog --title \ |
| 70 | + "Where do you want to mount $shares ?" \ |
| 71 | + --inputbox "" \ |
| 72 | + 6 80 "/armbian" 3>&1 1>&2 2>&3); then |
| 73 | + if mount_options=$(dialog --title \ |
| 74 | + "Which mount options do you want to use?" \ |
| 75 | + --inputbox "" \ |
| 76 | + 6 80 "auto,noatime 0 0" 3>&1 1>&2 2>&3); then |
| 77 | + mkdir -p ${mount_folder} |
| 78 | + read |
| 79 | + sed -i '\?^'$nfs_server:$shares'?d' /etc/fstab |
| 80 | + echo "${nfs_server}:${shares} ${mount_folder} nfs ${mount_options}" >> /etc/fstab |
| 81 | + systemctl daemon-reload |
| 82 | + mount ${mount_options} |
| 83 | + fi |
| 84 | + fi |
| 85 | + fi |
| 86 | + fi |
| 87 | + ;; |
| 88 | + "${commands[4]}") |
| 89 | + echo -e "\nUsage: ${module_options["module_nfs,feature"]} <command>" |
| 90 | + echo -e "Commands: ${module_options["module_nfs,example"]}" |
| 91 | + echo "Available commands:" |
| 92 | + echo -e "\tinstall\t- Install $title." |
| 93 | + echo -e "\tmanage\t- Edit exports in $title." |
| 94 | + echo -e "\tadd\t- Add exports to $title." |
| 95 | + echo -e "\tstatus\t- Installation status $title." |
| 96 | + echo -e "\tremove\t- Remove $title." |
| 97 | + echo |
| 98 | + ;; |
| 99 | + *) |
| 100 | + ${module_options["module_nfs,feature"]} ${commands[4]} |
| 101 | + ;; |
| 102 | + esac |
| 103 | +} |
0 commit comments