# 分配空间
首先需要在 VMware Fusion 中给虚拟机分配空间:
- 删除所有快照并关闭虚拟机
- 「设置」=> 「硬盘」=> 「磁盘大小」
- 调整为合适的值(这里我调整为 80G,这个值是虚拟机所能使用的最大磁盘大小,而非是新增的大小)
# Ubuntu 挂载空间
完成分配后,虚拟机 “看” 到的磁盘变大了,但由于还没有分配 & 挂载,所以此时,Ubuntu 并不能直接使用这片空间,还需进行如下配置。
# 确认使用了 LVM
Ubuntu Server 使用 Logical Volume Management
技术来管理磁盘分区,可以在不停止系统服务的前提下,就能动态增加硬盘空间。使用如下命令:
$ df -h | |
Filesystem Size Used Avail Use% Mounted on | |
udev 2.5G 0 2.5G 0% /dev | |
tmpfs 590M 1.6M 589M 1% /run | |
/dev/mapper/ubuntu--vg-ubuntu--lv 19G 14G 4.6G 75% / | |
tmpfs 2.9G 0 2.9G 0% /dev/shm | |
tmpfs 5.0M 0 5.0M 0% /run/lock | |
tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup | |
/dev/sda2 2.0G 1.1G 780M 58% /boot | |
/dev/loop0 64M 64M 0 100% /snap/core20/1828 | |
/dev/loop2 50M 50M 0 100% /snap/snapd/18357 | |
/dev/loop1 92M 92M 0 100% /snap/lxd/24061 | |
tmpfs 590M 0 590M 0% /run/user/0 |
看组名是 ubuntu--vg-ubuntu--lv
,确定使用了 LVM
# 创建新的物理卷
创建个 sda4,把新增的空闲空间搞成一个分区好了
$sudo parted /dev/sda | |
GNU Parted 3.3 | |
Using /dev/sda | |
Welcome to GNU Parted! Type 'help' to view a list of commands. | |
# 打印磁盘使用情况,可以看到有 42.9GB 的空闲空间 | |
(parted) print free | |
Model: VMware, VMware Virtual S (scsi) | |
Disk /dev/sda: 85.9GB | |
Sector size (logical/physical): 512B/512B | |
Partition Table: gpt | |
Disk Flags: | |
Number Start End Size File system Name Flags | |
17.4kB 1049kB 1031kB Free Space | |
1 1049kB 2097kB 1049kB bios_grub | |
2 2097kB 2150MB 2147MB ext4 | |
3 2150MB 42.9GB 40.8GB | |
42.9GB 85.9GB 42.9GB Free Space | |
# 创建新分区 | |
(parted) mkpart primary ext4 42.9GB 100% | |
(parted) print free | |
Model: VMware, VMware Virtual S (scsi) | |
Disk /dev/sda: 85.9GB | |
Sector size (logical/physical): 512B/512B | |
Partition Table: gpt | |
Disk Flags: | |
Number Start End Size File system Name Flags | |
17.4kB 1049kB 1031kB Free Space | |
1 1049kB 2097kB 1049kB bios_grub | |
2 2097kB 2150MB 2147MB ext4 | |
3 2150MB 42.9GB 40.8GB | |
4 42.9GB 85.9GB 42.9GB primary | |
85.9GB 85.9GB 1032kB Free Space | |
(parted) quit | |
# 创建了一个 ext4 类型主分区。设备路径是 /dev/sda4 | |
$sudo pvcreate /dev/sda4 | |
Physical volume "/dev/sda4" successfully created. |
# 把 ubuntu-vg 组扩展到 /dev/sda4 上
$sudo vgextend ubuntu-vg /dev/sda4 | |
Volume group "ubuntu-vg" successfully extended | |
$sudo lvm lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv | |
Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to 77 GiB (19964 extents). | |
Logical volume ubuntu-vg/ubuntu-lv successfully resized. |
# 扩展文件系统
$sudo resize2fs -p /dev/mapper/ubuntu--vg-ubuntu--lv | |
resize2fs 1.45.5 (07-Jan-2020) | |
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required | |
old_desc_blocks = 3, new_desc_blocks = 5 | |
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 10221568 (4k) blocks long. | |
$df -h | |
Filesystem Size Used Avail Use% Mounted on | |
udev 2.5G 0 2.5G 0% /dev | |
tmpfs 590M 1.6M 589M 1% /run | |
/dev/mapper/ubuntu--vg-ubuntu--lv 77G 14G 63G 18% / | |
tmpfs 2.9G 0 2.9G 0% /dev/shm | |
tmpfs 5.0M 0 5.0M 0% /run/lock | |
tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup | |
/dev/sda2 2.0G 1.1G 780M 58% /boot | |
/dev/loop0 64M 64M 0 100% /snap/core20/1828 | |
/dev/loop2 50M 50M 0 100% /snap/snapd/18357 | |
/dev/loop1 92M 92M 0 100% /snap/lxd/24061 | |
tmpfs 590M 0 590M 0% /run/user/0 |
完成~
# 参考
- https://oi.0w0.io/2022/04/20 / 在 vmware 下扩展 - ubuntu-20-04 - 磁盘分区(-dev-mapper-ubuntu-vg-ubuntu-lv)/