In this post I want to show how we can create a new LVM Volume Group (VG) and Logical Volumes (LVs) in Linux.



Initialize physical Volumes (PVs)

Creating a new volume group requires to add disks (block devices) to the volume group as storage. Disks in LVM terminology are so called physical volumes (PVs).

In order to assign a disk (block device) to a volume group, we first need to initialize the disks as LVM physical volumes which will add some LVM metadata to the disks.

The following command initializes our new disks /dev/sdc and /dev/sdd as LVM physical volumes (PVs) which then can be assigned to a volume group.

# pvcreate /dev/sdc /dev/sdd


By executing the pvs command you will get a list of all initialized LVM physical volumes as shown below. The first physical volume /dev/sdb is already assigned to the existing volume group named system_vg.


We can now create our new volume group as shown below.



Create a new Volume Group (VG)

To create a new volume group we can use the vgcreate command. Below I will create a new volume group named data_vg. The vgcreate command needs at least one physical volume and a name.

I will assign both previously created physical volumes to the volume group.

# vgcreate data_vg /dev/sdc /dev/sdd


We can now create new logical volumes (LVs) within this new volume group.



Create new Logical Volumes (LVs)

Logical volumes are partitions which will finally contain the file system and gets mounted on the root file system.

To create a logical volume we can use the lvcreate command as shown below. Here I will create three new logical volumes, named data1, data2 & data3 within my new volume group named data_vg.

# lvcreate  -L 50G -n data1 data_vg
# lvcreate  -L 50G -n data2 data_vg
# lvcreate  -L 50G -n data3 data_vg

-n --name String
 Specifies the name of a new LV.  When unspecified, a default name of "lvol#" is generated, where # is a number generated by LVM.

To use all of the unallocated space we can use the following command:
# lvcreate -l 100%FREE -n data1 data_vg


So far the logical volume doesn’t contain a file system, therefore we can use for the ext4 file system type the mkfs.ext4 command as shown below.

mkfs.ext2 -> ext2 file system
mkfs.ext3 -> ext3 file system
mkfs.ext4 -> ext4 file system
mkfs.btrfs -> btrfs file system
mkfs.xfs -> xfs file system


# mkfs.ext4 /dev/<volume group>/<logical volume>

# mkfs.ext4 /dev/data_vg/data1
# mkfs.ext4 /dev/data_vg/data2
# mkfs.ext4 /dev/data_vg/data3



Finally we can mount our logical volumes. Therefore I was first creating three new directories named data1, data2 & data3 on the root file system.




Create new Striped Logical Volumes (LVs)

To create the same logical volumes as above but this time as striped logical volumes, we can use the following parameter for the lvcreate command.

Because we previously initialized two disk (/dev/sdc & /dev/sdd) as physical volumes (PVs) and added them to our volume group (VG), we can now use a maximum stripes number of 2 on which the data will be spread across these disks.

When you create a striped logical volume, you specify the number of stripes with the -i argument of the lvcreate command. This determines over how many physical volumes the logical volume will be striped. The number of stripes cannot be greater than the number of physical volumes in the volume group (unless the –alloc anywhere argument is used).

If the underlying physical devices that make up a striped logical volume are different sizes, the maximum size of the striped volume is determined by the smallest underlying device. For example, in a two-legged stripe, the maximum size is twice the size of the smaller device. In a three-legged stripe, the maximum size is three times the size of the smallest device.

Source: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/lv_stripecreate#LV_stripecreate


# lvcreate -i 2 -I 4  -L 50G -n data1 data_vg
# lvcreate -i 2 -I 4  -L 50G -n data2 data_vg
# lvcreate -i 2 -I 4  -L 50G -n data3 data_vg

-i | --stripes Number
-I | --stripesize Size (k|UNIT)

To use all of the unallocated space for a logical volume we can use the following command:
# lvcreate -i 2 -I 4 -l 100%FREE -n data1 data_vg


More about striped logical volumes you will find in the following article by IBM.

Tuning logical volume striping
https://www.ibm.com/docs/en/aix/7.2?topic=performance-tuning-logical-volume-striping




Remove Volume Groups and Logical Volumes

About removing volume groups and logical volumes you can read my following post.





Links

How to create a volume group in Linux with LVM
https://www.redhat.com/sysadmin/create-volume-group

Removing Volume Groups
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/vg_remove

Removing Physical Volumes from a Volume Group
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/VG_remove_PV

Removing Physical Volumes
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/pv_remove

Physical Volume Administration
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/5/html/logical_volume_manager_administration/physvol_admin

How to create a physical volume in Linux using LVM
https://www.redhat.com/sysadmin/create-physical-volume?intcmp=701f20000012ngPAAQ

LVM Configuration Examples
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/logical_volume_manager_administration/lvm_examples#LVM_examples

Creating Striped Volumes
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/lv_stripecreate

Creating a Striped Logical Volume
https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/logical_volume_manager_administration/stripe_create_ex

Tuning logical volume striping
https://www.ibm.com/docs/en/aix/7.2?topic=performance-tuning-logical-volume-striping