Skip to content

Commit 56224a1

Browse files
authored
feat(virtual-machine): let each disk set its own boot order (#5266)
## What this PR does A `vm-instance` boots from its disks in the order they appear in `disks`: the template hardcodes `bootOrder: {{ add $i 1 }}`. Booting from installer or rescue media, such as an ISO attached as a CD-ROM next to the system disk, therefore means moving that media to the front of the list, and anything that treats `disks[0]` as the system disk then finds the ISO there instead. This adds an optional `bootOrder` to each entry of `disks`: - When set, it is the disk's boot priority (1 is tried first), independent of its position in the list. - When unset, the disk keeps its positional order (`index + 1`), so existing VMs render exactly as before. - The values schema requires `bootOrder >= 1`. - Duplicate effective boot orders are refused at render time with the names of both disks, rather than surfacing as a KubeVirt admission error on the VirtualMachine. This includes an explicit value colliding with another disk's positional one, so booting from a CD-ROM in second position means setting both disks: ```yaml disks: - name: system bootOrder: 2 - name: install-iso bus: sata bootOrder: 1 ``` `make generate` was run in the package, so the README, `values.schema.json`, the Go type in `api/apps/v1alpha1/vminstance` (with its regenerated deepcopy, since the field is a pointer) and the `vm-instance-rd` OpenAPI schema are all updated. A new suite, `tests/disk_boot_order_test.yaml`, mocks the DataVolumes the template looks up and covers: positional order when no disk sets `bootOrder`; a CD-ROM in second position booting first while the list order and volumes stay unchanged; an explicit value colliding with a positional one; two equal explicit values; and a value below 1 rejected by the schema. Run against the previous template, the CD-ROM and collision cases fail. `helm unittest packages/apps/vm-instance`: 6 suites, 77 tests passing. ### Screenshots No UI change. ### Downstream repositories - [ ] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: This adds a field to `vm-instance`'s `values.schema.json`, which the trigger map routes to `cozystack/terraform-provider-cozystack`: its `vminstance_schema.go` and `vminstance_model.go` model `disks` by hand, so `bootOrder` needs adding there. The follow-up has not been opened yet and the box is left blank until it is linked. The website reference page is regenerated from the README by the release bot; nothing else in the map is touched. ### Release note ```release-note feat(virtual-machine): disks in a vm-instance accept an optional `bootOrder`, so a VM can boot from an attached ISO or other rescue media without reordering its disks. Unset keeps the previous positional boot order. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Configure a boot priority for each VM disk. Lower values are tried first; disks without an explicit priority use their position in the disk list. * Disk priorities must be unique, and values below 1 are rejected. Priority changes do not reorder disks or their associated volumes. * **Documentation** * Added guidance and an example for configuring disk boot priorities. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 8741fb3 + d48897c commit 56224a1

8 files changed

Lines changed: 164 additions & 3 deletions

File tree

‎api/apps/v1alpha1/vminstance/types.go‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/apps/v1alpha1/vminstance/zz_generated.deepcopy.go‎

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/apps/vm-instance/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ virtctl ssh <user>@<vm>
4848
| `disks` | List of disks to attach. | `[]object` | `[]` |
4949
| `disks[i].name` | Disk name. | `string` | `""` |
5050
| `disks[i].bus` | Disk bus type (e.g. "sata"). | `string` | `""` |
51+
| `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` |
5152
| `networks` | Networks to attach the VM to. | `[]object` | `[]` |
5253
| `networks[i].name` | Network attachment name. | `string` | `""` |
5354
| `subnets` | Deprecated: use networks instead. | `[]object` | `[]` |

‎packages/apps/vm-instance/templates/vm.yaml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ spec:
9292
{{- end }}
9393
{{- end }}
9494
disks:
95+
{{- /* KubeVirt rejects two devices sharing a bootOrder, so a clash is
96+
reported here with the disk names rather than surfacing as a
97+
webhook denial on the VirtualMachine. */}}
98+
{{- $bootOrderOwners := dict }}
9599
{{- range $i, $disk := .Values.disks }}
100+
{{- $bootOrder := int ($disk.bootOrder | default (add $i 1)) }}
101+
{{- with get $bootOrderOwners (toString $bootOrder) }}
102+
{{- fail (printf "disks %q and %q both have bootOrder %d; boot orders must be unique" . $disk.name $bootOrder) }}
103+
{{- end }}
104+
{{- $_ := set $bootOrderOwners (toString $bootOrder) $disk.name }}
96105
- name: disk-{{ $disk.name }}
97106
{{- $dv := lookup "cdi.kubevirt.io/v1beta1" "DataVolume" $.Release.Namespace (printf "vm-disk-%s" $disk.name) }}
98107
{{- if $dv }}
@@ -104,7 +113,7 @@ spec:
104113
{{- else }}
105114
{{ $deviceKey }}: {}
106115
{{- end }}
107-
bootOrder: {{ add $i 1 }}
116+
bootOrder: {{ $bootOrder }}
108117
{{- else }}
109118
{{- fail (printf "Specified disk not exists in cluster: %s" .name) }}
110119
{{- end }}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
suite: vm-instance disk boot order
2+
3+
# The first entry of `disks` is the one callers treat as the system disk, so
4+
# booting from installer or rescue media must not require moving that media to
5+
# the front of the list. These cases pin that an explicit bootOrder decides
6+
# which device the VM boots from while list order stays as given, and that
7+
# leaving it unset keeps the positional order existing VMs were rendered with.
8+
release:
9+
name: test-vm
10+
namespace: tenant-test
11+
templates:
12+
- templates/vm.yaml
13+
set:
14+
fullnameOverride: test-vm
15+
instanceType: ""
16+
instanceProfile: ""
17+
resources:
18+
cpu: 1
19+
sockets: 1
20+
memory: 1Gi
21+
_cluster:
22+
scheduling:
23+
dedicatedNodesForWindowsVMs: "false"
24+
kubernetesProvider:
25+
scheme:
26+
"cdi.kubevirt.io/v1beta1/DataVolume":
27+
gvr:
28+
group: cdi.kubevirt.io
29+
version: v1beta1
30+
resource: datavolumes
31+
namespaced: true
32+
objects:
33+
- apiVersion: cdi.kubevirt.io/v1beta1
34+
kind: DataVolume
35+
metadata:
36+
name: vm-disk-system
37+
namespace: tenant-test
38+
annotations: {}
39+
- apiVersion: cdi.kubevirt.io/v1beta1
40+
kind: DataVolume
41+
metadata:
42+
name: vm-disk-iso
43+
namespace: tenant-test
44+
annotations:
45+
vm-disk.cozystack.io/optical: "true"
46+
- apiVersion: cdi.kubevirt.io/v1beta1
47+
kind: DataVolume
48+
metadata:
49+
name: vm-disk-data
50+
namespace: tenant-test
51+
annotations: {}
52+
tests:
53+
- it: keeps the positional boot order when no disk sets bootOrder
54+
set:
55+
disks:
56+
- name: system
57+
- name: iso
58+
- name: data
59+
asserts:
60+
- equal:
61+
path: spec.template.spec.domain.devices.disks
62+
value:
63+
- name: disk-system
64+
disk: {}
65+
bootOrder: 1
66+
- name: disk-iso
67+
cdrom: {}
68+
bootOrder: 2
69+
- name: disk-data
70+
disk: {}
71+
bootOrder: 3
72+
73+
- it: boots from a CD-ROM in second position without reordering the disks
74+
set:
75+
disks:
76+
- name: system
77+
bootOrder: 2
78+
- name: iso
79+
bus: sata
80+
bootOrder: 1
81+
- name: data
82+
asserts:
83+
- equal:
84+
path: spec.template.spec.domain.devices.disks
85+
value:
86+
- name: disk-system
87+
disk: {}
88+
bootOrder: 2
89+
- name: disk-iso
90+
cdrom:
91+
bus: sata
92+
bootOrder: 1
93+
- name: disk-data
94+
disk: {}
95+
bootOrder: 3
96+
- equal:
97+
path: spec.template.spec.volumes[0].dataVolume.name
98+
value: vm-disk-system
99+
100+
- it: refuses an explicit bootOrder that collides with another disk's positional one
101+
set:
102+
disks:
103+
- name: system
104+
- name: iso
105+
bootOrder: 1
106+
asserts:
107+
- failedTemplate:
108+
errorMessage: 'disks "system" and "iso" both have bootOrder 1; boot orders must be unique'
109+
110+
- it: refuses two disks given the same explicit bootOrder
111+
set:
112+
disks:
113+
- name: system
114+
bootOrder: 3
115+
- name: iso
116+
bootOrder: 3
117+
asserts:
118+
- failedTemplate:
119+
errorMessage: 'disks "system" and "iso" both have bootOrder 3; boot orders must be unique'
120+
121+
- it: rejects a bootOrder below 1 through the values schema
122+
set:
123+
disks:
124+
- name: system
125+
bootOrder: 0
126+
asserts:
127+
- failedTemplate:
128+
errorPattern: "minimum"

‎packages/apps/vm-instance/values.schema.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
"name"
7070
],
7171
"properties": {
72+
"bootOrder": {
73+
"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.",
74+
"type": "integer",
75+
"minimum": 1
76+
},
7277
"bus": {
7378
"description": "Disk bus type (e.g. \"sata\").",
7479
"type": "string"

‎packages/apps/vm-instance/values.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
## @field {string} name - Disk name.
1111
## @x-cozystack-options {source: vmdisk}
1212
## @field {string} [bus] - Disk bus type (e.g. "sata").
13+
## @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.
14+
## @minimum 1
1315

1416
## @typedef {struct} Network - Network to attach the VM to.
1517
## @field {string} [name] - Network attachment name.
@@ -67,6 +69,12 @@ disks: []
6769
## - name: example-system
6870
## - name: example-data
6971
## bus: sata
72+
## Boot from an attached ISO while keeping the system disk first in the list:
73+
## disks:
74+
## - name: example-system
75+
## bootOrder: 2
76+
## - name: example-iso
77+
## bootOrder: 1
7078

7179
## @param {[]Network} networks - Networks to attach the VM to.
7280
networks: []

0 commit comments

Comments
 (0)