LVM stands for Logical Volume Manager. It is a tool for logical volume management allowing us to allocate disks, strip, mirror and resize logical volumes. With LVM, we can manage partitions dynamically.

Recently at work, we opted to use LVM to create logical partitions instead of physical partition on Linux desktop. This post is just a note to remind me of some common used commands. If you want to learn more about LVM, hit this link instead.

Create a physical storage device for LVM

# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

To check for existing physical volumes, we can run one of these commands:

# pvdisplay 
  "/dev/sdb1" is a new physical volume of "1.82 TiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               1.82 TiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               TqqwPp-DveJ-5nY2-Sc2o-g2hX-rGrO-jICbbx

To display only size of the given physical volumes:

# pvdisplay -s
  Device "/dev/sdb1" has a capacity of 1.82 TiB

There’s also another command pvscan, used to scan all disks for physical volumes.

Creating the volume group

Volume groups are a pool of storage that consists of one ore more physical volumes.

# vgcreate data /dev/sdb1
  Volume group "data" successfully created

In the above example, the volume group data; is created on physical volume /dev/sdb1.

Similar to viewing the physical volumes, we can use these commands to view information about volume groups.

# vgdisplay -s
  "data" 1.82 TiB  [0         used / 1.82 TiB free]

# vgdisplay -c
  data:r/w:772:-1:0:0:0:-1:0:1:1:1953513472:4096:476932:0:476932:0BzzVW-XdUj-JXg4-527J-2FSo-TABt-W1GT1c

Creating the logical volume

Suppose we want to create a logical volume of 200GB called backup. Here is how it can be done.

# lvcreate -n backup -L 200GB data
  Logical volume "backup" created

We can check the just created logical volume with lvdisplay command.

# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/data/backup
  VG Name                data
  LV UUID                BwA37T-kIKt-jRfq-CShX-Fxk4-jw3N-iDCRIs
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                200.00 GiB
  Current LE             51200
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:5

or

# lvdisplay -c
  /dev/data/backup:data:3:1:-1:0:419430400:51200:-1:0:-1:252:5

Notice, there’s no -s option for lvdisplay command.

Before you can mount this volume on the system, there’s just one more thing– formatting this new volume. An example to format this volume as ext4:

# mkfs.ext4 /dev/mapper/data-backup

Read more: