vm-templating.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. QEMU VM templating
  2. ==================
  3. This document explains how to use VM templating in QEMU.
  4. For now, the focus is on VM memory aspects, and not about how to save and
  5. restore other VM state (i.e., migrate-to-file with ``x-ignore-shared``).
  6. Overview
  7. --------
  8. With VM templating, a single template VM serves as the starting point for
  9. new VMs. This allows for fast and efficient replication of VMs, resulting
  10. in fast startup times and reduced memory consumption.
  11. Conceptually, the VM state is frozen, to then be used as a basis for new
  12. VMs. The Copy-On-Write mechanism in the operating systems makes sure that
  13. new VMs are able to read template VM memory; however, any modifications
  14. stay private and don't modify the original template VM or any other
  15. created VM.
  16. !!! Security Alert !!!
  17. ----------------------
  18. When effectively cloning VMs by VM templating, hardware identifiers
  19. (such as UUIDs and NIC MAC addresses), and similar data in the guest OS
  20. (such as machine IDs, SSH keys, certificates) that are supposed to be
  21. *unique* are no longer unique, which can be a security concern.
  22. Please be aware of these implications and how to mitigate them for your
  23. use case, which might involve vmgenid, hot(un)plug of NIC, etc..
  24. Memory configuration
  25. --------------------
  26. In order to create the template VM, we have to make sure that VM memory
  27. ends up in a file, from where it can be reused for the new VMs:
  28. Supply VM RAM via memory-backend-file, with ``share=on`` (modifications go
  29. to the file) and ``readonly=off`` (open the file writable). Note that
  30. ``readonly=off`` is implicit.
  31. In the following command-line example, a 2GB VM is created, whereby VM RAM
  32. is to be stored in the ``template`` file.
  33. .. parsed-literal::
  34. |qemu_system| [...] -m 2g \\
  35. -object memory-backend-file,id=pc.ram,mem-path=template,size=2g,share=on,... \\
  36. -machine q35,memory-backend=pc.ram
  37. If multiple memory backends are used (vNUMA, DIMMs), configure all
  38. memory backends accordingly.
  39. Once the VM is in the desired state, stop the VM and save other VM state,
  40. leaving the current state of VM RAM reside in the file.
  41. In order to have a new VM be based on a template VM, we have to
  42. configure VM RAM to be based on a template VM RAM file; however, the VM
  43. should not be able to modify file content.
  44. Supply VM RAM via memory-backend-file, with ``share=off`` (modifications
  45. stay private), ``readonly=on`` (open the file readonly) and ``rom=off``
  46. (don't make the memory readonly for the VM). Note that ``share=off`` is
  47. implicit and that other VM state has to be restored separately.
  48. In the following command-line example, a 2GB VM is created based on the
  49. existing 2GB file ``template``.
  50. .. parsed-literal::
  51. |qemu_system| [...] -m 2g \\
  52. -object memory-backend-file,id=pc.ram,mem-path=template,size=2g,readonly=on,rom=off,... \\
  53. -machine q35,memory-backend=pc.ram
  54. If multiple memory backends are used (vNUMA, DIMMs), configure all
  55. memory backends accordingly.
  56. Note that ``-mem-path`` cannot be used for VM templating when creating the
  57. template VM or when starting new VMs based on a template VM.
  58. Incompatible features
  59. ---------------------
  60. Some features are incompatible with VM templating, as the underlying file
  61. cannot be modified to discard VM RAM, or to actually share memory with
  62. another process.
  63. vhost-user and multi-process QEMU
  64. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. vhost-user and multi-process QEMU are incompatible with VM templating.
  66. These technologies rely on shared memory, however, the template VMs
  67. don't actually share memory (``share=off``), even though they are
  68. file-based.
  69. virtio-balloon
  70. ~~~~~~~~~~~~~~
  71. virtio-balloon inflation and "free page reporting" cannot discard VM RAM
  72. and will repeatedly report errors. While virtio-balloon can be used
  73. for template VMs (e.g., report VM RAM stats), "free page reporting"
  74. should be disabled and the balloon should not be inflated.
  75. virtio-mem
  76. ~~~~~~~~~~
  77. virtio-mem cannot discard VM RAM that is managed by the virtio-mem
  78. device. virtio-mem will fail early when realizing the device. To use
  79. VM templating with virtio-mem, either hotplug virtio-mem devices to the
  80. new VM, or don't supply any memory to the template VM using virtio-mem
  81. (requested-size=0), not using a template VM file as memory backend for the
  82. virtio-mem device.
  83. VM migration
  84. ~~~~~~~~~~~~
  85. For VM migration, "x-release-ram" similarly relies on discarding of VM
  86. RAM on the migration source to free up migrated RAM, and will
  87. repeatedly report errors.
  88. Postcopy live migration fails discarding VM RAM on the migration
  89. destination early and refuses to activate postcopy live migration. Note
  90. that postcopy live migration usually only works on selected filesystems
  91. (shmem/tmpfs, hugetlbfs) either way.