From 8179e5213f76777fd8d45780cce61c40e867dd63 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 18 Jun 2009 16:16:12 +0000 Subject: [PATCH] * A module for building KVM/QEMU virtual machines from a NixOS configuration. No virtual disk image is generated; the VM shares a Nix store with the host. This makes it very fast to build new VMs. svn path=/nixos/branches/modular-nixos/; revision=15999 --- modules/virtualisation/qemu-vm.nix | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 modules/virtualisation/qemu-vm.nix diff --git a/modules/virtualisation/qemu-vm.nix b/modules/virtualisation/qemu-vm.nix new file mode 100644 index 00000000000..eae0541f338 --- /dev/null +++ b/modules/virtualisation/qemu-vm.nix @@ -0,0 +1,61 @@ +# This module creates a virtual machine from the NixOS configuration. +# Building the `config.system.build.vm' attribute gives you a command +# that starts a KVM/QEMU VM running the NixOS configuration defined in +# `config'. The Nix store is shared read-only with the host, which +# makes (re)building VMs very efficient. However, it also means you +# can't reconfigure the guest inside the guest - you need to rebuild +# the VM in the host. On the other hand, the root filesystem is a +# read/writable disk image persistent across VM reboots. + +{config, pkgs, ...}: + +{ + # All the modules the initrd needs to mount the host filesystem via + # CIFS. Also use paravirtualised network and block devices for + # performance. + boot.initrd.extraKernelModules = + ["cifs" "virtio_net" "virtio_pci" "virtio_blk" "virtio_balloon" "nls_utf8"]; + + fileSystems = + [ { mountPoint = "/"; + device = "/dev/vda"; + } + ]; + + # Mount the host filesystem and bind-mount its Nix store into our + # own root FS. + boot.initrd.postMountCommands = + '' + ipconfig 10.0.2.15:::::eth0:none + + mkdir /hostfs + ${pkgs.vmTools.mountCifs}/bin/mount.cifs //10.0.2.4/qemu /hostfs -o guest,username=nobody + + mkdir -p $targetRoot/nix/store + mount --bind /hostfs/nix/store $targetRoot/nix/store + ''; + + # Starting DHCP brings down eth0, which kills the connection to the + # host filesystem and thus deadlocks the system. + networking.useDHCP = false; + + system.build.vm = pkgs.runCommand "nixos-vm" {} + '' + ensureDir $out + ln -s ${config.system.build.system} $out/system + + ensureDir $out/bin + cat > $out/bin/run-nixos-vm <