如何在 Ubuntu 18.04 中增加交换文件?

如何在 Ubuntu 18.04 中增加交换文件?

我有一台配备 8 GB RAM 和 1TB HDD 的笔记本电脑。我的交换文件为 2 GB(Ubuntu 18.04 默认使用交换文件而不是单独的交换分区),我想增加它以使用休眠功能。

我想将其从 2 GB 增加到 16 GB。以下是 GParted 的屏幕截图:

GParted 屏幕截图

我尝试增加它fallocate -l 16G但没有成功。

另外还有以下图片free -m

<code>免费</code> 输出

答案1

从 Ubuntu 18.04 开始,使用交换文件而不是专用交换分区(使用 LVM 时除外)。交换文件名为swapfile。要更改此交换文件的大小:

  1. 禁用交换文件并将其删除(实际上没有必要,因为您将覆盖它)

    sudo swapoff /swapfile
    sudo rm  /swapfile
    
  2. 创建所需大小的新交换文件。感谢用户 Hackinet,您可以使用以下命令创建 4 GB 交换文件

    sudo fallocate -l 4G /swapfile
    

    在此命令中,调整4G到您想要的大小。

    或者,您可以使用dd,但输入正确的参数需要进行一些计算。如果您想创建一个 4 GB 的交换文件,则需要写入 4 * 1024 个 1024 2字节(= 1 MiB)的块。这将使您的计数等于 4 * 1024 = 4096。使用以下命令创建此大小的文件

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
    
  3. 仅为 root 分配读/写权限(不是严格需要,但它可以加强安全性)

    sudo chmod 600 /swapfile
    
  4. 将文件格式化为交换文件:

    sudo mkswap /swapfile
    
  5. 该文件将在下次重启时激活。如果要为当前会话激活它:

    sudo swapon /swapfile
    

您可以使用命令检查可用的交换swapon -s(无需 root 权限)。

答案2

建议man mkswap使用dd如下命令@vanadium 帖子

If  you  don't  know  the  page  size  that  your  machine uses, 
you may be able to look it up with 
"cat /proc/cpuinfo" 
(or you may not – the contents of this file depend on architecture and kernel version).

   To set up a swap file, it is necessary to create that file before   
   initializing  it  with  mkswap,  e.g. using a command like

          # fallocate --length 8GiB swapfile

   Note  that  a  swap  file must not contain any holes.  Using cp(1) to  
   create the file is not acceptable.
   Neither is use of fallocate(1) on file systems that support preallocated 
   files, such as XFS or ext4,  or on  copy-on-write  filesystems like btrfs.  

   It is recommended to use dd(1) and /dev/zero in these cases.
   Please read notes from swapon(8) before adding a swap file to copy-on- 
   write filesystems.

这里的笔记man swapon

NOTES
       You should not use swapon on a file with holes.  This can be seen in
       the system log as

              swapon: swapfile has holes.

       The swap file implementation in the kernel expects to be able to write  
       to the file directly, without the assistance  of the filesystem.  This 
       is a problem on preallocated files (e.g.  fallocate(1)) on filesys‐
       tems like XFS or ext4, and on copy-on-write filesystems like btrfs.

       It is recommended to use dd(1) and /dev/zero to avoid holes on XFS
       and ext4.

       swapon may not work correctly when using a swap file with some  
       versions of btrfs.  This is due to  btrfs being  a copy-on-write 
       filesystem: the file location may not be static and corruption can 
       result.  
       Btrfs actively disallows the use of swap files on its filesystems
       by refusing to map the file.

       One possible workaround is to map the swap file to a loopback device.  
       This will allow the filesystem to determine the mapping properly but  
       may come with a performance impact.

       Swap over NFS may not work.

相关内容