Home Linux Hardware
Script to semi-automate lv-fs creation:

Remove failed cdrom from lvm actions

vi /etc/lvm/lvm.conf
filter = [[ "r|/dev/cdrom|" ]]

aptitude install lvm2
modprobe dm-mod

May have to run this after reboot

dmsetup mknodes
vgscan --ignorelockingfailure
vgchange -ay --ignorelockingfailure 

Show available pv's and sizes


Create physical volume

pvcreate /dev/sdx

unconfigure a drive from lvm. Must not be assigned to a vg

pvremove /dev/sdx

Show info about physical volumes. Ie hard disk used by lvm

# Note with pvdisplay the PE size is the physical extent size. Minimal unit size that can be assigned to a lv. lv size = pe(x)

Disallows the allocation of physical extents on /dev/sdx. ie your planning on removing the disk.

pvchange -x n /dev/sdx
# re allow with pvchange -x y /dev/sdx

Expand the PV on /dev/sda1 after enlarging the partition with fdisk. Can also be used to shrink. see man page for pvresize

pvresize /dev/sda1

Create new vg

vgcreate myVG /dev/sdx
vgs myVG

list vg's. Note if the C attribute is present then this is a clustered vg.

vgscan # Also scans all the disks for volume groups and rebuilds the LVM cache file

Add a pv to a vg

vgextend <vgName> /dev/sdx

Remove a pv from a vg

vgreduce <vgName> /dev/sdx

Changing the maximum number of lv a vg can have. ex to 128

vgchange -l 128 /dev/<vgName>

Deactivate a vg

vgchange -a n <vgName>

Activate a vg

vgchange -a <vgName>

Remove a vg that contains no lv

vgremove <vgName>

Splitting a vg. Ie move the pv and its lv's to a new vg. can't move parts of lv's

vgsplit <originalVG> <newVG> /dev/sdx

Merge vg's 

vgmerge -v <vgOne> <vgTwo>

Rename a vg

vgrename <oldName> <newName>

see vgexport and vgimport to move a vg to a new system.
if /dev/<vgname> is not representative of the vg see: vgmknodes

Create new lv on vg

lvcreate -L 100G -n <lvName> <vgName>   # create a 100G lv
lvcreate -l 50%vg -n <lvName> <vgName>  # create a lv using 50% of the total size of the vg
lvcreate -l 100%FREE -n <lvName> <vgName>  # create a lv using 100% the free space in the vg
lvcreate -L 100G -n <lvName> <vgName> /dev/sdx # specifying a pv will create the lv on the pv in that vg
lvcreate -l 100 <lvName> <vgName> /dev/sdx:0-24 /dev/sdy:50:124  # using extents and spcecifying multiple pv with extent ranges. 

Creating a striped lv

lvcreate -L 50g -i2 -I64 -g <lvName> <vgName>  # create a 50g lv across 2 pv with a stripe of 64kb

Createing a mirrored lv

lvcreate -L 50g -m1 -n <lvName> <vgName> # create a mirror with 1 copy (-m1)

#Events for mirrored failures can be defined in the activation section of the lvm.conf file.
#To repair a failed mirrored lv see: lvconvert --repair

list lv's

lvscan # scans for all lv's in the system

make fs on lv

mkfs.ext4 -m1 /dev/myVG/mylv01

Resize a fs. When no size is specified to resize2fs it will take all the available space in the lv

lvresize -L+3G rootvg/LogVolHome
resize2fs -p /dev/mapper/rootvg-LogVolHome

#or in one command
lvresize -r -L+3G rootvg/LogVolHome

Reduce the size of a lv (the reduce size must be >= fs sizes of it)

lvreduce -l -3 <vgName>/<lvName>  #reduce by 3 extents

make a lv read-only

lvchange -pr <vgName>/<lvName>

rename a lv

lvrename <vgName>/<lvName> <vgName>/<newLvName>

remove a lv

lvremove <vgName>/<lvName>


Raid lvms requires rhl 6.3 or greater
Thin volumes (over commit physical storage) requires rhl 6.4 or greater