Linux 5.7.2 swapfile 无法挂载的解决方案

本文最后更新于 2020-06-12,文中内容可能已过时。


[FAILED] Failed to activate swap /swapfile.
[DEPEND] Dependency failed for Swap.

开机时报错如上, 开机之后交换文件无法正确被挂载.

在Arch Linux论坛上找到了解决方案如下:

SOLVED: Swap not working with Kernel 5.7.2

这个问题仅出现在ext4文件系统下的 Linux 5.7.2 上, 使用fallocate 创建的swap交换文件.

Arch Linux Wiki的解释如下:

Note: dynamic space allocation such as using fallocate is not supported, as it causes problems with some file systems such as F2FS[1] and will likely fail to activate at boot time with error “swapon: swapfile has holes” as of kernel 5.7. Hence, contiguous allocation, such as dd, is the only reliable way to allocate a swap file.[2]

因此删掉原有的swapfile, 用dd重新分配即可.

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
sudo chmod 600 /swapfile 
sudo mkswap /swapfile 
sudo swapon /swapfile