script and validation for the lvm


[root@ansserve1 ~]# cd /

[root@ansserve1 /]# ll

total 24

lrwxrwxrwx.   1 root root    7 Aug 11  2022 bin -> usr/bin

dr-xr-xr-x.   5 root root 4096 Aug 11  2022 boot

drwxr-xr-x.  20 root root 3200 May  8 20:11 dev

drwxr-xr-x. 143 root root 8192 May  8 20:11 etc

drwxr-xr-x.   3 root root   18 Aug 11  2022 home

lrwxrwxrwx.   1 root root    7 Aug 11  2022 lib -> usr/lib

lrwxrwxrwx.   1 root root    9 Aug 11  2022 lib64 -> usr/lib64

drwxr-xr-x.   2 root root    6 Apr 11  2018 media

drwxr-xr-x.   2 root root    6 Apr 11  2018 mnt

drwxr-xr-x.   3 root root   16 Aug 11  2022 opt

dr-xr-xr-x. 193 root root    0 May  8 20:11 proc

dr-xr-x---.   8 root root  245 Feb 20 16:29 root

drwxr-xr-x.  41 root root 1260 May  8 20:11 run

lrwxrwxrwx.   1 root root    8 Aug 11  2022 sbin -> usr/sbin

drwxr-xr-x.   2 root root    6 Apr 11  2018 srv

dr-xr-xr-x.  13 root root    0 May  8 20:11 sys

drwxrwxrwt.  21 root root 4096 May  8 20:11 tmp

drwxr-xr-x.  13 root root  155 Aug 11  2022 usr

drwxr-xr-x.  21 root root 4096 Feb 15 13:10 var

[root@ansserve1 /]# cd

[root@ansserve1 ~]# mkdir scripts

[root@ansserve1 ~]# cd scripts

[root@ansserve1 scripts]# ll

total 0

[root@ansserve1 scripts]# touch script1.sh

[root@ansserve1 scripts]# chmod 755 script1.sh 

[root@ansserve1 scripts]# vi script1.sh 

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

^C  

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

vg

Entered VG : vg

enter the LVM NAME

read mylv

Entered lvname: 

Enter the LV SIZE: 

^C

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

^C

[root@ansserve1 scripts]# vi script1.sh 

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

vg

Entered VG : vg

./script1.sh: line 47: unexpected EOF while looking for matching `"'

./script1.sh: line 49: syntax error: unexpected end of file

[root@ansserve1 scripts]# vi script1.sh 

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

vg

Entered VG : vg

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

Disk /dev/sdb not found

[root@ansserve1 scripts]# lsblk

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

sr0                       11:0    1 1024M  0 rom  

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sda1

Entered disk: /dev/sda1

enter vgname : 

vg

Entered VG : vg

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

Disk /dev/sda1 not found

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sda

Entered disk: /dev/sda

enter vgname : 

vg

Entered VG : vg

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

  Device /dev/sda excluded by a filter.

  Device /dev/sda excluded by a filter.

  Volume group "vg" not found

  Cannot process volume group vg

mke2fs 1.42.9 (28-Dec-2013)

Could not stat /dev/vg/data01 --- No such file or directory


The device apparently does not exist; did you specify it correctly?

LVM creation completed successfully

[root@ansserve1 scripts]# lsblk

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

sr0                       11:0    1 1024M  0 rom  

[root@ansserve1 scripts]# shutdown

Shutdown scheduled for Mon 2023-05-08 20:49:30 IST, use 'shutdown -c' to cancel.

[root@ansserve1 scripts]# 

Broadcast message from root@ansserve1.examle.com (Mon 2023-05-08 20:48:30 IST):


The system is going down for power-off at Mon 2023-05-08 20:49:30 IST!


shutdown -c


Broadcast message from root@ansserve1.examle.com (Mon 2023-05-08 20:49:10 IST):


The system shutdown has been cancelled at Mon 2023-05-08 20:50:10 IST!


[root@ansserve1 scripts]# shutdown -h now

Connection to 192.168.1.100 closed by remote host.

Connection to 192.168.1.100 closed.

balas-MacBook-Pro:~ bala$ ssh bala@192.168.1.100

^C

balas-MacBook-Pro:~ bala$ ssh bala@192.168.1.100

bala@192.168.1.100's password: 

Last login: Mon May  8 20:11:44 2023 from 192.168.1.35

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

[bala@ansserve1 ~]$ lsblk

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

sdb                        8:16   0   10G  0 disk 

sr0                       11:0    1 1024M  0 rom  

[bala@ansserve1 ~]$ sudo -i

[sudo] password for bala: 

[root@ansserve1 ~]# cd scripts

[root@ansserve1 scripts]# ll

total 4

-rwxr-xr-x. 1 root root 966 May  8 20:44 script1.sh

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

^C

[root@ansserve1 scripts]# vgs

  VG              #PV #LV #SN Attr   VSize   VFree

  centos_ansserve   1   3   0 wz--n- <63.00g 4.00m

[root@ansserve1 scripts]# vgcreate dataora 

  No command with matching syntax recognised.  Run 'vgcreate --help' for more information.

  Correct command syntax is:

  vgcreate VG_new PV ...


[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

  Physical volume "/dev/sdb" successfully created.

  /dev/centos_ansserve: already exists in filesystem

  Run `vgcreate --help' for more information.

  Volume group "centos_ansserve" has insufficient free space (1 extents): 2560 required.

mke2fs 1.42.9 (28-Dec-2013)

Could not stat /dev/centos_ansserve/data01 --- No such file or directory


The device apparently does not exist; did you specify it correctly?

LVM creation completed successfully

[root@ansserve1 scripts]# lsblk

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

sdb                        8:16   0   10G  0 disk 

sr0                       11:0    1 1024M  0 rom  

[root@ansserve1 scripts]# vi script1.sh 

[root@ansserve1 scripts]# ./script1.sh 

Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

Disk /dev/sdb is already a physical volume

[root@ansserve1 scripts]# pvremove /dev/sdb

  Labels on physical volume "/dev/sdb" successfully wiped.

[root@ansserve1 scripts]# lsblk

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

sdb                        8:16   0   10G  0 disk 

sr0                       11:0    1 1024M  0 rom  


[root@ansserve1 scripts]# ./script1.sh 


Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data01

Entered lvname: data01

Enter the LV SIZE: 

10G

Entered size is 10G

  Physical volume "/dev/sdb" successfully created.

  Volume group "centos_ansserve" successfully extended

  Logical volume "data01" 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

655360 inodes, 2621440 blocks

131072 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2151677952

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


Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done 


LVM creation completed successfully

[root@ansserve1 scripts]# df -h /data01

df: '/data01': No such file or directory



[root@ansserve1 scripts]# ./script1.sh 


Enter the disk like /dev/sdb

/dev/sdb

Entered disk: /dev/sdb

enter vgname : 

centos_ansserve

Entered VG : centos_ansserve

enter the LVM NAME

data02

Entered lvname: data02

Enter the LV SIZE: 

10G

Entered size is 10G

Disk /dev/sdb is already a physical volume

[root@ansserve1 scripts]# mkdir /data

[root@ansserve1 scripts]# cd /data

[root@ansserve1 data]# ll

total 0

[root@ansserve1 data]# mount /dev/centos_ansserve/data01 /data/

[root@ansserve1 data]# df -h /data/

Filesystem                          Size  Used Avail Use% Mounted on

/dev/mapper/centos_ansserve-data01  9.8G   37M  9.2G   1% /data

[root@ansserve1 data]# df -h .

Filesystem                        Size  Used Avail Use% Mounted on

/dev/mapper/centos_ansserve-root   41G  4.6G   37G  12% /

[root@ansserve1 data]# cd

[root@ansserve1 ~]# cd /data/

[root@ansserve1 data]# mount -a

[root@ansserve1 data]# df -h .

Filesystem                          Size  Used Avail Use% Mounted on

/dev/mapper/centos_ansserve-data01  9.8G   37M  9.2G   1% /data

[root@ansserve1 data]# cd

[root@ansserve1 ~]# cd scripts/

[root@ansserve1 scripts]# vi script1.sh 


[root@ansserve1 scripts]# cat script1.sh 

#!/bin/bash


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

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


# initialize disk as a PV

pvcreate "$DISK"


# create volume group

vgextend "$VG_NAME" "$DISK"


# create logical volume

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


# format logical volume with ext4 filesystem

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


echo "LVM creation completed successfully"


[root@ansserve1 scripts]# 


[root@ansserve1 scripts]# cat script2.sh 

#!/bin/bash


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

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

  echo "Volume group $VG_NAME already exists"

else

  # create VG

  vgcreate "$VG_NAME" "$DISK"

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"


Popular posts from this blog

Important concepts and bits

shell script to create and extending the volume