Tuesday, 30 May 2017

Linux LVM – Volume Creation & Operations

As of now we have seen physical volume administration and volume group administration on this Linux’s LVM tutorial series.Logical volume is the top most virtual object in the LVM. Once you have constructed physical volume and volume group,the next step will creating the logical volume and mount the filesystem for storing the data.Here we will see how to create a new logical volume with various layouts  linear,Mirror,stripped.And also we will see how to rename the logical volume.

The below image will explain about Logical volume manager’s virtual object construction level.You can see Logical volumes are the top most virtual objects in LVM.

1. Creating Logical Volumes:

Logical volumes can be created using “lvcreate” command. If you didn’t specific the logical volume name by default it will take name as lvol#  where #  is the internal number of the logical volume withing in volume group.

There are three type of volume layouts are possible in LVM.
1.Linear volume
2.Mirror Volume
3.Stripe Volume

1.1 How to create a Linear volume in LVM ? 
Linear logical volume can be created using “lvcreate” command by without specifying any additional options to it. This is the default volume layout in LVM where it allocates the space on next-free basis within the volume group on physical volumes

1.List the volume group available in your linux system.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  uavg        1   0   0 wz--n- 508.00m 508.00m
  vg_mylinz   1   2   0 wz--n-  19.51g      0
[root@mylinz ~]#

2.Create a new Linear logical volume with size of 100MB.
[root@mylinz ~]# lvcreate -L 100M uavg
  Logical volume "lvol0" created
[root@mylinz ~]#
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-a- 100.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]#
3.If you want to create a volume specific name,use the option “-n” .
[root@mylinz ~]# lvcreate -L 100M -n oravol1 uavg
  Logical volume "oravol1" created
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lvol0   uavg      -wi-a- 100.00m
  oravol1 uavg      -wi-a- 100.00m
  lv_root vg_mylinz -wi-ao  16.54g
  lv_swap vg_mylinz -wi-ao   2.97g
[root@mylinz ~]#

4.To get the complete volume details ,use lvdisplay command.
[root@mylinz ~]# lvdisplay /dev/uavg/oravol1
  --- Logical volume ---
  LV Name                /dev/uavg/oravol1
  VG Name                uavg
  LV UUID                gxTE82-XEdR-0c3M-xrso-bsb8-7dJG-fXDLFK
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[root@mylinz ~]#

5.If you want to create a volume on specific LUN or disk,use below command.
First find the disk which has free space to create volume.
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 308.00m
  /dev/sde   uavg      lvm2 a-   508.00m 508.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#
As per the above command output,we can choose /dev/sde to create a new volume in it.
[root@mylinz ~]# lvcreate -L 100M -n datavol1 uavg /dev/sde
  Logical volume "datavol1" created
[root@mylinz ~]# 
6.Check the detailed LVM logical volume details .
[root@mylinz ~]# lvs -o +devices
  LV       VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert Devices
  datavol1 uavg      -wi-a- 100.00m                               /dev/sde(0)
  lvol0    uavg      -wi-a- 100.00m                               /dev/sdd1(13)
  oravol1  uavg      -wi-a- 100.00m                               /dev/sdd1(38)
  lv_root  vg_mylinz -wi-ao  16.54g                               /dev/sda2(0)
  lv_swap  vg_mylinz -wi-ao   2.97g                               /dev/sda2(4234)
[root@mylinz ~]#
Here you can see that volume “datavol1” is created on “/dev/sde” as we specified on the lvcreate command.
7.We can also create a volume with percentage from the volume group. (Ex: 50% of VG need to be allocated to the new volume).Note:You need to “-l” to specify the percentage.
[root@mylinz ~]# lvcreate -l 50%VG -n appvol1 uavg
  Logical volume "appvol1" created
[root@mylinz ~]# lvs
  LV       VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  appvol1  uavg      -wi-a- 508.00m
  datavol1 uavg      -wi-a- 100.00m
  lvol0    uavg      -wi-a- 100.00m
  oravol1  uavg      -wi-a- 100.00m
  lv_root  vg_mylinz -wi-ao  16.54g
  lv_swap  vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   4   0 wz--n- 1016.00m 208.00m
  vg_mylinz   1   2   0 wz--n-   19.51g      0
[root@mylinz ~]#
8.Create a filesystem and mount it under desired mount point.
[root@mylinz ~]# mkfs -t ext4 /dev/uavg/appvol1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=32 blocks, Stripe width=96 blocks
27664 inodes, 110592 blocks
5529 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
1976 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 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@mylinz ~]# mount -t ext4 /dev/uavg/appvol1 /appvol1
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-appvol1
                      105M  5.6M   94M   6% /appvol1
[root@mylinz ~]#
1.2 How to create a mirror volume in LVM ? 
Whenever we want data redundancy from Linux level , you should go for data mirroring.
Redhat Linux provides software RAID also for mirroring the physical DISKS or LUNS. Here we will see how to create a mirrored volume from the volume group.

1.List the volume group.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  uavg        2   0   0 wz--n- 1016.00m 1016.00m
  vg_mylinz   1   2   0 wz--n-   19.51g       0
[root@mylinz ~]#
2.Create a mirror volume from volume group uavg.This command will force to keep the log in memory instead of disk.
[root@mylinz ~]#  lvcreate -L 100MB -m1 --corelog -n mirrorvol1 uavg
  Logical volume "mirrorvol1" created
[root@mylinz ~]#


3.Check the newly created volume “mirrorvol1” details.
[root@mylinz ~]# lvs -a -o +devices
  LV       VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert Devices
  mirrorvol1  uavg      mwi-a- 100.00m          100.00         mirrorvol1_mimage_0(0),mirrorvol1_mimage_1(0)
  [mirrorvol1_mimage_0] uavg      iwi-ao 100.00m                 /dev/sdd1(0)
  [mirrorvol1_mimage_1] uavg      iwi-ao 100.00m                 /dev/sde(0)
  lv_root       vg_mylinz -wi-ao  16.54g                         /dev/sda2(0)
  lv_swap       vg_mylinz -wi-ao   2.97g                         /dev/sda2(4234)
[root@mylinz ~]#
4.If you want to keep the corelog in disk,use the below command.
[root@mylinz ~]# lvcreate -L 100M -m1 -n mirrorvol1 uavg
  Insufficient suitable allocatable extents for logical volume : 25 more required
  Unable to allocate extents for mirror(s).
[root@mylinz ~]# lvcreate -L 50M -m1 -n mirrorvol1 uavg
  Rounding up size to full physical extent 52.00 MiB
  Insufficient suitable allocatable extents for logical volume : 13 more required
  Unable to allocate extents for mirror(s).
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 508.00m
  /dev/sde   uavg      lvm2 a-   508.00m 508.00m
  /dev/sdf             lvm2 a-     5.00g   5.00g
[root@mylinz ~]#
Oops…Yes.you will get the above error if you don’t have minimum three disks in volume group. LVM will always try to keep the mirror copies and logs in different disks .So you need three disks otherwise follow step 2 to create a mirror volume. (1.2.2)

Let me add one more disks to “uavg” and retry the above operation.
[root@mylinz ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda2  vg_mylinz lvm2 a-    19.51g      0
  /dev/sdd1  uavg      lvm2 a-   508.00m 508.00m
  /dev/sde   uavg      lvm2 a-   508.00m 508.00m
  /dev/sdf   uavg      lvm2 a-     5.00g   5.00g
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  uavg        3   0   0 wz--n-  5.99g 5.99g
  vg_mylinz   1   2   0 wz--n- 19.51g    0
[root@mylinz ~]#
[root@mylinz ~]# lvcreate -L 100M -m1 -n mirrorvol1 uavg
  Logical volume "mirrorvol1" created
[root@mylinz ~]# lvs -a -o +devices
  LV    VG    Attr   LSize   Origin Snap%  Move Log       Copy%  Convert Devices 
  mirrorvol1            uavg      mwi-a- 100.00m        mirrorvol1_mlog  60.00     mirrorvol1_mimage_0(0),mirrorvol1_mimage_1(0)
  [mirrorvol1_mimage_0] uavg      Iwi-ao 100.00m                    /dev/sdd1(0) 
  [mirrorvol1_mimage_1] uavg      Iwi-ao 100.00m                     /dev/sde(0) 
  [mirrorvol1_mlog]     uavg      lwi-ao   4.00m                    /dev/sdf(0)  
  lv_root               vg_mylinz -wi-ao  16.54g                    /dev/sda2(0) 
  lv_swap               vg_mylinz -wi-ao   2.97g                  /dev/sda2(423) 
[root@mylinz ~]#

.Good…We have successfully created a mirror volume with dedicated log disks(Step 1.2.4) and without disk using in memory log.(Step:1.2.2)
5. LVM provides an option to specify the number of mirror copies.Number of copies can be increased by increasing the “m” number.
[root@mylinz ~]# lvcreate -L 10M --corelog -m2 -n twowaymirvol1 uavg
  Rounding up size to full physical extent 12.00 MiB
  Logical volume "twowaymirvol1" created
[root@mylinz ~]# lvs
  LV            VG        Attr   LSize   Origin Snap%  Move Log             Copy%  Convert
  mirrorvol1    uavg      mwi-a- 100.00m             mirrorvol1_mlog 100.00
  twowaymirvol1 uavg      mwi-a-  12.00m                            100.00
  lv_root       vg_mylinz -wi-ao  16.54g
  lv_swap       vg_mylinz -wi-ao   2.97g
[root@mylinz ~]#

6.How to specify the devices for mirror ? Is it possible ?
Yes.Its possible.When you are creating the mirror volume,you can also specify which devices need to be used for mirror volume.
[root@mylinz ~]# lvcreate -L 10M --corelog -m1 -n oramirvol1 uavg /dev/sde /dev/sdf
  Rounding up size to full physical extent 12.00 MiB
  Logical volume "oramirvol1" created
[root@mylinz ~]#
[root@mylinz ~]# lvs |grep oramirvol1
  oramirvol1    uavg      mwi-a-  12.00m                              100.00
[root@mylinz ~]#


7.Create a filesystem and mount it .(Refer 1.1.8)

1.3 How to create a stripe volume in LVM ? 
Stripe volumes are very useful where you need performance.This volume will be mostly used where large sequential reads and writes happening.

1.List the volume group.
[root@mylinz ~]# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  uavg        3   0   0 wz--n-  5.99g 5.99g
  vg_mylinz   1   2   0 wz--n- 19.51g    0
[root@mylinz ~]#

2.Create a stripped volume. 
Options:
i – number of stripe (not more than number physical disks in the volume group)
I – Stripe size.
[root@mylinz ~]# lvcreate -L 100M -i 3 -I 32 -n strivol1 uavg
  Rounding size (25 extents) up to stripe boundary size (27 extents)
  Logical volume "strivol1" created
[root@mylinz ~]#

3.If you tried to create a volume with more stripes than available disks ,you will get below error.
[root@mylinz ~]# lvcreate -L 100M -i 5 -I 32 -n strivol2 uavg
  Number of stripes (5) must not exceed number of physical volumes (3)
[root@mylinz ~]#

4.Create a filesystem and mount it .(Refer 1.1.8).


2. Removing Logical Volumes:

Removing the logical volume is same for all the volume layouts (linear,stripe & mirror).

1.If you are attempt to remove the open volume,you will get below error.
[root@mylinz ~]# lvs
  LV       VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  strivol1 uavg      -wi-ao 108.00m
  lv_root  vg_mylinz -wi-ao  16.54g
  lv_swap  vg_mylinz -wi-ao   2.97g
[root@mylinz ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/uavg-strivol1
                      105M  5.6M   94M   6% /mnt
[root@mylinz ~]# lvremove /dev/uavg/strivol1
  Can't remove open logical volume "strivol1"
[root@mylinz ~]#

2.First umount the filesystem and close the volume using lvchange.
[root@mylinz ~]# umount /mnt
[root@mylinz ~]# lvchange -an /dev/uavg/strivol1
[root@mylinz ~]# lvs
  LV       VG        Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  strivol1 uavg      -wi--- 108.00m
  lv_root  vg_mylinz -wi-ao  16.54g
  lv_swap  vg_mylinz -wi-ao   2.97g
[root@mylinz ~]#

3.Remove the volume using “lvremove” command.
[root@mylinz ~]# lvremove /dev/uavg/strivol1
  Logical volume "strivol1" successfully removed
[root@mylinz ~]# lvs
  LV      VG        Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  lv_root vg_mylinz -wi-ao 16.54g
  lv_swap vg_mylinz -wi-ao  2.97g
[root@mylinz ~]#

Hope this article is informative for you. 

Thank you for visiting UnixArena. Please leave a comment if you have any doubt.
Suggestion are always welcome to improve this article and UnixArena.

No comments:

Post a Comment

What is PS2 - Continuation Interactive Prompt in Linux

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-lin...