linux中的通过/swapfile创建交换分区
一、交换分区
交换(SWAP)分区是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。通俗来讲就是让硬盘帮内存分担压力。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。二、实际操作
检查硬盘驱动器分区上的可用空间[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 889M 72M 48M 857M 707M
Swap: 2.0G 4.0M 2.0G

[root@localhost ~]# dd if=/dev/sdb of=/swapfile count=1M bs=8200
1048576+0 records in
1048576+0 records out
8598323200 bytes (8.6 GB) copied, 21.2291 s, 405 MB/s
[root@localhost ~]# ls -lh /swapfile
-rw-r--r-- 1 root root 8.1G Dec 2 03:14 /swapfile
三、修改/swapfile权限
[root@localhost ~]# chmod 600 /swapfile
[root@localhost ~]# ls -lh /swapfile
-rw------- 1 root root 8.1G Dec 2 03:14 /swapfile
四、格式化交换分区
[root@localhost ~]# mkswap -f /swapfile
mkswap: /swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 8396796 KiB
no label, UUID=61513af5-49ca-44ea-a67a-095b3b345b72
五、启用交换分区
[root@localhost ~]# swapon /swapfile
#验证是否启用成功
[root@localhost ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 2G 8.3M -2
/swapfile file 8G 0B -3
六、添加交换分区到fstab
为了能够让新的交换分区设备在重启后依然生效,需要按照下面的格式将相关信息写入配置文件中,并记得保存:[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Sep 19 08:31:20 2023
#
# 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
#
UUID=30a0583b-93fb-4c3b-a2d6-1887ace2bc73 / xfs defaults 0 0
UUID=e45b4d30-e70f-4f57-a3d8-ad7cea71d3e4 /boot xfs defaults 0 0
UUID=da7d896d-eca9-41cd-bfbf-bc4f2e3c1ea5 swap swap defaults 0 0
UUID=4830a6fd-fcd6-4cfb-a113-db0cc79b4ba4 swap swap defaults 0 0
#这里采用UUID和绝对路径的方式进行挂载都可以
#/swapfile swap swap defaults 0 0
写入到/etc/fstab文件中的设备信息并不会立即生效,需要使用mount -a参数进行自动挂载:
[root@localhost ~]# mount -a
#重新挂载以后,看下是否生效
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 818M 68M 57M 931M 780M
Swap: 10G 8.3M 9G
文章目录
关闭
共有 0 条评论