Update automatic swapfile creation for systemd
This commit is contained in:
parent
97a2de983b
commit
e8de4455ab
@ -74,9 +74,40 @@ with pkgs.lib;
|
|||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf ((length config.swapDevices) != 0) {
|
config = mkIf ((length config.swapDevices) != 0) {
|
||||||
|
|
||||||
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
||||||
(isYes "SWAP")
|
(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));
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -213,16 +213,6 @@ in
|
|||||||
fi
|
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";
|
daemonType = "daemon";
|
||||||
|
Loading…
Reference in New Issue
Block a user