2009-05-27 08:14:06 +00:00
|
|
|
|
# This module builds the initial ramdisk, which contains an init
|
|
|
|
|
# script that performs the first stage of booting the system: it loads
|
|
|
|
|
# the modules necessary to mount the root file system, then calls the
|
|
|
|
|
# init in the root file system to start the second boot stage.
|
2006-11-02 22:48:01 +00:00
|
|
|
|
|
2015-11-25 19:09:09 +00:00
|
|
|
|
{ config, lib, utils, pkgs, ... }:
|
2008-11-23 01:29:20 +00:00
|
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
|
with lib;
|
2009-05-27 09:46:50 +00:00
|
|
|
|
|
2010-06-01 16:38:42 +00:00
|
|
|
|
let
|
2009-05-27 09:46:50 +00:00
|
|
|
|
|
2013-01-16 12:17:57 +00:00
|
|
|
|
udev = config.systemd.package;
|
2012-08-14 19:31:15 +00:00
|
|
|
|
|
2019-04-13 21:22:56 +00:00
|
|
|
|
kernel-name = config.boot.kernelPackages.kernel.name or "kernel";
|
|
|
|
|
|
|
|
|
|
modulesTree = config.system.modulesTree.override { name = kernel-name + "-modules"; };
|
2018-01-08 13:13:28 +00:00
|
|
|
|
firmware = config.hardware.firmware;
|
2008-08-08 17:34:30 +00:00
|
|
|
|
|
2006-11-12 18:48:47 +00:00
|
|
|
|
|
2008-08-08 17:34:30 +00:00
|
|
|
|
# Determine the set of modules that we need to mount the root FS.
|
|
|
|
|
modulesClosure = pkgs.makeModulesClosure {
|
2009-12-15 16:38:20 +00:00
|
|
|
|
rootModules = config.boot.initrd.availableKernelModules ++ config.boot.initrd.kernelModules;
|
2008-08-08 17:34:30 +00:00
|
|
|
|
kernel = modulesTree;
|
2018-01-08 13:13:28 +00:00
|
|
|
|
firmware = firmware;
|
2009-12-15 16:38:20 +00:00
|
|
|
|
allowMissing = true;
|
2008-08-08 17:34:30 +00:00
|
|
|
|
};
|
2008-01-24 16:56:09 +00:00
|
|
|
|
|
2006-11-13 11:41:27 +00:00
|
|
|
|
|
2015-11-25 19:09:09 +00:00
|
|
|
|
# The initrd only has to mount `/` or any FS marked as necessary for
|
|
|
|
|
# booting (such as the FS containing `/nix/store`, or an FS needed for
|
|
|
|
|
# mounting `/`, like `/` on a loopback).
|
2016-08-23 18:01:35 +00:00
|
|
|
|
fileSystems = filter utils.fsNeededForBoot config.system.build.fileSystems;
|
2015-11-25 19:09:09 +00:00
|
|
|
|
|
2018-03-01 18:31:36 +00:00
|
|
|
|
# A utility for enumerating the shared-library dependencies of a program
|
2019-04-06 02:35:43 +00:00
|
|
|
|
findLibs = pkgs.buildPackages.writeShellScriptBin "find-libs" ''
|
2018-03-01 18:31:36 +00:00
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
declare -A seen
|
2020-08-25 10:02:53 +00:00
|
|
|
|
left=()
|
2018-03-01 18:31:36 +00:00
|
|
|
|
|
|
|
|
|
patchelf="${pkgs.buildPackages.patchelf}/bin/patchelf"
|
|
|
|
|
|
|
|
|
|
function add_needed {
|
|
|
|
|
rpath="$($patchelf --print-rpath $1)"
|
|
|
|
|
dir="$(dirname $1)"
|
|
|
|
|
for lib in $($patchelf --print-needed $1); do
|
|
|
|
|
left+=("$lib" "$rpath" "$dir")
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 10:02:53 +00:00
|
|
|
|
add_needed "$1"
|
2018-03-01 18:31:36 +00:00
|
|
|
|
|
|
|
|
|
while [ ''${#left[@]} -ne 0 ]; do
|
|
|
|
|
next=''${left[0]}
|
|
|
|
|
rpath=''${left[1]}
|
|
|
|
|
ORIGIN=''${left[2]}
|
|
|
|
|
left=("''${left[@]:3}")
|
|
|
|
|
if [ -z ''${seen[$next]+x} ]; then
|
|
|
|
|
seen[$next]=1
|
2018-06-20 02:17:26 +00:00
|
|
|
|
|
|
|
|
|
# Ignore the dynamic linker which for some reason appears as a DT_NEEDED of glibc but isn't in glibc's RPATH.
|
|
|
|
|
case "$next" in
|
|
|
|
|
ld*.so.?) continue;;
|
|
|
|
|
esac
|
|
|
|
|
|
2018-03-01 18:31:36 +00:00
|
|
|
|
IFS=: read -ra paths <<< $rpath
|
|
|
|
|
res=
|
|
|
|
|
for path in "''${paths[@]}"; do
|
|
|
|
|
path=$(eval "echo $path")
|
|
|
|
|
if [ -f "$path/$next" ]; then
|
|
|
|
|
res="$path/$next"
|
|
|
|
|
echo "$res"
|
|
|
|
|
add_needed "$res"
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
if [ -z "$res" ]; then
|
|
|
|
|
echo "Couldn't satisfy dependency $next" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
'';
|
2015-11-25 19:09:09 +00:00
|
|
|
|
|
2009-02-22 16:08:13 +00:00
|
|
|
|
# Some additional utilities needed in stage 1, like mount, lvm, fsck
|
|
|
|
|
# etc. We don't want to bring in all of those packages, so we just
|
|
|
|
|
# copy what we need. Instead of using statically linked binaries,
|
|
|
|
|
# we just copy what we need from Glibc and use patchelf to make it
|
|
|
|
|
# work.
|
2020-09-03 11:51:36 +00:00
|
|
|
|
extraUtils = pkgs.runCommandCC "extra-utils"
|
2018-02-28 02:05:41 +00:00
|
|
|
|
{ nativeBuildInputs = [pkgs.buildPackages.nukeReferences];
|
2012-05-21 19:26:07 +00:00
|
|
|
|
allowedReferences = [ "out" ]; # prevent accidents like glibc being included in the initrd
|
2008-08-08 17:34:30 +00:00
|
|
|
|
}
|
|
|
|
|
''
|
2014-10-19 17:29:28 +00:00
|
|
|
|
set +o pipefail
|
|
|
|
|
|
2013-01-22 15:26:51 +00:00
|
|
|
|
mkdir -p $out/bin $out/lib
|
2014-07-30 13:44:47 +00:00
|
|
|
|
ln -s $out/bin $out/sbin
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2015-03-29 00:15:41 +00:00
|
|
|
|
copy_bin_and_libs () {
|
2015-03-30 04:26:39 +00:00
|
|
|
|
[ -f "$out/bin/$(basename $1)" ] && rm "$out/bin/$(basename $1)"
|
2015-03-29 00:15:41 +00:00
|
|
|
|
cp -pdv $1 $out/bin
|
|
|
|
|
}
|
2009-02-22 16:08:13 +00:00
|
|
|
|
|
2012-05-21 19:26:07 +00:00
|
|
|
|
# Copy BusyBox.
|
2015-03-29 00:15:41 +00:00
|
|
|
|
for BIN in ${pkgs.busybox}/{s,}bin/*; do
|
|
|
|
|
copy_bin_and_libs $BIN
|
|
|
|
|
done
|
2012-05-21 19:26:07 +00:00
|
|
|
|
|
2020-11-24 15:29:28 +00:00
|
|
|
|
# Copy some util-linux stuff.
|
|
|
|
|
copy_bin_and_libs ${pkgs.util-linux}/sbin/blkid
|
2012-08-14 19:31:15 +00:00
|
|
|
|
|
2010-01-10 19:00:29 +00:00
|
|
|
|
# Copy dmsetup and lvm.
|
2020-09-03 11:51:36 +00:00
|
|
|
|
copy_bin_and_libs ${getBin pkgs.lvm2}/bin/dmsetup
|
|
|
|
|
copy_bin_and_libs ${getBin pkgs.lvm2}/bin/lvm
|
2009-02-22 16:08:13 +00:00
|
|
|
|
|
2009-03-31 09:27:11 +00:00
|
|
|
|
# Add RAID mdadm tool.
|
2015-03-29 00:15:41 +00:00
|
|
|
|
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm
|
2016-02-25 13:08:27 +00:00
|
|
|
|
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon
|
2009-03-29 10:59:13 +00:00
|
|
|
|
|
2009-02-22 16:08:13 +00:00
|
|
|
|
# Copy udev.
|
2015-10-03 18:52:03 +00:00
|
|
|
|
copy_bin_and_libs ${udev}/bin/udevadm
|
2020-08-12 17:50:37 +00:00
|
|
|
|
copy_bin_and_libs ${udev}/lib/systemd/systemd-sysctl
|
2015-10-03 18:52:03 +00:00
|
|
|
|
for BIN in ${udev}/lib/udev/*_id; do
|
2015-03-29 00:15:41 +00:00
|
|
|
|
copy_bin_and_libs $BIN
|
|
|
|
|
done
|
2020-08-12 17:50:37 +00:00
|
|
|
|
# systemd-udevd is only a symlink to udevadm these days
|
|
|
|
|
ln -sf udevadm $out/bin/systemd-udevd
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2016-04-01 15:30:34 +00:00
|
|
|
|
# Copy modprobe.
|
|
|
|
|
copy_bin_and_libs ${pkgs.kmod}/bin/kmod
|
|
|
|
|
ln -sf kmod $out/bin/modprobe
|
|
|
|
|
|
2018-06-19 11:23:39 +00:00
|
|
|
|
# Copy resize2fs if any ext* filesystems are to be resized
|
|
|
|
|
${optionalString (any (fs: fs.autoResize && (lib.hasPrefix "ext" fs.fsType)) fileSystems) ''
|
2015-09-24 16:13:14 +00:00
|
|
|
|
# We need mke2fs in the initrd.
|
|
|
|
|
copy_bin_and_libs ${pkgs.e2fsprogs}/sbin/resize2fs
|
|
|
|
|
''}
|
|
|
|
|
|
2017-04-02 18:45:44 +00:00
|
|
|
|
# Copy secrets if needed.
|
2020-04-11 15:24:55 +00:00
|
|
|
|
#
|
|
|
|
|
# TODO: move out to a separate script; see #85000.
|
2017-04-02 18:45:44 +00:00
|
|
|
|
${optionalString (!config.boot.loader.supportsInitrdSecrets)
|
|
|
|
|
(concatStringsSep "\n" (mapAttrsToList (dest: source:
|
|
|
|
|
let source' = if source == null then dest else source; in
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $(dirname "$out/secrets/${dest}")
|
2020-03-14 18:29:08 +00:00
|
|
|
|
# Some programs (e.g. ssh) doesn't like secrets to be
|
|
|
|
|
# symlinks, so we use `cp -L` here to match the
|
|
|
|
|
# behaviour when secrets are natively supported.
|
|
|
|
|
cp -Lr ${source'} "$out/secrets/${dest}"
|
2017-04-02 18:45:44 +00:00
|
|
|
|
''
|
|
|
|
|
) config.boot.initrd.secrets))
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-19 15:19:56 +00:00
|
|
|
|
${config.boot.initrd.extraUtilsCommands}
|
|
|
|
|
|
2015-03-30 04:53:25 +00:00
|
|
|
|
# Copy ld manually since it isn't detected correctly
|
2018-03-24 17:11:02 +00:00
|
|
|
|
cp -pv ${pkgs.stdenv.cc.libc.out}/lib/ld*.so.? $out/lib
|
2015-03-30 04:53:25 +00:00
|
|
|
|
|
2016-07-06 19:56:21 +00:00
|
|
|
|
# Copy all of the needed libraries
|
|
|
|
|
find $out/bin $out/lib -type f | while read BIN; do
|
|
|
|
|
echo "Copying libs for executable $BIN"
|
2018-03-01 18:31:36 +00:00
|
|
|
|
for LIB in $(${findLibs}/bin/find-libs $BIN); do
|
2016-08-08 14:45:45 +00:00
|
|
|
|
TGT="$out/lib/$(basename $LIB)"
|
|
|
|
|
if [ ! -f "$TGT" ]; then
|
|
|
|
|
SRC="$(readlink -e $LIB)"
|
|
|
|
|
cp -pdv "$SRC" "$TGT"
|
|
|
|
|
fi
|
2015-03-30 04:53:25 +00:00
|
|
|
|
done
|
|
|
|
|
done
|
|
|
|
|
|
2012-05-21 19:26:07 +00:00
|
|
|
|
# Strip binaries further than normal.
|
|
|
|
|
chmod -R u+w $out
|
2018-07-08 02:25:23 +00:00
|
|
|
|
stripDirs "$STRIP" "lib bin" "-s"
|
2012-05-21 19:26:07 +00:00
|
|
|
|
|
2009-02-22 16:08:13 +00:00
|
|
|
|
# Run patchelf to make the programs refer to the copied libraries.
|
2016-07-06 19:56:21 +00:00
|
|
|
|
find $out/bin $out/lib -type f | while read i; do
|
|
|
|
|
if ! test -L $i; then
|
|
|
|
|
nuke-refs -e $out $i
|
|
|
|
|
fi
|
|
|
|
|
done
|
2009-02-22 16:08:13 +00:00
|
|
|
|
|
2016-07-06 19:56:21 +00:00
|
|
|
|
find $out/bin -type f | while read i; do
|
|
|
|
|
if ! test -L $i; then
|
|
|
|
|
echo "patching $i..."
|
|
|
|
|
patchelf --set-interpreter $out/lib/ld*.so.? --set-rpath $out/lib $i || true
|
|
|
|
|
fi
|
2009-02-22 16:08:13 +00:00
|
|
|
|
done
|
|
|
|
|
|
2018-08-06 22:07:02 +00:00
|
|
|
|
if [ -z "${toString (pkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform)}" ]; then
|
2009-02-22 16:08:13 +00:00
|
|
|
|
# Make sure that the patchelf'ed binaries still work.
|
|
|
|
|
echo "testing patched programs..."
|
2012-05-21 19:26:07 +00:00
|
|
|
|
$out/bin/ash -c 'echo hello world' | grep "hello world"
|
2009-02-22 16:08:13 +00:00
|
|
|
|
export LD_LIBRARY_PATH=$out/lib
|
2015-03-30 04:26:39 +00:00
|
|
|
|
$out/bin/mount --help 2>&1 | grep -q "BusyBox"
|
2017-06-25 14:03:30 +00:00
|
|
|
|
$out/bin/blkid -V 2>&1 | grep -q 'libblkid'
|
2009-02-22 16:08:13 +00:00
|
|
|
|
$out/bin/udevadm --version
|
2015-03-30 04:26:39 +00:00
|
|
|
|
$out/bin/dmsetup --version 2>&1 | tee -a log | grep -q "version:"
|
|
|
|
|
LVM_SYSTEM_DIR=$out $out/bin/lvm version 2>&1 | tee -a log | grep -q "LVM"
|
2009-03-31 09:27:11 +00:00
|
|
|
|
$out/bin/mdadm --version
|
2011-12-28 21:46:35 +00:00
|
|
|
|
|
|
|
|
|
${config.boot.initrd.extraUtilsCommandsTest}
|
2018-02-28 02:05:41 +00:00
|
|
|
|
fi
|
2008-08-08 17:34:30 +00:00
|
|
|
|
''; # */
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2006-11-02 22:48:01 +00:00
|
|
|
|
|
2019-09-22 15:14:47 +00:00
|
|
|
|
linkUnits = pkgs.runCommand "link-units" {
|
|
|
|
|
allowedReferences = [ extraUtils ];
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
} ''
|
|
|
|
|
mkdir -p $out
|
|
|
|
|
cp -v ${udev}/lib/systemd/network/*.link $out/
|
|
|
|
|
'';
|
|
|
|
|
|
2018-11-08 10:59:03 +00:00
|
|
|
|
udevRules = pkgs.runCommand "udev-rules" {
|
|
|
|
|
allowedReferences = [ extraUtils ];
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
} ''
|
2014-06-30 12:56:10 +00:00
|
|
|
|
mkdir -p $out
|
2011-02-16 14:38:52 +00:00
|
|
|
|
|
|
|
|
|
echo 'ENV{LD_LIBRARY_PATH}="${extraUtils}/lib"' > $out/00-env.rules
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2015-10-03 18:52:03 +00:00
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/60-cdrom_id.rules $out/
|
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/60-persistent-storage.rules $out/
|
2019-09-22 15:14:47 +00:00
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/75-net-description.rules $out/
|
2015-10-03 18:52:03 +00:00
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/80-drivers.rules $out/
|
2019-09-22 15:14:47 +00:00
|
|
|
|
cp -v ${udev}/lib/udev/rules.d/80-net-setup-link.rules $out/
|
2011-08-24 21:24:39 +00:00
|
|
|
|
cp -v ${pkgs.lvm2}/lib/udev/rules.d/*.rules $out/
|
2014-11-10 21:08:10 +00:00
|
|
|
|
${config.boot.initrd.extraUdevRulesCommands}
|
2009-08-19 16:48:28 +00:00
|
|
|
|
|
|
|
|
|
for i in $out/*.rules; do
|
|
|
|
|
substituteInPlace $i \
|
|
|
|
|
--replace ata_id ${extraUtils}/bin/ata_id \
|
|
|
|
|
--replace scsi_id ${extraUtils}/bin/scsi_id \
|
|
|
|
|
--replace cdrom_id ${extraUtils}/bin/cdrom_id \
|
2019-07-25 13:32:08 +00:00
|
|
|
|
--replace ${pkgs.coreutils}/bin/basename ${extraUtils}/bin/basename \
|
2020-11-24 15:29:28 +00:00
|
|
|
|
--replace ${pkgs.util-linux}/bin/blkid ${extraUtils}/bin/blkid \
|
2020-05-24 00:27:51 +00:00
|
|
|
|
--replace ${getBin pkgs.lvm2}/bin ${extraUtils}/bin \
|
2019-07-25 13:32:08 +00:00
|
|
|
|
--replace ${pkgs.mdadm}/sbin ${extraUtils}/sbin \
|
2017-06-01 20:35:40 +00:00
|
|
|
|
--replace ${pkgs.bash}/bin/sh ${extraUtils}/bin/sh \
|
2019-09-22 15:14:47 +00:00
|
|
|
|
--replace ${udev} ${extraUtils}
|
2009-08-19 16:48:28 +00:00
|
|
|
|
done
|
2011-02-16 16:48:34 +00:00
|
|
|
|
|
|
|
|
|
# Work around a bug in QEMU, which doesn't implement the "READ
|
|
|
|
|
# DISC INFORMATION" SCSI command:
|
|
|
|
|
# https://bugzilla.redhat.com/show_bug.cgi?id=609049
|
|
|
|
|
# As a result, `cdrom_id' doesn't print
|
|
|
|
|
# ID_CDROM_MEDIA_TRACK_COUNT_DATA, which in turn prevents the
|
|
|
|
|
# /dev/disk/by-label symlinks from being created. We need these
|
|
|
|
|
# in the NixOS installation CD, so use ID_CDROM_MEDIA in the
|
|
|
|
|
# corresponding udev rules for now. This was the behaviour in
|
|
|
|
|
# udev <= 154. See also
|
|
|
|
|
# http://www.spinics.net/lists/hotplug/msg03935.html
|
|
|
|
|
substituteInPlace $out/60-persistent-storage.rules \
|
|
|
|
|
--replace ID_CDROM_MEDIA_TRACK_COUNT_DATA ID_CDROM_MEDIA
|
2008-08-08 22:44:45 +00:00
|
|
|
|
''; # */
|
|
|
|
|
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2008-08-08 17:34:30 +00:00
|
|
|
|
# The init script of boot stage 1 (loading kernel modules for
|
|
|
|
|
# mounting the root FS).
|
|
|
|
|
bootStage1 = pkgs.substituteAll {
|
2009-05-25 14:19:33 +00:00
|
|
|
|
src = ./stage-1-init.sh;
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2012-05-21 19:26:07 +00:00
|
|
|
|
shell = "${extraUtils}/bin/ash";
|
2009-02-22 16:08:13 +00:00
|
|
|
|
|
2008-08-08 17:34:30 +00:00
|
|
|
|
isExecutable = true;
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2018-06-10 20:18:55 +00:00
|
|
|
|
postInstall = ''
|
|
|
|
|
echo checking syntax
|
|
|
|
|
# check both with bash
|
2018-10-12 00:45:59 +00:00
|
|
|
|
${pkgs.buildPackages.bash}/bin/sh -n $target
|
2018-06-10 20:18:55 +00:00
|
|
|
|
# and with ash shell, just in case
|
2018-10-12 09:24:00 +00:00
|
|
|
|
${pkgs.buildPackages.busybox}/bin/ash -n $target
|
2018-06-10 20:18:55 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2019-09-22 15:14:47 +00:00
|
|
|
|
inherit linkUnits udevRules extraUtils modulesClosure;
|
2009-12-15 16:38:20 +00:00
|
|
|
|
|
2016-08-27 10:29:38 +00:00
|
|
|
|
inherit (config.boot) resumeDevice;
|
|
|
|
|
|
|
|
|
|
inherit (config.system.build) earlyMountScript;
|
2007-02-06 16:53:36 +00:00
|
|
|
|
|
2009-06-18 16:47:00 +00:00
|
|
|
|
inherit (config.boot.initrd) checkJournalingFS
|
2016-07-06 21:44:02 +00:00
|
|
|
|
preLVMCommands preDeviceCommands postDeviceCommands postMountCommands preFailCommands kernelModules;
|
2009-06-15 16:47:37 +00:00
|
|
|
|
|
2014-09-24 01:04:26 +00:00
|
|
|
|
resumeDevices = map (sd: if sd ? device then sd.device else "/dev/disk/by-label/${sd.label}")
|
2017-05-22 18:07:04 +00:00
|
|
|
|
(filter (sd: hasPrefix "/dev/" sd.device && !sd.randomEncryption.enable
|
2016-09-24 10:06:27 +00:00
|
|
|
|
# Don't include zram devices
|
|
|
|
|
&& !(hasPrefix "/dev/zram" sd.device)
|
|
|
|
|
) config.swapDevices);
|
2014-09-24 01:04:26 +00:00
|
|
|
|
|
2012-05-21 19:26:07 +00:00
|
|
|
|
fsInfo =
|
2015-10-21 17:37:14 +00:00
|
|
|
|
let f = fs: [ fs.mountPoint (if fs.device != null then fs.device else "/dev/disk/by-label/${fs.label}") fs.fsType (builtins.concatStringsSep "," fs.options) ];
|
2012-05-21 19:26:07 +00:00
|
|
|
|
in pkgs.writeText "initrd-fsinfo" (concatStringsSep "\n" (concatMap f fileSystems));
|
nixos: Add system-wide option to set the hostid
The old boot.spl.hostid option was not working correctly due to an
upstream bug.
Instead, now we will create the /etc/hostid file so that all applications
(including the ZFS kernel modules, ZFS user-space applications and other
unrelated programs) pick-up the same system-wide host id. Note that glibc
(and by extension, the `hostid` program) also respect the host id configured in
/etc/hostid, if it exists.
The hostid option is now mandatory when using ZFS because otherwise, ZFS will
require you to force-import your ZFS pools if you want to use them, which is
undesirable because it disables some of the checks that ZFS does to make sure it
is safe to import a ZFS pool.
The /etc/hostid file must also exist when booting the initrd, before the SPL
kernel module is loaded, so that ZFS picks up the hostid correctly.
The complexity in creating the /etc/hostid file is due to having to
write the host ID as a 32-bit binary value, taking into account the
endianness of the machine, while using only shell commands and/or simple
utilities (to avoid exploding the size of the initrd).
2014-10-23 02:59:06 +00:00
|
|
|
|
|
|
|
|
|
setHostId = optionalString (config.networking.hostId != null) ''
|
|
|
|
|
hi="${config.networking.hostId}"
|
|
|
|
|
${if pkgs.stdenv.isBigEndian then ''
|
|
|
|
|
echo -ne "\x''${hi:0:2}\x''${hi:2:2}\x''${hi:4:2}\x''${hi:6:2}" > /etc/hostid
|
|
|
|
|
'' else ''
|
|
|
|
|
echo -ne "\x''${hi:6:2}\x''${hi:4:2}\x''${hi:2:2}\x''${hi:0:2}" > /etc/hostid
|
|
|
|
|
''}
|
|
|
|
|
'';
|
2008-08-08 17:34:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The closure of the init script of boot stage 1 is what we put in
|
|
|
|
|
# the initial RAM disk.
|
|
|
|
|
initialRamdisk = pkgs.makeInitrd {
|
2019-04-13 21:22:56 +00:00
|
|
|
|
name = "initrd-${kernel-name}";
|
2020-08-22 22:12:42 +00:00
|
|
|
|
inherit (config.boot.initrd) compressor compressorArgs prepend;
|
2013-02-19 16:55:39 +00:00
|
|
|
|
|
2010-06-01 16:38:42 +00:00
|
|
|
|
contents =
|
|
|
|
|
[ { object = bootStage1;
|
|
|
|
|
symlink = "/init";
|
|
|
|
|
}
|
2012-03-13 13:26:21 +00:00
|
|
|
|
{ object = pkgs.writeText "mdadm.conf" config.boot.initrd.mdadmConf;
|
|
|
|
|
symlink = "/etc/mdadm.conf";
|
|
|
|
|
}
|
2018-11-08 10:59:03 +00:00
|
|
|
|
{ object = pkgs.runCommand "initrd-kmod-blacklist-ubuntu" {
|
|
|
|
|
src = "${pkgs.kmod-blacklist-ubuntu}/modprobe.conf";
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
} ''
|
2014-08-08 08:28:26 +00:00
|
|
|
|
target=$out
|
2018-02-28 02:14:48 +00:00
|
|
|
|
${pkgs.buildPackages.perl}/bin/perl -0pe 's/## file: iwlwifi.conf(.+?)##/##/s;' $src > $out
|
2014-08-08 08:28:26 +00:00
|
|
|
|
'';
|
|
|
|
|
symlink = "/etc/modprobe.d/ubuntu.conf";
|
|
|
|
|
}
|
2015-09-05 23:11:03 +00:00
|
|
|
|
{ object = pkgs.kmod-debian-aliases;
|
|
|
|
|
symlink = "/etc/modprobe.d/debian.conf";
|
|
|
|
|
}
|
2013-09-26 15:33:52 +00:00
|
|
|
|
];
|
2008-08-08 17:34:30 +00:00
|
|
|
|
};
|
2011-08-24 21:24:39 +00:00
|
|
|
|
|
2017-04-02 18:45:44 +00:00
|
|
|
|
# Script to add secret files to the initrd at bootloader update time
|
|
|
|
|
initialRamdiskSecretAppender =
|
2020-08-22 22:12:42 +00:00
|
|
|
|
let
|
|
|
|
|
compressorExe = initialRamdisk.compressorExecutableFunction pkgs;
|
|
|
|
|
in pkgs.writeScriptBin "append-initrd-secrets"
|
2017-04-02 18:45:44 +00:00
|
|
|
|
''
|
|
|
|
|
#!${pkgs.bash}/bin/bash -e
|
|
|
|
|
function usage {
|
|
|
|
|
echo "USAGE: $0 INITRD_FILE" >&2
|
|
|
|
|
echo "Appends this configuration's secrets to INITRD_FILE" >&2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$1"x = "--helpx" ]; then
|
|
|
|
|
usage
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
${lib.optionalString (config.boot.initrd.secrets == {})
|
|
|
|
|
"exit 0"}
|
|
|
|
|
|
|
|
|
|
export PATH=${pkgs.coreutils}/bin:${pkgs.cpio}/bin:${pkgs.gzip}/bin:${pkgs.findutils}/bin
|
|
|
|
|
|
|
|
|
|
function cleanup {
|
|
|
|
|
if [ -n "$tmp" -a -d "$tmp" ]; then
|
|
|
|
|
rm -fR "$tmp"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
|
|
tmp=$(mktemp -d initrd-secrets.XXXXXXXXXX)
|
|
|
|
|
|
|
|
|
|
${lib.concatStringsSep "\n" (mapAttrsToList (dest: source:
|
|
|
|
|
let source' = if source == null then dest else toString source; in
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $(dirname "$tmp/${dest}")
|
|
|
|
|
cp -a ${source'} "$tmp/${dest}"
|
|
|
|
|
''
|
|
|
|
|
) config.boot.initrd.secrets)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 21:24:59 +00:00
|
|
|
|
(cd "$tmp" && find . -print0 | sort -z | cpio -o -H newc -R +0:+0 --reproducible --null) | \
|
2020-08-22 22:12:42 +00:00
|
|
|
|
${compressorExe} ${lib.escapeShellArgs initialRamdisk.compressorArgs} >> "$1"
|
2017-04-02 18:45:44 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
in
|
2009-05-27 08:14:06 +00:00
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
{
|
|
|
|
|
options = {
|
2009-05-27 09:46:50 +00:00
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.resumeDevice = mkOption {
|
2014-09-24 01:04:26 +00:00
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
|
|
|
|
example = "/dev/sda3";
|
2013-10-30 16:28:12 +00:00
|
|
|
|
description = ''
|
2014-09-24 01:04:26 +00:00
|
|
|
|
Device for manual resume attempt during boot. This should be used primarily
|
2014-12-10 10:49:25 +00:00
|
|
|
|
if you want to resume from file. If left empty, the swap partitions are used.
|
|
|
|
|
Specify here the device where the file resides.
|
|
|
|
|
You should also use <varname>boot.kernelParams</varname> to specify
|
2014-09-24 01:04:26 +00:00
|
|
|
|
<literal><replaceable>resume_offset</replaceable></literal>.
|
2013-10-30 16:28:12 +00:00
|
|
|
|
'';
|
2013-09-04 11:05:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-02-14 13:11:33 +00:00
|
|
|
|
boot.initrd.enable = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = !config.boot.isContainer;
|
|
|
|
|
defaultText = "!config.boot.isContainer";
|
|
|
|
|
description = ''
|
|
|
|
|
Whether to enable the NixOS initial RAM disk (initrd). This may be
|
|
|
|
|
needed to perform some initialisation tasks (like mounting
|
|
|
|
|
network/encrypted file systems) before continuing the boot process.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-25 19:03:03 +00:00
|
|
|
|
boot.initrd.prepend = mkOption {
|
|
|
|
|
default = [ ];
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
description = ''
|
|
|
|
|
Other initrd files to prepend to the final initrd we are building.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.initrd.checkJournalingFS = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
2013-10-30 16:37:45 +00:00
|
|
|
|
Whether to run <command>fsck</command> on journaling filesystems such as ext3.
|
2013-09-04 11:05:09 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
boot.initrd.mdadmConf = mkOption {
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
2013-10-30 16:37:45 +00:00
|
|
|
|
Contents of <filename>/etc/mdadm.conf</filename> in stage 1.
|
2013-09-04 11:05:09 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
boot.initrd.preLVMCommands = mkOption {
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
2013-10-30 16:37:45 +00:00
|
|
|
|
Shell commands to be executed immediately before LVM discovery.
|
2013-09-04 11:05:09 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2016-01-04 15:53:42 +00:00
|
|
|
|
boot.initrd.preDeviceCommands = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.lines;
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed before udev is started to create
|
|
|
|
|
device nodes.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.initrd.postDeviceCommands = mkOption {
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed immediately after stage 1 of the
|
|
|
|
|
boot has loaded kernel modules and created device nodes in
|
2013-10-30 16:37:45 +00:00
|
|
|
|
<filename>/dev</filename>.
|
2013-09-04 11:05:09 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
boot.initrd.postMountCommands = mkOption {
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed immediately after the stage 1
|
|
|
|
|
filesystems have been mounted.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-06 21:44:02 +00:00
|
|
|
|
boot.initrd.preFailCommands = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.lines;
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed before the failure prompt is shown.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.initrd.extraUtilsCommands = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed in the builder of the
|
|
|
|
|
extra-utils derivation. This can be used to provide
|
|
|
|
|
additional utilities in the initial ramdisk.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2009-05-27 08:14:06 +00:00
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.initrd.extraUtilsCommandsTest = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
default = "";
|
2013-10-30 16:37:45 +00:00
|
|
|
|
type = types.lines;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed in the builder of the
|
|
|
|
|
extra-utils derivation after patchelf has done its
|
|
|
|
|
job. This can be used to test additional utilities
|
|
|
|
|
copied in extraUtilsCommands.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-10 21:08:10 +00:00
|
|
|
|
boot.initrd.extraUdevRulesCommands = mkOption {
|
|
|
|
|
internal = true;
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.lines;
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands to be executed in the builder of the
|
|
|
|
|
udev-rules derivation. This can be used to add
|
|
|
|
|
additional udev rules in the initial ramdisk.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
boot.initrd.compressor = mkOption {
|
2020-08-22 22:12:42 +00:00
|
|
|
|
default = "gzip";
|
|
|
|
|
type = types.unspecified; # We don't have a function type...
|
|
|
|
|
description = ''
|
|
|
|
|
The compressor to use on the initrd image. May be any of:
|
|
|
|
|
|
2020-12-17 21:59:49 +00:00
|
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem><para>The name of one of the predefined compressors, see <filename>pkgs/build-support/kernel/initrd-compressor-meta.nix</filename> for the definitions.</para></listitem>
|
|
|
|
|
<listitem><para>A function which, given the nixpkgs package set, returns the path to a compressor tool, e.g. <literal>pkgs: "''${pkgs.pigz}/bin/pigz"</literal></para></listitem>
|
|
|
|
|
<listitem><para>(not recommended, because it does not work when cross-compiling) the full path to a compressor tool, e.g. <literal>"''${pkgs.pigz}/bin/pigz"</literal></para></listitem>
|
|
|
|
|
</itemizedlist>
|
2020-08-22 22:12:42 +00:00
|
|
|
|
|
|
|
|
|
The given program should read data from stdin and write it to stdout compressed.
|
|
|
|
|
'';
|
2013-09-04 11:05:09 +00:00
|
|
|
|
example = "xz";
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-22 22:12:42 +00:00
|
|
|
|
boot.initrd.compressorArgs = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
|
description = "Arguments to pass to the compressor for the initrd image, or null to use the compressor's defaults.";
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-02 18:45:44 +00:00
|
|
|
|
boot.initrd.secrets = mkOption
|
2020-04-18 20:01:30 +00:00
|
|
|
|
{ default = {};
|
2017-04-02 18:45:44 +00:00
|
|
|
|
type = types.attrsOf (types.nullOr types.path);
|
|
|
|
|
description =
|
|
|
|
|
''
|
|
|
|
|
Secrets to append to the initrd. The attribute name is the
|
|
|
|
|
path the secret should have inside the initrd, the value
|
|
|
|
|
is the path it should be copied from (or null for the same
|
|
|
|
|
path inside and out).
|
|
|
|
|
'';
|
|
|
|
|
example = literalExample
|
|
|
|
|
''
|
|
|
|
|
{ "/etc/dropbear/dropbear_rsa_host_key" =
|
|
|
|
|
./secret-dropbear-key;
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-28 20:31:18 +00:00
|
|
|
|
boot.initrd.supportedFilesystems = mkOption {
|
|
|
|
|
default = [ ];
|
|
|
|
|
example = [ "btrfs" ];
|
2015-06-15 16:18:46 +00:00
|
|
|
|
type = types.listOf types.str;
|
2014-07-28 20:31:18 +00:00
|
|
|
|
description = "Names of supported filesystem types in the initial ramdisk.";
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-02 18:45:44 +00:00
|
|
|
|
boot.loader.supportsInitrdSecrets = mkOption
|
|
|
|
|
{ internal = true;
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description =
|
|
|
|
|
''
|
|
|
|
|
Whether the bootloader setup runs append-initrd-secrets.
|
|
|
|
|
If not, any needed secrets must be copied into the initrd
|
|
|
|
|
and thus added to the store.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
fileSystems = mkOption {
|
2020-08-22 23:28:45 +00:00
|
|
|
|
type = with lib.types; attrsOf (submodule {
|
2019-01-26 19:44:05 +00:00
|
|
|
|
options.neededForBoot = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
2020-07-27 00:05:21 +00:00
|
|
|
|
If set, this file system will be mounted in the initial ramdisk.
|
|
|
|
|
Note that the file system will always be mounted in the initial
|
|
|
|
|
ramdisk if its mount point is one of the following:
|
|
|
|
|
${concatStringsSep ", " (
|
|
|
|
|
forEach utils.pathsNeededForBoot (i: "<filename>${i}</filename>")
|
|
|
|
|
)}.
|
2019-01-26 19:44:05 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
});
|
2013-09-04 11:05:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-14 13:11:33 +00:00
|
|
|
|
config = mkIf config.boot.initrd.enable {
|
2014-12-10 10:49:25 +00:00
|
|
|
|
assertions = [
|
2015-11-25 19:09:09 +00:00
|
|
|
|
{ assertion = any (fs: fs.mountPoint == "/") fileSystems;
|
2013-11-18 15:26:39 +00:00
|
|
|
|
message = "The ‘fileSystems’ option does not specify your root file system.";
|
2014-12-10 10:49:25 +00:00
|
|
|
|
}
|
|
|
|
|
{ assertion = let inherit (config.boot) resumeDevice; in
|
|
|
|
|
resumeDevice == "" || builtins.substring 0 1 resumeDevice == "/";
|
|
|
|
|
message = "boot.resumeDevice has to be an absolute path."
|
|
|
|
|
+ " Old \"x:y\" style is no longer supported.";
|
|
|
|
|
}
|
2020-04-11 15:24:55 +00:00
|
|
|
|
# TODO: remove when #85000 is fixed
|
|
|
|
|
{ assertion = !config.boot.loader.supportsInitrdSecrets ->
|
|
|
|
|
all (source:
|
|
|
|
|
builtins.isPath source ||
|
2020-06-07 16:43:15 +00:00
|
|
|
|
(builtins.isString source && hasPrefix builtins.storeDir source))
|
2020-04-11 15:24:55 +00:00
|
|
|
|
(attrValues config.boot.initrd.secrets);
|
|
|
|
|
message = ''
|
|
|
|
|
boot.loader.initrd.secrets values must be unquoted paths when
|
|
|
|
|
using a bootloader that doesn't natively support initrd
|
|
|
|
|
secrets, e.g.:
|
|
|
|
|
|
|
|
|
|
boot.initrd.secrets = {
|
|
|
|
|
"/etc/secret" = /path/to/secret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Note that this will result in all secrets being stored
|
|
|
|
|
world-readable in the Nix store!
|
|
|
|
|
'';
|
|
|
|
|
}
|
2014-12-10 10:49:25 +00:00
|
|
|
|
];
|
|
|
|
|
|
2017-04-02 18:45:44 +00:00
|
|
|
|
system.build =
|
|
|
|
|
{ inherit bootStage1 initialRamdisk initialRamdiskSecretAppender extraUtils; };
|
2013-09-04 11:05:09 +00:00
|
|
|
|
|
|
|
|
|
system.requiredKernelConfig = with config.lib.kernelConfig; [
|
|
|
|
|
(isYes "TMPFS")
|
|
|
|
|
(isYes "BLK_DEV_INITRD")
|
|
|
|
|
];
|
|
|
|
|
|
2014-07-30 13:52:03 +00:00
|
|
|
|
boot.initrd.supportedFilesystems = map (fs: fs.fsType) fileSystems;
|
2014-07-28 20:31:18 +00:00
|
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
|
};
|
2006-11-02 22:48:01 +00:00
|
|
|
|
}
|