交换文件交换无效参数

交换文件交换无效参数

我从交换文件中进行交换已经工作了相当长一段时间,但由于某种原因它停止工作了。

sudo fallocate -l 4G /home/.swap/swapfile
sudo chmod 600 /home/.swap/swapfile
sudo mkswap /home/.swap/swapfile

# /etc/fstab
/home/.swap/swapfile swap swap defaults 0 0

sudo swapon -a

swapon: /home/.swap/swapfile: swapon failed: Invalid argument

我正在运行最新版本的 Fedora,那么更新后是否可能发生某些变化或者可能是什么原因?

答案1

请尝试更换

fallocate -l 4G /home/.swap/swapfile

dd if=/dev/zero of=/home/.swap/swapfile bs=1M count=4096

答案2

如果您正在使用BTFS你应该创建你的交换文件没有写时复制。这是用 完成的chattr +C。在向文件写入任何内容之前设置此属性非常重要:

truncate -s 0 swapfile
chattr +C swapfile
fallocate -l 2G swapfile
chmod 0600 swapfile
mkswap swapfile
swapon swapfile

/etc/fstab使用此条目将其添加到:

/path/swapfile        none        swap        defaults      0 0

我发现它记录在优良的材料

答案3

建立在接受的答案并附有解释:引用自交换(8)

有孔的锉刀

    内核中的交换文件实现期望能够直接写入文件,而不需要文件系统的帮助。对于有漏洞的文件或 Btrfs 等文件系统上的写时复制文件来说,这是一个问题。

    cp(1) 或 truncate(1) 等命令会创建有漏洞的文件。这些文件将被 swapon 拒绝。

    由fallocate(1) 创建的预分配文件也可能被解释为有漏洞的文件,具体取决于文件系统。  自 Linux 4.18 起,XFS 支持预分配的交换文件。

    创建交换文件的最便携的解决方案是使用 dd(1) 和/dev/zero.

斜体字似乎解释了一切。不幸的是,即使有详细的输出,也 swapon没有提及失败的原因(有漏洞的文件)。

答案4

这是一个很老的问题,但我刚刚遇到了同样的问题,这里讨论的任何内容都不适合我,但我在 btrfs 文件系统手册页中发现了这一点:

mkswapfile [-s size] file
       Create a new file that's suitable and formatted as a swapfile. Default size is  2GiB,  fixed  page
       size 4KiB, minimum size is 40KiB.

       A  swapfile  must  be  created  in a specific way: NOCOW and preallocated.  Subvolume containing a
       swapfile cannot be snapshotted and blocks of an activated swapfile cannot be balanced.

       Swapfile creation can be achieved by standalone commands too. Activation needs to be done by  com‐
       mand  swapon(8). See also command btrfs inspect-internal map-swapfile and the Swapfile feature de‐
       scription.

       NOTE:
          The command is a simplified version of 'mkswap', if you want to set label, page size, or  other
          parameters please use 'mkswap' proper.

       Options

       -s|--size SIZE
              Create swapfile of a given size SIZE (accepting k/m/g/e/p suffix).

       -U|--uuid UUID
              specify  UUID  to use, or a special value: clear (all zeros), random, time (time-based ran‐
              dom)

所以我这样做了:

sudo btrfs filesystem mkswapfile --size 12g --uuid clear /swapfile/path

而且效果很好

显然你的文件系统必须是 btrfs,如果我没记错的话,这是 Fedora 中的默认文件系统

相关内容