Logical Volume Manager (LVM) Operation

1. Overview

 

After having your Linux Operating Systems running on LVM partitions, it is now to gain the advantages of this. Let say one of the partition is running out of space and need to increase its disk space capacity. For this tutorial will show you how to increase the disk space capacity in /var partition.

2. Prerequisites

 

In this article, it is presumed that:

a. You have already install RHEL/CentOS 7 Linux server up and running on VLM partitions. In case that you don’t, you would probably like to read this link. Minimal RHEL/CentOS 7 Installation With Logical Volume Manager (LVM).
b. You have the root privilege to perform the LVM operation.

3. Create LVM Partition

 

Before we start to create the LVM partition, we should check available space capacity on the physical disk first. In the following output, we can see that the physical disk space capacity for /dev/sda is 32GB.

# fdisk -l | grep /dev
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    40904703    19927040   8e  Linux LVM
Disk /dev/mapper/vg–systems-lv–root: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Disk /dev/mapper/vg–systems-lv–usr: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–home: 3221 MB, 3221225472 bytes, 6291456 sectors
Disk /dev/mapper/vg–systems-lv–var: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–tmp: 2147 MB, 2147483648 bytes, 4194304 sectors

Then, we need to check how many GB of disk space that have already occupied by the Physical Volume (PV) of  the LVM. As we can see in the following output,  the Physical Volume /dev/sda2 have already occupied 19GB of 32GB total physical disk space. So in this case, we still have some available disk space to create more Physical Volume (PV).

# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  vg-systems lvm2 a–  19.00g    0

In case that it is already occupied all of the available space capacity of physical disk, we need to shutdown the VM and increase the size if its virtual disk or we can add more virtual disk to this VM. Either ways are absolutely possible.

To create the LVM partition on the physical disk /dev/sda, we use fdisk command as the following. Press “p” to list the current partition on the physical disk /dev/sda.

# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae829

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    40904703    19927040   8e  Linux LVM

Next, we can start to create the LVM partition by press “n” as shown below. Then press “p” again and we can see that there is one new partition, /dev/sda3, now created.

Command (m for help): n
Partition type:
p   primary (2 primary, 0 extended, 2 free)
e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (40904704-62914559, default 40904704):
Using default value 40904704
Last sector, +sectors or +size{K,M,G} (40904704-62914559, default 62914559):
Using default value 62914559
Partition 3 of type Linux and of size 10.5 GiB is set

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae829

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    40904703    19927040   8e  Linux LVM
/dev/sda3        40904704    62914559    11004928   83  Linux

The new created partition,/dev/sda3, is a normal Linux partition, it is not the LVM partition. Press “t” to change the partition type of /dev/sda3. To list the available partition type press “L“. Now let input “8e” to change the partition type of /dev/sda3 to be a LVM partition.

Press “p” to verify that the change and we should see now that /dev/sda3 is an LVM partition now.

Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): L

0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
f  W95 Ext’d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
Hex code (type L to list all codes):8e
Changed type of partition ‘Linux’ to ‘Linux LVM’

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae829

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    40904703    19927040   8e  Linux LVM
/dev/sda3        40904704    62914559    11004928   8e  Linux LVM

Press “w” to save and finish creating the VLM partition. You will see an warning message as the following that the new partition will be usable only after reboot the systems.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

So, let reboot the server with the following command.

# reboot

After reboot, verify the new created LVM partition again and we should see something as the following.

# fdisk -l | grep /dev
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    40904703    19927040   8e  Linux LVM
/dev/sda3        40904704    62914559    11004928   8e  Linux LVM
Disk /dev/mapper/vg–systems-lv–root: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Disk /dev/mapper/vg–systems-lv–usr: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–home: 3221 MB, 3221225472 bytes, 6291456 sectors
Disk /dev/mapper/vg–systems-lv–var: 4294 MB, 4294967296 bytes, 8388608 sectors
Disk /dev/mapper/vg–systems-lv–tmp: 2147 MB, 2147483648 bytes, 4194304 sectors

4. Create Physical Volume

 

Before creating a new Physical Volume (PV), we should check the existing Physical Volume first by using the following command. We can see that there is only one partition which is /dev/sda2 in the current Physical Volume (PV).

# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  vg-systems lvm2 a–  19.00g    0

Now let create a new Physical Volume from the LVM partition, /dev/sda3, that we have just created in the above step by executing the following command.

# pvcreate /dev/sda3
Physical volume “/dev/sda3” successfully created.

We can see that there is one more partition now in the Physical Volume (PV) which is /dev/sda3.

# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  vg-systems lvm2 a–  19.00g     0
/dev/sda3             lvm2 —  10.50g 10.50g

5. Extend Volume Group Space

 

Let check the existing Volume Group (VG) that it was created during the Linux CentOS 7 Operating Systems installation process. From the following output, we can see that there is no free space on the Volume Goup (VG) name “vg-systems”.

# vgs
VG         #PV #LV #SN Attr   VSize  VFree
vg-systems   1   6   0 wz–n- 19.00g    0

So now, let extend the Volume Group (VG) name “vg-systems” from Physical Volume (PV) /dev/sda3 as the following.

# vgextend vg-systems /dev/sda3
Volume group “vg-systems” successfully extended

If we check the Volume Group (VG) again, we will see that there is some available free space now on the Volume Group (VG) name “vg-systems”.

# vgs
VG         #PV #LV #SN Attr   VSize  VFree
vg-systems   2   6   0 wz–n- 29.49g 10.49g

Also, if we check the Physical Volume (PV) again, we can see that there are two partitions in the existing Physical Volume (PV).

# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  vg-systems lvm2 a–  19.00g     0
/dev/sda3  vg-systems lvm2 a–  10.49g 10.49g

6. Extend Logical Volume Space

 

To extend the Logical Volume (LV) disk space, we need to know the path of that particular Logical Volume (LV) by using command lvscan as the following.

# lvscan
ACTIVE            ‘/dev/vg-systems/lv-root’ [4.00 GiB] inherit
ACTIVE            ‘/dev/vg-systems/lv-home’ [3.00 GiB] inherit
ACTIVE            ‘/dev/vg-systems/lv-var’ [4.00 GiB] inherit
ACTIVE            ‘/dev/vg-systems/lv-swap’ [2.00 GiB] inherit
ACTIVE            ‘/dev/vg-systems/lv-usr’ [4.00 GiB] inherit
ACTIVE            ‘/dev/vg-systems/lv-tmp’ [2.00 GiB] inherit

Since we want to extend the disk space capacity of the /var partition, so let execute the following command.

# lvextend -L 8G /dev/vg-systems/lv-var
Size of logical volume vg-systems/lv-var changed from 4.00 GiB (1024 extents) to 8.00 GiB (2048 extents).
Logical volume vg-systems/lv-var successfully resized.

Verity the disk pace of the Logical Volume (LV) “lv-var” again. We should see that the space capacity of the Logical Volume (LV) “lv-var” has increased to 8GB.

# lvs
LV      VG         Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv-home vg-systems -wi-ao—- 3.00g
lv-root vg-systems -wi-ao—- 4.00g
lv-swap vg-systems -wi-ao—- 2.00g
lv-tmp  vg-systems -wi-ao—- 2.00g
lv-usr  vg-systems -wi-ao—- 4.00g
lv-var  vg-systems -wi-ao—- 8.00g

However, we still see the disk space of /var partition is 4GB.

# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg–systems-lv–root  4.0G   65M  4.0G   2% /
devtmpfs                          478M     0  478M   0% /dev
tmpfs                             489M     0  489M   0% /dev/shm
tmpfs                             489M  6.6M  482M   2% /run
tmpfs                             489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/vg–systems-lv–usr   4.0G  1.6G  2.5G  39% /usr
/dev/sda1                         509M  172M  338M  34% /boot
/dev/mapper/vg–systems-lv–tmp   2.0G   33M  2.0G   2% /tmp
/dev/mapper/vg–systems-lv–var   4.0G  280M  3.8G   7% /var
/dev/mapper/vg–systems-lv–home  3.0G   33M  3.0G   2% /home
tmpfs                              98M     0   98M   0% /run/user/1000

To resize the /var partition, which was formatted as XFS file system,  execute the following command.

# xfs_growfs /var/
meta-data=/dev/mapper/vg–systems-lv–var isize=256    agcount=4, agsize=262144 blks
=                       sectsz=512   attr=2, projid32bit=1
=                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1048576, imaxpct=25
=                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
=                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1048576 to 2097152

Let check the disk space of /var partition again. We see it is updated to 8GB now.

# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/vg–systems-lv–root  4.0G   65M  4.0G   2% /
devtmpfs                          478M     0  478M   0% /dev
tmpfs                             489M     0  489M   0% /dev/shm
tmpfs                             489M  6.6M  482M   2% /run
tmpfs                             489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/vg–systems-lv–usr   4.0G  1.6G  2.5G  39% /usr
/dev/sda1                         509M  172M  338M  34% /boot
/dev/mapper/vg–systems-lv–tmp   2.0G   33M  2.0G   2% /tmp
/dev/mapper/vg–systems-lv–var   8.0G  280M  7.8G   4% /var
/dev/mapper/vg–systems-lv–home  3.0G   33M  3.0G   2% /home
tmpfs                              98M     0   98M   0% /run/user/1000

If we check the Volume Group (VG) again we can see that the free space is decreased now.

# vgs
VG         #PV #LV #SN Attr   VSize  VFree
vg-systems   2   6   0 wz–n- 29.49g 6.49g

Let reboot the system to make sure that your server is still bootable after extending the disk space of Logical Volume “lv-var”.

# reboot

6. Conclusion

 

Now you should know about how to increase the disk space on your LVM partition. It is recommended that you practice it on VMware workstation to improve your confidence before apply it on production environment. If you have any questions or suggestions you can always leave your comments below. I will try all of my best to review and reply them.

Comments

comments