Home » Debian, Featured, Linux, Ubuntu

How-to extend a LVM Partition online

10 March 2009 23 Comments

Disk Array
Working with virtual machines is a pain if you don’t start to plan, from begining, all resources of your real machine. So for my virtual machines I always create a standard LVM partition for root (/) and from begging I allocate only necessary space for my machine (because linux kernel can’t boot from a LVM partition you will be forced to create the /boot partition as normal one and not as LVM partition). After that, if I want, to resize my LVM partition I can do it ONLINE (with / mounted read-write)

Resizing a virtual disk in virtual machine is not our subject today and we will presume we have already 28Gb of free space on our disk (or we have another empty disk installed).

Note: Is about LVM 2 not LVM 1.


First we need to create a LVM partition from our free space what we have on our disk. As always, under linux, we can use several programs to do that: parted, fdisk and cfdisk (this are the programs what I know, but is possible to be other available). Because I’m lazzy I use cfdisk, but also, if you know the commands from fdisk, you can create the LVM Partition very very quickly. parted is a little bit low level, but is not hard at all. Anyway you can choose what disk partitioning tool you want, but first let’s look at the disk space

randombugs:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/randombugs-root
1.6G 634M 914M 41% /
tmpfs 506M 0 506M 0% /lib/init/rw
udev 10M 80K 10M 1% /dev
tmpfs 506M 0 506M 0% /dev/shm
/dev/sda1 228M 9.4M 207M 5% /boot

So my root (/) partitions have size of 1.6G. Now just run you favorite partitioning tool

randombugs:~# cfdisk /dev/sda3

Yes, is my desktop. Do you like it ?

Yes, is my desktop. Do you like it ?

I presume is not necessary to tell you how to create the partition with cfdisk, because is very simple. Don’t forget to set up the partition type to 8e (Linux LVM). Also check if you have lvm2 programs installed

randombugs:~# dpkg -l | grep lvm
ii lvm2 2.02.39-6 The Linux Logical Volume Manager

And if you dont have them (you dont have this line starting with ii) then install them

randombugs:~# aptitude install lvm2

After that we need to initialize this disk partition, what we just created, for use by LVM, but first let’s take a look at physical volumes:

randombugs:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 randombugs lvm2 a- 1.76G 0
randombugs:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name randombugs
PV Size 1.76 GB / not usable 4.17 MB
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 450
Free PE 0
Allocated PE 450
PV UUID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

So we have just 1 physical volume named randombugs with size 1.76G. Now let’s create another physical volume:

randombugs:~# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created

Let’s look again at our physical volumes:

randombugs:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 randombugs lvm2 a- 1.76G 0
/dev/sda3 lvm2 -- 28.00G 28.00G

We have another physical volume, but this new one is not attached to any volume. To extend the size of our existing volume we need to attach our new physical volume, what we just created, to volume group randombugs.

randombugs:~# vgextend randombugs /dev/sda3
/dev/cdrom: open failed: Read-only file system
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Volume group "randombugs" successfully extended

Now we look again to physical volume.

randombugs:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 randombugs lvm2 a- 1.76G 0
/dev/sda3 randombugs lvm2 -- 28.00G 28.00G

randombugs:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name randombugs
PV Size 1.76 GB / not usable 4.17 MB
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 450
Free PE 0
Allocated PE 450
PV UUID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--- Physical volume ---
PV Name /dev/sda3
VG Name randombugs
PV Size 28.00 GB / not usable 2.69 MB
Allocatable yes
PE Size (KByte) 4096
Total PE 7167
Free PE 255
Allocated PE 6912
PV UUID yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Right now we have the physical volume increased with 28G and we can extend it, but before extending let’s look at logical volume properties

randombugs:~# lvdisplay
--- Logical volume ---
LV Name /dev/randombugs/root
VG Name randombugs
LV UUID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LV Write Access read/write
LV Status available
# open 1
LV Size 1.62 GB
Current LE 414
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0

--- Logical volume ---
LV Name /dev/randombugs/swap_1
VG Name randombugs
LV UUID yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
LV Write Access read/write
LV Status available
# open 2
LV Size 144.00 MB
Current LE 36
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1

So the LV size is 1.62 G now let’s extend it

randombugs:~# lvextend -L+27.9G /dev/randombugs/root
Rounding up size to full physical extent 27.90 GB
Extending logical volume root to 29.52 GB
Logical volume root successfully resized

and look again to logical volume properties before reisizing the file system.

randombugs:~# lvdisplay
--- Logical volume ---
LV Name /dev/randombugs/root
VG Name randombugs
LV UUID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LV Write Access read/write
LV Status available
# open 1
LV Size 28.62 GB
Current LE 7326
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0

--- Logical volume ---
LV Name /dev/randombugs/swap_1
VG Name randombugs
LV UUID yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
LV Write Access read/write
LV Status available
# open 2
LV Size 144.00 MB
Current LE 36
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1

Now the LV Size is 28.62G. Until now is ok.

And now resizing your filesystem. Is not necesarly to umount your partition, but if you want to do that you will need a rescue boot cd with lvm2 tools on it. It seems you can do that online without umounting your partition but I strongly recommend you to do a backup before that and if you have programs with heavy writing on your disk then stop them (all will work faster and is more safe).

randombugs:~# resize2fs /dev/mapper/randombugs-root
resize2fs 1.41.3 (12-Oct-2008)
Filesystem at /dev/randombugs/root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/randombugs/root to 7738368 (4k) blocks.
The filesystem on /dev/randombugs/root is now 7738368 blocks long.

If you get a message something like that then is all ok. Let’s look again at the disk space

randombugs:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/randombugs-root
30G 1.8G 26G 7% /
tmpfs 506M 0 506M 0% /lib/init/rw
udev 10M 80K 10M 1% /dev
tmpfs 506M 0 506M 0% /dev/shm
/dev/sda1 228M 9.4M 207M 5% /boot

So far so good.

Some people strongly recommend to fsck the filesystem after that … but is possible to refer to LVM 1 because right now I’m using this partition from several weeks and all still working as expected :)

Good luck!


23 Comments »

  • nate said:

    WOW. Thank you very much! Life saver!!!

    I cloned a 80GB drive to a 329 then needed to expand the 80GB partition to 290+.

    Thanks again.

    Nate

  • admin (author) said:

    I’m glad to see people using this how-to’s :)

    Regards

  • Daeron said:

    Just wanted to drop a note on HOW COOL THIS IS. Muchos graçias!

  • How to extend a LVM Partition online Random Bugs | fix my credit said:

    […] How to extend a LVM Partition online Random Bugs Posted by root 42 minutes ago (http://www.randombugs.com) After that if i want to resize my lvm partition i can do it online with mounted read write add your comment below or trackback from your own site boot bug clustering core cryptography database debian dhcp ext2 ext3 glassfish grub powered by wordpress log Discuss  |  Bury |  News | How to extend a LVM Partition online Random Bugs […]

  • senshikaze said:

    Thanks a bunch, worked like a charm. Increased workstation’s LVM from 640GB to 1.1TB.
    Loved the desktop!
    (NSFW, of course, but thankfully I can do this at home :) )

  • sarah said:

    Thank you SO much. I was struggling with a problem for weeks and this guide finally helped me to solve it. You are my favorite person on the whole internet right now!

  • Per said:

    This nailed my problem in 5 min on my Debian 5 distro running as virtual machine under KVM. Great job!!

  • mandela900 said:

    i followed this how-to for extend one volume but i got this error

    fsck 1.39 (29-May-2006)
    /dev/mapper/vg00/lvm01: The filesystem size (according to the superblock) is 52428800 blocks
    The physical size of the device is 39321600 blocks
    Either the superblock or the partition table is likely to be corrupt!

    /dev/mapper/vg00/lvm01: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
    (i.e., without -a or -p options)

    so please i need help for this

  • Scott Yee said:

    That great to help me much of the resize Red Hat Ent 4.
    I am Windows base IT, when coming to Linux base admin really hit my head of perform task.

    Best Regards,

  • admin (author) said:

    did you run resizefs ? Did you follow all the steps above ?

    Regards

  • Erick Morelos said:

    fantastic , howto

    gracias

  • Andrea said:

    among thousand “howto” guides, this is the only one which really works

    Greetings from Italy

  • Alex said:

    Random, unrelated question – got a link for your wallpaper? Feel free to mail it rather than link it if you’d prefer.

  • admin (author) said:

    :) You can get it from here http://ubuntusatanic.org/wallpapers/SE-Bathory.jpg

  • Jean Paul said:

    Thanks JP.

  • Dennis said:

    Thanks a bunch. Great instructions, helped me a lot.

  • Nahuel said:

    Thank youuuuuuuuuuuuuuuuuuuuuuuuuu!!!!!!!!!1

  • Neeraj said:

    Saved a lot of time for me. Thanks a lot.

  • mac said:

    thankss

    this worked from me on Ubuntu 10.40

  • Christof said:

    nice desktop :-) and extremly useful facts, thx.
    My cfdisk, fdisk and parted won’t add the new partion at the first try.
    After rebooting the VW, all this runs without problems.

  • mr.likey said:

    Hey bro, I like your desktop.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

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

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.