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