···3030 description = ''
3131 If this option is set, ‘device’ is interpreted as the
3232 path of a swapfile that will be created automatically
3333- with the indicated size (in megabytes) if it doesn't
3434- exist.
3333+ with the indicated size (in megabytes).
3534 '';
3635 };
3736···132131 script =
133132 ''
134133 ${optionalString (sw.size != null) ''
135135- if [ ! -e "${sw.device}" ]; then
134134+ currentSize=$(( $(stat -c "%s" "${sw.device}" 2>/dev/null || echo 0) / 1024 / 1024 ))
135135+ if [ "${toString sw.size}" != "$currentSize" ]; then
136136 fallocate -l ${toString sw.size}M "${sw.device}" ||
137137 dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
138138+ if [ "${toString sw.size}" -lt "$currentSize" ]; then
139139+ truncate --size "${toString sw.size}M" "${sw.device}"
140140+ fi
138141 chmod 0600 ${sw.device}
139142 ${optionalString (!sw.randomEncryption) "mkswap ${sw.realDevice}"}
140143 fi