咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

Ubuntu Server 扩展磁盘空间LVM

问题

在《VMWare为Ubuntu添加硬盘》一文中,我尝试为虚拟机增加一块磁盘挂载了 /data 路径下,这可以解决在“出现磁盘耗尽预警”之前增加磁盘,尽量将新的文件都放到 /data 路径下。

如果应用的数据无法迁移,需扩展根分区 / 的空间, 上面的方法则无法满足我的需求。

GParted

对于VMWare虚拟机,还可以考虑对虚拟机磁盘进行扩容,如:

这种方式对桌面版系统环境(单磁盘)可能会比较合适,扩展后利用 GParted软件 重新调整 /dev/sdb1 即可,有兴趣可以参考
ubuntu下对根目录磁盘扩容】

注意:GParted是一个GUI软件可以在桌面系统中安装启动 或者 下载iso文件挂载在光驱启动

但我没有尝试成功,为什么?
因为我们使用的是 ubuntu server 14.04 ,服务器版的默认都是LVM磁盘阵列,我没有专门学过LVM,但知道他比单硬盘复杂,他可以将多个硬盘虚拟化为一个硬盘。所以GParted处理起来没那么简单,所以我尝试一次就放弃了GParted,因为LVM本身就是能满足扩容需求的。

开始LVM扩容

思路

给VM增加一块硬盘(和物理机场景一样),加入到LVM中。

场景

一台老虚拟机服务器磁盘空间即将耗尽,导致现在无法对Gitlab进行备份操作,现在打算对系统根分区进行空间扩展,增加100G空间。

1
2
3
4
5
6
7
8
9
10
11
12
Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-31-generic x86_64)

root@bogon:~# df -lh
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 4.0K 2.9G 1% /dev
tmpfs 596M 3.2M 592M 1% /run
/dev/dm-0 19G 16G 1.9G 90% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 3.0G 4.0K 3.0G 1% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 41M 183M 19% /boot

1. 添加一块新硬盘

在“虚拟机设置” - “添加” - “硬盘” 给VM虚拟机添加一块100G的硬盘,重启虚拟机。

2. 查看所有硬盘信息

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
root@bogon:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b8cbd

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM

# 这里已经识别出新硬盘100G
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/mapper/bogon--vg-root: 20.1 GB, 20124270592 bytes
255 heads, 63 sectors/track, 2446 cylinders, total 39305216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/bogon--vg-root doesn't contain a valid partition table

Disk /dev/mapper/bogon--vg-swap_1: 1069 MB, 1069547520 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2088960 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/bogon--vg-swap_1 doesn't contain a valid partition table

3. 新增主分区

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
root@bogon:~# fdisk /dev/sdb 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x6f04a426.
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)

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199

# w保存更改
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@bogon:~#
# 重启读取分区表
root@bogon:~# partprobe /dev/sdb
root@bogon:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b8cbd

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 41940991 20719617 5 Extended
/dev/sda5 501760 41940991 20719616 8e Linux LVM

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
43 heads, 44 sectors/track, 110843 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f04a426

Device Boot Start End Blocks Id System
/dev/sdb1 2048 209715199 104856576 83 Linux

Disk /dev/mapper/bogon--vg-root: 20.1 GB, 20124270592 bytes
255 heads, 63 sectors/track, 2446 cylinders, total 39305216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/bogon--vg-root doesn’t contain a valid partition table

Disk /dev/mapper/bogon--vg-swap_1: 1069 MB, 1069547520 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2088960 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/bogon--vg-swap_1 doesn’t contain a valid partition table

4. 创建新的物理卷

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
root@bogon:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

# 显示目前存在的物理卷信息
root@bogon:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda5
VG Name bogon-vg
PV Size 19.76 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5058
Free PE 5
Allocated PE 5053
PV UUID HSucTv-jtIU-itcZ-VmXp-ygRg-Xe02-uC0I8u

"/dev/sdb1" is a new physical volume of "100.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID BEN0cc-twRr-d419-GdCC-BJ5a-3I3x-OwxNHH


#显示存在的卷组
root@bogon:~# vgdisplay
--- Volume group ---
VG Name bogon-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.76 GiB
PE Size 4.00 MiB
Total PE 5058
Alloc PE / Size 5053 / 19.74 GiB
Free PE / Size 5 / 20.00 MiB
VG UUID eW4kvx-NYtj-Udfe-NZCZ-ITzN-Ibbz-31na6C

得到以下关键信息:

  • 卷组 VG Name: bogon-vg
  • 卷组大小 VG Size: 19.76 GiB
  • 可分配的空间 Free PE / Size: 5 / 20.00 MiB

可分配的空间仅20MiB,所以我们要添加一个新的物理卷到卷组 bogon-vg

5. 扩展卷组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 把物理卷/dev/sdb1 扩展添加到卷组 bogon-vg 中
root@bogon:~# vgextend bogon-vg /dev/sdb1
Volume group "bogon-vg" successfully extended
root@bogon:~# vgdisplay
--- Volume group ---
VG Name bogon-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 119.75 GiB
PE Size 4.00 MiB
Total PE 30657
Alloc PE / Size 5053 / 19.74 GiB
Free PE / Size 25604 / 100.02 GiB #增加100G可用空间
VG UUID eW4kvx-NYtj-Udfe-NZCZ-ITzN-Ibbz-31na6C

6. 扩展逻辑卷

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
# 查看逻辑卷
root@bogon:~# lvdisplay
--- Logical volume ---
LV Path /dev/bogon-vg/root
LV Name root
VG Name bogon-vg
LV UUID dPvAEs-EUUu-nxlD-EU21-pHP1-qL5q-LTncQ6
LV Write Access read/write
LV Creation host, time bogon, 2017-04-18 15:13:48 +0800
LV Status available
# open 1
LV Size 18.74 GiB
Current LE 4798
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/bogon-vg/swap_1
LV Name swap_1
VG Name bogon-vg
LV UUID 0BSenv-UESB-hH9X-zRUv-xZsD-pjdk-kNMx4N
LV Write Access read/write
LV Creation host, time bogon, 2017-04-18 15:13:48 +0800
LV Status available
# open 2
LV Size 1020.00 MiB
Current LE 255
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
# 扩展逻辑卷为120G(注意:这里是最终大小,而不是增加的大小)
root@bogon:~# lvextend -L 120G /dev/bogon-vg/root
Extending logical volume root to 120.00 GiB
#空间不太够: 25922 - 25604 = 318
Insufficient free space: 25922 extents needed, but only 25604 available
root@bogon:~#
# 试一下扩展逻辑卷为119G
root@bogon:~# lvextend -L 119G /dev/bogon-vg/root
Extending logical volume root to 119.00 GiB
#空间不太够: 25666 - 25604 = 62,应该指定118G就可以了
Insufficient free space: 25666 extents needed, but only 25604 available
# 还有一个方法,直接分配所有可用空间
root@bogon:~# lvextend -l +100%FREE /dev/bogon-vg/root
Extending logical volume root to 118.76 GiB
Logical volume root successfully resized # 逻辑卷大小已经扩展成功

7. 重新计算逻辑卷

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
root@bogon:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 4.0K 2.9G 1% /dev
tmpfs 596M 3.2M 592M 1% /run
/dev/dm-0 19G 16G 1.9G 90% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 3.0G 4.0K 3.0G 1% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 41M 183M 19% /boot
# 文件系统还没有识别逻辑卷
root@bogon:~#
root@bogon:~#
root@bogon:~#
# 重新计算逻辑卷
root@bogon:~# resize2fs /dev/bogon-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/bogon-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 8
The filesystem on /dev/bogon-vg/root is now 31131648 blocks long.
# 已经可以识别逻辑卷
root@bogon:~# df -lh
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 4.0K 2.9G 1% /dev
tmpfs 596M 3.2M 592M 1% /run
/dev/dm-0 117G 16G 97G 14% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 3.0G 4.0K 3.0G 1% /run/shm
none 100M 0 100M 0% /run/user
/dev/sda1 236M 41M 183M 19% /boot
root@bogon:~#

扩容成功!

参考【cnblogs.com】