Re: Hibernation into swap file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, May 5, 2019 at 3:09 AM Andrei Borzenkov <arvidjaar@xxxxxxxxx> wrote:
>
> 05.05.2019 10:50, Maksim Fomin пишет:
> > Good day.
> >
> > Since 5.0 btrfs supports swap files. Does it support hibernation into
> > a swap file?
> >
> > With kernel version 5.0.10 (archlinux) and btrfs-progs 4.20.2
> > (unlikely to be relevant, but still) when I try to hibernate with
> > systemctl or by directly manipulating '/sys/power/resume' and
> > '/sys/power/resume_offset', the kernel logs:
> >
> > PM: Cannot find swap device, try swapon -a PM: Cannot get swap
> > writer
> >
>
> How exactly do you compute resume_offset? What are exact commands you
> ise to initiate hibernation? systemctl will likely not work anyway as
> systemd is using FIEMAP which returns logical offset of file extents in
> btrfs address space, not physical offset on containing device. You will
> need to jump from extent vaddr to device offset manually.


https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/power/swsusp-and-swap-files.txt?h=v5.0.13

This says the  resume_offset= is an "offset, in <PAGE_SIZE> units,
from the beginning of the partition which holds the swap file"

Use filefrag (uses FIEMAP) to get the virtual address, multiply by
4KIB to get bytes, and plug that into

btrfs-map-logical -l vaddrbyte <dev>

The physical number returned is also in bytes. Normally to get LBA
you'd divide by 512 (for anything other than a 4Kn drive), but if I
understand the kernel document correctly, it needs to be in x86_64
page size, so divide by 4096 instead.



> > After digging into this issue I suspect that currently btrfs does not
> > support hibernation into swap file (or does it?). Furthermore, I
> > suspect that kernel mechanism of accessing swap file via
> > 'resume_offset' is unreliable in btrfs case because it is more likely
> > (comparing to other fs) to move files across filesystem which
> > invalidates swap file offset, so the whole idea is dubious. So,
> >
> > 1) does btrfs supports hibernation into swap file? 2) is there
> > mechanism to lock swap file?
> >
>
> btrfs performs some checks before allowing file as swap and it also
> implicitly locks it during swap usage. Note that btrfs will disable
> snapshots of subvolume with swapfile.

Yes I think it's probably a best practice to have a "swaps" subvolume
created at the top level of the file system, to make sure contained
swapfiles are separate and immune from snapshotting the top level
subvolume itself. It's also reasonable to set chattr +C on that
subvolume, and files created there will inherit it.


>
> File also must be using single allocation profile. There is no way to
> force allocation profile for individual file I am aware of in case
> filesytem defaults to something else. Also for multi-device btrfs I am
> not aware of any way to force allocation of file on specific physical
> device, and this is required to use file as swap on btrfs. So you are
> likely restricted to single device single data profile.

Yes the man page specifically says it's supported only for single
device volumes.

> Finally you are not allowed to mount any filesystem read-write (or
> replay any pending log/journal) before doing resume. Does btrfs support
> "true" read-only mount, which is guaranteed to not change filesystem
> state in any way?

Resume is done by the kernel just after initramfs is unpacked, well
before the file system is ro mounted. I think it's probably a good
idea to make sure bootloader kernel command line contains the 'ro'
parameter. I don't know if Btrfs ro is guaranteed to change on-disk
metadata, I know logreplay affects the in-memory state of Btrfs but I
don't think it changes on-disk metadata.

But yeah, worth testing and making sure the backups are kept current.


-- 
Chris Murphy




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux