Walker News

How To Create Volume Group And File System With LVM2 In Linux?

LVM, shorts for Logical Volume Management, is available in most modern Linux distributions. If I recall correctly, the disk layout for file systems of Red Hat Enterprise Linux 4 default installation is based on Logical Volume Management.

As the name implies, LVM is a software technique to consolidate a group multiple block devices (hard disk or disk partition) and present them to OS as a single disk volume. In other words, system administrator can build or create a 500GB file system with 2 units of 250GB hard disk.

Another advantage of using LVM implementation is flexibility to perform on-the-fly resize for the file system that built on this virtual disk partition, by adding in more physical hard disks and disk partitions.

A visualized concept diagram of the Linux Logical Volume Manager or LVM.
A visualized concept diagram of the Linux Logical Volume Manager or LVM

How to create a new volume group (VG) and file system on Logical Volume (LV)?

Base on RHEL4 and LVM2 as the platform, here is the summary of steps to build a new volume group (VG), create a Logical Volume (LV), followed by making new file system on it.
As with most Linux commands, the LVM and file system related commands come with number of option switches to extend or overwrite the command default behaviour.

Obviously, this general guide doesn’t cover detail of all option switches. For your own good sake, just vet through the command manual page to see if any of those option switches applicable to your environment.

1) Use lvmdiskscan to find out how many block devices (hard disks or disk partitions) seen by OS are currently used for LVM:

Using lvmdiskscan to check the number of hard disks detected by OS.

2) With reference to lvmdiskscan output, the /dev/sda2 disk partition is not currently used for LVM. However, it might be used by Red Hat.

So, you’ve to verify or confirm which of the non-LVM block devices are not currently used by Red Hat with the df command. For example, this following df -h output clearly indicates that /dev/sda2 and /dev/sda3 are actually mounted as root and boot file system respectively:

Using df -h command to check Linux file system size and free disk space.

3) Now, let say I decides to create the new Volume Group (VG) with two unused SCSI hard disks, /dev/sdh and /dev/sdi. So, let’s use the pvcreate command to initialize these two SCSI hard disks for use by LVM:
pvcreate /dev/sdh /dev/sdi

The pvcreate command creates the VGDA, shorts of Volume Group Descriptor Area, for LVM to keep track Physical Extent (PE) allocation history, among other things. The initialized hard disk is known as Physical Volume (PV).
The manual page suggests setting a DOS disk partition to Linux LVM partition type (0x8e) prior to execute pvcreate command. But, why not to keep a standard by explicitly creating or changing non-LVM partition to LVM partition type?

1) To change existing, non-LVM disk partition to LVM partition type (0x8e) by using fdisk command:

Using Linux fdisk command to change a partition to LVM partition type.

2) To use the entire hard disk for LVM, simply proceed with fdisk to create one primary partition to take up whole hard disk space and then set the partition type to 0x8e.

The pvcreate manual page, however, suggest a faster approach by destroying the existing partition table, with this dd command:
dd if=/dev/zero of=/dev/sdi bs=512 count=1

4) Now, use vgcreate command to create a new Volume Group called vg7, using the block devices (/dev/sdh and /dev/sdi) initialized by pvcreate command in previous step (you may run pvdisplay to see difference of before and after executing vgcreate command):
vgcreate -s 16M vg7 /dev/sdh /dev/sdi

A Volume Group can be thought of a container that house all the PV specified by vgcreate or vgextend command. Conceptually, you should regard Volume Group as a virtual hard disk, as you can see later that it’s possible to “partition” the Volume Group into Logical Volume (LV).

The -s option switches specify the physical extent size (PE size) of the volume group. If this is not specifying, the default PE size is 4MB, which is best for most applications.
The maximum number of PE per Logical Volume (LV) is not applicable in LVM2. However, high number of PE could affect LVM tool performance.

As such, set a bigger PE size could effectively reducing number of PE per VG and LV (if the VG is big enough).

Take note that the name of VG must not conflict with existing VG. To list existing VG in system, execute vgscan

5) Next, we proceed to partition Volume Group into Logical Volume. A file system can only be built on Logical Volume (LV) but not Volume Group (VG). Therefore, at least one LV must be created per VG. For example, this lvcreate command create a 400MB Logical Volume (virtual disk partition) called lvol3 in the Volume Group called vg7:
lvcreate -L 400M -n lvol3 vg7

Similar to VG, the name of LV must not crash with the existing LV in the same VG. Again, this can be verified by executing lvscan
To use all free Physical Extent in Volume Group for Logical Volume, you can use -l option switch to specify the number of PE to create the LV. The number of free PE in Volume Group vg7 can be checked by executing vgdisplay vg7

6) Now, with the new Logical Volume (lvol3), we can create file system on this virtual disk partition. For example, this mkfs command create the Linux EXT3 file system on Logical Volume /dev/vg7/lvol3, with -m 2 option switch to specify 2% of the file system blocks reserved for super-user.:
mkfs -t ext3 -m 2 -v /dev/vg7/lvol3

The full path of logical volume can be checked by executing lvscan command.

7) Once the mkfs successfully creates EXT3 file system, it’s ready to mount the new file system. First, create a directory as mount point, let say this:
mkdir /NewVGMnt

8) Then execute the mount command to mount the new file system to /NewVGMnt:
mount -t ext3 /dev/vg7/lvol3 /mnt/NewVGMnt

To verify the mount point, just type df -h command to confirm it’s done successfully. To enable Linux automatically mount the new file system during boot-up, include this new file system to /etc/fstab system file.

Custom Search

  1. egorgry 11-03-09@04:48

    Good straight forward article. I’m amazed how many admins don’t use the -m switch with mkfs.

  2. johnny 07-07-09@04:30

    Excellent. Exactly what I was looking for. Thanks!

  3. Khalifa 07-10-09@01:43

    Pros work, i like that way …. keep it up and try to put an illustrated installation process

  4. ElMehdi 19-04-10@04:25

    Hello,
    Thank you for this article : I was always afraid of using LVM but your article showed it was easy.
    thank you again for the good work.
    cheers

  5. Mohammed ali 18-06-10@22:38

    Hello,
    Thank you for this article : I was always afraid of using LVM but your article showed it was easy.
    thank you again for the good work.
    cheers

  6. gcmartin 12-10-10@03:09

    Hi Walker….Great “2” articles (I read thru both on LVM)

    I have a special case that I would like to ask you to comment on.

    I have a single VG that has 3 LVs contained in a single PV. The HDD is 500GB that is partitioned into 2 partitions; /dev/sda2 is an LVM. 512MB is /dev/sda1 and the VG takes the remainder of the drive (sda2).

    I want to free up some space outside of the LVM so that I have 24GB available at the end of the HDD. To make room, I want to reduce the size of one of the LV and reduce the size of the partition respectively. And, because there are 3 LVs, I am unsure how to proceed.

    Here’s my scans that my help understand my plight and may help answer the correct sequence of steps I should follow to accomplish a reduction in the VG and HDD.
    Thanks in advance for any help.

    #fdisk -l
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x00000000
    
       Device Boot  Start     End      Blocks   Id  System
    /dev/sda1   *       1      66      524288   83  Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2          66   60801   487859712   8e  Linux LVM
    
    # pvscan
    PV /dev/sda2   VG pve   lvm2 [465.26 GB / 4.00 GB free]
    Total: 1 [465.26 GB] / in use: 1 [465.26 GB] / in no VG: 0 [0   ]
    # vgscan
      Reading all physical volumes.  This may take a while...
      Found volume group "pve" using metadata type lvm2
    # lvscan
      inactive          '/dev/pve/swap' [4.00 GB] inherit
      inactive          '/dev/pve/root' [96.00 GB] inherit
      inactive          '/dev/pve/data' [361.26 GB] inherit
    

    I can get you any additional displays if need.

  7. Walker 12-10-10@23:01

    I am not comfortable to give a solution until I tested it and confirm it works :-(

  8. Cam 03-02-14@21:38

    Thanks so much. This was such a brilliant and simple explanation of all the steps from creating a VG to mounting the filesystems.

    Only bit I found didnt work was the last command:

    mount -t ext3 /dev/vg7/lvol3 /mnt/NewVGMnt

    so I did:

    mount -t ext3 /dev/vg7/lvol3 /NewVGMnt

    and it was fine.

    Thanks again.

2017  •  Privacy Policy