Skip to content

How to setup LVM, dynamic partitions in Linux.

In the previous blog I showed how to add a new storage in Linux and split the disk into partitions. Today I will touch a bit more advanced topic and will show how to create logical volumes with LVM. There are plenty advantages of LVM:

  • you can create/resize/delete partitions while your system is running, without reboot.
  • merge multiple small disks space together, creating a bigger logical disk
  • create distributed I/O across all disks, similar to RAID, but much easier to set up.
  • create snapshots of the volume easily for disk backups. etc

Last time we used Ubuntu, this time we will use CentOS, as when it comes to storage management and commands and tools that we will use, they are pretty much similar:

[vagrant@centos ~]$ rpm -qa | grep lvm
lvm2-2.02.171-8.el7.x86_64
lvm2-libs-2.02.171-8.el7.x86_64
[vagrant@centos ~]$
ubuntu@zesty:~$ dpkg --list | grep lvm
ii  liblvm2app2.2:amd64                        2.02.167-1ubuntu5                         amd64        LVM2 application library
ii  liblvm2cmd2.02:amd64                       2.02.167-1ubuntu5                         amd64        LVM2 command library
ii  lvm2                                       2.02.167-1ubuntu5                         amd64        Linux Logical Volume Manager
ubuntu@zesty:~$

Let’s create a VM, make sure the directory you running the command is empty as vagrant is using rsync to synchronise contents of current directory with the VM, so if you have GBs of files, it might take a while without a reason:

vagrant init centos/7 && \
 vagrant up && \
 vagrant ssh 

If you didn’t have centos previously it will download about 385MB:

➜  ~ du  -sh ~/.vagrant.d/boxes/*
385M	/Users/kayanazimov/.vagrant.d/boxes/centos-VAGRANTSLASH-7
425M	/Users/kayanazimov/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-trusty64
269M	/Users/kayanazimov/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-xenial64
290M	/Users/kayanazimov/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-zesty64

Once inside, let’s check the existing storage devices:

[vagrant@centos ~]$ lsblk
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                       8:0    0   40G  0 disk
├─sda1                    8:1    0    1M  0 part
├─sda2                    8:2    0    1G  0 part /boot
└─sda3                    8:3    0   39G  0 part
  ├─VolGroup00-LogVol00 253:0    0 37.5G  0 lvm  /
  └─VolGroup00-LogVol01 253:1    0  1.5G  0 lvm  [SWAP]

Now let’s exit,, halt the vm, add 2 new disks of size 1GB and then start the vm and logon again,
If you don’t know how to add new disks to vm you can read first part of previous blog about storages.

Now let’s check disks again:

[vagrant@centos ~]$ lsblk
NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                       8:0    0   40G  0 disk
├─sda1                    8:1    0    1M  0 part
├─sda2                    8:2    0    1G  0 part /boot
└─sda3                    8:3    0   39G  0 part
  ├─VolGroup00-LogVol00 253:0    0 37.5G  0 lvm  /
  └─VolGroup00-LogVol01 253:1    0  1.5G  0 lvm  [SWAP]
sdb                       8:16   0    1G  0 disk
sdc                       8:32   0    1G  0 disk

As you can see sdb and sdc have been added. Let’s ask LVM which devices available to it:

[vagrant@centos ~]$ sudo lvmscan
sudo: lvmscan: command not found
[vagrant@centos ~]$ sudo lvmdiscan
sudo: lvmdiscan: command not found
[vagrant@centos ~]$ sudo lvmdiskscan
  /dev/VolGroup00/LogVol00 [     <37.47 GiB]
  /dev/VolGroup00/LogVol01 [       1.50 GiB]
  /dev/sda2                [       1.00 GiB]
  /dev/sda3                [     <39.00 GiB] LVM physical volume
  /dev/sdb                 [       1.00 GiB]
  /dev/sdc                 [       1.00 GiB]
  2 disks
  3 partitions
  0 LVM physical volume whole disks
  1 LVM physical volume

First we need to initialise a physical volumes for use by LVM:

[vagrant@centos ~]$ sudo pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

We can now display LVM physical volumes:

[vagrant@centos ~]$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               VolGroup00
  PV Size               <39.00 GiB / not usable 30.00 MiB
  Allocatable           yes (but full)
  PE Size               32.00 MiB
  Total PE              1247
  Free PE               0
  Allocated PE          1247
  PV UUID               avs1cf-mOCk-nPWi-j8PU-cpSR-iwP5-uASvHD

  "/dev/sdb" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ApBN4w-rcKj-CdrY-WVQe-bgND-Sd72-lt5jFP

  "/dev/sdc" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               B16Z7o-IrHm-N6QN-aupm-D3bE-J9UV-Lsa5ZH

As you can see LVM knows that these devices are new.

Next we need to create a volume group:

[vagrant@centos ~]$ sudo vgcreate my_volume_group_name /dev/sdc /dev/sdb
  Volume group "my_volume_group_name" successfully created

Again, let’s display it:

[vagrant@centos ~]$ sudo vgdisplay
  --- Volume group ---
  VG Name               my_volume_group_name
  System ID
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       0 / 0
  Free  PE / Size       510 / 1.99 GiB
  VG UUID               eFXwSk-Z7Lg-ruiY-Dp9p-Ton4-kRCu-IRq6kz
...
....

And finally lastly we need to create a logical volume:

[vagrant@centos ~]$ sudo lvcreate -L 1.5G -n my_logical_volume my_volume_group_name
  Logical volume "my_logical_volume" created.

Please note, I used 1.5GB despite having 2GB available, I did this on purpose, to show that
we can take two actual physical volumes, merge them into one single logical, and leave some part for future use.

As you can see there are 3 types of commands we used:

Let’s display our lvm:

[vagrant@centos ~]$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/my_volume_group_name/my_logical_volume
  LV Name                my_logical_volume
  VG Name                my_volume_group_name
  LV UUID                R25WU5-irTv-6MNy-Heb4-woUL-26zk-QWTYxi
  LV Write Access        read/write
  LV Creation host, time centos, 2017-12-03 20:47:04 +0000
  LV Status              available
  # open                 0
  LV Size                1.50 GiB
  Current LE             384
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

Let’s check how much we have fee in our group:

[vagrant@centos ~]$ sudo vgs -o +lv_size,lv_name
  VG                   #PV #LV #SN Attr   VSize   VFree   LSize   LV
  my_volume_group_name   2   1   0 wz--n-   1.99g 504.00m   1.50g my_logical_volume

One we done, we need to create a filesystem on our volume:

 
[vagrant@centos ~]$ sudo mkfs.ext4 /dev/my_volume_group_name/my_logical_volume
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
98304 inodes, 393216 blocks
19660 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=402653184
12 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

And finally mount it:

[vagrant@centos ~]$ sudo mkdir /my_mount_point

[vagrant@centos ~]$ sudo mount /dev/my_volume_group_name/my_logical_volume /my_mount_point/

We now ready to use it:

[vagrant@centos ~]$ df -h
Filesystem                                          Size  Used Avail Use% Mounted on
/dev/mapper/my_volume_group_name-my_logical_volume  1.5G  4.5M  1.4G   1% /my_mount_point..
....
...