2010-10-25 01:57:30 +01:00
|
|
|
{pkgs, config, ...}:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
2012-03-03 16:07:18 +00:00
|
|
|
luks = config.boot.initrd.luks;
|
|
|
|
|
2012-05-02 23:37:14 +01:00
|
|
|
openCommand = { name, device, keyFile, keyFileSize, allowDiscards, ... }: ''
|
2012-03-03 16:07:18 +00:00
|
|
|
# Wait for luksRoot to appear, e.g. if on a usb drive.
|
|
|
|
# XXX: copied and adapted from stage-1-init.sh - should be
|
|
|
|
# available as a function.
|
|
|
|
if ! test -e ${device}; then
|
|
|
|
echo -n "waiting 10 seconds for device ${device} to appear..."
|
2012-06-27 14:43:54 +01:00
|
|
|
for try in $(seq 10); do
|
2012-03-03 16:07:18 +00:00
|
|
|
sleep 1
|
|
|
|
if test -e ${device}; then break; fi
|
2012-03-04 21:00:35 +00:00
|
|
|
echo -n .
|
2012-03-03 16:07:18 +00:00
|
|
|
done
|
|
|
|
echo "ok"
|
|
|
|
fi
|
|
|
|
|
2012-08-02 10:39:31 +01:00
|
|
|
${optionalString (keyFile != null) ''
|
2012-05-02 23:37:14 +01:00
|
|
|
if ! test -e ${keyFile}; then
|
|
|
|
echo -n "waiting 10 seconds for key file ${keyFile} to appear..."
|
|
|
|
for try in $(seq 10); do
|
|
|
|
sleep 1
|
|
|
|
if test -e ${keyFile}; then break; fi
|
|
|
|
echo -n .
|
|
|
|
done
|
|
|
|
echo "ok"
|
|
|
|
fi
|
|
|
|
''}
|
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
# open luksRoot and scan for logical volumes
|
2012-05-02 23:37:14 +01:00
|
|
|
cryptsetup luksOpen ${device} ${name} ${optionalString allowDiscards "--allow-discards"} \
|
|
|
|
${optionalString (keyFile != null) "--key-file=${keyFile} ${optionalString (keyFileSize != null) "--keyfile-size=${toString keyFileSize}"}"}
|
2012-03-03 16:07:18 +00:00
|
|
|
'';
|
|
|
|
|
2012-03-04 21:00:35 +00:00
|
|
|
isPreLVM = f: f.preLVM;
|
|
|
|
preLVM = filter isPreLVM luks.devices;
|
|
|
|
postLVM = filter (f: !(isPreLVM f)) luks.devices;
|
|
|
|
|
2010-10-25 01:57:30 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
2012-03-03 16:07:18 +00:00
|
|
|
boot.initrd.luks.enable = mkOption {
|
|
|
|
default = false;
|
2012-05-15 21:45:01 +01:00
|
|
|
description = "Obsolete.";
|
2012-03-03 16:07:18 +00:00
|
|
|
};
|
2010-10-25 01:57:30 +01:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
boot.initrd.luks.devices = mkOption {
|
|
|
|
default = [ ];
|
2012-03-04 21:00:35 +00:00
|
|
|
example = [ { name = "luksroot"; device = "/dev/sda3"; preLVM = true; } ];
|
2012-05-15 21:45:01 +01:00
|
|
|
description = ''
|
2012-03-03 16:07:18 +00:00
|
|
|
The list of devices that should be decrypted using LUKS before trying to mount the
|
2010-10-25 01:57:30 +01:00
|
|
|
root partition. This works for both LVM-over-LUKS and LUKS-over-LVM setups.
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
The devices are decrypted to the device mapper names defined.
|
2010-10-25 01:57:30 +01:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
Make sure that initrd has the crypto modules needed for decryption.
|
2010-10-25 01:57:30 +01:00
|
|
|
'';
|
2012-03-04 21:00:35 +00:00
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
example = "luksroot";
|
|
|
|
type = types.string;
|
2012-04-26 13:21:45 +01:00
|
|
|
description = "Named to be used for the generated device in /dev/mapper.";
|
2012-03-04 21:00:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
device = mkOption {
|
|
|
|
example = "/dev/sda2";
|
|
|
|
type = types.string;
|
2012-04-26 15:53:58 +01:00
|
|
|
description = "Path of the underlying block device.";
|
2012-03-04 21:00:35 +00:00
|
|
|
};
|
|
|
|
|
2012-05-02 23:37:14 +01:00
|
|
|
keyFile = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = "/dev/sdb1";
|
|
|
|
type = types.nullOr types.string;
|
|
|
|
description = ''
|
|
|
|
The name of the file (can be a raw device or a partition) that
|
|
|
|
should be used as the decryption key for the encrypted device. If
|
|
|
|
not specified, you will be prompted for a passphrase instead.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
keyFileSize = mkOption {
|
|
|
|
default = null;
|
|
|
|
example = 4096;
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
description = ''
|
|
|
|
The size of the key file. Use this if only the beginning of the
|
|
|
|
key file should be used as a key (often the case if a raw device
|
|
|
|
or partition is used as key file). If not specified, the whole
|
|
|
|
<literal>keyFile</literal> will be used decryption, instead of just
|
|
|
|
the first <literal>keyFileSize</literal> bytes.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-03-04 21:00:35 +00:00
|
|
|
preLVM = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2012-04-26 13:21:45 +01:00
|
|
|
description = "Whether the luksOpen will be attempted before LVM scan or after it.";
|
2012-03-04 21:00:35 +00:00
|
|
|
};
|
2012-07-17 12:10:00 +01:00
|
|
|
|
|
|
|
allowDiscards = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to allow TRIM requests to the underlying device. This option
|
|
|
|
has security implications, please read the LUKS documentation before
|
|
|
|
activating in.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2012-03-04 21:00:35 +00:00
|
|
|
};
|
2010-10-25 01:57:30 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-05-15 21:45:01 +01:00
|
|
|
config = mkIf (luks.devices != []) {
|
2010-10-25 01:57:30 +01:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
# Some modules that may be needed for mounting anything ciphered
|
|
|
|
boot.initrd.kernelModules = [ "aes_generic" "aes_x86_64" "dm_mod" "dm_crypt"
|
|
|
|
"sha256_generic" "cbc" "cryptd" ];
|
2010-10-25 01:57:30 +01:00
|
|
|
|
2011-12-28 21:46:40 +00:00
|
|
|
# copy the cryptsetup binary and it's dependencies
|
2010-10-25 01:57:30 +01:00
|
|
|
boot.initrd.extraUtilsCommands = ''
|
2011-12-28 21:46:40 +00:00
|
|
|
cp -pdv ${pkgs.cryptsetup}/sbin/cryptsetup $out/bin
|
|
|
|
# XXX: do we have a function that does this?
|
|
|
|
for lib in $(ldd $out/bin/cryptsetup |grep '=>' |grep /nix/store/ |cut -d' ' -f3); do
|
|
|
|
cp -pdvn $lib $out/lib
|
|
|
|
cp -pvn $(readlink -f $lib) $out/lib
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
|
|
|
boot.initrd.extraUtilsCommandsTest = ''
|
2011-12-28 22:37:38 +00:00
|
|
|
$out/bin/cryptsetup --version
|
2010-10-25 01:57:30 +01:00
|
|
|
'';
|
|
|
|
|
2012-03-04 21:00:35 +00:00
|
|
|
boot.initrd.preLVMCommands = concatMapStrings openCommand preLVM;
|
|
|
|
boot.initrd.postDeviceCommands = concatMapStrings openCommand postLVM;
|
2012-03-08 20:49:26 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.cryptsetup ];
|
2010-10-25 01:57:30 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
}
|