Backtrack:  
 
by lunarg on July 29th 2007, at 15:18

Theoretic stuff

An LVM structure is build as follows:

  1. At the bottom is the PV (Physical Volume), which is basically just a partition (logical or not). LVM markers need to be placed on it for LVM to see it as a usable PV.
  2. Before actual volumes can be created, a VG (Volume Group) has to be created. A group is the second lowest structure. Only one VGs can be created on per PV, but a single VG can span multiple PVs, which makes VG a very neat thing.
  3. The final step (before the filesystem) is the LV (Logical Volume). This is the thing that will actual hold the filesystem and data. So when mkfs-ing, it will be done on this. Several LVs can occupy one VG, but unlike VG, an LV can not span VGs (so if you were to merge PVs, you need to do it at the VG level, and not at the LV level).

Preparation

To use LVM, you need to install the necessary Debian package. If it's a fresh install, and you designated a partition for use as lvm, this is done automatically. If not, do the following:


apt-get install lvm2

This will provide all the necessary startup scripts and tools to use and maintain LVM volumes.

Creating PVs, VGs and LVs

Using the Debian partitioner is the quickest way when doing a fresh install. However, you lose quite a bit of control over the creating process. Whenever possible, the manual way should be preferred, as it harvests some extra configuration options over the Debian partitioner:

  • VGs can span PVs.
  • Storage assigning through extents, rather than size (which is more accurate).
  • Creating snapshots.
  • Probably some other things I forgot to mention.

Using the Debian partitioner

In the Debian partitioner, designate a partition as LVM. The necessary tools will be installed automatically. After that, choose to configure LVM. The current partition layout will be applied (so do this as last), and the LVM menu will be shown.

First up, you have to create a VG. For naming convention, name the VG store (although it can be anything you want).

Next, you can create one or more LVs in the created VGs. The big one is usually named data or data1, but can of course be called anything.

When done, return to the partitioner. You'll notice there are now additional partitions available. Designate a file system and mount point like you normally would.

The manual way

Physical partitioning

Before you can use a partition as a PV, you need to give it the proper partition type ID. This is done with fdisk:

  1. Fire up fdisk: fdisk /dev/sda
  2. Enter t, then select the partition.
  3. Enter 8e. This is the code for Linux LVM.
  4. Enter w to write the changes.

Creating a PV

Next up, we have to write LVM markers to the bare partition. This is called creating a PV, and is done with the pv toolset. To simply create one, simply punch in:

pvcreate /dev/sda6

This will write LVM markers to /dev/sda6, and allows the creation of VGs.
It is possible to set the size to something other than the entire partition, but this is not really recommended.

Creating a VG

Almost there. We have to create a VG before we can do anything. Like said before, it's possible for one VG to span multiple PVs.
Creating and maintaining VGs is done with the vg set.

Creating a simple VG

To create a simple VG:

vgcreate store /dev/sda6

This will create a VG called store on /dev/sda6.

VG spanning across several PVs

To have a VG span across more than one PV, do the following:

vgcreate store /dev/sda6 /dev/sdb6 /dev/sdc6

The VG store will now span across /dev/sda6, /dev/sdb6 and /dev/sdc6. If the partitions were of the same size, we would have tripled the total capacity of our LVM.

Additional options
  • To limit the amount of LVs per VG, use the -l option: vgcreate -l 2 store /dev/sda6 would limit the max amount of LVs to 2.
  • Since it's possible to add additional PVs to an existing VG, -p allows to limit the amount of PVs that a VG can span: vgcreate -p 2 store /dev/sda6 would limit spanning to 2 PVs.
  • FIXME there are other options

Creating a LV

Get VG information

Unlike VG, when creating an LV, you always have to specify the extents (size) the LV will have. Therefore, before creating the LV, we have to check the number of extents that is (left) available in our VG.

By default, a single extent is around 4MB.

vgdisplay store

This will display some information. The fields we need are:

  • Total PE: the total number of physical extents. If there's one PV and one VG, this would match the Total PE of the PV. If it's a span, it would match the sum of the Total PE of all the PVs.
  • Alloc PE / size : the amount of PEs that is currently in use by one or more LVs. Behind the / is the actual size.
  • Free PE / size: the amount of PEs that is still free. It's this one that we will need.
Create a simple LV

To create a simple LV, we do something like this:

lvcreate -n data1 -l 1024 store

This would create an LV named data1, stored in the VG store, with a PE of 1024.

Or, you could specify the size in a more human readable format:

lvcreate -n data2 -L 8G store

This would create an LV named data2, stored in VG store, and is 8GB in size.

Using the LV

When creating LVs, udev takes care of the device nodes.
There are two ways to approach an LVM:

  • Directly using the right dev nodes
    The LVs are directly approachable through /dev/mapper. In there, you'll find the control device node (used for LVM handling). and devices nodes of each LV in the form VG-LV.

    In our sample, it would be /dev/mapper/store-data1.
  • Using logical symlinks (the preferred way)
    The preferred way of using LVs is by its logical symlink. These symlinks are created by udev as well, and are links to the direct dev nodes (see a few lines upwards).
    They are created in the form of /dev/VG/LV.

    In our sample, this would become /dev/store/data1.

    Since this method is relatively more overseeable, this is the preferred choice.
Create a filesystem

Creating a file system works like any other partition:

mke2fs -j /dev/store/data1 would create a file system on LV data1 of VG store.

Note that if you're planning on resizing LVs in the future, you need to mind the file system you use: not all file systems can be resized!!! Read the About linux filesystems page for more information (scroll down to the LVM bit).

Modifying fstab

Adding to /etc/fstab is easy:

/dev/mapper/store-data1  /srv/data   reiserfs   defaults   0   2

This would automount the LV data1 of VG store to mount point /srv/data at boot. The file system we used is reiserfs.

Note that we use the direct dev node, because at boot time, it is unclear whether the symlinks are already present. This should be checked someday.

Maintaining PVs, VGs and LVs

Obtain information

Information of an LV

  • To obtain information of all LVs: lvdisplay
  • For all LVs of a particular VG: lvdisplay store
  • For a particular LV: lvdisplay store/data1

Information of a VG

  • To obtain information of all VGs: vgdisplay
  • For a particular VG: vgdisplay store

Information of a PV

  • To obtain information of all VGs: pvdisplay
  • For a particular PV: pvdisplay /dev/sda6

Removing volumes

Removing an LV

Removing an LV is easy:

lvremove store/data1

Removing a VG

Like an LV:

vgremove store

Note that you can only remove a VG when it doesn't contain any LVs.

Removing a PV

Likewise:

pvremove /dev/sda6

Note that removing a PV effectively destroys everything on that PV.

Resizing an LV

Guidelines

This is probably the most wanted part... There are a few steps you must follow in order to succesfully resize an LV:

To increase the LV
  1. First: grow the LV,
  2. Only then: grow the file system.
To decrease the LV
  1. First: shrink the file system,
  2. Only then: shrink the LV.

Resizing the file system

Resizing the file system depends on the file system you're using.

ext2/ext3

To resize an ext2 or ext3 partition, we use resize2fs.

If we were to increase the size from 4GB to 8GB, we would do something like this:

resize2fs /dev/store/data1 8G
reiserfs (reiser3)

To resize a reiser3 partition, there are two ways (just like resizing the LV itself):

  • Either specify the amount to add or decrease
  • Specify the new total space

To resize, use resize_reiserfs.

In our sample, we grow our file system from 4GB to 8GB.

resize_reiserfs -s +4G /dev/store/data1

Or, like so:

resize_reiserfs -s 8G /dev/store/data1

Note that you cannot resize online: first, you have to unmount the partition.

Resize the LV

Resizing the LV can be done in two ways. In our sample below, we want to resize the extents from 1024 (4GB) to 2048 (8GB).

Either, you simply add or deduct the additional extents or space, like so:

lvresize -l +1024 store/data1

or

lvresize -L +4G store/data1

If we needed to decrease space, we would simply change the + to a -.

Alternatively, you can specify the new total amount of extents or space, like so:

lvresize -l 2048 store/data1

or

lvresize -L 8G store/data1
 
 
« June 2021»
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
27282930   
 
Links
 
Quote
« I needed a password with eight characters so I picked Snow White and the Seven Dwarves. »