Adding Swap Files

 Adding Swap Files  

If you do not have free disk space to create a swap partition and you do need to add swap space urgently, you can use a swap file as well. From a performance perspective, it does not even make that much difference if a swap file is used instead of a swap device such as a partition or a logical volume, and it may help you fixing an urgent need in a timely manner.  (如果没有多余的磁盘空间来增加交换分区,可使用交换文件代替。从性能方面来看,swap file跟swap device没多少区别)

To add a swap file, you need to create the file first. The  dd if=/dev/zero of=/ swapfile bs=1M count=100  command would add 100 blocks with a size of 1 Mebibyte from the /dev/zero device (which generates 0s) to the /swapfile file. The result is a 100 MiB file that can be configured as swap. To do so, you can follow the same procedure as for swap partitions. First use  mkswap /swapfile  to mark the file as a swap file, after which you can use  swapon /swapfile  to active it. 

例如:把原来的swap空间大小从2147MB添加到2247MB

[root@rhel7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1840          91        1603           8         144        1605
Swap:          2147           0        2147
[root@rhel7 /]# touch swapfileTest
[root@rhel7 /]# dd if=/dev/sda of=/swapfileTest bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 1.45588 s, 72.0 MB/s
[root@rhel7 /]# mkswap /swapfileTest 
mkswap: /swapfileTest: warning: don't erase bootbits sectors
        (dos partition table detected). Use -f to force.
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=c011c102-304a-4cb2-8106-2c69500a82a8
[root@rhel7 /]# swapon /swapfileTest 
swapon: /swapfileTest: insecure permissions 0644, 0600 suggested.    -----有提示,需要设置权限#chmod 0600 /swapfileTest
[root@rhel7 /]# free -m --比原来的2147添加的100MB
              total        used        free      shared  buff/cache   available
Mem:           1840          91        1398           8         350        1598
Swap:          2247           0        2247
[root@rhel7 /]# 

 将配置写入到etc/fstab文件中,否则重启系统后不生效:

# vi  /etc/fstab,增加如下行

/swapfileTest             swap          swap    defaults        0 0  

posted on 2016-08-04 10:47  遠離塵世の方舟  阅读(247)  评论(0编辑  收藏  举报

导航