Setup LVM and create logical volume

Creating an LVM on a disc / partition allows for a lot of flexibility in managing the storage. Managing a disc using LVM consists of a number of steps, from preparing the disc itself until mounting a volume.

With LVM the disc space is structured in a slightly different way than you might be used to. Additional layers of structure allow for more functionality and, most importantly, flexibility. LVM allows for easy resizing of logical volumes as well as providing RAID functionality to mirror or stripe data across physical discs. When the LVM manager is used, the following structure is applied.

In LVM a physical drive or partition is added as a so called “physical volume” (PV). On top of the physical volume is the “volume group” (VG) which may contain one or more physical volumes. Inside the volume groups, “logical volumes” (LV) are created. Logical volumes can then have file-systems created on them to be mounted.

To initializes a disc or partition to be used with the LVM, the “pvcreate(8)” command is used. The following will use the entire disc “sdb” as the physical volume. To use a disc partition, simply specify the device of the partition like “/dev/sdb1”.

$ sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

To verify the creation of the physical volume, pvdisplay(8) can be used. The output will show the pvysical volume with its details.

$ sudo pvdisplay /dev/sdb
  "/dev/sdb" is a new physical volume of "50,00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               50,00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KhzXQS-zCJd-Dljq-zwtJ-GdmL-4Q07-OKODNi

The output shows that this physical volume is not assigned to a volume group. To create a volume group for the physical volume, vgcreate(8) is used. The following command specifie the volume group name “test_vg” as the first parameter followed by the physical volume device “/dev/sdb”

$ sudo vgcreate vg_test /dev/sdb
  Volume group "vg_test" successfully created

To verify the creation of the volume group, the pvdisplay can be used again. This command shows the physical volume assigned to the volume group “test_vg”. The LVM command vgdisplay(8) shows details about the volume group itself.

$ sudo pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg_test
  PV Size               50,00 GiB / not usable 4,00 MiB
  Allocatable           yes 
  PE Size               4,00 MiB
  Total PE              12799
  Free PE               12799
  Allocated PE          0
  PV UUID               KhzXQS-zCJd-Dljq-zwtJ-GdmL-4Q07-OKODNi

$ sudo vgdisplay vg_test
  --- Volume group ---
  VG Name               vg_test
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               50,00 GiB
  PE Size               4,00 MiB
  Total PE              12799
  Alloc PE / Size       0 / 0   
  Free  PE / Size       12799 / 50,00 GiB
  VG UUID               1oGf1j-pTZS-d1gt-cGBC-aOTH-381i-oDcL0U

As the vgdisplay output shows, the volume group does not yet contain any logical volumes. Creating a logical volume is done using the lvcreate(8) command. The below command uses lvcreate to create a logical volume with the name “lv_test” (–name) and a size of 10GB (–size) on the volume group “vg_test”.

$ sudo lvcreate --name lv_test --size 10G vg_test
  Logical volume "lv_test" created.

With lvdisplay(8) the logical volumes can be shown. The optional parameter passed is the volume group name to show only the logical volumes in this particular volume group.

$ sudo lvdisplay vg_test
  --- Logical volume ---
  LV Path                /dev/vg_test/lv_test
  LV Name                lv_test
  VG Name                vg_test
  LV UUID                5SOd4z-CjRK-itE4-hzwI-wEiU-3vsv-2tD8pm
  LV Write Access        read/write
  LV Creation host, time centos7, 2017-03-01 10:19:58 +0000
  LV Status              available
  # open                 0
  LV Size                10,00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

The example above created a 10GB logical volume on a 50GB volume group. This allows for more logical volumes in the volume group. The amount of unassigned space can be shown using the vgdisplay command as used before. The following section in the output shows the allocated and free space of the volume group.

  Alloc PE / Size       2560 / 10,00 GiB
  Free  PE / Size       10239 / 40,00 GiB

Using the “LV Path” from the lvdisplay output shows the logical volume device file which can be used to address it. In the following a file-system “xfs” will be created on the new logical volume.

$ sudo mkfs.xfs /dev/vg_test/lv_test
meta-data=/dev/vg_test/lv_test   isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

With the file system created, the logical volume is now ready to be mounted and used.

Read more of my posts on my blog at

This entry was posted in Linux Administration and tagged , . Bookmark the permalink.