<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>k3mist™ &#187; mount point</title>
	<atom:link href="http://k3mist.com/tag/mount-point/feed/" rel="self" type="application/rss+xml" />
	<link>http://k3mist.com</link>
	<description>Development, Design &#38; System Administration</description>
	<lastBuildDate>Sat, 12 Sep 2009 15:40:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bash script to automate mounting and unmounting of nfs shares</title>
		<link>http://k3mist.com/linux/bash-script-automate-mount-unmount-nfs-share/</link>
		<comments>http://k3mist.com/linux/bash-script-automate-mount-unmount-nfs-share/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 17:06:47 +0000</pubDate>
		<dc:creator>k3mist</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mount]]></category>
		<category><![CDATA[mount point]]></category>
		<category><![CDATA[nfs share]]></category>
		<category><![CDATA[showmount]]></category>
		<category><![CDATA[umount]]></category>

		<guid isPermaLink="false">http://k3mist.com/?p=111</guid>
		<description><![CDATA[<p>This is a small script I wrote that automates creating a directory for the mount point and then mounts the nfs share on that diretory. It also accommodates removal when needed and unmount&#8217;s the nfs share and removes the directory that was created.</p>
<p><span id="more-111"></span></p>
<p><strong>Notes:</strong></p>
<p>Caution: The script assumes the nfs share is not an existing directory on the local machine. It uses the same mount points on the the local machine as the remote machine.</p>
<p>All mount points are located in <code>/media</code>. I did this to keep in unison with Ubuntu.</p>
<p>This was convenient for me but may not play well on other setups. Simply ask and I can modify the script to accommodate different setups easily. You&#8217;re also welcome to change it yourself <img src='http://k3mist.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Before using this script, you should make sure that your nfs shares are setup correctly and functioning without this script.</p>
<p><strong>Line 22:</strong> Replace this with your username:group on your local machine</p>
<p><strong>Lines 13 &amp; 24:</strong> Replace x.x.x with your local C class. EX: 192.168.1</p>
<p><strong>Usage:</strong></p>
<p>Make sure the script is executable with <code>chmod +x</code></p>
<p><strong>Mounting</strong></p>
<p><code>./filename 100 folder</code></p>
<ul>
<li>filename is self-explanatory I think.</li>
<li>100 is the last octet of the IP address that the nfs share is located on</li>
<li>folder is the remote mount point and local mount point for mounting</li>
</ul>
<p><strong>Unmounting</strong></p>
<p><code>./filename u folder</code><br />
<ul>
<li>u simply means unmount</li>
<li>folder is the mount location on the local machine</li>
</ul>
<textarea class="java" cols="30" rows="5" name="code">
#!/bin/bash

# MOUNT: ./mnt 100 folder
# UNMOUNT: ./mnt u folder

if [ $1 != "" ] &#038;&#038; [ $2 != "" ]; then

  DIR=/media/$2
  EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
  EXISTDIR=`ls /media/ | grep $2`;

  if [ $1 != "u" ]; then
    EXISTREMDIR=`showmount -e x.x.x.$1 | grep $DIR`;
  fi

  if [[ "$EXISTMOUNT" != *$DIR* ]] &#038;&#038; [ $1 != "u" ]  &#038;&#038;
     [[ "$EXISTREMDIR" = *$DIR* ]]; then
    
    echo &#8220;Creating: $DIR&#8221;
    sudo mkdir $DIR
    sudo chown username:group $DIR
    echo &#8220;done.&#8221;
    echo &#8220;Mounting: $DIR&#8221;
    sudo mount x.x.x.$1:$DIR $DIR
    echo &#8220;done.&#8221;
  
  fi
  
  if [ "$EXISTDIR" = $2 ] &#038;&#038; [ $1 = "u" ]; then
    
    echo &#8220;Unmounting: $DIR&#8221;
    sudo umount $DIR;
    echo &#8220;done.&#8221;
    
    # failsafe directory removal
    EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
    if [[ "$EXISTMOUNT" != *$DIR* ]]; then
      echo &#8220;Removing: $DIR&#8221;
      sudo rmdir $DIR
      echo &#8220;done.&#8221;
    fi
    
  fi
  
fi
</textarea>
<pre style="display:none;">
#!/bin/bash

# MOUNT: ./mnt 100 folder
# UNMOUNT: ./mnt u folder

if [ $1 != "" ] &#038;&#038; [ $2 != "" ]; then

  DIR=/media/$2
  EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
  EXISTDIR=`ls /media/ | grep $2`;

  if [ $1 != "u" ]; then
    EXISTREMDIR=`showmount -e 10.0.0.$1 | grep $DIR`;
  fi

  if [[ "$EXISTMOUNT" != *$DIR* ]] &#038;&#038; [ $1 != "u" ]  &#038;&#038;
     [[ "$EXISTREMDIR" = *$DIR* ]]; then
    
    echo "Creating: $DIR"
    sudo mkdir $DIR
    sudo chown username:group $DIR
    echo "done."
    echo "Mounting: $DIR"
    sudo mount 10.0.0.$1:$DIR $DIR
    echo "done."
  
  fi
  
  if [ "$EXISTDIR" = $2 ] &#038;&#038; [ $1 = "u" ]; then
    
    echo "Unmounting: $DIR"
    sudo umount $DIR;
    echo "done."
    
    # failsafe directory removal
    EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
    if [[ "$EXISTMOUNT" != *$DIR* ]]; then
      echo "Removing: $DIR"
      sudo rmdir $DIR
      echo "done."
    fi
    
  fi
  
fi
</pre>]]></description>
			<content:encoded><![CDATA[<p>This is a small script I wrote that automates creating a directory for the mount point and then mounts the nfs share on that diretory. It also accommodates removal when needed and unmount&#8217;s the nfs share and removes the directory that was created.</p>
<p><span id="more-111"></span></p>
<p><strong>Notes:</strong></p>
<p>Caution: The script assumes the nfs share is not an existing directory on the local machine. It uses the same mount points on the the local machine as the remote machine.</p>
<p>All mount points are located in <code>/media</code>. I did this to keep in unison with Ubuntu.</p>
<p>This was convenient for me but may not play well on other setups. Simply ask and I can modify the script to accommodate different setups easily. You&#8217;re also welcome to change it yourself <img src='http://k3mist.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Before using this script, you should make sure that your nfs shares are setup correctly and functioning without this script.</p>
<p><strong>Line 22:</strong> Replace this with your username:group on your local machine</p>
<p><strong>Lines 13 &amp; 24:</strong> Replace x.x.x with your local C class. EX: 192.168.1</p>
<p><strong>Usage:</strong></p>
<p>Make sure the script is executable with <code>chmod +x</code></p>
<p><strong>Mounting</strong></p>
<p><code>./filename 100 folder</code></p>
<ul>
<li>filename is self-explanatory I think.</li>
<li>100 is the last octet of the IP address that the nfs share is located on</li>
<li>folder is the remote mount point and local mount point for mounting</li>
</ul>
<p><strong>Unmounting</strong></p>
<p><code>./filename u folder</code><br />
<ul>
<li>u simply means unmount</li>
<li>folder is the mount location on the local machine</li>
</ul>
<textarea class="java" cols="30" rows="5" name="code">
#!/bin/bash

# MOUNT: ./mnt 100 folder
# UNMOUNT: ./mnt u folder

if [ $1 != "" ] &#038;&#038; [ $2 != "" ]; then

  DIR=/media/$2
  EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
  EXISTDIR=`ls /media/ | grep $2`;

  if [ $1 != "u" ]; then
    EXISTREMDIR=`showmount -e x.x.x.$1 | grep $DIR`;
  fi

  if [[ "$EXISTMOUNT" != *$DIR* ]] &#038;&#038; [ $1 != "u" ]  &#038;&#038;
     [[ "$EXISTREMDIR" = *$DIR* ]]; then
    
    echo &#8220;Creating: $DIR&#8221;
    sudo mkdir $DIR
    sudo chown username:group $DIR
    echo &#8220;done.&#8221;
    echo &#8220;Mounting: $DIR&#8221;
    sudo mount x.x.x.$1:$DIR $DIR
    echo &#8220;done.&#8221;
  
  fi
  
  if [ "$EXISTDIR" = $2 ] &#038;&#038; [ $1 = "u" ]; then
    
    echo &#8220;Unmounting: $DIR&#8221;
    sudo umount $DIR;
    echo &#8220;done.&#8221;
    
    # failsafe directory removal
    EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
    if [[ "$EXISTMOUNT" != *$DIR* ]]; then
      echo &#8220;Removing: $DIR&#8221;
      sudo rmdir $DIR
      echo &#8220;done.&#8221;
    fi
    
  fi
  
fi
</textarea>
<pre style="display:none;">
#!/bin/bash

# MOUNT: ./mnt 100 folder
# UNMOUNT: ./mnt u folder

if [ $1 != "" ] &#038;&#038; [ $2 != "" ]; then

  DIR=/media/$2
  EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
  EXISTDIR=`ls /media/ | grep $2`;

  if [ $1 != "u" ]; then
    EXISTREMDIR=`showmount -e 10.0.0.$1 | grep $DIR`;
  fi

  if [[ "$EXISTMOUNT" != *$DIR* ]] &#038;&#038; [ $1 != "u" ]  &#038;&#038;
     [[ "$EXISTREMDIR" = *$DIR* ]]; then
    
    echo "Creating: $DIR"
    sudo mkdir $DIR
    sudo chown username:group $DIR
    echo "done."
    echo "Mounting: $DIR"
    sudo mount 10.0.0.$1:$DIR $DIR
    echo "done."
  
  fi
  
  if [ "$EXISTDIR" = $2 ] &#038;&#038; [ $1 = "u" ]; then
    
    echo "Unmounting: $DIR"
    sudo umount $DIR;
    echo "done."
    
    # failsafe directory removal
    EXISTMOUNT=`sudo cat /etc/mtab | grep $DIR`;
    if [[ "$EXISTMOUNT" != *$DIR* ]]; then
      echo "Removing: $DIR"
      sudo rmdir $DIR
      echo "done."
    fi
    
  fi
  
fi
</pre>]]></content:encoded>
			<wfw:commentRss>http://k3mist.com/linux/bash-script-automate-mount-unmount-nfs-share/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
