利用文件来扩展交换分区,增减了灵活性,这种方法适用于硬盘中没有可分区的剩余空间,充分利用硬盘中的文件来提高系统的性能,也可以将文件拷贝到其它主机中使用。
扩充swap交换分区
新建swap交换文件
[[email protected] ~]# dd if=/dev/dd if=/dev/zero of=swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 7.34123 s, 146 MB/s
[[email protected] ~]# ls -lh /root/swapfile
-rw-r--r--. 1 root root 1.0G Aug 31 09:50 /root/swapfile
[[email protected] ~]# mkswap /root/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=b9990096-d55f-414d-a7e7-b16097af7088
修改fstab配置
[[email protected] ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:23:38 2016
#
# 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=80371af2-ee0e-481c-b71a-c9d25b11800a / xfs defaults 0 0
UUID=33974954-468b-490d-b84a-c955716d7146 /boot xfs defaults 0 0
UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir xfs defaults 0 0
UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap swap defaults 0 0
/root/swapfile swap swap defaults pri=1000 0
挂载文件
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
[[email protected] ~]# swapon -a #挂载时会报一个权限错误,需要将swapfile文件的权限修改为600
swapon: /root/swapfile: insecure permissions 0644, 0600 suggested.
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
/root/swapfile file 1048572 0 100
删除swap分区
取消激活swap分区
[[email protected] ~]# swapoff /root/swapfile
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3905532 0 -1
删除配置信息
[[email protected] ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 21 11:23:38 2016
#
# 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=80371af2-ee0e-481c-b71a-c9d25b11800a / xfs defaults 0 0
UUID=33974954-468b-490d-b84a-c955716d7146 /boot xfs defaults 0 0
UUID=346923cc-58f2-44b6-91d0-6ad8687ebd7b /testdir xfs defaults 0 0
UUID=981b1be0-81ec-46aa-bb2c-6d7f0d79aa46 swap swap defaults 0 0
/root/swapfile swap swap defaults pri=1000 0 #删除该行
删除文件
[[email protected] ~]# rm -f /root/swapfile