烹茶细论

LVM卷拓容

一、为空余分区创建新分区
fdisk /dev/sda 按p打印当前分区情况,然后按n添加一个新分区;
输入p创建主分区,输入分区序号后两次回车将空间全部分给这个分区;
输入t转换分区类型;输入8e表示转换为linux lvm分区;
p打印当前分区查看是否创建分区;
输入w写入保存退出。
提示设备或资源忙并且在/dev中找不到sda3刚创建的分区则需要重启,重启后可见。(有更好的方式请留言)

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
[root@localhost ~]# fdisk /dev/sda
Command (m for help):
Disk /dev/sda: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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: 0x00062d65
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 13055 104344576 8e Linux LVM
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (13055-39162, default 13055):
Using default value 13055
Last cylinder, +cylinders or +size{K,M,G} (13055-39162, default 39162):
Using default value 39162
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sda: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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: 0x00062d65
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 13055 104344576 8e Linux LVM
/dev/sda3 13055 39162 209711165 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: 设备或资源忙.
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.

创建新的分区如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# fdisk -l
Disk /dev/sda: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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: 0x00062d65
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 13055 104344576 8e Linux LVM
/dev/sda3 13055 39162 209711165 8e Linux LVM

二、创建PV

1
2
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created

三、将创建的PV加入到已有的逻辑卷组中
加入之前先查找卷组的名称,即VG Name的名字,我们要记住的是下面的vg_localhost,下面需要用到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg_localhost
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 99.51 GiB
PE Size 4.00 MiB
Total PE 25474
Alloc PE / Size 25474 / 99.51 GiB
Free PE / Size 0 / 0
VG UUID IdEmXl-yUwI-mFd3-i4wx-gUSL-qNfU-uLu1wk

将PV加入到已有的逻辑卷组中

1
2
[root@localhost ~]# vgextend vg_localhost /dev/sda3
Volume group "vg_localhost" successfully extended

查看是否添加进去,看 Free PE / Size 这行和之前的差异。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg_localhost
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 299.50 GiB
PE Size 4.00 MiB
Total PE 76672
Alloc PE / Size 25474 / 99.51 GiB
Free PE / Size 51198 / 199.99 GiB
VG UUID IdEmXl-yUwI-mFd3-i4wx-gUSL-qNfU-uLu1wk

四、将空闲的卷加入现有卷中
+51198即是上面Free PE / Size属性中的空闲值 vg_localhost-lv_home是data所在卷的名字

1
2
3
[root@localhost ~]# lvresize -l +51198 /dev/mapper/vg_localhost-lv_home
Extending logical volume lv_home to 248.67 GiB
Logical volume lv_home successfully resized

五、重设大小,激活分区

1
2
3
4
5
6
7
8
[root@localhost ~]# resize2fs /dev/mapper/vg_localhost-lv_home
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_localhost-lv_home is mounted on /data; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 16
Performing an on-line resize of /dev/mapper/vg_localhost-lv_home to 65187840 (4k) blocks.
The filesystem on /dev/mapper/vg_localhost-lv_home is now 65187840 blocks long.
[root@localhost ~]# df -THa

df -THa就可以看到/data分区大小改变了

总结步骤

  • fdisk /dev/sda (以sda为例,创建新分区sda3,)
  • pvcreate /dev/sda3 (创建PV)
  • vgextend vg_localhost /dev/sda3 (PV加如卷组)
  • lvresize -l +51198 /dev/mapper/vg_localhost-lv_home (将卷组中空余的空间加入需要拓容的卷)
  • resize2fs /dev/mapper/vg_localhost-lv_home (初始化)