Creating a Physical Volume
To create a new physical volume from free hard drive space or a hard drive partition, use the pvcreate command:
Replace <disk> with the device name of the hard drive:
or the partition name:
The <disk> specified can also be a meta device or loopback device, but using an entire hard disk or partition is more common. After creating a physical volume, you can either add it to an existing volume group or create a new volume group with the physical volume.
Creating and Modifying Volume Groups
A volume group can be created from one or more physical volumes. To scan the system for all physical volumes, use the pvscan command as root. It displays all PVs on the system. If the PV is part of a VG, it will display the name of the VG next to it.
To create a VG, execute the vgcreate command as root, where <vgname> is a unique name for the volume group and <pvlist> is one or more physical volumes to use, each separated by a space:
vgcreate <vgname> <pvlist>
For example, to create a VG with the name DatabaseVG from the first and second SCSI hard drives:
vgcreate DatabaseVG /dev/sda /dev/sdb
If a volume group already exists but needs to be expanded, use the vgextend command to add additional physical volumes to it:
vgextend <vgname> <pvlist>
To remove a physical volume from a volume group:
vgreduce <vgname> <pvlist>
Use caution when reducing a volume group because any logical volume using the PVs are removed from the VG and can no longer be accessed.
Creating and Modifying Logical Volumes
Now that the physical volumes are formed into volume groups, the volume groups can be divided into logical volumes, and the logical volumes can be formatted with a filesystem and assigned mount points.
Use the lvcreate command to create a logical volume. Each LV must have a unique name. If one is not specified with the -n <name> option, a name will be assigned to it. To create a logical volume from the volume group <vgname> of a certain size, specify the size unit after the value of the size such as 300G for 300 gigabytes:
lvcreate -n <lvname> --size <size> <vgname>
Each physical volume consists of physical extents, which are 4 megabytes in size by default. When the size is given in gigabytes, this size must be converted to physical extents, meaning that some amount of disk space may not be used. So, the number of physical extents to use when creating the logical volume can be given with the –l <numpe> option:
lvcreate -n <lvname> -l <numpe> <vgname>
To determine the number of physical extents in a logical volume group, issue the following command as root:
The Total PE line shows the number of physical extents for the volume group. The output should look similar, which shows a total of 1189 physical extents. Look for the Free PE / Size line to determine whether any free PEs are available to allocate to a new logical volume. shows 220 free physical extents.
By default, logical volumes are created linearly over the physical volumes. However, they can be striped over multiple PVs:
lvcreate -i<stripes> -I<stripesize> -l <numpe> -n <lvname> <vgname> <pvlist>
The -i<stripes> option sets the number of stripes, or physical volumes to use. The -I<stripesize> is the stripe size, which must be 2^n, where n is an integer from 2 to 9. Provide the number of PEs to use with the -l <numpe> option or give the size of the LV with the --size <size> option. The -n <lvname> option specifies the LV name, and <vgname> represents the name of the VG to use. Optionally, list the PVs to use, <pvlist>, at the end of the command separated by spaces. The number of PVs listed should be equal to the number of stripes.
After creating the logical volume, you must create a filesystem on it. To create an ext3 filesystem, execute the following as root:
mke2fs -j /dev/<vgname>/<lvname>
If the LV is to be used as swap, execute the following as root instead:
Next, still as the root user, create an empty directory as its mount point with the mkdir command, and use the mount command to mount the filesystem:
mount /dev/<vgname>/<lvname> /mount/point
If it mounts properly, the last step is to add it to /etc/fstab so it is mounted automatically at boot time. As root, add a line similar to the following, replacing with the appropriate values:
/dev/<vgname>/<lvname> /mount/point ext3 defaults 1 2
To extend a logical volume, expand the volume group if necessary, and then use the lvextend command. Either specify the final size of the logical volume:
lvextend --size <size> /dev/<vgname>/<lvname>
or specify how much to expand the logical volume:
lvextend --size +<addsize> /dev/<vgname>/<lvname>
Just like physical volumes are composed of 4KB physical extents, logical volumes consist of logical extents, which also have a default size of 4KB. Instead of specifying the size or amount of space to add in gigabytes, it is also possible to use the -l <numle> to provide the final number of logical extents or -l +<numle> to expand the logical volume by a certain number of logical extents.
After extending the logical volume, the filesystem on it must be expanded as well. If it is an ext3 filesystem (default filesystem for Red Hat Enterprise Linux), it can be expanded while it is still mountedTo do so, execute the following as root:
The filesystem is expanded to fill the entire logical volume unless a size is listed after the logical volume device name (be sure to list the size unit such as G for gigabyte after the size):
resize2fs /dev/<vgname>/<lvname> <size>
To remove a logical volume from a volume group, first unmount it with the umount command:
and then use the lvremove command:
To view the existing logical volumes along with information about them such as what VG they are a member of, the number of logical extents, and their size in gigabytes, execute the lvdisplay command as root as shown in.