feat(virtual-machine): let each disk set its own boot order

The boot order of a VM's disks was the order of the disks list, so the
only way to boot from installer or rescue media, such as an ISO
attached as a CD-ROM, was to move that media to the front of the list.
Callers that treat the first disk as the system disk then see the ISO
in its place.

An optional bootOrder per disk decides boot priority independently of
the list. Disks that leave it unset keep their positional order, so
existing VMs render unchanged. Duplicate effective boot orders are
refused at render time with the names of both disks, since KubeVirt
would otherwise deny the VirtualMachine with a less specific error.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
Mattia Eleuteri committed 2026-11-10 10:28:28 +01:00
1 parent 456473dec1
commit d48897cf82
8 files changed
+164 -3

No files matched your search

+3
View File
@@ -74,6 +74,9 @@ type ConfigSpec struct {
}
type Disk struct {
// Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.
// +kubebuilder:validation:Minimum=1
BootOrder *int `json:"bootOrder,omitempty"`
// Disk bus type (e.g. "sata").
Bus string `json:"bus,omitempty"`
// Disk name.
+8 -1
View File
@@ -61,7 +61,9 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
if in.Disks != nil {
in, out := &in.Disks, &out.Disks
*out = make([]Disk, len(*in))
copy(*out, *in)
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.Networks != nil {
in, out := &in.Networks, &out.Networks
@@ -100,6 +102,11 @@ func (in *ConfigSpec) DeepCopy() *ConfigSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Disk) DeepCopyInto(out *Disk) {
*out = *in
if in.BootOrder != nil {
in, out := &in.BootOrder, &out.BootOrder
*out = new(int)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Disk.
+1
View File
@@ -48,6 +48,7 @@ virtctl ssh <user>@<vm>
| `disks` | List of disks to attach. | `[]object` | `[]` |
| `disks[i].name` | Disk name. | `string` | `""` |
| `disks[i].bus` | Disk bus type (e.g. "sata"). | `string` | `""` |
| `disks[i].bootOrder` | Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both. | `*int` | `null` |
| `networks` | Networks to attach the VM to. | `[]object` | `[]` |
| `networks[i].name` | Network attachment name. | `string` | `""` |
| `subnets` | Deprecated: use networks instead. | `[]object` | `[]` |
+10 -1
View File
@@ -92,7 +92,16 @@ spec:
{{- end }}
{{- end }}
disks:
{{- /* KubeVirt rejects two devices sharing a bootOrder, so a clash is
reported here with the disk names rather than surfacing as a
webhook denial on the VirtualMachine. */}}
{{- $bootOrderOwners := dict }}
{{- range $i, $disk := .Values.disks }}
{{- $bootOrder := int ($disk.bootOrder | default (add $i 1)) }}
{{- with get $bootOrderOwners (toString $bootOrder) }}
{{- fail (printf "disks %q and %q both have bootOrder %d; boot orders must be unique" . $disk.name $bootOrder) }}
{{- end }}
{{- $_ := set $bootOrderOwners (toString $bootOrder) $disk.name }}
- name: disk-{{ $disk.name }}
{{- $dv := lookup "cdi.kubevirt.io/v1beta1" "DataVolume" $.Release.Namespace (printf "vm-disk-%s" $disk.name) }}
{{- if $dv }}
@@ -104,7 +113,7 @@ spec:
{{- else }}
{{ $deviceKey }}: {}
{{- end }}
bootOrder: {{ add $i 1 }}
bootOrder: {{ $bootOrder }}
{{- else }}
{{- fail (printf "Specified disk not exists in cluster: %s" .name) }}
{{- end }}
@@ -0,0 +1,128 @@
suite: vm-instance disk boot order
# The first entry of `disks` is the one callers treat as the system disk, so
# booting from installer or rescue media must not require moving that media to
# the front of the list. These cases pin that an explicit bootOrder decides
# which device the VM boots from while list order stays as given, and that
# leaving it unset keeps the positional order existing VMs were rendered with.
release:
name: test-vm
namespace: tenant-test
templates:
- templates/vm.yaml
set:
fullnameOverride: test-vm
instanceType: ""
instanceProfile: ""
resources:
cpu: 1
sockets: 1
memory: 1Gi
_cluster:
scheduling:
dedicatedNodesForWindowsVMs: "false"
kubernetesProvider:
scheme:
"cdi.kubevirt.io/v1beta1/DataVolume":
gvr:
group: cdi.kubevirt.io
version: v1beta1
resource: datavolumes
namespaced: true
objects:
- apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: vm-disk-system
namespace: tenant-test
annotations: {}
- apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: vm-disk-iso
namespace: tenant-test
annotations:
vm-disk.cozystack.io/optical: "true"
- apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: vm-disk-data
namespace: tenant-test
annotations: {}
tests:
- it: keeps the positional boot order when no disk sets bootOrder
set:
disks:
- name: system
- name: iso
- name: data
asserts:
- equal:
path: spec.template.spec.domain.devices.disks
value:
- name: disk-system
disk: {}
bootOrder: 1
- name: disk-iso
cdrom: {}
bootOrder: 2
- name: disk-data
disk: {}
bootOrder: 3
- it: boots from a CD-ROM in second position without reordering the disks
set:
disks:
- name: system
bootOrder: 2
- name: iso
bus: sata
bootOrder: 1
- name: data
asserts:
- equal:
path: spec.template.spec.domain.devices.disks
value:
- name: disk-system
disk: {}
bootOrder: 2
- name: disk-iso
cdrom:
bus: sata
bootOrder: 1
- name: disk-data
disk: {}
bootOrder: 3
- equal:
path: spec.template.spec.volumes[0].dataVolume.name
value: vm-disk-system
- it: refuses an explicit bootOrder that collides with another disk's positional one
set:
disks:
- name: system
- name: iso
bootOrder: 1
asserts:
- failedTemplate:
errorMessage: 'disks "system" and "iso" both have bootOrder 1; boot orders must be unique'
- it: refuses two disks given the same explicit bootOrder
set:
disks:
- name: system
bootOrder: 3
- name: iso
bootOrder: 3
asserts:
- failedTemplate:
errorMessage: 'disks "system" and "iso" both have bootOrder 3; boot orders must be unique'
- it: rejects a bootOrder below 1 through the values schema
set:
disks:
- name: system
bootOrder: 0
asserts:
- failedTemplate:
errorPattern: "minimum"
@@ -69,6 +69,11 @@
"name"
],
"properties": {
"bootOrder": {
"description": "Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.",
"type": "integer",
"minimum": 1
},
"bus": {
"description": "Disk bus type (e.g. \"sata\").",
"type": "string"
+8
View File
@@ -10,6 +10,8 @@
## @field {string} name - Disk name.
## @x-cozystack-options {source: vmdisk}
## @field {string} [bus] - Disk bus type (e.g. "sata").
## @field {*int} [bootOrder] - Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.
## @minimum 1
## @typedef {struct} Network - Network to attach the VM to.
## @field {string} [name] - Network attachment name.
@@ -67,6 +69,12 @@ disks: []
## - name: example-system
## - name: example-data
## bus: sata
## Boot from an attached ISO while keeping the system disk first in the list:
## disks:
## - name: example-system
## bootOrder: 2
## - name: example-iso
## bootOrder: 1
## @param {[]Network} networks - Networks to attach the VM to.
networks: []
+1 -1
View File
@@ -12,7 +12,7 @@ spec:
singular: vminstance
plural: vminstances
openAPISchema: |-
{"title":"Chart Values","type":"object","properties":{"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"externalMethod":{"description":"Method to pass through traffic to the VM.","type":"string","default":"PortList","enum":["PortList","WholeIP"]},"externalPorts":{"description":"Ports to forward from outside the cluster.","type":"array","default":[22],"items":{"type":"integer"}},"externalAllowICMP":{"description":"Whether to accept ICMP traffic to the VM in PortList mode (preserves ping and PMTU discovery). No effect in WholeIP mode. Default true so ping behaves as users expect even when port filtering is in effect.","type":"boolean","default":true},"runStrategy":{"description":"Requested running state of the VirtualMachineInstance","type":"string","default":"Always","enum":["Always","Halted","Manual","RerunOnFailure","Once"]},"instanceType":{"description":"Virtual Machine instance type. Ignored when `resources` sizes the VM, which needs whole-number cpu and sockets plus memory; the whole type is dropped then, so any dedicated CPU placement, hugepages, NUMA or realtime settings it carries go with it. While this field is set, a `resources` block that sizes only part of the VM is rejected outright instead of ignored, so complete it or clear it.","type":"string","default":"u1.medium","x-cozystack-options":{"source":"instancetype"}},"instanceProfile":{"description":"Virtual Machine preferences profile.","type":"string","default":"ubuntu","x-cozystack-options":{"source":"instanceprofile"}},"disks":{"description":"List of disks to attach.","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"bus":{"description":"Disk bus type (e.g. \"sata\").","type":"string"},"name":{"description":"Disk name.","type":"string","x-cozystack-options":{"source":"vmdisk"}}}}},"networks":{"description":"Networks to attach the VM to.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"subnets":{"description":"Deprecated: use networks instead.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"gpus":{"description":"List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"name":{"description":"The name of the GPU resource to attach.","type":"string","x-cozystack-options":{"source":"gpu"}}}}},"cpuModel":{"description":"Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map","type":"string","default":""},"firmware":{"description":"Firmware and boot configuration (UEFI/BIOS selection, Secure Boot, persistent EFI NVRAM).","type":"object","default":{},"properties":{"bootloader":{"description":"Bootloader to boot the VM with: \"uefi\" (OVMF) or \"bios\" (SeaBIOS). Empty inherits the instanceProfile default.","type":"string"},"efiPersistent":{"description":"Persist EFI NVRAM (e.g. enrolled Secure Boot keys, such as an updated Microsoft UEFI CA) across reboots. Only applies when bootloader is \"uefi\".","type":"boolean"},"secureBoot":{"description":"Enable UEFI Secure Boot. Only applies when bootloader is \"uefi\".","type":"boolean"}}},"resources":{"description":"Resource configuration for the virtual machine. Set whole-number cpu and sockets together with memory to size the VM directly, which overrides `instanceType`. Alongside an `instanceType`, a block that sizes only part of the VM is rejected; a whole-number cpu or sockets on its own sizes nothing and is ignored.","type":"object","default":{},"properties":{"cpu":{"description":"Number of CPU cores allocated, as a positive whole number such as `2`. Millicore or other unit-suffixed, fractional, zero, negative, above 4294967295 and quoted leading-zero values are rejected at render time, because the value becomes the VM's unsigned integer `domain.cpu.cores`.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"memory":{"description":"Amount of memory allocated, as a quantity of at least `1M` such as `8Gi`. Less than 1M is rejected at render time, because KubeVirt refuses a non-zero amount below 1M and a zero or negative amount sizes nothing.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"sockets":{"description":"Number of CPU sockets (vCPU topology), as a positive whole number such as `1`. Millicore or other unit-suffixed, fractional, zero, nLine truncated
{"title":"Chart Values","type":"object","properties":{"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"externalMethod":{"description":"Method to pass through traffic to the VM.","type":"string","default":"PortList","enum":["PortList","WholeIP"]},"externalPorts":{"description":"Ports to forward from outside the cluster.","type":"array","default":[22],"items":{"type":"integer"}},"externalAllowICMP":{"description":"Whether to accept ICMP traffic to the VM in PortList mode (preserves ping and PMTU discovery). No effect in WholeIP mode. Default true so ping behaves as users expect even when port filtering is in effect.","type":"boolean","default":true},"runStrategy":{"description":"Requested running state of the VirtualMachineInstance","type":"string","default":"Always","enum":["Always","Halted","Manual","RerunOnFailure","Once"]},"instanceType":{"description":"Virtual Machine instance type. Ignored when `resources` sizes the VM, which needs whole-number cpu and sockets plus memory; the whole type is dropped then, so any dedicated CPU placement, hugepages, NUMA or realtime settings it carries go with it. While this field is set, a `resources` block that sizes only part of the VM is rejected outright instead of ignored, so complete it or clear it.","type":"string","default":"u1.medium","x-cozystack-options":{"source":"instancetype"}},"instanceProfile":{"description":"Virtual Machine preferences profile.","type":"string","default":"ubuntu","x-cozystack-options":{"source":"instanceprofile"}},"disks":{"description":"List of disks to attach.","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"bootOrder":{"description":"Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.","type":"integer","minimum":1},"bus":{"description":"Disk bus type (e.g. \"sata\").","type":"string"},"name":{"description":"Disk name.","type":"string","x-cozystack-options":{"source":"vmdisk"}}}}},"networks":{"description":"Networks to attach the VM to.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"subnets":{"description":"Deprecated: use networks instead.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"gpus":{"description":"List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"name":{"description":"The name of the GPU resource to attach.","type":"string","x-cozystack-options":{"source":"gpu"}}}}},"cpuModel":{"description":"Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map","type":"string","default":""},"firmware":{"description":"Firmware and boot configuration (UEFI/BIOS selection, Secure Boot, persistent EFI NVRAM).","type":"object","default":{},"properties":{"bootloader":{"description":"Bootloader to boot the VM with: \"uefi\" (OVMF) or \"bios\" (SeaBIOS). Empty inherits the instanceProfile default.","type":"string"},"efiPersistent":{"description":"Persist EFI NVRAM (e.g. enrolled Secure Boot keys, such as an updated Microsoft UEFI CA) across reboots. Only applies when bootloader is \"uefi\".","type":"boolean"},"secureBoot":{"description":"Enable UEFI Secure Boot. Only applies when bootloader is \"uefi\".","type":"boolean"}}},"resources":{"description":"Resource configuration for the virtual machine. Set whole-number cpu and sockets together with memory to size the VM directly, which overrides `instanceType`. Alongside an `instanceType`, a block that sizes only part of the VM is rejected; a whole-number cpu or sockets on its own sizes nothing and is ignored.","type":"object","default":{},"properties":{"cpu":{"description":"Number of CPU cores allocated, as a positive whole number such as `2`. Millicore or other unit-suffixed, fractional, zero, negative, above 4294967295 and quoted leading-zero values are rejected at render time, because the value becomes the VM's unsigned integer `domain.cpu.cores`.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"memory":{"description":"Amount of memory allocated, as a quantity of at least `1M` such as `8Gi`. Less than 1M is rejected at render time, because KubeVirt refuses a non-Line truncated
release:
prefix: vm-instance-
labels: