Friday, March 16, 2007

Setting Up LVM(Logical Volume Manager) Using The Command Line Tools.

This is very useful to automate the setup, e.g. when you have lots of LV(Logical Volumes) to be created. We simply follow the three basic steps:

Step 1 (PVs): pvcreate /dev/device [/dev/device] ...
The command initializes (a) physical device(s) as (a) physical volume(s) (PV). Partitions must have the partition ID 8E (use fdisk, cfdisk or yast for partitioning), (whole) disks must have no partition table(Though you can create Physical Extents even if you have some raw space on your harddrive). To see all physical devices that are available, use the command lvmdiskscan. The command pvscan shows all existing PVs. If a disk (the whole disk, e.g. /dev/sdc) cannot be used and pvcreate complain about it having a partition table, after making sure you really want to use this disk for LVM and do not have anything else on it you can erase the partition table with dd if=/dev/zero if=/dev/device−name bs=1k count=1(followed by blockdev −−rereadpt to tell the kernel to re−read the p.table).

Step 2 (VGs): vgcreate vg−name /dev/device [/dev/device] ...
The vgcreate command creates a volume group with all the physical volumes specified. The default extent size is 4 MB. The limit for the number of physical extents/logical extents on a PV/LV is 65534 due to kernel limits. Therefore, the default size of 4MB limits the max. size of any PV or LV to 4M*65534~255GB.Should you have larger PVs or require bigger LVs you can increase the extent size. A PE is also the smallest unit by which you can increase, decrease or address (e.g. to move PEs to other PVs) storage in LVM.

Step 3 (LVs): lvcreate −L size[K|M|G|T] −n lv−name vg−name
The lvcreate command creates a logical volume on the volume group vg−name. If no name is given for the LV the default name lvol# is used, where # is the internal number of the LV. The LV can be created as striped (RAID0) over several or all available PVs using the −i # parameter, where # specifies the number of stripes. Normally LVs use up any space available on the PVs on a next−free basis, i.e. if you do the creation of LVs one by one you essentially get concatenation, until you start shrinking, growing, deleting and so on,when space gets freed and reallocated anywhere on the PVs.This is all. From now on you can use the LVs by referring to them via /dev/vg−name/lv−name. Use them like you would use any partition.

Automatic server setup from stored LVM setup.
The LVM of the server can be reset using lvmchange. This is a last resort if something goes wrong. It does not change the LVM setup stored on the physical devices, only the data in the running system is reset.To restore an LVM setup from the one stored on disk, run first vgscan to find all VGs and then vgchange −a y to activate them. This is what happens at each boot time, see init file /etc/init.d/boot (look for vcgscan). You can completely delete the local LVM setup information with rm −rf /etc/lvm*, including all device files for accessing the LVs (/dev/vg−name/lv−name), and it will automatically be recreated next time you boot or issue the vgscan command manually. This means that you do not have to do anything after a reboot to be able to use your LVs again, the runtime LVM setup happens automatically during a system boot, if any LVM setup is detected on any of the attached physical devices.