LVM: Logical volumn manager logical volume Management
What is used?
LVM converts a storage device into a PV, organizes one or more PVS into one or more volume groups, and then creates a LV on the volume group. After creating the LV and formatting it, you can mount and use it. The benefits of using LVM to manage hard disks are that you can adjust the partition size without shutting down and expand the disk easily. A single partition can be located on multiple hard disks. The size of each partition is not required. You can create a snapshot volume.
Glossary
PV: physical volume, located at the bottom layer of LVM. Each PV is composed of one partition. VG can be added only after the partition is marked as PV.
VG: volume group, which consists of one or more PVS. Volume groups are used to manage PVS. You can delete and add PVS here. The PE size is also specified here.
PE: physical disk located in PV. The default value is 4 m. The unit can only be 2 to the Npower, 4 m, 8 m, and so on. You cannot change the volume group after it is specified during volume group creation. Is the minimum capacity unit of the volume group allocated to the LV.
LV: logical volume. After creating the LV, format the LV and then mount it to use it.
Snapshot volume: Takes a snapshot of a partition in a short time. It is a special type of LV. Generally, data cannot be written here for backup. Application Scenario: it may take an hour to back up a partition. If the first copy is File A, and the last copy is file B, the content of File A may be one hour ago. It may have been an hour since file B was copied, the content of file B is one hour later. In this way, the entire data is shown in the beginning and end, which is quite inappropriate. If there is no image volume, you need to stop writing the original volume and then back up the volume. The delay can be imagined. The Snapshot circle stores the changed files after the snapshot in its own space and stores the unchanged files in the original partition. With this mechanism, the data backed up through the snapshot volume is at the same time. The Snapshot volume size is determined by the number of changed files. The maximum size is the same as that of the original volume. The Snapshot volume and the original volume must be on the same VG.
LVM creation sequence: Hard Disk Partition --> convert partition to PV --> Create VG --> Create LV --> Format LV --> Mount
Note: When partitioning a hard disk, you must mark the partition type as 8e. If this parameter is not marked, the PV may be obtained to another system and the PV of this partition cannot be scanned.
Command
PV commands
Create a volume group using pvcreate
PVs: view the physical volume group of the current system
Pvdisplay View Details of the physical volume group of the current system
Pvremove remove PV
For example, pvremove/dev/sdb1
Pvmove moves the data in the specified PV to other PVS, provided that the other PVS have sufficient space.
For example, pvmove/dev/sdb1
Pvscan: When PV is moved to another computer, PV scan is performed,
VG commands
Vgcreate create VG
-S: Specifies the PE size, in the unit of K, M, G, T, E, P, but if it is 2 to the Npower, no special requirements do not need to be specified.
Vgs displays the VG information of the current system (vg0 is provided by the system .)
Vgdisplay displays the VG details of the current system
Vgremove command to delete VG
Vgextend VG Extension
Vgreduce reduces VG. If PV in VG has data, you need to use pvmove to move the data in PV.
Vgscan VG scan command
Vgrename VG Renamed
LV commands
Lvcreate create LV
-N lvname: Specifies the LV name.
-L 2G: the Common Unit of LV size is m, M, G, G, T, T. For example, 2g is specified here.
-S: Create a snapshot volume
-P specifies the permission. r indicates read-only, RW indicates read and write.
Lvcreate-N mylv-l 2500g myvg
LVS displays LV
Lvdisplay displays LV For more details
Lvremove remove LV
Lvextend LV Extension
Lvreduce LV reduction
Lvscan LV
Other commands
Resizee2fs command for adding or reducing file systems
Fuser: Check whether a mount point is used by a process.
Ex2fsck detects the ext system file system.
Create a 2 tb lv and extend it to 2.5 TB. Then, create a snapshot volume for it. (Using ext as an example)
Create LVM
1. Five GB hard disks are mounted to the system. First, two hard disks are partitioned and the specified partition type is marked as 8e.
#fidsk /dev/sdc
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DD/wKioL1PH61CzbqZgAAM-J3QtguE925.jpg "Title =" 1.png" alt = "wKioL1PH61CzbqZgAAM-J3QtguE925.jpg"/>
2. Create a hard disk in this partition to a PV.
#pvcreate /dev/sd{c,d}1
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKiom1PH6n7Rw05wAABt96r33YQ840.jpg "style =" float: none; "Title =" 2.png" alt = "wkiom1ph6n7rw05waabt96r33yq840.jpg"/>
Prompt: Creation successful
#pvs
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKioL1PH65jSMSzrAACA5TN58RM874.jpg "style =" float: none; "Title =" 3.png" alt = "wkiol1ph65jsmszraaca5tn58rm874.jpg"/>
/Dev/sda2 comes with the system.
3. Create a volume group
#vgcreate myvg /dev/sd{c,d}1 #vgs
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3E/DE/wKiom1PH6n7hGcL6AADAPXR_2k4926.jpg "style =" float: none; "Title =" 4.png" alt = "wkiom1ph6n7hgcl6aadapxr_2k1_6.jpg"/>
Run the pvdisplay command to view more detailed information.
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKioL1PH65jRi3ZxAAKFILPqWkw789.jpg "style =" float: none; "Title =" 5clipboard.png "alt =" wkiol1ph65jri3zxaakfilpqwkw789.jpg "/>
4. After creating VG, you can create LV.
#lvcreate -n mystor -L 999g myvg #lvs
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKiom1PH6n7iYsM7AACjNxHhIuM695.jpg "style =" float: none; "Title =" 6.png" alt = "wkiom1ph6n7iysm7aacjnxhhium695.jpg"/>
After creating the LV, the device file is displayed in the directory with the same name as VG under/dev.
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3E/DE/wKioL1PH65iw3lePAABpu0ihyp0839.jpg "style =" float: none; "Title =" 7.png" alt = "wkiol1ph65iw3lepaabpu0ihyp0839.jpg"/>
5. Format and mount the file.
#mkfs -t ext4 /dev/myvg/mystor #mount /dev/myvg/mystor /data
650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/> 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKioL1PH65iCDeYJAAD4CTWhst0303.jpg "style =" float: none; "Title =" 8.png" alt = "wkiol1ph65icdeyjaad4ctwhst0303.jpg"/>
Extended LVM
1. Create a new hard disk partition and add it to VG.
#vgextend myvg /dev/sde1 /dev/sdf1#vgs
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKiom1PH65LStvLaAADv2Pw6GJE014.jpg "style =" float: none; "Title =" 9.png" alt = "wkiom1ph65lstvlaaadv2pw6gje014.jpg"/>
2. Added and started to expand LV.
#lvextend -L +500g /dev/myvg/mystor
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKioL1PH7KyxKB6tAAB7ulWXWlw919.jpg "style =" float: none; "Title =" 10.png" alt = "wkiol1ph7kyxkb6taab7ulwxwlw919.jpg"/>
Succeeded
#lvs
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKioL1PH7KzhzFB8AACq99GXM8I927.jpg "style =" float: none; "Title =" 11.png" alt = "wkiol1ph7kzhzfb8aacq99gxm8i927.jpg"/>
3. The file system is extended.
#resize2fs /dev/myvg/mystor
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKiom1PH65OBVWHEAAEcspfnlEs274.jpg "style =" float: none; "Title =" 12.png" alt = "wkiom1ph65obvwheaaecspfnles274.jpg"/>
Before Expansion
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3E/DE/wKioL1PH7KygEmTQAADxQt4AWDE868.jpg "style =" float: none; "Title =" 13.png" alt = "wkiol1ph7kygemtqaadxqt4awde868.jpg"/>
After expansion
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKiom1PH65PBiQQiAAEBgNCRt5s080.jpg "style =" float: none; "Title =" 14.png" alt = "wkiom1ph65pbiqqiaaebgncrt5s080.jpg"/>
LVM creates a snapshot volume
Before creating a snapshot volume, make sure there is no program to write the original volume. Otherwise, when creating a snapshot, the file content is changed as the temporal snapshot. To minimize the LV usage during snapshot creation, the three commands are put together for execution. The first command means to temporarily set the mount point of the original volume to read-only, the second command creates a snapshot volume, and the third command restores the original volume mount point modified earlier.
mount -o remount,ro /dev/myvg/mystor && lvcreate -L 1g -s -p r -n mystor-snap /dev/myvg/mystor && mount -o remount,rw /dev/myvg/mystor /data
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3E/DE/wKiom1PH7EqSiJVxAABYlq4hrKs071.jpg "style =" float: none; "Title =" 15.png" alt = "wkiom1ph7eqsijvxaabylq4hrks071.jpg"/>
The command is too long to be fully intercepted.
Then mount the snapshot volume
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3E/DE/wKioL1PH7WSBXHG_AAB9xMxuzX8369.jpg "style =" float: none; "Title =" 16.png" alt = "wkiol1ph7wsbxhg_aab9xmxuzx8jw.jpg"/>
After the backup is complete, the snapshot volume is detached and then deleted. It makes no sense to leave a snapshot volume.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3E/DE/wKiom1PH7ErQZcgXAAA6-nrqB8c991.jpg "style =" float: none; "Title =" 17.png" alt = "wKiom1PH7ErQZcgXAAA6-nrqB8c991.jpg"/>
Summary
To create an LVM, follow these steps: Create a partition on fdisk and set the partition type to 8E (Linux LVM) --> Create a PV --> Create a VG (whether to specify the PE size) --> Create LV --> Format LV --> Mount
Step for LV Extension: If the VG space is insufficient, you need to add space for the VG. If the space is sufficient, use the lvextend command to extend the partition capacity, and then use the resizee2fs command to expand the partition capacity.
Snapshot volume: The volume must be in the same VG as the original volume, and the VG must have enough space. When creating a snapshot volume, ensure that the original volume has no program access. Try to create the original volume at the minimum time. Mount the snapshot volume. After the backup is complete, delete the snapshot volume.
LV reduction: uninstall LV (umount) --> Use (e2fsck-F) to forcibly detect the file system, make sure there is no problem after detection --> use the resize2fs command to scale down the file system --> use the lvreduce command to scale down --> mount the reduced LV. Note: The minimum reduction must not be lower than the used space.
VG reduction: Use the pvmove command to move the files on the PV to other PVS. Other PV space must be sufficient --> use the vgreduce command to remove PV.
Finally, I attached what Marco Ma said: "There is a danger to scale down. Please be careful when performing this operation !"
This article from the "red face easily die lofty sentiments long" blog, please be sure to keep this source http://wukui.blog.51cto.com/1080241/1439748