******************************************************************
World of LVM: Part I
World of LVM: Part II
******************************************************************
Everybody had plenty of time to read the first part of the “World of LVM” series, actually you had like 9 months, so you should be familiar with the following words:
- Volume Group
- Physical Volume
- Logical Volume
- Physical Extents
If you are not, go back to the first part of the series and do a fast read!
In this second part, I’ll teach you in 5 steps how to create your first filesystem based on top of LVM (version 2) in a RedHat distribution (I think that I should clarify: RedHat Based), if you are running a debian-based distro, you may need to install some packages to get LVM support working, but the concepts and the commands remains the same.
This clarified, let’s get started.
First of all, we need a Physical Volume (PV). Let’s assume we have a new disk, called /dev/hdb, ready to be used. This one will be our PV, but we need to initialize it properly. We have two choices:
- Use the entire disk directly (no partition)
o Run (as root): pvcreate /dev/hdb
- Create a partition and use that partition as PV
o Using fdisk or your favorite partition tool, create the /dev/hdb1 partition
o Run (as root): pvcreate /dev/hdb1
Whether you choose one or the other is your concern, I go with the partitioned layout, as Anaconda (the RedHat installer) uses that schema and I like to keep things as homogeneous as possible.
Next step will be creating the actual Volume Group, which is as simple as using the “vgcreate” command, this command uses the following syntax:
vgcreate
[-A|--autobackup {y|n}]
[--addtag Tag]
[--alloc AllocationPolicy]
[-c|--clustered {y|n}]
[-d|--debug]
[-h|--help]
[-l|--maxlogicalvolumes MaxLogicalVolumes]
[-M|--metadatatype 1|2]
[-p|--maxphysicalvolumes MaxPhysicalVolumes]
[-s|--physicalextentsize PhysicalExtentSize[kKmMgGtTpPeE]]
[-t|--test]
[-v|--verbose]
[--version]
VolumeGroupName PhysicalVolume [PhysicalVolume...]
There are many options, so we’ll cover the “basic” ones, as usual in the unix world, you may want to check “man vgcreate” to learn the rest.
· -l -> Max Logical Volumes: this parameter specifies how many logical volumes you can have in this volume group. You can change this number afterwards with “vgchange”.
· -p -> Max Physical Volumes: this parameter specifies how many Physical Volumes you can use in this Volume Group, again, you can change this number with vgchange
· -s -> Physical Extent size: this parameter indicates how big is each PE (each chunk of allocated space inside the Volume Group). You shouldn’t think in changing this parameter afterwards, so you should choose the proper size here. How to know? Easy, think of fragmentation, if you make small chunks (4M), you will have less external fragmentation (you’ll be able to allocate more P.E.s contiguously) but internal fragmentation will grow (data fragments will be splattered in different physical parts of the physical drive) so in the case of databases I’ld rather recommend big chunks. In case of small files, better go with small-medium sized Physical Extents. Also, when dealing with big drives, you should go with big extents, as there is a limitation in the number of extents a VG can have.
Once this is understood, let’s go with the creation of our VG, which we call “vg_shares”
vgcreate –l 4 –p 4 –s 32M vg_shares /dev/hdb1
So, now we have our volume group created, we just need to use it to create an LV, and we’ll be able to format it, and mount it actually.
We’ll use “lvcreate” for this purpose, which has a syntax similar to vgcreate (actually all the lvm-related commands are similar)
lvcreate -s|--snapshot
[-c|--chunksize]
[-A|--autobackup {y|n}]
[--addtag Tag]
[--alloc AllocationPolicy]
[-C|--contiguous {y|n}]
[-d|--debug]
[-h|-?|--help]
[-i|--stripes Stripes [-I|--stripesize StripeSize]]
{-l|--extents LogicalExtentsNumber[%{VG|LV|PVS|FREE}] |
-L|--size LogicalVolumeSize[kKmMgGtTpPeE]}
[-M|--persistent {y|n}] [--major major] [--minor minor]
[-n|--name LogicalVolumeName]
[-p|--permission {r|rw}]
[-r|--readahead ReadAheadSectors|auto|none]
[-t|--test]
[-v|--verbose]
[--version]
OriginalLogicalVolume[Path] [PhysicalVolumePath...]
As usual, we’ll check the basic options and leave for “man” the rest of them:
· L (size) -> you can use the size and the unit (M -> megabyte, G -> Gigabyte…)
· n (name) -> place here the name of the logical volume
Let’s assume we want to create a logical volume called lv_shares_management, sized 100G and that we have enough space in vg_shares:
lvcreate –L 100G –n lv_shares_management /dev/vg_shares
Now we have a device file for this logical volume in /dev/vg_shares/lv_shares_management. We can now format the logical volume as if it was a regular partition, with a filesystem of our choose, and then put it in fstab.
Let’s assume ext3:
mkfs.ext3 /dev/vg_shares/lv_shares_management
The line in fstab would look similar to this one:
/dev/vg_shares/lv_shares_management /var/shares/management ext3 defaults 1 2
As a conclusion, let’s see which steps did we follow:
1. Mark a drive or partition as Physical Volume, with the pvcreate command
2. Create a Volume Group using our newly created Physical Volume, by using the vgcreate command
3. Create a Logical Volume, using part of the space available in our new volume group, issuing an lvcreate command
4. Format the newly created logical volume using our favorite FS type and add it to fstab
Tip for low budgeted geeks: if you want to try out LVM2 and don’t have a hard drive around, you can create a PV using a regular file created with dd:
Dd if=/dev/null of=/home/user/pv_file bs=1M count=1024
Or use a virtual machine (VirtualBox is a good free / opensource choice if you don’t want to start with Xen)
Next chapter: Maintaining a Volume Group –extend, clean…-







redundancy
SW Raid + LVM