方法#3:创建一个交换文件。

  1. 在当前文件系统上创建一个交换文件,例如在 root 上,为此可以创建一个新目录。
$ sudo mkdir /swap
  1. 在这个新目录中创建一个新文件,在本例中创建了一个 2Gb 的新文件。
$ sudo dd if=/dev/zero of=/swap/swapfile1 bs=1M count=2048
  1. 在已创建的文件上创建一个新的交换区。
$ sudo mkswap  /swap/swapfile1
  1. 更改文件的权限。
$ sudo chmod 600 /swap/swapfile1
  1. 将交换分区添加到 /etc/fstab 文件中,如下所示:
/swap/swapfile1    swap   swap      defaults       0 0
  1. 加载为实例创建的新交换空间。
$ sudo swapon -a
  1. 要列出交换设备,请运行以下命令。
$ sudo swapon -s

可以使用以下命令检查实例可用的新交换区域。

$ sudo free -m
total used free shared buff/cache available
Mem: 14763 256 11898 65 2608 14102
Swap: 12661 0 12661          ### Total Swap area
在 Linux 上增加交换空间

有时默认物理内存不足以为某些任务或者进程提供服务。
可能会发生进程将更多内存交换进和交换出交换空间的情况。
这篇文章有助于解释如何在 Linux 系统上增加交换内存。

这篇文章将解释 3 种不同的方法,这取决于每种情况下使用这些选项中的任何一种的可用性。

on  It Road.com

方法#2:在操作系统磁盘上新建分区

1、在原来的磁盘上新建一个分区,这仅适用于有一些可用空间的情况,可用空间可以用以下命令检查。

$ sudo parted /dev/sda print free
Model: ORACLE BlockVolume (scsi)
Disk /dev/sda: 50.0GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 211MB 210MB fat16 EFI System Partition boot
2 211MB 8801MB 8590MB linux-swap(v1)
3 8801MB 50.0GB 41.2GB xfs
50.0GB 50.0GB 1032kB Free Space        ### Free space
  1. 如果有空闲空间,使用以下命令在原始磁盘上创建一个新分区。
$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n
Partition number (4-128, default 4): 4
First sector (34-98566110, default = 97675264) or {+-}size{KMGTP}:     ### Press 'Enter' for default
Last sector (97675264-98566110, default = 98566110) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM
a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap
a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID
a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap
a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum
a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+
af01 Apple RAID af02 Apple RAID offline af03 Apple label
af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot
bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT
eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit
Hex code or GUID (L to show codes, Enter = 8300): 8200     ### Choose Linux Swap type.
Changed type of partition to 'Linux swap'
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 98566144 sectors, 47.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 25B6B886-EA7F-4DE1-9260-979D9F34D17F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 98566110
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00 EFI System Partition
2 1050624 17827839 8.0 GiB 8200
3 17827840 97675263 38.1 GiB 0700
4 97675264 98566110 435.0 MiB 8200 Linux swap    ### New Partition
  1. 运行以下命令重新加载分区表上的更改。
$ sudo partprobe /dev/sda
  1. 在原盘上刚刚创建的新分区上新建一个交换区。
$ sudo mkswap /dev/sda4
Setting up swapspace version 1, size = 1020 KiB
no label, UUID=eeb4e9e0-386f-4fbd-919a-130e2c17079e
  1. 将交换条目添加到 /etc/fstab 文件中,如下所示:
UUID=1a50efd3-ddc6-4dab-b3ad-xxxxxxxx    swap     swap         defaults,_netdev,x-initrd.mount  0  0
  1. 加载为实例创建的新交换空间。
$ sudo swapon -a
  1. 要列出交换设备,请运行以下命令。
# sudo swapon -s

注意:如果使用此方法,请考虑如果将来分区 #3 需要更多空间,则需要删除最后一个分区(交换分区)才能扩展分区 #3.

方法#1:使用新磁盘

  1. 将可用存储中的新磁盘添加到 Linux 实例。

  2. 将新磁盘提供给实例并且操作系统检测到它后,运行以下命令在此新磁盘上创建新的交换空间/分区。
    请注意,磁盘上的剩余空间可根据需要用于任何其他文件系统。

$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-104857566, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-104857566, default = 104857566) or {+-}size{KMGTP}: 4096000    ### Select the desired size
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8200    ### Type L to list all the FS types an select the Linux Swap.
Changed type of partition to 'Linux swap'
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
  1. 运行以下命令重新加载分区表上的更改。
$ sudo partprobe /dev/sdb
  1. 在刚刚创建的新分区上新建一个交换区:
$ sudo mkswap /dev/sdb1
Setting up swapspace version 1, size = 1020 KiB
no label, UUID=eeb4e9e0-386f-4fbd-919a-130e2c17079e
  1. 将交换条目添加到 /etc/fstab 文件中,如下所示,最好的建议是使用分区的 UUID 而不是名称。
UUID=1a50efd3-ddc6-4dab-b3ad-xxxxxxxx      swap        swap        defaults,_netdev,x-initrd.mount    0  0

6.加载已经为Instance创建的新swap空间以在需要时开始使用它,需要发出以下命令。

# sudo swapon -a
  1. 要列出交换设备,请运行以下命令。
# sudo swapon -s
  1. 最后,运行以下命令将其添加到服务器的 initramfs。
# sudo dracut -f -v
日期:2020-09-17 00:14:55 来源:oir作者:oir