1. 程式人生 > 其它 >新增swap交換空間

新增swap交換空間

** 新增swap交換空間的步驟如下:**

** 第一步:確保系統中有足夠的空間來用做swap交換空間 **
我使用的是KVM,準備在一個獨立的檔案系統中新增一個swap交換檔案,在/opt/image中新增2G的swap交換檔案

** 第二步:新增交換檔案並設定其大小為2G **
使用如下命令

[root@sense image]# dd if=/dev/zero of=/opt/image/swap bs=1024 count=2048000

過段時間就返回如下結果:

2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 272.867 seconds, 7.7 MB/s

** 第三步:建立(設定)交換空間 **
使用命令mkswap

[root@sense image]# mkswap /opt/image/swap
Setting up swapspace version 1, size = 2097147 kB

** 第四步:檢查現有的交換空間大小 **
使用命令free

[root@sense image]# free -m
total used free shared buffers cached
Mem: 1011 989 21 0 1 875
-/+ buffers/cache: 112 898
Swap: 1027 0 1027

或者檢查meminfo檔案

grep SwapTotal /proc/meminfo

** 第五步:啟動新增加的2G的交換空間 **
使用命令swapon

[root@sense image]# swapon /opt/image/swap

** 第六步:確認新增加的2G交換空間已經生效 **
使用命令free

[[root@sense image]# free -m
total used free shared buffers cached
Mem: 1011 995 15 0 4 877
-/+ buffers/cache: 113 897
Swap: 3027 0 3027

或者檢查meminfo檔案

grep SwapTotal /proc/meminfo

** 第七步:修改/etc/fstab

檔案,使得新加的2G交換空間在系統重新啟動後自動生效 **
在檔案最後加入:

/opt/image/swap swap swap defaults 0 0