Wednesday, January 22, 2014

Logical Volume Manager (LVM)

Logical Volume Manager (LVM)


This article describes a basic logic behind a Linux logical volume manager by showing real examples of configuration and usage. Although Debian Linux will be used for this tutorial, you can also apply the same command line syntax with other Linux distributions such as Red Hat, Mandriva, SuSe Linux and others.

1. This is what we are going to do

Logical Volume Manager

Scenario:

In this example let us
  1. Create 3 partitions of size each 100MB.
  2. Convert them into physical  volumes.
  3. Combine physical volumes into volume group.
  4. Finally create a logical volume from the volume group.

Create Partitions

Use fdisk command to create and manage partions.
To view the existing partitions use following command
[root@server ~]# 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 above output shows us two physical hard disks. The /dev/sda contains three partitions and no space to create additional partions. And the second drive /dev/sdb contains no partions yet. So let us use the second one in this tutorial.
Now let us create three partions of each size 100MB using fdisk command.
[root@server ~]# 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 whether the partions have 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 partions.
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@server ~]# 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.
Again we will check the existing partitions using fdisk command.
[root@server ~]# 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 above output shows three partions has been created in the /dev/sdb disk. If fdisk -l doesn’t show the output reboot to take effect.

Create Physical Volumes

Now create physical volumes using the command pvcreate.
[root@server ~]# 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
We can use like this also
[root@server ~]# pvcreate /dev/sdb{1,2,3} 

To verify the newly created physical volumes use the command pvdisplay.
[root@server ~]# 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 Volume Groups

Create a new volume group called vg1 using two physical volumes /dev/sdb1 and /dev/sdb2 using the command vgcreate.
[root@server ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 
  Volume group "vg1" successfully created
We can use like this also
[root@server ~]# vgcreate vg1 /dev/sdb{1,2}
To verify the volume group has been created or not use the command vgdisplay.
[root@server ~]# 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 Logical Volume

To create logical volume use the command lvcreate. Let us create a logical volume called lv1 with size 200MB.
[root@server ~]# lvcreate -L 200M vg1 -n lv1
  Logical volume "lv1" created
Verify the logical volume is created or not using command lvdisplay.
[root@server ~]# 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 the logical volume

Now format the newly created logical volume and mount it in the /mnt directory or wherever you want.
[root@server ~]# 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.
And mount the logical volume in the /mnt mount point.
[root@server ~]# mount /dev/vg1/lv1 /mnt/
Now the logical volume is successfully mounted in /mnt. You can use the new logical volume to store your datas.
[root@server ~]# cd /mnt/
[root@server mnt]# touch file1 file2 file3
[root@server mnt]# mkdir dir1 dir2 dir3
[root@server mnt]# ls
dir1  dir2  dir3  file1  file2  file3  lost+found

No comments:

Post a Comment