summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorBjørn Forsman <bjorn.forsman@gmail.com>2016-07-06 13:41:40 +0200
committerBjørn Forsman <bjorn.forsman@gmail.com>2016-07-06 16:04:27 +0200
commitb30852ed41f7e6729de9a75a71a671ce7d03dff9 (patch)
tree48a927e5c092b645afc63849c31d421fc3ed6291 /nixos
parent12c055240dcf2aec5f13ca1bfbae6a44e5cd42be (diff)
downloadnixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar.gz
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar.bz2
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar.lz
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar.xz
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.tar.zst
nixlib-b30852ed41f7e6729de9a75a71a671ce7d03dff9.zip
nixos/swap: support for resizing swapfile
Currently NixOS creates the swapfile (with the specified size) only if
it doesn't already exist. Changing the swapfile size afterwards will not
have any effect.

This commit changes that so the swapfile will be recreated whenever
swapDevices.*.size is changed (or more precisely, whenever the actual
file size differs from the configured one), allowing both growing and
shrinking the swapfile.

The service unit has "restartIfChanged = false", so we don't have to
worry about the swapfile being in use at the time this code is run (you
have to reboot for swapfile changes).

fallocate doesn't shrink files, use truncate for that. truncate can also
be used to grow files, but it creates "holes" in the file which doesn't
work with swapfiles.
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/config/swap.nix9
1 files changed, 6 insertions, 3 deletions
diff --git a/nixos/modules/config/swap.nix b/nixos/modules/config/swap.nix
index f0353c5a35ec..62b6e011713e 100644
--- a/nixos/modules/config/swap.nix
+++ b/nixos/modules/config/swap.nix
@@ -30,8 +30,7 @@ let
         description = ''
           If this option is set, ‘device’ is interpreted as the
           path of a swapfile that will be created automatically
-          with the indicated size (in megabytes) if it doesn't
-          exist.
+          with the indicated size (in megabytes).
         '';
       };
 
@@ -132,9 +131,13 @@ in
             script =
               ''
                 ${optionalString (sw.size != null) ''
-                  if [ ! -e "${sw.device}" ]; then
+                  currentSize=$(( $(stat -c "%s" "${sw.device}" 2>/dev/null || echo 0) / 1024 / 1024 ))
+                  if [ "${toString sw.size}" != "$currentSize" ]; then
                     fallocate -l ${toString sw.size}M "${sw.device}" ||
                       dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
+                    if [ "${toString sw.size}" -lt "$currentSize" ]; then
+                      truncate --size "${toString sw.size}M" "${sw.device}"
+                    fi
                     chmod 0600 ${sw.device}
                     ${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"}
                   fi