Last week, I covered online extension of file systems using Logical Volume Manager (LVM). This week looks at how to create physical volumes, volume groups, and logical volumes.

The first step is to create the physical volumes. LVM on Linux is a little different to LVM on HP-UX in one aspect: LVM on Linux allows a whole disk or a partition on a disk to be used as a Physical Volume (PV). If partitions are being used, then the first step is to use the command fdisk to see what partitions exist on a disk, and to create a new LVM partition.
Figure A shows the operation of fdisk. The red circles show the inputs required. Stepping through the options for fdisk, the first option is n, which will add a new partition. The defaults for the partition number and sector sizes are sufficient for most cases. The next step is to select the type of partition; here, the type is 8e for Linux LVM. Once this is done, typing in p will print out the partition table. One final step is required, which is to write the new partition table to disk. The w option does this and exits on completion. The partition is now ready for use.

Figure A

The command pvcreate is used to prepare a disk partition or a whole disk for use by LVM. Figure B shows the command used and the output.

Figure B

One point to note on pvcreate. The command does check first to see if there is an existing LVM header on the PV. If there is, an error is output and the physical volume will not be created. Figure C shows the output.

Figure C

In this case, running pvdisplay on the PV will show which volume group the PV is part of (as shown in Figure D). It is prudent to note here the dangers of running pvcreate -ff. The -f option by itself will attempt to create a physical volume. If a volume group label is detected on the disk, then an error will be output and the physical volume will not be created, as shown in Figure C. If there is certainty that the volume is not being used in another volume group then -ff can be used, but the emphasis is that there must be 100% certainty.

Figure D

Having created the physical volumes the next step is to create the volume group. At least one physical volume is required to create a volume group. The command to use is vgcreate <volume group name> <device name>. Figure E shows the command string to use for volume group creation.

Figure E

The command vgdisplay can be used to verify the volume group has been created, as shown in Figure F.

Figure F

One area to be mindful of is the group of settings for Physical Extent (PE) size. The default PE size is 4MB. Generally, there is no need to deviate from this size unless it is specifically requested. The PE size is configurable, but once the volume group is created the PE size cannot be changed easily. It can be changed, but the way this is done is to remove and recreate the volume group. This may not be desirable, particularly if the volume group has a number of logical volumes that are in use.

Creation of the volume group leads into creation of logical volumes. The command lvcreate is used to create logical volumes. There are a few options that can be used. The -L option allows specification of the logical volume size in Megabytes, Terabytes, Petabytes or Exabytes (using the suffixes M, T, P or E respectively). The default is Megabytes. The -l option specifies physical extents. One useful option from a System Administration perspective is the -n option, which is used to specify a name for the logical volume. If this is not specified, the logical volume name defaults to the next default logical volume number.

There are other options that lvcreate has, such as -m (mirror), -i (stripes) and -I (stripe size). A full examination of these options will be provided in a future post. They are mentioned here for information.

Figure G shows the command string used for a simple lvcreate.

Figure G

Successful creation of a logical volume leads to the final step: creation of a file system. The main file systems (ext2, ext3, ext4 or reiserfs) can be created by the mkfs command with a -t option to specify the type. In this example, an ext4 file system is created.

Summing up, LVM can provide flexibility in managing physical volumes. This discussion has mainly been about how to create a volume group and logical volumes, but there is potential for other options to be used, such as mirroring or striping.