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