From 15d44498f9ec7b6481dcc0608a1ef89265c654c2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 12 Jun 2012 13:41:51 +0000 Subject: [PATCH] =?UTF-8?q?*=20Add=20a=20=E2=80=98size=E2=80=99=20option?= =?UTF-8?q?=20to=20=E2=80=98swapDevices=E2=80=99=20to=20create=20swapfiles?= =?UTF-8?q?=20on=20the=20fly.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit svn path=/nixos/trunk/; revision=34478 --- modules/config/swap.nix | 13 +++++++++++-- modules/tasks/filesystems.nix | 13 ++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/config/swap.nix b/modules/config/swap.nix index 550cd4755af..f0a6ceeda81 100644 --- a/modules/config/swap.nix +++ b/modules/config/swap.nix @@ -33,13 +33,22 @@ with pkgs.lib; device = mkOption { example = "/dev/sda3"; - type = types.string; + type = types.uniq types.string; description = "Path of the device."; }; label = mkOption { example = "swap"; - type = types.string; + type = types.uniq types.string; + description = '' + Label of the device. Can be used instead of device. + ''; + }; + + size = mkOption { + default = null; + example = "swap"; + type = types.nullOr types.int; description = '' Label of the device. Can be used instead of device. ''; diff --git a/modules/tasks/filesystems.nix b/modules/tasks/filesystems.nix index 5848e21688c..9495cac6785 100644 --- a/modules/tasks/filesystems.nix +++ b/modules/tasks/filesystems.nix @@ -22,7 +22,7 @@ let # Swap devices. ${flip concatMapStrings config.swapDevices (sw: - "${sw.device} none swap\n" + "${sw.device} none swap\n" )} ''; @@ -213,6 +213,17 @@ in ${flip concatMapStrings config.fileSystems (fs: optionalString fs.autocreate '' mkdir -p -m 0755 '${fs.mountPoint}' '')} + + # 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";