本次内容
1、什么是LVM
2、为什么使用LVM
3、LVM的操作命令
4、LVM的具体操作实现
一、什么是LVM
LVM:Logical Volume Manager,即逻辑卷管理,它是Linux环境下的一种磁盘管理方式,其功能在于能弹性调整文件系统的容量。LVM中有几个专业名词,重点来说以下一个
PV:物理卷
LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
VG:卷组
由一个或多个PV构成,为上层的逻辑卷提供空间
LV:逻辑卷
类似于磁盘的分区,建立在VG之上,可以再其上面创建文件系统
PE:PV的基本存储单元,通常大小为4MB
关系如下图所示:
可以理解为将PV打碎成一个个的PE,然后将这所有的PE组合成一个VG,此时VG就由所有的PE构成,创建LV时,就是将PE分配给LV,在LV上面我们可以创建文件系统并挂载使用了。
二、为什么使用LVM
想象一下,如果当初装机时候你给其中一个分区20G,并且将它挂载在/home目录上,如果有一天,其中的数据太多,20G已经不够用了,这么搞呢,可能也就是新增加一块硬盘,重新分区并且格式化,然后将/home下的数据完整的复制过来,然后卸载原来的分区,挂载新的分区。好一个繁琐的过程,而且如果下次空间又满了,还得重新重复以上的步骤。
此时,LVM的光辉就显现出来了,最大的作用就是在于可以弹性的更改LV的的容量。那么问题来了,它是怎样弹性更改LV的容量,如下图所示
如果LV的空间不足时,分三种情况:
1、当VG还有空间时,直接将VG中的PE分配给LV,扩大其容量
2、如果VG空间分配完毕,应该向VG中增加PV,从而扩展VG的容量,然后步骤同1
3、如果现有PV使用完,可以将新的分区或者硬盘创建为PV,然后步骤同2
三、LVM的操作命令
1、PV管理工具
创建pv
pvcreate /dev/DEVICE
显示pv信息
pvs:简要pv信息显示
pvdisplay
2、vg管理工具
显示卷组
vgs
vgdisplay
创建卷组
vgcreate [-s #[kKmMgGtTpPeE]] VGNAME PVPATH
管理卷组
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
删除卷组
先做pvmove,再做vgremove
3、lv管理工具
显示逻辑卷
lvs
Lvdisplay
创建逻辑卷
lvcreate-L #[mMgGtT] -n NAME VolumeGroup
删除逻辑卷
lvremove/dev/VG_NAME/LV_NAME
重设文件系统大小
fsadm[options] resize device [new_size[BKMGTEP]]
resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
4、扩展和缩减逻辑卷
扩展逻辑卷:
# lvextend-L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# resize2fs /dev/VG_NAME/LV_NAME
缩减逻辑卷:
# umount/dev/VG_NAME/LV_NAME
# e2fsck -f /dev/VG_NAME/LV_NAME
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
# lvreduce-L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
# mount
四、LVM具体是如何实现的
如何在自己的虚拟机上实现LVM逻辑卷管理?步骤如下;
1)创建分区并且将分区类型更改为8e
[[email protected] ~]# fdisk /dev/sdb //对/dev/sdb硬盘进行分区 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0009c3cd Device Boot Start End Blocks Id System Command (m for help): n //创建一个新的分区 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-167772159, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): +100M Partition 1 of type Linux and of size 100 MiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e //更改分区类型为8e Changed type of partition ‘Linux‘ to ‘Linux LVM‘ //显示更改为Linux LVM Command (m for help): p //查看一下刚才创建的分区,system类型显示为Linux LVM Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0009c3cd Device Boot Start End Blocks Id System /dev/sdb1 2048 206847 102400 8e Linux LVM Command (m for help): w //存盘退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [[email protected] ~]# partprobe //更新内存信息
重复上面步骤创建/dev/sdb2,/dev/sdb3
[[email protected] ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p //显示分区信息 Disk /dev/sdb: 85.9 GB, 85899345920 bytes, 167772160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0009c3cd Device Boot Start End Blocks Id System /dev/sdb1 2048 206847 102400 8e Linux LVM //现在共有三个LVM类型的分区 /dev/sdb2 206848 411647 102400 8e Linux LVM /dev/sdb3 411648 616447 102400 8e Linux LVM
2)创建PV
[[email protected] ~]# pvcreate /dev/sdb{1,2,3} //将/dev/sdb{1,2,3}创建为PV Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created
3)创建VG
[[email protected] ~]# vgcreate myvg /dev/sdb{1,2} //将/dev/sdb{1,2}创建为卷组,卷组名为myvg Volume group "myvg" successfully created
4)创建LV
[[email protected] ~]# lvcreate -n mylv -L 100M myvg //创建大小为100M,名字为mylv的LV Logical volume "mylv" created. [[email protected] ~]# lvdisplay //查看系统上LV的信息,这里仅仅有且只有一个,即刚刚创建的 --- Logical volume --- LV Path /dev/myvg/mylv //路径为/dev/myvg/mylv LV Name mylv //名称为mylv VG Name myvg //卷组名为myvg LV UUID OkCyEj-sTkC-6Kch-svPr-UAgv-mQuf-uRofNj LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-08-30 14:46:48 +0800 LV Status available # open 0 LV Size 100.00 MiB //大小为100MB Current LE 25 //LE数量,其实就是PE,因为被分配到了LV里面,所以改叫LE Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
5)格式化并挂载使用
[[email protected] ~]# mkfs.ext4 /dev/myvg/mylv //将mylv格式化ext4的文件系统 [[email protected] ~]# mount /dev/myvg/mylv /testdir/ //挂载到/testdir目录下 [[email protected] ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 41922560 931284 40991276 3% / devtmpfs 486140 0 486140 0% /dev tmpfs 500664 0 500664 0% /dev/shm tmpfs 500664 6936 493728 2% /run tmpfs 500664 0 500664 0% /sys/fs/cgroup /dev/sda3 20961280 3149160 17812120 16% /usr /dev/sda1 496300 140512 355788 29% /boot tmpfs 100136 0 100136 0% /run/user/0 /dev/mapper/myvg-mylv 95054 1550 86336 2% /testdir //挂载成功!
6)进入挂载目录并且正常使用
[[email protected] ~]# cd /testdir/ //进入挂载的目录 [[email protected] testdir]# ls lost+found [[email protected] testdir]# touch f1 //创建文件 [[email protected] testdir]# ls //创建成功! f1 lost+found [[email protected] testdir]# pwd /testdir
如果此分区的空间即将被填满,如何扩展LV的容量?步骤如下
1)如果VG还有空间
[[email protected] testdir]# lvextend -L +50M /dev/myvg/mylv //增加50M Rounding size to boundary between physical extents: 52.00 MiB Size of logical volume myvg/mylv changed from 100.00 MiB (25 extents) to 152.00 MiB (38 extents). Logical volume mylv successfully resized. [[email protected] testdir]# lvs //简略显示LV的信息 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 152.00m //增加了52M,以为PE是最小存储单元,所以必须是PE的整数倍
虽然LV显示已经增加,但是已经挂载的目录好像有些问题
[[email protected] testdir]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 40G 910M 40G 3% / devtmpfs 475M 0 475M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 483M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 20G 3.1G 17G 16% /usr /dev/sda1 485M 138M 348M 29% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv 93M 1.6M 85M 2% /testdir //容量并没有增加
此时需要从新设置文件系统的容量
[[email protected] testdir]# resize2fs /dev/myvg/mylv //重新设置LV的容量 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv is mounted on /testdir; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/myvg/mylv is now 155648 blocks long. [[email protected] testdir]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 40G 910M 40G 3% / devtmpfs 475M 0 475M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 483M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 20G 3.1G 17G 16% /usr /dev/sda1 485M 138M 348M 29% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv 144M 1.6M 133M 2% /testdir //容量成功增加
2)如果VG空间分配完毕,此时需要向VG中添加PV
[[email protected] testdir]# vgextend myvg /dev/sdb3 //将之前创建的PV/dev/sdb3添加至卷组myvg Volume group "myvg" successfully extended [[email protected] testdir]# vgdisplay //查看VG的详细信息 --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 3 Act PV 3 VG Size 288.00 MiB //增加了/dev/sdb3的容量,现在为288MB的空间。增加成功! PE Size 4.00 MiB Total PE 72 Alloc PE / Size 38 / 152.00 MiB //已经分配的PE/空间大小 Free PE / Size 34 / 136.00 MiB //空闲的的PE/空间大小 VG UUID ObuSOV-RmEg-m2Ec-GIe1-JzhC-E3kd-1XgOP
VG空间成功增加,以下步骤同上1
3)如果PV使用完毕,那就创建分区,并且更改分区类型为8e,然后pvcreate。(创建PV时候也可以直接使用磁盘)
[[email protected] testdir]# fdisk /dev/sdc Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xd7e4d605 Device Boot Start End Blocks Id System /dev/sdc1 2048 206847 102400 8e Linux LVM //LVM类型的分区 [[email protected] testdir]# pvcreate /dev/sdc1 //创建/dev/sdc1为PV Physical volume "/dev/sdc1" successfully created [[email protected] ~]# pvcreate /dev/sd{d,e} //将/dev/sd{d,e}创建为PV Physical volume "/dev/sdd" successfully created Physical volume "/dev/sde" successfully created
如果你觉得逻辑卷分配的空间太多了,也可以缩减逻辑卷
1、卸载LV
[[email protected] ~]# df -h //查看磁盘挂载信息 Filesystem Size Used Avail Use% Mounted on /dev/sda2 40G 910M 40G 3% / devtmpfs 475M 0 475M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.9M 483M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 20G 3.1G 17G 16% /usr /dev/sda1 485M 138M 348M 29% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv 144M 1.6M 132M 2% /testdir //已使用1.6M,剩余132M可用 [[email protected] ~]# umount /dev/myvg/mylv //注意:缩减逻辑卷时候必须先卸载
2、检测文件系统 (如果不执行这一步,直接执行第三步时候会报错)
[[email protected] ~]# e2fsck -f /dev/myvg/mylv e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylv: 12/37544 files (8.3% non-contiguous), 10390/155648 blocks
3、重设文件系统的大小
[[email protected] ~]# resize2fs /dev/myvg/mylv 100M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv to 102400 (1k) blocks. The filesystem on /dev/myvg/mylv is now 102400 blocks long.
4、缩减LV的容量
[[email protected] ~]# lvreduce -L 100M /dev/myvg/mylv //将LV缩减到100M WARNING: Reducing active logical volume to 100.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: yes Size of logical volume myvg/mylv changed from 152.00 MiB (38 extents) to 100.00 MiB (25 extents). Logical volume mylv successfully resized. [[email protected] ~]# lvs //查看LV的大小 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 100.00m //显示100M,缩减成功
5、重新挂载并查看
[[email protected] ~]# mount /dev/myvg/mylv /testdir/ [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 40G 910M 40G 3% / devtmpfs 475M 0 475M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 483M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 20G 3.1G 17G 16% /usr /dev/sda1 485M 138M 348M 29% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv 93M 1.6M 85M 2% /testdir //缩减成功!
注意缩减时候,计算好要缩减的空间大小,缩减的空间大小如果比已经使用的空间还要小,那就会出问题了。而且,为避免文件损坏,缩减时候一定要首先卸载逻辑卷,然后再检查一次文件系统。所以,缩减前最好对原始数据做好备份。
谢谢浏览...