How to Create LVM Using Multiple Disks on a Linux Server
When you want to combine multiple disks into flexible logical volumes. This article explains how to create LVM using multiple disks on a Linux server.
A hard drive or a set of hard drives can be combined together to a single logical volume or multiple logical volumes using LVM. Using LVM we can easily extend a logical volume. You can combine one or more SAN storage volumes to a single partition using LVM. The layers LVM uses are physical layer, volume group, logical volume.
Initially we should create the disk partition using fdisk and set the LVM flag “8e” for the partition.
We can find the devices that LVM can see and manage using below command.
#lvmdiskscan
This command will display all the blocks which LVM can manage. In this example we are using 2 hard disks /dev/sda and /dev/sdb to set up LVM.
In order to set up LVM, we need to complete the below steps in order.
i. Creating physical volumes from the hard disks
ii. Creating volume group from physical volume
iii. Creating logical volumes from volume groups
Creating physical volumes from the hard disks
We can set the hard disks as physical volumes using “pvcreate” command
#pvcreate /dev/sda /dev/sdb
The above command will mark the disks /dev/sda and /dev/sdb as physical volumes. You can verify the same using the “pvdisplay” command.
Creating volume group from physical volume
The next step is to add the physical volumes to the volume group. We need to set a name for the volume group. In this example we choose the volume group name as “vgroup”. We use the command “vgcreate” to create the volume group.
#vgcreate vgroup /dev/sda /dev/sdb
The above command creates a new volume group with name “vgroup” and adds the 2 physical volumes to the volume group. Using the “vgdisplay” command you can verify it.
Creating logical volumes from volume group
As we have created the volume group we can create multiple LVMs from the volume group. As compared to old partitioning, when working with logical volumes, we do not need to know the layout of the volume since LVM maps and handles this for us
We use the “lvcreate” command to create the LVM. We need to specify the name and required size of LVM while creating.
#lvcreate -n mylvm1 -L 2G vgroup
Here “mylvm1” is the name of the LVM and we have defined 2GB as the size and it is created from the volume group “vgroup”. Using the “lvdisplay” command we can see the created LVM’s.
After creating the LVM we can use them as normal block storages. As other storage devices, the lvms will be present in ‘/dev’ directory under their volume group name, example “/dev/vgroup/mylvm”. In order to use these we need to format the LVM using the filesystem and mount it.
#mkfs.ext3 /dev/vgroup/mylvm
After formatting, we can mount it to the server permanently by adding the below line in “/etc/fstab”
/dev/vgroup/mylvm /mountdir ext3 defaults 0 0
Once this is done you can mount the lvm using the “mount -a” command.