LVM是Linux系统中对磁盘分区进行管理的一种逻辑机制,它是建立在硬盘和分区之上,文件系统之下的一个逻辑层,在建立文件系统时屏蔽下层的磁盘分区布局,能够保持现有数据不变的情况下动态调整磁盘容量,提高磁盘灵活性
在虚拟机中安装一块新硬盘,大小为8G
fdisk -l 识别系统中的磁盘设备
[[email protected] ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000b85f2 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2611 20458496 8e Linux LVM Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
然后对新硬盘设备/dev/sdb进行分区,设置大小并更改分区类型为LVM
[[email protected] ~]# fdisk /dev/sdb ... Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1044, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM)
新建两个分区并查看分区情况
Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa5851005 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 8e Linux LVM /dev/sdb2 263 524 2104515 8e Linux LVM
w命令保存退出fdisk分区工具
变更硬盘的分区设置后,建议将系统重启,或者执行partprobe命令获取新的分区情况
[[email protected] ~]# partprobe /dev/sdb
建立pv、vg、lv。将lv空间大小设置为3G,通过pvdisplay、vgdisplay、lvdisplay查看详细信息
[[email protected] ~]# pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [[email protected] ~]# vgcreate vg_xb /dev/sdb1 /dev/sdb2 Volume group "vg_xb" successfully created [[email protected] ~]# lvcreate -L 3G -n lv_xb vg_xb Logical volume "lv_xb" created.
创建文件系统(将lv空间格式化)
[[email protected] ~]# mkfs.ext3 /dev/vg_xb/lv_xb
挂载文件系统,新建一个文件夹xb,将设备文件挂载到此目录中
[[email protected] ~]# mkdir /xb [[email protected] ~]# mount /dev/vg_xb/lv_xb /xb/
查看系统中挂载各文件系统的磁盘使用情况,可以看到新建的lv空间大小为3G
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 2.5G 15G 15% / tmpfs 364M 0 364M 0% /dev/shm /dev/sda1 477M 36M 416M 8% /boot /dev/mapper/vg_xb-lv_xb 3.0G 69M 2.8G 3% /xb
然而当逻辑卷空间不足时,还需要从所在卷组中分割额外的空间给逻辑卷,前提是该卷组中有尚未分配的空间否则先扩展卷组容量
此时,我们lv的大小为3G,vg大小为4G,当我们lv空间不足需要扩展时,可以从vg剩余部分中分割一部分给lv使用
◆想要增加lv空间的大小,先卸载文件系统,并使用e2fsck命令检查文件系统的正确性
[[email protected] ~]# umount /xb/ [[email protected] ~]# e2fsck -f /dev/vg_xb/lv_xb
增加lv空间,增加300M,用lvdisplay查看lv大小变为3.3G
[[email protected] ~]# lvextend -L +300M /dev/vg_xb/lv_xb
重新识别文件系统的大小
[[email protected] ~]# resize2fs /dev/vg_xb/lv_xb
最后使用mount重新挂载文件系统,使用df -h命令显示文件系统的大小为3.3G
◆当lv空间需要缩减时,也需要先卸载文件系统并检查文件系统的正确性
然后先缩减文件系统也就是格式化后的可用空间的大小
将文件系统的大小减小到2G
[[email protected] ~]# resize2fs /dev/vg_xb/lv_xb 2G 然后再减小lv的空间,减小1G [[email protected] ~]# lvreduce -L -1G /dev/vg_xb/lv_xb WARNING: Reducing active logical volume to 2.29 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_xb? [y/n]: y Size of logical volume vg_xb/lv_xb changed from 3.29 GiB (843 extents) to 2.29 GiB (587 extents). Logical volume lv_xb successfully resized
重新挂载文件系统
查看文件系统的大小和lv空间的大小,此时文件系统大小为2G,lv空间大小为3G
注意:
缩小后的lv空间容量必须大于等于文件系统的容量,否则会导致数据丢失。
缩小后的lv空间必须比已用空间大才能保证数据不会丢失