CentOS 6.4 LVM的建立与使用
时间:2014-09-24 15:51 来源:linux.it.net.cn 作者:it
要使用LVM,需要先了解几个参数的意义。
PV:Physical Volume 实体滚动条
VG:Volume Group 滚动条群组
LVM大磁盘是许多PV整合成VG,VG最多能包含65534个PE。
PE:Physical Extend 实体延伸区 LVM默认使用4MB的PE区块,PE是整个LVM最小的储存区块,简单说PE有点像文件系统里面的block。
LV:Logical Volume 逻辑滚动条
VG会被切成LV,LV的大小与LV内的PE有关,LVM的文件名通常指定为/dev/vgname/lvname的样式
PE LV VG之间的关系图:
通过PV VG LV的规划后,利用mkfs可以将你的LV格式化为可以利用的文件系统。
LV写入硬盘的方式:
线性模式(liner): 举例来说 /dev/sda1 /dev/sda2加入到VG当中,并且整个VG只有一个LV时,线性模式就是:当/dev/sda1容量用完后,/dev/sda2才会被使用。(建议的模式)
交错模式(triped):有点像RAID 0,理论上读写性能会比较好。
LVM默认的读写模式是线性模式,如果你使用triped模式,当一个partition损坏时,所有数据都会损毁。
LVM需要核心支持且需安装lvm2这个软件。
我要实现的环境:
分割4个partition,每个partition的容量为1G,且system ID为8e;
全部的partition整合为一个VG,VG名称为yaovg,且PE大小为16M;
全部的VG容量都给LV,LV的名称为yaolv;
这个LV格式化为ext4的文件系统,挂载在/mnt/lvm中
本人都是用vmware虚拟机做的,添加一块硬盘/dev/sdb
对这块硬盘/dev/sdb分区
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[root@minilinux ~]# 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-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-1305, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-1305, default 1305): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 3
First cylinder (265-1305, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-1305, default 1305):
Using default value 1305
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (265-1305, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-1305, default 1305): +1G
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (397-1305, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-1305, default 1305): +1G
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (529-1305, default 529):
Using default value 529
Last cylinder, +cylinders or +size{K,M,G} (529-1305, default 1305): +1G
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (661-1305, default 661):
Using default value 661
Last cylinder, +cylinders or +size{K,M,G} (661-1305, default 1305): +1G
Command (m for help): p
Disk /dev/sdb: 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: 0xb8b17d21
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 1305 8361832+ 5 Extended
/dev/sdb5 265 396 1060258+ 83 Linux
/dev/sdb6 397 528 1060258+ 83 Linux
/dev/sdb7 529 660 1060258+ 83 Linux
/dev/sdb8 661 792 1060258+ 83 Linux
Command (m for help): t
Partition number (1-8): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-8): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-8): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-8): 8
Hex code (type L to list codes): 8e
Changed system type of partition 8 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 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: 0xb8b17d21
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 1305 8361832+ 5 Extended
/dev/sdb5 265 396 1060258+ 8e Linux LVM
/dev/sdb6 397 528 1060258+ 8e Linux LVM
/dev/sdb7 529 660 1060258+ 8e Linux LVM
/dev/sdb8 661 792 1060258+ 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.
[root@minilinux ~]# partprobe
PV阶段:
建立PV很简单,直接使用pvcreate即可!
pvcreate:将实体partition建立成为PV
pvscan:查找目前系统里任何具有PV的磁盘
pvdisplay:显示目前系统上面的PV状态
pvremove:将PV属性移除,让该partition不具有PV属性
将/dev/sdb5-/dev/sdb8建立成为PV格式
1
2
3
4
5
6
7
[root@minilinux ~]# pvscan
No matching physical volumes found
[root@minilinux ~]# pvcreate /dev/sdb{5,6,7,8}
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
Can't open /dev/sdb7 exclusively. Mounted filesystem?
Can't open /dev/sdb8 exclusively. Mounted filesystem?
没挂载上去,就是创建不了。查找了很多资料,还是没解决。
所以决定再添加一块硬盘/dev/sdc试试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
[root@minilinux ~]# 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 0xe0ff2586.
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-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-1305, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-1305, default 1305): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (265-1305, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-1305, default 1305): +1G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder (397-1305, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-1305, default 1305):
Using default value 1305
Command (m for help): n
First cylinder (397-1305, default 397):
Using default value 397
Last cylinder, +cylinders or +size{K,M,G} (397-1305, default 1305): +1G
Command (m for help): p
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: 0xe0ff2586
Device Boot Start End Blocks Id System
/dev/sdc1 1 132 1060258+ 83 Linux
/dev/sdc2 133 264 1060290 83 Linux
/dev/sdc3 265 396 1060290 83 Linux
/dev/sdc4 397 1305 7301542+ 5 Extended
/dev/sdc5 397 528 1060258+ 83 Linux
Command (m for help): t
Partition number (1-5): 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-5): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): p
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: 0xe0ff2586
Device Boot Start End Blocks Id System
/dev/sdc1 1 132 1060258+ 8e Linux LVM
/dev/sdc2 133 264 1060290 8e Linux LVM
/dev/sdc3 265 396 1060290 8e Linux LVM
/dev/sdc4 397 1305 7301542+ 5 Extended
/dev/sdc5 397 528 1060258+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@minilinux ~]# partprobe
分区完毕!
1
2
3
4
5
6
7
[root@minilinux ~]# pvscan
No matching physical volumes found
[root@minilinux ~]# pvcreate /dev/sdc{1,2,3,5}
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc2" successfully created
Physical volume "/dev/sdc3" successfully created
Physical volume "/dev/sdc5" successfully created
创建成功了
更详细显示系统上每个PV信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@minilinux ~]# pvdisplay
"/dev/sdc1" is a new physical volume of "1.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 1.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID UB7cyX-pHjU-JO9x-a0c4-esnM-9k6B-C3ACif
"/dev/sdc2" is a new physical volume of "1.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc2
VG Name
PV Size 1.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Jj0fcC-a5MI-l539-8h3E-SeK5-BdGg-S4ih2D
"/dev/sdc3" is a new physical volume of "1.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc3
VG Name
PV Size 1.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID iqeTJj-Lb8h-ilVk-jkg0-cC5q-exDc-5pvd0b
"/dev/sdc5" is a new physical volume of "1.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc5
VG Name
PV Size 1.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID lcyKsS-zSco-ZPn0-778m-X76b-3VN2-VdL9OM
这样PV就建立好了
VG阶段:
vgcreate:主要建立VG的指令
vgscan:查找系统上面是否有VG存在?
vgdisplay:显示目前系统上面的VG状态
vgextend:在VG内增加额外的PV
vgreduce:在VG内移除PV
vgchange:设定VG是否启动
vgremove:删除一个VG
VG的名称是自定义的
将/dev/sdc1-/dev/sdc3建立成为一个VG,指定PE为16M
1
2
3
4
5
[root@minilinux ~]# vgcreate -s 16M yaovg /dev/sdc{1,2,3}
Volume group "yaovg" successfully created
[root@minilinux ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "yaovg" using metadata type lvm2
发现存在yaovg的VG
1
2
3
4
5
6
[root@minilinux ~]# pvscan
PV /dev/sdc1 VG yaovg lvm2 [1.00 GiB / 1.00 GiB free]
PV /dev/sdc2 VG yaovg lvm2 [1.00 GiB / 1.00 GiB free]
PV /dev/sdc3 VG yaovg lvm2 [1.00 GiB / 1.00 GiB free]
PV /dev/sdc5 lvm2 [1.01 GiB]
Total: 4 [4.01 GiB] / in use: 3 [3.00 GiB] / in no VG: 1 [1.01 GiB]
三个PV被用了,剩下一个/dev/sdc5的PV还没用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@minilinux ~]# vgdisplay
--- Volume group ---
VG Name yaovg
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 3.00 GiB
PE Size 16.00 MiB
Total PE 192
Alloc PE / Size 0 / 0
Free PE / Size 192 / 3.00 GiB
VG UUID 9nAH51-isaO-GKOB-X5NB-sgWg-0shh-Ph77m8
一个VG就建立好了!
将剩余的PV/dev/sdc5)也给yaovg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@minilinux ~]# vgextend yaovg /dev/sdc5
Volume group "yaovg" successfully extended
[root@minilinux ~]# vgdisplay
--- Volume group ---
VG Name yaovg
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 4.00 GiB
PE Size 16.00 MiB
Total PE 256
Alloc PE / Size 0 / 0
Free PE / Size 256 / 4.00 GiB
VG UUID 9nAH51-isaO-GKOB-X5NB-sgWg-0shh-Ph77m8
LV阶段:
创建完VG后,就是要建立分区了,分区就是所谓的LV喽,要将那个yaovg分割为yaolv。这个VG的容量都分配到yaolv里面去。
lvcreate:建立LV
lvscan:查看系统上面的LV
lvdisplay:显示系统上面的LV状态
lvextend:在LV里面增加容量
lvreduce:在LV里面减少容量
lvremove:删除一个LV
lvresize:对LV进行容量大小的调整
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@minilinux ~]# lvcreate -l 256 -n yaolv yaovg
Logical volume "yaolv" created
[root@minilinux ~]# ll /dev/yaovg/yaolv
lrwxrwxrwx. 1 root root 7 Jul 9 23:20 /dev/yaovg/yaolv -> ../dm-0
[root@minilinux ~]# lvdisplay
--- Logical volume ---
LV Path /dev/yaovg/yaolv
LV Name yaolv
VG Name yaovg
LV UUID 2kC7wu-tK00-bM5n-3RDf-C09Y-vYdw-8VoDIU
LV Write Access read/write
LV Creation host, time minilinux, 2013-07-09 23:20:30 +0800
LV Status available
# open 0
LV Size 4.00 GiB
Current LE 256
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
LV的名称必须使用全名/dev/yaovg/yaolv
文件系统阶段
格式化 挂载 查看我的LV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@minilinux ~]# mkfs -t ext4 /dev/yaovg/yaolv
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
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 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
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@minilinux ~]# mkdir /mnt/lvm
[root@minilinux ~]# mount /dev/yaovg/yaolv /mnt/lvm
[root@minilinux ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 19609276 1339268 17273912 8% /
tmpfs 118188 0 118188 0% /dev/shm
/dev/mapper/yaovg-yaolv
4128448 139256 3779480 4% /mnt/lvm
LV的名称为/dev/yaovg/yaolv是为了让使用者找到我们所需要的数据,实际上LVM使用的装置是放在/dev/mapper/目录下。现在已经建立好一个LV,你可以使用/mnt/lvm里面的东东了!
增大LV容量
只要这样做就好
1 用fdisk设定新的具有8e system ID的partition
2 pvcreate建立PV
3 vgextend将PV加入yaovg
4 lvresize将新加入的PV内的PE加入yaolv中
5 通过resize2fs将文件系统的容量确实增加
新增一个512M的partition /dev/sdc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@minilinux ~]# fdisk /dev/sdc
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
First cylinder (529-1305, default 529):
Using default value 529
Last cylinder, +cylinders or +size{K,M,G} (529-1305, default 1305): +512M
Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 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.
[root@minilinux ~]# partprobe
建立新的PV
1
2
3
4
5
6
7
8
9
[root@minilinux ~]# pvcreate /dev/sdc6
Physical volume "/dev/sdc6" successfully created
[root@minilinux ~]# pvscan
PV /dev/sdc1 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc2 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc3 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc5 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc6 lvm2 [517.69 MiB]
Total: 5 [4.51 GiB] / in use: 4 [4.00 GiB] / in no VG: 1 [517.69 MiB]
加上VG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@minilinux ~]# vgextend yaovg /dev/sdc6
Volume group "yaovg" successfully extended
[root@minilinux ~]# vgdisplay
--- Volume group ---
VG Name yaovg
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 0
Max PV 0
Cur PV 5
Act PV 5
VG Size 4.50 GiB
PE Size 16.00 MiB
Total PE 288
Alloc PE / Size 256 / 4.00 GiB
Free PE / Size 32 / 512.00 MiB
VG UUID 9nAH51-isaO-GKOB-X5NB-sgWg-0shh-Ph77m8
放大LV,用lvresize增加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@minilinux ~]# lvresize -l +32 /dev/yaovg/yaolv
Extending logical volume yaolv to 4.50 GiB
Logical volume yaolv successfully resized
[root@minilinux ~]# lvdisplay
--- Logical volume ---
LV Path /dev/yaovg/yaolv
LV Name yaolv
VG Name yaovg
LV UUID 2kC7wu-tK00-bM5n-3RDf-C09Y-vYdw-8VoDIU
LV Write Access read/write
LV Creation host, time minilinux, 2013-07-09 23:20:30 +0800
LV Status available
# open 0
LV Size 4.50 GiB
Current LE 288
Segments 5
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
文件系统没有增加,LVM可以在线直接处理,不需要umount.文件系统的容量,用resize2fs来处理
1
2
3
4
5
6
7
8
9
10
[root@minilinux ~]# resize2fs /dev/yaovg/yaolv
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/yaovg/yaolv is mounted on /mnt/lvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/yaovg/yaolv to 1179648 (4k) blocks.
The filesystem on /dev/yaovg/yaolv is now 1179648 blocks long.
[root@minilinux ~]# df -h /mnt/lvm
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/yaovg-yaolv
4.5G 136M 4.1G 4% /mnt/lvm
减少LV容量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@minilinux ~]# resize2fs /dev/yaovg/yaolv 4000M
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/yaovg/yaolv is mounted on /mnt/lvm; on-line resizing required
On-line shrinking from 1179648 to 1024000 not supported.
缩小文件无法在线进行,必须先卸载
[root@minilinux ~]# umount /mnt/lvm
[root@minilinux ~]# resize2fs /dev/yaovg/yaolv 4000M
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/yaovg/yaolv' first.
要先进行磁盘检查
[root@minilinux ~]# e2fsck -f /dev/yaovg/yaolv
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/yaovg/yaolv: 11/294912 files (0.0% non-contiguous), 53334/1179648 blocks
[root@minilinux ~]# resize2fs /dev/yaovg/yaolv 4000M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/yaovg/yaolv to 1024000 (4k) blocks.
The filesystem on /dev/yaovg/yaolv is now 1024000 blocks long.
[root@minilinux ~]# mount /dev/yaovg/yaolv /mnt/lvm
[root@minilinux ~]# df -h /mnt/lvm
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/yaovg-yaolv
3.9G 136M 3.6G 4% /mnt/lvm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
[root@minilinux ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdc1
VG Name yaovg
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID UB7cyX-pHjU-JO9x-a0c4-esnM-9k6B-C3ACif
--- Physical volume ---
PV Name /dev/sdc2
VG Name yaovg
PV Size 1.01 GiB / not usable 11.44 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID Jj0fcC-a5MI-l539-8h3E-SeK5-BdGg-S4ih2D
--- Physical volume ---
PV Name /dev/sdc3
VG Name yaovg
PV Size 1.01 GiB / not usable 11.44 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID iqeTJj-Lb8h-ilVk-jkg0-cC5q-exDc-5pvd0b
--- Physical volume ---
PV Name /dev/sdc5
VG Name yaovg
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID lcyKsS-zSco-ZPn0-778m-X76b-3VN2-VdL9OM
--- Physical volume ---
PV Name /dev/sdc6
VG Name yaovg
PV Size 517.69 MiB / not usable 5.69 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 32
Free PE 0
Allocated PE 32
PV UUID fPetiR-GrUd-CiVk-sWBI-F9d6-x223-HF3798
要移除/dev/sdc5,这个PV有64个PE
[root@minilinux ~]# lvresize -l -64 /dev/yaovg/yaolv
WARNING: Reducing active and open logical volume to 3.50 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce yaolv? [y/n]: y
Reducing logical volume yaolv to 3.50 GiB
Logical volume yaolv successfully resized
[root@minilinux ~]# lvdisplay
--- Logical volume ---
LV Path /dev/yaovg/yaolv
LV Name yaolv
VG Name yaovg
LV UUID 2kC7wu-tK00-bM5n-3RDf-C09Y-vYdw-8VoDIU
LV Write Access read/write
LV Creation host, time minilinux, 2013-07-09 23:20:30 +0800
LV Status available
# open 1
LV Size 3.50 GiB
Current LE 224
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
这样LV缩小了,但是要确定/dev/sdc5里面的PE完全不被使用,才能够移除/dev/sdc6。
[root@minilinux ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdc1
VG Name yaovg
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID UB7cyX-pHjU-JO9x-a0c4-esnM-9k6B-C3ACif
--- Physical volume ---
PV Name /dev/sdc2
VG Name yaovg
PV Size 1.01 GiB / not usable 11.44 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID Jj0fcC-a5MI-l539-8h3E-SeK5-BdGg-S4ih2D
--- Physical volume ---
PV Name /dev/sdc3
VG Name yaovg
PV Size 1.01 GiB / not usable 11.44 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 64
Free PE 0
Allocated PE 64
PV UUID iqeTJj-Lb8h-ilVk-jkg0-cC5q-exDc-5pvd0b
--- Physical volume ---
PV Name /dev/sdc5
VG Name yaovg
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 64
Free PE 32
Allocated PE 32
PV UUID lcyKsS-zSco-ZPn0-778m-X76b-3VN2-VdL9OM
--- Physical volume ---
PV Name /dev/sdc6
VG Name yaovg
PV Size 517.69 MiB / not usable 5.69 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 32
Free PE 32
Allocated PE 0
PV UUID fPetiR-GrUd-CiVk-sWBI-F9d6-x223-HF3798
发现未使用的PE在/dev/sdc6上
[root@minilinux ~]# pvmove /dev/sdc5 /dev/sdc6
/dev/sdc5: Moved: 3.1%
/dev/sdc5: Moved: 100.0%
移除/dev/sdc5
[root@minilinux ~]# vgreduce yaovg /dev/sdc5
Removed "/dev/sdc5" from volume group "yaovg"
[root@minilinux ~]# pvscan
PV /dev/sdc1 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc2 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc3 VG yaovg lvm2 [1.00 GiB / 0 free]
PV /dev/sdc6 VG yaovg lvm2 [512.00 MiB / 0 free]
PV /dev/sdc5 lvm2 [1.01 GiB]
Total: 5 [4.51 GiB] / in use: 4 [3.50 GiB] / in no VG: 1 [1.01 GiB]
[root@minilinux ~]# pvremove /dev/sdc5
Labels on physical volume "/dev/sdc5" successfully wiped
这样你的文件系统 实际的LV VG都变小了,那个/dev/sdc5也可以进行其他的用途。
(责任编辑:IT)
要使用LVM,需要先了解几个参数的意义。
PV:Physical Volume 实体滚动条
VG:Volume Group 滚动条群组 LVM大磁盘是许多PV整合成VG,VG最多能包含65534个PE。
PE:Physical Extend 实体延伸区 LVM默认使用4MB的PE区块,PE是整个LVM最小的储存区块,简单说PE有点像文件系统里面的block。
LV:Logical Volume 逻辑滚动条 VG会被切成LV,LV的大小与LV内的PE有关,LVM的文件名通常指定为/dev/vgname/lvname的样式
PE LV VG之间的关系图:
通过PV VG LV的规划后,利用mkfs可以将你的LV格式化为可以利用的文件系统。 LV写入硬盘的方式: 线性模式(liner): 举例来说 /dev/sda1 /dev/sda2加入到VG当中,并且整个VG只有一个LV时,线性模式就是:当/dev/sda1容量用完后,/dev/sda2才会被使用。(建议的模式)
交错模式(triped):有点像RAID 0,理论上读写性能会比较好。
LVM默认的读写模式是线性模式,如果你使用triped模式,当一个partition损坏时,所有数据都会损毁。
LVM需要核心支持且需安装lvm2这个软件。
我要实现的环境: 分割4个partition,每个partition的容量为1G,且system ID为8e; 全部的partition整合为一个VG,VG名称为yaovg,且PE大小为16M; 全部的VG容量都给LV,LV的名称为yaolv; 这个LV格式化为ext4的文件系统,挂载在/mnt/lvm中
本人都是用vmware虚拟机做的,添加一块硬盘/dev/sdb 对这块硬盘/dev/sdb分区
PV阶段: 建立PV很简单,直接使用pvcreate即可! pvcreate:将实体partition建立成为PV pvscan:查找目前系统里任何具有PV的磁盘 pvdisplay:显示目前系统上面的PV状态 pvremove:将PV属性移除,让该partition不具有PV属性
将/dev/sdb5-/dev/sdb8建立成为PV格式
没挂载上去,就是创建不了。查找了很多资料,还是没解决。 所以决定再添加一块硬盘/dev/sdc试试。
分区完毕!
创建成功了
更详细显示系统上每个PV信息
这样PV就建立好了
VG阶段: vgcreate:主要建立VG的指令 vgscan:查找系统上面是否有VG存在? vgdisplay:显示目前系统上面的VG状态 vgextend:在VG内增加额外的PV vgreduce:在VG内移除PV vgchange:设定VG是否启动 vgremove:删除一个VG
VG的名称是自定义的
将/dev/sdc1-/dev/sdc3建立成为一个VG,指定PE为16M
发现存在yaovg的VG
三个PV被用了,剩下一个/dev/sdc5的PV还没用
一个VG就建立好了!
将剩余的PV/dev/sdc5)也给yaovg
LV阶段: 创建完VG后,就是要建立分区了,分区就是所谓的LV喽,要将那个yaovg分割为yaolv。这个VG的容量都分配到yaolv里面去。 lvcreate:建立LV lvscan:查看系统上面的LV lvdisplay:显示系统上面的LV状态 lvextend:在LV里面增加容量 lvreduce:在LV里面减少容量 lvremove:删除一个LV lvresize:对LV进行容量大小的调整
LV的名称必须使用全名/dev/yaovg/yaolv
文件系统阶段 格式化 挂载 查看我的LV
LV的名称为/dev/yaovg/yaolv是为了让使用者找到我们所需要的数据,实际上LVM使用的装置是放在/dev/mapper/目录下。现在已经建立好一个LV,你可以使用/mnt/lvm里面的东东了!
增大LV容量
只要这样做就好
1 用fdisk设定新的具有8e system ID的partition 3 vgextend将PV加入yaovg 4 lvresize将新加入的PV内的PE加入yaolv中 5 通过resize2fs将文件系统的容量确实增加
新增一个512M的partition /dev/sdc6
建立新的PV
加上VG
放大LV,用lvresize增加
文件系统没有增加,LVM可以在线直接处理,不需要umount.文件系统的容量,用resize2fs来处理
减少LV容量
(责任编辑:IT) |