当我们使用esxi创建openwrt的虚拟机时,采取直接添加现有硬盘时可能会出现根目录内存不足的情况,本文将介绍一种为虚拟机添加硬盘的方法.首先在esxi后台停止openwrt的虚拟机,然后为openwrt添加一块新标准硬盘容量自己随意.

磁盘分区

SSH 终端连接 Open­Wrt,运行下面命令查看磁盘信息

1
fdisk -l

此时,会显示一个未被使用的磁盘 sdb 即新磁盘,,屏幕打印信息如下:

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
root@xiaoming:~# fdisk -l
Disk /dev/loop0: 874.75 MiB, 917241856 bytes, 1791488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
GPT PMBR size mismatch (2130463 != 2130494) will be corrected by write.
The backup GPT table is corrupt, but the primary appears OK, so that will be used.
The backup GPT table is not on the end of the device.


Disk /dev/sda: 1.02 GiB, 1090813440 bytes, 2130495 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: 79C88912-01B9-8267-AA0B-42099DBC2600

Device Start End Sectors Size Type
/dev/sda1 512 33279 32768 16M Linux filesystem
/dev/sda2 33280 2130431 2097152 1G Linux filesystem
/dev/sda128 34 511 478 239K BIOS boot

Partition table entries are not in disk order.


Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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

若要使用这块新磁盘,就需要对新磁盘进行分区,运行以下命令:

1
fdisk /dev/sdb

创建一个主分区,分区操作步骤如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@xiaoming:~# fdisk /dev/sdb

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

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x91cff149.

Command (m for help): n (输入n 创建分区)
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p (输入p 创建主分区)
Partition number (1-4, default 1): (直接回车,默认创建一个分区)
First sector (2048-4194303, default 2048): (直接回车)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4194303, default 4194303):(直接回车)

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): w (输入w 写入分区表)
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

查看磁盘信息:

1
fdisk -l

屏幕打印信息如下:

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
root@xiaoming:~# fdisk -l
Disk /dev/loop0: 874.75 MiB, 917241856 bytes, 1791488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
GPT PMBR size mismatch (2130463 != 2130494) will be corrected by write.
The backup GPT table is corrupt, but the primary appears OK, so that will be used.
The backup GPT table is not on the end of the device.


Disk /dev/sda: 1.02 GiB, 1090813440 bytes, 2130495 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: 79C88912-01B9-8267-AA0B-42099DBC2600

Device Start End Sectors Size Type
/dev/sda1 512 33279 32768 16M Linux filesystem
/dev/sda2 33280 2130431 2097152 1G Linux filesystem
/dev/sda128 34 511 478 239K BIOS boot

Partition table entries are not in disk order.


Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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: dos
Disk identifier: 0x91cff149

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4194303 4192256 2G 83 Linux

其中 /dev/sdb1 即新创建的新分区分配的盘符格式化将分区格式化 ext4 文件系统:

1
mkfs.ext4 /dev/sdb1

挂载分区

进入 Open­Wrt 管理后台,依次点击 系统 - 挂载点 找到并点击全局设置中的 生成配置挂载点 找到创建的新分区,点击 修改 重新调整挂载项目的设置。勾选 启用此挂载点挂载点 选择为 作为根文件系统使用 ,完整复制根目录准备中的所有命令行后,点击 保存并应用image.png⚠️ 上面复制得到的命令行不要直接运行!!!不要直接运行!!!否则报错。需要手动修改命令行中 mount /dev/sda1 /tmp/extroot 为 fdisk 创建的新分区盘符,例如这里应修改为 mount /dev/sdb1 /tmp/extroot,然后进入 SSH 终端,运行修改后的完整命令行,如下:

1
2
3
4
5
6
7
mkdir -p /tmp/introot
mkdir -p /tmp/extroot
mount --bind / /tmp/introot
mount /dev/sdb1 /tmp/extroot # 修改后
tar -C /tmp/introot -cvf - . | tar -C /tmp/extroot -xf -
umount /tmp/introot
umount /tmp/extroot

回车键到底,直到跑完所有命令行。然后运行 reboot 重启 Open­Wrt ~image.png