| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Author: Steven Shiau, <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | |
| 5 | # Load DRBL setting and functions |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | # |
| 10 | export LC_ALL=C |
| 11 | |
| 12 | Usagescip-shell shell shell-project 0.0.0 Usage().() { |
| 13 | echoscip-shell system . . echo. "Get the NFS server IP address of some IP address from DRBL setting." |
| 14 | echoscip-shell system . . echo. "Normally this command is used in DRBL clients." |
| 15 | echoscip-shell system . . echo. "Usage:" |
| 16 | echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. $0) IP_ADDRESS" |
| 17 | echoscip-shell system . . echo. "Ex: $(basenamescip-shell system . . basename. $0) 192.168.1.1" |
| 18 | } |
| 19 | |
| 20 | # |
| 21 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 22 | case "$1" in |
| 23 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 24 | Usagescip-shell shell shell-project 0.0.0 Usage(). >&2 |
| 25 | exit 2 ;; |
| 26 | *) break ;; |
| 27 | esac |
| 28 | done |
| 29 | |
| 30 | # Load config file |
| 31 | if [scip-shell system . . `[`. -f /etc/diskless-image/configscip-shell filesystem . . `/etc/diskless-image/config`. ]; then |
| 32 | . /etc/diskless-image/configscip-shell source . . `/etc/diskless-image/config`. |
| 33 | mode="client" |
| 34 | elif [scip-shell system . . `[`. -f $drbl_common_root/etc/diskless-image/config ]; then |
| 35 | . $drbl_common_root/etc/diskless-image/config |
| 36 | mode="server" |
| 37 | else |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | IP=$1 |
| 42 | [scip-shell system . . `[`. -z "$IP" ] && exit 1 |
| 43 | |
| 44 | # On client side, just use route to get the default gw, and it's nfsserver |
| 45 | # On server side, get a table to grep. |
| 46 | case "$mode" in |
| 47 | "client") |
| 48 | # On client side |
| 49 | # 1st: try to use the mount status to get the nfs server, e.g. |
| 50 | # 192.168.120.183:/tftpboot/node_root / |
| 51 | # 2nd: Try to use gateway as NFS server |
| 52 | nfsserver_cand="$(LC_ALL=C findmntscip-shell system . . findmnt. -nr -o source --target /scip-shell filesystem . . `/`. | awkscip-shell system . . awk. -F":" '{print $1}')" |
| 53 | gateway="$(LC_ALL=C ip route show default 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. '/^default/ {for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}scip-shell filesystem . . `/^default/ {for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}`.')" |
| 54 | if `is_valid_IPv4_add "$nfsserver_cand"`; then |
| 55 | nfsserver="$nfsserver_cand" |
| 56 | elif [scip-shell system . . `[`. -n "$gateway" ]; then |
| 57 | nfsserver="$gateway" |
| 58 | else |
| 59 | echoscip-shell system . . echo. "NFSSERVER is not found, use default one: \"$nfsserver_default\"." 1>&2 |
| 60 | nfsserver=$nfsserver_default |
| 61 | fi |
| 62 | ;; |
| 63 | "server") |
| 64 | for i in $NFSSERVER_LIST; do |
| 65 | if [scip-shell system . . `[`. -n "$(grepscip-shell system . . grep. -Ew "$IP" $drbl_syscfg/clients-of-${i}.txt 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ]; then |
| 66 | nfsserver="$i" |
| 67 | break |
| 68 | fi |
| 69 | done |
| 70 | ;; |
| 71 | esac |
| 72 | |
| 73 | if [scip-shell system . . `[`. -n "$nfsserver" ]; then |
| 74 | echoscip-shell system . . echo. $nfsserver |
| 75 | exit 0 |
| 76 | else |
| 77 | exit 1 |
| 78 | fi |