How to increase swap with ZFS on Ubuntu 20.04 to enable hibernation?
Preface
I made a mistake and installed Ubuntu 20.04 LTS with an encrypted ZFS / OpenZFS root
partition on my productive working desktop in a dual boot configuration (see partition table below).
But I didn't pay proper attention on the tutorial I was following and now it seems that I have a Swap partition of 4G although I have 32G of RAM (EDIT: I found out how I'm able to increase the Swap but I still can't go into hibernation — see below).
Therefore I cannot put this machine on hibernation.
What's the best way to solve this issue, so that I can put this machine into hibernation?
Current state
$ sudo fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 1.88 TiB, 2048408248320 bytes, 4000797360 sectors
Disk model: KXG60PNV2T04 NVMe KIOXIA 2048GB
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 1FCA11ED-9263-4C06-A8DF-594C0DE84AFA
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 1085439 1083392 529M Windows recovery environment
/dev/nvme0n1p2 1085440 1290239 204800 100M EFI System
/dev/nvme0n1p3 1290240 1323007 32768 16M Microsoft reserved
/dev/nvme0n1p4 1323008 409420488 408097481 194.6G Microsoft basic data
/dev/nvme0n1p5 409421824 410920959 1499136 732M Windows recovery environment
/dev/nvme0n1p6 410923008 415117311 4194304 2G Linux filesystem
/dev/nvme0n1p7 415117312 4000797326 3585680015 1.7T Linux filesystem
$ sudo swapon --show --output all
NAME TYPE SIZE USED PRIO UUID LABEL
/dev/zd0 partition 4G 0B -2 1e1fb013-69d9-4878-b358-6b8ee53d5b09
$ sudo zpool list -v
NAME SIZE ALLOC FREE CKPOINT EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT
Root 1.66T 535G 1.13T - - 2% 31% 1.00x ONLINE -
nvme0n1p7 1.66T 535G 1.13T - - 2% 31.5% - ONLINE
$ sudo zpool status -v
pool: Root
state: ONLINE
scan: none requested
config:
NAME STATE READ WRITE CKSUM
Root ONLINE 0 0 0
nvme0n1p7 ONLINE 0 0 0
errors: No known data errors
$ sudo zfs list
NAME USED AVAIL REFER MOUNTPOINT
Root 539G 1.08T 192K none
Root/root 534G 1.08T 530G /
Root/root/00a891589b00cebc55cb6767e266ca38ac201daf3a6048c1f33d7d55c0710533 544K 1.08T 143M legacy
[…] # many other legacy mount points
Root/root/ffe52d35c873e6a417ee12c7025d848dac1f269b8078266f00a8d8088fd34384 396K 1.08T 607M legacy
Root/swap 4.25G 1.08T 172M -
$ sudo zfs get all Root/swap
NAME PROPERTY VALUE SOURCE
Root/swap type volume -
Root/swap creation So Aug 2 15:36 2020 -
Root/swap used 4.25G -
Root/swap available 1.08T -
Root/swap referenced 172M -
Root/swap compressratio 1.00x -
Root/swap reservation none default
Root/swap volsize 4G local
Root/swap volblocksize 4K -
Root/swap checksum on default
Root/swap compression off local
Root/swap readonly off default
Root/swap createtxg 3746 -
Root/swap copies 1 default
Root/swap refreservation 4.25G local
Root/swap guid 12379969387189982956 -
Root/swap primarycache metadata local
Root/swap secondarycache none local
Root/swap usedbysnapshots 0B -
Root/swap usedbydataset 172M -
Root/swap usedbychildren 0B -
Root/swap usedbyrefreservation 4.08G -
Root/swap logbias throughput local
Root/swap objsetid 278 -
Root/swap dedup off default
Root/swap mlslabel none default
Root/swap sync always local
Root/swap refcompressratio 1.00x -
Root/swap written 172M -
Root/swap logicalused 169M -
Root/swap logicalreferenced 169M -
Root/swap volmode default default
Root/swap snapshot_limit none default
Root/swap snapshot_count none default
Root/swap snapdev hidden default
Root/swap context none default
Root/swap fscontext none default
Root/swap defcontext none default
Root/swap rootcontext none default
Root/swap redundant_metadata all default
Root/swap encryption aes-256-gcm -
Root/swap keylocation none default
Root/swap keyformat passphrase -
Root/swap pbkdf2iters 342K -
Root/swap encryptionroot Root -
Root/swap keystatus available -
What I tried
Creating a Swapfile
I thought a swapfile might be an easy fix but I was obviously wrong:
$ sudo dd if=/dev/zero of=/swapfile bs=1MiB count=$((32*1024))
32768+0 records in
32768+0 records out
34359738368 bytes (34 GB, 32 GiB) copied, 14.5783 s, 2.4 GB/s
$ sudo chmod 600 /swapfile
$ sudo swapon /swapfile
swapon: /swapfile: skipping - it appears to have holes.
So I guess I should rather increase the swap partition.
It's also not clear to me why swapon
claims that the swap partition has 4G
although zfs get all Root/swap
says that 1.08T
are available.
Anything with GParted
GParted won't recognize much. This could be related to the bug in os-prober
(GParted issue 14, Debian bug 888114, os-prober issue 1848496, openzfs issues 9801 and 9069)
Replacing the small Swap partition (=increasing the Swap partition on ZFS)
The creation command was inspired by a comment on GitHub but you should keep in mind not to use -o sync=always
.
$ sudo zfs create -V 32G -b $(getconf PAGESIZE) -o logbias=throughput -o primarycache=metadata -o secondarycache=none -o com.sun:auto-snapshot=false -o compression=zle Root/swap_two
$ sudo zfs rename Root/swap Root/swap_bak
$ sudo zfs rename Root/swap_two Root/swap
$ sudo mkswap -f /dev/zvol/Root/swap
$ sudo swapon /dev/zvol/Root/swap
ensuring that this entry is in /etc/fstab
:
/dev/zvol/Root/swap none swap discard 0 0
then reboot and remove the tiny legacy swap:
$ sudo zfs destroy Root/swap_bak
While this seem to have worked, I still cannot hibernate:
$ sudo systemctl hibernate
Failed to hibernate system via logind: Sleep verb "hibernate" not supported
Therefore I'm still open for any advice. Maybe it needs a dedicated resume
parameter in GRUB_CMDLINE_LINUX_DEFAULT
?
In the end the reason wasn't related to ZFS. Even after resizing the swap it didn't work because it is simply disabled on newer Ubuntu versions (it will be reconsidered though).
To solve this, you have to activate hibernation.
- Open
com.ubuntu.desktop.pkla
in an editor:
$ sudo vim /var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla
# or on some machines
$ sudo vim /etc/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla
Search for the entries Disable hibernate by default in upower
and Disable hibernate by default in logind
and change each ResultActive
value to yes
.
Reboot and test the result with
$ sudo systemctl hibernate
# or
$ sudo pm-hibernate
If it still doesn't work, you might have to deactivate things like Fast boot
and Secure boot
in your BIOS (ATTENTION: it is very likely that you have to do that on newer machines).
If the test doesn't result in an error anymore, you should also be able to assign Hibernations to certain actions in Ubuntu settings. For instance on pressing the power button.