Genode Packages collection https://git.sr.ht/~ehmry/genodepkgs/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

qemu-vm.nix 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. { config, lib, pkgs, ... }:
  2. with lib;
  3. with import ../tests/lib/qemu-flags.nix { inherit pkgs; };
  4. let
  5. qemu = config.system.build.qemu;
  6. cfg = config.virtualisation;
  7. consoles = lib.concatMapStringsSep " " (c: "console=${c}") cfg.qemu.consoles;
  8. efiPrefix = if (pkgs.stdenv.isi686 || pkgs.stdenv.isx86_64) then
  9. "${pkgs.buildPackages.OVMF.fd}/FV/OVMF"
  10. else if pkgs.stdenv.isAarch64 then
  11. "${pkgs.buildPackages.OVMF.fd}/FV/AAVMF"
  12. else
  13. throw "No EFI firmware available for platform";
  14. efiFirmware = "${efiPrefix}_CODE.fd";
  15. efiVarsDefault = "${efiPrefix}_VARS.fd";
  16. # Shell script to start the VM.
  17. startVM = ''
  18. #! ${pkgs.buildPackages.runtimeShell}
  19. NIX_DISK_IMAGE=$(readlink -f ''${NIX_DISK_IMAGE:-${config.virtualisation.diskImage}})
  20. if ! test -w "$NIX_DISK_IMAGE"; then
  21. ${qemu}/bin/qemu-img create -f qcow2 -b $NIX_DISK_IMAGE $TMPDIR/disk.img || exit 1
  22. NIX_DISK_IMAGE=$TMPDIR/disk.img
  23. fi
  24. if ! test -e "$NIX_DISK_IMAGE"; then
  25. ${qemu}/bin/qemu-img create -f qcow2 "$NIX_DISK_IMAGE" \
  26. ${toString config.virtualisation.diskSize}M || exit 1
  27. fi
  28. # Create a directory for storing temporary data of the running VM.
  29. if [ -z "$TMPDIR" -o -z "$USE_TMPDIR" ]; then
  30. TMPDIR=$(mktemp -d nix-vm.XXXXXXXXXX --tmpdir)
  31. fi
  32. # Start QEMU.
  33. set -v
  34. exec ${qemuBinary qemu} \
  35. -name ${config.system.name} \
  36. -m ${toString config.virtualisation.memorySize} \
  37. -smp ${toString config.virtualisation.cores} \
  38. ${concatStringsSep " " config.virtualisation.qemu.networkingOptions} \
  39. ${toString config.virtualisation.qemu.options} \
  40. $NIX_DISK_IMAGE \
  41. $QEMU_OPTS \
  42. "$@"
  43. '';
  44. in {
  45. options = {
  46. virtualisation.memorySize = mkOption {
  47. default = 384;
  48. description = ''
  49. Memory size (M) of virtual machine.
  50. '';
  51. };
  52. virtualisation.diskSize = mkOption {
  53. default = 512;
  54. description = ''
  55. Disk size (M) of virtual machine.
  56. '';
  57. };
  58. virtualisation.diskImage = mkOption {
  59. default = "./${config.system.name}.qcow2";
  60. description = ''
  61. Path to the disk image containing the root filesystem.
  62. The image will be created on startup if it does not
  63. exist.
  64. '';
  65. };
  66. virtualisation.emptyDiskImages = mkOption {
  67. default = [ ];
  68. type = types.listOf types.int;
  69. description = ''
  70. Additional disk images to provide to the VM. The value is
  71. a list of size in megabytes of each disk. These disks are
  72. writeable by the VM.
  73. '';
  74. };
  75. virtualisation.graphics = mkOption {
  76. default = true;
  77. description = ''
  78. Whether to run QEMU with a graphics window, or in nographic mode.
  79. Serial console will be enabled on both settings, but this will
  80. change the preferred console.
  81. '';
  82. };
  83. virtualisation.cores = mkOption {
  84. default = 1;
  85. type = types.int;
  86. description = ''
  87. Specify the number of cores the guest is permitted to use.
  88. The number can be higher than the available cores on the
  89. host system.
  90. '';
  91. };
  92. virtualisation.pathsInNixDB = mkOption {
  93. default = [ ];
  94. description = ''
  95. The list of paths whose closure is registered in the Nix
  96. database in the VM. All other paths in the host Nix store
  97. appear in the guest Nix store as well, but are considered
  98. garbage (because they are not registered in the Nix
  99. database in the guest).
  100. '';
  101. };
  102. virtualisation.vlans = mkOption {
  103. default = [ 1 ];
  104. example = [ 1 2 ];
  105. description = ''
  106. Virtual networks to which the VM is connected. Each
  107. number <replaceable>N</replaceable> in this list causes
  108. the VM to have a virtual Ethernet interface attached to a
  109. separate virtual network on which it will be assigned IP
  110. address
  111. <literal>192.168.<replaceable>N</replaceable>.<replaceable>M</replaceable></literal>,
  112. where <replaceable>M</replaceable> is the index of this VM
  113. in the list of VMs.
  114. '';
  115. };
  116. virtualisation.writableStore = mkOption {
  117. default = true; # FIXME
  118. description = ''
  119. If enabled, the Nix store in the VM is made writable by
  120. layering an overlay filesystem on top of the host's Nix
  121. store.
  122. '';
  123. };
  124. virtualisation.writableStoreUseTmpfs = mkOption {
  125. default = true;
  126. description = ''
  127. Use a tmpfs for the writable store instead of writing to the VM's
  128. own filesystem.
  129. '';
  130. };
  131. networking.primaryIPAddress = mkOption {
  132. default = "";
  133. internal = true;
  134. description = "Primary IP address used in /etc/hosts.";
  135. };
  136. virtualisation.qemu = {
  137. options = mkOption {
  138. type = types.listOf types.unspecified;
  139. default = [ ];
  140. example = [ "-vga std" ];
  141. description = "Options passed to QEMU.";
  142. };
  143. consoles = mkOption {
  144. type = types.listOf types.str;
  145. default = let consoles = [ "${qemuSerialDevice},115200n8" "tty0" ];
  146. in if cfg.graphics then consoles else reverseList consoles;
  147. example = [ "console=tty1" ];
  148. description = ''
  149. The output console devices to pass to the kernel command line via the
  150. <literal>console</literal> parameter, the primary console is the last
  151. item of this list.
  152. By default it enables both serial console and
  153. <literal>tty0</literal>. The preferred console (last one) is based on
  154. the value of <option>virtualisation.graphics</option>.
  155. '';
  156. };
  157. networkingOptions = mkOption {
  158. default = [
  159. "-net nic,netdev=user.0,model=virtio"
  160. "-netdev user,id=user.0\${QEMU_NET_OPTS:+,$QEMU_NET_OPTS}"
  161. ];
  162. type = types.listOf types.str;
  163. description = ''
  164. Networking-related command-line options that should be passed to qemu.
  165. The default is to use userspace networking (slirp).
  166. If you override this option, be advised to keep
  167. ''${QEMU_NET_OPTS:+,$QEMU_NET_OPTS} (as seen in the default)
  168. to keep the default runtime behaviour.
  169. '';
  170. };
  171. diskInterface = mkOption {
  172. default = "ahci";
  173. example = "usb";
  174. type = types.enum [ "ahci" "usb" "virtio" ];
  175. description = "The interface used for the virtual hard disks.";
  176. };
  177. kernel = mkOption {
  178. type = types.path;
  179. description = "Guest kernel.";
  180. };
  181. initrd = mkOption {
  182. type = types.path;
  183. description = "Guest initrd.";
  184. };
  185. cmdline = mkOption {
  186. type = types.str;
  187. description = "Command line options to pass to guest.";
  188. };
  189. };
  190. virtualisation.useBootLoader = mkOption {
  191. default = false;
  192. description = ''
  193. If enabled, the virtual machine will be booted using the
  194. regular boot loader (i.e., GRUB 1 or 2). This allows
  195. testing of the boot loader. If
  196. disabled (the default), the VM directly boots the NixOS
  197. kernel and initial ramdisk, bypassing the boot loader
  198. altogether.
  199. '';
  200. };
  201. virtualisation.useEFIBoot = mkOption {
  202. default = false;
  203. description = ''
  204. If enabled, the virtual machine will provide a EFI boot
  205. manager.
  206. useEFIBoot is ignored if useBootLoader == false.
  207. '';
  208. };
  209. virtualisation.efiVars = mkOption {
  210. default = "./${config.system.name}-efi-vars.fd";
  211. description = ''
  212. Path to nvram image containing UEFI variables. The will be created
  213. on startup if it does not exist.
  214. '';
  215. };
  216. virtualisation.bios = mkOption {
  217. default = null;
  218. type = types.nullOr types.package;
  219. description = ''
  220. An alternate BIOS (such as <package>qboot</package>) with which to start the VM.
  221. Should contain a file named <literal>bios.bin</literal>.
  222. If <literal>null</literal>, QEMU's builtin SeaBIOS will be used.
  223. '';
  224. };
  225. };
  226. config = {
  227. # FIXME: Consolidate this one day.
  228. virtualisation.qemu.options = mkMerge [
  229. (mkIf cfg.useEFIBoot [
  230. "-drive if=pflash,format=raw,unit=0,readonly,file=${efiFirmware}"
  231. "-drive if=pflash,format=raw,unit=1,file=$NIX_EFI_VARS"
  232. ])
  233. (mkIf (cfg.bios != null) [ "-bios ${cfg.bios}/bios.bin" ])
  234. (mkIf (!cfg.graphics) [ "-nographic" ])
  235. ];
  236. system.build.vm = pkgs.runCommand "nixos-vm" { preferLocalBuild = true; } ''
  237. mkdir -p $out/bin
  238. ln -s ${
  239. pkgs.writeScript "run-nixos-vm" startVM
  240. } $out/bin/run-${config.system.name}-vm
  241. '';
  242. };
  243. }