Difference between revisions of "LVM"

From Noah.org
Jump to navigationJump to search
 
(3 intermediate revisions by the same user not shown)
Line 53: Line 53:
  
 
== Examples ==
 
== Examples ==
 +
 +
=== find the volume group name from a logical volume name ===
 +
 +
Note that this may be ambiguous and return more than one value if different volume groups have different logical volumes with the same name. Set '''LV_NAME''' to the logical volume name you want to search for.
 +
<pre>
 +
lvs --noheadings -o vg_name,lv_name | grep ${LV_NAME} | sed -e "s/^[[:space:]]*\([^[:space:]]*\).*/\1/"
 +
</pre>
  
 
=== list volume group names ===
 
=== list volume group names ===
 
<pre>
 
<pre>
 
vgs --noheadings -o vg_name
 
vgs --noheadings -o vg_name
 +
</pre>
 +
 +
=== list logical volume names ===
 +
<pre>
 +
lvs --noheadings -o lv_name
 +
</pre>
 +
 +
=== list both volume group and logical volume names ===
 +
<pre>
 +
lvs --noheadings -o lv_name,vg_name
 
</pre>
 
</pre>
  

Latest revision as of 23:34, 4 December 2013

See also LVM Howto


You can resize ext2/ext3 filesystems while mounted.

copy an LVM virtual machine partition

This was based on a Xen virtual machine system I manage.

#!/bin/sh
VG_NAME=$1
LV_NAME=$2
SSH_LOGIN=$3
LV_SIZE=$(lvs --noheadings -o lv_size /dev/${VG_NAME}/${LV_NAME})
ssh ${SSH_LOGIN} "lvcreate -L ${LV_SIZE} -n ${LV_NAME} ${VG_NAME}"
dd bs=1M if=/dev/${VG_NAME}/${LV_NAME} | ssh -C ${SSH_LOGIN} 'dd of=/dev/${VG_NAME}/${LV_NAME}'

For Xen, you will also want to copy the guest.cfg file.

scp /etc/xen/guest.cfg ${SSH_LOGIN}:/etc/xen/.

Remember

a Volume Group is the equivalent of a physical device

It can be made up of a collection of drives. You can add or remove drives from a VolGroup. Note that this isn't strictly correct. Parts of a drive can be part of different VolGroups.

a Logical Volume is the quivalent of a partition

Logical Volumes are mapped to a device in /dev. A LogVol is what you actually mount to a mount point.

Basic commands

  • lvs -- an alternative interface to volume management.
  • vgdisplay -- display attributes of volume groups. See also `vga` for a more brief display.
  • pvdisplay -- display attributes of physical volumes. See also `pvs` for a more brief display. Use this to figure out how much physical space is available.
  • vgscan -- to identify your volume groups by name. example: VolGroup00
  • lvscan -- to identify your logical volumes by name. example: LogVol00 LogVol01 LogVol02 LogVol03 LogVol04
  • pvcreate -- Add a partition to a physical volume.
  • vgextend -- Add a partition to a volume_group.
  • df -- to see which device names are mapped to which Logical Volumes. The device name is what mount/umount/ext2online care about.
  • ext2online -- this resizes an online ext2 or ext3 filesystem. You do not have to umount it. If you don't have ext2online then your Linux is old. You will have to umount the device first; use ext2resize; then mount the device again.

Examples

find the volume group name from a logical volume name

Note that this may be ambiguous and return more than one value if different volume groups have different logical volumes with the same name. Set LV_NAME to the logical volume name you want to search for.

lvs --noheadings -o vg_name,lv_name | grep ${LV_NAME} | sed -e "s/^[[:space:]]*\([^[:space:]]*\).*/\1/"

list volume group names

vgs --noheadings -o vg_name

list logical volume names

lvs --noheadings -o lv_name

list both volume group and logical volume names

lvs --noheadings -o lv_name,vg_name

extend an ext3 partition to be 100GB

Say that you have some unallocated space that you want to give to /var. Use `vgs` too see how much unallocated space is free. If not enough space is free then you will have to shrink another logical volume or add more physical disk. Say that /var is mounted on device /dev/mapper/VolGroup00-LogVol04.

Use the following the determine how much free space you can extend to:

# vgdisplay

Look for the line " Free PE / Size".

Use the following commands:

lvextend -L100GB /dev/VolGroup00/LogVol04
ext2online /dev/mapper/VolGroup00-LogVol04

Device Mapper

What is this dm or mod-dm.ko thing? This is the very heart of LVM. See `man dmsetup` or visit Device-mapper Resource Page.

Sometimes while using partimage you might get this error:

/dev/dm inode doesn't exists.
Partimage can create it for you.
You can also use the manual mknod
command. Do you want this inode
to be created for you now ?

The solution is to create the /dev/dm node:

# mknod -m 644 /dev/dm b 240 0

Which device mapper device name maps to a LVM volume name?

It's annoying that LVM uses friendly symlink names under /dev/VolGroup00/ to work with. The Linux kernel only pays attention to the block device name (the name given by the Device Mapper) which look like /dev/dm-* where * is a number. So if you run iostat you will see disk statistics for the block device names, /dev/dm-*, but now you have to go through an extra step to get the logical volume name associated with the block device name.

The following works pretty well.

# dmsetup info -c --noheadings -o name /dev/dm-5
VolGroup00-LogVol04

You can also list all block device names mapped to a logical volume name.

# dmsetup info -c --noheadings --separator " " -o blkdevname,name
dm-0 VolGroup00-root
dm-1 VolGroup00-swap
dm-2 VolGroup00-LogVol01
dm-3 VolGroup00-LogVol02
dm-4 VolGroup00-LogVol03
dm-5 VolGroup00-LogVol04
dm-6 VolGroup00-LogVol05

Something like this also works, but it is not very pretty.

ls -la /dev/VolGroup00/ | grep dm-5

Note that this won't work. This shows the device of the Volume Group for a logical volume.

lvs -o +devices