| 1 | #!/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/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 | usage() { |
| 23 | echo "To generate or clean NFS exports for DRBL clients" |
| 24 | echo "Usage: $0 [Options] {generate|clean}" |
| 25 | echo "Options:" |
| 26 | language_help_prompt_by_idx_no |
| 27 | echo "-a, --all-subnet: Make all subnet can access to this NFS server." |
| 28 | echo "-n, --no-restart: Not restart NFS service" |
| 29 | echo "-v, --verbose: Verbose mode." |
| 30 | echo "Example: To generate NFS exports for DRBL clients" |
| 31 | echo "$0 generate" |
| 32 | } |
| 33 | |
| 34 | # |
| 35 | set_nfsd_number() { |
| 36 | local total_nfsd=$1 |
| 37 | if [ -z "$total_nfsd" ]; then |
| 38 | 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 [ -e /etc/debian_version ]; then |
| 45 | # Debian |
| 46 | if [ -f /etc/default/nfs-kernel-server ]; then |
| 47 | # The nfs-kernel-server reads /etc/default/nfs-kernel-server (Sarge) |
| 48 | perl -p -i -e "s/RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /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 [ -f /etc/init.d/nfs-kernel-server ]; then |
| 54 | perl -p -i -e "s/^RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/init.d/nfs-kernel-server |
| 55 | fi |
| 56 | fi |
| 57 | elif [ -e /etc/SuSE-release ]; then |
| 58 | # SUSE |
| 59 | perl -p -i -e "s/^USE_KERNEL_NFSD_NUMBER=.*/USE_KERNEL_NFSD_NUMBER=$total_nfsd/g" /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 grep -qE "\<RPCNFSDCOUNT=.*" /etc/init.d/$NFS_SRV_NAME 2>/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 grep -q "^[#]*[[:space:]]*RPCNFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/null; then |
| 67 | perl -p -i -e "s/^.*RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 68 | else |
| 69 | cat <<-NFSRPC_END > /etc/sysconfig/$NFS_SRV_NAME |
| 70 | RPCNFSDCOUNT=$total_nfsd |
| 71 | NFSRPC_END |
| 72 | fi |
| 73 | elif grep -q "\<NFSDCOUNT=.*" /etc/init.d/$NFS_SRV_NAME 2>/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 grep -q "^[#]*[[:space:]]*NFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/null; then |
| 76 | perl -p -i -e "s/^.*NFSDCOUNT=.*/NFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 77 | else |
| 78 | cat <<-NFSRPC_END > /etc/sysconfig/$NFS_SRV_NAME |
| 79 | NFSDCOUNT=$total_nfsd |
| 80 | NFSRPC_END |
| 81 | fi |
| 82 | elif grep -q "\<RPCNFSDCOUNT=.*" /etc/sysconfig/$NFS_SRV_NAME 2>/dev/null; then |
| 83 | # For systemd-like, E.g. FC17 |
| 84 | perl -p -i -e "s/^.*RPCNFSDCOUNT=.*/RPCNFSDCOUNT=$total_nfsd/g" /etc/sysconfig/$NFS_SRV_NAME |
| 85 | else |
| 86 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 87 | 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 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 89 | fi |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | # load the setting, especiall "drbl_mode". |
| 94 | [ -f "/etc/drbl/drbl_deploy.conf" ] && . /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 [ $# -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 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 113 | 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 | echo "Generating the NFS exports for DRBL clients... " |
| 126 | echo "Backup the original $nfs_exports as $nfs_exports.drblsave" |
| 127 | [ -f "$nfs_exports" ] && mv -f $nfs_exports ${nfs_exports}.drblsave |
| 128 | time_now="$(date "+%T %Y/%m/%d")" |
| 129 | cat <<EOF > $nfs_exports |
| 130 | # Generated by DRBL at $time_now |
| 131 | EOF |
| 132 | if [ "$all_subnet" = "yes" ]; then |
| 133 | # export all subnet to clients |
| 134 | echo "Exporting whole subnet to clients..." |
| 135 | subnet_list="$(get-client-ip-list | awk -F"." '{print $1"."$2"."$3}' | sort | uniq )" |
| 136 | for subnet in $subnet_list; do |
| 137 | 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 [ -d /opt/ ]; then |
| 144 | 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 [ -z "$(echo $ocsroot | grep -Ew "^/home")" -o \ |
| 150 | -n "$(awk -F" " '{print $2}' /proc/mounts | grep -Ew "$ocsroot" | grep -Ev "(^/home|^/opt)")" ]; then |
| 151 | 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 | 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 | cat <<EOF >> $nfs_exports |
| 163 | $d $subnet.*($EXPORTS_NFS_RW_NRS_OPT) |
| 164 | EOF |
| 165 | done |
| 166 | # append one space line |
| 167 | cat <<EOF >> $nfs_exports |
| 168 | |
| 169 | EOF |
| 170 | done |
| 171 | else |
| 172 | # export one by one |
| 173 | echo "Exporting to clients by IP address line-by-line..." |
| 174 | for ip in `get-client-ip-list`; do |
| 175 | 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 [ -d /opt/ ]; then |
| 182 | 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 [ -z "$(echo $ocsroot | grep -Ew "^/home")" -o \ |
| 188 | -n "$(awk -F" " '{print $2}' /proc/mounts | grep -Ew "$ocsroot" | grep -Ev "(^/home|^/opt)")" ]; then |
| 189 | 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 | 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 | cat <<EOF >> $nfs_exports |
| 201 | $d $ip($EXPORTS_NFS_RW_NRS_OPT) |
| 202 | EOF |
| 203 | done |
| 204 | # append one space line |
| 205 | cat <<EOF >> $nfs_exports |
| 206 | |
| 207 | EOF |
| 208 | done |
| 209 | fi |
| 210 | |
| 211 | # export for every client's etc, var |
| 212 | if [ "$drbl_mode" = "full_drbl_mode" -o "$clonezilla_mode" = "full_clonezilla_mode" ]; then |
| 213 | echo "Full DRBL or Full Clonezilla mode, exporting client's directories etc, var, root..." |
| 214 | for ip in `get-client-ip-list`; do |
| 215 | [ ! -d "$drblroot/$ip/" ] && continue |
| 216 | [ -n "$verbose" ] && echo "Creating exports for directories etc, var, root of node $ip..." |
| 217 | cat <<EOF >> $nfs_exports |
| 218 | $drblroot/$ip/ $ip($EXPORTS_NFS_RW_NRS_OPT) |
| 219 | EOF |
| 220 | done |
| 221 | fi |
| 222 | |
| 223 | echo "The $nfs_exports setting is ok now!" |
| 224 | # rm /var/lib/nfs/rmtab to avoid a long time try when restart NFS |
| 225 | [ -f /var/lib/nfs/rmtab ] && rm -f /var/lib/nfs/rmtab |
| 226 | |
| 227 | # calculate the number of the NFS daemon |
| 228 | nfsd_no="$(get-necessary-nfsd-no)" |
| 229 | if [ -z "$nfsd_no" ]; then |
| 230 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 231 | echo "Unable to get necessary NFSD number, program terminated!!!" |
| 232 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 233 | exit 1 |
| 234 | fi |
| 235 | set_nfsd_number $nfsd_no |
| 236 | |
| 237 | # restart nfs if necessary |
| 238 | if [ "$restart_nfs" = "yes" ]; then |
| 239 | echo "Restarting NFS service..." |
| 240 | echo "$msg_delimiter_star_line" |
| 241 | /etc/init.d/$NFS_SRV_NAME restart |
| 242 | echo "$msg_delimiter_star_line" |
| 243 | fi |
| 244 | ;; |
| 245 | "clean") |
| 246 | echo "Now disable the NFS exports for DRBL clients..." |
| 247 | # stop nfs if necessary |
| 248 | if [ "$restart_nfs" = "yes" ]; then |
| 249 | echo "Stopping NFS service..." |
| 250 | echo "$msg_delimiter_star_line" |
| 251 | /etc/init.d/$NFS_SRV_NAME stop |
| 252 | echo "$msg_delimiter_star_line" |
| 253 | fi |
| 254 | if [ -f $nfs_exports ]; then |
| 255 | echo "Remove the $nfs_exports..." |
| 256 | [ -f "$nfs_exports" ] && mv -f $nfs_exports ${nfs_exports}.drblsave |
| 257 | fi |
| 258 | echo "done!" |
| 259 | ;; |
| 260 | *) |
| 261 | usage |
| 262 | exit 1 |
| 263 | esac |