Managing Logical Volumes in Linux

Logical Volume Management

The LVM (i.e Logical Volume Management) is a mechanism to virtualize the disks. LVM creates the virtual disk partitions out of one or more physical hard drives, that allows you to increase, reduce, or move those partition from drive to drive as your needs change.

If a disk starts to fail, a replacement disk can be registered as a physical volume with the volume group and the logical volume’s extents can be migrated to the new disk.

lvm2

As Pictures shows the structure of LVM. LVM consists of Physical Volume, Volume Group and Logical Volume.

In this article, I am going to explaining how to create and removing a logical volume.

Components of LVM in Linux.

  • Physical devices are the storage devices that used to stored data in a logical volume. These are the block devices and could be disk partitions, whole disks, RAID arrays, or SAN disks. A device must be initialized as an LVM physical volume in order to be used with LVM.
  • Physical Volume (PV) is a the standard partition that are used to register underlying physical device for use in volume groups.
  • Physical Extent(PE) is a chuck of data that act as the smallest storage block on a PV. LVM automatically segments PVs into PE.
  • Volume group(VG) are storage pools made up of one or more physical volumes. A PV can only be appoint to a single VG. A VG consist of unused spaced and number of logical volumes.
  • Logical Volume(LV) is  composed of a group of LEs. You can format and mount any file system on an LV. The size of these LV can easily be increased and decreased as per the requirement.
  • Logical Extent(LE) is also chunk of disk space. Every LE is mapped to a specific PE.

Implementing LVM Storage

a.) Creating a logical Volume

1.) Prepare the Physical device.

Use fdisk to create a new partition for use with LVM.

[root@server1 ~]# fdisk /dev/sde
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/sde: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0bd0ce49

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1            2048    20971519    10484736    5  Extended

Command (m for help): n
Partition type:
   p   primary (0 primary, 1 extended, 3 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (4096-20971519, default 4096): 4096
Last sector, +sectors or +size{K,M,G} (4096-20971519, default 20971519): +5G
Partition 5 of type Linux and of size 5 GiB is set

Command (m for help): t
Partition number (1,5, default 5): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sde: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0bd0ce49

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1            2048    20971519    10484736    5  Extended
/dev/sde5            4096    10489855     5242880   8e  Linux LVM

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.

Use “m” for help, “p” to print existing partition table, “n” to create a new partition, “t”  to change the partition type, “w” to write the changes.

Note: Always set the partition type to Linux LVM on LVM partition.

2.) Create a physical volume.

Create a PV on newly created partition (i.e /dev/sde5)

#pvcreate <partition name>

[root@server1 ~]# pvcreate /dev/sde5
  Physical volume "/dev/sde5" successfully created
[root@server1 ~]#pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda3  centos lvm2 a--  10.87g 4.00m
  /dev/sda5  vgitt  lvm2 a--   6.83g 6.83g
  /dev/sde5         lvm2 ---   5.00g 5.00g
[root@server1 ~]# 

The above command will list all the PVs in the system. You can also use “pvdisplay” command for more details.

3.) Create a volume group.

vgcreate is used to create a pool of one or more physical volumes.

# vgcreate <volgroup name> <partition name>

[root@server1 ~]# vgcreate itcare-vg /dev/sde5
  Volume group "itcare-vg" successfully created
[root@server1 ~]#  vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  centos      1   3   0 wz--n- 10.87g 4.00m
  itcare-vg   1   0   0 wz--n-  5.00g 5.00g
  vgitt       1   0   0 wz--n-  6.83g 6.83g
[root@server1 ~]# 

Note: The size of the VG is determined by the total number of physical extents in the pool.

4.) Create a logical Volume.

lvcreate creates a new logical volume from the available physical extents in a volume group.

# lvcreate -L <size of LV> -n <Name of LV> <VG Name> 

[root@server1 ~]# lvcreate -L 2G -n itcare-lv1 -v itcare-vg
 Logical volume "itcare-lv1" created.
[root@server1 ~]# lvs
  LV         VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home       centos    -wi-ao---- 2.00g                                                    
  root       centos    -wi-ao---- 7.00g                                                    
  var        centos    -wi-ao---- 1.86g                                                    
  itcare-lv1 itcare-vg -wi-a----- 2.00g                                                    
[root@server1 ~]#

Note: There are multiple ways to specific the size: “l” generally expects physical extent values, while “-L” expects sizes in bytes.

Some example:

  • lvcreate -l 128: Resize the logical volume to exactly 128 extents in size.
  • lvcreate -l +128: Add 128 extents to the current size of the logical volume.
  • lvcreate -L 128M: Resize the logical Volume to exactly 128 MB.
  • lvcreate -L +128M:  Add 128MB to the current size of the logical volume.
  • lvcreate -l +50%FREE: Add 50% of the current free space in the VG to the LV.

5.) Add the file system.

Use “Mkfs” to create the “xfs” file system on the logical volume.

[root@server1 ~]# mkfs.xfs /dev/itcare-vg/itcare-lv1 
meta-data=/dev/itcare-vg/itcare-lv1 isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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
[root@server1 ~]#

6.) Create mount directory and add an entry to fstab file.

Use “mkdir” command to create the directory.

[root@server1 ~]# mkdir /itcaredir
[root@server1 ~]#

#vim /etc/fstab Add an entry to the fstab file.

/dev/itcare-vg/itcare-lv1       /itcaredir      xfs     defaults        0 0

~
~
:wq

Run “Mount -a” command.

[root@server1 ~]# mount -a
[root@server1 ~]# df -h /itcaredir
Filesystem                          Size  Used Avail Use% Mounted on
/dev/mapper/itcare--vg-itcare--lv1  2.0G   33M  2.0G   2% /itcaredir
[root@server1 ~]# cd /itcaredir/
[root@server1 itcaredir]# touch file{1..5}.txt
[root@server1 itcaredir]# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[root@server1 itcaredir]#

Obverse LVM Status Information.

Use “pvdisplay” command to display information about physical volumes.

[root@server1 ~]# pvdisplay /dev/sde5
  --- Physical volume ---
  PV Name               /dev/sde5
  VG Name               itcare-vg
  PV Size               5.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1279
  Free PE               767
  Allocated PE          512
  PV UUID               1smWt1-t2Y3-gXsf-YKzG-SNAQ-rZWc-6nSZtA
   
[root@server1 ~]#
  • PV Name maps to the device name.
  • VG Name shows the volume group name where the PV is assign.
  • PV Size shows the Physical size of PV, including any unusable space.
  • PE Size is the physical extend size, which is the smallest size a logical volume can be allocated.

There are 1279 PEs, of which 767 PEs are free for allocation to LVs in the future and 512 PEs are currently allocated to LVs. These convert to MB values as follows:

Total 5116 MB (1279 PEs x 4 MB);remember 4MB are unusable. For Free 3068 MB (767 PEs x 4 MB) and for allocated 2048 MB (512 PEs x 4 MB).

  • Free PE shows how many PE units are available for allocation to new logical volume.

Use “vgdisplay” command to display the information of volume groups.

[root@server1 ~]# vgdisplay itcare-vg
  --- Volume group ---
  VG Name               itcare-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               5.00 GiB
  PE Size               4.00 MiB
  Total PE              1279
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       767 / 3.00 GiB
  VG UUID               J28PN7-fImO-QaQJ-XZpC-Xi50-R7h4-MzmRMY
[root@server1 ~]#
  • VG Name is the name of volume group.
  • VG Size is the total size of the storage pool available for logical volume allocation.
  • Free PE / Size shows how much space is free in the VG for allocating to new LVs.

Use “lgdisplay” command to display the information of logical groups.

[root@server1 ~]# lvdisplay /dev/itcare-vg/itcare-lv1 
  --- Logical volume ---
  LV Path                /dev/itcare-vg/itcare-lv1
  LV Name                itcare-lv1
  VG Name                itcare-vg
  LV UUID                yjlGp7-nAWh-8YZk-8XHo-5VeV-UFpC-cQStOj
  LV Write Access        read/write
  LV Creation host, time server1, 2016-09-25 09:38:43 -0400
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   
[root@server1 ~]#
  • LV Path shows the device name of this logical volume.
  • LV Size shows the total size of the LV.
  • Current LE shows the number of logical extends used by this LV. Usually LE maps to a physical extent in the VG.

Removing a logical volume

1.)  Prepare the file system.

Move all important data to another location, then use “umount” command to unmount the file system.

[root@server1 ~]# umount /itcaredir/
[root@server1 ~]#

Note: Remove the entry from fstab file.

2.) Remove the logical Volume.

#lvremove <devicename>

[root@server1 ~]# lvremove /dev/itcare-vg/itcare-lv1 
Do you really want to remove active logical volume itcare-lv1? [y/n]: y
  Logical volume "itcare-lv1" successfully removed
[root@server1 ~]#

3.) Remove the Volume group.

#vgremove <volumegroup name>

[root@server1 ~]# vgremove itcare-vg
  Volume group "itcare-vg" successfully removed
[root@server1 ~]#

4.) Remove the Physical volume.

#pvremove <partition disk>

[root@server1 ~]# pvremove /dev/sde5
  Labels on physical volume "/dev/sde5" successfully wiped
[root@server1 ~]#

Note: The Disk partition is now free for reallocation or reformatting.!!!

Read Also:

Extending Logical Volume
Reducing Logical Volume
Creating Striped Volumes
Migrating the Logical Volume Data

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz