linux系统中添加交换分区
什么是交换分区?
SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计的目的是为了解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。
交换分区能够划分多大?
在生产环境中,交换分区的大小一般为真实物理内存的1.5-2倍。
1、对/dev/sdb添加一个磁盘分区
[root@PC1linuxprobe dev]# 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: 0xe287b3a9
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-41943039, default 4196352): ## 此处回车
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
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: 0xe287b3a9
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 14682111 5242880 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.
2、查看添加的磁盘分区
[root@PC1linuxprobe dev]# find sd*
sda
sda1
sda2
sdb
sdb1
[root@PC1linuxprobe 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@PC1linuxprobe dev]# find sd*
sda
sda1
sda2
sdb
sdb1
sdb2
3、对磁盘分区进行交换分区格式化
[root@PC1linuxprobe dev]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=208a06d0-30e7-473a-b90a-bf920bf545a8
4、挂载
[root@PC1linuxprobe dev]# free -h
total used free shared buffers cached
Mem: 1.9G 1.1G 859M 10M 1.7M 293M
-/+ buffers/cache: 833M 1.1G
Swap: 2.0G 0B 2.0G
[root@PC1linuxprobe dev]# swapon /dev/sdb2
[root@PC1linuxprobe dev]# free -h
total used free shared buffers cached
Mem: 1.9G 1.1G 855M 10M 1.7M 293M
-/+ buffers/cache: 837M 1.1G
Swap: 7.0G 0B 7.0G
5、设为开机自动挂载
[root@PC1linuxprobe dev]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 5 15:23:01 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-root / xfs defaults 1 1
UUID=0ba20ae9-dd51-459f-ac48-7f7e81385eb8 /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/sdb1 /devide xfs defaults 0 0
[root@PC1linuxprobe dev]# echo -e "/dev/sdb\tswap\tswap\tdefaults\t0\t0" >> /etc/fstab
[root@PC1linuxprobe dev]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Nov 5 15:23:01 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-root / xfs defaults 1 1
UUID=0ba20ae9-dd51-459f-ac48-7f7e81385eb8 /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/sdb1 /devide xfs defaults 0 0
/dev/sdb swap swap defaults 0 0
总结,添加交换分区:
- fdisk交互命令进行磁盘分区:fdisk /dev/sdb
- swap格式化:mkswap /dev/sdb2
- swapon命令挂载:swapon /dev/sdb2
- 设置开机自动挂载:echo -e "/dev/sdb2\tswap\tswap\tdefaults\t0\t0" >> /dev/fstab
注:
- 交换分区大小通常设置为物理内存的1.5-2倍
- 真实内存耗尽才使用交换分区
- 交换分区格式化命令为:mkswap
- 交换分区挂载命令为:swapon /dev/disk* ,而且不需要指定挂载点
- 交换分区的默然挂载点为:swap
- 交换分区默认的文件系统为:swap