Shell script to create LVM disk in Linux (10GB LUN creation within 1.5 sec’s)

Posted: November 23, 2015 in Create LVM Disk, Shell Script

Scenario :

I want to create 10GB LVM partition….using fdisk -l get disk name “/dev/sdd”….. below script we have to pass 4 arguments like LUN name, Volume Group name,Logical Volume name and Mount Points…. first scan the disk and create LVM partition…

script :

[root@autel ~]# cat diskmanagr.sh
#!/bin/bash

echo -e ” \nScan and Detect newly connected SCSI LUN”
host=`ls -l /sys/class/scsi_host/ | grep -v total | awk ‘{print $9}’ | awk -F”host” ‘{print $2}’`
for i in $host
do
echo “Rescaning scsi host /sys/class/scsi_host/host$i”
echo “- – -” > /sys/class/scsi_host/host$i/scan
done
echo -e “\n All the SCSI LUN scanned Sucessfully…..”
lun=$1
vg=$2
lv=$3
mp=$4
mkdir -p $mp
echo -e “\n\n Partition creation is begin”
partx -av $lun
pvcreate $lun
echo -e “\nPhysical Volume $lun Created”
vgcreate $vg $lun
echo -e “\n Volume Group $vgCreated”
lvcreate -l 100%FREE -n $lv $vg
echo -e “\n Logical Volume $lv Created”
mkfs.ext4 /dev/$vg/$lv
echo “/dev/$vg/$lv      $mp     ext4    defaults        1 2 ” | cat >> /etc/fstab
mount -a
s=` df -h | grep /test | awk ‘{print $1}’`
echo -e “\nNewly created LUN $mp = $s ”
echo -e “Partition successfully created”

output :

[root@autel ~]# time ./diskmanagr.sh /dev/sdd new disk10 /autel

Scan and Detect newly connected SCSI LUN
Rescaning scsi host /sys/class/scsi_host/host0
Rescaning scsi host /sys/class/scsi_host/host1
Rescaning scsi host /sys/class/scsi_host/host2

All the SCSI LUN scanned Sucessfully…..

Partition creation is begin
device /dev/sdd: start 0 size 20971520
gpt: 0 slices
Physical volume “/dev/sdd” successfully created

Physical Volume /dev/sdd Created
Volume group “new” successfully created

Volume Group
Logical volume “disk10” created

Logical Voume disk10 Created
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2620416 blocks
131020 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Newly created LUN /autel = 5.0G
Partition successfully created

real    0m1.487s
user    0m0.034s
sys     0m0.873s
[root@autel ~]#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s