LVM in Ubuntu

Teng   January 16, 2016   No Comments on LVM in Ubuntu

So how do I start using LVM?

The alternate installer has the ability to set up and install to LVM, and is the supported way of doing so. You can install the lvm2 package on an existing system, or the desktop livecd and manually set it up, and then install to it. This is what I will cover. (Ubuntu 12.10 has since introduced LVM support from the installation live CD.)

First, you need a Physical Volume. Typically you start with a hard disk, and create an LVM type partition on it. You can create one with gparted or fdisk, and usually only want one partition to use the whole disk, since LVM will handle subdividing it into Logical Volumes. In gparted, you need to check the lvm flag when creating the partition, and with fdisk, tag the type with code 8e.

Once you have your LVM partition, you need to initialize it as a Physical Volume. Assuming this partition is /dev/sda1:

sudo pvcreate /dev/sda1

This writes the LVM header to the partition, which identifies it as a Physical Volume, and sets up a small area to hold the metadata describing everything about the Volume Group, and the the rest of the partition as unused Physical Extents. After that, you need to create a Volume Group named foo:

sudo vgcreate foo /dev/sda1

Now you have a Volume Group named foo. I suggest you change foo to a name meaningful to you. foo contains only one Physical Volume. Now you want to create a Logical Volume from some of the free space in foo:

 sudo lvcreate -n bar -L 5g foo 

This creates a Logical Volume named bar in Volume Group foo using 5 GB of space. If you are installing, you probably want to create a Logical Volume like this to use as a root filesystem, and one for swap, and maybe one for /home. I currently have a Logical Volume for a Lucid install, and one for a Maverick install, so that is what I named those volumes. You can find the block device for this Logical Volume in ‘/dev/foo/bar’ or ‘dev/mapper/foo-bar’.

You might also want to try the lvs and pvs commands, which list the Logical Volumes and Physical Volumes respectively, and their more detailed variants;lvdisplay and pvdisplay.

If you are doing this from the desktop livecd, once you have created your Logical Volumes from the terminal, you can run the installer, and use manual partitioning to select how to use each Logical Volume, and then install.

Resizing Partitions

You can extend a Logical Volume with:

 sudo lvextend -L +5g foo/bar 

This will add 5 GB to the bar Logical Volume in the foo Volume Group. You can specify an absolute size if you want instead of a relative size by omitting the leading +. The space is allocated from any free space anywhere in the bar Volume Group. If you have multiple Physical Volumes you can add the names of one or more of them to the end of the command to limit which ones should be used to satisfy the request.

After extending the Logical Volume you need to expand the filesystem to use the new space. For ext 3/4, you simply run:

 sudo resize2fs /dev/foo/bar 

Moving Partitions

If you only have one Physical Volume then you probably will not ever need to move, but if you add a new disk, you might want to. To move the Logical Volume bar off of Physical Volume /dev/sda1, you run:

 sudo pvmove -n bar /dev/sda1 

If you omit the -n bar argument, then all Logical Volumes on the /dev/sda1 Physical Volume will be moved. If you only have one other Physical Volume, then that is where it will be moved to, or you can add the name of one or more specific Physical Volumes that should be used to satisfy the request, instead of any Physical Volume in the Volume Group with free space. This process can be resumed safely if interrupted by a crash or power failure, and can be done while the Logical Volume(s) in question are in use. You can also add -b to perform the move in the background and return immediately, or -i s to have it print how much progress it has made every s seconds. If you background the move, you can check its progress with the lvs command.

Remove the virtual group/logical volume/physical volume

sudo vgremove <groupname>

More information:

  1. How to Manage and Use LVM (Logical Volume Management) in Ubuntu
  2. lvm
  3. How To extend root partition

sshot4d33ca58bce04

Leave a Reply