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