Saturday, January 3, 2009

Setting Up LVM in Linux

Logical Volume Management: A system that allows you to group several hard drive partitions together, and then create arbitrary virtual partitions on top of it. This is super nice because when you run out of space on your hard drive, just add a second one, and instead of dealing with the C: and D: stupidity of Windows, just grow the file system onto the new hard drive invisibly. Want a bigger hard drive, without ever turning off the computer? Plug it in, add to the disk pool, migrate the partition onto it, grow to fit new hard drive, remove old one (note: only works with hot plug hardware, but even without it, still more convenient than the alternative).

Like always, I'm doing all of this in Ubuntu Linux (In this case, Ibex 8.10). Another outstanding guide for this is the Howto Forge's article.

Do realize that LVM is super confusing, so before anything else, have a good time reading the Wikipedia article to get the terminology straight:
  • Physical volume: The real partitions on each individual hard drive.
  • Volume group: The collection of physical volumes.
  • Logical volume: The imaginary partition made from the pool of physical volumes in the volume group.
Realize that LVM is below the file system, so once you create a logical volume, you need to format it before its useful.

My Setup
Two hard drives, sda (120GB) and sdb(100GB). You can have most of the file system (except the /boot/ folder) in LVM, but I'm lazy (and the installer borked) so I first just installed Linux in a 10GB partition on sda, created a 1GB swap space on sdb, and left the rest of it as empty space. The goal here is to make one huge /home/ partition between these two hard drives, giving me about 200GB of storage space. If I need to grow it later, I can take out my CD drives (who uses them after installing Linux anyways?) and install two more hard drives.

sudo apt-get install lvm2

Setting up the Physical Volumes
Now we need to create physical volumes (PVs) on the two hard drives to add to the volume group (VG). Using fdisk, create a partition on each, labeled for LVM:
sudo fdisk /dev/sda
and create a new primary partition, numbering it something not used. (Press m for a list of commands, in thiss case I pressed n enter p enter 2 enter) Use all the free space by hitting enter two more times. Set the partition type by pressing t, and select the partition you just created (2 in my case). Press L to list all the codes, but we just want 8e. When done, w to save and exit. Do the same thing for /dev/sdb as well. Both times, fdisk should crap out with a "WARNING: Re-reading the partition table failed with error 16: Device or resource busy" so reboot to detect the new partitions.

At this point we have:
  • /dev/sda1 (10GB): root for the Linux system
  • /dev/sda2 (~105GB): unformatted
  • /dev/sdb2 (~95GB): unformatted
  • /dev/sdb3 (1GB): swap space
The partition numbers are totally arbitrary (1-4), so if yours are different, just keep track of them.

Now turn these empty partitions into physical volumes:
sudo pvcreate /dev/sda2 /dev/sdb2
and check them with
sudo pvdisplay

Setting up the Volume Group
Now that we have the PVs setup, create a Volume Group to add them to. Once that is setup, we can create the final logical volume to format as a normal file system. We need to name the VG, and I usually just name it after the computer, KWF4 in this case, but you can name it whatever you want.
sudo vgcreate KWF4 /dev/sda2 /dev/sdb2
and check it with
sudo vgdisplay (surprised?)

Setting up the Logical Volume
We now have what appears (through a virtual disk driver) to the computer as a 194GB unformatted hard drive. We now need to create a LV on it to hold the /home/ file system, which I am so cleverly naming "home." I am using the total size of the VG (194.70GB), but that isn't required. You can setup several smaller partitions, but I'm not doing anything that complicated.
sudo lvcreate --name home --size 194.70GB KWF4
and check it with (wait for it...):
sudo lvdisplay

Notice that it lists the LV as /dev/KWF4/home. The LVM driver makes it appear as a normal partition to Linux, which is pretty cool. It's times like these that I really love Linux more than Windows.

Formatting the LV
With the logical volume all ready to go, all we have left is to format it with a file system, and add it to /etc/fstab so it gets mounted every time we turn on the computer. I'm using ext3, since it's standard and I don't have any special need for anything else (Reiser4, XFS, and ext4 come to mind)
sudo mkfs.ext3 /dev/KWF4/home
And since this thing is 200GB, I reset the auto-fsck count to a larger number
sudo tune2fs -c 75 -i 365d /dev/KWF4/home

Finally, add this line to /etc/fstab so the new home partition gets mounted on every reboot.
/dev/KWF4/home /home/ ext3 relatime 0 2

I also mounted the new partition and copied my home folder from the original install as well.
cd /home/
sudo mkdir temp
sudo mount /dev/KWF4/home temp
sudo cp -r kenneth temp/ (This borked on kenneth/.gvfs, still seemed to work fine though)
sudo chown -R kenneth:kenneth temp/kenneth/

And hopefully everything worked and we're done! Now reboot, and run mount to make sure everything worked. (Look for a /dev/mapper/KWF4-home on /home line)


Edit (1/5/09): Something is broken. It all worked fine until I hit Gigabyte #106, which seems to line up just right with the second hard drive, which I know works[1]. This is the first time I've used a slave device on this particular computer. The swap partition worked though, which is driving me nuts. LVM driver problem? Some other error which happens to line up? All I know is that the whole computer locks up and the HHD activity light is full on, with no activity.

[1] Yeah, I know; a little piece of me died when I made that assertion.

No comments:

Post a Comment