Monday, December 30, 2013

Setting up NFS

With NFS you can share folders on network and mount them on other computers just like local filesystems. I'm using NFS shared folder for transmission daemon's download folder and for owncloud.

Basic configuration is done in file /etc/exports, where you can specify shared folders:
#Arch-KrisKo exports
/mnt/transmission 172.16.0.2(rw,sync,no_subtree_check)
/mnt/owncloud 172.16.0.2(rw,sync,no_subtree_check)
/usr/share/webapps/owncloud 172.16.0.2(rw,sync,no_subtree_check)

In my exports file I have 3 mountpoints for host 172.16.0.2 (which is my PC).

Next step is to start/enable nfsd service:
systemctl start/enable nfsd
systemctl start/enable rpc-mountd
NOTE: on current archlinux run:
systemctl start/enable nfs-server

NOTE: if you are using iptables you need to enable few ports for nfs to work:
## Allow NFS
-A TCP -p tcp --dport 111 -j ACCEPT
-A UDP -p udp --dport 111 -j ACCEPT
-A TCP -p tcp --dport 829 -j ACCEPT
-A UDP -p udp --dport 829 -j ACCEPT
-A TCP -p tcp --dport 20048 -j ACCEPT
-A UDP -p udp --dport 20048 -j ACCEPT
-A TCP -p tcp --dport 2049 -j ACCEPT
-A UDP -p udp --dport 2049 -j ACCEPT

Now all you need to do is start rpcbind service on your computer:
systemctl start/enable rpcbind
After this you should be able to mount given mountpoints:
mount 172.16.0.1:/mnt/transmission /your/mountpoint

Few useful commands:
exportfs -view active exports
exportfs -r -reexport all directories (use this if you've made changes to /etc/exports)

To mount NFS shared folders, I use following script:
#!/bin/bash

usage(){
echo "USAGE: $0 (option)"
echo -e "OPTIONS:"
echo -e " t\ttransmission"
echo -e " o\towncloud"
echo -e " "
exit 0
}

if [ "x$1" == "x" ]; then
        usage
fi

#we need to have privileges
if [ "$UID" != 0 ]; then
        exec sudo $0 $1
fi

#if rpcbind is not running, start it
if [ $(pgrep rpcbind)x == "x" ]; then
    echo Starting rpcbind service...
    systemctl start rpcbind
fi

echo Mounting remote NFS share...
case "$1" in
    "t" )   mount -o soft 172.16.0.1:/mnt/transmission /mnt/transmission; echo DONE;;
    "o" )   if [ $(\mount | grep -cs /usr/share/webapps/owncloud) == 1 ]; then
                echo INFO: /usr/share/webapps/owncloud is already mounted
            else
                echo INFO: mounting 172.16.0.1:/usr/share/webapps/owncloud
                mount -o soft 172.16.0.1:/usr/share/webapps/owncloud /usr/share/webapps/owncloud || read -p "FAILED, press CTRL-C to exit"
            fi
            if [ $(\mount | grep -cs /mnt/owncloud) == 1 ]; then
                echo INFO: /mnt/owncloud is already mounted
            else
                echo INFO: mounting 172.16.0.1:/mnt/owncloud
                mount -o soft 172.16.0.1:/mnt/owncloud /mnt/owncloud || read -p "FAILED, press CTRL-C to exit"
            fi
            echo Starting php-fpm service... && systemctl start php-fpm && echo DONE || echo FAILED;;
    * ) echo Unknown option.; usage;;
esac

exit 0
NOTE: for owncloud mount a php-fpm instance is started, this serves for php-fpm cluster and it will be desribed in later post.

No comments:

Post a Comment