LVM逻辑卷管理(Logical Volume Manager)
什么是LVM?LVM的产生及作用
随着数据的逐渐增加,当初设计的磁盘大小已经满足不了现在的需求了,当磁盘面临着满盘时,运维人员如何解决问题?是换一个更大的磁盘,把原先的数据copy到现在更大的磁盘里面,继续撑一段时间,还是寻找其它方法?LVM的出现很好的解决了这一问题。LVM是linux环境下对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。
物理卷physical volume (PV)
物理卷是硬盘分区,也可以是整个硬盘或已创建的Software RAID 卷。
卷组volume group (VG)
卷组是LVM 中最高抽象层,是由一个或多个物理卷所组成的存储器池。
物理块physical extent (PE)
物理卷按大小相等的“块”为单位存储,块的大小与卷组中逻辑卷块的大小相同。
逻辑块logical extent (LE)
逻辑卷按“块”为单位存储,在一卷组中的所有逻辑卷的块大小是相同的。
逻辑卷logical volume (LV)
逻辑卷相当于非LVM 系统中的分区,它在卷组上建立,是一个标准的块设备,可以在其上建立文件系统。
从一块硬盘到能使用LV文件系统的步骤:
硬盘----分区----PV----VG----LV----格式化(LV为ext文件系统)----挂载
LVM的安装
查看lvm 是否安装
[root@mage 192.168.206.143 ~ ] # rpm -qa | grep lvm lvm2-libs-2.02.111-2.el6_6.1.x86_64 lvm2-2.02.111-2.el6_6.1.x86_64
我这里是安装了,如果未安装,则安装。
[root@mage 192.168.206.143 ~ ] # yum install lvm2
创建和管理LVM
1、PV的相关操作
# fdisk /dev/sdb 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-2741, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2741, default 2741): +5G Partition number (1-4): 2 First cylinder (655-2741, default 655): Using default value 655 Last cylinder, +cylinders or +size{K,M,G} (655-2741, default 2741): +5G Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part【8e Linux LVM】 df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 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): p Disk /dev/sdb: 22.5 GB, 22548578304 bytes 255 heads, 63 sectors/track, 2741 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: 0x090eed83 Device Boot Start End Blocks Id System /dev/sdb1 1 654 5253223+ 8e Linux LVM /dev/sdb2 655 1308 5253255 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
pvcreate 创建PV
[root@mage 192.168.206.143 ~ ] # pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created
pvromve 移除PV,让该partition不具有PV属性
[root@mage 192.168.206.143 ~ ] # pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@mage 192.168.206.143 ~ ] # pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created
pvdisplay 显示PV的详细信息,用pvs也可以显示pv的信息
[root@mage 192.168.206.143 ~ ] # pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g
pvmove 移动pv,将此PV的数据转移至其它PV,让该partition不具有PV属性
[root@mage 192.168.206.143 ~ ] # pvmove /dev/sda1 /dev/sda2 #把sda1的数据转移到sda2上面 /dev/sda1: Moved: 100.0%
pvscan 搜索目前系统里面任何具有PV的磁盘或分区
[root@mage 192.168.206.143 ~ ] # pvscan PV /dev/sdb1 VG myvg lvm2 [10.00 GiB / 10.00 GiB free] PV /dev/sdb2 VG myvg lvm2 [10.00 GiB / 10.00 GiB free] Total: 2 [20.00 GiB] / in use: 2 [20.00 GiB] / in no VG: 0 [0 ]
2、VG的相关操作
vgcreate 创建vg,-s PE:指定PE大小,默认为4M
[root@mage 192.168.206.143 ~ ] # vgcreate myvg /dev/sdb1 /dev/sdb2 Volume group "myvg" successfully created
vgdisplay 显示VG的详细信息,用vgs也可以显示VG的信息
vgremove 移除vg,只需跟vg的名称即可
[root@mage 192.168.206.143 ~ ] # vgremove myvg Volume group "myvg" successfully removed
vgextend vg的扩展,先必须添加其它的PV
首先准备好一个PV--->然后使用vgextend命令即可完成扩展
[root@mage 192.168.206.143 ~ ] #创建PV # pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sda1" successfully created Physical volume "/dev/sda2" successfully created [root@mage 192.168.206.143 ~ ] #创建VG # vgcreate myvg /dev/sdb1 /dev/sdb2 volume group called myvg already exists. [root@mage 192.168.206.143 ~ ] #把另外一个分区创建为PV # pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created [root@mage 192.168.206.143 ~ ] #在myvg组里面扩展VG # vgextend myvg /dev/sdc1 Volume group "myvg" successfully extended
vgreduce vg的缩减
确定要移除的PV--->将此PV的数据转移至其它PV---->从卷组中将此PV移除
[root@mage 192.168.206.143 ~ ] #把/dev/sdc1上的数据移动到/dev/sdb1上面 # pvmove /dev/sdc1 /dev/sdb1 No data to move for myvg #我这里没有数据,因为是空盘 [root@mage 192.168.206.143 ~ ] #从VG中把PV移除 # vgreduce myvg /dev/sdc1 Removed "/dev/sdc1" from volume group "myvg"
3、LV的相关操作
lvcreate 创建LV,相关参数:-l 指定PE个数,-L指定LV的大小,
-s创建一个快照卷,-n LV的名称
[root@mage 192.168.206.143 ~ ] # lvcreate -L 5G -n lv1 myvg Logical volume "lv1" created
lvremove 移除lv,需要移除lv的设备文件:
/dev/myvg/lv1或者/dev/mapper/myvg-lv1
[root@mage 192.168.206.143 ~ ] # lvremove /dev/myvg/lv1 Do you really want to remove active logical volume lv1? [y/n]: y Logical volume "lv1" successfully removed
lvextend 扩展LV
先确定要扩展多大-->确定当前VG有足够的空闲空间-->扩展(1、物理边界---2、逻辑边界)
当VG不够时,先增加PV,在扩展VG,最后在VG上面扩展LV。
[root@mage 192.168.206.143 ~ ] #格式化LV # mke2fs -t ext4 /dev/myvg/lv1 [root@mage 192.168.206.143 ~ ] #实现物理扩容 # lvextend -L +4G /dev/myvg/lv1 Size of logical volume myvg/lv1 changed from 5.00 GiB (1280 extents) to 9.00 GiB (2304 extents). #之前5G ,扩容至9G Logical volume lv1 successfully resized [root@mage 192.168.206.143 ~ ] #实现逻辑扩容 # resize2fs /dev/myvg/lv1 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/lv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/lv1 is now 2359296 blocks long.
lvreduce 缩小LV
先卸载--->然后减小逻辑边界---->最后减小物理边界--->在检测文件系统 ====谨慎用===
[root@mage 192.168.206.143 ~ ] # umount /dev/mapper/myvg-lv1 [root@mage 192.168.206.143 ~ ] # e2fsck -f /dev/myvg/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/myvg/lv1: 11/589824 files (0.0% non-contiguous), 72671/2359296 blocks [root@mage 192.168.206.143 ~ ] #把LV减少2G # resize2fs /dev/myvg/lv1 7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/lv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/lv1 is now 1835008 blocks long. [root@mage 192.168.206.143 ~ ] #查看LV发现大小还是之前的,没有变化。 # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 myvg -wi-a----- 9.00g [root@mage 192.168.206.143 ~ ] # lvreduce -L -2G /dev/myvg/lv1 #减少lv的物理边界,指定减少2G # lvreduce -L -2G /dev/myvg/lv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y Size of logical volume myvg/lv1 changed from 9.00 GiB (2304 extents) to 7.00 GiB (1792 extents). Logical volume lv1 successfully resized [root@mage 192.168.206.143 ~ ] #lvs #查看lv的大小发现已经实际的减少了 # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 myvg -wi-a----- 7.00g [root@mage 192.168.206.143 ~ ] e2fsck -f /dev/myvg/lv1 #减少之后检查文件系统 # e2fsck -f /dev/myvg/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/myvg/lv1: 11/458752 files (0.0% non-contiguous), 64447/1835008 blocks