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