ESXI linux虚拟机磁盘扩容

ESXI linux virtual machain disk expansion

Posted by alovn on May 18, 2023

ESXI(VMware)里linux系统的磁盘空间有点不够用了,所以要扩容一下。它不像Windows系统那样简单的点几下鼠标就完成扩容了。这里简单记录一下操作过程。

首先关闭linux虚拟机,然后进入ESXI, 编辑虚拟机, 直接修改硬盘大小, 这里原先是50GB,直接设置到100GB。然后启动linux虚拟机后进入系统,

首先查看挂载点和文件系统类型,

1
2
3
4
5
6
7
8
alovn@ubuntu-desktop:~$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  1.6G  1.7M  1.6G   1% /run
/dev/sda3      ext4    98G   15G   80G  16% /
tmpfs          tmpfs  7.9G   36M  7.8G   1% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2      vfat   512M  6.1M  506M   2% /boot/efi
tmpfs          tmpfs  1.6G  2.4M  1.6G   1% /run/user/1000

比如这里要对根分区/进行扩容, 可以看到磁盘文件分区是 /dev/sda3,文件系统类型是ext4。

然后查看磁盘分区情况

1
2
3
4
5
6
7
8
9
10
11
12
13
alovn@ubuntu-desktop:~$ sudo fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D08810AD-21EF-431A-9B8E-481245778CB1

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   1054719   1050624  513M EFI System
/dev/sda3  1054720 105906176 104851456 50 Linux filesystem

可以看到磁盘 /dev/sda 共有100GB, /dev/sda3 使用了50GB, 还有大约50GB。

使用 fdisk 修改分区大小:

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
alovn@ubuntu-desktop:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): m # 键入m, 查看命令帮助

Help:

GPT
M   enter protective/hybrid MBR

Generic
d   delete a partition
F   list free unpartitioned space
l   list known partition types
n   add a new partition
p   print the partition table
t   change a partition type
v   verify the partition table
i   print information about a partition

Misc
m   print this menu
x   extra functionality (experts only)

Script
I   load disk layout from sfdisk script file
O   dump disk layout to sfdisk script file

Save & Exit
w   write table to disk and exit
q   quit without saving changes

Create a new label
g   create a new empty GPT partition table
G   create a new empty SGI (IRIX) partition table
o   create a new empty DOS partition table
s   create a new empty Sun partition table

Command (m for help): d # 首先删除原来到分区
Partition number (1-3, default 3): 3 # 键入分区编号,这里是3

Partition 3 has been deleted.

Command (m for help): n # 新建分区
Partition number (3-128, default 3): 3 # 新建的分区编号,3
First sector (1054720-209715166, default 1054720): # 扇区开始位置,默认即可, 回车确认
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1054720-209715166, default 209715166): # 扇区结束位置,默认全部分配,回车确认

Created a new partition 3 of type 'Linux filesystem' and of size 100 GiB.
Partition #3 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: y

The signature will be removed by a write command.

Command (m for help): p # 打印新的分区信息, 可以看到现在是100GB
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D08810AD-21EF-431A-9B8E-481245778CB1

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   1054719   1050624  513M EFI System
/dev/sda3  1054720 209715166 208660447 100G Linux filesystem

Filesystem/RAID signature on partition 3 will be wiped.

Command (m for help): w # 保存分区信息
The partition table has been altered.
Syncing disks.

保存分区信息后,需要重启系统,执行 reboot

重启之后需要执行 resize2fs 更新文件系统的大小:

1
2
3
4
5
alovn@ubuntu-desktop:$ sudo resize2fs /dev/sda3
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 13
The filesystem on /dev/sda3 is now 26082555 (4k) blocks long.

再次查看分区信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
alovn@ubuntu-desktop:~$ sudo fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D08810AD-21EF-431A-9B8E-481245778CB1

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   1054719   1050624  513M EFI System
/dev/sda3  1054720 209715166 208660447 100G Linux filesystem

alovn@ubuntu-desktop:~$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  1.6G  1.7M  1.6G   1% /run
/dev/sda3      ext4    98G   15G   80G  16% /
tmpfs          tmpfs  7.9G   52M  7.8G   1% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2      vfat   512M  6.1M  506M   2% /boot/efi
tmpfs          tmpfs  1.6G  2.4M  1.6G   1% /run/user/1000

到这里扩容已经完成了。

注意:如果你的linux磁盘扩容后,发现使用fdisk查看磁盘分区已经扩容成功,而用df查看文件系统大小却没有变化,那么可能是你重启后忘记执行上面的 resize2fs 命令了。这是因为fdisk查看的是磁盘大小,而df查看的是文件系统大小,两者是有区别的。

resize2fs 用于扩展文件系统的逻辑边界, 它适用于 ext2、ext3、ext4文件系统,如果是xfs文件系统,则需要使用 xfs_growfs