Share, , Google Plus, Pinterest,

Print

Posted in:

LVM – Logical Volume Management on CentOS 6

LVM quickly explained:

In computer storage, logical volume management or LVM provides a method of allocating space on mass-storage devices that is more flexible than conventional partitioning schemes. In particular, a volume manager can concatenate, stripe together or otherwise combine partitions into larger virtual ones that administrators can re-size or move, potentially without interrupting system use. As seen in the LVM design scheme, Physical Volumes (PV) are the bottom layer of LVM. The second layer are Volume Groups (VG) and the final, third layer are Logical Volumes (LV).

In Part 1 (LVM – Physical Volume Management on CentOS 6) we learned how to manage PV’s and prepared PV’s to be added to Volume Groups (VG’s). In Part 2 (LVM – Physical Volume Management on CentOS 6) we learned how to manage VG’s. In this post we will learn how to manage Logical Volumes (LV’s).

Logical Volume Management is a part 3/3 of LVM Management on CentOS 6!

In this post we will learn how to display, create, extend with filesystem resize, reduce with filesystem resize, resize, rename and remove Logical Volumes (LV).

Here is the basic design scheme:

LVM Logical Volume scheme (source: RedHat.com)
LVM Logical Volume scheme (source: RedHat.com)

Let’s start our Logical Volume Management on CentOS 6 guide!

1. Display Logical Volumes

We can display all Logical Volumes (LV’s) on the system with the “lvdisplay” command:

[root@foo ~]# lvdisplay
   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol01
   LV Name                LogVol01
   VG Name                vg_foo
   LV UUID                qtyLV0-EZuh-2b0f-raUG-EcIM-5teW-W3QyeW
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:33 +0200
   LV Status              available
   # open                 1
   LV Size                13.04 GiB
   Current LE             3339
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol00
   LV Name                LogVol00
   VG Name                vg_foo
   LV UUID                bMxELt-4cN1-lpzb-j7nd-Ixyr-mpsE-vmO1Oe
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:48 +0200
   LV Status              available
   # open                 1
   LV Size                1.46 GiB
   Current LE             375
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:1

We can also display Logical Volumes with “lvscan” or “lvs” commands, but as we see the output is a bit different:

[root@foo ~]# lvscan
   ACTIVE            '/dev/vg_foo/LogVol01' [13.04 GiB] inherit
   ACTIVE            '/dev/vg_foo/LogVol00' [1.46 GiB] inherit
[root@foo ~]# lvs
   LV       VG     Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
   LogVol00 vg_foo -wi-ao---  1.46g                                             
   LogVol01 vg_foo -wi-ao--- 13.04g

In the following examples i will be mostly using “lvdisplay” command, because it gives me the most information.

We see there are currently two existing Logical Volumes present. The first is called LogVol00 and the second is called LogVol01. These LV’s hold the system root and swap partition. With the “lvdisplay” command we can see additional information – LV Status, LV UUID, Current LE,…

2. Create Logical Volume

To create new Logical Volume (LV) we first have to check the desired Volume Group (VG) for free Physical Extent (PE) – Free PE’s.

[root@foo ~]# vgdisplay 
   --- Volume group ---
   VG Name               GeekPeek
   System ID             
   Format                lvm2
   Metadata Areas        3
   Metadata Sequence No  16
   VG Access             read/write
   VG Status             resizable
   MAX LV                0
   Cur LV                0
   Open LV               0
   Max PV                0
   Cur PV                3
   Act PV                3
   VG Size               5.99 GiB
   PE Size               2.00 MiB
   Total PE              3066
   Alloc PE / Size       0 / 0   
   Free  PE / Size       3066 / 5.99 GiB
   VG UUID               oj2GS6-l5aE-6WNv-QPJy-r1pC-ahSQ-EzFiKc

   --- Volume group ---
   VG Name               vg_foo
   System ID             
   Format                lvm2
   Metadata Areas        1
   Metadata Sequence No  3
   VG Access             read/write
   VG Status             resizable
   MAX LV                0
   Cur LV                2
   Open LV               2
   Max PV                0
   Cur PV                1
   Act PV                1
   VG Size               14.51 GiB
   PE Size               4.00 MiB
   Total PE              3714
   Alloc PE / Size       3714 / 14.51 GiB
   Free  PE / Size       0 / 0   
   VG UUID               3n3nzs-dzHK-rApY-EApz-Zz2H-JQVP-TlcYfF

As we can see, Volume Group “GeekPeek” has all of the 3066 PE’s free which means 5.99GiB of allocatable LV space. We can now create a Logical Volume with “lvcreate” command. Simple syntax is “lvcreate -n lvname -L size vgname“.

[root@foo ~]# lvcreate -n GeekVol01 -L 3GiB GeekPeek
Logical volume "GeekVol01" created

If we use the -l parameter instead of -L we are allocating Physical Extents and not actual size in GB, MB or KB. Check “lvcreate –help” for more info.

 [root@foo ~]# lvcreate -n GeekVol01 -l 1536 GeekPeek
   Logical volume "GeekVol01" created

 [root@foo ~]# lvdisplay
   --- Logical volume ---
   LV Path                /dev/GeekPeek/GeekVol01
   LV Name                GeekVol01
   VG Name                GeekPeek
   LV UUID                F99J0T-07s8-LJAy-rNvI-GZNd-DppC-HXsXai
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-07-09 16:18:41 +0200
   LV Status              available
   # open                 0
   LV Size                3.00 GiB
   Current LE             1536
   Segments               2
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:2

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol01
   LV Name                LogVol01
   VG Name                vg_foo
   LV UUID                qtyLV0-EZuh-2b0f-raUG-EcIM-5teW-W3QyeW
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:33 +0200
   LV Status              available
   # open                 1
   LV Size                13.04 GiB
   Current LE             3339
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol00
   LV Name                LogVol00
   VG Name                vg_foo
   LV UUID                bMxELt-4cN1-lpzb-j7nd-Ixyr-mpsE-vmO1Oe
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:48 +0200
   LV Status              available
   # open                 1
   LV Size                1.46 GiB
   Current LE             375
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:1

Both commands created 3GiB large Logical Volume called “GeekVol01“.

3. Extend Logical Volume

We can extend a Logical Volume with “lvextend” command. The simple syntax is “lvextend -L +size vgname/lvname“. You can also resize the filesystem, but you need to add -r parameter “lvextend -L +size vgname/lvname -r“. In our example we will extend LV called “GeekVol01” on a VG called “GeekPeek” from 3GB to 5GB (+2GB in size).

Of course you first have to check if you have enough space available in your VG to complete the action!!

[root@foo ~]# lvextend -L +2GiB GeekPeek/GeekVol01
   Extending logical volume GeekVol01 to 5.00 GiB
   Logical volume GeekVol01 successfully resized

 

To automatically resize LV and filesystem use the -r parameter! We can resize filesystem on the fly, while mounted!

[root@foo ~]# lvextend -L +2GiB GeekPeek/GeekVol01 -r
   Extending logical volume GeekVol01 to 5.00 GiB
   Logical volume GeekVol01 successfully resized
 resize2fs 1.41.12 (17-May-2010)
 Filesystem at /dev/mapper/GeekPeek-GeekVol01 is mounted on /geek; on-line resizing required
 old desc_blocks = 1, new_desc_blocks = 1
 Performing an on-line resize of /dev/mapper/GeekPeek-GeekVol01 to 1310720 (4k) blocks.
 The filesystem on /dev/mapper/GeekPeek-GeekVol01 is now 1310720 blocks long.
[root@foo ~]# lvdisplay 
   --- Logical volume ---
   LV Path                /dev/GeekPeek/GeekVol01
   LV Name                GeekVol01
   VG Name                GeekPeek
   LV UUID                F99J0T-07s8-LJAy-rNvI-GZNd-DppC-HXsXai
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-07-09 16:18:41 +0200
   LV Status              available
   # open                 0
   LV Size                5.00 GiB
   Current LE             2560
   Segments               3
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:2

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol01
   LV Name                LogVol01
   VG Name                vg_foo
   LV UUID                qtyLV0-EZuh-2b0f-raUG-EcIM-5teW-W3QyeW
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:33 +0200
   LV Status              available
   # open                 1
   LV Size                13.04 GiB
   Current LE             3339
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol00
   LV Name                LogVol00
   VG Name                vg_foo
   LV UUID                bMxELt-4cN1-lpzb-j7nd-Ixyr-mpsE-vmO1Oe
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:48 +0200
   LV Status              available
   # open                 1
   LV Size                1.46 GiB
   Current LE             375
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:1

When extending LV, do not forget the plus sign before the extend size! +2GiB!!

4. Reduce Logical Volume

We can reduce a Logical Volume with “lvreduce” command. The simple syntax is “lvreduce -L -size vgname/lvname“. You can also resize the filesystem, but you need to add -r parameter “lvreduce -L -size vgname/lvname -r“. In our example we will reduce LV called “GeekVol01” on a VG called “GeekPeek” from 5GB to 3GB (-2GB in size). We should deactivate our LV first with “lvchange” and later reactivate it:

[root@foo ~]# lvchange -an GeekPeek/GeekVol01
[root@foo ~]# lvreduce -L -2GiB geekpeek/GeekVol01 
   Reducing logical volume GeekVol01 to 3.00 GiB
   Logical volume GeekVol01 successfully resized
[root@foo ~]# lvchange -ay GeekPeek/GeekVol01

 

To automatically resize LV and your filesystem use the -r parameter:

[root@foo ~]# lvreduce -L -2GiB GeekPeek/GeekVol01 -r
 Do you want to unmount "/geek"? [Y|n] y
 fsck from util-linux-ng 2.17.2
 /dev/mapper/GeekPeek-GeekVol01: 18/327680 files (0.0% non-contiguous), 694497/1310720 blocks
 resize2fs 1.41.12 (17-May-2010)
 Resizing the filesystem on /dev/mapper/GeekPeek-GeekVol01 to 786432 (4k) blocks.
 The filesystem on /dev/mapper/GeekPeek-GeekVol01 is now 786432 blocks long.

   Reducing logical volume GeekVol01 to 3.00 GiB
   Logical volume GeekVol01 successfully resized

 [root@foo ~]# lvdisplay 
   --- Logical volume ---
   LV Path                /dev/GeekPeek/GeekVol01
   LV Name                GeekVol01
   VG Name                GeekPeek
   LV UUID                F99J0T-07s8-LJAy-rNvI-GZNd-DppC-HXsXai
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-07-09 16:18:41 +0200
   LV Status              available
   # open                 1
   LV Size                3.00 GiB
   Current LE             1536
   Segments               2
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:2

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol01
   LV Name                LogVol01
   VG Name                vg_foo
   LV UUID                qtyLV0-EZuh-2b0f-raUG-EcIM-5teW-W3QyeW
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:33 +0200
   LV Status              available
   # open                 1
   LV Size                13.04 GiB
   Current LE             3339
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol00
   LV Name                LogVol00
   VG Name                vg_foo
   LV UUID                bMxELt-4cN1-lpzb-j7nd-Ixyr-mpsE-vmO1Oe
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:48 +0200
   LV Status              available
   # open                 1
   LV Size                1.46 GiB
   Current LE             375
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:1

When reducing LV, do not forget the minus sign before the extend size! -2GiB!!

5. Resize Logical Volume

We can extend and reduce LV’s with “lvresize” command. Simple syntax to reduce a LV is “lvresize -L -size vgname/lvname” – again to resize filesystem to add -r at the end. Simple syntax to extend a LV is “lvresize -L +size vgname/lvname” – again to resize filesystem to add -r at the end. We will reduce our LV called “GeekVol01” from 5GB to 3GB and then extend from 3GB back to 5GB – with filesystem resize!

[root@foo ~]# lvresize -L -2GiB geekpeek/GeekVol01 -r
 Do you want to unmount "/geek"? [Y|n] y

 fsck from util-linux-ng 2.17.2
 /dev/mapper/geekpeek-GeekVol01: 18/327680 files (0.0% non-contiguous), 694497/1310720 blocks
 resize2fs 1.41.12 (17-May-2010)
 Resizing the filesystem on /dev/mapper/geekpeek-GeekVol01 to 786432 (4k) blocks.
 The filesystem on /dev/mapper/geekpeek-GeekVol01 is now 786432 blocks long.

   Reducing logical volume GeekVol01 to 3.00 GiB
   Logical volume GeekVol01 successfully resized
[root@foo ~]# lvresize -L +2GiB geekpeek/GeekVol01 -r
 Extending logical volume GeekVol01 to 5.00 GiB
 Logical volume GeekVol01 successfully resized
 resize2fs 1.41.12 (17-May-2010)
 Filesystem at /dev/mapper/geekpeek-GeekVol01 is mounted on /geek; on-line resizing required
 old desc_blocks = 1, new_desc_blocks = 1
 Performing an on-line resize of /dev/mapper/geekpeek-GeekVol01 to 1310720 (4k) blocks.
 The filesystem on /dev/mapper/geekpeek-GeekVol01 is now 1310720 blocks long.

6. Rename Logical Volume

We can rename Logical Volume with “lvrename” command. The simple syntax is “lvrename vgname1/lvname1 vgname1/lvname2“. In our example we will rename our LV called “GeekVol01” existing on VG called “GeekPeek” to “RenameVol01“:

[root@foo ~]# lvrename GeekPeek/GeekVol01 GeekPeek/RenameVol01
Renamed "GeekVol01" to "RenameVol01" in volume group "geekpeek"
[root@foo ~]# lvdisplay 
   --- Logical volume ---
   LV Path                /dev/geekpeek/RenameVol01
   LV Name                RenameVol01
   VG Name                geekpeek
   LV UUID                F99J0T-07s8-LJAy-rNvI-GZNd-DppC-HXsXai
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-07-09 16:18:41 +0200
   LV Status              available
   # open                 1
   LV Size                5.00 GiB
   Current LE             2560
   Segments               3
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:2

7. Remove Logical Volume

We can remove Logical Volumes with “lvremove” command. The simple syntax is “vgremove vgname/lvname“. In our example we will remove LV called “GeekVol01” existing on VG called “GeekPeek“. First deactivate LV and then remove it:

All data from LV you will remove will be lost!!

[root@foo ~]# lvchange -an geekpeek
[root@foo ~]# lvremove GeekPeek/GeekVol01
   Logical volume "GeekVol01" successfully removed
[root@foo ~]# lvdisplay 
   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol01
   LV Name                LogVol01
   VG Name                vg_foo
   LV UUID                qtyLV0-EZuh-2b0f-raUG-EcIM-5teW-W3QyeW
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:33 +0200
   LV Status              available
   # open                 1
   LV Size                13.04 GiB
   Current LE             3339
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:0

   --- Logical volume ---
   LV Path                /dev/vg_foo/LogVol00
   LV Name                LogVol00
   VG Name                vg_foo
   LV UUID                bMxELt-4cN1-lpzb-j7nd-Ixyr-mpsE-vmO1Oe
   LV Write Access        read/write
   LV Creation host, time foo.geekpeek.net, 2013-05-08 10:59:48 +0200
   LV Status              available
   # open                 1
   LV Size                1.46 GiB
   Current LE             375
   Segments               1
   Allocation             inherit
   Read ahead sectors     auto
   - currently set to     256
   Block device           253:1

  • Pingback: LVM - Volume Group Management on CentOS 6 GeekPeek.Net()

  • Pingback: LVM - Physical Volume Management on CentOS 6 GeekPeek.Net()

  • shaddana

    How to use your script?, sorry cause I’m new on opensource

    • Mitch

      Hello shaddana!
      You need to download the script and transfer it to your computer/server. on the server you must make this script executable by running “chmod +x scriptname”. After that you run it with command “sh scriptname” or “./scriptname” and follow the instructions. Hope this helps!
      Regards, Mitch

  • andre

    forgot the most important in your tutorial.
    When you lvresize you’re only resizing the virtual volume. The actual file system partition isn’t affected. You still need to resize it with resize2fs before the space shows up in df

    • Mitch

      Hi Andre! I must disagree with you!

      I have joined “resize2fs” command with “lvreduce”, “lvextend” or “lvresize”. If you read the tutorial carefully you will notice the “-r” parameter used with these commands. The “-r” parameter is actually “resize2fs” command used with “lv” commands so if you used same command as i did in tutorial you also resized your filesystem on the fly! No need to run “resize2fs” separately…

      Regards,
      Mitch

  • amit Sharma

    awsumm …tutorials very helpful..in understanding the basic concept..

    • Mitch

      Hi Amit! Thanks, it is nice to hear my tutorials are helpful!

      Regards,
      Mitch

  • Collette

    Good tutorial. But you left out 1 very important thing. When you are resizing (either shrinking or extending) a volume ALWAYS make a BACKUP before you type the lvm commands.
    Not strictly needed, but it might save you in case you make an error or if the operation fails for whatever reason.
    I lost a logical volume once because of a power failure that did happen while resizing the volume. Somehow the filesystem was so badly damaged because of it that the volume couldn’t be mounted anymore. Luckily I did have a backup.

  • Robert

    You can’t shrink a LogVol mounted on /
    Howto do that?
    The disk is on vmware

    • Mitch

      Hi robert, shrinking root mount point can’t be done online. You should boot a live linux distro of your choice and shrink volume. Regards, Mitch

  • Sridhar

    Nice tutorial. I tried reducing the size with ‘lvresize’. and reduced a LV size by 5GB successfully. And I want to create a new partition with that 5GB. But I am not able to find that space.
    xvda 202:0 0 150G 0 disk
    ├─xvda1 202:1 0 500M 0 part /boot
    └─xvda2 202:2 0 149.5G 0 part
    ├─VolGroup-lv_root (dm-0) 253:0 0 50G 0 lvm /
    ├─VolGroup-lv_swap (dm-1) 253:1 0 3.9G 0 lvm [SWAP]
    └─VolGroup-lv_home (dm-2) 253:2 0 90.6G 0 lvm /home
    I reduced lv_home from 95 to 90GB. Now where this free space is available ?. I want to create 2 or 3 partitions using that 5GB. Would you please tell me how to proceed. Thanks.

    • Swish

      You can do a vgs

  • Pingback: nothingOS » Resize or add new LVM partition/disk on Centos()