In this tutorial, I will teach you how to use LVM.

Some info from wikipedia:
rhrl LVM - Geek-KB.com

LVM is a logical volume manager for the Linux kernel; it manages disk drives and similar mass-storage devices. The term “volume” refers to a disk drive or partition thereof. It was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.

The installers for the Arch LinuxCrunchBangCentOSDebianFedoraGentooMandrivaMontaVista LinuxopenSUSEPardusRed Hat Enterprise LinuxSlackwareSLEDSLESLinux Mint, and Ubuntu distributions are LVM-aware and can install a bootable system with a root filesystem on a logical volume.


Logical Volume Manager (LVM);

LVM is a tool for logical volume management which is used to allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes. LVM physical volumes can be placed on other block devices which might span two or more disks. Since a physical volume cannot span over multiple drives, to span over more than one drive, create one or more physical volumes per drive. The volume groups can be divided into logical volumes, which are assigned mount points, such as /home and / and file system types, such as ext2 or ext3 or ext4. When “partitions” reach their full capacity, free space from the volume group can be added to the logical volume to increase the size of the partition. When a new hard drive is added to the system, it can be added to the volume group, and partitions that are logical volumes can be increased in size.
On the other hand, if a system is partitioned with the ext4 file system, the hard drive is divided into partitions of defined sizes. If a partition becomes full, it is not easy to expand the size of the partition. Even if the partition is moved to another hard drive, the original hard drive space has to be reallocated as a different partition or not used.
In this how-to tutorial let’s learn some basics of LVM commands.

Scenario:

In this example we will:

Create 3 partitions each of size 100MB.
Convert them into physical volumes.
Combine physical volumes into volume group.
Finally create a logical volume from the volume group.

This will be done in 9 steps:

Step 1: Create Partitions
Step 2: Create Physical Volumes.
Step 3: Create Volume Groups
Step 4: Create Logical Volume
Step 5: Format and Mount the logical volume
Step 6: Extend Volume Group Size
Step 7: Remove Logical Volume
Step 8: Remove Volume Group
Step 9: Remove Physical Volume

 

Step 1

Create Partitions

Use fdisk command to create and manage partions.
To view the existing partitions use following command


[root@geek-kb ~]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5ad41371

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 60802 487873536 8e Linux LVM

Disk /dev/sdb: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c3eb2

   Device Boot      Start         End      Blocks   Id  System

The above output shows us two physical hard disks. The /dev/sda contains a LVM, one more partition and no space to create additional partitions. And the second drive /dev/sdb contains no partitions yet. So let’s use the second one in this tutorial.

Now let’s create three partitions of each size 100MB using the fdisk command:


[root@geek-kb ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-15267, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-15267, default 15267): +100M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (102-15267, default 102):
Using default value 102
Last cylinder, +cylinders or +size{K,M,G} (102-15267, default 15267): +100M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (203-15267, default 203):
Using default value 203
Last cylinder, +cylinders or +size{K,M,G} (203-15267, default 15267): +100M

To check whether the partitions have been created use the parameter “p” and hit Enter:

Command (m for help): p

Disk /dev/sdb: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c3eb2

Device Boot Start End Blocks Id System
/dev/sdb1 1 101 103408 83 Linux
/dev/sdb2 102 202 103424 83 Linux
/dev/sdb3 203 303 103424 83 Linux

Save the newly created partitions:


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

Update the kernel to save the changes without restarting the system.

[root@geek-kb ~]# partprobe 
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.

Let’s display existing partitions again using the command fdisk -l :

[root@geek-kb ~]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5ad41371

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       60802   487873536   8e  Linux LVM

Disk /dev/sdb: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c3eb2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         101      103408   83  Linux
/dev/sdb2             102         202      103424   83  Linux
/dev/sdb3             203         303      103424   83  Linux

The above output shows three partitions have been created on the /dev/sdb disk. If fdisk -l doesn’t show the output reboot the server in order to apply changes.
Step 2:

Create Physical Volumes

Note: If you chose “minimal installaion” during the operating system installaion process, the commands pvcreate, lvcreate, vgcreate etc.. will not be included. In order to use these commands you will have to install the lvm2 package first.

[root@geek-kb ~]# yum install lvm2
Loaded plugins: rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install ProcessThe
Resolving Dependencies
--> Running transaction check
---> Package lvm2.i686 0:2.02.72-8.el6 set to be updated
--> Processing Dependency: lvm2-libs = 2.02.72-8.el6 for package: lvm2-2.02.72-8.el6.i686
--> Processing Dependency: libdevmapper-event.so.1.02(Base) for package: lvm2-2.02.72-8.el6.i686
--> Processing Dependency: libdevmapper-event.so.1.02 for package: lvm2-2.02.72-8.el6.i686
--> Running transaction check
---> Package device-mapper-event-libs.i686 0:1.02.53-8.el6 set to be updated
---> Package lvm2-libs.i686 0:2.02.72-8.el6 set to be updated
--> Processing Dependency: device-mapper-event >= 1.02.53-8.el6 for package: lvm2-libs-2.02.72-8.el6.i686
--> Running transaction check
---> Package device-mapper-event.i686 0:1.02.53-8.el6 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
 Package                      Arch     Version              Repository     Size
================================================================================
Installing:
 lvm2                         i686     2.02.72-8.el6        localrepo     514 k
Installing for dependencies:
 device-mapper-event          i686     1.02.53-8.el6        localrepo      79 k
 device-mapper-event-libs     i686     1.02.53-8.el6        localrepo      74 k
 lvm2-libs                    i686     2.02.72-8.el6        localrepo     565 k
Transaction Summary
================================================================================
Install       4 Package(s)
Upgrade       0 Package(s)
Total download size: 1.2 M
Installed size: 2.5 M
Is this ok [y/N]: y
Downloading Packages:
--------------------------------------------------------------------------------
Total                                            11 MB/s | 1.2 MB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : device-mapper-event-libs-1.02.53-8.el6.i686              1/4 
  Installing     : device-mapper-event-1.02.53-8.el6.i686                   2/4 
  Installing     : lvm2-libs-2.02.72-8.el6.i686                             3/4 
  Installing     : lvm2-2.02.72-8.el6.i686                                  4/4 
Installed:
  lvm2.i686 0:2.02.72-8.el6                                                     
Dependency Installed:
  device-mapper-event.i686 0:1.02.53-8.el6                                      
  device-mapper-event-libs.i686 0:1.02.53-8.el6                                 
  lvm2-libs.i686 0:2.02.72-8.el6                                                
Complete!

Now, let’s create physical volumes using the command pvcreate :

[root@geek-kb ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb3" successfully created

To verify the newly created physical volumes use the command pvdisplay:


[root@geek-kb ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "100.98 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               100.98 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               miDJNq-zx5T-cXjc-smCd-VEdT-jYuJ-vhOks1

  "/dev/sdb2" is a new physical volume of "101.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name
  PV Size               101.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               a7Z9eC-a4r9-ENR6-rZMO-F3of-l4b1-rIYwCj

  "/dev/sdb3" is a new physical volume of "101.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name
  PV Size               101.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9wQkmc-8TA8-WsRt-ykus-X2Nq-Q2b5-y0w0Bu

Step 3:

Create Volume Groups

Create a new volume group called vg1 using two physical volumes /dev/sdb1 and /dev/sdb2 using the command vgcreate:

[root@geek-kb ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
  Volume group "vg1" successfully created

In order to verify the volume group has been created successfully use the command vgdisplay:

[root@geek-kb ~]# vgdisplay
--- Volume group ---
VG Name vg1
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 196.00 MiB
PE Size 4.00 MiB
Total PE 49
Alloc PE / Size 0 / 0
Free PE / Size 49 / 196.00 MiB
VG UUID nSeBNt-dsDL-L6aT-Aiem-QpCk-Z01g-0yGn9d

Step 4:

Create Logical Volume

In order to create logical volume use the command lvcreate.
Let’s create a logical volume called lv1 with size 190MB:

[root@geek-kb ~]# lvcreate -L 190M vg1 -n lv1
  Rounding up size to full physical extent 192.00 MiB
  Logical volume "lv1" created

Verify that the Logical Volume has been created successfully, use the command lvdisplay:


[root@geek-kb ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                j2XM1M-27xq-ph7P-Prsn-Atjp-kLc9-AFanNT
  LV Write Access        read/write
  LV Creation host, time geek-kb, 2013-12-20 19:24:17 +0200
  LV Status              available
  # open                 0
  LV Size                192.00 MiB
  Current LE             48
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3

Step 5:

Format and Mount the logical volume

Now format the newly created logical volume and mount it in the /mnt directory or wherever you want:

[root@geek-kb ~]# mkfs.ext4  /dev/vg1/lv1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
49152 inodes, 196608 blocks
9830 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
24 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

And Mount the newly created logical volume:

[root@geek-kb ~]# mount -t ext4 /dev/vg1/lv1 /mnt/lv1

Display the mounted logical volume:

[root@geek-kb ~]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centolel-lv_root
53G 7.8G 45G 15% /
tmpfs 1.6G 152k 1.6G 1% /dev/shm
/dev/sda1 508M 69M 413M 15% /boot
/dev/mapper/vg_centolel-lv_home
436G 333M 414G 1% /home
/dev/mapper/vg1-lv1 195M 5.8M 180M 4% /mnt/lv1

Now the logical volume is successfully mounted in /mnt/lv1. You can use the new logical volume to store your data:

[root@geek-kb ~]# cd /mnt/lv1/
[root@geek-kb lv1]# mkdir 1 2 3
[root@geek-kb lv1]# touch file1 file2 file3
[root@geek-kb lv1]# ls
1  2  3  file1  file2  file3  lost+found

Step 6:

Extend Volume Group Size

If you’re running out of space in the logical volume, you can extend the size of it easily if your physical disk contains free space or with additional physical disks (Hard disk).
For example, let’s extend the volume group vg1 using the physical volume /dev/sdb3 that we create earlier  and let’s add additonal 100MB to logical volume lv1:

[root@geek-kb lv1]# vgextend vg1 /dev/sdb3
Volume group "vg1" successfully extended

Then, let’s resize logical volume lv1:

[root@geek-kb lv1]# lvresize -L +100M /dev/vg1/lv1
Extending logical volume lv1 to 292.00 MiB
Logical volume lv1 successfully resized

Next, resize the filesystem of logical volume lv1:


[root@geek-kb lv1]# resize2fs /dev/vg1/lv1
Filesystem at /dev/vg1/lv1 is mounted on /mnt/lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg1/lv1 to 299008 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 299008 blocks long.

Now, let’s verify the new size of logical volume lv1:

[root@geek-kb lv1]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID j2XM1M-27xq-ph7P-Prsn-Atjp-kLc9-AFanNT
LV Write Access read/write
LV Creation host, time geek-kb, 2013-12-20 19:24:17 +0200
LV Status available
# open 1
LV Size 292.00 MiB
Current LE 73
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3

Done. the new size of logical volume lv1 is extended by 100MB.
Step 7:

Remove Logical Volume

Unmount logical volume lv1 abd remove using the command lvremove :

[root@geek-kb lv1]# cd
[root@geek-kb ~]# umount /mnt/lv1
[root@geek-kb ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume lv1? [y/n]: y
Logical volume "lv1" successfully removed

Step 8:

Remove Volume Group:

[root@geek-kb ~]# vgremove vg1
Volume group "vg1" successfully removed

Step 9:

Remove Physical Volume:




[root@geek-kb ~]# pvremove /dev/sdb1 /dev/sdb2 /dev/sdb3
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped
Labels on physical volume "/dev/sdb3" successfully wiped

I hope you like this article, please feel free to leave comments or ask questions.

Comments

comments