How to manage logical volumes

Note:
This documentation has moved to a new home! Please update your bookmarks to the new URL for the up-to-date version of this page.

The Ubuntu Server installer has the ability to set up and install to LVM partitions, and this is the supported way of doing so. If you would like to know more about any of the topics in this page, refer to our explanation of logical volume management (LVM).

Create the physical volume

First, you need a physical volume. Typically you start with a hard disk, and create a regular partition whose type is “LVM” on it. You can create it with gparted or fdisk, and usually only want one LVM-type partition in 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 initialise it as a physical volume. Assuming this partition is /dev/sda1:

sudo pvcreate /dev/sda1

Create the volume group

After that, you can create a volume group; in our example, it will be named foo and uses only one physical volume:

sudo vgcreate foo /dev/sda1

Create a logical volume

Now you want to create a logical volume from some of the free space in foo:

sudo lvcreate --name bar --size 5g foo

This creates a logical volume named bar in volume group foo using 5 GB of space. 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.

Resize a partition

You can extend a logical volume with:

sudo lvextend --resizefs --size +5g foo/bar

This will add 5 GB to the bar logical volume in the foo volume group, and will automatically resize the underlying filesystem (if supported). The space is allocated from free space anywhere in the bar volume group. You can specify an absolute size instead of a relative size if you want by omitting the leading +.

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 are used to fulfil the request.

Move a partition

If you only have one physical volume then you are unlikely to 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 can run:

sudo pvmove --name bar /dev/sda1

If you omit the --name 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. Otherwise 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 --background to perform the move in the background and return immediately, or --interval 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.

Create a snapshot

When you create a snapshot, you create a new logical volume to act as a clone of the original logical volume.

The snapshot volume does not initially use any space, but as changes are made to the original volume, the changed blocks are copied to the snapshot volume before they are changed in order to preserve them. This means that the more changes you make to the origin, the more space the snapshot needs. If the snapshot volume uses all of the space allocated to it, then the snapshot is broken and can not be used any more, leaving you with only the modified origin.

The lvs command will tell you how much space has been used in a snapshot logical volume. If it starts to get full, you might want to extend it with the lvextend command. To create a snapshot of the bar logical volume and name it lv_snapshot, run:

sudo lvcreate --snapshot --name lv_snapshot --size 5g foo/bar

This will create a snapshot named lv_snapshot of the original logical volume bar and allocate 5 GB of space for it. Since the snapshot volume only stores the areas of the disk that have changed since it was created, it can be much smaller than the original volume.

While you have the snapshot, you can mount it if you wish to see the original filesystem as it appeared when you made the snapshot. In the above example you would mount the /dev/foo/lv_snapshot device. You can modify the snapshot without affecting the original, and the original without affecting the snapshot. For example, if you take a snapshot of your root logical volume, make changes to your system, and then decide you would like to revert the system back to its previous state, you can merge the snapshot back into the original volume, which effectively reverts it to the state it was in when you made the snapshot. To do this, you can run:

sudo lvconvert --merge foo/lv_snapshot

If the origin volume of foo/lv_snapshot is in use, it will inform you that the merge will take place the next time the volumes are activated. If this is the root volume, then you will need to reboot for this to happen. At the next boot, the volume will be activated and the merge will begin in the background, so your system will boot up as if you had never made the changes since the snapshot was created, and the actual data movement will take place in the background while you work.

This page was last modified 3 months ago. Help improve this document in the forum.