freebsd-src
freebsd-src copied to clipboard
glabel: Add support for detecting Linux swap
This will benefit dual-boot installations and also live-CD's.
How I tested it:
- Compile kernel without GEOM_LABEL
-
cd /sys/modules/geom/geom_label
-
make
-
sudo make install
- Reboot
- Attach virtual disk to VM.
-
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) -
sudo chroot /compat/linux mkswap -L MYSWAP /dev/vtbd1s1
-
sudo sh -c 'echo geom_label_load=\"YES\" >> /boot/loader.conf'
- Reboot
-
sudo gnop create -v -o 4096 /dev/swaplinux/MYSWAP
-
sudo swapon /dev/swaplinux/MYSWAP.nop
-
swapinfo | grep MYSWAP
Cleanup:
-
sudo swapoff /dev/swaplinux/MYSWAP.nop
-
sudo gnop destroy /dev/swaplinux/MYSWAP.nop
For testing the mount patch that does the gnop dance automatically:
-
cd /usr/src/sbin/swapon
-
make
-
sudo make install
-
sudo swapon /dev/swaplinux/MYSWAP
-
swapinfo |grep MYSWAP
-
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
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?
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 callsmkswap
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 byswapinfo
.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.
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
Will submit a patch for swapon(8) that recognizes Linux swap to call gnop(8).
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.
Ping? What's the status of these updates?
Re-reading everything, it's clear there's not consensus for this approach. Should that be worked out, please submit a new pull request.
@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?
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