]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer.pm
api: update vm: print drive string for newly allocated/imported drives
[qemu-server.git] / PVE / QemuServer.pm
CommitLineData
1e3baf05
DM
1package PVE::QemuServer;
2
3use strict;
990fc5e2 4use warnings;
3ff84d6f 5
5da072fb
TL
6use Cwd 'abs_path';
7use Digest::SHA;
8use Fcntl ':flock';
9use Fcntl;
1e3baf05 10use File::Basename;
5da072fb 11use File::Copy qw(copy);
1e3baf05
DM
12use File::Path;
13use File::stat;
14use Getopt::Long;
5da072fb
TL
15use IO::Dir;
16use IO::File;
17use IO::Handle;
18use IO::Select;
19use IO::Socket::UNIX;
1e3baf05 20use IPC::Open3;
c971c4f2 21use JSON;
1f30ac3a 22use MIME::Base64;
5da072fb
TL
23use POSIX;
24use Storable qw(dclone);
f85951dc 25use Time::HiRes qw(gettimeofday usleep);
5da072fb 26use URI::Escape;
425441e6 27use UUID;
5da072fb 28
82841214 29use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file);
5b65b00d 30use PVE::CGroup;
48cf040f 31use PVE::DataCenterConfig;
5da072fb 32use PVE::Exception qw(raise raise_param_exc);
3b56383b 33use PVE::Format qw(render_duration render_bytes);
81d6e4e1 34use PVE::GuestHelpers qw(safe_string_ne safe_num_ne safe_boolean_ne);
1e3baf05 35use PVE::INotify;
4df98f2f 36use PVE::JSONSchema qw(get_standard_option parse_property_string);
1e3baf05 37use PVE::ProcFSTools;
fbec3f89 38use PVE::PBSClient;
91bd6c90 39use PVE::RPCEnvironment;
5da072fb 40use PVE::Storage;
b71351a7 41use PVE::SysFSTools;
d04d6af1 42use PVE::Systemd;
82841214 43use PVE::Tools qw(run_command file_read_firstline file_get_contents dir_glob_foreach get_host_arch $IPV6RE);
5da072fb
TL
44
45use PVE::QMPClient;
46use PVE::QemuConfig;
2f18c84d 47use PVE::QemuServer::Helpers qw(min_version config_aware_timeout);
5da072fb 48use PVE::QemuServer::Cloudinit;
5b65b00d 49use PVE::QemuServer::CGroup;
d786a274 50use PVE::QemuServer::CPUConfig qw(print_cpu_device get_cpu_options);
75748d44 51use PVE::QemuServer::Drive qw(is_valid_drivename drive_is_cloudinit drive_is_cdrom drive_is_read_only parse_drive print_drive);
2ea5fb7e 52use PVE::QemuServer::Machine;
5da072fb 53use PVE::QemuServer::Memory;
0a13e08e 54use PVE::QemuServer::Monitor qw(mon_cmd);
74c17b7a 55use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port parse_hostpci);
5da072fb 56use PVE::QemuServer::USB qw(parse_usb_device);
1e3baf05 57
28e129cc
AD
58my $have_sdn;
59eval {
60 require PVE::Network::SDN::Zones;
61 $have_sdn = 1;
62};
63
102cf9d8 64my $EDK2_FW_BASE = '/usr/share/pve-edk2-firmware/';
96ed3574 65my $OVMF = {
b5099b4f 66 x86_64 => {
90b20b15
DC
67 '4m-no-smm' => [
68 "$EDK2_FW_BASE/OVMF_CODE_4M.fd",
69 "$EDK2_FW_BASE/OVMF_VARS_4M.fd",
70 ],
71 '4m-no-smm-ms' => [
72 "$EDK2_FW_BASE/OVMF_CODE_4M.fd",
73 "$EDK2_FW_BASE/OVMF_VARS_4M.ms.fd",
74 ],
b5099b4f
SR
75 '4m' => [
76 "$EDK2_FW_BASE/OVMF_CODE_4M.secboot.fd",
77 "$EDK2_FW_BASE/OVMF_VARS_4M.fd",
78 ],
79 '4m-ms' => [
80 "$EDK2_FW_BASE/OVMF_CODE_4M.secboot.fd",
81 "$EDK2_FW_BASE/OVMF_VARS_4M.ms.fd",
82 ],
83 default => [
84 "$EDK2_FW_BASE/OVMF_CODE.fd",
85 "$EDK2_FW_BASE/OVMF_VARS.fd",
86 ],
87 },
88 aarch64 => {
89 default => [
90 "$EDK2_FW_BASE/AAVMF_CODE.fd",
91 "$EDK2_FW_BASE/AAVMF_VARS.fd",
92 ],
93 },
96ed3574 94};
2ddc0a5c 95
7f0b5beb 96my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
1e3baf05 97
8d88a594
TL
98# Note about locking: we use flock on the config file protect against concurent actions.
99# Aditionaly, we have a 'lock' setting in the config file. This can be set to 'migrate',
100# 'backup', 'snapshot' or 'rollback'. Most actions are not allowed when such lock is set.
101# But you can ignore this kind of lock with the --skiplock flag.
1e3baf05 102
97d62eb7 103cfs_register_file('/qemu-server/',
1858638f
DM
104 \&parse_vm_config,
105 \&write_vm_config);
1e3baf05 106
3ea94c60
DM
107PVE::JSONSchema::register_standard_option('pve-qm-stateuri', {
108 description => "Some command save/restore state from this location.",
109 type => 'string',
110 maxLength => 128,
111 optional => 1,
112});
113
c6737ef1
DC
114PVE::JSONSchema::register_standard_option('pve-qemu-machine', {
115 description => "Specifies the Qemu machine type.",
116 type => 'string',
9471e48b 117 pattern => '(pc|pc(-i440fx)?-\d+(\.\d+)+(\+pve\d+)?(\.pxe)?|q35|pc-q35-\d+(\.\d+)+(\+pve\d+)?(\.pxe)?|virt(?:-\d+(\.\d+)+)?(\+pve\d+)?)',
c6737ef1
DC
118 maxLength => 40,
119 optional => 1,
120});
121
bf8fc5a3
FG
122PVE::JSONSchema::register_standard_option('pve-targetstorage', {
123 description => "Mapping from source to target storages. Providing only a single storage ID maps all source storages to that storage. Providing the special value '1' will map each source storage to itself.",
124 type => 'string',
a4d828e3 125 format => 'storage-pair-list',
bf8fc5a3
FG
126 optional => 1,
127});
128
1e3baf05
DM
129#no warnings 'redefine';
130
38277afc
TL
131my $nodename_cache;
132sub nodename {
133 $nodename_cache //= PVE::INotify::nodename();
134 return $nodename_cache;
135}
1e3baf05 136
ec3582b5
WB
137my $watchdog_fmt = {
138 model => {
139 default_key => 1,
140 type => 'string',
141 enum => [qw(i6300esb ib700)],
142 description => "Watchdog type to emulate.",
143 default => 'i6300esb',
144 optional => 1,
145 },
146 action => {
147 type => 'string',
148 enum => [qw(reset shutdown poweroff pause debug none)],
149 description => "The action to perform if after activation the guest fails to poll the watchdog in time.",
150 optional => 1,
151 },
152};
153PVE::JSONSchema::register_format('pve-qm-watchdog', $watchdog_fmt);
154
9d66b397
SI
155my $agent_fmt = {
156 enabled => {
8d88a594 157 description => "Enable/disable communication with a Qemu Guest Agent (QGA) running in the VM.",
9d66b397
SI
158 type => 'boolean',
159 default => 0,
160 default_key => 1,
161 },
162 fstrim_cloned_disks => {
0a4aff09 163 description => "Run fstrim after moving a disk or migrating the VM.",
9d66b397
SI
164 type => 'boolean',
165 optional => 1,
166 default => 0
167 },
48657158
MD
168 type => {
169 description => "Select the agent type",
170 type => 'string',
171 default => 'virtio',
172 optional => 1,
173 enum => [qw(virtio isa)],
174 },
9d66b397
SI
175};
176
55655ebc
DC
177my $vga_fmt = {
178 type => {
179 description => "Select the VGA type.",
180 type => 'string',
181 default => 'std',
182 optional => 1,
183 default_key => 1,
7c954c42 184 enum => [qw(cirrus qxl qxl2 qxl3 qxl4 none serial0 serial1 serial2 serial3 std virtio vmware)],
55655ebc
DC
185 },
186 memory => {
187 description => "Sets the VGA memory (in MiB). Has no effect with serial display.",
188 type => 'integer',
189 optional => 1,
190 minimum => 4,
191 maximum => 512,
192 },
193};
194
6dbcb073
DC
195my $ivshmem_fmt = {
196 size => {
197 type => 'integer',
198 minimum => 1,
199 description => "The size of the file in MB.",
200 },
201 name => {
202 type => 'string',
203 pattern => '[a-zA-Z0-9\-]+',
204 optional => 1,
205 format_description => 'string',
206 description => "The name of the file. Will be prefixed with 'pve-shm-'. Default is the VMID. Will be deleted when the VM is stopped.",
207 },
208};
209
1448547f
AL
210my $audio_fmt = {
211 device => {
212 type => 'string',
213 enum => [qw(ich9-intel-hda intel-hda AC97)],
214 description => "Configure an audio device."
215 },
216 driver => {
217 type => 'string',
211785ee 218 enum => ['spice', 'none'],
1448547f
AL
219 default => 'spice',
220 optional => 1,
221 description => "Driver backend for the audio device."
222 },
223};
224
c4df18db
AL
225my $spice_enhancements_fmt = {
226 foldersharing => {
227 type => 'boolean',
228 optional => 1,
d282a24d 229 default => '0',
c4df18db
AL
230 description => "Enable folder sharing via SPICE. Needs Spice-WebDAV daemon installed in the VM."
231 },
232 videostreaming => {
233 type => 'string',
234 enum => ['off', 'all', 'filter'],
d282a24d 235 default => 'off',
c4df18db
AL
236 optional => 1,
237 description => "Enable video streaming. Uses compression for detected video streams."
238 },
239};
240
2cf61f33
SR
241my $rng_fmt = {
242 source => {
243 type => 'string',
244 enum => ['/dev/urandom', '/dev/random', '/dev/hwrng'],
245 default_key => 1,
8d88a594
TL
246 description => "The file on the host to gather entropy from. In most cases '/dev/urandom'"
247 ." should be preferred over '/dev/random' to avoid entropy-starvation issues on the"
248 ." host. Using urandom does *not* decrease security in any meaningful way, as it's"
249 ." still seeded from real entropy, and the bytes provided will most likely be mixed"
250 ." with real entropy on the guest as well. '/dev/hwrng' can be used to pass through"
251 ." a hardware RNG from the host.",
2cf61f33
SR
252 },
253 max_bytes => {
254 type => 'integer',
8d88a594
TL
255 description => "Maximum bytes of entropy allowed to get injected into the guest every"
256 ." 'period' milliseconds. Prefer a lower value when using '/dev/random' as source. Use"
257 ." `0` to disable limiting (potentially dangerous!).",
2cf61f33
SR
258 optional => 1,
259
8d88a594
TL
260 # default is 1 KiB/s, provides enough entropy to the guest to avoid boot-starvation issues
261 # (e.g. systemd etc...) while allowing no chance of overwhelming the host, provided we're
262 # reading from /dev/urandom
2cf61f33
SR
263 default => 1024,
264 },
265 period => {
266 type => 'integer',
8d88a594
TL
267 description => "Every 'period' milliseconds the entropy-injection quota is reset, allowing"
268 ." the guest to retrieve another 'max_bytes' of entropy.",
2cf61f33
SR
269 optional => 1,
270 default => 1000,
271 },
272};
273
26b443c8
TL
274my $meta_info_fmt = {
275 'ctime' => {
276 type => 'integer',
277 description => "The guest creation timestamp as UNIX epoch time",
278 minimum => 0,
279 optional => 1,
280 },
af2a1a1c
TL
281 'creation-qemu' => {
282 type => 'string',
283 description => "The QEMU (machine) version from the time this VM was created.",
284 pattern => '\d+(\.\d+)+',
285 optional => 1,
286 },
26b443c8
TL
287};
288
1e3baf05
DM
289my $confdesc = {
290 onboot => {
291 optional => 1,
292 type => 'boolean',
293 description => "Specifies whether a VM will be started during system bootup.",
294 default => 0,
295 },
296 autostart => {
297 optional => 1,
298 type => 'boolean',
299 description => "Automatic restart after crash (currently ignored).",
300 default => 0,
301 },
2ff09f52 302 hotplug => {
483ceeab
TL
303 optional => 1,
304 type => 'string', format => 'pve-hotplug-features',
305 description => "Selectively enable hotplug features. This is a comma separated list of"
8d88a594
TL
306 ." hotplug features: 'network', 'disk', 'cpu', 'memory' and 'usb'. Use '0' to disable"
307 ." hotplug completely. Using '1' as value is an alias for the default `network,disk,usb`.",
b3c2bdd1 308 default => 'network,disk,usb',
2ff09f52 309 },
1e3baf05
DM
310 reboot => {
311 optional => 1,
312 type => 'boolean',
313 description => "Allow reboot. If set to '0' the VM exit on reboot.",
314 default => 1,
315 },
316 lock => {
317 optional => 1,
318 type => 'string',
319 description => "Lock/unlock the VM.",
159719e5 320 enum => [qw(backup clone create migrate rollback snapshot snapshot-delete suspending suspended)],
1e3baf05
DM
321 },
322 cpulimit => {
323 optional => 1,
c6f773b8 324 type => 'number',
52261945 325 description => "Limit of CPU usage.",
8d88a594
TL
326 verbose_description => "Limit of CPU usage.\n\nNOTE: If the computer has 2 CPUs, it has"
327 ." total of '2' CPU time. Value '0' indicates no CPU limit.",
1e3baf05 328 minimum => 0,
c6f773b8 329 maximum => 128,
483ceeab 330 default => 0,
1e3baf05
DM
331 },
332 cpuunits => {
333 optional => 1,
334 type => 'integer',
483ceeab 335 description => "CPU weight for a VM, will be clamped to [1, 10000] in cgroup v2.",
67498860
TL
336 verbose_description => "CPU weight for a VM. Argument is used in the kernel fair scheduler."
337 ." The larger the number is, the more CPU time this VM gets. Number is relative to"
338 ." weights of all the other running VMs.",
e65e35ca 339 minimum => 1,
237239bf 340 maximum => 262144,
67498860 341 default => 'cgroup v1: 1024, cgroup v2: 100',
1e3baf05
DM
342 },
343 memory => {
344 optional => 1,
345 type => 'integer',
8d88a594
TL
346 description => "Amount of RAM for the VM in MB. This is the maximum available memory when"
347 ." you use the balloon device.",
1e3baf05
DM
348 minimum => 16,
349 default => 512,
350 },
13a48620 351 balloon => {
483ceeab
TL
352 optional => 1,
353 type => 'integer',
354 description => "Amount of target RAM for the VM in MB. Using zero disables the ballon driver.",
8b1accf7
DM
355 minimum => 0,
356 },
357 shares => {
483ceeab
TL
358 optional => 1,
359 type => 'integer',
360 description => "Amount of memory shares for auto-ballooning. The larger the number is, the"
8d88a594
TL
361 ." more memory this VM gets. Number is relative to weights of all other running VMs."
362 ." Using zero disables auto-ballooning. Auto-ballooning is done by pvestatd.",
8b1accf7
DM
363 minimum => 0,
364 maximum => 50000,
365 default => 1000,
13a48620 366 },
1e3baf05
DM
367 keyboard => {
368 optional => 1,
369 type => 'string',
8d88a594
TL
370 description => "Keyboard layout for VNC server. The default is read from the"
371 ."'/etc/pve/datacenter.cfg' configuration file. It should not be necessary to set it.",
e95fe75f 372 enum => PVE::Tools::kvmkeymaplist(),
aea47dd6 373 default => undef,
1e3baf05
DM
374 },
375 name => {
376 optional => 1,
7fabe17d 377 type => 'string', format => 'dns-name',
1e3baf05
DM
378 description => "Set a name for the VM. Only used on the configuration web interface.",
379 },
cdd20088
AD
380 scsihw => {
381 optional => 1,
382 type => 'string',
52261945 383 description => "SCSI controller model",
6731a4cf 384 enum => [qw(lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi)],
cdd20088
AD
385 default => 'lsi',
386 },
1e3baf05
DM
387 description => {
388 optional => 1,
389 type => 'string',
a200af10
TL
390 description => "Description for the VM. Shown in the web-interface VM's summary."
391 ." This is saved as comment inside the configuration file.",
392 maxLength => 1024 * 8,
1e3baf05
DM
393 },
394 ostype => {
395 optional => 1,
396 type => 'string',
483ceeab 397 enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 win10 win11 l24 l26 solaris)],
52261945
DM
398 description => "Specify guest operating system.",
399 verbose_description => <<EODESC,
400Specify guest operating system. This is used to enable special
401optimization/features for specific operating systems:
402
403[horizontal]
404other;; unspecified OS
405wxp;; Microsoft Windows XP
406w2k;; Microsoft Windows 2000
407w2k3;; Microsoft Windows 2003
408w2k8;; Microsoft Windows 2008
409wvista;; Microsoft Windows Vista
410win7;; Microsoft Windows 7
44c2a647 411win8;; Microsoft Windows 8/2012/2012r2
1f5828f2 412win10;; Microsoft Windows 10/2016/2019
179b9f1b 413win11;; Microsoft Windows 11/2022
52261945 414l24;; Linux 2.4 Kernel
a5269260 415l26;; Linux 2.6 - 5.X Kernel
52261945 416solaris;; Solaris/OpenSolaris/OpenIndiania kernel
1e3baf05
DM
417EODESC
418 },
419 boot => {
420 optional => 1,
2141a802 421 type => 'string', format => 'pve-qm-boot',
483ceeab
TL
422 description => "Specify guest boot order. Use the 'order=' sub-property as usage with no"
423 ." key or 'legacy=' is deprecated.",
1e3baf05
DM
424 },
425 bootdisk => {
426 optional => 1,
427 type => 'string', format => 'pve-qm-bootdisk',
2141a802 428 description => "Enable booting from specified disk. Deprecated: Use 'boot: order=foo;bar' instead.",
03e480fc 429 pattern => '(ide|sata|scsi|virtio)\d+',
1e3baf05
DM
430 },
431 smp => {
432 optional => 1,
433 type => 'integer',
434 description => "The number of CPUs. Please use option -sockets instead.",
435 minimum => 1,
436 default => 1,
437 },
438 sockets => {
439 optional => 1,
440 type => 'integer',
441 description => "The number of CPU sockets.",
442 minimum => 1,
443 default => 1,
444 },
445 cores => {
446 optional => 1,
447 type => 'integer',
448 description => "The number of cores per socket.",
449 minimum => 1,
450 default => 1,
451 },
8a010eae
AD
452 numa => {
453 optional => 1,
454 type => 'boolean',
1917695c 455 description => "Enable/disable NUMA.",
8a010eae
AD
456 default => 0,
457 },
7023f3ea
AD
458 hugepages => {
459 optional => 1,
460 type => 'string',
461 description => "Enable/disable hugepages memory.",
462 enum => [qw(any 2 1024)],
463 },
f36e9894
SR
464 keephugepages => {
465 optional => 1,
466 type => 'boolean',
467 default => 0,
4df98f2f
TL
468 description => "Use together with hugepages. If enabled, hugepages will not not be deleted"
469 ." after VM shutdown and can be used for subsequent starts.",
f36e9894 470 },
de9d1e55 471 vcpus => {
3bd18e48
AD
472 optional => 1,
473 type => 'integer',
de9d1e55 474 description => "Number of hotplugged vcpus.",
3bd18e48 475 minimum => 1,
de9d1e55 476 default => 0,
3bd18e48 477 },
1e3baf05
DM
478 acpi => {
479 optional => 1,
480 type => 'boolean',
481 description => "Enable/disable ACPI.",
482 default => 1,
483 },
bc84dcca 484 agent => {
ab6a046f 485 optional => 1,
8d88a594 486 description => "Enable/disable communication with the Qemu Guest Agent and its properties.",
9d66b397
SI
487 type => 'string',
488 format => $agent_fmt,
ab6a046f 489 },
1e3baf05
DM
490 kvm => {
491 optional => 1,
492 type => 'boolean',
493 description => "Enable/disable KVM hardware virtualization.",
494 default => 1,
495 },
496 tdf => {
497 optional => 1,
498 type => 'boolean',
8c559505
DM
499 description => "Enable/disable time drift fix.",
500 default => 0,
1e3baf05 501 },
19672434 502 localtime => {
1e3baf05
DM
503 optional => 1,
504 type => 'boolean',
8d88a594
TL
505 description => "Set the real time clock (RTC) to local time. This is enabled by default if"
506 ." the `ostype` indicates a Microsoft Windows OS.",
1e3baf05
DM
507 },
508 freeze => {
509 optional => 1,
510 type => 'boolean',
511 description => "Freeze CPU at startup (use 'c' monitor command to start execution).",
512 },
513 vga => {
514 optional => 1,
55655ebc
DC
515 type => 'string', format => $vga_fmt,
516 description => "Configure the VGA hardware.",
4df98f2f
TL
517 verbose_description => "Configure the VGA Hardware. If you want to use high resolution"
518 ." modes (>= 1280x1024x16) you may need to increase the vga memory option. Since QEMU"
519 ." 2.9 the default VGA display type is 'std' for all OS types besides some Windows"
520 ." versions (XP and older) which use 'cirrus'. The 'qxl' option enables the SPICE"
521 ." display server. For win* OS you can select how many independent displays you want,"
522 ." Linux guests can add displays them self.\nYou can also run without any graphic card,"
523 ." using a serial device as terminal.",
1e3baf05 524 },
0ea9541d
DM
525 watchdog => {
526 optional => 1,
527 type => 'string', format => 'pve-qm-watchdog',
52261945 528 description => "Create a virtual hardware watchdog device.",
4df98f2f
TL
529 verbose_description => "Create a virtual hardware watchdog device. Once enabled (by a guest"
530 ." action), the watchdog must be periodically polled by an agent inside the guest or"
531 ." else the watchdog will reset the guest (or execute the respective action specified)",
0ea9541d 532 },
1e3baf05
DM
533 startdate => {
534 optional => 1,
19672434 535 type => 'string',
1e3baf05 536 typetext => "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)",
4df98f2f
TL
537 description => "Set the initial date of the real time clock. Valid format for date are:"
538 ."'now' or '2006-06-17T16:01:21' or '2006-06-17'.",
1e3baf05
DM
539 pattern => '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)',
540 default => 'now',
541 },
43574f73 542 startup => get_standard_option('pve-startup-order'),
68eda3ab
AD
543 template => {
544 optional => 1,
545 type => 'boolean',
546 description => "Enable/disable Template.",
547 default => 0,
548 },
1e3baf05
DM
549 args => {
550 optional => 1,
551 type => 'string',
52261945
DM
552 description => "Arbitrary arguments passed to kvm.",
553 verbose_description => <<EODESCR,
c7a8aad6 554Arbitrary arguments passed to kvm, for example:
1e3baf05
DM
555
556args: -no-reboot -no-hpet
c7a8aad6
FG
557
558NOTE: this option is for experts only.
1e3baf05
DM
559EODESCR
560 },
561 tablet => {
562 optional => 1,
563 type => 'boolean',
564 default => 1,
52261945 565 description => "Enable/disable the USB tablet device.",
4df98f2f
TL
566 verbose_description => "Enable/disable the USB tablet device. This device is usually needed"
567 ." to allow absolute mouse positioning with VNC. Else the mouse runs out of sync with"
568 ." normal VNC clients. If you're running lots of console-only guests on one host, you"
569 ." may consider disabling this to save some context switches. This is turned off by"
570 ." default if you use spice (`qm set <vmid> --vga qxl`).",
1e3baf05
DM
571 },
572 migrate_speed => {
573 optional => 1,
574 type => 'integer',
575 description => "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
576 minimum => 0,
577 default => 0,
578 },
579 migrate_downtime => {
580 optional => 1,
04432191 581 type => 'number',
1e3baf05
DM
582 description => "Set maximum tolerated downtime (in seconds) for migrations.",
583 minimum => 0,
04432191 584 default => 0.1,
1e3baf05
DM
585 },
586 cdrom => {
587 optional => 1,
b799312f 588 type => 'string', format => 'pve-qm-ide',
8485b9ba 589 typetext => '<volume>',
1e3baf05
DM
590 description => "This is an alias for option -ide2",
591 },
592 cpu => {
593 optional => 1,
594 description => "Emulated CPU type.",
595 type => 'string',
5d008ad3 596 format => 'pve-vm-cpu-conf',
1e3baf05 597 },
b7ba6b79
DM
598 parent => get_standard_option('pve-snapshot-name', {
599 optional => 1,
600 description => "Parent snapshot name. This is used internally, and should not be modified.",
601 }),
982c7f12
DM
602 snaptime => {
603 optional => 1,
604 description => "Timestamp for snapshots.",
605 type => 'integer',
606 minimum => 0,
607 },
18bfb361
DM
608 vmstate => {
609 optional => 1,
610 type => 'string', format => 'pve-volume-id',
4df98f2f
TL
611 description => "Reference to a volume which stores the VM state. This is used internally"
612 ." for snapshots.",
18bfb361 613 },
253624c7
FG
614 vmstatestorage => get_standard_option('pve-storage-id', {
615 description => "Default storage for VM state volumes/files.",
616 optional => 1,
617 }),
c6737ef1 618 runningmachine => get_standard_option('pve-qemu-machine', {
4df98f2f
TL
619 description => "Specifies the QEMU machine type of the running vm. This is used internally"
620 ." for snapshots.",
c6737ef1 621 }),
ea1c2110 622 runningcpu => {
4df98f2f
TL
623 description => "Specifies the QEMU '-cpu' parameter of the running vm. This is used"
624 ." internally for snapshots.",
ea1c2110
SR
625 optional => 1,
626 type => 'string',
627 pattern => $PVE::QemuServer::CPUConfig::qemu_cmdline_cpu_re,
628 format_description => 'QEMU -cpu parameter'
629 },
c6737ef1 630 machine => get_standard_option('pve-qemu-machine'),
d731ecbe
WB
631 arch => {
632 description => "Virtual processor architecture. Defaults to the host.",
633 optional => 1,
634 type => 'string',
635 enum => [qw(x86_64 aarch64)],
636 },
2796e7d5
DM
637 smbios1 => {
638 description => "Specify SMBIOS type 1 fields.",
639 type => 'string', format => 'pve-qm-smbios1',
5d004b00 640 maxLength => 512,
2796e7d5
DM
641 optional => 1,
642 },
cb0e4540
AG
643 protection => {
644 optional => 1,
645 type => 'boolean',
4df98f2f
TL
646 description => "Sets the protection flag of the VM. This will disable the remove VM and"
647 ." remove disk operations.",
cb0e4540
AG
648 default => 0,
649 },
3edb45e7 650 bios => {
a783c78e 651 optional => 1,
3edb45e7
DM
652 type => 'string',
653 enum => [ qw(seabios ovmf) ],
654 description => "Select BIOS implementation.",
655 default => 'seabios',
a783c78e 656 },
6ee499ff
DC
657 vmgenid => {
658 type => 'string',
659 pattern => '(?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])',
660 format_description => 'UUID',
4df98f2f
TL
661 description => "Set VM Generation ID. Use '1' to autogenerate on create or update, pass '0'"
662 ." to disable explicitly.",
663 verbose_description => "The VM generation ID (vmgenid) device exposes a 128-bit integer"
664 ." value identifier to the guest OS. This allows to notify the guest operating system"
665 ." when the virtual machine is executed with a different configuration (e.g. snapshot"
666 ." execution or creation from a template). The guest operating system notices the"
667 ." change, and is then able to react as appropriate by marking its copies of"
668 ." distributed databases as dirty, re-initializing its random number generator, etc.\n"
669 ."Note that auto-creation only works when done through API/CLI create or update methods"
670 .", but not when manually editing the config file.",
f7ed64e7 671 default => "1 (autogenerated)",
6ee499ff
DC
672 optional => 1,
673 },
9e784b11
DC
674 hookscript => {
675 type => 'string',
676 format => 'pve-volume-id',
677 optional => 1,
678 description => "Script that will be executed during various steps in the vms lifetime.",
679 },
6dbcb073
DC
680 ivshmem => {
681 type => 'string',
682 format => $ivshmem_fmt,
4df98f2f
TL
683 description => "Inter-VM shared memory. Useful for direct communication between VMs, or to"
684 ." the host.",
6dbcb073 685 optional => 1,
2e7b5925
AL
686 },
687 audio0 => {
688 type => 'string',
1448547f 689 format => $audio_fmt,
194b65f1 690 description => "Configure a audio device, useful in combination with QXL/Spice.",
2e7b5925
AL
691 optional => 1
692 },
c4df18db
AL
693 spice_enhancements => {
694 type => 'string',
695 format => $spice_enhancements_fmt,
696 description => "Configure additional enhancements for SPICE.",
697 optional => 1
698 },
b8e7068a
DC
699 tags => {
700 type => 'string', format => 'pve-tag-list',
701 description => 'Tags of the VM. This is only meta information.',
702 optional => 1,
703 },
2cf61f33
SR
704 rng0 => {
705 type => 'string',
706 format => $rng_fmt,
707 description => "Configure a VirtIO-based Random Number Generator.",
708 optional => 1,
709 },
26b443c8
TL
710 meta => {
711 type => 'string',
712 format => $meta_info_fmt,
713 description => "Some (read-only) meta-information about this guest.",
714 optional => 1,
715 },
9ed7a77c
WB
716};
717
cb702ebe
DL
718my $cicustom_fmt = {
719 meta => {
720 type => 'string',
721 optional => 1,
4df98f2f
TL
722 description => 'Specify a custom file containing all meta data passed to the VM via"
723 ." cloud-init. This is provider specific meaning configdrive2 and nocloud differ.',
cb702ebe
DL
724 format => 'pve-volume-id',
725 format_description => 'volume',
726 },
727 network => {
728 type => 'string',
729 optional => 1,
4df98f2f
TL
730 description => 'Specify a custom file containing all network data passed to the VM via'
731 .' cloud-init.',
cb702ebe
DL
732 format => 'pve-volume-id',
733 format_description => 'volume',
734 },
735 user => {
736 type => 'string',
737 optional => 1,
4df98f2f
TL
738 description => 'Specify a custom file containing all user data passed to the VM via'
739 .' cloud-init.',
cb702ebe
DL
740 format => 'pve-volume-id',
741 format_description => 'volume',
742 },
101beafe
CH
743 vendor => {
744 type => 'string',
745 optional => 1,
746 description => 'Specify a custom file containing all vendor data passed to the VM via'
747 .' cloud-init.',
748 format => 'pve-volume-id',
749 format_description => 'volume',
750 },
cb702ebe
DL
751};
752PVE::JSONSchema::register_format('pve-qm-cicustom', $cicustom_fmt);
753
9ed7a77c 754my $confdesc_cloudinit = {
41cd94a0
WB
755 citype => {
756 optional => 1,
757 type => 'string',
4df98f2f
TL
758 description => 'Specifies the cloud-init configuration format. The default depends on the'
759 .' configured operating system type (`ostype`. We use the `nocloud` format for Linux,'
760 .' and `configdrive2` for windows.',
545eec65 761 enum => ['configdrive2', 'nocloud', 'opennebula'],
41cd94a0 762 },
7b42f951
WB
763 ciuser => {
764 optional => 1,
765 type => 'string',
4df98f2f
TL
766 description => "cloud-init: User name to change ssh keys and password for instead of the"
767 ." image's configured default user.",
7b42f951
WB
768 },
769 cipassword => {
770 optional => 1,
771 type => 'string',
4df98f2f
TL
772 description => 'cloud-init: Password to assign the user. Using this is generally not'
773 .' recommended. Use ssh keys instead. Also note that older cloud-init versions do not'
774 .' support hashed passwords.',
7b42f951 775 },
cb702ebe
DL
776 cicustom => {
777 optional => 1,
778 type => 'string',
4df98f2f
TL
779 description => 'cloud-init: Specify custom files to replace the automatically generated'
780 .' ones at start.',
cb702ebe
DL
781 format => 'pve-qm-cicustom',
782 },
0c9a7596
AD
783 searchdomain => {
784 optional => 1,
785 type => 'string',
4df98f2f
TL
786 description => "cloud-init: Sets DNS search domains for a container. Create will'
787 .' automatically use the setting from the host if neither searchdomain nor nameserver'
788 .' are set.",
0c9a7596
AD
789 },
790 nameserver => {
791 optional => 1,
792 type => 'string', format => 'address-list',
4df98f2f
TL
793 description => "cloud-init: Sets DNS server IP address for a container. Create will'
794 .' automatically use the setting from the host if neither searchdomain nor nameserver'
795 .' are set.",
0c9a7596
AD
796 },
797 sshkeys => {
798 optional => 1,
799 type => 'string',
800 format => 'urlencoded',
1d1c4e1c 801 description => "cloud-init: Setup public SSH keys (one key per line, OpenSSH format).",
0c9a7596 802 },
1e3baf05
DM
803};
804
805# what about other qemu settings ?
806#cpu => 'string',
807#machine => 'string',
808#fda => 'file',
809#fdb => 'file',
810#mtdblock => 'file',
811#sd => 'file',
812#pflash => 'file',
813#snapshot => 'bool',
814#bootp => 'file',
815##tftp => 'dir',
816##smb => 'dir',
817#kernel => 'file',
818#append => 'string',
819#initrd => 'file',
820##soundhw => 'string',
821
822while (my ($k, $v) = each %$confdesc) {
823 PVE::JSONSchema::register_standard_option("pve-qm-$k", $v);
824}
825
1e3baf05 826my $MAX_USB_DEVICES = 5;
5bdcf937 827my $MAX_NETS = 32;
bae179aa 828my $MAX_SERIAL_PORTS = 4;
1989a89c 829my $MAX_PARALLEL_PORTS = 3;
2ed5d572
AD
830my $MAX_NUMA = 8;
831
ffc0d8c7
WB
832my $numa_fmt = {
833 cpus => {
834 type => "string",
835 pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
52261945 836 description => "CPUs accessing this NUMA node.",
ffc0d8c7
WB
837 format_description => "id[-id];...",
838 },
839 memory => {
840 type => "number",
52261945 841 description => "Amount of memory this NUMA node provides.",
ffc0d8c7
WB
842 optional => 1,
843 },
844 hostnodes => {
845 type => "string",
846 pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
52261945 847 description => "Host NUMA nodes to use.",
ffc0d8c7
WB
848 format_description => "id[-id];...",
849 optional => 1,
850 },
851 policy => {
852 type => 'string',
853 enum => [qw(preferred bind interleave)],
52261945 854 description => "NUMA allocation policy.",
ffc0d8c7
WB
855 optional => 1,
856 },
857};
858PVE::JSONSchema::register_format('pve-qm-numanode', $numa_fmt);
2ed5d572
AD
859my $numadesc = {
860 optional => 1,
ffc0d8c7 861 type => 'string', format => $numa_fmt,
52261945 862 description => "NUMA topology.",
2ed5d572
AD
863};
864PVE::JSONSchema::register_standard_option("pve-qm-numanode", $numadesc);
865
866for (my $i = 0; $i < $MAX_NUMA; $i++) {
867 $confdesc->{"numa$i"} = $numadesc;
868}
1e3baf05 869
f7bc17ca
TL
870my $nic_model_list = [
871 'e1000',
872 'e1000-82540em',
873 'e1000-82544gc',
874 'e1000-82545em',
e83dd50a 875 'e1000e',
f7bc17ca
TL
876 'i82551',
877 'i82557b',
878 'i82559er',
879 'ne2k_isa',
880 'ne2k_pci',
881 'pcnet',
882 'rtl8139',
883 'virtio',
884 'vmxnet3',
885];
6b64503e 886my $nic_model_list_txt = join(' ', sort @$nic_model_list);
1e3baf05 887
52261945
DM
888my $net_fmt_bridge_descr = <<__EOD__;
889Bridge to attach the network device to. The Proxmox VE standard bridge
890is called 'vmbr0'.
891
892If you do not specify a bridge, we create a kvm user (NATed) network
893device, which provides DHCP and DNS services. The following addresses
894are used:
895
896 10.0.2.2 Gateway
897 10.0.2.3 DNS Server
898 10.0.2.4 SMB Server
899
900The DHCP server assign addresses to the guest starting from 10.0.2.15.
901__EOD__
902
cd9c34d1 903my $net_fmt = {
399d96db 904 macaddr => get_standard_option('mac-addr', {
4df98f2f
TL
905 description => "MAC address. That address must be unique withing your network. This is"
906 ." automatically generated if not specified.",
399d96db 907 }),
7f694a71
DM
908 model => {
909 type => 'string',
4df98f2f
TL
910 description => "Network Card Model. The 'virtio' model provides the best performance with"
911 ." very low CPU overhead. If your guest does not support this driver, it is usually"
912 ." best to use 'e1000'.",
7f694a71
DM
913 enum => $nic_model_list,
914 default_key => 1,
915 },
916 (map { $_ => { keyAlias => 'model', alias => 'macaddr' }} @$nic_model_list),
dbdcc5cd 917 bridge => get_standard_option('pve-bridge-id', {
52261945 918 description => $net_fmt_bridge_descr,
cd9c34d1 919 optional => 1,
dbdcc5cd 920 }),
cd9c34d1
WB
921 queues => {
922 type => 'integer',
923 minimum => 0, maximum => 16,
924 description => 'Number of packet queues to be used on the device.',
cd9c34d1
WB
925 optional => 1,
926 },
927 rate => {
928 type => 'number',
929 minimum => 0,
52261945 930 description => "Rate limit in mbps (megabytes per second) as floating point number.",
cd9c34d1
WB
931 optional => 1,
932 },
933 tag => {
934 type => 'integer',
9f41a659 935 minimum => 1, maximum => 4094,
cd9c34d1 936 description => 'VLAN tag to apply to packets on this interface.',
cd9c34d1
WB
937 optional => 1,
938 },
939 trunks => {
940 type => 'string',
941 pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
942 description => 'VLAN trunks to pass through this interface.',
7f694a71 943 format_description => 'vlanid[;vlanid...]',
cd9c34d1
WB
944 optional => 1,
945 },
946 firewall => {
947 type => 'boolean',
948 description => 'Whether this interface should be protected by the firewall.',
cd9c34d1
WB
949 optional => 1,
950 },
951 link_down => {
952 type => 'boolean',
52261945 953 description => 'Whether this interface should be disconnected (like pulling the plug).',
cd9c34d1
WB
954 optional => 1,
955 },
61a14cde
AD
956 mtu => {
957 type => 'integer',
958 minimum => 1, maximum => 65520,
0530177b 959 description => "Force MTU, for VirtIO only. Set to '1' to use the bridge MTU",
61a14cde
AD
960 optional => 1,
961 },
cd9c34d1 962};
52261945 963
1e3baf05
DM
964my $netdesc = {
965 optional => 1,
7f694a71 966 type => 'string', format => $net_fmt,
52261945 967 description => "Specify network devices.",
1e3baf05 968};
52261945 969
1e3baf05
DM
970PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
971
0c9a7596
AD
972my $ipconfig_fmt = {
973 ip => {
974 type => 'string',
975 format => 'pve-ipv4-config',
976 format_description => 'IPv4Format/CIDR',
977 description => 'IPv4 address in CIDR format.',
978 optional => 1,
979 default => 'dhcp',
980 },
981 gw => {
982 type => 'string',
983 format => 'ipv4',
984 format_description => 'GatewayIPv4',
985 description => 'Default gateway for IPv4 traffic.',
986 optional => 1,
987 requires => 'ip',
988 },
989 ip6 => {
990 type => 'string',
991 format => 'pve-ipv6-config',
992 format_description => 'IPv6Format/CIDR',
993 description => 'IPv6 address in CIDR format.',
994 optional => 1,
995 default => 'dhcp',
996 },
997 gw6 => {
998 type => 'string',
999 format => 'ipv6',
1000 format_description => 'GatewayIPv6',
1001 description => 'Default gateway for IPv6 traffic.',
1002 optional => 1,
1003 requires => 'ip6',
1004 },
1005};
1006PVE::JSONSchema::register_format('pve-qm-ipconfig', $ipconfig_fmt);
1007my $ipconfigdesc = {
1008 optional => 1,
1009 type => 'string', format => 'pve-qm-ipconfig',
1010 description => <<'EODESCR',
1011cloud-init: Specify IP addresses and gateways for the corresponding interface.
1012
1013IP addresses use CIDR notation, gateways are optional but need an IP of the same type specified.
1014
4df98f2f
TL
1015The special string 'dhcp' can be used for IP addresses to use DHCP, in which case no explicit
1016gateway should be provided.
988be8d0
ML
1017For IPv6 the special string 'auto' can be used to use stateless autoconfiguration. This requires
1018cloud-init 19.4 or newer.
0c9a7596 1019
4df98f2f
TL
1020If cloud-init is enabled and neither an IPv4 nor an IPv6 address is specified, it defaults to using
1021dhcp on IPv4.
0c9a7596
AD
1022EODESCR
1023};
1024PVE::JSONSchema::register_standard_option("pve-qm-ipconfig", $netdesc);
1025
1e3baf05
DM
1026for (my $i = 0; $i < $MAX_NETS; $i++) {
1027 $confdesc->{"net$i"} = $netdesc;
9ed7a77c
WB
1028 $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
1029}
1030
1031foreach my $key (keys %$confdesc_cloudinit) {
1032 $confdesc->{$key} = $confdesc_cloudinit->{$key};
1e3baf05
DM
1033}
1034
ffa42b86
DC
1035PVE::JSONSchema::register_format('pve-volume-id-or-qm-path', \&verify_volume_id_or_qm_path);
1036sub verify_volume_id_or_qm_path {
822c8a07
WB
1037 my ($volid, $noerr) = @_;
1038
6e55f579
FE
1039 return $volid if $volid eq 'none' || $volid eq 'cdrom';
1040
1041 return verify_volume_id_or_absolute_path($volid, $noerr);
1042}
1043
1044PVE::JSONSchema::register_format('pve-volume-id-or-absolute-path', \&verify_volume_id_or_absolute_path);
1045sub verify_volume_id_or_absolute_path {
1046 my ($volid, $noerr) = @_;
1047
1048 return $volid if $volid =~ m|^/|;
ffa42b86 1049
822c8a07
WB
1050 $volid = eval { PVE::JSONSchema::check_format('pve-volume-id', $volid, '') };
1051 if ($@) {
d1c1af4b 1052 return if $noerr;
822c8a07
WB
1053 die $@;
1054 }
1055 return $volid;
1056}
1057
ff6ffe20 1058my $usb_fmt = {
a6b9aee4
DC
1059 host => {
1060 default_key => 1,
1061 type => 'string', format => 'pve-qm-usb-device',
1062 format_description => 'HOSTUSBDEVICE|spice',
52261945
DM
1063 description => <<EODESCR,
1064The Host USB device or port or the value 'spice'. HOSTUSBDEVICE syntax is:
1065
1066 'bus-port(.port)*' (decimal numbers) or
1067 'vendor_id:product_id' (hexadeciaml numbers) or
1068 'spice'
1069
1070You can use the 'lsusb -t' command to list existing usb devices.
1071
4df98f2f
TL
1072NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such
1073machines - use with special care.
52261945
DM
1074
1075The value 'spice' can be used to add a usb redirection devices for spice.
1076EODESCR
a6b9aee4
DC
1077 },
1078 usb3 => {
1079 optional => 1,
1080 type => 'boolean',
97ef5356 1081 description => "Specifies whether if given host option is a USB3 device or port.",
52261945 1082 default => 0,
a6b9aee4
DC
1083 },
1084};
1085
1e3baf05
DM
1086my $usbdesc = {
1087 optional => 1,
ff6ffe20 1088 type => 'string', format => $usb_fmt,
52261945 1089 description => "Configure an USB device (n is 0 to 4).",
1e3baf05
DM
1090};
1091PVE::JSONSchema::register_standard_option("pve-qm-usb", $usbdesc);
1092
bae179aa
DA
1093my $serialdesc = {
1094 optional => 1,
ca0cef26 1095 type => 'string',
1b0b51ed 1096 pattern => '(/dev/.+|socket)',
52261945
DM
1097 description => "Create a serial device inside the VM (n is 0 to 3)",
1098 verbose_description => <<EODESCR,
52261945
DM
1099Create a serial device inside the VM (n is 0 to 3), and pass through a
1100host serial device (i.e. /dev/ttyS0), or create a unix socket on the
1101host side (use 'qm terminal' to open a terminal connection).
bae179aa 1102
4df98f2f
TL
1103NOTE: If you pass through a host serial device, it is no longer possible to migrate such machines -
1104use with special care.
bae179aa 1105
52261945 1106CAUTION: Experimental! User reported problems with this option.
bae179aa
DA
1107EODESCR
1108};
bae179aa 1109
1989a89c
DA
1110my $paralleldesc= {
1111 optional => 1,
ca0cef26 1112 type => 'string',
9ecc8431 1113 pattern => '/dev/parport\d+|/dev/usb/lp\d+',
52261945
DM
1114 description => "Map host parallel devices (n is 0 to 2).",
1115 verbose_description => <<EODESCR,
19672434 1116Map host parallel devices (n is 0 to 2).
1989a89c 1117
4df98f2f
TL
1118NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such
1119machines - use with special care.
1989a89c 1120
52261945 1121CAUTION: Experimental! User reported problems with this option.
1989a89c
DA
1122EODESCR
1123};
1989a89c
DA
1124
1125for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
1126 $confdesc->{"parallel$i"} = $paralleldesc;
1127}
1128
bae179aa
DA
1129for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
1130 $confdesc->{"serial$i"} = $serialdesc;
1131}
1132
74c17b7a
SR
1133for (my $i = 0; $i < $PVE::QemuServer::PCI::MAX_HOSTPCI_DEVICES; $i++) {
1134 $confdesc->{"hostpci$i"} = $PVE::QemuServer::PCI::hostpcidesc;
040b06b7 1135}
1e3baf05 1136
e0fd2b2f
FE
1137for my $key (keys %{$PVE::QemuServer::Drive::drivedesc_hash}) {
1138 $confdesc->{$key} = $PVE::QemuServer::Drive::drivedesc_hash->{$key};
cdb0931f
DA
1139}
1140
1e3baf05
DM
1141for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
1142 $confdesc->{"usb$i"} = $usbdesc;
1143}
1144
5cfa9f5f
SR
1145my $boot_fmt = {
1146 legacy => {
1147 optional => 1,
1148 default_key => 1,
1149 type => 'string',
1150 description => "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)."
1151 . " Deprecated, use 'order=' instead.",
1152 pattern => '[acdn]{1,4}',
1153 format_description => "[acdn]{1,4}",
1154
1155 # note: this is also the fallback if boot: is not given at all
1156 default => 'cdn',
1157 },
1158 order => {
1159 optional => 1,
1160 type => 'string',
1161 format => 'pve-qm-bootdev-list',
1162 format_description => "device[;device...]",
1163 description => <<EODESC,
1164The guest will attempt to boot from devices in the order they appear here.
1165
1166Disks, optical drives and passed-through storage USB devices will be directly
1167booted from, NICs will load PXE, and PCIe devices will either behave like disks
1168(e.g. NVMe) or load an option ROM (e.g. RAID controller, hardware NIC).
1169
1170Note that only devices in this list will be marked as bootable and thus loaded
1171by the guest firmware (BIOS/UEFI). If you require multiple disks for booting
1172(e.g. software-raid), you need to specify all of them here.
1173
1174Overrides the deprecated 'legacy=[acdn]*' value when given.
1175EODESC
1176 },
1177};
1178PVE::JSONSchema::register_format('pve-qm-boot', $boot_fmt);
1179
1180PVE::JSONSchema::register_format('pve-qm-bootdev', \&verify_bootdev);
1181sub verify_bootdev {
1182 my ($dev, $noerr) = @_;
1183
f9dde219
SR
1184 my $special = $dev =~ m/^efidisk/ || $dev =~ m/^tpmstate/;
1185 return $dev if PVE::QemuServer::Drive::is_valid_drivename($dev) && !$special;
5cfa9f5f
SR
1186
1187 my $check = sub {
1188 my ($base) = @_;
1189 return 0 if $dev !~ m/^$base\d+$/;
1190 return 0 if !$confdesc->{$dev};
1191 return 1;
1192 };
1193
1194 return $dev if $check->("net");
1195 return $dev if $check->("usb");
1196 return $dev if $check->("hostpci");
1197
d1c1af4b 1198 return if $noerr;
5cfa9f5f
SR
1199 die "invalid boot device '$dev'\n";
1200}
1201
1202sub print_bootorder {
1203 my ($devs) = @_;
4c27b18c 1204 return "" if !@$devs;
5cfa9f5f
SR
1205 my $data = { order => join(';', @$devs) };
1206 return PVE::JSONSchema::print_property_string($data, $boot_fmt);
1207}
1208
1e3baf05
DM
1209my $kvm_api_version = 0;
1210
1211sub kvm_version {
1e3baf05
DM
1212 return $kvm_api_version if $kvm_api_version;
1213
808a65b5 1214 open my $fh, '<', '/dev/kvm' or return;
1e3baf05 1215
646f2df4
WB
1216 # 0xae00 => KVM_GET_API_VERSION
1217 $kvm_api_version = ioctl($fh, 0xae00, 0);
808a65b5 1218 close($fh);
1e3baf05 1219
646f2df4 1220 return $kvm_api_version;
1e3baf05
DM
1221}
1222
1476b99f
DC
1223my $kvm_user_version = {};
1224my $kvm_mtime = {};
1e3baf05
DM
1225
1226sub kvm_user_version {
1476b99f 1227 my ($binary) = @_;
1e3baf05 1228
1476b99f
DC
1229 $binary //= get_command_for_arch(get_host_arch()); # get the native arch by default
1230 my $st = stat($binary);
1e3baf05 1231
1476b99f
DC
1232 my $cachedmtime = $kvm_mtime->{$binary} // -1;
1233 return $kvm_user_version->{$binary} if $kvm_user_version->{$binary} &&
1234 $cachedmtime == $st->mtime;
1235
1236 $kvm_user_version->{$binary} = 'unknown';
1237 $kvm_mtime->{$binary} = $st->mtime;
1e3baf05 1238
09b11429
TL
1239 my $code = sub {
1240 my $line = shift;
1241 if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
1476b99f 1242 $kvm_user_version->{$binary} = $2;
09b11429
TL
1243 }
1244 };
19672434 1245
1476b99f 1246 eval { run_command([$binary, '--version'], outfunc => $code); };
09b11429 1247 warn $@ if $@;
1e3baf05 1248
1476b99f 1249 return $kvm_user_version->{$binary};
1e3baf05
DM
1250
1251}
4df98f2f
TL
1252my sub extract_version {
1253 my ($machine_type, $version) = @_;
1254 $version = kvm_user_version() if !defined($version);
8eb73377 1255 return PVE::QemuServer::Machine::extract_version($machine_type, $version)
4df98f2f 1256}
1e3baf05 1257
db70021b
TL
1258sub kernel_has_vhost_net {
1259 return -c '/dev/vhost-net';
1260}
1e3baf05 1261
1e3baf05
DM
1262sub option_exists {
1263 my $key = shift;
1264 return defined($confdesc->{$key});
19672434 1265}
1e3baf05 1266
1e3baf05 1267my $cdrom_path;
1e3baf05
DM
1268sub get_cdrom_path {
1269
1270 return $cdrom_path if $cdrom_path;
1271
1272 return $cdrom_path = "/dev/cdrom" if -l "/dev/cdrom";
1273 return $cdrom_path = "/dev/cdrom1" if -l "/dev/cdrom1";
1274 return $cdrom_path = "/dev/cdrom2" if -l "/dev/cdrom2";
1275}
1276
1277sub get_iso_path {
1278 my ($storecfg, $vmid, $cdrom) = @_;
1279
1280 if ($cdrom eq 'cdrom') {
1281 return get_cdrom_path();
1282 } elsif ($cdrom eq 'none') {
1283 return '';
1284 } elsif ($cdrom =~ m|^/|) {
1285 return $cdrom;
1286 } else {
6b64503e 1287 return PVE::Storage::path($storecfg, $cdrom);
1e3baf05
DM
1288 }
1289}
1290
1291# try to convert old style file names to volume IDs
1292sub filename_to_volume_id {
1293 my ($vmid, $file, $media) = @_;
1294
0c9a7596 1295 if (!($file eq 'none' || $file eq 'cdrom' ||
1e3baf05 1296 $file =~ m|^/dev/.+| || $file =~ m/^([^:]+):(.+)$/)) {
19672434 1297
d1c1af4b 1298 return if $file =~ m|/|;
19672434 1299
1e3baf05
DM
1300 if ($media && $media eq 'cdrom') {
1301 $file = "local:iso/$file";
1302 } else {
1303 $file = "local:$vmid/$file";
1304 }
1305 }
1306
1307 return $file;
1308}
1309
1310sub verify_media_type {
1311 my ($opt, $vtype, $media) = @_;
1312
1313 return if !$media;
1314
1315 my $etype;
1316 if ($media eq 'disk') {
a125592c 1317 $etype = 'images';
1e3baf05
DM
1318 } elsif ($media eq 'cdrom') {
1319 $etype = 'iso';
1320 } else {
1321 die "internal error";
1322 }
1323
1324 return if ($vtype eq $etype);
19672434 1325
1e3baf05
DM
1326 raise_param_exc({ $opt => "unexpected media type ($vtype != $etype)" });
1327}
1328
1329sub cleanup_drive_path {
1330 my ($opt, $storecfg, $drive) = @_;
1331
1332 # try to convert filesystem paths to volume IDs
1333
1334 if (($drive->{file} !~ m/^(cdrom|none)$/) &&
1335 ($drive->{file} !~ m|^/dev/.+|) &&
1336 ($drive->{file} !~ m/^([^:]+):(.+)$/) &&
19672434 1337 ($drive->{file} !~ m/^\d+$/)) {
1e3baf05 1338 my ($vtype, $volid) = PVE::Storage::path_to_volume_id($storecfg, $drive->{file});
4df98f2f
TL
1339 raise_param_exc({ $opt => "unable to associate path '$drive->{file}' to any storage"})
1340 if !$vtype;
1e3baf05
DM
1341 $drive->{media} = 'cdrom' if !$drive->{media} && $vtype eq 'iso';
1342 verify_media_type($opt, $vtype, $drive->{media});
1343 $drive->{file} = $volid;
1344 }
1345
1346 $drive->{media} = 'cdrom' if !$drive->{media} && $drive->{file} =~ m/^(cdrom|none)$/;
1347}
1348
b3c2bdd1
DM
1349sub parse_hotplug_features {
1350 my ($data) = @_;
1351
1352 my $res = {};
1353
1354 return $res if $data eq '0';
a1b7d579 1355
b3c2bdd1
DM
1356 $data = $confdesc->{hotplug}->{default} if $data eq '1';
1357
45827685 1358 foreach my $feature (PVE::Tools::split_list($data)) {
b3c2bdd1
DM
1359 if ($feature =~ m/^(network|disk|cpu|memory|usb)$/) {
1360 $res->{$1} = 1;
1361 } else {
596a0a20 1362 die "invalid hotplug feature '$feature'\n";
b3c2bdd1
DM
1363 }
1364 }
1365 return $res;
1366}
1367
1368PVE::JSONSchema::register_format('pve-hotplug-features', \&pve_verify_hotplug_features);
1369sub pve_verify_hotplug_features {
1370 my ($value, $noerr) = @_;
1371
1372 return $value if parse_hotplug_features($value);
1373
d1c1af4b 1374 return if $noerr;
b3c2bdd1
DM
1375
1376 die "unable to parse hotplug option\n";
1377}
1378
28ef82d3
DM
1379sub scsi_inquiry {
1380 my($fh, $noerr) = @_;
1381
1382 my $SG_IO = 0x2285;
1383 my $SG_GET_VERSION_NUM = 0x2282;
1384
1385 my $versionbuf = "\x00" x 8;
1386 my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf);
1387 if (!$ret) {
1388 die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr;
d1c1af4b 1389 return;
28ef82d3 1390 }
97d62eb7 1391 my $version = unpack("I", $versionbuf);
28ef82d3
DM
1392 if ($version < 30000) {
1393 die "scsi generic interface too old\n" if !$noerr;
d1c1af4b 1394 return;
28ef82d3 1395 }
97d62eb7 1396
28ef82d3
DM
1397 my $buf = "\x00" x 36;
1398 my $sensebuf = "\x00" x 8;
f334aa3e 1399 my $cmd = pack("C x3 C x1", 0x12, 36);
97d62eb7 1400
28ef82d3
DM
1401 # see /usr/include/scsi/sg.h
1402 my $sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I";
1403
f606d5bd
TL
1404 my $packet = pack(
1405 $sg_io_hdr_t, ord('S'), -3, length($cmd), length($sensebuf), 0, length($buf), $buf, $cmd, $sensebuf, 6000
1406 );
28ef82d3
DM
1407
1408 $ret = ioctl($fh, $SG_IO, $packet);
1409 if (!$ret) {
1410 die "scsi ioctl SG_IO failed - $!\n" if !$noerr;
d1c1af4b 1411 return;
28ef82d3 1412 }
97d62eb7 1413
28ef82d3
DM
1414 my @res = unpack($sg_io_hdr_t, $packet);
1415 if ($res[17] || $res[18]) {
1416 die "scsi ioctl SG_IO status error - $!\n" if !$noerr;
d1c1af4b 1417 return;
28ef82d3
DM
1418 }
1419
1420 my $res = {};
f606d5bd 1421 $res->@{qw(type removable vendor product revision)} = unpack("C C x6 A8 A16 A4", $buf);
28ef82d3 1422
f606d5bd
TL
1423 $res->{removable} = $res->{removable} & 128 ? 1 : 0;
1424 $res->{type} &= 0x1F;
09984754 1425
28ef82d3
DM
1426 return $res;
1427}
1428
1429sub path_is_scsi {
1430 my ($path) = @_;
1431
d1c1af4b 1432 my $fh = IO::File->new("+<$path") || return;
28ef82d3
DM
1433 my $res = scsi_inquiry($fh, 1);
1434 close($fh);
1435
1436 return $res;
1437}
1438
db656e5f 1439sub print_tabletdevice_full {
d559309f 1440 my ($conf, $arch) = @_;
b467f79a 1441
3392d6ca 1442 my $q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf);
db656e5f
DM
1443
1444 # we use uhci for old VMs because tablet driver was buggy in older qemu
d559309f 1445 my $usbbus;
3392d6ca 1446 if (PVE::QemuServer::Machine::machine_type_is_q35($conf) || $arch eq 'aarch64') {
d559309f
WB
1447 $usbbus = 'ehci';
1448 } else {
1449 $usbbus = 'uhci';
1450 }
b467f79a 1451
db656e5f
DM
1452 return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
1453}
1454
d559309f 1455sub print_keyboarddevice_full {
6971c38e 1456 my ($conf, $arch) = @_;
d559309f 1457
d1c1af4b 1458 return if $arch ne 'aarch64';
d559309f
WB
1459
1460 return "usb-kbd,id=keyboard,bus=ehci.0,port=2";
1461}
1462
a183df68
TL
1463my sub get_drive_id {
1464 my ($drive) = @_;
1465 return "$drive->{interface}$drive->{index}";
1466}
1467
ca916ecc 1468sub print_drivedevice_full {
d559309f 1469 my ($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type) = @_;
ca916ecc
DA
1470
1471 my $device = '';
1472 my $maxdev = 0;
19672434 1473
a183df68 1474 my $drive_id = get_drive_id($drive);
ca916ecc 1475 if ($drive->{interface} eq 'virtio') {
4df98f2f
TL
1476 my $pciaddr = print_pci_addr("$drive_id", $bridges, $arch, $machine_type);
1477 $device = "virtio-blk-pci,drive=drive-$drive_id,id=${drive_id}${pciaddr}";
1478 $device .= ",iothread=iothread-$drive_id" if $drive->{iothread};
2ed36a41 1479 } elsif ($drive->{interface} eq 'scsi') {
6731a4cf 1480
ee034f5c 1481 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
2ed36a41
DM
1482 my $unit = $drive->{index} % $maxdev;
1483 my $devicetype = 'hd';
69bcf246
WB
1484 my $path = '';
1485 if (drive_is_cdrom($drive)) {
1486 $devicetype = 'cd';
29b19529 1487 } else {
69bcf246
WB
1488 if ($drive->{file} =~ m|^/|) {
1489 $path = $drive->{file};
1490 if (my $info = path_is_scsi($path)) {
8e3c33ab 1491 if ($info->{type} == 0 && $drive->{scsiblock}) {
69bcf246
WB
1492 $devicetype = 'block';
1493 } elsif ($info->{type} == 1) { # tape
1494 $devicetype = 'generic';
1495 }
1496 }
1497 } else {
1498 $path = PVE::Storage::path($storecfg, $drive->{file});
1499 }
1500
a034e3d6 1501 # for compatibility only, we prefer scsi-hd (#2408, #2355, #2380)
4df98f2f 1502 my $version = extract_version($machine_type, kvm_user_version());
a034e3d6 1503 if ($path =~ m/^iscsi\:\/\// &&
2ea5fb7e 1504 !min_version($version, 4, 1)) {
69bcf246
WB
1505 $devicetype = 'generic';
1506 }
1507 }
1508
ef88eaaa 1509 if (!$conf->{scsihw} || $conf->{scsihw} =~ m/^lsi/ || $conf->{scsihw} eq 'pvscsi') {
4df98f2f 1510 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit";
69bcf246 1511 } else {
4df98f2f
TL
1512 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,channel=0,scsi-id=0"
1513 .",lun=$drive->{index}";
69bcf246 1514 }
4df98f2f 1515 $device .= ",drive=drive-$drive_id,id=$drive_id";
cdd20088 1516
6c875f9f
NC
1517 if ($drive->{ssd} && ($devicetype eq 'block' || $devicetype eq 'hd')) {
1518 $device .= ",rotation_rate=1";
1519 }
e741c516 1520 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn};
6c875f9f
NC
1521
1522 } elsif ($drive->{interface} eq 'ide' || $drive->{interface} eq 'sata') {
e0fd2b2f 1523 my $maxdev = ($drive->{interface} eq 'sata') ? $PVE::QemuServer::Drive::MAX_SATA_DISKS : 2;
2ed36a41
DM
1524 my $controller = int($drive->{index} / $maxdev);
1525 my $unit = $drive->{index} % $maxdev;
1526 my $devicetype = ($drive->{media} && $drive->{media} eq 'cdrom') ? "cd" : "hd";
1527
6c875f9f
NC
1528 $device = "ide-$devicetype";
1529 if ($drive->{interface} eq 'ide') {
1530 $device .= ",bus=ide.$controller,unit=$unit";
1531 } else {
1532 $device .= ",bus=ahci$controller.$unit";
1533 }
4df98f2f 1534 $device .= ",drive=drive-$drive_id,id=$drive_id";
6c875f9f
NC
1535
1536 if ($devicetype eq 'hd') {
1537 if (my $model = $drive->{model}) {
1538 $model = URI::Escape::uri_unescape($model);
1539 $device .= ",model=$model";
1540 }
1541 if ($drive->{ssd}) {
1542 $device .= ",rotation_rate=1";
1543 }
0f2812c2 1544 }
e741c516 1545 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn};
2ed36a41
DM
1546 } elsif ($drive->{interface} eq 'usb') {
1547 die "implement me";
1548 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
1549 } else {
1550 die "unsupported interface type";
ca916ecc
DA
1551 }
1552
3b408e82
DM
1553 $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex};
1554
a70e7e6c
TL
1555 if (my $serial = $drive->{serial}) {
1556 $serial = URI::Escape::uri_unescape($serial);
1557 $device .= ",serial=$serial";
1558 }
1559
1560
ca916ecc
DA
1561 return $device;
1562}
1563
15b21acc 1564sub get_initiator_name {
46f58b5f 1565 my $initiator;
15b21acc 1566
d1c1af4b 1567 my $fh = IO::File->new('/etc/iscsi/initiatorname.iscsi') || return;
46f58b5f
DM
1568 while (defined(my $line = <$fh>)) {
1569 next if $line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/;
15b21acc
MR
1570 $initiator = $1;
1571 last;
1572 }
46f58b5f
DM
1573 $fh->close();
1574
15b21acc
MR
1575 return $initiator;
1576}
1577
776c5f50 1578sub print_drive_commandline_full {
6d5673c3 1579 my ($storecfg, $vmid, $drive, $pbs_name, $io_uring) = @_;
1e3baf05 1580
d81f0f09
DM
1581 my $path;
1582 my $volid = $drive->{file};
5921764c 1583 my $format = $drive->{format};
a183df68 1584 my $drive_id = get_drive_id($drive);
370b05e7 1585
0fe779a6
WB
1586 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
1587 my $scfg = $storeid ? PVE::Storage::storage_config($storecfg, $storeid) : undef;
1588
d81f0f09
DM
1589 if (drive_is_cdrom($drive)) {
1590 $path = get_iso_path($storecfg, $vmid, $volid);
a183df68 1591 die "$drive_id: cannot back cdrom drive with PBS snapshot\n" if $pbs_name;
d81f0f09 1592 } else {
d81f0f09
DM
1593 if ($storeid) {
1594 $path = PVE::Storage::path($storecfg, $volid);
5921764c 1595 $format //= qemu_img_format($scfg, $volname);
d81f0f09
DM
1596 } else {
1597 $path = $volid;
5921764c 1598 $format //= "raw";
d81f0f09
DM
1599 }
1600 }
1601
5921764c
SR
1602 my $is_rbd = $path =~ m/^rbd:/;
1603
1e3baf05 1604 my $opts = '';
5921764c 1605 my @qemu_drive_options = qw(heads secs cyls trans media cache rerror werror aio discard);
1e3baf05 1606 foreach my $o (@qemu_drive_options) {
5fc74861 1607 $opts .= ",$o=$drive->{$o}" if defined($drive->{$o});
19672434 1608 }
8a267708
DC
1609
1610 # snapshot only accepts on|off
1611 if (defined($drive->{snapshot})) {
1612 my $v = $drive->{snapshot} ? 'on' : 'off';
1613 $opts .= ",snapshot=$v";
1614 }
1615
1f91f7b4
TL
1616 if (defined($drive->{ro})) { # ro maps to QEMUs `readonly`, which accepts `on` or `off` only
1617 $opts .= ",readonly=" . ($drive->{ro} ? 'on' : 'off');
12e1d472
DC
1618 }
1619
fb8e95a2
WB
1620 foreach my $type (['', '-total'], [_rd => '-read'], [_wr => '-write']) {
1621 my ($dir, $qmpname) = @$type;
1622 if (my $v = $drive->{"mbps$dir"}) {
1623 $opts .= ",throttling.bps$qmpname=".int($v*1024*1024);
1624 }
1625 if (my $v = $drive->{"mbps${dir}_max"}) {
1626 $opts .= ",throttling.bps$qmpname-max=".int($v*1024*1024);
1627 }
1628 if (my $v = $drive->{"bps${dir}_max_length"}) {
1629 $opts .= ",throttling.bps$qmpname-max-length=$v";
1630 }
1631 if (my $v = $drive->{"iops${dir}"}) {
1632 $opts .= ",throttling.iops$qmpname=$v";
1633 }
1634 if (my $v = $drive->{"iops${dir}_max"}) {
8aca1654 1635 $opts .= ",throttling.iops$qmpname-max=$v";
fb8e95a2
WB
1636 }
1637 if (my $v = $drive->{"iops${dir}_max_length"}) {
8aca1654 1638 $opts .= ",throttling.iops$qmpname-max-length=$v";
fb8e95a2
WB
1639 }
1640 }
1641
5921764c
SR
1642 if ($pbs_name) {
1643 $format = "rbd" if $is_rbd;
a183df68
TL
1644 die "$drive_id: Proxmox Backup Server backed drive cannot auto-detect the format\n"
1645 if !$format;
5921764c
SR
1646 $opts .= ",format=alloc-track,file.driver=$format";
1647 } elsif ($format) {
1648 $opts .= ",format=$format";
1649 }
d81f0f09 1650
b2ee900e
WB
1651 my $cache_direct = 0;
1652
1653 if (my $cache = $drive->{cache}) {
1654 $cache_direct = $cache =~ /^(?:off|none|directsync)$/;
0fe779a6 1655 } elsif (!drive_is_cdrom($drive) && !($scfg && $scfg->{type} eq 'btrfs' && !$scfg->{nocow})) {
b2ee900e
WB
1656 $opts .= ",cache=none";
1657 $cache_direct = 1;
1658 }
1659
628937f5
SR
1660 # io_uring with cache mode writeback or writethrough on krbd will hang...
1661 my $rbd_no_io_uring = $scfg && $scfg->{type} eq 'rbd' && $scfg->{krbd} && !$cache_direct;
1662
78a3ada7
TL
1663 # io_uring with cache mode writeback or writethrough on LVM will hang, without cache only
1664 # sometimes, just plain disable...
1665 my $lvm_no_io_uring = $scfg && $scfg->{type} eq 'lvm';
ec5d198e 1666
b2ee900e 1667 if (!$drive->{aio}) {
ec5d198e 1668 if ($io_uring && !$rbd_no_io_uring && !$lvm_no_io_uring) {
6d5673c3
SR
1669 # io_uring supports all cache modes
1670 $opts .= ",aio=io_uring";
b2ee900e 1671 } else {
6d5673c3
SR
1672 # aio native works only with O_DIRECT
1673 if($cache_direct) {
1674 $opts .= ",aio=native";
1675 } else {
1676 $opts .= ",aio=threads";
1677 }
b2ee900e
WB
1678 }
1679 }
11490cf2 1680
6e47c3b4
WB
1681 if (!drive_is_cdrom($drive)) {
1682 my $detectzeroes;
7d4e30f3 1683 if (defined($drive->{detect_zeroes}) && !$drive->{detect_zeroes}) {
6e47c3b4
WB
1684 $detectzeroes = 'off';
1685 } elsif ($drive->{discard}) {
1686 $detectzeroes = $drive->{discard} eq 'on' ? 'unmap' : 'on';
1687 } else {
1688 # This used to be our default with discard not being specified:
1689 $detectzeroes = 'on';
1690 }
5921764c
SR
1691
1692 # note: 'detect-zeroes' works per blockdev and we want it to persist
1693 # after the alloc-track is removed, so put it on 'file' directly
1694 my $dz_param = $pbs_name ? "file.detect-zeroes" : "detect-zeroes";
1695 $opts .= ",$dz_param=$detectzeroes" if $detectzeroes;
6e47c3b4 1696 }
f1e05305 1697
5921764c
SR
1698 if ($pbs_name) {
1699 $opts .= ",backing=$pbs_name";
1700 $opts .= ",auto-remove=on";
1701 }
1702
1703 # my $file_param = $pbs_name ? "file.file.filename" : "file";
1704 my $file_param = "file";
1705 if ($pbs_name) {
1706 # non-rbd drivers require the underlying file to be a seperate block
1707 # node, so add a second .file indirection
1708 $file_param .= ".file" if !$is_rbd;
1709 $file_param .= ".filename";
1710 }
1711 my $pathinfo = $path ? "$file_param=$path," : '';
1e3baf05 1712
3ebfcc86 1713 return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
1e3baf05
DM
1714}
1715
5921764c
SR
1716sub print_pbs_blockdev {
1717 my ($pbs_conf, $pbs_name) = @_;
1718 my $blockdev = "driver=pbs,node-name=$pbs_name,read-only=on";
1719 $blockdev .= ",repository=$pbs_conf->{repository}";
1720 $blockdev .= ",snapshot=$pbs_conf->{snapshot}";
1721 $blockdev .= ",archive=$pbs_conf->{archive}";
1722 $blockdev .= ",keyfile=$pbs_conf->{keyfile}" if $pbs_conf->{keyfile};
1723 return $blockdev;
1724}
1725
cc4d6182 1726sub print_netdevice_full {
d559309f 1727 my ($vmid, $conf, $net, $netid, $bridges, $use_old_bios_files, $arch, $machine_type) = @_;
cc4d6182 1728
cc4d6182
DA
1729 my $device = $net->{model};
1730 if ($net->{model} eq 'virtio') {
1731 $device = 'virtio-net-pci';
1732 };
1733
d559309f 1734 my $pciaddr = print_pci_addr("$netid", $bridges, $arch, $machine_type);
5e2068d2 1735 my $tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
a9410357 1736 if ($net->{queues} && $net->{queues} > 1 && $net->{model} eq 'virtio'){
4df98f2f
TL
1737 # Consider we have N queues, the number of vectors needed is 2 * N + 2, i.e., one per in
1738 # and out of each queue plus one config interrupt and control vector queue
a9410357
AD
1739 my $vectors = $net->{queues} * 2 + 2;
1740 $tmpstr .= ",vectors=$vectors,mq=on";
1741 }
cc4d6182 1742 $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex} ;
ba9e1000 1743
0530177b
TL
1744 if (my $mtu = $net->{mtu}) {
1745 if ($net->{model} eq 'virtio' && $net->{bridge}) {
1746 my $bridge_mtu = PVE::Network::read_bridge_mtu($net->{bridge});
1747 if ($mtu == 1) {
1748 $mtu = $bridge_mtu;
1749 } elsif ($mtu < 576) {
1750 die "netdev $netid: MTU '$mtu' is smaller than the IP minimum MTU '576'\n";
1751 } elsif ($mtu > $bridge_mtu) {
1752 die "netdev $netid: MTU '$mtu' is bigger than the bridge MTU '$bridge_mtu'\n";
1753 }
1754 $tmpstr .= ",host_mtu=$mtu";
1755 } else {
1756 warn "WARN: netdev $netid: ignoring MTU '$mtu', not using VirtIO or no bridge configured.\n";
61a14cde 1757 }
61a14cde
AD
1758 }
1759
ba9e1000
DM
1760 if ($use_old_bios_files) {
1761 my $romfile;
1762 if ($device eq 'virtio-net-pci') {
1763 $romfile = 'pxe-virtio.rom';
1764 } elsif ($device eq 'e1000') {
1765 $romfile = 'pxe-e1000.rom';
e83dd50a
TL
1766 } elsif ($device eq 'e1000e') {
1767 $romfile = 'pxe-e1000e.rom';
ba9e1000
DM
1768 } elsif ($device eq 'ne2k') {
1769 $romfile = 'pxe-ne2k_pci.rom';
1770 } elsif ($device eq 'pcnet') {
1771 $romfile = 'pxe-pcnet.rom';
1772 } elsif ($device eq 'rtl8139') {
1773 $romfile = 'pxe-rtl8139.rom';
1774 }
1775 $tmpstr .= ",romfile=$romfile" if $romfile;
1776 }
1777
cc4d6182
DA
1778 return $tmpstr;
1779}
1780
1781sub print_netdev_full {
d559309f 1782 my ($vmid, $conf, $arch, $net, $netid, $hotplug) = @_;
cc4d6182
DA
1783
1784 my $i = '';
1785 if ($netid =~ m/^net(\d+)$/) {
1786 $i = int($1);
1787 }
1788
1789 die "got strange net id '$i'\n" if $i >= ${MAX_NETS};
1790
1791 my $ifname = "tap${vmid}i$i";
1792
1793 # kvm uses TUNSETIFF ioctl, and that limits ifname length
1794 die "interface name '$ifname' is too long (max 15 character)\n"
1795 if length($ifname) >= 16;
1796
1797 my $vhostparam = '';
6f0cb675 1798 if (is_native($arch)) {
db70021b 1799 $vhostparam = ',vhost=on' if kernel_has_vhost_net() && $net->{model} eq 'virtio';
6f0cb675 1800 }
cc4d6182
DA
1801
1802 my $vmname = $conf->{name} || "vm$vmid";
1803
a9410357 1804 my $netdev = "";
208ba94e 1805 my $script = $hotplug ? "pve-bridge-hotplug" : "pve-bridge";
a9410357 1806
cc4d6182 1807 if ($net->{bridge}) {
4df98f2f
TL
1808 $netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/$script"
1809 .",downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
cc4d6182 1810 } else {
a9410357 1811 $netdev = "type=user,id=$netid,hostname=$vmname";
cc4d6182 1812 }
a9410357
AD
1813
1814 $netdev .= ",queues=$net->{queues}" if ($net->{queues} && $net->{model} eq 'virtio');
1815
1816 return $netdev;
cc4d6182 1817}
1e3baf05 1818
55655ebc
DC
1819my $vga_map = {
1820 'cirrus' => 'cirrus-vga',
1821 'std' => 'VGA',
1822 'vmware' => 'vmware-svga',
1823 'virtio' => 'virtio-vga',
1824};
1825
1826sub print_vga_device {
2ea5fb7e 1827 my ($conf, $vga, $arch, $machine_version, $machine, $id, $qxlnum, $bridges) = @_;
55655ebc
DC
1828
1829 my $type = $vga_map->{$vga->{type}};
86c9fafe 1830 if ($arch eq 'aarch64' && defined($type) && $type eq 'virtio-vga') {
d559309f
WB
1831 $type = 'virtio-gpu';
1832 }
55655ebc 1833 my $vgamem_mb = $vga->{memory};
6021c7a5
AL
1834
1835 my $max_outputs = '';
55655ebc
DC
1836 if ($qxlnum) {
1837 $type = $id ? 'qxl' : 'qxl-vga';
6021c7a5 1838
c5a4c92c 1839 if (!$conf->{ostype} || $conf->{ostype} =~ m/^(?:l\d\d)|(?:other)$/) {
6021c7a5 1840 # set max outputs so linux can have up to 4 qxl displays with one device
2ea5fb7e 1841 if (min_version($machine_version, 4, 1)) {
9e8976ea
TL
1842 $max_outputs = ",max_outputs=4";
1843 }
6021c7a5 1844 }
55655ebc 1845 }
6021c7a5 1846
55655ebc
DC
1847 die "no devicetype for $vga->{type}\n" if !$type;
1848
1849 my $memory = "";
1850 if ($vgamem_mb) {
1851 if ($vga->{type} eq 'virtio') {
1852 my $bytes = PVE::Tools::convert_size($vgamem_mb, "mb" => "b");
1853 $memory = ",max_hostmem=$bytes";
1854 } elsif ($qxlnum) {
1855 # from https://www.spice-space.org/multiple-monitors.html
1856 $memory = ",vgamem_mb=$vga->{memory}";
1857 my $ram = $vgamem_mb * 4;
1858 my $vram = $vgamem_mb * 2;
1859 $memory .= ",ram_size_mb=$ram,vram_size_mb=$vram";
1860 } else {
1861 $memory = ",vgamem_mb=$vga->{memory}";
1862 }
1863 } elsif ($qxlnum && $id) {
1864 $memory = ",ram_size=67108864,vram_size=33554432";
1865 }
1866
789fe8e8
AL
1867 my $edidoff = "";
1868 if ($type eq 'VGA' && windows_version($conf->{ostype})) {
b5d32c6b 1869 $edidoff=",edid=off" if (!defined($conf->{bios}) || $conf->{bios} ne 'ovmf');
789fe8e8
AL
1870 }
1871
3392d6ca 1872 my $q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf);
55655ebc
DC
1873 my $vgaid = "vga" . ($id // '');
1874 my $pciaddr;
1875 if ($q35 && $vgaid eq 'vga') {
daadd5a4 1876 # the first display uses pcie.0 bus on q35 machines
d559309f 1877 $pciaddr = print_pcie_addr($vgaid, $bridges, $arch, $machine);
55655ebc 1878 } else {
d559309f 1879 $pciaddr = print_pci_addr($vgaid, $bridges, $arch, $machine);
55655ebc
DC
1880 }
1881
789fe8e8 1882 return "$type,id=${vgaid}${memory}${max_outputs}${pciaddr}${edidoff}";
55655ebc
DC
1883}
1884
ffc0d8c7
WB
1885sub parse_number_sets {
1886 my ($set) = @_;
1887 my $res = [];
1888 foreach my $part (split(/;/, $set)) {
1889 if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) {
1890 die "invalid range: $part ($2 < $1)\n" if defined($2) && $2 < $1;
1891 push @$res, [ $1, $2 ];
2ed5d572 1892 } else {
ffc0d8c7 1893 die "invalid range: $part\n";
2ed5d572
AD
1894 }
1895 }
ffc0d8c7
WB
1896 return $res;
1897}
2ed5d572 1898
ffc0d8c7
WB
1899sub parse_numa {
1900 my ($data) = @_;
1901
4df98f2f 1902 my $res = parse_property_string($numa_fmt, $data);
ffc0d8c7
WB
1903 $res->{cpus} = parse_number_sets($res->{cpus}) if defined($res->{cpus});
1904 $res->{hostnodes} = parse_number_sets($res->{hostnodes}) if defined($res->{hostnodes});
2ed5d572
AD
1905 return $res;
1906}
1907
1e3baf05
DM
1908# netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
1909sub parse_net {
1910 my ($data) = @_;
1911
4df98f2f 1912 my $res = eval { parse_property_string($net_fmt, $data) };
cd9c34d1
WB
1913 if ($@) {
1914 warn $@;
d1c1af4b 1915 return;
1e3baf05 1916 }
b5b99790
WB
1917 if (!defined($res->{macaddr})) {
1918 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
1919 $res->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
1920 }
0c9a7596
AD
1921 return $res;
1922}
1923
1924# ipconfigX ip=cidr,gw=ip,ip6=cidr,gw6=ip
1925sub parse_ipconfig {
1926 my ($data) = @_;
1927
4df98f2f 1928 my $res = eval { parse_property_string($ipconfig_fmt, $data) };
0c9a7596
AD
1929 if ($@) {
1930 warn $@;
d1c1af4b 1931 return;
0c9a7596
AD
1932 }
1933
1934 if ($res->{gw} && !$res->{ip}) {
1935 warn 'gateway specified without specifying an IP address';
d1c1af4b 1936 return;
0c9a7596
AD
1937 }
1938 if ($res->{gw6} && !$res->{ip6}) {
1939 warn 'IPv6 gateway specified without specifying an IPv6 address';
d1c1af4b 1940 return;
0c9a7596
AD
1941 }
1942 if ($res->{gw} && $res->{ip} eq 'dhcp') {
1943 warn 'gateway specified together with DHCP';
d1c1af4b 1944 return;
0c9a7596
AD
1945 }
1946 if ($res->{gw6} && $res->{ip6} !~ /^$IPV6RE/) {
1947 # gw6 + auto/dhcp
1948 warn "IPv6 gateway specified together with $res->{ip6} address";
d1c1af4b 1949 return;
0c9a7596
AD
1950 }
1951
1952 if (!$res->{ip} && !$res->{ip6}) {
1953 return { ip => 'dhcp', ip6 => 'dhcp' };
1954 }
1955
1e3baf05
DM
1956 return $res;
1957}
1958
1959sub print_net {
1960 my $net = shift;
1961
cd9c34d1 1962 return PVE::JSONSchema::print_property_string($net, $net_fmt);
1e3baf05
DM
1963}
1964
1965sub add_random_macs {
1966 my ($settings) = @_;
1967
1968 foreach my $opt (keys %$settings) {
1969 next if $opt !~ m/^net(\d+)$/;
1970 my $net = parse_net($settings->{$opt});
1971 next if !$net;
1972 $settings->{$opt} = print_net($net);
1973 }
1974}
1975
055d554d
DM
1976sub vm_is_volid_owner {
1977 my ($storecfg, $vmid, $volid) = @_;
1978
1979 if ($volid !~ m|^/|) {
1980 my ($path, $owner);
1981 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
1982 if ($owner && ($owner == $vmid)) {
1983 return 1;
1984 }
1985 }
1986
d1c1af4b 1987 return;
055d554d
DM
1988}
1989
055d554d
DM
1990sub vmconfig_register_unused_drive {
1991 my ($storecfg, $vmid, $conf, $drive) = @_;
1992
2d9ddec5
WB
1993 if (drive_is_cloudinit($drive)) {
1994 eval { PVE::Storage::vdisk_free($storecfg, $drive->{file}) };
1995 warn $@ if $@;
1996 } elsif (!drive_is_cdrom($drive)) {
055d554d
DM
1997 my $volid = $drive->{file};
1998 if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
8793d495 1999 PVE::QemuConfig->add_unused_volume($conf, $volid, $vmid);
055d554d
DM
2000 }
2001 }
2002}
2003
1f30ac3a 2004# smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str][,base64=bool]
ff6ffe20 2005my $smbios1_fmt = {
bd27e851
WB
2006 uuid => {
2007 type => 'string',
2008 pattern => '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
2009 format_description => 'UUID',
52261945 2010 description => "Set SMBIOS1 UUID.",
bd27e851
WB
2011 optional => 1,
2012 },
2013 version => {
2014 type => 'string',
1f30ac3a
CE
2015 pattern => '[A-Za-z0-9+\/]+={0,2}',
2016 format_description => 'Base64 encoded string',
52261945 2017 description => "Set SMBIOS1 version.",
bd27e851
WB
2018 optional => 1,
2019 },
2020 serial => {
2021 type => 'string',
1f30ac3a
CE
2022 pattern => '[A-Za-z0-9+\/]+={0,2}',
2023 format_description => 'Base64 encoded string',
52261945 2024 description => "Set SMBIOS1 serial number.",
bd27e851
WB
2025 optional => 1,
2026 },
2027 manufacturer => {
2028 type => 'string',
1f30ac3a
CE
2029 pattern => '[A-Za-z0-9+\/]+={0,2}',
2030 format_description => 'Base64 encoded string',
52261945 2031 description => "Set SMBIOS1 manufacturer.",
bd27e851
WB
2032 optional => 1,
2033 },
2034 product => {
2035 type => 'string',
1f30ac3a
CE
2036 pattern => '[A-Za-z0-9+\/]+={0,2}',
2037 format_description => 'Base64 encoded string',
52261945 2038 description => "Set SMBIOS1 product ID.",
bd27e851
WB
2039 optional => 1,
2040 },
2041 sku => {
2042 type => 'string',
1f30ac3a
CE
2043 pattern => '[A-Za-z0-9+\/]+={0,2}',
2044 format_description => 'Base64 encoded string',
52261945 2045 description => "Set SMBIOS1 SKU string.",
bd27e851
WB
2046 optional => 1,
2047 },
2048 family => {
2049 type => 'string',
1f30ac3a
CE
2050 pattern => '[A-Za-z0-9+\/]+={0,2}',
2051 format_description => 'Base64 encoded string',
52261945 2052 description => "Set SMBIOS1 family string.",
bd27e851
WB
2053 optional => 1,
2054 },
1f30ac3a
CE
2055 base64 => {
2056 type => 'boolean',
2057 description => 'Flag to indicate that the SMBIOS values are base64 encoded',
2058 optional => 1,
2059 },
2796e7d5
DM
2060};
2061
2796e7d5
DM
2062sub parse_smbios1 {
2063 my ($data) = @_;
2064
4df98f2f 2065 my $res = eval { parse_property_string($smbios1_fmt, $data) };
bd27e851 2066 warn $@ if $@;
2796e7d5
DM
2067 return $res;
2068}
2069
cd11416f
DM
2070sub print_smbios1 {
2071 my ($smbios1) = @_;
ff6ffe20 2072 return PVE::JSONSchema::print_property_string($smbios1, $smbios1_fmt);
cd11416f
DM
2073}
2074
ff6ffe20 2075PVE::JSONSchema::register_format('pve-qm-smbios1', $smbios1_fmt);
2796e7d5 2076
0ea9541d
DM
2077sub parse_watchdog {
2078 my ($value) = @_;
2079
d1c1af4b 2080 return if !$value;
0ea9541d 2081
4df98f2f 2082 my $res = eval { parse_property_string($watchdog_fmt, $value) };
ec3582b5 2083 warn $@ if $@;
0ea9541d
DM
2084 return $res;
2085}
2086
9d66b397 2087sub parse_guest_agent {
a2af1bbe 2088 my ($conf) = @_;
9d66b397 2089
a2af1bbe 2090 return {} if !defined($conf->{agent});
9d66b397 2091
a2af1bbe 2092 my $res = eval { parse_property_string($agent_fmt, $conf->{agent}) };
9d66b397
SI
2093 warn $@ if $@;
2094
2095 # if the agent is disabled ignore the other potentially set properties
2096 return {} if !$res->{enabled};
2097 return $res;
2098}
2099
a2af1bbe
TL
2100sub get_qga_key {
2101 my ($conf, $key) = @_;
2102 return undef if !defined($conf->{agent});
2103
2104 my $agent = parse_guest_agent($conf);
2105 return $agent->{$key};
2106}
2107
55655ebc
DC
2108sub parse_vga {
2109 my ($value) = @_;
2110
2111 return {} if !$value;
4df98f2f 2112 my $res = eval { parse_property_string($vga_fmt, $value) };
55655ebc
DC
2113 warn $@ if $@;
2114 return $res;
2115}
2116
2cf61f33
SR
2117sub parse_rng {
2118 my ($value) = @_;
2119
d1c1af4b 2120 return if !$value;
2cf61f33 2121
4df98f2f 2122 my $res = eval { parse_property_string($rng_fmt, $value) };
2cf61f33
SR
2123 warn $@ if $@;
2124 return $res;
2125}
2126
26b443c8
TL
2127sub parse_meta_info {
2128 my ($value) = @_;
2129
2130 return if !$value;
2131
2132 my $res = eval { parse_property_string($meta_info_fmt, $value) };
2133 warn $@ if $@;
2134 return $res;
2135}
2136
2137sub new_meta_info_string {
2138 my () = @_; # for now do not allow to override any value
2139
2140 return PVE::JSONSchema::print_property_string(
2141 {
af2a1a1c 2142 'creation-qemu' => kvm_user_version(),
26b443c8
TL
2143 ctime => "". int(time()),
2144 },
2145 $meta_info_fmt
2146 );
2147}
2148
cc181036
TL
2149sub qemu_created_version_fixups {
2150 my ($conf, $forcemachine, $kvmver) = @_;
2151
2152 my $meta = parse_meta_info($conf->{meta}) // {};
2153 my $forced_vers = PVE::QemuServer::Machine::extract_version($forcemachine);
2154
2155 # check if we need to apply some handling for VMs that always use the latest machine version but
2156 # had a machine version transition happen that affected HW such that, e.g., an OS config change
2157 # would be required (we do not want to pin machine version for non-windows OS type)
2158 if (
2159 (!defined($conf->{machine}) || $conf->{machine} =~ m/^(?:pc|q35|virt)$/) # non-versioned machine
2160 && (!defined($meta->{'creation-qemu'}) || !min_version($meta->{'creation-qemu'}, 6, 1)) # created before 6.1
2161 && (!$forced_vers || min_version($forced_vers, 6, 1)) # handle snapshot-rollback/migrations
2162 && min_version($kvmver, 6, 1) # only need to apply the change since 6.1
2163 ) {
2164 my $q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf);
2165 if ($q35 && $conf->{ostype} && $conf->{ostype} eq 'l26') {
2166 # this changed to default-on in Q 6.1 for q35 machines, it will mess with PCI slot view
2167 # and thus with the predictable interface naming of systemd
2168 return ['-global', 'ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off'];
2169 }
2170 }
2171 return;
2172}
2173
1e3baf05
DM
2174PVE::JSONSchema::register_format('pve-qm-usb-device', \&verify_usb_device);
2175sub verify_usb_device {
2176 my ($value, $noerr) = @_;
2177
2178 return $value if parse_usb_device($value);
2179
d1c1af4b 2180 return if $noerr;
19672434 2181
1e3baf05
DM
2182 die "unable to parse usb device\n";
2183}
2184
1e3baf05
DM
2185# add JSON properties for create and set function
2186sub json_config_properties {
c1accf9d 2187 my ($prop, $with_disk_alloc) = @_;
1e3baf05 2188
5a08fb9c
TL
2189 my $skip_json_config_opts = {
2190 parent => 1,
2191 snaptime => 1,
2192 vmstate => 1,
2193 runningmachine => 1,
2194 runningcpu => 1,
26b443c8 2195 meta => 1,
5a08fb9c
TL
2196 };
2197
1e3baf05 2198 foreach my $opt (keys %$confdesc) {
5a08fb9c 2199 next if $skip_json_config_opts->{$opt};
c1accf9d
FE
2200
2201 if ($with_disk_alloc && is_valid_drivename($opt)) {
2202 $prop->{$opt} = $PVE::QemuServer::Drive::drivedesc_hash_with_alloc->{$opt};
2203 } else {
2204 $prop->{$opt} = $confdesc->{$opt};
2205 }
1e3baf05
DM
2206 }
2207
2208 return $prop;
2209}
2210
39051ac0
FE
2211# Properties that we can read from an OVF file
2212sub json_ovf_properties {
2213 my $prop = {};
2214
2215 for my $device (PVE::QemuServer::Drive::valid_drive_names()) {
2216 $prop->{$device} = {
2217 type => 'string',
2218 format => 'pve-volume-id-or-absolute-path',
2219 description => "Disk image that gets imported to $device",
2220 optional => 1,
2221 };
2222 }
2223
2224 $prop->{cores} = {
2225 type => 'integer',
2226 description => "The number of CPU cores.",
2227 optional => 1,
2228 };
2229 $prop->{memory} = {
2230 type => 'integer',
2231 description => "Amount of RAM for the VM in MB.",
2232 optional => 1,
2233 };
2234 $prop->{name} = {
2235 type => 'string',
2236 description => "Name of the VM.",
2237 optional => 1,
2238 };
2239
2240 return $prop;
2241}
2242
d41121fd
DM
2243# return copy of $confdesc_cloudinit to generate documentation
2244sub cloudinit_config_properties {
2245
2246 return dclone($confdesc_cloudinit);
2247}
2248
1e3baf05
DM
2249sub check_type {
2250 my ($key, $value) = @_;
2251
2252 die "unknown setting '$key'\n" if !$confdesc->{$key};
2253
2254 my $type = $confdesc->{$key}->{type};
2255
6b64503e 2256 if (!defined($value)) {
1e3baf05
DM
2257 die "got undefined value\n";
2258 }
2259
2260 if ($value =~ m/[\n\r]/) {
2261 die "property contains a line feed\n";
2262 }
2263
2264 if ($type eq 'boolean') {
19672434
DM
2265 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
2266 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
2267 die "type check ('boolean') failed - got '$value'\n";
1e3baf05
DM
2268 } elsif ($type eq 'integer') {
2269 return int($1) if $value =~ m/^(\d+)$/;
2270 die "type check ('integer') failed - got '$value'\n";
04432191
AD
2271 } elsif ($type eq 'number') {
2272 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
2273 die "type check ('number') failed - got '$value'\n";
1e3baf05
DM
2274 } elsif ($type eq 'string') {
2275 if (my $fmt = $confdesc->{$key}->{format}) {
1e3baf05 2276 PVE::JSONSchema::check_format($fmt, $value);
19672434
DM
2277 return $value;
2278 }
1e3baf05 2279 $value =~ s/^\"(.*)\"$/$1/;
19672434 2280 return $value;
1e3baf05
DM
2281 } else {
2282 die "internal error"
2283 }
2284}
2285
1e3baf05 2286sub destroy_vm {
75854662 2287 my ($storecfg, $vmid, $skiplock, $replacement_conf, $purge_unreferenced) = @_;
1e3baf05 2288
ffda963f 2289 my $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 2290
ffda963f 2291 PVE::QemuConfig->check_lock($conf) if !$skiplock;
1e3baf05 2292
5e67a2d2
DC
2293 if ($conf->{template}) {
2294 # check if any base image is still used by a linked clone
3ab0f925 2295 PVE::QemuConfig->foreach_volume_full($conf, { include_unused => 1 }, sub {
5e67a2d2 2296 my ($ds, $drive) = @_;
5e67a2d2
DC
2297 return if drive_is_cdrom($drive);
2298
2299 my $volid = $drive->{file};
5e67a2d2
DC
2300 return if !$volid || $volid =~ m|^/|;
2301
2302 die "base volume '$volid' is still in use by linked cloned\n"
2303 if PVE::Storage::volume_is_base_and_used($storecfg, $volid);
2304
2305 });
2306 }
2307
3e07c6d5 2308 my $volids = {};
ba1a1984 2309 my $remove_owned_drive = sub {
1e3baf05 2310 my ($ds, $drive) = @_;
9c52f5ed 2311 return if drive_is_cdrom($drive, 1);
1e3baf05
DM
2312
2313 my $volid = $drive->{file};
ff1a2432 2314 return if !$volid || $volid =~ m|^/|;
3e07c6d5 2315 return if $volids->{$volid};
1e3baf05 2316
6b64503e 2317 my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
ff1a2432 2318 return if !$path || !$owner || ($owner != $vmid);
1e3baf05 2319
3e07c6d5 2320 $volids->{$volid} = 1;
a2f50f01 2321 eval { PVE::Storage::vdisk_free($storecfg, $volid) };
31b52247 2322 warn "Could not remove disk '$volid', check manually: $@" if $@;
ba1a1984
FE
2323 };
2324
2325 # only remove disks owned by this VM (referenced in the config)
2326 my $include_opts = {
2327 include_unused => 1,
2328 extra_keys => ['vmstate'],
2329 };
2330 PVE::QemuConfig->foreach_volume_full($conf, $include_opts, $remove_owned_drive);
2331
2332 for my $snap (values %{$conf->{snapshots}}) {
2333 next if !defined($snap->{vmstate});
2334 my $drive = PVE::QemuConfig->parse_volume('vmstate', $snap->{vmstate}, 1);
2335 next if !defined($drive);
2336 $remove_owned_drive->('vmstate', $drive);
2337 }
19672434 2338
3e07c6d5
FG
2339 PVE::QemuConfig->foreach_volume_full($conf->{pending}, $include_opts, $remove_owned_drive);
2340
75854662 2341 if ($purge_unreferenced) { # also remove unreferenced disk
d0ff75d9 2342 my $vmdisks = PVE::Storage::vdisk_list($storecfg, undef, $vmid, undef, 'images');
75854662
TL
2343 PVE::Storage::foreach_volid($vmdisks, sub {
2344 my ($volid, $sid, $volname, $d) = @_;
2345 eval { PVE::Storage::vdisk_free($storecfg, $volid) };
2346 warn $@ if $@;
2347 });
2348 }
dfda979e 2349
b04ea584 2350 if (defined $replacement_conf) {
8baf8bc7 2351 PVE::QemuConfig->write_config($vmid, $replacement_conf);
dfda979e
DJ
2352 } else {
2353 PVE::QemuConfig->destroy_config($vmid);
2354 }
1e3baf05
DM
2355}
2356
1e3baf05 2357sub parse_vm_config {
ad5812d8 2358 my ($filename, $raw, $strict) = @_;
1e3baf05 2359
d1c1af4b 2360 return if !defined($raw);
1e3baf05 2361
554ac7e7 2362 my $res = {
fc1ddcdc 2363 digest => Digest::SHA::sha1_hex($raw),
0d18dcfc 2364 snapshots => {},
0d732d16 2365 pending => {},
554ac7e7 2366 };
1e3baf05 2367
ad5812d8
FG
2368 my $handle_error = sub {
2369 my ($msg) = @_;
2370
2371 if ($strict) {
2372 die $msg;
2373 } else {
2374 warn $msg;
2375 }
2376 };
2377
19672434 2378 $filename =~ m|/qemu-server/(\d+)\.conf$|
1e3baf05
DM
2379 || die "got strange filename '$filename'";
2380
2381 my $vmid = $1;
2382
0d18dcfc 2383 my $conf = $res;
b0ec896e 2384 my $descr;
e297c490 2385 my $section = '';
0581fe4f 2386
0d18dcfc
DM
2387 my @lines = split(/\n/, $raw);
2388 foreach my $line (@lines) {
1e3baf05 2389 next if $line =~ m/^\s*$/;
be190583 2390
eab09f4e 2391 if ($line =~ m/^\[PENDING\]\s*$/i) {
e297c490 2392 $section = 'pending';
b0ec896e
DM
2393 if (defined($descr)) {
2394 $descr =~ s/\s+$//;
2395 $conf->{description} = $descr;
2396 }
2397 $descr = undef;
e297c490 2398 $conf = $res->{$section} = {};
eab09f4e
AD
2399 next;
2400
0d732d16 2401 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
e297c490 2402 $section = $1;
b0ec896e
DM
2403 if (defined($descr)) {
2404 $descr =~ s/\s+$//;
2405 $conf->{description} = $descr;
2406 }
2407 $descr = undef;
e297c490 2408 $conf = $res->{snapshots}->{$section} = {};
0d18dcfc
DM
2409 next;
2410 }
1e3baf05 2411
0581fe4f 2412 if ($line =~ m/^\#(.*)\s*$/) {
b0ec896e 2413 $descr = '' if !defined($descr);
0581fe4f
DM
2414 $descr .= PVE::Tools::decode_text($1) . "\n";
2415 next;
2416 }
2417
1e3baf05 2418 if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
b0ec896e 2419 $descr = '' if !defined($descr);
0581fe4f 2420 $descr .= PVE::Tools::decode_text($2);
0d18dcfc
DM
2421 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
2422 $conf->{snapstate} = $1;
1e3baf05
DM
2423 } elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
2424 my $key = $1;
2425 my $value = $2;
0d18dcfc 2426 $conf->{$key} = $value;
ef824322 2427 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
e297c490 2428 my $value = $1;
ef824322
DM
2429 if ($section eq 'pending') {
2430 $conf->{delete} = $value; # we parse this later
2431 } else {
ad5812d8 2432 $handle_error->("vm $vmid - property 'delete' is only allowed in [PENDING]\n");
eab09f4e 2433 }
15cf7698 2434 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
1e3baf05
DM
2435 my $key = $1;
2436 my $value = $2;
2437 eval { $value = check_type($key, $value); };
2438 if ($@) {
ad5812d8 2439 $handle_error->("vm $vmid - unable to parse value of '$key' - $@");
1e3baf05 2440 } else {
b799312f 2441 $key = 'ide2' if $key eq 'cdrom';
1e3baf05 2442 my $fmt = $confdesc->{$key}->{format};
b799312f 2443 if ($fmt && $fmt =~ /^pve-qm-(?:ide|scsi|virtio|sata)$/) {
1e3baf05
DM
2444 my $v = parse_drive($key, $value);
2445 if (my $volid = filename_to_volume_id($vmid, $v->{file}, $v->{media})) {
2446 $v->{file} = $volid;
71c58bb7 2447 $value = print_drive($v);
1e3baf05 2448 } else {
ad5812d8 2449 $handle_error->("vm $vmid - unable to parse value of '$key'\n");
1e3baf05
DM
2450 next;
2451 }
2452 }
2453
b799312f 2454 $conf->{$key} = $value;
1e3baf05 2455 }
f8d2a1ce 2456 } else {
ad5812d8 2457 $handle_error->("vm $vmid - unable to parse config: $line\n");
1e3baf05
DM
2458 }
2459 }
2460
b0ec896e
DM
2461 if (defined($descr)) {
2462 $descr =~ s/\s+$//;
2463 $conf->{description} = $descr;
2464 }
0d18dcfc 2465 delete $res->{snapstate}; # just to be sure
1e3baf05
DM
2466
2467 return $res;
2468}
2469
1858638f
DM
2470sub write_vm_config {
2471 my ($filename, $conf) = @_;
1e3baf05 2472
0d18dcfc
DM
2473 delete $conf->{snapstate}; # just to be sure
2474
1858638f
DM
2475 if ($conf->{cdrom}) {
2476 die "option ide2 conflicts with cdrom\n" if $conf->{ide2};
2477 $conf->{ide2} = $conf->{cdrom};
2478 delete $conf->{cdrom};
2479 }
1e3baf05
DM
2480
2481 # we do not use 'smp' any longer
1858638f
DM
2482 if ($conf->{sockets}) {
2483 delete $conf->{smp};
2484 } elsif ($conf->{smp}) {
2485 $conf->{sockets} = $conf->{smp};
2486 delete $conf->{cores};
2487 delete $conf->{smp};
1e3baf05
DM
2488 }
2489
ee2f90b1 2490 my $used_volids = {};
0d18dcfc 2491
ee2f90b1 2492 my $cleanup_config = sub {
ef824322 2493 my ($cref, $pending, $snapname) = @_;
1858638f 2494
ee2f90b1
DM
2495 foreach my $key (keys %$cref) {
2496 next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
ef824322 2497 $key eq 'snapstate' || $key eq 'pending';
ee2f90b1 2498 my $value = $cref->{$key};
ef824322
DM
2499 if ($key eq 'delete') {
2500 die "propertry 'delete' is only allowed in [PENDING]\n"
2501 if !$pending;
2502 # fixme: check syntax?
2503 next;
2504 }
ee2f90b1
DM
2505 eval { $value = check_type($key, $value); };
2506 die "unable to parse value of '$key' - $@" if $@;
1858638f 2507
ee2f90b1
DM
2508 $cref->{$key} = $value;
2509
74479ee9 2510 if (!$snapname && is_valid_drivename($key)) {
ed221350 2511 my $drive = parse_drive($key, $value);
ee2f90b1
DM
2512 $used_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
2513 }
1e3baf05 2514 }
ee2f90b1
DM
2515 };
2516
2517 &$cleanup_config($conf);
ef824322
DM
2518
2519 &$cleanup_config($conf->{pending}, 1);
2520
ee2f90b1 2521 foreach my $snapname (keys %{$conf->{snapshots}}) {
15c6e277 2522 die "internal error: snapshot name '$snapname' is forbidden" if lc($snapname) eq 'pending';
ef824322 2523 &$cleanup_config($conf->{snapshots}->{$snapname}, undef, $snapname);
1e3baf05
DM
2524 }
2525
1858638f
DM
2526 # remove 'unusedX' settings if we re-add a volume
2527 foreach my $key (keys %$conf) {
2528 my $value = $conf->{$key};
ee2f90b1 2529 if ($key =~ m/^unused/ && $used_volids->{$value}) {
1858638f 2530 delete $conf->{$key};
1e3baf05 2531 }
1858638f 2532 }
be190583 2533
0d18dcfc 2534 my $generate_raw_config = sub {
b0ec896e 2535 my ($conf, $pending) = @_;
0581fe4f 2536
0d18dcfc
DM
2537 my $raw = '';
2538
2539 # add description as comment to top of file
b0ec896e
DM
2540 if (defined(my $descr = $conf->{description})) {
2541 if ($descr) {
2542 foreach my $cl (split(/\n/, $descr)) {
2543 $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
2544 }
2545 } else {
2546 $raw .= "#\n" if $pending;
2547 }
0d18dcfc
DM
2548 }
2549
2550 foreach my $key (sort keys %$conf) {
4df98f2f 2551 next if $key =~ /^(digest|description|pending|snapshots)$/;
0d18dcfc
DM
2552 $raw .= "$key: $conf->{$key}\n";
2553 }
2554 return $raw;
2555 };
0581fe4f 2556
0d18dcfc 2557 my $raw = &$generate_raw_config($conf);
ef824322
DM
2558
2559 if (scalar(keys %{$conf->{pending}})){
2560 $raw .= "\n[PENDING]\n";
b0ec896e 2561 $raw .= &$generate_raw_config($conf->{pending}, 1);
ef824322
DM
2562 }
2563
0d18dcfc
DM
2564 foreach my $snapname (sort keys %{$conf->{snapshots}}) {
2565 $raw .= "\n[$snapname]\n";
2566 $raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
1858638f 2567 }
1e3baf05 2568
1858638f
DM
2569 return $raw;
2570}
1e3baf05 2571
19672434 2572sub load_defaults {
1e3baf05
DM
2573
2574 my $res = {};
2575
2576 # we use static defaults from our JSON schema configuration
2577 foreach my $key (keys %$confdesc) {
2578 if (defined(my $default = $confdesc->{$key}->{default})) {
2579 $res->{$key} = $default;
2580 }
2581 }
19672434 2582
1e3baf05
DM
2583 return $res;
2584}
2585
2586sub config_list {
2587 my $vmlist = PVE::Cluster::get_vmlist();
2588 my $res = {};
2589 return $res if !$vmlist || !$vmlist->{ids};
2590 my $ids = $vmlist->{ids};
38277afc 2591 my $nodename = nodename();
1e3baf05 2592
1e3baf05
DM
2593 foreach my $vmid (keys %$ids) {
2594 my $d = $ids->{$vmid};
2595 next if !$d->{node} || $d->{node} ne $nodename;
5ee957cc 2596 next if !$d->{type} || $d->{type} ne 'qemu';
1e3baf05
DM
2597 $res->{$vmid}->{exists} = 1;
2598 }
2599 return $res;
2600}
2601
64e13401
DM
2602# test if VM uses local resources (to prevent migration)
2603sub check_local_resources {
2604 my ($conf, $noerr) = @_;
2605
ca6abacf 2606 my @loc_res = ();
19672434 2607
ca6abacf
TM
2608 push @loc_res, "hostusb" if $conf->{hostusb}; # old syntax
2609 push @loc_res, "hostpci" if $conf->{hostpci}; # old syntax
64e13401 2610
ca6abacf 2611 push @loc_res, "ivshmem" if $conf->{ivshmem};
6dbcb073 2612
0d29ab3b 2613 foreach my $k (keys %$conf) {
a9ce7583 2614 next if $k =~ m/^usb/ && ($conf->{$k} =~ m/^spice(?![^,])/);
d44712fc
EK
2615 # sockets are safe: they will recreated be on the target side post-migrate
2616 next if $k =~ m/^serial/ && ($conf->{$k} eq 'socket');
ca6abacf 2617 push @loc_res, $k if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
64e13401
DM
2618 }
2619
ca6abacf 2620 die "VM uses local resources\n" if scalar @loc_res && !$noerr;
64e13401 2621
ca6abacf 2622 return \@loc_res;
64e13401
DM
2623}
2624
719893a9 2625# check if used storages are available on all nodes (use by migrate)
47152e2e
DM
2626sub check_storage_availability {
2627 my ($storecfg, $conf, $node) = @_;
2628
912792e2 2629 PVE::QemuConfig->foreach_volume($conf, sub {
47152e2e
DM
2630 my ($ds, $drive) = @_;
2631
2632 my $volid = $drive->{file};
2633 return if !$volid;
2634
2635 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
2636 return if !$sid;
2637
2638 # check if storage is available on both nodes
0d2db084
FE
2639 my $scfg = PVE::Storage::storage_check_enabled($storecfg, $sid);
2640 PVE::Storage::storage_check_enabled($storecfg, $sid, $node);
24b84b47 2641
3148f0b0
TL
2642 my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
2643
2644 die "$volid: content type '$vtype' is not available on storage '$sid'\n"
2645 if !$scfg->{content}->{$vtype};
47152e2e
DM
2646 });
2647}
2648
719893a9
DM
2649# list nodes where all VM images are available (used by has_feature API)
2650sub shared_nodes {
2651 my ($conf, $storecfg) = @_;
2652
2653 my $nodelist = PVE::Cluster::get_nodelist();
2654 my $nodehash = { map { $_ => 1 } @$nodelist };
38277afc 2655 my $nodename = nodename();
be190583 2656
912792e2 2657 PVE::QemuConfig->foreach_volume($conf, sub {
719893a9
DM
2658 my ($ds, $drive) = @_;
2659
2660 my $volid = $drive->{file};
2661 return if !$volid;
2662
2663 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
2664 if ($storeid) {
2665 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
2666 if ($scfg->{disable}) {
2667 $nodehash = {};
2668 } elsif (my $avail = $scfg->{nodes}) {
2669 foreach my $node (keys %$nodehash) {
2670 delete $nodehash->{$node} if !$avail->{$node};
2671 }
2672 } elsif (!$scfg->{shared}) {
2673 foreach my $node (keys %$nodehash) {
2674 delete $nodehash->{$node} if $node ne $nodename
2675 }
2676 }
2677 }
2678 });
2679
2680 return $nodehash
2681}
2682
f25852c2
TM
2683sub check_local_storage_availability {
2684 my ($conf, $storecfg) = @_;
2685
2686 my $nodelist = PVE::Cluster::get_nodelist();
2687 my $nodehash = { map { $_ => {} } @$nodelist };
2688
912792e2 2689 PVE::QemuConfig->foreach_volume($conf, sub {
f25852c2
TM
2690 my ($ds, $drive) = @_;
2691
2692 my $volid = $drive->{file};
2693 return if !$volid;
2694
2695 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
2696 if ($storeid) {
2697 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
2698
2699 if ($scfg->{disable}) {
2700 foreach my $node (keys %$nodehash) {
32075a2c 2701 $nodehash->{$node}->{unavailable_storages}->{$storeid} = 1;
f25852c2
TM
2702 }
2703 } elsif (my $avail = $scfg->{nodes}) {
2704 foreach my $node (keys %$nodehash) {
2705 if (!$avail->{$node}) {
32075a2c 2706 $nodehash->{$node}->{unavailable_storages}->{$storeid} = 1;
f25852c2
TM
2707 }
2708 }
2709 }
2710 }
2711 });
2712
32075a2c
TL
2713 foreach my $node (values %$nodehash) {
2714 if (my $unavail = $node->{unavailable_storages}) {
2715 $node->{unavailable_storages} = [ sort keys %$unavail ];
2716 }
2717 }
2718
f25852c2
TM
2719 return $nodehash
2720}
2721
babf613a 2722# Compat only, use assert_config_exists_on_node and vm_running_locally where possible
1e3baf05 2723sub check_running {
7e8dcf2c 2724 my ($vmid, $nocheck, $node) = @_;
1e3baf05 2725
babf613a
SR
2726 PVE::QemuConfig::assert_config_exists_on_node($vmid, $node) if !$nocheck;
2727 return PVE::QemuServer::Helpers::vm_running_locally($vmid);
1e3baf05
DM
2728}
2729
2730sub vzlist {
19672434 2731
1e3baf05
DM
2732 my $vzlist = config_list();
2733
d036e418 2734 my $fd = IO::Dir->new($PVE::QemuServer::Helpers::var_run_tmpdir) || return $vzlist;
1e3baf05 2735
19672434 2736 while (defined(my $de = $fd->read)) {
1e3baf05
DM
2737 next if $de !~ m/^(\d+)\.pid$/;
2738 my $vmid = $1;
6b64503e
DM
2739 next if !defined($vzlist->{$vmid});
2740 if (my $pid = check_running($vmid)) {
1e3baf05
DM
2741 $vzlist->{$vmid}->{pid} = $pid;
2742 }
2743 }
2744
2745 return $vzlist;
2746}
2747
b1a70cab
DM
2748our $vmstatus_return_properties = {
2749 vmid => get_standard_option('pve-vmid'),
2750 status => {
2751 description => "Qemu process status.",
2752 type => 'string',
2753 enum => ['stopped', 'running'],
2754 },
2755 maxmem => {
2756 description => "Maximum memory in bytes.",
2757 type => 'integer',
2758 optional => 1,
2759 renderer => 'bytes',
2760 },
2761 maxdisk => {
2762 description => "Root disk size in bytes.",
2763 type => 'integer',
2764 optional => 1,
2765 renderer => 'bytes',
2766 },
2767 name => {
2768 description => "VM name.",
2769 type => 'string',
2770 optional => 1,
2771 },
2772 qmpstatus => {
2773 description => "Qemu QMP agent status.",
2774 type => 'string',
2775 optional => 1,
2776 },
2777 pid => {
2778 description => "PID of running qemu process.",
2779 type => 'integer',
2780 optional => 1,
2781 },
2782 uptime => {
2783 description => "Uptime.",
2784 type => 'integer',
2785 optional => 1,
2786 renderer => 'duration',
2787 },
2788 cpus => {
2789 description => "Maximum usable CPUs.",
2790 type => 'number',
2791 optional => 1,
2792 },
e6ed61b4 2793 lock => {
11efdfa5 2794 description => "The current config lock, if any.",
e6ed61b4
DC
2795 type => 'string',
2796 optional => 1,
b8e7068a
DC
2797 },
2798 tags => {
2799 description => "The current configured tags, if any",
2800 type => 'string',
2801 optional => 1,
2802 },
949112c3
FE
2803 'running-machine' => {
2804 description => "The currently running machine type (if running).",
2805 type => 'string',
2806 optional => 1,
2807 },
2808 'running-qemu' => {
2809 description => "The currently running QEMU version (if running).",
2810 type => 'string',
2811 optional => 1,
2812 },
b1a70cab
DM
2813};
2814
1e3baf05
DM
2815my $last_proc_pid_stat;
2816
03a33f30
DM
2817# get VM status information
2818# This must be fast and should not block ($full == false)
2819# We only query KVM using QMP if $full == true (this can be slow)
1e3baf05 2820sub vmstatus {
03a33f30 2821 my ($opt_vmid, $full) = @_;
1e3baf05
DM
2822
2823 my $res = {};
2824
19672434 2825 my $storecfg = PVE::Storage::config();
1e3baf05
DM
2826
2827 my $list = vzlist();
3618ee99
EK
2828 my $defaults = load_defaults();
2829
694fcad4 2830 my ($uptime) = PVE::ProcFSTools::read_proc_uptime(1);
1e3baf05 2831
ae4915a2
DM
2832 my $cpucount = $cpuinfo->{cpus} || 1;
2833
1e3baf05
DM
2834 foreach my $vmid (keys %$list) {
2835 next if $opt_vmid && ($vmid ne $opt_vmid);
2836
9f78b695 2837 my $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 2838
ad2cad72 2839 my $d = { vmid => int($vmid) };
8a0addab 2840 $d->{pid} = int($list->{$vmid}->{pid}) if $list->{$vmid}->{pid};
1e3baf05
DM
2841
2842 # fixme: better status?
2843 $d->{status} = $list->{$vmid}->{pid} ? 'running' : 'stopped';
2844
776c5f50 2845 my $size = PVE::QemuServer::Drive::bootdisk_size($storecfg, $conf);
af990afe
DM
2846 if (defined($size)) {
2847 $d->{disk} = 0; # no info available
1e3baf05
DM
2848 $d->{maxdisk} = $size;
2849 } else {
2850 $d->{disk} = 0;
2851 $d->{maxdisk} = 0;
2852 }
2853
3618ee99
EK
2854 $d->{cpus} = ($conf->{sockets} || $defaults->{sockets})
2855 * ($conf->{cores} || $defaults->{cores});
ae4915a2 2856 $d->{cpus} = $cpucount if $d->{cpus} > $cpucount;
d7c8364b 2857 $d->{cpus} = $conf->{vcpus} if $conf->{vcpus};
ae4915a2 2858
1e3baf05 2859 $d->{name} = $conf->{name} || "VM $vmid";
3618ee99
EK
2860 $d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024)
2861 : $defaults->{memory}*(1024*1024);
1e3baf05 2862
8b1accf7 2863 if ($conf->{balloon}) {
4bdb0514 2864 $d->{balloon_min} = $conf->{balloon}*(1024*1024);
3618ee99
EK
2865 $d->{shares} = defined($conf->{shares}) ? $conf->{shares}
2866 : $defaults->{shares};
8b1accf7
DM
2867 }
2868
1e3baf05
DM
2869 $d->{uptime} = 0;
2870 $d->{cpu} = 0;
1e3baf05
DM
2871 $d->{mem} = 0;
2872
2873 $d->{netout} = 0;
2874 $d->{netin} = 0;
2875
2876 $d->{diskread} = 0;
2877 $d->{diskwrite} = 0;
2878
75a2a423 2879 $d->{template} = 1 if PVE::QemuConfig->is_template($conf);
4d8c851b 2880
8107b378 2881 $d->{serial} = 1 if conf_has_serial($conf);
e6ed61b4 2882 $d->{lock} = $conf->{lock} if $conf->{lock};
b8e7068a 2883 $d->{tags} = $conf->{tags} if defined($conf->{tags});
8107b378 2884
1e3baf05
DM
2885 $res->{$vmid} = $d;
2886 }
2887
2888 my $netdev = PVE::ProcFSTools::read_proc_net_dev();
2889 foreach my $dev (keys %$netdev) {
2890 next if $dev !~ m/^tap([1-9]\d*)i/;
2891 my $vmid = $1;
2892 my $d = $res->{$vmid};
2893 next if !$d;
19672434 2894
1e3baf05
DM
2895 $d->{netout} += $netdev->{$dev}->{receive};
2896 $d->{netin} += $netdev->{$dev}->{transmit};
604ea644
AD
2897
2898 if ($full) {
ad2cad72
FE
2899 $d->{nics}->{$dev}->{netout} = int($netdev->{$dev}->{receive});
2900 $d->{nics}->{$dev}->{netin} = int($netdev->{$dev}->{transmit});
604ea644
AD
2901 }
2902
1e3baf05
DM
2903 }
2904
1e3baf05
DM
2905 my $ctime = gettimeofday;
2906
2907 foreach my $vmid (keys %$list) {
2908
2909 my $d = $res->{$vmid};
2910 my $pid = $d->{pid};
2911 next if !$pid;
2912
694fcad4
DM
2913 my $pstat = PVE::ProcFSTools::read_proc_pid_stat($pid);
2914 next if !$pstat; # not running
19672434 2915
694fcad4 2916 my $used = $pstat->{utime} + $pstat->{stime};
1e3baf05 2917
694fcad4 2918 $d->{uptime} = int(($uptime - $pstat->{starttime})/$cpuinfo->{user_hz});
1e3baf05 2919
694fcad4 2920 if ($pstat->{vsize}) {
6b64503e 2921 $d->{mem} = int(($pstat->{rss}/$pstat->{vsize})*$d->{maxmem});
1e3baf05
DM
2922 }
2923
2924 my $old = $last_proc_pid_stat->{$pid};
2925 if (!$old) {
19672434
DM
2926 $last_proc_pid_stat->{$pid} = {
2927 time => $ctime,
1e3baf05
DM
2928 used => $used,
2929 cpu => 0,
1e3baf05
DM
2930 };
2931 next;
2932 }
2933
7f0b5beb 2934 my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz};
1e3baf05
DM
2935
2936 if ($dtime > 1000) {
2937 my $dutime = $used - $old->{used};
2938
ae4915a2 2939 $d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus};
1e3baf05 2940 $last_proc_pid_stat->{$pid} = {
19672434 2941 time => $ctime,
1e3baf05
DM
2942 used => $used,
2943 cpu => $d->{cpu},
1e3baf05
DM
2944 };
2945 } else {
2946 $d->{cpu} = $old->{cpu};
1e3baf05
DM
2947 }
2948 }
2949
f5eb281a 2950 return $res if !$full;
03a33f30
DM
2951
2952 my $qmpclient = PVE::QMPClient->new();
2953
64e7fcf2
DM
2954 my $ballooncb = sub {
2955 my ($vmid, $resp) = @_;
2956
2957 my $info = $resp->{'return'};
38babf81
DM
2958 return if !$info->{max_mem};
2959
64e7fcf2
DM
2960 my $d = $res->{$vmid};
2961
38babf81
DM
2962 # use memory assigned to VM
2963 $d->{maxmem} = $info->{max_mem};
2964 $d->{balloon} = $info->{actual};
2965
2966 if (defined($info->{total_mem}) && defined($info->{free_mem})) {
2967 $d->{mem} = $info->{total_mem} - $info->{free_mem};
2968 $d->{freemem} = $info->{free_mem};
64e7fcf2
DM
2969 }
2970
604ea644 2971 $d->{ballooninfo} = $info;
64e7fcf2
DM
2972 };
2973
03a33f30
DM
2974 my $blockstatscb = sub {
2975 my ($vmid, $resp) = @_;
2976 my $data = $resp->{'return'} || [];
2977 my $totalrdbytes = 0;
2978 my $totalwrbytes = 0;
604ea644 2979
03a33f30
DM
2980 for my $blockstat (@$data) {
2981 $totalrdbytes = $totalrdbytes + $blockstat->{stats}->{rd_bytes};
2982 $totalwrbytes = $totalwrbytes + $blockstat->{stats}->{wr_bytes};
604ea644
AD
2983
2984 $blockstat->{device} =~ s/drive-//;
2985 $res->{$vmid}->{blockstat}->{$blockstat->{device}} = $blockstat->{stats};
03a33f30
DM
2986 }
2987 $res->{$vmid}->{diskread} = $totalrdbytes;
2988 $res->{$vmid}->{diskwrite} = $totalwrbytes;
2989 };
2990
949112c3
FE
2991 my $machinecb = sub {
2992 my ($vmid, $resp) = @_;
2993 my $data = $resp->{'return'} || [];
2994
2995 $res->{$vmid}->{'running-machine'} =
2996 PVE::QemuServer::Machine::current_from_query_machines($data);
2997 };
2998
2999 my $versioncb = sub {
3000 my ($vmid, $resp) = @_;
3001 my $data = $resp->{'return'} // {};
3002 my $version = 'unknown';
3003
3004 if (my $v = $data->{qemu}) {
3005 $version = $v->{major} . "." . $v->{minor} . "." . $v->{micro};
3006 }
3007
3008 $res->{$vmid}->{'running-qemu'} = $version;
3009 };
3010
03a33f30
DM
3011 my $statuscb = sub {
3012 my ($vmid, $resp) = @_;
64e7fcf2 3013
03a33f30 3014 $qmpclient->queue_cmd($vmid, $blockstatscb, 'query-blockstats');
949112c3
FE
3015 $qmpclient->queue_cmd($vmid, $machinecb, 'query-machines');
3016 $qmpclient->queue_cmd($vmid, $versioncb, 'query-version');
64e7fcf2
DM
3017 # this fails if ballon driver is not loaded, so this must be
3018 # the last commnand (following command are aborted if this fails).
38babf81 3019 $qmpclient->queue_cmd($vmid, $ballooncb, 'query-balloon');
03a33f30
DM
3020
3021 my $status = 'unknown';
3022 if (!defined($status = $resp->{'return'}->{status})) {
3023 warn "unable to get VM status\n";
3024 return;
3025 }
3026
3027 $res->{$vmid}->{qmpstatus} = $resp->{'return'}->{status};
3028 };
3029
3030 foreach my $vmid (keys %$list) {
3031 next if $opt_vmid && ($vmid ne $opt_vmid);
3032 next if !$res->{$vmid}->{pid}; # not running
3033 $qmpclient->queue_cmd($vmid, $statuscb, 'query-status');
3034 }
3035
b017fbda 3036 $qmpclient->queue_execute(undef, 2);
03a33f30 3037
6891fd70
SR
3038 foreach my $vmid (keys %$list) {
3039 next if $opt_vmid && ($vmid ne $opt_vmid);
e5b18771
FG
3040 next if !$res->{$vmid}->{pid}; #not running
3041
6891fd70
SR
3042 # we can't use the $qmpclient since it might have already aborted on
3043 # 'query-balloon', but this might also fail for older versions...
3044 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support") };
3045 $res->{$vmid}->{'proxmox-support'} = $qemu_support // {};
3046 }
3047
03a33f30
DM
3048 foreach my $vmid (keys %$list) {
3049 next if $opt_vmid && ($vmid ne $opt_vmid);
3050 $res->{$vmid}->{qmpstatus} = $res->{$vmid}->{status} if !$res->{$vmid}->{qmpstatus};
3051 }
3052
1e3baf05
DM
3053 return $res;
3054}
3055
8107b378
DC
3056sub conf_has_serial {
3057 my ($conf) = @_;
3058
3059 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
3060 if ($conf->{"serial$i"}) {
3061 return 1;
3062 }
3063 }
3064
3065 return 0;
3066}
3067
d5535a00
TL
3068sub conf_has_audio {
3069 my ($conf, $id) = @_;
3070
3071 $id //= 0;
3072 my $audio = $conf->{"audio$id"};
d1c1af4b 3073 return if !defined($audio);
d5535a00 3074
4df98f2f 3075 my $audioproperties = parse_property_string($audio_fmt, $audio);
d5535a00
TL
3076 my $audiodriver = $audioproperties->{driver} // 'spice';
3077
3078 return {
3079 dev => $audioproperties->{device},
b0f96836 3080 dev_id => "audiodev$id",
d5535a00
TL
3081 backend => $audiodriver,
3082 backend_id => "$audiodriver-backend${id}",
3083 };
3084}
3085
b01de199 3086sub audio_devs {
1cc5ed1b 3087 my ($audio, $audiopciaddr, $machine_version) = @_;
b01de199
TL
3088
3089 my $devs = [];
3090
3091 my $id = $audio->{dev_id};
1cc5ed1b
AL
3092 my $audiodev = "";
3093 if (min_version($machine_version, 4, 2)) {
3094 $audiodev = ",audiodev=$audio->{backend_id}";
3095 }
b01de199
TL
3096
3097 if ($audio->{dev} eq 'AC97') {
1cc5ed1b 3098 push @$devs, '-device', "AC97,id=${id}${audiopciaddr}$audiodev";
b01de199
TL
3099 } elsif ($audio->{dev} =~ /intel\-hda$/) {
3100 push @$devs, '-device', "$audio->{dev},id=${id}${audiopciaddr}";
1cc5ed1b
AL
3101 push @$devs, '-device', "hda-micro,id=${id}-codec0,bus=${id}.0,cad=0$audiodev";
3102 push @$devs, '-device', "hda-duplex,id=${id}-codec1,bus=${id}.0,cad=1$audiodev";
b01de199
TL
3103 } else {
3104 die "unkown audio device '$audio->{dev}', implement me!";
3105 }
3106
3107 push @$devs, '-audiodev', "$audio->{backend},id=$audio->{backend_id}";
3108
3109 return $devs;
3110}
3111
f9dde219
SR
3112sub get_tpm_paths {
3113 my ($vmid) = @_;
3114 return {
3115 socket => "/var/run/qemu-server/$vmid.swtpm",
3116 pid => "/var/run/qemu-server/$vmid.swtpm.pid",
3117 };
3118}
3119
3120sub add_tpm_device {
3121 my ($vmid, $devices, $conf) = @_;
3122
3123 return if !$conf->{tpmstate0};
3124
3125 my $paths = get_tpm_paths($vmid);
3126
3127 push @$devices, "-chardev", "socket,id=tpmchar,path=$paths->{socket}";
3128 push @$devices, "-tpmdev", "emulator,id=tpmdev,chardev=tpmchar";
3129 push @$devices, "-device", "tpm-tis,tpmdev=tpmdev";
3130}
3131
3132sub start_swtpm {
3133 my ($storecfg, $vmid, $tpmdrive, $migration) = @_;
3134
3135 return if !$tpmdrive;
3136
3137 my $state;
3138 my $tpm = parse_drive("tpmstate0", $tpmdrive);
3139 my ($storeid, $volname) = PVE::Storage::parse_volume_id($tpm->{file}, 1);
3140 if ($storeid) {
3141 $state = PVE::Storage::map_volume($storecfg, $tpm->{file});
3142 } else {
3143 $state = $tpm->{file};
3144 }
3145
3146 my $paths = get_tpm_paths($vmid);
3147
3148 # during migration, we will get state from remote
3149 #
3150 if (!$migration) {
3151 # run swtpm_setup to create a new TPM state if it doesn't exist yet
3152 my $setup_cmd = [
3153 "swtpm_setup",
3154 "--tpmstate",
3155 "file://$state",
3156 "--createek",
3157 "--create-ek-cert",
3158 "--create-platform-cert",
3159 "--lock-nvram",
3160 "--config",
3161 "/etc/swtpm_setup.conf", # do not use XDG configs
3162 "--runas",
3163 "0", # force creation as root, error if not possible
3164 "--not-overwrite", # ignore existing state, do not modify
3165 ];
3166
3167 push @$setup_cmd, "--tpm2" if $tpm->{version} eq 'v2.0';
3168 # TPM 2.0 supports ECC crypto, use if possible
3169 push @$setup_cmd, "--ecc" if $tpm->{version} eq 'v2.0';
3170
3171 run_command($setup_cmd, outfunc => sub {
3172 print "swtpm_setup: $1\n";
3173 });
3174 }
3175
3176 my $emulator_cmd = [
3177 "swtpm",
3178 "socket",
3179 "--tpmstate",
3180 "backend-uri=file://$state,mode=0600",
3181 "--ctrl",
3182 "type=unixio,path=$paths->{socket},mode=0600",
3183 "--pid",
3184 "file=$paths->{pid}",
3185 "--terminate", # terminate on QEMU disconnect
3186 "--daemon",
3187 ];
3188 push @$emulator_cmd, "--tpm2" if $tpm->{version} eq 'v2.0';
3189 run_command($emulator_cmd, outfunc => sub { print $1; });
3190
6bbcd71f 3191 my $tries = 100; # swtpm may take a bit to start before daemonizing, wait up to 5s for pid
f85951dc 3192 while (! -e $paths->{pid}) {
90c41bac 3193 die "failed to start swtpm: pid file '$paths->{pid}' wasn't created.\n" if --$tries == 0;
6bbcd71f 3194 usleep(50_000);
f85951dc
SR
3195 }
3196
f9dde219
SR
3197 # return untainted PID of swtpm daemon so it can be killed on error
3198 file_read_firstline($paths->{pid}) =~ m/(\d+)/;
3199 return $1;
3200}
3201
86b8228b
DM
3202sub vga_conf_has_spice {
3203 my ($vga) = @_;
3204
55655ebc
DC
3205 my $vgaconf = parse_vga($vga);
3206 my $vgatype = $vgaconf->{type};
3207 return 0 if !$vgatype || $vgatype !~ m/^qxl([234])?$/;
590e698c
DM
3208
3209 return $1 || 1;
86b8228b
DM
3210}
3211
d731ecbe
WB
3212sub is_native($) {
3213 my ($arch) = @_;
3214 return get_host_arch() eq $arch;
3215}
3216
045749f2
TL
3217sub get_vm_arch {
3218 my ($conf) = @_;
3219 return $conf->{arch} // get_host_arch();
3220}
3221
d731ecbe
WB
3222my $default_machines = {
3223 x86_64 => 'pc',
3224 aarch64 => 'virt',
3225};
3226
0761e619
TL
3227sub get_installed_machine_version {
3228 my ($kvmversion) = @_;
3229 $kvmversion = kvm_user_version() if !defined($kvmversion);
3230 $kvmversion =~ m/^(\d+\.\d+)/;
3231 return $1;
3232}
3233
3234sub windows_get_pinned_machine_version {
3235 my ($machine, $base_version, $kvmversion) = @_;
3236
3237 my $pin_version = $base_version;
3238 if (!defined($base_version) ||
3239 !PVE::QemuServer::Machine::can_run_pve_machine_version($base_version, $kvmversion)
3240 ) {
3241 $pin_version = get_installed_machine_version($kvmversion);
3242 }
3243 if (!$machine || $machine eq 'pc') {
3244 $machine = "pc-i440fx-$pin_version";
3245 } elsif ($machine eq 'q35') {
3246 $machine = "pc-q35-$pin_version";
3247 } elsif ($machine eq 'virt') {
3248 $machine = "virt-$pin_version";
3249 } else {
3250 warn "unknown machine type '$machine', not touching that!\n";
3251 }
3252
3253 return $machine;
3254}
3255
045749f2 3256sub get_vm_machine {
ac0077cc 3257 my ($conf, $forcemachine, $arch, $add_pve_version, $kvmversion) = @_;
045749f2
TL
3258
3259 my $machine = $forcemachine || $conf->{machine};
d731ecbe 3260
9471e48b 3261 if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
4dd1e83c
TL
3262 $kvmversion //= kvm_user_version();
3263 # we must pin Windows VMs without a specific version to 5.1, as 5.2 fixed a bug in ACPI
3264 # layout which confuses windows quite a bit and may result in various regressions..
3265 # see: https://lists.gnu.org/archive/html/qemu-devel/2021-02/msg08484.html
3266 if (windows_version($conf->{ostype})) {
0761e619 3267 $machine = windows_get_pinned_machine_version($machine, '5.1', $kvmversion);
4dd1e83c 3268 }
045749f2
TL
3269 $arch //= 'x86_64';
3270 $machine ||= $default_machines->{$arch};
ac0077cc 3271 if ($add_pve_version) {
ac0077cc
SR
3272 my $pvever = PVE::QemuServer::Machine::get_pve_version($kvmversion);
3273 $machine .= "+pve$pvever";
3274 }
3275 }
3276
d4be7f31
SR
3277 if ($add_pve_version && $machine !~ m/\+pve\d+?(?:\.pxe)?$/) {
3278 my $is_pxe = $machine =~ m/^(.*?)\.pxe$/;
3279 $machine = $1 if $is_pxe;
3280
ac0077cc
SR
3281 # for version-pinned machines that do not include a pve-version (e.g.
3282 # pc-q35-4.1), we assume 0 to keep them stable in case we bump
3283 $machine .= '+pve0';
d4be7f31
SR
3284
3285 $machine .= '.pxe' if $is_pxe;
045749f2
TL
3286 }
3287
3288 return $machine;
d731ecbe
WB
3289}
3290
90b20b15
DC
3291sub get_ovmf_files($$$) {
3292 my ($arch, $efidisk, $smm) = @_;
96ed3574 3293
b5099b4f 3294 my $types = $OVMF->{$arch}
96ed3574
WB
3295 or die "no OVMF images known for architecture '$arch'\n";
3296
b5099b4f
SR
3297 my $type = 'default';
3298 if (defined($efidisk->{efitype}) && $efidisk->{efitype} eq '4m') {
90b20b15
DC
3299 $type = $smm ? "4m" : "4m-no-smm";
3300 $type .= '-ms' if $efidisk->{'pre-enrolled-keys'};
b5099b4f
SR
3301 }
3302
3303 return $types->{$type}->@*;
96ed3574
WB
3304}
3305
6908fd9b
WB
3306my $Arch2Qemu = {
3307 aarch64 => '/usr/bin/qemu-system-aarch64',
3308 x86_64 => '/usr/bin/qemu-system-x86_64',
3309};
3310sub get_command_for_arch($) {
3311 my ($arch) = @_;
3312 return '/usr/bin/kvm' if is_native($arch);
3313
3314 my $cmd = $Arch2Qemu->{$arch}
3315 or die "don't know how to emulate architecture '$arch'\n";
3316 return $cmd;
3317}
3318
05a4c550
SR
3319# To use query_supported_cpu_flags and query_understood_cpu_flags to get flags
3320# to use in a QEMU command line (-cpu element), first array_intersect the result
3321# of query_supported_ with query_understood_. This is necessary because:
3322#
3323# a) query_understood_ returns flags the host cannot use and
3324# b) query_supported_ (rather the QMP call) doesn't actually return CPU
3325# flags, but CPU settings - with most of them being flags. Those settings
3326# (and some flags, curiously) cannot be specified as a "-cpu" argument.
3327#
3328# query_supported_ needs to start up to 2 temporary VMs and is therefore rather
3329# expensive. If you need the value returned from this, you can get it much
3330# cheaper from pmxcfs using PVE::Cluster::get_node_kv('cpuflags-$accel') with
3331# $accel being 'kvm' or 'tcg'.
3332#
3333# pvestatd calls this function on startup and whenever the QEMU/KVM version
3334# changes, automatically populating pmxcfs.
3335#
3336# Returns: { kvm => [ flagX, flagY, ... ], tcg => [ flag1, flag2, ... ] }
3337# since kvm and tcg machines support different flags
3338#
3339sub query_supported_cpu_flags {
52cffab6 3340 my ($arch) = @_;
05a4c550 3341
52cffab6
SR
3342 $arch //= get_host_arch();
3343 my $default_machine = $default_machines->{$arch};
3344
3345 my $flags = {};
05a4c550
SR
3346
3347 # FIXME: Once this is merged, the code below should work for ARM as well:
3348 # https://lists.nongnu.org/archive/html/qemu-devel/2019-06/msg04947.html
3349 die "QEMU/KVM cannot detect CPU flags on ARM (aarch64)\n" if
3350 $arch eq "aarch64";
3351
3352 my $kvm_supported = defined(kvm_version());
3353 my $qemu_cmd = get_command_for_arch($arch);
3354 my $fakevmid = -1;
3355 my $pidfile = PVE::QemuServer::Helpers::pidfile_name($fakevmid);
3356
3357 # Start a temporary (frozen) VM with vmid -1 to allow sending a QMP command
3358 my $query_supported_run_qemu = sub {
3359 my ($kvm) = @_;
3360
3361 my $flags = {};
3362 my $cmd = [
3363 $qemu_cmd,
3364 '-machine', $default_machine,
3365 '-display', 'none',
378ad769 3366 '-chardev', "socket,id=qmp,path=/var/run/qemu-server/$fakevmid.qmp,server=on,wait=off",
05a4c550
SR
3367 '-mon', 'chardev=qmp,mode=control',
3368 '-pidfile', $pidfile,
3369 '-S', '-daemonize'
3370 ];
3371
3372 if (!$kvm) {
3373 push @$cmd, '-accel', 'tcg';
3374 }
3375
3376 my $rc = run_command($cmd, noerr => 1, quiet => 0);
3377 die "QEMU flag querying VM exited with code " . $rc if $rc;
3378
3379 eval {
3380 my $cmd_result = mon_cmd(
3381 $fakevmid,
3382 'query-cpu-model-expansion',
3383 type => 'full',
3384 model => { name => 'host' }
3385 );
3386
3387 my $props = $cmd_result->{model}->{props};
3388 foreach my $prop (keys %$props) {
3389 next if $props->{$prop} ne '1';
3390 # QEMU returns some flags multiple times, with '_', '.' or '-'
3391 # (e.g. lahf_lm and lahf-lm; sse4.2, sse4-2 and sse4_2; ...).
3392 # We only keep those with underscores, to match /proc/cpuinfo
3393 $prop =~ s/\.|-/_/g;
3394 $flags->{$prop} = 1;
3395 }
3396 };
3397 my $err = $@;
3398
6bbcd71f 3399 # force stop with 10 sec timeout and 'nocheck', always stop, even if QMP failed
05a4c550
SR
3400 vm_stop(undef, $fakevmid, 1, 1, 10, 0, 1);
3401
3402 die $err if $err;
3403
3404 return [ sort keys %$flags ];
3405 };
3406
3407 # We need to query QEMU twice, since KVM and TCG have different supported flags
3408 PVE::QemuConfig->lock_config($fakevmid, sub {
3409 $flags->{tcg} = eval { $query_supported_run_qemu->(0) };
3410 warn "warning: failed querying supported tcg flags: $@\n" if $@;
3411
3412 if ($kvm_supported) {
3413 $flags->{kvm} = eval { $query_supported_run_qemu->(1) };
3414 warn "warning: failed querying supported kvm flags: $@\n" if $@;
3415 }
3416 });
3417
3418 return $flags;
3419}
3420
3421# Understood CPU flags are written to a file at 'pve-qemu' compile time
3422my $understood_cpu_flag_dir = "/usr/share/kvm";
3423sub query_understood_cpu_flags {
3424 my $arch = get_host_arch();
3425 my $filepath = "$understood_cpu_flag_dir/recognized-CPUID-flags-$arch";
3426
3427 die "Cannot query understood QEMU CPU flags for architecture: $arch (file not found)\n"
3428 if ! -e $filepath;
3429
3430 my $raw = file_get_contents($filepath);
3431 $raw =~ s/^\s+|\s+$//g;
3432 my @flags = split(/\s+/, $raw);
3433
3434 return \@flags;
3435}
3436
d3f9db4d
TL
3437my sub get_cpuunits {
3438 my ($conf) = @_;
f43d7f67
TL
3439 my $is_cgroupv2 = PVE::CGroup::cgroup_mode() == 2;
3440
3441 my $cpuunits = $conf->{cpuunits};
3442 return $is_cgroupv2 ? 100 : 1024 if !defined($cpuunits);
3443
3444 if ($is_cgroupv2) {
3445 $cpuunits = 10000 if $cpuunits >= 10000; # v1 can be higher, so clamp v2 there
e65e35ca
TL
3446 } else {
3447 $cpuunits = 2 if $cpuunits < 2; # v2 can be lower, so clamp v1 there
f43d7f67
TL
3448 }
3449 return $cpuunits;
d3f9db4d 3450}
e5a6919c
FE
3451
3452# Since commit 277d33454f77ec1d1e0bc04e37621e4dd2424b67 in pve-qemu, smm is not off by default
3453# anymore. But smm=off seems to be required when using SeaBIOS and serial display.
3454my sub should_disable_smm {
3455 my ($conf, $vga) = @_;
3456
3457 return (!defined($conf->{bios}) || $conf->{bios} eq 'seabios') &&
bec87424 3458 $vga->{type} && $vga->{type} =~ m/^(serial\d+|none)$/;
e5a6919c
FE
3459}
3460
1e3baf05 3461sub config_to_command {
5921764c
SR
3462 my ($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu,
3463 $pbs_backing) = @_;
1e3baf05
DM
3464
3465 my $cmd = [];
3326ae19 3466 my ($globalFlags, $machineFlags, $rtcFlags) = ([], [], []);
5bdcf937 3467 my $devices = [];
5bdcf937 3468 my $bridges = {};
b42d3cf9 3469 my $ostype = $conf->{ostype};
4317f69f 3470 my $winversion = windows_version($ostype);
d731ecbe 3471 my $kvm = $conf->{kvm};
38277afc 3472 my $nodename = nodename();
d731ecbe 3473
045749f2 3474 my $arch = get_vm_arch($conf);
1476b99f
DC
3475 my $kvm_binary = get_command_for_arch($arch);
3476 my $kvmver = kvm_user_version($kvm_binary);
045749f2 3477
a04dd5c4
SR
3478 if (!$kvmver || $kvmver !~ m/^(\d+)\.(\d+)/ || $1 < 3) {
3479 $kvmver //= "undefined";
3480 die "Detected old QEMU binary ('$kvmver', at least 3.0 is required)\n";
3481 }
3482
9471e48b
TL
3483 my $add_pve_version = min_version($kvmver, 4, 1);
3484
3485 my $machine_type = get_vm_machine($conf, $forcemachine, $arch, $add_pve_version);
4df98f2f 3486 my $machine_version = extract_version($machine_type, $kvmver);
d731ecbe 3487 $kvm //= 1 if is_native($arch);
4317f69f 3488
a77a53ae 3489 $machine_version =~ m/(\d+)\.(\d+)/;
ac0077cc 3490 my ($machine_major, $machine_minor) = ($1, $2);
ac0077cc 3491
b516c848
SR
3492 if ($kvmver =~ m/^\d+\.\d+\.(\d+)/ && $1 >= 90) {
3493 warn "warning: Installed QEMU version ($kvmver) is a release candidate, ignoring version checks\n";
3494 } elsif (!min_version($kvmver, $machine_major, $machine_minor)) {
4df98f2f
TL
3495 die "Installed QEMU version '$kvmver' is too old to run machine type '$machine_type',"
3496 ." please upgrade node '$nodename'\n"
b516c848 3497 } elsif (!PVE::QemuServer::Machine::can_run_pve_machine_version($machine_version, $kvmver)) {
ac0077cc 3498 my $max_pve_version = PVE::QemuServer::Machine::get_pve_version($machine_version);
4df98f2f
TL
3499 die "Installed qemu-server (max feature level for $machine_major.$machine_minor is"
3500 ." pve$max_pve_version) is too old to run machine type '$machine_type', please upgrade"
3501 ." node '$nodename'\n";
ac0077cc
SR
3502 }
3503
3504 # if a specific +pve version is required for a feature, use $version_guard
3505 # instead of min_version to allow machines to be run with the minimum
3506 # required version
3507 my $required_pve_version = 0;
3508 my $version_guard = sub {
3509 my ($major, $minor, $pve) = @_;
3510 return 0 if !min_version($machine_version, $major, $minor, $pve);
47f35977
SR
3511 my $max_pve = PVE::QemuServer::Machine::get_pve_version("$major.$minor");
3512 return 1 if min_version($machine_version, $major, $minor, $max_pve+1);
ac0077cc
SR
3513 $required_pve_version = $pve if $pve && $pve > $required_pve_version;
3514 return 1;
3515 };
a77a53ae 3516
4df98f2f
TL
3517 if ($kvm && !defined kvm_version()) {
3518 die "KVM virtualisation configured, but not available. Either disable in VM configuration"
3519 ." or enable in BIOS.\n";
d731ecbe 3520 }
bfcd9b7e 3521
3392d6ca 3522 my $q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf);
4d3f29ed 3523 my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
249c4a6c
AD
3524 my $use_old_bios_files = undef;
3525 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files($machine_type);
db656e5f 3526
1476b99f 3527 push @$cmd, $kvm_binary;
1e3baf05
DM
3528
3529 push @$cmd, '-id', $vmid;
3530
e4d4cda1
HR
3531 my $vmname = $conf->{name} || "vm$vmid";
3532
3533 push @$cmd, '-name', $vmname;
3534
27b25d03
SR
3535 push @$cmd, '-no-shutdown';
3536
1e3baf05
DM
3537 my $use_virtio = 0;
3538
d036e418 3539 my $qmpsocket = PVE::QemuServer::Helpers::qmp_socket($vmid);
378ad769 3540 push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server=on,wait=off";
c971c4f2
AD
3541 push @$cmd, '-mon', "chardev=qmp,mode=control";
3542
2ea5fb7e 3543 if (min_version($machine_version, 2, 12)) {
b4496b9e 3544 push @$cmd, '-chardev', "socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect=5";
71bd73b5
DC
3545 push @$cmd, '-mon', "chardev=qmp-event,mode=control";
3546 }
1e3baf05 3547
d036e418 3548 push @$cmd, '-pidfile' , PVE::QemuServer::Helpers::pidfile_name($vmid);
19672434 3549
1e3baf05
DM
3550 push @$cmd, '-daemonize';
3551
2796e7d5 3552 if ($conf->{smbios1}) {
1f30ac3a
CE
3553 my $smbios_conf = parse_smbios1($conf->{smbios1});
3554 if ($smbios_conf->{base64}) {
3555 # Do not pass base64 flag to qemu
3556 delete $smbios_conf->{base64};
3557 my $smbios_string = "";
3558 foreach my $key (keys %$smbios_conf) {
3559 my $value;
3560 if ($key eq "uuid") {
3561 $value = $smbios_conf->{uuid}
3562 } else {
3563 $value = decode_base64($smbios_conf->{$key});
3564 }
3565 # qemu accepts any binary data, only commas need escaping by double comma
3566 $value =~ s/,/,,/g;
3567 $smbios_string .= "," . $key . "=" . $value if $value;
3568 }
3569 push @$cmd, '-smbios', "type=1" . $smbios_string;
3570 } else {
3571 push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
3572 }
2796e7d5
DM
3573 }
3574
3edb45e7 3575 if ($conf->{bios} && $conf->{bios} eq 'ovmf') {
b5099b4f
SR
3576 my $d;
3577 if (my $efidisk = $conf->{efidisk0}) {
3578 $d = parse_drive('efidisk0', $efidisk);
3579 }
3580
90b20b15 3581 my ($ovmf_code, $ovmf_vars) = get_ovmf_files($arch, $d, $q35);
818c3b8d 3582 die "uefi base image '$ovmf_code' not found\n" if ! -f $ovmf_code;
2ddc0a5c 3583
818c3b8d 3584 my ($path, $format);
b4dc6475 3585 my $read_only_str = '';
b5099b4f 3586 if ($d) {
2ddc0a5c 3587 my ($storeid, $volname) = PVE::Storage::parse_volume_id($d->{file}, 1);
13bca7b4 3588 $format = $d->{format};
2ddc0a5c
DC
3589 if ($storeid) {
3590 $path = PVE::Storage::path($storecfg, $d->{file});
13bca7b4
WB
3591 if (!defined($format)) {
3592 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
3593 $format = qemu_img_format($scfg, $volname);
3594 }
2ddc0a5c
DC
3595 } else {
3596 $path = $d->{file};
13bca7b4
WB
3597 die "efidisk format must be specified\n"
3598 if !defined($format);
2ddc0a5c 3599 }
b4dc6475
FG
3600
3601 $read_only_str = ',readonly=on' if drive_is_read_only($conf, $d);
2ddc0a5c 3602 } else {
4dcce9ee
TL
3603 warn "no efidisk configured! Using temporary efivars disk.\n";
3604 $path = "/tmp/$vmid-ovmf.fd";
96ed3574 3605 PVE::Tools::file_copy($ovmf_vars, $path, -s $ovmf_vars);
13bca7b4 3606 $format = 'raw';
2ddc0a5c 3607 }
4dcce9ee 3608
818ce80e
DC
3609 my $size_str = "";
3610
3611 if ($format eq 'raw' && $version_guard->(4, 1, 2)) {
3612 $size_str = ",size=" . (-s $ovmf_vars);
3613 }
3614
738dc81c
TL
3615 # SPI flash does lots of read-modify-write OPs, without writeback this gets really slow #3329
3616 my $cache = "";
3617 if ($path =~ m/^rbd:/) {
3618 $cache = ',cache=writeback';
3619 $path .= ':rbd_cache_policy=writeback'; # avoid write-around, we *need* to cache writes too
3620 }
6aaad230 3621
378ad769 3622 push @$cmd, '-drive', "if=pflash,unit=0,format=raw,readonly=on,file=$ovmf_code";
b4dc6475 3623 push @$cmd, '-drive', "if=pflash,unit=1$cache,format=$format,id=drive-efidisk0$size_str,file=${path}${read_only_str}";
a783c78e
AD
3624 }
3625
483ceeab 3626 if ($q35) { # tell QEMU to load q35 config early
7583d156 3627 # we use different pcie-port hardware for qemu >= 4.0 for passthrough
2ea5fb7e 3628 if (min_version($machine_version, 4, 0)) {
7583d156
DC
3629 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35-4.0.cfg';
3630 } else {
3631 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
3632 }
3633 }
da8b4189 3634
cc181036
TL
3635 if (defined(my $fixups = qemu_created_version_fixups($conf, $forcemachine, $kvmver))) {
3636 push @$cmd, $fixups->@*;
3637 }
3638
844d8fa6
DC
3639 if ($conf->{vmgenid}) {
3640 push @$devices, '-device', 'vmgenid,guid='.$conf->{vmgenid};
3641 }
3642
d40e5e18 3643 # add usb controllers
4df98f2f
TL
3644 my @usbcontrollers = PVE::QemuServer::USB::get_usb_controllers(
3645 $conf, $bridges, $arch, $machine_type, $usbdesc->{format}, $MAX_USB_DEVICES);
d40e5e18 3646 push @$devices, @usbcontrollers if @usbcontrollers;
55655ebc 3647 my $vga = parse_vga($conf->{vga});
2fa3151e 3648
55655ebc
DC
3649 my $qxlnum = vga_conf_has_spice($conf->{vga});
3650 $vga->{type} = 'qxl' if $qxlnum;
2fa3151e 3651
55655ebc 3652 if (!$vga->{type}) {
869ad4a7
WB
3653 if ($arch eq 'aarch64') {
3654 $vga->{type} = 'virtio';
2ea5fb7e 3655 } elsif (min_version($machine_version, 2, 9)) {
55655ebc 3656 $vga->{type} = (!$winversion || $winversion >= 6) ? 'std' : 'cirrus';
a2a5cd64 3657 } else {
55655ebc 3658 $vga->{type} = ($winversion >= 6) ? 'std' : 'cirrus';
a2a5cd64 3659 }
5acbfe9e
DM
3660 }
3661
1e3baf05 3662 # enable absolute mouse coordinates (needed by vnc)
fa3b3ce0
TL
3663 my $tablet = $conf->{tablet};
3664 if (!defined($tablet)) {
5acbfe9e 3665 $tablet = $defaults->{tablet};
590e698c 3666 $tablet = 0 if $qxlnum; # disable for spice because it is not needed
55655ebc 3667 $tablet = 0 if $vga->{type} =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
5acbfe9e
DM
3668 }
3669
d559309f
WB
3670 if ($tablet) {
3671 push @$devices, '-device', print_tabletdevice_full($conf, $arch) if $tablet;
3672 my $kbd = print_keyboarddevice_full($conf, $arch);
3673 push @$devices, '-device', $kbd if defined($kbd);
3674 }
b467f79a 3675
e5d611c3 3676 my $bootorder = device_bootorder($conf);
2141a802 3677
74c17b7a 3678 # host pci device passthrough
13d68979 3679 my ($kvm_off, $gpu_passthrough, $legacy_igd) = PVE::QemuServer::PCI::print_hostpci_devices(
41af2dfc 3680 $vmid, $conf, $devices, $vga, $winversion, $q35, $bridges, $arch, $machine_type, $bootorder);
1e3baf05
DM
3681
3682 # usb devices
ae36393d 3683 my $usb_dev_features = {};
2ea5fb7e 3684 $usb_dev_features->{spice_usb3} = 1 if min_version($machine_version, 4, 0);
ae36393d 3685
4df98f2f 3686 my @usbdevices = PVE::QemuServer::USB::get_usb_devices(
2141a802 3687 $conf, $usbdesc->{format}, $MAX_USB_DEVICES, $usb_dev_features, $bootorder);
d40e5e18 3688 push @$devices, @usbdevices if @usbdevices;
2141a802 3689
1e3baf05 3690 # serial devices
bae179aa 3691 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
fa3b3ce0
TL
3692 my $path = $conf->{"serial$i"} or next;
3693 if ($path eq 'socket') {
3694 my $socket = "/var/run/qemu-server/${vmid}.serial$i";
3695 push @$devices, '-chardev', "socket,id=serial$i,path=$socket,server=on,wait=off";
3696 # On aarch64, serial0 is the UART device. Qemu only allows
3697 # connecting UART devices via the '-serial' command line, as
3698 # the device has a fixed slot on the hardware...
3699 if ($arch eq 'aarch64' && $i == 0) {
3700 push @$devices, '-serial', "chardev:serial$i";
9f9d2fb2 3701 } else {
9f9d2fb2
DM
3702 push @$devices, '-device', "isa-serial,chardev=serial$i";
3703 }
fa3b3ce0
TL
3704 } else {
3705 die "no such serial device\n" if ! -c $path;
3706 push @$devices, '-chardev', "tty,id=serial$i,path=$path";
3707 push @$devices, '-device', "isa-serial,chardev=serial$i";
34978be3 3708 }
1e3baf05
DM
3709 }
3710
3711 # parallel devices
1989a89c 3712 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
34978be3 3713 if (my $path = $conf->{"parallel$i"}) {
19672434 3714 die "no such parallel device\n" if ! -c $path;
32e69805 3715 my $devtype = $path =~ m!^/dev/usb/lp! ? 'tty' : 'parport';
4c5dbaf6 3716 push @$devices, '-chardev', "$devtype,id=parallel$i,path=$path";
5bdcf937 3717 push @$devices, '-device', "isa-parallel,chardev=parallel$i";
34978be3 3718 }
1e3baf05
DM
3719 }
3720
b01de199 3721 if (min_version($machine_version, 4, 0) && (my $audio = conf_has_audio($conf))) {
2e7b5925 3722 my $audiopciaddr = print_pci_addr("audio0", $bridges, $arch, $machine_type);
1cc5ed1b 3723 my $audio_devs = audio_devs($audio, $audiopciaddr, $machine_version);
b01de199 3724 push @$devices, @$audio_devs;
2e7b5925 3725 }
19672434 3726
f9dde219
SR
3727 add_tpm_device($vmid, $devices, $conf);
3728
1e3baf05
DM
3729 my $sockets = 1;
3730 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
3731 $sockets = $conf->{sockets} if $conf->{sockets};
3732
3733 my $cores = $conf->{cores} || 1;
3bd18e48 3734
de9d1e55 3735 my $maxcpus = $sockets * $cores;
76267728 3736
de9d1e55 3737 my $vcpus = $conf->{vcpus} ? $conf->{vcpus} : $maxcpus;
76267728 3738
de9d1e55
AD
3739 my $allowed_vcpus = $cpuinfo->{cpus};
3740
483ceeab 3741 die "MAX $allowed_vcpus vcpus allowed per VM on this node\n" if ($allowed_vcpus < $maxcpus);
1e3baf05 3742
483ceeab 3743 if ($hotplug_features->{cpu} && min_version($machine_version, 2, 7)) {
69c81430
AD
3744 push @$cmd, '-smp', "1,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3745 for (my $i = 2; $i <= $vcpus; $i++) {
3746 my $cpustr = print_cpu_device($conf,$i);
3747 push @$cmd, '-device', $cpustr;
3748 }
3749
3750 } else {
3751
3752 push @$cmd, '-smp', "$vcpus,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3753 }
1e3baf05
DM
3754 push @$cmd, '-nodefaults';
3755
dbea4415 3756 push @$cmd, '-boot', "menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg";
1e3baf05 3757
6b64503e 3758 push @$cmd, '-no-acpi' if defined($conf->{acpi}) && $conf->{acpi} == 0;
1e3baf05 3759
6b64503e 3760 push @$cmd, '-no-reboot' if defined($conf->{reboot}) && $conf->{reboot} == 0;
1e3baf05 3761
84902837 3762 if ($vga->{type} && $vga->{type} !~ m/^serial\d+$/ && $vga->{type} ne 'none'){
4df98f2f
TL
3763 push @$devices, '-device', print_vga_device(
3764 $conf, $vga, $arch, $machine_version, $machine_type, undef, $qxlnum, $bridges);
d036e418 3765 my $socket = PVE::QemuServer::Helpers::vnc_socket($vmid);
378ad769 3766 push @$cmd, '-vnc', "unix:$socket,password=on";
b7be4ba9 3767 } else {
55655ebc 3768 push @$cmd, '-vga', 'none' if $vga->{type} eq 'none';
b7be4ba9
AD
3769 push @$cmd, '-nographic';
3770 }
3771
1e3baf05 3772 # time drift fix
6b64503e 3773 my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
8c559505 3774 my $useLocaltime = $conf->{localtime};
1e3baf05 3775
4317f69f
AD
3776 if ($winversion >= 5) { # windows
3777 $useLocaltime = 1 if !defined($conf->{localtime});
7a131888 3778
4317f69f
AD
3779 # use time drift fix when acpi is enabled
3780 if (!(defined($conf->{acpi}) && $conf->{acpi} == 0)) {
3781 $tdf = 1 if !defined($conf->{tdf});
462e8d19 3782 }
4317f69f 3783 }
462e8d19 3784
4317f69f
AD
3785 if ($winversion >= 6) {
3786 push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
3787 push @$cmd, '-no-hpet';
1e3baf05
DM
3788 }
3789
8c559505
DM
3790 push @$rtcFlags, 'driftfix=slew' if $tdf;
3791
2f6f002c 3792 if ($conf->{startdate} && $conf->{startdate} ne 'now') {
8c559505
DM
3793 push @$rtcFlags, "base=$conf->{startdate}";
3794 } elsif ($useLocaltime) {
3795 push @$rtcFlags, 'base=localtime';
3796 }
1e3baf05 3797
58c64ad5
SR
3798 if ($forcecpu) {
3799 push @$cmd, '-cpu', $forcecpu;
3800 } else {
2f6f002c 3801 push @$cmd, get_cpu_options($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough);
58c64ad5 3802 }
519ed28c 3803
0567a4d5 3804 PVE::QemuServer::Memory::config($conf, $vmid, $sockets, $cores, $defaults, $hotplug_features, $cmd);
370b05e7 3805
1e3baf05
DM
3806 push @$cmd, '-S' if $conf->{freeze};
3807
b20df606 3808 push @$cmd, '-k', $conf->{keyboard} if defined($conf->{keyboard});
1e3baf05 3809
48657158
MD
3810 my $guest_agent = parse_guest_agent($conf);
3811
3812 if ($guest_agent->{enabled}) {
d036e418 3813 my $qgasocket = PVE::QemuServer::Helpers::qmp_socket($vmid, 1);
378ad769 3814 push @$devices, '-chardev', "socket,path=$qgasocket,server=on,wait=off,id=qga0";
48657158 3815
60f03a11 3816 if (!$guest_agent->{type} || $guest_agent->{type} eq 'virtio') {
48657158
MD
3817 my $pciaddr = print_pci_addr("qga0", $bridges, $arch, $machine_type);
3818 push @$devices, '-device', "virtio-serial,id=qga0$pciaddr";
3819 push @$devices, '-device', 'virtserialport,chardev=qga0,name=org.qemu.guest_agent.0';
3820 } elsif ($guest_agent->{type} eq 'isa') {
3821 push @$devices, '-device', "isa-serial,chardev=qga0";
3822 }
ab6a046f
AD
3823 }
3824
e5d611c3
TL
3825 my $rng = $conf->{rng0} ? parse_rng($conf->{rng0}) : undef;
3826 if ($rng && $version_guard->(4, 1, 2)) {
05853188
SR
3827 check_rng_source($rng->{source});
3828
2cf61f33
SR
3829 my $max_bytes = $rng->{max_bytes} // $rng_fmt->{max_bytes}->{default};
3830 my $period = $rng->{period} // $rng_fmt->{period}->{default};
2cf61f33
SR
3831 my $limiter_str = "";
3832 if ($max_bytes) {
3833 $limiter_str = ",max-bytes=$max_bytes,period=$period";
3834 }
3835
2cf61f33 3836 my $rng_addr = print_pci_addr("rng0", $bridges, $arch, $machine_type);
2cf61f33
SR
3837 push @$devices, '-object', "rng-random,filename=$rng->{source},id=rng0";
3838 push @$devices, '-device', "virtio-rng-pci,rng=rng0$limiter_str$rng_addr";
3839 }
3840
1d794448 3841 my $spice_port;
2fa3151e 3842
590e698c
DM
3843 if ($qxlnum) {
3844 if ($qxlnum > 1) {
ac087616 3845 if ($winversion){
2f6f002c 3846 for (my $i = 1; $i < $qxlnum; $i++){
4df98f2f
TL
3847 push @$devices, '-device', print_vga_device(
3848 $conf, $vga, $arch, $machine_version, $machine_type, $i, $qxlnum, $bridges);
590e698c
DM
3849 }
3850 } else {
3851 # assume other OS works like Linux
55655ebc
DC
3852 my ($ram, $vram) = ("134217728", "67108864");
3853 if ($vga->{memory}) {
3854 $ram = PVE::Tools::convert_size($qxlnum*4*$vga->{memory}, 'mb' => 'b');
3855 $vram = PVE::Tools::convert_size($qxlnum*2*$vga->{memory}, 'mb' => 'b');
3856 }
3857 push @$cmd, '-global', "qxl-vga.ram_size=$ram";
3858 push @$cmd, '-global', "qxl-vga.vram_size=$vram";
2fa3151e
AD
3859 }
3860 }
3861
d559309f 3862 my $pciaddr = print_pci_addr("spice", $bridges, $arch, $machine_type);
95a4b4a9 3863
af0eba7e 3864 my $pfamily = PVE::Tools::get_host_address_family($nodename);
91152441
WB
3865 my @nodeaddrs = PVE::Tools::getaddrinfo_all('localhost', family => $pfamily);
3866 die "failed to get an ip address of type $pfamily for 'localhost'\n" if !@nodeaddrs;
4d316a63
AL
3867
3868 push @$devices, '-device', "virtio-serial,id=spice$pciaddr";
3869 push @$devices, '-chardev', "spicevmc,id=vdagent,name=vdagent";
3870 push @$devices, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
3871
91152441
WB
3872 my $localhost = PVE::Network::addr_to_ip($nodeaddrs[0]->{addr});
3873 $spice_port = PVE::Tools::next_spice_port($pfamily, $localhost);
943340a6 3874
4df98f2f
TL
3875 my $spice_enhancement_str = $conf->{spice_enhancements} // '';
3876 my $spice_enhancement = parse_property_string($spice_enhancements_fmt, $spice_enhancement_str);
caab114a
TL
3877 if ($spice_enhancement->{foldersharing}) {
3878 push @$devices, '-chardev', "spiceport,id=foldershare,name=org.spice-space.webdav.0";
3879 push @$devices, '-device', "virtserialport,chardev=foldershare,name=org.spice-space.webdav.0";
3880 }
c4df18db 3881
caab114a 3882 my $spice_opts = "tls-port=${spice_port},addr=$localhost,tls-ciphers=HIGH,seamless-migration=on";
4df98f2f
TL
3883 $spice_opts .= ",streaming-video=$spice_enhancement->{videostreaming}"
3884 if $spice_enhancement->{videostreaming};
3885
caab114a 3886 push @$devices, '-spice', "$spice_opts";
1011b570
DM
3887 }
3888
8d9ae0d2
DM
3889 # enable balloon by default, unless explicitly disabled
3890 if (!defined($conf->{balloon}) || $conf->{balloon}) {
3326ae19 3891 my $pciaddr = print_pci_addr("balloon0", $bridges, $arch, $machine_type);
8d9ae0d2
DM
3892 push @$devices, '-device', "virtio-balloon-pci,id=balloon0$pciaddr";
3893 }
1e3baf05 3894
0ea9541d
DM
3895 if ($conf->{watchdog}) {
3896 my $wdopts = parse_watchdog($conf->{watchdog});
3326ae19 3897 my $pciaddr = print_pci_addr("watchdog", $bridges, $arch, $machine_type);
0a40e8ea 3898 my $watchdog = $wdopts->{model} || 'i6300esb';
5bdcf937
AD
3899 push @$devices, '-device', "$watchdog$pciaddr";
3900 push @$devices, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
0ea9541d
DM
3901 }
3902
1e3baf05 3903 my $vollist = [];
941e0c42 3904 my $scsicontroller = {};
26ee04b6 3905 my $ahcicontroller = {};
cdd20088 3906 my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
1e3baf05 3907
5881b913
DM
3908 # Add iscsi initiator name if available
3909 if (my $initiator = get_initiator_name()) {
3910 push @$devices, '-iscsi', "initiator-name=$initiator";
3911 }
3912
912792e2 3913 PVE::QemuConfig->foreach_volume($conf, sub {
1e3baf05
DM
3914 my ($ds, $drive) = @_;
3915
ff1a2432 3916 if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
3f11f0d7 3917 check_volume_storage_type($storecfg, $drive->{file});
1e3baf05 3918 push @$vollist, $drive->{file};
ff1a2432 3919 }
afdb31d5 3920
4dcce9ee
TL
3921 # ignore efidisk here, already added in bios/fw handling code above
3922 return if $drive->{interface} eq 'efidisk';
f9dde219
SR
3923 # similar for TPM
3924 return if $drive->{interface} eq 'tpmstate';
4dcce9ee 3925
1e3baf05 3926 $use_virtio = 1 if $ds =~ m/^virtio/;
3b408e82 3927
2141a802 3928 $drive->{bootindex} = $bootorder->{$ds} if $bootorder->{$ds};
3b408e82 3929
2f6f002c 3930 if ($drive->{interface} eq 'virtio'){
51f492cd
AD
3931 push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread};
3932 }
3933
2f6f002c 3934 if ($drive->{interface} eq 'scsi') {
cdd20088 3935
ee034f5c 3936 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
6731a4cf 3937
b8fb1c03
SR
3938 die "scsi$drive->{index}: machine version 4.1~pve2 or higher is required to use more than 14 SCSI disks\n"
3939 if $drive->{index} > 13 && !&$version_guard(4, 1, 2);
3940
3326ae19 3941 my $pciaddr = print_pci_addr("$controller_prefix$controller", $bridges, $arch, $machine_type);
a1b7d579 3942 my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
fc8b40fd
AD
3943
3944 my $iothread = '';
3945 if($conf->{scsihw} && $conf->{scsihw} eq "virtio-scsi-single" && $drive->{iothread}){
3946 $iothread .= ",iothread=iothread-$controller_prefix$controller";
3947 push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
e7a5104d
DC
3948 } elsif ($drive->{iothread}) {
3949 warn "iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n";
fc8b40fd
AD
3950 }
3951
6e11f143
AD
3952 my $queues = '';
3953 if($conf->{scsihw} && $conf->{scsihw} eq "virtio-scsi-single" && $drive->{queues}){
3954 $queues = ",num_queues=$drive->{queues}";
370b05e7 3955 }
6e11f143 3956
4df98f2f
TL
3957 push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues"
3958 if !$scsicontroller->{$controller};
cdd20088 3959 $scsicontroller->{$controller}=1;
2f6f002c 3960 }
3b408e82 3961
26ee04b6 3962 if ($drive->{interface} eq 'sata') {
2f6f002c 3963 my $controller = int($drive->{index} / $PVE::QemuServer::Drive::MAX_SATA_DISKS);
3326ae19 3964 my $pciaddr = print_pci_addr("ahci$controller", $bridges, $arch, $machine_type);
4df98f2f
TL
3965 push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr"
3966 if !$ahcicontroller->{$controller};
2f6f002c 3967 $ahcicontroller->{$controller}=1;
26ee04b6 3968 }
46f58b5f 3969
5921764c
SR
3970 my $pbs_conf = $pbs_backing->{$ds};
3971 my $pbs_name = undef;
3972 if ($pbs_conf) {
3973 $pbs_name = "drive-$ds-pbs";
3974 push @$devices, '-blockdev', print_pbs_blockdev($pbs_conf, $pbs_name);
3975 }
3976
6d5673c3
SR
3977 my $drive_cmd = print_drive_commandline_full(
3978 $storecfg, $vmid, $drive, $pbs_name, min_version($kvmver, 6, 0));
3dc33a72
FG
3979
3980 # extra protection for templates, but SATA and IDE don't support it..
75748d44 3981 $drive_cmd .= ',readonly=on' if drive_is_read_only($conf, $drive);
4ef13a7f 3982
15b21acc 3983 push @$devices, '-drive',$drive_cmd;
4df98f2f
TL
3984 push @$devices, '-device', print_drivedevice_full(
3985 $storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type);
1e3baf05
DM
3986 });
3987
cc4d6182 3988 for (my $i = 0; $i < $MAX_NETS; $i++) {
2141a802
SR
3989 my $netname = "net$i";
3990
3991 next if !$conf->{$netname};
3992 my $d = parse_net($conf->{$netname});
d0a86b24 3993 next if !$d;
1e3baf05 3994
d0a86b24 3995 $use_virtio = 1 if $d->{model} eq 'virtio';
1e3baf05 3996
2141a802 3997 $d->{bootindex} = $bootorder->{$netname} if $bootorder->{$netname};
1e3baf05 3998
2141a802 3999 my $netdevfull = print_netdev_full($vmid, $conf, $arch, $d, $netname);
d0a86b24 4000 push @$devices, '-netdev', $netdevfull;
5bdcf937 4001
d0a86b24 4002 my $netdevicefull = print_netdevice_full(
2141a802 4003 $vmid, $conf, $d, $netname, $bridges, $use_old_bios_files, $arch, $machine_type);
4df98f2f 4004
d0a86b24 4005 push @$devices, '-device', $netdevicefull;
5bdcf937 4006 }
1e3baf05 4007
6dbcb073 4008 if ($conf->{ivshmem}) {
4df98f2f 4009 my $ivshmem = parse_property_string($ivshmem_fmt, $conf->{ivshmem});
e3c27a6a 4010
6dbcb073
DC
4011 my $bus;
4012 if ($q35) {
4013 $bus = print_pcie_addr("ivshmem");
4014 } else {
4015 $bus = print_pci_addr("ivshmem", $bridges, $arch, $machine_type);
4016 }
e3c27a6a
TL
4017
4018 my $ivshmem_name = $ivshmem->{name} // $vmid;
4019 my $path = '/dev/shm/pve-shm-' . $ivshmem_name;
4020
6dbcb073 4021 push @$devices, '-device', "ivshmem-plain,memdev=ivshmem$bus,";
4df98f2f
TL
4022 push @$devices, '-object', "memory-backend-file,id=ivshmem,share=on,mem-path=$path"
4023 .",size=$ivshmem->{size}M";
6dbcb073
DC
4024 }
4025
2513b862
DC
4026 # pci.4 is nested in pci.1
4027 $bridges->{1} = 1 if $bridges->{4};
4028
3326ae19
TL
4029 if (!$q35) { # add pci bridges
4030 if (min_version($machine_version, 2, 3)) {
fc79e813
AD
4031 $bridges->{1} = 1;
4032 $bridges->{2} = 1;
4033 }
6731a4cf 4034 $bridges->{3} = 1 if $scsihw =~ m/^virtio-scsi-single/;
2513b862
DC
4035 }
4036
4037 for my $k (sort {$b cmp $a} keys %$bridges) {
4038 next if $q35 && $k < 4; # q35.cfg already includes bridges up to 3
13d68979
SR
4039
4040 my $k_name = $k;
4041 if ($k == 2 && $legacy_igd) {
4042 $k_name = "$k-igd";
4043 }
3326ae19 4044 my $pciaddr = print_pci_addr("pci.$k_name", undef, $arch, $machine_type);
2513b862 4045 my $devstr = "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr";
3326ae19
TL
4046
4047 if ($q35) { # add after -readconfig pve-q35.cfg
2513b862
DC
4048 splice @$devices, 2, 0, '-device', $devstr;
4049 } else {
4050 unshift @$devices, '-device', $devstr if $k > 0;
f8e83f05 4051 }
19672434
DM
4052 }
4053
ac0077cc
SR
4054 if (!$kvm) {
4055 push @$machineFlags, 'accel=tcg';
4056 }
4057
e5a6919c
FE
4058 push @$machineFlags, 'smm=off' if should_disable_smm($conf, $vga);
4059
ac0077cc
SR
4060 my $machine_type_min = $machine_type;
4061 if ($add_pve_version) {
4062 $machine_type_min =~ s/\+pve\d+$//;
4063 $machine_type_min .= "+pve$required_pve_version";
4064 }
4065 push @$machineFlags, "type=${machine_type_min}";
4066
5bdcf937 4067 push @$cmd, @$devices;
2f6f002c
TL
4068 push @$cmd, '-rtc', join(',', @$rtcFlags) if scalar(@$rtcFlags);
4069 push @$cmd, '-machine', join(',', @$machineFlags) if scalar(@$machineFlags);
4070 push @$cmd, '-global', join(',', @$globalFlags) if scalar(@$globalFlags);
8c559505 4071
7ceade4c
DC
4072 if (my $vmstate = $conf->{vmstate}) {
4073 my $statepath = PVE::Storage::path($storecfg, $vmstate);
24d1f93a 4074 push @$vollist, $vmstate;
7ceade4c 4075 push @$cmd, '-loadstate', $statepath;
b85666cf 4076 print "activating and using '$vmstate' as vmstate\n";
7ceade4c
DC
4077 }
4078
85fcf79e
FG
4079 if (PVE::QemuConfig->is_template($conf)) {
4080 # needed to workaround base volumes being read-only
4081 push @$cmd, '-snapshot';
4082 }
4083
76350670
DC
4084 # add custom args
4085 if ($conf->{args}) {
4086 my $aa = PVE::Tools::split_args($conf->{args});
4087 push @$cmd, @$aa;
4088 }
4089
1d794448 4090 return wantarray ? ($cmd, $vollist, $spice_port) : $cmd;
1e3baf05 4091}
19672434 4092
05853188
SR
4093sub check_rng_source {
4094 my ($source) = @_;
4095
4096 # mostly relevant for /dev/hwrng, but doesn't hurt to check others too
4097 die "cannot create VirtIO RNG device: source file '$source' doesn't exist\n"
4098 if ! -e $source;
4099
4100 my $rng_current = '/sys/devices/virtual/misc/hw_random/rng_current';
4101 if ($source eq '/dev/hwrng' && file_read_firstline($rng_current) eq 'none') {
4df98f2f
TL
4102 # Needs to abort, otherwise QEMU crashes on first rng access. Note that rng_current cannot
4103 # be changed to 'none' manually, so once the VM is past this point, it's no longer an issue.
4104 die "Cannot start VM with passed-through RNG device: '/dev/hwrng' exists, but"
4105 ." '$rng_current' is set to 'none'. Ensure that a compatible hardware-RNG is attached"
4106 ." to the host.\n";
05853188
SR
4107 }
4108}
4109
943340a6 4110sub spice_port {
1011b570 4111 my ($vmid) = @_;
943340a6 4112
0a13e08e 4113 my $res = mon_cmd($vmid, 'query-spice');
943340a6
DM
4114
4115 return $res->{'tls-port'} || $res->{'port'} || die "no spice port\n";
1011b570
DM
4116}
4117
86fdcfb2
DA
4118sub vm_devices_list {
4119 my ($vmid) = @_;
4120
0a13e08e 4121 my $res = mon_cmd($vmid, 'query-pci');
f721624b 4122 my $devices_to_check = [];
ceea9078
DM
4123 my $devices = {};
4124 foreach my $pcibus (@$res) {
f721624b
DC
4125 push @$devices_to_check, @{$pcibus->{devices}},
4126 }
4127
4128 while (@$devices_to_check) {
4129 my $to_check = [];
4130 for my $d (@$devices_to_check) {
4131 $devices->{$d->{'qdev_id'}} = 1 if $d->{'qdev_id'};
4132 next if !$d->{'pci_bridge'};
4133
4134 $devices->{$d->{'qdev_id'}} += scalar(@{$d->{'pci_bridge'}->{devices}});
4135 push @$to_check, @{$d->{'pci_bridge'}->{devices}};
f78cc802 4136 }
f721624b 4137 $devices_to_check = $to_check;
f78cc802
AD
4138 }
4139
0a13e08e 4140 my $resblock = mon_cmd($vmid, 'query-block');
f78cc802
AD
4141 foreach my $block (@$resblock) {
4142 if($block->{device} =~ m/^drive-(\S+)/){
4143 $devices->{$1} = 1;
1dc4f496
DM
4144 }
4145 }
86fdcfb2 4146
0a13e08e 4147 my $resmice = mon_cmd($vmid, 'query-mice');
3d7389fe
DM
4148 foreach my $mice (@$resmice) {
4149 if ($mice->{name} eq 'QEMU HID Tablet') {
4150 $devices->{tablet} = 1;
4151 last;
4152 }
4153 }
4154
deb091c5
DC
4155 # for usb devices there is no query-usb
4156 # but we can iterate over the entries in
4157 # qom-list path=/machine/peripheral
0a13e08e 4158 my $resperipheral = mon_cmd($vmid, 'qom-list', path => '/machine/peripheral');
deb091c5
DC
4159 foreach my $per (@$resperipheral) {
4160 if ($per->{name} =~ m/^usb\d+$/) {
4161 $devices->{$per->{name}} = 1;
4162 }
4163 }
4164
1dc4f496 4165 return $devices;
86fdcfb2
DA
4166}
4167
ec21aa11 4168sub vm_deviceplug {
d559309f 4169 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
ae57f6b3 4170
3392d6ca 4171 my $q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf);
db656e5f 4172
95d6343b
DA
4173 my $devices_list = vm_devices_list($vmid);
4174 return 1 if defined($devices_list->{$deviceid});
4175
4df98f2f
TL
4176 # add PCI bridge if we need it for the device
4177 qemu_add_pci_bridge($storecfg, $conf, $vmid, $deviceid, $arch, $machine_type);
fee46675 4178
3d7389fe 4179 if ($deviceid eq 'tablet') {
d559309f 4180 qemu_deviceadd($vmid, print_tabletdevice_full($conf, $arch));
d559309f 4181 } elsif ($deviceid eq 'keyboard') {
d559309f 4182 qemu_deviceadd($vmid, print_keyboarddevice_full($conf, $arch));
4eb68604 4183 } elsif ($deviceid =~ m/^usb(\d+)$/) {
f745762b 4184 die "usb hotplug currently not reliable\n";
50bbe377
TL
4185 # since we can't reliably hot unplug all added usb devices and usb
4186 # passthrough breaks live migration we disable usb hotplugging for now
4187 #qemu_deviceadd($vmid, PVE::QemuServer::USB::print_usbdevice_full($conf, $deviceid, $device));
fee46675 4188 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
22de899a
AD
4189 qemu_iothread_add($vmid, $deviceid, $device);
4190
3326ae19
TL
4191 qemu_driveadd($storecfg, $vmid, $device);
4192 my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
fee46675 4193
3326ae19 4194 qemu_deviceadd($vmid, $devicefull);
fee46675
DM
4195 eval { qemu_deviceaddverify($vmid, $deviceid); };
4196 if (my $err = $@) {
63c2da2f
DM
4197 eval { qemu_drivedel($vmid, $deviceid); };
4198 warn $@ if $@;
fee46675 4199 die $err;
5e5dcb73 4200 }
2733141c 4201 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
3326ae19
TL
4202 my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
4203 my $pciaddr = print_pci_addr($deviceid, undef, $arch, $machine_type);
a1b7d579 4204 my $scsihw_type = $scsihw eq 'virtio-scsi-single' ? "virtio-scsi-pci" : $scsihw;
2733141c 4205
3326ae19 4206 my $devicefull = "$scsihw_type,id=$deviceid$pciaddr";
fee46675 4207
fc8b40fd
AD
4208 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{iothread}) {
4209 qemu_iothread_add($vmid, $deviceid, $device);
4210 $devicefull .= ",iothread=iothread-$deviceid";
4211 }
4212
6e11f143
AD
4213 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{queues}) {
4214 $devicefull .= ",num_queues=$device->{queues}";
4215 }
4216
3326ae19
TL
4217 qemu_deviceadd($vmid, $devicefull);
4218 qemu_deviceaddverify($vmid, $deviceid);
fee46675 4219 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
d559309f 4220 qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device, $arch, $machine_type);
fee46675 4221 qemu_driveadd($storecfg, $vmid, $device);
a1b7d579 4222
acfc6ef8 4223 my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
fee46675
DM
4224 eval { qemu_deviceadd($vmid, $devicefull); };
4225 if (my $err = $@) {
63c2da2f
DM
4226 eval { qemu_drivedel($vmid, $deviceid); };
4227 warn $@ if $@;
fee46675 4228 die $err;
a4f091a0 4229 }
fee46675 4230 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
d1c1af4b 4231 return if !qemu_netdevadd($vmid, $conf, $arch, $device, $deviceid);
8718099c 4232
3392d6ca 4233 my $machine_type = PVE::QemuServer::Machine::qemu_machine_pxe($vmid, $conf);
95d3be58
DC
4234 my $use_old_bios_files = undef;
4235 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files($machine_type);
8718099c 4236
4df98f2f
TL
4237 my $netdevicefull = print_netdevice_full(
4238 $vmid, $conf, $device, $deviceid, undef, $use_old_bios_files, $arch, $machine_type);
95d3be58 4239 qemu_deviceadd($vmid, $netdevicefull);
79046fd1
DC
4240 eval {
4241 qemu_deviceaddverify($vmid, $deviceid);
4242 qemu_set_link_status($vmid, $deviceid, !$device->{link_down});
4243 };
fee46675
DM
4244 if (my $err = $@) {
4245 eval { qemu_netdevdel($vmid, $deviceid); };
4246 warn $@ if $@;
4247 die $err;
95d3be58 4248 }
fee46675 4249 } elsif (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
40f28a9f 4250 my $bridgeid = $2;
d559309f 4251 my $pciaddr = print_pci_addr($deviceid, undef, $arch, $machine_type);
40f28a9f 4252 my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
a1b7d579 4253
40f28a9f 4254 qemu_deviceadd($vmid, $devicefull);
fee46675 4255 qemu_deviceaddverify($vmid, $deviceid);
fee46675 4256 } else {
a1b7d579 4257 die "can't hotplug device '$deviceid'\n";
40f28a9f
AD
4258 }
4259
5e5dcb73 4260 return 1;
a4dea331
DA
4261}
4262
3eec5767 4263# fixme: this should raise exceptions on error!
ec21aa11 4264sub vm_deviceunplug {
f19d1c47 4265 my ($vmid, $conf, $deviceid) = @_;
873c2d69 4266
95d6343b
DA
4267 my $devices_list = vm_devices_list($vmid);
4268 return 1 if !defined($devices_list->{$deviceid});
4269
2141a802
SR
4270 my $bootdisks = PVE::QemuServer::Drive::get_bootdisks($conf);
4271 die "can't unplug bootdisk '$deviceid'\n" if grep {$_ eq $deviceid} @$bootdisks;
63c2da2f 4272
d559309f 4273 if ($deviceid eq 'tablet' || $deviceid eq 'keyboard') {
3d7389fe 4274 qemu_devicedel($vmid, $deviceid);
4eb68604 4275 } elsif ($deviceid =~ m/^usb\d+$/) {
f745762b 4276 die "usb hotplug currently not reliable\n";
50bbe377
TL
4277 # when unplugging usb devices this way, there may be remaining usb
4278 # controllers/hubs so we disable it for now
4279 #qemu_devicedel($vmid, $deviceid);
4280 #qemu_devicedelverify($vmid, $deviceid);
63c2da2f 4281 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
9a66c311 4282 my $device = parse_drive($deviceid, $conf->{$deviceid});
f19d1c47 4283
a8d0fec3
TL
4284 qemu_devicedel($vmid, $deviceid);
4285 qemu_devicedelverify($vmid, $deviceid);
4286 qemu_drivedel($vmid, $deviceid);
9a66c311 4287 qemu_iothread_del($vmid, $deviceid, $device);
2733141c 4288 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
63c2da2f 4289 qemu_devicedel($vmid, $deviceid);
8ce30dde 4290 qemu_devicedelverify($vmid, $deviceid);
63c2da2f 4291 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
9a66c311 4292 my $device = parse_drive($deviceid, $conf->{$deviceid});
cfc817c7 4293
a8d0fec3 4294 qemu_devicedel($vmid, $deviceid);
52b361af 4295 qemu_devicedelverify($vmid, $deviceid);
a8d0fec3 4296 qemu_drivedel($vmid, $deviceid);
a1b7d579 4297 qemu_deletescsihw($conf, $vmid, $deviceid);
8ce30dde 4298
9a66c311
FG
4299 qemu_iothread_del($vmid, "virtioscsi$device->{index}", $device)
4300 if $conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single');
63c2da2f 4301 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
a8d0fec3
TL
4302 qemu_devicedel($vmid, $deviceid);
4303 qemu_devicedelverify($vmid, $deviceid);
4304 qemu_netdevdel($vmid, $deviceid);
63c2da2f
DM
4305 } else {
4306 die "can't unplug device '$deviceid'\n";
2630d2a9
DA
4307 }
4308
5e5dcb73
DA
4309 return 1;
4310}
4311
4312sub qemu_deviceadd {
4313 my ($vmid, $devicefull) = @_;
873c2d69 4314
d695b5b7
AD
4315 $devicefull = "driver=".$devicefull;
4316 my %options = split(/[=,]/, $devicefull);
f19d1c47 4317
0a13e08e 4318 mon_cmd($vmid, "device_add" , %options);
5e5dcb73 4319}
afdb31d5 4320
5e5dcb73 4321sub qemu_devicedel {
fee46675 4322 my ($vmid, $deviceid) = @_;
63c2da2f 4323
0a13e08e 4324 my $ret = mon_cmd($vmid, "device_del", id => $deviceid);
5e5dcb73
DA
4325}
4326
22de899a 4327sub qemu_iothread_add {
a8d0fec3 4328 my ($vmid, $deviceid, $device) = @_;
22de899a
AD
4329
4330 if ($device->{iothread}) {
4331 my $iothreads = vm_iothreads_list($vmid);
4332 qemu_objectadd($vmid, "iothread-$deviceid", "iothread") if !$iothreads->{"iothread-$deviceid"};
4333 }
4334}
4335
4336sub qemu_iothread_del {
a8d0fec3 4337 my ($vmid, $deviceid, $device) = @_;
22de899a 4338
22de899a
AD
4339 if ($device->{iothread}) {
4340 my $iothreads = vm_iothreads_list($vmid);
4341 qemu_objectdel($vmid, "iothread-$deviceid") if $iothreads->{"iothread-$deviceid"};
4342 }
4343}
4344
4d3f29ed 4345sub qemu_objectadd {
a8d0fec3 4346 my ($vmid, $objectid, $qomtype) = @_;
4d3f29ed 4347
0a13e08e 4348 mon_cmd($vmid, "object-add", id => $objectid, "qom-type" => $qomtype);
4d3f29ed
AD
4349
4350 return 1;
4351}
4352
4353sub qemu_objectdel {
a8d0fec3 4354 my ($vmid, $objectid) = @_;
4d3f29ed 4355
0a13e08e 4356 mon_cmd($vmid, "object-del", id => $objectid);
4d3f29ed
AD
4357
4358 return 1;
4359}
4360
5e5dcb73 4361sub qemu_driveadd {
fee46675 4362 my ($storecfg, $vmid, $device) = @_;
5e5dcb73 4363
6d5673c3
SR
4364 my $kvmver = get_running_qemu_version($vmid);
4365 my $io_uring = min_version($kvmver, 6, 0);
4366 my $drive = print_drive_commandline_full($storecfg, $vmid, $device, undef, $io_uring);
7a69fc3c 4367 $drive =~ s/\\/\\\\/g;
0a13e08e 4368 my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"");
fee46675 4369
5e5dcb73 4370 # If the command succeeds qemu prints: "OK"
fee46675
DM
4371 return 1 if $ret =~ m/OK/s;
4372
4373 die "adding drive failed: $ret\n";
5e5dcb73 4374}
afdb31d5 4375
5e5dcb73 4376sub qemu_drivedel {
a8d0fec3 4377 my ($vmid, $deviceid) = @_;
873c2d69 4378
0a13e08e 4379 my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-$deviceid");
5e5dcb73 4380 $ret =~ s/^\s+//;
a1b7d579 4381
63c2da2f 4382 return 1 if $ret eq "";
a1b7d579 4383
63c2da2f 4384 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
a1b7d579
DM
4385 return 1 if $ret =~ m/Device \'.*?\' not found/s;
4386
63c2da2f 4387 die "deleting drive $deviceid failed : $ret\n";
5e5dcb73 4388}
f19d1c47 4389
5e5dcb73 4390sub qemu_deviceaddverify {
fee46675 4391 my ($vmid, $deviceid) = @_;
873c2d69 4392
5e5dcb73
DA
4393 for (my $i = 0; $i <= 5; $i++) {
4394 my $devices_list = vm_devices_list($vmid);
4395 return 1 if defined($devices_list->{$deviceid});
4396 sleep 1;
afdb31d5 4397 }
fee46675
DM
4398
4399 die "error on hotplug device '$deviceid'\n";
5e5dcb73 4400}
afdb31d5 4401
5e5dcb73
DA
4402
4403sub qemu_devicedelverify {
63c2da2f
DM
4404 my ($vmid, $deviceid) = @_;
4405
a1b7d579 4406 # need to verify that the device is correctly removed as device_del
63c2da2f 4407 # is async and empty return is not reliable
5e5dcb73 4408
5e5dcb73
DA
4409 for (my $i = 0; $i <= 5; $i++) {
4410 my $devices_list = vm_devices_list($vmid);
4411 return 1 if !defined($devices_list->{$deviceid});
4412 sleep 1;
afdb31d5 4413 }
63c2da2f
DM
4414
4415 die "error on hot-unplugging device '$deviceid'\n";
873c2d69
DA
4416}
4417
cdd20088 4418sub qemu_findorcreatescsihw {
d559309f 4419 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
cfc817c7 4420
ee034f5c 4421 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
2733141c
AD
4422
4423 my $scsihwid="$controller_prefix$controller";
cfc817c7
DA
4424 my $devices_list = vm_devices_list($vmid);
4425
a8d0fec3 4426 if (!defined($devices_list->{$scsihwid})) {
d559309f 4427 vm_deviceplug($storecfg, $conf, $vmid, $scsihwid, $device, $arch, $machine_type);
cfc817c7 4428 }
fee46675 4429
cfc817c7
DA
4430 return 1;
4431}
4432
8ce30dde
AD
4433sub qemu_deletescsihw {
4434 my ($conf, $vmid, $opt) = @_;
4435
4436 my $device = parse_drive($opt, $conf->{$opt});
4437
a1511b3c 4438 if ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
2733141c
AD
4439 vm_deviceunplug($vmid, $conf, "virtioscsi$device->{index}");
4440 return 1;
4441 }
4442
ee034f5c 4443 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
8ce30dde
AD
4444
4445 my $devices_list = vm_devices_list($vmid);
4446 foreach my $opt (keys %{$devices_list}) {
e0fd2b2f
FE
4447 if (is_valid_drivename($opt)) {
4448 my $drive = parse_drive($opt, $conf->{$opt});
a8d0fec3 4449 if ($drive->{interface} eq 'scsi' && $drive->{index} < (($maxdev-1)*($controller+1))) {
8ce30dde
AD
4450 return 1;
4451 }
4452 }
4453 }
4454
4455 my $scsihwid="scsihw$controller";
4456
4457 vm_deviceunplug($vmid, $conf, $scsihwid);
4458
4459 return 1;
4460}
4461
281fedb3 4462sub qemu_add_pci_bridge {
d559309f 4463 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
40f28a9f
AD
4464
4465 my $bridges = {};
281fedb3
DM
4466
4467 my $bridgeid;
4468
d559309f 4469 print_pci_addr($device, $bridges, $arch, $machine_type);
40f28a9f
AD
4470
4471 while (my ($k, $v) = each %$bridges) {
4472 $bridgeid = $k;
4473 }
fee46675 4474 return 1 if !defined($bridgeid) || $bridgeid < 1;
281fedb3 4475
40f28a9f
AD
4476 my $bridge = "pci.$bridgeid";
4477 my $devices_list = vm_devices_list($vmid);
4478
281fedb3 4479 if (!defined($devices_list->{$bridge})) {
d559309f 4480 vm_deviceplug($storecfg, $conf, $vmid, $bridge, $arch, $machine_type);
40f28a9f 4481 }
281fedb3 4482
40f28a9f
AD
4483 return 1;
4484}
4485
25088687
DM
4486sub qemu_set_link_status {
4487 my ($vmid, $device, $up) = @_;
4488
0a13e08e 4489 mon_cmd($vmid, "set_link", name => $device,
25088687
DM
4490 up => $up ? JSON::true : JSON::false);
4491}
4492
2630d2a9 4493sub qemu_netdevadd {
d559309f 4494 my ($vmid, $conf, $arch, $device, $deviceid) = @_;
2630d2a9 4495
d559309f 4496 my $netdev = print_netdev_full($vmid, $conf, $arch, $device, $deviceid, 1);
73aa03b8 4497 my %options = split(/[=,]/, $netdev);
2630d2a9 4498
bf5aef9b
DC
4499 if (defined(my $vhost = $options{vhost})) {
4500 $options{vhost} = JSON::boolean(PVE::JSONSchema::parse_boolean($vhost));
4501 }
4502
4503 if (defined(my $queues = $options{queues})) {
4504 $options{queues} = $queues + 0;
4505 }
4506
0a13e08e 4507 mon_cmd($vmid, "netdev_add", %options);
73aa03b8 4508 return 1;
2630d2a9
DA
4509}
4510
4511sub qemu_netdevdel {
4512 my ($vmid, $deviceid) = @_;
4513
0a13e08e 4514 mon_cmd($vmid, "netdev_del", id => $deviceid);
2630d2a9
DA
4515}
4516
16521d63 4517sub qemu_usb_hotplug {
d559309f 4518 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
16521d63
DC
4519
4520 return if !$device;
4521
4522 # remove the old one first
4523 vm_deviceunplug($vmid, $conf, $deviceid);
4524
4525 # check if xhci controller is necessary and available
4526 if ($device->{usb3}) {
4527
4528 my $devicelist = vm_devices_list($vmid);
4529
4530 if (!$devicelist->{xhci}) {
d559309f 4531 my $pciaddr = print_pci_addr("xhci", undef, $arch, $machine_type);
16521d63
DC
4532 qemu_deviceadd($vmid, "nec-usb-xhci,id=xhci$pciaddr");
4533 }
4534 }
4535 my $d = parse_usb_device($device->{host});
4536 $d->{usb3} = $device->{usb3};
4537
4538 # add the new one
d559309f 4539 vm_deviceplug($storecfg, $conf, $vmid, $deviceid, $d, $arch, $machine_type);
16521d63
DC
4540}
4541
838776ab 4542sub qemu_cpu_hotplug {
8edc9c08 4543 my ($vmid, $conf, $vcpus) = @_;
838776ab 4544
3392d6ca 4545 my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
1e881b75 4546
8edc9c08
AD
4547 my $sockets = 1;
4548 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
4549 $sockets = $conf->{sockets} if $conf->{sockets};
4550 my $cores = $conf->{cores} || 1;
4551 my $maxcpus = $sockets * $cores;
838776ab 4552
8edc9c08 4553 $vcpus = $maxcpus if !$vcpus;
3a11fadb 4554
8edc9c08
AD
4555 die "you can't add more vcpus than maxcpus\n"
4556 if $vcpus > $maxcpus;
3a11fadb 4557
8edc9c08 4558 my $currentvcpus = $conf->{vcpus} || $maxcpus;
1e881b75 4559
eba3e64d 4560 if ($vcpus < $currentvcpus) {
1e881b75 4561
2ea5fb7e 4562 if (PVE::QemuServer::Machine::machine_version($machine_type, 2, 7)) {
1e881b75
AD
4563
4564 for (my $i = $currentvcpus; $i > $vcpus; $i--) {
4565 qemu_devicedel($vmid, "cpu$i");
4566 my $retry = 0;
4567 my $currentrunningvcpus = undef;
4568 while (1) {
65af8c31 4569 $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
1e881b75 4570 last if scalar(@{$currentrunningvcpus}) == $i-1;
961af8a3 4571 raise_param_exc({ vcpus => "error unplugging cpu$i" }) if $retry > 5;
1e881b75
AD
4572 $retry++;
4573 sleep 1;
4574 }
4575 #update conf after each succesfull cpu unplug
4576 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4577 PVE::QemuConfig->write_config($vmid, $conf);
4578 }
4579 } else {
961af8a3 4580 die "cpu hot-unplugging requires qemu version 2.7 or higher\n";
1e881b75
AD
4581 }
4582
4583 return;
4584 }
838776ab 4585
65af8c31 4586 my $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
961af8a3 4587 die "vcpus in running vm does not match its configuration\n"
8edc9c08 4588 if scalar(@{$currentrunningvcpus}) != $currentvcpus;
838776ab 4589
2ea5fb7e 4590 if (PVE::QemuServer::Machine::machine_version($machine_type, 2, 7)) {
eba3e64d
AD
4591
4592 for (my $i = $currentvcpus+1; $i <= $vcpus; $i++) {
4593 my $cpustr = print_cpu_device($conf, $i);
4594 qemu_deviceadd($vmid, $cpustr);
4595
4596 my $retry = 0;
4597 my $currentrunningvcpus = undef;
4598 while (1) {
65af8c31 4599 $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
eba3e64d 4600 last if scalar(@{$currentrunningvcpus}) == $i;
961af8a3 4601 raise_param_exc({ vcpus => "error hotplugging cpu$i" }) if $retry > 10;
eba3e64d
AD
4602 sleep 1;
4603 $retry++;
4604 }
4605 #update conf after each succesfull cpu hotplug
4606 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4607 PVE::QemuConfig->write_config($vmid, $conf);
4608 }
4609 } else {
4610
4611 for (my $i = $currentvcpus; $i < $vcpus; $i++) {
0a13e08e 4612 mon_cmd($vmid, "cpu-add", id => int($i));
eba3e64d 4613 }
838776ab
AD
4614 }
4615}
4616
affd2f88 4617sub qemu_block_set_io_throttle {
277ca170
WB
4618 my ($vmid, $deviceid,
4619 $bps, $bps_rd, $bps_wr, $iops, $iops_rd, $iops_wr,
9196a8ec
WB
4620 $bps_max, $bps_rd_max, $bps_wr_max, $iops_max, $iops_rd_max, $iops_wr_max,
4621 $bps_max_length, $bps_rd_max_length, $bps_wr_max_length,
4622 $iops_max_length, $iops_rd_max_length, $iops_wr_max_length) = @_;
affd2f88 4623
f3f323a3
AD
4624 return if !check_running($vmid) ;
4625
0a13e08e 4626 mon_cmd($vmid, "block_set_io_throttle", device => $deviceid,
277ca170
WB
4627 bps => int($bps),
4628 bps_rd => int($bps_rd),
4629 bps_wr => int($bps_wr),
4630 iops => int($iops),
4631 iops_rd => int($iops_rd),
4632 iops_wr => int($iops_wr),
4633 bps_max => int($bps_max),
4634 bps_rd_max => int($bps_rd_max),
4635 bps_wr_max => int($bps_wr_max),
4636 iops_max => int($iops_max),
4637 iops_rd_max => int($iops_rd_max),
9196a8ec
WB
4638 iops_wr_max => int($iops_wr_max),
4639 bps_max_length => int($bps_max_length),
4640 bps_rd_max_length => int($bps_rd_max_length),
4641 bps_wr_max_length => int($bps_wr_max_length),
4642 iops_max_length => int($iops_max_length),
4643 iops_rd_max_length => int($iops_rd_max_length),
4644 iops_wr_max_length => int($iops_wr_max_length),
277ca170 4645 );
f3f323a3 4646
affd2f88
AD
4647}
4648
c1175c92
AD
4649sub qemu_block_resize {
4650 my ($vmid, $deviceid, $storecfg, $volid, $size) = @_;
4651
ed221350 4652 my $running = check_running($vmid);
c1175c92 4653
7246e8f9 4654 $size = 0 if !PVE::Storage::volume_resize($storecfg, $volid, $size, $running);
c1175c92
AD
4655
4656 return if !$running;
4657
375db731
FE
4658 my $padding = (1024 - $size % 1024) % 1024;
4659 $size = $size + $padding;
4660
190c8461
SR
4661 mon_cmd(
4662 $vmid,
4663 "block_resize",
4664 device => $deviceid,
4665 size => int($size),
4666 timeout => 60,
4667 );
c1175c92
AD
4668}
4669
1ab0057c
AD
4670sub qemu_volume_snapshot {
4671 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4672
ed221350 4673 my $running = check_running($vmid);
1ab0057c 4674
9d83932d 4675 if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
0a13e08e 4676 mon_cmd($vmid, 'blockdev-snapshot-internal-sync', device => $deviceid, name => $snap);
e5eaa028
WL
4677 } else {
4678 PVE::Storage::volume_snapshot($storecfg, $volid, $snap);
4679 }
1ab0057c
AD
4680}
4681
fc46aff9
AD
4682sub qemu_volume_snapshot_delete {
4683 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4684
ed221350 4685 my $running = check_running($vmid);
fc46aff9 4686
a2f1554b
AD
4687 if($running) {
4688
4689 $running = undef;
4690 my $conf = PVE::QemuConfig->load_config($vmid);
912792e2 4691 PVE::QemuConfig->foreach_volume($conf, sub {
a2f1554b
AD
4692 my ($ds, $drive) = @_;
4693 $running = 1 if $drive->{file} eq $volid;
4694 });
4695 }
4696
9d83932d 4697 if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
0a13e08e 4698 mon_cmd($vmid, 'blockdev-snapshot-delete-internal-sync', device => $deviceid, name => $snap);
1ef7592f
AD
4699 } else {
4700 PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
4701 }
fc46aff9
AD
4702}
4703
264e519f 4704sub set_migration_caps {
27a5be53 4705 my ($vmid, $savevm) = @_;
a89fded1 4706
acc10e51
SR
4707 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support") };
4708
27a5be53
SR
4709 my $bitmap_prop = $savevm ? 'pbs-dirty-bitmap-savevm' : 'pbs-dirty-bitmap-migration';
4710 my $dirty_bitmaps = $qemu_support->{$bitmap_prop} ? 1 : 0;
4711
8b8345f3 4712 my $cap_ref = [];
a89fded1
AD
4713
4714 my $enabled_cap = {
8b8345f3 4715 "auto-converge" => 1,
0b0a47e8 4716 "xbzrle" => 1,
8b8345f3
DM
4717 "x-rdma-pin-all" => 0,
4718 "zero-blocks" => 0,
acc10e51 4719 "compress" => 0,
27a5be53 4720 "dirty-bitmaps" => $dirty_bitmaps,
a89fded1
AD
4721 };
4722
0a13e08e 4723 my $supported_capabilities = mon_cmd($vmid, "query-migrate-capabilities");
a89fded1 4724
8b8345f3 4725 for my $supported_capability (@$supported_capabilities) {
b463a3ce
SP
4726 push @$cap_ref, {
4727 capability => $supported_capability->{capability},
22430fa2
DM
4728 state => $enabled_cap->{$supported_capability->{capability}} ? JSON::true : JSON::false,
4729 };
a89fded1
AD
4730 }
4731
0a13e08e 4732 mon_cmd($vmid, "migrate-set-capabilities", capabilities => $cap_ref);
8b8345f3 4733}
a89fded1 4734
912792e2
FE
4735sub foreach_volid {
4736 my ($conf, $func, @param) = @_;
4737
4738 my $volhash = {};
4739
4740 my $test_volid = sub {
ae180b8f 4741 my ($key, $drive, $snapname) = @_;
912792e2 4742
ae180b8f 4743 my $volid = $drive->{file};
912792e2
FE
4744 return if !$volid;
4745
4746 $volhash->{$volid}->{cdrom} //= 1;
ae180b8f 4747 $volhash->{$volid}->{cdrom} = 0 if !drive_is_cdrom($drive);
912792e2 4748
ae180b8f 4749 my $replicate = $drive->{replicate} // 1;
912792e2
FE
4750 $volhash->{$volid}->{replicate} //= 0;
4751 $volhash->{$volid}->{replicate} = 1 if $replicate;
4752
4753 $volhash->{$volid}->{shared} //= 0;
ae180b8f 4754 $volhash->{$volid}->{shared} = 1 if $drive->{shared};
912792e2
FE
4755
4756 $volhash->{$volid}->{referenced_in_config} //= 0;
4757 $volhash->{$volid}->{referenced_in_config} = 1 if !defined($snapname);
4758
4759 $volhash->{$volid}->{referenced_in_snapshot}->{$snapname} = 1
4760 if defined($snapname);
ae180b8f
FE
4761
4762 my $size = $drive->{size};
4763 $volhash->{$volid}->{size} //= $size if $size;
4764
4765 $volhash->{$volid}->{is_vmstate} //= 0;
4766 $volhash->{$volid}->{is_vmstate} = 1 if $key eq 'vmstate';
4767
f9dde219
SR
4768 $volhash->{$volid}->{is_tpmstate} //= 0;
4769 $volhash->{$volid}->{is_tpmstate} = 1 if $key eq 'tpmstate0';
4770
ae180b8f
FE
4771 $volhash->{$volid}->{is_unused} //= 0;
4772 $volhash->{$volid}->{is_unused} = 1 if $key =~ /^unused\d+$/;
a6be63ac
FE
4773
4774 $volhash->{$volid}->{drivename} = $key if is_valid_drivename($key);
912792e2
FE
4775 };
4776
ae180b8f
FE
4777 my $include_opts = {
4778 extra_keys => ['vmstate'],
4779 include_unused => 1,
4780 };
4781
0b953b8e 4782 PVE::QemuConfig->foreach_volume_full($conf, $include_opts, $test_volid);
912792e2
FE
4783 foreach my $snapname (keys %{$conf->{snapshots}}) {
4784 my $snap = $conf->{snapshots}->{$snapname};
0b953b8e 4785 PVE::QemuConfig->foreach_volume_full($snap, $include_opts, $test_volid, $snapname);
912792e2
FE
4786 }
4787
4788 foreach my $volid (keys %$volhash) {
4789 &$func($volid, $volhash->{$volid}, @param);
4790 }
4791}
4792
81d95ae1 4793my $fast_plug_option = {
7498eb64 4794 'lock' => 1,
81d95ae1 4795 'name' => 1,
a1b7d579 4796 'onboot' => 1,
81d95ae1
DM
4797 'shares' => 1,
4798 'startup' => 1,
b0ec896e 4799 'description' => 1,
ec647db4 4800 'protection' => 1,
8cad5e9b 4801 'vmstatestorage' => 1,
9e784b11 4802 'hookscript' => 1,
b8e7068a 4803 'tags' => 1,
81d95ae1
DM
4804};
4805
3a11fadb
DM
4806# hotplug changes in [PENDING]
4807# $selection hash can be used to only apply specified options, for
4808# example: { cores => 1 } (only apply changed 'cores')
4809# $errors ref is used to return error messages
c427973b 4810sub vmconfig_hotplug_pending {
3a11fadb 4811 my ($vmid, $conf, $storecfg, $selection, $errors) = @_;
c427973b 4812
8e90138a 4813 my $defaults = load_defaults();
045749f2
TL
4814 my $arch = get_vm_arch($conf);
4815 my $machine_type = get_vm_machine($conf, undef, $arch);
c427973b
DM
4816
4817 # commit values which do not have any impact on running VM first
3a11fadb
DM
4818 # Note: those option cannot raise errors, we we do not care about
4819 # $selection and always apply them.
4820
4821 my $add_error = sub {
4822 my ($opt, $msg) = @_;
4823 $errors->{$opt} = "hotplug problem - $msg";
4824 };
c427973b
DM
4825
4826 my $changes = 0;
4827 foreach my $opt (keys %{$conf->{pending}}) { # add/change
81d95ae1 4828 if ($fast_plug_option->{$opt}) {
c427973b
DM
4829 $conf->{$opt} = $conf->{pending}->{$opt};
4830 delete $conf->{pending}->{$opt};
4831 $changes = 1;
4832 }
4833 }
4834
4835 if ($changes) {
ffda963f 4836 PVE::QemuConfig->write_config($vmid, $conf);
c427973b
DM
4837 }
4838
b3c2bdd1 4839 my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
c427973b 4840
5b65b00d 4841 my $cgroup = PVE::QemuServer::CGroup->new($vmid);
98bc3aeb 4842 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
d321c4a9 4843 foreach my $opt (sort keys %$pending_delete_hash) {
3a11fadb 4844 next if $selection && !$selection->{$opt};
d321c4a9 4845 my $force = $pending_delete_hash->{$opt}->{force};
3a11fadb 4846 eval {
51a6f637
AD
4847 if ($opt eq 'hotplug') {
4848 die "skip\n" if ($conf->{hotplug} =~ /memory/);
4849 } elsif ($opt eq 'tablet') {
b3c2bdd1 4850 die "skip\n" if !$hotplug_features->{usb};
3a11fadb 4851 if ($defaults->{tablet}) {
d559309f
WB
4852 vm_deviceplug($storecfg, $conf, $vmid, 'tablet', $arch, $machine_type);
4853 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard', $arch, $machine_type)
4854 if $arch eq 'aarch64';
3a11fadb 4855 } else {
d559309f
WB
4856 vm_deviceunplug($vmid, $conf, 'tablet');
4857 vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64';
3a11fadb 4858 }
4eb68604 4859 } elsif ($opt =~ m/^usb\d+/) {
f745762b 4860 die "skip\n";
50bbe377
TL
4861 # since we cannot reliably hot unplug usb devices we are disabling it
4862 #die "skip\n" if !$hotplug_features->{usb} || $conf->{$opt} =~ m/spice/i;
4863 #vm_deviceunplug($vmid, $conf, $opt);
8edc9c08 4864 } elsif ($opt eq 'vcpus') {
b3c2bdd1 4865 die "skip\n" if !$hotplug_features->{cpu};
8edc9c08 4866 qemu_cpu_hotplug($vmid, $conf, undef);
9c2f7069 4867 } elsif ($opt eq 'balloon') {
81d95ae1 4868 # enable balloon device is not hotpluggable
75b51053
DC
4869 die "skip\n" if defined($conf->{balloon}) && $conf->{balloon} == 0;
4870 # here we reset the ballooning value to memory
4871 my $balloon = $conf->{memory} || $defaults->{memory};
0a13e08e 4872 mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
81d95ae1
DM
4873 } elsif ($fast_plug_option->{$opt}) {
4874 # do nothing
3eec5767 4875 } elsif ($opt =~ m/^net(\d+)$/) {
b3c2bdd1 4876 die "skip\n" if !$hotplug_features->{network};
3eec5767 4877 vm_deviceunplug($vmid, $conf, $opt);
74479ee9 4878 } elsif (is_valid_drivename($opt)) {
b3c2bdd1 4879 die "skip\n" if !$hotplug_features->{disk} || $opt =~ m/(ide|sata)(\d+)/;
19120f99 4880 vm_deviceunplug($vmid, $conf, $opt);
3dc38fbb 4881 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
4d3f29ed
AD
4882 } elsif ($opt =~ m/^memory$/) {
4883 die "skip\n" if !$hotplug_features->{memory};
6779f1ac 4884 PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt);
c8effec3 4885 } elsif ($opt eq 'cpuunits') {
d3f9db4d 4886 $cgroup->change_cpu_shares(undef, 1024);
58be00f1 4887 } elsif ($opt eq 'cpulimit') {
25de70ae 4888 $cgroup->change_cpu_quota(undef, undef); # reset, cgroup module can better decide values
3d7389fe 4889 } else {
e56beeda 4890 die "skip\n";
3d7389fe 4891 }
3a11fadb
DM
4892 };
4893 if (my $err = $@) {
e56beeda
DM
4894 &$add_error($opt, $err) if $err ne "skip\n";
4895 } else {
3a11fadb 4896 delete $conf->{$opt};
98bc3aeb 4897 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
3d7389fe 4898 }
3d7389fe
DM
4899 }
4900
e5a66e48 4901 my ($apply_pending_cloudinit, $apply_pending_cloudinit_done);
9ed7a77c 4902 $apply_pending_cloudinit = sub {
e5a66e48
WB
4903 return if $apply_pending_cloudinit_done; # once is enough
4904 $apply_pending_cloudinit_done = 1; # once is enough
4905
9ed7a77c 4906 my ($key, $value) = @_;
9ed7a77c
WB
4907
4908 my @cloudinit_opts = keys %$confdesc_cloudinit;
4909 foreach my $opt (keys %{$conf->{pending}}) {
4910 next if !grep { $_ eq $opt } @cloudinit_opts;
4911 $conf->{$opt} = delete $conf->{pending}->{$opt};
4912 }
4913
e6ec384f
AD
4914 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
4915 foreach my $opt (sort keys %$pending_delete_hash) {
4916 next if !grep { $_ eq $opt } @cloudinit_opts;
4917 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
4918 delete $conf->{$opt};
4919 }
4920
9ed7a77c
WB
4921 my $new_conf = { %$conf };
4922 $new_conf->{$key} = $value;
4923 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($new_conf, $vmid);
4924 };
4925
3d7389fe 4926 foreach my $opt (keys %{$conf->{pending}}) {
3a11fadb 4927 next if $selection && !$selection->{$opt};
3d7389fe 4928 my $value = $conf->{pending}->{$opt};
3a11fadb 4929 eval {
51a6f637
AD
4930 if ($opt eq 'hotplug') {
4931 die "skip\n" if ($value =~ /memory/) || ($value !~ /memory/ && $conf->{hotplug} =~ /memory/);
4932 } elsif ($opt eq 'tablet') {
b3c2bdd1 4933 die "skip\n" if !$hotplug_features->{usb};
3a11fadb 4934 if ($value == 1) {
d559309f
WB
4935 vm_deviceplug($storecfg, $conf, $vmid, 'tablet', $arch, $machine_type);
4936 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard', $arch, $machine_type)
4937 if $arch eq 'aarch64';
3a11fadb 4938 } elsif ($value == 0) {
d559309f
WB
4939 vm_deviceunplug($vmid, $conf, 'tablet');
4940 vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64';
3a11fadb 4941 }
4eb68604 4942 } elsif ($opt =~ m/^usb\d+$/) {
f745762b 4943 die "skip\n";
50bbe377
TL
4944 # since we cannot reliably hot unplug usb devices we disable it for now
4945 #die "skip\n" if !$hotplug_features->{usb} || $value =~ m/spice/i;
4946 #my $d = eval { parse_property_string($usbdesc->{format}, $value) };
4947 #die "skip\n" if !$d;
4948 #qemu_usb_hotplug($storecfg, $conf, $vmid, $opt, $d, $arch, $machine_type);
8edc9c08 4949 } elsif ($opt eq 'vcpus') {
b3c2bdd1 4950 die "skip\n" if !$hotplug_features->{cpu};
3a11fadb
DM
4951 qemu_cpu_hotplug($vmid, $conf, $value);
4952 } elsif ($opt eq 'balloon') {
81d95ae1 4953 # enable/disable balloning device is not hotpluggable
8fe689e7 4954 my $old_balloon_enabled = !!(!defined($conf->{balloon}) || $conf->{balloon});
a1b7d579 4955 my $new_balloon_enabled = !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});
81d95ae1
DM
4956 die "skip\n" if $old_balloon_enabled != $new_balloon_enabled;
4957
3a11fadb 4958 # allow manual ballooning if shares is set to zero
4cc1efa6 4959 if ((defined($conf->{shares}) && ($conf->{shares} == 0))) {
9c2f7069 4960 my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
0a13e08e 4961 mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
9c2f7069 4962 }
a1b7d579 4963 } elsif ($opt =~ m/^net(\d+)$/) {
3eec5767 4964 # some changes can be done without hotplug
a1b7d579 4965 vmconfig_update_net($storecfg, $conf, $hotplug_features->{network},
d559309f 4966 $vmid, $opt, $value, $arch, $machine_type);
74479ee9 4967 } elsif (is_valid_drivename($opt)) {
f9dde219 4968 die "skip\n" if $opt eq 'efidisk0' || $opt eq 'tpmstate0';
a05cff86 4969 # some changes can be done without hotplug
9ed7a77c
WB
4970 my $drive = parse_drive($opt, $value);
4971 if (drive_is_cloudinit($drive)) {
4972 &$apply_pending_cloudinit($opt, $value);
4973 }
b3c2bdd1 4974 vmconfig_update_disk($storecfg, $conf, $hotplug_features->{disk},
9e7bce2c 4975 $vmid, $opt, $value, $arch, $machine_type);
4d3f29ed
AD
4976 } elsif ($opt =~ m/^memory$/) { #dimms
4977 die "skip\n" if !$hotplug_features->{memory};
6779f1ac 4978 $value = PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt, $value);
c8effec3 4979 } elsif ($opt eq 'cpuunits') {
64cc310f
TL
4980 my $new_cpuunits = get_cpuunits({ $opt => $conf->{pending}->{$opt} }); # to clamp
4981 $cgroup->change_cpu_shares($new_cpuunits, 1024);
58be00f1 4982 } elsif ($opt eq 'cpulimit') {
c6f773b8 4983 my $cpulimit = $conf->{pending}->{$opt} == 0 ? -1 : int($conf->{pending}->{$opt} * 100000);
5b65b00d 4984 $cgroup->change_cpu_quota($cpulimit, 100000);
74ea2c65
AD
4985 } elsif ($opt eq 'agent') {
4986 vmconfig_update_agent($conf, $opt, $value);
3a11fadb 4987 } else {
e56beeda 4988 die "skip\n"; # skip non-hot-pluggable options
3d7389fe 4989 }
3a11fadb
DM
4990 };
4991 if (my $err = $@) {
e56beeda
DM
4992 &$add_error($opt, $err) if $err ne "skip\n";
4993 } else {
3a11fadb
DM
4994 $conf->{$opt} = $value;
4995 delete $conf->{pending}->{$opt};
3d7389fe 4996 }
3d7389fe 4997 }
4df15a03
OB
4998
4999 PVE::QemuConfig->write_config($vmid, $conf);
c427973b 5000}
055d554d 5001
3dc38fbb
WB
5002sub try_deallocate_drive {
5003 my ($storecfg, $vmid, $conf, $key, $drive, $rpcenv, $authuser, $force) = @_;
5004
5005 if (($force || $key =~ /^unused/) && !drive_is_cdrom($drive, 1)) {
5006 my $volid = $drive->{file};
5007 if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
5008 my $sid = PVE::Storage::parse_volume_id($volid);
5009 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
cee01bcb
WB
5010
5011 # check if the disk is really unused
cee01bcb 5012 die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
e0fd2b2f 5013 if PVE::QemuServer::Drive::is_volume_in_use($storecfg, $conf, $key, $volid);
cee01bcb 5014 PVE::Storage::vdisk_free($storecfg, $volid);
3dc38fbb 5015 return 1;
40b977f3
WL
5016 } else {
5017 # If vm is not owner of this disk remove from config
5018 return 1;
3dc38fbb
WB
5019 }
5020 }
5021
d1c1af4b 5022 return;
3dc38fbb
WB
5023}
5024
5025sub vmconfig_delete_or_detach_drive {
5026 my ($vmid, $storecfg, $conf, $opt, $force) = @_;
5027
5028 my $drive = parse_drive($opt, $conf->{$opt});
5029
5030 my $rpcenv = PVE::RPCEnvironment::get();
5031 my $authuser = $rpcenv->get_user();
5032
5033 if ($force) {
5034 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
5035 try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser, $force);
5036 } else {
5037 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $drive);
5038 }
5039}
5040
98bc3aeb
OB
5041
5042
055d554d 5043sub vmconfig_apply_pending {
eb5e482d
OB
5044 my ($vmid, $conf, $storecfg, $errors) = @_;
5045
a644de29
OB
5046 return if !scalar(keys %{$conf->{pending}});
5047
eb5e482d
OB
5048 my $add_apply_error = sub {
5049 my ($opt, $msg) = @_;
5050 my $err_msg = "unable to apply pending change $opt : $msg";
5051 $errors->{$opt} = $err_msg;
5052 warn $err_msg;
5053 };
c427973b
DM
5054
5055 # cold plug
055d554d 5056
98bc3aeb 5057 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
d321c4a9 5058 foreach my $opt (sort keys %$pending_delete_hash) {
fb4d1ba2 5059 my $force = $pending_delete_hash->{$opt}->{force};
eb5e482d 5060 eval {
3d48b95a
OB
5061 if ($opt =~ m/^unused/) {
5062 die "internal error";
5063 } elsif (defined($conf->{$opt}) && is_valid_drivename($opt)) {
eb5e482d 5064 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
eb5e482d
OB
5065 }
5066 };
5067 if (my $err = $@) {
5068 $add_apply_error->($opt, $err);
055d554d 5069 } else {
98bc3aeb 5070 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
055d554d 5071 delete $conf->{$opt};
055d554d
DM
5072 }
5073 }
5074
3d48b95a 5075 PVE::QemuConfig->cleanup_pending($conf);
055d554d
DM
5076
5077 foreach my $opt (keys %{$conf->{pending}}) { # add/change
3d48b95a 5078 next if $opt eq 'delete'; # just to be sure
eb5e482d 5079 eval {
3d48b95a 5080 if (defined($conf->{$opt}) && is_valid_drivename($opt)) {
eb5e482d 5081 vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
eb5e482d
OB
5082 }
5083 };
5084 if (my $err = $@) {
5085 $add_apply_error->($opt, $err);
055d554d 5086 } else {
eb5e482d 5087 $conf->{$opt} = delete $conf->{pending}->{$opt};
055d554d 5088 }
055d554d 5089 }
3d48b95a
OB
5090
5091 # write all changes at once to avoid unnecessary i/o
5092 PVE::QemuConfig->write_config($vmid, $conf);
055d554d
DM
5093}
5094
3eec5767 5095sub vmconfig_update_net {
d559309f 5096 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
3eec5767
DM
5097
5098 my $newnet = parse_net($value);
5099
5100 if ($conf->{$opt}) {
5101 my $oldnet = parse_net($conf->{$opt});
5102
0f1af9e7
OB
5103 if (safe_string_ne($oldnet->{model}, $newnet->{model}) ||
5104 safe_string_ne($oldnet->{macaddr}, $newnet->{macaddr}) ||
5105 safe_num_ne($oldnet->{queues}, $newnet->{queues}) ||
3eec5767
DM
5106 !($newnet->{bridge} && $oldnet->{bridge})) { # bridge/nat mode change
5107
5108 # for non online change, we try to hot-unplug
7196b757 5109 die "skip\n" if !$hotplug;
3eec5767
DM
5110 vm_deviceunplug($vmid, $conf, $opt);
5111 } else {
5112
5113 die "internal error" if $opt !~ m/net(\d+)/;
5114 my $iface = "tap${vmid}i$1";
a1b7d579 5115
0f1af9e7
OB
5116 if (safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
5117 safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
5118 safe_string_ne($oldnet->{trunks}, $newnet->{trunks}) ||
5119 safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
3eec5767 5120 PVE::Network::tap_unplug($iface);
28e129cc
AD
5121
5122 if ($have_sdn) {
5123 PVE::Network::SDN::Zones::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
5124 } else {
5125 PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
5126 }
0f1af9e7 5127 } elsif (safe_num_ne($oldnet->{rate}, $newnet->{rate})) {
4f4fbeb0
WB
5128 # Rate can be applied on its own but any change above needs to
5129 # include the rate in tap_plug since OVS resets everything.
5130 PVE::Network::tap_rate_limit($iface, $newnet->{rate});
3eec5767 5131 }
38c590d9 5132
0f1af9e7 5133 if (safe_string_ne($oldnet->{link_down}, $newnet->{link_down})) {
25088687
DM
5134 qemu_set_link_status($vmid, $opt, !$newnet->{link_down});
5135 }
5136
38c590d9 5137 return 1;
3eec5767
DM
5138 }
5139 }
a1b7d579 5140
7196b757 5141 if ($hotplug) {
d559309f 5142 vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet, $arch, $machine_type);
38c590d9
DM
5143 } else {
5144 die "skip\n";
5145 }
3eec5767
DM
5146}
5147
74ea2c65
AD
5148sub vmconfig_update_agent {
5149 my ($conf, $opt, $value) = @_;
5150
5151 die "skip\n" if !$conf->{$opt};
5152
5153 my $hotplug_options = { fstrim_cloned_disks => 1 };
5154
5155 my $old_agent = parse_guest_agent($conf);
5156 my $agent = parse_guest_agent({$opt => $value});
5157
33f8b887 5158 for my $option (keys %$agent) { # added/changed options
74ea2c65
AD
5159 next if defined($hotplug_options->{$option});
5160 die "skip\n" if safe_string_ne($agent->{$option}, $old_agent->{$option});
5161 }
5162
33f8b887 5163 for my $option (keys %$old_agent) { # removed options
74ea2c65
AD
5164 next if defined($hotplug_options->{$option});
5165 die "skip\n" if safe_string_ne($old_agent->{$option}, $agent->{$option});
5166 }
33f8b887
TL
5167
5168 return; # either no actual change (e.g., format string reordered) or just hotpluggable changes
74ea2c65
AD
5169}
5170
a05cff86 5171sub vmconfig_update_disk {
9e7bce2c 5172 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
a05cff86
DM
5173
5174 my $drive = parse_drive($opt, $value);
5175
4df98f2f
TL
5176 if ($conf->{$opt} && (my $old_drive = parse_drive($opt, $conf->{$opt}))) {
5177 my $media = $drive->{media} || 'disk';
5178 my $oldmedia = $old_drive->{media} || 'disk';
5179 die "unable to change media type\n" if $media ne $oldmedia;
a05cff86 5180
4df98f2f 5181 if (!drive_is_cdrom($old_drive)) {
a05cff86 5182
4df98f2f 5183 if ($drive->{file} ne $old_drive->{file}) {
a05cff86 5184
4df98f2f 5185 die "skip\n" if !$hotplug;
a05cff86 5186
4df98f2f
TL
5187 # unplug and register as unused
5188 vm_deviceunplug($vmid, $conf, $opt);
5189 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive)
a1b7d579 5190
4df98f2f
TL
5191 } else {
5192 # update existing disk
5193
5194 # skip non hotpluggable value
5195 if (safe_string_ne($drive->{discard}, $old_drive->{discard}) ||
5196 safe_string_ne($drive->{iothread}, $old_drive->{iothread}) ||
5197 safe_string_ne($drive->{queues}, $old_drive->{queues}) ||
5198 safe_string_ne($drive->{cache}, $old_drive->{cache}) ||
5199 safe_string_ne($drive->{ssd}, $old_drive->{ssd})) {
5200 die "skip\n";
5201 }
a05cff86 5202
4df98f2f
TL
5203 # apply throttle
5204 if (safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
5205 safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
5206 safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
5207 safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
5208 safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
5209 safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr}) ||
5210 safe_num_ne($drive->{mbps_max}, $old_drive->{mbps_max}) ||
5211 safe_num_ne($drive->{mbps_rd_max}, $old_drive->{mbps_rd_max}) ||
5212 safe_num_ne($drive->{mbps_wr_max}, $old_drive->{mbps_wr_max}) ||
5213 safe_num_ne($drive->{iops_max}, $old_drive->{iops_max}) ||
5214 safe_num_ne($drive->{iops_rd_max}, $old_drive->{iops_rd_max}) ||
5215 safe_num_ne($drive->{iops_wr_max}, $old_drive->{iops_wr_max}) ||
5216 safe_num_ne($drive->{bps_max_length}, $old_drive->{bps_max_length}) ||
5217 safe_num_ne($drive->{bps_rd_max_length}, $old_drive->{bps_rd_max_length}) ||
5218 safe_num_ne($drive->{bps_wr_max_length}, $old_drive->{bps_wr_max_length}) ||
5219 safe_num_ne($drive->{iops_max_length}, $old_drive->{iops_max_length}) ||
5220 safe_num_ne($drive->{iops_rd_max_length}, $old_drive->{iops_rd_max_length}) ||
5221 safe_num_ne($drive->{iops_wr_max_length}, $old_drive->{iops_wr_max_length})) {
5222
5223 qemu_block_set_io_throttle(
5224 $vmid,"drive-$opt",
5225 ($drive->{mbps} || 0)*1024*1024,
5226 ($drive->{mbps_rd} || 0)*1024*1024,
5227 ($drive->{mbps_wr} || 0)*1024*1024,
5228 $drive->{iops} || 0,
5229 $drive->{iops_rd} || 0,
5230 $drive->{iops_wr} || 0,
5231 ($drive->{mbps_max} || 0)*1024*1024,
5232 ($drive->{mbps_rd_max} || 0)*1024*1024,
5233 ($drive->{mbps_wr_max} || 0)*1024*1024,
5234 $drive->{iops_max} || 0,
5235 $drive->{iops_rd_max} || 0,
5236 $drive->{iops_wr_max} || 0,
5237 $drive->{bps_max_length} || 1,
5238 $drive->{bps_rd_max_length} || 1,
5239 $drive->{bps_wr_max_length} || 1,
5240 $drive->{iops_max_length} || 1,
5241 $drive->{iops_rd_max_length} || 1,
5242 $drive->{iops_wr_max_length} || 1,
5243 );
a05cff86 5244
4df98f2f 5245 }
a1b7d579 5246
4df98f2f
TL
5247 return 1;
5248 }
4de1bb25 5249
4df98f2f 5250 } else { # cdrom
a1b7d579 5251
4df98f2f
TL
5252 if ($drive->{file} eq 'none') {
5253 mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
5254 if (drive_is_cloudinit($old_drive)) {
5255 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
5256 }
5257 } else {
5258 my $path = get_iso_path($storecfg, $vmid, $drive->{file});
ce9fce79 5259
4df98f2f
TL
5260 # force eject if locked
5261 mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
ce9fce79 5262
4df98f2f
TL
5263 if ($path) {
5264 mon_cmd($vmid, "blockdev-change-medium",
5265 id => "$opt", filename => "$path");
4de1bb25 5266 }
a05cff86 5267 }
4df98f2f
TL
5268
5269 return 1;
a05cff86
DM
5270 }
5271 }
5272
a1b7d579 5273 die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
4de1bb25 5274 # hotplug new disks
f7b4356f 5275 PVE::Storage::activate_volumes($storecfg, [$drive->{file}]) if $drive->{file} !~ m|^/dev/.+|;
d559309f 5276 vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive, $arch, $machine_type);
a05cff86
DM
5277}
5278
13cfe3b7 5279# called in locked context by incoming migration
ba5396b5
FG
5280sub vm_migrate_get_nbd_disks {
5281 my ($storecfg, $conf, $replicated_volumes) = @_;
13cfe3b7
FG
5282
5283 my $local_volumes = {};
912792e2 5284 PVE::QemuConfig->foreach_volume($conf, sub {
13cfe3b7
FG
5285 my ($ds, $drive) = @_;
5286
5287 return if drive_is_cdrom($drive);
41c8671e 5288 return if $ds eq 'tpmstate0';
13cfe3b7
FG
5289
5290 my $volid = $drive->{file};
5291
5292 return if !$volid;
5293
5294 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
5295
5296 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5297 return if $scfg->{shared};
ba5396b5
FG
5298
5299 # replicated disks re-use existing state via bitmap
5300 my $use_existing = $replicated_volumes->{$volid} ? 1 : 0;
5301 $local_volumes->{$ds} = [$volid, $storeid, $volname, $drive, $use_existing];
13cfe3b7 5302 });
ba5396b5
FG
5303 return $local_volumes;
5304}
5305
5306# called in locked context by incoming migration
5307sub vm_migrate_alloc_nbd_disks {
5308 my ($storecfg, $vmid, $source_volumes, $storagemap) = @_;
13cfe3b7 5309
13cfe3b7 5310 my $nbd = {};
ba5396b5 5311 foreach my $opt (sort keys %$source_volumes) {
5668463b 5312 my ($volid, $storeid, $volname, $drive, $use_existing, $format) = @{$source_volumes->{$opt}};
ba5396b5
FG
5313
5314 if ($use_existing) {
5315 $nbd->{$opt}->{drivestr} = print_drive($drive);
5316 $nbd->{$opt}->{volid} = $volid;
5317 $nbd->{$opt}->{replicated} = 1;
13cfe3b7
FG
5318 next;
5319 }
13cfe3b7 5320
5668463b
FG
5321 # storage mapping + volname = regular migration
5322 # storage mapping + format = remote migration
5323 # order of precedence, filtered by whether storage supports it:
5324 # 1. explicit requested format
5325 # 2. format of current volume
5326 # 3. default format of storage
bf8fc5a3 5327 if (!$storagemap->{identity}) {
82a03671 5328 $storeid = PVE::JSONSchema::map_id($storagemap, $storeid);
13cfe3b7 5329 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
5668463b
FG
5330 if (!$format || !grep { $format eq $_ } @$validFormats) {
5331 if ($volname) {
5332 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5333 my $fileFormat = qemu_img_format($scfg, $volname);
5334 $format = $fileFormat
5335 if grep { $fileFormat eq $_ } @$validFormats;
5336 }
5337 $format //= $defFormat;
5338 }
13cfe3b7 5339 } else {
5668463b 5340 # can't happen for remote migration, so $volname is always defined
13cfe3b7
FG
5341 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5342 $format = qemu_img_format($scfg, $volname);
5343 }
5344
4df98f2f
TL
5345 my $size = $drive->{size} / 1024;
5346 my $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, undef, $size);
13cfe3b7
FG
5347 my $newdrive = $drive;
5348 $newdrive->{format} = $format;
5349 $newdrive->{file} = $newvolid;
5350 my $drivestr = print_drive($newdrive);
ba5396b5
FG
5351 $nbd->{$opt}->{drivestr} = $drivestr;
5352 $nbd->{$opt}->{volid} = $newvolid;
13cfe3b7
FG
5353 }
5354
5355 return $nbd;
5356}
5357
5358# see vm_start_nolock for parameters, additionally:
5359# migrate_opts:
bf8fc5a3 5360# storagemap = parsed storage map for allocating NBD disks
3898a563
FG
5361sub vm_start {
5362 my ($storecfg, $vmid, $params, $migrate_opts) = @_;
5363
84da8217 5364 return PVE::QemuConfig->lock_config($vmid, sub {
3898a563
FG
5365 my $conf = PVE::QemuConfig->load_config($vmid, $migrate_opts->{migratedfrom});
5366
4ef13a7f
FG
5367 die "you can't start a vm if it's a template\n"
5368 if !$params->{skiptemplate} && PVE::QemuConfig->is_template($conf);
3898a563 5369
d544e0e0 5370 my $has_suspended_lock = PVE::QemuConfig->has_lock($conf, 'suspended');
8e0c97bb
SR
5371 my $has_backup_lock = PVE::QemuConfig->has_lock($conf, 'backup');
5372
5373 my $running = check_running($vmid, undef, $migrate_opts->{migratedfrom});
5374
5375 if ($has_backup_lock && $running) {
5376 # a backup is currently running, attempt to start the guest in the
5377 # existing QEMU instance
5378 return vm_resume($vmid);
5379 }
3898a563
FG
5380
5381 PVE::QemuConfig->check_lock($conf)
d544e0e0
FE
5382 if !($params->{skiplock} || $has_suspended_lock);
5383
5384 $params->{resume} = $has_suspended_lock || defined($conf->{vmstate});
3898a563 5385
8e0c97bb 5386 die "VM $vmid already running\n" if $running;
3898a563 5387
ba5396b5
FG
5388 if (my $storagemap = $migrate_opts->{storagemap}) {
5389 my $replicated = $migrate_opts->{replicated_volumes};
5390 my $disks = vm_migrate_get_nbd_disks($storecfg, $conf, $replicated);
5391 $migrate_opts->{nbd} = vm_migrate_alloc_nbd_disks($storecfg, $vmid, $disks, $storagemap);
5392
5393 foreach my $opt (keys %{$migrate_opts->{nbd}}) {
5394 $conf->{$opt} = $migrate_opts->{nbd}->{$opt}->{drivestr};
5395 }
5396 }
13cfe3b7 5397
84da8217 5398 return vm_start_nolock($storecfg, $vmid, $conf, $params, $migrate_opts);
3898a563
FG
5399 });
5400}
5401
5402
0c498cca
FG
5403# params:
5404# statefile => 'tcp', 'unix' for migration or path/volid for RAM state
5405# skiplock => 0/1, skip checking for config lock
4ef13a7f 5406# skiptemplate => 0/1, skip checking whether VM is template
0c498cca 5407# forcemachine => to force Qemu machine (rollback/migration)
58c64ad5 5408# forcecpu => a QEMU '-cpu' argument string to override get_cpu_options
0c498cca
FG
5409# timeout => in seconds
5410# paused => start VM in paused state (backup)
3898a563 5411# resume => resume from hibernation
5921764c
SR
5412# pbs-backing => {
5413# sata0 => {
5414# repository
5415# snapshot
5416# keyfile
5417# archive
5418# },
5419# virtio2 => ...
5420# }
0c498cca 5421# migrate_opts:
ba5396b5 5422# nbd => volumes for NBD exports (vm_migrate_alloc_nbd_disks)
0c498cca
FG
5423# migratedfrom => source node
5424# spice_ticket => used for spice migration, passed via tunnel/stdin
5425# network => CIDR of migration network
5426# type => secure/insecure - tunnel over encrypted connection or plain-text
0c498cca 5427# nbd_proto_version => int, 0 for TCP, 1 for UNIX
fd95d780
FG
5428# replicated_volumes => which volids should be re-used with bitmaps for nbd migration
5429# tpmstate_vol => new volid of tpmstate0, not yet contained in config
3898a563
FG
5430sub vm_start_nolock {
5431 my ($storecfg, $vmid, $conf, $params, $migrate_opts) = @_;
1e3baf05 5432
3898a563
FG
5433 my $statefile = $params->{statefile};
5434 my $resume = $params->{resume};
3dcb98d5 5435
3898a563
FG
5436 my $migratedfrom = $migrate_opts->{migratedfrom};
5437 my $migration_type = $migrate_opts->{type};
7ceade4c 5438
84da8217
FG
5439 my $res = {};
5440
3898a563
FG
5441 # clean up leftover reboot request files
5442 eval { clear_reboot_request($vmid); };
5443 warn $@ if $@;
1e3baf05 5444
3898a563
FG
5445 if (!$statefile && scalar(keys %{$conf->{pending}})) {
5446 vmconfig_apply_pending($vmid, $conf, $storecfg);
5447 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5448 }
64457ed4 5449
104f47a9
ML
5450 # don't regenerate the ISO if the VM is started as part of a live migration
5451 # this way we can reuse the old ISO with the correct config
5452 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid) if !$migratedfrom;
055d554d 5453
fd95d780
FG
5454 # override TPM state vol if migrated, conf is out of date still
5455 if (my $tpmvol = $migrate_opts->{tpmstate_vol}) {
5456 my $parsed = parse_drive("tpmstate0", $conf->{tpmstate0});
5457 $parsed->{file} = $tpmvol;
5458 $conf->{tpmstate0} = print_drive($parsed);
5459 }
5460
3898a563 5461 my $defaults = load_defaults();
0c9a7596 5462
3898a563
FG
5463 # set environment variable useful inside network script
5464 $ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
6c47d546 5465
3898a563 5466 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
9e784b11 5467
3898a563 5468 my $forcemachine = $params->{forcemachine};
ea1c2110 5469 my $forcecpu = $params->{forcecpu};
3898a563 5470 if ($resume) {
ea1c2110 5471 # enforce machine and CPU type on suspended vm to ensure HW compatibility
3898a563 5472 $forcemachine = $conf->{runningmachine};
ea1c2110 5473 $forcecpu = $conf->{runningcpu};
3898a563
FG
5474 print "Resuming suspended VM\n";
5475 }
7ceade4c 5476
5921764c
SR
5477 my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid,
5478 $conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'});
6c47d546 5479
3898a563
FG
5480 my $migration_ip;
5481 my $get_migration_ip = sub {
5482 my ($nodename) = @_;
b24e1ac2 5483
3898a563 5484 return $migration_ip if defined($migration_ip);
b24e1ac2 5485
3898a563 5486 my $cidr = $migrate_opts->{network};
0c498cca 5487
3898a563
FG
5488 if (!defined($cidr)) {
5489 my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
5490 $cidr = $dc_conf->{migration}->{network};
5491 }
b24e1ac2 5492
3898a563
FG
5493 if (defined($cidr)) {
5494 my $ips = PVE::Network::get_local_ip_from_cidr($cidr);
b24e1ac2 5495
3898a563
FG
5496 die "could not get IP: no address configured on local " .
5497 "node for network '$cidr'\n" if scalar(@$ips) == 0;
b24e1ac2 5498
3898a563
FG
5499 die "could not get IP: multiple addresses configured on local " .
5500 "node for network '$cidr'\n" if scalar(@$ips) > 1;
b24e1ac2 5501
3898a563
FG
5502 $migration_ip = @$ips[0];
5503 }
b24e1ac2 5504
3898a563
FG
5505 $migration_ip = PVE::Cluster::remote_node_ip($nodename, 1)
5506 if !defined($migration_ip);
b24e1ac2 5507
3898a563
FG
5508 return $migration_ip;
5509 };
b24e1ac2 5510
3898a563
FG
5511 my $migrate_uri;
5512 if ($statefile) {
5513 if ($statefile eq 'tcp') {
5514 my $localip = "localhost";
5515 my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
5516 my $nodename = nodename();
2de2d6f7 5517
3898a563
FG
5518 if (!defined($migration_type)) {
5519 if (defined($datacenterconf->{migration}->{type})) {
5520 $migration_type = $datacenterconf->{migration}->{type};
5521 } else {
5522 $migration_type = 'secure';
b7a5a225 5523 }
3898a563 5524 }
b7a5a225 5525
3898a563
FG
5526 if ($migration_type eq 'insecure') {
5527 $localip = $get_migration_ip->($nodename);
5528 $localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
5529 }
2de2d6f7 5530
3898a563
FG
5531 my $pfamily = PVE::Tools::get_host_address_family($nodename);
5532 my $migrate_port = PVE::Tools::next_migrate_port($pfamily);
5533 $migrate_uri = "tcp:${localip}:${migrate_port}";
5534 push @$cmd, '-incoming', $migrate_uri;
5535 push @$cmd, '-S';
1c9d54bf 5536
3898a563
FG
5537 } elsif ($statefile eq 'unix') {
5538 # should be default for secure migrations as a ssh TCP forward
5539 # tunnel is not deterministic reliable ready and fails regurarly
5540 # to set up in time, so use UNIX socket forwards
5541 my $socket_addr = "/run/qemu-server/$vmid.migrate";
5542 unlink $socket_addr;
54323eed 5543
3898a563 5544 $migrate_uri = "unix:$socket_addr";
1c9d54bf 5545
3898a563
FG
5546 push @$cmd, '-incoming', $migrate_uri;
5547 push @$cmd, '-S';
1c9d54bf 5548
3898a563
FG
5549 } elsif (-e $statefile) {
5550 push @$cmd, '-loadstate', $statefile;
5551 } else {
5552 my $statepath = PVE::Storage::path($storecfg, $statefile);
5553 push @$vollist, $statefile;
5554 push @$cmd, '-loadstate', $statepath;
5555 }
5556 } elsif ($params->{paused}) {
5557 push @$cmd, '-S';
5558 }
5559
1fb1822e
DC
5560 my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $resume);
5561
5562 my $pci_devices = {}; # host pci devices
74c17b7a 5563 for (my $i = 0; $i < $PVE::QemuServer::PCI::MAX_HOSTPCI_DEVICES; $i++) {
1fb1822e
DC
5564 my $dev = $conf->{"hostpci$i"} or next;
5565 $pci_devices->{$i} = parse_hostpci($dev);
5566 }
5567
9c85548f
DC
5568 # do not reserve pciid for mediated devices, sysfs will error out for duplicate assignment
5569 my $real_pci_devices = [ grep { !(defined($_->{mdev}) && scalar($_->{pciid}->@*) == 1) } values $pci_devices->%* ];
5570
5571 # map to a flat list of pci ids
5572 my $pci_id_list = [ map { $_->{id} } map { $_->{pciid}->@* } $real_pci_devices->@* ];
5573
1fb1822e
DC
5574 # reserve all PCI IDs before actually doing anything with them
5575 PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, $start_timeout);
5576
5577 eval {
5578 for my $id (sort keys %$pci_devices) {
5579 my $d = $pci_devices->{$id};
5580 for my $dev ($d->{pciid}->@*) {
5581 PVE::QemuServer::PCI::prepare_pci_device($vmid, $dev->{id}, $id, $d->{mdev});
5582 }
5583 }
5584 };
5585 if (my $err = $@) {
5586 eval { PVE::QemuServer::PCI::remove_pci_reservation($pci_id_list) };
5587 warn $@ if $@;
5588 die $err;
3898a563 5589 }
1e3baf05 5590
3898a563 5591 PVE::Storage::activate_volumes($storecfg, $vollist);
1e3baf05 5592
3898a563 5593 eval {
6bbcd71f 5594 run_command(['/bin/systemctl', 'stop', "$vmid.scope"], outfunc => sub{}, errfunc => sub{});
3898a563
FG
5595 };
5596 # Issues with the above 'stop' not being fully completed are extremely rare, a very low
5597 # timeout should be more than enough here...
39abafc8 5598 PVE::Systemd::wait_for_unit_removed("$vmid.scope", 20);
3898a563 5599
d3f9db4d 5600 my $cpuunits = get_cpuunits($conf);
3898a563 5601
3898a563
FG
5602 my %run_params = (
5603 timeout => $statefile ? undef : $start_timeout,
5604 umask => 0077,
5605 noerr => 1,
5606 );
1e3baf05 5607
3898a563
FG
5608 # when migrating, prefix QEMU output so other side can pick up any
5609 # errors that might occur and show the user
5610 if ($migratedfrom) {
5611 $run_params{quiet} = 1;
5612 $run_params{logfunc} = sub { print "QEMU: $_[0]\n" };
5613 }
8bf30c2a 5614
212220a4 5615 my %systemd_properties = (
3898a563 5616 Slice => 'qemu.slice',
354e61aa
SR
5617 KillMode => 'process',
5618 SendSIGKILL => 0,
5619 TimeoutStopUSec => ULONG_MAX, # infinity
3898a563 5620 );
7023f3ea 5621
6cbd3eb8 5622 if (PVE::CGroup::cgroup_mode() == 2) {
212220a4 5623 $systemd_properties{CPUWeight} = $cpuunits;
6cbd3eb8 5624 } else {
212220a4 5625 $systemd_properties{CPUShares} = $cpuunits;
6cbd3eb8
AD
5626 }
5627
3898a563 5628 if (my $cpulimit = $conf->{cpulimit}) {
212220a4 5629 $systemd_properties{CPUQuota} = int($cpulimit * 100);
3898a563 5630 }
212220a4 5631 $systemd_properties{timeout} = 10 if $statefile; # setting up the scope shoul be quick
7023f3ea 5632
3898a563
FG
5633 my $run_qemu = sub {
5634 PVE::Tools::run_fork sub {
212220a4 5635 PVE::Systemd::enter_systemd_scope($vmid, "Proxmox VE VM $vmid", %systemd_properties);
6e0216d8 5636
f9dde219
SR
5637 my $tpmpid;
5638 if (my $tpm = $conf->{tpmstate0}) {
5639 # start the TPM emulator so QEMU can connect on start
5640 $tpmpid = start_swtpm($storecfg, $vmid, $tpm, $migratedfrom);
5641 }
5642
3898a563 5643 my $exitcode = run_command($cmd, %run_params);
f9dde219 5644 if ($exitcode) {
23bee97d
FE
5645 if ($tpmpid) {
5646 warn "stopping swtpm instance (pid $tpmpid) due to QEMU startup error\n";
5647 kill 'TERM', $tpmpid;
5648 }
f9dde219
SR
5649 die "QEMU exited with code $exitcode\n";
5650 }
503308ed 5651 };
3898a563 5652 };
503308ed 5653
3898a563 5654 if ($conf->{hugepages}) {
7023f3ea 5655
3898a563
FG
5656 my $code = sub {
5657 my $hugepages_topology = PVE::QemuServer::Memory::hugepages_topology($conf);
5658 my $hugepages_host_topology = PVE::QemuServer::Memory::hugepages_host_topology();
7023f3ea 5659
3898a563
FG
5660 PVE::QemuServer::Memory::hugepages_mount();
5661 PVE::QemuServer::Memory::hugepages_allocate($hugepages_topology, $hugepages_host_topology);
7023f3ea 5662
503308ed 5663 eval { $run_qemu->() };
3898a563 5664 if (my $err = $@) {
f36e9894
SR
5665 PVE::QemuServer::Memory::hugepages_reset($hugepages_host_topology)
5666 if !$conf->{keephugepages};
3898a563
FG
5667 die $err;
5668 }
77cde36b 5669
f36e9894
SR
5670 PVE::QemuServer::Memory::hugepages_pre_deallocate($hugepages_topology)
5671 if !$conf->{keephugepages};
3898a563
FG
5672 };
5673 eval { PVE::QemuServer::Memory::hugepages_update_locked($code); };
1e3baf05 5674
3898a563
FG
5675 } else {
5676 eval { $run_qemu->() };
5677 }
afdb31d5 5678
3898a563
FG
5679 if (my $err = $@) {
5680 # deactivate volumes if start fails
5681 eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); };
1fb1822e
DC
5682 eval { PVE::QemuServer::PCI::remove_pci_reservation($pci_id_list) };
5683
3898a563
FG
5684 die "start failed: $err";
5685 }
62de2cbd 5686
1fb1822e
DC
5687 # re-reserve all PCI IDs now that we can know the actual VM PID
5688 my $pid = PVE::QemuServer::Helpers::vm_running_locally($vmid);
5689 eval { PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, undef, $pid) };
5690 warn $@ if $@;
5691
3898a563 5692 print "migration listens on $migrate_uri\n" if $migrate_uri;
84da8217 5693 $res->{migrate_uri} = $migrate_uri;
eb8cddb5 5694
3898a563
FG
5695 if ($statefile && $statefile ne 'tcp' && $statefile ne 'unix') {
5696 eval { mon_cmd($vmid, "cont"); };
5697 warn $@ if $@;
5698 }
2189246c 5699
3898a563 5700 #start nbd server for storage migration
13cfe3b7 5701 if (my $nbd = $migrate_opts->{nbd}) {
3898a563 5702 my $nbd_protocol_version = $migrate_opts->{nbd_proto_version} // 0;
2189246c 5703
3898a563
FG
5704 my $migrate_storage_uri;
5705 # nbd_protocol_version > 0 for unix socket support
5706 if ($nbd_protocol_version > 0 && $migration_type eq 'secure') {
5707 my $socket_path = "/run/qemu-server/$vmid\_nbd.migrate";
5708 mon_cmd($vmid, "nbd-server-start", addr => { type => 'unix', data => { path => $socket_path } } );
5709 $migrate_storage_uri = "nbd:unix:$socket_path";
5710 } else {
5711 my $nodename = nodename();
5712 my $localip = $get_migration_ip->($nodename);
5713 my $pfamily = PVE::Tools::get_host_address_family($nodename);
5714 my $storage_migrate_port = PVE::Tools::next_migrate_port($pfamily);
5715
4df98f2f
TL
5716 mon_cmd($vmid, "nbd-server-start", addr => {
5717 type => 'inet',
5718 data => {
5719 host => "${localip}",
5720 port => "${storage_migrate_port}",
5721 },
5722 });
3898a563
FG
5723 $localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
5724 $migrate_storage_uri = "nbd:${localip}:${storage_migrate_port}";
2189246c
AD
5725 }
5726
84da8217
FG
5727 $res->{migrate_storage_uri} = $migrate_storage_uri;
5728
13cfe3b7 5729 foreach my $opt (sort keys %$nbd) {
ba5396b5
FG
5730 my $drivestr = $nbd->{$opt}->{drivestr};
5731 my $volid = $nbd->{$opt}->{volid};
3898a563 5732 mon_cmd($vmid, "nbd-server-add", device => "drive-$opt", writable => JSON::true );
84da8217
FG
5733 my $nbd_uri = "$migrate_storage_uri:exportname=drive-$opt";
5734 print "storage migration listens on $nbd_uri volume:$drivestr\n";
ba5396b5
FG
5735 print "re-using replicated volume: $opt - $volid\n"
5736 if $nbd->{$opt}->{replicated};
84da8217
FG
5737
5738 $res->{drives}->{$opt} = $nbd->{$opt};
5739 $res->{drives}->{$opt}->{nbd_uri} = $nbd_uri;
3898a563
FG
5740 }
5741 }
a89fded1 5742
3898a563
FG
5743 if ($migratedfrom) {
5744 eval {
5745 set_migration_caps($vmid);
5746 };
5747 warn $@ if $@;
5748
5749 if ($spice_port) {
5750 print "spice listens on port $spice_port\n";
84da8217 5751 $res->{spice_port} = $spice_port;
3898a563 5752 if ($migrate_opts->{spice_ticket}) {
4df98f2f
TL
5753 mon_cmd($vmid, "set_password", protocol => 'spice', password =>
5754 $migrate_opts->{spice_ticket});
3898a563 5755 mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
95a4b4a9 5756 }
3898a563 5757 }
95a4b4a9 5758
3898a563
FG
5759 } else {
5760 mon_cmd($vmid, "balloon", value => $conf->{balloon}*1024*1024)
5761 if !$statefile && $conf->{balloon};
25088687 5762
3898a563
FG
5763 foreach my $opt (keys %$conf) {
5764 next if $opt !~ m/^net\d+$/;
5765 my $nicconf = parse_net($conf->{$opt});
5766 qemu_set_link_status($vmid, $opt, 0) if $nicconf->{link_down};
e18b0b99 5767 }
3898a563 5768 }
a1b7d579 5769
3898a563
FG
5770 mon_cmd($vmid, 'qom-set',
5771 path => "machine/peripheral/balloon0",
5772 property => "guest-stats-polling-interval",
5773 value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
eb065317 5774
3898a563
FG
5775 if ($resume) {
5776 print "Resumed VM, removing state\n";
5777 if (my $vmstate = $conf->{vmstate}) {
5778 PVE::Storage::deactivate_volumes($storecfg, [$vmstate]);
5779 PVE::Storage::vdisk_free($storecfg, $vmstate);
7ceade4c 5780 }
ea1c2110 5781 delete $conf->@{qw(lock vmstate runningmachine runningcpu)};
3898a563
FG
5782 PVE::QemuConfig->write_config($vmid, $conf);
5783 }
7ceade4c 5784
3898a563 5785 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
84da8217
FG
5786
5787 return $res;
1e3baf05
DM
5788}
5789
1e3baf05 5790sub vm_commandline {
b14477e7 5791 my ($storecfg, $vmid, $snapname) = @_;
1e3baf05 5792
ffda963f 5793 my $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 5794
e8a26810 5795 my ($forcemachine, $forcecpu);
b14477e7
RV
5796 if ($snapname) {
5797 my $snapshot = $conf->{snapshots}->{$snapname};
87d92707
TL
5798 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
5799
ea1c2110
SR
5800 # check for machine or CPU overrides in snapshot
5801 $forcemachine = $snapshot->{runningmachine};
5802 $forcecpu = $snapshot->{runningcpu};
092868c4 5803
87d92707 5804 $snapshot->{digest} = $conf->{digest}; # keep file digest for API
b14477e7 5805
b14477e7
RV
5806 $conf = $snapshot;
5807 }
5808
1e3baf05
DM
5809 my $defaults = load_defaults();
5810
e8a26810 5811 my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu);
1e3baf05 5812
5930c1ff 5813 return PVE::Tools::cmd2string($cmd);
1e3baf05
DM
5814}
5815
5816sub vm_reset {
5817 my ($vmid, $skiplock) = @_;
5818
ffda963f 5819 PVE::QemuConfig->lock_config($vmid, sub {
1e3baf05 5820
ffda963f 5821 my $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 5822
ffda963f 5823 PVE::QemuConfig->check_lock($conf) if !$skiplock;
1e3baf05 5824
0a13e08e 5825 mon_cmd($vmid, "system_reset");
ff1a2432
DM
5826 });
5827}
5828
5829sub get_vm_volumes {
5830 my ($conf) = @_;
1e3baf05 5831
ff1a2432 5832 my $vollist = [];
d5769dc2 5833 foreach_volid($conf, sub {
392f8b5d 5834 my ($volid, $attr) = @_;
ff1a2432 5835
d5769dc2 5836 return if $volid =~ m|^/|;
ff1a2432 5837
d5769dc2
DM
5838 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
5839 return if !$sid;
ff1a2432
DM
5840
5841 push @$vollist, $volid;
1e3baf05 5842 });
ff1a2432
DM
5843
5844 return $vollist;
5845}
5846
5847sub vm_stop_cleanup {
70b04821 5848 my ($storecfg, $vmid, $conf, $keepActive, $apply_pending_changes) = @_;
ff1a2432 5849
745fed70 5850 eval {
ff1a2432 5851
254575e9
DM
5852 if (!$keepActive) {
5853 my $vollist = get_vm_volumes($conf);
5854 PVE::Storage::deactivate_volumes($storecfg, $vollist);
f9dde219
SR
5855
5856 if (my $tpmdrive = $conf->{tpmstate0}) {
5857 my $tpm = parse_drive("tpmstate0", $tpmdrive);
5858 my ($storeid, $volname) = PVE::Storage::parse_volume_id($tpm->{file}, 1);
5859 if ($storeid) {
5860 PVE::Storage::unmap_volume($storecfg, $tpm->{file});
5861 }
5862 }
254575e9 5863 }
a1b7d579 5864
ab6a046f 5865 foreach my $ext (qw(mon qmp pid vnc qga)) {
961bfcb2
DM
5866 unlink "/var/run/qemu-server/${vmid}.$ext";
5867 }
a1b7d579 5868
6dbcb073 5869 if ($conf->{ivshmem}) {
4df98f2f 5870 my $ivshmem = parse_property_string($ivshmem_fmt, $conf->{ivshmem});
4c5a6a24
TL
5871 # just delete it for now, VMs which have this already open do not
5872 # are affected, but new VMs will get a separated one. If this
5873 # becomes an issue we either add some sort of ref-counting or just
5874 # add a "don't delete on stop" flag to the ivshmem format.
6dbcb073
DC
5875 unlink '/dev/shm/pve-shm-' . ($ivshmem->{name} // $vmid);
5876 }
5877
1fb1822e 5878 my $ids = [];
6ab45bd7
DC
5879 foreach my $key (keys %$conf) {
5880 next if $key !~ m/^hostpci(\d+)$/;
5881 my $hostpciindex = $1;
5882 my $d = parse_hostpci($conf->{$key});
5883 my $uuid = PVE::SysFSTools::generate_mdev_uuid($vmid, $hostpciindex);
5884
5885 foreach my $pci (@{$d->{pciid}}) {
2fd24788 5886 my $pciid = $pci->{id};
1fb1822e 5887 push @$ids, $pci->{id};
6ab45bd7
DC
5888 PVE::SysFSTools::pci_cleanup_mdev_device($pciid, $uuid);
5889 }
5890 }
1fb1822e 5891 PVE::QemuServer::PCI::remove_pci_reservation($ids);
6ab45bd7 5892
70b04821 5893 vmconfig_apply_pending($vmid, $conf, $storecfg) if $apply_pending_changes;
745fed70
DM
5894 };
5895 warn $@ if $@; # avoid errors - just warn
1e3baf05
DM
5896}
5897
575d19da
DC
5898# call only in locked context
5899sub _do_vm_stop {
5900 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive) = @_;
9269013a 5901
575d19da
DC
5902 my $pid = check_running($vmid, $nocheck);
5903 return if !$pid;
1e3baf05 5904
575d19da
DC
5905 my $conf;
5906 if (!$nocheck) {
5907 $conf = PVE::QemuConfig->load_config($vmid);
5908 PVE::QemuConfig->check_lock($conf) if !$skiplock;
5909 if (!defined($timeout) && $shutdown && $conf->{startup}) {
5910 my $opts = PVE::JSONSchema::pve_parse_startup_order($conf->{startup});
5911 $timeout = $opts->{down} if $opts->{down};
e6c3b671 5912 }
575d19da
DC
5913 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-stop');
5914 }
19672434 5915
575d19da
DC
5916 eval {
5917 if ($shutdown) {
a2af1bbe 5918 if (defined($conf) && get_qga_key($conf, 'enabled')) {
0a13e08e 5919 mon_cmd($vmid, "guest-shutdown", timeout => $timeout);
9269013a 5920 } else {
0a13e08e 5921 mon_cmd($vmid, "system_powerdown");
1e3baf05
DM
5922 }
5923 } else {
0a13e08e 5924 mon_cmd($vmid, "quit");
1e3baf05 5925 }
575d19da
DC
5926 };
5927 my $err = $@;
1e3baf05 5928
575d19da
DC
5929 if (!$err) {
5930 $timeout = 60 if !defined($timeout);
1e3baf05
DM
5931
5932 my $count = 0;
e6c3b671 5933 while (($count < $timeout) && check_running($vmid, $nocheck)) {
1e3baf05
DM
5934 $count++;
5935 sleep 1;
5936 }
5937
5938 if ($count >= $timeout) {
575d19da
DC
5939 if ($force) {
5940 warn "VM still running - terminating now with SIGTERM\n";
5941 kill 15, $pid;
5942 } else {
5943 die "VM quit/powerdown failed - got timeout\n";
5944 }
5945 } else {
5946 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
5947 return;
1e3baf05 5948 }
575d19da 5949 } else {
d60cbb97
TL
5950 if (!check_running($vmid, $nocheck)) {
5951 warn "Unexpected: VM shutdown command failed, but VM not running anymore..\n";
5952 return;
5953 }
5954 if ($force) {
575d19da
DC
5955 warn "VM quit/powerdown failed - terminating now with SIGTERM\n";
5956 kill 15, $pid;
5957 } else {
5958 die "VM quit/powerdown failed\n";
5959 }
5960 }
5961
5962 # wait again
5963 $timeout = 10;
5964
5965 my $count = 0;
5966 while (($count < $timeout) && check_running($vmid, $nocheck)) {
5967 $count++;
5968 sleep 1;
5969 }
5970
5971 if ($count >= $timeout) {
5972 warn "VM still running - terminating now with SIGKILL\n";
5973 kill 9, $pid;
5974 sleep 1;
5975 }
1e3baf05 5976
575d19da
DC
5977 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
5978}
5979
5980# Note: use $nocheck to skip tests if VM configuration file exists.
5981# We need that when migration VMs to other nodes (files already moved)
5982# Note: we set $keepActive in vzdump stop mode - volumes need to stay active
5983sub vm_stop {
5984 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
5985
5986 $force = 1 if !defined($force) && !$shutdown;
5987
5988 if ($migratedfrom){
5989 my $pid = check_running($vmid, $nocheck, $migratedfrom);
5990 kill 15, $pid if $pid;
5991 my $conf = PVE::QemuConfig->load_config($vmid, $migratedfrom);
5992 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive, 0);
5993 return;
5994 }
5995
5996 PVE::QemuConfig->lock_config($vmid, sub {
5997 _do_vm_stop($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive);
ff1a2432 5998 });
1e3baf05
DM
5999}
6000
165411f0
DC
6001sub vm_reboot {
6002 my ($vmid, $timeout) = @_;
6003
6004 PVE::QemuConfig->lock_config($vmid, sub {
66026117 6005 eval {
165411f0 6006
66026117
OB
6007 # only reboot if running, as qmeventd starts it again on a stop event
6008 return if !check_running($vmid);
165411f0 6009
66026117 6010 create_reboot_request($vmid);
165411f0 6011
66026117
OB
6012 my $storecfg = PVE::Storage::config();
6013 _do_vm_stop($storecfg, $vmid, undef, undef, $timeout, 1);
165411f0 6014
66026117
OB
6015 };
6016 if (my $err = $@) {
3c1c3fe6 6017 # avoid that the next normal shutdown will be confused for a reboot
66026117
OB
6018 clear_reboot_request($vmid);
6019 die $err;
6020 }
165411f0
DC
6021 });
6022}
6023
75c24bba 6024# note: if using the statestorage parameter, the caller has to check privileges
1e3baf05 6025sub vm_suspend {
48b4cdc2 6026 my ($vmid, $skiplock, $includestate, $statestorage) = @_;
159719e5
DC
6027
6028 my $conf;
6029 my $path;
6030 my $storecfg;
6031 my $vmstate;
1e3baf05 6032
ffda963f 6033 PVE::QemuConfig->lock_config($vmid, sub {
1e3baf05 6034
159719e5 6035 $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 6036
159719e5 6037 my $is_backing_up = PVE::QemuConfig->has_lock($conf, 'backup');
e79706d4 6038 PVE::QemuConfig->check_lock($conf)
159719e5
DC
6039 if !($skiplock || $is_backing_up);
6040
6041 die "cannot suspend to disk during backup\n"
6042 if $is_backing_up && $includestate;
bcb7c9cf 6043
159719e5
DC
6044 if ($includestate) {
6045 $conf->{lock} = 'suspending';
6046 my $date = strftime("%Y-%m-%d", localtime(time()));
6047 $storecfg = PVE::Storage::config();
75c24bba
DC
6048 if (!$statestorage) {
6049 $statestorage = find_vmstate_storage($conf, $storecfg);
6050 # check permissions for the storage
6051 my $rpcenv = PVE::RPCEnvironment::get();
6052 if ($rpcenv->{type} ne 'cli') {
6053 my $authuser = $rpcenv->get_user();
6054 $rpcenv->check($authuser, "/storage/$statestorage", ['Datastore.AllocateSpace']);
6055 }
6056 }
6057
6058
4df98f2f
TL
6059 $vmstate = PVE::QemuConfig->__snapshot_save_vmstate(
6060 $vmid, $conf, "suspend-$date", $storecfg, $statestorage, 1);
159719e5
DC
6061 $path = PVE::Storage::path($storecfg, $vmstate);
6062 PVE::QemuConfig->write_config($vmid, $conf);
6063 } else {
0a13e08e 6064 mon_cmd($vmid, "stop");
159719e5 6065 }
1e3baf05 6066 });
159719e5
DC
6067
6068 if ($includestate) {
6069 # save vm state
6070 PVE::Storage::activate_volumes($storecfg, [$vmstate]);
6071
6072 eval {
27a5be53 6073 set_migration_caps($vmid, 1);
0a13e08e 6074 mon_cmd($vmid, "savevm-start", statefile => $path);
159719e5 6075 for(;;) {
0a13e08e 6076 my $state = mon_cmd($vmid, "query-savevm");
159719e5
DC
6077 if (!$state->{status}) {
6078 die "savevm not active\n";
6079 } elsif ($state->{status} eq 'active') {
6080 sleep(1);
6081 next;
6082 } elsif ($state->{status} eq 'completed') {
b0a9a385 6083 print "State saved, quitting\n";
159719e5
DC
6084 last;
6085 } elsif ($state->{status} eq 'failed' && $state->{error}) {
6086 die "query-savevm failed with error '$state->{error}'\n"
6087 } else {
6088 die "query-savevm returned status '$state->{status}'\n";
6089 }
6090 }
6091 };
6092 my $err = $@;
6093
6094 PVE::QemuConfig->lock_config($vmid, sub {
6095 $conf = PVE::QemuConfig->load_config($vmid);
6096 if ($err) {
6097 # cleanup, but leave suspending lock, to indicate something went wrong
6098 eval {
0a13e08e 6099 mon_cmd($vmid, "savevm-end");
159719e5
DC
6100 PVE::Storage::deactivate_volumes($storecfg, [$vmstate]);
6101 PVE::Storage::vdisk_free($storecfg, $vmstate);
ea1c2110 6102 delete $conf->@{qw(vmstate runningmachine runningcpu)};
159719e5
DC
6103 PVE::QemuConfig->write_config($vmid, $conf);
6104 };
6105 warn $@ if $@;
6106 die $err;
6107 }
6108
6109 die "lock changed unexpectedly\n"
6110 if !PVE::QemuConfig->has_lock($conf, 'suspending');
6111
0a13e08e 6112 mon_cmd($vmid, "quit");
159719e5
DC
6113 $conf->{lock} = 'suspended';
6114 PVE::QemuConfig->write_config($vmid, $conf);
6115 });
6116 }
1e3baf05
DM
6117}
6118
6119sub vm_resume {
289e0b85 6120 my ($vmid, $skiplock, $nocheck) = @_;
1e3baf05 6121
ffda963f 6122 PVE::QemuConfig->lock_config($vmid, sub {
0a13e08e 6123 my $res = mon_cmd($vmid, 'query-status');
c2786bed 6124 my $resume_cmd = 'cont';
8e0c97bb 6125 my $reset = 0;
c2786bed 6126
8e0c97bb
SR
6127 if ($res->{status}) {
6128 return if $res->{status} eq 'running'; # job done, go home
6129 $resume_cmd = 'system_wakeup' if $res->{status} eq 'suspended';
6130 $reset = 1 if $res->{status} eq 'shutdown';
c2786bed
DC
6131 }
6132
289e0b85 6133 if (!$nocheck) {
1e3baf05 6134
ffda963f 6135 my $conf = PVE::QemuConfig->load_config($vmid);
1e3baf05 6136
e79706d4
FG
6137 PVE::QemuConfig->check_lock($conf)
6138 if !($skiplock || PVE::QemuConfig->has_lock($conf, 'backup'));
289e0b85 6139 }
3e24733b 6140
8e0c97bb
SR
6141 if ($reset) {
6142 # required if a VM shuts down during a backup and we get a resume
6143 # request before the backup finishes for example
6144 mon_cmd($vmid, "system_reset");
6145 }
0a13e08e 6146 mon_cmd($vmid, $resume_cmd);
1e3baf05
DM
6147 });
6148}
6149
5fdbe4f0
DM
6150sub vm_sendkey {
6151 my ($vmid, $skiplock, $key) = @_;
1e3baf05 6152
ffda963f 6153 PVE::QemuConfig->lock_config($vmid, sub {
1e3baf05 6154
ffda963f 6155 my $conf = PVE::QemuConfig->load_config($vmid);
f5eb281a 6156
7b7c6d1b 6157 # there is no qmp command, so we use the human monitor command
0a13e08e 6158 my $res = PVE::QemuServer::Monitor::hmp_cmd($vmid, "sendkey $key");
d30820d6 6159 die $res if $res ne '';
1e3baf05
DM
6160 });
6161}
6162
3e16d5fc
DM
6163# vzdump restore implementaion
6164
ed221350 6165sub tar_archive_read_firstfile {
3e16d5fc 6166 my $archive = shift;
afdb31d5 6167
3e16d5fc
DM
6168 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
6169
6170 # try to detect archive type first
387ba257 6171 my $pid = open (my $fh, '-|', 'tar', 'tf', $archive) ||
3e16d5fc 6172 die "unable to open file '$archive'\n";
387ba257 6173 my $firstfile = <$fh>;
3e16d5fc 6174 kill 15, $pid;
387ba257 6175 close $fh;
3e16d5fc
DM
6176
6177 die "ERROR: archive contaions no data\n" if !$firstfile;
6178 chomp $firstfile;
6179
6180 return $firstfile;
6181}
6182
ed221350
DM
6183sub tar_restore_cleanup {
6184 my ($storecfg, $statfile) = @_;
3e16d5fc
DM
6185
6186 print STDERR "starting cleanup\n";
6187
6188 if (my $fd = IO::File->new($statfile, "r")) {
6189 while (defined(my $line = <$fd>)) {
6190 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
6191 my $volid = $2;
6192 eval {
6193 if ($volid =~ m|^/|) {
6194 unlink $volid || die 'unlink failed\n';
6195 } else {
ed221350 6196 PVE::Storage::vdisk_free($storecfg, $volid);
3e16d5fc 6197 }
afdb31d5 6198 print STDERR "temporary volume '$volid' sucessfuly removed\n";
3e16d5fc
DM
6199 };
6200 print STDERR "unable to cleanup '$volid' - $@" if $@;
6201 } else {
6202 print STDERR "unable to parse line in statfile - $line";
afdb31d5 6203 }
3e16d5fc
DM
6204 }
6205 $fd->close();
6206 }
6207}
6208
d1e92cf6 6209sub restore_file_archive {
a0d1b1a2 6210 my ($archive, $vmid, $user, $opts) = @_;
3e16d5fc 6211
a2ec5a67
FG
6212 return restore_vma_archive($archive, $vmid, $user, $opts)
6213 if $archive eq '-';
6214
c6d51783
AA
6215 my $info = PVE::Storage::archive_info($archive);
6216 my $format = $opts->{format} // $info->{format};
6217 my $comp = $info->{compression};
91bd6c90
DM
6218
6219 # try to detect archive format
6220 if ($format eq 'tar') {
6221 return restore_tar_archive($archive, $vmid, $user, $opts);
6222 } else {
6223 return restore_vma_archive($archive, $vmid, $user, $opts, $comp);
6224 }
6225}
6226
d1e92cf6
DM
6227# hepler to remove disks that will not be used after restore
6228my $restore_cleanup_oldconf = sub {
6229 my ($storecfg, $vmid, $oldconf, $virtdev_hash) = @_;
6230
912792e2 6231 PVE::QemuConfig->foreach_volume($oldconf, sub {
d1e92cf6
DM
6232 my ($ds, $drive) = @_;
6233
6234 return if drive_is_cdrom($drive, 1);
6235
6236 my $volid = $drive->{file};
6237 return if !$volid || $volid =~ m|^/|;
6238
6239 my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
6240 return if !$path || !$owner || ($owner != $vmid);
6241
6242 # Note: only delete disk we want to restore
6243 # other volumes will become unused
6244 if ($virtdev_hash->{$ds}) {
6245 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
6246 if (my $err = $@) {
6247 warn $err;
6248 }
6249 }
6250 });
6251
6252 # delete vmstate files, after the restore we have no snapshots anymore
6253 foreach my $snapname (keys %{$oldconf->{snapshots}}) {
6254 my $snap = $oldconf->{snapshots}->{$snapname};
6255 if ($snap->{vmstate}) {
6256 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
6257 if (my $err = $@) {
6258 warn $err;
6259 }
6260 }
6261 }
6262};
6263
9f3d73bc
DM
6264# Helper to parse vzdump backup device hints
6265#
6266# $rpcenv: Environment, used to ckeck storage permissions
6267# $user: User ID, to check storage permissions
6268# $storecfg: Storage configuration
6269# $fh: the file handle for reading the configuration
6270# $devinfo: should contain device sizes for all backu-up'ed devices
6271# $options: backup options (pool, default storage)
6272#
6273# Return: $virtdev_hash, updates $devinfo (add devname, virtdev, format, storeid)
6274my $parse_backup_hints = sub {
6275 my ($rpcenv, $user, $storecfg, $fh, $devinfo, $options) = @_;
d1e92cf6 6276
36d4bdcb
TL
6277 my $check_storage = sub { # assert if an image can be allocate
6278 my ($storeid, $scfg) = @_;
6279 die "Content type 'images' is not available on storage '$storeid'\n"
6280 if !$scfg->{content}->{images};
6281 $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
6282 if $user ne 'root@pam';
6283 };
d1e92cf6 6284
36d4bdcb 6285 my $virtdev_hash = {};
9f3d73bc
DM
6286 while (defined(my $line = <$fh>)) {
6287 if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
6288 my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
6289 die "archive does not contain data for drive '$virtdev'\n"
6290 if !$devinfo->{$devname};
6291
6292 if (defined($options->{storage})) {
6293 $storeid = $options->{storage} || 'local';
6294 } elsif (!$storeid) {
6295 $storeid = 'local';
d1e92cf6 6296 }
9f3d73bc
DM
6297 $format = 'raw' if !$format;
6298 $devinfo->{$devname}->{devname} = $devname;
6299 $devinfo->{$devname}->{virtdev} = $virtdev;
6300 $devinfo->{$devname}->{format} = $format;
6301 $devinfo->{$devname}->{storeid} = $storeid;
6302
62af60cd 6303 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
36d4bdcb 6304 $check_storage->($storeid, $scfg); # permission and content type check
d1e92cf6 6305
9f3d73bc
DM
6306 $virtdev_hash->{$virtdev} = $devinfo->{$devname};
6307 } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
6308 my $virtdev = $1;
6309 my $drive = parse_drive($virtdev, $2);
36d4bdcb 6310
9f3d73bc
DM
6311 if (drive_is_cloudinit($drive)) {
6312 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
5364990d
TL
6313 $storeid = $options->{storage} if defined ($options->{storage});
6314 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
9f3d73bc 6315 my $format = qemu_img_format($scfg, $volname); # has 'raw' fallback
d1e92cf6 6316
36d4bdcb 6317 $check_storage->($storeid, $scfg); # permission and content type check
9f8ba326 6318
9f3d73bc
DM
6319 $virtdev_hash->{$virtdev} = {
6320 format => $format,
5364990d 6321 storeid => $storeid,
9f3d73bc
DM
6322 size => PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE,
6323 is_cloudinit => 1,
6324 };
d1e92cf6 6325 }
9f3d73bc
DM
6326 }
6327 }
d1e92cf6 6328
9f3d73bc
DM
6329 return $virtdev_hash;
6330};
d1e92cf6 6331
9f3d73bc
DM
6332# Helper to allocate and activate all volumes required for a restore
6333#
6334# $storecfg: Storage configuration
6335# $virtdev_hash: as returned by parse_backup_hints()
6336#
6337# Returns: { $virtdev => $volid }
6338my $restore_allocate_devices = sub {
6339 my ($storecfg, $virtdev_hash, $vmid) = @_;
d1e92cf6 6340
9f3d73bc
DM
6341 my $map = {};
6342 foreach my $virtdev (sort keys %$virtdev_hash) {
6343 my $d = $virtdev_hash->{$virtdev};
6344 my $alloc_size = int(($d->{size} + 1024 - 1)/1024);
6345 my $storeid = $d->{storeid};
6346 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
d1e92cf6 6347
9f3d73bc
DM
6348 # test if requested format is supported
6349 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
6350 my $supported = grep { $_ eq $d->{format} } @$validFormats;
6351 $d->{format} = $defFormat if !$supported;
d1e92cf6 6352
9f3d73bc
DM
6353 my $name;
6354 if ($d->{is_cloudinit}) {
6355 $name = "vm-$vmid-cloudinit";
c997e24a
ML
6356 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
6357 if ($scfg->{path}) {
6358 $name .= ".$d->{format}";
6359 }
d1e92cf6
DM
6360 }
6361
4df98f2f
TL
6362 my $volid = PVE::Storage::vdisk_alloc(
6363 $storecfg, $storeid, $vmid, $d->{format}, $name, $alloc_size);
d1e92cf6 6364
9f3d73bc
DM
6365 print STDERR "new volume ID is '$volid'\n";
6366 $d->{volid} = $volid;
d1e92cf6 6367
9f3d73bc 6368 PVE::Storage::activate_volumes($storecfg, [$volid]);
d1e92cf6 6369
9f3d73bc 6370 $map->{$virtdev} = $volid;
d1e92cf6
DM
6371 }
6372
9f3d73bc
DM
6373 return $map;
6374};
d1e92cf6 6375
c62d7cf5 6376sub restore_update_config_line {
eabac302 6377 my ($cookie, $map, $line, $unique) = @_;
91bd6c90 6378
98a4b3fb
FE
6379 return '' if $line =~ m/^\#qmdump\#/;
6380 return '' if $line =~ m/^\#vzdump\#/;
6381 return '' if $line =~ m/^lock:/;
6382 return '' if $line =~ m/^unused\d+:/;
6383 return '' if $line =~ m/^parent:/;
6384
6385 my $res = '';
91bd6c90 6386
b5b99790 6387 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
91bd6c90
DM
6388 if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
6389 # try to convert old 1.X settings
6390 my ($id, $ind, $ethcfg) = ($1, $2, $3);
6391 foreach my $devconfig (PVE::Tools::split_list($ethcfg)) {
6392 my ($model, $macaddr) = split(/\=/, $devconfig);
b5b99790 6393 $macaddr = PVE::Tools::random_ether_addr($dc->{mac_prefix}) if !$macaddr || $unique;
91bd6c90
DM
6394 my $net = {
6395 model => $model,
6396 bridge => "vmbr$ind",
6397 macaddr => $macaddr,
6398 };
6399 my $netstr = print_net($net);
6400
98a4b3fb 6401 $res .= "net$cookie->{netcount}: $netstr\n";
91bd6c90
DM
6402 $cookie->{netcount}++;
6403 }
6404 } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && $unique) {
6405 my ($id, $netstr) = ($1, $2);
6406 my $net = parse_net($netstr);
b5b99790 6407 $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}) if $net->{macaddr};
91bd6c90 6408 $netstr = print_net($net);
98a4b3fb 6409 $res .= "$id: $netstr\n";
f9dde219 6410 } elsif ($line =~ m/^((ide|scsi|virtio|sata|efidisk|tpmstate)\d+):\s*(\S+)\s*$/) {
91bd6c90 6411 my $virtdev = $1;
907ea891 6412 my $value = $3;
d9faf790
WB
6413 my $di = parse_drive($virtdev, $value);
6414 if (defined($di->{backup}) && !$di->{backup}) {
98a4b3fb 6415 $res .= "#$line";
c0f7406e 6416 } elsif ($map->{$virtdev}) {
8fd57431 6417 delete $di->{format}; # format can change on restore
91bd6c90 6418 $di->{file} = $map->{$virtdev};
71c58bb7 6419 $value = print_drive($di);
98a4b3fb 6420 $res .= "$virtdev: $value\n";
91bd6c90 6421 } else {
98a4b3fb 6422 $res .= $line;
91bd6c90 6423 }
1a0c2f03 6424 } elsif (($line =~ m/^vmgenid: (.*)/)) {
babecffe 6425 my $vmgenid = $1;
6ee499ff 6426 if ($vmgenid ne '0') {
1a0c2f03 6427 # always generate a new vmgenid if there was a valid one setup
6ee499ff
DC
6428 $vmgenid = generate_uuid();
6429 }
98a4b3fb 6430 $res .= "vmgenid: $vmgenid\n";
19a5dd55
WL
6431 } elsif (($line =~ m/^(smbios1: )(.*)/) && $unique) {
6432 my ($uuid, $uuid_str);
6433 UUID::generate($uuid);
6434 UUID::unparse($uuid, $uuid_str);
6435 my $smbios1 = parse_smbios1($2);
6436 $smbios1->{uuid} = $uuid_str;
98a4b3fb 6437 $res .= $1.print_smbios1($smbios1)."\n";
91bd6c90 6438 } else {
98a4b3fb 6439 $res .= $line;
91bd6c90 6440 }
98a4b3fb
FE
6441
6442 return $res;
c62d7cf5 6443}
9f3d73bc
DM
6444
6445my $restore_deactivate_volumes = sub {
6446 my ($storecfg, $devinfo) = @_;
6447
6448 my $vollist = [];
6449 foreach my $devname (keys %$devinfo) {
6450 my $volid = $devinfo->{$devname}->{volid};
6451 push @$vollist, $volid if $volid;
6452 }
6453
6454 PVE::Storage::deactivate_volumes($storecfg, $vollist);
6455};
6456
6457my $restore_destroy_volumes = sub {
6458 my ($storecfg, $devinfo) = @_;
6459
6460 foreach my $devname (keys %$devinfo) {
6461 my $volid = $devinfo->{$devname}->{volid};
6462 next if !$volid;
6463 eval {
6464 if ($volid =~ m|^/|) {
6465 unlink $volid || die 'unlink failed\n';
6466 } else {
6467 PVE::Storage::vdisk_free($storecfg, $volid);
6468 }
6469 print STDERR "temporary volume '$volid' sucessfuly removed\n";
6470 };
6471 print STDERR "unable to cleanup '$volid' - $@" if $@;
6472 }
6473};
91bd6c90
DM
6474
6475sub scan_volids {
9a8ba127 6476 my ($cfg, $vmid) = @_;
91bd6c90 6477
9a8ba127 6478 my $info = PVE::Storage::vdisk_list($cfg, undef, $vmid, undef, 'images');
91bd6c90
DM
6479
6480 my $volid_hash = {};
6481 foreach my $storeid (keys %$info) {
6482 foreach my $item (@{$info->{$storeid}}) {
6483 next if !($item->{volid} && $item->{size});
5996a936 6484 $item->{path} = PVE::Storage::path($cfg, $item->{volid});
91bd6c90
DM
6485 $volid_hash->{$item->{volid}} = $item;
6486 }
6487 }
6488
6489 return $volid_hash;
6490}
6491
68b108ee 6492sub update_disk_config {
91bd6c90 6493 my ($vmid, $conf, $volid_hash) = @_;
be190583 6494
91bd6c90 6495 my $changes;
9b29cbd0 6496 my $prefix = "VM $vmid";
91bd6c90 6497
c449137a
DC
6498 # used and unused disks
6499 my $referenced = {};
91bd6c90 6500
5996a936
DM
6501 # Note: it is allowed to define multiple storages with same path (alias), so
6502 # we need to check both 'volid' and real 'path' (two different volid can point
6503 # to the same path).
6504
c449137a 6505 my $referencedpath = {};
be190583 6506
91bd6c90 6507 # update size info
0c4fef3f 6508 PVE::QemuConfig->foreach_volume($conf, sub {
ca04977c 6509 my ($opt, $drive) = @_;
91bd6c90 6510
ca04977c
FE
6511 my $volid = $drive->{file};
6512 return if !$volid;
4df98f2f 6513 my $volume = $volid_hash->{$volid};
91bd6c90 6514
ca04977c
FE
6515 # mark volid as "in-use" for next step
6516 $referenced->{$volid} = 1;
4df98f2f 6517 if ($volume && (my $path = $volume->{path})) {
ca04977c 6518 $referencedpath->{$path} = 1;
91bd6c90 6519 }
ca04977c
FE
6520
6521 return if drive_is_cdrom($drive);
4df98f2f 6522 return if !$volume;
ca04977c 6523
4df98f2f 6524 my ($updated, $msg) = PVE::QemuServer::Drive::update_disksize($drive, $volume->{size});
ca04977c
FE
6525 if (defined($updated)) {
6526 $changes = 1;
6527 $conf->{$opt} = print_drive($updated);
9b29cbd0 6528 print "$prefix ($opt): $msg\n";
ca04977c
FE
6529 }
6530 });
91bd6c90 6531
5996a936 6532 # remove 'unusedX' entry if volume is used
ca04977c
FE
6533 PVE::QemuConfig->foreach_unused_volume($conf, sub {
6534 my ($opt, $drive) = @_;
6535
6536 my $volid = $drive->{file};
6537 return if !$volid;
6538
f7d1505b
TL
6539 my $path;
6540 $path = $volid_hash->{$volid}->{path} if $volid_hash->{$volid};
c449137a 6541 if ($referenced->{$volid} || ($path && $referencedpath->{$path})) {
68b108ee 6542 print "$prefix remove entry '$opt', its volume '$volid' is in use\n";
5996a936
DM
6543 $changes = 1;
6544 delete $conf->{$opt};
6545 }
c449137a
DC
6546
6547 $referenced->{$volid} = 1;
6548 $referencedpath->{$path} = 1 if $path;
ca04977c 6549 });
5996a936 6550
91bd6c90
DM
6551 foreach my $volid (sort keys %$volid_hash) {
6552 next if $volid =~ m/vm-$vmid-state-/;
c449137a 6553 next if $referenced->{$volid};
5996a936
DM
6554 my $path = $volid_hash->{$volid}->{path};
6555 next if !$path; # just to be sure
c449137a 6556 next if $referencedpath->{$path};
91bd6c90 6557 $changes = 1;
53b81297 6558 my $key = PVE::QemuConfig->add_unused_volume($conf, $volid);
68b108ee 6559 print "$prefix add unreferenced volume '$volid' as '$key' to config\n";
c449137a 6560 $referencedpath->{$path} = 1; # avoid to add more than once (aliases)
91bd6c90
DM
6561 }
6562
6563 return $changes;
6564}
6565
6566sub rescan {
9224dcee 6567 my ($vmid, $nolock, $dryrun) = @_;
91bd6c90 6568
20519efc 6569 my $cfg = PVE::Storage::config();
91bd6c90 6570
53b81297 6571 print "rescan volumes...\n";
9a8ba127 6572 my $volid_hash = scan_volids($cfg, $vmid);
91bd6c90
DM
6573
6574 my $updatefn = sub {
6575 my ($vmid) = @_;
6576
ffda963f 6577 my $conf = PVE::QemuConfig->load_config($vmid);
be190583 6578
ffda963f 6579 PVE::QemuConfig->check_lock($conf);
91bd6c90 6580
03da3f0d
DM
6581 my $vm_volids = {};
6582 foreach my $volid (keys %$volid_hash) {
6583 my $info = $volid_hash->{$volid};
6584 $vm_volids->{$volid} = $info if $info->{vmid} && $info->{vmid} == $vmid;
6585 }
6586
68b108ee 6587 my $changes = update_disk_config($vmid, $conf, $vm_volids);
91bd6c90 6588
9224dcee 6589 PVE::QemuConfig->write_config($vmid, $conf) if $changes && !$dryrun;
91bd6c90
DM
6590 };
6591
6592 if (defined($vmid)) {
6593 if ($nolock) {
6594 &$updatefn($vmid);
6595 } else {
ffda963f 6596 PVE::QemuConfig->lock_config($vmid, $updatefn, $vmid);
91bd6c90
DM
6597 }
6598 } else {
6599 my $vmlist = config_list();
6600 foreach my $vmid (keys %$vmlist) {
6601 if ($nolock) {
6602 &$updatefn($vmid);
6603 } else {
ffda963f 6604 PVE::QemuConfig->lock_config($vmid, $updatefn, $vmid);
be190583 6605 }
91bd6c90
DM
6606 }
6607 }
6608}
6609
9f3d73bc
DM
6610sub restore_proxmox_backup_archive {
6611 my ($archive, $vmid, $user, $options) = @_;
6612
6613 my $storecfg = PVE::Storage::config();
6614
6615 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
6616 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
6617
9f3d73bc 6618 my $fingerprint = $scfg->{fingerprint};
503e96f8 6619 my $keyfile = PVE::Storage::PBSPlugin::pbs_encryption_key_file_name($storecfg, $storeid);
9f3d73bc 6620
fbec3f89 6621 my $repo = PVE::PBSClient::get_repository($scfg);
dea4b04c 6622
26731a3c 6623 # This is only used for `pbs-restore` and the QEMU PBS driver (live-restore)
9f3d73bc
DM
6624 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $storeid);
6625 local $ENV{PBS_PASSWORD} = $password;
6626 local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
6627
6628 my ($vtype, $pbs_backup_name, undef, undef, undef, undef, $format) =
6629 PVE::Storage::parse_volname($storecfg, $archive);
6630
6631 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
6632
6633 die "got unexpected backup format '$format'\n" if $format ne 'pbs-vm';
6634
6635 my $tmpdir = "/var/tmp/vzdumptmp$$";
6636 rmtree $tmpdir;
6637 mkpath $tmpdir;
6638
6639 my $conffile = PVE::QemuConfig->config_file($vmid);
9f3d73bc
DM
6640 # disable interrupts (always do cleanups)
6641 local $SIG{INT} =
6642 local $SIG{TERM} =
6643 local $SIG{QUIT} =
6644 local $SIG{HUP} = sub { print STDERR "got interrupt - ignored\n"; };
6645
6646 # Note: $oldconf is undef if VM does not exists
6647 my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
6648 my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
98a4b3fb 6649 my $new_conf_raw = '';
9f3d73bc
DM
6650
6651 my $rpcenv = PVE::RPCEnvironment::get();
6652 my $devinfo = {};
6653
6654 eval {
6655 # enable interrupts
6656 local $SIG{INT} =
6657 local $SIG{TERM} =
6658 local $SIG{QUIT} =
6659 local $SIG{HUP} =
6660 local $SIG{PIPE} = sub { die "interrupted by signal\n"; };
6661
6662 my $cfgfn = "$tmpdir/qemu-server.conf";
6663 my $firewall_config_fn = "$tmpdir/fw.conf";
6664 my $index_fn = "$tmpdir/index.json";
6665
6666 my $cmd = "restore";
6667
6668 my $param = [$pbs_backup_name, "index.json", $index_fn];
6669 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
6670 my $index = PVE::Tools::file_get_contents($index_fn);
6671 $index = decode_json($index);
6672
6673 # print Dumper($index);
6674 foreach my $info (@{$index->{files}}) {
6675 if ($info->{filename} =~ m/^(drive-\S+).img.fidx$/) {
6676 my $devname = $1;
6677 if ($info->{size} =~ m/^(\d+)$/) { # untaint size
6678 $devinfo->{$devname}->{size} = $1;
6679 } else {
6680 die "unable to parse file size in 'index.json' - got '$info->{size}'\n";
6681 }
6682 }
6683 }
6684
4df98f2f
TL
6685 my $is_qemu_server_backup = scalar(
6686 grep { $_->{filename} eq 'qemu-server.conf.blob' } @{$index->{files}}
6687 );
9f3d73bc
DM
6688 if (!$is_qemu_server_backup) {
6689 die "backup does not look like a qemu-server backup (missing 'qemu-server.conf' file)\n";
6690 }
6691 my $has_firewall_config = scalar(grep { $_->{filename} eq 'fw.conf.blob' } @{$index->{files}});
6692
6693 $param = [$pbs_backup_name, "qemu-server.conf", $cfgfn];
6694 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
6695
6696 if ($has_firewall_config) {
6697 $param = [$pbs_backup_name, "fw.conf", $firewall_config_fn];
6698 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
6699
6700 my $pve_firewall_dir = '/etc/pve/firewall';
6701 mkdir $pve_firewall_dir; # make sure the dir exists
6702 PVE::Tools::file_copy($firewall_config_fn, "${pve_firewall_dir}/$vmid.fw");
6703 }
6704
6705 my $fh = IO::File->new($cfgfn, "r") ||
a1cbe55c 6706 die "unable to read qemu-server.conf - $!\n";
9f3d73bc
DM
6707
6708 my $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options);
6709
6710 # fixme: rate limit?
6711
6712 # create empty/temp config
6713 PVE::Tools::file_set_contents($conffile, "memory: 128\nlock: create");
6714
6715 $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash) if $oldconf;
6716
6717 # allocate volumes
6718 my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
6719
6f94e162
SR
6720 foreach my $virtdev (sort keys %$virtdev_hash) {
6721 my $d = $virtdev_hash->{$virtdev};
6722 next if $d->{is_cloudinit}; # no need to restore cloudinit
9f3d73bc 6723
55c7f9cf 6724 # this fails if storage is unavailable
6f94e162 6725 my $volid = $d->{volid};
6f94e162 6726 my $path = PVE::Storage::path($storecfg, $volid);
9f3d73bc 6727
f9dde219
SR
6728 # for live-restore we only want to preload the efidisk and TPM state
6729 next if $options->{live} && $virtdev ne 'efidisk0' && $virtdev ne 'tpmstate0';
55c7f9cf 6730
6f94e162
SR
6731 my $pbs_restore_cmd = [
6732 '/usr/bin/pbs-restore',
6733 '--repository', $repo,
6734 $pbs_backup_name,
6735 "$d->{devname}.img.fidx",
6736 $path,
6737 '--verbose',
6738 ];
55fb78aa 6739
6f94e162
SR
6740 push @$pbs_restore_cmd, '--format', $d->{format} if $d->{format};
6741 push @$pbs_restore_cmd, '--keyfile', $keyfile if -e $keyfile;
9f3d73bc 6742
6f94e162
SR
6743 if (PVE::Storage::volume_has_feature($storecfg, 'sparseinit', $volid)) {
6744 push @$pbs_restore_cmd, '--skip-zero';
26731a3c 6745 }
6f94e162
SR
6746
6747 my $dbg_cmdstring = PVE::Tools::cmd2string($pbs_restore_cmd);
6748 print "restore proxmox backup image: $dbg_cmdstring\n";
6749 run_command($pbs_restore_cmd);
9f3d73bc
DM
6750 }
6751
6752 $fh->seek(0, 0) || die "seek failed - $!\n";
6753
9f3d73bc
DM
6754 my $cookie = { netcount => 0 };
6755 while (defined(my $line = <$fh>)) {
c62d7cf5 6756 $new_conf_raw .= restore_update_config_line(
98a4b3fb 6757 $cookie,
98a4b3fb
FE
6758 $map,
6759 $line,
6760 $options->{unique},
6761 );
9f3d73bc
DM
6762 }
6763
6764 $fh->close();
9f3d73bc
DM
6765 };
6766 my $err = $@;
6767
26731a3c
SR
6768 if ($err || !$options->{live}) {
6769 $restore_deactivate_volumes->($storecfg, $devinfo);
6770 }
9f3d73bc
DM
6771
6772 rmtree $tmpdir;
6773
6774 if ($err) {
9f3d73bc
DM
6775 $restore_destroy_volumes->($storecfg, $devinfo);
6776 die $err;
6777 }
6778
f7551170
SR
6779 if ($options->{live}) {
6780 # keep lock during live-restore
6781 $new_conf_raw .= "\nlock: create";
6782 }
6783
98a4b3fb 6784 PVE::Tools::file_set_contents($conffile, $new_conf_raw);
9f3d73bc
DM
6785
6786 PVE::Cluster::cfs_update(); # make sure we read new file
6787
6788 eval { rescan($vmid, 1); };
6789 warn $@ if $@;
26731a3c
SR
6790
6791 PVE::AccessControl::add_vm_to_pool($vmid, $options->{pool}) if $options->{pool};
6792
6793 if ($options->{live}) {
fefd65a1
SR
6794 # enable interrupts
6795 local $SIG{INT} =
6796 local $SIG{TERM} =
6797 local $SIG{QUIT} =
6798 local $SIG{HUP} =
6799 local $SIG{PIPE} = sub { die "got signal ($!) - abort\n"; };
26731a3c 6800
fefd65a1
SR
6801 my $conf = PVE::QemuConfig->load_config($vmid);
6802 die "cannot do live-restore for template\n" if PVE::QemuConfig->is_template($conf);
26731a3c 6803
f9dde219
SR
6804 # these special drives are already restored before start
6805 delete $devinfo->{'drive-efidisk0'};
6806 delete $devinfo->{'drive-tpmstate0-backup'};
fefd65a1 6807 pbs_live_restore($vmid, $conf, $storecfg, $devinfo, $repo, $keyfile, $pbs_backup_name);
f7551170
SR
6808
6809 PVE::QemuConfig->remove_lock($vmid, "create");
26731a3c
SR
6810 }
6811}
6812
6813sub pbs_live_restore {
6814 my ($vmid, $conf, $storecfg, $restored_disks, $repo, $keyfile, $snap) = @_;
6815
88cabb62 6816 print "starting VM for live-restore\n";
daf829ec 6817 print "repository: '$repo', snapshot: '$snap'\n";
26731a3c
SR
6818
6819 my $pbs_backing = {};
8986e36e 6820 for my $ds (keys %$restored_disks) {
26731a3c 6821 $ds =~ m/^drive-(.*)$/;
88cabb62
SR
6822 my $confname = $1;
6823 $pbs_backing->{$confname} = {
26731a3c
SR
6824 repository => $repo,
6825 snapshot => $snap,
6826 archive => "$ds.img.fidx",
6827 };
88cabb62
SR
6828 $pbs_backing->{$confname}->{keyfile} = $keyfile if -e $keyfile;
6829
6830 my $drive = parse_drive($confname, $conf->{$confname});
6831 print "restoring '$ds' to '$drive->{file}'\n";
26731a3c
SR
6832 }
6833
fd70c843 6834 my $drives_streamed = 0;
26731a3c
SR
6835 eval {
6836 # make sure HA doesn't interrupt our restore by stopping the VM
6837 if (PVE::HA::Config::vm_is_ha_managed($vmid)) {
fd70c843 6838 run_command(['ha-manager', 'set', "vm:$vmid", '--state', 'started']);
26731a3c
SR
6839 }
6840
fd70c843
TL
6841 # start VM with backing chain pointing to PBS backup, environment vars for PBS driver
6842 # in QEMU (PBS_PASSWORD and PBS_FINGERPRINT) are already set by our caller
bfb12678 6843 vm_start_nolock($storecfg, $vmid, $conf, {paused => 1, 'pbs-backing' => $pbs_backing}, {});
26731a3c 6844
26697640
SR
6845 my $qmeventd_fd = register_qmeventd_handle($vmid);
6846
26731a3c
SR
6847 # begin streaming, i.e. data copy from PBS to target disk for every vol,
6848 # this will effectively collapse the backing image chain consisting of
6849 # [target <- alloc-track -> PBS snapshot] to just [target] (alloc-track
6850 # removes itself once all backing images vanish with 'auto-remove=on')
6851 my $jobs = {};
8986e36e 6852 for my $ds (sort keys %$restored_disks) {
26731a3c
SR
6853 my $job_id = "restore-$ds";
6854 mon_cmd($vmid, 'block-stream',
6855 'job-id' => $job_id,
6856 device => "$ds",
6857 );
6858 $jobs->{$job_id} = {};
6859 }
6860
6861 mon_cmd($vmid, 'cont');
6862 qemu_drive_mirror_monitor($vmid, undef, $jobs, 'auto', 0, 'stream');
6863
a09b39f1
TL
6864 print "restore-drive jobs finished successfully, removing all tracking block devices"
6865 ." to disconnect from Proxmox Backup Server\n";
6866
8986e36e 6867 for my $ds (sort keys %$restored_disks) {
26731a3c
SR
6868 mon_cmd($vmid, 'blockdev-del', 'node-name' => "$ds-pbs");
6869 }
26697640
SR
6870
6871 close($qmeventd_fd);
26731a3c
SR
6872 };
6873
6874 my $err = $@;
6875
6876 if ($err) {
8b8893c3 6877 warn "An error occurred during live-restore: $err\n";
26731a3c
SR
6878 _do_vm_stop($storecfg, $vmid, 1, 1, 10, 0, 1);
6879 die "live-restore failed\n";
6880 }
9f3d73bc
DM
6881}
6882
91bd6c90
DM
6883sub restore_vma_archive {
6884 my ($archive, $vmid, $user, $opts, $comp) = @_;
6885
91bd6c90
DM
6886 my $readfrom = $archive;
6887
7c536e11
WB
6888 my $cfg = PVE::Storage::config();
6889 my $commands = [];
6890 my $bwlimit = $opts->{bwlimit};
6891
6892 my $dbg_cmdstring = '';
6893 my $add_pipe = sub {
6894 my ($cmd) = @_;
6895 push @$commands, $cmd;
6896 $dbg_cmdstring .= ' | ' if length($dbg_cmdstring);
6897 $dbg_cmdstring .= PVE::Tools::cmd2string($cmd);
91bd6c90 6898 $readfrom = '-';
7c536e11
WB
6899 };
6900
6901 my $input = undef;
6902 if ($archive eq '-') {
6903 $input = '<&STDIN';
6904 } else {
6905 # If we use a backup from a PVE defined storage we also consider that
6906 # storage's rate limit:
6907 my (undef, $volid) = PVE::Storage::path_to_volume_id($cfg, $archive);
6908 if (defined($volid)) {
6909 my ($sid, undef) = PVE::Storage::parse_volume_id($volid);
6910 my $readlimit = PVE::Storage::get_bandwidth_limit('restore', [$sid], $bwlimit);
6911 if ($readlimit) {
6912 print STDERR "applying read rate limit: $readlimit\n";
9444c6e4 6913 my $cstream = ['cstream', '-t', $readlimit*1024, '--', $readfrom];
7c536e11
WB
6914 $add_pipe->($cstream);
6915 }
6916 }
6917 }
6918
6919 if ($comp) {
c6d51783
AA
6920 my $info = PVE::Storage::decompressor_info('vma', $comp);
6921 my $cmd = $info->{decompressor};
6922 push @$cmd, $readfrom;
7c536e11 6923 $add_pipe->($cmd);
91bd6c90
DM
6924 }
6925
6926 my $tmpdir = "/var/tmp/vzdumptmp$$";
6927 rmtree $tmpdir;
6928
6929 # disable interrupts (always do cleanups)
5b97ef24
TL
6930 local $SIG{INT} =
6931 local $SIG{TERM} =
6932 local $SIG{QUIT} =
6933 local $SIG{HUP} = sub { warn "got interrupt - ignored\n"; };
91bd6c90
DM
6934
6935 my $mapfifo = "/var/tmp/vzdumptmp$$.fifo";
6936 POSIX::mkfifo($mapfifo, 0600);
6937 my $fifofh;
808a65b5 6938 my $openfifo = sub { open($fifofh, '>', $mapfifo) or die $! };
91bd6c90 6939
7c536e11 6940 $add_pipe->(['vma', 'extract', '-v', '-r', $mapfifo, $readfrom, $tmpdir]);
91bd6c90
DM
6941
6942 my $oldtimeout;
6943 my $timeout = 5;
6944
6945 my $devinfo = {};
6946
6947 my $rpcenv = PVE::RPCEnvironment::get();
6948
ffda963f 6949 my $conffile = PVE::QemuConfig->config_file($vmid);
91bd6c90 6950
ae200950 6951 # Note: $oldconf is undef if VM does not exist
ffda963f
FG
6952 my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
6953 my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
98a4b3fb 6954 my $new_conf_raw = '';
ed221350 6955
7c536e11
WB
6956 my %storage_limits;
6957
91bd6c90 6958 my $print_devmap = sub {
91bd6c90
DM
6959 my $cfgfn = "$tmpdir/qemu-server.conf";
6960
6961 # we can read the config - that is already extracted
6962 my $fh = IO::File->new($cfgfn, "r") ||
a1cbe55c 6963 die "unable to read qemu-server.conf - $!\n";
91bd6c90 6964
6738ab9c 6965 my $fwcfgfn = "$tmpdir/qemu-server.fw";
3457d090
WL
6966 if (-f $fwcfgfn) {
6967 my $pve_firewall_dir = '/etc/pve/firewall';
6968 mkdir $pve_firewall_dir; # make sure the dir exists
6969 PVE::Tools::file_copy($fwcfgfn, "${pve_firewall_dir}/$vmid.fw");
6970 }
6738ab9c 6971
9f3d73bc 6972 my $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $cfg, $fh, $devinfo, $opts);
91bd6c90 6973
c8964278
FE
6974 foreach my $info (values %{$virtdev_hash}) {
6975 my $storeid = $info->{storeid};
6976 next if defined($storage_limits{$storeid});
6977
6978 my $limit = PVE::Storage::get_bandwidth_limit('restore', [$storeid], $bwlimit) // 0;
6979 print STDERR "rate limit for storage $storeid: $limit KiB/s\n" if $limit;
6980 $storage_limits{$storeid} = $limit * 1024;
7c536e11
WB
6981 }
6982
91bd6c90 6983 foreach my $devname (keys %$devinfo) {
be190583
DM
6984 die "found no device mapping information for device '$devname'\n"
6985 if !$devinfo->{$devname}->{virtdev};
91bd6c90
DM
6986 }
6987
ed221350 6988 # create empty/temp config
be190583 6989 if ($oldconf) {
ed221350 6990 PVE::Tools::file_set_contents($conffile, "memory: 128\n");
d1e92cf6 6991 $restore_cleanup_oldconf->($cfg, $vmid, $oldconf, $virtdev_hash);
ed221350
DM
6992 }
6993
9f3d73bc
DM
6994 # allocate volumes
6995 my $map = $restore_allocate_devices->($cfg, $virtdev_hash, $vmid);
6996
6997 # print restore information to $fifofh
91bd6c90
DM
6998 foreach my $virtdev (sort keys %$virtdev_hash) {
6999 my $d = $virtdev_hash->{$virtdev};
9f3d73bc
DM
7000 next if $d->{is_cloudinit}; # no need to restore cloudinit
7001
7c536e11 7002 my $storeid = $d->{storeid};
9f3d73bc 7003 my $volid = $d->{volid};
7c536e11
WB
7004
7005 my $map_opts = '';
7006 if (my $limit = $storage_limits{$storeid}) {
7007 $map_opts .= "throttling.bps=$limit:throttling.group=$storeid:";
7008 }
8fd57431 7009
91bd6c90 7010 my $write_zeros = 1;
88240a83 7011 if (PVE::Storage::volume_has_feature($cfg, 'sparseinit', $volid)) {
91bd6c90
DM
7012 $write_zeros = 0;
7013 }
7014
9f3d73bc 7015 my $path = PVE::Storage::path($cfg, $volid);
87056e18 7016
9f3d73bc 7017 print $fifofh "${map_opts}format=$d->{format}:${write_zeros}:$d->{devname}=$path\n";
91bd6c90 7018
9f3d73bc 7019 print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros})\n";
91bd6c90
DM
7020 }
7021
7022 $fh->seek(0, 0) || die "seek failed - $!\n";
7023
91bd6c90
DM
7024 my $cookie = { netcount => 0 };
7025 while (defined(my $line = <$fh>)) {
c62d7cf5 7026 $new_conf_raw .= restore_update_config_line(
98a4b3fb 7027 $cookie,
98a4b3fb
FE
7028 $map,
7029 $line,
7030 $opts->{unique},
7031 );
91bd6c90
DM
7032 }
7033
7034 $fh->close();
91bd6c90
DM
7035 };
7036
7037 eval {
7038 # enable interrupts
6cb0144a
EK
7039 local $SIG{INT} =
7040 local $SIG{TERM} =
7041 local $SIG{QUIT} =
7042 local $SIG{HUP} =
7043 local $SIG{PIPE} = sub { die "interrupted by signal\n"; };
91bd6c90
DM
7044 local $SIG{ALRM} = sub { die "got timeout\n"; };
7045
7046 $oldtimeout = alarm($timeout);
7047
7048 my $parser = sub {
7049 my $line = shift;
7050
7051 print "$line\n";
7052
7053 if ($line =~ m/^DEV:\sdev_id=(\d+)\ssize:\s(\d+)\sdevname:\s(\S+)$/) {
7054 my ($dev_id, $size, $devname) = ($1, $2, $3);
7055 $devinfo->{$devname} = { size => $size, dev_id => $dev_id };
7056 } elsif ($line =~ m/^CTIME: /) {
46f58b5f 7057 # we correctly received the vma config, so we can disable
3cf90d7a
DM
7058 # the timeout now for disk allocation (set to 10 minutes, so
7059 # that we always timeout if something goes wrong)
7060 alarm(600);
91bd6c90
DM
7061 &$print_devmap();
7062 print $fifofh "done\n";
7063 my $tmp = $oldtimeout || 0;
7064 $oldtimeout = undef;
7065 alarm($tmp);
7066 close($fifofh);
808a65b5 7067 $fifofh = undef;
91bd6c90
DM
7068 }
7069 };
be190583 7070
7c536e11
WB
7071 print "restore vma archive: $dbg_cmdstring\n";
7072 run_command($commands, input => $input, outfunc => $parser, afterfork => $openfifo);
91bd6c90
DM
7073 };
7074 my $err = $@;
7075
7076 alarm($oldtimeout) if $oldtimeout;
7077
9f3d73bc 7078 $restore_deactivate_volumes->($cfg, $devinfo);
5f96f4df 7079
808a65b5 7080 close($fifofh) if $fifofh;
91bd6c90 7081 unlink $mapfifo;
9f3d73bc 7082 rmtree $tmpdir;
91bd6c90
DM
7083
7084 if ($err) {
9f3d73bc 7085 $restore_destroy_volumes->($cfg, $devinfo);
91bd6c90
DM
7086 die $err;
7087 }
7088
98a4b3fb 7089 PVE::Tools::file_set_contents($conffile, $new_conf_raw);
91bd6c90 7090
ed221350
DM
7091 PVE::Cluster::cfs_update(); # make sure we read new file
7092
91bd6c90
DM
7093 eval { rescan($vmid, 1); };
7094 warn $@ if $@;
26731a3c
SR
7095
7096 PVE::AccessControl::add_vm_to_pool($vmid, $opts->{pool}) if $opts->{pool};
91bd6c90
DM
7097}
7098
7099sub restore_tar_archive {
7100 my ($archive, $vmid, $user, $opts) = @_;
7101
9c502e26 7102 if ($archive ne '-') {
ed221350 7103 my $firstfile = tar_archive_read_firstfile($archive);
32e54050 7104 die "ERROR: file '$archive' does not look like a QemuServer vzdump backup\n"
9c502e26
DM
7105 if $firstfile ne 'qemu-server.conf';
7106 }
3e16d5fc 7107
20519efc 7108 my $storecfg = PVE::Storage::config();
ebb55558 7109
4b026937
TL
7110 # avoid zombie disks when restoring over an existing VM -> cleanup first
7111 # pass keep_empty_config=1 to keep the config (thus VMID) reserved for us
7112 # skiplock=1 because qmrestore has set the 'create' lock itself already
ffda963f 7113 my $vmcfgfn = PVE::QemuConfig->config_file($vmid);
b04ea584 7114 destroy_vm($storecfg, $vmid, 1, { lock => 'restore' }) if -f $vmcfgfn;
ed221350 7115
3e16d5fc
DM
7116 my $tocmd = "/usr/lib/qemu-server/qmextract";
7117
2415a446 7118 $tocmd .= " --storage " . PVE::Tools::shellquote($opts->{storage}) if $opts->{storage};
a0d1b1a2 7119 $tocmd .= " --pool " . PVE::Tools::shellquote($opts->{pool}) if $opts->{pool};
3e16d5fc
DM
7120 $tocmd .= ' --prealloc' if $opts->{prealloc};
7121 $tocmd .= ' --info' if $opts->{info};
7122
a0d1b1a2 7123 # tar option "xf" does not autodetect compression when read from STDIN,
9c502e26 7124 # so we pipe to zcat
2415a446
DM
7125 my $cmd = "zcat -f|tar xf " . PVE::Tools::shellquote($archive) . " " .
7126 PVE::Tools::shellquote("--to-command=$tocmd");
3e16d5fc
DM
7127
7128 my $tmpdir = "/var/tmp/vzdumptmp$$";
7129 mkpath $tmpdir;
7130
7131 local $ENV{VZDUMP_TMPDIR} = $tmpdir;
7132 local $ENV{VZDUMP_VMID} = $vmid;
a0d1b1a2 7133 local $ENV{VZDUMP_USER} = $user;
3e16d5fc 7134
ffda963f 7135 my $conffile = PVE::QemuConfig->config_file($vmid);
98a4b3fb 7136 my $new_conf_raw = '';
3e16d5fc
DM
7137
7138 # disable interrupts (always do cleanups)
6cb0144a
EK
7139 local $SIG{INT} =
7140 local $SIG{TERM} =
7141 local $SIG{QUIT} =
7142 local $SIG{HUP} = sub { print STDERR "got interrupt - ignored\n"; };
3e16d5fc 7143
afdb31d5 7144 eval {
3e16d5fc 7145 # enable interrupts
6cb0144a
EK
7146 local $SIG{INT} =
7147 local $SIG{TERM} =
7148 local $SIG{QUIT} =
7149 local $SIG{HUP} =
7150 local $SIG{PIPE} = sub { die "interrupted by signal\n"; };
3e16d5fc 7151
9c502e26
DM
7152 if ($archive eq '-') {
7153 print "extracting archive from STDIN\n";
7154 run_command($cmd, input => "<&STDIN");
7155 } else {
7156 print "extracting archive '$archive'\n";
7157 run_command($cmd);
7158 }
3e16d5fc
DM
7159
7160 return if $opts->{info};
7161
7162 # read new mapping
7163 my $map = {};
7164 my $statfile = "$tmpdir/qmrestore.stat";
7165 if (my $fd = IO::File->new($statfile, "r")) {
7166 while (defined (my $line = <$fd>)) {
7167 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
7168 $map->{$1} = $2 if $1;
7169 } else {
7170 print STDERR "unable to parse line in statfile - $line\n";
7171 }
7172 }
7173 $fd->close();
7174 }
7175
7176 my $confsrc = "$tmpdir/qemu-server.conf";
7177
f7d1505b 7178 my $srcfd = IO::File->new($confsrc, "r") || die "unable to open file '$confsrc'\n";
3e16d5fc 7179
91bd6c90 7180 my $cookie = { netcount => 0 };
3e16d5fc 7181 while (defined (my $line = <$srcfd>)) {
c62d7cf5 7182 $new_conf_raw .= restore_update_config_line(
98a4b3fb 7183 $cookie,
98a4b3fb
FE
7184 $map,
7185 $line,
7186 $opts->{unique},
7187 );
3e16d5fc
DM
7188 }
7189
7190 $srcfd->close();
3e16d5fc 7191 };
7dc7f315 7192 if (my $err = $@) {
ed221350 7193 tar_restore_cleanup($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info};
3e16d5fc 7194 die $err;
afdb31d5 7195 }
3e16d5fc
DM
7196
7197 rmtree $tmpdir;
7198
98a4b3fb 7199 PVE::Tools::file_set_contents($conffile, $new_conf_raw);
91bd6c90 7200
ed221350
DM
7201 PVE::Cluster::cfs_update(); # make sure we read new file
7202
91bd6c90
DM
7203 eval { rescan($vmid, 1); };
7204 warn $@ if $@;
3e16d5fc
DM
7205};
7206
65a5ce88 7207sub foreach_storage_used_by_vm {
18bfb361
DM
7208 my ($conf, $func) = @_;
7209
7210 my $sidhash = {};
7211
912792e2 7212 PVE::QemuConfig->foreach_volume($conf, sub {
8ddbcf8b
FG
7213 my ($ds, $drive) = @_;
7214 return if drive_is_cdrom($drive);
18bfb361
DM
7215
7216 my $volid = $drive->{file};
7217
7218 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
be190583 7219 $sidhash->{$sid} = $sid if $sid;
8ddbcf8b 7220 });
18bfb361
DM
7221
7222 foreach my $sid (sort keys %$sidhash) {
7223 &$func($sid);
7224 }
7225}
7226
6c9f59c1
TL
7227my $qemu_snap_storage = {
7228 rbd => 1,
7229};
e5eaa028 7230sub do_snapshots_with_qemu {
9d83932d
SR
7231 my ($storecfg, $volid, $deviceid) = @_;
7232
7233 return if $deviceid =~ m/tpmstate0/;
e5eaa028
WL
7234
7235 my $storage_name = PVE::Storage::parse_volume_id($volid);
8aa2ed7c 7236 my $scfg = $storecfg->{ids}->{$storage_name};
f7d1505b 7237 die "could not find storage '$storage_name'\n" if !defined($scfg);
e5eaa028 7238
8aa2ed7c 7239 if ($qemu_snap_storage->{$scfg->{type}} && !$scfg->{krbd}){
e5eaa028
WL
7240 return 1;
7241 }
7242
7243 if ($volid =~ m/\.(qcow2|qed)$/){
7244 return 1;
7245 }
7246
d1c1af4b 7247 return;
e5eaa028
WL
7248}
7249
4dcc780c 7250sub qga_check_running {
a4938c72 7251 my ($vmid, $nowarn) = @_;
4dcc780c 7252
0a13e08e 7253 eval { mon_cmd($vmid, "guest-ping", timeout => 3); };
4dcc780c 7254 if ($@) {
a4938c72 7255 warn "Qemu Guest Agent is not running - $@" if !$nowarn;
4dcc780c
WL
7256 return 0;
7257 }
7258 return 1;
7259}
7260
04a69bb4
AD
7261sub template_create {
7262 my ($vmid, $conf, $disk) = @_;
7263
04a69bb4 7264 my $storecfg = PVE::Storage::config();
04a69bb4 7265
912792e2 7266 PVE::QemuConfig->foreach_volume($conf, sub {
9cd07842
DM
7267 my ($ds, $drive) = @_;
7268
7269 return if drive_is_cdrom($drive);
7270 return if $disk && $ds ne $disk;
7271
7272 my $volid = $drive->{file};
bbd56097 7273 return if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid);
9cd07842 7274
04a69bb4
AD
7275 my $voliddst = PVE::Storage::vdisk_create_base($storecfg, $volid);
7276 $drive->{file} = $voliddst;
71c58bb7 7277 $conf->{$ds} = print_drive($drive);
ffda963f 7278 PVE::QemuConfig->write_config($vmid, $conf);
04a69bb4 7279 });
04a69bb4
AD
7280}
7281
92bdc3f0
DC
7282sub convert_iscsi_path {
7283 my ($path) = @_;
7284
7285 if ($path =~ m|^iscsi://([^/]+)/([^/]+)/(.+)$|) {
7286 my $portal = $1;
7287 my $target = $2;
7288 my $lun = $3;
7289
7290 my $initiator_name = get_initiator_name();
7291
7292 return "file.driver=iscsi,file.transport=tcp,file.initiator-name=$initiator_name,".
7293 "file.portal=$portal,file.target=$target,file.lun=$lun,driver=raw";
7294 }
7295
7296 die "cannot convert iscsi path '$path', unkown format\n";
7297}
7298
5133de42 7299sub qemu_img_convert {
988e2714 7300 my ($src_volid, $dst_volid, $size, $snapname, $is_zero_initialized) = @_;
5133de42
AD
7301
7302 my $storecfg = PVE::Storage::config();
7303 my ($src_storeid, $src_volname) = PVE::Storage::parse_volume_id($src_volid, 1);
7304 my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid, 1);
7305
af1f1ec0 7306 die "destination '$dst_volid' is not a valid volid form qemu-img convert\n" if !$dst_storeid;
6bb91c17 7307
af1f1ec0
DC
7308 my $cachemode;
7309 my $src_path;
7310 my $src_is_iscsi = 0;
bdd1feef 7311 my $src_format;
6bb91c17 7312
af1f1ec0
DC
7313 if ($src_storeid) {
7314 PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
5133de42 7315 my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
af1f1ec0
DC
7316 $src_format = qemu_img_format($src_scfg, $src_volname);
7317 $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
7318 $src_is_iscsi = ($src_path =~ m|^iscsi://|);
7319 $cachemode = 'none' if $src_scfg->{type} eq 'zfspool';
a23d57d5 7320 } elsif (-f $src_volid || -b $src_volid) {
af1f1ec0 7321 $src_path = $src_volid;
e0fd2b2f 7322 if ($src_path =~ m/\.($PVE::QemuServer::Drive::QEMU_FORMAT_RE)$/) {
af1f1ec0
DC
7323 $src_format = $1;
7324 }
7325 }
5133de42 7326
af1f1ec0 7327 die "source '$src_volid' is not a valid volid nor path for qemu-img convert\n" if !$src_path;
5133de42 7328
af1f1ec0
DC
7329 my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
7330 my $dst_format = qemu_img_format($dst_scfg, $dst_volname);
7331 my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
7332 my $dst_is_iscsi = ($dst_path =~ m|^iscsi://|);
5133de42 7333
af1f1ec0
DC
7334 my $cmd = [];
7335 push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-n';
bdd1feef
TL
7336 push @$cmd, '-l', "snapshot.name=$snapname"
7337 if $snapname && $src_format && $src_format eq "qcow2";
af1f1ec0
DC
7338 push @$cmd, '-t', 'none' if $dst_scfg->{type} eq 'zfspool';
7339 push @$cmd, '-T', $cachemode if defined($cachemode);
7340
7341 if ($src_is_iscsi) {
7342 push @$cmd, '--image-opts';
7343 $src_path = convert_iscsi_path($src_path);
bdd1feef 7344 } elsif ($src_format) {
af1f1ec0
DC
7345 push @$cmd, '-f', $src_format;
7346 }
92bdc3f0 7347
af1f1ec0
DC
7348 if ($dst_is_iscsi) {
7349 push @$cmd, '--target-image-opts';
7350 $dst_path = convert_iscsi_path($dst_path);
7351 } else {
7352 push @$cmd, '-O', $dst_format;
7353 }
92bdc3f0 7354
af1f1ec0 7355 push @$cmd, $src_path;
92bdc3f0 7356
af1f1ec0
DC
7357 if (!$dst_is_iscsi && $is_zero_initialized) {
7358 push @$cmd, "zeroinit:$dst_path";
7359 } else {
7360 push @$cmd, $dst_path;
7361 }
92bdc3f0 7362
af1f1ec0
DC
7363 my $parser = sub {
7364 my $line = shift;
7365 if($line =~ m/\((\S+)\/100\%\)/){
7366 my $percent = $1;
7367 my $transferred = int($size * $percent / 100);
b5e9d97b
TL
7368 my $total_h = render_bytes($size, 1);
7369 my $transferred_h = render_bytes($transferred, 1);
92bdc3f0 7370
6629f976 7371 print "transferred $transferred_h of $total_h ($percent%)\n";
988e2714 7372 }
5133de42 7373
af1f1ec0 7374 };
5133de42 7375
af1f1ec0
DC
7376 eval { run_command($cmd, timeout => undef, outfunc => $parser); };
7377 my $err = $@;
7378 die "copy failed: $err" if $err;
5133de42
AD
7379}
7380
7381sub qemu_img_format {
7382 my ($scfg, $volname) = @_;
7383
e0fd2b2f 7384 if ($scfg->{path} && $volname =~ m/\.($PVE::QemuServer::Drive::QEMU_FORMAT_RE)$/) {
5133de42 7385 return $1;
be190583 7386 } else {
5133de42 7387 return "raw";
5133de42
AD
7388 }
7389}
7390
cfad42af 7391sub qemu_drive_mirror {
bc6c8231 7392 my ($vmid, $drive, $dst_volid, $vmiddst, $is_zero_initialized, $jobs, $completion, $qga, $bwlimit, $src_bitmap) = @_;
cfad42af 7393
5a345967
AD
7394 $jobs = {} if !$jobs;
7395
7396 my $qemu_target;
7397 my $format;
35e4ab04 7398 $jobs->{"drive-$drive"} = {};
152fe752 7399
1e5143de 7400 if ($dst_volid =~ /^nbd:/) {
87955688 7401 $qemu_target = $dst_volid;
5a345967 7402 $format = "nbd";
5a345967 7403 } else {
5a345967
AD
7404 my $storecfg = PVE::Storage::config();
7405 my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid);
7406
7407 my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
cfad42af 7408
5a345967 7409 $format = qemu_img_format($dst_scfg, $dst_volname);
21ccdb50 7410
5a345967 7411 my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
21ccdb50 7412
5a345967
AD
7413 $qemu_target = $is_zero_initialized ? "zeroinit:$dst_path" : $dst_path;
7414 }
988e2714
WB
7415
7416 my $opts = { timeout => 10, device => "drive-$drive", mode => "existing", sync => "full", target => $qemu_target };
88383920
DM
7417 $opts->{format} = $format if $format;
7418
bc6c8231
FG
7419 if (defined($src_bitmap)) {
7420 $opts->{sync} = 'incremental';
7421 $opts->{bitmap} = $src_bitmap;
7422 print "drive mirror re-using dirty bitmap '$src_bitmap'\n";
7423 }
7424
9fa05d31 7425 if (defined($bwlimit)) {
f6409f61
TL
7426 $opts->{speed} = $bwlimit * 1024;
7427 print "drive mirror is starting for drive-$drive with bandwidth limit: ${bwlimit} KB/s\n";
9fa05d31
SI
7428 } else {
7429 print "drive mirror is starting for drive-$drive\n";
7430 }
21ccdb50 7431
6dde5ea2 7432 # if a job already runs for this device we get an error, catch it for cleanup
0a13e08e 7433 eval { mon_cmd($vmid, "drive-mirror", %$opts); };
5a345967
AD
7434 if (my $err = $@) {
7435 eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
6dde5ea2
TL
7436 warn "$@\n" if $@;
7437 die "mirroring error: $err\n";
5a345967
AD
7438 }
7439
e02fb126 7440 qemu_drive_mirror_monitor ($vmid, $vmiddst, $jobs, $completion, $qga);
5a345967
AD
7441}
7442
db1f8b39
FG
7443# $completion can be either
7444# 'complete': wait until all jobs are ready, block-job-complete them (default)
7445# 'cancel': wait until all jobs are ready, block-job-cancel them
7446# 'skip': wait until all jobs are ready, return with block jobs in ready state
9e671722 7447# 'auto': wait until all jobs disappear, only use for jobs which complete automatically
5a345967 7448sub qemu_drive_mirror_monitor {
9e671722 7449 my ($vmid, $vmiddst, $jobs, $completion, $qga, $op) = @_;
e02fb126 7450
db1f8b39 7451 $completion //= 'complete';
9e671722 7452 $op //= "mirror";
2e953867 7453
08ac653f 7454 eval {
5a345967
AD
7455 my $err_complete = 0;
7456
3b56383b 7457 my $starttime = time ();
08ac653f 7458 while (1) {
9e671722 7459 die "block job ('$op') timed out\n" if $err_complete > 300;
5a345967 7460
0a13e08e 7461 my $stats = mon_cmd($vmid, "query-block-jobs");
3b56383b 7462 my $ctime = time();
08ac653f 7463
9e671722 7464 my $running_jobs = {};
0ea24bf0 7465 for my $stat (@$stats) {
9e671722
SR
7466 next if $stat->{type} ne $op;
7467 $running_jobs->{$stat->{device}} = $stat;
5a345967 7468 }
08ac653f 7469
5a345967 7470 my $readycounter = 0;
67fb9de6 7471
0ea24bf0 7472 for my $job_id (sort keys %$jobs) {
1057fc74 7473 my $job = $running_jobs->{$job_id};
5a345967 7474
1057fc74 7475 my $vanished = !defined($job);
0ea24bf0 7476 my $complete = defined($jobs->{$job_id}->{complete}) && $vanished;
9e671722 7477 if($complete || ($vanished && $completion eq 'auto')) {
3b56383b 7478 print "$job_id: $op-job finished\n";
0ea24bf0 7479 delete $jobs->{$job_id};
5a345967
AD
7480 next;
7481 }
7482
1057fc74 7483 die "$job_id: '$op' has been cancelled\n" if !defined($job);
f34ebd52 7484
1057fc74
TL
7485 my $busy = $job->{busy};
7486 my $ready = $job->{ready};
7487 if (my $total = $job->{len}) {
7488 my $transferred = $job->{offset} || 0;
5a345967
AD
7489 my $remaining = $total - $transferred;
7490 my $percent = sprintf "%.2f", ($transferred * 100 / $total);
08ac653f 7491
3b56383b
TL
7492 my $duration = $ctime - $starttime;
7493 my $total_h = render_bytes($total, 1);
7494 my $transferred_h = render_bytes($transferred, 1);
7495
7496 my $status = sprintf(
7497 "transferred $transferred_h of $total_h ($percent%%) in %s",
7498 render_duration($duration),
7499 );
7500
7501 if ($ready) {
7502 if ($busy) {
7503 $status .= ", still busy"; # shouldn't even happen? but mirror is weird
7504 } else {
7505 $status .= ", ready";
7506 }
7507 }
67daf692
TL
7508 print "$job_id: $status\n" if !$jobs->{$job_id}->{ready};
7509 $jobs->{$job_id}->{ready} = $ready;
5a345967 7510 }
f34ebd52 7511
1057fc74 7512 $readycounter++ if $job->{ready};
5a345967 7513 }
b467f79a 7514
5a345967
AD
7515 last if scalar(keys %$jobs) == 0;
7516
7517 if ($readycounter == scalar(keys %$jobs)) {
9e671722
SR
7518 print "all '$op' jobs are ready\n";
7519
7520 # do the complete later (or has already been done)
7521 last if $completion eq 'skip' || $completion eq 'auto';
5a345967
AD
7522
7523 if ($vmiddst && $vmiddst != $vmid) {
1a988fd2
DC
7524 my $agent_running = $qga && qga_check_running($vmid);
7525 if ($agent_running) {
5619e74a 7526 print "freeze filesystem\n";
0a13e08e 7527 eval { mon_cmd($vmid, "guest-fsfreeze-freeze"); };
d6cdfae4 7528 warn $@ if $@;
5619e74a
AD
7529 } else {
7530 print "suspend vm\n";
7531 eval { PVE::QemuServer::vm_suspend($vmid, 1); };
d6cdfae4 7532 warn $@ if $@;
5619e74a
AD
7533 }
7534
5a345967
AD
7535 # if we clone a disk for a new target vm, we don't switch the disk
7536 PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs);
5619e74a 7537
1a988fd2 7538 if ($agent_running) {
5619e74a 7539 print "unfreeze filesystem\n";
0a13e08e 7540 eval { mon_cmd($vmid, "guest-fsfreeze-thaw"); };
d6cdfae4 7541 warn $@ if $@;
5619e74a
AD
7542 } else {
7543 print "resume vm\n";
d6cdfae4
FE
7544 eval { PVE::QemuServer::vm_resume($vmid, 1, 1); };
7545 warn $@ if $@;
5619e74a
AD
7546 }
7547
2e953867 7548 last;
5a345967
AD
7549 } else {
7550
0ea24bf0 7551 for my $job_id (sort keys %$jobs) {
5a345967 7552 # try to switch the disk if source and destination are on the same guest
0ea24bf0 7553 print "$job_id: Completing block job_id...\n";
5a345967 7554
e02fb126 7555 my $op;
db1f8b39 7556 if ($completion eq 'complete') {
e02fb126 7557 $op = 'block-job-complete';
db1f8b39 7558 } elsif ($completion eq 'cancel') {
e02fb126
ML
7559 $op = 'block-job-cancel';
7560 } else {
7561 die "invalid completion value: $completion\n";
7562 }
0ea24bf0 7563 eval { mon_cmd($vmid, $op, device => $job_id) };
5a345967 7564 if ($@ =~ m/cannot be completed/) {
3b56383b 7565 print "$job_id: block job cannot be completed, trying again.\n";
5a345967
AD
7566 $err_complete++;
7567 }else {
0ea24bf0
TL
7568 print "$job_id: Completed successfully.\n";
7569 $jobs->{$job_id}->{complete} = 1;
5a345967
AD
7570 }
7571 }
2e953867 7572 }
08ac653f 7573 }
08ac653f 7574 sleep 1;
cfad42af 7575 }
08ac653f 7576 };
88383920 7577 my $err = $@;
08ac653f 7578
88383920 7579 if ($err) {
5a345967 7580 eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
3b56383b 7581 die "block job ($op) error: $err";
88383920 7582 }
5a345967
AD
7583}
7584
7585sub qemu_blockjobs_cancel {
7586 my ($vmid, $jobs) = @_;
7587
7588 foreach my $job (keys %$jobs) {
bd2d5fe6 7589 print "$job: Cancelling block job\n";
0a13e08e 7590 eval { mon_cmd($vmid, "block-job-cancel", device => $job); };
5a345967
AD
7591 $jobs->{$job}->{cancel} = 1;
7592 }
7593
7594 while (1) {
0a13e08e 7595 my $stats = mon_cmd($vmid, "query-block-jobs");
5a345967
AD
7596
7597 my $running_jobs = {};
7598 foreach my $stat (@$stats) {
7599 $running_jobs->{$stat->{device}} = $stat;
7600 }
7601
7602 foreach my $job (keys %$jobs) {
7603
bd2d5fe6
WB
7604 if (defined($jobs->{$job}->{cancel}) && !defined($running_jobs->{$job})) {
7605 print "$job: Done.\n";
5a345967
AD
7606 delete $jobs->{$job};
7607 }
7608 }
7609
7610 last if scalar(keys %$jobs) == 0;
7611
7612 sleep 1;
cfad42af
AD
7613 }
7614}
7615
152fe752 7616sub clone_disk {
1196086f
FE
7617 my ($storecfg, $source, $dest, $full, $newvollist, $jobs, $completion, $qga, $bwlimit) = @_;
7618
7619 my ($vmid, $running) = $source->@{qw(vmid running)};
25166060 7620 my ($src_drivename, $drive, $snapname) = $source->@{qw(drivename drive snapname)};
1196086f 7621
25166060 7622 my ($newvmid, $dst_drivename, $efisize) = $dest->@{qw(vmid drivename efisize)};
1196086f 7623 my ($storage, $format) = $dest->@{qw(storage format)};
152fe752 7624
5f957592
FE
7625 my $use_drive_mirror = $full && $running && $src_drivename && !$snapname;
7626
25166060
FE
7627 if ($src_drivename && $dst_drivename && $src_drivename ne $dst_drivename) {
7628 die "cloning from/to EFI disk requires EFI disk\n"
7629 if $src_drivename eq 'efidisk0' || $dst_drivename eq 'efidisk0';
7630 die "cloning from/to TPM state requires TPM state\n"
7631 if $src_drivename eq 'tpmstate0' || $dst_drivename eq 'tpmstate0';
5f957592
FE
7632
7633 # This would lead to two device nodes in QEMU pointing to the same backing image!
7634 die "cannot change drive name when cloning disk from/to the same VM\n"
7635 if $use_drive_mirror && $vmid == $newvmid;
25166060
FE
7636 }
7637
1d1f8f9a
FE
7638 die "cannot move TPM state while VM is running\n"
7639 if $use_drive_mirror && $src_drivename eq 'tpmstate0';
7640
152fe752
DM
7641 my $newvolid;
7642
25166060
FE
7643 print "create " . ($full ? 'full' : 'linked') . " clone of drive ";
7644 print "$src_drivename " if $src_drivename;
7645 print "($drive->{file})\n";
7646
152fe752 7647 if (!$full) {
258e646c 7648 $newvolid = PVE::Storage::vdisk_clone($storecfg, $drive->{file}, $newvmid, $snapname);
152fe752
DM
7649 push @$newvollist, $newvolid;
7650 } else {
5a345967 7651
152fe752
DM
7652 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
7653 $storeid = $storage if $storage;
7654
44549149 7655 my $dst_format = resolve_dst_disk_format($storecfg, $storeid, $volname, $format);
152fe752 7656
931432bd 7657 my $name = undef;
d0abc774 7658 my $size = undef;
7fe8b44c
TL
7659 if (drive_is_cloudinit($drive)) {
7660 $name = "vm-$newvmid-cloudinit";
c997e24a
ML
7661 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
7662 if ($scfg->{path}) {
7663 $name .= ".$dst_format";
7664 }
7fe8b44c
TL
7665 $snapname = undef;
7666 $size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
25166060 7667 } elsif ($dst_drivename eq 'efidisk0') {
7344af7b 7668 $size = $efisize or die "internal error - need to specify EFI disk size\n";
25166060 7669 } elsif ($dst_drivename eq 'tpmstate0') {
5f5aba25 7670 $dst_format = 'raw';
f9dde219 7671 $size = PVE::QemuServer::Drive::TPMSTATE_DISK_SIZE;
d0abc774 7672 } else {
3bae384f 7673 ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 10);
7fe8b44c 7674 }
b5688f69
FE
7675 $newvolid = PVE::Storage::vdisk_alloc(
7676 $storecfg, $storeid, $newvmid, $dst_format, $name, ($size/1024)
7677 );
152fe752
DM
7678 push @$newvollist, $newvolid;
7679
3999f370 7680 PVE::Storage::activate_volumes($storecfg, [$newvolid]);
1dbd6d30 7681
7fe8b44c 7682 if (drive_is_cloudinit($drive)) {
1b485263
ML
7683 # when cloning multiple disks (e.g. during clone_vm) it might be the last disk
7684 # if this is the case, we have to complete any block-jobs still there from
7685 # previous drive-mirrors
7686 if (($completion eq 'complete') && (scalar(keys %$jobs) > 0)) {
7687 qemu_drive_mirror_monitor($vmid, $newvmid, $jobs, $completion, $qga);
7688 }
7fe8b44c
TL
7689 goto no_data_clone;
7690 }
7691
988e2714 7692 my $sparseinit = PVE::Storage::volume_has_feature($storecfg, 'sparseinit', $newvolid);
5f957592 7693 if ($use_drive_mirror) {
5f957592
FE
7694 qemu_drive_mirror($vmid, $src_drivename, $newvolid, $newvmid, $sparseinit, $jobs,
7695 $completion, $qga, $bwlimit);
7696 } else {
d189e590 7697 # TODO: handle bwlimits
25166060 7698 if ($dst_drivename eq 'efidisk0') {
818ce80e
DC
7699 # the relevant data on the efidisk may be smaller than the source
7700 # e.g. on RBD/ZFS, so we use dd to copy only the amount
7701 # that is given by the OVMF_VARS.fd
62375438 7702 my $src_path = PVE::Storage::path($storecfg, $drive->{file}, $snapname);
818ce80e 7703 my $dst_path = PVE::Storage::path($storecfg, $newvolid);
fdfdc80e 7704
62375438
FE
7705 my $src_format = (PVE::Storage::parse_volname($storecfg, $drive->{file}))[6];
7706
fdfdc80e
FE
7707 # better for Ceph if block size is not too small, see bug #3324
7708 my $bs = 1024*1024;
7709
62375438
FE
7710 my $cmd = ['qemu-img', 'dd', '-n', '-O', $dst_format];
7711 push $cmd->@*, '-l', $snapname if $src_format eq 'qcow2' && $snapname;
7712 push $cmd->@*, "bs=$bs", "osize=$size", "if=$src_path", "of=$dst_path";
7713 run_command($cmd);
818ce80e
DC
7714 } else {
7715 qemu_img_convert($drive->{file}, $newvolid, $size, $snapname, $sparseinit);
7716 }
be190583 7717 }
152fe752
DM
7718 }
7719
7fe8b44c 7720no_data_clone:
3bae384f 7721 my ($size) = eval { PVE::Storage::volume_size_info($storecfg, $newvolid, 10) };
152fe752 7722
3b53c471
FE
7723 my $disk = dclone($drive);
7724 delete $disk->{format};
152fe752 7725 $disk->{file} = $newvolid;
3bae384f 7726 $disk->{size} = $size if defined($size);
152fe752
DM
7727
7728 return $disk;
7729}
7730
98cfd8b6
AD
7731sub get_running_qemu_version {
7732 my ($vmid) = @_;
0a13e08e 7733 my $res = mon_cmd($vmid, "query-version");
98cfd8b6
AD
7734 return "$res->{qemu}->{major}.$res->{qemu}->{minor}";
7735}
7736
249c4a6c
AD
7737sub qemu_use_old_bios_files {
7738 my ($machine_type) = @_;
7739
7740 return if !$machine_type;
7741
7742 my $use_old_bios_files = undef;
7743
7744 if ($machine_type =~ m/^(\S+)\.pxe$/) {
7745 $machine_type = $1;
7746 $use_old_bios_files = 1;
7747 } else {
4df98f2f 7748 my $version = extract_version($machine_type, kvm_user_version());
249c4a6c
AD
7749 # Note: kvm version < 2.4 use non-efi pxe files, and have problems when we
7750 # load new efi bios files on migration. So this hack is required to allow
7751 # live migration from qemu-2.2 to qemu-2.4, which is sometimes used when
7752 # updrading from proxmox-ve-3.X to proxmox-ve 4.0
2ea5fb7e 7753 $use_old_bios_files = !min_version($version, 2, 4);
249c4a6c
AD
7754 }
7755
7756 return ($use_old_bios_files, $machine_type);
7757}
7758
818ce80e 7759sub get_efivars_size {
ff84f0e3
FE
7760 my ($conf, $efidisk) = @_;
7761
818ce80e 7762 my $arch = get_vm_arch($conf);
ff84f0e3 7763 $efidisk //= $conf->{efidisk0} ? parse_drive('efidisk0', $conf->{efidisk0}) : undef;
90b20b15
DC
7764 my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf);
7765 my (undef, $ovmf_vars) = get_ovmf_files($arch, $efidisk, $smm);
818ce80e
DC
7766 die "uefi vars image '$ovmf_vars' not found\n" if ! -f $ovmf_vars;
7767 return -s $ovmf_vars;
7768}
7769
7770sub update_efidisk_size {
7771 my ($conf) = @_;
7772
7773 return if !defined($conf->{efidisk0});
7774
7775 my $disk = PVE::QemuServer::parse_drive('efidisk0', $conf->{efidisk0});
7776 $disk->{size} = get_efivars_size($conf);
7777 $conf->{efidisk0} = print_drive($disk);
7778
7779 return;
7780}
7781
f9dde219
SR
7782sub update_tpmstate_size {
7783 my ($conf) = @_;
7784
7785 my $disk = PVE::QemuServer::parse_drive('tpmstate0', $conf->{tpmstate0});
7786 $disk->{size} = PVE::QemuServer::Drive::TPMSTATE_DISK_SIZE;
7787 $conf->{tpmstate0} = print_drive($disk);
7788}
7789
90b20b15
DC
7790sub create_efidisk($$$$$$$) {
7791 my ($storecfg, $storeid, $vmid, $fmt, $arch, $efidisk, $smm) = @_;
3e1f1122 7792
90b20b15 7793 my (undef, $ovmf_vars) = get_ovmf_files($arch, $efidisk, $smm);
96ed3574 7794 die "EFI vars default image not found\n" if ! -f $ovmf_vars;
3e1f1122 7795
af1f1ec0
DC
7796 my $vars_size_b = -s $ovmf_vars;
7797 my $vars_size = PVE::Tools::convert_size($vars_size_b, 'b' => 'kb');
3e1f1122
TL
7798 my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
7799 PVE::Storage::activate_volumes($storecfg, [$volid]);
7800
af1f1ec0 7801 qemu_img_convert($ovmf_vars, $volid, $vars_size_b, undef, 0);
340dbcf7 7802 my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 3);
3e1f1122 7803
340dbcf7 7804 return ($volid, $size/1024);
3e1f1122
TL
7805}
7806
22de899a
AD
7807sub vm_iothreads_list {
7808 my ($vmid) = @_;
7809
0a13e08e 7810 my $res = mon_cmd($vmid, 'query-iothreads');
22de899a
AD
7811
7812 my $iothreads = {};
7813 foreach my $iothread (@$res) {
7814 $iothreads->{ $iothread->{id} } = $iothread->{"thread-id"};
7815 }
7816
7817 return $iothreads;
7818}
7819
ee034f5c
AD
7820sub scsihw_infos {
7821 my ($conf, $drive) = @_;
7822
7823 my $maxdev = 0;
7824
7fe1b688 7825 if (!$conf->{scsihw} || ($conf->{scsihw} =~ m/^lsi/)) {
ee034f5c 7826 $maxdev = 7;
a1511b3c 7827 } elsif ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
ee034f5c
AD
7828 $maxdev = 1;
7829 } else {
7830 $maxdev = 256;
7831 }
7832
7833 my $controller = int($drive->{index} / $maxdev);
4df98f2f
TL
7834 my $controller_prefix = ($conf->{scsihw} && $conf->{scsihw} eq 'virtio-scsi-single')
7835 ? "virtioscsi"
7836 : "scsihw";
ee034f5c
AD
7837
7838 return ($maxdev, $controller, $controller_prefix);
7839}
a1511b3c 7840
4317f69f
AD
7841sub windows_version {
7842 my ($ostype) = @_;
7843
7844 return 0 if !$ostype;
7845
7846 my $winversion = 0;
7847
7848 if($ostype eq 'wxp' || $ostype eq 'w2k3' || $ostype eq 'w2k') {
7849 $winversion = 5;
7850 } elsif($ostype eq 'w2k8' || $ostype eq 'wvista') {
7851 $winversion = 6;
7852 } elsif ($ostype =~ m/^win(\d+)$/) {
7853 $winversion = $1;
7854 }
7855
7856 return $winversion;
7857}
7858
44549149
EK
7859sub resolve_dst_disk_format {
7860 my ($storecfg, $storeid, $src_volname, $format) = @_;
7861 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
7862
7863 if (!$format) {
7864 # if no target format is specified, use the source disk format as hint
7865 if ($src_volname) {
7866 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
7867 $format = qemu_img_format($scfg, $src_volname);
7868 } else {
7869 return $defFormat;
7870 }
7871 }
7872
7873 # test if requested format is supported - else use default
7874 my $supported = grep { $_ eq $format } @$validFormats;
7875 $format = $defFormat if !$supported;
7876 return $format;
7877}
7878
66cebc46
DC
7879# NOTE: if this logic changes, please update docs & possibly gui logic
7880sub find_vmstate_storage {
7881 my ($conf, $storecfg) = @_;
7882
7883 # first, return storage from conf if set
7884 return $conf->{vmstatestorage} if $conf->{vmstatestorage};
7885
7886 my ($target, $shared, $local);
7887
7888 foreach_storage_used_by_vm($conf, sub {
7889 my ($sid) = @_;
7890 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
7891 my $dst = $scfg->{shared} ? \$shared : \$local;
7892 $$dst = $sid if !$$dst || $scfg->{path}; # prefer file based storage
7893 });
7894
7895 # second, use shared storage where VM has at least one disk
7896 # third, use local storage where VM has at least one disk
7897 # fall back to local storage
7898 $target = $shared // $local // 'local';
7899
7900 return $target;
7901}
7902
6ee499ff 7903sub generate_uuid {
ae2fcb3b
EK
7904 my ($uuid, $uuid_str);
7905 UUID::generate($uuid);
7906 UUID::unparse($uuid, $uuid_str);
6ee499ff
DC
7907 return $uuid_str;
7908}
7909
7910sub generate_smbios1_uuid {
7911 return "uuid=".generate_uuid();
ae2fcb3b
EK
7912}
7913
9c152e87
TL
7914sub nbd_stop {
7915 my ($vmid) = @_;
7916
0a13e08e 7917 mon_cmd($vmid, 'nbd-server-stop');
9c152e87
TL
7918}
7919
dae98db9
DC
7920sub create_reboot_request {
7921 my ($vmid) = @_;
7922 open(my $fh, '>', "/run/qemu-server/$vmid.reboot")
7923 or die "failed to create reboot trigger file: $!\n";
7924 close($fh);
7925}
7926
7927sub clear_reboot_request {
7928 my ($vmid) = @_;
7929 my $path = "/run/qemu-server/$vmid.reboot";
7930 my $res = 0;
7931
7932 $res = unlink($path);
7933 die "could not remove reboot request for $vmid: $!"
7934 if !$res && $! != POSIX::ENOENT;
7935
7936 return $res;
7937}
7938
5cfa9f5f
SR
7939sub bootorder_from_legacy {
7940 my ($conf, $bootcfg) = @_;
7941
7942 my $boot = $bootcfg->{legacy} || $boot_fmt->{legacy}->{default};
7943 my $bootindex_hash = {};
7944 my $i = 1;
7945 foreach my $o (split(//, $boot)) {
7946 $bootindex_hash->{$o} = $i*100;
7947 $i++;
7948 }
7949
7950 my $bootorder = {};
7951
7952 PVE::QemuConfig->foreach_volume($conf, sub {
7953 my ($ds, $drive) = @_;
7954
7955 if (drive_is_cdrom ($drive, 1)) {
7956 if ($bootindex_hash->{d}) {
7957 $bootorder->{$ds} = $bootindex_hash->{d};
7958 $bootindex_hash->{d} += 1;
7959 }
7960 } elsif ($bootindex_hash->{c}) {
7961 $bootorder->{$ds} = $bootindex_hash->{c}
7962 if $conf->{bootdisk} && $conf->{bootdisk} eq $ds;
7963 $bootindex_hash->{c} += 1;
7964 }
7965 });
7966
7967 if ($bootindex_hash->{n}) {
7968 for (my $i = 0; $i < $MAX_NETS; $i++) {
7969 my $netname = "net$i";
7970 next if !$conf->{$netname};
7971 $bootorder->{$netname} = $bootindex_hash->{n};
7972 $bootindex_hash->{n} += 1;
7973 }
7974 }
7975
7976 return $bootorder;
7977}
7978
7979# Generate default device list for 'boot: order=' property. Matches legacy
7980# default boot order, but with explicit device names. This is important, since
7981# the fallback for when neither 'order' nor the old format is specified relies
7982# on 'bootorder_from_legacy' above, and it would be confusing if this diverges.
7983sub get_default_bootdevices {
7984 my ($conf) = @_;
7985
7986 my @ret = ();
7987
7988 # harddisk
7989 my $first = PVE::QemuServer::Drive::resolve_first_disk($conf, 0);
7990 push @ret, $first if $first;
7991
7992 # cdrom
7993 $first = PVE::QemuServer::Drive::resolve_first_disk($conf, 1);
7994 push @ret, $first if $first;
7995
7996 # network
7997 for (my $i = 0; $i < $MAX_NETS; $i++) {
7998 my $netname = "net$i";
7999 next if !$conf->{$netname};
8000 push @ret, $netname;
8001 last;
8002 }
8003
8004 return \@ret;
8005}
8006
e5d611c3
TL
8007sub device_bootorder {
8008 my ($conf) = @_;
8009
8010 return bootorder_from_legacy($conf) if !defined($conf->{boot});
8011
8012 my $boot = parse_property_string($boot_fmt, $conf->{boot});
8013
8014 my $bootorder = {};
8015 if (!defined($boot) || $boot->{legacy}) {
8016 $bootorder = bootorder_from_legacy($conf, $boot);
8017 } elsif ($boot->{order}) {
8018 my $i = 100; # start at 100 to allow user to insert devices before us with -args
8019 for my $dev (PVE::Tools::split_list($boot->{order})) {
8020 $bootorder->{$dev} = $i++;
8021 }
8022 }
8023
8024 return $bootorder;
8025}
8026
65911545
SR
8027sub register_qmeventd_handle {
8028 my ($vmid) = @_;
8029
8030 my $fh;
8031 my $peer = "/var/run/qmeventd.sock";
8032 my $count = 0;
8033
8034 for (;;) {
8035 $count++;
8036 $fh = IO::Socket::UNIX->new(Peer => $peer, Blocking => 0, Timeout => 1);
8037 last if $fh;
8038 if ($! != EINTR && $! != EAGAIN) {
8039 die "unable to connect to qmeventd socket (vmid: $vmid) - $!\n";
8040 }
8041 if ($count > 4) {
8042 die "unable to connect to qmeventd socket (vmid: $vmid) - timeout "
8043 . "after $count retries\n";
8044 }
8045 usleep(25000);
8046 }
8047
8048 # send handshake to mark VM as backing up
8049 print $fh to_json({vzdump => {vmid => "$vmid"}});
8050
8051 # return handle to be closed later when inhibit is no longer required
8052 return $fh;
8053}
8054
65e866e5
DM
8055# bash completion helper
8056
8057sub complete_backup_archives {
8058 my ($cmdname, $pname, $cvalue) = @_;
8059
8060 my $cfg = PVE::Storage::config();
8061
8062 my $storeid;
8063
8064 if ($cvalue =~ m/^([^:]+):/) {
8065 $storeid = $1;
8066 }
8067
8068 my $data = PVE::Storage::template_list($cfg, $storeid, 'backup');
8069
8070 my $res = [];
8071 foreach my $id (keys %$data) {
8072 foreach my $item (@{$data->{$id}}) {
f43a4f12 8073 next if $item->{format} !~ m/^vma\.(${\PVE::Storage::Plugin::COMPRESSOR_RE})$/;
65e866e5
DM
8074 push @$res, $item->{volid} if defined($item->{volid});
8075 }
8076 }
8077
8078 return $res;
8079}
8080
8081my $complete_vmid_full = sub {
8082 my ($running) = @_;
8083
8084 my $idlist = vmstatus();
8085
8086 my $res = [];
8087
8088 foreach my $id (keys %$idlist) {
8089 my $d = $idlist->{$id};
8090 if (defined($running)) {
8091 next if $d->{template};
8092 next if $running && $d->{status} ne 'running';
8093 next if !$running && $d->{status} eq 'running';
8094 }
8095 push @$res, $id;
8096
8097 }
8098 return $res;
8099};
8100
8101sub complete_vmid {
8102 return &$complete_vmid_full();
8103}
8104
8105sub complete_vmid_stopped {
8106 return &$complete_vmid_full(0);
8107}
8108
8109sub complete_vmid_running {
8110 return &$complete_vmid_full(1);
8111}
8112
335af808
DM
8113sub complete_storage {
8114
8115 my $cfg = PVE::Storage::config();
8116 my $ids = $cfg->{ids};
8117
8118 my $res = [];
8119 foreach my $sid (keys %$ids) {
8120 next if !PVE::Storage::storage_check_enabled($cfg, $sid, undef, 1);
c4c844ef 8121 next if !$ids->{$sid}->{content}->{images};
335af808
DM
8122 push @$res, $sid;
8123 }
8124
8125 return $res;
8126}
8127
255e9c54
AL
8128sub complete_migration_storage {
8129 my ($cmd, $param, $current_value, $all_args) = @_;
8130
8131 my $targetnode = @$all_args[1];
8132
8133 my $cfg = PVE::Storage::config();
8134 my $ids = $cfg->{ids};
8135
8136 my $res = [];
8137 foreach my $sid (keys %$ids) {
8138 next if !PVE::Storage::storage_check_enabled($cfg, $sid, $targetnode, 1);
8139 next if !$ids->{$sid}->{content}->{images};
8140 push @$res, $sid;
8141 }
8142
8143 return $res;
8144}
8145
b08c37c3
DC
8146sub vm_is_paused {
8147 my ($vmid) = @_;
8148 my $qmpstatus = eval {
8149 PVE::QemuConfig::assert_config_exists_on_node($vmid);
8150 mon_cmd($vmid, "query-status");
8151 };
8152 warn "$@\n" if $@;
8153 return $qmpstatus && $qmpstatus->{status} eq "paused";
8154}
8155
3f11f0d7
LS
8156sub check_volume_storage_type {
8157 my ($storecfg, $vol) = @_;
8158
8159 my ($storeid, $volname) = PVE::Storage::parse_volume_id($vol);
8160 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
8161 my ($vtype) = PVE::Storage::parse_volname($storecfg, $vol);
8162
8163 die "storage '$storeid' does not support content-type '$vtype'\n"
8164 if !$scfg->{content}->{$vtype};
8165
8166 return 1;
8167}
8168
1e3baf05 81691;