软路由部署在ESXi环境的小内存环境中,最近经常访问异常,通过日志查看到多次出现内存溢出的情况,由于环境限制,无法再给虚拟机添加内存,只能给虚拟机添加swap空间;

操作前

root@OpenWrt:~# free
              total        used        free      shared  buff/cache   available
Mem:        1011104      431264      205688       12452      374152      558964
Swap:             0           0           0

操作步骤

  1. 给openwrt添加第二块磁盘并分区
root@OpenWrt:~# fdisk -l


Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual I
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: 0x98042d75

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *        512    33279    32768  16M 83 Linux
/dev/sda2         33792  4195327  4161536   2G 83 Linux
/dev/sda3       4196352 41943039 37746688  18G 83 Linux

Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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
root@OpenWrt:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.35.1).
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 0xe0c61459.

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-8388607, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-8388607, default 8388607):

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

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@OpenWrt:~# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual I
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: 0x98042d75

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *        512    33279    32768  16M 83 Linux
/dev/sda2         33792  4195327  4161536   2G 83 Linux
/dev/sda3       4196352 41943039 37746688  18G 83 Linux

Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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: 0xe0c61459

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8388607 8386560   4G 83 Linux
  1. 将磁盘格式化成swap
root@OpenWrt:~# mkswap /dev/sdb1
Setting up swapspace version 1, size = 4293914624 bytes
  1. 挂载swap分区
root@OpenWrt:~# swapon /dev/sdb1
root@OpenWrt:~# free
              total        used        free      shared  buff/cache   available
Mem:        1011104      434436      200120       13504      376548      556068
Swap:       4193276           0     4193276
root@OpenWrt:~# cat /etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/sdb1 swap swap
root@OpenWrt:~#
  1. 操作结果确认
    重启虚拟机系统,验证结果;
root@OpenWrt:~# free
              total        used        free      shared  buff/cache   available
Mem:        1011104      434436      200120       13504      376548      556068
Swap:       4193276           0     4193276
最后修改:2021 年 04 月 13 日 02 : 23 PM
如果觉得我的文章对你有用,请随意赞赏