linux系统中添加swap交换分区、删除swap交换分区

      swap交换分区是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不常用的数据临时存放在硬盘中,以便腾出物理内存空间让更活跃的程序服务来

使用的技术,其设计的目的是解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存满,所以只有当真实的

物理内存耗尽后才会调用交换分区的资源。在生产环境中,交换分区的大小一般为真实物理内存的1.5~2倍。(linux就该这么学p128)

      swap及交换分区,类似于windows虚拟内存的作用,其作用可简单描述为,在系统物理内存不足时,将暂时不用的数据存放到交换空间所在的硬盘上,

从而可以腾出内存来让别的程序运行。(https://blog.csdn.net/qq_37344125/article/details/107446554)

      通常swap都是在装系统硬盘分区时设定,这里只是探讨一下系统已经安装好后,扩展swap分区的方法.下面我们来将下如何在安装好的linux下增加swap交换分区.

可以有两种方法来增加swap分区,一种是将新的分区来作为swap,另一种是在磁盘中创建一个大的文件来作swap.(http://www.ttlsa.com/linux/update-linux-swap/)

 

1、如何查看当前的swap分区,swapon -s  命令

[root@linuxprobe dev]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1

 

2、使用新的分区来做swap (增加交换分区有两种方法,一种是新的交换分区来做swap,另外一种是在磁盘中创建一个大的文件来做swap)

[root@linuxprobe dev]# pwd  ##查看当前路径
/dev
[root@linuxprobe dev]# find sd*  ## 查看当前的硬盘及分区情况,两块同接口硬盘sda、sdb,sdb一个主分区sdb1
sda
sda1
sda2
sdb
sdb1
[root@linuxprobe dev]# fdisk /dev/sdb  ## 对/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): p  ## 查看分区,一个分区

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd55cd25f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6293503     3145728   83  Linux

Command (m for help): n  ## 新增分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p  ## 选择p,新增主分区
Partition number (2-4, default 2): 2  ## 选择主分区编号2
First sector (6293504-41943039, default 6293504):  ## 定义起始的扇区位置,默认敲回车键即可
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +5G  ## 定义分区结束的扇区位置,实际是定义分区大小,输入+5G,新的分区5G
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p ## 输入p查看分区情况,可见新增了一个分区

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd55cd25f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6293503     3145728   83  Linux
/dev/sdb2         6293504    16779263     5242880   83  Linux

Command (m for help): w  ## 输入w保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@linuxprobe dev]# pwd  ## 查看当前路径
/dev
[root@linuxprobe dev]# find sd*  ## 查看硬盘及分区情况,发现新增的分区未及时同步
sda
sda1
sda2
sdb
sdb1
[root@linuxprobe dev]# partprobe  ## 这个命令雷士刷新
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# find sd*  ## 再次查看硬盘及分区情况,新的分区已经同步
sda sda1 sda2 sdb sdb1 sdb2 [root@linuxprobe dev]# mkswap /dev/sdb2 ## 使用swap分区专用的格式化命令mkswap进行格式话 mkswap: /dev/sdb2: warning: wiping old swap signature. Setting up swapspace version 1, size = 5242876 KiB no label, UUID=b9fd91b4-b51e-4e9f-81c1-0d1446820dd7 [root@linuxprobe dev]# free -h ## 查看未挂载前swap分区大小 total used free shared buffers cached Mem: 1.9G 1.3G 686M 9.2M 1.7M 549M -/+ buffers/cache: 750M 1.2G Swap: 2.0G 0B 2.0G [root@linuxprobe dev]# swapon -s ## 查看当前swap分区 Filename Type Size Used Priority /dev/dm-0 partition 2097148 0 -1 [root@linuxprobe dev]# swapon /dev/sdb2 ## 使用swapon命令把准备好的swap分区设备正式挂载到系统上 [root@linuxprobe dev]# free -h ## 查看swap分区大小,发现已经曾加了5G total used free shared buffers cached Mem: 1.9G 1.3G 682M 9.2M 1.7M 549M -/+ buffers/cache: 754M 1.2G Swap: 7.0G 0B 7.0G [root@linuxprobe dev]# swapon -s ## 查看当前swap分区 Filename Type Size Used Priority /dev/dm-0 partition 2097148 0 -1 /dev/sdb2 partition 5242876 0 -2

将挂载后的swap分区写入开机自动启动,修改 /etc/fstab配置文件

[root@linuxprobe dev]# cat /etc/fstab  ## 查看修改前情况

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
[root@linuxprobe dev]# echo -e "/dev/sdb2\tswap\tswap\tdefaults\t0\t0" >> /etc/fstab  ## 修改,也可以使用vim编辑器
[root@linuxprobe dev]# cat /etc/fstab  ## 查看修改后 /etc/fstab文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0

 

 

3、创建文件来做swap分区

[root@linuxprobe mnt]# cd /mnt/  ## 选择一个目录用于创建swap文件
[root@linuxprobe mnt]# ls
sdb1test
[root@linuxprobe mnt]# mkdir fileswap  ## 创建放置swap文件的目录
[root@linuxprobe mnt]# ls
fileswap  sdb1test
[root@linuxprobe mnt]# cd fileswap/
[root@linuxprobe fileswap]# ls
[root@linuxprobe fileswap]# dd if=/dev/zero bs=1M count=2048 of=swapfile  ## 创建大小为2G的swap文件
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 12.6299 s, 170 MB/s
[root@linuxprobe fileswap]# ll -h  ## 查看
total 2.0G
-rw-r--r--. 1 root root 2.0G Oct 24 13:46 swapfile
[root@linuxprobe fileswap]# mkswap swapfile  ## 使用mkswap命令对创建的swap文件进行格式话
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=64e6c009-9e1e-4e23-9881-28ec85f13ef0
[root@linuxprobe fileswap]# ll -h  ## 查看,没啥变化
total 2.0G
-rw-r--r--. 1 root root 2.0G Oct 24 13:51 swapfile
[root@linuxprobe fileswap]# chmod 600 swapfile  ## 修改权限 ??
[root@linuxprobe fileswap]# ll -h  ## 查看
total 2.0G
-rw-------. 1 root root 2.0G Oct 24 13:51 swapfile
[root@linuxprobe fileswap]# free -h  ## 查看挂载前的swap分区空间大小
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.9G        73M       9.2M       220K       1.1G
-/+ buffers/cache:       761M       1.2G
Swap:         7.0G         0B       7.0G
[root@linuxprobe fileswap]# swapon -s  ## 查看挂载前的swap分区
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
[root@linuxprobe fileswap]# swapon swapfile   ## 挂载创建的swap分区
[root@linuxprobe fileswap]# free -h   ## 查看挂载后的swap分区大小,发现增加了2G
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.9G        71M       9.2M       220K       1.1G
-/+ buffers/cache:       763M       1.2G
Swap:         9.0G         0B       9.0G
[root@linuxprobe fileswap]# swapon -s   ## 查看挂载后swap分区,多出一个分区
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
/mnt/fileswap/swapfile                  file    2097148 0       -3

设置为开机自动启动,修改/etc/fstab配置文件:

[root@linuxprobe fileswap]# cat /etc/fstab  ## 查看修改前的配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
[root@linuxprobe fileswap]# echo -e "/mnt/fileswap/swapfile\tswap\tswap\tdefaults\t0\t0" >> /etc/fstab  ## 修改配置文件,也可以使用vim编辑器
[root@linuxprobe fileswap]# cat /etc/fstab  ## 查看修改后的配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
/mnt/fileswap/swapfile  swap    swap    defaults        0       0

 

4、如何删除swap分区

首先删除开启自动启动的配置文件对应行:

[root@linuxprobe fileswap]# cat /etc/fstab  ## 首先查看配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0
/dev/sdb2       swap    swap    defaults        0       0
/mnt/fileswap/swapfile  swap    swap    defaults        0       0
[root@linuxprobe fileswap]# wc -l /etc/fstab ## 统计行数,一共15行
15 /etc/fstab
[root@linuxprobe fileswap]# sed '14,15d' /etc/fstab -i  ## 删除最后两行(这里根据swap分区所在行数而定),也可以使用vim编辑器删除
[root@linuxprobe fileswap]# cat /etc/fstab ## 查看删除效果

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# 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
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
/dev/sdb1       /mnt/sdb1test   xfs     defaults        0       0

 

使用swapoff命令删除交换分区

[root@linuxprobe mnt]# swapon -s  ## 查看删除前的分区
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
/mnt/fileswap/swapfile                  file    2097148 0       -3
[root@linuxprobe fileswap]# free -h  ## 查看删除前交换分区大小
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.9G        71M       9.2M       220K       1.1G
-/+ buffers/cache:       763M       1.2G
Swap:         9.0G         0B       9.0G
[root@linuxprobe mnt]# swapoff /mnt/fileswap/swapfile  ## 使用swapoff 删除一个交换分区
[root@linuxprobe mnt]# swapon -s  ## 查看交换分区,已经少了一个交换分区
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1
/dev/sdb2                               partition       5242876 0       -2
[root@linuxprobe mnt]# free -h  ## 查看删除后交换分区大小,减少了2G
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.9G        72M       9.2M       220K       1.1G
-/+ buffers/cache:       761M       1.2G
Swap:         7.0G         0B       7.0G
[root@linuxprobe mnt]# swapoff /dev/sdb2  ## 删除 /dev/sdb2分区
[root@linuxprobe mnt]# swapon -s  ## 产看分区
Filename                                Type            Size    Used    Priority
/dev/dm-0                               partition       2097148 0       -1
[root@linuxprobe mnt]# free -h ## 减少了5G
             total       used       free     shared    buffers     cached
Mem:          1.9G       1.9G        76M       9.2M       220K       1.1G
-/+ buffers/cache:       757M       1.2G
Swap:         2.0G         0B       2.0G

 

删除/dev/sdb2磁盘分区

[root@linuxprobe mnt]# cd /dev  ## 切换目录
[root@linuxprobe dev]# find sdb*  ## 查看当前的磁盘分区
sdb
sdb1
sdb2
[root@linuxprobe dev]# fdisk /dev/sdb  ## 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): p ## 查看,共两个磁盘分区

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd55cd25f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6293503     3145728   83  Linux
/dev/sdb2         6293504    16779263     5242880   83  Linux

Command (m for help): d  ## 输入删除选项
Partition number (1,2, default 2): 2 ## 选择2
Partition 2 is deleted

Command (m for help): p  ## 查看磁盘分区,已剩一个磁盘分区

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xd55cd25f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     6293503     3145728   83  Linux

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 with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@linuxprobe dev]# pwd
/dev
[root@linuxprobe dev]# find sdb*  ## 查看 /dev/sdb磁盘分区
sdb
sdb1
sdb2
[root@linuxprobe dev]# partprobe  ## 刷新
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# find sdb*  ## /dev/sdb2已经删除
sdb
sdb1

 

posted @ 2020-10-24 15:20  小鲨鱼2018  阅读(3212)  评论(0编辑  收藏  举报