Create and remove physical volumes, assign physical volumes to volume groups, and create and delete logical volumes (LVM) Part I

We use here, the partitions of the disk /dev/vdb, created in the past post

Note: To work with LVM, a good practice is change the type of partition to 8e(LVM) :

# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): t
Partition number (1-5, default 5):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/vdb: 2254 MB, 2254858240 bytes, 4404020 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd4db8a1e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 514047 256000 8e Linux LVM
/dev/vdb2 514048 1026047 256000 8e Linux LVM
/dev/vdb3 1026048 1538047 256000 8e Linux LVM
/dev/vdb4 1538048 4404019 1432986 5 Extended
/dev/vdb5 1540096 2052095 256000 8e Linux LVM

The steps required to configure LVM are:

  1. Create physical volumes from the hard drives.
  2. Create volume groups from the physical volumes.
  3. Create logical volumes from the volume groups.

1. Create physical volumes from the hard drives.

To display a PV:

Write pvdisplay command to display attributes of a physical volume:

# pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name myvg1
PV Size 6.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1535
Free PE 1
Allocated PE 1534
PV UUID XLLp8I-uvfS-eccY-vfdw-frdw-frep34

Write pvs command to display formatted output about physical volumes:

# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 4.00m

To create a PV:

# fdisk -l   //--->(to see the partitions)
...
Device Boot Start End Blocks Id System
/dev/vdb1 2048 514047 256000 8e Linux LVM
/dev/vdb2 514048 1026047 256000 8e Linux LVM
/dev/vdb3 1026048 1538047 256000 8e Linux LVM
/dev/vdb4 1538048 4404019 1432986 5 Extended
/dev/vdb5 1540096 2052095 256000 8e Linux LVM
...
# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
# pvcreate /dev/vdb2
# pvcreate /dev/vdb3
# pvcreate /dev/vdb5

To remove a PV:

# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 4.00m
/dev/vdb1 lvm2 --- 250.00m 250.00m
/dev/vdb2 lvm2 --- 250.00m 250.00m
/dev/vdb3 lvm2 --- 250.00m 250.00m
/dev/vdb5 lvm2 --- 250.00m 250.00m
# pvremove /dev/vdb5
Labels on physical volume "/dev/vdb5" successfully wiped.
# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 4.00m
/dev/vdb1 lvm2 --- 250.00m 250.00m
/dev/vdb2 lvm2 --- 250.00m 250.00m
/dev/vdb3 lvm2 --- 250.00m 250.00m

2. Create volume groups from the physical volumes.

To display a VG:

Write vgdisplay command to display attributes of volume groups:

# vgdisplay
--- Volume group ---
VG Name myvg1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 6.00 GiB
PE Size 4.00 MiB
Total PE 1535
Alloc PE / Size 1534 / 5.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 4D4lHC-8TYx-Hp28-eu2O-foJj-UTaL-6HfSXX

Write vgs command to display formatted output about volume groups:

# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 1 2 0 wz--n- 6.00g 4.00m

To create a VG:

# pvs      //  --->(to see the partitions)
PV VG Fmt Attr PSize PFree
/dev/vda2 myvg1 lvm2 a-- 6.00g 4.00m
/dev/vdb1 lvm2 --- 250.00m 250.00m
/dev/vdb2 lvm2 --- 250.00m 250.00m
/dev/vdb3 lvm2 --- 250.00m 250.00m
/dev/vdb5 lvm2 --- 250.00m 250.00m

syntax : vgcreate + group_name + path pv(1 or more)

# vgcreate myvg2 /dev/vdb3 /dev/vdb5
Volume group "myvg2" successfully created

Another way to create this vg is:
# vgcreate myvg3 -s 16M /dev/vdb3 /dev/vdb5

Where -s 16M = size of the Phisical Extend in this VG (by default is 4MG).

To remove a VG:

# vgremove myvg2
Volume group "myvg2" successfully removed

To assign a PV to a VG:

# vgextend myvg1 /dev/vdb1 /dev/vdb2
Volume group “myvg1” successfully extended

and check:

# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 3 2 0 wz–n- 6.48g 500.00m
myvg2 2 0 0 wz–n- 496.00m 496.00m

For more details on this point, see LVM part II

3. Create logical volumes from the volumegroups.

To create a LV:

Option one:

If the size is given in MB format:

syntax: (command) + (from VG) + (-n LVname) + (-L size in MB)

# lvcreate myvg1 -n lv01 -L 300M
Logical volume "lv01" created

Option two:

If the size is given in Logical Extend format:

syntax: (command) + (from VG) + (-n LVname) + (-l size in LE)

# lvcreate myvg1 -n lv01 -l 25

Where :
myvg1 = VG from where the lv will be extracted.
-n lv01 = name of log. vol.
-l 25 = size of the logical extend


Note: by default is 4MB.

To display a LV:

Write lvs command to display formatted output about logical volumes:

Write lvdisplay command to display attributes of a logical volume:

# lvdisplay
...
--- Logical volume ---
LV Path /dev/myvg1/lv01
LV Name lv01
VG Name myvg1
LV UUID VkMOIp-xrUd-xMpH-LKm4-TFOw-C0ah-hk0J3x
LV Write Access read/write
LV Creation host, time cliente1.example.com, 2019-04-17 21:05:22 -0300
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
Segments 2
Allocation inherit
Read ahead sectors auto
currently set to 8192
Block device 253:2

To remove a LV:

# lvcreate myvg1 -n lv03 -L 10M  //I created this lv to eliminate it//
Rounding up size to full physical extent 12.00 MiB
Logical volume "lv03" created.
# lvremove /dev/myvg1/lv03
Do you really want to remove active logical volume myvg1/lv03? [y/n]: y
Logical volume "lv03" successfully removed

Related Posts

One thought on “Create and remove physical volumes, assign physical volumes to volume groups, and create and delete logical volumes (LVM) Part I

Leave a Reply

Your email address will not be published. Required fields are marked *