Monday, September 24, 2018

Automatically Mount a Shared Drive using autofs

Recently we faced a strange issue while working with our Jenkins build. As a part of the Jenkins build we copy our libraries to a shared windows drive that is mounted on our Jenkins Server. This used to work fine until there was a network issue which caused our build to fail coz the shared drive is unmounted. We started working on making sure that that mount drives are available when doing a build. We then started to work on a strategy to mount drives when needed and also to make sure drives are mounted automatically when they are unmounted due to network or any other issues. 

This is where autofs comes into picture. We generally add the mount details to the fstab file. Autofs is more intelligent then fstab because of following main advantages, 
  1. Mounted remote resource are automatically unmounted after a period of inactivity 
  2. Unmounted resources are automatically mounted again when you try to access the mount point 
  3. We don’t need to create a mount point before hand, only the mount parent directories 

Install the autofs package using 
yum install cifs-utils autofs 

Once we install, we will see a auto.master file in the /etc location. This is file where will mount location, time to wait etc. Let's add the content to 
/etc/auto.master file as 
/mnt/server    /etc/auto.cifsshares  --timeout=30 –ghost 

This file contains a list of location that need to be automounted under the /mnt/server location. The actual mount point will be configured in the /etc/auto.cifsshares file 
The /etc/auto.cifsshares file look as, 
[root@wabld-agnt-ci2 etc]# cat auto.cifsshares 
GAL   -fstype=cifs,rw,_netdev,noserverino,file_mode=0755,dir_mode=0755,
vers=1.0, credentials=/root/.cifs_credentials,uid=995,iocharset=utf8    ://generic/GAPA 
 ( space between GAL and -fstype and also before ://generic/GAPA)

The above content says that mount a location called “GAL” under the directory “/mnt/server” ( defined in the auto.master ) with type as cifs. It also says to use a credentials file called “/root/.cifs_credentials” for user name and password to mount the path. 
Create a credential file as .cifs_credentials with user name and password as, 
[root@wabld-agnt-ci2 ~]# cat  .cifs_credentials 

The mount location that we are trying to mount on this server is from //generic/GAPA.

So once we run this autofs server , we will be able to see all the file under the //generic/GAPA under the /mnt/server/GAL directory.  The timeout=30 says to unmount the drive after 30 minutes of inactivity. Start the service using “service autofs restart” 

Once configured start the service using the service autofs restart and check the mount command which will be, 
[root@agentci2 ~]# mount | grep nas 
/etc/auto.cifsshares on /generic type autofs (rw,relatime,fd=24,pgrp=13732,timeout=30,minproto=5,maxproto=5,indirect) 

This is how we will be configuring the autofs service on a centos machine. More to Come. Happy learning :-)

No comments :

Post a Comment