LVM: Logical Volume Manager: Difference between revisions

From DISI
Jump to navigation Jump to search
(Created page on Logical Volume Manager basics. Also includes a quick example on expanding a logical volume)
 
No edit summary
Line 13: Line 13:


'''Volume Group''' - a group of physical volumes that are linked together under a single name.   
'''Volume Group''' - a group of physical volumes that are linked together under a single name.   
   Use command 'vgs' to see volume groups.  The number of Physical Volumes is 2 because both /dev/vda2 and /dev/vdb1 are under this group, vg_system.  Because vg_system is a grouping of /dev/vda2 and /dev/vdb1, the volume group's size is the sum of the physical size of the two physical disk partitions.   
   Use command 'vgs' to see volume groups.  The number of Physical Volumes is 2 because both /dev/vda2 and /dev/vdb1 are under this group, vg_system.   
  Because vg_system is a grouping of /dev/vda2 and /dev/vdb1, the volume group's size is the sum of the physical size of the two physical disk partitions.   
   [root@tau ~]# vgs
   [root@tau ~]# vgs
   VG        #PV #LV #SN Attr  VSize  VFree
   VG        #PV #LV #SN Attr  VSize  VFree

Revision as of 22:26, 18 April 2017

LVM provides some great uses such as creating a single logical volume out of multiple hard disks (similar to RAID 0) and letting you expand or reduce filesystems and disk sizes of logical volumes even while the disks are live and operational. But care needs to be taken in understanding how to execute these procedures because you can accidentally delete data if you are not careful.

Intro

Here's some basic information to get you started with the VM tau used as the example:

Physical Volume - are physical (or virtual if they are VM images) disk drives. LVM will not recognize them unless they are declared to be allocated for LVM usage using the 'pvcreate' command.

 Use command 'pvs' to see the physical volumes that have been allocated to lvm:
 [root@tau ~]# pvs
 PV         VG        Fmt  Attr PSize  PFree
 /dev/vda2  vg_system lvm2 a--u 14.84g    0 
 /dev/vdb1  vg_system lvm2 a--u 19.97g    0

Volume Group - a group of physical volumes that are linked together under a single name.

 Use command 'vgs' to see volume groups.  The number of Physical Volumes is 2 because both /dev/vda2 and /dev/vdb1 are under this group, vg_system.  
 Because vg_system is a grouping of /dev/vda2 and /dev/vdb1, the volume group's size is the sum of the physical size of the two physical disk partitions.  
 [root@tau ~]# vgs
 VG        #PV #LV #SN Attr   VSize  VFree
 vg_system   2   2   0 wz--n- 34.81g    0 

Logical Volume - A logical partition of a volume group. All logical volumes are listed under the directory /dev/mapper.

 Use command 'lvs' to see logical volumes.  Under here, I have two logical volumes under volume group vg_system.  
 [root@tau ~]# lvs
 LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 lv_root vg_system -wi-ao---- 31.31g                                                    
 lv_swap vg_system -wi-ao----  3.50g


Rules to Remember about LVM

1) Think of Logical Volumes as a container and the filesystem is the contents. If you wish to reduce a logical volume size, you must reduce the filesystem's size first if you can. Reducing a logical volume size before reducing the filesystem's size will lead to the lost data! (It would be like having a full cup of water then reducing the size of your cup)
2) The opposite holds true. If you wish to expand a logical volume, expand the logical volume and then expand the filesystem or else the size will remain the same! Use the command resize2fs.
3) To summarize LVM:

   a) Physical volumes are disks or disk partitions that are initialized to be used by LVM.  
   b) Physical volumes form a volume group.  
   c) Logical volumes are partitions of volume groups.  
   d) Filesystems are placed in logical volumes.  

Expanding a Logical Volume After Adding a New Physical/Virtual Disk

Here's the steps I took to increase the logical volume size of the VM Tau:

1) Tau's root directory was previously 11.34 Gbs which was too small and kept filling up. From this command, I can see / belongs to a logical volume because its filesystem exists on /dev/mapper.

 [root@tau usr]# df -hl
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/mapper/vg_system-lv_root
                        12G   11G   41M 100% /
 tmpfs                 1.9G     0  1.9G   0% /dev/shm
 /dev/vda1             146M  102M   37M  74% /boot

 [root@tau usr]# lvs
 LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 lv_root vg_system -wi-ao---- 11.34g                                                    
 lv_swap vg_system -wi-ao----  3.50g 

2) I listed out the existing physical volumes and volume groups using the command 'pvs' and 'vgs' to take into account what existed already. I could see we were using one disk partition /dev/vda2 as the physical volume and it belonged to the volume group 'vg_system'

 [root@tau usr]# pvs
 PV         VG        Fmt  Attr PSize  PFree
 /dev/vda2  vg_system lvm2 a--u 14.84g    0 
 [root@tau usr]# vgs
 VG        #PV #LV #SN Attr   VSize  VFree
 vg_system   1   2   0 wz--n- 14.84g    0 

3) I had a disk partition /dev/vdb1 which was added but unused. Since it was free, I first initialized it to be an LVM physical volume.

 [root@tau usr]# pvcreate /dev/vdb1
 Physical volume "/dev/vdb1" successfully created
 [root@tau ~]# pvs
 PV         VG        Fmt  Attr PSize  PFree
 /dev/vda2  vg_system lvm2 a--u 14.84g    0 
 /dev/vdb1  vg_system lvm2 a--u 19.97g    0 

4) I added the new physical volume, /dev/vdb1, to the existing volume group vg_system. You can see the size of the volume group extends after I did vgextend.

 [root@tau usr]# vgs
 VG        #PV #LV #SN Attr   VSize  VFree
 vg_system   1   2   0 wz--n- 14.84g    0 
 [root@tau usr]# vgextend vg_system /dev/vdb1
 Volume group "vg_system" successfully extended
 [root@tau usr]# vgs
 VG        #PV #LV #SN Attr   VSize  VFree 
 vg_system   2   2   0 wz--n- 34.81g 19.97g

5) After the volume group expanded, next comes the logical volume. I issued the command to resize the logical volume lv_root to fill up all the free space in the volume group vg_system

 [root@tau usr]# lvs
 LV      VG        Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 lv_root vg_system -wi-ao---- 11.34g                                                    
 lv_swap vg_system -wi-ao----  3.50g 
 [root@tau usr]# lvextend --verbose -l +100%FREE /dev/vg_system/lv_root
   Using volume group(s) on command line.
   Converted 100%FREE into at most 639 physical extents.
   Archiving volume group "vg_system" metadata (seqno 5).
   Extending logical volume vg_system/lv_root to up to 31.31 GiB
 Size of logical volume vg_system/lv_root changed from 11.34 GiB (363 extents) to 31.31 GiB (1002 extents).
   Loading vg_system-lv_root table (253:0)
   Suspending vg_system-lv_root (253:0) with device flush
   Resuming vg_system-lv_root (253:0)
   Creating volume group backup "/etc/lvm/backup/vg_system" (seqno 6).
 Logical volume lv_root successfully resized.

6) Lastly, we need to resize the filesystem that resides on logical volume lv_root.

 [root@tau usr]# resize2fs /dev/mapper/vg_system-lv_root 
 resize2fs 1.41.12 (17-May-2010)
 Filesystem at /dev/mapper/vg_system-lv_root is mounted on /; on-line resizing required
 old desc_blocks = 1, new_desc_blocks = 2
 Performing an on-line resize of /dev/mapper/vg_system-lv_root to 8208384 (4k) blocks.
 The filesystem on /dev/mapper/vg_system-lv_root is now 8208384 blocks long.

7) Then everything should be swell!

 [root@tau usr]# df -h .
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/mapper/vg_system-lv_root
                      31G   11G   19G  36% /