]>
Commit | Line | Data |
---|---|---|
e0fd2b2f FE |
1 | package PVE::QemuServer::Drive; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Storage; | |
7 | use PVE::JSONSchema qw(get_standard_option); | |
8 | ||
9 | use base qw(Exporter); | |
10 | ||
11 | our @EXPORT_OK = qw( | |
12 | is_valid_drivename | |
13 | drive_is_cloudinit | |
14 | drive_is_cdrom | |
75748d44 | 15 | drive_is_read_only |
e0fd2b2f FE |
16 | parse_drive |
17 | print_drive | |
e0fd2b2f FE |
18 | ); |
19 | ||
20 | our $QEMU_FORMAT_RE = qr/raw|cow|qcow|qcow2|qed|vmdk|cloop/; | |
21 | ||
22 | PVE::JSONSchema::register_standard_option('pve-qm-image-format', { | |
23 | type => 'string', | |
24 | enum => [qw(raw cow qcow qed qcow2 vmdk cloop)], | |
25 | description => "The drive's backing file's data format.", | |
26 | optional => 1, | |
27 | }); | |
28 | ||
29 | my $MAX_IDE_DISKS = 4; | |
30 | my $MAX_SCSI_DISKS = 31; | |
31 | my $MAX_VIRTIO_DISKS = 16; | |
32 | our $MAX_SATA_DISKS = 6; | |
33 | our $MAX_UNUSED_DISKS = 256; | |
34 | ||
35 | our $drivedesc_hash; | |
36 | ||
37 | my %drivedesc_base = ( | |
38 | volume => { alias => 'file' }, | |
39 | file => { | |
40 | type => 'string', | |
41 | format => 'pve-volume-id-or-qm-path', | |
42 | default_key => 1, | |
43 | format_description => 'volume', | |
44 | description => "The drive's backing volume.", | |
45 | }, | |
46 | media => { | |
47 | type => 'string', | |
48 | enum => [qw(cdrom disk)], | |
49 | description => "The drive's media type.", | |
50 | default => 'disk', | |
51 | optional => 1 | |
52 | }, | |
53 | cyls => { | |
54 | type => 'integer', | |
55 | description => "Force the drive's physical geometry to have a specific cylinder count.", | |
56 | optional => 1 | |
57 | }, | |
58 | heads => { | |
59 | type => 'integer', | |
60 | description => "Force the drive's physical geometry to have a specific head count.", | |
61 | optional => 1 | |
62 | }, | |
63 | secs => { | |
64 | type => 'integer', | |
65 | description => "Force the drive's physical geometry to have a specific sector count.", | |
66 | optional => 1 | |
67 | }, | |
68 | trans => { | |
69 | type => 'string', | |
70 | enum => [qw(none lba auto)], | |
71 | description => "Force disk geometry bios translation mode.", | |
72 | optional => 1, | |
73 | }, | |
74 | snapshot => { | |
75 | type => 'boolean', | |
76 | description => "Controls qemu's snapshot mode feature." | |
77 | . " If activated, changes made to the disk are temporary and will" | |
78 | . " be discarded when the VM is shutdown.", | |
79 | optional => 1, | |
80 | }, | |
81 | cache => { | |
82 | type => 'string', | |
83 | enum => [qw(none writethrough writeback unsafe directsync)], | |
84 | description => "The drive's cache mode", | |
85 | optional => 1, | |
86 | }, | |
87 | format => get_standard_option('pve-qm-image-format'), | |
88 | size => { | |
89 | type => 'string', | |
90 | format => 'disk-size', | |
91 | format_description => 'DiskSize', | |
92 | description => "Disk size. This is purely informational and has no effect.", | |
93 | optional => 1, | |
94 | }, | |
95 | backup => { | |
96 | type => 'boolean', | |
97 | description => "Whether the drive should be included when making backups.", | |
98 | optional => 1, | |
99 | }, | |
100 | replicate => { | |
101 | type => 'boolean', | |
102 | description => 'Whether the drive should considered for replication jobs.', | |
103 | optional => 1, | |
104 | default => 1, | |
105 | }, | |
106 | rerror => { | |
107 | type => 'string', | |
108 | enum => [qw(ignore report stop)], | |
109 | description => 'Read error action.', | |
110 | optional => 1, | |
111 | }, | |
112 | werror => { | |
113 | type => 'string', | |
114 | enum => [qw(enospc ignore report stop)], | |
115 | description => 'Write error action.', | |
116 | optional => 1, | |
117 | }, | |
118 | aio => { | |
119 | type => 'string', | |
59e59342 | 120 | enum => [qw(native threads io_uring)], |
e0fd2b2f FE |
121 | description => 'AIO type to use.', |
122 | optional => 1, | |
123 | }, | |
124 | discard => { | |
125 | type => 'string', | |
126 | enum => [qw(ignore on)], | |
127 | description => 'Controls whether to pass discard/trim requests to the underlying storage.', | |
128 | optional => 1, | |
129 | }, | |
130 | detect_zeroes => { | |
131 | type => 'boolean', | |
132 | description => 'Controls whether to detect and try to optimize writes of zeroes.', | |
133 | optional => 1, | |
134 | }, | |
135 | serial => { | |
136 | type => 'string', | |
137 | format => 'urlencoded', | |
138 | format_description => 'serial', | |
139 | maxLength => 20*3, # *3 since it's %xx url enoded | |
140 | description => "The drive's reported serial number, url-encoded, up to 20 bytes long.", | |
141 | optional => 1, | |
142 | }, | |
143 | shared => { | |
144 | type => 'boolean', | |
145 | description => 'Mark this locally-managed volume as available on all nodes', | |
146 | verbose_description => "Mark this locally-managed volume as available on all nodes.\n\nWARNING: This option does not share the volume automatically, it assumes it is shared already!", | |
147 | optional => 1, | |
148 | default => 0, | |
149 | } | |
150 | ); | |
151 | ||
152 | my %iothread_fmt = ( iothread => { | |
153 | type => 'boolean', | |
154 | description => "Whether to use iothreads for this drive", | |
155 | optional => 1, | |
156 | }); | |
157 | ||
158 | my %model_fmt = ( | |
159 | model => { | |
160 | type => 'string', | |
161 | format => 'urlencoded', | |
162 | format_description => 'model', | |
163 | maxLength => 40*3, # *3 since it's %xx url enoded | |
164 | description => "The drive's reported model name, url-encoded, up to 40 bytes long.", | |
165 | optional => 1, | |
166 | }, | |
167 | ); | |
168 | ||
169 | my %queues_fmt = ( | |
170 | queues => { | |
171 | type => 'integer', | |
172 | description => "Number of queues.", | |
173 | minimum => 2, | |
174 | optional => 1 | |
175 | } | |
176 | ); | |
177 | ||
178 | my %scsiblock_fmt = ( | |
179 | scsiblock => { | |
180 | type => 'boolean', | |
181 | description => "whether to use scsi-block for full passthrough of host block device\n\nWARNING: can lead to I/O errors in combination with low memory or high memory fragmentation on host", | |
182 | optional => 1, | |
183 | default => 0, | |
184 | }, | |
185 | ); | |
186 | ||
187 | my %ssd_fmt = ( | |
188 | ssd => { | |
189 | type => 'boolean', | |
190 | description => "Whether to expose this drive as an SSD, rather than a rotational hard disk.", | |
191 | optional => 1, | |
192 | }, | |
193 | ); | |
194 | ||
195 | my %wwn_fmt = ( | |
196 | wwn => { | |
197 | type => 'string', | |
198 | pattern => qr/^(0x)[0-9a-fA-F]{16}/, | |
199 | format_description => 'wwn', | |
200 | description => "The drive's worldwide name, encoded as 16 bytes hex string, prefixed by '0x'.", | |
201 | optional => 1, | |
202 | }, | |
203 | ); | |
204 | ||
205 | my $add_throttle_desc = sub { | |
206 | my ($key, $type, $what, $unit, $longunit, $minimum) = @_; | |
207 | my $d = { | |
208 | type => $type, | |
209 | format_description => $unit, | |
210 | description => "Maximum $what in $longunit.", | |
211 | optional => 1, | |
212 | }; | |
213 | $d->{minimum} = $minimum if defined($minimum); | |
214 | $drivedesc_base{$key} = $d; | |
215 | }; | |
216 | # throughput: (leaky bucket) | |
217 | $add_throttle_desc->('bps', 'integer', 'r/w speed', 'bps', 'bytes per second'); | |
218 | $add_throttle_desc->('bps_rd', 'integer', 'read speed', 'bps', 'bytes per second'); | |
219 | $add_throttle_desc->('bps_wr', 'integer', 'write speed', 'bps', 'bytes per second'); | |
220 | $add_throttle_desc->('mbps', 'number', 'r/w speed', 'mbps', 'megabytes per second'); | |
221 | $add_throttle_desc->('mbps_rd', 'number', 'read speed', 'mbps', 'megabytes per second'); | |
222 | $add_throttle_desc->('mbps_wr', 'number', 'write speed', 'mbps', 'megabytes per second'); | |
223 | $add_throttle_desc->('iops', 'integer', 'r/w I/O', 'iops', 'operations per second'); | |
224 | $add_throttle_desc->('iops_rd', 'integer', 'read I/O', 'iops', 'operations per second'); | |
225 | $add_throttle_desc->('iops_wr', 'integer', 'write I/O', 'iops', 'operations per second'); | |
226 | ||
227 | # pools: (pool of IO before throttling starts taking effect) | |
228 | $add_throttle_desc->('mbps_max', 'number', 'unthrottled r/w pool', 'mbps', 'megabytes per second'); | |
229 | $add_throttle_desc->('mbps_rd_max', 'number', 'unthrottled read pool', 'mbps', 'megabytes per second'); | |
230 | $add_throttle_desc->('mbps_wr_max', 'number', 'unthrottled write pool', 'mbps', 'megabytes per second'); | |
231 | $add_throttle_desc->('iops_max', 'integer', 'unthrottled r/w I/O pool', 'iops', 'operations per second'); | |
232 | $add_throttle_desc->('iops_rd_max', 'integer', 'unthrottled read I/O pool', 'iops', 'operations per second'); | |
233 | $add_throttle_desc->('iops_wr_max', 'integer', 'unthrottled write I/O pool', 'iops', 'operations per second'); | |
234 | ||
235 | # burst lengths | |
236 | $add_throttle_desc->('bps_max_length', 'integer', 'length of I/O bursts', 'seconds', 'seconds', 1); | |
237 | $add_throttle_desc->('bps_rd_max_length', 'integer', 'length of read I/O bursts', 'seconds', 'seconds', 1); | |
238 | $add_throttle_desc->('bps_wr_max_length', 'integer', 'length of write I/O bursts', 'seconds', 'seconds', 1); | |
239 | $add_throttle_desc->('iops_max_length', 'integer', 'length of I/O bursts', 'seconds', 'seconds', 1); | |
240 | $add_throttle_desc->('iops_rd_max_length', 'integer', 'length of read I/O bursts', 'seconds', 'seconds', 1); | |
241 | $add_throttle_desc->('iops_wr_max_length', 'integer', 'length of write I/O bursts', 'seconds', 'seconds', 1); | |
242 | ||
243 | # legacy support | |
244 | $drivedesc_base{'bps_rd_length'} = { alias => 'bps_rd_max_length' }; | |
245 | $drivedesc_base{'bps_wr_length'} = { alias => 'bps_wr_max_length' }; | |
246 | $drivedesc_base{'iops_rd_length'} = { alias => 'iops_rd_max_length' }; | |
247 | $drivedesc_base{'iops_wr_length'} = { alias => 'iops_wr_max_length' }; | |
248 | ||
249 | my $ide_fmt = { | |
250 | %drivedesc_base, | |
251 | %model_fmt, | |
252 | %ssd_fmt, | |
253 | %wwn_fmt, | |
254 | }; | |
255 | PVE::JSONSchema::register_format("pve-qm-ide", $ide_fmt); | |
256 | ||
98c3d99e FE |
257 | my $ALLOCATION_SYNTAX_DESC = |
258 | "Use the special syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume."; | |
259 | ||
e0fd2b2f FE |
260 | my $idedesc = { |
261 | optional => 1, | |
262 | type => 'string', format => $ide_fmt, | |
98c3d99e FE |
263 | description => "Use volume as IDE hard disk or CD-ROM (n is 0 to " .($MAX_IDE_DISKS -1) . "). " . |
264 | $ALLOCATION_SYNTAX_DESC, | |
e0fd2b2f FE |
265 | }; |
266 | PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc); | |
267 | ||
268 | my $scsi_fmt = { | |
269 | %drivedesc_base, | |
270 | %iothread_fmt, | |
271 | %queues_fmt, | |
272 | %scsiblock_fmt, | |
273 | %ssd_fmt, | |
274 | %wwn_fmt, | |
275 | }; | |
276 | my $scsidesc = { | |
277 | optional => 1, | |
278 | type => 'string', format => $scsi_fmt, | |
98c3d99e FE |
279 | description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . "). " . |
280 | $ALLOCATION_SYNTAX_DESC, | |
e0fd2b2f FE |
281 | }; |
282 | PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc); | |
283 | ||
284 | my $sata_fmt = { | |
285 | %drivedesc_base, | |
286 | %ssd_fmt, | |
287 | %wwn_fmt, | |
288 | }; | |
289 | my $satadesc = { | |
290 | optional => 1, | |
291 | type => 'string', format => $sata_fmt, | |
98c3d99e FE |
292 | description => "Use volume as SATA hard disk or CD-ROM (n is 0 to " . ($MAX_SATA_DISKS - 1). "). " . |
293 | $ALLOCATION_SYNTAX_DESC, | |
e0fd2b2f FE |
294 | }; |
295 | PVE::JSONSchema::register_standard_option("pve-qm-sata", $satadesc); | |
296 | ||
297 | my $virtio_fmt = { | |
298 | %drivedesc_base, | |
299 | %iothread_fmt, | |
300 | }; | |
301 | my $virtiodesc = { | |
302 | optional => 1, | |
303 | type => 'string', format => $virtio_fmt, | |
98c3d99e FE |
304 | description => "Use volume as VIRTIO hard disk (n is 0 to " . ($MAX_VIRTIO_DISKS - 1) . "). " . |
305 | $ALLOCATION_SYNTAX_DESC, | |
e0fd2b2f FE |
306 | }; |
307 | PVE::JSONSchema::register_standard_option("pve-qm-virtio", $virtiodesc); | |
308 | ||
b5099b4f SR |
309 | my %efitype_fmt = ( |
310 | efitype => { | |
311 | type => 'string', | |
312 | enum => [qw(2m 4m)], | |
313 | description => "Size and type of the OVMF EFI vars. '4m' is newer and recommended," | |
314 | . " and required for Secure Boot. For backwards compatibility, '2m' is used" | |
315 | . " if not otherwise specified.", | |
316 | optional => 1, | |
317 | default => '2m', | |
318 | }, | |
319 | 'ms-keys' => { | |
320 | type => 'boolean', | |
321 | description => "Pre-enroll the Microsoft Standard UEFI Secure Boot keys if" | |
322 | . " used with 'efitype=4m'. Note that this will enable Secure Boot by" | |
323 | . " default, though it can still be turned off from within the VM.", | |
324 | optional => 1, | |
325 | default => 0, | |
326 | }, | |
327 | ); | |
328 | ||
e0fd2b2f FE |
329 | my $efidisk_fmt = { |
330 | volume => { alias => 'file' }, | |
331 | file => { | |
332 | type => 'string', | |
333 | format => 'pve-volume-id-or-qm-path', | |
334 | default_key => 1, | |
335 | format_description => 'volume', | |
336 | description => "The drive's backing volume.", | |
337 | }, | |
338 | format => get_standard_option('pve-qm-image-format'), | |
339 | size => { | |
340 | type => 'string', | |
341 | format => 'disk-size', | |
342 | format_description => 'DiskSize', | |
343 | description => "Disk size. This is purely informational and has no effect.", | |
344 | optional => 1, | |
345 | }, | |
b5099b4f | 346 | %efitype_fmt, |
e0fd2b2f FE |
347 | }; |
348 | ||
349 | my $efidisk_desc = { | |
350 | optional => 1, | |
351 | type => 'string', format => $efidisk_fmt, | |
98c3d99e FE |
352 | description => "Configure a Disk for storing EFI vars. " . |
353 | $ALLOCATION_SYNTAX_DESC . " Note that SIZE_IN_GiB is ignored here " . | |
354 | "and that the default EFI vars are copied to the volume instead.", | |
e0fd2b2f FE |
355 | }; |
356 | ||
357 | PVE::JSONSchema::register_standard_option("pve-qm-efidisk", $efidisk_desc); | |
358 | ||
f9dde219 SR |
359 | my %tpmversion_fmt = ( |
360 | version => { | |
361 | type => 'string', | |
362 | enum => [qw(v1.2 v2.0)], | |
2b9ee944 TL |
363 | description => "The TPM interface version. v2.0 is newer and should be preferred." |
364 | ." Note that this cannot be changed later on.", | |
f9dde219 SR |
365 | optional => 1, |
366 | default => 'v2.0', | |
367 | }, | |
368 | ); | |
369 | my $tpmstate_fmt = { | |
370 | volume => { alias => 'file' }, | |
371 | file => { | |
372 | type => 'string', | |
373 | format => 'pve-volume-id-or-qm-path', | |
374 | default_key => 1, | |
375 | format_description => 'volume', | |
376 | description => "The drive's backing volume.", | |
377 | }, | |
378 | size => { | |
379 | type => 'string', | |
380 | format => 'disk-size', | |
381 | format_description => 'DiskSize', | |
382 | description => "Disk size. This is purely informational and has no effect.", | |
383 | optional => 1, | |
384 | }, | |
385 | %tpmversion_fmt, | |
386 | }; | |
387 | my $tpmstate_desc = { | |
388 | optional => 1, | |
389 | type => 'string', format => $tpmstate_fmt, | |
390 | description => "Configure a Disk for storing TPM state. " . | |
391 | $ALLOCATION_SYNTAX_DESC . " Note that SIZE_IN_GiB is ignored here " . | |
392 | "and that the default size of 4 MiB will always be used instead. The " . | |
393 | "format is also fixed to 'raw'.", | |
394 | }; | |
395 | use constant TPMSTATE_DISK_SIZE => 4 * 1024 * 1024; | |
396 | ||
397 | my $alldrive_fmt = { | |
398 | %drivedesc_base, | |
399 | %iothread_fmt, | |
400 | %model_fmt, | |
401 | %queues_fmt, | |
402 | %scsiblock_fmt, | |
403 | %ssd_fmt, | |
404 | %wwn_fmt, | |
405 | %tpmversion_fmt, | |
b5099b4f | 406 | %efitype_fmt, |
f9dde219 SR |
407 | }; |
408 | ||
43c4c7b6 FE |
409 | my $unused_fmt = { |
410 | volume => { alias => 'file' }, | |
411 | file => { | |
412 | type => 'string', | |
413 | format => 'pve-volume-id', | |
414 | default_key => 1, | |
415 | format_description => 'volume', | |
416 | description => "The drive's backing volume.", | |
417 | }, | |
418 | }; | |
419 | ||
420 | my $unuseddesc = { | |
421 | optional => 1, | |
422 | type => 'string', format => $unused_fmt, | |
423 | description => "Reference to unused volumes. This is used internally, and should not be modified manually.", | |
424 | }; | |
425 | ||
e0fd2b2f FE |
426 | for (my $i = 0; $i < $MAX_IDE_DISKS; $i++) { |
427 | $drivedesc_hash->{"ide$i"} = $idedesc; | |
428 | } | |
429 | ||
430 | for (my $i = 0; $i < $MAX_SATA_DISKS; $i++) { | |
431 | $drivedesc_hash->{"sata$i"} = $satadesc; | |
432 | } | |
433 | ||
434 | for (my $i = 0; $i < $MAX_SCSI_DISKS; $i++) { | |
435 | $drivedesc_hash->{"scsi$i"} = $scsidesc; | |
436 | } | |
437 | ||
438 | for (my $i = 0; $i < $MAX_VIRTIO_DISKS; $i++) { | |
439 | $drivedesc_hash->{"virtio$i"} = $virtiodesc; | |
440 | } | |
441 | ||
442 | $drivedesc_hash->{efidisk0} = $efidisk_desc; | |
f9dde219 | 443 | $drivedesc_hash->{tpmstate0} = $tpmstate_desc; |
e0fd2b2f | 444 | |
43c4c7b6 FE |
445 | for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) { |
446 | $drivedesc_hash->{"unused$i"} = $unuseddesc; | |
447 | } | |
e0fd2b2f FE |
448 | |
449 | sub valid_drive_names { | |
450 | # order is important - used to autoselect boot disk | |
451 | return ((map { "ide$_" } (0 .. ($MAX_IDE_DISKS - 1))), | |
452 | (map { "scsi$_" } (0 .. ($MAX_SCSI_DISKS - 1))), | |
453 | (map { "virtio$_" } (0 .. ($MAX_VIRTIO_DISKS - 1))), | |
454 | (map { "sata$_" } (0 .. ($MAX_SATA_DISKS - 1))), | |
f9dde219 SR |
455 | 'efidisk0', |
456 | 'tpmstate0'); | |
e0fd2b2f FE |
457 | } |
458 | ||
459 | sub is_valid_drivename { | |
460 | my $dev = shift; | |
461 | ||
43c4c7b6 | 462 | return defined($drivedesc_hash->{$dev}) && $dev !~ /^unused\d+$/; |
e0fd2b2f FE |
463 | } |
464 | ||
465 | PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk); | |
466 | sub verify_bootdisk { | |
467 | my ($value, $noerr) = @_; | |
468 | ||
469 | return $value if is_valid_drivename($value); | |
470 | ||
d1c1af4b | 471 | return if $noerr; |
e0fd2b2f FE |
472 | |
473 | die "invalid boot disk '$value'\n"; | |
474 | } | |
475 | ||
476 | sub drive_is_cloudinit { | |
477 | my ($drive) = @_; | |
478 | return $drive->{file} =~ m@[:/]vm-\d+-cloudinit(?:\.$QEMU_FORMAT_RE)?$@; | |
479 | } | |
480 | ||
481 | sub drive_is_cdrom { | |
482 | my ($drive, $exclude_cloudinit) = @_; | |
483 | ||
484 | return 0 if $exclude_cloudinit && drive_is_cloudinit($drive); | |
485 | ||
486 | return $drive && $drive->{media} && ($drive->{media} eq 'cdrom'); | |
487 | } | |
488 | ||
75748d44 FG |
489 | sub drive_is_read_only { |
490 | my ($conf, $drive) = @_; | |
491 | ||
492 | return 0 if !PVE::QemuConfig->is_template($conf); | |
493 | ||
494 | # don't support being marked read-only | |
495 | return $drive->{interface} ne 'sata' && $drive->{interface} ne 'ide'; | |
496 | } | |
497 | ||
e0fd2b2f FE |
498 | # ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]] |
499 | # [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no] | |
500 | # [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop] | |
501 | # [,aio=native|threads][,discard=ignore|on][,detect_zeroes=on|off] | |
502 | # [,iothread=on][,serial=serial][,model=model] | |
503 | ||
504 | sub parse_drive { | |
505 | my ($key, $data) = @_; | |
506 | ||
507 | my ($interface, $index); | |
508 | ||
509 | if ($key =~ m/^([^\d]+)(\d+)$/) { | |
510 | $interface = $1; | |
511 | $index = $2; | |
512 | } else { | |
d1c1af4b | 513 | return; |
e0fd2b2f FE |
514 | } |
515 | ||
43c4c7b6 | 516 | if (!defined($drivedesc_hash->{$key})) { |
e0fd2b2f | 517 | warn "invalid drive key: $key\n"; |
d1c1af4b | 518 | return; |
e0fd2b2f | 519 | } |
43c4c7b6 FE |
520 | |
521 | my $desc = $drivedesc_hash->{$key}->{format}; | |
e0fd2b2f | 522 | my $res = eval { PVE::JSONSchema::parse_property_string($desc, $data) }; |
d1c1af4b | 523 | return if !$res; |
e0fd2b2f FE |
524 | $res->{interface} = $interface; |
525 | $res->{index} = $index; | |
526 | ||
527 | my $error = 0; | |
528 | foreach my $opt (qw(bps bps_rd bps_wr)) { | |
529 | if (my $bps = defined(delete $res->{$opt})) { | |
530 | if (defined($res->{"m$opt"})) { | |
531 | warn "both $opt and m$opt specified\n"; | |
532 | ++$error; | |
533 | next; | |
534 | } | |
535 | $res->{"m$opt"} = sprintf("%.3f", $bps / (1024*1024.0)); | |
536 | } | |
537 | } | |
538 | ||
539 | # can't use the schema's 'requires' because of the mbps* => bps* "transforming aliases" | |
540 | for my $requirement ( | |
541 | [mbps_max => 'mbps'], | |
542 | [mbps_rd_max => 'mbps_rd'], | |
543 | [mbps_wr_max => 'mbps_wr'], | |
544 | [miops_max => 'miops'], | |
545 | [miops_rd_max => 'miops_rd'], | |
546 | [miops_wr_max => 'miops_wr'], | |
547 | [bps_max_length => 'mbps_max'], | |
548 | [bps_rd_max_length => 'mbps_rd_max'], | |
549 | [bps_wr_max_length => 'mbps_wr_max'], | |
550 | [iops_max_length => 'iops_max'], | |
551 | [iops_rd_max_length => 'iops_rd_max'], | |
552 | [iops_wr_max_length => 'iops_wr_max']) { | |
553 | my ($option, $requires) = @$requirement; | |
554 | if ($res->{$option} && !$res->{$requires}) { | |
555 | warn "$option requires $requires\n"; | |
556 | ++$error; | |
557 | } | |
558 | } | |
559 | ||
d1c1af4b | 560 | return if $error; |
e0fd2b2f | 561 | |
d1c1af4b TL |
562 | return if $res->{mbps_rd} && $res->{mbps}; |
563 | return if $res->{mbps_wr} && $res->{mbps}; | |
564 | return if $res->{iops_rd} && $res->{iops}; | |
565 | return if $res->{iops_wr} && $res->{iops}; | |
e0fd2b2f FE |
566 | |
567 | if ($res->{media} && ($res->{media} eq 'cdrom')) { | |
d1c1af4b TL |
568 | return if $res->{snapshot} || $res->{trans} || $res->{format}; |
569 | return if $res->{heads} || $res->{secs} || $res->{cyls}; | |
570 | return if $res->{interface} eq 'virtio'; | |
e0fd2b2f FE |
571 | } |
572 | ||
573 | if (my $size = $res->{size}) { | |
d1c1af4b | 574 | return if !defined($res->{size} = PVE::JSONSchema::parse_size($size)); |
e0fd2b2f FE |
575 | } |
576 | ||
577 | return $res; | |
578 | } | |
579 | ||
580 | sub print_drive { | |
581 | my ($drive) = @_; | |
582 | my $skip = [ 'index', 'interface' ]; | |
583 | return PVE::JSONSchema::print_property_string($drive, $alldrive_fmt, $skip); | |
584 | } | |
585 | ||
2141a802 SR |
586 | sub get_bootdisks { |
587 | my ($conf) = @_; | |
588 | ||
f7d1505b TL |
589 | my $bootcfg; |
590 | $bootcfg = PVE::JSONSchema::parse_property_string('pve-qm-boot', $conf->{boot}) if $conf->{boot}; | |
2141a802 SR |
591 | |
592 | if (!defined($bootcfg) || $bootcfg->{legacy}) { | |
593 | return [$conf->{bootdisk}] if $conf->{bootdisk}; | |
594 | return []; | |
595 | } | |
596 | ||
597 | my @list = PVE::Tools::split_list($bootcfg->{order}); | |
598 | @list = grep {is_valid_drivename($_)} @list; | |
599 | return \@list; | |
600 | } | |
601 | ||
776c5f50 | 602 | sub bootdisk_size { |
e0fd2b2f FE |
603 | my ($storecfg, $conf) = @_; |
604 | ||
2141a802 | 605 | my $bootdisks = get_bootdisks($conf); |
d1c1af4b | 606 | return if !@$bootdisks; |
30664f14 DC |
607 | for my $bootdisk (@$bootdisks) { |
608 | next if !is_valid_drivename($bootdisk); | |
609 | next if !$conf->{$bootdisk}; | |
610 | my $drive = parse_drive($bootdisk, $conf->{$bootdisk}); | |
611 | next if !defined($drive); | |
612 | next if drive_is_cdrom($drive); | |
613 | my $volid = $drive->{file}; | |
614 | next if !$volid; | |
615 | return $drive->{size}; | |
616 | } | |
e0fd2b2f | 617 | |
30664f14 | 618 | return; |
e0fd2b2f FE |
619 | } |
620 | ||
621 | sub update_disksize { | |
9b29cbd0 | 622 | my ($drive, $newsize) = @_; |
e0fd2b2f | 623 | |
d1c1af4b | 624 | return if !defined($newsize); |
e0fd2b2f | 625 | |
63e313f3 | 626 | my $oldsize = $drive->{size} // 0; |
e0fd2b2f | 627 | |
63e313f3 | 628 | if ($newsize != $oldsize) { |
e0fd2b2f FE |
629 | $drive->{size} = $newsize; |
630 | ||
631 | my $old_fmt = PVE::JSONSchema::format_size($oldsize); | |
632 | my $new_fmt = PVE::JSONSchema::format_size($newsize); | |
633 | ||
9b29cbd0 FE |
634 | my $msg = "size of disk '$drive->{file}' updated from $old_fmt to $new_fmt"; |
635 | ||
636 | return ($drive, $msg); | |
e0fd2b2f FE |
637 | } |
638 | ||
d1c1af4b | 639 | return; |
e0fd2b2f FE |
640 | } |
641 | ||
642 | sub is_volume_in_use { | |
643 | my ($storecfg, $conf, $skip_drive, $volid) = @_; | |
644 | ||
645 | my $path = PVE::Storage::path($storecfg, $volid); | |
646 | ||
647 | my $scan_config = sub { | |
ab5b97d8 | 648 | my ($cref) = @_; |
e0fd2b2f FE |
649 | |
650 | foreach my $key (keys %$cref) { | |
651 | my $value = $cref->{$key}; | |
652 | if (is_valid_drivename($key)) { | |
653 | next if $skip_drive && $key eq $skip_drive; | |
654 | my $drive = parse_drive($key, $value); | |
655 | next if !$drive || !$drive->{file} || drive_is_cdrom($drive); | |
656 | return 1 if $volid eq $drive->{file}; | |
657 | if ($drive->{file} =~ m!^/!) { | |
658 | return 1 if $drive->{file} eq $path; | |
659 | } else { | |
660 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); | |
661 | next if !$storeid; | |
662 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid, 1); | |
663 | next if !$scfg; | |
a0d8d859 | 664 | return 1 if $path eq PVE::Storage::path($storecfg, $drive->{file}); |
e0fd2b2f FE |
665 | } |
666 | } | |
667 | } | |
668 | ||
669 | return 0; | |
670 | }; | |
671 | ||
672 | return 1 if &$scan_config($conf); | |
673 | ||
674 | undef $skip_drive; | |
675 | ||
ab5b97d8 FE |
676 | for my $snap (values %{$conf->{snapshots}}) { |
677 | return 1 if $scan_config->($snap); | |
e0fd2b2f FE |
678 | } |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
683 | sub resolve_first_disk { | |
5cfa9f5f | 684 | my ($conf, $cdrom) = @_; |
e0fd2b2f | 685 | my @disks = valid_drive_names(); |
5cfa9f5f | 686 | foreach my $ds (@disks) { |
e0fd2b2f FE |
687 | next if !$conf->{$ds}; |
688 | my $disk = parse_drive($ds, $conf->{$ds}); | |
5cfa9f5f SR |
689 | next if drive_is_cdrom($disk) xor $cdrom; |
690 | return $ds; | |
e0fd2b2f | 691 | } |
d1c1af4b | 692 | return; |
e0fd2b2f FE |
693 | } |
694 | ||
695 | 1; |