| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | # |
| 5 | # generate the NFS exports or clean the old one. |
| 6 | |
| 7 | # Load DRBL setting and functions |
| 8 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 9 | |
| 10 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 11 | |
| 12 | # |
| 13 | check_if_root |
| 14 | |
| 15 | # setting |
| 16 | nfs_exports="/etc/exportsscip-shell filesystem . . `/etc/exports`." |
| 17 | EXPORTS_NFS_RO_NRS_OPT="ro,async,no_root_squash,no_subtree_check" |
| 18 | EXPORTS_NFS_RW_NRS_OPT="rw,sync,no_root_squash,no_subtree_check" |
| 19 | EXPORTS_NFS_RW_RS_OPT="rw,sync,root_squash,no_subtree_check" |
| 20 | |
| 21 | # main |
| 22 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 23 | echoscip-shell system . . echo. "To generate or clean NFS exports for DRBL clients" |
| 24 | echoscip-shell system . . echo. "Usage: $0 [Options] {generate|clean}" |
| 25 | echoscip-shell system . . echo. "Options:" |
| 26 | language_help_prompt_by_idx_no |
| 27 | echoscip-shell system . . echo. "-a, --all-subnet: Make all subnet can access to this NFS server." |
| 28 | echoscip-shell system . . echo. "-n, --no-restart: Not restart NFS service" |
| 29 | echoscip-shell system . . echo. "-v, --verbose: Verbose mode." |
| 30 | echoscip-shell system . . echo. "Example: To generate NFS exports for DRBL clients" |
| 31 | echoscip-shell system . . echo. "$0 generate" |
| 32 | } |
| 33 | |
| 34 | # |
| 35 | set_nfsd_numberscip-shell shell shell-project 0.0.0 set_nfsd_number().() { |
| 36 | local total_nfsd=$1 |
| 37 | if [scip-shell system . . `[`. -z "$total_nfsd" ]; then |
| 38 | echoscip-shell system . . echo. "No total_nfsd is assigned!!! Program terminated!" |
| 39 | exit 1 |
| 40 | fi |
| 41 | # set the RPCNFSDCOUNT to $total_client_no*$NFSD_RATIO (say "40") |
| 42 | # in /etc/sysconfig/nfs instead of 8 in /etc/init.d/nfs (for RH-like) |
| 43 | # |
| 44 | if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then |
| 45 | # Debian |
| 46 | if [scip-shell system . . `[`. -f /etc/default/nfs-kernel-serverscip-shell filesystem . . `/etc/default/nfs-kernel-server`. ]; then |
| 47 | # The nfs-kernel-server reads /etc/default/nfs-kernel-server (Sarge) |
| 48 | perlscip-shell system . . perl. -p -i -e "s/RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/default/nfs-kernel-serverscip-shell filesystem . . `/etc/default/nfs-kernel-server`. |
| 49 | else |
| 50 | # The nfs-kernel-server does NOT support /etc/default/nfs-kernel-server, |
| 51 | # put it in the service (Woody). |
| 52 | # If it's unfsd, not config file at all. Therefore we test it before modify it. |
| 53 | if [scip-shell system . . `[`. -f /etc/init.d/nfs-kernel-serverscip-shell filesystem . . `/etc/init.d/nfs-kernel-server`. ]; then |
| 54 | perlscip-shell system . . perl. -p -i -e "s/^RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/init.d/nfs-kernel-serverscip-shell filesystem . . `/etc/init.d/nfs-kernel-server`. |
| 55 | fi |
| 56 | fi |
| 57 | elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then |
| 58 | # SUSE |
| 59 | perlscip-shell system . . perl. -p -i -e "s/^USE_KERNEL_NFSD_NUMBER=.*/USE_KERNEL_NFSD_NUMBER=$total_nfsd/g" /etc/sysconfig/nfsscip-shell filesystem . . `/etc/sysconfig/nfs`. |
| 60 | else |
| 61 | # RH-like |
| 62 | # Fedora uses service name "nfs", so does Mandriva 2007 or earlier. However, from Mandriva 2008.0, it's nfs-server. Therefore we use $NFS_SRV_NAME from drbl.conf |
| 63 | # We need to know the parameter for NFS daemon number, check that in /etc/init.d/nfs. Maybe it RPCNFSDCOUNT or NFSDCOUNT |
| 64 | if grepscip-shell system . . grep. -qE "\<RPCNFSDCOUNT=.*" /etc/init.d/$NFS_SRV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 65 | # For Mandriva 2006, the /etc/sysconfig/nfs already exists when fresh installed. We had better to keep other parameters, not "cat ..." to overwrite all. |
| 66 | if grepscip-shell system . . grep. -q "^[#]*[[:space:]]*RPCNFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 67 | perlscip-shell system . . perl. -p -i -e "s/^.*RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 68 | else |
| 69 | catscip-shell system . . cat. <<-NFSRPC_END > /etc/sysconfig/$NFS_SRV_NAME |
| 70 | RPCNFSDCOUNT=$total_nfsd |
| 71 | NFSRPC_END |
| 72 | fi |
| 73 | elif grepscip-shell system . . grep. -q "\<NFSDCOUNT=.*" /etc/init.d/$NFS_SRV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 74 | # For Mandrake 9.2-10.1, the /etc/sysconfig/nfs already exists when fresh installed. We had better to keep other parameters. |
| 75 | if grepscip-shell system . . grep. -q "^[#]*[[:space:]]*NFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 76 | perlscip-shell system . . perl. -p -i -e "s/^.*NFSDCOUNT=.*/NFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 77 | else |
| 78 | catscip-shell system . . cat. <<-NFSRPC_END > /etc/sysconfig/$NFS_SRV_NAME |
| 79 | NFSDCOUNT=$total_nfsd |
| 80 | NFSRPC_END |
| 81 | fi |
| 82 | elif grepscip-shell system . . grep. -q "\<RPCNFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 83 | # For systemd-like, E.g. FC17 |
| 84 | perlscip-shell system . . perl. -p -i -e "s/^.*RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 85 | else |
| 86 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 87 | echoscip-shell system . . echo. "Warning!!! Unable to determine the NFS daemon number parameter (Not RPCNFSDCOUNT or NFSDCOUNT)! This NFS service will not be good enough when there is heavy client traffic!!!" |
| 88 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 89 | fi |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | # load the setting, especiall "drbl_mode". |
| 94 | [scip-shell system . . `[`. -f "/etc/drbl/drbl_deploy.confscip-shell filesystem . . `/etc/drbl/drbl_deploy.conf`." ] && . /etc/drbl/drbl_deploy.confscip-shell source . . `/etc/drbl/drbl_deploy.conf`. |
| 95 | |
| 96 | # default setting |
| 97 | all_subnet="no" |
| 98 | restart_nfs="yes" |
| 99 | specified_lang="en_US" |
| 100 | |
| 101 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 102 | case "$1" in |
| 103 | -a|--all-subnet) |
| 104 | all_subnet="yes" |
| 105 | shift;; |
| 106 | -n|--no-restart) |
| 107 | restart_nfs="no" |
| 108 | shift;; |
| 109 | -v|--verbose) |
| 110 | shift; verbose="on" |
| 111 | ;; |
| 112 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 113 | usagescip-shell shell shell-project 0.0.0 usage(). >& 2 |
| 114 | exit 2 ;; |
| 115 | *) break ;; |
| 116 | esac |
| 117 | done |
| 118 | mode="$1" |
| 119 | |
| 120 | # |
| 121 | ask_and_load_lang_set $specified_lang |
| 122 | |
| 123 | case "$mode" in |
| 124 | "generate") |
| 125 | echoscip-shell system . . echo. "Generating the NFS exports for DRBL clients... " |
| 126 | echoscip-shell system . . echo. "Backup the original $nfs_exports as $nfs_exports.drblsave" |
| 127 | [scip-shell system . . `[`. -f "$nfs_exports" ] && mvscip-shell system . . mv. -f $nfs_exports ${nfs_exports}.drblsave |
| 128 | time_now="$(datescip-shell system . . date. "+%T %Y/%m/%d")" |
| 129 | catscip-shell system . . cat. <<EOF > $nfs_exports |
| 130 | # Generated by DRBL at $time_now |
| 131 | EOF |
| 132 | if [scip-shell system . . `[`. "$all_subnet" = "yes" ]; then |
| 133 | # export all subnet to clients |
| 134 | echoscip-shell system . . echo. "Exporting whole subnet to clients..." |
| 135 | subnet_list="$(get-client-ip-list | awkscip-shell system . . awk. -F"." '{print $1"."$2"."$3}' | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. )" |
| 136 | for subnet in $subnet_list; do |
| 137 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 138 | $drbl_common_root $subnet.*($EXPORTS_NFS_RO_NRS_OPT) |
| 139 | /usr $subnet.*($EXPORTS_NFS_RO_NRS_OPT) |
| 140 | /home $subnet.*($EXPORTS_NFS_RW_NRS_OPT,crossmnt) |
| 141 | /var/spool/mail $subnet.*($EXPORTS_NFS_RW_RS_OPT) |
| 142 | EOF |
| 143 | if [scip-shell system . . `[`. -d /opt/scip-shell filesystem . . `/opt/`. ]; then |
| 144 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 145 | /opt $subnet.*($EXPORTS_NFS_RO_NRS_OPT,crossmnt) |
| 146 | EOF |
| 147 | fi |
| 148 | # if $ocsroot is not under /home, or if $ocsroot is a mount point, and not under /home or /opt, we have to export it |
| 149 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $ocsroot | grepscip-shell system . . grep. -Ew "^/home")" -o \ |
| 150 | -n "$(awkscip-shell system . . awk. -F" " '{print $2}' /proc/mountsscip-shell filesystem . . `/proc/mounts`. | grepscip-shell system . . grep. -Ew "$ocsroot" | grepscip-shell system . . grep. -Ev "(^/home|^/opt)")" ]; then |
| 151 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 152 | $ocsroot $subnet.*($EXPORTS_NFS_RW_NRS_OPT,crossmnt) |
| 153 | EOF |
| 154 | fi |
| 155 | # add user assigned dir to exports |
| 156 | for d in $diskless_root_dir_ro_user_add; do |
| 157 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 158 | $d $subnet.*($EXPORTS_NFS_RO_NRS_OPT) |
| 159 | EOF |
| 160 | done |
| 161 | for d in $diskless_root_dir_rw_user_add; do |
| 162 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 163 | $d $subnet.*($EXPORTS_NFS_RW_NRS_OPT) |
| 164 | EOF |
| 165 | done |
| 166 | # append one space line |
| 167 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 168 | |
| 169 | EOF |
| 170 | done |
| 171 | else |
| 172 | # export one by one |
| 173 | echoscip-shell system . . echo. "Exporting to clients by IP address line-by-line..." |
| 174 | for ip in `get-client-ip-list`; do |
| 175 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 176 | $drbl_common_root $ip($EXPORTS_NFS_RO_NRS_OPT) |
| 177 | /usr $ip($EXPORTS_NFS_RO_NRS_OPT) |
| 178 | /home $ip($EXPORTS_NFS_RW_NRS_OPT,crossmnt) |
| 179 | /var/spool/mail $ip($EXPORTS_NFS_RW_RS_OPT) |
| 180 | EOF |
| 181 | if [scip-shell system . . `[`. -d /opt/scip-shell filesystem . . `/opt/`. ]; then |
| 182 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 183 | /opt $ip($EXPORTS_NFS_RO_NRS_OPT,crossmnt) |
| 184 | EOF |
| 185 | fi |
| 186 | # if $ocsroot is not under /home, or if $ocsroot is a mount point, and not under /home or /opt, we have to export it |
| 187 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $ocsroot | grepscip-shell system . . grep. -Ew "^/home")" -o \ |
| 188 | -n "$(awkscip-shell system . . awk. -F" " '{print $2}' /proc/mountsscip-shell filesystem . . `/proc/mounts`. | grepscip-shell system . . grep. -Ew "$ocsroot" | grepscip-shell system . . grep. -Ev "(^/home|^/opt)")" ]; then |
| 189 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 190 | $ocsroot $ip($EXPORTS_NFS_RW_NRS_OPT,crossmnt) |
| 191 | EOF |
| 192 | fi |
| 193 | # add user assigned dir to exports |
| 194 | for d in $diskless_root_dir_ro_user_add; do |
| 195 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 196 | $d $ip($EXPORTS_NFS_RO_NRS_OPT) |
| 197 | EOF |
| 198 | done |
| 199 | for d in $diskless_root_dir_rw_user_add; do |
| 200 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 201 | $d $ip($EXPORTS_NFS_RW_NRS_OPT) |
| 202 | EOF |
| 203 | done |
| 204 | # append one space line |
| 205 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 206 | |
| 207 | EOF |
| 208 | done |
| 209 | fi |
| 210 | |
| 211 | # export for every client's etc, var |
| 212 | if [scip-shell system . . `[`. "$drbl_mode" = "full_drbl_mode" -o "$clonezilla_mode" = "full_clonezilla_mode" ]; then |
| 213 | echoscip-shell system . . echo. "Full DRBL or Full Clonezilla mode, exporting client's directories etc, var, root..." |
| 214 | for ip in `get-client-ip-list`; do |
| 215 | [scip-shell system . . `[`. ! -d "$drblroot/$ip/" ] && continue |
| 216 | [scip-shell system . . `[`. -n "$verbose" ] && echoscip-shell system . . echo. "Creating exports for directories etc, var, root of node $ip..." |
| 217 | catscip-shell system . . cat. <<EOF >> $nfs_exports |
| 218 | $drblroot/$ip/ $ip($EXPORTS_NFS_RW_NRS_OPT) |
| 219 | EOF |
| 220 | done |
| 221 | fi |
| 222 | |
| 223 | echoscip-shell system . . echo. "The $nfs_exports setting is ok now!" |
| 224 | # rm /var/lib/nfs/rmtab to avoid a long time try when restart NFS |
| 225 | [scip-shell system . . `[`. -f /var/lib/nfs/rmtabscip-shell filesystem . . `/var/lib/nfs/rmtab`. ] && rmscip-shell system . . rm. -f /var/lib/nfs/rmtabscip-shell filesystem . . `/var/lib/nfs/rmtab`. |
| 226 | |
| 227 | # calculate the number of the NFS daemon |
| 228 | nfsd_no="$(get-necessary-nfsd-no)" |
| 229 | if [scip-shell system . . `[`. -z "$nfsd_no" ]; then |
| 230 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 231 | echoscip-shell system . . echo. "Unable to get necessary NFSD number, program terminated!!!" |
| 232 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 233 | exit 1 |
| 234 | fi |
| 235 | set_nfsd_numberscip-shell shell shell-project 0.0.0 set_nfsd_number(). $nfsd_no |
| 236 | |
| 237 | # restart nfs if necessary |
| 238 | if [scip-shell system . . `[`. "$restart_nfs" = "yes" ]; then |
| 239 | echoscip-shell system . . echo. "Restarting NFS service..." |
| 240 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 241 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$NFS_SRV_NAME`.NFS_SRV_NAME restart |
| 242 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 243 | fi |
| 244 | ;; |
| 245 | "clean") |
| 246 | echoscip-shell system . . echo. "Now disable the NFS exports for DRBL clients..." |
| 247 | # stop nfs if necessary |
| 248 | if [scip-shell system . . `[`. "$restart_nfs" = "yes" ]; then |
| 249 | echoscip-shell system . . echo. "Stopping NFS service..." |
| 250 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 251 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$NFS_SRV_NAME`.NFS_SRV_NAME stop |
| 252 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 253 | fi |
| 254 | if [scip-shell system . . `[`. -f $nfs_exports ]; then |
| 255 | echoscip-shell system . . echo. "Remove the $nfs_exports..." |
| 256 | [scip-shell system . . `[`. -f "$nfs_exports" ] && mvscip-shell system . . mv. -f $nfs_exports ${nfs_exports}.drblsave |
| 257 | fi |
| 258 | echoscip-shell system . . echo. "done!" |
| 259 | ;; |
| 260 | *) |
| 261 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 262 | exit 1 |
| 263 | esac |