2010-10-25 00:57:30 +00: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 00:57:30 +00: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 00:57:30 +00: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 00:57:30 +00: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 00:57:30 +00:00
|
|
|
root partition. This works for both LVM-over-LUKS and LUKS-over-LVM setups.
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
The devices are decrypted to the device mapper names defined.
|
2010-10-25 00:57:30 +00:00
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
Make sure that initrd has the crypto modules needed for decryption.
|
2010-10-25 00:57:30 +00:00
|
|
|
'';
|
2012-03-04 21:00:35 +00:00
|
|
|
|
|
|
|
type = types.list types.optionSet;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
example = "luksroot";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
Name of the interface.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
device = mkOption {
|
|
|
|
example = "/dev/sda2";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
IP address of the interface. Leave empty to configure the
|
|
|
|
interface using DHCP.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
preLVM = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether the luksOpen will be attempted before LVM scan or after it.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2010-10-25 00:57:30 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-03-03 16:07:18 +00:00
|
|
|
config = mkIf luks.enable {
|
2010-10-25 00:57:30 +00: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 00:57:30 +00:00
|
|
|
|
2011-12-28 21:46:40 +00:00
|
|
|
# copy the cryptsetup binary and it's dependencies
|
2010-10-25 00:57:30 +00: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 00:57:30 +00:00
|
|
|
'';
|
|
|
|
|
2012-03-04 21:00:35 +00:00
|
|
|
boot.initrd.preLVMCommands = concatMapStrings openCommand preLVM;
|
|
|
|
boot.initrd.postDeviceCommands = concatMapStrings openCommand postLVM;
|
2010-10-25 00:57:30 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
}
|