Resizing Ubuntu's root file system with LVM

Resizing Ubuntu's root file system with LVM
04-05-07 05:57
Age: 1 yrs



Category: Typo3


For a long time had a lot of trouble with LVM. I had wanted increase the size of my root partition, but was always unable to do so. I finally figured how to do it, with a little help from Knoppix LiveCD.

I did all this in VMware, but it should work for any other systems as well. I don't document EVERYTHING here, e.g. the full syntax of all commands, man pages are your friends. I also assume the reader has some conceptual knowledge of LVM.


1. Install the hardware.


2. Start Ubuntu. Do a "fdisk -l" just to confirm that. You need to have root permissions. In Ubuntu, a "sudo -s" will do that for you.


In this screen /dev/sdc is the new hard disk.


3.


Fdisk the new hard disk "fdisk /dev/sdc":


i. Type "n" to create a new partition,
ii. then "p" to select to create a primary partition,
iii. then "1" for the partition number,
iv. then "1" to start from the first cylinder,
v. for the last cylinder I just pressed enter for the default max size. You can enter whatever you want here.


vi. We need to say this is a LVM partition. Type "t" to set the partition's system id. Then "8e" to set the partition for LVM.

Finally type "w" to write the changes to the disk.


4. If you "fdisk -l" now, you will see that /dev/sdc1 is now a LVM partition.


5. We create the physical volume (PV) for the new partition. AFAIK, its just one PV per LVM partition. This is simple. "pvcreate /dev/sdc1" will do it for you. (note: "pvdisplay" will show you your PVs)


6. I'm adding a PV to en existing Volume Group (VG), so I'll just do a "vgextend". The syntax is "vgextend <<existing VG>> <<new PV>>". So in my case, its "vgextend Ubuntu /dev/sdc1". I do a "vgdisplay" to see the current state of my Ubuntu VG.


As you can see, my VG size is 19.75GB. It used to be 11.75. My new harddisk sdc is 8GB. You can also see that Free PE is 8GB. That's because the VG has grown by 8GB, but none of the Logical Volumes (LV) floating inside it has been extended to use the extra breathing room yet. Uh huh. In fact, instead of extending one of the LVs in fill up the extra space, I could also create a NEW LV to fill it up. Heck I could create 2 LVs. But let's just keep it conceptually simple: I'm going to extend an existing LV to use the new binary real estate. As it turns out, because this LV contains the root file system, this leads to some complications later. Read on.


7. Time to extend the LV to use the extra space. The command is "lvextend -L+8G /dev/Ubuntu/root" This says, "extend the Logical Volume /dev/Ubuntu/root, yes! Increase it by 8 GB."


Oops! What's going on? I'm confused. I added a 8GB hard disk right? I do a vgdisplay just to confirm that I do indeed have 8GB of free VG space:


There it is! 8GB. So what's going on? ahhhh, I see that the Free PE is 2047. If we multiply the number of PE by the PE size, we get a number close to 8GB, but not exactly: 2047 x 4MB = 8188MB. Ok, let's try it again, but instead of increasing it by 8GB, we'll increase it by 8188MB, which is a little less than 8GB.


It works.


8. Now the LV may be more than 19GB, but my file system is only 12GB. This is analogous to adding 10 more floors to a office building of 50 floors (extending the LV), but the directory at the lobby only lists offices up to the 50th floor. The directory doesn't know that there are now physically 10 extra floors up to 60.

What we have to do is resize the filesystem. For a ext3 file system, there is a script called "resize2fs". For Reiser FS, its called "resize_reiserfs". I assume other file systems have their own command for resizing themselves. Before you resize, you need to check the file system. For ext2/ext3, this is done by doing "e2fsck". For Reiser, its "reiserfsck". So the commands are "e2fsck -f /dev/Ubuntu/root" and "resize2fs -f /dev/Ubuntu/root". So I'll just type these 2 commands and I'll just.... !! Now what?

Ahhh.... If I do a "df", I can see that the LV /dev/Ubuntu/root is mounted on the root file system. I'll have to un-mount the root file system to resize it. Un-mount the root filesystem? That's impossible. Its like the snake eating its own tail. Cutting off the branch you're sitting on.

So what do we do? We'll need to boot to a live CD, so that our root file system is not mounted. I use Knoppix 5.1. If you haven't yet downloaded Knoppix, get it now. Chances are you'll be using it a couple more times in your life.


9. So I shutdown my Ubuntu, insert my knoppix and boot from the CD. At the boot prompt, I type "knoppix 2", which is knoppix syntax for "please boot to text mode".


10. The latest Knoppix should have LVM utilities. 5.1 definitely has them. So I do a "lvdisplay", and voila, I see my logical volumes, and they have the same names too! /dev/Ubuntu/root. Cool. But when I do a "ls /dev/Ubuntu", that block device does not exist.

What's going on?

It appears that Knoppix does not automatically mount the LVM filesystems, nor have them as block devices in the /dev directory. So to make available, we use this command: "vgchange -a y". This basically says "change the attrbute 'availability' to 'yes'". Since I do not specify any specific volume group, all volume groups will become available.


Yes, /dev/Ubuntu/root is now available.


11. We now check the file system and resize it. It takes a couple of seconds.


12. Sweet. We reboot out of Knoppix into good ol Ubuntu. We do a "df", and our root filesystem now has 20 GB.


Thanks for reading this.








<- Back to: Articles

Work Blog

Breaking News:
27-07-07 16:52
Flex Data Services installation on Ubuntu

I recently had to install Flex Data Services (now called LiveCycle Data Services, but I installed FD...

[more]

04-05-07 05:57
Resizing Ubuntu's root file system with LVM

For a long time had a lot of trouble with LVM. I had wanted increase the size of my root partition, ...

[more]
Cat: Typo3

27-03-07 06:02
Subclipse Error when creating tags

I use Subclipse, the Subversion client for Eclipse, for development. And I got an error when I tried...

[more]
Cat: Typo3

go to Archive ->