# BTRFS Swap "Not enough swap space for hibernation"

**URL:** https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805
**Category:** Help
**Created:** [December 13, 2023, 3:13pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805 "2023-12-13T15:13:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![charmoniumQ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/charmoniumq/32/4855_2.png) [@charmoniumQ](https://discourse.nixos.org/u/charmoniumQ)
#### Post date: [December 13, 2023, 3:13pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/1 "2023-12-13T15:13:01Z")

</div>

`systemctl hibernate` says `Not enough swap space`, but `free -h` indicates that “free” swap is greater than “total” mem.

```auto
$ systemctl hibernate
Call to Hibernate failed: Not enough swap space for hibernation

$ free -h
               total used free shared buff/cache available
Mem: 15Gi 6.8Gi 199Mi 2.5Gi 8.4Gi 5.8Gi
Swap: 23Gi 9.0Mi 23Gi

```

If it matters (which I don’t think it should), my swap partition is a btrfs subvolume on luks encrypted disk.

```auto
$ swapon
NAME TYPE SIZE USED PRIO
/swap/swapfile file 24G 139.2M -2

$ mount -v | grep swap
/dev/mapper/crypted on /swap type btrfs (rw,relatime,compress=zstd:3,ssd,noacl,space_cache=v2,subvolid=260,subvol=/swap)

$ cat /etc/fstab | grep -i swap
# To make changes, edit the fileSystems and swapDevices NixOS options
/dev/mapper/crypted /swap btrfs subvol=swap 0 0
# Swap devices.
/swap/swapfile none swap defaults

$ sudo btrfs sub list / | grep swap
ID 260 gen 6590 top level 5 path swap

```

My disk configuration [here](https://github.com/charmoniumQ/homelab/blob/54b6091da5fb52d43d94e6aff1ebb42dbebbc23c/hosts/laptop/disko.nix). It uses disko, so I found it easier to just `nix repl` and evaluate whatever attributes you need to answer the question.

```auto
$ nix repl '.#nixosConfigurations.laptop'
Welcome to Nix 2.19.2. Type :? for help.

Loading installable 'git+file:///home/sam/box/homelab#nixosConfigurations.laptop'...
Added 9 variables.
nix-repl> config.boot.kernelParams
["acpi_osi=\"!Windows 2020\"" "nvme.noacpi=1" "loglevel=4"]
(some NixOS systems would have "nohibernate" in here; not me.)

nix-repl> config.fileSystems
{ "/" = { ... }; "/boot" = { ... }; "/home" = { ... }; "/nix" = { ... }; "/persist" = { ... }; "/swap" = { ... }; "/var/log" = { ... }; }

nix-repl> config.fileSystems."/swap"
{ autoFormat = false; autoResize = false; depends = [...]; device = "/dev/mapper/crypted"; encrypted = { ... }; formatOptions = null; fsType = "btrfs"; label = null; mountPoint = "/swap"; neededForBoot = false; noCheck = false; options = [...]; stratis = { ... }; }

nix-repl> config.swapDevices               
[{ ... }]

nix-repl> (builtins.head config.swapDevices).device
"/swap/swapfile"

```

---

<div class="post-metadata">

### Author: ![Solene](https://discourse.nixos.org/user_avatar/discourse.nixos.org/solene/32/5738_2.png) [@Solene](https://discourse.nixos.org/u/Solene)
#### Post date: [December 13, 2023, 3:17pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/2 "2023-12-13T15:17:42Z")

</div>

In journalctl or dmesg you may have the reason why it doesn’t want to hibernate.

For instance, if the swap file is on a BTRFS volume spread across more than 1 disk, it will refuse.

---

<div class="post-metadata">

### Author: ![charmoniumQ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/charmoniumq/32/4855_2.png) [@charmoniumQ](https://discourse.nixos.org/u/charmoniumQ)
#### Post date: [December 13, 2023, 3:30pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/3 "2023-12-13T15:30:49Z")

</div>

Neither `journalctl` nor `dmesg` contain the word “swap” or “hibernate” case insensitive. Not sure what’s going on here.

---

<div class="post-metadata">

### Author: ![be9857](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/b/f9ae1b/32.png) [@be9857](https://discourse.nixos.org/u/be9857)
#### Post date: [December 22, 2023, 6:57pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/4 "2023-12-22T18:57:05Z")

</div>

same configuration here and same problem  
also nothing shows in my journalctl or dmesg

---

<div class="post-metadata">

### Author: ![simplejack-src](https://discourse.nixos.org/user_avatar/discourse.nixos.org/simplejack-src/32/4600_2.png) [@simplejack-src](https://discourse.nixos.org/u/simplejack-src)
#### Post date: [December 23, 2023, 12:27am UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/5 "2023-12-23T00:27:00Z")

</div>

I had this error myself (couple releases ago). In my particular issue it was a systemd bug (not sure if its still a bug or the same issue your hitting). In my particular case the workaround (knowing that the swap size is of sufficient size):

```auto
# In your NixOS configuration
systemd.services.systemd-logind.environment = {
   SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK = "1";
};

```

---

<div class="post-metadata">

### Author: ![be9857](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/b/f9ae1b/32.png) [@be9857](https://discourse.nixos.org/u/be9857)
#### Post date: [December 23, 2023, 6:51pm UTC](https://discourse.nixos.org/t/btrfs-swap-not-enough-swap-space-for-hibernation/36805/6 "2023-12-23T18:51:03Z")

</div>

@Solene  
good news I found some new context using @simplejack-src hacks  
so at least we know where is the problem  
and make this bug report  
[https://github.com/NixOS/nix/issues/9660](https://github.com/NixOS/nix/issues/9660)
