#!/bin/bash # vars EXPORTS_FILE="/etc/exports" BEGIN_LLIUREX="### BEGIN LLIUREX EXPORTED FILESYSTEMS" END_LLIUREX="### END LLIUREX EXPORTED FILESYSTEMS" #ocfs2 vars VAR_LIST="OCFS2_NET_EXPORT_DIRS LLIUREX_PXE_FLAVOURS" LLX_GETVAR="/usr/sbin/llxcfg-showvars" eval `${LLX_GETVAR} ${VAR_LIST}` # funcs normalize_dir(){ echo "$1" |sed -e "s%^%/%;s%/\{2,\}%/%g;s%/$%%" } mount_test(){ if ! mount |grep -q "^${1}[[:blank:]]\+on[[:blank:]]\+${2}\b" ; then mount --bind "$1" "$2" &> /dev/null || return 1 fi return 0 } mount_check_umount(){ if mount |grep -q "^${1}[[:blank:]]\+on[[:blank:]]\+${2}\b" ; then umount -l "$2" &> /dev/null || true fi } system_keytab(){ echo "nfs/server.${SRV_DOMAIN}" return 0 } client_keytab(){ system_keytab case $NFS_POLICY in strict) get_dhcp_host_list |sed -ne "/\(^[^,]\+,\)\{3\}/{s%^%nfs/%;s%,.*$%%;s%$%.${SRV_DOMAIN}%p}" ;; medium) get_dhcp_host_list |sed -e "s%^%nfs/%;s%,.*$%%;s%$%.${SRV_DOMAIN}%" ;; loose) get_dhcp_host_list |sed -e "s%^%nfs/%;s%,.*$%%;s%$%.${SRV_DOMAIN}%" get_noreg_host_list |sed -e "s%^%nfs/%;s%,.*$%%;s%$%.${SRV_DOMAIN}%" ;; esac } gen_exports(){ # get current exports or original one: if [ -r "$EXPORTS_FILE" ] ; then sed -e "/^${BEGIN_LLIUREX}$/,/^${END_LLIUREX}$/d" "$EXPORTS_FILE" else llxcfg-cpkg plainview "$EXPORTS_FILE" || true fi # add begin mark echo "$BEGIN_LLIUREX" llxcfg-config dump nfs-server/exports-raw # read exports conffiles llxcfg-config dump nfs-server/exports-live |sed -ne "/${VALIDATE_REGEXP}/p" |while read nfs nmnt nrw nopts ; do # normalize data nfs="$(normalize_dir "$nfs")" nmnt="$(normalize_dir "$nmnt")" # test fs if [ -d "$nfs" ] && [ -d "$nmnt" ]; then # test / mount fs if mount_test "${nfs}" "${nmnt}" ; then NOPTS="_@_NFS_EXPORT_OPTIONS_@_" [ -z "$nopts" ] || NOPTS="$nopts" echo "${nmnt} *(${nrw},${NOPTS})" fi elif [ ! -d "$nfs" ]; then mount_check_umount "${nfs}" "${nmnt}" fi done # add a line for "root" export directory [ -d "$NFS_EXPORT_DIR" ] || mkdir -p "$NFS_EXPORT_DIR" echo "_@_NFS_EXPORT_DIR_@_ gss/krb5(ro,fsid=0,_@_NFS_EXPORT_OPTIONS_@_)" llxcfg-config dump nfs-server/exports |sed -ne "/${VALIDATE_REGEXP}/p" |while read nfs nmnt nrw nopts ; do # normalize data nfs="$(normalize_dir "$nfs")" nmnt="$(normalize_dir "$nmnt")" # test fs if [ -d "$nfs" ] ; then # test mount point under /export [ -d "${NFS_EXPORT_DIR}${nmnt}" ] || mkdir -p "${NFS_EXPORT_DIR}${nmnt}" # test / mount fs if mount_test "${nfs}" "${NFS_EXPORT_DIR}${nmnt}" ; then NOPTS="_@_NFS_EXPORT_OPTIONS_@_" [ -z "$nopts" ] || NOPTS="$nopts" ##Check if OCFS2 is defined if [ "$OCFS2_NET_EXPORT_DIRS" ] ; then for ocfs_dir in $OCFS2_NET_EXPORT_DIRS; do echo "${NFS_EXPORT_DIR}/net/${ocfs_dir} gss/krb5(${nrw},${NOPTS})" done fi ##End OCFS2 echo "${NFS_EXPORT_DIR}${nmnt} gss/krb5(${nrw},${NOPTS})" # generate netmount file # echo "server:/ ${nmnt} nfs4 sec=krb5,proto=tcp,port=2049" >> "${NETMOUNT_FILE}" echo "server:${nmnt} ${nmnt} nfs4 sec=krb5,proto=tcp,port=2049,noatime,nodiratime" >> "${NETMOUNT_FILE}" fi fi done echo "$END_LLIUREX" } # main VALIDATE_REGEXP="^\([^[:blank:]#]\+[[:blank:]]\+\)\{2\}\(ro\|rw\)\([[:blank:]]\+[^[:blank:]]\+\)*" VAR_LIST="NFS_EXPORT_DIR NFS_POLICY SRV_DOMAIN" eval "$(llxcfg-showvars $VAR_LIST)" # libreria LIB_FILE="/usr/share/lliurex/lliurex-srv-common/lliurex-netfuncs.sh" [ -e "$LIB_FILE" ] || exit 1 . $LIB_FILE [ "$NFS_EXPORT_DIR" ] || exit 1 NFS_EXPORT_DIR="$(normalize_dir "$NFS_EXPORT_DIR")" # install templates llxcfg-config list nfs-server/templates |while read rname ; do llxcfg-config read "nfs-server/templates/$rname" |skel-install -t "/etc/$rname" done # generate exports NETMOUNT_FILE="./netmount" :> "$NETMOUNT_FILE" gen_exports |skel-install -t "$EXPORTS_FILE" # export mounts to netmount client via netconfig cat "$NETMOUNT_FILE" |llxcfg-config write "netconfig/conffiles/netmount/nfs-server" # generate krb5 server conffile to generate keytabs for clients (via system keytab) # TODO: separate clients keytab # system_keytab |llxcfg-config write krb5-server/system-keytab/nfs-server # client_keytab |llxcfg-config write krb5-server/client-keytab/nfs-server # TOD export system keytab exit 0