click me  

     Return to Linux Home Page.


Managing Physical & Logical Volumes

by Jeff Hunter, Sr. Database Administrator

Overview

The following article presents an overview of the commands used to manage physical and logical volumes for use with Logical Volume Manager (LVM) in Linux.

Before considering the various commands for LVM, lets first look at just what is meant by some of the terminology of LVM.

NOTE: Using Logical Volume Manager assumes that the disk or disks you will be using do not have a formatted partition. In Linux you would use the fdisk to remove the parition. KEEP IN MIND THAT THIS WILL REMOVE ALL DATA ON THAT DRIVE AND WILL NOT BE ABLE TO BE RECOVERED!!! As an example, lets remove the formatted partition on an external FireWire drive in Linux. Simply run the fdisk command and follow the screens to remove all partitions.

  # fdisk /dev/sda

A Logical Volume lives in a Volume Group that is made up of one or more Physical Volumes. All Volume Groups are part of the Logical Volume Manager. Here is a table that lists the three types of Logical Volume's.

Logical Volume Types
Logical Volume Physical Device File System
volume group one or more disks vgdisplay
physical volume group physical extents on a drive pvdisplay
logical volume group multiple physical volume groups, one or more disks lvdisplay

The following table provides an overview of some of the commands used in LVM and the functions they service.

LVM Commands
Command LVM Function
vgcreate Create a Volume Group. (Create a subset of the overall LVM.)
pvcreate Create a Physical Volume, assign to Volume Group. (Specify a disk for inclusion in the overall LVM.)
vgextend Add a new physical disk to a volume group.
lvcreate Create a Logical Volume. (storage area for related files that is part of a Volume Group. A Volume Group consists of many logical volumes.)
lvextend Increase the size of a Logical Volume.
lvreduce Decrease the size of a Logical Volume.
lvremove Removes a Logical Volume. (frees the storage area set aside for a logical volume.)
vgreduce Reduce a Volume Group. (Reduces the number of disks in a Volume Group.)
vgremove Remove a Volume Group. (Removes the designation of a group of disks as a Volume Group.)
vgdisplay Volume Group Display. (Displays information about one or more Volume Groups.)
pvdisplay Physical Volume Display. (Displays information about one or more Volume Groups.)
lvdisplay Logical Volume Display. (Displays information about one or more Logical Volumes.)

Initializing Disks or Disk Partitions
Before you can use a disk or disk partition as a physical volume, you will have to initialize it. There are two ways to handle initializing a disk - as an entire disk or as a partition.
Create Physical / Logical Volumes
First, use fdisk to remove any formatted partitions on the disk.

The following set of commands perform the three steps required to create logical volumes:

  1. Use pvcreate to create a Physical Volume for use by the Logical Volume Manager (LVM).
  2. Use vgcreate to create a Volume Group for the drive or for the partition you want to use for RAW devices. Here we do the entire single drive. In our example (below), the command will allow 256 logical partitions and 256 physical partitions with a 128K extent size.
  3. Use lvcreate to create the Logical Volume(s) inside the volume group.
pvcreate -d /dev/sda
vgcreate -l 256 -p 256 -s 128k /dev/pv1 /dev/sda
lvcreate -L 500m /dev/pv1
lvcreate -L 500m /dev/pv1
lvcreate -L 300m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 100m /dev/pv1
lvcreate -L 200m /dev/pv1
lvcreate -L 200m /dev/pv1
lvcreate -L 200m /dev/pv1
lvcreate -L 200m /dev/pv1
lvcreate -L 200m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 150m /dev/pv1
lvcreate -L 50m /dev/pv1
lvcreate -L 50m /dev/pv1
lvcreate -L 500m /dev/pv1
Click here to see the output from the above commands.
Now lets scan the LVM:
  # lvscan
  lvscan -- no volume groups found
View Physical / Logical Volumes
Delete Physical / Logical Volumes
Before going into the details of how to drop all of the physical and logical volumes created in this article, it is very common to only want to remove a Logical Volume from a Volume Group. Let's say we no longer needed lvol15. We can remove it and place its PE's back in the empty pool for the Volume Group. First, unmounting its filesystem (if it is mounted). Next, deactive it with lvchange and finally delete it with lvremove. Here is an example that removes the lvol15:
  # lvchange -a n /dev/pv1/lvol15

  # lvremove /dev/pv1/lvol15
  lvremove -- do you really want to remove "/dev/pv1/lvol15"? [y/n]: y
  lvremove -- doing automatic backup of volume group "pv1"
  lvremove -- logical volume "/dev/pv1/lvol15" successfully removed

Now, it you would like to delete all physical and logical volumes created in this article. In this example, we no longer need the /dev/pv1 Physical Volume.

lvremove -f /dev/pv1/lvol1
lvremove -f /dev/pv1/lvol2
lvremove -f /dev/pv1/lvol3
lvremove -f /dev/pv1/lvol4
lvremove -f /dev/pv1/lvol5
lvremove -f /dev/pv1/lvol6
lvremove -f /dev/pv1/lvol7
lvremove -f /dev/pv1/lvol8
lvremove -f /dev/pv1/lvol9
lvremove -f /dev/pv1/lvol10
lvremove -f /dev/pv1/lvol11
lvremove -f /dev/pv1/lvol12
lvremove -f /dev/pv1/lvol13
lvremove -f /dev/pv1/lvol14
lvremove -f /dev/pv1/lvol15
lvremove -f /dev/pv1/lvol16
lvremove -f /dev/pv1/lvol17
lvremove -f /dev/pv1/lvol18
lvremove -f /dev/pv1/lvol19
lvremove -f /dev/pv1/lvol20
lvremove -f /dev/pv1/lvol21
lvremove -f /dev/pv1/lvol22
lvremove -f /dev/pv1/lvol23
lvremove -f /dev/pv1/lvol24
lvremove -f /dev/pv1/lvol25
lvremove -f /dev/pv1/lvol26

vgchange -a n /dev/pv1

vgremove /dev/pv1
Click here to see the output from the above commands.



Last modified on: Tuesday, 26-Jul-2005 13:57:48 EDT
Page Count: 45007