Create and resize LVM (CentOS 7)

LVMCreate

if you add new drive to the system and it is absent, then use

# grep mpt /sys/class/scsi_host/host?/proc_name

Which should return a line like  /sys/class/scsi_host/host0/proc_name:mptspi

where host0 is the relevant field. Use this to rescan the bus with the following command

# echo "- - -" > /sys/class/scsi_host/host0/scan

In the above command the the hyphens represent controller, channel, lun, so – – – indicates all controllers, all channels and all luns should be scanned.

Create LVM from the beginning; disk for LVM will be sdb (can be created lvm with multiple disks at a time)

# echo "n
p
1


w" | fdisk /dev/sdb

Creates a volume group descriptor at the start of disk.

# pvcreate /dev/sdb1

Creates volume group “myvolgr” and add disk to it (can be added more disk simultaneously for example vgcreate   myvolgr   /dev/sdb1 /dev/sdb2 /dev/sdc)

#vgcreate   myvolgr   /dev/sdb1

Creates a logical volume called “lv” that uses 100% of the total space in volume group “myvolgr”

# lvcreate  -l  100%VG  -n  lv  myvolgr

Format logical volume to ext4 file system.

# mkfs.ext4  /dev/myvolrg/lv

Mount disk to a directory  and etc.

# mkdir /media/lvm1

And add to fstab

# vim /etc/fstab
/dev/myvolgr/lv    /media/lvm1   ext4   defaults   0    0
#mount -a
# df   -hT

Expand (increase)

# echo "n
p
1


w" | fdisk /dev/sdc
# pvcreate /dev/sdc1

The following command adds the physical volume /dev/sdc1 to the volume group “myvolgr”.

# vgextend myvolgr /dev/sdc1

The pvscan command will show physical volumes on a disks, this should confirm the original /dev/sdb1 partition and the newly created physical volume /dev/sdc1

# pvscan

output:

  PV /dev/sdb1   VG myvolgr    lvm2 [20.00 GiB / 0    free]

  PV /dev/sdc1   VG myvolgr    lvm2 [20.00 GiB / 20.00 GiB free]

  PV /dev/sda2   VG centos      lvm2 [19.51 GiB / 40.00 MiB free]

  Total: 3 [59.50 GiB] / in use: 3 [59.50 GiB] / in no VG: 0 [0   ]

The following command extends the logical volume called “lv” to fill all of the unallocated space in the volume group “myvolgr” also it resize file system. 

# lvresize -r -l 100%VG /dev/myvolgr/lv

Reduce (decrease)

report information about physical volumes

# pvs

output:

  PV         VG     Fmt  Attr PSize  PFree

  /dev/sda2  centos lvm2 a–  19.51g 40.00m

  /dev/sdb1  myvolgr  lvm2 a–  20.00g     0

  /dev/sdc1  myvolgr  lvm2 a–  20.00g     0

will remove /dev/sdb1

# umount /media/lvm1/

Before being able to attempt to shrink the size of an LVM volume, you must first run a file system check on it. If you don’t do this, you will get an error message. The -f flag makes the check run even if the file system appears clean.

# e2fsck -f /dev/myvolgr/lv

The command below will shrink the file system so that it is only 19G in size total

# resize2fs /dev/myvolgr/lv 19G

Shrink the size of the logical volume with the “lvreduce” command to 19Gb (reduce by 19G use -L -19G).

# lvreduce -L 19G /dev/myvolgr/lv

This will extend the file system to use all available space within the logical volume

# resize2fs /dev/myvolgr/lv

Move physical extents (move data from disk to other disk of logical volume).

# pvmove /dev/sdb1

Remove disk from volume group.

# vgreduce myvolgr /dev/sdb1
# pvs

output:

  PV         VG     Fmt  Attr PSize  PFree

  /dev/sda2  centos lvm2 a–  19.51g   40.00m

  /dev/sdb1         lvm2 —  20.00g   20.00g

  /dev/sdc1  myvolgr  lvm2 a–  20.00g 1020.00m

One thought on “Create and resize LVM (CentOS 7)”

  1. rescan SCSI for new space in exist drive
    ls /sys/class/scsi_device/
    echo 1 > /sys/class/scsi_device/0:0:0:0/device/rescan

    rescan partition table
    partx -v -a /dev/sda

Leave a comment