Here is a little script to create a mount point using CSV file which has a mount point name, size, and VG name.
Caution : Use script on your own risk!
Do not use it on production servers. Test it and use it on newly built/dev/testing servers.
Below is the script code. Save it under /tmp/lvm_script.sh
and also save your CSV file under the same directory with the name list.csv
CSV file format is mount point name,size in GB,VG name. For example : /data,10,data_vg
Script code :
#Script to create mount point using CSV file
#Author : Shrikant Lavhate (kerneltalks.com)
#Save CSV file as list.csv in current working directory with format mount point name,size in GB,VG name
chckfail()
{
if [ $? -ne 0 ];then
echo "Check error above. Halting..."
exit 1
fi
}
for i in `cat list.csv`
do
kt_mountname=`echo $i | cut -d, -f1`
kt_lvname=`echo $i |cut -d, -f1|cut -c 2-|tr / _`
kt_vgname=`echo $i | cut -d, -f3`
kt_lvsize=`echo $i | cut -d, -f2`
kt_lvsize="${kt_lvsize}G"
lvcreate -n $kt_lvname -L $kt_lvsize $kt_vgname >/dev/null
chckfail
mkfs.ext4 /dev/$kt_vgname/$kt_lvname >/dev/null
chckfail
mkdir -p $kt_mountname >/dev/null
chckfail
mount /dev/$kt_vgname/$kt_lvname $kt_mountname>/dev/null
chckfail
echo "/dev/$kt_vgname/$kt_lvname $kt_mountname ext4 defaults 0 0">>/etc/fstab
chckfail
done
Breaking the code :
Quick walk through above code.
- Part one is
chckfail
function which used to check if the command ran is successful or not. If the command failed, it will stop the execution of the script and exits. - Variable part extracts mount point name, size, VG to be used details from CSV file. It also creates LV names out of mount point name in CSV
- Standard LVM commands to create LV, format it with EXT4, create mount point directory, and mount LV on it.
- Finally, it adds an entry to /etc/fstab for the persistent mount.
Modifying script for your requirement :
- If you are using size in MB then remove line
kt_lvsize="${kt_lvsize}G"
- If you are using size in TB then replace
G
withT
in above mentioned line. - If you are using filesystem other than
ext4
then changemkfs.ext4
&/etc/fstab
command accordingly.
HZ says
What do you think about this: https://gist.github.com/haa-zee/48ef0acc1da97cb6c7b68f3935e6eea2 ?
Shrikant Lavhate says
More concise & perfect! Kudos!