Without Logical Volume Management (LVM), the decision on how to partition your hard drives during the Red Hat Linux installation is critical. Once drives are partitioned, there is no easy way to expand the available space.
For example, assume you ve set up the /home directory in a separate partition. You ve planned ahead and assumed that you ll have enough space for 10 users. But your company expands, and suddenly, you need space on /home for 20 users. Without LVM, there is no easy way to expand the size of the /home partition. You would need to back up the files from /home , find a partition with the space that you need, and then restore the files to that new partition.
LVM allows you to reallocate chunks of disk space between different filesystems. So with LVM, if you have extra room in a filesystem such as /var , you can reallocate that space to /home .
You can create a LVM volume group during the installation of Red Hat Linux, as described in Chapter 03 . You can also create and manage a LVM volume group using the techniques described in the following sections. Even if you ve already created a LVM volume group when you installed Red Hat Linux, read on. LVM does not help unless you can use the commands described in this section to increase and decrease the size of your logical volumes .
LVM is essentially a mapping of different physical sections of a hard drive. Once collected together into a logical volume, filesystems such as /home and /usr can be mounted on that volume. Logical volumes can be reorganized to include additional hard drive space.
That is the short version of what you can do with LVM. To really understand what happens in LVM, start with some fundamental definitions:
Physical volume (PV) A physical volume (PV) usually corresponds to a standard primary or logical partition on a hard drive.
Physical extent (PE) A physical extent (PE) is a chunk of disk space. Physical volumes are divided up into a number of equal sized PEs.
Logical extent (LE) A logical extent (LE) is a chunk of disk space. The size of an LE in an LVM system is the same as the size of PEs on that system. Every LE corresponds to a specific PE.
Logical volume (LV) A logical volume (LV) is a collection of LEs. You can mount filesystems such as /usr and /boot on an LV.
Volume group (VG) The LVs on your system, collected together, form a volume group (VG). When you configure an LVM system, most commands are applied to a VG.
If you re implementing LVM for the first time, it may be more convenient to configure it on a new hard drive. After installing the drive, do not install any partitions on it yet. You can create a PV on an entire hard drive. For example, if the hard drive is the slave on the secondary IDE connector, the Linux device is /dev/hdd . To create a PV on that disk, run the following command:
# pvcreate /dev/hdd
Alternatively, if you ve already set up partitions with a utility such as fdisk , you can set up PVs on specific partitions. First, run fdisk to change the system ID of the desired partition. Once you re in the fdisk menu, the following commands would change hypothetical partition number 10 on a hard drive:
Command (m for help): t Partition number (1-15): 1 Hex code (type L to list codes): 8e
Don t use this command on any partition where you want to keep the data. Once the type is changed to Linux LVM, you can then create a physical volume with a command such as:
# pvcreate /dev/hdd1
Once you ve configured two or more PVs, the next step is to create a volume group.
A VG is a collection of PVs that are configured on one or more hard drives. You can create a VG from existing PVs. When you add more PVs, you can add them to existing VGs.
It s easy to create a VG. You can even give that VG the name of your choice, such as programs, with a command such as:
# vgcreate programs /dev/sdc1 /dev/sdd1
Once you have a VG, it s easy to add PVs with a slightly different command:
# vgextend programs /dev/sde1
Now you can organize a VG into chunks that you can set up in a PV.
Finally, you can create a logical volume where you can mount a filesystem such as /home or /var . But first, you need to know the size of a PE in your volume. You can do this and more with the vgdisplay command. Using the VG created in the previous section, this requires a command like:
# vgdisplay programs
A sample output is shown in Figure 7.12. As you can see, this includes information on the maximum number of logical and physical volumes for this group, the size of this volume group, and the size of PEs in this group (in the figure, it s 4MB).
Now you can create an LV of the size that you need, with a command such as the following:
# lvcreate -l num_of_PEs programs -n logicvol
From the above example, the name of the new LV is logicvol . You know that each PE is 4MB in size. If you wanted to set up a 200MB logicvol LVM partition, substitute 50 for num_of_PEs .
This creates a new device, /dev/programs/logicvol . You can now format and mount that device just like any other hard drive partition. For example, the following commands format it to the ext3 filesystem and mount it on the /tmp directory:
# mkfs -j /dev/programs/logicvol # mount -t ext3 /dev/programs/logicvol /tmp
Now it s easy to increase the size of /dev/programs/logicvol . Assuming you have spare PEs, just use the lvextend command. The following example increases the size of /dev/programs/logicvol to 300MB:
# lvextend -L300M /dev/programs/logicvol