我创建了一个交换文件,如下所述:https://wiki.archlinux.org/index.php/Swap#Swap_file_creation
当我尝试休眠时出现以下错误:
$ echo disk > /sys/power/state
echo: write error: no such device
同样在 dmesg 中,我得到:
[30721.352822] [drm] Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.
[30721.454735] acpi LNXPOWER:07: Turning OFF
[30721.454987] acpi LNXPOWER:02: Turning OFF
[30721.455365] acpi LNXPOWER:01: Turning OFF
[30721.455563] PM: Cannot find swap device, try swapon -a
[30721.455563] PM: Cannot get swap writer
[30721.553943] OOM killer enabled.
[30721.553944] Restarting tasks ... done.
如中所述https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate#Hibernation_into_swap_file,我设置了以下内核参数:
resume=UUID=2bfb4ccd-6b80-4806-8a08-4f1e67ef035f
resume_offset=148378880
以下是获取resume_offset 和resume 参数值的方法:
$ filefrag -v /swapfile
Filesystem type is: 9123683e
File size of /swapfile is 536870912 (131072 blocks of 4096 bytes)
ext: logical_offset: physical_offset: length: expected: flags:
0: 0.. 0: 148378880.. 148378880: 1:
1: 1.. 131071: 148378881.. 148509951: 131071: last,unwritten,eof
$ findmnt -no SOURCE,UUID -T /swapfile
/dev/mapper/nvme0n1p2[/root] 2bfb4ccd-6b80-4806-8a08-4f1e67ef035f
当我尝试使用 时swap_offset
,出现以下问题:
$ swap-offset /swapfile
ioctl(FIBMAP) failed: Invalid argument
那么在使用 BTRFS 时是否可以使用交换文件进行休眠?看来内核不知道交换文件的正确位置。
答案1
你/笑话(https://www.reddit.com/user/kjoke/)让我很高兴:
filefrag 错误地计算了 BTRFS 的偏移量,但有一个脚本可以计算正确的偏移量:https://github.com/osandov/osandov-linux/blob/master/scripts/btrfs_map_physical.c
下载并gcc btrfs_map_physical.c -o btrfs_map_physical
获取交换文件的第一个物理偏移量:
$ sudo ./btrfs-map-physical /swap | head -n2
FILE OFFSET EXTENT TYPE LOGICAL SIZE LOGICAL OFFSET PHYSICAL SIZE DEVID PHYSICAL OFFSET
0 regular 4096 607759892480 268435456 1 608833634304
这里:608833634304
。将此偏移量除以页面大小:
$ getconf PAGESIZE
4096
所以resume_offset
是608833634304 / 4096 = 148641024
.添加此值与resume
参数作为内核参数并重新启动。之后systemd hibernate
仍然会失败,因为 systemd 忽略内核参数并错误地计算偏移量,但echo disk > /sys/power/state
可以工作。