You are here

LVM commands

It is confusing that a logical volume can contain either a partitioned drive or a single file-system with no partition table. Doing an fdisk -lu will determine which.

A partition can contain several file-systems, such as boot and a “nested” LVM partition. The link at the bottom gives examples of how to access this.

Create and mount a LV

/usr/sbin/lvcreate -L 20G -n jessica VolGroup00
/sbin/mkfs -j /dev/VolGroup00/jessica -L jessica #format jessica as ext3
mkfs.ext4 -L ext4volname /dev/vg/newvol # or ext4
mount /dev/VolGroup00/jessica /home/jessica/backup

Move a file-based VM into an LVM based one

/usr/sbin/lvcreate -L 10G -n vm_amf VolGroup00 #create the LV
dd if=/home/xen/domains/xen2.example.com/disk.img of=/dev/VolGroup00/vm_amf
/sbin/e2fsck -f /dev/VolGroup00/vm_amf
/sbin/resize2fs /dev/VolGroup00/vm_amf
/usr/sbin/lvcreate -L 1G -n vm_amf_sw VolGroup00 #create the LV swap file
/sbin/mkswap /dev/VolGroup00/vm_amf_sw

edit /etc/xen/amf.cfg :

disk = [ 'phy:/dev/VolGroup00/vm_amf,hda1,w', 'phy:/dev/VolGroup00/vm_amf_sw,hda2,w' ]
/usr/sbin/xm create -c /etc/xen/amf.cfg #start up the machine

change host name

vi /etc/hosts # change the
echo amf.isnew.org > /etc/hostname
/etc/init.d/hostname.sh start
#test with:
hostname
hostname -f

Mount LVM disks within a VM from the host system

kpartx -av /dev/VolGroup00/nameOfImage     #create mapping
#this will give you the mappings available
#ls -l /dev/mapper/ #will give you their names
mount /dev/mapper/nameofmappingp1 mountpoint #probably ends in p1
#do stuff
umount mountpoint #must do this before removing mapping
kpartx -dv /dev/VolGroup00/nameOfImage     #remove mapping
  • To mount LVM disks within another LV (as long as the volume groups have different names):
    • follow the above approach to map the LV, but don’t mount it (it won’t mount anyway)
vgscan #then run this command to re-scan
vgchange -ay VolGroup00 #activate (don't forget to de-activate etc. in reverse order)
kpartx ... # may not be necessary

rename a logical volume:

vgchange -a n /dev/VolGroup00 #de-activate volume
vgrename /dev/VolGroup00 /dev/VolGroup0
vgchange -ay VolGroup0 #re-activate volume

Mount LVM from Ubuntu Live CD

apt-get install lvm2
fdisk -lu
pvscan
vgscan
vgchange -a y
lvscan
mount /dev/VolGroup00/LogVol00 /mnt

resize a logical volume

VOL=VolGroup00/opt_zimbra
#if it is a VM a better alternative is to shut down the machine and then check integrity on host machine
umount /dev/$VOL #do this on machine that is actually using volume
lsof | grep /dev/$VOL #if can't unmount because a file is open

lvextend -L+8G /dev/$VOL #makes 8 Gb larger
e2fsck /dev/$VOL # have to check integrity before resizing
resize2fs /dev/$VOL # resize to size of file system
mount /dev/$VOL

Reduce size of logical volume and filesystem

MAPPED=/dev/mapper/VolGroup00-clones
MOUNT=/clones
NEWSIZE=140G

df -h $MOUNT # find size (note that this uses base-2 Gb which will report small volumes
umount $MOUNT
e2fsck -f $MAPPED
resize2fs $MAPPED $NEWSIZE
lvreduce -L $NEWSIZE $MAPPED
e2fsck -f $MAPPED # to be safe
mount $MOUNT
df -h $MOUNT # find final size
Topic: