zhangguanzhang's Blog

虚拟化扩容硬盘大小

字数统计: 2k阅读时长: 9 min
2017/04/20

写于 2021/08/27 ,为了避免排到前面,所以日期修了下。

about

讲解下扩分区的步骤。扩容只有两种情况,lvm(改 pv 所在分区的硬盘大小) 和非 lvm 纯分区的。无论是哪种,都得先扩容挂载路径所在的分区。推荐使用 parted 命令扩,且推荐使用最新版的 parted。新增硬盘扩到 lvm 里就不讲了,简单得不行。要先确保现场安装好新版本的 parted ,老版本的 parted 没有 resizepart 的命令,有条件的话把 gdisk 也可以安装上。

早期的 Linux 为了兼容 windows 的磁盘,只支持 MBR ,后面支持了 GPT。传统的老牌的分区工具是 fdisk,但是 fdisk 出道太早,只支持 MBR(Master Boot Record),并不支持 GPT(GUID Partition Table),无法操作超过 2T 的磁盘,因此 gdiskparted 等分区工具横空出世。两种类型分区表的信息存放不一样。所以我们扩容的时候得区分。

扩容硬盘的话,我们应该在扩硬盘大小之前先去看下该硬盘的分区表类型可以直接使用下面命令获取 Partition Table 的行确认:

1
$ parted /dev/xxx p |& grep Table

一般是 msdos 或者 gpt

实战

先虚拟化上修改要扩容的硬盘,增加大小,没识别到容量更新的话按照下面操作就不需要重启机器了。

rescue 模式

如果扩容的分区有很多业务进程读写,例如我们的系统分区。那就需要关机,进入 rescue 模式里去扩容了:

  • cdrom 给机器挂载了一个 centos7.x的 minimal 的 ISO(最好 7.x 的最新版,比如 7.9),
  • 设置机器从 cdrom 启动,
  • 选择 Troubleshooting –> Rescue a CentOS Linux system
  • 进入菜单后选择 3) Skip to shell

然后就按照后面的扩容操作了

触发硬盘重新扫描

假如硬盘容量是 2g 挂载在 /data 目录。下面所示,/dev/vda1 挂载在 /data 目录。纯分区扩容的话只能硬盘容量扩大,也即是虚拟化上修改,vmwarekvm 都能修改。

1
2
3
4
5
6
7
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
sr0 11:0 1 4M 0 rom
vda 253:0 0 2G 0 disk
└─vda1 253:1 0 2G 0 part /data

如果修改后 lsblk 命令没识别,尝试下面的触发重新扫描:

1
2
3
4
5
6
7
8
9
# 查找硬盘属于的host,下面是 host2
$ ls -l /sys/block/vda
lrwxrwxrwx 1 root root 0 Aug 27 04:13 /sys/block/vda -> ../devices/pci0000:00/0000:00:05.0/virtio1/host2/target2:0:0/2:0:0:1/block/vda

# 触发扫描 host2
echo "- - -" > /sys/class/scsi_host/host2/scan

# 上面不行就下面这个
echo 1 > /sys/class/block/vda/device/rescan

查看容量增加了

1
2
3
4
5
6
7
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
vda 8:16 0 7G 0 disk
└─vda1 8:17 0 2G 0 part /data
sr0 11:0 1 4M 0 rom

非 lvm,纯分区

msdos (MBR)

MBR 的分区表信息都是存放在头部的 512(446+64+2)64 字节里。所以硬盘大小增加了,在 parted 操作硬盘的时候不会有啥警告信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
sr0 11:0 1 4M 0 rom
vda 253:0 0 2G 0 disk
└─vda1 253:1 0 2G 0 part /data
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
sr0 11:0 1 4M 0 rom
vda 253:0 0 7G 0 disk
└─vda1 253:1 0 2G 0 part /data

先 umount 掉扩容分区所在的路径:

1
umount /data

使用 partedresizepart 扩容最后一个分区,下面信息可以看到,这个环境上最后一个分区的数字是 1,所以 resizepart 1

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
$ parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 7516MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 2147MB 2146MB primary xfs

(parted) resizepart 1 # <--- 最后一个分区的数字
End? [2147MB]? 100% # <--- 直接输入 100% 使用剩下所有分区
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 7516MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 7516MB 7515MB primary xfs

(parted) q
Information: You may need to update /etc/fstab.

手动挂载下,查看容量上去了。

1
2
3
4
5
6
7
8
$ mount /dev/vda1 /data
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
sr0 11:0 1 4M 0 rom
vda 253:0 0 7G 0 disk
└─vda1 253:1 0 7G 0 part /data

可以 df -h |& grep /data 确认下,如果文件系统容量没上去就相应的命令扩容下,比如 xfs 使用下面扩容

1
xfs_growfs /dev/vda1

GPT

GPT 的分区表会在硬盘的头和尾巴都保存一份数据,所以扩大硬盘后,使用 parted 硬盘后 p 打印信息的时候在尾部找不到备份的数据,会告警,需要修复。先 umount 掉路径:

1
umount /data/

上面可以看到硬盘大小从 2g 增加到 7g , 我们先 parted 扩容最后一个分区,先打印下信息:

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
$ parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the space (an extra 10485760 blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 7516MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 2146MB 2145MB xfs 1

(parted) resizepart 1 # <--- 上面最后一个分区的数字
End? [2146MB]? 100% # <--- 直接输入 100% 使用剩下所有分区
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 7516MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 7516MB 7515MB xfs 1

(parted) q

q 退出来后,能看到扩容成功,挂载后看到文件系统没扩容,所以得扩容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ lsblk                                                   
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
sr0 11:0 1 1G 0 rom
sr1 11:1 1 4M 0 rom
vda 253:0 0 7G 0 disk
└─vda1 253:1 0 7G 0 part
$ mount /dev/vda1 /data
$ df -h |& grep /data
/dev/vda1 2.0G 33M 2.0G 2% /data
$ xfs_growfs /dev/vda1
$ df -h |& grep /data # 扩容完成
/dev/vda1 7.0G 33M 7.0G 1% /data

lvm

就不啰嗦了,直接命令,vda已经从之前的 40G 增加了 20G

1
2
3
4
5
6
7
8
9
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 464K 0 rom
sr1 11:1 1 1024M 0 rom
vda 252:0 0 60G 0 disk
├─vda1 252:1 0 2M 0 part
├─vda2 252:2 0 200M 0 part /boot
└─vda3 252:3 0 39.8G 0 part
└─centos7-root 253:0 0 39.8G 0 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
$ parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 3146kB 2097kB primary
2 3146kB 213MB 210MB primary xfs boot
3 213MB 42.9GB 42.7GB primary lvm
(parted) resizepart 3
End? [42.9GB]? 100%
(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 3146kB 2097kB primary
2 3146kB 213MB 210MB primary xfs boot
3 213MB 64.0GB 63.8GB primary lvm

(parted) quit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ pvdisplay                                               
--- Physical volume ---
PV Name /dev/vda3
VG Name centos7
PV Size 39.80 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10189
Free PE 0
Allocated PE 10189
PV UUID VVXMV7-zVhi-NSVz-frrY-YKFg-BoJH-ir33p3
$ pvresize /dev/vda3
Physical volume "/dev/vda3" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

扩容根分区所在的 lv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ lvdisplay 
--- Logical volume ---
LV Path /dev/centos7/root
LV Name root
VG Name centos7
LV UUID npjBgn-XPHe-dNS2-bJGd-kYp2-RayO-dWttAc
LV Write Access read/write
LV Creation host, time localhost, 2018-06-30 14:35:21 +0800
LV Status available
# open 1
LV Size 39.80 GiB
Current LE 10189
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
$ lvextend -l +100%FREE /dev/centos7/root
Size of logical volume centos7/root changed from 39.80 GiB (10189 extents) to 59.40 GiB (15207 extents).
Logical volume centos7/root successfully resized.
$ xfs_growfs /dev/centos7/root
CATALOG
  1. 1. about
  2. 2. 实战
    1. 2.1. rescue 模式
    2. 2.2. 触发硬盘重新扫描
    3. 2.3. 非 lvm,纯分区
      1. 2.3.1. msdos (MBR)
      2. 2.3.2. GPT
    4. 2.4. lvm