Tuesday 21 September 2010

Linux LVM tips


 

LVM Tools

LVM Tool

Description

pvcreate

Create physical volume from a hard drive

vgcreate

Create logical volume group from one or more physical volumes

vgextend

Add a physical volume to an existing volume group

vgreduce

Remove a physical volume from a volume group

lvcreate

Create a logical volume from available space in the volume group

lvextend

Extend the size of a logical volume from free physical extents in the logical volume group

lvremove

Remove a logical volume from a logical volume group, after unmounting it

vgdisplay

Show properties of existing volume group

lvdisplay

Show properties of existing logical volumes

pvscan

Show properties of existing physical volumes

Operations on Logical Volumes

Among typical operations:

A Walkthrough of the LVM for Linux

Adding a disk to the Volume Group

Next, we'll add /dev/hda6 to the Volume Group. Just type vgextend vg01 /dev/hda6 and you're done! You can check this out by using vgdisplay -v vg01. Note that there are now a lot more PEs available!

Moving Creating a striped Logical Volume

Note that LVM created your whole Logical Volume on one Physical Volume within the Volume Group. You can also stripe an LV across two Physical Volumes with the -i flag in lvcreate. We'll create a new LV, lv02, striped across hda5 and hda6. Type lvcreate -l4 -nlv02 -i2 vg01 /dev/hda5 /dev/hda6. Specifying the PV on the command line tells LVM which PEs to use, while the -i2 command tells it to stripe it across the two.

You now have an LV striped across two PVs!

Moving data within a Volume Group

Up to now, PEs and LEs were pretty much interchangable. They are the same size and are mapped automatically by LVM. This does not have to be the case, though. In fact, you can move an entire LV from one PV to another, even while the disk is mounted and in use! This will impact your performance, but it can prove useful.

Let's move lv01 to hda6 from hda5. Type pvmove -n/dev/vg01/lv01 /dev/hda5 /dev/hda6. This will move all LEs used by lv01 mapped to PEs on /dev/hda5 to new PEs on /dev/hda6. Effectively, this migrates data from hda5 to hda6. It takes a while, but when it's done, take a look with lvdisplay -v /dev/vg01/lv01 and notice that it now resides entirely on /dev/hda6!

Removing a Logical Volume from a Volume Group

Let's say we no longer need lv02. We can remove it and place its PEs back in the empty pool for the Volume Group. First, unmounting its filesystem. Next, deactivate it with lvchange -a n /dev/vg01/lv02. Finally, delete it by typing lvremove /dev/vg01/lv02. Look at the Volume Group and notice that the PEs are now unused.

Removing a disk from the Volume Group

You can also remove a disk from a volume group. We aren't using hda5 anymore, so we can remove it from the Volume Group. Just type vgreduce vg01 /dev/hda5 and it's gone!

A file system on logical volume may be extended. Also more space may be added to a VG by adding new partitions or devices  with the command: vgextend. For example:

lvextend -L +4G /dev/VolGroup00/LogVol04

The command pvmove can be used in several ways to move any LV elsewhere. There are also many more commands to rename, remove, split, merge, activate, deactivate and get extended information about current PV's, VG's and LV's.

Here is a typical du map of a server with volume manager installed. As you can see all partitions except /boot partition are referred vi path /dev/mapper/VolGroup00-LogVolxx where xx is two digit number:

                

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

4128448 316304 3602432 9% /

/dev/sda3 194449 22382 162027 13% /boot

none 2020484 0 2020484 0% /dev/shm

/dev/mapper/VolGroup00-LogVol05

4128448 42012 3876724 2% /home

/dev/mapper/VolGroup00-LogVol03

4128448 41640 3877096 2% /tmp

/dev/mapper/VolGroup00-LogVol02

8256952 3189944 4647580 41% /usr

/dev/mapper/VolGroup00-LogVol04

8256952 174232 7663344 3% /var

/dev/hde 594366 594366 0 100% /media/cdrecorder

                

Resiliency to renumbering of physical hard disks

LVM identifies PVs by UUID, not by device name.

Each disk (PV) is labeled with a UUID, which uniquely identifies it to the system. 'vgscan' identifies this after a new disk is added that changes your drive numbering. Most distros run vgscan in the lvm startup scripts to cope with this on reboot after a hardware addition. If you're doing a hot-add, you'll have to run this by hand I think. On the other hand, if your vg is activated and being used, the renumbering should not affect it at all. It's only the activation that needs the identifier, and the worst case scenario is that the activation will fail without a vgscan with a complaint about a missing PV.

The failure or removal of a drive that LVM is currently using will cause problems with current use and future activations of the VG that was using it.

How to get information about free space

vgdisplay shows logical volumes one by one and provides the information about free disk space on each:

vgdisplay volume_group_one | grep "Total PE"

How to create a new volume

# vgcreate vg01 /dev/hda2 /dev/hda10
  Volume group "vg01" successfully created

How to create and mount a partition

  1. # lvcreate -L 5G -n data vg02
      Logical volume "data" created

     

  2. # mkfs -t ext3 /dev/vg02/data
     

  3. # mkdir /data
    # mount /dev/vg02/data /data/

  4. # df -h /data
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/test--volume-data
                          50.0G   33M  5.0G   1% /data

  5. Add it to /etc/fstab

How to extend the partition

If one wishes to use all the free physical extents on the volume group one can achieve this using the lvm
lvextend command :

lvm lvextend -L +4G /dev/VolGroup00/LogVol04 # extend /var

ext2online /dev/VolGroup00/LogVol04

Option -l operates with free extents . This adds the 7153 free extents to the logical volume:

# lvm lvextend -l+7153 /dev/TestVG/TestLV

Extending logical volume TestLV to 30.28 GB

Logical volume TestLV successfully resized

 "lvextend -L +54 /dev/vg01/lvol10 /dev/sdk3" tries to extend the size of that logical volume by 54MB on physical volume /dev/sdk3. This is only possible if /dev/sdk3 is a member of volume group vg01.

Then the pvcreate command is used to create the new physical volume using the new partition, and the pvs again to verify the new physical volume. See  redhat.com Knowledgebase

How to remove LVM partionon

Use lvremove to Remove a logical volume from a logical volume group, after unmounting it

syntax:

lvremove [-A/--autobackup y/n] [-d/--debug] [-f/--force] [-h/-?/--help] [-t/--test] [-v/--verbose] LogicalVolumePath [LogicalVolumePath...]

lvremove removes one or more logical volumes. Confirmation will be requested before deactivating any active logical volume prior to removal. Logical volumes cannot be deactivated or removed while they are open (e.g. if they contain a mounted filesystem).
 

Options.

-f, --force
Remove active logical volumes without confirmation.


EXAMPLES
Remove the active logical volume lvol1 in volume group vg00 without
asking for confirmation:

lvremove -f vg00/lvol1

Remove all logical volumes in volume group vg00:

lvremove vg00


 

TIPS:

Missing drives in a volume group have caused me a lot of problems. If you just want to excommunicate the drive from your vg, the following works:


 

vgreduce --removemissing vg00


 


 

# pvscan

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

PV unknown device VG milan lvm2 [30.00 GB / 29.90 GB free]

PV /dev/sdg VG milan lvm2 [30.00 GB / 29.90 GB free]


 

# vgscan

Reading all physical volumes. This may take a while...

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

Couldn't find all physical volumes for volume group milan.

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

Couldn't find all physical volumes for volume group milan.

Volume group "milan" not found


 

# lvscan

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

Couldn't find all physical volumes for volume group milan.

Couldn't find device with uuid 'pQMQJ3-Rq3M-kQD9-M6DB-UWLi-Dyk4-3pWoa7'.

Couldn't find all physical volumes for volume group milan.

Volume group "milan" not found


 


 

LINKS:


 

More information about LVM at http://www.softpanorama.org/Commercial_linuxes/logical_volume_manager.shtml

1 comment:

  1. this command saved the day:

    vgreduce --removemissing vg00

    Many thanks

    ReplyDelete