For Linux users, when installing a Linux machine, one of the problems encountered is to estimate and assign enough hard disk space to each partition. Whether it is a system administrator who is looking for a space for the server, it is a very common problem with a common user who is going to exhaust. The method of solving is usually used using symbolic links, or some tools for adjusting partition size (such as parted). However, this is just a temporary solution, soon, we will face the same problem.
If you are a system administrator, manage the number, the server connected to the Internet, then you will bring a lot loss to the company one minute. In addition, use this method, after modifying the partition table, each time you have to restart the system. LVM (logical volume) can help us solve these problems.
LVM profile
Linux LVM can make management work easier. The LVM is close to the storage space relative to the hard disk and partition. Before using LVM, first look at the relevant concepts that will be used.
Physical volume
The physical volume refers to a hard disk partition or a device similar to a hard disk partition (such as a RAID device).
Logical volume
One or more physical volumes form a logical volume. For LVM, logical volumes are similar to hard disk partitions in non-LVM systems. Logical volumes can contain a file system (such as / home or / usr).
Volume group
One or more logical volumes form a volume group. For LVM, the volume group is similar to a physical hard disk in a non-LVM system. The volume group combines a plurality of logical volumes to form a manageable unit.
LVM work mode
Let's take a look at how LVM works. Each physical volume is divided into several basic units, that is, the so-called PE (Physical Extents). The size of the PE is variable, but must be the same as the physical volume of its belongings. In each physical volume, each PE has a unique number. PE is a minimum unit that can be addressed by the LVM in a physical storage.
Each logical volume is also divided into some basic units that can be addressed, that is, the so-called LE (Logical Extents). In the same volume group, the size and PE of Le are the same, it is clear that the size of Le is the same for all logical volumes in a volume group.
In a physical volume, each PE has a unique number, but it is not necessarily necessary for logical volumes. This is because the logical volume can be composed of some physical volumes when these PE ID numbers cannot be used. Therefore, the LE ID number is used to identify the specific PE associated with it. As mentioned earlier, the Le and PEs are one or one. Each storage area is addressed access or the ID of the LE is used, and the data is written on the physical storage device.
You may feel weird, where all the metadata in logical volumes and logical volume groups have been sent. Similar to the non-LVM system, data about the partition is stored in the partition table, and the partition table is stored in the starting position of each physical volume. VGDA (Volume Group Descriptor Area) function is like a partition table of LVM, which is stored in the start of each physical volume.
VGDA consists of the following information:
◆ A PV descriptor ◆ a VG descriptor ◆ LV descriptor ◆ some PE descriptor
When the system starts LV, the VG is activated and the VGDA is loaded to memory. VGDA helps identify the actual storage location of the LV. When the system wants to access the storage device, the mapping mechanism established by VGDA is used to access the actual physical location to perform I / O operations.
start working
Let's take a look at how to use LVM.
Step 1: Configure the kernel. Before installing the LVM, there should be a LVM module in the kernel, you can use the following steps:
#CD / USR / SRC / Linux # Make MenuConfig
Select Multi-Device Support (RAID AND LVM) submenu, select the following two options: [*] Multiple Devices Driver Support (RAID AND LVM) <*> Logical Volume Manager (LVM) Support.
Step 2: Check the total amount of space over the drive on the drive. This can be completed by the following command:
# DF-h FileSystem Size Used Avail Use% MOUNTED ON / DEV / HDA1 3.1G 2.7G 398M 87% / / DEV / HDA2 4.0G 3.2G 806M 80% / Home / DEV / HDA5 2.1G 1.0G 1.1G 48% / VAR
Step 3: Create an LVM partition on the hard disk. Use fdisk or other partition tool to create an LVM partition. The partition type of Linux LVM is 8E.
# fdisk / dev / hda press p (to print the partition table) And n (to create a new partition)
Step 4: Create a physical volume. The following command will create a volume group descriptor at the beginning of the partition:
# pvcreate / dev / hda6 pvcreate - -physical volume "/ dev / hda6" successful "successful" / dev / hda6 "successful created # pvcreate / dev / hda7 pvcreate- - Physical Volume" / dev / hda7 "successfully created
Step 5: Create a volume group. Create a new volume group by the following method and add two physical volumes:
# Vgcreate test_lvm / dev / hda6 / dev / hda7 vgcreate- - INFO: using default physical extent size 4 MB vgcreate- - INFO: maximum logical volume size is 255.99 Gigabyte vgcreate- - doing automatic backup of volume group "test_lvm" Vgcreate- - Volume Group "Test_lvm" SuccessFully CREATED and ACTIVATED
The above command will create a volume group called Test_LVM, including / DEV / HDA6 and / DEV / HDA7 two physical volumes. Use the following command to activate the volume group:
# vgchange -ay test_lvm
Use the "vgdisplay" command to view the details of the established volume group.
# vgdisplay --- Volume Group --- VG Name Test_lvm VG Access Read / Write VG Status Available / Resizable VG # 0 MAX LV 256 CUR LV 1 Open LV 0 Max LV Size 255.99 GB MAX PV 256 Cur PV 2 ACT PV 2 VG Size 3.91 GB PE Size 4 MB Total PE 1000 Alloc PE / Size 256/1 GB Free PE / Size 744 / 2.91 GB VG UUID T34ZIT-HDPS-UO6R-CBDT-UJEQ-EEPB-GF435E
Step 6: Create a logical volume. Create a logical volume in the volume group using the lvcreate command:
# lvcreate -l2g -nlogvol1 test_lvm
Step 7: Create a file system. Select to use the Reiserfs log file system on this logical volume: # mkreiserfs / dev / test_lvm / logvol1
Use the mount command to load the newly created file system.
# mount -t reiserfs / dev / test_lvm / logvol1 / mnt / lv1
Step 8: Add an entry to / etc / fstab and /etc/lilo.conf. Add the following entry in / etc / fstab, load the file system at startup:
/ dev / test_lvm / logvol1 / mnt / lv1 reiserfs defaults 1 1
If there is no overlay of the original kernel, copy a reconfined kernel and choose whether to use LVM at startup. Below is the content of the LILO file:
Image = / boot / lvm_kernel_image label = linux-lvm root = / dev / hda1 initrd = / boot / init_image ramdisk = 8192
After adding the above, use the following command to reload LILO:
# / sbin / lilo
Step 9: Modify the size of the logical volume. You can easily modify the size of the logical volume using the LVEXTEND command, and increase the logical volume size method is as follows:
# Lvextend -L 1G / dev / test_lvm / logvol1 lvextend - extending logical volume "/ dev / test_lvm / logvol1" to 3GB lvextend - doing automatic backup of volume group "test_lvm" lvextend - logical volume "/ dev / test_lvm / logvol1 "SuccessFully Extended
Similarly, the method of reducing the logical volume size is as follows:
# Lvreduce -L-1G / dev / test_lvm / lv1 lvreduce - -Warning: reducing active logical volume to 2GB lvreduce- - This may destroy your data (filesystem etc.) lvreduce - -do you really want to reduce "/ DEV / TEST_LVM / LV1 "? [Y / N]: Y LvReduce- - doing Automatic Backup of Volume Group" Test_lvm "LvReduce- - logical volume" / dev / test_lvm / lv1 "successfully reduced