Operating systems

Working with physical volumes, logical volumes, and volume groups in LVM

Scott Reeves demonstrates how to work with LVM, creating physical volumes, volume groups, and logical volumes with examples of the command strings.

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.

About

Scott Reeves has worked for Hewlett Packard on HP-UX servers and SANs, and has worked in similar areas in the past at IBM. Currently he works as an independent IT consultant, specializing in Wi-Fi networks and SANs.

5 comments
veerukoks
veerukoks

Hi scott ,

                I am not able to create vg or lvm on a HA machine(i.e a server which has 2 physical machines in one),i have one 1 TB SCSI hard drive.The HA machine supports SAS drives,using converter  i have connected the 1 TB hardisk. 

While installing linux OS,i am not able create a LVM group and LVM.I get a error as choose a physical HD /RAID . Even in command mode there is no options to create LVM group.(i was installing RHEL 5.7).


I need to install around 14 OS in that hardisk.So i need a LVM and one extended partation.Can you help me please . 

Brainstorms
Brainstorms

If you get the message above when you try to create a PV with 'pvcreate' you don't (generally) need the '-ff' option. That's a sledgehammer, usually needed when you're in the unenviable position of having to string back together a busted up LVM group. (I've done this... it ain't pretty.) Run 'pvs' to get a quick list of PVs on your system. (And yes, 'vgs' and 'lvs' give you a quick list of VGs and LVs on your system. One helpful form of the last command is lvs -a -o +devices which will not only list your LVs, but it will show you what PVs each LV is using. VERY useful info when you're moving your PEs from disk to disk.) So if 'pvs' shows that your intended PV was previously a PV, you can enter pvremove /dev/sdXY to take it out, then put it back in again with 'pvcreate /dev/sdXY'. No need for sledgehammers. It's most handy to use LVM with RAID, in which case you'll be creating PVs from RAID md's. It all works the same, you just use the form pvcreate /dev/mdX vgextend db_arch /dev/mdX and you've added a RAID disk (typically a RAID-1 set) to your VG to create/extend LVs with. Aside from the redundancy, it makes your LVM more bulletproof and gives you more flexibility to do things such as migrate to other disks and even split a RAID-1 array to move your entire FS to a new machine (or location) -- while still using the existing system (which stays in place on a degraded RAID-1 set. (You can add new mirrors later.) Fun stuff... :^)

Brainstorms
Brainstorms

Often when you write the partition table back out, you'll get a notification that the kernel couldn't properly figure out what you just did to the disk, and so it will continue to use the old partition table -- and that you'd better not try to do anything to your disk until you've rebooted... That's a pain --obviously-- but often you can avoid this by entering partprobe when you get this message. It tells the system to re-probe the disk (as though it were in the process of booting up) and often --not always-- will get the kernel to clear its head and "get" your new partition configuration. That can save an undesirable reboot...

Brainstorms
Brainstorms

It would be mkfs -t ext4 /dev/db_arch/archive01 However, this will create a (default) 5% reserve ("for the superuser") -- it's meant to be emergency space for an OS to use if your logs, etc. fill up your OS partition. You generally don't need this for a data-only partition. (I still allow 1%; it saves 4% of my disk space and makes it available for my use.) You can control this overhead with the '-m' switch: mkfs -t ext4 -m 1 /dev/arch_db/archive01 will reserve only 1% instead of 5%. (Some use '-m 0' and allow no reserve.) I also follow this up with an 'fsck' on the created LV "just to be sure".

larrymarsden
larrymarsden

It appears the last fina,l but critical to use the volume, step is mentioned but the actual command line example is missing ?? Otherwise, an excellent step-by-step outline.