再给服务器添加zabbix监控的时候,发现服务器有个报错“Lack of free swap space on localhost”,通过查找得知,在安装服务器的时候忘了划分swap分区。为了减少报错,现在直接划分swap分区,具体步骤如下:


1,使用dd命令创建一个swap分区

1
2
3
4
[root@cms home] # dd if=/dev/zero of=/home/swap bs=1024 count=8192000
8192000+0 records  in
8192000+0 records out
8388608000 bytes (8.4 GB) copied, 33.5006 s, 250 MB /s

这样就创建了一个分区大小为8G的/home/swap文件,接下来格式化该分区文件。


2,格式化/home/swap文件。

1
2
3
4
5
[root@cms home] # mkswap /home/swap 
mkswap:  /home/swap : warning: don't erase bootbits sectors
         on whole disk. Use -f to force.
Setting up swapspace version 1, size = 8191996 KiB
no label, UUID=4ae031dd-eefe-43ba-8fb0-c09719d896b6


3,用swapon命令把/home/swap文件分区划成swap分区

1
2
3
4
5
6
[root@cms home] # swapon /home/swap 
[root@cms home] # free -m
              total       used        free      shared    buffers     cached
Mem:          7872       7730        142          0         70       6394
-/+ buffers /cache :       1264       6608
Swap:         7999          0       7999


4,修改/etc/fstab文件添加/home/swap swap swap default 0 0,即使重启也能自动挂载。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@cms home] # vi /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Tue Jan 26 21:10:02 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f8cb78d5-8419-4bcf-8684-9e8a94480f22 /                       ext4    defaults        1 1
UUID=9e4bacd3-490c-4e87-a5e0-fec240a3e229  /usr                     xfs     defaults        1 2
tmpfs                    /dev/shm                 tmpfs   defaults        0 0
devpts                   /dev/pts                 devpts  gid=5,mode=620  0 0
sysfs                    /sys                     sysfs   defaults        0 0
proc                     /proc                    proc    defaults        0 0
  /home/swap              swap                    swap    default         0 0
"/etc/fstab"  16L, 788C written




备注:count的计算公式: count=SIZE*1024  (size以MB为单位)