天天看点

Linux之centos7环境下扩展SWAP分区一、swap分区简介二、扩展SWAP分区的三种方法三、常用swap命令

一、swap分区简介

        Swap分区在系统的物理内存不够用的时候,把硬盘内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap分区中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中。众所周知,现代操作系统都实现了“虚拟内存”这一技术,不但在功能上突破了物理内存的限制,使程序可以操纵大于实际物理内存的空间,更重要的是,“虚拟内存”是隔离每个进程的安全保护网,使每个进程都不受其它程序的干扰。

        通常情况下,Swap空间应大于或等于物理内存的大小,最小不应小于64M,通常Swap空间的大小应是物理内存的2-2.5倍。但根据不同的应用,应有不同的配置:如果是小的桌面系统,则只需要较小的Swap空间,而大的服务器系统则视情况不同需要不同大小的Swap空间。特别是数据库服务器和Web服务器,随着访问量的增加,对Swap空间的要求也会增加,具体配置参见各服务器产品的说明。

        另外,Swap分区的数量对性能也有很大的影响。因为Swap交换的操作是磁盘IO的操作,如果有多个Swap交换区,Swap空间的分配会以轮流的方式操作于所有的Swap,这样会大大均衡IO的负载,加快Swap交换的速度。如果只有一个交换区,所有的交换操作会使交换区变得很忙,使系统大多数时间处于等待状态,效率很低。用性能监视工具就会发现,此时的CPU并不很忙,而系统却慢。这说明,瓶颈在IO上,依靠提高CPU的速度是解决不了问题的。

二、扩展SWAP分区的三种方法

一)、通过LVM扩容

通过LVM扩容SWAP分区前天条件为系统使用LVM管理磁盘,且vg卷组有可分配空间。

0、检查swap大小

[[email protected] ~]# lvdisplay

— Logical volume —

LV Path /dev/centos/swap

LV Name swap

VG Name centos

LV UUID MESyl1-3HeV-ZchW-dUm6-AMjp-vede-Yvimel

LV Write Access read/write

LV Creation host, time localhost, 2020-12-31 05:12:59 -0500

LV Status available

# open 2

LV Size 1.99 GiB

Current LE 510

Segments 1

Allocation inherit

Read ahead sectors auto

- currently set to 8192

Block device 253:1

当前SWAP分区大小2G

1、检查卷组可用空间

[[email protected] ~]# vgdisplay

— Volume group —

VG Name centos

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 12

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 3

Open LV 3

Max PV 0

Cur PV 2

Act PV 2

VG Size <27.66 GiB

PE Size 4.00 MiB

Total PE 7080

Alloc PE / Size 6142 / 23.99 GiB

Free PE / Size 938 / 3.66 GiB

VG UUID h7zLOd-BvJI-juZw-8DZt-f2Pe-Zxbz-1I36t7

卷组剩余空间3.66G

2、取消swap分区挂载

[[email protected] ~]# swapoff /dev/mapper/centos-swap

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 1.8G 115M 1.5G 8.8M 140M 1.5G

Swap: 0B 0B 0B

3、检查swap分区

[[email protected] ~]# e2fsck -f /dev/mapper/centos-swap

e2fsck 1.42.9 (28-Dec-2013)

ext2fs_open2: Bad magic number in super-block

e2fsck: Superblock invalid, trying backup blocks…

e2fsck: Bad magic number in super-block while trying to open /dev/mapper/centos-swap

The superblock could not be read or does not describe a correct ext2

filesystem. If the device is valid and it really contains an ext2

filesystem (and not swap or ufs or something else), then the superblock

is corrupt, and you might try running e2fsck with an alternate superblock:

e2fsck -b 8193 <device>

4、扩容swap分区

[[email protected] ~]# lvextend -L 4G /dev/mapper/centos-swap

Size of logical volume centos/swap changed from 1.99 GiB (510 extents) to 4.00 GiB (1024 extents).

Logical volume centos/swap successfully resized.

5、格式化swap分区为swap格式

[[email protected] ~]# mkswap /dev/mapper/centos-swap

mkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.

Setting up swapspace version 1, size = 4194300 KiB

no label, UUID=7b41169c-bb9d-4f85-9dec-7c11ffb572a2

6、挂载swap分区

[[email protected] ~]# swapon /dev/mapper/centos-swap

7、检查swap分区大小

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 1.8G 118M 1.5G 8.8M 140M 1.5G

Swap: 4.0G 0B 4.0G

二)、使用独立的分区来实现

1、检查确认当前swap大小

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 1.8G 117M 1.5G 8.8M 140M 1.5G

Swap: 2.0G 0B 2.0G

2、检查确认空闲磁盘

[[email protected] ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 20G 0 disk

├─sda1 8:1 0 1G 0 part /boot

└─sda2 8:2 0 18.3G 0 part

├─centos-root 253:0 0 10G 0 lvm /

├─centos-swap 253:1 0 2G 0 lvm [SWAP]

└─centos-home 253:2 0 12G 0 lvm /home

sdb 8:16 0 10G 0 disk

sdc 8:32 0 10G 0 disk

└─sdc1 8:33 0 9.3G 0 part

├─centos-swap 253:1 0 2G 0 lvm [SWAP]

└─centos-home 253:2 0 12G 0 lvm /home

sr0 11:0 1 4.3G 0 rom

空闲可用磁盘sdb

3、新建磁盘分区,磁盘格式为swap

[[email protected] ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

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

Be careful before using the write command.

Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p): p

Partition number (1-4, default 1):

First sector (2048-20971519, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G

Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): t

Selected partition 1

Hex code (type L to list all codes): 82

Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

4、通知系统分区表的变化

[[email protected] ~]# partprobe

Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.

4、将新建分区格式化为swap

[[email protected] ~]# mkswap /dev/sdb1

Setting up swapspace version 1, size = 2097148 KiB

no label, UUID=029ddda5-ca64-418a-b083-778341ecc3e5

注意把此处的 UUID=XXX 复制下来,第7步需要使用

5、挂载新建swap分区

[[email protected] ~]# swapon /dev/sdb1

6、检查确认扩容后的swap大小

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 1.8G 118M 1.5G 8.8M 138M 1.5G

Swap: 4.0G 0B 4.0G

7、自动挂载swap分区

#vim /etc/fstab

添加如下内容后保存,其中XXX为swap分区的UUID

UUID=XXXX swap swap defaults 0 0

本实例如下:

UUID=029ddda5-ca64-418a-b083-778341ecc3e5 swap swap defaults 0 0

三)、文件模拟分区创建swap文件系统

1、检查swap分区大小

[[email protected] ~]# free -h

total used free shared buff/cache available

Mem: 1.8G 116M 1.1G 8.8M 615M 1.5G

Swap: 4.0G 0B 4.0G

2、选择有足够剩余空间的磁盘目录

[[email protected] home]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/centos-root 9.8G 1.2G 8.1G 13% /

devtmpfs 908M 0 908M 0% /dev

tmpfs 920M 0 920M 0% /dev/shm

tmpfs 920M 8.9M 911M 1% /run

tmpfs 920M 0 920M 0% /sys/fs/cgroup

/dev/sda1 976M 115M 794M 13% /boot

/dev/mapper/centos-home 12G 6.8G 4.4G 61% /home

tmpfs 184M 0 184M 0% /run/user/0

根分区和home分区有足够剩余空间

3、创建swap文件

创建一个2G大小的swap文件,文件大小及为swap扩容大小,按需创建

[[email protected] /]# dd if=/dev/zero of=swapfile bs=1G count=2

2+0 records in

2+0 records out

2147483648 bytes (2.1 GB) copied, 121.791 s, 17.6 MB/s

4、修改文件权限为600

[[email protected] /]# chmod 600 swapfile

4、格式swapfile格式化为swap分区

[[email protected] /]# mkswap -L swap_file swapfile

Setting up swapspace version 1, size = 2097148 KiB

LABEL=swap_file, UUID=0e2fe387-e8d0-473a-b85f-7dffe2d6d8f9

5、挂载新的swap分区

[[email protected] /]# swapon /swapfile

6、检查最新swap大小

[[email protected] /]# free -h

total used free shared buff/cache available

Mem: 1.8G 118M 1.1G 8.8M 619M 1.5G

Swap: 6.0G 0B 6.0G

7、自动挂载swap分区

#vim /etc/fstab

添加如下内容后保存,其中XXX为swap分区的UUID

UUID=XXXX swap swap defaults 0 0

或者

/swapfile swap swap defaults 0 0

三、常用swap命令

#swapon -a 挂载所有配置在/etc/fstab中的swap分区

#swapon -s 查看swap分区的信息,顺便看看自己刚刚创建的swap分区是否生效

#swapoff /dev/sdb1 取消swap分区sdb1挂载

#swapon /dev/sdb1 挂载swap分区sdb1

#mkswap /dev/sdb1 将磁盘分区sdb1格式化为swap分区

#swapon -V查看swap命令工具版本

继续阅读