mirror of
https://github.com/nix-community/disko.git
synced 2026-12-03 14:33:28 +01:00
make-disk-image: add impure variant
This commit is contained in:
1 parent
493b347d8f
commit
ab5f8eea75
2 files changed
+112
-29
No files matched your search
+4
-1
@@ -11,7 +11,10 @@ let
|
|||||||
diskoLib = {
|
diskoLib = {
|
||||||
|
|
||||||
# like make-disk-image.nix from nixpkgs, but with disko config
|
# like make-disk-image.nix from nixpkgs, but with disko config
|
||||||
makeDiskImage = args: import ./make-disk-image.nix ({ inherit diskoLib; } // args);
|
makeDiskImage = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).pure;
|
||||||
|
|
||||||
|
# a version of makeDiskImage which runs outside of the store
|
||||||
|
makeDiskImageScript = args: (import ./make-disk-image.nix ({ inherit diskoLib; } // args)).impure;
|
||||||
|
|
||||||
testLib = import ./tests.nix { inherit lib makeTest eval-config; };
|
testLib = import ./tests.nix { inherit lib makeTest eval-config; };
|
||||||
# like lib.types.oneOf but instead of a list takes an attrset
|
# like lib.types.oneOf but instead of a list takes an attrset
|
||||||
|
|||||||
+108
-28
@@ -4,6 +4,7 @@
|
|||||||
, lib ? pkgs.lib
|
, lib ? pkgs.lib
|
||||||
, name ? "${nixosConfig.config.networking.hostName}-disko-images"
|
, name ? "${nixosConfig.config.networking.hostName}-disko-images"
|
||||||
, extraPostVM ? ""
|
, extraPostVM ? ""
|
||||||
|
, checked ? false
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
cleanedConfig = diskoLib.testLib.prepareDiskoConfig nixosConfig.config diskoLib.testLib.devices;
|
cleanedConfig = diskoLib.testLib.prepareDiskoConfig nixosConfig.config diskoLib.testLib.devices;
|
||||||
@@ -13,46 +14,125 @@ let
|
|||||||
boot.loader.grub.devices = lib.mkForce cleanedConfig.boot.loader.grub.devices;
|
boot.loader.grub.devices = lib.mkForce cleanedConfig.boot.loader.grub.devices;
|
||||||
}];
|
}];
|
||||||
};
|
};
|
||||||
in
|
dependencies = with pkgs; [
|
||||||
pkgs.vmTools.runInLinuxVM (pkgs.runCommand name {
|
bash
|
||||||
buildInputs = with pkgs; [
|
coreutils
|
||||||
|
gnused
|
||||||
systemdMinimal
|
systemdMinimal
|
||||||
nixos-install-tools
|
nixos-install-tools
|
||||||
nix
|
nix
|
||||||
utillinux
|
utillinux
|
||||||
];
|
];
|
||||||
preVM = ''
|
preVM = ''
|
||||||
# TODO: get size either dynamically or from disko config
|
|
||||||
${lib.concatMapStringsSep "\n" (disk: "truncate -s ${disk.imageSize} ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)}
|
${lib.concatMapStringsSep "\n" (disk: "truncate -s ${disk.imageSize} ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)}
|
||||||
'';
|
'';
|
||||||
postVM = ''
|
postVM = ''
|
||||||
mkdir -p $out
|
# shellcheck disable=SC2154
|
||||||
${lib.concatMapStringsSep "\n" (disk: "cp ${disk.name}.raw $out/${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)}
|
mkdir -p "$out"
|
||||||
|
${lib.concatMapStringsSep "\n" (disk: "cp ${disk.name}.raw \"$out\"/${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)}
|
||||||
${extraPostVM}
|
${extraPostVM}
|
||||||
'';
|
'';
|
||||||
|
builder = ''
|
||||||
|
# running udev, stolen from stage-1.sh
|
||||||
|
echo "running udev..."
|
||||||
|
ln -sfn /proc/self/fd /dev/fd
|
||||||
|
ln -sfn /proc/self/fd/0 /dev/stdin
|
||||||
|
ln -sfn /proc/self/fd/1 /dev/stdout
|
||||||
|
ln -sfn /proc/self/fd/2 /dev/stderr
|
||||||
|
mkdir -p /etc/udev
|
||||||
|
ln -sfn ${systemToInstall.config.system.build.etc}/etc/udev/rules.d /etc/udev/rules.d
|
||||||
|
mkdir -p /dev/.mdadm
|
||||||
|
${pkgs.systemdMinimal}/lib/systemd/systemd-udevd --daemon
|
||||||
|
udevadm trigger --action=add
|
||||||
|
udevadm settle
|
||||||
|
|
||||||
|
# populate nix db, so nixos-install doesn't complain
|
||||||
|
export NIX_STATE_DIR=$TMPDIR/state
|
||||||
|
nix-store --load-db < ${pkgs.closureInfo {
|
||||||
|
rootPaths = [ systemToInstall.config.system.build.toplevel ];
|
||||||
|
}}/registration
|
||||||
|
|
||||||
|
${systemToInstall.config.system.build.diskoScript}
|
||||||
|
${pkgs.nixos-install-tools}/bin/nixos-install --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
|
||||||
|
'';
|
||||||
QEMU_OPTS = lib.concatMapStringsSep " " (disk: "-drive file=${disk.name}.raw,if=virtio,cache=unsafe,werror=report") (lib.attrValues nixosConfig.config.disko.devices.disk);
|
QEMU_OPTS = lib.concatMapStringsSep " " (disk: "-drive file=${disk.name}.raw,if=virtio,cache=unsafe,werror=report") (lib.attrValues nixosConfig.config.disko.devices.disk);
|
||||||
|
in {
|
||||||
|
pure = pkgs.vmTools.runInLinuxVM (pkgs.runCommand name {
|
||||||
|
buildInputs =dependencies;
|
||||||
|
inherit preVM QEMU_OPTS;
|
||||||
|
memSize = 1024;
|
||||||
|
} builder);
|
||||||
|
impure = diskoLib.writeCheckedBash { inherit checked pkgs; } name ''
|
||||||
|
set -efu
|
||||||
|
export PATH=${lib.makeBinPath dependencies}
|
||||||
|
showUsage() {
|
||||||
|
cat <<\USAGE
|
||||||
|
Usage: $script [options]
|
||||||
|
|
||||||
memSize = 1024;
|
Options:
|
||||||
} ''
|
* --pre-format-files <src> <dst>
|
||||||
# running udev, stolen from stage-1.sh
|
copies the src to the dst on the VM, before disko is run
|
||||||
echo "running udev..."
|
This is useful to provide secrets like LUKS keys, or other files you need for formating
|
||||||
ln -sfn /proc/self/fd /dev/fd
|
* --post-format-files <src> <dst>
|
||||||
ln -sfn /proc/self/fd/0 /dev/stdin
|
copies the src to the dst on the finished image.
|
||||||
ln -sfn /proc/self/fd/1 /dev/stdout
|
These end up in the images later and is useful if you want to add some extra stateful files
|
||||||
ln -sfn /proc/self/fd/2 /dev/stderr
|
USAGE
|
||||||
mkdir -p /etc/udev
|
}
|
||||||
ln -sfn ${systemToInstall.config.system.build.etc}/etc/udev/rules.d /etc/udev/rules.d
|
|
||||||
mkdir -p /dev/.mdadm
|
|
||||||
${pkgs.systemdMinimal}/lib/systemd/systemd-udevd --daemon
|
|
||||||
udevadm trigger --action=add
|
|
||||||
udevadm settle
|
|
||||||
|
|
||||||
# populate nix db, so nixos-install doesn't complain
|
export out=$PWD
|
||||||
export NIX_STATE_DIR=$TMPDIR/state
|
TMPDIR=$(mktemp -d); export TMPDIR
|
||||||
nix-store --load-db < ${pkgs.closureInfo {
|
trap 'rm -rf "$TMPDIR"' EXIT
|
||||||
rootPaths = [ systemToInstall.config.system.build.toplevel ];
|
cd "$TMPDIR"
|
||||||
}}/registration
|
|
||||||
|
|
||||||
${systemToInstall.config.system.build.diskoScript}
|
mkdir copy_before_disko copy_after_disko
|
||||||
${pkgs.nixos-install-tools}/bin/nixos-install --system ${systemToInstall.config.system.build.toplevel} --keep-going --no-channel-copy -v --no-root-password --option binary-caches ""
|
|
||||||
'')
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
--pre-format-files)
|
||||||
|
src=$2
|
||||||
|
dst=$3
|
||||||
|
cp --reflink=auto -r "$src" copy_before_disko/"$(echo "$dst" | base64)"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--post-format-files)
|
||||||
|
src=$2
|
||||||
|
dst=$3
|
||||||
|
cp --reflink=auto -r "$src" copy_after_disko/"$(echo "$dst" | base64)"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
showUsage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
export preVM=${diskoLib.writeCheckedBash { inherit pkgs checked; } "preVM.sh" ''
|
||||||
|
set -efu
|
||||||
|
mv copy_before_disko copy_after_disko xchg/
|
||||||
|
${preVM}
|
||||||
|
''}
|
||||||
|
export postVM=${diskoLib.writeCheckedBash { inherit pkgs checked; } "postVM.sh" postVM}
|
||||||
|
export origBuilder=${pkgs.writeScript "disko-builder" ''
|
||||||
|
set -eu
|
||||||
|
export PATH=${lib.makeBinPath dependencies}
|
||||||
|
for src in /tmp/xchg/copy_before_disko/*; do
|
||||||
|
dst=$(basename "$src" | base64 -d)
|
||||||
|
mkdir -p "$(dirname "$dst")"
|
||||||
|
cp -r "$src" "$dst"
|
||||||
|
done
|
||||||
|
set -f
|
||||||
|
${builder}
|
||||||
|
set +f
|
||||||
|
for dir in /tmp/xchg/copy_after_disko/*; do
|
||||||
|
dst=/mnt/$(basename "$src" | base64 -d)
|
||||||
|
mkdir -p "$(dirname "$dst")"
|
||||||
|
cp -r "$src" "$dst"
|
||||||
|
done
|
||||||
|
''}
|
||||||
|
export QEMU_OPTS=${lib.escapeShellArg "${QEMU_OPTS} -m 1024"}
|
||||||
|
${pkgs.bash}/bin/sh -e ${pkgs.vmTools.vmRunCommand pkgs.vmTools.qemuCommandLinux}
|
||||||
|
cd /
|
||||||
|
'';
|
||||||
|
}
|
||||||
Reference in new issue
Block a user