Total Pageviews

Sunday, 29 January 2012

XEN VPS下,修改swap虚拟内存的大小



[root@gamedb ~]# free –m

[root@gamedb tmp]# dd if=/dev/zero of=/tmp/big_swap bs=1024 count=2000000

[root@gamedb tmp]# du -sh /tmp/big_swap

[root@gamedb tmp]# mkswap /tmp/big_swap

[root@gamedb tmp]# swapon /tmp/big_swap

[root@gamedb tmp]# free –m

[root@gamedb tmp]# vi /etc/fstab

# LABEL=SWAP-hda2         swap                    swap    defaults        0 0

  /tmp/big_swap           swap                    swap    defaults        0 0

一、查看内存和swap大小,发现swap是内存大小的一半

 [root@gamedb ~]# free -m

             total       used       free     shared    buffers     cached

Mem:          1001        338        662          0          8        307

-/  buffers/cache:         22        978

Swap:          509          0        509

二、决定修改swap大小,首先在空间合适处创建用于分区的swap文件:

[root@gamedb tmp]# dd if=/dev/zero of=/tmp/big_swap bs=1024 count=2000000

读入了 2000000 0 个块

输出了 2000000 0 个块

(注意:if 表示 infile,of 表示outfile,bs=1024 表示写入的每个块的大小为1024B=1KB(1024B字节=1024*8bit位)

三、查看创建的文件大小是否符合要求:

[root@gamedb tmp]# du -sh big_swap

2.0G    big_swap

[root@gamedb tmp]# ls -al big_swap

-rw-r--r--  1 root root 2048000000  6月 18 11:55 big_swap

四、将目的文件设置为swap分区文件:

[root@gamedb tmp]# mkswap big_swap

Setting up swapspace version 1, size = 2047995 kB

五、激活swap,立即启用交换分区文件:

[root@gamedb tmp]# swapon big_swap

六、再次查看内存和虚拟内存,发现已经被正常修改:

[root@gamedb tmp]# free -m

             total       used       free     shared    buffers     cached

Mem:          1001        984         16          0          2        931

-/  buffers/cache:         50        950

Swap:         2462          0       2462
七、若要想使开机时自启用,则需修改文件/etc/fstab中的swap行:
[root@gamedb tmp]# vi /etc/fstab

# LABEL=SWAP-hda2         swap                    swap    defaults        0 0

  /tmp/big_swap           swap                    swap    defaults        0 0
=======================================================================================
动态修改swap大小的方法——使用swap文件

1.首先用命令free查看系统内 Swap 分区大小。
free -m

total used free shared buffers cached
Mem: 1002 964 38 0 21 410
-/  buffers/cache: 532 470
Swap: 951 32 929

可以看到 Swap 只有951M,不符合 Oracle-xe-client 的安装要求。

2.创建一个 Swap 文件。
mkdir swap
cd swap
sudo dd if=/dev/zero of=swapfile bs=1024 count=100000

出现下列提示,上面命令中的 count 即代表swap文件大小。

记录了 100000 0 的读入
记录了 100000 0 的写出
102400000 字节 (102 MB) 已复制,0.74704 秒,137 MB/秒

把生成的文件转换成 Swap 文件
sudo mkswap swapfile

Setting up swapspace version 1, size = 102395 kB
no label, UUID=09fde987-5567-498a-a60b-477e302a988b

3.激活 Swap 文件。
sudo swapon swapfile

再次查看 free -m 的结果。

total used free shared buffers cached
Mem: 1002 967 34 0 22 410
-/  buffers/cache: 534 467
Swap: 1053 32 1021

添加成功。

扩展:
如果需要卸载这个 swap 文件,可以进入建立的 swap 文件目录。执行下列命令。
sudo swapoff swapfile

如果需要一直保持这个 swap ,可以把它写入 /etc/fstab 文件。
swapfilepath swap swap defaults 0 0