Skip to content

Commit

Permalink
libvirt: When building image with boot_config != null, build image in…
Browse files Browse the repository at this point in the history
… two stages to share the first build
  • Loading branch information
Nadrieril committed Apr 20, 2017
1 parent 314bef1 commit 68231c1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
92 changes: 87 additions & 5 deletions nix/libvirtd-image.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
rec {
let
makeconfig = { system, config }:
(import <nixpkgs/nixos/lib/eval-config.nix> {
inherit system;
modules = [ config ];
}).config;

in rec {
base_image_config = {
fileSystems."/".device = "/dev/disk/by-label/nixos";

Expand All @@ -15,10 +22,7 @@ rec {
config ? base_image_config
}:
let
cfg = (import <nixpkgs/nixos/lib/eval-config.nix> {
inherit system;
modules = [ config ];
}).config;
cfg = makeconfig { inherit system config; };

in pkgs.vmTools.runInLinuxVM (
# TODO: Use <nixpkgs/nixos/lib/make-disk-image.nix> when
Expand Down Expand Up @@ -129,4 +133,82 @@ rec {
);


deploy_in_nixos_image = {
system ? builtins.currentSystem,
pkgs ? import <nixpkgs> {},
base_image,
config
}:
let
cfg = makeconfig { inherit system config; };

in pkgs.vmTools.runInLinuxVM (
pkgs.runCommand "libvirtd-deploy-in-nixos-image"
{ memSize = 768;
preVM =
''
mkdir $out
diskImage=$out/image
${pkgs.vmTools.qemu}/bin/qemu-img create -f qcow2 -b ${base_image}/disk.qcow2 $diskImage
mv closure xchg/
'';
postVM =
''
mv $diskImage $out/disk.qcow2
'';
buildInputs = [ pkgs.utillinux pkgs.perl ];
exportReferencesGraph =
[ "closure" cfg.system.build.toplevel ];
}
''
. /sys/class/block/vda1/uevent
mknod /dev/vda1 b $MAJOR $MINOR
mkdir /mnt
mount /dev/vda1 /mnt
# The initrd expects these directories to exist.
mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
# Avoid "groups does not exist" warnings
mkdir -p /etc/nix
echo 'build-users-group = ' > /etc/nix/nix.conf
mkdir -p /mnt/etc/nix
echo 'build-users-group = ' > /mnt/etc/nix/nix.conf
sourceStore='${pkgs.nix}/bin/nix-store'
targetStore='chroot /mnt ${cfg.nix.package.out}/bin/nix-store'
echo "filling Nix store..."
set -f
# Copy missing paths in the closure to the target nix store.
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
missing=$(NIX_DB_DIR=/mnt/nix/var/nix/db $sourceStore --check-validity --print-invalid $storePaths)
for path in $missing; do
${pkgs.rsync}/bin/rsync -a $path /mnt/nix/store/
done
# Register the paths in the Nix database.
$targetStore --register-validity < /tmp/xchg/closure
# TODO: Replace with the following
# when https://github.com/NixOS/nix/issues/1134 is fixed
#printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | $sourceStore --load-db
#$sourceStore --export $missing | $targetStore --import
# Create the system profile to allow nixos-rebuild to work.
chroot /mnt ${cfg.nix.package.out}/bin/nix-env \
-p /nix/var/nix/profiles/system --set ${cfg.system.build.toplevel}
# Generate the GRUB menu.
ln -s vda /dev/sda
chroot /mnt ${cfg.system.build.toplevel}/bin/switch-to-configuration boot
umount /mnt/proc /mnt/dev /mnt/sys
umount /mnt
''
);
}
17 changes: 8 additions & 9 deletions nix/libvirtd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ let
services.openssh.extraConfig = "UseDNS no";
};

ssh_image = if config.deployment.libvirtd.boot_config == null then
let base_image = libvirt_image_helpers.create_nixos_image {
size = sz;
config = base_config;
};
base_image = libvirt_image_helpers.create_nixos_image {
size = sz;
config = base_config;
};

in libvirt_image_helpers.edit_image {
ssh_image = if config.deployment.libvirtd.boot_config == null then
libvirt_image_helpers.edit_image {
inherit pkgs base_image;
cmd = ''
mkdir -p /mnt/etc/ssh/authorized_keys.d
Expand All @@ -28,9 +28,8 @@ let
}

else
libvirt_image_helpers.create_nixos_image {
inherit pkgs;
size = sz;
libvirt_image_helpers.deploy_in_nixos_image {
inherit pkgs base_image;
config.imports = [
base_config
{ users.users.root.openssh.authorizedKeys.keys = [ ssh_pubkey ]; }
Expand Down

0 comments on commit 68231c1

Please sign in to comment.