shell script to create and extending the volume

[bala@ansserve1 ~]$ ./script2.sh 

This script must be run as root.

[bala@ansserve1 ~]$ sudo ./script2.sh

[sudo] password for bala: 

--------list the devices-------

NAME                       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                          8:0    0   64G  0 disk 

|-sda1                       8:1    0    1G  0 part /boot

`-sda2                       8:2    0   63G  0 part 

  |-centos_ansserve-root   253:0    0 40.8G  0 lvm  /

  |-centos_ansserve-swap   253:1    0  2.3G  0 lvm  [SWAP]

  |-centos_ansserve-home   253:2    0 19.9G  0 lvm  /home

  `-centos_ansserve-data03 253:3    0   10G  0 lvm  

sdb                          8:16   0   10G  0 disk 

`-centos_ansserve-data03   253:3    0   10G  0 lvm  

sdc                          8:32   0    8G  0 disk 

sdd                          8:48   0    8G  0 disk 

sr0                         11:0    1 1024M  0 rom  

Enter the disk like /dev/sdb

/dev/sdc

Entered disk: /dev/sdc

Entered Disk is correct format.

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data02

Entered lvname: data02

Enter the LV SIZE: 

7G

Entered size is 7G

c:create LVM

e:Extend LVM

c

  Physical volume "/dev/sdc" successfully created.

  Volume group "centos_ansserve" successfully extended

WARNING: ext4 signature detected on /dev/centos_ansserve/data02 at offset 1080. Wipe it? [y/n]: y

  Wiping ext4 signature on /dev/centos_ansserve/data02.

  Logical volume "data02" created.

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

458752 inodes, 1835008 blocks

91750 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1879048192

56 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


Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done 


LVM creation completed successfully

mount point creation

--------------------

Enter the mount point  /mnt

Entered path : /mnt

-----------Added the default entry in fstab------------

/dev/centos_ansserve/data02    /mnt/data02    ext4    defaults    0 0

--------list the devices after creation-------

NAME                       MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                          8:0    0   64G  0 disk 

|-sda1                       8:1    0    1G  0 part /boot

`-sda2                       8:2    0   63G  0 part 

  |-centos_ansserve-root   253:0    0 40.8G  0 lvm  /

  |-centos_ansserve-swap   253:1    0  2.3G  0 lvm  [SWAP]

  |-centos_ansserve-home   253:2    0 19.9G  0 lvm  /home

  `-centos_ansserve-data03 253:3    0   10G  0 lvm  

sdb                          8:16   0   10G  0 disk 

`-centos_ansserve-data03   253:3    0   10G  0 lvm  

sdc                          8:32   0    8G  0 disk 

`-centos_ansserve-data02   253:4    0    7G  0 lvm  /mnt/data02

sdd                          8:48   0    8G  0 disk 

sr0                         11:0    1 1024M  0 rom  





script:


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



[bala@ansserve1 ~]$ cat script2.sh 

#!/bin/bash

if [[ $EUID -ne 0 ]]; then

  echo "This script must be run as root."

  exit 1

fi


#list the devices

echo "--------list the devices-------"

lsblk


# set variables for disk, volume group, and logical volume names

echo "Enter the disk like /dev/sdb"

read disk

DISK=$disk

echo "Entered disk: $DISK"

# entered disk comparision

if [[ "${DISK}" == /dev/* ]]; then

    echo "Entered Disk is correct format."

else

    echo "Entered DISK ${DISK} is incorect.please enter in full format /dev/sd"

    exit 1

fi

echo "enter vgname : "

read myvg

VG_NAME=$myvg

echo "Entered VG : $VG_NAME"

echo "enter the LVM NAME"

read mylv

LV_NAME=$mylv

echo "Entered lvname: $LV_NAME"

echo "Enter the LV SIZE: "

read size


LV_SIZE=$size

echo "Entered size is $LV_SIZE"


#lvcreate

function create_lv {


# check if disk exists

if ! lsblk -o NAME | grep -q "^$(basename "$DISK")$"; then

  echo "Disk $DISK not found"

  exit 1

fi


# check if disk is already initialized as a PV

if pvs "$DISK" &> /dev/null; then

  echo "Disk $DISK is already a physical volume"

  exit 1

fi


# check if disk is already initialized as a PV

if pvs "$DISK" &> /dev/null; then

  echo "Disk $DISK is already a physical volume"

else

  # initialize disk as a PV

  pvcreate "$DISK"

fi


# check if VG already exists

if vgdisplay "$VG_NAME" &> /dev/null; then

  vgextend "$VG_NAME" "$DISK"

else

  # create VG

  echo "Volume group $VG_NAME does not exists"

  exit 1

fi

# check if LV already exists

if lvdisplay "/dev/$VG_NAME/$LV_NAME" &> /dev/null; then

  echo "Logical volume /dev/$VG_NAME/$LV_NAME already exists"

else

  # create LV

  lvcreate -L "$LV_SIZE" -n "$LV_NAME" "$VG_NAME"

fi


# format logical volume with ext4 filesystem

mkfs.ext4 "/dev/$VG_NAME/$LV_NAME"


echo "LVM creation completed successfully"


echo "mount point creation"

echo "--------------------"


read -p "Enter the mount point  " mnt_point

echo "Entered path : $mnt_point"

# Create a mount point directory if it doesn't exist

if [ ! -d "${mnt_point}/${LV_NAME}" ]; then

    sudo mkdir ${mnt_point}/${LV_NAME}

fi

# Add the new logical volume to /etc/fstab for automatic mounting at boot time

echo "-----------Added the default entry in fstab------------"

echo "/dev/${VG_NAME}/${LV_NAME}    ${mnt_point}/${LV_NAME}    ext4    defaults    0 0" | sudo tee -a /etc/fstab


# Mount the new logical volume

sudo mount /dev/${VG_NAME}/${LV_NAME} ${mnt_point}/${LV_NAME}

#list the devices

echo "--------list the devices after creation-------"

lsblk

} # Add a semicolon here to end the function definition



function extend_lv {

  if lvdisplay "/dev/$VG_NAME/$LV_NAME" &> /dev/null; then

   # echo "Logical volume /dev/$VG_NAME/$LV_NAME already exists"

    # extend LV

    lvextend -L "+$LV_SIZE"  "/dev/$VG_NAME/$LV_NAME" -r

    exit 0

  else

    echo "Exiting without making any changes: volume does not exists"

    exit 0

  fi

}  # Add a semicolon here to end the function definition

echo "c:create LVM"

echo "e:Extend LVM"

read lv_input

case $lv_input in

    c|C)

       create_lv

      ;;

    e|E)

       extend_lv

      ;;

    *)

      echo "Exiting without making any changes."

      exit 0

      ;;

esac


[bala@ansserve1 ~]$ 



Popular posts from this blog

script and validation for the lvm

Important concepts and bits