Merge pull request #16755 from bjornfor/swapfile-resize

nixos/swap: support for resizing swapfile

authored by

Eelco Dolstra and committed by
GitHub
0d5be595 851cd1e6

+6 -3
+6 -3
nixos/modules/config/swap.nix
··· 30 30 description = '' 31 31 If this option is set, ‘device’ is interpreted as the 32 32 path of a swapfile that will be created automatically 33 - with the indicated size (in megabytes) if it doesn't 34 - exist. 33 + with the indicated size (in megabytes). 35 34 ''; 36 35 }; 37 36 ··· 132 131 script = 133 132 '' 134 133 ${optionalString (sw.size != null) '' 135 - if [ ! -e "${sw.device}" ]; then 134 + currentSize=$(( $(stat -c "%s" "${sw.device}" 2>/dev/null || echo 0) / 1024 / 1024 )) 135 + if [ "${toString sw.size}" != "$currentSize" ]; then 136 136 fallocate -l ${toString sw.size}M "${sw.device}" || 137 137 dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size} 138 + if [ "${toString sw.size}" -lt "$currentSize" ]; then 139 + truncate --size "${toString sw.size}M" "${sw.device}" 140 + fi 138 141 chmod 0600 ${sw.device} 139 142 ${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"} 140 143 fi