Merge pull request #16755 from bjornfor/swapfile-resize
nixos/swap: support for resizing swapfile
This commit is contained in:
commit
0d5be595a7
@ -30,8 +30,7 @@ let
|
|||||||
description = ''
|
description = ''
|
||||||
If this option is set, ‘device’ is interpreted as the
|
If this option is set, ‘device’ is interpreted as the
|
||||||
path of a swapfile that will be created automatically
|
path of a swapfile that will be created automatically
|
||||||
with the indicated size (in megabytes) if it doesn't
|
with the indicated size (in megabytes).
|
||||||
exist.
|
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -132,9 +131,13 @@ in
|
|||||||
script =
|
script =
|
||||||
''
|
''
|
||||||
${optionalString (sw.size != null) ''
|
${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}" ||
|
fallocate -l ${toString sw.size}M "${sw.device}" ||
|
||||||
dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
|
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}
|
chmod 0600 ${sw.device}
|
||||||
${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"}
|
${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"}
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user