一、 前言
??? 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何xx评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某个分区需要的容量,还要预见该分区以后可能需要的容量的{zd0}值。因为如果估计不准确,当遇到某个分区不够用时管理员可能甚至要备份整个系统、xx硬盘、重新对硬盘分区,然后恢复数据到新分区。
??? 虽然现在有很多动态调整磁盘的工具可以使用,例如Partation Magic等等,但是它并不能xx解决问题,因为某个分区可能会再次被耗尽;另外一个方面这需要重新引导系统才能实现,对于很多关键的服务器,停机是不可接受的,而且对于添加新硬盘,希望一个能跨越多个硬盘驱动器的文件系统时,分区调整程序就不能解决问题。
??? 因此xx的解决方法应该是在零停机前提下可以自如对文件系统的大小进行调整,可以方便实现文件系统跨越不同磁盘和分区。幸运的是Linux提供的逻辑盘卷管理(LVM,Logical Volume Manager)机制就是一个xx的解决方案。
??? LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logical volumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配,例如按照使用用途进行定义:“development”和“sales”,而不是使用物理磁盘名“sda”和“sdb”。而且当系统添加了新的磁盘,通过LVM管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可。
二、 LVM基本术语
??? 前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。首先我们讨论以下几个LVM术语:
??? 1.物理存储介质(The physical media)
????? 这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统{zd1}层的存储单元。
??? 2.物理卷(physical volume)
????? 物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
??? 3.卷组(Volume Group)
????? LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
??? 4.逻辑卷(logical volume)
????? LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
??? 5.PE(physical extent)
????? 每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有{wy}编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
??? 6.LE(logical extent)
????? 逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
?
??? 首先可以看到,物理卷(PV)被由大小等同的基本单元PE组成。
??? 一个卷组由一个或多个物理卷组成,
??? 从上图可以看到,PE和LE有着一一对应的关系。逻辑卷建立在卷组上。逻辑卷就相当于非LVM系统的磁盘分区,可以在其上创建文件系统。
??? 下图是磁盘分区、卷组、逻辑卷和文件系统之间的逻辑关系的示意图:
??? 和非LVM系统将包含分区信息的元数据保存在位于分区的起始位置的分区表中一样,逻辑卷以及卷组相关的元数据也是保存在位于物理卷起始处的VGDA(卷组描述符区域)中。VGDA包括以下内容: PV描述符、VG描述符、LV描述符、和一些PE描述符 。
??? 系统启动LVM时xxVG,并将VGDA加载至内存,来识别LV的实际物理存储位置。当系统进行I/O操作时,就会根据VGDA建立的映射机制来访问实际的物理位置。
四、 创建和管理LVM
以在虚拟机中虚拟的一个125M的硬盘为示例
1.创建LVM卷
[root@huanxiangwu dev]# df -h /dev/hdd 文件系统 容量 已用 可用 已用% 挂载点 - 125M 88K 125M 1% /dev
创建物理分区
[root@huanxiangwu dev]# fdisk /dev/hdd Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklab el Building a new DOS disklabel. 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) Command (m for help): p Disk /dev/hdd: 115 MB, 115343360 bytes 16 heads, 63 sectors/track, 223 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-223, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-223, default 223): Using default value 223 Command (m for help): t Selected partition 1 Hex code (type L to list codes): L 0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris 1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx 5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data 6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility 8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt 9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/hdd: 115 MB, 115343360 bytes 16 heads, 63 sectors/track, 223 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Device Boot Start End Blocks Id System /dev/hdd1 1 223 112360+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
查看LVM分区
[root@huanxiangwu dev]# sfdisk -l /dev/hdd Disk /dev/hdd: 223 cylinders, 16 heads, 63 sectors/track Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/hdd1 0+ 222 223- 112360+ 8e Linux LVM /dev/hdd2 0 - 0 0 0 Empty /dev/hdd3 0 - 0 0 0 Empty /dev/hdd4 0 - 0 0 0 Empty
将hdd1制作成LVM物理卷
[root@huanxiangwu dev]# pvcreate /dev/hdd1 Physical volume "/dev/hdd1" successfully created
查看物理LVM分区
[root@huanxiangwu dev]# pvs PV VG Fmt Attr PSize PFree /dev/hda2 VolGroup00 lvm2 a- 7.88G 0 /dev/hdd1 lvm2 -- 109.73M 109.73M
使用vgcreate创建vg_test卷组
[root@huanxiangwu dev]# vgcreate vg_test /dev/hdd1 Volume group "vg_test" successfully created
查看当前卷组
[root@huanxiangwu dev]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup00 1 2 0 wz--n- 7.88G 0 vg_test 1 0 0 wz--n- 108.00M 108.00M
使用lvcreate在vg_test卷组里新建一个10M大小的LVM分区
[root@huanxiangwu dev]# lvcreate --size 10M --name lvm_u1 vg_test Rounding up size to full physical extent 12.00 MB Logical volume "lvm_u1" created
查看逻辑卷信息
[root@huanxiangwu dev]# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert LogVol00 VolGroup00 -wi-ao 7.38G LogVol01 VolGroup00 -wi-ao 512.00M lvm_u1 vg_test -wi-a- 12.00M
查看卷组剩余空间(96M)
[root@huanxiangwu dev]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup00 1 2 0 wz--n- 7.88G 0 vg_test 1 1 0 wz--n- 108.00M 96.00M
在LVM分区上创建ext3文件系统
[root@huanxiangwu dev]# mkfs.ext3 /dev/mapper/vg_test-lvm_u1 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 3072 inodes, 12288 blocks 614 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=12582912 2 block groups 8192 blocks per group, 8192 fragments per group 1536 inodes per group Superblock backups stored on blocks: 8193 Writing inode tables: done Creating journal (1024 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
2.使用LVM卷
使用mount挂载后就可以正常使用了
[root@huanxiangwu dev]# mkdir /mnt/u1 [root@huanxiangwu dev]# mount -t ext3 /dev/mapper/vg_test-lvm_u1 /mnt/u1/ [root@huanxiangwu dev]# df -m /mnt/u1/ 文件系统 1M-块 已用 可用 已用% 挂载点 /dev/mapper/vg_test-lvm_u1 12 2 10 11% /mnt/u1
3.扩大LVM卷
卸载卷
[root@huanxiangwu dev]# umount /mnt/u1/
调整卷大小
[root@huanxiangwu dev]# lvresize --size 50M /dev/vg_test/lvm_u1 Rounding up size to full physical extent 52.00 MB Extending logical volume lvm_u1 to 52.00 MB Logical volume lvm_u1 successfully resized
使用e2fsck检查整个文件系统
[root@huanxiangwu dev]# e2fsck -f /dev/vg_test/lvm_u1 e2fsck 1.39 (29-May-2006) 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/vg_test/lvm_u1: 13/3072 files (7.7% non-contiguous), 5180/12288 blocks
使用resize2fs调整此卷上的ext3文件系统大小
[root@huanxiangwu dev]# resize2fs /dev/vg_test/lvm_u1 52M resize2fs 1.39 (29-May-2006) Resizing the filesystem on /dev/vg_test/lvm_u1 to 53248 (1k) blocks. The filesystem on /dev/vg_test/lvm_u1 is now 53248 blocks long.
然后就可以重新使用mount挂载使用了
4.缩小LVM卷
过程跟扩大lvm差不多
卸载卷
[root@huanxiangwu dev]# umount /mnt/u1/
使用e2fsck检查整个文件系统
[root@huanxiangwu dev]# e2fsck -f /dev/vg_test/lvm_u1 e2fsck 1.39 (29-May-2006) 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/vg_test/lvm_u1: 13/10752 files (7.7% non-contiguous), 6248/53248 blocks
使用resize2fs调整此卷上的ext3文件系统大小
[root@huanxiangwu dev]# resize2fs /dev/vg_test/lvm_u1 30M resize2fs 1.39 (29-May-2006) Resizing the filesystem on /dev/vg_test/lvm_u1 to 30720 (1k) blocks. The filesystem on /dev/vg_test/lvm_u1 is now 30720 blocks long.
调整卷大小
[root@huanxiangwu dev]# lvresize --size 30M /dev/vg_test/lvm_u1 Rounding up size to full physical extent 32.00 MB WARNING: Reducing active logical volume to 32.00 MB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lvm_u1? [y/n]: y Reducing logical volume lvm_u1 to 32.00 MB Logical volume lvm_u1 successfully resized
重新使用mount挂载使用
5.删除LVM逻辑卷和组
卸载卷
[root@huanxiangwu dev]# umount /mnt/u1/
删除卷组里的LVM逻辑卷
[root@huanxiangwu dev]# lvremove /dev/vg_test/lvm_u1 Do you really want to remove active logical volume lvm_u1? [y/n]: y Logical volume "lvm_u1" successfully removed
删除LVM卷组
[root@huanxiangwu dev]# vgremove vg_test Volume group "vg_test" successfully removed
注:前三部分概念部分摘自: