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