如何利用硬盘空间为linux服务器或xen的vps添加swap分区?请仔细看下面的命令以及结果,这是在centos系统上执行的操作:
[[email protected] opt]# free
total used free shared buffers cached
Mem: 636724 279852 356872 0 13532 204360
-/+ buffers/cache: 61960 574764
Swap: 6020 0 6020
[[email protected] opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 10G 2.8G 6.7G 30% /
none 311M 0 311M 0% /dev/shm
/dev/sdf5 43G 181M 40G 1% /home
[[email protected] opt]# ls -al
total 6056
drwxr-xr-x 4 root root 4096 Feb 28 22:38 .
drwxr-xr-x 22 root root 4096 Feb 28 22:39 ..
drwxr-xr-x 7 root root 4096 Feb 28 21:31 rightscale
drwxr-xr-x 3 root root 4096 Aug 5 2010 sun
-rw-r--r-- 1 root root 6172672 Feb 28 22:40 swapfile
[[email protected] opt]# /sbin/swapoff swapfile (如果已经有swap文件,先要删除他)
[[email protected] opt]# free
total used free shared buffers cached
Mem: 636724 277992 358732 0 13672 204372
-/+ buffers/cache: 59948 576776
Swap: 0 0 0
[[email protected] opt]# dd if=/dev/zero of=/opt/swapfile bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 3.98724 seconds, 135 MB/s
[[email protected] opt]# ls -al
total 524820
drwxr-xr-x 4 root root 4096 Feb 28 22:38 .
drwxr-xr-x 22 root root 4096 Feb 28 22:39 ..
-rw-r--r-- 1 root root 536870912 Feb 28 22:50 swapfile
[[email protected] opt]# mkswap swapfile
Setting up swapspace version 1, size = 536866 kB
[[email protected] opt]# swapon swapfile
[[email protected] opt]# swapon -s
Filename Type Size Used Priority
/opt/swapfile file 524280 0 -2
[[email protected] opt]# free
total used free shared buffers cached
Mem: 636724 623872 12852 0 5404 552384
-/+ buffers/cache: 66084 570640
Swap: 524280 0 524280
[[email protected] opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 10G 3.3G 6.2G 35% /
none 311M 0 311M 0% /dev/shm
/dev/sdf5 43G 181M 40G 1% /home
下面说一下Xen VPS添加swap交换分区的方法。
1、进入一个目录
cd /opt/
2、获取725M的文件块:
dd if=/dev/zero of=/opt/swapfile bs=1M count=725
3、创建swap文件
mkswap swapfile
4、激活swap文件
swapon swapfile
5、查看一下swap是否正确
swapon -s
6、加入fstab文件让系统引导时自动启动
echo "/opt/swapfile swap swap defaults 0 0" >> /etc/fstab