KubeVirt Sidecar Hooks#
Overview#
KubeVirt sidecar hooks are sidecar containers injected into the virt-launcher pod that can mutate libvirt domain XML or cloud-init data before a VM starts. They are the primary extension point for per-VM hardware customization (e.g., SMBIOS fields, CPU topology tweaks, cloud-init injection) without modifying KubeVirt core .
The feature requires the Sidecar feature gate to be enabled in the KubeVirt CR :
spec:
configuration:
developerConfiguration:
featureGates:
- Sidecar
β οΈ Once enabled, every VMI owner can inject arbitrary code into the
virt-launchercontext. This is a cluster-wide trust decision .
There are currently two parallel mechanisms for configuring sidecar hooks:
- Legacy:
hooks.kubevirt.io/hookSidecarsVMI annotation (existing, stable) - Newer: Plugin CRD with
DomainHook(alpha, VEP 190 / PR #18102)
Hook Points and gRPC Protocol#
Sidecar containers communicate with virt-launcher over Unix-domain sockets in /var/run/kubevirt-hooks using gRPC. The hook manager polls that directory on startup, negotiates a protocol version with each socket, then sorts callbacks by priority.
Supported hook points :
| Hook Point | Trigger | Input β Output |
|---|---|---|
onDefineDomain | Before libvirt domain creation | VMI JSON + domain XML β modified domain XML |
preCloudInitIso | Before cloud-init ISO build | VMI JSON + CloudInitData β modified CloudInitData |
Shutdown | On VM shutdown | (v1alpha3 only) |
Protocol versions are negotiated newest-first :
v1alpha3β addsShutdownhook; preferredv1alpha2β addspreCloudInitIsov1alpha1βonDefineDomainonly
The sidecar shim must declare its version via --version v1alpha2 (or equivalent) at startup.
hookSidecars Annotation (Legacy)#
The hooks.kubevirt.io/hookSidecars annotation on a VMI is a JSON array that instructs virt-controller to inject one or more sidecar containers into the virt-launcher pod .
Key fields per entry:
| Field | Purpose |
|---|---|
image | Sidecar container image (defaults to built-in sidecar-shim-image) |
imagePullPolicy | IfNotPresent / Always / Never |
args | Typically ["--version", "v1alpha2"] to declare gRPC version |
configMap.name/key/hookPath | Mount a ConfigMap script at the hook path |
pvc.name/volumePath/sharedComputePath | Mount a PVC, optionally shared with the compute container |
The hookPath for ConfigMap entries must be either /usr/bin/onDefineDomain or /usr/bin/preCloudInitIso .
Important: On a VirtualMachine (not VMI), the annotation must be placed under spec.template.metadata.annotations β annotations on the VM object itself are not propagated to generated VMIs .
Minimal ConfigMap example :
hooks.kubevirt.io/hookSidecars: '[{"args": ["--version", "v1alpha2"],
"configMap": {"name": "my-config-map", "key": "my_script.sh", "hookPath": "/usr/bin/onDefineDomain"}}]'
sidecar-shim Image and ConfigMap Scripts#
The sidecar-shim binary (source: cmd/sidecars/sidecar_shim.go) is the standard entrypoint that handles gRPC protocol wiring so developers only need to write the hook logic itself. It searches $PATH for executables named after hook points (e.g., onDefineDomain) and invokes them with the hook inputs as flags :
--vmi <VMI JSON>β full VMI spec--domain <domain XML>β current libvirt domain XML
The binary/script must print the modified XML to stdout.
Two deployment patterns:
-
Custom image: Build a container image that uses
sidecar-shim-imageas the base, install your binary or script at/usr/bin/onDefineDomain, and reference it in the annotation'simagefield. See the smbios sidecar example. -
ConfigMap script: Store a shell or Python script in a ConfigMap key.
virt-controllermounts the script at the specifiedhookPathinside the sidecar container. No custom image is needed β use the defaultsidecar-shim-imageand addconfigMapto the annotation entry .
Plugin-Based Domain Hooks (VEP 190)#
PR #18102 (merged June 2026) introduced a structured alternative via the Plugin API (alpha, Plugins feature gate) .
How it works:
A Plugin CRD resource specifies a DomainHook with a socketPath under /var/run/kubevirt-plugin/<plugin-name>/*.sock. The sidecar container exposes a MutateDomain gRPC RPC at that socket. Virt-launcher calls it during domain definition with :
- Current domain XML
- Serialized VMI
- Invocation context:
Boot,MigrationSource, orMigrationTarget
Security controls at the socket layer :
- Admission webhook enforces socket path must be within the plugin's named directory and end in
.sock - Virt-launcher rejects symlink sockets and paths that escape the plugin directory at runtime
- Non-compute containers mounting the plugin socket volume must use
subPath(pod admission policy)
This differs from the legacy annotation: Plugin hooks are registered via the Plugin CRD (cluster operator-controlled) rather than per-VMI annotations (user-controlled), giving operators more governance over which hooks run.
Security Considerations#
Current threat model: A 2025 Quarkslab/OSTIF security audit (Finding LOW-15) identified that the hook mechanism implicitly trusts user-supplied sidecar images. Any principal with VMI create permission can inject a hook that :
- Modifies libvirt domain XML (CPU, devices, networking)
- Alters cloud-init data before first boot
- Influences VM security posture before runtime isolation
Specific gaps:
- Image provenance: No enforcement of image signing or registry policy on
hookSidecarsimages today . - ConfigMap scripts: The
sidecar-shimexecutes scripts from ConfigMaps viaexec.Commandwithout verifying their contents or integrity. - Plugin socket validation: Virt-launcher validates socket paths (rejects symlinks, path escapes) but this constrains socket selection, not the trustworthiness of the plugin code itself.
In-progress mitigations:
- GSoC 2026 proposal (issue #16552) proposes admission-time cryptographic verification of
hookSidecarsimages using Sigstore/Cosign in thevirt-apiadmission webhook . Verification of ConfigMap-based scripts is listed as a stretch goal. - The Plugin CRD approach (VEP 190) provides operator-level governance by moving hook registration from user-controlled VMI annotations to cluster operator-controlled Plugin resources .
Operational guidance: In multi-tenant or regulated environments, restrict Sidecar feature gate usage and audit which principals can create VMIs with the hookSidecars annotation until image signing enforcement is available.