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-03-04 21:00:35 +00:00
|
|
|
openCommand = { name, device, ... }: ''
|
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..."
|
|
|
|
for ((try = 0; try < 10; try++)); do
|
|
|
|
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
|
|
|
|
|
|
|
|
# open luksRoot and scan for logical volumes
|
|
|
|
cryptsetup luksOpen ${device} ${name}
|
|
|
|
'';
|
|
|
|
|
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;
|
|
|
|
description = '';
|
|
|
|
Have luks in the initrd.
|
|
|
|
'';
|
|
|
|
};
|
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; } ];
|
2010-10-25 01:57:30 +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 13:21:45 +01:00
|
|
|
description = "Path of the underlying block device."
|
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
|
|
|
};
|
|
|
|
};
|
2010-10-25 01:57:30 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
config = mkIf luks.enable {
|
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
|
|
|
}
|