nav-left cat-right
cat-right
Matthias Pospiech > Linux > Local network shares with linux

Local network shares with linux

  1. NFS shares
  2. Configuration files
  3. automount
  4. setup cron job

NFS shares

First NFS needs to be activated (client and server), then we can start with the configuration files.

Configuration files

The exported shares are listed in /etc/exports
/home/links/Musik/      192.168.2.0/255.255.255.0(ro,root_squash,sync)
/home/links/Install/    192.168.2.0/255.255.255.0(ro,root_squash,sync)
/home/matthias/ 192.168.2.0/255.255.255.0(rw,root_squash,sync)
/home/sarah/    192.168.2.0/255.255.255.0(rw,root_squash,sync)
/var/lib/smart  192.168.2.0/255.255.255.0(rw,no_root_squash,sync)
rw means Read and Write, root_squash that no the root rights are mapped to user nobody on the server with a client root accessing the server. This does not happen with no_root_squash. The imported files systems are listed in /etc/fstab
## NFS Server Mounts
matthiasmobil:/home/matthias /home/Exchange/MatthiasMobil/matthias	nfs	 rw,soft     0    0
matthiasmobil:/home/sarah  	 /home/Exchange/MatthiasMobil/sarah    	nfs     rw,soft     0    0
# matthias:/home/matthias     /home/Exchange/Matthias/matthias  	nfs     ro,soft     0    0
# matthias:/home/sarah       	 /home/Exchange/Matthias/sarah     	nfs     rw,soft     0    0
sarah:/home/matthias    	 /home/Exchange/Sarah/matthias  		nfs     rw,soft     0    0
sarah:/home/sarah       	 /home/Exchange/Sarah/sarah     		nfs     rw,soft     0    0
The option soft has to be set, because we are not using a server but simple clients which can be up or down any time. If soft is not set the client will hang until the server is up again. The directories listed in this fstab file have to exsist of course. This should be enough for the configuration. The exports file is reread with „exportfs -a“ and the nfs shares mounted with „mount -a“.

automount

There is however a severe problem. If one share goes down and up the nfs mount is lost. The problem is that NFS does not detect and handle that it goes down nor has it any capability of reconnecting. This feature is usually called automounting. I once implemented a simple script which uses a regular cron job to mount or unmount if necessary. automount.nfs
#!/bin/sh
 
# --------------------------------------------------------- #
function server_exists ()
{
	# ping Optionen:
	# -c : Anzahl von pings
	# -n : nur numerisch, keine DNS Abfragen
	TESTSERVER=$1
	if (ping -c 1 -n -w 2 $TESTSERVER > /dev/null 2>&1) ; then
		# echo "$TESTSEVER: ping ok" 		
		return 0   # Success.		
	else
		# echo "$TESTSEVER: ping nicht ok" 
		return 1   # Failed 
	fi
}  
 
# --------------------------------------------------------- #
function is_mounted ()
{
	# 1. mount aufrufen.
	# 2. Mit grep Zeile mit $MOUNTDIR ausfiltern.
	# 3. Anzahl der Zeilen zaehlen.
	# 4. Alle Leerzeichen entfernen.
	# Wenn eine Zeile vorhanden war, ist der Wert von RETURN 1
 
 
	MOUNTDIR=$1
	TESTVALUE=$(mount | grep $MOUNTDIR | wc -l | tr -d " ")
 
	if [ $TESTVALUE -eq 0 ]
	then
		return 1   # not
	else
		return 0   # is mounted
	fi
}  
# --------------------------------------------------------- #
function do_mount ()
{
	MOUNTDIR=$1
	mount $MOUNTDIR
}  
 
# --------------------------------------------------------- #
function force_unmount ()
{
	MOUNTDIR=$1
	umount -l $MOUNTDIR
}  
# --------------------------------------------------------- #
function dir_exists()
{
	TESTDIR=$1	
	if [ -d "$TESTDIR" ]  # Test if target directory exists.
	then 
		return=0
	else
		return=1
	fi
}  
 
 
# MAIN ---------------------------------------------------- #
 
# if host exists then 
# 	if not mounted do mount
#   if mounted do nothing
# if host does not exist then
#	if not mounted do nothing
#	if mounted force unmount
 
echo ">>" `date -R`
MOUNTBASEDIR=/home/Exchange
HOSTS=(Matthias MatthiasMobil Sarah)
USERS=(matthias sarah Musik Install)
for SERVER in "${HOSTS[@]}"
do
	if (dir_exists "$MOUNTBASEDIR/$SERVER"); then
		echo ">>> teste Server: '$SERVER'"
		if (server_exists $SERVER); then
			echo "> Rechner '$SERVER' existiert"
			for REMOTEUSER in "${USERS[@]}"
			do
				#echo "> teste '$REMOTEUSER'"
				MOUNTDIR="$MOUNTBASEDIR/$SERVER/$REMOTEUSER"
				if [ -d $MOUNTDIR ]
				then
					if is_mounted $MOUNTDIR; then
						echo "> '$REMOTEUSER' von Server '$SERVER' schon gemountet"
					else
						do_mount $MOUNTDIR
						echo "> gemountet: Benutzer '$REMOTEUSER' von Server '$SERVER'"
						echo "[automount.nfs] mountet '$REMOTEUSER' from server '$SERVER' to $MOUNTDIR" >>  $LOGFILE
					fi
				else
					echo "> Fehler: Verzeichnis $MOUNTDIR existiert nicht"
				fi
			done
		else
			echo "> Rechner '$SERVER' existiert NICHT"
			for REMOTEUSER in "${USERS[@]}"
			do
				#echo "> teste '$REMOTEUSER'"
				MOUNTDIR="$MOUNTBASEDIR/$SERVER/$REMOTEUSER"
				if [ -d $MOUNTDIR ]
				then				
					if is_mounted $MOUNTDIR; then
						echo "> erzwinge umount von '$REMOTEUSER' von Server '$SERVER'"
						echo "[automount.nfs] forced umount of '$MOUNTDIR' since nfsserver '$SERVER' down " >>  $LOGFILE
						force_unmount $MOUNTDIR
					else
						echo "> '$REMOTEUSER' von Server '$SERVER' nicht gemountet"
					fi
				else
					echo "> Fehler: Verzeichnis $MOUNTDIR existiert nicht"
				fi
			done
		fi
		echo ""
	else
		echo "> Fehler: Verzeichnis $MOUNTBASEDIR/$SERVER existiert nicht"
	fi
done

setup cron job

A good tutorial for cron can found at Cron – Die Zeitschaltuhr für Linux. The crontab shall be for user root, so we get user rights with „su“. The current crontab list can be retrieved with „crontab -l“. It can be edited with „crontab -e“ which simply open vi. We then insert the line
*/10 * * * * /home/Exchange/automount.nfs >> /home/Exchange/automount.log
This should start the automount.nfs script every 10 minutes.

Einen Kommentar schreiben

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*

This blog is kept spam free by WP-SpamFree.