From bdfa5122ea1b56e41768853b859ff16758287919 Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Thu, 19 Sep 2019 01:27:42 +0900 Subject: [PATCH] Convert libvirtd to use make-disk-image This fixes #931 by reusing the image builder of . This image builder produces nixos.qcow2 instead of disk.qcow2. This image is now "baseless", so it obsoletes "qemu-img rebase". We can also remove our special-case image builder libvirtd-image.nix as a whole. --- nix/libvirtd-image.nix | 89 -------------------------------- nix/libvirtd.nix | 38 +++++--------- nixopsvirtd/backends/libvirtd.py | 6 +-- 3 files changed, 13 insertions(+), 120 deletions(-) delete mode 100644 nix/libvirtd-image.nix diff --git a/nix/libvirtd-image.nix b/nix/libvirtd-image.nix deleted file mode 100644 index 1325f1a..0000000 --- a/nix/libvirtd-image.nix +++ /dev/null @@ -1,89 +0,0 @@ -{ system ? builtins.currentSystem, size ? "10" }: -let - pkgs = import {}; - config = (import { - inherit system; - modules = [ { - fileSystems."/".device = "/dev/disk/by-label/nixos"; - - boot.loader.grub.version = 2; - boot.loader.grub.device = "/dev/sda"; - boot.loader.timeout = 0; - - services.openssh.enable = true; - services.openssh.startWhenNeeded = false; - services.openssh.extraConfig = "UseDNS no"; - } ]; - }).config; - -in pkgs.vmTools.runInLinuxVM ( - pkgs.runCommand "libvirtd-image" - { memSize = 768; - preVM = - '' - mkdir $out - diskImage=$out/image - ${pkgs.vmTools.qemu}/bin/qemu-img create -f qcow2 $diskImage "${size}G" - mv closure xchg/ - ''; - postVM = - '' - mv $diskImage $out/disk.qcow2 - ''; - buildInputs = [ pkgs.utillinux pkgs.perl ]; - exportReferencesGraph = - [ "closure" config.system.build.toplevel ]; - } - '' - # Create a single / partition. - ${pkgs.parted}/sbin/parted /dev/vda mklabel msdos - ${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s - - # Create an empty filesystem and mount it. - ${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda1 - ${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1 - mkdir /mnt - mount /dev/vda1 /mnt - - # The initrd expects these directories to exist. - mkdir /mnt/dev /mnt/proc /mnt/sys - mount --bind /proc /mnt/proc - mount --bind /dev /mnt/dev - mount --bind /sys /mnt/sys - - # Copy all paths in the closure to the filesystem. - storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure) - - echo "filling Nix store..." - mkdir -p /mnt/nix/store - set -f - cp -prd $storePaths /mnt/nix/store/ - - mkdir -p /mnt/etc/nix - echo 'build-users-group = ' > /mnt/etc/nix/nix.conf - - # Register the paths in the Nix database. - printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \ - chroot /mnt ${config.nix.package.out}/bin/nix-store --load-db - - # Create the system profile to allow nixos-rebuild to work. - chroot /mnt ${config.nix.package.out}/bin/nix-env \ - -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel} - - # `nixos-rebuild' requires an /etc/NIXOS. - mkdir -p /mnt/etc/nixos - touch /mnt/etc/NIXOS - - # `switch-to-configuration' requires a /bin/sh - mkdir -p /mnt/bin - ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh - - # Generate the GRUB menu. - ln -s vda /dev/sda - chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot - - umount /mnt/proc /mnt/dev /mnt/sys - umount /mnt - '' -) - diff --git a/nix/libvirtd.nix b/nix/libvirtd.nix index 03eb128..e956bd4 100644 --- a/nix/libvirtd.nix +++ b/nix/libvirtd.nix @@ -3,33 +3,19 @@ with lib; let - sz = toString config.deployment.libvirtd.baseImageSize; - base_image = import ./libvirtd-image.nix { size = sz; }; the_key = builtins.getEnv "NIXOPS_LIBVIRTD_PUBKEY"; - ssh_image = pkgs.vmTools.runInLinuxVM ( - pkgs.runCommand "libvirtd-ssh-image" - { memSize = 768; - preVM = - '' - mkdir $out - diskImage=$out/image - ${pkgs.vmTools.qemu}/bin/qemu-img create -f qcow2 -b ${base_image}/disk.qcow2 $diskImage - ''; - buildInputs = [ pkgs.utillinux ]; - postVM = - '' - mv $diskImage $out/disk.qcow2 - ''; - } - '' - mkdir /mnt - mount /dev/vda1 /mnt - - mkdir -p /mnt/etc/ssh/authorized_keys.d - echo '${the_key}' > /mnt/etc/ssh/authorized_keys.d/root - umount /mnt - '' - ); + ssh_image = import { + name = "libvirtd-ssh-image"; + format = "qcow2"; + diskSize = config.deployment.libvirtd.baseImageSize * 1024; + config = config; + contents = [{ + source = (pkgs.writeText "authorized_keys.d-root" the_key); + target = "/etc/ssh/authorized_keys.d/root"; + }]; + lib = pkgs.lib; + inherit pkgs; + }; in { diff --git a/nixopsvirtd/backends/libvirtd.py b/nixopsvirtd/backends/libvirtd.py index 1e58ffb..e383b40 100644 --- a/nixopsvirtd/backends/libvirtd.py +++ b/nixopsvirtd/backends/libvirtd.py @@ -192,11 +192,7 @@ def _prepare_storage_volume(self): capture_stdout=True, env=newEnv).rstrip() temp_disk_path = os.path.join(self.depl.tempdir, 'disk-{}.qcow2'.format(self.name)) - shutil.copyfile(os.path.join(temp_image_path, 'disk.qcow2'), temp_disk_path) - # Rebase onto empty backing file to prevent breaking the disk image - # when the backing file gets garbage collected. - self._logged_exec(["qemu-img", "rebase", "-f", "qcow2", "-b", - "", temp_disk_path]) + shutil.copyfile(os.path.join(temp_image_path, 'nixos.qcow2'), temp_disk_path) self.logger.log("uploading disk image...") image_info = self._get_image_info(temp_disk_path)