Linux CentOS 8(交换分区的添加与管理)



目录

  • 一、项目介绍
  • 二、相关概念
  • 2.1 交换分区
  • 2.2 交换分区的大小设置
  • 2.3 交换分区对性能的影响
  • 三、任务操作
  • 任务1 交换分区的添加



一、项目介绍

本节将介绍Linux(Centos 8)中磁盘中交换分区的添加与管理。

二、相关概念

2.1 交换分区

Linux 系统中的交换分区是当物理内存(RAM)被充满时,作为物理内存的缓存来使用。当系统需要更多的内存资源,而物理内存已经充满,内存中不活跃的页就会被移动到交换分区上。交换分区位于硬盘上,所以它的存取速度比物理内存要慢。

一般情况下,交换分区的大小应当相当于计算机内存的两倍,但不能超过2048MB(2GB)。

2.2 交换分区的大小设置

Windows系统中交换分区(虚拟内存)直接放在系统磁盘上,且是自动管理的,但Linux则不同,它必须使用独立的分区(独立的文件系统)作为交换分区(swap)。在不同的情况下,交换分区的大小的设定机制是不同的:

  1. 批处理类的服务器(科学计算类的服务器):交换分区尽可能大,比如为物理内存的2倍或4倍;
  2. 应用程序类的服务器(提供web服务/数据库服务器):对性能要求较高,故交换分区尽可能小,比如1G;
  3. 如果无法确定是科学计算类还是应用程序类的服务器,当物理内存小于或等于2G,那么 swap = memory * 2;
    当物理内存大于2G,小于4G,那么 swap = memory * 1.5;
    当物理内存大于4G,那么 swap = 4G。

2.3 交换分区对性能的影响

(1)优点:

  • 当内存完全填满时提供溢出空间
  • 可以将很少需要的程序从高速内存中移开
  • 允许休眠

(2)缺点:

  • 占用硬盘空间,因为 SWAP 分区不会动态调整大小
  • 会增加硬盘的磨损
  • 不一定能提高性能

三、任务操作

任务1 交换分区的添加

1.1 查看在硬盘上的交换分区

[root@localhost ~]# free -m
total   used     free      shared  buff/cache   available
Mem:    1536     232       970       24  333       1133
Swap:   2047      0        2047

1.2 列出所有可用块设备的信息

[root@localhost ~]# 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   19G  0 part 
  ├─cl-root 253:0    0   17G  0 lvm  /
  └─cl-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
└─sdb1        8:17   0    1G  0 part 
sr0          11:0    1  7.7G  0 rom

1.3 创建交换分区

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xce772a98.

Command (m for help): n                   //创建新的分区

Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
   
Select (default p): p                    //分区类型为主分区

Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): p                  //查看分区信息

Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0xce772a98
Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 4196351 4194304   2G 83 Linux

Command (m for help): t                    //更改分区ID

Selected partition 1

Hex code (type L to list all codes): L     //查看可选项

0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 ……

Hex code (type L to list all codes): 82   //Linux swap的16进制编码为82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p                  //再次查看分区情况

Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0xce772a98
Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 4196351 4194304   2G 82 Linux swap / Solaris

Command (m for help): w                 //保存退出

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

1.4 mkswap命令进行格式化

[root@localhost ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=da23a585-f715-4023-a902-34df48b186b1

1.5 把准备好的SWAP交换分区设备正式的挂载到系统中,并查看。

[root@localhost ~]# free -m
         total      used     free    shared  buff/cache   available
Mem:     1536       212      1076        8         248        1170
Swap:    2047       0        2047
[root@localhost ~]# swapon /dev/sdb1    //启动交换分区
[root@localhost ~]# free -m
        total      used      free    shared  buff/cache   available
Mem:    1536        213      1073        8         249        1168
Swap:   4095        0        4095

1.6 设置开机启动自动挂载

[root@localhost ~]# vim /etc/fstab 
[root@localhost ~]# cat /etc/fstab 
# /etc/fstab
# Created by anaconda on Thu Jun  3 22:25:51 2021
# 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.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=ad229ff4-8126-495a-b98f-954230112e1a /boot                   ext4    defaults        1 2
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
/dev/sdb1             swap      swap       defaults   0        0
# 要挂载的文件系统  	挂载点    文件系统类型     默认    不备份   不自检

1.7 禁止所有交换分区的命令

[root@localhost ~]# swapoff -a 
[root@localhost ~]# free -m
         total   used      free    shared  buff/cache   available
Mem:     1536     220       959        8        356        1157
Swap:             0           0           0