How to Increase the size of a Linux LVM by expanding the virtual machine disk

This post will cover how to increase the disk space for a VMware virtual machine running Linux that is using logical volume manager (LVM). Firstly we will be increasing the size of the actual disk on the VMware virtual machine, so at the hardware level – this is the VM’s .vmdk file. Once this is complete we will get into the virtual machine and make the necessary changes through the operating system in order to take advantage of the additional space that has been provided by the hard drive being extended. This will involve creating a new partition with the new space, expanding the volume group and logical group, then finally resizing the file system.

As there are a number of different ways to increase disk space I have also posted some different methods here:

Important Note: Be very careful when working with the commands in this article as they have the potential to cause a lot of damage to your data. If you are working with virtual machines make sure you take a snapshot of your virtual machine beforehand, or otherwise have some other form of up to date backup before proceeding. Note that a snapshot must not be taken until after the virtual disk has been increased, otherwise you will not be able to increase it. It could also be worth cloning the virtual machine first and testing out this method on the clone.

Prerequisites: As this method uses the additional space to create a primary partition, you must not already have 4 partitions as you will not be able to create more than 4. If you do not have space for another partition then you will need to consider a different method, there are some others in the above list.

Throughout my examples I will be working with a VMware virtual machine running Debian 6, this was set up with a 20gb disk and we will be increasing it by 10gb for a total final size of 30gb.

Identifying the partition type

As this method focuses on working with LVM, we will first confirm that our partition type is actually Linux LVM by running the below command.

fdisk -l

fdisk

As you can see in the above image /dev/sda5 is listed as “Linux LVM” and it has the ID of 8e. The 8e hex code shows that it is a Linux LVM, while 83 shows a Linux native partition. Now that we have confirmed we are working with an LVM we can continue. For increasing the size of a Linux native partition (hex code 83) see this article.

Below is the disk information showing that our initial setup only has the one 20gb disk currently, which is under the logical volume named /dev/mapper/Mega-root – this is what we will be expanding with the new disk.
disk free

Note that /dev/mapper/Mega-root is the volume made up from /dev/sda5 currently – this is what we will be expanding.

Increasing the virtual hard disk

First off we increase the allocated disk space on the virtual machine itself. This is done by right clicking the virtual machine in vSphere, selecting edit settings, and then selecting the hard disk. In the below image I have changed the previously set hard disk of 20gb to 30gb while the virtual machine is up and running. Once complete click OK, this is all that needs to be done in VMware for this process.

vSphere settings

If you are not able to modify the size of the disk, the provisioned size setting is greyed out. This can happen if the virtual machine has a snapshot in place, these will need to be removed prior to making the changes to the disk. Alternatively you may need to shut down the virtual machine if it does not allow you to add or increase disks on the fly, if this is the case make the change then power it back on.

Detect the new disk space

Once the physical disk has been increased at the hardware level, we need to get into the operating system and create a new partition that makes use of this space to proceed.

Before we can do this we need to check that the new unallocated disk space is detected by the server, you can use “fdisk -l” to list the primary disk. You will most likely see that the disk space is still showing as the same original size, at this point you can either reboot the server and it will detect the changes on boot or you can rescan your devices to avoid rebooting by running the below command. Note you may need to change host0 depending on your setup.

echo "- - -" > /sys/class/scsi_host/host0/scan

Below is an image after performing this and confirming that the new space is displaying.

fdisk

Partition the new disk space

As outlined in my previous images the disk in my example that I am working with is /dev/sda, so we use fdisk to create a new primary partition to make use of the new expanded disk space. Note that we do not have 4 primary partitions already in place, making this method possible.

fdisk /dev/sda

We are now using fdisk to create a new partition, the inputs I have entered in are shown below in bold. Note that you can press ‘m’ to get a full listing of the fdisk commands.

‘n’ was selected for adding a new partition.

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n

‘p’ is then selected as we are making a primary partition.

Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p

As I already have /dev/sda1 and /dev/sda2 as shown in previous images, I have gone with using ’3′ for this new partition which will be created as /dev/sda3

Partition number (1-4): 3

We just press enter twice above as by default the first and last cylinders of the unallocated space should be correct. After this the partition is then ready.

First cylinder (2611-3916, default 2611): "enter"
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-3916, default 3916): "enter"
Using default value 3916

‘t’ is selected to change to a partition’s system ID, in this case we change to ’3′ which is the one we just created.

Command (m for help): t
Partition number (1-5): 3

The hex code ’8e’ was entered as this is the code for a Linux LVM which is what we want this partition to be, as we will be joining it with the original /dev/sda5 Linux LVM.

Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

‘w’ is used to write the table to disk and exit, basically all the changes that have been done will be saved and then you will be exited from fdisk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

You will see a warning which basically means in order to use the new table with the changes a system reboot is required. If you can not see the new partition using “fdisk -l” you may be able to run “partprobe -s” to rescan the partitions. In my test I did not require either of those things at this stage (I do a reboot later on), straight after pressing ‘w’ in fdisk I was able to see the new /dev/sda3 partition of my 10gb of space as displayed in the below image.

For CentOS/RHEL run a “partx -a /dev/sda3″ to avoid rebooting later on.

fdisk

That’s all for partitioning, we now have a new partition which is making use of the previously unallocated disk space from the increase in VMware.

Increasing the logical volume

We use the pvcreate command which creates a physical volume for later use by the logical volume manager (LVM). In this case the physical volume will be our new /dev/sda3 partition.

root@Mega:~# pvcreate /dev/sda3
  Device /dev/sda3 not found (or ignored by filtering).

In order to get around this you can either reboot, or use partprobe/partx as previously mentioned to avoid a reboot, as in this instance the disk does not appear to be there correctly despite showing in “fdisk -l”. After a reboot or partprobe/partx use the same command which will succeed.

root@Mega:~# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

Next we need to confirm the name of the current volume group using the vgdisplay command. The name will vary depending on your setup, for me it is the name of my test server. vgdisplay provides lots of information on the volume group, I have only shown the name and the current size of it for this example.

root@Mega:~# vgdisplay
  --- Volume group ---
  VG Name               Mega
...
VG Size               19.76 GiB

Now we extend the ‘Mega’ volume group by adding in the physical volume of /dev/sda3 which we created using the pvcreate command earlier.

root@Mega:~# vgextend Mega /dev/sda3
  Volume group "Mega" successfully extended

Using the pvscan command we scan all disks for physical volumes, this should confirm the original /dev/sda5 partition and the newly created physical volume /dev/sda3

root@Mega:~# pvscan
  PV /dev/sda5   VG Mega   lvm2 [19.76 GiB / 0    free]
  PV /dev/sda3   VG Mega   lvm2 [10.00 GiB / 10.00 GiB free]
  Total: 2 [29.75 GiB] / in use: 2 [29.75 GiB] / in no VG: 0 [0   ]

Next we need to increase the logical volume (rather than the physical volume) which basically means we will be taking our original logical volume and extending it over our new partition/physical volume of /dev/sda3.

Firstly confirm the name of the logical volume using lvdisplay. This name will vary depending on your setup.

root@Mega:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/Mega/root

The logical volume is then extended using the lvextend command.

root@Mega:~# lvextend /dev/Mega/root /dev/sda3
  Extending logical volume root to 28.90 GiB
  Logical volume root successfully resized

There is then one final step which is to resize the file system so that it can take advantage of this additional space, this is done using the resize2fs command. Note that this may take some time to complete, it took about 30 seconds for my additional space.

root@Mega:~# resize2fs /dev/Mega/root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/Mega/root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/Mega/root to 7576576 (4k) blocks.
The filesystem on /dev/Mega/root is now 7576576 blocks long.

That’s it, now with the ‘df’ command we can see that the total available disk space has been increased.

disk free after expansion

Summary

With this method we have increased the virtual disk drive through VMware, created a new partition out of this newly unallocated space within the guest OS, turned it into a physical volume, extended the volume group, and then finally extended the original logical volume over the newer physical volume resulting in overall disk space being increased successfully.

Leave a comment ?

78 Comments.

  1. Nice post. I really like that LVM on linux has finally caught up to the various unix flavors (esp. AIX). Even with a VM I tend to just add more hard disks and then on the OS side add them to VG, extendlv, etc.etc..

    Great post!

  2. Thanks. Easy to follow instructions.

  3. I added 5gb and did all the extending etc. Later I felt I should have added 10gb instead of 5. I know I can add one more partition, but could you show me how to delete the 5gb partition (assuming it has not been written to) and add a 3rd partition of higher size?

    • Hi Russel,

      You’d be much better off just adding another 5gb again using the same process to reach the total of 10gb.

      You can reduce the size of the partition you extended after unmounting it, however with VMware I believe you will not be able to decrease the virtual disk size, as it is not aware of where the data is in that space which is why you can only increase and not decrease the virtual disk.

  4. Ruben Espadas

    Great post!

    Thank you.

  5. Very nice post…!! Thank you very much..saved my day

  6. Thanks a lot, really helped me while expanding my disk space on my new Linux box.

  7. Thanks a lot for this really helpful HowTo! ;-)

  8. i am getting resize2fs: bad magic number in super-block while trying to open /dev/VolGroup00/Logvol01

    couldn’t find valid filesystem superblock

    please help

    • Hi Anil,

      Sounds like you may have some corruption, have you tried running a file system check on /dev/VolGroup00/Logvol01 ?

      Also if you are using an xfs volume you may need to try another tool such as xfs_growfs rather than resize2fs.

      • I get the same error. How do I debug this issue? To figure if corruption or if setup something wrong. Followed the instructions exactly.

        • Did you try running a fsck?

          • Thanks for the cmd, I tried that and got the following:


            fsck from util-linux 2.21.2
            fsck: fsck.LVM2_member: not found
            fsck: error 2 while executing fsck.LVM2_member for /dev/sda2

            Then I ran e2fsck /dev/vg/lvol0 brings up the same point about bad magic number:


            ext2fs_open2: Bad Magic number in super-block
            ....
            e2fsck: Bad magic number in super-block while trying to open /dev/vg/lvol0

            The superblock could not be read or does not describe a correct ext2
            ...

            Any other suggestions. TIA, greatly appreciated.

    • I had the same problem, but I was using the wrong lv. I went over the steps again very carefully and it worked. I was trying to extend the root partition and was mistakenly giving the commands to extend the swap partition, which it just won’t let you do. Hope it helps. Good luck.

      • I also got the error. Retraced my steps and saw that I had mistakenly done the lvextend on the wrong volume. Two were returned and I typed in the second by accident.

        Had to delete the partion and lvremove it. Then did these excellent steps again.

        For instructions on removing or deleting volumes:
        http://www.howtoforge.com/linux_lvm_p2

  9. Really nice and intuitive post, thanks a lot!

  10. Thank you very much for this! Finally an instruction that even i can understand :razz:

  11. The reboot is not required if you run “partprobe -s” instead.

  12. Hey,

    After partition the new disk space (from 10GB to 25GB) my space in new partion SDA3 does not corresponde to the actual size added (15GB). Any idea what is going on?

    I tried follow the tutorial even with the wrong size but i got stucked in the error on the next step: “pvcreate /dev/sda3″

    Link with partitions”http://img832.imageshack.us/img832/1281/53pk.png”

    Thanks

    • I have this same problem and can’t get past it, it appears /dev/sda5 took all my new blocks?

      • I figured out the issue; by default the Ubuntu Server setup for some reason left 1023 blocks between /dev/sda2 and /dev/sda5 and the defaults automatically grab that block-space. You will have to type in the block number that follows the last block of /dev/sda5 and then you should be fine! :)

  13. Great post, worked smoothly. Only minor deviation was to handle the defaults for the start and end of the new partition being wrong.

    I had a small unallocated space earlier in the Virtual Disk and defaults grabbed it instead of the ‘new’ unused space at the end.

  14. Thanks a lot for the easy to follow instructions. Was over in a flash :smile:

  15. Great post, had some issues when i tried it out ..

    Was able to expand space at vmware after reboot also /dev/sda increased at linux level, but when i tried fdisk /dev/sda i had the below -

    root@homeserver2 ~]# fdisk /dev/sda

    The number of cylinders for this disk is set to 10443.
    There is nothing wrong with that, but this is larger than 1024,
    and could in certain setups cause problems with:
    1) software that runs at boot time (e.g., old versions of LILO)
    2) booting and partitioning software from other OSs
    (e.g., DOS FDISK, OS/2 FDISK)

    Command (m for help): n
    No free sectors available

    • Hi Praveen,

      I was having the same issue.

      I then hit “u” to change display/entry units and recreated the partition for /dev/sda3

      Now it’s showing the correct start and end identifiers and shows correct free disk space for the partition.

      Good luck!

  16. Great post, many many thanks…

  17. /dev/sda3 wouldn’t work for me I kept receiving the “Device /dev/sda3 not found (or ignored by filtering)” message. When I changed it to /dev/sda6, it worked.

  18. Pulled me right out of the fire sir! Thank you very much!

  19. It all work perfectly till the very last step where you need to resize the file system. THen Im getting this error:

    resize2fs: Device or resource busy while trying to open /dev….
    Couldn’t find valid filesystem superblock

    any advice?

  20. Great post for me too. It worked like a charm.Thank you

  21. Thought I would add,

    You don’t need to reboot to do pvcreate.
    install partprobe before you start this.

    Then run partprobe /dev/sdX

    • If you are on CentOS/RHEL you can use partx

      root@server [~]# pvcreate /dev/sda3
      Device /dev/sda3 not found (or ignored by filtering).

      root@server [~]# partx -a /dev/sda

      root@cserver [~]# pvcreate /dev/sda3
      Physical volume “/dev/sda3″ successfully created

      • Let me add to this. RHEL 5 you can use partprobe, RHEL 6 you can use partx -v -a /dev/sda

        I think it’s worth updating the post with this to save people a reboot!

  22. In Centos, to rescan the virtual device I had to

    echo 1 > /sys/block/sdb/device/rescan (for sdb, in my case)

    The one on the original article didn’t work for me.

    Hope it helps!

  23. Also, not need to reboot if pvcreate fails. Just issue:

    lvmdiskscan -v

    that will wipe LVM cache and add the new partition.

    ;-)

  24. G Prasanth Kumar

    On my ESXi server I am using complete disk space available(I have not created LVM). I added a new partition and mounted that and made it by default.

    To increase the diskspace following are the steps that I have followed:
    Step1: Check the size with ‘fdisk -l’ & ‘df -Th’
    Step2: From ESXi vSphere client increase the disk space.
    Step3: Restart the server(vm server on ESXi server)
    Step4: ‘fdisk -l;df -Th’ doesn’t show any difference with the output from Step1
    Step4: resize2fs
    Step5: ‘fdisk -l;df -Th’ shows the difference.

    Correct me if I am wrong.

  25. Thank you very very ! :razz:

  26. You rock.

    I recently increased the size of my cloud server, btu disk space was left as the old disk. Followed these tutorial, saved me and the support team wasted time.

  27. This tutorial rocks ;)

  28. Great document. Really helpfull.
    But I would like to add the following :

    1) After rescanning it is possible you don’t see the extended disk, in that case you should enable rescanning for that device with -> echo 1 /sys/block/sdX/device/rescan

    where sdX is the disk that was extended.

    2) on pvcreate, you don’t need to reboot, a partprobe on the disk should be sufficient to have it succeed after.

  29. Best lvm resizing guide so far. Good work.

  30. Muchas gracias!!!, me ayudo mucho! :)

  31. THANKS A LOT !!!!
    I went to at least 10 other sites with super complicated instructions. Finally found your wonderful to the point set by step guide.
    You made my day, Thanks a LOT

  32. Thank you very much !!! this is really good article and asy to follow instructions. :)

  33. Great post! Very easy to follow and worked like a charm..Thank you sir!

    • I totally agree with Amit.
      Great post. Clear Instructions. Superb Job.

      Thank you soooooo much!! Thanks Jarrod :smile:

  34. very useful thanks for your instructions

  35. Just wanted to say thank you for the great instructions! Went to a bunch of websites and yours was the only one which was clear and accurate.

  36. Thanks a million, the instruction you gave were so straightforward and clear you made the whole process very easy.

  37. Super clear instructions! Thank you, Jarrod.

  38. Everything worked great until the last step. I ran

    resize2fs /dev/ubuntu-vg/root

    But receive “open: no such file or directory while opening /dev/ubuntu-vg/root”

    I was using /dev/ubuntu-vg/root for the lvextend command which ran fine.

    lvdisplay lists
    LV Name /dev/ubuntu-vg/root

    any help would be appreciated.

    • I followed the entire guide by using a live bootable Gparted iso image, and I got the same error at the last step. But there’s only one simple thing you need to do: reboot into your VM ubuntu system, and apply the last command in your a shell:
      $ sudo resize2fs /dev/ubuntu-vg/root

      I expanded my linux VM by 15GB and this step took around 0.5 – 1 mins. After the process is completed, verify that you have an expanded available free space at your primary linux partition by issuing:
      $ df -h

      Hope this helps!

  39. Thanks. This article was extremely helpful.

  40. :razz:
    Great doc, everything works well, Thanks

  41. Trust me, this is a well needed article. Pity I didn’t come across this first before I ran into a lot of trouble.
    Great Work, Thanks.

  42. :grin:
    from 20 GB to 40 GB

    [root@tester ~]# lvextend /dev/vg_tester/lv_root /dev/sda3
    Extending logical volume lv_root to 35.56 GiB
    Logical volume lv_root successfully resized

    ive been try before in real production

    Thanks

  43. You are just awesome. :lol:

  44. Great doc, Thanks

  45. This worked perfectly. Thank you for the easy to follow intructions.

  46. Congratulations, wonderful post, helped me a lot!
    Thank you very much!

  47. Thanks a lot for this post. I use this as reference for my day to day work. Without this life wouldhave become miserable :grin:

  48. Very helpful. Thanks!

  49. worked great; thanks for putting this together.

    i noticed two possible bugs:
    1. the partx command on rhel6 should be for /dev/sda, not /dev/sda3
    2. the lvdisplay output should show LV Path rather than LV name, as the path is what needs to be passed to lvextend (on my rhel6 system)

  50. Thanks a lot Jarrod !!!
    I followed your instructions and it was just perfect

  51. Thanks, Great work

  52. Under RH ELS 6.2, I had to do this to see the extra space and then /dev/sdb2:
    # echo 1 > /sys/block/sdb/device/rescan (now see the extra space using “fdisk -l”)
    # partx -a /dev/sdb (now see /dev/sdb2)

  53. Thanks, worked like a charm.

  54. Thanks for the great post!
    Needed the parted tools.
    apt-get install parted

    Thanks again!

  55. amazing tutorial!!!
    one question though, do i have to stop the services on the machine i work (tomcat, mongod, mysql ext.)
    when i preform these steps?
    thanks

    • Thanks, nope you can increase on the fly with service running, extending the LV will not interfere with those running services.

  56. Thank you, great post. I have used it twice now. It has been bookmarked. Thanks Again!!!

  57. Well done, there are a lot of confusing posts about this process, this one is excellent. Step by step, and makes sense.

    Thank you

  58. Nice tutorial. Its amazing :cool:

Leave a Comment


× 8 = forty eight


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>