一、基本概念
1、LVM是逻辑盘卷管理(LogicalVolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。
2、why:通常在安装Linux系统的时候对磁盘进行一盘的分区,一旦日后分区或整个磁盘的空间不够用,就难以再扩容。但是使用lvm来管理磁盘的分区,由于是建立在磁盘和分区上的一个逻辑层,可以灵活管理存储空间,而且可以跨硬盘进行管理存储空间,而且不会对原有的数据文件造成损害。
3、lvm的基本术语
PV:Physical Volume,指磁盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
PE:Physical Extent,每一个物理卷PV被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷(PV)由大小等同的基本单元PE组成。
VG:Volume Group,类似于非LVM系统中的物理磁盘,其由一个或多个物理卷PV组成。可以在卷组上创建一个或多个LV(逻辑卷)。
LV:Logical Volume,类似于非LVM系统中的磁盘分区,逻辑卷建立在卷组VG之上。在逻辑卷LV之上可以建立文件系统(比如/home或者/usr等)。
LE:Logical Extent,逻辑卷LV也被划分为可被寻址的基本单位,称为LE。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
上述的基本关系如下图
二、LVM的建立(配置LVM的流程:fdisk创建分区及修改ID为8e–>把分区转换为PV—>创建VG—>创建LV—>格式华LV—>mount文件系统
1、常用工具
pv管理工具: pvs:简要pv信息显示 pvdisplay:显示pv的详细信息 pvcreate /dev/DEVICE: 创建pv -v:显示过程 -f:强制, -uuid:直接指定UUID vg管理工具: vgs vgdisplay vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...][-s #[kKmMgGtTpPeE]]指定PE大小 vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 先做pvmove vgremove lv管理工具: lvs lvdisplay lvcreate -L #[mMgGtT] -n NAME VolumeGroup lvremove /dev/VG_NAME/LV_NAME 扩展逻辑卷: lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME resize2fs /dev/VG_NAME/LV_NAME df -lh 缩减逻辑卷: 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 快照:snapshot lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name -p, --permission {r|rw} -n, --name LogicalVolume{Name|Path} -s, --snapshot OriginalLogicalVolume{Name|Path}
2、lvm的构建过程
先给系统添加硬盘并分区
[root@localhost ~]# fdisk /dev/sdc 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 (mforhelp): tSelected partition1Hex code (typeLtolist codes): 8e //需要把分区的id修改为Linux LVMChangedsystemtypeofpartition1to8e (Linux LVM) Command (mforhelp): pDisk /dev/sdc: 10.7 GB, 10737418240bytes255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of16065 * 512 = 8225280bytesSector size (logical/physical): 512bytes / 512bytesI/O size (minimum/optimal): 512bytes / 512bytesDisk identifier: 0x16f72b9f Device Boot StartEnd Blocks IdSystem/dev/sdc1 11305104823818e Linux LVM
1)、将磁盘设定为PV
[root@localhost ~]# pvcreate /dev/sda5 Physical volume "/dev/sda5" successfully created [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@localhost ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a-- 58.59g 0 /dev/sda5 lvm2 --- 10.00g 10.00g /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdc1 lvm2 --- 10.00g 10.00g [root@localhost ~]# pvdisplay /dev/sdb1"/dev/sdb1" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID GGKTNl-Yi0I-5RJX-WiHJ-iQSe-tvzO-vqRo8N
2)、建立VG
[root@localhost ~]# vgcreate -s 6M myvg /dev/sda5 /dev/sd[b-c]1 //创建一个名为myvg的VG,并且设定PE大小为6M Volume group "myvg" successfully created [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 130 wz--n- 58.59g 0 myvg 300 wz--n- 29.98g 29.98g [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 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 3 Act PV 3 VG Size 29.98 GiB PE Size 6.00 MiB Total PE 5117 Alloc PE / Size 0 / 0 Free PE / Size 5117 / 29.98 GiB VG UUID WtwVcH-Ny7F-kAcN-MI5H-0AgA-b6rF-fUiuJB [root@localhost ~]# pvdisplay /dev/sda5 --- Physical volume --- PV Name /dev/sda5 VG Name myvg //显示此PE已经加入到myvg卷组中 PV Size 10.00 GiB / not usable 2.54 MiB Allocatable yes PE Size 6.00 MiB //PE大小由VG创建时确定,所以现在才有显示大小 Total PE 1707 Free PE 1707 Allocated PE 0 PV UUID flJPpR-s9VI-OO5N-iSOm-K1nN-thOl-XknRjP
3)、建立LV
[root@localhost ~]# lvcreate -L 10G -n mylv myvg //创建一个名为mylv 10G大小的LV Rounding up size to full physical extent 10.00 GiB Logical volume "mylv" created. [root@localhost ~]# lvs //显示LV的简要信息 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert LogVol00 VolGroup -wi-ao---- 39.06g LogVol01 VolGroup -wi-ao---- 9.76g LogVol02 VolGroup -wi-ao---- 9.77g mylv myvg -wi-a----- 10.00g [root@localhost ~]# lvdisplay /dev/myvg/mylv --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID RFCRTA-hd1C-6DPh-tVdK-EVy7-mF47-0qfTJh LV Write Access read/write //默认为读写模式 LV Creation host, time localhost.localdomain, 2016-06-1117:42:47 +0800 LV Status available # open 0 LV Size 10.00 GiB //10G大小 Current LE 1707 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:3
4)、格式化LV并挂载
[root@localhost ~]# mke2fs -t ext4 -L "myLVM" /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) 警告: 512 块未使用. 文件系统标签=myLVM 操作系统:Linux块大小=4096 (log=2) 分块大小=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks656640 inodes, 2621440 blocks131097 blocks (5.00%) reserved for the super user 第一个数据块=0Maximum filesystem blocks=268435456080block groups32768 blocks per group, 32768 fragments per group8208 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 26 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# blkid /dev/myvg/mylv /dev/myvg/mylv: LABEL="myLVM"UUID="384e1993-9517-423a-b69b-26bbad53c3d3"TYPE="ext4" [root@localhost ~]# mount /dev/myvg/mylv /backup/ [root@localhost ~]# mount | grep "mylv"/dev/mapper/myvg-mylv on /backup type ext4 (rw)
3、lvm的扩展
[root@localhost ~]# lvextend -L +5G /dev/myvg/mylv //给mylv增加5G的空间大小 Rounding size to boundary between physical extents: 5.00 GiB Size of logical volume myvg/mylv changed from 10.00 GiB (1707 extents) to 15.01 GiB (2561 extents). Logical volume mylv successfully resized [root@localhost ~]# lvdisplay /dev/myvg/mylv --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID RFCRTA-hd1C-6DPh-tVdK-EVy7-mF47-0qfTJh LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-06-1117:42:47 +0800 LV Status available # open 1 LV Size 15.01 GiB //已增加了5G的大小 Current LE 2561 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2[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 = 1, new_desc_blocks = 1Performing an on-line resize of /dev/myvg/mylv to 3933696 (4k) blocks. The filesystem on /dev/myvg/mylv is now 3933696 blocks long. [root@localhost ~]# df -lh /dev/myvg/mylv Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv 15G 25M 14G 1% /backup //此时显示文件系统大小也也扩容后大小
4、LVM的缩减
[root@localhost ~]# umount /backup/ //①先卸载 [root@localhost ~]# e2fsck -f /dev/myvg/mylv //②强制检测 e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 myLVM: 12/993168 files (0.0% non-contiguous), 101500/3933696 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv 5G //③先缩减文件系统的大小到5Gresize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to1310720 (4k) blocks. The filesystem on /dev/myvg/mylv isnow1310720 blocks long. [root@localhost ~]# lvreduce -L5G /dev/myvg/mylv //④再缩减LV到5G大小 Rounding sizeto boundary betweenphysical extents: 5.00 GiB WARNING: Reducing active logical volume to5.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce mylv? [y/n]: y Sizeoflogical volume myvg/mylv changedfrom15.01 GiB (2561 extents) to5.00 GiB (854 extents). Logical volume mylv successfully resized [root@localhost ~]# lvdisplay /dev/myvg/mylv --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID RFCRTA-hd1C-6DPh-tVdK-EVy7-mF47-0qfTJh LV Write Accessread/write LV Creation host, time localhost.localdomain, 2016-06-1117:42:47 +0800 LV Status available # open0 LV Size5.00 GiB //此时以显示为缩减后的大小 Current LE 854 Segments 1 Allocation inherit Read ahead sectors auto - currently setto256Block device 253:2 [root@localhost ~]# mount /dev/myvg/mylv /backup/ //最后重新挂载 [root@localhost ~]# df -lh /dev/myvg/mylv Filesystem Size Used Avail Use% Mounted on/dev/mapper/myvg-mylv 4.8G20M4.6G1% /backup //文件系统此时也为5G大小
4、快照功能的实现
[root@localhost ~]# lvcreate -p r -s -L 512M -n snmylv /dev/myvg/mylv //创建一个大小为512M 只有读权限的快照卷 Rounding up size to full physical extent 516.00 MiB Logical volume "snmylv" created. [root@localhost ~]# ll /dev/myvg总用量 0lrwxrwxrwx. 1 root root 7 6月 11 18:51 mylv -> ../dm-2lrwxrwxrwx. 1 root root 7 6月 11 18:51 snaplv -> ../dm-6lrwxrwxrwx. 1 root root 7 6月 11 18:51 snmylv -> ../dm-8[root@localhost ~]# lvdisplay /dev/myvg/snmylv --- Logical volume --- LV Path /dev/myvg/snmylv LV Name snmylv VG Name myvg LV UUID QG7ORH-YRyE-RTFA-JRCe-WhGk-mXAL-rjo0sl LV Write Access read only //只读权限 LV Creation host, time localhost.localdomain, 2016-06-11 18:51:15 +0800 LV snapshot status active destination for mylv LV Status available # open 0 LV Size 5.00 GiB //原卷大小 Current LE 854 COW-table size 516.00 MiB //快照卷大小 COW-table LE 86 Allocated to snapshot 0.00% Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:8
原创文章,作者:mouse015110,如若转载,请注明出处:http://www.178linux.com/18082