LVM简介
LVM全称Logical Volume Manager(逻辑卷管理),是将几个物理分区(或硬盘)通过软件组合成一块看起来是独立大硬盘(VG),然后对这块大硬盘分割成可使用的逻辑卷(LV),最终能够挂载使用,以达到对磁盘空间进行弹性管理的目的。
LVM的基本原理
基本术语
dm(device mapper):将一个或多个底层块设备组织成一个逻辑设备的模块
PV(Physical Volume,物理卷):物理分区(或硬盘)通过 fdisk 调整系统识别码 (system ID) 成为 8e (LVM 的识别码),然后再经过 pvcreate 的命令将他转成 LVM 最底层的物理卷(PV)
VG(Volume Group,卷组):由多个 PV 整合而成,每个 VG 最多能包含 65534 个 PE
PE(Physical Extend,物理块):LVM 最小的储存区块,默认大小 4MB。文件数据都是藉由写入 PE 来处理的。
LV(Logical Volume,逻辑卷):类似于非 LVM 系统中的磁盘分区,逻辑卷建立在卷组 VG 之上,有多个 PE组成
LE(Logical Extent,逻辑卷):LV 中的 PE 称为 LE
基本流程
物理分区(或硬盘)转化为PV,基于PV创建VG,VG包含若干个PE,通过划分VG中的PE组成LV,LV格式化成文件系统,最终进行挂载。
原理图
注:图片来自维基百科
LVM的使用
相关工具及命令
-
PV管理工具
pvs:简要PV信息显示
pvdisplay:显示PV的详细信息
pvcreate /dev/DEVICE:创建PV
-
VG管理工具
vgs:简要VG显示信息
vgdisplay:显示VG的详细信息
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]:创建VG
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]:扩展VG
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]:缩减VG
vgremove:移除VG
注意:对VG做减法时,先做pvmove,把需要移除的pv上的pe先移到同一卷组中其他pv上
-
LV管理工具
lvs:简要LV信息显示
lvdisplay:显示LV的详细信息
lvcreate -L #[mMgGtT] -n NAME VolumeGroup:创建LV
lvremove /dev/VG_NAME/LV_NAME:移除LV
-
扩展逻辑卷
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
-L #:扩展至#大小
-L +#:扩展#大小
resize2fs /dev/VG_NAME/LV_NAME:重新定义文件系统的大小
-
缩减逻辑卷
umount /dev/VG_NAME/LV_NAME:卸载指定的LV挂载
e2fsck -f /dev/VG_NAME/LV_NAME:检查文件系统
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]:重新定义LV的大小
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME:缩减LV大小
mount
-
快照(snapshot)
lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name:创建快照
LVM的创建
目标:创建一个有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录
-
系统环境
操作系统:CentOS 6.7 磁盘信息: Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 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: 0x00013624 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 15666 125316096 8e Linux LVM Disk /dev/sdb: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 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
-
初始化磁盘分区
注:为简便操作,实验将基于sdb盘的物理分区
[root@centos6 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x6cd53b57. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-3916, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1307-3916, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-3916, default 3916): +10G Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t 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): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos6 ~]# partx -a /dev/sdb #通知内核重读分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2
-
创建PV
[root@centos6 ~]# pvcreate /dev/sdb[1-2] Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created [root@centos6 ~]# pvs /dev/sdb* #查看简要信息,单独pvs显示所有PV PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g [root@centos6 ~]# pvdisplay /dev/sdb* #查看详细信息,单独pvdisplay显示所有PV Failed to find device for physical volume "/dev/sdb". "/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 fRfYN1-N1xX-VBtQ-u4hH-FKjd-OSGL-N1n6b1 "/dev/sdb2" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID fjJ8ED-VTVk-8IJb-DCdx-82Bl-Go7g-wApVuK
-
创建VG
[root@centos6 ~]# vgcreate -s 16 testvg /dev/sdb[1-2] #创建名为testvg PE大小为16MB的卷组 Volume group "testvg" successfully created [root@centos6 ~]# vgs testvg #可根据VG名称查找 VG #PV #LV #SN Attr VSize VFree testvg 2 0 0 wz--n- 20.00g 20.00g [root@centos6 ~]# vgdisplay testvg #可根据VG名称查找 --- Volume group --- VG Name testvg System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 20.00 GiB PE Size 16.00 MiB Total PE 1280 Alloc PE / Size 0 / 0 Free PE / Size 1280 / 20.00 GiB VG UUID QPvR7X-2uRS-U8Ba-Kzz5-Z1Z0-WZ2i-WkkBpB
-
创建LV
[root@centos6 ~]# lvcreate -L 5g -n testlv testvg #在testvg下创建容量为5G名称为testlv的LV Logical volume "testlv" created. [root@centos6 ~]# lvs /dev/testvg LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert testlv testvg -wi-a----- 5.00g [root@centos6 ~]# lvs /dev/testvg/testlv LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert testlv testvg -wi-a----- 5.00g [root@centos6 ~]# lvdisplay /dev/testvg/testlv --- Logical volume --- LV Path /dev/testvg/testlv LV Name testlv VG Name testvg LV UUID JNTrHH-bx2p-Z5k2-U6Cx-Fboi-BnGn-nNlqmZ LV Write Access read/write LV Creation host, time centos6.7, 2016-04-17 16:39:15 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 320 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:3
-
LV格式化及挂载
[root@centos6 ~]# mke2fs -t ext4 /dev/testvg/testlv mke2fs 1.41.12 (17-May-2010) 文件系统标签= 操作系统:Linux 块大小=4096 (log=2) 分块大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user 第一个数据块=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 正在写入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@centos6 ~]# mkdir /users #创建users目录 [root@centos6 ~]# mount /dev/testvg/testlv /users/ 将LV挂载到users目录 [root@centos6 ~]# mount #查看是否挂载成功 /dev/mapper/vg_centos6-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) /dev/mapper/vg_centos6-lv_home on /home type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/testvg-testlv on /users type ext4 (rw) #表示已经挂载成功 [root@centos6 ~]# df -lh #查看分区情况 Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos6-lv_root 50G 3.9G 43G 9% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 37M 416M 9% /boot /dev/mapper/vg_centos6-lv_home 67G 54M 63G 1% /home /dev/mapper/testvg-testlv 4.8G 10M 4.6G 1% /users
LVM的扩展
-
VG扩展
注:sdb3空间大小为5G,创建过程省略
[root@centos6 ~]# vgextend testvg /dev/sdb3 #扩展testvg,将sdb3的空间提供给testvg Volume group "testvg" successfully extended [root@centos6 ~]# vgs testvg #查看扩展后的容量 VG #PV #LV #SN Attr VSize VFree testvg 3 1 0 wz--n- 25.00g 20.00g #成功扩展5G容量
-
LV扩展
[root@centos6 ~]# lvextend -L +10g /dev/testvg/testlv #扩展10G空间给testlv Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 15.00 GiB (960 extents). Logical volume testlv successfully resized [root@centos6 ~]# lvs /dev/testvg/testlv #查看testlv信息,空间大小为15G LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert testlv testvg -wi-ao---- 15.00g [root@centos6 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos6-lv_root 50G 3.9G 43G 9% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 37M 416M 9% /boot /dev/mapper/vg_centos6-lv_home 67G 54M 63G 1% /home /dev/mapper/testvg-testlv 4.8G 10M 4.6G 1% /users #分区空间不变,扩展的空间生效需要重新定义分区大小 [root@centos6 ~]# resize2fs /dev/testvg/testlv #分区大小重新定义 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/testvg/testlv to 3932160 (4k) blocks. The filesystem on /dev/testvg/testlv is now 3932160 blocks long. [root@centos6 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos6-lv_root 50G 3.9G 43G 9% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 37M 416M 9% /boot /dev/mapper/vg_centos6-lv_home 67G 54M 63G 1% /home /dev/mapper/testvg-testlv 15G 13M 14G 1% /users #扩展空间已经生效
注:如果文件系统是处于mount状态下,resize2fs命令只能做到扩容
LVM缩减
注:LVM缩减有风险,操作需谨慎
-
VG缩减
[root@centos6 ~]# pvs -o+pv_used #查看pv的已使用情况 PV VG Fmt Attr PSize PFree Used /dev/sda2 vg_centos6 lvm2 a-- 119.51g 0 119.51g /dev/sdb1 testvg lvm2 a-- 10.00g 0 10.00g /dev/sdb2 testvg lvm2 a-- 10.00g 5.00g 5.00g /dev/sdb3 testvg lvm2 a-- 5.00g 5.00g 0 [root@centos6 ~]# pvmove /dev/sdb2 /dev/sdb3 #防止数据丢失将sdb2非空闲的PE与sdb3的空间PE交换 /dev/sdb2: Moved: 0.3% /dev/sdb2: Moved: 6.2% /dev/sdb2: Moved: 14.1% /dev/sdb2: Moved: 21.6% /dev/sdb2: Moved: 27.2% /dev/sdb2: Moved: 34.4% /dev/sdb2: Moved: 40.6% /dev/sdb2: Moved: 48.1% /dev/sdb2: Moved: 54.7% /dev/sdb2: Moved: 60.9% /dev/sdb2: Moved: 67.8% /dev/sdb2: Moved: 74.1% /dev/sdb2: Moved: 80.6% /dev/sdb2: Moved: 87.8% /dev/sdb2: Moved: 93.4% /dev/sdb2: Moved: 98.8% /dev/sdb2: Moved: 100.0% [root@centos6 ~]# pvs -o+pv_used PV VG Fmt Attr PSize PFree Used /dev/sda2 vg_centos6 lvm2 a-- 119.51g 0 119.51g /dev/sdb1 testvg lvm2 a-- 10.00g 0 10.00g /dev/sdb2 testvg lvm2 a-- 10.00g 10.00g 0 #此pv已经空闲,可进行缩减VG /dev/sdb3 testvg lvm2 a-- 5.00g 0 5.00g [root@centos6 ~]# vgreduce testvg /dev/sdb2 #缩减VG Removed "/dev/sdb2" from volume group "testvg" [root@centos6 ~]# vgs testvg VG #PV #LV #SN Attr VSize VFree testvg 2 1 0 wz--n- 15.00g 0 #成功缩减VG
-
LV缩减
[root@centos6 ~]# umount /users/ #卸载挂载目录 [root@centos6 ~]# e2fsck -f /dev/testvg/testlv #检查文件系统,该步骤不可少 e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/testvg/testlv: 11/983040 files (0.0% non-contiguous), 97664/3932160 blocks [root@centos6 ~]# resize2fs /dev/testvg/testlv 10g resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/testvg/testlv to 2621440 (4k) blocks. The filesystem on /dev/testvg/testlv is now 2621440 blocks long. [root@centos6 ~]# lvreduce -L -5g /dev/testvg/testlv #缩减5G LV的空间 WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce testlv? [y/n]: y Size of logical volume testvg/testlv changed from 15.00 GiB (960 extents) to 10.00 GiB (640 extents). Logical volume testlv successfully resized [root@centos6 ~]# lvs /dev/testvg/testlv #查看testlv信息 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert testlv testvg -wi-ao---- 10.00g [root@centos6 ~]# mount /dev/testvg/testlv /users/ #重新挂载 [root@centos6 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos6-lv_root 50G 3.9G 43G 9% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 477M 37M 416M 9% /boot /dev/mapper/vg_centos6-lv_home 67G 54M 63G 1% /home /dev/mapper/testvg-testlv 9.8G 12M 9.3G 1% /users
快照
LVM快照功能是在在时间点上对快照源分区的所有文件的元数据进行保存,如果源数据未发生改变,则访问快照卷的相应文件将直接指向源分区的源文件,如源文件发生改变,旧数据将拷贝到快照卷的预留空间中起到备份数据的作用
-
快照创建
[root@centos6 users]# cp /etc/fstab /users/ [root@centos6 users]# ls fstab lost+found [root@centos6 users]# cat fstab # # /etc/fstab # Created by anaconda on Fri Mar 4 11:09:09 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vg_centos6-lv_root / ext4 defaults 1 1 UUID=3efc9d2d-ff60-4491-84c4-e1beb6701b83 /boot ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [root@centos6 users]# lvcreate -L 5g -p r -s -n testlv-snap /dev/testvg/testlv #创建testlv的快照卷 Logical volume "testlv-snap" created. [root@centos6 users]# mkdir /backup [root@centos6 users]# mount /dev/testvg/testlv-snap /backup/ #将快照卷挂载到/backup目录 mount: block device /dev/mapper/testvg-testlv--snap is write-protected, mounting read-only
-
测试
[root@centos6 users]# vim fstab #编辑源文件 [root@centos6 users]# cat fstab # [root@centos6 users]# cat /backup/fstab #查看快照卷中的文件,发现文件内容不变实现了备份 # # /etc/fstab # Created by anaconda on Fri Mar 4 11:09:09 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vg_centos6-lv_root / ext4 defaults 1 1 UUID=3efc9d2d-ff60-4491-84c4-e1beb6701b83 /boot ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_home /home ext4 defaults 1 2 /dev/mapper/vg_centos6-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
原创文章,作者:Net18-海滨,如若转载,请注明出处:http://www.178linux.com/15011