How to Create LVM Volume Group and Logical Volume

Create LVM Volume Group and Logical Volume
Logical Volume Manager (LVM) is a device mapper target that provides logical volume management for the Linux kernel. LVM allows to create Logical Volumes from underlying Physical Hard Disks (Physiacal Volumes). Logical Volumes can be easily extended and shrunk according to the file system capacity demands.

LVM Objects:
Physical Volume (PV): underlying physical storage for LVM. These are most often physical hard disks, but also can be: partitions, RAID volumes, etc…
Volume Group (VG): storage pool, created from one or more Physical Volumes.
Physical Extent: small chunk of Physical Volume, used during Volume Group creation.
Logical Extent: mapped to Physical Extent, acting as front-end chunk for Logical Volumes.
Logical Volume: group of Logical Extents, used by the system as hard drive partition.

LVM Volume Group creation and Logical Volume creation is pretty simple:

1. Prepare hard disks for use with LVM
In this tutorial we will use two hard disks to create Physical Volumes for LVM: /dev/vdb, /dev/vdc.

Verify hard disks:

[root@tuxfixer ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@tuxfixer ~]# fdisk -l /dev/vdc

Disk /dev/vdc: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Create partition /dev/vdb1 on /dev/vdb device and set it’s file system type to: Linux LVM (hex code: 8e):

[root@wordpress ~]# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x3d359b8e.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): 4194303

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/vdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x3d359b8e

Device     Boot Start     End Sectors Size Id Type
/dev/vdb1        2048 4194303 4192256   2G 8e Linux LVM


Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Create partition /dev/vdc1 on /dev/vdc device and set it’s file system type to: Linux LVM (hex code: 8e):

[root@tuxfixer ~]# fdisk /dev/vdc

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x7537f08d.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-6291455, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-6291455, default 6291455): 6291455

Created a new partition 1 of type 'Linux' and of size 3 GiB.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/vdc: 3 GiB, 3221225472 bytes, 6291456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7537f08d

Device     Boot Start     End Sectors Size Id Type
/dev/vdc1        2048 6291455 6289408   3G 8e Linux LVM


Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2. Create Physical Volumes
Create Physical Volumes from newly created partitions: /dev/vdb1, /dev/vdc1:

[root@tuxfixer ~]# pvcreate /dev/vdb1 /dev/vdc1
  Physical volume "/dev/vdb1" successfully created
  Physical volume "/dev/vdc1" successfully created

Verify Physical Volumes:

[root@tuxfixer ~]# pvdisplay /dev/vdb1
  "/dev/vdb1" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fdQ5Eq-KtkI-Wstd-pVvG-84jD-nJQw-rxDu4l
   
[root@tuxfixer ~]# pvdisplay /dev/vdc1
  "/dev/vdc1" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdc1
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               lf8Vwo-7Cz1-hAvD-IlYr-e7JF-jIJX-RN2A0X

3. Create Volume Group
Let’s create Volume Group named VolGroup1 based on created Physical Volumes: /dev/vdb1, /dev/vdc1 using 2MiB sized chunks:

[root@tuxfixer ~]# vgcreate -s 2M VolGroup1 /dev/vdb1 /dev/vdc1
  Volume group "VolGroup1" successfully created

Note: you can omit chunk size parameter (-s), then the Volume Group will be created using default chunk size of 4MiB

Verify newly created Volume Group:

[root@tuxfixer ~]# vgdisplay VolGroup1
  --- Volume group ---
  VG Name               VolGroup1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.00 GiB
  PE Size               2.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 5.00 GiB
  VG UUID               MrFAHS-Un0z-bum3-k2Xg-uvPn-9Riw-w6ftol

Note: pay attention to VG Size: 5GiB, it’s the sum of two Physical Volumes: /dev/vdb1, /dev/vdc1. Allocated Physical Extents equals zero (Alloc PE / Size: 0 / 0) and Free Physical Extents equals 5 GiB (Free PE / Size: 2558 / 5.00 GiB) because we haven’t created Logical Volumes yet and the Volume Group is empty.

4. Create Logical Volume
Create new 1GiB Logical Volume named LogVol1 in existing Volume Group VolGroup1:

[root@tuxfixer ~]# lvcreate -n LogVol1 -L 1G VolGroup1
  Logical volume "LogVol1" created.

Verify Logical Volume:

[root@tuxfixer ~]# lvdisplay /dev/VolGroup1/LogVol1
  --- Logical volume ---
  LV Path                /dev/VolGroup1/LogVol1
  LV Name                LogVol1
  VG Name                VolGroup1
  LV UUID                w2M2x3-swgH-wku6-vsdM-DCIr-c2hw-82QvW0
  LV Write Access        read/write
  LV Creation host, time wordpress, 2015-11-15 22:52:31 +0100
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

5. Create file system on Logical Volume
Now we can format LogVol1 in order to be usable in the system, let’s use ext4 file system and label extra_volume:

[root@tuxfixer ~]# mkfs -t ext4 -L extra_volume /dev/VolGroup1/LogVol1
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: a375d652-eab4-4fa8-9efe-3113eca621fa
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Verify newly created file system on LogVol1:

[root@tuxfixer ~]# blkid
/dev/vda1: UUID="1164d8f4-ae84-4451-be0f-210a6173989a" TYPE="ext4" PARTUUID="7583be5c-01"
/dev/vda2: UUID="3khed5-0vu0-66fV-VGA4-EC6G-eY0U-5HoThh" TYPE="LVM2_member" PARTUUID="7583be5c-02"
/dev/mapper/fedora-root: UUID="d2ca7304-551e-497d-8401-b54f72f11005" TYPE="ext4"
/dev/mapper/fedora-swap: UUID="c41b6f17-ece1-403e-b6d8-66b270db4e12" TYPE="swap"
/dev/vdb1: UUID="fdQ5Eq-KtkI-Wstd-pVvG-84jD-nJQw-rxDu4l" TYPE="LVM2_member" PARTUUID="3d359b8e-01"
/dev/vdc1: UUID="lf8Vwo-7Cz1-hAvD-IlYr-e7JF-jIJX-RN2A0X" TYPE="LVM2_member" PARTUUID="7537f08d-01"
/dev/mapper/VolGroup1-LogVol1: LABEL="extra_volume" UUID="a375d652-eab4-4fa8-9efe-3113eca621fa" TYPE="ext4"

Note: our new LogVol1 is mapped by the system as device: /dev/mapper/VolGroup1-LogVol1

6. Create mount point and mount Logical Volume
Let’s create mount point for /dev/mapper/VolGroup1-LogVol1 in fstab, for example: /mnt. Edit /etc/fstab and add an entry at the bottom of the file:

[root@tuxfixer ~]# vim /etc/fstab
/dev/mapper/fedora-root                    /      ext4  defaults 1 1
UUID=1164d8f4-ae84-4451-be0f-210a6173989a  /boot  ext4  defaults 1 2
/dev/mapper/fedora-swap                    swap   swap  defaults 0 0
/dev/mapper/VolGroup1-LogVol1              /mnt   ext4  defaults 1 2 

Run mount -a to mount all the file systems in fstab, including /mnt on LogVol1:

[root@tuxfixer ~]# mount -a

Verify mount point:

[root@tuxfixer ~]# df -hT
Filesystem                    Type      Size  Used Avail Use% Mounted on
devtmpfs                      devtmpfs  999M     0  999M   0% /dev
tmpfs                         tmpfs    1008M   72K 1008M   1% /dev/shm
tmpfs                         tmpfs    1008M  816K 1008M   1% /run
tmpfs                         tmpfs    1008M     0 1008M   0% /sys/fs/cgroup
/dev/mapper/fedora-root       ext4       18G  3.3G   13G  21% /
tmpfs                         tmpfs    1008M   12K 1008M   1% /tmp
/dev/vda1                     ext4      477M  122M  326M  28% /boot
tmpfs                         tmpfs     202M  8.0K  202M   1% /run/user/1000
/dev/mapper/VolGroup1-LogVol1 ext4      976M  1.3M  908M   1% /mnt

That’s it, we have just created Volume Group and Logical Volume 🙂

7. Additional Logical Volumes and Volume Groups
After creating 1GiB LogVol1 on 5GiB VolGroup1 we still have about 4GiB of free space in VolGroup1 to use:

[root@tuxfixer ~]# vgdisplay VolGroup1
  --- Volume group ---
  VG Name               VolGroup1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.00 GiB
  PE Size               2.00 MiB
  Total PE              2558
  Alloc PE / Size       512 / 1.00 GiB
  Free  PE / Size       2046 / 4.00 GiB
  VG UUID               MrFAHS-Un0z-bum3-k2Xg-uvPn-9Riw-w6ftol

We can use this free space to create more Logical Volumes (LogVol2, LogVol3, etc…) within VolGroup1 or even create new Volume Groups (VolGroup2, VolGroup3, etc…) using the same commands as we have used above. Free space can also be used to extend existing LogVol1.

Share on: Share on FacebookShare on Google+Tweet about this on TwitterShare on StumbleUponShare on LinkedInPin on PinterestBuffer this pageShare on TumblrDigg thisFlattr the authorShare on RedditShare on VKShare on Yummly
Liked it? Take a second to support tuxfixer.com on Patreon!
Hadoop Developer Training

Leave a Reply

Name *
Email *
Website