1. LVM(Logical Volume Manager):
可以弹性调整文件系统的容量,即把多个物理分区整合为像一个磁盘一样,实现文件系统自由扩展,缩减
2. LVM的组成:
PV(Physical Volume):物理卷
LVM基本的逻辑储存块,但比基本的物理储存块(分区,硬盘等)多出一些LVM有关的管理参数
VG (Volume Group): 卷组
由多个PV组成,类似于非LVM系统中的物理硬盘.
LV (Logical Volume):逻辑卷
类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
PE (Physical Extend):物理扩展块
相当于文件系统中的block,调整PE会影响到VG的最大容量
LVM各组件之间的关系图示:
3.具体实现流程:
(1)先划分4个分区,每个分区10G,systemID为8e
(2) 4个分区整合成一个VG,每个PE16M,vg名为“textvg"
(3) 把VG容量划分LV,LV名为“textlv”
(4) 然后把LV格式化为ext4文件系统,并挂载到/mnt/lvm中
划分分区:
Command (m for help): p 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: 0x0004aa62 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7859 7923 521113+ 83 Linux /dev/sda4 7924 15665 62187615 5 Extended Command (m for help): n #添加新分区 First cylinder (7924-15665, default 7924): #指定柱面号,选择默认即可 Using default value 7924 Last cylinder, +cylinders or +size{K,M,G} (7924-15665, default 15665): +10G #指定分区大小 Command (m for help): t #指定分区类型 Partition number (1-5): 5 #指定类型的分区号 Hex code (type L to list codes): 8e #指定为8e,即Linux LVM Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): w #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.
创建完以后内核无法读取新建分区表信息,通过partx -a 命令告知内核:
[root@localhost ~]# partx -a /dev/sda BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5 BLKPG: Device or resource busy error adding partition 6 BLKPG: Device or resource busy error adding partition 7
然后分区信息就可以通过fdisk -l 看到了:
[root@localhost ~]# fdisk -l 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: 0x0004aa62 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7859 62914560 8e Linux LVM /dev/sda3 7859 7923 521113+ 83 Linux /dev/sda4 7924 15665 62187615 5 Extended /dev/sda5 7924 9229 10490413+ 8e Linux LVM /dev/sda6 9230 10535 10490413+ 8e Linux LVM /dev/sda7 10536 11841 10490413+ 8e Linux LVM
上边sda5-sda7即为我们创建的新分区
4.创建PV:
常用工具
pvcreate: 将物理分区新建为PV
pvscan: 简要显示PV信息
pvdisplay: 显示PV详细信息
root@localhost ~]# pvscan #查看当前系统是否有PV,一下为以前创建 PV /dev/sda2 VG vg0 lvm2 [59.99 GiB / 7.99 GiB free] Total: 1 [59.99 GiB] / in use: 1 [59.99 GiB] / in no VG: 0 [0 ] [root@localhost ~]# pvcreate /dev/sda{5,6,7,8}#一起创建分区5,6,7,8 Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created Physical volume "/dev/sda7" successfully created Physical volume "/dev/sda8" successfully created
[root@localhost ~]# pvscan PV /dev/sda5 lvm2 [10.00 GiB] PV /dev/sda6 lvm2 [10.00 GiB] PV /dev/sda7 lvm2 [10.00 GiB] PV /dev/sda8 lvm2 [10.00 GiB]
详细列出PV信息:
[root@localhost ~]# pvdisplay "/dev/sda5" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sda5 #分区设备名称 VG Name PV Size 10.00 GiB #PV容量大小 Allocatable NO #是否被分配 PE Size 0 #PE大小 Total PE 0 #共有几个PE Free PE 0 #空余的PE Allocated PE 0 #未被分配的PE数量 PV UUID fbU7AS-pATY-CTt4-be8l-l72p-zFnP-qGBKk7#PV的UUID
5.创建VG:
常用工具:
vgscan:显示vg简要信息
vgdisplay:显示vg详细信息
vgcreate:创建vg
vgextend:扩充vg大小
vgreduce:缩减vg大小,前提先pvremove PV
[root@localhost ~]# vgcreate -s 16M textvg /dev/sda{5,6,7} Volume group "textvg" successfully created [root@localhost ~]# vgscan Reading all physical volumes. This may take a while... Found volume group "textvg" using metadata type lvm2
VG创建成功
[root@localhost ~]# pvscan PV /dev/sda5 VG textvg lvm2 [10.00 GiB / 10.00 GiB free] PV /dev/sda6 VG textvg lvm2 [10.00 GiB / 10.00 GiB free] PV /dev/sda7 VG textvg lvm2 [10.00 GiB / 10.00 GiB free] PV /dev/sda8 lvm2 [10.00 GiB]
[root@localhost ~]# vgdisplay --- Volume group --- VG Name textvg 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 30.00 GiB #VG大小为30G PE Size 16.00 MiB Total PE 1920 Alloc PE / Size 0 / 0 Free PE / Size 1920 / 30.00 GiB VG UUID m6wlkq-7bzX-qkwN-NK5j-wTEJ-mXt0-qewikv
上图为vg的详细信息
如果我们想要增加VG的容量,我们可以把剩余的sda8扩展到VG里
[root@localhost ~]# vgextend textvg /dev/sda8 Volume group "textvg" successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name textvg System ID Format lvm2 Metadata Areas 4 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 4 Act PV 4 VG Size 40.00 GiB #VG大小由30G增加到40G PE Size 16.00 MiB Total PE 2560 Alloc PE / Size 0 / 0 Free PE / Size 2560 / 40.00 GiB VG UUID m6wlkq-7bzX-qkwN-NK5j-wTEJ-mXt0-qewikv
6.创建LV
命令为lvcreate 创建lv
-l:后接PE的个数
-L:后接PE的大小
-n: 后接LV的名称
......
其他命令同上
[root@localhost ~]# lvcreate -L 40G -n textlv textvg#将整个VG空间分配给LV,大小为40G Logical volume "textlv" created#创建成功 [root@localhost ~]# lvdisplay#查看一下LV的详细信息 --- Logical volume --- LV Path /dev/textvg/textlv LV Name textlv VG Name textvg LV UUID wvK7EH-Mumg-chhg-XIZ2-fBfv-Feoz-td8Zr6 LV Write Access read/write LV Creation host, time localhost.localdomain, 2015-08-16 09:52:55 +0800 LV Status available # open 0 LV Size 40.00 GiB#大小即为40G Current LE 2560 Segments 4 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:4
接下来就是为LV格式化文件系统和挂载:
[root@localhost ~]# mkfs -t ext4 /dev/textvg/textlv #格式化LV为ext4格式 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 2621440 inodes, 10485760 blocks 524288 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 320 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, 1605632, 2654208, 4096000, 7962624 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 21 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /mnt/lvm#新建一个挂载点 [root@localhost ~]# mount /dev/textvg/textlv /mnt/lvm/#挂载LV到挂载点 [root@localhost ~]# df#查看磁盘信息最后一行 Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg0-root 20511356 306004 19156776 2% / tmpfs 502176 0 502176 0% /dev/shm /dev/sda1 194241 27901 156100 16% /boot /dev/mapper/vg0-usr 10190136 1963888 7701960 21% /usr /dev/mapper/vg0-var 20511356 201768 19261012 2% /var /dev/sda3 488384 396 461936 1% /mnt/sda3 /dev/mapper/textvg-textlv 41153856 49032 39007672 1% /mnt/lvm #LV创建成功
到此LV就已经创建完成
7.LV扩展:
LVM最大的特点就是可以实现弹性调整磁盘容量
(1)用fdisk添加systemID为8e的分区
(2)用pvcreate创建PV
(3)利用vgextend将PV加入textvg
(4)利用lvresize将加入PV里的PE加入LV中
(5)用resize2fs增加文件系统容量
首先用fdisk添加一个分区sda9具体就不截图了。。
然后创建PV
[root@localhost ~]# pvcreate /dev/sda9#创建新加磁盘sda9到PV Physical volume "/dev/sda9" successfully created [root@localhost ~]# pvscan#列出可以看到sda9已经创建 PV /dev/sda5 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda6 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda7 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda8 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda2 VG vg0 lvm2 [59.99 GiB / 7.99 GiB free] PV /dev/sda9 lvm2 [10.00 GiB] Total: 6 [110.00 GiB] / in use: 5 [99.99 GiB] / in no VG: 1 [10.00 GiB] [root@localhost ~]# vgextend textvg /dev/sda9#加入VG Volume group "textvg" successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name textvg System ID Format lvm2 Metadata Areas 5 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 5 Act PV 5 VG Size 50.00 GiB#加入后由原来的40G达到50G PE Size 16.00 MiB Total PE 3200 Alloc PE / Size 2560 / 40.00 GiB Free PE / Size 640 / 10.00 GiB VG UUID m6wlkq-7bzX-qkwN-NK5j-wTEJ-mXt0-qewikv
[root@localhost ~]# lvresize -L +10G /dev/textvg/textlv #同样放大LV Size of logical volume textvg/textlv changed from 40.00 GiB (2560 extents) to 50.00 GiB (3200 extents). Logical volume textlv successfully resized [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/textvg/textlv LV Name textlv VG Name textvg LV UUID wvK7EH-Mumg-chhg-XIZ2-fBfv-Feoz-td8Zr6 LV Write Access read/write LV Creation host, time localhost.localdomain, 2015-08-16 09:52:55 +0800 LV Status available # open 1 LV Size 50.00 GiB Current LE 3200 Segments 5 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:4
到此扩展结束
大家看到扩展后文件系统并没有增加容量,可以使用resize2fs解决
[root@localhost ~]# df -h /mnt/lvm/#显示到LV大小还是原来的49G. Filesystem Size Used Avail Use% Mounted on /dev/mapper/textvg-textlv 40G 48M 38G 1% /mnt/lvm
[root@localhost ~]# resize2fs /dev/textvg/textlv #resize2fs重新定义文件系统大小 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/textvg/textlv is mounted on /mnt/lvm; on-line resizing required old desc_blocks = 3, new_desc_blocks = 4 Performing an on-line resize of /dev/textvg/textlv to 13107200 (4k) blocks. The filesystem on /dev/textvg/textlv is now 13107200 blocks long. [root@localhost ~]# df -h /mnt/lvm/#再次列出后就为50G了 Filesystem Size Used Avail Use% Mounted on /dev/mapper/textvg-textlv 50G 52M 47G 1% /mnt/lvm
大家看到lvm大小就为正常的50G 了
既然能扩展同样就能缩减。。
8.LV缩减
我们随便找一个分区抽调出来来实现LV缩减
假如我们抽调/dev/sda8
[root@localhost ~]# pvdisplay /dev/sda8#先查看一下抽调的Sda8的大小 --- Physical volume --- PV Name /dev/sda8 VG Name textvg PV Size 10.00 GiB / not usable 4.54 MiB#这里显示为10G Allocatable yes (but full) #PV大小为10G PE Size 16.00 MiB Total PE 640 Free PE 0 Allocated PE 640 PV UUID c3rW7L-WcGZ-Aghs-VXzZ-XPIw-Onu8-xTK9z4
从上边我们看到sda8的PV size为10G,那我们就缩减10G
首先卸载挂载点,然后用fsck对磁盘进行检测
[root@localhost ~]# umount /mnt/lvm/#卸载挂载点 [root@localhost ~]# fsck -f -t ext4 /dev/textvg/textlv #强制检测分区 fsck from util-linux-ng 2.17.2 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/textvg-textlv: 11/3276800 files (0.0% non-contiguous), 251699/13107200 blocks
[root@localhost ~]# pvscan #显示一下PV的总量,需要缩减多少 PV /dev/sda5 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda6 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda7 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda8 VG textvg lvm2 [10.00 GiB / 0 free] PV /dev/sda9 VG textvg lvm2 [10.00 GiB / 0 free] Total: 6 [109.99 GiB] / in use: 6 [109.99 GiB] / in no VG: 0 [0 ] [root@localhost ~]# resize2fs /dev/textvg/textlv 40G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/textvg/textlv to 10485760 (4k) blocks. The filesystem on /dev/textvg/textlv is now 10485760 blocks long. [root@localhost ~]# mount /dev/textvg/textlv /mnt/lvm/#挂载到/mnt/lvm [root@localhost ~]# df -h /mnt/lvm/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/textvg-textlv 40G 48M 38G 1% /mnt/lvm #这样就缩减到40G
这样就实现了LVM的创建,扩展和缩减。。。。
转载于:https://blog.51cto.com/zhaohongfei/1689898