0%

如何为Ubuntu虚拟机扩展磁盘

VMWare安装了Ubuntu1904虚拟机,空间不足了,此篇文章将讲解如何扩容磁盘。

查看磁盘使用情况

1
2
3
4
5
6
7
8
9
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.9M 794M 1% /run
/dev/sda1 20G 18G 921M 96% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 40K 796M 1% /run/user/1000

可以看出20G的磁盘使用率达到96%了,需要赶紧扩容了。

在VMWare中设置虚拟机属性

首先把Ubuntu虚拟机关机。

在VMWare中可以看到磁盘大小为20G。

点击”扩展”按钮。

磁盘从原始的20G扩展到30G

这里操作完成以后,还需要进入到Ubuntu系统中进行分区和扩展操作。

重启Ubuntu虚拟机

再次查看硬盘使用情况。

1
2
3
4
5
6
7
8
9
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.8M 794M 1% /run
/dev/sda1 20G 18G 978M 95% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 28K 796M 1% /run/user/1000

可以发现,实质上是没有变化的。但通过fdisk可以看到/dev/sda设备已经有30G了。

1
2
3
4
5
6
7
8
9
10
11
sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
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: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 41940991 41938944 20G 83 Linux

备份数据

为了防止数据丢失,为虚拟机做一个快照吧。

扩容操作

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
sudo fdisk /dev/sda
[sudo] password for simon:

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


Command (m for help): m

Help:

DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag

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
u change display/entry units
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):

可以先打印分区表看看基本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): p
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
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: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 41940991 41938944 20G 83 Linux

Command (m for help):

如果有交换分区(Linux swap),比如/dev/sdan,则需要关闭。

1
sudo swapoff /dev/sdan

然后删除分区

1
2
3
4
5
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help):

新建分区

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914559, default 62914559):

Created a new partition 1 of type 'Linux' and of size 30 GiB.
Partition #1 contains a ext4 signature.

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

打印看一下

1
2
3
4
5
6
7
8
9
10
11
Command (m for help): p
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
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: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 62914559 62912512 30G 83 Linux

确认无误

1
2
3
Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

写入

1
2
3
Command (m for help): w
The partition table has been altered.
Syncing disks.

重启虚拟机

1
sudo reboot

不要着急,这个时候查看磁盘,还是会显示20G的。

执行扩容操作

1
2
3
4
5
6
sudo resize2fs /dev/sda1
[sudo] password for simon:
resize2fs 1.44.6 (5-Mar-2019)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 7864064 (4k) blocks long.

最后再次查看磁盘信息

1
2
3
4
5
6
7
8
9
10
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.7M 794M 1% /run
/dev/sda1 30G 18G 11G 63% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 1.2M 794M 1% /run/user/123
tmpfs 796M 0 796M 0% /run/user/1000

我们发现/dev/sda1已经有30G了,扩容成功。