VM是 Logical Volume Manager(逻辑卷管理)的简写,LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。
通俗的话来说下功能 就是动态无影响扩展分区大小!!
前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。首先我们讨论以下几个LVM术语:
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
简单来说就是:
PV:是物理的磁盘分区
VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。
LV:也就是从VG中划分的逻辑分区
三.安装以及操作演示
该软件一般默认安装
[root@mysql2 ~]# rpm -qa |grep lvm
l
vm2-2.02.98-9.el6.x86_64 lvm2-libs-2.02.98-9.el6.x86_64
如果命令结果输入类似于上例,那么说明系统已经安装了LVM管理工具;如果命令没有输出则说明没有安装LVM管理工具,则需要安装
[root@mysql2 ~]# 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: 0x0006afb5 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2358 18631680 83 Linux /dev/sda3 2358 2611 2031616 82 Linux swap / Solaris
通过fdisk -l 可以看出目前分区空间以及完全使用完了
所以需要加一块磁盘
然后一直点击next知道最后选择你想加的磁盘大小 我这里是20G
[root@mysql2 ~]# reboot
Broadcast message from [email protected] (/dev/pts/1) at 18:47 ... The system is going down for reboot NOW!
重启加载
[root@mysql2 ~]# 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: 0x0006afb5 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2358 18631680 83 Linux /dev/sda3 2358 2611 2031616 82 Linux swap / Solaris Disk /dev/sdb: 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: 0x00000000
看到多出一块磁盘叫/dev/sdb
此时可以将该磁盘转为物理卷
[root@mysql2 ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree /dev/sdb lvm2 a-- 20.00g 20.00g
当然了我这里是直接将/dev/sdb全部的分掉 也可以先用fdisk 分区成/dev/sdb1 /dev/sdb2之后再做物理卷
[root@mysql2 ~]# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped
这个是删除刚刚的物理卷
今天主要学的是逻辑卷对于分区不做多讲
步骤详细开始:
[root@mysql2 ~]# pvcreate /dev/sdb
[root@mysql2 ~]# vgcreate vg1 /dev/sdb
Volume group "vg1" successfully created
创建卷组
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 0 0 wz--n- 20.00g 20.00g
[root@mysql2 ~]# vgdisplay
--- Volume group --- VG Name vg1 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 20.00 GiB PE Size 4.00 MiB Total PE 5119 Alloc PE / Size 0 / 0 Free PE / Size 5119 / 20.00 GiB VG UUID sawr9U-x7US-f0qw-kFPC-z4sU-1ZAr-JWXzjb
通过vgs查看一下 vgdisplay是以详细的方式查看 同理曾在pvdisplay, pvs 和lvdisplay, lvs
[root@mysql2 ~]# lvcreate -L 1G -n lv1 vg1
Logical volume "lv1" created
创建一个1G的逻辑卷
[root@mysql2 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-a---- 1.00g
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 19.00g
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree /dev/sdb vg1 lvm2 a-- 20.00g 19.00g
可以看出还剩下19G 空间
[root@mysql2 ~]# ls /dev/mapper/vg1-lv1
看到了有了这个逻辑卷 可以开始格式化并且加载到分区了
[root@mysql2 ~]# mkfs.ext4 /dev/mapper/vg1-lv1
mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
至此格式化完成紧接着可以挂载
[root@mysql2 ~]# mkdir dandan
[root@mysql2 ~]# mount /dev/mapper/vg1-lv1 ~/dandan
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 1008M 34M 924M 4% /root/dandan
如果说以后需要扩展
假设这是我一个真实环境现在1G用掉了超过80% 担心磁盘爆掉,假设又是在线上 所以动态扩展空间就非常必要了
现在将他扩展到20G
[root@mysql2 ~]# lvextend -L +18.9G /dev/mapper/vg1-lv1 Rounding size to boundary between physical extents: 18.90 GiB Extending logical volume lv1 to 19.90 GiB Logical volume lv1 successfully resized [root@mysql2 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao--- 19.90g [root@mysql2 ~]# resize2fs /dev/mapper/vg1-lv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/vg1-lv1 is mounted on /root/dandan; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/mapper/vg1-lv1 to 5217280 (4k) blocks.
一个块是4K 哦 可以自己设置块的大小
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 20G 35M 19G 1% /root/dandan
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 96.00m
看到还有96M 干脆全部加掉吧
[root@mysql2 ~]# lvextend -L +96.00Mib /dev/mapper/vg1-lv1
Extending logical volume lv1 to 20.00 GiB Logical volume lv1 successfully resized
[root@mysql2 ~]# resize2fs /dev/mapper/vg1-lv1
至此动态增长已经完成
现在老板又说空间不够了又要加
这次我给加的是10G
[root@mysql2 ~]# 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: 0x0006afb5 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2358 18631680 83 Linux /dev/sda3 2358 2611 2031616 82 Linux swap / Solaris Disk /dev/sdb: 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: 0x00000000 Disk /dev/sdc: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 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
Disk /dev/mapper/vg1-lv1: 21.5 GB, 21470642176 bytes 这是之前的sdb 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: 0x00000000
看到了sdc了吧
[root@mysql2 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 0
[root@mysql2 ~]# vgextend vg1 /dev/sdc
Volume group "vg1" successfully extended
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 2 1 0 wz--n- 29.99g 10.00g
看到了vg1扩展了10G 现在老板要把这10G 全部加到LV1里面
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 2 1 0 wz--n- 29.99g 9.99g
[root@mysql2 ~]# lvextend -L +9.9999G /dev/mapper/vg1-lv1
Rounding size to boundary between physical extents: 9.99 GiB Extending logical volume lv1 to 29.99 GiB Logical volume lv1 successfully resized
注意有的时候你vgs查看明明有10G 却加不了10G原因是因为一个block块是4K 你不是刚刚好整除的块数 所以你就加9.9999G吧
[root@mysql2 ~]# resize2fs /dev/mapper/vg1-lv1 如果发现不行所以执行下面的命令
resize2fs 1.41.12 (17-May-2010) Please run 'e2fsck -f /dev/mapper/vg1-lv1' first.
[root@mysql2 ~]# e2fsck -f /dev/mapper/vg1-lv1
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 /dev/mapper/vg1-lv1: 11/1310720 files (0.0% non-contiguous), 91153/5217280 blocks
[root@mysql2 ~]# resize2fs /dev/mapper/vg1-lv1
resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/vg1-lv1 to 7862272 (4k) blocks. The filesystem on /dev/mapper/vg1-lv1 is now 7862272 blocks long.
[root@mysql2 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-a---- 29.99g
[root@mysql2 ~]# mount /dev/mapper/vg1-lv1 /root/dandan/ 因为之前重启过机器所以需要再次挂载如果希望启动机器自动挂载的话
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 30G 172M 28G 1% /root/dandan
[root@mysql2 ~]# vi /etc/fstab
再最后加上
/dev/mapper/vg1-lv1 /root/dandan ext4 defaults 0 0
保存退出
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 30G 35M 28G 1% /root/dandan
扩展逻辑卷先扩大lv再扩大文件系统
缩小逻辑卷 先缩小文件系统再缩小lv
又过了一阵子发现不需要那么大空间了 又要缩到10G
[root@mysql2 ~]# umount /root/dandan/
[root@mysql2 ~]# e2fsck -f /dev/mapper/vg1-lv1
e2fsck 1.41.12 (17-May-2010) /dev/mapper/vg1-lv1: clean, 11/1966080 files, 167409/7862272 blocks 检查下文件系统的完成性 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 /dev/mapper/vg1-lv1: 11/1966080 files (0.0% non-contiguous), 167409/7862272 blocks
[root@mysql2 ~]# resize2fs /dev/mapper/vg1-lv1 10G
resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/vg1-lv1 to 2621440 (4k) blocks. The filesystem on /dev/mapper/vg1-lv1 is now 2621440 blocks long. 将文件系统减小到10G
[root@mysql2 ~]# mount /dev/mapper/vg1-lv1 /root/dandan/
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 9.9G 164M 9.2G 2% /root/dandan
[root@mysql2 ~]# umount /dev/mapper/vg1-lv1
[root@mysql2 ~]# lvchange -an /dev/mapper/vg1-lv1 -an的意思是不让cd到/dev/mapper/vg1-lv1 -ay的意思是可以cd到/dev/mapper/vg1-lv1
暂时不让使用逻辑卷
[root@mysql2 ~]# lvreduce -L -19G /dev/mapper/vg1-lv1
Reducing logical volume lv1 to 9.99 GiB Logical volume lv1 successfully resized [root@mysql2 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi------ 9.99g
[root@mysql2 ~]# lvchange -ay /dev/mapper/vg1-lv1
删除掉之后可以再打开逻辑卷让使用
注:如果不做lvchange操作而直接做减小逻辑卷,可能会导致数据寸坏,【数据无价】。如果要动态就不要lvchange -an 了
然后移除多余的卷组
[root@mysql2 ~]# lvchange -an /dev/mapper/vg1-lv1
[root@mysql2 ~]# vgchange -an /dev/vg1
0 logical volume(s) in volume group "vg1" now active
[root@mysql2 ~]# vgreduce vg1 /dev/sdc
Removed "/dev/sdc" from volume group "vg1" 将/dev/sdc从vg1中取出
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 10.00g 看到只 剩下一个PV
[root@mysql2 ~]# vgreduce vg1 /dev/sdc
Removed "/dev/sdc" from volume group "vg1"
[root@mysql2 ~]# lvchange -ay /dev/mapper/vg1-lv1
[root@mysql2 ~]# vgchange -ay /dev/vg1
1 logical volume(s) in volume group "vg1" now active
[root@mysql2 ~]# mount /dev/mapper/vg1-lv1 /root/dandan/
[root@mysql2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 7.2G 9.5G 44% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 291M 33M 243M 12% /boot /dev/mapper/vg1-lv1 9.9G 151M 9.2G 2% /root/dandan
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg1 lvm2 a-- 20.00g 10.00g
/dev/sdc vg1 lvm2 a-- 10.00g 10.00g 注意这个是之前的
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree /dev/sdb vg1 lvm2 a-- 20.00g 10.00g /dev/sdc lvm2 a-- 10.00g 10.00g 这个是现在的
两个做一下对比知道现在的/dev/sdc已经不属于任何的卷组了对吧
至此可以remove 掉/dev/sdc了
[root@mysql2 ~]# pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped
这样sdc盘又可以干其他的事情去了
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree /dev/sdb vg1 lvm2 a-- 20.00g 10.00g
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 10.00g
[root@mysql2 ~]# pvs
PV VG Fmt Attr PSize PFree /dev/sdb vg1 lvm2 a-- 20.00g 10.00g
[root@mysql2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree vg1 1 1 0 wz--n- 20.00g 10.00g
[root@mysql2 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi------ 9.99g
至此成功摘除/dev/sdc 可以拿出来 卖掉买恒大冰泉!!