Great God Linux Advanced six: Logical Volume Manager (lvm)

1. Understanding

(1) Logical Volume Manager (LVM: Logical Volume Manager): disk partitions under Linux a mechanism for management; it is implemented by Heinz Mauelshagen on the Linux 2.4 kernel, the latest version: a stable version 1.0.5, development a common problem encountered is difficult to decide how to correctly assessing version 1.1.0-rc2, LVM2 and Linux users to install the development version of the Linux operating system, the size of each partition to assign the appropriate hard disk space; ordinary disk partition management after a good logical partitioning can not change its size; when a logical partition can not fit a file, the file is limited by the upper file system can not span multiple partitions to store, so it can not be put on another disk ; encountered when a partition space is exhausted, the solution is often to use a symbolic link or use the tool to adjust the partition size, but this is only a temporary solution that does not solve the problem fundamentally; with the Linux logical Volume Manager the emergence of function, these problems are solved, users can easily without the need to shut down The whole size of each partition.
(1.1) usage scenarios : in their daily work or study, as the use of computers continue to be, our disk space may be getting smaller and smaller, we can not come to replace large hard drive, because the hard disk has a price, the data is priceless. This time we can use LVM, LV storage virtualization, logical volume, you will not be subject to a physical disk with the size of the other, and hardware-related storage settings are hidden, you can not stop or uninstall the application file system adjust the volume or size of such data migration can reduce operating costs, LVM biggest feature is the ability to dynamically manage the disk. Because the size of the logical volume is dynamically adjusted, but without losing existing data. If we integrated the new hard drive, it will not change the existing upper logical volume. As a dynamic disk management mechanism, technology has greatly enhanced the flexibility of logical disk management.
(1.2) mechanism: Underlying physical hard disk by the stroke encapsulated abstract and logical manner presented to the upper application. In traditional disk management mechanism, we have the upper application is direct access to the file system to the physical layer of hard to read. In the LVM, by the underlying hard encapsulation, when we operate on the underlying physical hard disk, which is no longer performed for the operation to partition ashamed, but rather by its underlying logical volume is called a thing disk management operations. LVM is a logical layer between the partition and the file system is added to the lower shielding disk file system partition layout. Provide an abstract storage volume, a file system on a storage volume.
(2) physical storage medium (Physical Storage Media): refers to the physical storage device system: magnetic disks, such as: / dev / hda, / dev / sda the like, the bottom of the storage unit is a storage system.
(3) physical volume (Physical Volume, PV): refers to a disk partition or device (e.g., RAID) having the same function from a logical disk partition, is the basic storage logical block LVM; however, and basic physical storage medium (e.g., partition , disk, etc.) compare, but contains management parameters associated with LVM. LVM allows you to save the current physical volume on each physical volume to 2 parts of a default copy metadata stored at the beginning of the device 2 is stored at the end of the second backup device.
Volume Group (4) (volume group, VG): non-LVM system similar to a physical disk, consisting of one or more physical volume PV composition; can create one or more of LV (logical volumes) in the volume group.
(5) a logical volume (Logical Volume, LV): similar to non-LVM partition system, a logical volume based on the volume VG of groups; LV logical volume on the file system can be established (such as / home or / usr, etc. ).
(6) logic block(Logical Extent, LE): LV logical volumes are also divided into the basic addressable unit may be referred to as LE; in the same volume group, LE and PE are the same size, and one correspondence.
(7) Physical Development (pe): vm smallest memory unit; (created by default size is an integer multiple of 4M; can also be modified)
Here Insert Picture Description

2.LVM common operations

Features command Note
1. Establish lvm Logical Volume ## Note: When first set up the device to establish, and then loaded the logical volume
(1) View test material fdisk -l ## view disk device
(2) Production test material fdisk /dev/vdb ## to / dev / vdb operation
t>>>1>>>L>>>8e>>> t>>>2>>>L>>>8e>>> t>>>1>>>L>>>8e>>>p>>>wq ## three virtual partition lvm
partprobe ## synchronous partition table
(3) ## Adding lvm parameters
watch -n 1 'pvs;echo======;vgs;echo ========;lvs” ## monitoring
pvcreate /dev/vdb{1…3} ## Create a physical partition
vgcreate johnsonlee /dev/vdb{1…3} ## create a volume group johnsonlee
lvcreate -n johnson_curry -L 1.2G johnsonlee ## create a logical volume of johnson_curry 1.2G
ll /dev/mapper/johnsonlee-johnsonlee_curry ## generates a virtual device
2. Delete lvm logical volume ## deleted when the first logical volume to delete, and then delete the device
(1) lvremove -f /dev/datebase/johnsonlee ## experiments delete the logical sub-volume datebase volume group: johnsonlee
(2) vgremove date ## Delete datebase volume group
(3) pvremove /dev/vdb* ## Delete physical partition
3 and mount the logical volume formatted
(1) mkfs.ext4 /dev/johnsonlee-johnsonlee_curry ## formatted logical volume device
(2) temporary mount mkdir /lvm ## establish mount directory
mount /dev/johnsonlee-johnsonlee_curry /lvm ## temporary mount device to the file system
(3) permanently mounts mount tail -n 1
vim /etc/tstab ## Open the configuration file
/dev/mapper/johnsonlee-johnsonlee_curry /lvm ext4 defaults 0 0 ## ## edit the configuration file
mount -a ## Verify that the entry into force
4.1 sufficient tensile physical partition (ext2 / 3/4) ## Note: (1) stretching it, first stretching apparatus, stretched and then file system; (2) off-line stretching then need to uninstall
(1) watch -n 1 ‘pvs;echo======;vgs;echo ========;lvs” ## Input Monitoring command
(2) lvextend -L 1G /dev/johnsonlee-johnsonlee_curry ## directly to the stretching 1G
lvextend -L +200M /dev/johnsonlee-johnsonlee_curry ## basis of the original drawing 200M
(3) lvs ## to see whether the logical device stretching success
(4) df -h ## whether the size of the file system and found no success stretching into force
(5) resize2fs /dev/johnsonlee-johnsonlee_curry ## update the file system
(6) df -h ## whether the size of the file system and found no success stretching into force
4.2 insufficient tensile physical partition (ext2 / 3/4 2.5G) ## Note: (1) stretching it, first stretching apparatus, stretched and then file system; (2) off-line stretching then need to uninstall
(1) watch -n 1 ‘pvs;echo======;vgs;echo ========;lvs” ## Input Monitoring command
(2) Create a physical volume
n 4 >>>n 5 >>>t 5 L 8e >>>p>>>wq ## to 5 virtual partition lvm
partprobe ## synchronous partition table
pvcreate /dev/vdb5 ## 创建物理分区
(3) vgextend johnsonlee /dev/vdb5 ##将新的物理卷添加到物理卷组
(4) vgs ## 查看拉伸后的物理卷组的大小
(5) lvextend -L 2.5G /dev/johnsonlee-johnsonlee_curry ##扩展逻辑卷大小到2.5G
(6) lvs ##查看逻辑卷设备是否拉伸成功
(7) df -h ## 文件系统的大小是否拉伸成功发现并没有生效
(8) resize2fs /dev/johnsonlee-johnsonlee_curry ##更新文件系统
(9) df -h ## 文件系统的大小是否拉伸成功发现并没有生效
4.3xfs格式逻辑卷的拉伸(不能缩减1G>>>1.4G)
(1) 格式化 mkfs.xfs /dev/datebase/johnsonlee
(2) 确定卷组是否有足够的空间可以拉伸 vgs
(3)逻辑卷拉伸命令 lvextend -L 1.8G /dev/datebase/johnsonlee ## 拉伸到1.8G
lvextend -L +300M /dev/datebase/johnsonlee ##在原有的基础上+300M
(4)查看拉伸后的空间大小 lvs ##查兰逻辑卷是否拉伸成功
df -h ##查看文件系统的大小是否拉伸成功
(5) 更新文件系统 xfs_growfs /dev/datebase/johnsonlee
(6) 查看 更新后的文件系统 df -h
5.逻辑卷的压缩(ext2/3/4) ##注意:压缩的话,先压缩文件,再压缩设备
(1) ##卸载逻辑卷 umount /dev/mapper/johnsonlee-johnsonlee_curry
(2)对文件系统进行校验 e2fsck -fv /dev/johnsonlee-johnsonlee_curry
(3)缩小文件系统到1G resize2fs /dev/johnsonlee-johnsonlee_curry 1G
(4)缩小逻辑卷 lvreduce -L 1G /dev/johnsonlee-johnsonlee_curry
(5)查看卷组中是否能有空闲的分区,有的移除; vgreduce johnsonlee /dev/vdb3 ##删除vg分区 3
premove /dev/vdb3 ## 删除物理分区3
6.逻辑卷的快照 ## 如果想要获取以前快照的数据。删除快照,重新照
(1) umount /data ##先卸载再进行快照
(2) lvcreate -L 10M -n lve-snap -s /dev/johnsonlee-johnsonlee_curry ##建立快照
(3) lvs ## 查看
(4) mount /dev/johnsonlee-johnsonlee_curry /lvm ##挂载快照
(5) cd /lvm ##打开目录
(6) touch file ##创建文件
(7) mkfs.ext4 /dev/johnsonlee/ lve-snap ##格式化
(8) lvremove /dev/johnsonlee/ lve-snap ## 删除快照
(9) lvcreate -L 10M -n lve-snap -s /dev/johnsonlee-johnsonlee_curry ##新建快照
(10) mount /dev/johnsonlee-johnsonlee_curry /lvm ## ##挂载快照
(11) ls ##发现文件依然在

3.LVM操作结果

RESULT1:建立lvm逻辑卷
Here Insert Picture DescriptionRESULT2:删除lvm逻辑卷

Here Insert Picture DescriptionRESULT3:逻辑卷挂载以及格式化
Here Insert Picture DescriptionRESULT4.1 物理分区够用的拉伸(ext2/3/4)
Here Insert Picture Description
RESULT4.2 物理分区不足(2.5G)的拉伸(ext2/3/4)

Here Insert Picture DescriptionRESULT4.3逻辑卷的拉伸(xfs)
Here Insert Picture Description
RESULT5 逻辑卷的压缩(ext2/3/4)
Here Insert Picture DescriptionRESULT6逻辑卷的快照
Here Insert Picture Description

4.例题

Here Insert Picture Description
答案:
Here Insert Picture Description

发布了33 篇原创文章 · 获赞 8 · 访问量 2285

Guess you like

Origin blog.csdn.net/Pierce110110/article/details/103004642