逻辑卷管理器 LVM
lvm概述
LVM是Logical Volume Manager(逻辑卷管理器)的简写,又译为逻辑卷宗管理器、逻辑扇区管理器、逻辑磁盘管理器。是Linux核心所提供的逻辑卷管理(Logical Volume Management)功能。它在硬盘的硬盘分区之上,又创建了一个逻辑层以方便系统管理硬盘分区系统。
物理卷(PV,Physical Volume):
物理卷就是指磁盘,磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组(VG,Volume Group):
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(LV,Logical Volume):
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
dm:device mapper 设备映射组件,讲一个或多个底层设备组织成一个逻辑设备的模块;
lvm:逻辑卷管理器
为了演示操作我们首先创建一个分区
[root@localhost ~]# fdisk /dev/sdd
调整分区属性:
pv管理工具:
pvs:简要pv显示工具
pvdisplay:显示pv详细信息
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 (pv名称) VG Name vg0 (卷组名称) PV Size 60.00 GiB / not usable 8.00 MiB (大小) Allocatable yes PE Size 8.00 MiB (PE 大小) Total PE 7679 (多少个PE) Free PE 1023 Allocated PE 6656 PV UUID lAXPRM-LRkZ-3Btn-DgMf-XCED-44RA-38xRfz
创建pv工具:
pvcreate /dev/DEVICE
[root@localhost ~]# pvcreate -v /dev/sdd1 Wiping cache of LVM-capable devices Set up physical volume for "/dev/sdd1" with 20980827 available sectors Zeroing start of device /dev/sdd1 Writing physical volume data to disk "/dev/sdd1" Physical volume "/dev/sdd1" successfully created 这里的-v显示过程信息;
创建完成查看:
[root@localhost ~]# pvdisplay /dev/sdd1 "/dev/sdd1" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdd1 VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ywKdX1-XnNt-dY7C-lBO2-ochy-Podm-xFAxwb 这里由于没有加入VG,所以PE Size为0,VG Name为空;
创建PV完成,顺便说一下PV的删除:pvremove
[root@localhost ~]# pvremove /dev/sdd1 Labels on physical volume "/dev/sdd1" successfully wiped
再次查看PV就只剩下原来的PV了,我们已经把 PV Name 为/dev/sdd1的PV删除了
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg0 PV Size 60.00 GiB / not usable 8.00 MiB Allocatable yes PE Size 8.00 MiB Total PE 7679 Free PE 1023 Allocated PE 6656 PV UUID lAXPRM-LRkZ-3Btn-DgMf-XCED-44RA-38xRfz [root@localhost ~]#
PV 创建完成后接下来我们看下VG的创建:
1)显示卷组
[root@localhost ~]# vgs (显示简要信息) VG #PV #LV #SN Attr VSize VFree vg0 1 4 0 wz--n- 59.99g 7.99g [root@localhost ~]# vgdisplay (显示详细信息) --- Volume group --- VG Name vg0 (卷组名称) System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 4 Open LV 4 Max PV 0 Cur PV 1 Act PV 1 VG Size 59.99 GiB PE Size 8.00 MiB Total PE 7679 Alloc PE / Size 6656 / 52.00 GiB Free PE / Size 1023 / 7.99 GiB VG UUID Qk4MX1-IDP7-ob8t-4S9U-WknH-3KqW-mYkXP5
创建VG:
vgcreate [-s PhysicalExtent- Size[mMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [Physi-calDevicePath…]
[root@localhost ~]# vgcreate myvg /dev/sdd1 Volume group "myvg" successfully created
查看一下:
[root@localhost ~]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 10.00 GiB PE Size 4.00 MiB Total PE 2560 Alloc PE / Size 0 / 0 Free PE / Size 2560 / 10.00 GiB VG UUID ylsKNp-2e0M-lW4S-CMdb-a3qi-bdSe-6kMfPf PE大小在创建后固定不可更改,要指明大小使用-s选项;
VG创建完成可以对VG扩展(vgextend)或者缩减(vgreduce);
扩展VG:
1)扩展要添加新的PV
[root@localhost ~]# fdisk /dev/sdd (创建一个新的分区) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): p Disk /dev/sdd: 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: 0x191fb9c7 Device Boot Start End Blocks Id System /dev/sdd1 1 1306 10490413+ 8e Linux LVM Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-2610, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +5G Command (m for help): p Disk /dev/sdd: 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: 0x191fb9c7 Device Boot Start End Blocks Id System /dev/sdd1 1 1306 10490413+ 8e Linux LVM /dev/sdd2 1307 1960 5253255 83 Linux Command (m for help): t (更改分区属性8e) Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdd: 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: 0x191fb9c7 Device Boot Start End Blocks Id System /dev/sdd1 1 1306 10490413+ 8e Linux LVM /dev/sdd2 1307 1960 5253255 8e Linux LVM
2)扩展VG:
[root@localhost ~]# vgextend myvg /dev/sdd2 (把/dev/sdd2添加到myvg) Physical volume "/dev/sdd2" successfully created Volume group "myvg" successfully extended [root@localhost ~]# vgdisplay myvg (查看myvg ,发现容量已经15G了) --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 · Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 15.01 GiB PE Size 4.00 MiB Total PE 3842 Alloc PE / Size 0 / 0 Free PE / Size 3842 / 15.01 GiB VG UUID ylsKNp-2e0M-lW4S-CMdb-a3qi-bdSe-6kMfPf
VG的缩减:
1)如果/dev/sdd2上有数据我们需要先把数据移走
[root@localhost ~]# pvmove /dev/sdd2 No data to move for myvg (这里我们的分区上没有数据)
2)使用VGreduce命令:
[root@localhost ~]# vgreduce myvg /dev/sdd2 Removed "/dev/sdd2" from volume group "myvg" [root@localhost ~]# vgdisplay (查看已经缩减到10G了) --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 10.00 GiB PE Size 4.00 MiB Total PE 2560 Alloc PE / Size 0 / 0 Free PE / Size 2560 / 10.00 GiB VG UUID ylsKNp-2e0M-lW4S-CMdb-a3qi-bdSe-6kMfPf
LV管理工具:
lvs:查看简要信息
lvdisplay:查看详细信息
逻辑卷创建:
lvcreate -L #[mMgGtT] -n NAME VolumeGroup (L指明大小 -n 名称) 注意:这里要创建的大小在卷组必须有足够空间,否则会创建失败。 [root@localhost ~]# lvcreate -L 2G -n mylv myvg Logical volume "mylv" created. [root@localhost ~]# ls /dev/mapper/ control myvg-mylv vg0-root vg0-swap vg0-usr vg0-var 创建一个2G的LV,完成后就可以格式化挂载使用了 [root@localhost ~]# mke2fs -t ext4 -b 1024 -L MYLV /dev/myvg/mylv [root@localhost ~]# mount /dev/myvg/mylv /backup/ [root@localhost ~]# mount ... /dev/mapper/myvg-mylv on /backup type ext4 (rw)
扩展逻辑卷:
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
-L +#[mMgGtT]与 -L #[mMgGtT]
比如:-L+ 5G 表示扩展5G ,-L 5G 表示扩展后达到5G
[root@localhost ~]# lvextend -L 5G /dev/myvg/mylv Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 5.00 GiB (1280 extents). Logical volume mylv successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 5.00g
这里我们查看挂载会发现分区大小还是2G,并没有达到我们扩展后的5G。
[root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 2.0G 3.1M 1.9G 1% /backup
尝试重新挂载之后问题并没有解决,因为扩展LV之后文件系统并没有随之扩大,换句话说,逻辑卷大小扩大,文件系统还是原来的大小,这里我们使用的是ext文件系统,需要我们使用resize2fs工具把/dev/myvg/mylv 扩大
[root@localhost ~]# resize2fs /dev/myvg/mylv (没有指明扩展大小就是默认使用全部大小) resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /backup; on-line resizing required old desc_blocks = 8, new_desc_blocks = 20 Performing an on-line resize of /dev/myvg/mylv to 5242880 (1k) blocks. The filesystem on /dev/myvg/mylv is now 5242880 blocks long.
在查看一下:
[root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 4.9G 3.4M 4.7G 1% /backup 结果已经扩大到5G了。
缩减逻辑卷:
lvreduce
缩减是要先把文件系统大小缩减到指定大小,在缩减LV到指定大小。
缩减逻辑卷步骤:
1)先要卸载:
umount /dev/VG_NAME/LV_NAME [root@localhost ~]# umount /backup/ (卸载挂载) [root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg0-root 20511356 343640 19119140 2% / tmpfs 1954260 80 1954180 1% /dev/shm /dev/sda1 194241 39970 144031 22% /boot /dev/mapper/vg0-usr 10190136 1964380 7701468 21% /usr /dev/mapper/vg0-var 20511356 152716 19310064 1% /var /dev/sr0 3824484 3824484 0 100% /media/CentOS_6.8_Final /dev/sr0 3824484 3824484 0 100% /mnt/cdrom
2)文件系统检测:
e2fsck -f /dev/VG_NAME/LV_NAME (-f 强制检测和修复) [root@localhost ~]# e2fsck -f /dev/myvg/mylv e2fsck 1.41.12 (17-May-2010) 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 MYLV: 11/327680 files (0.0% non-contiguous), 119693/5242880 blocks
3)缩减逻辑边界:缩减后的容量一定要能容纳原有数据。
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT] [root@localhost ~]# resize2fs /dev/myvg/mylv 3G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 3145728 (1k) blocks. The filesystem on /dev/myvg/mylv is now 3145728 blocks long.
4)使用lvreduce命令缩减逻辑卷:
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
[root@localhost ~]# lvreduce -L 3G /dev/myvg/mylv WARNING: Reducing active logical volume to 3.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylv? [y/n]: y Size of logical volume myvg/mylv changed from 5.00 GiB (1280 extents) to 3.00 GiB (768 extents). Logical volume mylv successfully resized.
5)重新挂载使用:
[root@localhost ~]# mount /dev/myvg/mylv /backup/ [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 3.0G 3.3M 2.8G 1% /backup
完成
LVM快照
快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝。对于需要备份或者复制的现有数据集临时拷贝以及其它操 作来说,快照是最合适的选择。快照只有在它们和原来的逻辑卷不同时才会消耗空间。 在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变才会使用这些空间。当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快 照中更改的数据。建立快照的卷大小只需要原始逻辑卷的15%~20%就够了。也可以使用lvextend放大快照。
快照创建:
lvcreate -L #[mMgGtT] -p r -s -n snap_lv_name original_lv_name
-p:指明读、写权限
-s:指明是快照
-n:名称
[root@localhost backup]# lvcreate -s -L 512M -n mylv_snap -p r /dev/myvg/mylv Logical volume "mylv_snap" created.
复制一个文件到/backup
[root@localhost backup]# cp /etc/issue ./ [root@localhost backup]# ls issue lost+found
创建/dev/myvg/mylv 的快照,名称为mylv_snap,大小512M,只读权限
[root@localhost backup]# lvcreate -s -L 512M -n mylv_snap -p r /dev/myvg/mylv Logical volume "mylv_snap" created.
在/mnt下建立mysnap目录,
[root@localhost backup]# mkdir /mnt/mysnap
挂载
[root@localhost backup]# mount /dev/myvg/mylv_snap /mnt/mysnap/ mount: block device /dev/mapper/myvg-mylv_snap is write-protected, mounting read-only [root@localhost backup]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg0-root ext4 20G 336M 19G 2% / tmpfs tmpfs 1.9G 80K 1.9G 1% /dev/shm /dev/sda1 ext4 190M 40M 141M 22% /boot /dev/mapper/vg0-usr ext4 9.8G 1.9G 7.4G 21% /usr /dev/mapper/vg0-var ext4 20G 150M 19G 1% /var /dev/sr0 iso9660 3.7G 3.7G 0 100% /media/CentOS_6.8_Final /dev/sr0 iso9660 3.7G 3.7G 0 100% /mnt/cdrom /dev/mapper/myvg-mylv ext4 3.0G 3.3M 2.8G 1% /backup /dev/mapper/myvg-mylv_snap ext4 3.0G 3.3M 2.8G 1% /mnt/mysnap
查看分区内文件和备份文件
[root@localhost backup]# cat issue CentOS release 6.8 (Final) Kernel \r on an \m [root@localhost backup]# cat /mnt/mysnap/ cat: /mnt/mysnap/: Is a directory [root@localhost backup]# cat /mnt/mysnap/issue CentOS release 6.8 (Final) Kernel \r on an \m
重新编辑下原文件
[root@localhost ~]# cat /backup/issue CentOS release 6.8 (Final) Kernel \r on an \m test test test test test
查看备份文件
[root@localhost ~]# cat /mnt/mysnap/issue CentOS release 6.8 (Final) Kernel \r on an \m
要想恢复,把快照卷中的文件复制出去就可以了
备份卷的删除:
1)卸载快照卷
[root@localhost ~]# umount /mnt/mysnap/
2)lvremove :逻辑卷删除命令
[root@localhost ~]# lvremove /dev/myvg/mylv_snap Do you really want to remove active logical volume mylv_snap? [y/n]: y Logical volume "mylv_snap" successfully removed
删除逻辑卷:
1)卸载逻辑卷挂载
[root@localhost ~]# umount /backup/
2)删除逻辑卷:
[root@localhost ~]# lvremove /dev/myvg/mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed
3)删除VG:
[root@localhost ~]# vgremove /dev/myvg Volume group "myvg" successfully removed [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg0 1 4 0 wz--n- 59.99g 7.99g
4)删除PV:
[root@localhost ~]# pvremove /dev/sdd1 Labels on physical volume "/dev/sdd1" successfully wiped [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a--u 59.99g 7.99g /dev/sdd2 lvm2 ---- 5.01g 5.01g [root@localhost ~]# pvremove /dev/sdd2 Labels on physical volume "/dev/sdd2" successfully wiped [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg0 lvm2 a--u 59.99g 7.99g
原创文章,作者:zanghonglei,如若转载,请注明出处:http://www.178linux.com/42715