使用交換檔案透過 BTRFS 和核心 5.0.16-gentoo 進行休眠

使用交換檔案透過 BTRFS 和核心 5.0.16-gentoo 進行休眠

我建立了一個交換文件,如下所述: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/Sus​​​​pend_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_offset608833634304 / 4096 = 148641024.新增此值與resume參數作為核心參數並重新啟動。之後systemd hibernate仍然會失敗,因為 systemd 忽略內核參數並錯誤地計算偏移量,但echo disk > /sys/power/state可以工作。

相關內容