Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- srcmnt="192.168.2.10:/mnt/storage/"
- dstmnt="/home/zefie/terra/"
- mounts=( anime homevid isos kenzi-backup share tvshows )
- HOST=$(echo -n $srcmnt | cut -d':' -f1)
- srcdown=1;
- srcsrvup=0;
- nmap=$(which nmap);
- ping=$(which ping);
- user=$(whoami)
- check_host() {
- echo -n " * Waiting for $HOST "
- while [ $srcdown -ne "0" ]; do
- $ping -q -c 1 -w 1 $HOST &> /dev/null; srcdown=$?;
- if [ $srcdown -eq "0" ]; then
- break;
- fi
- echo -n '.'; sleep 3;
- done
- while [ $srcsrvup -ne "1" ]; do
- srcsrvup=$($nmap -sT -p 2049 -P0 -T5 -n $HOST | grep open | wc -l)
- if [ $srcsrvup -eq "1" ]; then
- break;
- fi
- echo -n '.'; sleep 3;
- done
- echo " Up."
- }
- mount_nfs() {
- if [ $(echo -n $nmap | wc -m) -le 1 ]; then
- echo "Error: nmap required";
- exit;
- fi
- if [ $(echo -n $ping | wc -m) -le 1 ]; then
- echo "Error: ping required";
- exit;
- fi
- for i in "${mounts[@]}"; do
- echo " * Preparing to mount ${srcmnt}${i} ..."
- if [ $(echo $srcsrvup) -ne "1" ]; then
- check_host
- fi
- if [ ! -d ${dstmnt}${i} ]; then
- mkdir -p ${dstmnt}${i}
- fi
- if [ "$(grep ${srcmnt}${i} /etc/mtab | wc -l)" -ne "1" ]; then
- skipmount=0
- echo -n " * Mounting ${srcmnt}${i} on ${dstmnt}${i} "
- while [ "$skipmount" -eq "0" ]; do
- if [ ! "$(grep ${srcmnt}${i} /etc/mtab)" ]; then
- if [ "$user" = "root" ]; then
- echo -n "as root..."
- mount -t nfs ${srcmnt}${i} ${dstmnt}${i} &> /dev/null
- else
- if [ "$(grep ${srcmnt}${i} /etc/fstab | grep user | wc -l)" -eq "1" ]; then
- echo -n "as non-root user ${user}..."
- mount ${srcmnt}${i}
- else
- echo ""
- echo " * Could not mount ${srcmnt}${i} .. You are not root, and mount is not user mountable in /etc/fstab"
- skipmount=1
- fi
- fi
- if [ "$skipmount" -ne "1" ]; then
- if [ "$(grep ${srcmnt}${i} /etc/mtab | wc -l)" -ne "1" ]; then
- echo -n "."
- sleep 10
- else
- echo " Done."
- skipmount=1
- fi
- fi
- fi
- done
- else
- echo " * Already mounted: ${srcmnt}${i} "
- fi
- done
- }
- umount_nfs () {
- for i in "${mounts[@]}"; do
- echo " * Preparing to unmount ${srcmnt}${i} ..."
- skipmount=0
- if [ "$(grep ${srcmnt}${i} /etc/mtab | wc -l)" -eq "1" ]; then
- dstmnt=$(grep ${srcmnt}${i} /etc/mtab | cut -d' ' -f 2);
- echo -n " * Unmounting ${srcmnt}${i} from $dstmnt "
- while [ "$skipmount" -eq "0" ]; do
- if [ "$(grep ${srcmnt}${i} /etc/mtab | wc -l)" -eq "1" ]; then
- if [ "$user" = "root" ]; then
- echo -n "as root..."
- umount $dstmnt &> /dev/null
- else
- if [ "$(grep ${srcmnt}${i} /etc/fstab | grep user | wc -l)" -eq "1" ]; then
- echo -n "as non-root user ${user}..."
- umount ${srcmnt}${i}
- else
- echo ""
- echo " * Could not unmount ${srcmnt}${i} .. You are not root, and mount is not user mountable";
- skipmount=1
- fi
- fi
- if [ "$(grep ${srcmnt}${i} /etc/mtab | wc -l)" -eq "1" ]; then
- echo -n "."
- sleep 10
- else
- echo " Done."
- skipmount=1
- fi
- fi
- done
- else
- echo " * Not mounted: ${srcmnt}${i} "
- fi
- done
- }
- case "$1" in
- "mount")
- mount_nfs
- ;;
- "umount")
- umount_nfs
- ;;
- "unmount")
- umount_nfs
- ;;
- *)
- echo "Usage: $0 [mount|umount]"
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement