Thursday, January 24, 2008

How to add disk space with LVM on Linux

I have often had the need to add disk space to a server. The newer distributions of Linux (SUSE Linux Enterprise Server - "SLES" 9 and greater, Red Hat Enterprise Linux - "RHEL" 4 and greater) use Logical Volume Manager (LVM) which makes this a fairly simple task.
The general steps are:
  1. Physically put the disk in the server and make it available either in an array or some other way.
  2. Use fdisk to create new partition on the disk.
  3. Extend existing volume group onto new partition.
  4. Extend existing logical volume onto new space.
  5. Extend filesystem.
I'll go into more detail how to do each of these steps on SLES and RHEL. For this example we are going to assume that our new disk that has been added to our server is /dev/sdc. We will also assume that we have a volume group named rootvg and a logical volume on that volume group named homelv. Also in this example, /dev/rootvg/homelv is mounted on our Linux system as /home. Let's get started.
  1. Physically put the disk in the server and make it available either in an array or some other way.
    • This can vary widely depending on what hardware you are on, so we will not cover this portion in this tutorial. You may want to use Google to search for how to do this on your hardware.
  2. Use fdisk to create new partition on the disk.
    • As root run fdisk on the new disk. For example you would run fdisk /dev/sdc.
    • On this new disk you will create a new partition. Inside of the fdisk program you create a new partition by typing the letter "n" followed by the enter key.
    • It will then ask you if you want it to be a primary partition or an extended partition. For this example we will just assume we want it to be a primary partition. To do this we type the letter "p" followed by the enter key.
    • Next it will ask for the starting point and ending point of this partition. For this example, we will simply select the defaults by hitting the enter key twice.
    • If you want to view the details of the new partition, you can type the letter "p" from the fdisk prompt.
    • Next we need to change the type for this new partition. We do this by typing the letter "t" followed by the enter key.
    • It asks to enter in what type we want it to be. We need it to be of the Linux LVM type. The Hex code for Linux LVM is 8e. We type in "8e" followed by the enter key and it will tell us that it changed the system type for this partition to Linux LVM.
    • We then need to save these changes and exit fdisk. We do this by typing the letter "w" (for write) followed by the enter key.
  3. Use pvcreate to lay down the "setup" on the physical partition.
    • This sets up physical extents and other things that LVM needs. The command we run is "pvcreate /dev/sdc1"
  4. Extend existing volume group onto new partition.
    • For this example we are going to assume that we have an existing volume group named rootvg. We can display information about this volume group using the vgdisplay command. If we type "vgdisplay" without any arguments, it will display all the volume groups. If we just want information about the "rootvg" volume group, we would type "vgdisplay rootvg".
    • To extend the rootvg volume group onto the new partition, we use the vgextend command. Type "vgextend rootvg /dev/sdc1" followed by the enter key. This will extend the rootvg volume group onto the new partition, /dev/sdc1.
  5. Extend existing logical volume onto new space.
    • For this example we are going to assume that we have an existing logical volume on the rootvg volume group that is named homelv. The full path to this logical volume would be "/dev/rootvg/homelv". We can view information about this logical volume by typing "lvdisplay /dev/rootvg/homelv".
    • To extend this existing logical volume onto the new space we use the lvextend command. Type "lvextend -L +20G /dev/rootvg/homelv /dev/sdc1". This command says that we want to increase the /dev/rootvg/homelv logical volume by 20GB. This is only possible if /dev/sdc1 is a member of volume group rootvg and there is enough free disk space on it.
  6. The last thing we need to do is increase the filesystem.
    • Which command we use to increase the filesystem depends on what filesystem type we are currently using. If we are using reiserfs, we will use the "resize_reiserfs" command. If we are using ext3, we will use either the "ext2online" or the "resize2fs" command. On RHEL 5, we can use the "resize2fs" command, which will recognize that the filesystem is currently mounted and perform an "on-line" resize.
    • On both resize_reiserfs and ext2online, the size is optional. If we want the filesystem to take up all the available space on our logical volume, we don't need to put the size. To extend the filesystem type either "ext2online /dev/rootvg/homelv" or "resize_reiserfs /dev/rootvg/homelv".
You should now be able to run "df -h" and see the additional disk space available in the /home directory.

36 comments:

Muthukumar Krithivasan said...

After fdisk i had use pvcreate for the new partition before doing lvextend. This is for RHEL4.

Thad said...

Thanks for pointing that out. For some reason I always miss that step. I always seem to run vgextend and get the error that the physical volume doesn't exist and that prompts me to run pvcreate. I have edited the post to add that in.

Sean said...

Thanks Thad, this is the only sensible step by step guide I've found for to this procedure.

Nice work.

Kemi said...

Thanks Thad, helped out with a task I needed to do on RHAS4.

Brian Chostner said...
This comment has been removed by the author.
ProudPapa said...

Worked like a charm first time with no data loss. Thanks Thad!

Anonymous said...

Thanks Thad

I've played this game with Fedora9 running on VMVare.

Actually you simply extend VMVare virtual disk, start machine and .... before pvcreate you need reboot :)

Tnaks - simple clean and easy

viswa08 said...

Dear Thad,
Thanks for sharing broad steps,wel Iam basically Oracle DBA,need your valuable suggestion,to proceed with RAID5,our server admin says its not possible to add in RAID 5 we are using redhat linux 4.0.

Thanks in advance.

Regards,
Viswanath.

Available Internet said...

Thanks for a nice walktrough :-) How long does it take to format and online resize if I add 200G?

Dale B said...

I had space added to netapp partition and I can't see it on linux. I've tried to recreate partition using fdisk and fdisk only sees 100 GB and netapp partition is supposed to be 300 GB. Any idea of how I can get Linux to see additional 200 GB of space added or can you point me to some docs? I am stumped and exhausted.

Thad said...

In my environment, our netapp partitions are shared to us via NFS, so we don't have any problems seeing the new space. Outside of a reboot, I'm not sure how to fix the problem you are experiencing. Maybe a call to NetApp is in order?

Stefan said...

Thank you a lot for this walkthru. All the necessary "side info" is described! Not too much, not too less info, perfect! Successfully resized my very first logical volume using this info.

martyn said...

Thank you for your information. That was very usefully on my job! Thank you.

leocor said...

Thanks Thad, this post has been very very helpful.

Mark Nelson said...

Thanks a lot for your post. helped me out!

csm said...

Great steps! Was trying to "extend" my root partition in CentOS 5.3 under ESXi and it was killing me. Once I realized I had a logical volume, your post was perfect for getting the steps right (too bad "pvresize" would not work for me, but this is just as good). General info: all is fast up to the last "resize2fs" which took 5 min for 8GB on an HP ProLiant DL360 G4 dual xeon 3.4 with and HP U320 SCSI. Thank you!

Steve Deerfield said...

After spending 2 days looking for a solution, this post was what I was looking for. Suggestion: add a few more tags to this so people can find it easier. Thanks so much!

Anonymous said...

This article help me a lot! thanks for sharing Thad!

Anonymous said...

thanks.
its things like this that make doing linux admin easier for a non-linux admin (but all around linux junkie) :)

Superkikim said...

Clear, and straight-forward.

Thank you

Scooterista Marginado said...

Wow.. like a charm!!

joejvgblogger said...

Good job Thad. I do not have to do this very often and was rusty. This is the best one I have seen. Great Blog. Please do more on Linux topics as you seem to really know your stuff!

joejvgblogger said...

Hi Thad,

I am running CENTOS 5.5 on one of my Linux servers and ran across something readers may find helpful. I received an error on step 6 trying to resize the File System. Upon checking /etc/fstab I saw the file type of /dev/VolGroup00/LogVol01 was swap space. I then ran across this article http://www.how2centos.com/centos-lvm-resizing-guide/ which explained how to remove the excess swap space, add it to the main Logical Volume /dev/VolGroup00/LogVol00, resize it, and rebuild the /dev/VolGroup00/LogVol00 swap space. Might be helpful to people that run across this in CENTOS 5.5.

Anonymous said...

Thank you very much for this blog. It has been very helpful!

Muhammad Sergani said...

You need to do "partprobe" after fdisk, this is to re-load the partition table into the memory.

Anonymous said...

Great guide, thanks.

There is a step missing, after 2.3 it asks you which partition (1-4) and you have to choose.

Anonymous said...

Nice article .. thanks

Chris Markle said...

Thank you for this post. I just added more space to a Centos 5.8 LV with this procedure and it worked perfectly. Appreciate you posting this. Chris

Swaroop said...

Perfect :-) thanks a lot for such a valuable information.

goutham said...

Hi
thanks for the document.nice explanation for newbie like me.


thanks for your hard work

Anonymous said...

Badass link! This helped me after searching all day for the same information, great job!

Anonymous said...

This is by far the best post on expanding LVM. So many other "guides" deal with theory. This makes it clear and I've learned more about the levels in the process. Great, great service to us.

Francisco Larramendi said...

The use of pvcreate should be included in "The general steps" at de begining of the article. Thanks, very helpful.

Snapman said...

Just another "Thank you very much!" comment...this saved my butt.

Ben said...

Awesome - I really like the detail you put in here. Two quick comments. First, a resize making the filesystem larger can usually be done with the filesystem mounted (nice to see finally!) but shrinking is a bit tricky and cannot be done mounted. Learned that the semi-hard way.
,br>Second, I still don't do this often enough to remember 8e is the Linux LVM type. Fortunately "fdisk" has some really straightforward menus to use.

Have I mentioned I <3 Linux? Thanks for the very detailed post!

narendra a said...

Dear sir,

i have proliant server 365p how should i install linux redhat 6.2 os, i am confusing while installing because it has 430hdd and 4tb hdd is there, So please help me how to install red hat with proper installation step by step......

Thanku