freebsd-src icon indicating copy to clipboard operation
freebsd-src copied to clipboard

glabel: Add support for detecting Linux swap

Open ricardobranco777 opened this issue 1 year ago • 5 comments

This will benefit dual-boot installations and also live-CD's.

How I tested it:

  1. Compile kernel without GEOM_LABEL
  2. cd /sys/modules/geom/geom_label
  3. make
  4. sudo make install
  5. Reboot
  6. Attach virtual disk to VM.
  7. echo -e 'n\np\n1\n\n\nt\n82\nw' | sudo chroot /compat/linux fdisk /dev/vtbd1 to create a Linux swap partition (type 82)
  8. sudo chroot /compat/linux mkswap -L MYSWAP /dev/vtbd1s1
  9. sudo sh -c 'echo geom_label_load=\"YES\" >> /boot/loader.conf'
  10. Reboot
  11. sudo gnop create -v -o 4096 /dev/swaplinux/MYSWAP
  12. sudo swapon /dev/swaplinux/MYSWAP.nop
  13. swapinfo | grep MYSWAP

Cleanup:

  1. sudo swapoff /dev/swaplinux/MYSWAP.nop
  2. sudo gnop destroy /dev/swaplinux/MYSWAP.nop

For testing the mount patch that does the gnop dance automatically:

  1. cd /usr/src/sbin/swapon
  2. make
  3. sudo make install
  4. sudo swapon /dev/swaplinux/MYSWAP
  5. swapinfo |grep MYSWAP
  6. sudo swapoff /dev/swaplinux/MYSWAP

The commands for using gnop can be automated with this patch to swapon: https://github.com/freebsd/freebsd-src/pull/1084

ricardobranco777 avatar Jan 23 '24 21:01 ricardobranco777

What is the purpose of the labeling?

To detect labelled Linux swap partitions that we can reuse.

Note that you cannot use Linux swap partition for FreeBSD swap/core dump as is because this would potentially damage the Linux' swap header, unless I miss something that magically prevents the corruption.

I didn't notice any corruption but I can stress test it further by filling the swap and checking the headers.

swapon on Linux calls mkswap if needed anyway: https://github.com/util-linux/util-linux/blob/master/sys-utils/swapon.c#L330

If I am right, you need to chomp the first page from the volume (by some intermediate geom).

Maybe you're right as the size of the partition shown by fdisk is the same as the Capacity shown by swapinfo.

How can I fix that?

ricardobranco777 avatar Jan 24 '24 15:01 ricardobranco777

What is the purpose of the labeling?

To detect labelled Linux swap partitions that we can reuse.

Note that you cannot use Linux swap partition for FreeBSD swap/core dump as is because this would potentially damage the Linux' swap header, unless I miss something that magically prevents the corruption.

I didn't notice any corruption but I can stress test it further by filling the swap and checking the headers.

Perhaps. Did you see the swapping at all?

swapon on Linux calls mkswap if needed anyway: https://github.com/util-linux/util-linux/blob/master/sys-utils/swapon.c#L330

Yes, but damaging the header would make this label not appear on next taste, unless Linux boot repaired the damage.

If I am right, you need to chomp the first page from the volume (by some intermediate geom).

Maybe you're right as the size of the partition shown by fdisk is the same as the Capacity shown by swapinfo.

How can I fix that?

This is not completely trivial, you need to create an intermediate provider that does the translation for size and block number for io. I do not have a good example other than gpart.

kostikbel avatar Jan 25 '24 11:01 kostikbel

This is not completely trivial, you need to create an intermediate provider that does the translation for size and block number for io. I do not have a good example other than gpart.

This can be done with gnop to skip the first 4096 bytes like this:

sudo gnop create -v -o 4096 /dev/swaplinux/MYSWAP sudo swapon /dev/swaplinux/MYSWAP.nop

ricardobranco777 avatar Jan 25 '24 19:01 ricardobranco777

Will submit a patch for swapon(8) that recognizes Linux swap to call gnop(8).

ricardobranco777 avatar Jan 25 '24 21:01 ricardobranco777

This is a weird thing. Assume I as user created a gnop device and want to start the swapping on it. First, your patch would then lay yet another gnop provider on top of the underlying provider on swapon. On swapoff, the gnop provider is destroyed, and it could be mine instead of newly created.

I'm aware of it and I plan to fix this in another PR to swapon.

ricardobranco777 avatar Jan 26 '24 09:01 ricardobranco777

Ping? What's the status of these updates?

bsdimp avatar Apr 23 '24 21:04 bsdimp

Re-reading everything, it's clear there's not consensus for this approach. Should that be worked out, please submit a new pull request.

bsdimp avatar Apr 24 '24 03:04 bsdimp

@kostikbel @bsdimp If I'm reading https://github.com/freebsd/freebsd-src/blob/main/sys/vm/swap_pager.c#L2432 right, FreeBSD already skips the first blocks to "avoid overwriting avoid overwriting any bsd label at the front of the partition". Much like NetBSD does. Can you please confirm?

ricardobranco777 avatar Apr 26 '24 17:04 ricardobranco777

I'm 96.5% sure FreeBSD skips the first page like NetBSD.

I opened a new version with the only difference being s/4096/PAGE_SIZE to accomodate for other architectures (on Linux, x86_64 & s390x use 4096, ppc64le uses 65536 & aarch64 uses 16384).

https://github.com/freebsd/freebsd-src/pull/1205

ricardobranco777 avatar Apr 26 '24 18:04 ricardobranco777