Wednesday, May 21, 2014

LVM automatic FileSystem creation

Instead of adding entry to the file, you may want to have interactive one to prompt.

# cat /tmp/a
# vg    FS      Size
#-------------------
datavg  WEB     30G
datavg  WEBDATA 50G
datavg  WEB_DEV        30G
datavg  WEB_DATA        30G
datavg  PKGS    20G

]# cat myFS.sh
#!/bin/bash
# Kamal
# Creating FS using LVM on Linux
#
set -e
cat /tmp/a | grep -v "^#" | while read myvg myfs mysize
do
echo " Creating $myfs Volume"
#echo lvcreate -L ${mysize} -n ${myfs} ${myvg}
lvcreate -L ${mysize} -n ${myfs} ${myvg}
mkfs.ext4 /dev/${myvg}/${myfs}
#mkfs.ext4 /dev/${myvg}/${myfs}
# EOF
done

# cat mymounts.sh
mkdir -p /web /webdata /webc/dev /webcdata /pkgs
 echo " ================================================"
sleep 2
 echo "Backing up /etc/fstab file"
cp -p /etc/fstab /etc/fstab.05.16.2014
sleep 2
 echo "#################################" >>/etc/fstab
 echo "/dev/datavg/WEB         /web                    ext4    defaults      1 2" >>/etc/fstab
 echo "/dev/datavg/WEBDATA         /webdata            ext4    defaults        1 2" >>/etc/fstab
 echo "/dev/datavg/WEB_DEV         /webc/dev          ext4    defaults        1 2" >>/etc/fstab
 echo "/dev/datavg/WEB_DATA         /webcdata          ext4    defaults        1 2 " >>/etc/fstab
 echo "/dev/datavg/PKGS         /pkgs                  ext4    defaults    1 2" >>/etc/fstab
 echo " Mounting filesystems"
sleep 2
mount -a
sleep 2
 echo "Displaying currently mounted fs"
df -h /web /webdata /webc/dev /webcdata /pkgs
echo "Task completed"

echo "================================================="
echo "Backing up /etc/fstab file"
cp -p /etc/fstab /etc/fstab.05.16.2014
sleep 2
echo "#################################" >>/etc/fstab
echo "/dev/datavg/WEB         /web                    ext4    defaults      1 2" >>/etc/fstab
echo "/dev/datavg/WEBDATA         /webdata            ext4    defaults        1 2" >>/etc/fstab
echo "/dev/datavg/WEB_DEV         /webc/dev          ext4    defaults        1 2" >>/etc/fstab
echo "/dev/datavg/WEB_DATA         /webcdata          ext4    defaults        1 2 " >>/etc/fstab
echo "/dev/datavg/PKGS         /pkgs                  ext4    defaults    1 2" >>/etc/fstab
sleep 2
echo " "
echo "Creating mount points"
mkdir -p /web /webdata /webc/dev /webcdata /pkgs
echo " "
echo " Mounting filesystems"
sleep 2
mount -a
sleep 2
echo "Displaying currently mounted fs"
df -h /web /webdata /webc/dev /webcdata /pkgs
echo "Task completed"
echo "Now changing ownership"
chown -R edsmgr:edsgrp /web /webdata /webc/dev /webcdata /pkgs


===================================================


1. Files on this task
 a. Script: lvm_create.sh
 b. config file: LVM_WEBLOGIC
2. Put both file on same directory
or
- create two directories bin and etc.
- put the script under bin dir and config under etc.

# cat lvm_create.sh
#!/bin/sh
# SAM Bhusal
# Automate FS creation task. This script tested and verified on Redhat Enterprise 6.x version.
# Config file entry eg.
# # file_name: LVM_WEBLOGIC
# # For Weblogic
# # vg    FS      Size(GB) Mnt point
# --------------------------------
# datavg  WEB     10     /web
# datavg  WEBDATA 15     /webdata
#
# Mon Feb 24 12:13:24 EST 2014
# Update: Thu Aug 14 09:55:12 EDT 2014
# Added the entry for mount point on config file.
# Added entry to the fstab.
#
if [ `/usr/bin/whoami` != "root" ]
then
        echo "You must be root to run this script"
        exit 1
fi
# Creating LVM.
/bin/cat LVM_WEBLOGIC | grep -v "^#" | while read myvg myfs mysize mymt
do
        echo " "
        # Check if mount point already exists.
        #/bin/df -h ${mymt}
        /bin/ls -ld ${mymt}
        if [ $? -eq 0 ]; then
                echo "Mount Point exists."
                echo "Please review the config file."
                exit 1
        fi
        /sbin/lvcreate -L ${mysize}G -n ${myfs} ${myvg}
        /sbin/mkfs.ext4 /dev/${myvg}/${myfs}
        # Create mount point
        /bin/mkdir -p ${mymt}
        # Add Entry to fstab
        echo "###########################################################################" >>/etc/fstab
        echo "/dev/${myvg}/${myfs}      ${mymt} ext4    defaults        1 2" >>/etc/fstab
        # Mount the filesystem.
        /bin/mount -a
        # Verify if the filesystems were created.
        /bin/df -h      ${mymt}
        # Check the condition if the process is successful.
        if [ $? -eq 0 ]; then
                echo "Successfully Created fileystem."
                continue
        else
                echo "Failed, please review the error"
                # exit
        fi
echo " ----------------end----------------"
done
# EOF

# cat LVM_WEBLOGIC
# file_name: LVM_WEBLOGIC
# SAM Bhusal
# For Weblogic
# vg    FS       Size(GB)  mPoint
#---------------------------------------
datavg  PKGS            20      /pkgs
datavg  WWW             10      /www
datavg  WWW_DOCROOT     10      /www/docroot
datavg  WWW_SERVERS     10      /www/servers
datavg  WEB_TEST       10      /web/test
datavg  WEB_DATA        10      /webdata

No comments:

Post a Comment