Home / LVM / Linux – LVM – Volume Group Operations

Linux – LVM – Volume Group Operations

We have already seen the basics of logical volume manager structure on the previous article.Here we are going to see about Logical volume manager’s  volume group operations and management. Volume group is high level container in LVM which contains one or more physical disk or LUNS. A volume group can span in to multiple disks, whether internal or external disks. External disks are typically SAN but could be external SCSI or ISCSI disks.According to the filesystem requirements ,you can add or remove disk from volume group easily.This flexibility provides volumes can be re-sized dynamically. 





How to create new volume group ?
1.List the physical disks which were brought under logical volume manager control using pvcreate command.

[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1            lvm2 a-   511.98m 511.98m
  /dev/sde             lvm2 a-   512.00m 512.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#
 
2.Create a new volume group using disk /dev/sdd1.Here the volumegroup name is “uavg”.

[root@mylinz ~]# vgcreate uavg /dev/sdd1
  Volume group "uavg" successfully created
[root@mylinz ~]#


3.Verify the new volume group.

[root@mylinz ~]# vgs uavg
  VG   #PV #LV #SN Attr   VSize   VFree
  uavg   1   0   0 wz--n- 508.00m 508.00m
[root@mylinz ~]#
[root@mylinz ~]# pvs |grep uavg
  /dev/sdd1  uavg      lvm2 a-   508.00m 508.00m
[root@mylinz ~]#


4.For detailed volume group information,use below mentioned command.

[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               508.00 MiB
  PE Size               4.00 MiB
  Total PE              127
  Alloc PE / Size       0 / 0
  Free  PE / Size       127 / 508.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#


How to rename volume Group ? can we rename the VG on fly ?
We can rename the volume group in Redhat Linux using vgrename command.The rename can be done on the fly without any impact. Here i am going to show the vgrename and proving that no impact on this activity.

1.List the currently mounted from volume group “uavg”

[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]#


2.List the available volume group.

[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0

3.Rename the volume group using “vgrename” command.

[root@mylinz ~]# vgrename uavg uavg_new
  Volume group "uavg" successfully renamed to "uavg_new"
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg_new    1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#


4.Check the volume status .You can see still volume is available for operation.

[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]#
[root@mylinz ~]# cd /mnt
[root@mylinz mnt]# touch 3 4 5 6
[root@mylinz mnt]# ls -lrt
total 18
drwx------. 2 root root 12288 Aug  6 22:55 lost+found
-rw-r--r--. 1 root root     0 Aug  7 00:32 1
-rw-r--r--. 1 root root     0 Aug  7 00:32 7
-rw-r--r--. 1 root root     0 Aug 12 21:17 6
-rw-r--r--. 1 root root     0 Aug 12 21:17 5
-rw-r--r--. 1 root root     0 Aug 12 21:17 4
-rw-r--r--. 1 root root     0 Aug 12 21:17 3
[root@mylinz mnt]#


5.But the volume is still reflecting the old device.This can be removed after remounting the volume.This can be down when you have down time for the server.Please don;t forget to update “fstab” according to the new volume group name.

[root@mylinz ~]# umount /mnt
[root@mylinz ~]# mount -t ext4 /dev/mapper/uavg_new-ualvol1 /mnt
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg_new-ualvol1
                       51M  4.9M   43M  11% /mnt
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg_new    1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#


How to Extend the volume group ?
Volume group can be extend on the fly by adding new disks or LUNS to the existing volume group.

1.List the volume group .

[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

2.List the available physical volumes for extending the volume group “uavg”.
Check out if you have any doubt to create new physical volume from new disks or LUNS.

[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 456.00m
  /dev/sde             lvm2 a-   512.00m 512.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#

3.Let me choose “sde” to extend volume group “uavg”.

[root@mylinz ~]# vgextend uavg /dev/sde
  Volume group "uavg" successfully extended
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   1   0 wz--n- 1016.00m 964.00m
  vg_mylinz   1   2   0 wz--n-   19.51g      0
[root@mylinz ~]#

From the above output ,you can see volume group “uavg” has been extended successfully. 

4.Check the “pvs” command output. /dev/sde will show as part of “uavg” now.

[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 456.00m
  /dev/sde   uavg      lvm2 a-   508.00m 508.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#


How to scan a disks for LVM ? 
You need to scan a LVM disks whenever there is a hardware changes on your server.Hardware changes may be a newly added or removed disks which will be hotplug disks or new disks added to SAN systems.

[root@mylinz ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "uavg" using metadata type lvm2
  Found volume group "vg_mylinz" using metadata type lvm2
[root@mylinz ~]#


How to decrease the volume group size ? or How to remove disks from LVM ?
Disks can be removed from volume group if its not used for any volumes.

1.First find out the disks which we are planning to remove it from volume group  is not used for any volumes using below mentioned commands.

[root@mylinz ~]# lvs -a -o +devices
LV      VG        Attr   LSize  Origin Snap%  Move Log Copy%  Convert Devices
ualvol1 uavg      -wi-a- 52.00m                                      /dev/sdd1(0)
lv_root vg_mylinz -wi-ao 16.54g                                      /dev/sda2(0)
lv_swap vg_mylinz -wi-ao  2.97g                                   /dev/sda2(4234)
[root@mylinz ~]#  pvs  -a -o +devices |grep uavg
  /dev/sdd1              uavg      lvm2 a-   508.00m 456.00m /dev/sdd1(0)
  /dev/sdd1              uavg      lvm2 a-   508.00m 456.00m
  /dev/sde               uavg      lvm2 a-   508.00m 508.00m
  /dev/uavg/ualvol1                     --        0       0
[root@mylinz ~]#

From the above commands output,we can see disk “sde” is not used for any volumes (lvs command output) in volume group “uavg” . 

2.Check the disk details. From this details you can confirm ,PE (i.e physical extends) are not used in VG. (Total PE=127 & Free PE=127). 

[root@mylinz ~]# pvdisplay /dev/sde
  --- Physical volume ---
  PV Name               /dev/sde
  VG Name               uavg
  PV Size               512.00 MiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              127
  Free PE               127
  Allocated PE          0
  PV UUID               FadWLT-LjD8-v8VB-pboY-eZbK-vYpE-ZWq0i9
[root@mylinz ~]#

3.List the volume group details before removing the physical volume.

[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   1   0 wz--n- 1016.00m 964.00m
  vg_mylinz   1   2   0 wz--n-   19.51g      0
[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1016.00 MiB
  PE Size               4.00 MiB
  Total PE              254
  Alloc PE / Size       13 / 52.00 MiB
  Free  PE / Size       241 / 964.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#


4.Now we are ready to remove the “/dev/sde” from volume group “uavg”.

[root@mylinz ~]# vgreduce uavg /dev/sde
  Removed "/dev/sde" from volume group "uavg"
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]# vgdisplay uavg
  --- Volume group ---
  VG Name               uavg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               508.00 MiB
  PE Size               4.00 MiB
  Total PE              127
  Alloc PE / Size       13 / 52.00 MiB
  Free  PE / Size       114 / 456.00 MiB
  VG UUID               c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9
[root@mylinz ~]#

From the above outputs ,you can see #PV reduced to “1”  and volume group size also reduced. 

How to activate and Deactivate the volume group ? 
By default volume group will be in active mode. But some circumstances,you need to put the volume group in disabled mode or inactive mode thus unknown to Linux kernel. Here we will see how to activate and deactivate the volume group.

1.List the volume groups.

[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#


2.Deactive the volume group “uavg”

[root@mylinz ~]# vgchange -a n uavg
  0 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

you can not deactivate VG if any opened volumes from that volumegroup. You have to unmount all the volumes from the volume group before deactivating it .You will get below error if any volumes in opened state .

[root@mylinz ~]# vgchange -a n uavg
  Can't deactivate volume group "uavg" with 1 open logical volume(s)
[root@mylinz ~]#


3.Check the volume status.It will be in “Not Available” status.

[root@mylinz ~]# lvdisplay /dev/uavg/ualvol1
  --- Logical volume ---
  LV Name                /dev/uavg/ualvol1
  VG Name                uavg
  LV UUID                6GB8TR-ih7d-vg7J-xCLE-A8OH-gmwy-3XLyOb
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                52.00 MiB
  Current LE             13
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

[root@mylinz ~]#


4.You can activate the volume group use same  command with different options.

[root@mylinz ~]# vgchange -a y uavg
  1 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]# lvdisplay /dev/uavg/ualvol1
  --- Logical volume ---
  LV Name                /dev/uavg/ualvol1
  VG Name                uavg
  LV UUID                6GB8TR-ih7d-vg7J-xCLE-A8OH-gmwy-3XLyOb
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                52.00 MiB
  Current LE             13
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#


5.Mount the volume .We are back to normal operation.

[root@mylinz ~]# mount -t ext4 /dev/mapper/uavg-ualvol1 /mnt
[root@mylinz ~]#


How to backup & restore the LVM volumegroup metadata ?
Automatically Metadata backups and archives are created whenever you create new volume group Linux system.By default backup stored in /etc/lvm/backup and archives are stored in /etc/lvm/archive .We can also manually backup the lvm configuration using “vgcfgbackup” command.

1.Run “vgcfgbackup” command to take new configuration backup for volume group “uavg”.

[root@mylinz ~]# vgcfgbackup uavg
  Volume group "uavg" successfully backed up.
[root@mylinz ~]#


2.You can find the new configuration file under the below mentioned location.

[root@mylinz ~]# cd /etc/lvm/
[root@mylinz lvm]# ls -lrt
total 36
-rw-r--r--. 1 root root 21744 Aug 18  2010 lvm.conf
drwx------. 2 root root  4096 Aug 12 23:57 archive
drwx------. 2 root root  4096 Aug 13 00:27 backup
drwx------. 2 root root  4096 Aug 13 00:27 cache
[root@mylinz lvm]# cd backup/
[root@mylinz backup]# ls -lrt
total 8
-rw-------. 1 root root 1474 Jun  3  2012 vg_mylinz
-rw-------. 1 root root 1164 Aug 13 00:27 uavg
[root@mylinz backup]# file uavg
uavg: ASCII text
[root@mylinz backup]#
[root@mylinz backup]# more uavg
# Generated by LVM2 version 2.02.72(2) (2010-07-28): Tue Aug 13 00:27:46 2013

contents = "Text Format Volume Group"
version = 1

description = "Created *after* executing 'vgcfgbackup /root/uavg.meta.bck uavg'"

creation_host = "mylinz"        # Linux mylinz 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64
creation_time = 1376333866      # Tue Aug 13 00:27:46 2013

uavg {
        id = "c87FyZ-5DND-oQ3n-iTh1-Vb1f-nBML-vUBUE9"
        seqno = 10
        status = ["RESIZEABLE", "READ", "WRITE"]
        flags = []
        extent_size = 8192              # 4 Megabytes
        max_lv = 0
        max_pv = 0
        metadata_copies = 0


3.To restore the volume group meta data,you below command.

[root@mylinz ~]# vgcfgrestore uavg
  Restored volume group uavg
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#


How to export/Move  the volume group to other Linux node ?
Complete LVM volume group can be moved from one system to another system using vg commands.Here we will see step by step guide for this migration.
1.Unmount all the volumes from volume group which needs to be migrated.

2.Make the volume group inactive using “vgchange” command to ensure there will no I/O to the VG.

[root@mylinz ~]# vgchange -a n uavg
  0 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]#



3.Export the volumegroup .

[root@mylinz ~]# vgexport uavg
  Volume group "uavg" successfully exported
[root@mylinz ~]#


4.You can verify the exported status using “pvscan” command.

[root@mylinz ~]# pvscan
  PV /dev/sdd1    is in exported VG uavg [508.00 MiB / 456.00 MiB free]
  PV /dev/sda2   VG vg_mylinz       lvm2 [19.51 GiB / 0    free]
  PV /dev/sde                       lvm2 [512.00 MiB]
  PV /dev/sdf                       lvm2 [5.00 GiB]
  Total: 4 [25.50 GiB] / in use: 2 [20.00 GiB] / in no VG: 2 [5.50 GiB]
[root@mylinz ~]#


5.Now assign the disks from SAN level to the system where you want to import the volume group.

6.Scan the disks and make the disks available for VG import. 
Check out the Disks or LUN scanning procedure in Redhat Linux.

7.Import the volume group.

[root@mylinz ~]# vgimport uavg
  Volume group "uavg" successfully imported
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   1   0 wz--n- 508.00m 456.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

8.Activate the volume group for normal operation.

[root@mylinz ~]# vgchange -a y uavg
  1 logical volume(s) in volume group "uavg" now active
[root@mylinz ~]#

How to recreate the device files for LVM volumes ?
Due to server crash or other reason, we may loose the LVM device files and volume group directory . In those situation ,you need to recreate what you have lost. LVM provides command called “vgmknodes” which will help you to recreate those missing files.Here is a small experiment.

1.Let me remove device file for volume “uavg-ualvol1”  which is part of VG “uavg” . 
[root@mylinz ~]# cd /dev/mapper/
[root@mylinz mapper]# ls -lrt
total 0
crw-rw----. 1 root root 10, 58 Aug  5 19:28 control
lrwxrwxrwx. 1 root root      7 Aug  5 19:28 vg_mylinz-lv_root -> ../dm-0
lrwxrwxrwx. 1 root root      7 Aug  5 19:28 vg_mylinz-lv_swap -> ../dm-1
lrwxrwxrwx. 1 root root      7 Aug 13 00:47 uavg-ualvol1 -> ../dm-2
[root@mylinz mapper]# rm uavg-ualvol1
rm: remove symbolic link `uavg-ualvol1'? y
[root@mylinz mapper]#
2.Let me move the “/etc/lvm” to .old.

[root@mylinz etc]# mv lvm lvm.old
[root@mylinz etc]# ls -lrt |grep lvm
drwx------.  5 root   root     4096 Jun  1  2012 lvm.old
[root@mylinz etc]#

3.Let me run “vgmknodes” and see whether this command is able to recreate the removed device file and lvm directory .

[root@mylinz etc]# vgmknodes


4.Check whether devices files are created or not. 

[root@mylinz mapper]# ls -lrt
total 0
crw-rw----. 1 root root  10, 58 Aug  5 19:28 control
lrwxrwxrwx. 1 root root       7 Aug  5 19:28 vg_mylinz-lv_root -> ../dm-0
lrwxrwxrwx. 1 root root       7 Aug  5 19:28 vg_mylinz-lv_swap -> ../dm-1
brw-rw----. 1 root disk 253,  2 Aug 13 00:54 uavg-ualvol1
[root@mylinz mapper]#

wow…its recreated. 

5.Let me check /etc/lvm directory is created or not .

[root@mylinz etc]# ls -lrt |grep lvm
drwx------.  5 root   root     4096 Jun  1  2012 lvm.old
drwxr-xr-x.  3 root   root     4096 Aug 13 00:54 lvm
[root@mylinz etc]#

Awesome…Its recreated. 

How to remove the volume group ?
You can remove the volume group using vgremove command.
If any volumes from the volume group in mounted status ,you will get below error .

[root@mylinz ~]# vgremove uavg
Do you really want to remove volume group "uavg" containing 1 logical volumes? [y/n]: y
  Can't remove open logical volume "ualvol1"
[root@mylinz ~]#


Un-mount the volume and  remove the volume group.

[root@mylinz ~]# vgremove uavg
Do you really want to remove volume group "uavg" containing 1 logical volumes? [y/n]: y
[root@mylinz ~]#
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

Hope this article shared enough information about LVM2 volume group administration. 
You can also split and combine volume groups in LVM2 like veritas volume manager.

Please leave a comment if you have any doubt on this . Share it in social networks to reach all the Linux administrators and beginners. 

Thank you for visiting UnixArena.

VMTURBO-CLOUD-CAPACITY

7 comments

  1. Great, It is helping me lot!!!

  2. You are really doing a great job Unix Arena….your posted articles are of worth!!

  3. thank you ! this is a articles great . it is helping me understand about LVM very much.

  4. Very good and informative post.
    Just one correction though, at least in RHEL. The VG metadata archive folder is “/etc/lvm/archive” and not “/etc/lvm/archives”

  5. I have a related question. Kindly help.
    I have couple of VGs on LUNs shared between to RHEL 6u5 servers. The VGs are active and file systems mounted on the second server.
    Scenario: I accidentally do ‘vgremove -f’ for the VGs from the fist node. It perform the vgremove without any hesitation as filesystems are no mounted on the first node. As a consequence of this action, corresponding VGs and LVs are no longer listed when we try ‘vgs’ or ‘lvs’ commands. But filesystems continue to be mounted and no IO errors.

    question: How to I safely recover the lost VGs without disturbing the second server? Should I do ‘vgcfgrestore’ on the first server where I dd ‘vgremove -f’ or on the second server?

Leave a Reply

Your email address will not be published. Required fields are marked *