Tuesday, January 1, 2013

Lvextend to the rescue. - Increasing the size of Root partition in Linux Filesystems.

I'm a linux newbie. I've been playing with it for about a year and a half now and I still see myself a novice user. Probably because I'm not a proper computer science student or may be I wasn't good enough with linux filesystems. Yeah, these things are such a pain in the ass. I agree. Root, Swap, /Boot, Logical volume, physical volume, ext4, lvm, sda, partition, bla-bla-bla, bla-bla-bla... "Oh my God! Spare me the linux mumbo jumbo. I just wanted to be an average user. I didn't want my linux experience to suck bad! Especially with drives and partitions. Grant me this wish, please..." I begged. "What is it son?", asked the Almighty.
"My linux root partition (the partition represented by "/") is running out of space. What do I do? I'm not good with partition manager or the command lines, for I know nothing." God replied, "Here's how..."
First understand what Logical Volume and a Physical volume is. Physical volume is the physical, abstract partition that exists. It is hard coded. That is how the disk drives and the drive partitions in it exists. Logical partitions doesn't exist as a single entity altogether for you to find and point at. They are made out of groups of physical volumes. I'd rather suggest you to refer to the diagram at the LVM Wikipedia page for more information.
Well, here's the real problem. You have installed your linux operating system in your PC by some means. Either using conventional installer mechanism, like Anaconda in Fedora or RedHat supported OSes or like me, with "Install inside Windows" as in Ubuntu. :P
Whatever it is, let's assume for the sake of this article or for real that you had messed up with the partition sizes and now you notice that the size of your root is annoyingly small. Annoying because of the pop-ups that come up, as and when you start to meddle something in it or when you boot your PC up. Goal is to increase any of the size of root, obviously. But, you want your other files to be intact and your existing Windows-Linux dual boot to be undisturbed or rather to put it precisely, everything to be the same except that you want your root size to be increased. "No way!." would tell the old me. "Bitch please... Get me your computer.", I shall tell now.
I'm taking that the linux OS you run shall work in legacy grub or the old grub, to be clear to all(E.g. Fedora 15, 16). Because, that's where you'd find a /boot partition as against grub2 where you wouldn't find one like that. Not an issue. All that really matters is to identify the root partition and increase the size, which I'm sure you can recognize as to what it is. That is, to find if it is /dev/sda9 or /dev/sda10 or whatever it is. And also, we're considering that the file system of the root partition to be lvm2 pv. (linux volume management).

Now, first step: Shut down your computer and make a bootable device with a linux OS. Preferably Fedora, because that's what I'm going to be using throughout and I guess it'll be easy for you to follow. If not, not a problem. Just make sure you install the packages using command line or through GUI.

Step 2: Boot from bootable device and install "Gparted." GUI partition manager for linux in the Live OS.
su -c 'yum install gparted'
snapshot1
Step 3: Run it as adminstrator, find a suitable drive and create a partition out of it, as unallocated space. The space you take out of the original drive is purely your wish. You may take out 5GB or 10GB and all that is left to you. If you can allocate an entire drive to increase the size of root, then fine. Else, follow this way.
Step 4: Right click the unallocated space and make a partition, preferably the same format as that of the lvm2 pv. (ext3 or ext4) After you format, let's assume the new device formed under sda to be /dev/sda10. Replace the sda10 with whatever you get, like if it is sda5, you take it to be /dev/sda5. Right?
snapshot2
Step 5: You can't simply extend this new volume to the root. That is because Gparted doesn't work with LVMs. You'll have to go the command line way.
Type,
"pvcreate /dev/sda10"
Step 6: Type
vgextent <volumegroupname> /dev/sda10
How do you find your volumegroup name ? Type
vgdisplay
and the VG name in it, specifies the Volume Group Name which in my case was vg_laptop.
So the command would be,
vgextent vg_laptop /dev/sda10
snapshot3
Step 7: Next is to find the number of extents. You can achieve this either using command
vgdisplay -v
as like "vgdisplay -v vg_laptop" where the Free PE value gives the number of extents.
Alternatively, you can use, vgdisplay command, that again displays the Free PE value.
Here, I found my Free PE value to be somewhere around 319. (Don't get confused with the screenshots provided. They have been given just for example purpose and doesn't actually reflect the original values that were executed for implementation.)
Step 8: Type
"lvdisplay"
to find the Logical Volume Path.
Which in this case was found to be: LV Path: /dev/vg_laptop/lv_swap
Step 9: Type
lvextend -l +<number-of-free-extents-found-in-step7> <logical-volume-path>
e.g., lvextend -l +319 /dev/vg_laptop/lv_swap
Step 10:
resize2fs /dev/vg_laptop/lv_swap
Voila ! Your root size has just been increased.
Sources :

http://serverfault.com/questions/297631/extend-volume-group-with-adjacent-unallocated-space
http://www.centos.org/docs/5/html/Cluster_Logical_Volume_Manager/nofreeext.html
Post a Comment