lvm creation

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

--------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 

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

sdd                          8:48   0    8G  0 disk 

`-centos_ansserve-data06   253:5    0    7G  0 lvm  /mnt/data06

sr0                         11:0    1 1024M  0 rom  

Enter the disk like /dev/sdb

/dev/sdd

Entered disk: /dev/sdd

Entered Disk is correct format.

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data07

Entered lvname: data07

Enter the LV SIZE: 

7G

Entered size is 7G

Disk /dev/sdd is already a physical volume

[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"



# 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

Comments