Update automatic swapfile creation for systemd

This commit is contained in:
Eelco Dolstra 2012-10-12 16:47:11 -04:00
parent 97a2de983b
commit e8de4455ab
2 changed files with 31 additions and 10 deletions

View File

@ -74,9 +74,40 @@ with pkgs.lib;
};
config = mkIf ((length config.swapDevices) != 0) {
system.requiredKernelConfig = with config.lib.kernelConfig; [
(isYes "SWAP")
];
# Create missing swapfiles.
# FIXME: support changing the size of existing swapfiles.
boot.systemd.services =
let
escapePath = s: # FIXME: slow
replaceChars ["/" "-"] ["-" "\\x2d"] (substring 1 (stringLength s) s);
createSwapDevice = sw: assert sw.device != ""; nameValuePair "mkswap-${escapePath sw.device}"
{ description = "Initialisation of Swapfile ${sw.device}";
wantedBy = [ "${escapePath sw.device}.swap" ];
before = [ "${escapePath sw.device}.swap" ];
path = [ pkgs.utillinux ];
script =
''
if [ ! -e "${sw.device}" ]; then
fallocate -l ${toString sw.size}M "${sw.device}" ||
dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
mkswap ${sw.device}
fi
'';
unitConfig.RequiresMountsFor = "${dirOf sw.device}";
unitConfig.DefaultDependencies = false; # needed to prevent a cycle
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
};
in listToAttrs (map createSwapDevice (filter (sw: sw.size != null) config.swapDevices));
};
}

View File

@ -213,16 +213,6 @@ in
fi
'')}
# Create missing swapfiles.
# FIXME: support changing the size of existing swapfiles.
${flip concatMapStrings config.swapDevices (sw: optionalString (sw.size != null) ''
if [ ! -e "${sw.device}" -a -e "$(dirname "${sw.device}")" ]; then
# FIXME: use fallocate on filesystems that support it.
dd if=/dev/zero of="${sw.device}" bs=1M count=${toString sw.size}
mkswap ${sw.device}
fi
'')}
'';
daemonType = "daemon";