LVM: Logical Volume Manager: Difference between revisions

From DISI
Jump to navigation Jump to search
(Added entire section regarding the use of LVM snapshot. This tool will allow us an easy way to backup VM images without pausing/stopping the VMs.)
Line 150: Line 150:


6) While the snapshot is active, backup the snapshot images with your backup tool of choice.
6) While the snapshot is active, backup the snapshot images with your backup tool of choice.
   '''[root@vav backups]# tar -czvf nu_vm_images-20171009.tar /root/image-backups/'''
   '''[root@vav backups]# tar -czvf nu_vm_images-20171009.tar /root/image-backups/nu-disk1.qcow2'''
  tar: Removing leading '/' from member names
  /root/image-backups/nu-disk1.qcow2
Other Reference: http://www.tldp.org/HOWTO/LVM-HOWTO/snapshots_backup.html

Revision as of 17:20, 10 October 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% /

LVM Live Snapshot

Live logical volume snapshots are a way for us to back up files that are constantly changing (such as VMs and databases). When a logical volume snapshot contains all data when the snapshot was created. The data in the snapshot does not change so it can be backed up. When an LV snapshot is created, a new logical volume becomes available for you to mount. Once the snapshot is mounted, all the data can be accessed and then backed up, separate from the original data which might continue to change.

Example: I store my virtual machine images on /var/lib/libvirt/images. In this example, I have many image files. One of these image files is constantly updated because the VM is actively running which changes the image file contents. I would like to backup the VM images directory but directly copying image files while they are active causes the copies to be corrupted so we will use a logical volume snapshot to copy the contents to another directory where it won't be continuously updated.

1) Identify the logical volume you want to snapshot. Use 'lvs' to list all the logical volumes. Below, I want to snapshot the lv_images logical volume where my VM image files reside.

 [root@vav ~]# lvs
 LV             VG        Attr       LSize   Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
 lv_images      vg_data   owi-aos---   2.95t                                                                                    
 lv_root        vg_system -wi-ao----  34.19g                                                       
 lv_scratch     vg_system -wi-ao---- 229.16g    

2) Find the logical volume in /dev/<volume_group_name>/<logical_volume_name>.

[root@vav ~]# ls /dev/vg_data/lv_images
/dev/vg_data/lv_images

3) Use lvcreate to begin creating a logical volume snapshot.

[root@vav ~]# lvcreate -L 2G -s -n lv_images_snap /dev/vg_data/lv_images 
Logical volume "lv_images_snap" created.
 -L <size 2 GB>: limits differences between snapshot and logical volume to be 2GB.  After 2GB of changes, the lv snapshot will become unusable.  
 -s <snapshot>: creates snapshot
 -n <name>: snapshot name will be lv_images_snap
 <logical volume path>: points to logical volume to snapshot (/dev/vg_data/lv_images)

4) Use 'lvs' to list logical volumes again. The new snapshot should appear.

 [root@vav ~]# lvs
 LV             VG        Attr       LSize   Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
 lv_images      vg_data   owi-aos---   2.95t                                                       
 lv_images_snap vg_data   swi-a-s---   2.00g      lv_images 0.72                                   
 lv_root        vg_system -wi-ao----  34.19g                                                       
 lv_scratch     vg_system -wi-ao---- 229.16g

5) Now that the logical volume exists, we can mount it somewhere to access these files. They will mirror the contents of the logical volume at the time the snapshot was created but if any changes happen in the original location, the snapshot will not contain them.

[root@vav ~]# mkdir image-backups
[root@vav ~]# mount /dev/vg_data/lv_images_snap /root/image-backups/
[root@vav ~]# ls -lh /root/image-backups/
total 67G
drwxr-xr-x. 4 root root 4.0K Sep 12 11:21 backups
-rwxr--r--. 1 root root 7.2G Jun 27 21:46 chronos-disk1
drwxr-xr-x. 2 root root 4.0K Oct  9 15:30 inactive
-rw-r--r--. 1 root root  14G Jun 27 21:46 kappa-disk1.qcow2
drwxr-xr-x. 2 root root 4.0K Jun 29 14:41 livesnaps
drwx------. 2 qemu qemu  16K Apr 24 15:52 lost+found
-rwxr-----. 1 qemu qemu  16G Oct  9 16:28 nu-disk1.qcow2
-rwxr-----. 1 root root  16G Oct  5 16:20 nu-disk1.qcow2~2017-10-05 
-rwxr-----. 1 qemu qemu  15G May 22 16:06 zeta-disk1

6) While the snapshot is active, backup the snapshot images with your backup tool of choice.

 [root@vav backups]# tar -czvf nu_vm_images-20171009.tar /root/image-backups/nu-disk1.qcow2
  tar: Removing leading '/' from member names
 /root/image-backups/nu-disk1.qcow2

Other Reference: http://www.tldp.org/HOWTO/LVM-HOWTO/snapshots_backup.html