Sunday, July 21, 2013

LVM and partitions

One thing to consider when installing Linux is hard drive partitions. The default for most Linux distributions is to have one big partition for root and a small swap partition. That's it.

This is less than ideal. It's particularly inconvenient for those that have a lot of user data and like to experiment with different varieties of Linux. With one's user data being in the same partition as the OS... you end up having to backup and restore all your data whenever you want to wipe your OS and install something different.

It's super nice to have a separate partition for /home where you park all your user data and settings. Then you can wipe /root anytime you want and install a new OS without your data being affected (of course you would never do that without some sort of backup, just in case).

This is why I chose to use LVM when installing Linux Mint. Logical Volume Manager (LVM) is a partitioning scheme with an abstraction layer that makes resizing partitions a pretty straight forward thing.

Unfortunately I had trouble with the Linux Mint installer. It would allow me to use LVM, but it would not allow me to configure the LVM via the installation interface. (I have since learned... it's best to boot into a live Linux DVD and setup LVM before doing the install)

Anyway, so I installed Linux Mint into a single LVM volume on both my machines. I then tracked down an excellent article on how to resize LVM volumes and the file systems within them.

In order to do this I booted my machine off of a live Linux DVD, because you can't resize these partitions while they are mounted. Once booted off the live DVD, I issued the command "sudo su" in order to gain root access (there is no password on a live DVD).

I was then able to use the following commands to make this all happen...

vgdisplay mint-vg

(displays the info for the volume group mint-vg. issuing the vgdisplay command without an argument will display all volume groups.)

lvdisplay /dev/mint-vg/root

(displays the info for the logical volume root in the volume group mint-vg. issuing the lvdisplay command without an argument will display all logical volumes.)

lvresize -L 500G /dev/mint-vg/root

(resizes the logical volume root in the volume group mint-vg to be 500GB.)

lvcreate -L 500G -n home mint-vg

(creates a logical volume named home in the volume group mint-vg with a size of 500GB.)

lvresize -l +100%FREE /dev/mint-vg/home

(resizes the logical volume home in the volume group mint-vg to use 100% of the space available in the volume group mint-vg.)

mke2fs -t ext4 /dev/mapper/mint-vg-home
(creates a new ext4 file system in the logical volume home in the volume group mint-vg.)

resize2fs /dev/mapper/mint-vg-root 500G

(resizes the file system on the logical volume root in the volume group mint-vg to a size of 500GB.)

resize2fs -p /dev/mapper/mint-vg-home

(resizes the file system on the logical volume home in the volume group mint-vg to take the maximum space available on the logical volume.)

e2fsck -f /dev/mapper/mint-vg-root

(checks the file system on the logical volume root in the volume group mint-vg for errors.)

I first shrunk my original /root file system using the resize2fs command. Then I shrunk the logical volume that contained it to match using lvresize. I then created a new logical volume for /home using lvcreate. Then I pumped it up to maximum size using "lvresize -l +100%FREE" which expands it to fill any remaining space in the volume group. I then created an ext4 file system in the new logical volume using mke2fs. I then used resize2fs with the -p parameter in order to expand it to fill all available space in the logical volume.

Now comes the fun part. How to actually make the swap? I found a great knowledge base article on the ubuntu web site that tells exactly how to do this.

The main nuggets I got from this article were two commands:

sudo rsync -aXS --exclude='/*/.gvfs' /home/. /media/home/.

(this command duplicates the /home directory while preserving ownership and permissions.)

cd / && sudo mv /home /old_home && sudo mkdir /home

(this actually does the swap by executing several commands in sequence.)

If you're going to do this you should read the article. Because there are some /etc/fstab modifications that need to happen between steps.

Easy peasy.