Contact for queries :

Login

  UpComing Live WebEx Workshop Series

LVM Tasks in day to day Linux admin Job

Task1 – Installing LVM packages and verifying the LVM commands

Step 1. Install the required packages:

# yum install -y lvm2

Step 2. Verify that it is installed:

# rpm -qa | grep lvm
lvm2-libs-2.02.72-8.el6.x86_64
lvm2-2.02.72-8.el6.x86_64

This package is usually installed by default, but verify that it is. Without this package, you are unable to use LVM on your system. Let’s go through the model again, but this time make use of the management  commands.

First, let’s look at the physical volumes.

Step 3. To view the physical volumes, use the pvs command:

# pvs
PV VG Fmt Attr PSize PFree
/dev/hda2 vg_rhel601 lvm2 a- 19.51g 0

Step 4. You could also get more detailed information about the physical volume with the pvdisplay command:

# pvdisplay
— Physical volume —
PV Name /dev/hda2
VG Name vg_rhel601
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID XI3mef-f2Qm-J5Ri-jiU9-qI8S-hBTI-No6tPn

By default, when Red Hat is installed, it uses LVM to manage the disks. The preceding output shows the physical volume created during the installation of Red Hat on the lab box. The next layer in the LVM structure would be the volume group.

Step 5. To view information about the volume group, use the vgs command:

# vgs
VG #PV #LV #SN Attr VSize VFree
vg_rhel601 1 2 0 wz—n- 19.51g 0

Step 6. To get more detailed information about the volume group, use the vgdisplay command:

# vgdisplay
— Volume group —
VG Name vg_rhel601
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID pDycLd-SMos-RLeW-656R-xUZv-b45T-485S8D

Finally, you can look at the last layer, the logical volumes.

Step 7. To view the logical volumes, use the lvs command:

# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_root vg_rhel601 -wi-ao 15.57g
lv_swap vg_rhel601 -wi-ao 3.94g
Step 8. To get more detailed information about the logical volumes, use the lvdisplay command:
# lvdisplay
— Logical volume —
LV Name /dev/vg_rhel601/lv_root
VG Name vg_rhel601

LV UUID I1rLRo-T8Qu-uprj-FdIh-oCQi-6PgL-d0bGeC
LV Write Access read/write
LV Status available
# open 1
LV Size 15.57 GiB
Current LE 3986
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
— Logical volume —
LV Name /dev/vg_rhel601/lv_swap
VG Name vg_rhel601
LV UUID JgOxey-h7Ih-mFgO-6mdt-uibZ-9W9H-I8tZKv
LV Write Access read/write
LV Status available
# open 1
LV Size 3.94 GiB
Current LE 1008
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:1

TASK 2 :  Creating an LVM Partition

Let’s use disk /dev/hdb for the LVM partition. First, you need to initialize the disk to be used for a physical volume.

Step 1. To initialize the disk, use the pvcreate command:

# pvcreate /dev/hdb
Physical volume “/dev/hdb” successfully created

Step 2. Verify that the physical volume was created successfully:

# pvdisplay /dev/hdb
“/dev/hdb” is a new physical volume of “8.00 GiB”
— NEW Physical volume —
PV Name /dev/hdb
VG Name
PV Size 8.00 GiB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0

Allocated PE 0
PV UUID QlCLgF-esaN-5PvR-PWWV-D6jZ-O207-rHg90j

WARNING: Physical volumes are limited to one physical disk or one partition of a physical disk per one physical volume. With the disk initialized, you can next create a volume group and add the physical disk to it.

Step 3. To create the volume group, use the vgcreate command:

# vgcreate vg_group01 /dev/hdb
Volume group “vg_group01” successfully created

Step 4. Verify that the volume group was created successfully:

# vgdisplay -v VolGroup01
Using volume group(s) on command line
Finding volume group “vg_group01”
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 8.00 GiB
PE Size 4.00 MiB
Total PE 2047
Alloc PE / Size 0 / 0
Free PE / Size 2047 / 8.00 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz
— Physical volumes —
PV Name /dev/sdb
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9
PV Status allocatable
Total PE / Free PE 2047 / 2047

When volume groups are created and initialized, the physical volumes are broken down into physical extents (the unit of measurement for LVM).

This is significant because you can adjust the size of how data is stored based on the size of each physical extent, defined when the volume group is created (the default is 4MB).

Although you have added only one physical volume to the volume group, you still have created a pool of storage space. Now you can set up logical volumes that hold the partitions for the system.

To create a logical volume, use the lvcreate command and specify the size of the partition that you’d like to create.

The size can be specified in kilobytes, megabytes, gigabytes, or logical extents (LE). Like physical extents, logical extents are a unit of measure when dealing with logical volumes.

They map directly to physical  extents where the actual data is kept. Dealing with megabytes is probably the easiest here.

Step 5. Create a partition 3GB in size:

# lvcreate -L 3000 VolGroup01
Logical volume “lvol0” created

Step 6. Verify that the logical volume was created successfully:

# lvdisplay vg_group01
— Logical volume —
LV Name /dev/vg_group01/lvol0
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 2.93 GiB
Current LE 750
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2

Step 7. Use the vgdisplay command again and see whether the logical volume has been added to it:

# vgdisplay -v VolGroup01
Using volume group(s) on command line
Finding volume group “vg_group01”
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 8.00 GiB
PE Size 4.00 MiB
Total PE 2047
Alloc PE / Size 750 / 2.93 GiB
Free PE / Size 1297 / 5.07 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz
— Logical volume —
LV Name /dev/vg_group01/lvol0
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 2.93 GiB
Current LE 750
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
— Physical volumes —
PV Name /dev/sdb
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9
PV Status allocatable
Total PE / Free PE 2047 / 1297

By default, all logical volumes take the name lvol#. You can change this and specify the name that you’d like to use for your logical volume partition with the lvcreate command (when you’re creating your partition).
Use the -n option to specify a name.

Step 8. Alternatively, you could do the following:

# lvcreate –L 3000 –n SecretData vg_group01

This approach would have created the logical volume the same as before, but the name would be SecretData instead of lvol0. If you have already created your logical volume and you forgot to give it a name, you don’t have to delete it and create it again.

Using the lvrename command, you can change the name of a logical partition:

# lvrename /dev/vg_group01/lvol0 /dev/vg_group01/SecretData
Renamed “lvol0” to “SecretData” in volume group “vg_group01”

Step 9. Verify with the following command:

# lvdisplay vg_group01
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 2.93 GiB
Current LE 750
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2

Task 3: Resizing a LVM Partition

The single best feature of LVM is that you can reduce or expand your logical volumes and volume groups. If you are running out of room on a particular logical volume or volume group, you can add another physical volume to the volume group and then expand the logical volume to give you more room.

To extend a logical volume, use the lvextend command.

Step 1. Add 2GB more to the SecretData logical volume:

# lvextend -L +2000 /dev/vg_group01/SecretData
Extending logical volume SecretData to 4.88 GB
Logical volume SecretData successfully resized

Step 2. You also could specify a new size instead of using the + to add:

# lvextend -L 6000 /dev/VolGroup01/SecretData
Extending logical volume SecretData to 5.86 GB
Logical volume SecretData successfully resized

Step 3. Either of the preceding commands yields the same results. As a third alternative, you could also use the lvresize command:

# lvresize -L 7000 /dev/VolGroup01/SecretData
Extending logical volume SecretData to 6.84 GB
Logical volume SecretData successfully resized

Step 4. Verify the change with the following command:

# lvdisplay vg_group01
Using logical volume(s) on command line
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 6.84 GiB
Current LE 1750
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2

Suppose, though, that you want to add a new physical volume so that you can extend your volume group (possibly to add a new partition). You can use vgextend to expand your volume group.

Step 5. Create a new physical volume somewhere:

# pvcreate /dev/hdc
Physical volume “/dev/hdc” successfully created

Step 6. Extend your volume group to incorporate that new physical volume:

# vgextend VolGroup01 /dev/hdc
Volume group “vg_group01” successfully extended

Step 7. Now view the details of the newly increased volume group:

# vgdisplay -v vg_group01
Using volume group(s) on command line
Finding volume group “vg_group01”
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 15.99 GiB
PE Size 4.00 MiB
Total PE 4094
Alloc PE / Size 1750 / 6.84 GiB
Free PE / Size 2344 / 9.16 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 6.84 GiB
Current LE 1750
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
— Physical volumes —
PV Name /dev/sdb
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9
PV Status allocatable
Total PE / Free PE 2047 / 297
PV Name /dev/sdc
PV UUID Wn52Tv-ZEJR-IkYb-2oi1-5Iw7-2oN7-wHihl4
PV Status allocatable
Total PE / Free PE 2047 / 2047

You can see that the VG Size has gone up to 15.99GB from 8GB earlier. Just as you can use commands to increase or extend the LVM structures, you can also reduce them. Although extension creates space that isn’t there, you need to be extra careful when removing space because doing so could cause data loss. To decrease a logical volume, you use the lvreduce command, which has a similar syntax to lvextend.

Step 8. Reduce the SecretData logical volume to 2GB in size:

# lvresize -L 4000 /dev/vg_group01/SecretData
WARNING: Reducing active logical volume to 3.91 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce SecretData? [y/n]: y
Reducing logical volume SecretData to 3.91 GiB
Logical volume SecretData successfully resized

Step 9. You could also use the following command:

# lvresize -L 2000 /dev/vg_group01/SecretData
WARNING: Reducing active logical volume to 1.95 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce SecretData? [y/n]: y
Reducing logical volume SecretData to 1.95 GiB
Logical volume SecretData successfully resized

Reducing a volume group works in the same manner.

Step 10. To reduce the volume group to no longer include the physical volume /dev/hdc, you can use the vgreduce command:

# vgreduce vg_group01 /dev/hdc
Removed “/dev/hdc” from volume group “vg_group01”

Step 11. Use the vgdisplay command to verify expansion or reduction of volume groups and logical volumes:

# vgdisplay vg_group01
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 12
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 8.00 GiB
PE Size 4.00 MiB
Total PE 2047
Alloc PE / Size 500 / 1.95 GiB
Free PE / Size 1547 / 6.04 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz

Task 4: Migrating Data from one Physical disk to another

Suppose you have a drive that is old or dying and you’d like to remove it from the system. On a system with normal partitions, you would have to copy all the data from one disk to another while the disk is offline (because of file locks).

Having LVM makes this easier because you can migrate your data from one disk to another, even while the disk is online! This capability is very useful when you need to replace a disk.

If you want to replace /dev/hdb because it’s failing, you can use the pvmove command to migrate the physical extents (which is really your data) to another physical volume (/dev/hdc). Because you removed /dev/hdc from the volume group in the previous section, you need to add it back.

Step 1. Verify that you have two physical volumes inside the vg_group01 volume group:

# vgdisplay –v vg_group01
Using volume group(s) on command line
Finding volume group “vg_group01”
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 14
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 15.99 GiB
PE Size 4.00 MiB
Total PE 4094
Alloc PE / Size 1750 / 6.84 GiB
Free PE / Size 2344 / 9.16 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 1.95 GiB
Current LE 500
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
— Physical volumes —
PV Name /dev/sdb
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9
PV Status allocatable
Total PE / Free PE 2047 / 297

Step 2. Because there is only a single physical disk, you need to add back /dev/hdc to the volume group:

# vgextend vg_group01 /dev/sdc
Volume group “vg_group01” successfully extended

Step 3. Also create a logical volume to hold the migrated data:

# lvcreate -L 5000 vg_group01
Logical volume “lvol0” created

Step 4. Verify all logical volumes are in place:

# lvdisplay vg_group01
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 1.95 GiB
Current LE 500
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
— Logical volume —
LV Name /dev/vg_group01/lvol0
VG Name vg_group01
LV UUID 6vsP79-Gu7W-PO4p-tCHg-OMqN-W8QN-QAIrhH
LV Write Access read/write
LV Status available
# open 0
LV Size 4.88 GiB
Current LE 1250
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:3

Step 5. You should also verify that you now have two physical disks for the vg_group01 volume group:

# vgdisplay -v vg_group01
Using volume group(s) on command line
Finding volume group “vg_group01”
— Volume group —
VG Name vg_group01
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 14
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 15.99 GiB
PE Size 4.00 MiB
Total PE 4094
Alloc PE / Size 1750 / 6.84 GiB
Free PE / Size 2344 / 9.16 GiB
VG UUID IvjXga-898Y-1vCC-azRt-pszL-PeWR-E5athz
— Logical volume —
LV Name /dev/vg_group01/SecretData
VG Name vg_group01
LV UUID mk9dJM-3qt7-ypbC-nsks-I8Gh-9V3d-4BNE6s
LV Write Access read/write
LV Status available
# open 0
LV Size 1.95 GiB
Current LE 500
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2
— Logical volume —
LV Name /dev/vg_group01/lvol0
VG Name vg_group01
LV UUID 6vsP79-Gu7W-PO4p-tCHg-OMqN-W8QN-QAIrhH
LV Write Access read/write
LV Status available
# open 0
LV Size 4.88 GiB
Current LE 1250
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:3
— Physical volumes —
PV Name /dev/sdb
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9
PV Status allocatable
Total PE / Free PE 2047 / 297
PV Name /dev/sdc
PV UUID Wn52Tv-ZEJR-IkYb-2oi1-5Iw7-2oN7-wHihl4
PV Status allocatable
Total PE / Free PE 2047 / 2047

Step 6. Migrate the data from the “dying” drive:

# pvmove /dev/hdb /dev/hdc
/dev/sdb: Moved: 0.2%
/dev/sdb: Moved: 7.0%
/dev/sdb: Moved: 13.7%
/dev/sdb: Moved: 20.2%
/dev/sdb: Moved: 26.9%
/dev/sdb: Moved: 28.6%
/dev/sdb: Moved: 35.4%
/dev/sdb: Moved: 40.8%
/dev/sdb: Moved: 47.2%
/dev/sdb: Moved: 53.9%
/dev/sdb: Moved: 60.4%
/dev/sdb: Moved: 67.0%
/dev/sdb: Moved: 73.7%
/dev/sdb: Moved: 80.4%
/dev/sdb: Moved: 87.0%
/dev/sdb: Moved: 93.8%
/dev/sdb: Moved: 100.0%

Make sure that you have more than one physical volume; otherwise, there will be nowhere for the data to move.

Step 7. Verify that the physical volume is empty:

# pvdisplay /dev/hdb
— Physical volume —
PV Name /dev/sdb
VG Name vg_group01
PV Size 8.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2047
Free PE 2047
Allocated PE 0
PV UUID B0K2qH-0UF4-gphE-MLi3-RFXH-HEP9-9FMbA9

Task 5: Deleting a LVM partition

It is just as important to understand how to delete LVM partitions as it is to create them. This is a common task when you are upgrading or redesigning a file system layout.

Step 1. To remove a logical volume, use the lvremove command:

# lvremove /dev/vg_group01/SecretData
Do you really want to remove active logical volume SecretData? [y/n]: y
Logical volume “SecretData” successfully removed

A nice feature about lvremove is that it has a -t option, which allows you to test the removal of a logical partition without actually deleting it.

The commands to remove volume groups and physical volumes also support the -t option.

WARNING: Although this advice should be common sense, make sure you back up any data before deleting anything within the LVM structure. Removing logical volumes, volume groups, and physical volumes is destructive and results in all data loss for that object within the LVM structure. The deletion occurs even if there is data on any part of the LVM structure.

Going down the LVM layers, you could also remove the entire volume group if you want to.

Step 2. Use the vgremove command to remove the volume group:

# vgremove vg_group01
Do you really want to remove volume group “vg_group01” containing
1 logical volumes? [y/n]: y
Do you really want to remove active logical volume lvol0? [y/n]: y
Logical volume “lvol0” successfully removed
Volume group “vg_group01” successfully removed

You can also do both steps in one command by using the -f option with vgremove:

# vgremove -f VolGroup01

Step 3. Verify the changes:

# vgdisplay vg_group01
Volume group “vg_group01” not found

You should no longer see vg_group01 (as displayed here). At the last layer are the physical volumes, which you can remove with the pvremove command. Using this command completely wipes all LVM metadata
(essentially the whole disk drive).

Step 4. Wipe all the current physical volumes:

# pvremove /dev/hdb
Labels on physical volume “/dev/hdb” successfully wiped
# pvremove /dev/hdc
Labels on physical volume “/dev/hdc” successfully wiped

November 14, 2015

0 responses on "LVM Tasks in day to day Linux admin Job"

Leave a Message

Your email address will not be published. Required fields are marked *

About iGURKUL

IGURKUL I.T. Training Hub offering various Career Certification courses in Computer Networking, Unix, Linux, Cloud Computing and DevOps Technologies. With its rich experience in IT training service sector, iGURKUL has been able to set Industry best practices in IT Training for the past five years.

In Past five years, more than 5000 professionals have been trained by iGURKUL for System administration, Cloud Computing and DevOps Skill set through our Online Training portal www.unixadminschool.com. And , each day , more than 10000 working professionals from all over the globe visiting our knowledge base www.unixadminschool.com/blog for the best practices and Knowledge learning.

top
copyright protected - 2011 © igurkul I.T. solutions. All rights reserved.