Saturday, June 13, 2009

Automated LVM mounting script

After having had enough of walking people through how to mount a LVM from a LiveCD I finally got eternally tired of if. This script was done entirely in self defense, however as it is meant to be run to rescue an installation that was done on LVM, it might be handy for a few others as well as it boils down to just 4 commands ... OK one of them is a chained command, but whose counting.

Please note, this script needs to be run with root priveledges and was written to run using LVM2.

I hope it helps someone else.

p.s. sorry for the text size, but it was the only way to keep the formatting correct.


#! /bin/bash
## This script is released under the GPL version 2
## copyright (2009) James Cook
## Thanks goes to Klaus Knopper who reminded me of something
## very simple that I had forgotten at the time, thanks bud.
## the author may be contacted at:
## azerthoth (at) gmail.com

## Check for user is root
## Thanks to micia for the suggestion
if [ $UID -ne 0 ]; then
echo "You need to be root to run this script!"
exit 1
fi
## get them all into /dev/mapper
modprobe dm-mod 2> /dev/null || :
vgscan --ignorelockingfailure --mknodes || :
vgchange -aly --ignorelockingfailure || return 2
clear
mkdir /LVM
cd /dev/mapper
## Create directories and mount
for FILE in *; do
test -b "$FILE" && mkdir /LVM/$FILE && mount /dev/mapper/$FILE /LVM/$FILE 2>/dev/null
done
## List good partitions
echo "Cleaning up LVMs that were swap partitions or with unsupported"
echo "File Systems from the list. This will not effect those partitions"
echo "There is just no need to list or parse them"
rmdir /LVM/Vol* 2>/dev/null"
echo " "
echo "The following LVM(s) were mounted for you and are ready to use"
echo " "
ls /LVM
echo " "
echo "You can find them in /LVM"