What Is VM Templating and How To Enable It¶
What is VM templating¶
VM templating is a Kata Containers feature that enables new VM creation using a cloning technique. When enabled, new VMs are created by cloning from a pre-created template VM, and they will share the same initramfs, kernel and agent memory in readonly mode. It is very much like a process fork done by the kernel but here we fork VMs.
How is this different from VMCache¶
Both VMCache and VM templating help speed up new container creation. When VMCache enabled, new VMs are created by the VMCache server. So it is not vulnerable to share memory CVE because each VM doesn't share the memory. VM templating saves a lot of memory if there are many Kata Containers running on the same host.
What are the Pros¶
VM templating helps speed up new container creation and saves a lot of memory if there are many Kata Containers running on the same host. If you are running a density workload, or care a lot about container startup speed, VM templating can be very useful.
In one example, we created 100 Kata Containers each claiming 128MB guest memory and ended up saving 9GB of memory in total when VM templating is enabled, which is about 72% of the total guest memory. See full results here.
In another example, we created ten Kata Containers with containerd shimv2 and calculated the average boot up speed for each of them. The result showed that VM templating speeds up Kata Containers creation by as much as 38.68%. See full results here.
What are the Cons¶
One drawback of VM templating is that it cannot avoid cross-VM side-channel attack such as CVE-2015-2877 that originally targeted at the Linux KSM feature. It was concluded that "Share-until-written approaches for memory conservation among mutually untrusting tenants are inherently detectable for information disclosure, and can be classified as potentially misunderstood behaviors rather than vulnerabilities."
Warning: If you care about such attack vector, do not use VM templating or KSM.
How to enable VM templating¶
VM templating can be enabled by changing your Kata Containers config file (/usr/share/defaults/kata-containers/configuration.toml,
overridden by /etc/kata-containers/configuration.toml if provided) such that:
qemuversionv4.1.0or above is specified inhypervisor.qemu->pathsectionenable_template = trueinitrd =is setimage =option is commented out or removedshared_fsshould not bevirtio-fs
Then you can create a VM templating for later usage by calling
and purge it by callingIf you do not want to call kata-runtime factory init by hand,
the very first Kata container you create will automatically create a VM templating.
Prepare multiple templates with runtime-rs¶
In the runtime configuration, set enable_template = true and set
template_pool to an absolute directory under the hypervisor's factory
table. To select a template based on sandbox resource limits, enable
static_sandbox_resource_mgmt under [runtime].
For Dragonball, use the same runtime.use_passfd_io setting when creating
and restoring templates from a pool. When enabled, runtime.passfd_listener_port
must also match. Pool lookup ignores these settings, so a mismatch can cause
container I/O setup to fail after restore. To change either effective setting,
use a separate pool or destroy and recreate the affected templates.
If each size has its own runtime configuration file, set the same
template_pool in each file and use --config to select the file for each
template. Set default_vcpus and default_memory to the effective VM size,
including guest overhead. Set default_maxvcpus to at least default_vcpus
rounded up to the next whole vCPU (for example, at least 2 for 1.5 vCPUs).
For static sizing, an explicit default_maxmemory limit must cover
default_memory. An explicit --memory-mib override can raise this limit
up to the host's memory capacity.
sudo kata-ctl factory init --config /etc/kata-containers/small.toml
sudo kata-ctl factory init --config /etc/kata-containers/large.toml
To create several sizes from one configuration, use --cpus and
--memory-mib to override the effective vCPU count and memory in MiB for
that command. You can use either option with --config. Omitted values come
from the selected configuration, or the default runtime configuration when
--config is omitted.
sudo kata-ctl factory init --cpus 1 --memory-mib 768
# Static sizing (Pod limits + overhead): ceil(1 + 0.18) = 2 vCPUs, 512 + 32 = 544 MiB.
sudo kata-ctl factory init --cpus 2 --memory-mib 544
A sandbox uses the snapshot that matches its effective hypervisor and agent configuration after resource sizing. Pool matching rounds fractional vCPU counts up to the VM's boot vCPU count and also checks the maximum vCPU count. The sandbox cold-boots if a required snapshot file is missing or the saved VM configuration does not match. Use the same configuration and size options to inspect or remove a single template:
sudo kata-ctl factory status --cpus 2 --memory-mib 544
sudo kata-ctl factory destroy --cpus 2 --memory-mib 544
Each snapshot has its own tmpfs and consumes memory until destroyed.
Before changing a profile or upgrading Kata, run factory destroy for each
template you want to retire, using the same configuration and size options
used to create it. A change to the effective configuration (for example,
kernel_params) can change the directory hash, leaving the old tmpfs mounted
even though factory status and factory destroy now look in a different directory.
To clean up an old entry manually, inspect the vm_config.json files under
<template_pool>/vm-*/ to identify the template. Verify that its directory
is a tmpfs mount point with findmnt --mountpoint "$entry", where entry is
the full path to that directory. Once you have confirmed it is the template
you want to retire, run sudo umount "$entry" && sudo rmdir "$entry".
Configurations without template_pool continue to use the single
template_path directory and its existing snapshots.