Bash: Automatically Mount File Systems on Volume Group if Present
In my laptop I have a solid state disk, and frankly I am way past addicted to solid state disks, but what you get in performance you lose in capacity. As such I have had to be creative with how I can have the capacity and the performance that I need. Lately the solution has been to have a spinning disk in the slot that would normally be occupied by my CD/DVD drive. However the problem with this, revealed itself to be that my Fedora install would actually fail to boot when it tried and failed to mount the file systems if this device was not present. So my first “solution” was to set noauto in the /etc/fstab for these secondary file systems. Which presented a new problem… I would forget to manually mount these devices before launching Virtualbox to start my VMs. So I decided that I would need to have an auto-mount script which would check for the volume group to be present and then if it was mount the file systems contained within the /etc/fstab which live on the volume group.
Create the Service Script
Below you will see the script I created. This script will test for the presence of a volume group, and if present on invocation (boot up or manual) then it will mount each logical volume defined in the /etc/fstab of the present volume group.
Name : custom_mount.sh
Version : 1.0.3
MD5 : 0b8cf3b0b5c9d3c16e4384fde88e8659
SHA256 : 877ef66ab464e71172986fa9cd5510f0ad6752f05a414af703878d7e058c845b
URL : http://source.allanglesit.net/pub/custom_mount.sh
#!/bin/bash # chkconfig: 345 85 15 # description: Will automatically mount a removable device if present. # #: Script Name : custom_mount.sh #: Version : 1.0.3 #: Author : Matthew Mattoon - http://blog.allanglesit.com #: Date Created : August 15, 2012 #: Date Updated : February 20, 2013 #: Description : Automount Removable Logical Volumes Script. #: Examples : custom_mount.sh ACTION #: : custom_mount.sh start vgtest="vg_data" vgs=`vgs | grep $vgtest` case "$1" in start) if [ -n "$vgs" ] then echo "Logical Volume Group: $vgtest present." mounts=`cat /etc/fstab | grep $vgtest | grep noauto | tr '\t' ' ' | tr -s ' ' | cut -d " " -f 2` for mount in $mounts do if [ -z "`mount | grep $mount`" ] then echo "Mounting $mount file system..." mount $mount else echo "File system $mount is already mounted..." fi done else echo "Logical Volume Group: $vgtest not present." exit 1 fi ;; stop) if [ -n "$vgs" ] then echo "Logical Volume Group: $vgtest present." mounts=`cat /etc/fstab | grep $vgtest | grep noauto | tr '\t' ' ' | tr -s ' ' | cut -d " " -f 2` for mount in $mounts do if [ -n "`mount | grep $mount`" ] then echo "Unmounting $mount file system..." umount $mount else echo "File system $mount is already unmounted..." fi done else echo "Logical Volume Group: $vgtest not present." exit 1 fi ;; *) echo "Usage: $0 start" exit 1 esac
Create the Service to Auto Start
Based on my /etc/init.d/custom_mount script it will start in run levels 3, 4, and 5.
# chkconfig --add custom_mount