• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

Linux SWAP详解Lack of free swap space on zabbix添加swap交换分区【显哥出品,必为精品】

技术栈 lixian 3年前 (2021-03-30) 1263次浏览 1个评论 扫描二维码
文章目录[隐藏]
dd if=/dev/zero of=/home/swap-file bs=8M count=1024
mkswap /home/swap-file
swapon /home/swap-file
vim /etc/fstab 
/home/swap-file				swap			  swap    defaults        0 0

一.SWAP 说明

1.1 SWAP 概述

当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。

 

这个是SWAP 交换分区的作用。 实际上,我们更关注的应该是SWAP分区的大小问题。 设置多大才是最优的。

 

一般来说可以按照如下规则设置swap大小:

4G以内的物理内存,SWAP 设置为内存的2倍。

4-8G的物理内存,SWAP 等于内存大小。

8-64G 的物理内存,SWAP 设置为8G。

64-256G物理内存,SWAP 设置为16G。

1.2 系统在什么情况下才会使用SWAP?

实际上,并不是等所有的物理内存都消耗完毕之后,才去使用swap的空间,什么时候使用是由swappiness 参数值控制。

[root@rhce ~]# cat /proc/sys/vm/swappiness

60

该值默认值是60.

swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,
swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。

1.3 如何修改swappiness参数?

–临时性修改:

[root@rhce ~]# sysctl vm.swappiness=10

vm.swappiness = 10

[root@rhce ~]# cat /proc/sys/vm/swappiness

10

这里我们的修改已经生效,但是如果我们重启了系统,又会变成60.
–永久修改:

在/etc/sysctl.conf 文件里添加如下参数:
vm.swappiness=10

 

或者:

[root@rhce ~]# echo 'vm.swappiness=10' >>/etc/sysctl.conf

保存,重启,就生效了。

二.管理SWAP

2.1 查看系统当前SWAP 空间大小

[root@dave ~]# free -m

            total       used       free    shared    buffers     cached

Mem:         1954       1923         31          0         21       1345

-/+ buffers/cache:        555       1399

Swap:        1999         21       1978

 

2.2 释放SWAP 空间

假设我们的系统出现了性能问题,我们通过vmstat命令看到有大量的swap,而我们的物理内存又很充足,那么我们可以手工把swap 空间释放出来。让进程去使用物理内存,从而提高性能。

[root@dave ~]# vmstat 1 5

procs -----------memory---------- ---swap-------io---- --system-- -----cpu-----

 r  b  swpd   free   buff cache   si   so   bi    bo   in  cs us sy id wa st

 0  0 22272  32620  22032 1378312    0   0    33    38   0   41 1  2 96  0  0

 0  0 22272  32612  22032 1378340    0   0     0     0 902 1627  0  5 95 0  0

 0  0 22272  32612  22032 1378340    0   0     0     0 905 1636  1  8 91 0  0

 0  0 22272  32612  22032 1378340    0   0     0    32 907 1616  0  6 94 0  0

 0  0 22272  32612  22032 1378340    0   0     0     0 924 1651  0  8 92 0  0
[root@dave ~]# free -m
            total       used       free    shared    buffers    cached

Mem:         1954       1923         31          0         21       1345

-/+ buffers/cache:        555       1399

Swap:         1999         21       1978

注意:free命令默认单位为k, -m 单位为M。 我们这里的swap使用了21M的空间。

–查看当前swap 的使用

[root@dave ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       2047992 22272   -1

[root@dave ~]# cat /proc/swaps

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       2047992 22272   -1

[root@dave ~]#

 

swapon –s 等于 cat/proc/swaps

 

–关闭swap 交换分区:

[root@dave ~]# swapoff/dev/sda2

[root@dave ~]# swapon -s

Filename                                Type            Size    Used   Priority

 

–查看swap的使用情况:

[root@dave ~]# free -m

            total       used       free    shared    buffers     cached

Mem:         1954       1939         15          0         21       1343

-/+ buffers/cache:        573       1381

Swap:           0          0          0

–启用swap分区:

[root@dave ~]# swapon /dev/sda2

 

–验证状态:

[root@dave ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       2047992 0       -1

这里/dev/sda2是我们在安装操作系统时划分的磁盘分区。实际上,我们也可以使用文件来做为交换分区。具体后面会演示。

我们上面写的是具体名称,也可是使用swapoff  -a进程,演示如下:

[root@dave ~]# swapoff -a

[root@dave ~]# swapon -s

Filename                                Type            Size    Used   Priority

[root@dave ~]# swapon -a

[root@dave ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       2047992 0       -1

我们查看/etc/fstab 文件,我们的swap 也配置到开启自动启动了。

[root@dave ~]# cat /etc/fstab

# /etc/fstab

# Created by anaconda on Wed Aug 15 00:09:26 2012

#

# Accessible filesystems, by reference, aremaintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info

#

UUID=beea0820-3ac3-4551-9bf0-1b2a462d3ab4 /                       ext4    defaults        1 1

UUID=e5ec9543-143f-4e3b-b8a7-4fa05b01836a/boot                   ext4    defaults        1 2

UUID=b256c0bb-9000-456b-b9eb-18239b5df5ddswap                    swap    defaults        0 0

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

 
简单的说:

(1)ext分区是否启用由mount及umount控制。

(2)swap分区是否启动,由swapon及swapoff控制。

我们对swap 空间的释放,也是通过关闭swap分区,在启动swap 分区来实现的。

2.3 使用文件来作为SWAP 交换分区

 

我们这里使用文件添加一个交换区,具体操作如下:

 

–在根目录下生成一个文件:swap-file,大小1G:

[root@dave u01]# dd if=/dev/zero of=/swap-file bs=1M count=1024

1024+0 records in

1024+0 records out

1073741824 bytes (1.1 GB) copied, 5.91518 s, 182MB/s

[root@dave u01]# cd /

[root@dave /]# ls

bin  cgroup  etc   lib   lost+found  misc  net proc  sbin     srv       sys  u01  usr

boot dev     home  lib64 media       mnt   opt root  selinux  swap-file tmp  u02  var

[root@dave /]#

–将生成的文件格式化成交换分区:

[root@dave /]# mkswap /swap-file

mkswap: /swap-file: warning: don't erase bootbitssectors

        onwhole disk. Use -f to force.

Setting up swapspace version 1, size = 1048572 KiB

no label, UUID=653bbeb5-4abb-4295-b110-5847e073140d

--这里没有分区的lable,只有一个UUID。

–启动swap分区并查看状态:

[root@dave /]# swapon /swap-file

[root@dave /]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       2047992 0       -1

/swap-file                              file            1048568 0       -2

这里我们就看到了2个swap。

 

但是这个只对当前有效,如果想下次重启系统后还继续有效,需要将配置写入到/etc/fstab文件中。

 

在/etc/fstab文件中添加如下内容:

UUID=653bbeb5-4abb-4295-b110-5847e073140d swap                    swap    defaults        0 0

或者:

/swap-file swap                    swap    defaults        0 0

2.4 使用磁盘添加swap

先划分一个1G的磁盘分区出来:
 



[root@rhce /]# fdisk /dev/sdb

Device contains neither a valid DOS partitiontable, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier0x65edb587.

Changes will remain in memory only, until youdecide to write them.

After that, of course, the previous content won'tbe recoverable.

Warning: invalid flag 0x0000 of partition table 4will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to

        switch off the mode (command 'c') and change display units to

        sectors (command 'u').

 

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-652, default 1):

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-652,default 652): +1G

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

[root@rhce /]# fdisk -l

 

Disk /dev/sda: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x000bcd24

 

   DeviceBoot      Start         End      Blocks  Id  System

/dev/sda1  *           1         128    1024000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2             128         511    3072000   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             511        2611   16874496   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x65edb587


   DeviceBoot      Start         End      Blocks  Id  System

/dev/sdb1               1         132    1060258+  83  Linux

…

 

注意这里fdisk ID的编号。 这里显示的83,它代表这个分区是ext的分区,这个是不能用来做swap的。

 

在fdisk 命令可以看到具体数据的含义:

Command (m for help): l


ext的分区ID号:0x83   

swap的分区ID号:0x82

–将/dev/sdb1分区改成swap 分区:

[root@rhce ~]# fdisk /dev/sdb


WARNING: DOS-compatible mode is deprecated. It'sstrongly recommended to

        switch off the mode (command 'c') and change display units to

        sectors (command 'u').

 

Command (m for help): h

h: unknown command


 

Command (m for help): t

Selected partition 1

Hex code (type L to list codes): L

 

Hex code (type L to list codes):82

Changed system type of partition1 to 82 (Linux swap / Solaris)

–注意这里更改磁盘分区的ID类型,如果不更在,在系统重启时会根据ID来判断磁盘的类型,那样在使用时就会出现错误。

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed witherror 16: Device or resource busy.

The kernel still uses the old table. The new tablewill be used at

the next reboot or after you run partprobe(8) orkpartx(8)

Syncing disks.

[root@rhce ~]#

 

 

–格式化swap分区:

[root@rhce /]# mkswap /dev/sdb1

Setting up swapspace version 1, size = 1060252 KiB

no label, UUID= bc913e6b-209a-49a7-b561-9b8c57294681

 

[root@rhce /]# mkswap –L swap-disk/dev/sdc1

 

一个设置了Lable,一个没设置。我们在添加/etc/fstab时,可是用LABLE名称,或者使用UUID来配置。 具体如上。

 


Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x65edb587

 

   DeviceBoot      Start         End      Blocks  Id  System

/dev/sdb1               1         132    1060258+  82  Linux swap / Solaris

 

 

Disk /dev/sdc: 5368 MB, 5368709120 bytes

181 heads, 40 sectors/track, 1448 cylinders

Units = cylinders of 7240 * 512 = 3706880 bytes

Sector size (logical/physical): 512 bytes / 512bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000

 

   DeviceBoot      Start         End      Blocks  Id  System

/dev/sdc1               1         291    1053400   82  Linux swap / Solaris

 

注意: 使用blkid 查看类型已经变成了swap:

[root@rhce ~]# blkid

/dev/sda1: UUID="935f7fb2-2ef4-486f-ae4e-265eaf9f580f"TYPE="ext4"

/dev/sda2:UUID="43343402-9188-4cdc-9c79-0e36ab737517" TYPE="swap"

/dev/sda3:UUID="7e49fb2c-6fd8-461a-95d9-65d1f3994160" TYPE="ext4"

/dev/sdb1:UUID="bc913e6b-209a-49a7-b561-9b8c57294681" TYPE="swap"

/dev/sdc1:LABEL="swap-disk"UUID="799b4379-ca59-4508-ad04-2d18847b3fd9" TYPE="swap"

[root@rhce ~]#

 

这里的类型已经改变,我们可以尝试手工的启动swap:

[root@rhce ~]# swapon /dev/sdc1

[root@rhce ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       3071992 0       -1

/dev/sdb1                               partition       1060248 0       -2

/dev/sdc1                               partition       1053392 0       -3

 

这里可以直接启用swap。

我们这里把/dev/dbb1和/dev/sdc1都添加/etc/fstab 文件,在重启一下看看。 添加内容如下:

UUID=bc913e6b-209a-49a7-b561-9b8c57294681 swap                    swap    defaults        0 0

LABEL=swap-disk        swap    swap    defaults        0 0

 

重启系统后查看:

[root@rhce ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       3071992 0       -1

/dev/sdb1                               partition       1060248 0       -2

/dev/sdc1                               partition       1053392 0       -3

 

–对/dev/sdb1 进行测试:

[root@rhce ~]# swapoff /dev/sdb1

[root@rhce ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       3071992 0       -1

[root@rhce ~]# swapon /dev/sdb1

[root@rhce ~]# swapon -s

Filename                                Type            Size    Used   Priority

/dev/sda2                               partition       3071992 0       -1

/dev/sdb1                               partition       1060248 0       -2

2.5 SWAP 分区的删除

 

    Swapoff 文件之后,删除掉对应的空间,在从/etc/fstab删除对应的记录即可。


本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:Linux SWAP详解Lack of free swap space on zabbix添加swap交换分区【显哥出品,必为精品】
喜欢 (1)

您必须 登录 才能发表评论!

(1)个小伙伴在吐槽
  1. Reading your article helped me a lot and I agree with you. But I still have some doubts, can you clarify for me? I'll keep an eye out for your answers.