Introduction to LVM
LVM (Logical Volume Management) is an extremely handy tool to learn because it allows storage to be handled in a very easy manner. LVM allows you to abstract various pieces of physical storage into groups that can be carved into chunks that form the basis of file systems (virtual partitions if you like). It also allows you to combine physical partitions into groups, resize these groups (grow or shrink), and effectively manage these groups.
Here is a easy diagram shows disk,PV, VG, and LV relationship in LVM
sda1 sdb1 (PV:s on partitions or whole disks) \ / \ / diskvg (VG) / | \ / | \ usrlv rootlv varlv (LV:s) | | | ext2 reiserfs xfs (filesystems)
LVM makes things so convience that even small system can get benefit from it. If you are managing a server, or a cluster, then LVM definitely is your choice. Here are some concepts.
Physical Volumes (PV’s) are really partitions on a disk (hence the use of the term “physical”). Above the PV’s is one or more Volume Groups (VG). Then you partition the VG into Logical Volumes (LV’s). These LV’s are used for creating file systems.
There are some fundamental concepts in LVM that you need to understand (and master). the diagram above shows the main components in LVM.
In this case there are two drives,
/dev/sdb. Each of these drives has two partitions.
Check disk drive partitions, the partition type should be “8e”. When you list the partitions using fdisk, the partitions that are PV’s should read “8e” which corresponds to “Linux LVM” as shown below.
# fdisk -l /dev/sdb Disk /dev/sdb: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 30400 244187968+ 8e Linux LVM /dev/sdb2 30401 60801 244196032+ 8e Linux LVM
Notice the partition type. This has to be done for every partition on the drives being used with LVM.
Once the PV's are created, using LVM is fairly easy by just knowing a few commands. The first command is how to make the physical volumes (PV's) using the command "pvcreate".
# pvcreate /dev/sdb1 /dev/sdc1 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdc1" successfully created
If you like, you can get more extensive information about the PV's with a simple command, "pvdisplay".
# pvdisplay "/dev/sdb1" is a new physical volume of "232.88 GB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 232.88 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID j4XKT6-OI2y-cPMK-YpNR-gmgc-es67-ey4CV2 "/dev/sdc1" is a new physical volume of "232.88 GB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 232.88 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID qLC1pM-y07j-3Pq4-waiV-BRzc-vySF-KTesHH
Notice that each PV is given a PV UUID. This is a unique address that identifies the PV. Also notice that it tells you if the PV has been assigned to a VG (Volume Group). In this case, none of the PV's have been assigned to a VG.
The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit. You can think Volume Group (VG) as the “virtual hard drive”. It gathers real partitions (PV’s) into a virtual drive (the VG).
The next step is to create the VG (Volume Group) from the PV's using the command "vgcreate".
# vgcreate diskvg /dev/sdb1 /dev/sdc1 Volume group "diskvg" successfully created
Notice that the first argument to
vgcreate is the name of the VG (in this case, diskvg). Similarly to "pvdisplay" there is a "vgdisplay" command.
# vgdisplay --- Volume group --- VG Name diskvg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 465.76 GB PE Size 4.00 MB Total PE 119234 Alloc PE / Size 0 / 0 Free PE / Size 119234 / 465.76 GB VG UUID oNH6jk-PBE0-mR0c-aaDi-3Fys-y5SQ-0tVaxX
If you don't happen to remember the name of a VG or you happen upon a new system to administer, then you can use a command called "vgscan" that will tell what VG's are on the system.
# vgscan Reading all physical volumes. This may take a while... Found volume group "diskvg" using metadata type lvm2
This command will scan all disks for VG's. This is very useful if you've forgotten the layout of the PV's and VG's. It's also HIGHLY recommended that you keep written notes of the LVM layout in case someone has to pick up administration of the system.
The equivalent of a disk partition in a non-LVM system. The LV is visible as a standard block device; as such the LV can contain a file system. You can think of the LV’s as “virtual partitions.” Then you can create file systems or whatever else you need to do with the LV’s.
The next step after creating the VG is to partition it or create LV's using the command "lvcreate". For this example, only a single LV will be created.
# lvcreate --name /dev/diskvg/home_lv --size 300G Logical volume "home_lv" created
The name used in the lvcreate command is the full path of the LV (this is another reason it is a good idea to keep really good notes). You can get information on the LV using the command "lvdisplay".
# lvdisplay --- Logical volume --- LV Name /dev/diskvg/home_lv VG Name diskvg LV UUID BXXUyW-z8vS-6tgR-N5SW-tttW-j6Yb-6XEyya LV Write Access read/write LV Status available # open 0 LV Size 450.00 GB Current LE 115200 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
Notice that it tells you the VG that is used for the LV ("diskvg" in this case). There is lots of other information in the output as well. It's highly recommended you look at the man pages and tutorials on the web to learn this information.
As with VG's, you can always scan for the Logical Volumes using "lvscan".
# lvdisplay # lvscan ACTIVE '/dev/diskvg/home_lv' [450.00 GB] inherit
The output from the command with the default options is fairly simple but there is a "verbose" option that will give you more information.
At this point, you are ready to create a file system on this LV. In this case, an ext3 file system will be created.
# mkfs.ext3 /dev/diskvg/home_lv mke2fs 1.41.7 (29-June-2009) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 29491200 inodes, 117964800 blocks 5898240 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 3600 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Notice that the "partition" used is the full path to the LV,
/dev/diskvg/home_lv. After the file system is created, you can mount the file system as you would any other.