Friday, 16 September 2016

What Is Linux

Linux is a free open-source operating system based on Unix. Linus Torvalds originally created Linux with the
assistance of developers from around the world. Linux is:
• Free
• Unix Like
• Open Source
• Network operating system
Strictly speaking, Linux is a kernel. A kernel provides access to the computer hardware and control access to
resources such as:
• Files and data.
• Running programs.
• Loading programs into memory.
• Networks.
• Security and firewall.
• Other resources etc.
The kernel decides who will use a resource, for how long and when.You can download the Linux kernel from the
official web site. However, the Linux kernel itself is useless unless you get all the applications such as text editors,
email clients, browsers, office applications, etc. Therefore, someone came up with idea of a Linux distribution. A
typical Linux distribution includes:
• Linux kernel.
• GNU application utilities such as text editors, browsers etc.
• Collection of various GUI (X windows) applications and utilities.
• Office application software.
• Software development tools and compilers.
• Thousands of ready to use application software packages.
• Linux Installation programs/scripts.
• Linux post installation management tools daily work such as adding users, installing applications, etc.
• And, a Shell to glue everything together.
Corporate and small businesses users need support while running Linux, so companies such as Red Hat or Novell
provide Linux tech-support and sell it as product. Nevertheless, community driven Linux distributions do exist such
as Debian, Gentoo and they are entirely free. There are over 200+ Linux distributions.

Monday, 12 September 2016

Linux Logical Volume Management

Logical Volume Manager

What is LVM:-

LVM stands for Logical Volume Manager and is a way of grouping disk drives and/or partition in a way where instead of dealing with hard and fast physical partitions the data is managed in a virtual basis where the virtual partitions can be resized.
By using LVM, all physical disk or partitions, no matter what size, how scattered they are, cab be abstracted and viewed as a single storage source.
LVM is a method of grouping of multiple disks or partitions to create logical representation of one disk to os.
LVM adds a logical layer between the physical disk (this can be a real disk, RAID array, or something else) and the filesystem. This logical layer (or a volume) then allows you to dynamically resize the logical volume (if supported by the filesystem), transparently move the filesystems to a different physical location, create read/write snapshots of a filesystem and spread your data over multiple physical locations.

Note:- There is one limitation that a LVM cannot be used for the /boot.

LVM organization:-
LVM is structured into three elements:
a) Volumes:- Physical and logical volumes & volume groups.
b) Extents:- Physical and logical extents.
c) Device-mapper:- Device mapper is modular linux kernel driver. It provides generic framework to map
one block device to other.
Device-mapper:-
The uapplication interface to the Device Mapper is the ioctl system call
The user interface is the dmsetup command.
The kernel space consists of ioctl interface, device mapper. Ioctl are interface that is used for
communication between the kernel space and user space. Device Mapper is a generic de vice
mapping facility that is used to map lower level hard disk with Logical Volume Management or
LVM.

Role of device-mapper:-
The role of device mapper is to maintain a table which contains complete information of all the
devices on which LVM is formed.
It stores all this information in a structure known as dm-table.
As the logical volume is created the device mapper maps and store the device information
in its table known as dm table. It is linear table which store all the information such as
begin, length and major and minor number which is required to indentify the device.
It contain another structure dm-target which hold the logical begin offset and the length of a particular device which are used to map the data from the user space to the hardware level.
LVM Anatomy:-
Physical Volume:- A physical volume (PV) is another name for a regular physical disk or partition that is used or will be used by LVM. To use device as LVM logical volume it must be initialized as PV,
initializing a block device as a physical volume places a label near the start of the device. By default, the LVM label is placed in the second 512-byte sector. You can overwrite this default by placing the label on any of the first 4 sectors. This allows LVM volumes to co-exist with other users of these sectors, if necessary.
The LVM label identifies the device as an LVM physical volume. It contains a random unique identifier (the UUID) for the physical volume. It also stores the size of the block device in bytes, and it records where the LVM metadata will be stored on the device.
The LVM metadata contains the configuration details of the LVM volume groups on your system. By default, an identical copy of the metadata is maintained in every metadata area in every physical volume within the volume group. LVM metadata is small and stored as ASCII.
Currently LVM allows you to store 0, 1 or 2 identical copies of its metadata on each physical volume. The default is 1 copy. Once you configure the number of metadata copies on the physical volume, you cannot change that number at a later time. The first copy is stored at the start of the device, shortly after the label. If there is a second copy, it is placed at the end of the device. If you accidentally overwrite the area at the beginning of your disk by writing to a different disk than you intend, a second copy of the metadata at the end of the device will allow you to recover the metadata.



It iscreated by pvcreate command.

Volume group:- Volume group is aggregation of Physical volumes. Volume group is created when atleast one PV is added to it. The space from all PV in VG is summed up to from large pool of storage, which is then used to build one or more logical volume(LV), the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated, Within a physical volume, extents are referred to as physical extents.
The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.

Physical Extent:- When a volume group is created, the physical volume added to it is divided into chunks of data called physical extents (Pes). An extent is the smallest unit of space that can be allocated, Within a physical volume, extents are referred to as physical extents.

Note: -
The physical extent size (PE size) of a LVM volume group (VG) is fixed upon the creation of VG. In Linux command line, the -s option switch of vgcreate command is to explicitly set the physical extent size (PE size) on physical volumes (PV) of the volume group (VG). PE size is defaulted to 4MB if it’s not set explicitly. However, once this value has been set, it’s not possible to change a PE size without recreating the volume group which would involve backing up and restoring data on any logical volumes!
What’s the maximum size of a logical volume in Linux LVM?
Why there is a limitation on maximum size of logical volume?
In order to limit the Linux kernel memory usage, there is a limit of 65,536 physical extents (PE) per logical volume (LV). Hence,
the LVM PE size will directly determine the maximum size of a logical volume (LV)! For example, 4MB PE size (the default PE size) will limit single logical volume (LV) to 256GB, 16MB PE size will limit single LV to grow beyond 1TB, etc.

Beside the PE size, the maximum size of single LV is also limited by CPU architecture and Linux kernel version:
  • Linux kernel version 2.4.x limit the maximum LV size to 2TB.
  • Some older Linux kernel prior to 2.4.x, the maximum LV size is limited to 1TB (caused by the integer signedness problems in the block layer).
  • The combination of 32-bit CPU and Linux kernel version 2.6.x, the limit of logical volume size is maximized at 16TB.
  • For Linux kernel 2.6.x running on 64-bit CPU, the maximum LV size is 8EB (extremely terrible big storage for this time being!)
Logical Volume:- The Logical Volume is logical partition created from disk space assigned to the Volume Group. There are three types of LVM logical volumes: linear volumes, striped volumes, and mirrored volumes.
LV can be increased or decreased in size and can use space taken from several PV inside VG.

Logical Extent:- Each logical volume is split into chunks of disk space, known as logical extents. The extent size is the same for all logical volumes in the volume group. LE points to the PE.
The PE and LE size s are usually kept same within VG. LE can be however smaller or larger than PE. The default LE size is 4Mb.

Physical Volume Administration:-

a) Initializing Physical Volumes
Use the pvcreate command to initialize a block device to be used as a physical volume. Initialization is analogous to formatting a file system.
The following command initializes /dev/sdd1, /dev/sde1, and /dev/sdf1 for use as LVM physical volumes.
pvcreate /dev/sdd1 /dev/sde1 /dev/sdf1
The following command initializes /dev/sdd1, /dev/sde1, and /dev/sdf1 and creates two metadata copies for use as LVM physical volumes
pvcreate –metadatacopies 2 /dev/sdd1 /dev/sde1 /dev/sdf1
b)Displaying Physical Volumes :-
There are three commands you can use to display properties of LVM physical volumes: pvs, pvdisplay, and pvscan.
The pvs command provides physical volume information in a configurable form, displaying one line per physical volume. The pvs command provides a great deal of format control, and is useful for scripting. For information on using the pvs command to customize your output.
The pvdisplay command provides a verbose multi-line output for each physical volume. It displays physical properties (size, extents, volume group, etc.) in a fixed format.
The following example shows the output of the pvdisplay command for a single physical volume.
# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               new_vg
  PV Size               17.14 GB / not usable 3.40 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              4388
  Free PE               4375
  Allocated PE          13
  PV UUID               Joqlch-yWSj-kuEn-IdwM-01S9-XO8M-mcpsVe
The pvscan command scans all supported LVM block devices in the system for physical volumes.
The following command shows all physical devices found:
# pvscan
 PV /dev/sdb2   VG vg0   lvm2 [964.00 MB / 0   free]
 PV /dev/sdc1   VG vg0   lvm2 [964.00 MB / 428.00 MB free]
 PV /dev/sdc2            lvm2 [964.84 MB]
 Total: 3 [2.83 GB] / in use: 2 [1.88 GB] / in no VG: 1 [964.84 MB]
You can define a filter in the lvm.conf so that this command will avoid scanning specific physical volumes. For information on using filters to control which devices are scanned

c)Scanning for Block Devices:- You can scan for block devices that may be used as physical volumes with the lvmdiskscan command
d) Preventing Allocation on a Physical Volume
You can prevent allocation of physical extents on the free space of one or more physical volumes with the pvchange command. This may be necessary if there are disk errors, or if you will be removing the physical volume.
The following command disallows the allocation of physical extents on /dev/sdk1.
pvchange -x n /dev/sdk1
You can also use the -xy arguments of the pvchange command to allow allocation where it had previously been disallowed.
e) Removing Physical Volumes:-
If a device is no longer required for use by LVM, you can remove the LVM label with the pvremove command. Executing the pvremove command zeroes the LVM metadata on an empty physical volume.
If the physical volume you want to remove is currently part of a volume group, you must remove it from the volume group with the vgreduce command
pvremove /dev/ram15
  Labels on physical volume "/dev/ram15" successfully wiped

Volume Group Administration:-

a) Creating Volume Groups:- To create a volume group from one or more physical volumes, use the vgcreate command. The vgcreate command creates a new volume group by name and adds at least one physical volume to it.

The following command creates a volume group named vg1 that contains physical volumes /dev/sdd1 and /dev/sde1.



vgcreate vg1 /dev/sdd1 /dev/sde1
When physical volumes are used to create a volume group, its disk space is divided into 4MB extents, by default. This extent is the minimum amount by which the logical volume may be increased or decreased in size. Large numbers of extents will have no impact on I/O performance of the logical volume.

vgcreate -s 16m vg1 /dev/sdd1 /dev/sde1
You can put limits on the number of physical or logical volumes the volume group can have by using the -p and -l arguments of the vgcreate command.
By default, a volume group allocates physical extents according to common-sense rules such as not placing parallel stripes on the same physical volume. This is the normal allocation policy. You can use the --alloc argument of the vgcreate command to specify an allocation policy of contiguous, anywhere, or cling.
The contiguous policy requires that new extents are adjacent to existing extents. If there are sufficient free extents to satisfy an allocation request but a normal allocation policy would not use them, the anywhere allocation policy will, even if that reduces performance by placing two stripes on the same physical volume. The cling policy places new extents on the same physical volume as existing extents in the same stripe of the logical volume. These policies can be changed using the vgchange command.
In general, allocation policies other than normal are required only in special cases where you need to specify unusual or nonstandard extent allocation.
LVM volume groups and underlying logical volumes are included in the device special file directory tree in the /dev directory with the following layout:
/dev/vg/lv/
b) Adding Physical Volumes to a Volume Group:- To add additional physical volumes to an existing volume group, use the vgextend command. The vgextend command increases a volume group's capacity by adding one or more free physical volumes.
The following command adds the physical volume /dev/sdf1 to the volume group vg1.
vgextend vg1 /dev/sdf
c) Displaying Volume Groups
There are two commands you can use to display properties of LVM volume groups: vgs and vgdisplay.
The vgscan command, which scans all the disks for volume groups and rebuilds the LVM cache file, also displays the volume groups. For information on the vgscan command.
The vgs command provides volume group information in a configurable form, displaying one line per volume group. The vgs command provides a great deal of format control, and is useful for scripting. For information on using the vgs command to customize your output.
The vgdisplay command displays volume group properties (such as size, extents, number of physical volumes, etc.) in a fixed form. The following example shows the output of a vgdisplay command for the volume group new_vg. If you do not specify a volume group, all existing volume groups are displayed.
# vgdisplay new_vg
  --- Volume group ---
  VG Name               new_vg
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               51.42 GB
  PE Size               4.00 MB
  Total PE              13164
  Alloc PE / Size       13 / 52.00 MB
  Free  PE / Size       13151 / 51.37 GB
  VG UUID               jxQJ0a-ZKk0-OpMO-0118-nlwO-wwqd-fD5D32

d) Scanning Disks for Volume Groups to Build the Cache File:-
The vgscan command scans all supported disk devices in the system looking for LVM physical volumes and volume groups. This builds the LVM cache in the /etc/lvm/.cache file, which maintains a listing of current LVM devices.
LVM runs the vgscan command automatically at system startup and at other times during LVM operation, such as when you execute a vgcreate command or when LVM detects an inconsistency. \

Note: -You may need to run the vgscan command manually when you change your hardware configuration and add or delete a device from a node, causing new devices to be visible to the system that were not present at system bootup. This may be necessary, for example, when you add new disks to the system on a SAN or hotplug a new disk that has been labeled as a physical volume.
vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "new_vg" using metadata type lvm2
  Found volume group "officevg" using metadata type lvm2
e)Removing Physical Volumes from a Volume Group
To remove unused physical volumes from a volume group, use the vgreduce command. The vgreduce command shrinks a volume group's capacity by removing one or more empty physical volumes.
If PV has some PE allocated to LV then will have to migrate data to another PV.
Ex: pvmove /dev/sdb1 /dev/sdc1
vgreduce /dev/sdb1

Before removing a physical volume from a volume group, you can make sure that the physical volume is not used by any logical volumes by using the pvdisplay command.
# pvdisplay /dev/hda1

-- Physical volume ---
PV Name               /dev/hda1
VG Name               myvg
PV Size               1.95 GB / NOT usable 4 MB [LVM: 122 KB]
PV#                   1
PV Status             available
Allocatable           yes (but full)
Cur LV                1
PE Size (KByte)       4096
Total PE              499
Free PE               0
Allocated PE          499
PV UUID               Sd44tK-9IRw-SrMC-MOkn-76iP-iftz-OVSen7
If the physical volume is still being used you will have to migrate the data to another physical volume using the pvmove command. Then use the vgreduce command to remove the physical volume:
The following command removes the physical volume /dev/sda1 from the volume group my_volume_group.
vgreduce my_volume_group /dev/sda1

f)Changing the Parameters of a Volume Group:-
The vgchange command is used to deactivate and activate volume groups, as described in You can also use this command to change several volume group parameters for an existing volume group.
The following command changes the maximum number of logical volumes of volume group vg00 to 128.
vgchange -l 128 /dev/vg00
The following example deactivates the volume group my_volume_group.
vgchange -a n my_volume_group
For a description of the volume group parameters you can change with the vgchange command, see the vgchange(8) man page.

g) Removing Volume Groups:-
To remove a volume group that contains no logical volumes, use the vgremove command.
# vgremove officevg
  Volume group "officevg" successfully removed
h)Splitting a Volume Group :-
To split the physical volumes of a volume group and create a new volume group, use the vgsplit command.
Logical volumes cannot be split between volume groups. Each existing logical volume must be entirely on the physical volumes forming either the old or the new volume group. If necessary, however, you can use the pvmove command to force the split.
The following example splits off the new volume group smallvg from the original volume group bigvg.
# vgsplit bigvg smallvg /dev/ram15
  Volume group "smallvg" successfully split from "bigvg"
Note:- If LV is mounted, then unmout LV, deactivate both first LV and then VG

I) Combining Volume Groups:-

Two combine two volume groups into a single volume group, use the vgmerge command. You can merge an inactive "source" volume with an active or an inactive "destination" volume if the physical extent sizes of the volume are equal and the physical and logical volume summaries of both volume groups fit into the destination volume groups limits.
The following command merges the inactive volume group my_vg into the active or inactive volume group databases giving verbose runtime information.
vgmerge -v databases my_vg
j) Backing Up Volume Group Metadata :-
Metadata backups and archives are automatically created on every volume group and logical volume configuration change unless disabled in the lvm.conf file. By default, the metadata backup is stored in the /etc/lvm/backup file and the metadata archives are stored in the /etc/lvm/archives file. You can manually back up the metadata to the /etc/lvm/backup file with the vgcfgbackup
command.
Ex: vgcfgbackup or
vgcfgbackup volume_gruoup_name
The vgcfrestore command restores the metadata of a volume group from the archive to all the physical volumes in the volume groups.
k) Renaming a Volume Group :- 
Use the vgrename command to rename an existing volume group.
Either of the following commands renames the existing volume group vg02 to my_volume_group
vgrename /dev/vg02 /dev/my_volume_group
vgrename vg02 my_volume_group
l) Moving a Volume Group to Another System
You can move an entire LVM volume group to another system. It is recommended that you use the vgexport and vgimport commands when you do this.
The vgexport command makes an inactive volume group inaccessible to the system, which allows you to detach its physical volumes. The vgimport command makes a volume group accessible to a machine again after the vgexport command has made it inactive.
To move a volume group form one system to another, perform the following steps:
  1. Make sure that no users are accessing files on the active volumes in the volume group, then unmount the logical volumes.
  2. Use the -a n argument of the vgchange -a n command to mark the volume group as inactive, which prevents any further activity on the volume group.
  3. Use the vgexport vg_name command to export the volume group. This prevents it from being accessed by the system from which you are removing it.
After you export the volume group, the physical volume will show up as being in an exported volume group when you execute the pvscan command, as in the following example.
[root@tng3-1]# pvscan
  PV /dev/sda1    is in exported VG myvg [17.15 GB / 7.15 GB free]
  PV /dev/sdc1    is in exported VG myvg [17.15 GB / 15.15 GB free]
  PV /dev/sdd1   is in exported VG myvg [17.15 GB / 15.15 GB free]
  ...
When the system is next shut down, you can unplug the disks that constitute the volume group and connect them to the new system.
  1. When the disks are plugged into the new system, use the vgimport command to import the volume group, making it accessible to the new system.
  2. Activate the volume group with the -a y argument of the vgchange command.
  3. Mount the file system to make it available for use.
m) Recreating a Volume Group Directory :-
To recreate a volume group directory and logical volume special files, use the vgmknodes command. This command checks the LVM2 special files in the /dev directory that are needed for active logical volumes. It creates any special files that are missing removes unused ones.
You can incorporate the vgmknodes command into the vgscan command by specifying the mknodes argument to the vgscan command.

Logical Volume Administration.

Creating Logical Volumes :-

To create a logical volume, use the lvcreate command. You can create linear volumes, striped volumes, and mirrored volumes, as described in the following subsections.
If you do not specify a name for the logical volume, the default name lvol# is used where # is the internal number of the logical volume.
The following sections provide examples of logical volume creation for the three types of logical volumes you can create with LVM.

Note : - The allocation policy to use: contiguous, cling, normal, anywhere or inherit. When a command needs to allocate physical extents from the volume group, the allocation policy controls how they are chosen. Each volume group and logical volume has an allocation policy. The default for a
volume group is normal which applies common-sense rules such as not placing parallel stripes on
the same physical volume. The default for a logical volume is inherit which applies the same pol-
icy as for the volume group. These policies can be changed using lvchange (8) and vgchange (8) or
over-ridden on the command line of any command that performs allocation. The contiguous policy
requires that new extents be placed adjacent to existing extents. The cling policy places new
extents on the same physical volume as existing extents in the same stripe of the Logical Volume.
If there are sufficient free extents to satisfy an allocation request but normal doesn’t use them,
anywhere will - even if that reduces performance by placing two stripes on the same physical volume.


a) Creating Linear Volumes :-

When you create a logical volume, the logical volume is carved from a volume group using the free extents on the physical volumes that make up the volume group. Normally logical volumes use up any space available on the underlying physical volumes on a next-free basis. Modifying the logical volume frees and reallocates space in the physical volumes.
The following command creates a logical volume 10 gigabytes in size in the volume group vg1.
lvcreate -L 10G vg1
The following command creates a 1500 megabyte linear logical volume named testlv in the volume group testvg, creating the block device /dev/testvg/testlv.
lvcreate -L1500 -n testlv testvg
The following command creates a 50 gigabyte logical volume named gfslv from the free extents in volume group vg0.
lvcreate -L 50G -n gfslv vg0
You can use the -l argument of the lvcreate command to specify the size of the logical volume in extents. You can also use this argument to specify the percentage of the volume group to use for the logical volume. The following command creates a logical volume called mylv that uses 60% of the total space in volume group testvol.
lvcreate -l 60%VG -n mylv testvg
You can also use the -l argument of the lvcreate command to specify the percentage of the remaining free space in a volume group as the size of the logical volume. The following command creates a logical volume called yourlv that uses all of the unallocated space in the volume group testvol.
lvcreate -l 100%FREE -n yourlv testvg
You can use -l argument of the lvcreate command to create a logical volume that uses the entire volume group. Another way to create a logical volume that uses the entire volume group is to use the vgdisplay command to find the "Total PE" size and to use those results as input to the the lvcreate command.
The following commands create a logical volume called mylv that fills the volume group named testvg.
# vgdisplay testvg | grep "Total PE"
Total PE              10230
# lvcreate -l 10230 testvg -n mylv
The underlying physical volumes used to create a logical volume can be important if the physical volume needs to be removed, so you may need to consider this possibility when you create the logical volume. For information on removing a physical volume from a volume group.
To create a logical volume to be allocated from a specific physical volume in the volume group, specify the physical volume or volumes at the end at the lvcreate command line. The following command creates a logical volume named testlv in volume group testvg allocated from the physical volume /dev/sdg1,
lvcreate -L 1500 -ntestlv testvg /dev/sdg1
You can specify which extents of a physical volume are to be used for a logical volume. The following example creates a linear logical volume out of extents 0 through 25 of physical volume /dev/sda1 and extents 50 through 125 of physical volume /dev/sdb1 in volume group testvg.
lvcreate -l 100 -n testlv testvg /dev/sda1:0-25 /dev/sdb1:50-125
The following example creates a linear logical volume out of extents 0 through 25 of physical volume /dev/sda1 and then continues laying out the logical volume at extent 100.
lvcreate -l 100 -n testlv testvg /dev/sda1:0-25:100-
The default policy for how the extents of a logical volume are allocated is inherit, which applies the same policy as for the volume group. These policies can be changed using the lvchange command. For information on allocation policies

b) Creating Striped Volumes:-
For large sequential reads and writes, creating a striped logical volume can improve the efficiency of the data I/O. For general information about striped volumes. When you create a striped logical volume, you specify the number of stripes with the -i argument of the lvcreate command. This determines over how many physical volumes the logical volume will be striped. The number of stripes cannot be greater than the number of physical volumes in the volume group (unless the --alloc anywhere argument is used).
If the underlying physical devices that make up a striped logical volume are different sizes, the maximum size of the striped volume is determined by the smallest underlying device. For example, in a two-legged stripe, the maximum size is twice the size of the smaller device. In a three-legged stripe, the maximum size is three times the size of the smallest device.
The following command creates a striped logical volume across 2 physical volumes with a stripe of 64kB. The logical volume is 50 gigabytes in size, is named gfslv, and is carved out of volume group vg0.
lvcreate -L 50G -i2 -I64 -n gfslv vg0
As with linear volumes, you can specify the extents of the physical volume that you are using for the stripe. The following command creates a striped volume 100 extents in size that stripes across two physical volumes, is named stripelv and is in volume group testvg. The stripe will use sectors 0-50 of /dev/sda1 and sectors 50-100 of /dev/sdb1.
# lvcreate -l 100 -i2 -nstripelv testvg /dev/sda1:0-50 /dev/sdb1:50-100
  Using default stripesize 64.00 KB
  Logical volume "stripelv" created
To view how stripe is created use this command
lvs -a -o +stripes,stripesize,devices

LV VG Attr LSize Origin Snap% Move Log Copy% #Str Stripe Devices

pav vada -wi-ao 484.00M 1 0 /dev/sda5(0)

vol1 vg -wi-a- 640.00M 2 64.00K /dev/sdb1(0),/dev/sdc1(0)

vol2 vg -wi-a- 192.00M 2 64.00K /dev/sdb1(20),/dev/sdc1(20)

c) Creating Mirrored Volumes :-
When you create a mirrored volume, you specify the number of copies of the data to make with the -m argument of the lvcreate command. Specifying -m1 creates one mirror, which yields two copies of the file system: a linear logical volume plus one copy. Similarly, specifying -m2 creates two mirrors, yielding three copies of the file system.
The following command creates a mirrored logical volume with a single mirror. The volume is 50 gigabytes in size, is named mirrorlv, and is carved out of volume group vg0:
lvcreate -L 50G -m1 -n mirrorlv vg0
An LVM mirror divides the device being copied into regions that, by default, are 512KB in size. You can use the -R argument of the lvcreate command to specify the region size in MB. You can also change the default region size by editing the mirror_region_size setting in the lvm.conf file.

Note :-

Due to limitations in the cluster infrastructure, cluster mirrors greater than 1.5TB cannot be created with the default region size of 512KB. Users that require larger mirrors should increase the region size from its default to something larger. Failure to increase the region size will cause LVM creation to hang and may hang other LVM commands as well.
As a general guideline for specifying the region size for mirrors that are larger than 1.5TB, you could take your mirror size in terabytes and round up that number to the next power of 2, using that number as the -R argument to the lvcreate command. For example, if your mirror size is 1.5TB, you could specify -R 2. If your mirror size is 3TB, you could specify -R 4. For a mirror size of 5TB, you could specify -R 8.
The following command creates a mirrored logical volume with a region size of 2MB:
lvcreate -m1 -L 2T -R 2 -n mirror vol_group
LVM maintains a small log which it uses to keep track of which regions are in sync with the mirror or mirrors. By default, this log is kept on disk, which keeps it persistent across reboots. You can specify instead that this log be kept in memory with the --corelog argument; this eliminates the need for an extra log device, but it requires that the entire mirror be resynchronized at every reboot.
The following command creates a mirrored logical volume from the volume group bigvg. The logical is named ondiskmirvol and has a single mirror. The volume is 12MB in size and keeps the mirror log in memory.
# lvcreate -L 12MB -m1 --corelog -n ondiskmirvol bigvg
  Logical volume "ondiskmirvol" created
The mirror log is created on a separate device from the devices on which any of the mirror legs are created. It is possible, however, to create the mirror log on the same device as one of the mirror legs by using the --alloc anywhere argument of the vgcreate command. This may degrade performance, but it allows you to create a mirror even if you have only two underlying devices.
The following command creates a mirrored logical volume with a single mirror for which the mirror log is on the same device as one of the mirror legs. In this example, the volume group vg0 consists of only two devices. This command creates a 500 megabyte volume named mirrorlv in the vg0 volume group.
lvcreate -L 500M -m1 -n mirrorlv -alloc anywhere vg0
When a mirror is created, the mirror regions are synchronized. For large mirror components, the sync process may take a long time. When you are creating a new mirror that does not need to be revived, you can specify the nosync argument to indicate that an initial synchronization from the first device is not required.
You can specify which devices to use for the mirror logs and log, and which extents of the devices to use. To force the log onto a particular disk, specify exactly one extent on the disk on which it will be placed. LVM does not necessary respect the order in which devices are listed in the command line. If any physical volumes are listed that is the only space on which allocation will take place. Any physical extents included in the list that are already allocated will get ignored.
The following command creates a mirrored logical volume with a single mirror. The volume is 500 megabytes in size, it is named mirrorlv, and it is carved out of volume group vg0. The first leg of the mirror is on device /dev/sda1, the second leg of the mirror is on device /dev/sdb1, and the mirror log is on /dev/sdc1.
lvcreate -L 500M -m1 -n mirrorlv vg0 /dev/sda1 /dev/sdb1 /dev/sdc1
The following command creates a mirrored logical volume with a single mirror. The volume is 500 megabytes in size, it is named mirrorlv, and it is carved out of volume group vg0. The first leg of the mirror is on extents 0 through 499 of device /dev/sda1, the second leg of the mirror is on extents 0 through 499 of device /dev/sdb1, and the mirror log starts on extent 0 of device /dev/sdc1. These are 1MB extents. If any of the specified extents have already been allocated, they will be ignored.
lvcreate -L 500M -m1 -n mirrorlv vg0 /dev/sda1:0-499 /dev/sdb1:0-499 /dev/sdc1:0
d)Changing Mirrored Volume Configuration:-
You can convert a logical volume from a mirrored volume to a linear volume or from a linear volume to a mirrored volume with the lvconvert command. You can also use this command to reconfigure other mirror parameters of an existing logical volume, such as corelog.
When you convert a logical volume to a mirrored volume, you are basically creating mirror legs for an existing volume. This means that your volume group must contain the devices and space for the mirror legs and for the mirror log.
If you lose a leg of a mirror, LVM converts the volume to a linear volume so that you still have access to the volume, without the mirror redundancy. After you replace the leg, you can use the lvconvert command to restore the mirror.
The following command converts the linear logical volume vg00/lvol1 to a mirrored logical volume.
lvconvert -m1 vg00/lvol1
The following command converts the mirrored logical volume vg00/lvol1 to a linear logical volume, removing the mirror leg.
lvconvert -m0 vg00/lvol1
e)Persistent Device Numbers:- 
Major and minor device numbers are allocated dynamically at module load. Some applications work best if the block device always is activated with the same device (major and minor) number. You can specify these with the lvcreate and the lvchange commands by using the following arguments:
--persistent y --major major --minor minor
Use a large minor number to be sure that it hasn't already been allocated to another device dynamically.
If you are exporting a file system using NFS, specifying the fsid parameter in the exports file may avoid the need to set a persistent device number within LVM.
f) Changing the Parameters of a Logical Volume Group:-
To change the parameters of a logical volume, use the lvchange command. For a listing of the parameters you can change, see the lvchange(8) man page.
You can use the lvchange command to activate and deactivate logical volumes. To activate and deactivate all the logical volumes in a volume group at the same time, use the vgchange command.
The following command changes the permission on volume lvol1 in volume group vg00 to be read-only.
lvchange -pr vg00/lvol1
g) Renaming Logical Volumes:-
To rename an existing logical volume, use the lvrename command.
Either of the following commands renames logical volume lvold in volume group vg02 to lvnew.
lvrename /dev/vg02/lvold /dev/vg02/lvnew
lvrename vg02 lvold lvnew
h) Removing Logical Volumes:-
To remove an inactive logical volume, use the lvremove command. If the logical volume is currently mounted, unmount the volume before removing it. In addition, in a clustered environment you must deactivate a logical volume before it can be removed.
The following command removes the logical volume /dev/testvg/testlv. from the volume group testvg. Note that in this case the logical volume has not been deactivated.
[root@tng3-1 lvm]# lvremove /dev/testvg/testlv
Do you really want to remove active logical volume "testlv"? [y/n]: y
  Logical volume "testlv" successfully removed
You could explicitly deactivate the logical volume before removing it with the lvchange -an command, in which case you would not see the prompt verifying whether you want to remove an active logical volume.

i) Growing Logical Volumes:-
To increase the size of a logical volume, use the lvextend command.
After extending the logical volume, you will need to increase the size of the associated file system to match.
When you extend the logical volume, you can indicate how much you want to extend the volume, or how large you want it to be after you extend it.
The following command extends the logical volume /dev/myvg/homevol to 12 gigabytes.
# lvextend -L12G /dev/myvg/homevol 
lvextend -- extending logical volume "/dev/myvg/homevol" to 12 GB
lvextend -- doing automatic backup of volume group "myvg"
lvextend -- logical volume "/dev/myvg/homevol" successfully extended
The following command adds another gigabyte to the logical volume /dev/myvg/homevol.
# lvextend -L+1G /dev/myvg/homevol
lvextend -- extending logical volume "/dev/myvg/homevol" to 13 GB
lvextend -- doing automatic backup of volume group "myvg"
lvextend -- logical volume "/dev/myvg/homevol" successfully extended
As with the lvcreate command, you can use the -l argument of the lvextend command to specify the number of extents by which to increase the size of the logical volume. You can also use this argument to specify a percentage of the volume group, or a percentage of the remaining free space in the volume group. The following command extends the logical volume called testlv to fill all of the unallocated space in the volume group myvg.
[root@tng3-1 ~]# lvextend -l +100%FREE /dev/myvg/testlv
  Extending logical volume testlv to 68.59 GB
  Logical volume testlv successfully resized
After you have extended the logical volume it is necessary to increase the file system size to match.
By default, most file system resizing tools will increase the size of the file system to be the size of the underlying logical volume so you do not need to worry about specifying the same size for each of the two commands.

j) Extending a Striped Volume:-
In order to increase the size of a striped logical volume, there must be enough free space on the underlying physical volumes that make up the volume group to support the stripe. For example, if you have a two-way stripe that that uses up an entire volume group, adding a single physical volume to the volume group will not enable you to extend the stripe. Instead, you must add at least two physical volumes to the volume group.
For example, consider a volume group vg that consists of two underlying physical volumes, as displayed with the following vgs command.
# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg     2   0   0 wz--n- 271.31G 271.31G
You can create a stripe using the entire amount of space in the volume group.
# lvcreate -n stripe1 -L 271.31G -i 2 vg
  Using default stripesize 64.00 KB
  Rounding up size to full physical extent 271.31 GB
  Logical volume "stripe1" created
# lvs -a -o +devices
  LV      VG   Attr   LSize   Origin Snap%  Move Log Copy%  Devices
  stripe1 vg   -wi-a- 271.31G                               /dev/sda1(0),/dev/sdb1(0)
Note that the volume group now has no more free space.
# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg     2   1   0 wz--n- 271.31G    0
The following command adds another physical volume to the volume group, which then has 135G of additional space.
# vgextend vg /dev/sdc1
  Volume group "vg" successfully extended
# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg     3   1   0 wz--n- 406.97G 135.66G
At this point you cannot extend the striped logical volume to the full size of the volume group, because two underlying devices are needed in order to stripe the data.
# lvextend vg/stripe1 -L 406G
  Using stripesize of last segment 64.00 KB
  Extending logical volume stripe1 to 406.00 GB
  Insufficient suitable allocatable extents for logical volume stripe1: 34480 
more required
To extend the striped logical volume, add another physical volume and then extend the logical volume. In this example, having added two physical volumes to the volume group we can extend the logical volume to the full size of the volume group.
# vgextend vg /dev/sdd1
  Volume group "vg" successfully extended
# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  vg     4   1   0 wz--n- 542.62G 271.31G
# lvextend vg/stripe1 -L 542G
  Using stripesize of last segment 64.00 KB
  Extending logical volume stripe1 to 542.00 GB
  Logical volume stripe1 successfully resized
If you do not have enough underlying physical devices to extend the striped logical volume, it is possible to extend the volume anyway if it does not matter that the extension is not striped, which may result in uneven performance. When adding space to the logical volume, the default operation is to use the same striping parameters of the last segment of the existing logical volume, but you can override those parameters. The following example extends the existing striped logical volume to use the remaining free space after the initial lvextend command fails.
# lvextend vg/stripe1 -L 406G
  Using stripesize of last segment 64.00 KB
  Extending logical volume stripe1 to 406.00 GB
  Insufficient suitable allocatable extents for logical volume stripe1: 34480 
more required
# lvextend -i1 -l+100%FREE vg/stripe1
k) Shrinking Logical Volumes:-
To reduce the size of a logical volume, first unmount the file system. You can then use the lvreduce command to shrink the volume. After shrinking the volume, remount the file system.
The following example reduces the size of logical volume lvol1 in volume group vg00 by 3 logical extents.
lvreduce -l -3 vg00/lvol1

Creating Snapshot Volumes :-

Use the -s argument of the lvcreate command to create a snapshot volume. A snapshot volume is writeable.

Note :-

LVM snapshots are not supported across the nodes in a cluster. You cannot create a snapshot volume in a clustered volume group.
The following command creates a snapshot logical volume that is 100 megabytes in size named /dev/vg00/snap. This creates a snapshot of the origin logical volume named /dev/vg00/lvol1. If the original logical volume contains a file system, you can mount the snapshot logical volume on an arbitrary directory in order to access the contents of the file system to run a backup while the original file system continues to get updated.
lvcreate --size 100M --snapshot --name snap /dev/vg00/lvol1
After you create a snapshot logical volume, specifying the origin volume on the lvdisplay command yields output that includes a a list of all snapshot logical volumes and their status (active or inactive).
The following example shows the status of the logical volume /dev/new_vg/lvol0, for which a snapshot volume /dev/new_vg/newvgsnap has been created.
# lvdisplay /dev/new_vg/lvol0
  --- Logical volume ---
  LV Name                /dev/new_vg/lvol0
  VG Name                new_vg
  LV UUID                LBy1Tz-sr23-OjsI-LT03-nHLC-y8XW-EhCl78
  LV Write Access        read/write
  LV snapshot status     source of
                         /dev/new_vg/newvgsnap1 [active]
  LV Status              available
  # open                 0
  LV Size                52.00 MB
  Current LE             13
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:2
The lvs command, by default, displays the origin volume and the current percentage of the snapshot volume being used for each snapshot volume. The following example shows the default output for the lvs command for a system that includes the logical volume /dev/new_vg/lvol0, for which a snapshot volume /dev/new_vg/newvgsnap has been created.
# lvs
  LV         VG     Attr   LSize  Origin Snap%  Move Log Copy%
  lvol0      new_vg owi-a- 52.00M
  newvgsnap1 new_vg swi-a-  8.00M lvol0    0.20

Note

Because the snapshot increases in size as the origin volume changes, it is important to monitor the percentage of the snapshot volume regularly with the lvs command to be sure it does not fill. A snapshot that is 100% full is lost completely, as a write to unchanged parts of the origin would be unable to succeed without corrupting the snapshot.

Online Data Relocation : -

You can move data while the system is in use with the pvmove command.
The pvmove command breaks up the data to be moved into sections and creates a temporary mirror to move each section. For more information on the operation of the pvmove command, see the pvmove(8) man page.
Because the pvmove command uses mirroring, it is not cluster-aware and needs exclusive access to a volume.
The following command moves all allocated space off the physical volume /dev/sdc1 to other free physical volumes in the volume group:
pvmove /dev/sdc1
The following command moves just the extents of the logical volume MyLV.
pvmove -n MyLV /dev/sdc1
Since the pvmove command can take a long time to execute, you may want to run the command in the background to avoid display of progress updates in the foreground. The following command moves all extents allocated to to the physical volume /dev/sdc1 over to /dev/sdf1 in the background.
pvmove -b /dev/sdc1 /dev/sdf1
The following command reports the progress of the move as a percentage at five second intervals.
pvmove -i5 /dev/sdd1

Customized Reporting for LVM

You can produce concise and customizable reports of LVM objects with the pvs, lvs, and vgs commands. The reports that these commands generate include one line of output for each object. Each line contains an ordered list of fields of properties related to the object. There are five ways to select the objects to be reported: by physical volume, volume group, logical volume, physical volume segment, and logical volume segment.
The following sections provide:
  • A summary of command arguments you can use to control the format of the generated report.
  • A list of the fields you can select for each LVM object.
  • A summary of command arguments you can use to sort the generated report.
  • Instructions for specifying the units of the report output.


a) Format Control : -
Whether you use the pvs, lvs, or vgs command determines the default set of fields displayed and the sort order. You can control the output of these commands with the following arguments:
  • You can change what fields are displayed to something other than the default by using the -o argument. For example, the following output is the default display for the pvs command (which displays information about physcial volumes).
# pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sdb1  new_vg lvm2 a-   17.14G 17.14G
  /dev/sdc1  new_vg lvm2 a-   17.14G 17.09G
  /dev/sdd1  new_vg lvm2 a-   17.14G 17.14G
The following command displays only the physical volume name and size.
# pvs -o pv_name,pv_size
  PV         PSize
  /dev/sdb1  17.14G
  /dev/sdc1  17.14G
  /dev/sdd1  17.14G
  • You can append a field to the output with the plus sign (+), which is used in combination with the -o argument.
The following example displays the UUID of the physical volume in addition to the default fields.
# pvs -o +pv_uuid
  PV         VG     Fmt  Attr PSize  PFree  PV UUID
  /dev/sdb1  new_vg lvm2 a-   17.14G 17.14G onFF2w-1fLC-ughJ-D9eB-M7iv-6XqA-dqGeXY
  /dev/sdc1  new_vg lvm2 a-   17.14G 17.09G Joqlch-yWSj-kuEn-IdwM-01S9-X08M-mcpsVe
  /dev/sdd1  new_vg lvm2 a-   17.14G 17.14G yvfvZK-Cf31-j75k-dECm-0RZ3-0dGW-UqkCS
  • Adding the -v argument to a command includes some extra fields. For example, the pvs -v command will display the DevSize and PV UUID fields in addition to the default fields.
# pvs -v
    Scanning for physical volume names
  PV         VG     Fmt  Attr PSize  PFree  DevSize PV UUID
  /dev/sdb1  new_vg lvm2 a-   17.14G 17.14G  17.14G onFF2w-1fLC-ughJ-D9eB-M7iv-6XqA-dqGeXY
  /dev/sdc1  new_vg lvm2 a-   17.14G 17.09G  17.14G Joqlch-yWSj-kuEn-IdwM-01S9-XO8M-mcpsVe
  /dev/sdd1  new_vg lvm2 a-   17.14G 17.14G  17.14G yvfvZK-Cf31-j75k-dECm-0RZ3-0dGW-tUqkCS
  • The --noheadings argument suppresses the headings line. This can be useful for writing scripts.
The following example uses the --noheadings argument in combination with the pv_name argument, which will generate a list of all physical volumes.
# pvs --noheadings -o pv_name
  /dev/sdb1
  /dev/sdc1
  /dev/sdd1
  • The --separator separator argument uses separator to separate each field.
The following example separates the default output fields of the pvs command with an equals sign (=).
# pvs --separator =
  PV=VG=Fmt=Attr=PSize=PFree
  /dev/sdb1=new_vg=lvm2=a-=17.14G=17.14G
  /dev/sdc1=new_vg=lvm2=a-=17.14G=17.09G
  /dev/sdd1=new_vg=lvm2=a-=17.14G=17.14G
To keep the fields aligned when using the separator argument, use the separator argument in conjunction with the --aligned argument.
# pvs --separator = --aligned
  PV        =VG    =Fmt =Attr=PSize =PFree
  /dev/sdb1 =new_vg=lvm2=a-  =17.14G=17.14G
  /dev/sdc1 =new_vg=lvm2=a-  =17.14G=17.09G
  /dev/sdd1 =new_vg=lvm2=a-  =17.14G=17.14G
You can use the -P argument of the lvs or vgs command to display information about a failed volume that would otherwise not appear in the output.
For a full listing of display arguments, see the pvs(8), vgs(8) and lvs(8) man pages.
Volume group fields can be mixed with either physical volume (and physical volume segment) fields or with logical volume (and logical volume segment) fields, but physical volume and logical volume fields cannot be mixed. For example, the following command will display one line of output for each physical volume.
# vgs -o +pv_name
  VG     #PV #LV #SN Attr   VSize  VFree  PV
  new_vg   3   1   0 wz--n- 51.42G 51.37G /dev/sdc1
  new_vg   3   1   0 wz--n- 51.42G 51.37G /dev/sdd1
  new_vg   3   1   0 wz--n- 51.42G 51.37G /dev/sdb1
b) Object Selection
This section provides a series of tables that list the information you can display about the LVM objects with the pvs, vgs, and lvs commands.
For convenience, a field name prefix can be dropped if it matches the default for the command. For example, with the pvs command, name means pv_name, but with the vgs command, name is interpreted as vg_name.
Executing the following command is the equivalent of executing pvs -o pv_free.
# pvs -o +free
  PFree
  17.14G
  17.09G
  17.14G

The pvs Command

the pvs command, along with the field name as it appears in the header display and a description of the field.
Argument Header Description
dev_size DevSize The size of the underlying device on which the physical volume was created
pe_start 1st PE Offset to the start of the first physical extent in the underlying device
pv_attr Attr Status of the physical volume: (a)llocatable or e(x)ported.
pv_fmt Fmt The metadata format of the physical volume (lvm2 or lvm1)
pv_free PFree The free space remaining on the physical volume
pv_name PV The physical volume name
pv_pe_alloc_count Alloc Number of used physical extents
pv_pe_count PE Number of physical extents
pvseg_size SSize The segment size of the physical volume
pvseg_start Start The starting physical extent of the physical volume segment
pv_size PSize The size of the physical volume
pv_tags PV Tags LVM tags attached to the physical volume
pv_used Used The amount of space currently used on the physical volume
pv_uuid PV UUID The UUID of the physical volume
Table 4.1. pvs Display Fields

The pvs command displays the following fields by default: pv_name, vg_name, pv_fmt, pv_attr, pv_size, pv_free. The display is sorted by pv_name.
# pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sdb1  new_vg lvm2 a-   17.14G 17.14G
  /dev/sdc1  new_vg lvm2 a-   17.14G 17.09G
  /dev/sdd1  new_vg lvm2 a-   17.14G 17.13G
Using the -v argument with the pvs command adds the following fields to the default display: dev_size, pv_uuid.
# pvs -v
    Scanning for physical volume names
  PV         VG     Fmt  Attr PSize  PFree  DevSize PV UUID
  /dev/sdb1  new_vg lvm2 a-   17.14G 17.14G  17.14G onFF2w-1fLC-ughJ-D9eB-M7iv-6XqA-dqGeXY
  /dev/sdc1  new_vg lvm2 a-   17.14G 17.09G  17.14G Joqlch-yWSj-kuEn-IdwM-01S9-XO8M-mcpsVe
  /dev/sdd1  new_vg lvm2 a-   17.14G 17.13G  17.14G yvfvZK-Cf31-j75k-dECm-0RZ3-0dGW-tUqkCS
You can use the --segments argument of the pvs command to display information about each physical volume segment. A segment is a group of extents. A segment view can be useful if you want to see whether your logical volume is fragmented.
The pvs --segments command displays the following fields by default: pv_name, vg_name, pv_fmt, pv_attr, pv_size, pv_free, pvseg_start, pvseg_size. The display is sorted by pv_name and pvseg_size within the physical volume.
# pvs --segments
  PV         VG         Fmt  Attr PSize  PFree  Start SSize
  /dev/hda2  VolGroup00 lvm2 a-   37.16G 32.00M     0  1172
  /dev/hda2  VolGroup00 lvm2 a-   37.16G 32.00M  1172    16
  /dev/hda2  VolGroup00 lvm2 a-   37.16G 32.00M  1188     1
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G     0    26
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G    26    24
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G    50    26
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G    76    24
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G   100    26
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G   126    24
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G   150    22
  /dev/sda1  vg         lvm2 a-   17.14G 16.75G   172  4217
  /dev/sdb1  vg         lvm2 a-   17.14G 17.14G     0  4389
  /dev/sdc1  vg         lvm2 a-   17.14G 17.14G     0  4389
  /dev/sdd1  vg         lvm2 a-   17.14G 17.14G     0  4389
  /dev/sde1  vg         lvm2 a-   17.14G 17.14G     0  4389
  /dev/sdf1  vg         lvm2 a-   17.14G 17.14G     0  4389
  /dev/sdg1  vg         lvm2 a-   17.14G 17.14G     0  4389
You can use the pvs -a command to see devices detected by LVM that have not been initialized as LVM physical volumes.
# pvs -a
  PV                             VG     Fmt  Attr PSize  PFree
  /dev/VolGroup00/LogVol01                   --       0      0
  /dev/new_vg/lvol0                          --       0      0
  /dev/ram                                   --       0      0
  /dev/ram0                                  --       0      0
  /dev/ram2                                  --       0      0
  /dev/ram3                                  --       0      0
  /dev/ram4                                  --       0      0
  /dev/ram5                                  --       0      0
  /dev/ram6                                  --       0      0
  /dev/root                                  --       0      0
  /dev/sda                                   --       0      0
  /dev/sdb                                   --       0      0
  /dev/sdb1                      new_vg lvm2 a-   17.14G 17.14G
  /dev/sdc                                   --       0      0
  /dev/sdc1                      new_vg lvm2 a-   17.14G 17.09G
  /dev/sdd                                   --       0      0
  /dev/sdd1                      new_vg lvm2 a-   17.14G 17.14G

The vgs Command

the vgs command, along with the field name as it appears in the header display and a description of the field.
Argument Header Description
lv_count #LV The number of logical volumes the volume group contains
max_lv MaxLV The maximum number of logical volumes allowed in the volume group (0 if unlimited)
max_pv MaxPV The maximum number of physical volumes allowed in the volume group (0 if unlimited)
pv_count #PV The number of physical volumes that define the volume group
snap_count #SN The number of snapshots the volume group contains
vg_attr Attr Status of the volume group: (w)riteable, (r)eadonly, resi(z)eable, e(x)ported, (p)artial and (c)lustered.
vg_extent_count #Ext The number of physical extents in the volume group
vg_extent_size Ext The size of the physical extents in the volume group
vg_fmt Fmt The metadata format of the volume group (lvm2 or lvm1)
vg_free VFree Size of the free space remaining in the volume group
vg_free_count Free Number of free physical extents in the volume group
vg_name VG The volume group name
vg_seqno Seq Number representing the revision of the volume group
vg_size VSize The size of the volume group
vg_sysid SYS ID LVM1 System ID
vg_tags VG Tags LVM tags attached to the volume group
vg_uuid VG UUID The UUID of the volume group
Table 4.2. vgs Display Fields

The vgs command displays the following fields by default: vg_name, pv_count, lv_count, snap_count, vg_attr, vg_size, vg_free. The display is sorted by vg_name.
# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  new_vg   3   1   1 wz--n- 51.42G 51.36G
Using the -v argument with the vgs command adds the following fields to the default display: vg_extent_size, vg_uuid.
# vgs -v
    Finding all volume groups
    Finding volume group "new_vg"
  VG     Attr   Ext   #PV #LV #SN VSize  VFree  VG UUID
  new_vg wz--n- 4.00M   3   1   1 51.42G 51.36G jxQJ0a-ZKk0-OpMO-0118-nlwO-wwqd-fD5D32

The lvs Command

Table 4.3, “lvs Display Fields” lists the display arguments of the lvs command, along with the field name as it appears in the header display and a description of the field.
Argument Header Description
chunksize
chunk_size

Chunk Unit size in a snapshot volume
copy_percent Copy% The synchronization percentage of a mirrored logical volume; also used when physical extents are being moved with the pv_move command
devices Devices The underlying devices that make up the logical volume: the physical volumes, logical volumes, and start physical extents and logical extents
lv_attr Attr The status of the logical volume. The logical volume attribute bits are as follows:
Bit 1: Volume type: (m)irrored, (M)irrored without initial sync, (o)rigin, (p)vmove, (s)napshot, invalid (S)napshot, (v)irtual
Bit2: Permissions: (w)riteable, (r)ead-only
Bit 3: Allocation policy: (c)ontiguous, (n)ormal, (a)nywhere, (i)nherited. This is capitalized if the volume is currently locked against allocation changes, for example while executing the pvmove command.
Bit 4: fixed (m)inor
Bit 5 State: (a)ctive, (s)uspended, (I)nvalid snapshot, invalid (S)uspended snapshot, mapped (d)evice present without tables, mapped device present with (i)nactive table
Bit 6: device (o)pen
lv_kernel_major KMaj Actual major device number of the logical volume (-1 if inactive)
lv_kernel_minor KMIN Actual minor device number of the logical volume (-1 if inactive)
lv_major Maj The persistent major device number of the logical volume (-1 if not specified)
lv_minor Min The persistent minor device number of the logical volume (-1 if not specified)
lv_name LV The name of the logical volume
lv_size LSize The size of the logical volume
lv_tags LV Tags LVM tags attached to the logical volume
lv_uuid LV UUID The UUID of the logical volume.
mirror_log Log Device on which the mirror log resides
modules Modules Corresponding kernel device-mapper target necessary to use this logical volume
move_pv Move Source physical volume of a temporary logical volume created with the pvmove command
origin Origin The origin device of a snapshot volume
regionsize
region_size

Region The unit size of a mirrored logical volume
seg_count #Seg The number of segments in the logical volume
seg_size SSize The size of the segments in the logical volume
seg_start Start Offset of the segment in the logical volume
seg_tags Seg Tags LVM tags attached to the segments of the logical volume
segtype Type The segment type of a logical volume (for example: mirror, striped, linear)
snap_percent Snap% Current percentage of a snapshot volume that is in use
stripes #Str Number of stripes or mirrors in a logical volume
stripesize
stripe_size

Stripe Unit size of the stripe in a striped logical volume
Table 4.3. lvs Display Fields

The lvs command displays the following fields by default: lv_name, vg_name, lv_attr, lv_size, origin, snap_percent, move_pv, mirror_log, copy_percent. The default display is sorted by vg_name and lv_name within the volume group.
# lvs
  LV         VG     Attr   LSize  Origin Snap%  Move Log Copy%
  lvol0      new_vg owi-a- 52.00M
  newvgsnap1 new_vg swi-a-  8.00M lvol0    0.20
Using the -v argument with the lvs command adds the following fields to the default display: seg_count, lv_major, lv_minor, lv_kernel_major, lv_kernel_minor, lv_uuid.
# lvs -v
    Finding all logical volumes
  LV         VG     #Seg Attr   LSize  Maj Min KMaj KMin Origin Snap%  Move Copy%  Log LV UUID
  lvol0      new_vg    1 owi-a- 52.00M  -1  -1 253  3                                  LBy1Tz-sr23-OjsI-LT03-nHLC-y8XW-EhCl78
  newvgsnap1 new_vg    1 swi-a-  8.00M  -1  -1 253  5    lvol0    0.20                 1ye1OU-1cIu-o79k-20h2-ZGF0-qCJm-CfbsIx
You can use the --segments argument of the lvs command to display information with default columns that emphasize the segment information. When you use the segments argument, the seg prefix is optional. The lvs --segments command displays the following fields by default: lv_name, vg_name, lv_attr, stripes, segtype, seg_size. The default display is sorted by vg_name, lv_name within the volume group, and seg_start within the logical volume. If the logical volumes were fragmented, the output from this command would show that.
# lvs --segments
  LV       VG         Attr   #Str Type   SSize
  LogVol00 VolGroup00 -wi-ao    1 linear  36.62G
  LogVol01 VolGroup00 -wi-ao    1 linear 512.00M
  lv       vg         -wi-a-    1 linear 104.00M
  lv       vg         -wi-a-    1 linear 104.00M
  lv       vg         -wi-a-    1 linear 104.00M
  lv       vg         -wi-a-    1 linear  88.00M
Using the -v argument with the lvs --segments command adds the following fields to the default display: seg_start, stripesize, chunksize.
# lvs -v --segments
    Finding all logical volumes
  LV         VG     Attr   Start SSize  #Str Type   Stripe Chunk
  lvol0      new_vg owi-a-    0  52.00M    1 linear     0     0
  newvgsnap1 new_vg swi-a-    0   8.00M    1 linear     0  8.00K
The following example shows the default output of the lvs command on a system with one logical volume configured, followed by the default output of the lvs command with the segments argument specified.
# lvs
  LV    VG     Attr   LSize  Origin Snap%  Move Log Copy%
  lvol0 new_vg -wi-a- 52.00M
# lvs --segments
  LV    VG     Attr   #Str Type   SSize
  lvol0 new_vg -wi-a-    1 linear 52.00M
c) Sorting LVM Reports :-
Normally the entire output of the lvs, vgs, or pvs command has to be generated and stored internally before it can be sorted and columns aligned correctly. You can specify the --unbuffered argument to display unsorted output as soon as it is generated.
To specify an alternative ordered list of columns to sort on, use the -O argument of any of the reporting commands. It is not necessary to include these fields within the output itself.
The following example shows the output of the pvs command that displays the physical volume name, size, and free space.
# pvs -o pv_name,pv_size,pv_free
  PV         PSize  PFree
  /dev/sdb1  17.14G 17.14G
  /dev/sdc1  17.14G 17.09G
  /dev/sdd1  17.14G 17.14G
The following example shows the same output, sorted by the free space field.
# pvs -o pv_name,pv_size,pv_free -O pv_free
  PV         PSize  PFree
  /dev/sdc1  17.14G 17.09G
  /dev/sdd1  17.14G 17.14G
  /dev/sdb1  17.14G 17.14G
The following example shows that you do not need to display the field on which you are sorting.
# pvs -o pv_name,pv_size -O pv_free
  PV         PSize
  /dev/sdc1  17.14G
  /dev/sdd1  17.14G
  /dev/sdb1  17.14G
To display a reverse sort, precede a field you specify after the -O argument with the - character.
# pvs -o pv_name,pv_size,pv_free -O -pv_free
  PV         PSize  PFree
  /dev/sdd1  17.14G 17.14G
  /dev/sdb1  17.14G 17.14G
  /dev/sdc1  17.14G 17.09G

LVM Troubleshooting : -

a) troubleshooting Diagnostics
If a command is not working as expected, you can gather diagnostics in the following ways:
  • Use the -v, -vv, -vvv, or -vvvv argument of any command for increasingly verbose levels of output.
  • If the problem is related to the logical volume activation, set 'activation = 1' in the 'log' section of the configuration file and run the command with the -vvvv argument. After you have finished examining this output be sure to reset this parameter to 0, to avoid possible problems with the machine locking during low memory situations.
  • Run the lvmdump command, which provides an information dump for diagnostic purposes. For information, see the lvmdump(8) man page.
  • Execute the lvs -v, pvs -a or “dmsetup info -c” command for additional system information.
  • Examine the last backup of the metadata in the /etc/lvm/backup file and archived versions in the /etc/lvm/archive file.
  • Check the current configuration information by running the lvm dumpconfig command.
  • Check the .cache file in the /etc/lvm directory for a record of which devices have physical volumes on them.
b) Displaying Information on Failed Devices :-
You can use the -P(provide access to volume groups that are only partially
available) argument of the lvs or vgs command to display information about a failed volume that would otherwise not appear in the output. This argument permits some operations even though the metatdata is not completely consistent internally. For example, if one of the devices that made up the volume group vg failed, the vgs command might show the following output.
[root@link-07 tmp]# vgs -o +devices
  Volume group "vg" not found
If you specify the -P argument of the vgs command, the volume group is still unusable but you can see more information about the failed device.
[root@link-07 tmp]# vgs -P -o +devices
  Partial mode. Incomplete volume groups will be activated read-only.
  VG   #PV #LV #SN Attr   VSize VFree Devices
  vg     9   2   0 rz-pn- 2.11T 2.07T unknown device(0)
  vg     9   2   0 rz-pn- 2.11T 2.07T unknown device(5120),/dev/sda1(0)
In this example, the failed device caused both a linear and a striped logical volume in the volume group to fail. The lvs command without the -P argument shows the following output.
[root@link-07 tmp]# lvs -a -o +devices
  Volume group "vg" not found
Using the -P argument shows the logical volumes that have failed.
[root@link-07 tmp]# lvs -P -a -o +devices
  Partial mode. Incomplete volume groups will be activated read-only.
  LV     VG   Attr   LSize  Origin Snap%  Move Log Copy%  Devices
  linear vg   -wi-a- 20.00G                               unknown device(0)
  stripe vg   -wi-a- 20.00G                               unknown device(5120),/dev/sda1(0)
The following examples show the output of the pvs and lvs commands with the -P argument specified when a leg of a mirrored logical volume has failed.
root@link-08 ~]#  vgs -a -o +devices -P
  Partial mode. Incomplete volume groups will be activated read-only.
  VG    #PV #LV #SN Attr   VSize VFree Devices
  corey   4   4   0 rz-pnc 1.58T 1.34T my_mirror_mimage_0(0),my_mirror_mimage_1(0)
  corey   4   4   0 rz-pnc 1.58T 1.34T /dev/sdd1(0)
  corey   4   4   0 rz-pnc 1.58T 1.34T unknown device(0)
  corey   4   4   0 rz-pnc 1.58T 1.34T /dev/sdb1(0)
[root@link-08 ~]# lvs -a -o +devices -P
  Partial mode. Incomplete volume groups will be activated read-only.
  LV                   VG    Attr   LSize   Origin Snap%  Move Log            Copy%  Devices
  my_mirror            corey mwi-a- 120.00G                    my_mirror_mlog   1.95 my_mirror_mimage_0(0),my_mirror_mimage_1(0)
  [my_mirror_mimage_0] corey iwi-ao 120.00G                                          unknown device(0)
  [my_mirror_mimage_1] corey iwi-ao 120.00G                                          /dev/sdb1(0)
  [my_mirror_mlog]     corey lwi-ao   4.00M                                          /dev/sdd1(0)
c) Recovering from LVM Mirror Failure :-
This section provides an example of recovering from a situation where one leg of an LVM mirrored volume fails because the underlying device for a physical volume goes down. When a mirror leg fails, LVM converts the mirrored volume into a linear volume, which continues to operate as before but without the mirrored redundancy. At that point, you can add a new disk device to the system to use as a replacement physical device and rebuild the mirror.
The following command creates the physical volumes which will be used for the mirror.
[root@link-08 ~]# pvcreate /dev/sd[abcdefgh][12]
  Physical volume "/dev/sda1" successfully created
  Physical volume "/dev/sda2" successfully created
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdc1" successfully created
  Physical volume "/dev/sdc2" successfully created
  Physical volume "/dev/sdd1" successfully created
  Physical volume "/dev/sdd2" successfully created
  Physical volume "/dev/sde1" successfully created
  Physical volume "/dev/sde2" successfully created
  Physical volume "/dev/sdf1" successfully created
  Physical volume "/dev/sdf2" successfully created
  Physical volume "/dev/sdg1" successfully created
  Physical volume "/dev/sdg2" successfully created
  Physical volume "/dev/sdh1" successfully created
  Physical volume "/dev/sdh2" successfully created
The following commands creates the volume group vg and the mirrored volume groupfs.
[root@link-08 ~]# vgcreate vg /dev/sd[abcdefgh][12]
  Volume group "vg" successfully creatlled
[root@link-08 ~]# lvcreate -L 750M -n groupfs -m 1 vg /dev/sda1 /dev/sdb1 /dev/sdc1
  Rounding up size to full physical extent 752.00 MB
  Logical volume "groupfs" created
You can use the lvs command to verify the layout of the mirrored volume and the underlying devices for the mirror leg and the mirror log. Note that in the first example the mirror is not yet completely synced; you should wait until the Copy% field displays 100.00 before continuing.
[root@link-08 ~]# lvs -a -o +devices
  LV                 VG   Attr   LSize   Origin Snap%  Move Log          Copy% Devices
  groupfs            vg   mwi-a- 752.00M                    groupfs_mlog 21.28 groupfs_mimage_0(0),groupfs_mimage_1(0)
  [groupfs_mimage_0] vg   iwi-ao 752.00M                                       /dev/sda1(0)
  [groupfs_mimage_1] vg   iwi-ao 752.00M                                       /dev/sdb1(0)
  [groupfs_mlog]     vg   lwi-ao   4.00M                                       /dev/sdc1(0)

[root@link-08 ~]# lvs -a -o +devices
  LV                 VG   Attr   LSize   Origin Snap%  Move Log          Copy%  Devices
  groupfs            vg   mwi-a- 752.00M                    groupfs_mlog 100.00 groupfs_mimage_0(0),groupfs_mimage_1(0)
  [groupfs_mimage_0] vg   iwi-ao 752.00M                                        /dev/sda1(0)
  [groupfs_mimage_1] vg   iwi-ao 752.00M                                        /dev/sdb1(0)
  [groupfs_mlog]     vg   lwi-ao   4.00M     i                                  /dev/sdc1(0)
In this example, the primary leg of the mirror /dev/sda1 fails. Any write activity to the mirrored volume causes LVM to detect the failed mirror. When this occurs, LVM converts the mirror into a single linear volume. In this case, to trigger the conversion, we execute a dd command
[root@link-08 ~]# dd if=/dev/zero of=/dev/vg/groupfs count=10
10+0 records in
10+0 records out
You can use the lvs command to verify that the device is now a linear device. Because of the failed disk, I/O errors occur.
[root@link-08 ~]# lvs -a -o +devices
  /dev/sda1: read failed after 0 of 2048 at 0: Input/output error
  /dev/sda2: read failed after 0 of 2048 at 0: Input/output error
  LV      VG   Attr   LSize   Origin Snap%  Move Log Copy%  Devices
  groupfs vg   -wi-a- 752.00M                               /dev/sdb1(0)
At this point you should still be able to use the logical volume, but there will be no mirror redundancy.
To rebuild the mirrored volume, you replace the broken drive and recreate the physical volume. If you use the same disk rather than replacing it with a new one, you will see "inconsistent" warnings when you run the pvcreate command.
[root@link-08 ~]# pvcreate /dev/sda[12]
  Physical volume "/dev/sda1" successfully created
  Physical volume "/dev/sda2" successfully created

[root@link-08 ~]# pvscan
  PV /dev/sdb1   VG vg   lvm2 [67.83 GB / 67.10 GB free]
  PV /dev/sdb2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdc1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdc2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdd1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdd2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sde1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sde2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdf1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdf2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdg1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdg2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdh1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdh2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sda1           lvm2 [603.94 GB]
  PV /dev/sda2           lvm2 [603.94 GB]
  Total: 16 [2.11 TB] / in use: 14 [949.65 GB] / in no VG: 2 [1.18 TB]
Next you extend the original volume group with the new physical volume.
[root@link-08 ~]# vgextend vg /dev/sda[12]
  Volume group "vg" successfully extended

[root@link-08 ~]# pvscan
  PV /dev/sdb1   VG vg   lvm2 [67.83 GB / 67.10 GB free]
  PV /dev/sdb2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdc1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdc2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdd1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdd2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sde1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sde2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdf1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdf2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdg1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdg2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdh1   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sdh2   VG vg   lvm2 [67.83 GB / 67.83 GB free]
  PV /dev/sda1   VG vg   lvm2 [603.93 GB / 603.93 GB free]
  PV /dev/sda2   VG vg   lvm2 [603.93 GB / 603.93 GB free]
  Total: 16 [2.11 TB] / in use: 16 [2.11 TB] / in no VG: 0 [0   ]
Convert the linear volume back to its original mirrored state.
[root@link-08 ~]# lvconvert -m 1 /dev/vg/groupfs /dev/sda1 /dev/sdb1 /dev/sdc1
  Logical volume mirror converted.
You can use the lvs command to verify that the mirror is restored.
[root@link-08 ~]# lvs -a -o +devices
  LV                 VG   Attr   LSize   Origin Snap%  Move Log          Copy% Devices
  groupfs            vg   mwi-a- 752.00M                    groupfs_mlog 68.62 groupfs_mimage_0(0),groupfs_mimage_1(0)
  [groupfs_mimage_0] vg   iwi-ao 752.00M                                       /dev/sdb1(0)
  [groupfs_mimage_1] vg   iwi-ao 752.00M                                       /dev/sda1(0)
  [groupfs_mlog]     vg   lwi-ao   4.00M                                       /dev/sdc1(0)
d) Recovering Physical Volume Metadata:- 
If the volume group metadata area of a physical volume is accidentally overwritten or otherwise destroyed, you will get an error message indicating that the metadata area is incorrect, or that the system was unable to find a physical volume with a particular UUID. You may be able to recover the data the physical volume by writing a new metadata area on the physical volume specifying the same UUID as the lost metadata.

Caution

You should not attempt this procedure with a working LVM logical volume. You will lose your data if you specify the incorrect UUID.
The following example shows the sort of output you may see if the metadata area is missing or corrupted.
[root@link-07 backup]# lvs -a -o +devices
  Couldn't find device with uuid 'FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk'.
  Couldn't find all physical volumes for volume group VG.
  Couldn't find device with uuid 'FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk'.
  Couldn't find all physical volumes for volume group VG.
  ...
You may be able to find the UUID for the physical volume that was overwritten by looking in the /etc/lvm/archive directory. Look in the file VolumeGroupName_xxxx.vg for the last known valid archived LVM metadata for that volume group.
Alternately, you may find that deactivating the volume and setting the partial (-P) argument will enable you to find the UUID of the missing corrupted physical volume.
[root@link-07 backup]# vgchange -an --partial
  Partial mode. Incomplete volume groups will be activated read-only.
  Couldn't find device with uuid 'FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk'.
  Couldn't find device with uuid 'FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk'.
  ...
Use the --uuid and --restorefile arguments of the pvcreate command to restore the physical volume. The following example labels the /dev/sdh1 device as a physical volume with the UUID indicated above, FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk. This command restores the physical volume label with the metadata information contained in VG_00050.vg, the most recent good archived metatdata for volume group . The restorefile argument instructs the pvcreate command to make the new physical volume compatible with the old one on the volume group, ensuring that the the new metadata will not be placed where the old physical volume contained data (which could happen, for example, if the original pvcreate command had used the command line arguments that control metadata placement, or it the physical volume was originally created using a different version of the software that used different defaults). The pvcreate command overwrites only the LVM metadata areas and does not affect the existing data areas.
[root@link-07 backup]# pvcreate --uuid "FmGRh3-zhok-iVI8-7qTD-S5BI-MAEN-NYM5Sk" --restorefile /etc/lvm/archive/VG_00050.vg /dev/sdh1
  Physical volume "/dev/sdh1" successfully created
You can then use the vgcfgrestore command to restore the volume group's metadata.
[root@link-07 backup]# vgcfgrestore VG
  Restored volume group VG
You can now display the logical volumes.
[root@link-07 backup]# lvs -a -o +devices
  LV     VG   Attr   LSize   Origin Snap%  Move Log Copy%  Devices
  stripe VG   -wi--- 300.00G                               /dev/sdh1 (0),/dev/sda1(0)
  stripe VG   -wi--- 300.00G                               /dev/sdh1 (34728),/dev/sdb1(0)
The following commands activate the volumes and display the active volumes.
[root@link-07 backup]# lvchange -ay /dev/VG/stripe
[root@link-07 backup]# lvs -a -o +devices
  LV     VG   Attr   LSize   Origin Snap%  Move Log Copy%  Devices
  stripe VG   -wi-a- 300.00G                               /dev/sdh1 (0),/dev/sda1(0)
  stripe VG   -wi-a- 300.00G                               /dev/sdh1 (34728),/dev/sdb1(0)
If the on-disk LVM metadata takes as least as much space as what overrode it, this command can recover the physical volume. If what overrode the metadata went past the metadata area, the data on the volume may have been affected. You might be able to use the fsck command to recover that data.
e) Removing Lost Physical Volumes from a Volume Group :-
If you lose a physical volume, you can activate the remaining physical volumes in the volume group with the --partial argument of the vgchange command. You can remove all the logical volumes that used that physical volume from the volume group with the --removemissing argument of the vgreduce command.
It is recommended that you run the vgreduce command with the --test argument to verify what you will be destroying.
Like most LVM operations, the vgreduce command is reversible in a sense if you immediately use the vgcfgrestore command to restore the volume group metadata to its previous state. For example, if you used the --removemissing argument of the vgreduce command without the --test argument and find you have removed logical volumes you wanted to keep, you can still replace the physical volume and use another vgcfgrestore command to return the volume group to its previous state.

Recovering a Lost LVM Volume Disk

Server Configuration

In all three examples, a server with SUSE Linux Enterprise Server 10 with Service Pack 1 (SLES10 SP1) will be used with LVM version 2. The examples will use a volume group called "sales" with a linear logical volume called "reports". The logical volume and it's mount point are shown below. You will need to substitute your mount points and volume names as needed to match your specific environment.
ls-lvm:~ # cat /proc/partitions
major minor  #blocks  name

   8     0    4194304 sda
   8     1     514048 sda1
   8     2    1052257 sda2
   8     3          1 sda3
   8     5     248976 sda5
   8    16     524288 sdb
   8    32     524288 sdc
   8    48     524288 sdd

ls-lvm:~ # pvcreate /dev/sda5 /dev/sd[b-d]
  Physical volume "/dev/sda5" successfully created
  Physical volume "/dev/sdb" successfully created
  Physical volume "/dev/sdc" successfully created
  Physical volume "/dev/sdd" successfully created

ls-lvm:~ # vgcreate sales /dev/sda5 /dev/sd[b-d]
  Volume group "sales" successfully created

ls-lvm:~ # lvcreate -n reports -L +1G sales
  Logical volume "reports" created

ls-lvm:~ # pvscan
  PV /dev/sda5   VG sales   lvm2 [240.00 MB / 240.00 MB free]
  PV /dev/sdb    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdc    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdd    VG sales   lvm2 [508.00 MB / 500.00 MB free]
  Total: 4 [1.72 GB] / in use: 4 [1.72 GB] / in no VG: 0 [0   ]

ls-lvm:~ # vgs
  VG    #PV #LV #SN Attr   VSize VFree
  sales   4   1   0 wz--n- 1.72G 740.00M

ls-lvm:~ # lvs
  LV      VG    Attr   LSize Origin Snap%  Move Log Copy%
  reports sales -wi-ao 1.00G

ls-lvm:~ # mount | grep sales
/dev/mapper/sales-reports on /sales/reports type ext3 (rw)

ls-lvm:~ # df -h /sales/reports
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/sales-reports
                     1008M   33M  925M   4% /sales/reports

Disk Belonging to a Volume Group Removed

Removing a disk, belonging to a logical volume group, from the server may sound a bit strange, but with Storage Area Networks (SAN) or fast paced schedules, it happens.
Symptom:
The first thing you may notice when the server boots are messages like:
"Couldn't find all physical volumes for volume group sales."
"Couldn't find device with uuid '56pgEk-0zLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'."
'Volume group "sales" not found'
If you are automatically mounting /dev/sales/reports, then the server will fail to boot and prompt you to login as root to fix the problem.
  1. Type root's password.
  2. Edit the /etc/fstab file.
  3. Comment out the line with /dev/sales/report
  4. Reboot
The LVM symptom is a missing sales volume group. Typing cat /proc/partitions confirms the server is missing one of it's disks.
ls-lvm:~ # cat /proc/partitions
major minor  #blocks  name

   8     0    4194304 sda
   8     1     514048 sda1
   8     2    1052257 sda2
   8     3          1 sda3
   8     5     248976 sda5
   8    16     524288 sdb
   8    32     524288 sdc

ls-lvm:~ # pvscan
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  PV /dev/sda5        VG sales   lvm2 [240.00 MB / 240.00 MB free]
  PV /dev/sdb         VG sales   lvm2 [508.00 MB / 0    free]
  PV unknown device   VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdc         VG sales   lvm2 [508.00 MB / 500.00 MB free]
  Total: 4 [1.72 GB] / in use: 4 [1.72 GB] / in no VG: 0 [0   ]
Solution:
  1. Fortunately, the meta data and file system on the disk that was /dev/sdc are intact.
  2. So the recovery is to just put the disk back.
  3. Reboot the server.
  4. The /etc/init.d/boot.lvm start script will scan and activate the volume group at boot time.
  5. Don't forget to uncomment the /dev/sales/reports device in the /etc/fstab file.
If this procedure does not work, then you may have corrupt LVM meta data.

Corrupted LVM Meta Data

The LVM meta data does not get corrupted very often; but when it does, the file system on the LVM logical volume should also be considered unstable. The goal is to recover the LVM volume, and then check file system integrity.
Symptom 1:
Attempting to activate the volume group gives the following:
ls-lvm:~ # vgchange -ay sales
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  Couldn't read volume group metadata.
  Volume group sales metadata is inconsistent
  Volume group for uuid not found: m4Cg2vkBVSGe1qSMNDf63v3fDHqN4uEkmWoTq5TpHpRQwmnAGD18r44OshLdHj05
  0 logical volume(s) in volume group "sales" now active
This symptom is the result of a minor change in the meta data. In fact, only three bytes were overwritten. Since only a portion of the meta data was damaged, LVM can compare it's internal check sum against the meta data on the device and know it's wrong. There is enough meta data for LVM to know that the "sales" volume group and devices exit, but are unreadable.
ls-lvm:~ # pvscan
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  PV /dev/sda5   VG sales   lvm2 [240.00 MB / 240.00 MB free]
  PV /dev/sdb    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdc    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdd    VG sales   lvm2 [508.00 MB / 500.00 MB free]
  Total: 4 [1.72 GB] / in use: 4 [1.72 GB] / in no VG: 0 [0   ]
Notice pvscan shows all devices present and associated with the sales volume group. It's not the device UUID that is not found, but the volume group UUID.
Solution 1:
  1. Since the disk was never removed, leave it as is.
  2. There were no device UUID errors, so don't attempt to restore the UUIDs.
  3. This is a good candidate to just try restoring the LVM meta data.
ls-lvm:~ # vgcfgrestore sales
  /dev/sdc: Checksum error
  /dev/sdc: Checksum error
  Restored volume group sales

ls-lvm:~ # vgchange -ay sales
  1 logical volume(s) in volume group "sales" now active

ls-lvm:~ # pvscan
  PV /dev/sda5   VG sales   lvm2 [240.00 MB / 240.00 MB free]
  PV /dev/sdb    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdc    VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdd    VG sales   lvm2 [508.00 MB / 500.00 MB free]
  Total: 4 [1.72 GB] / in use: 4 [1.72 GB] / in no VG: 0 [0   ]
  1. Run a file system check on /dev/sales/reports.
ls-lvm:~ # e2fsck /dev/sales/reports
e2fsck 1.38 (30-Jun-2005)
/dev/sales/reports: clean, 961/131072 files, 257431/262144 blocks

ls-lvm:~ # mount /dev/sales/reports /sales/reports/

ls-lvm:~ # df -h /sales/reports/
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/sales-reports
                     1008M  990M     0 100% /sales/reports
Symptom 2:
Minor damage to the LVM meta data is easily fixed with vgcfgrestore. If the meta data is gone, or severely damaged, then LVM will consider that disk as an "unknown device." If the volume group contains only one disk, then the volume group and it's logical volumes will simply be gone. In this case the symptom is the same as if the disk was accidentally removed, with the exception of the device name. Since /dev/sdc was not actually removed from the server, the devices are still labeled a through d.
ls-lvm:~ # pvscan
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  PV /dev/sda5        VG sales   lvm2 [240.00 MB / 240.00 MB free]
  PV /dev/sdb         VG sales   lvm2 [508.00 MB / 0    free]
  PV unknown device   VG sales   lvm2 [508.00 MB / 0    free]
  PV /dev/sdd         VG sales   lvm2 [508.00 MB / 500.00 MB free]
  Total: 4 [1.72 GB] / in use: 4 [1.72 GB] / in no VG: 0 [0   ]
Solution 2:
  1. First, replace the disk. Most likely the disk is already there, just damaged.
  2. Since the UUID on /dev/sdc is not there, a vgcfgrestore will not work.
ls-lvm:~ # vgcfgrestore sales
  Couldn't find device with uuid '56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu'.
  Couldn't find all physical volumes for volume group sales.
  Restore failed.
  1. Comparing the output of cat /proc/partitions and pvscan shows the missing device is /dev/sdc, and pvscan shows which UUID it needs for that device. So, copy and paste the UUID that pvscan shows for /dev/sdc.
ls-lvm:~ # pvcreate --uuid 56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu /dev/sdc
  Physical volume "/dev/sdc" successfully created
  1. Restore the LVM meta data
ls-lvm:~ # vgcfgrestore sales
  Restored volume group sales

ls-lvm:~ # vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "sales" using metadata type lvm2

ls-lvm:~ # vgchange -ay sales
  1 logical volume(s) in volume group "sales" now active
  1. Run a file system check on /dev/sales/reports.
ls-lvm:~ # e2fsck /dev/sales/reports
e2fsck 1.38 (30-Jun-2005)
/dev/sales/reports: clean, 961/131072 files, 257431/262144 blocks

ls-lvm:~ # mount /dev/sales/reports /sales/reports/

ls-lvm:~ # df -h /sales/reports
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/sales-reports
                     1008M  990M     0 100% /sales/reports

Disk Permanently Removed

This is the most severe case. Obviously if the disk is gone and unrecoverable, the data on that disk is likewise unrecoverable. This is a great time to feel good knowing you have a solid backup to rely on. However, if the good feelings are gone, and there is no backup, how do you recover as much data as possible from the remaining disks in the volume group? No attempt will be made to address the data on the unrecoverable disk; this topic will be left to the data recovery experts.
Symptom:
The symptom will be the same as Symptom 2 in the Corrupted LVM Meta Data section above. You will see errors about an "unknown device" and missing device with UUID.
Solution:
  1. Add a replacement disk to the server. Make sure the disk is empty.
  2. Create the LVM meta data on the new disk using the old disk's UUID that pvscan displays.
ls-lvm:~ # pvcreate --uuid 56ogEk-OzLS-cKBc-z9vJ-kP65-DUBI-hwZPSu /dev/sdc
  Physical volume "/dev/sdc" successfully created
  1. Restore the backup copy of the LVM meta data for the sales volume group.
ls-lvm:~ # vgcfgrestore sales
  Restored volume group sales

ls-lvm:~ # vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "sales" using metadata type lvm2

ls-lvm:~ # vgchange -ay sales
  1 logical volume(s) in volume group "sales" now active
  1. Run a file system check to rebuild the file system.
ls-lvm:~ # e2fsck -y /dev/sales/reports
e2fsck 1.38 (30-Jun-2005)
--snip--
Free inodes count wrong for group #5 (16258, counted=16384).
Fix? yes

Free inodes count wrong (130111, counted=130237).
Fix? yes

/dev/sales/reports: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sales/reports: 835/131072 files (5.7% non-contiguous), 137213/262144 blocks
  1. Mount the file system and recover as much data as possible.
  2. NOTE: If the missing disk contains the beginning of the file system, then the file system's superblock will be missing. You will need to rebuild or use an alternate superblock. Restoring a file system superblock is outside the scope of this article, please refer to your file system's documentation.

Conclusion

LVM by default keeps backup copies of it's meta data for all LVM devices. These backup files are stored in /etc/lvm/backup and /etc/lvm/archive. If a disk is removed or the meta data gets damaged in some way, it can be easily restored, if you have backups of the meta data. This is why it is highly recommended to never turn off LVM's auto backup feature. Even if a disk is permanently removed from the volume group, it can be reconstructed, and often times the remaining data on the file system recovered.