In this guide, we'll start with a default Ubuntu 12.10 (quantal quetzal) LVM installation and run you through how you can use LVM as a flexible solution for your storage needs. Having said that, any other Ubuntu release or other Linux based distribution using the same set of tools (called lvm2) will find this stuff useful.
Note that in this guide, we'll carry out all the LVM actions in our examples with the command line tools whilst using a GUI tool to help illustrate what has been done.
It's also worth pointing out now that the Ubuntu 12.10 installer does not support creating the partitions for a custom LVM layout itself, but it will allow you to use a custom layout that you have already prepared with live CD/USB (in which case you would select the "something else" option rather than "Use LVM with the new Ubuntu installation". We shall cover this in a future guide).
The default layout should be OK for most people's purposes, in any case bear in mind that you can always change the layout yourself later if required (which we shall also be covering in a future guide).
What is LVM?
In human terms, the logical volume manager (LVM) in Ubuntu & other Linux distributions is software that allows for a higher level and more flexible management of your hard disks by creating logical ones (called logical volumes).
These logical volumes can be made up from different physical hard disks, yet thanks to software mapping they will appear as one volume on which a file system can be installed.
In other words, small physical chunks of disk space from anywhere on any disk can be melded together to provide what appears to be one big disk.
Through LVM, you can create what appears to be a normal usable partition that underneath, potentially consists of different physical underlying disks and/or partitions.
As you'll see later in this guide (and also in future follow up guides), LVM allows you to do some really neat stuff.
Always backup important data before modifying volumes!
Before you carry out any sort of operations on your disks (whether using LVM or not) you should always back up important data. There is always a risk of accidental data loss.
There are many reasons why you may want to use LVM. Although there is a bit of a learning curve to using LVM, once you know how to use it, it allows for far more flexibility than messing around with physical partitions ever will. Below are some of the reasons people choose to use LVM:
- No need to know how much space a partition will need in advance because logical volumes can be easily be resized. You can even extend them while they are in use! If you run out of space, just throw a new physical disk or partition into your setup then allocate that resource to wherever you need it. Alternatively you could reduce the size of one of your volumes and give the resource to another volume that's running low on space.
- You can move a file system on a logical volume to another underlying physical disk while still in use. This allows you to do things like remove a hard disk from your system without any down time.
- You can create Logical Volume Management Snapshots of your current system which you can easily revert to (for example if you screwed something up by installing new software or messed up a config file somewhere.). You can mount these snapshots if need be, you can even boot into a snapshot if required. One nice thing about snapshots is that they don't use up a lot of space. It's only when data is changed on your main volume, that the original data gets copied to the snapshot (this concept is called copy on write).
- You can create one or more mirrors of a logical volume on different underlying disks so that if your main hard disk fails, you'll have no data loss.
- You can create striped logical volumes which can improve performance because the underlying disks can be read in parallel.
In order to be able to use a physical disk or physical partition with LVM, it must be initialised for LVM use.
As part of this stage, physical disks/partitions get carved up (conceptually, by the LVM software - not physically!) into small chunks (of 4MiB by default) called physical extents.
After this initialisation, the relevant physical disk or physical partition is referred to as a physical volume. Many different underlying disks can be modeled this way. The space they provide can then be thrown into the same pot, or to use the correct term volume group. In one of these volume groups, LVM can use these physical extents as the building blocks for a logical volume, which appears to your Linux system as a normal partition on a normal hard disk. As such, the logical volume is where your file system gets created.
In the logical volume, the chunks that map to physical extents are called logical extents.
So to look at it another way, the contiguous logical extents which make up the logical volume can map to non-contiguous physical extents, thus residing in different physical locations on a disk and even different disks.
You can create more than one logical volume from the pool of physical extents in a given volume group.
A volume group is responsible for tying together a pool of physical disk space to one or more usable logical volumes.
If need be, you can create more than one volume group.
Most desktop users will probably only need to use one LVM volume group. However, in special cases (for example production servers), people may want to create more that one volume group. For example for a server that streams video the owner may want at least two volume groups, one consisting of several normal physical disks perhaps used for logging, and another volume group consisting of super fast disks to fetch the data for streaming.
For illustration purposes, lets just show you what a default 12.10 LVM install onto one 500GB hard disk would look like.
The default install consists of 3 partitions: /dev/sda1
(used for /boot
), /dev/sda2
which is used for an "extended partition" which contains /dev/sda5
. This /dev/sda5
is a partition configured for LVM. It is used to provide space for the two default logical volumes. One for swap space, and the other for the root file system /
.
How does this appear to fdisk
and gparted
?
fdisk
The command line tool fdisk
sees this:
As you can see, the partition /dev/sda5
is an LVM partition. The partition id for it is set as type 8e
.
It is recommended (good practice) that any partition used by LVM is marked as type 8e
.
gparted
When we look at the same default layout using gparted
(shown below), we get a better idea about how things are laid out physically on the hard disk:
We can see that /dev/sda2
is indeed an extended partition and it holds a partition used for an LVM physical volume (pv). But still we have no information about the LVM's logical volume layout or the way that physical volumes map to the logical volumes.
As you may have guessed from the above fdisk
and gparted
screenshots, these tools do not yet have the functionality to identify any logical volumes. They can only identify that a given partition is being used by the logical volume manager.
Below is a screenshot from a handy graphical tool which you can use to view all logical and physical volumes.
As you can see, the blue cylinder shows the logical volumes swap_1
and root
which are installed as the default 12.10 LVM layout.
The red cylinder shows the underlying physical layout, which consists of one single partition called /dev/sda5
. As you can see from the text on the right of the screenshot, there is one volume group called ubuntu
. By clicking on the volumes under physical view and logical view, you can see more information about each physical and logical volumes respectively. In this case, with a default install you'll be able to see details about your swap_1
and root
logical volumes.
The above tool (which can be quite handy) is called system-config-lvm
. To install it if you're using a laptop or desktop:
sudo apt-get install system-config-lvm
You can also use this tool to perform your LVM actions for you. However, it does not recognise raid properly, so could mislead people using raid.
To properly understand how to administer LVM, you really ought to know how to use the command line tools (which are packaged as lvm2 and already included in a Ubuntu 12.10 LVM install). Having said that you can get most jobs done using the system-config-lvm
GUI.
An interesting thing to point out about the default Ubuntu 12.10 layout is that /dev/sda1
(where /boot got installed) is on a real physical partition.
Strictly speaking, /boot
could also have been installed on a logical volume rather than a real physical partition because Ubuntu uses grub2
which does recognise logical volumes.
This is a good time to point out where you can find the logical volumes on your filesystem (or rather what to call them when you need to reference them).
You can use this type of path: /dev/volumeGroup/logicalVolume
or this type of path: /dev/mapper/volumeGroup-logicalVolume
So, taking swap_1
as an example, you can use either of these two ways to reference it:
/dev/ubuntu/swap_1
or
/dev/mapper/ubuntu-swap_1
If you do an ls -la
on these, you'll see they both point to the same underlying block device.
Now then, lets press on using the default 12.10 LVM layout.
Lets start out at the command line by introducing the three main display commands: pvdisplay
, vgdisplay
, and lvdisplay
.
To see what physical volumes are configured for LVM, use the pvdisplay
command. When used with its -m
option, you can also see the range of physical extents and how they map to logical extents.
sudo pvdisplay -m
Output will look something like that shown below:
Note that if you only want to display info about specific volume(s), just include the name(s) as part of the command, so for example to see info about /dev/sda5
specifically, you would use:
sudo pvdisplay -m /dev/sda5
As we described earlier, a volume group manages a pool of physical extents and the logical volumes you've built from them. To see details about the volume groups that exist on your system, use the vgdisplay
command:
sudo vgdisplay
This will produce output like that shown below:
To see a lot more information (verbose mode), use with -vv
.
Note that to only see the info about a specific volume group or groups, just include the name(s) as part of the command, for example to display the ubuntu volume group, use:
sudo vgdisplay ubuntu
Finally, if you'd like to display the logical volumes on your system, use the lvdisplay
command.
sudo lvdisplay
Sample output is shown below:
As with the other display commands, to display details about specific logical volume(s), just include the name(s) as part of the command, so to display info about swap_1
logical volume, you could use:
sudo lvdisplay /dev/ubuntu/swap_1
You have two choices when choosing how to add physical disk space to logical volume management. You can either use a whole disk or you can use one or more partitions on a disk.
When other operating systems have visibility of the disk, it is recommended that you pass partitions to LVM rather than whole disks. The reason for this is because if a whole disk is used for LVM, an operating system other than Linux may not recognise the LVM metadata and show the disk as being empty, thus adding risk of accidental overwrite.
Prepare A Whole Disk For LVM
Note when you setup a whole disk for use with LVM, any existing data on that disk will be lost! For whole disks (that is when you want to use the whole disk for LVM) you first need to erase any existing partition table by zeroing out the first sector. Suppose we added a new disk which was represented by the device /dev/sdb
, we'd use:
dd if=/dev/zero of=/dev/sdb bs=512 count=1
Now the whole disk /dev/sdb
is ready to be initialised for use with LVM.
Prepare A Partition For Use With LVM
Alternatively, to prepare a partition for LVM, when you create the partition, you should follow the below instructions for setting the LVM type.
Using fdisk
Run the following command:
sudo fdisk /dev/sdb
Please verify that this is the disk you wish to use, press p as shown below.
If you are certain that this is the disk you want to use, then proceed.
As you can see, in our example we're using a new 250GB disk.
Press n to create a new partition, then press p to indicate that its a primary partition.
Now you need to enter the partition number. If this is a new disk, press 1 here because we're creating the first partition, otherwise press the appropriate partition number for your new partition.
As you can see above, you'll be prompted to enter the first sector and the last sector to use for the partition. If you're using the whole disk for LVM (as we are), just hit return to accept the default first, then hit return again to accept the default last sector.
Next, we need to set the type of the partition to
8e
(which signifies LVM).Hit t, then enter
8e
as shown below.Now we need to write those changes by entering w:
You can now verify that these changes have been made by running:
sudo fdisk -l /dev/sdb
You can see the output from our
/dev/sdb
shown below which shows that/dev/sdb1
has system type as "Linux LVM":The partition
/dev/sdb1
is now ready to be initialised for Logical Volume Management.
Creating A Physical Volume From Physical Disks/Partitions
Assuming you've already prepared your disk or partition for LVM as described above, before adding it to a volume group's pool, the physical disk or partition must be "initialised" to create a physical volume that is recognised by LVM.
In order to initialise a disk or partition for use with LVM, you need to use the pvcreate
command.
To initialise a whole disk as a physical volume, use:
sudo pvcreate /dev/sdb
To initialise a partition as physical volume, just use the partitions device name. For example for the first partition on disk /dev/sdb
(which is called /dev/sdb1
), use:
sudo pvcreate /dev/sdb1
The output from using pvcreate
to initialise /dev/sdb1
as an LVM physical volume is shown below:
In order to add a physical volume to a volume group's pool of physical space, you need to use the vgextend
command.
In our example, we'll add the partition /dev/sdb1
(approx 250GB) to the ubuntu volume group.
Here's a screenshot of the ubuntu volume group's info before we do that:
If we then run the command:
sudo vgextend ubuntu /dev/sdb1
You should see this message:
Volume group "ubuntu" successfully extended
Here's what the volume group looks like after that command:
As a side note, if you now do a pvdisplay
on /dev/sdb1
using:
sudo pvdisplay /dev/sdb1
You'll see something like this:
This tells you how many physical extents /dev/sdb1
adds to the volume group ubuntu, and that all these physical extents are free and available for use in logical volumes (in the ubuntu group).
If you have free space in your volume group, you can easily extend the root logical volume.
In our example, we shall extend the root filesystem "/" which is part of a default Ubuntu 12.10 install. The same principles apply when extending any logical volume.
After the logical volume has been extended, you must also extend the size of the underlying filesystem (which in our case is ext4). As you'll see shortly, we can do all of this as part of the same command.
To extend a logical volume, you can use either the lvextend
command or the lvresize command. The lvextend
command is considered a safer option because it won't allow you to reduce the size of the logical volume should you make a mistake when entering the command.
We can use the --resizefs
option to automatically resize the underlying filesystem.
Purly for illustration purposes, we'll extend our 457.6GiB (491.34GB in hard disk terms) root logical volume (or another way of expressing the size is: 117146 logical extents).
Let's extend it by a further 20000 extents (which will extend it from 457.6GiB to 535.73GiB).
To do this and also resize the underlying ext4 filesystem, we'd use the command:
sudo lvextend --resizefs --extents +20000 /dev/ubuntu/root
The output of this command will look like this:
If you'd rather extend by a specific size specified in Megabytes, Gigabytes, Terabytes, Petabytes, or Exabytes, you could use M
,G
,T
,P
, or E
respectively (or the relevant letter in lower case). For example, the following command would extend by 20GiB (that is 20 Gigabytes in the sense of 20*1024*1024*1024 bytes, aka 20GiB).
sudo lvextend --resizefs --size +20G /dev/ubuntu/root
Furthermore, if you'd like to extend to a specific overall size, you would simply specify that size (no +
used), for example to extend to 550GiB, use:
sudo lvextend --resizefs --size 550G /dev/ubuntu/root
Also note that if you wish to use physical extents from a particular physical volume to fulfill the request (that is you want to specify the underlying disk used) you can do so for any of the above commands by appending one or more physical volume names.
So for example, we had 12 free physical extents in our ubuntu volume group before we added the /dev/sdb1
physical volume to the group. When we extended the root logical volume by 20000 PE's, that 12 (residing on /dev/sda5
) were used along with 19988 taken from /dev/sdb1
. Had we wanted that all 20000 PE's from /dev/sdb1
we could have used:
sudo lvextend --resizefs --extents +20000 /dev/ubuntu/root /dev/sdb1
For illustration purposes, the screenshot below shows how things look now for us (given that we didn't specify we wanted all 20000 physical extents from same physical volume).
If you're curious, take a look at the volumes under Physical View and Logical Views in the system-config-lvm
tool to see exactly how things look visually.
When you create a new logical volume, you create it in a volume group. That volume group must have sufficient free space (physical extents) to create your logical volume.
Note that when choosing the name of your logical volume, it is best not to use a -
(that is dash or hyphen) in the name if you plan on using the logical volume with grub2
. So for example, grub2
would have issues with finding my-lv
but not with my_lv
.
In our example here (which has nothing to do with grub2
), we'll create a logical volume for our music called music_lv
in the ubuntu volume group.
We can see how much free space the volume group (called ubuntu) has by running the command:
sudo vgdisplay ubuntu
In our case, we see this in the output:
Free PE / Size 39630 / 154.80 GiB
Lets create a logical volume called music_lv
of size 30000 PE (physical extents).
In order to do this, we need to use the lvcreate
command like so:
sudo lvcreate --extents 30000 -n music_lv ubuntu
As before, you can specify Megabytes, Gigabytes, Terabytes, Petabytes, or Exabytes using M
, G
, T
, P
, or E
respectively along with the --size
option.
So we could have created a 100GiB volume like this (if we wanted that size):
sudo lvcreate --size 100G -n music_lv ubuntu
If the logical volume creation worked OK, you'll see the message:
Logical volume "music_lv" created
As things stand, we can't do much with music_lv
yet because we have no filesystem on it. Lets create an ext4 filesystem on our new logical volume using the mkfs.ext4
command:
sudo mkfs.ext4 /dev/ubuntu/music_lv
The output should look something like this:
And for illustration purposes, the screenshot below shows our new logical volume music_lv
is part of the LVM layout:
A look at the Logical View for our new music_lv
volume shows the following:
If you'd like Music in your home directory to use (mount to) music_lv
logical volume, you could do the following (swap your user name for tutonics):
sudo mount /dev/ubuntu/music_lv /home/tutonics/Music/
Then sort out the permissions:
sudo chown tutonics /home/tutonics/Music
sudo chgrp tutonics /home/tutonics/Music
Notice in the above screenshot that the text to the right says Mount point when rebooted: None. If you'd like this to be mounted when you boot your box, you should add an entry to /etc/fstab
.
To get an idea as to what you could use as this /etc/fstab
entry run:
cat /etc/mtab | grep music_lv
When you want to reduce the size of a logical volume, you cannot be mounted on that volume. Hence, if you wish to reduce the size of your root
logical volume, you'll need to do it with a live disk.
In this example, we'll reduce the size of the music_lv
that we just created above. Lets reduce it by 10000 PE's (Currently it consists of 30000, so after shrinking/reducing the logical volume, it will consist of 20000 physical extents). Ensure the filesystem on the music_lv
is not mounted.
It had been mounted on /media/tutonics/6221ff7b-1aae-490b-adb2-54c7919d3286
so we can unmount it using the command:
sudo umount /media/tutonics/6221ff7b-1aae-490b-adb2-54c7919d3286
Now for a couple of golden rules to follow when reducing the size of a logical volume:
- Always make sure you have removed enough data from the filesystem on that logical volume to allow for the size you want to reduce by, otherwise your data could be destroyed.
- Always shrink the size of the filesystem before you shrink the logical volume, otherwise your data will be lost. (to be safe, we'll ask for both to be done as part of the same command).
OK, to carry out the reduction, use the lvreduce
(or lvresize
) command along with the --resizefs
option as shown below:
sudo lvreduce --resizefs --extents -10000 /dev/ubuntu/music_lv
Here is a screenshot of the output from this command:
As usual, if you want you can specify to reduce by a certain number of Gigabytes by using the --size
option instead, for example to reduce by 20 GiB, use:
sudo lvreduce --resizefs --size -20G /dev/ubuntu/music_lv
or to reduce to a specific size, say 100GiB, use:
sudo lvreduce --resizefs --size 100G /dev/ubuntu/music_lv
In our case because we reduced by 10000 PE's (from 30K to 20K), our music_lv
logical volume now looks like this:
What's Next?
- In the next part of our LVM guide, we'll show you how to create and use Logical Volume Management Snapshots.
- Thanks to the programmers of LVM2 and also the authors of the LVM-HOWTO