Hyper-V 环境下给 CentOS 磁盘扩容

使用Hyper-V安装CentOS虚拟机,使用起来非常方便。但遇到磁盘空间不够时,不像Windows虚拟机那样方便给磁盘空间扩容。 总体两分二步:

1. vhdx扩容

关闭虚拟机,然后给磁盘扩容,操作方便网上教程多,这里不再赘述。

2. CentOS扩容

2.1 查看分区表

1$ lsblk
2NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
3sda               8:0    0   20G  0 disk 
4├─sda1            8:1    0  200M  0 part /boot/efi
5├─sda2            8:2    0    1G  0 part /boot
6└─sda3            8:3    0  8.8G  0 part 
7  ├─centos-root 253:0    0  7.8G  0 lvm  /
8  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]

以上内容可以看出,sda已经扩容到20G,但sda1+sda2+sda3只有10G说明vhdx扩容成功。

2.2 建立新分区

 1$ fdisk /dev/sda
 2
 3The device presents a logical sector size that is smaller than
 4the physical sector size. Aligning to a physical sector (or optimal
 5I/O) size boundary is recommended, or performance may be impacted.
 6欢迎使用 fdisk (util-linux 2.23.2) 7
 8更改将停留在内存中,直到您决定将更改写入磁盘。
 9使用写入命令前请三思。
10
11
12命令(输入 m 获取帮助):n
13Partition type:
14   p   primary (1 primary, 0 extended, 3 free)
15   e   extended
16Select (default p): 
17Using default response p
18分区号 (2-4,默认 2)19起始 扇区 (20971520-41943039,默认为 20971520)20将使用默认值 20971520
21Last 扇区, +扇区 or +size{K,M,G} (20971520-41943039,默认为 41943039):+10G
22值超出范围。
23Last 扇区, +扇区 or +size{K,M,G} (20971520-41943039,默认为 41943039):+9.9G
24不支持的后缀:“.9G”。
25支持:10^N: KB (千字节), MB (兆字节), GB (吉字节)
26            2^N: K  (约千字节), M  (约兆字节), G  (约吉字节)
27Last 扇区, +扇区 or +size{K,M,G} (20971520-41943039,默认为 41943039):+9G 
28分区 2 已设置为 Linux 类型,大小设为 9 GiB
29
30命令(输入 m 获取帮助):w
31The partition table has been altered!
32
33Calling ioctl() to re-read partition table.
34
35WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
36The kernel still uses the old table. The new table will be used at
37the next reboot or after you run partprobe(8) or kpartx(8)
38正在同步磁盘。

重读分区表时,发生错误

1$ partprobe
2Error: 备份 GPT 表不像应该的那样出现在磁盘的末尾。这可能意味这其它操作系统相信磁盘小一些。通过将备份移动到末尾 (并删除旧备份) 来修正?
3Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current setting? 

查看分区表时,发现新分区未创建成功

1$ lsblk
2NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
3sda               8:0    0   20G  0 disk 
4├─sda1            8:1    0  200M  0 part /boot/efi
5├─sda2            8:2    0    1G  0 part /boot
6└─sda3            8:3    0  8.8G  0 part 
7  ├─centos-root 253:0    0  7.8G  0 lvm  /
8  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
9

使用 parted 修复分区表

 1$ parted /dev/sda
 2GNU Parted 3.1
 3使用 /dev/sda
 4Welcome to GNU Parted! Type 'help' to view a list of commands.
 5(parted) p                                                                
 6错误: 备份 GPT 表不像应该的那样出现在磁盘的末尾。这可能意味这其它操作系统相信磁盘小一些。通过将备份移动到末尾 (并删除旧备份) 来修正?
 7修正/Fix/忽略/Ignore/放弃/Cancel? fix                                     
 8警告: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current
 9setting? 
10修正/Fix/忽略/Ignore? fix                                                 
11Model: Msft Virtual Disk (scsi)
12Disk /dev/sda: 21.5GB
13Sector size (logical/physical): 512B/4096B
14Partition Table: gpt
15Disk Flags: 
16
17Number  Start   End     Size    File system  Name                  标志
18 1      1049kB  211MB   210MB   fat16        EFI System Partition  启动
19 2      211MB   1285MB  1074MB  xfs
20 3      1285MB  10.7GB  9452MB                                     lvm
21
22(parted) p                                                                
23Model: Msft Virtual Disk (scsi)
24Disk /dev/sda: 21.5GB
25Sector size (logical/physical): 512B/4096B
26Partition Table: gpt
27Disk Flags: 
28
29Number  Start   End     Size    File system  Name                  标志
30 1      1049kB  211MB   210MB   fat16        EFI System Partition  启动
31 2      211MB   1285MB  1074MB  xfs
32 3      1285MB  10.7GB  9452MB                                     lvm
33
34(parted) quit                                                             

再次建立新分区

 1$ fdisk /dev/sda
 2WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
 3欢迎使用 fdisk (util-linux 2.23.2) 4
 5更改将停留在内存中,直到您决定将更改写入磁盘。
 6使用写入命令前请三思。
 7
 8
 9命令(输入 m 获取帮助):n
10分区号 (4-128,默认 4)11第一个扇区 (34-41943006,默认 20969472)12Last sector, +sectors or +size{K,M,G,T,P} (20969472-41943006,默认 41943006):+10G
13已创建分区 4
14
15
16命令(输入 m 获取帮助):w
17The partition table has been altered!
18
19Calling ioctl() to re-read partition table.
20
21WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
22The kernel still uses the old table. The new table will be used at
23the next reboot or after you run partprobe(8) or kpartx(8)
24正在同步磁盘。

2.3 重读分区表

1$ partprobe

重读分区表,正常不会有任何输出

再次查看分区表,可以看到 sda4 分区已经创建好了

1$ lsblk
2NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
3sda               8:0    0   20G  0 disk 
4├─sda1            8:1    0  200M  0 part /boot/efi
5├─sda2            8:2    0    1G  0 part /boot
6├─sda3            8:3    0  8.8G  0 part 
7│ ├─centos-root 253:0    0  7.8G  0 lvm  /
8│ └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
9└─sda4            8:4    0   10G  0 part 

2.4 将 sda4 分区加入 centos-root 分区

 1$ lvm
 2lvm> pvcreate /dev/sda4
 3  Physical volume "/dev/sda4" successfully created.
 4lvm> pvdisplay
 5  --- Physical volume ---
 6  PV Name               /dev/sda3
 7  VG Name               centos
 8  PV Size               8.80 GiB / not usable 2.00 MiB
 9  Allocatable           yes (but full)
10  PE Size               4.00 MiB
11  Total PE              2253
12  Free PE               0
13  Allocated PE          2253
14  PV UUID               D5K7Ds-a0I8-67Kt-LE1J-HKi7-aaKX-CLaJTg
15   
16  "/dev/sda4" is a new physical volume of "10.00 GiB"
17  --- NEW Physical volume ---
18  PV Name               /dev/sda4
19  VG Name               
20  PV Size               10.00 GiB
21  Allocatable           NO
22  PE Size               0   
23  Total PE              0
24  Free PE               0
25  Allocated PE          0
26  PV UUID               digjnH-8XFC-kMQT-nOEL-y9FJ-ImaB-t8FB7d
27   
28lvm> vgdisplay
29  --- Volume group ---
30  VG Name               centos
31  System ID             
32  Format                lvm2
33  Metadata Areas        1
34  Metadata Sequence No  3
35  VG Access             read/write
36  VG Status             resizable
37  MAX LV                0
38  Cur LV                2
39  Open LV               2
40  Max PV                0
41  Cur PV                1
42  Act PV                1
43  VG Size               8.80 GiB
44  PE Size               4.00 MiB
45  Total PE              2253
46  Alloc PE / Size       2253 / 8.80 GiB
47  Free  PE / Size       0 / 0   
48  VG UUID               f77tkP-7xzc-qcyf-t0JN-OYOQ-ZfLL-PJPD8w
49   
50lvm> vgextend centos /dev/sda4
51  Volume group "centos" successfully extended
52lvm> vgdisplay
53  --- Volume group ---
54  VG Name               centos
55  System ID             
56  Format                lvm2
57  Metadata Areas        2
58  Metadata Sequence No  4
59  VG Access             read/write
60  VG Status             resizable
61  MAX LV                0
62  Cur LV                2
63  Open LV               2
64  Max PV                0
65  Cur PV                2
66  Act PV                2
67  VG Size               <18.80 GiB
68  PE Size               4.00 MiB
69  Total PE              4812
70  Alloc PE / Size       2253 / 8.80 GiB
71  Free  PE / Size       2559 / <10.00 GiB
72  VG UUID               f77tkP-7xzc-qcyf-t0JN-OYOQ-ZfLL-PJPD8w
73   
74lvm> lvextend -l +100%FREE /dev/centos/root
75  Size of logical volume centos/root changed from 7.80 GiB (1997 extents) to <17.80 GiB (4556 extents).
76  Logical volume centos/root successfully resized.
77lvm> exit
78  Exiting.
79
80$ xfs_growfs /dev/centos/root
81meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=511232 blks
82         =                       sectsz=4096  attr=2, projid32bit=1
83         =                       crc=1        finobt=0 spinodes=0
84data     =                       bsize=4096   blocks=2044928, imaxpct=25
85         =                       sunit=0      swidth=0 blks
86naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
87log      =internal               bsize=4096   blocks=2560, version=2
88         =                       sectsz=4096  sunit=1 blks, lazy-count=1
89realtime =none                   extsz=4096   blocks=0, rtextents=0
90data blocks changed from 2044928 to 4665344

再次查看磁盘容量与分区表,centos-root分区空间已经扩容为17.8G

 1$ df -h
 2文件系统                 容量  已用  可用 已用% 挂载点
 3devtmpfs                 876M     0  876M    0% /dev
 4tmpfs                    887M     0  887M    0% /dev/shm
 5tmpfs                    887M  8.4M  879M    1% /run
 6tmpfs                    887M     0  887M    0% /sys/fs/cgroup
 7/dev/mapper/centos-root   18G  7.3G   11G   41% /
 8/dev/sda2               1014M  215M  800M   22% /boot
 9/dev/sda1                200M   12M  189M    6% /boot/efi
10tmpfs                    178M     0  178M    0% /run/user/0
11
12$ lsblk
13NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
14sda               8:0    0   20G  0 disk 
15├─sda1            8:1    0  200M  0 part /boot/efi
16├─sda2            8:2    0    1G  0 part /boot
17├─sda3            8:3    0  8.8G  0 part 
18│ ├─centos-root 253:0    0 17.8G  0 lvm  /
19│ └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
20└─sda4            8:4    0   10G  0 part 
21  └─centos-root 253:0    0 17.8G  0 lvm  /

参考文档

相关专栏文章