A quick HOWTO for LVM2 and Linux
This is just a quick overview of LVM2 and how to use it. LVM in general makes managing servers much easier due to being more flexible with storage devices. Disk management is much simpler. No longer do you have to use an entire disk partition for a file system, you can just create a new logival volume and go on.
You can see the partition layout of this box. I’ll be playing around with /dev/hda6 which is a single partition labeled as Linux/LVM.
Device Boot Start End Blocks Id System
/dev/hda1 1 609 4891761 83 Linux
/dev/hda2 610 734 1004062+ 82 Linux swap / Solaris
/dev/hda3 735 1951 9775552+ 83 Linux
/dev/hda4 3168 19457 130849425 5 Extended
/dev/hda5 8034 19457 91763248+ 7 HPFS/NTFS
/dev/hda6 3168 4384 9775521 8e Linux LVM
The first step to using LVM is assigning an unused partition or physical disk to LVM. You do this by running the ‘pvcreate’ command. The device can be a disk partition, entire whole disk, meta device, or a loop back file.
firmo log # pvcreate /dev/hda6
Physical volume “/dev/hda6″ successfully created
firmo log # pvs
PV VG Fmt Attr PSize PFree
/dev/hda6 lvm2 — 9.32G 9.32G
Once the physical volume is initialized, you will have to create a volume group using the ‘vgcreate’ command. The volume group is a collection of physical volumes as created above. Your logical volumes will live within this volume group. It’s generally best to make a volume group for a certain group of storage since it will be somewhat sharing the same backend devices, such as rootvg in the AIX world. If you were to eventually create another volume group for a groupd of database filesystems, you would probably name it accordingly. One of the reasons I generally keep storage groups separate is for data migrations. If you have SAN storage going to host1, and have a single volume group with 10 different applications running in it, it’ll make migration of one or two applications very difficult when moving to a new host. If you have a volume group for each different application, you can easily manipulate that individual volume group to export it to a new host.
Here is an example of creating volume group test_vg using physical volume /dev/hda6
firmo ~ # vgcreate test_vg /dev/hda6
Volume group “test_vg” successfully created
The ‘vgs’ command will show you detected volume groups.
firmo ~ # vgs
VG #PV #LV #SN Attr VSize VFree
test_vg 1 0 0 wz–n- 9.32G 9.32G
Now that you have a usable volume group, you will need to create a logical volume to actually write the data to. This is done using the ‘lvcreate’ command as shown below.
This command is creating a 2GB logical volume that is read/write using test_vg to grab its storage from.
firmo linux # lvcreate -L 2G -p rw test_vg
Logical volume “lvol0″ created
Notice I didn’t specify a name, so the logical volume is named lvol0 by default. If you were to do this again, you could have lvol1, etc.
firmo linux # lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvol0 test_vg -wi-a- 2.00G
Here I specify name for the LV.
firmo linux # lvcreate -L 2G -p rw -n test_lv test_vg
Logical volume “test_lv” created
firmo linux # lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
lvol0 test_vg -wi-a- 2.00G
test_lv test_vg -wi-a- 2.00G
Now the device is ready for use. I will format it with reiserfs.
firmo linux # mkreiserfs /dev/test_vg/test_lv
Once the file system is created, you are free to mount the volume. Notice the mount device, it will be /dev/volume_group/logical_volume.
firmo linux # mount /dev/test_vg/test_lv /test
firmo linux # df -h /test
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/test_vg-test_lv
2.0G 33M 2.0G 2% /test
Once you’re done playing around, you can go ahead and delete the logical volume and volume group. You can even remove the physical volume definition for that matter.
The volume must be unmounted before removing the device as shown.
firmo linux # lvremove /dev/test_vg/test_lv
Can’t remove open logical volume “test_lv”
firmo linux # umount /test
firmo linux # lvremove /dev/test_vg/test_lv
Do you really want to remove active logical volume “test_lv”? [y/n]: y
Logical volume “test_lv” successfully removedfirmo linux # lvremove -f /dev/test_vg/lvol0
Logical volume “lvol0″ successfully removed
Now I remove the test_vg volume group
firmo linux # vgremove test_vg
Volume group “test_vg” successfully removed
And remove the physical volume
firmo linux # pvremove /dev/hda6
Labels on physical volume “/dev/hda6″ successfully wiped
