LVM introduction:
LVM (Logical Volume Manager) Chinese name: Logical Volume Manager, essentially a virtual device driver, is a new addition between the block device and the physical device in the kernel Abstract level, it can combine several disks (physical volume, PhysicalVolume) to form a storage pool or volume group (VolumeGroup).
LVM can create new logical devices by dividing logical volumes of different sizes (LogicalVolume) from the volume group each time. The underlying original disk is no longer directly controlled by the kernel, but controlled by the LVM layer. For upper-level applications, volume groups have replaced disk blocks as the basic unit of data storage.
LVM manages the physical extents of all physical volumes and maintains the mapping between logical extents and physical extents. LVM logical devices provide the same functions as physical disks to upper-layer applications, such as file system creation and data access. However, LVM logical devices are not restricted by physical constraints. The logical volume does not have to be a contiguous space. It can span many physical volumes and can be arbitrarily resized at any time. Compared with physical disks, it is easier to manage disk space.
The advantages and disadvantages of LVM:
We usually use computers. When the disk space is insufficient, there is generally no way to expand the disk space. We can only add new ones The hard disk can be used only after partitioning the new hard disk and formatting it. Of course this is sufficient for our personal computers, except for a bit of trouble. However, in a working environment, it is difficult for us to shut down the server and add a new hard disk. We must know that the server running important services is generally running 24 hours a day. At this time, we can use LVM to dynamically change the disk of the system. manage.
< /p>
Basic concept of LVM mechanism:
Schematic diagram of volume group (VG):
Term explanation:
The physical media (The physical media): This refers to the storage device of the system: hard disk, such as: /dev/hda1, /dev/sda, etc., is the lowest storage unit of the storage system.
Physical volume (physical volume) PV: Physical volume refers to a hard disk partition or a device that logically has the same function as a disk partition (such as RAID ) Is the basic storage logic block of LVM, but compared with basic physical storage media (such as partitions, disks, etc.), it contains management parameters related to LVM.
Volume Group (Volume Group) VG: LVM volume group is similar to the physical hard disk in non-LVM system, which is composed of physical volumes. One or more “LVM partitions” (logical volumes) can be created on the volume group. The LVM volume group is composed of one or more physical volumes.
Logical volume LV: LVM logical volume is similar to hard disk partition in non-LVM system, and can be created on logical volume File system (such as /home or /usr, etc.).
Physical extent PE: Each physical volume is divided into basic units called PE (Physical Extents), with a unique number The PE is the smallest unit that can be addressed by LVM. The size of the PE is configurable and the default is 4MB.
- Logical extent (logical extent) LE: Logical volumes are also divided into basic addressable units called LE (Logical Extents). In the same volume group, the size of LE and PE are the same, and there is a one-to-one correspondence.
LVM management commands:
Main command:
pvcreate command: Create a new physical Volume (format: pvcreate…device name 1…[device name 2])
vgcreate command: create a new volume group (format: vgcreate…volume Group name…Physical volume name 1…Physical volume name 2)
lvcreate command: Create a new logical volume (format: lvcreate…-L.. .Capacity…-n…logical volume name…volume group name)
lvextend command: add storage space for logical volume (format: lvextend.. .-L…+Capacity…/dev/Volume group name/Logical volume name)
The process of creating a logical volume by LVM (actual operation):
First Step: Create a physical volume (PV).
1. Add three 20G hard disks to the virtual machine, and then restart the Linux system (the hard disk can only be recognized after the system is restarted, and restart with the “reboot” or “init6” command).
< p>2. Use the fdisk command to check whether the hard disk is added successfully. Input: fdisk -l
3. Configure the disk sdb, allocate all the space to a primary partition, and then convert the partition format to LVM format. (I wrote about the content of disk management in the previous blog. If you don’t know how to do it, you can check it out)
Enter: fdisk /dev/sdb----------Manage Disk sdb
input: n---------------------------new partition
input: p------ --------------------Select the partition type as the primary partition
Enter ------------------ -------------The default partition number is set to 1
Enter ------------------------- ------Set the starting sector by default
Enter ------------------------------Default allocation Full space for partition 1
input: t--------------------------Convert the partition format
input: 8e-- ---------------------LVM format code (you can enter L query if you don’t know)
Enter: w--------- ---------------Save and exit
4. Use the above method to configure the disk sdc and sdd as in the disk sdb, save and exit the disk configuration panel. (Because the process is the same, I won’t go into details)
5. Use the fdisk command again to check whether the disk partition is configured successfully. Input: fdisk -l
6. Use the pvcreate command to create three disk partitions into three physical volumes.
(I used regular expression input, which is more convenient, and the command line result is the same as “pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1”) input: pvcreate / dev/sd{b..d}1
Step 2: Create a new volume group (VG).
1. Add physical volumes sdb1, sdc1, sdd1 to the volume group mailbox.
Input: vgcreate mailbox /dev/sd{b..d}1---------Create a new volume group mailbox and add physical volumes sdb1, sdc1, sdd1 into it
Input: pvscan-------------------------------------------View physical volume information
< /p>
2. Check the detailed information of the volume group mailbox.
input: vgdisplay----------display volume group details
Step 3: Create a new logical volume (LV)
h2>
1. Create a new logical volume mbox01 for the mailbox volume group. (The size of my disk is 20G, and the logical volume of 25G can obviously be created across disks)
Enter: lvcreate -L 25G -n mbox01 mailbox----------for mailbox volume The group creates a new logical volume named mbox01 with a capacity of 25G
input: lvdisplay------------------------------ -----------------Display detailed information about logical volumes
2. Create a logical volume mbox02 for the mailbox volume group and set the capacity to 15G.
Input: lvcreate -L 15G -n mbox02 mailbox
Input: lvdisplay
3. If the space of the logical volume mbox01 is less, we can extend it by 10G Space.
Input: lvextend -L +10G /dev/mailbox/mbox01--------Extend mbox01 by 10G space, "+" means how much capacity to increase, if not with "+" means expansion How much capacity.
Input: lvdisplay
Step 4: Format and mount the logical volume
1. Format the logical volume mbox01 and mbox02.
input: mkfs.xfs /dev/mailbox/mbox01----------format the logical volume mbox01 into xfs file format
input: mkfs.xfs /dev/ mailbox/mbox02----------format the logical volume mbox02 into the xfs file format
2. Create two new directories under the /mnt/ directory as the mount points of the two logical volumes .
Enter: mkdir /mnt/mbox0{1..2}----------Create two new directories named mbox01 and mbox02 under the /mnt/ directory
Input: ls /mnt/-------------------------------- view files or directories under /mnt/ directory pre>3. Add the automatic mounting commands for logical volumes mbox01 and mbox02 in the /etc/fstab file. (I wrote about auto-mounting in my previous blog, friends who don’t know how to do it can take a look)
Enter: vim /etc/fstab---------------- -------------------------------------------------- ------------Use the vim editor to edit the fstab file
Enter: /dev/mailbox/mbox01 /mnt/mbox01 xfs defaults 0 0--------- -Add auto-mount command
Enter: /dev/mailbox/mbox02 /mnt/mbox02 xfs defaults 0 0----------Add auto-mount command
Enter: mount -a -------------------------------------------------- ----------------------------------Mount all devices
Input: df -hT--- -------------------------------------------------- -----------------------------------View mounting informationLVM expansion:
The expansion of the logical volume (LV):
The ext2/ext3/ext4 file system, the adjustment command is resize2fs, or directly use "-r" to automatically expand the file system size:
< p>1 .Unmount the mbox01 logical volume, and then format the file format as ext4.
input: umount /dev/mailbox/mbox01-------------unmount mbox01
input: mkfs.ext4 /dev/mailbox/mbox01-- --------The formatted file format is ext4
2. Re-mount mbox01, and then view the mounting details.input: mount /dev/mailbox/mbox01 /mnt/mbox01----------mount mbox01
input: df -hT-------- -------------------------------------------------- ---View mounting details3. We added 1G capacity to mbox01, and then checked the mounting details and found that the capacity did not increase.
Input: lvextend -L +1G /dev/mailbox/mbox01---------------Increase capacity
Input: df -hT---- -------------------------------------------------- -------View mounting details4. Use the resize2fs command to perform capacity adjustment, and then check that the capacity increase is successful.
input: resize2fs /dev/mailbox/mbox01-------------------------execute adjustment
input: df -hT------------------------------------------------ -------------View mounting details5. Use the "-r" option to directly and automatically expand the file system size.
Input: lvextend -L +1G /dev/mailbox/mbox01 -r-------------Increase the capacity of mbox01 by 1G and automatically expand
Input: df -hT---- -------------------------------------------------- -------View mounting detailsThe adjustment command for the xfs file system is xfs_growfs, or directly use "-r" to automatically expand the file system size:
1. If you are using the mbox02 process If the capacity is not enough, we can directly expand the capacity, but the premise is that there is enough capacity to support the volume group.
Input: lvextend -L +5G /dev/mailbox/mbox02----------Add 5G capacity to the mbox02 logical volume
Input: lvdisplay /dev/mailbox/mbox02----- --------------Display the detailed information of mbox02 logical volume2. We then used the df -hT command to check the mount information and found that the capacity of mbox02 did not increase. This is because the file system has not been synchronized, we just need to synchronize it with the xfs_growfs command.
Enter: df -hT-------------------------------------- -----------View mounting details
input: xfs_growfs /dev/mailbox/mbox02-----------execute adjustment
input: df -hT -------------------------------------------------Check Mounting details
3. Use "-r" to directly expand the file system size automatically.
Input: lvextend -L +1G /dev/mailbox/mbox02 -r-------------Increase the capacity of mbox02 by 1G and automatically expand
Input: df- hT------------------------------------------------- ------------View mounting detailsVolume group (VG) expansion:
1. If we find that the remaining space of the volume group is insufficient, first We add a hard drive.
< p>2. After adding the hard disk, the system is usually restarted, but the server in the enterprise cannot be restarted at will. Here we use the echo "---"> /sys/class/scsi_host/host0/scan command to rescan the scsi bus to add Disk.
input: echo "---"> /sys/class/scsi_host/host0/scan----------scan scsi bus to add hard disk
input: fdisk -l /dev/sde---------------------------------------------- ----View the newly added disk sde3. Format the new disk and add it to the mailbox of the volume group to check that the space has been added successfully.
Input: pvcreate /dev/sde-----------------------Create a new physical volume /dev/sde
Input: vgextend mailbox /dev/sde----------Add the disk /dev/sde to the volume group mailbox
Input: vgs----------- --------------------------------View volume group information