Monday, 6 June 2011

Create and remove physical volumes, assign physical volumes to volume groups, create and delete logical volumes

This is another objective that is not very useful by itself, after all once you create a logical volume, you still have to create a file system and mount it, anyway, let's get cracking.

Assuming you have an empty disk you can simply do the following to create a physical volume:

pvcreate /dev/sdc - this assumes that you have a a device called /dev/sdc


This is not very useful, namely to have a single physical volume, the only thing you have done is given yourself the oportunity to add more storage later, which is not actually a great thing, but for this objective you are better off with at least two disks.

Before we move any further, let's cover the remove physical volumes objective:

pvremove /dev/sdc

There are limitations to what the command can do, e.g. don't expect it to remove a physical volume that has a volume group.

Note that the -ff option will remove it almost no matter what, use at your own risk.

You can list the physical volumes with the following commands:

pvs

pvdisplay

Assuming that you have created two physical volumes, /dev/sdc & /dev/sdd, we can create a volume group like this

vgcreate volname /dev/sdc /dev/sdd

Alternatively, you could this to mimic a real life situation where you create a logical volume, run out of space and need to extend, in which case you add a new physical volume to the volume group:

vgcreate volname /dev/sdc - this creates the volume group volname

vgextend volname /dev/sdd - this adds the /dev/sdd phyical volume to the volname volume group

Say you made a mistake and you actually wanted to add /dev/sde rather than /dev/sdd to volname, just run the following

vgreduce volname /dev/sdd - needless to say that there will be limitations to this command as well

You can list the volume groups with the following commands:

vgs

vgdisplay

And finally we come to the useful part, the logical volumes, by now, you probably have sussed out what the commands are going to be: lvcreate, lvremove, lvs, lvdisplay, 100% right.

lvcreate -l 10%VG volname -n lvol1 - This will create a logical volume called lvol1 that is 10% the size of the volumegroup volname.

lvcreate -L 500M volname -n lvol2 - This will create a logical volume called lvol2 that is 500M, this will only work if there is enough free space in volname.


lvcreate -i 2 -l 10%FREE volname -n lvol3 - This will create a logical volume called lvol3 that is 10% of  the size of the space left in volname. The -i 2 switch will ensure that volume group spans two physical volumes, again this will only work if there are two or more physical volumes.

You've just realized that you wanted a bigger lvol3, no problem, just extend it with:

lvextend -l 20%FREE /dev/volname/lvol3

The lvreduce command behaves in a similar manner

In order to delete the lvol3 you would type:

lvremove /dev/volname/lvol3
or
lvremove /dev/mapper/volname-lvol3

No comments:

Post a Comment