≡ Menu

How To Create LVM Using vgcreate, lvcreate, and lvextend lvm2 Commands

LVM stands for Logical Volume Manager.

With LVM, we can create logical partitions that can span across one or more physical hard drives. First, the hard drives are divided into physical volumes, then those physical volumes are combined together to create the volume group and finally the logical volumes are created from volume group.

The LVM commands listed in this article are used under Ubuntu Distribution. But, it is the same for other Linux distributions.

Before we start, install the lvm2 package as shown below.

$ sudo apt-get intall lvm2

To create a LVM, we need to run through the following steps.

  • Select the physical storage devices for LVM
  • Create the Volume Group from Physical Volumes
  • Create Logical Volumes from Volume Group

Select the Physical Storage Devices for LVM – Use pvcreate, pvscan, pvdisplay Commands

In this step, we need to choose the physical volumes that will be used to create the LVM. We can create the physical volumes using pvcreate command as shown below.

$ sudo pvcreate /dev/sda6 /dev/sda7 
Physical volume "/dev/sda6" successfully created                                                 
Physical volume "/dev/sda7" successfully created 

As shown above two physical volumes are created – /dev/sda6 and /dev/sda7.

If the physical volumes are already created, you can view them using the pvscan command as shown below.

$ sudo pvscan                                                                   
  PV /dev/sda6                      lvm2 [1.86 GB]                                                 
  PV /dev/sda7                      lvm2 [1.86 GB]                                                 
  Total: 2 [3.72 GB] / in use: 0 [0   ] / in no VG: 2 [3.72 GB]      

You can view the list of physical volumes with attributes like size, physical extent size, total physical extent size, the free space, etc., using pvdisplay command as shown below.

$ sudo pvdisplay 
--- Physical volume --- 
  PV Name             /dev/sda6 
  VG Name             
  PV Size               1.86 GB / not usable 2.12 MB 
  Allocatable           yes 
  PE Size (KByte)    4096 
  Total PE              476 
  Free PE               456 
  Allocated PE          20 
  PV UUID               m67TXf-EY6w-6LuX-NNB6-kU4L-wnk8-NjjZfv 
 
  --- Physical volume --- 
  PV Name             /dev/sda7 
  VG Name             
  PV Size               1.86 GB / not usable 2.12 MB 
  Allocatable           yes 
  PE Size (KByte)    4096 
  Total PE              476 
  Free PE               476 
  Allocated PE          0 
  PV UUID               b031x0-6rej-BcBu-bE2C-eCXG-jObu-0Boo0x 

Note : PE – Physical Extents are nothing but equal-sized chunks. The default size of extent is 4MB.

Create the Volume Group – Use vgcreate, vgdisplay Commands

Volume groups are nothing but a pool of storage that consists of one or more physical volumes. Once you create the physical volume, you can create the volume group (VG) from these physical volumes (PV).

In this example, the volume group vol_grp1 is created from the two physical volumes as shown below.

$ sudo vgcreate vol_grp1 /dev/sda6 /dev/sda7                                  
  Volume  group "vol_grp1" successfully created          

LVM processes the storage in terms of extents. We can also change the extent size (from the default size 4MB) using -s flag.

vgdisplay command lists the created volume groups.

$ sudo vgdisplay 
  --- Volume group ---              
  VG Name                     vol_grp1  
  System ID                         
  Format                        lvm2        
  Metadata Areas            2           
  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                        2           
  Act PV                       2           
  VG Size                      3.72 GB     
  PE Size                      4.00 MB     
  Total PE                     952         
  Alloc PE / Size             0 / 0       
  Free  PE / Size            952 / 3.72 GB 
  VG UUID                     Kk1ufB-rT15-bSWe-5270-KDfZ-shUX-FUYBvR 

LVM Create: Create Logical Volumes – Use lvcreate, lvdisplay command

Now, everything is ready to create the logical volumes from the volume groups. lvcreate command creates the logical volume with the size of 80MB.

$ sudo lvcreate -l 20 -n logical_vol1 vol_grp1 
  Logical volume "logical_vol1" created      

Use lvdisplay command as shown below, to view the available logical volumes with its attributes.

 
$ sudo lvdisplay                                  
  --- Logical volume ---                                             
  LV Name                /dev/vol_grp1/logical_vol1              
  VG Name                vol_grp1                                  
  LV UUID                 ap8sZ2-WqE1-6401-Kupm-DbnO-2P7g-x1HwtQ      
  LV Write Access      read/write                                  
  LV Status              available                                   
  # open                  0                                           
  LV Size                  80.00 MB                                    
  Current LE              20                                          
  Segments               1                                           
  Allocation               inherit                                     
  Read ahead sectors  auto                                        
  - currently set to     256                                         
  Block device            252:0              
 

After creating the appropriate filesystem on the logical volumes, it becomes ready to use for the storage purpose.

$ sudo  mkfs.ext3 /dev/vol_grp1/logical_vol1 

LVM resize: Change the size of the logical volumes – Use lvextend Command

We can extend the size of the logical volumes after creating it by using lvextend utility as shown below. The changes the size of the logical volume from 80MB to 100MB.

$ sudo lvextend -L100 /dev/vol_grp1/logical_vol1 
  Extending logical volume logical_vol1 to 100.00 MB 
  Logical volume logical_vol1 successfully resized 

We can also add additional size to a specific logical volume as shown below.

$ sudo lvextend -L+100 /dev/vol_grp1/logical_vol1 
  Extending logical volume logical_vol1 to 200.00 MB 
  Logical volume logical_vol1 successfully resized 
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • madrox007 August 6, 2010, 1:42 am

    This article is very good.
    Please tell how to reduce the logical volume successfully.
    That’s the tricky part.

  • Topper August 11, 2010, 12:47 am

    That’s simple – use lvreduce.
    Tricky is how to make a LVM from USB HDD and to be mounted on restart – exmpl. mount first USB storage and than mount as LVM2… I can’t do it on SuSE SLE

  • joofoo November 22, 2010, 4:00 pm

    When the partition is already in use and you are reducing it, you first have to reduce the filesystem (e.g. run ‘resize2fs’ on the logical volume, in the case of ext2/ext3 filesystems), and afterwards you use lvreduce. Otherwise you are running under serious risk of loosing your data!
    (the reverse order applies with lvextend: After running lvextend, you will need to run resize2fs to get usable space, when the partition is already in use)

  • Topper November 23, 2010, 4:32 am

    The user asks “Please tell how to reduce the logical volume successfully.”
    I suppose he knows about file system 🙂

  • Gabriel Francisco August 1, 2011, 2:17 pm

    Topper

    1 – reduce the filesystem. Ex: resise2fs /dev/vg0/lvol0 40G
    2 – reduce the lv . ex : lvresise -L 30G logicalvol
    3 – reduce the vg … etc..

  • Imran January 17, 2012, 4:06 am

    Nice article, an example of assigning entire disk can someone.
    Use Free PE # to assign the full disk,
    Free PE / Size 952 / 3.72 GB
    In this case use Free PE # 952 ,this will assign the entire disk 3.72 GB, use -l and + sign.
    #sudo lvextend -l +952 /dev/vol_grp1/logical_vol1
    Also resize the file system resize2fs after extending the lvm
    #sudo resize2fs /dev/vol_grp1/logical_vol1

  • Imran January 17, 2012, 4:14 am

    Nice article, an example of assigning the entire disk.
    Use Free PE # to assign the full disk, Free PE / Size 952 / 3.72 GB

    In this case use Free PE # 952 ,this will assign the entire disk 3.72 GB, use -l and + sign.

    #sudo lvextend -l +952 /dev/vol_grp1/logical_vol1

    Alo resize the file system resize2fs after extending the lvm
    #sudo resize2fs /dev/vol_grp1/logical_vol1

  • Topper January 17, 2012, 5:44 am

    Not exactly, quoting:

    4.1.11. I’m trying to fill my vg, and vgdisplay/vgs says that I have 1.87 GB free, but when I do an lvcreate vg -L1.87G it says “insufficient free extends”. What’s going on?

    The 1.87 GB figure is rounded to 2 decimal places, so it’s probably 1.866 GB or something. This is a human-readable output to give you a general idea of how big the VG is. If you want to specify an exact size, you must use extents instead of some multiple of bytes.

    In the case of vgdisplay, use the Free PE count instead of the human readable capacity.

    Free PE / Size 478 / 1.87 GB
    ^^^

    So, this would indicate that you should do run

    # lvcreate vg -l478

    Note that instead of an upper-case ‘L’, we used a lower-case ‘l’ to tell lvm to use extents instead of bytes.

    In the case of vgs, you need to instruct it to tell you how many extents are available:

    # vgs -o +vg_free_count,vg_extent_count

    This tell vgs to add the free extents and the total number of extents to the end of the vgs listing. Use the free extent number the same way you would in the above vgdisplay case.

  • xscousr January 20, 2012, 10:28 am

    Here’s a question.
    First some background

    I have a CentOS 6.2 install with two a LVM2 VG group created across two block devices, /dev/sda and /dev/sdb. These are two fiber channel SAN units, formatted as RAID 6 with 2 hot spares.They are attached via two hba’s to the host.

    Within the VG there are to lv’s, stripped across 20 disks.
    LV Name /dev/test/XFStest
    VG Name test
    LV Name /dev/test/EXT4test
    VG Name test

    Problem: I have to downgrade to CentOS 5.6. The host server has its own disks,

    I have not done anything to the current OS as of yet, i’m making sure I have all steps in place prior to doing this. Can anyone here provide me with the proper method?

    How do i restore the lvm and mount it to the newly installed 5.6 install?

    I’ve backed up /etc/lvm

    Thanks for any assistance

  • Topper January 20, 2012, 1:09 pm

    It could be easy only with lvs command but…
    Quote:
    Recovering The LVM Setup

    The LVM configuration file cannot be created by an easy command like the mdadm.conf, but LVM stores one or more copy(s) of the configuration file content at the beginning of the partition. I use the command dd to extract the first part of the partition and write it to a text file:

    This could be helpfull for your case

    I personally used this once in disaster recovery

    Good luck

  • Linux March 22, 2012, 12:04 am

    Hi,
    I used this command to increase already created logical volumes..
    lvextend -L+1806 /dev/vgroot/plat_var
    Now when I do LV display, it shows me the increased size:
    LV Name /dev/vgroot/plat_var
    VG Name vgroot
    LV UUID mO3lQ0-vAz8-bCqc-Al8Y-0mEi-BNEU-uhn36J
    LV Write Access read/write
    LV Status available
    # open 1
    LV Size 14.69 GB
    Current LE 470
    Segments 1
    Allocation inherit
    Read ahead sectors auto
    – currently set to 256
    Block device 253:4

    But when I do df -kh, it did not showed up the updated size
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/vgroot-plat_var1
    3.9G 167M 3.6G 5% /var/var1
    /dev/sda1 251M 21M 218M 9% /boot
    tmpfs 2.0G 0 2.0G 0% /dev/shm

    When I go to /dev/.vgroot folder I see this mapping ” plat_var -> /dev/mapper/vgroot-plat_var1″

    Now I am not getting that even after increasing the size why it is not shown up in vgroot-plat-var1

    Can anyone suggest something on this…

  • Topper March 22, 2012, 2:16 pm

    Must extend AND file system with tool according your file system type.
    But Gparted is already good choice.

  • Andrea November 30, 2013, 8:06 am

    Hi, I wonder if you can help. I am stuck, I cannot see the volume with lvdisplay. please, can you explain with easy step what do to?

    If I run:

    lvm>vgextend vol_grp1 /dev/sda3
    Physical volum /dev/sda3 is already in volum vol_grp1.
    Unable to add physical volume /dev/sda3 to volume group vol_grp1

    lvm>pvscan
    PV /dev/sda3 VG vol_grp1 lvm2 [72 GiB / 72 Gib free]
    PV /dev/sda2 VG vg_freeswitch lvm2 [7.51 Gib / 0 free]

    lvm>pvdisplay
    — Logical volume —
    PV Name /dev/sda3
    VG Name vol_grp1

    — Logical volume —
    PV Name /dev/sda2
    VG Name vg_freeswitch

    lvm>vgdisplay
    — Logical volume —
    VG Name vol_grp1
    System ID —

    — Logical volume —
    VG Name vg_freeswitch
    System ID —

    lvm>lvdisplay
    — Logical volume —
    LV Path /dev/vg_freeswitch/lv_root
    LV Name lv_root

    — Logical volume —
    LV Path /dev/vg_freeswitch_lv_swap
    LV Name lv_swap

    Thanks a lot!
    Andrea

  • Chandu December 20, 2013, 10:39 am

    Hi Ramesh,

    How to see the which mount points(FS)are under perticular VG in LVM.

    In AIX we can see…

  • Rajesh.raf February 1, 2014, 1:42 am

    Hi Topper lvm2 package is not available in my system, need internet for install lvm2 package ????????

  • hockeycokey March 13, 2014, 6:28 am

    Hi,

    Thanks for another awesome and informative article.

    i was just wondering is it necessary to use fdisk at the beginning to create partition on physical disk and set to Linux LVM?

    Thanks,

  • Topper March 13, 2014, 2:37 pm

    @hockeycokey no you don’t need to.

  • Jay April 12, 2014, 2:41 pm

    80M in article…20M in diagram 😉

    update: whoops I guess I missed the small “L” (this refers to size lvcreate -L) hence my confusion. Great article.

  • Rajesh Valluri June 5, 2014, 9:25 pm

    Thanks for the simple yet effective article. I was able to add my 4 and 2 TB SATA drives to my CENTOS server with the info in this article.

    I did have an issue where I missed the 80MB size of the logical volume and panicked for a moment, then I realized you were just doing it for a portion of the total disk space.

    Thanks once again.

  • nilesh July 31, 2014, 3:03 am

    What about shell scripting….

  • Thilina December 28, 2016, 11:27 am

    Thanks

  • Prashant March 27, 2017, 3:06 pm

    why we cant use entire PV size at the time of LVM creation or extend?

  • nivas April 22, 2017, 8:31 pm

    Hi Geekstuff,

    Here is a doubt what happens if we format the mounted lvm partition with ext4, does it cause for any data loss.

    Thanks,
    Nivas