LVM创建与管理

一、什么是LVM.?
LVM是Logical Volume Manager(逻辑卷管理)的简写, 它是Linux环境下磁盘分区继续管理的一种机制,
它由Heinz Mauelshagen在Linux2.4内核上实现;
二、为什么要用LVM.?
文件系统建立在逻辑卷上, 而逻辑卷可根据需要改变大小(在卷组容量范围内)以满足需求;
使用LVM主要是方便管理、增加了系统的扩展性, 可以随时按要求增大,或根据使用情况对各逻辑进行调整,
当系统空间不足而加入新的磁盘时,不必把用户的数据从原硬盘迁移到新硬盘, 而只需把新硬盘加入到卷组并扩充逻辑卷即可;

三、配置与管理
1. 测试环境;
[root@Centos1 ~]# cat /etc/redhat-release && uname -r
CentOS release 6.6 (Final)
2.6.32-504.12.2.el6.x86_64
2. 创建分区;
[root@Centos1 ~]# fdisk /dev/sdc
=================================================================
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb4007a2c.
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-2610, default 1): #起始柱面
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +8G #分区大小为8G

Command (m for help): n #新建一个分区;
Command action
e extended
p primary partition (1-4)
p #创建主分区
Partition number (1-4): 2 #分区编号
First cylinder (1046-2610, default 1046): #起始柱面
Using default value 1046
Last cylinder, +cylinders or +size{K,M,G} (1046-2610, default 2610): +8G #分区大小为8G

Command (m for help): n #新建一个分区;
Command action
e extended
p primary partition (1-4)
p #创建主分区
Partition number (1-4): 3 #分区编号
First cylinder (2091-2610, default 2091): #起始柱面
Using default value 2091
Last cylinder, +cylinders or +size{K,M,G} (2091-2610, default 2610): +8G #分区大小为8G
Value out of range. #提示硬盘所剩容量没有8G.
Last cylinder, +cylinders or +size{K,M,G} (2091-2610, default 2610): #结束柱面为默认. 大概还剩4G.
Using default value 2610

Command (m for help): p #查看分区

Disk /dev/sdc: 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: 0xb4007a2c

Device Boot Start End Blocks Id System
/dev/sdc1 1 1045 8393931 83 Linux
/dev/sdc2 1046 2090 8393962+ 83 Linux
/dev/sdc3 2091 2610 4176900 83 Linux

Command (m for help): t #更改分区id
Partition number (1-4): 1 #分区编号
Hex code (type L to list codes): 8e #LVM的分区id为8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): t #更改分区id
Partition number (1-4): 2 #分区编号
Hex code (type L to list codes): 8e #LVM的分区id为8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): t #更改分区id
Partition number (1-4): 3 #分区编号
Hex code (type L to list codes): 8e #LVM的分区id为8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p #查看分区

Disk /dev/sdc: 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: 0xb4007a2c

Device Boot Start End Blocks Id System
/dev/sdc1 1 1045 8393931 8e Linux LVM
/dev/sdc2 1046 2090 8393962+ 8e Linux LVM
/dev/sdc3 2091 2610 4176900 8e Linux LVM

Command (m for help): w #写入分区表
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
=================================================================
3. 创建物理卷(PV);
[root@Centos1 ~]# pvcreate /dev/sdc{1,2,3}
Physical volume “/dev/sdc1” successfully created
Physical volume “/dev/sdc2” successfully created
Physical volume “/dev/sdc3” successfully created
[root@Centos1 ~]# pvdisplay #列出已经创建的物理卷;

4. 创建卷组VG01(VG);
[root@Centos1 ~]# vgcreate VG01 /dev/sdc{1,2,3}
Volume group “VG01” successfully created
[root@Centos1 ~]# vgremove VG01 #将卷组VG01移除;
Volume group “VG01” successfully removed
[root@Centos1 ~]# vgdisplay #列出已经创建的卷组;
#默认PE大小为4MB, PE是卷组的最小存储的单元, 可以通过-s参数修改大小;

5. 分割500M给新的逻辑卷LV01(LV);
[root@Centos1 ~]# lvcreate -L 500M -n LV01 VG01
Logical volume “LV01” created
[root@Centos1 ~]# lvdisplay #列出已经创建的逻辑卷;

6. 格式化逻辑卷;
[root@Centos1 ~]# mkfs.ext4 /dev/VG01/LV01
7. 挂载逻辑卷;
[root@Centos1 ~]# mount /dev/VG01/LV01 /data
[root@Centos1 ~]# df -h

四、扩展逻辑卷、卷组和减少逻辑卷大小
1. 逻辑卷LV01不够用了, 我们如何给它增加空间呢?
[root@Centos1 ~]# lvextend -L +500M /dev/VG01/LV01
[root@Centos1 ~]# resize2fs /dev/VG01/LV01 #同步文件系统
[root@Centos1 ~]# df -h

2. 扩展卷组;
[root@Centos1 ~]# pvcreate /dev/sdd1 #注: 对sdd硬盘分区后一定要将分区id改为’8e’
[root@Centos1 ~]# vgextend VG01 /dev/sdd1
[root@Centos1 ~]# vgdisplay

3. 减少逻辑卷大小;
卸载 –> e2fsck检测剩余空间 –> resize2fs 同步文件系统 –> 使用lvreduce命令将逻辑卷减少.
注意: 文件系统大小和逻辑卷大小一定要保护一致才行. 如果逻辑卷大于文件系统,由于部分区域未格式化成文件系统会
造成空间的浪费.如果逻辑卷小于文件系统,那数据就出问题了.
[root@Centos1 ~]# umount /data/ #卸载/data目录;
[root@Centos1 ~]# e2fsck -f /dev/VG01/LV01 #检测剩余空间;

[root@Centos1 ~]# resize2fs /dev/VG01/LV01 600M #将文件系统系统减少到700M;
[root@Centos1 ~]# lvreduce -L 600 /dev/VG01/LV01 #将逻辑卷减少到700M;
[root@Centos1 ~]# mount /dev/VG01/LV01 /data
ps: 笔者练习时, 逻辑卷小于系统,
4. 当磁盘或分区损坏时, 如何转移数据;
pvmove 转移空间数据 –> vgreduce命令将即将坏的磁盘或者分区从卷组VG01里面移除 –> pvremove命令将即将坏的磁盘
或者分区从系统中删除掉 –> 拆除或修复磁盘.
[root@Centos1 ~]# pvmove /dev/sdc1
/dev/sdc1: Moved: 2.0%
/dev/sdc1: Moved: 100.0%
[root@Centos1 ~]# vgreduce VG01 /dev/sdc1
Removed “/dev/sdc1” from volume group “VG01”
[root@Centos1 ~]# pvremove /dev/sdc1
Labels on physical volume “/dev/sdc1” successfully wiped
5. 删除整个逻辑卷;
umount卸载逻辑卷 –> 修改配置文件/etc/fstab(若是之前更改过) –> lvremove 删除逻辑卷LV01 –> vgremove 删除卷组VG01 –> pvremove 将物理卷转换成普通卷
[root@Centos1 ~]# umount /dev/VG01/LV01
[root@Centos1 ~]# lvremove /dev/VG01/LV01
Do you really want to remove active logical volume LV01? [y/n]: y
Logical volume “LV01” successfully removed[root@Centos1 ~]# vgremove VG01
volume “VG01” successfully removed
[root@Centos1 ~]# pvremove /dev/sdc1
No PV label found on /dev/sdc1. #之前已卸载
[root@Centos1 ~]# pvremove /dev/sdc2
Labels on physical volume “/dev/sdc2” successfully wiped
[root@Centos1 ~]# pvremove /dev/sdc3
Labels on physical volume “/dev/sdc3” successfully wiped
PS: 删除完了, 别忘记修改分区ID标识, 将’8e(LVM)’改为’83(Linux分区)’;

此条目发表在linux分类目录,贴了标签。将固定链接加入收藏夹。