Linux Basics-LVM (Logical Volume Manager) Tutorial

Logical volume manager (LVM)

LVM is a logical volume management tool used for disk allocation, logical volume striping, mirroring, and resizing. With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes. LVM physical volumes can be located on other block devices that span two or more disks. To span multiple drives, create one or more physical volumes for each drive, because physical volumes cannot span multiple drives. Volume groups can be divided into logical volumes with the following mount points: /home And / And file system types such as ext2, ext3, ext4. When a “partition” reaches its maximum capacity, you can increase the size of the partition by adding free space from the volume group to the logical volume. As new hard drives are added to the system, they can be added to volume groups, increasing the size of partitions, which are logical volumes.

On the other hand, if the system is partitioned with the ext4 file system, the hard drive will be divided into partitions of a defined size. Once the partition is full, expanding the size of the partition is not easy. Even if you move the partition to another hard drive, do not reallocate or use the space on the original hard drive as another partition.

In this how-to tutorial you will learn the basics of LVM commands.

scenario

In this example,

  1. Create three partitions, each 100MB in size.
  2. Convert them to physical volumes.
  3. Join physical volumes to volume groups.
  4. Finally, create a logical volume from the volume group.

Create a partition

Create and manage partitions using the fdisk command.

To view existing partitions, use the following command:

[[email protected] ~]# fdisk -l
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x000ac451
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         128     1024000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             128         291     1310720   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             291        1045     6052864   83  Linux

The output above shows two physical hard disks. / dev / sda contains 3 partitions and there is no space to create additional partitions. And the second drive / dev / sdb does not yet contain any partitions. So let’s use the second one in this tutorial.

Now use the fdisk command to create three partitions, each 100MB in size.

[[email protected] ~]# 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-1044, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-1044, default 15): 
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-1044, default 1044): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (29-1044, default 29): 
Using default value 29
Last cylinder, +cylinders or +size{K,M,G} (29-1044, default 1044): +100M

To check if a partition has been created, use the parameter “p”.

Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux

Save the newly created partition.

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 changes without restarting the system.

[[email protected] ~]# 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.

Check the existing partition again using the fdisk command.

[[email protected] ~]# fdisk -l
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x0007b12c
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x000ac451
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         128     1024000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             128         291     1310720   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             291        1045     6052864   83  Linux

The output above shows that three partitions were created on the / dev / sdb disk. Enabled if fdisk -l does not show a restart of the output.

Create a physical volume

Note: If you install the server in minimal mode, you will not find the commands “pvcreate”, “lvcreate”, “vgcreate”, etc. To use that command, first install the “lvm2” package.

[[email protected] ~]# 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 create the physical volume using the command pvcreate.

[[email protected] ~]# 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 check the newly created physical volume, use the command pvdisplay.

[[email protected] ~]# pvdisplay 
  "/dev/sdb1" is a new physical volume of "109.79 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               109.79 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               jQl5F4-DyLj-SkHu-4lhZ-J3nQ-zax9-aT8sc4
   
  "/dev/sdb2" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               i4MHvw-8hYB-Fwz8-fxTL-G3mu-fl5E-zGYhDO
   
  "/dev/sdb3" is a new physical volume of "109.82 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               109.82 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               99qkNw-3oAw-vXwg-WE6U-zyKO-Ffs3-rDSqUY

Create a volume group

Use the command vgcreate to create a new volume group called vg1 using the two physical volumes / dev / sdb1 and / dev / sdb2.

[[email protected] ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 
  Volume group "vg1" successfully created

Use the vgdisplay command to check whether the volume group has been created.

[[email protected] ~]# 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               216.00 MiB
  PE Size               4.00 MiB
  Total PE              54
  Alloc PE / Size       0 / 0   
  Free  PE / Size       54 / 216.00 MiB
  VG UUID               ds3OtP-DMUx-33nN-HDar-eqNj-uIED-41gjqI

Create a logical volume

To create a logical volume, use the command lvcreate. Create a logical volume called lv1 with a size of 200MB.

[[email protected] ~]# lvcreate -L 200M vg1 -n lv1
  Logical volume "lv1" created

Use the command lvdisplay to check whether the logical volume has been created.

[[email protected] ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/vg1/lv1
  VG Name                vg1
  LV UUID                dgLZ79-JZdn-NUSF-fUS1-YVFk-36qs-iuafhE
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                200.00 MiB
  Current LE             50
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Format and mount a logical volume

Next, format the newly created logical volume and mount it in the / mnt directory or anywhere.

[[email protected] ~]# 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
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 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 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Then mount the logical volume on the / mnt mount point.

[[email protected] ~]# mount /dev/vg1/lv1 /mnt/

You have successfully mounted the logical volume at / mnt. You can use the new logical volume to store your data.

[[email protected] ~]# cd /mnt/
[[email protected] mnt]# touch file1 file2 file3
[[email protected] mnt]# mkdir dir1 dir2 dir3
[[email protected] mnt]# ls
dir1  dir2  dir3  file1  file2  file3  lost+found

Increase volume group size

If the logical volume is running out of space, if the physical disk has free space or if there is an additional physical disk (hard disk), you can easily expand its size.
For example, suppose you extend volume group vg1 using physical volume / dev / sdb3. In addition, add 100MB to logical volume lv1.

[[email protected] mnt]# vgextend vg1 /dev/sdb3 
  Volume group "vg1" successfully extended

Next, change the size of the logical volume lv1.

[[email protected] mnt]# lvresize -L +100M /dev/vg1/lv1 
  Extending logical volume lv1 to 300.00 MiB
  Logical volume lv1 successfully resized

Change the size of the file system of the logical volume lv1.

[[email protected] mnt]# resize2fs /dev/vg1/lv1 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg1/lv1 to 307200 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 307200 blocks long.

Next, check the new size of the logical volume lv1.

[[email protected] mnt]# lvdisplay /dev/vg1/lv1 
  --- Logical volume ---
  LV Name                /dev/vg1/lv1
  VG Name                vg1
  LV UUID                dgLZ79-JZdn-NUSF-fUS1-YVFk-36qs-iuafhE
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                300.00 MiB
  Current LE             75
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Has completed. The size of logical volume lv1 has now been increased by 100 MB.

Delete logical volume

Exit from the / mnt mount point, unmount the logical volume lv1 and remove it using the command lvremove.

[[email protected] mnt]# cd ..
[[email protected] /]# umount /mnt/
[[email protected] /]# lvremove /dev/vg1/lv1 
Do you really want to remove active logical volume lv1? [y/n]: y
  Logical volume "lv1" successfully removed

Delete volume group

[[email protected] /]# vgremove /dev/vg1
  Volume group "vg1" successfully removed

Delete physical volume

[[email protected] /]# 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

.

Sidebar