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