]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer.pm
bump version to 3.1-29
[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;
26f11676 30use PVE::QMPClient;
91bd6c90 31use PVE::RPCEnvironment;
6b64503e 32use Time::HiRes qw(gettimeofday);
1e3baf05 33
7f0b5beb 34my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
1e3baf05 35
19672434 36# Note about locking: we use flock on the config file protect
1e3baf05
DM
37# against concurent actions.
38# Aditionaly, we have a 'lock' setting in the config file. This
22c377f0 39# can be set to 'migrate', 'backup', 'snapshot' or 'rollback'. Most actions are not
1e3baf05
DM
40# allowed when such lock is set. But you can ignore this kind of
41# lock with the --skiplock flag.
42
97d62eb7 43cfs_register_file('/qemu-server/',
1858638f
DM
44 \&parse_vm_config,
45 \&write_vm_config);
1e3baf05 46
3ea94c60
DM
47PVE::JSONSchema::register_standard_option('skiplock', {
48 description => "Ignore locks - only root is allowed to use this option.",
afdb31d5 49 type => 'boolean',
3ea94c60
DM
50 optional => 1,
51});
52
53PVE::JSONSchema::register_standard_option('pve-qm-stateuri', {
54 description => "Some command save/restore state from this location.",
55 type => 'string',
56 maxLength => 128,
57 optional => 1,
58});
59
8abd398b
DM
60PVE::JSONSchema::register_standard_option('pve-snapshot-name', {
61 description => "The name of the snapshot.",
62 type => 'string', format => 'pve-configid',
63 maxLength => 40,
64});
65
1e3baf05
DM
66#no warnings 'redefine';
67
68unless(defined(&_VZSYSCALLS_H_)) {
69 eval 'sub _VZSYSCALLS_H_ () {1;}' unless defined(&_VZSYSCALLS_H_);
70 require 'sys/syscall.ph';
71 if(defined(&__x86_64__)) {
72 eval 'sub __NR_fairsched_vcpus () {499;}' unless defined(&__NR_fairsched_vcpus);
73 eval 'sub __NR_fairsched_mknod () {504;}' unless defined(&__NR_fairsched_mknod);
74 eval 'sub __NR_fairsched_rmnod () {505;}' unless defined(&__NR_fairsched_rmnod);
75 eval 'sub __NR_fairsched_chwt () {506;}' unless defined(&__NR_fairsched_chwt);
76 eval 'sub __NR_fairsched_mvpr () {507;}' unless defined(&__NR_fairsched_mvpr);
77 eval 'sub __NR_fairsched_rate () {508;}' unless defined(&__NR_fairsched_rate);
78 eval 'sub __NR_setluid () {501;}' unless defined(&__NR_setluid);
79 eval 'sub __NR_setublimit () {502;}' unless defined(&__NR_setublimit);
80 }
81 elsif(defined( &__i386__) ) {
82 eval 'sub __NR_fairsched_mknod () {500;}' unless defined(&__NR_fairsched_mknod);
83 eval 'sub __NR_fairsched_rmnod () {501;}' unless defined(&__NR_fairsched_rmnod);
84 eval 'sub __NR_fairsched_chwt () {502;}' unless defined(&__NR_fairsched_chwt);
85 eval 'sub __NR_fairsched_mvpr () {503;}' unless defined(&__NR_fairsched_mvpr);
86 eval 'sub __NR_fairsched_rate () {504;}' unless defined(&__NR_fairsched_rate);
87 eval 'sub __NR_fairsched_vcpus () {505;}' unless defined(&__NR_fairsched_vcpus);
88 eval 'sub __NR_setluid () {511;}' unless defined(&__NR_setluid);
89 eval 'sub __NR_setublimit () {512;}' unless defined(&__NR_setublimit);
90 } else {
91 die("no fairsched syscall for this arch");
92 }
93 require 'asm/ioctl.ph';
94 eval 'sub KVM_GET_API_VERSION () { &_IO(0xAE, 0x);}' unless defined(&KVM_GET_API_VERSION);
95}
96
97sub fairsched_mknod {
98 my ($parent, $weight, $desired) = @_;
99
6b64503e 100 return syscall(&__NR_fairsched_mknod, int($parent), int($weight), int($desired));
1e3baf05
DM
101}
102
103sub fairsched_rmnod {
104 my ($id) = @_;
105
6b64503e 106 return syscall(&__NR_fairsched_rmnod, int($id));
1e3baf05
DM
107}
108
109sub fairsched_mvpr {
110 my ($pid, $newid) = @_;
111
6b64503e 112 return syscall(&__NR_fairsched_mvpr, int($pid), int($newid));
1e3baf05
DM
113}
114
115sub fairsched_vcpus {
116 my ($id, $vcpus) = @_;
117
6b64503e 118 return syscall(&__NR_fairsched_vcpus, int($id), int($vcpus));
1e3baf05
DM
119}
120
121sub fairsched_rate {
122 my ($id, $op, $rate) = @_;
123
6b64503e 124 return syscall(&__NR_fairsched_rate, int($id), int($op), int($rate));
1e3baf05
DM
125}
126
127use constant FAIRSCHED_SET_RATE => 0;
128use constant FAIRSCHED_DROP_RATE => 1;
129use constant FAIRSCHED_GET_RATE => 2;
130
131sub fairsched_cpulimit {
132 my ($id, $limit) = @_;
133
6b64503e 134 my $cpulim1024 = int($limit * 1024 / 100);
1e3baf05
DM
135 my $op = $cpulim1024 ? FAIRSCHED_SET_RATE : FAIRSCHED_DROP_RATE;
136
6b64503e 137 return fairsched_rate($id, $op, $cpulim1024);
1e3baf05
DM
138}
139
140my $nodename = PVE::INotify::nodename();
141
142mkdir "/etc/pve/nodes/$nodename";
143my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
144mkdir $confdir;
145
146my $var_run_tmpdir = "/var/run/qemu-server";
147mkdir $var_run_tmpdir;
148
149my $lock_dir = "/var/lock/qemu-server";
150mkdir $lock_dir;
151
152my $pcisysfs = "/sys/bus/pci";
153
1e3baf05
DM
154my $confdesc = {
155 onboot => {
156 optional => 1,
157 type => 'boolean',
158 description => "Specifies whether a VM will be started during system bootup.",
159 default => 0,
160 },
161 autostart => {
162 optional => 1,
163 type => 'boolean',
164 description => "Automatic restart after crash (currently ignored).",
165 default => 0,
166 },
2ff09f52
DA
167 hotplug => {
168 optional => 1,
e8b9c17c 169 type => 'boolean',
6c52b679 170 description => "Allow hotplug for disk and network device",
2dbe827e 171 default => 0,
2ff09f52 172 },
1e3baf05
DM
173 reboot => {
174 optional => 1,
175 type => 'boolean',
176 description => "Allow reboot. If set to '0' the VM exit on reboot.",
177 default => 1,
178 },
179 lock => {
180 optional => 1,
181 type => 'string',
182 description => "Lock/unlock the VM.",
22c377f0 183 enum => [qw(migrate backup snapshot rollback)],
1e3baf05
DM
184 },
185 cpulimit => {
186 optional => 1,
187 type => 'integer',
188 description => "Limit of CPU usage in per cent. Note if the computer has 2 CPUs, it has total of 200% CPU time. Value '0' indicates no CPU limit.\n\nNOTE: This option is currently ignored.",
189 minimum => 0,
190 default => 0,
191 },
192 cpuunits => {
193 optional => 1,
194 type => 'integer',
195 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.",
196 minimum => 0,
197 maximum => 500000,
198 default => 1000,
199 },
200 memory => {
201 optional => 1,
202 type => 'integer',
7878afeb 203 description => "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device.",
1e3baf05
DM
204 minimum => 16,
205 default => 512,
206 },
13a48620
DA
207 balloon => {
208 optional => 1,
209 type => 'integer',
8b1accf7
DM
210 description => "Amount of target RAM for the VM in MB. Using zero disables the ballon driver.",
211 minimum => 0,
212 },
213 shares => {
214 optional => 1,
215 type => 'integer',
216 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",
217 minimum => 0,
218 maximum => 50000,
219 default => 1000,
13a48620 220 },
1e3baf05
DM
221 keyboard => {
222 optional => 1,
223 type => 'string',
224 description => "Keybord layout for vnc server. Default is read from the datacenter configuration file.",
e95fe75f 225 enum => PVE::Tools::kvmkeymaplist(),
1e3baf05
DM
226 default => 'en-us',
227 },
228 name => {
229 optional => 1,
7fabe17d 230 type => 'string', format => 'dns-name',
1e3baf05
DM
231 description => "Set a name for the VM. Only used on the configuration web interface.",
232 },
cdd20088
AD
233 scsihw => {
234 optional => 1,
235 type => 'string',
236 description => "scsi controller model",
5b952ff5 237 enum => [qw(lsi lsi53c810 virtio-scsi-pci megasas pvscsi)],
cdd20088
AD
238 default => 'lsi',
239 },
1e3baf05
DM
240 description => {
241 optional => 1,
242 type => 'string',
0581fe4f 243 description => "Description for the VM. Only used on the configuration web interface. This is saved as comment inside the configuration file.",
1e3baf05
DM
244 },
245 ostype => {
246 optional => 1,
247 type => 'string',
6b9d84cf 248 enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 l24 l26 solaris)],
1e3baf05
DM
249 description => <<EODESC,
250Used to enable special optimization/features for specific
251operating systems:
252
253other => unspecified OS
254wxp => Microsoft Windows XP
255w2k => Microsoft Windows 2000
256w2k3 => Microsoft Windows 2003
257w2k8 => Microsoft Windows 2008
258wvista => Microsoft Windows Vista
259win7 => Microsoft Windows 7
a70ebde3 260win8 => Microsoft Windows 8/2012
1e3baf05
DM
261l24 => Linux 2.4 Kernel
262l26 => Linux 2.6/3.X Kernel
6b9d84cf 263solaris => solaris/opensolaris/openindiania kernel
1e3baf05 264
6b9d84cf 265other|l24|l26|solaris ... no special behaviour
a70ebde3 266wxp|w2k|w2k3|w2k8|wvista|win7|win8 ... use --localtime switch
1e3baf05
DM
267EODESC
268 },
269 boot => {
270 optional => 1,
271 type => 'string',
272 description => "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n).",
273 pattern => '[acdn]{1,4}',
32baffb4 274 default => 'cdn',
1e3baf05
DM
275 },
276 bootdisk => {
277 optional => 1,
278 type => 'string', format => 'pve-qm-bootdisk',
279 description => "Enable booting from specified disk.",
03e480fc 280 pattern => '(ide|sata|scsi|virtio)\d+',
1e3baf05
DM
281 },
282 smp => {
283 optional => 1,
284 type => 'integer',
285 description => "The number of CPUs. Please use option -sockets instead.",
286 minimum => 1,
287 default => 1,
288 },
289 sockets => {
290 optional => 1,
291 type => 'integer',
292 description => "The number of CPU sockets.",
293 minimum => 1,
294 default => 1,
295 },
296 cores => {
297 optional => 1,
298 type => 'integer',
299 description => "The number of cores per socket.",
300 minimum => 1,
301 default => 1,
302 },
3bd18e48
AD
303 maxcpus => {
304 optional => 1,
305 type => 'integer',
306 description => "Maximum cpus for hotplug.",
307 minimum => 1,
308 default => 1,
309 },
1e3baf05
DM
310 acpi => {
311 optional => 1,
312 type => 'boolean',
313 description => "Enable/disable ACPI.",
314 default => 1,
315 },
bc84dcca 316 agent => {
ab6a046f
AD
317 optional => 1,
318 type => 'boolean',
319 description => "Enable/disable Qemu GuestAgent.",
be79c214 320 default => 0,
ab6a046f 321 },
1e3baf05
DM
322 kvm => {
323 optional => 1,
324 type => 'boolean',
325 description => "Enable/disable KVM hardware virtualization.",
326 default => 1,
327 },
328 tdf => {
329 optional => 1,
330 type => 'boolean',
8c559505
DM
331 description => "Enable/disable time drift fix.",
332 default => 0,
1e3baf05 333 },
19672434 334 localtime => {
1e3baf05
DM
335 optional => 1,
336 type => 'boolean',
337 description => "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS.",
338 },
339 freeze => {
340 optional => 1,
341 type => 'boolean',
342 description => "Freeze CPU at startup (use 'c' monitor command to start execution).",
343 },
344 vga => {
345 optional => 1,
346 type => 'string',
ef5e2be2 347 description => "Select VGA type. If you want to use high resolution modes (>= 1280x1024x16) then you should use option 'std' or 'vmware'. Default is 'std' for win8/win7/w2k8, and 'cirrur' for other OS types. Option 'qxl' enables the SPICE display sever. You can also run without any graphic card using a serial devive as terminal.",
2fa3151e 348 enum => [qw(std cirrus vmware qxl serial0 serial1 serial2 serial3 qxl2 qxl3 qxl4)],
1e3baf05 349 },
0ea9541d
DM
350 watchdog => {
351 optional => 1,
352 type => 'string', format => 'pve-qm-watchdog',
353 typetext => '[[model=]i6300esb|ib700] [,[action=]reset|shutdown|poweroff|pause|debug|none]',
354 description => "Create a virtual hardware watchdog device. Once enabled (by a guest action), the watchdog must be periodically polled by an agent inside the guest or else the guest will be restarted (or execute the action specified)",
355 },
1e3baf05
DM
356 startdate => {
357 optional => 1,
19672434 358 type => 'string',
1e3baf05
DM
359 typetext => "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)",
360 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'.",
361 pattern => '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)',
362 default => 'now',
363 },
59411c4e
DM
364 startup => {
365 optional => 1,
366 type => 'string', format => 'pve-qm-startup',
367 typetext => '[[order=]\d+] [,up=\d+] [,down=\d+] ',
368 description => "Startup and shutdown behavior. Order is a non-negative number defining the general startup order. Shutdown in done with reverse ordering. Additionally you can set the 'up' or 'down' delay in seconds, which specifies a delay to wait before the next VM is started or stopped.",
369 },
68eda3ab
AD
370 template => {
371 optional => 1,
372 type => 'boolean',
373 description => "Enable/disable Template.",
374 default => 0,
375 },
1e3baf05
DM
376 args => {
377 optional => 1,
378 type => 'string',
379 description => <<EODESCR,
380Note: this option is for experts only. It allows you to pass arbitrary arguments to kvm, for example:
381
382args: -no-reboot -no-hpet
383EODESCR
384 },
385 tablet => {
386 optional => 1,
387 type => 'boolean',
388 default => 1,
5acbfe9e 389 description => "Enable/disable the usb tablet device. This device is usually needed to allow absolute mouse positioning with VNC. Else the mouse runs out of sync with normal VNC clients. If you're running lots of console-only guests on one host, you may consider disabling this to save some context switches. This is turned of by default if you use spice (vga=qxl).",
1e3baf05
DM
390 },
391 migrate_speed => {
392 optional => 1,
393 type => 'integer',
394 description => "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
395 minimum => 0,
396 default => 0,
397 },
398 migrate_downtime => {
399 optional => 1,
04432191 400 type => 'number',
1e3baf05
DM
401 description => "Set maximum tolerated downtime (in seconds) for migrations.",
402 minimum => 0,
04432191 403 default => 0.1,
1e3baf05
DM
404 },
405 cdrom => {
406 optional => 1,
407 type => 'string', format => 'pve-qm-drive',
408 typetext => 'volume',
409 description => "This is an alias for option -ide2",
410 },
411 cpu => {
412 optional => 1,
413 description => "Emulated CPU type.",
414 type => 'string',
98a0dc5e 415 enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge Haswell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
eac6899d 416 default => 'kvm64',
1e3baf05 417 },
b7ba6b79
DM
418 parent => get_standard_option('pve-snapshot-name', {
419 optional => 1,
420 description => "Parent snapshot name. This is used internally, and should not be modified.",
421 }),
982c7f12
DM
422 snaptime => {
423 optional => 1,
424 description => "Timestamp for snapshots.",
425 type => 'integer',
426 minimum => 0,
427 },
18bfb361
DM
428 vmstate => {
429 optional => 1,
430 type => 'string', format => 'pve-volume-id',
431 description => "Reference to a volume which stores the VM state. This is used internally for snapshots.",
432 },
3bafc510
DM
433 machine => {
434 description => "Specific the Qemu machine type.",
435 type => 'string',
436 pattern => '(pc|pc(-i440fx)?-\d+\.\d+|q35|pc-q35-\d+\.\d+)',
437 maxLength => 40,
438 optional => 1,
439 },
2796e7d5
DM
440 smbios1 => {
441 description => "Specify SMBIOS type 1 fields.",
442 type => 'string', format => 'pve-qm-smbios1',
443 typetext => "[manufacturer=str][,product=str][,version=str][,serial=str] [,uuid=uuid][,sku=str][,family=str]",
444 maxLength => 256,
445 optional => 1,
446 },
1e3baf05
DM
447};
448
449# what about other qemu settings ?
450#cpu => 'string',
451#machine => 'string',
452#fda => 'file',
453#fdb => 'file',
454#mtdblock => 'file',
455#sd => 'file',
456#pflash => 'file',
457#snapshot => 'bool',
458#bootp => 'file',
459##tftp => 'dir',
460##smb => 'dir',
461#kernel => 'file',
462#append => 'string',
463#initrd => 'file',
464##soundhw => 'string',
465
466while (my ($k, $v) = each %$confdesc) {
467 PVE::JSONSchema::register_standard_option("pve-qm-$k", $v);
468}
469
470my $MAX_IDE_DISKS = 4;
f62db2a4 471my $MAX_SCSI_DISKS = 14;
a2650619 472my $MAX_VIRTIO_DISKS = 16;
cdb0931f 473my $MAX_SATA_DISKS = 6;
1e3baf05 474my $MAX_USB_DEVICES = 5;
5bdcf937 475my $MAX_NETS = 32;
1e3baf05 476my $MAX_UNUSED_DISKS = 8;
040b06b7 477my $MAX_HOSTPCI_DEVICES = 2;
bae179aa 478my $MAX_SERIAL_PORTS = 4;
1989a89c 479my $MAX_PARALLEL_PORTS = 3;
1e3baf05
DM
480
481my $nic_model_list = ['rtl8139', 'ne2k_pci', 'e1000', 'pcnet', 'virtio',
e4c6e0b8 482 'ne2k_isa', 'i82551', 'i82557b', 'i82559er', 'vmxnet3'];
6b64503e 483my $nic_model_list_txt = join(' ', sort @$nic_model_list);
1e3baf05 484
1e3baf05
DM
485my $netdesc = {
486 optional => 1,
487 type => 'string', format => 'pve-qm-net',
a9410357 488 typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,queues=<nbqueues>][,rate=<mbps>][,tag=<vlanid>][,firewall=0|1]",
1e3baf05 489 description => <<EODESCR,
19672434 490Specify network devices.
1e3baf05
DM
491
492MODEL is one of: $nic_model_list_txt
493
19672434 494XX:XX:XX:XX:XX:XX should be an unique MAC address. This is
1e3baf05
DM
495automatically generated if not specified.
496
497The bridge parameter can be used to automatically add the interface to a bridge device. The Proxmox VE standard bridge is called 'vmbr0'.
498
499Option 'rate' is used to limit traffic bandwidth from and to this interface. It is specified as floating point number, unit is 'Megabytes per second'.
500
501If you specify no bridge, we create a kvm 'user' (NATed) network device, which provides DHCP and DNS services. The following addresses are used:
502
50310.0.2.2 Gateway
50410.0.2.3 DNS Server
50510.0.2.4 SMB Server
506
507The DHCP server assign addresses to the guest starting from 10.0.2.15.
508
509EODESCR
510};
511PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
512
513for (my $i = 0; $i < $MAX_NETS; $i++) {
514 $confdesc->{"net$i"} = $netdesc;
515}
516
517my $drivename_hash;
19672434 518
1e3baf05
DM
519my $idedesc = {
520 optional => 1,
521 type => 'string', format => 'pve-qm-drive',
8d87f8aa 522 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
3c770faa 523 description => "Use volume as IDE hard disk or CD-ROM (n is 0 to " .($MAX_IDE_DISKS -1) . ").",
1e3baf05
DM
524};
525PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
526
527my $scsidesc = {
528 optional => 1,
529 type => 'string', format => 'pve-qm-drive',
8d87f8aa 530 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
3c770faa 531 description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . ").",
1e3baf05
DM
532};
533PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc);
534
cdb0931f
DA
535my $satadesc = {
536 optional => 1,
537 type => 'string', format => 'pve-qm-drive',
8d87f8aa 538 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
3c770faa 539 description => "Use volume as SATA hard disk or CD-ROM (n is 0 to " . ($MAX_SATA_DISKS - 1). ").",
cdb0931f
DA
540};
541PVE::JSONSchema::register_standard_option("pve-qm-sata", $satadesc);
542
1e3baf05
DM
543my $virtiodesc = {
544 optional => 1,
545 type => 'string', format => 'pve-qm-drive',
8d87f8aa 546 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
3c770faa 547 description => "Use volume as VIRTIO hard disk (n is 0 to " . ($MAX_VIRTIO_DISKS - 1) . ").",
1e3baf05
DM
548};
549PVE::JSONSchema::register_standard_option("pve-qm-virtio", $virtiodesc);
550
551my $usbdesc = {
552 optional => 1,
553 type => 'string', format => 'pve-qm-usb-device',
80401dd8 554 typetext => 'host=HOSTUSBDEVICE|spice',
1e3baf05 555 description => <<EODESCR,
2fe1a152 556Configure an USB device (n is 0 to 4). This can be used to
1e3baf05
DM
557pass-through usb devices to the guest. HOSTUSBDEVICE syntax is:
558
19672434 559'bus-port(.port)*' (decimal numbers) or
1e3baf05
DM
560'vendor_id:product_id' (hexadeciaml numbers)
561
19672434 562You can use the 'lsusb -t' command to list existing usb devices.
1e3baf05
DM
563
564Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
565
80401dd8
DM
566The value 'spice' can be used to add a usb redirection devices for spice.
567
1e3baf05
DM
568EODESCR
569};
570PVE::JSONSchema::register_standard_option("pve-qm-usb", $usbdesc);
571
040b06b7
DA
572my $hostpcidesc = {
573 optional => 1,
574 type => 'string', format => 'pve-qm-hostpci',
2e3b7e2a 575 typetext => "[host=]HOSTPCIDEVICE [,driver=kvm|vfio] [,rombar=on|off] [,pcie=0|1] [,x-vga=on|off]",
040b06b7
DA
576 description => <<EODESCR,
577Map host pci devices. HOSTPCIDEVICE syntax is:
578
579'bus:dev.func' (hexadecimal numbers)
580
581You can us the 'lspci' command to list existing pci devices.
582
0cea6a01
DM
583The 'rombar' option determines whether or not the device's ROM will be visible in the guest's memory map (default is 'on').
584
040b06b7
DA
585Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
586
587Experimental: user reported problems with this option.
588EODESCR
589};
590PVE::JSONSchema::register_standard_option("pve-qm-hostpci", $hostpcidesc);
591
bae179aa
DA
592my $serialdesc = {
593 optional => 1,
ca0cef26 594 type => 'string',
9f9d2fb2 595 pattern => '(/dev/ttyS\d+|socket)',
bae179aa 596 description => <<EODESCR,
9f9d2fb2 597Create a serial device inside the VM (n is 0 to 3), and pass through a host serial device, or create a unix socket on the host side (use 'qm terminal' to open a terminal connection).
bae179aa
DA
598
599Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
600
601Experimental: user reported problems with this option.
602EODESCR
603};
bae179aa 604
1989a89c
DA
605my $paralleldesc= {
606 optional => 1,
ca0cef26 607 type => 'string',
9ecc8431 608 pattern => '/dev/parport\d+|/dev/usb/lp\d+',
1989a89c 609 description => <<EODESCR,
19672434 610Map host parallel devices (n is 0 to 2).
1989a89c
DA
611
612Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
613
614Experimental: user reported problems with this option.
615EODESCR
616};
1989a89c
DA
617
618for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
619 $confdesc->{"parallel$i"} = $paralleldesc;
620}
621
bae179aa
DA
622for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
623 $confdesc->{"serial$i"} = $serialdesc;
624}
625
040b06b7
DA
626for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
627 $confdesc->{"hostpci$i"} = $hostpcidesc;
628}
1e3baf05
DM
629
630for (my $i = 0; $i < $MAX_IDE_DISKS; $i++) {
631 $drivename_hash->{"ide$i"} = 1;
632 $confdesc->{"ide$i"} = $idedesc;
633}
634
cdb0931f
DA
635for (my $i = 0; $i < $MAX_SATA_DISKS; $i++) {
636 $drivename_hash->{"sata$i"} = 1;
637 $confdesc->{"sata$i"} = $satadesc;
638}
639
1e3baf05
DM
640for (my $i = 0; $i < $MAX_SCSI_DISKS; $i++) {
641 $drivename_hash->{"scsi$i"} = 1;
642 $confdesc->{"scsi$i"} = $scsidesc ;
643}
644
645for (my $i = 0; $i < $MAX_VIRTIO_DISKS; $i++) {
646 $drivename_hash->{"virtio$i"} = 1;
647 $confdesc->{"virtio$i"} = $virtiodesc;
648}
649
650for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
651 $confdesc->{"usb$i"} = $usbdesc;
652}
653
654my $unuseddesc = {
655 optional => 1,
656 type => 'string', format => 'pve-volume-id',
657 description => "Reference to unused volumes.",
658};
659
660for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
661 $confdesc->{"unused$i"} = $unuseddesc;
662}
663
664my $kvm_api_version = 0;
665
666sub kvm_version {
667
668 return $kvm_api_version if $kvm_api_version;
669
6b64503e 670 my $fh = IO::File->new("</dev/kvm") ||
1e3baf05
DM
671 return 0;
672
6b64503e 673 if (my $v = $fh->ioctl(KVM_GET_API_VERSION(), 0)) {
1e3baf05
DM
674 $kvm_api_version = $v;
675 }
676
677 $fh->close();
678
679 return $kvm_api_version;
680}
681
682my $kvm_user_version;
683
684sub kvm_user_version {
685
686 return $kvm_user_version if $kvm_user_version;
687
688 $kvm_user_version = 'unknown';
689
690 my $tmp = `kvm -help 2>/dev/null`;
19672434 691
fa7ae705 692 if ($tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)[,\s]/) {
1e3baf05
DM
693 $kvm_user_version = $2;
694 }
695
696 return $kvm_user_version;
697
698}
699
700my $kernel_has_vhost_net = -c '/dev/vhost-net';
701
702sub disknames {
703 # order is important - used to autoselect boot disk
19672434 704 return ((map { "ide$_" } (0 .. ($MAX_IDE_DISKS - 1))),
1e3baf05 705 (map { "scsi$_" } (0 .. ($MAX_SCSI_DISKS - 1))),
cdb0931f
DA
706 (map { "virtio$_" } (0 .. ($MAX_VIRTIO_DISKS - 1))),
707 (map { "sata$_" } (0 .. ($MAX_SATA_DISKS - 1))));
1e3baf05
DM
708}
709
710sub valid_drivename {
711 my $dev = shift;
712
6b64503e 713 return defined($drivename_hash->{$dev});
1e3baf05
DM
714}
715
716sub option_exists {
717 my $key = shift;
718 return defined($confdesc->{$key});
19672434 719}
1e3baf05
DM
720
721sub nic_models {
722 return $nic_model_list;
723}
724
725sub os_list_description {
726
727 return {
728 other => 'Other',
729 wxp => 'Windows XP',
730 w2k => 'Windows 2000',
731 w2k3 =>, 'Windows 2003',
732 w2k8 => 'Windows 2008',
733 wvista => 'Windows Vista',
734 win7 => 'Windows 7',
a70ebde3 735 win8 => 'Windows 8/2012',
1e3baf05
DM
736 l24 => 'Linux 2.4',
737 l26 => 'Linux 2.6',
19672434 738 };
1e3baf05
DM
739}
740
1e3baf05
DM
741my $cdrom_path;
742
743sub get_cdrom_path {
744
745 return $cdrom_path if $cdrom_path;
746
747 return $cdrom_path = "/dev/cdrom" if -l "/dev/cdrom";
748 return $cdrom_path = "/dev/cdrom1" if -l "/dev/cdrom1";
749 return $cdrom_path = "/dev/cdrom2" if -l "/dev/cdrom2";
750}
751
752sub get_iso_path {
753 my ($storecfg, $vmid, $cdrom) = @_;
754
755 if ($cdrom eq 'cdrom') {
756 return get_cdrom_path();
757 } elsif ($cdrom eq 'none') {
758 return '';
759 } elsif ($cdrom =~ m|^/|) {
760 return $cdrom;
761 } else {
6b64503e 762 return PVE::Storage::path($storecfg, $cdrom);
1e3baf05
DM
763 }
764}
765
766# try to convert old style file names to volume IDs
767sub filename_to_volume_id {
768 my ($vmid, $file, $media) = @_;
769
770 if (!($file eq 'none' || $file eq 'cdrom' ||
771 $file =~ m|^/dev/.+| || $file =~ m/^([^:]+):(.+)$/)) {
19672434 772
1e3baf05 773 return undef if $file =~ m|/|;
19672434 774
1e3baf05
DM
775 if ($media && $media eq 'cdrom') {
776 $file = "local:iso/$file";
777 } else {
778 $file = "local:$vmid/$file";
779 }
780 }
781
782 return $file;
783}
784
785sub verify_media_type {
786 my ($opt, $vtype, $media) = @_;
787
788 return if !$media;
789
790 my $etype;
791 if ($media eq 'disk') {
a125592c 792 $etype = 'images';
1e3baf05
DM
793 } elsif ($media eq 'cdrom') {
794 $etype = 'iso';
795 } else {
796 die "internal error";
797 }
798
799 return if ($vtype eq $etype);
19672434 800
1e3baf05
DM
801 raise_param_exc({ $opt => "unexpected media type ($vtype != $etype)" });
802}
803
804sub cleanup_drive_path {
805 my ($opt, $storecfg, $drive) = @_;
806
807 # try to convert filesystem paths to volume IDs
808
809 if (($drive->{file} !~ m/^(cdrom|none)$/) &&
810 ($drive->{file} !~ m|^/dev/.+|) &&
811 ($drive->{file} !~ m/^([^:]+):(.+)$/) &&
19672434 812 ($drive->{file} !~ m/^\d+$/)) {
1e3baf05
DM
813 my ($vtype, $volid) = PVE::Storage::path_to_volume_id($storecfg, $drive->{file});
814 raise_param_exc({ $opt => "unable to associate path '$drive->{file}' to any storage"}) if !$vtype;
815 $drive->{media} = 'cdrom' if !$drive->{media} && $vtype eq 'iso';
816 verify_media_type($opt, $vtype, $drive->{media});
817 $drive->{file} = $volid;
818 }
819
820 $drive->{media} = 'cdrom' if !$drive->{media} && $drive->{file} =~ m/^(cdrom|none)$/;
821}
822
823sub create_conf_nolock {
824 my ($vmid, $settings) = @_;
825
6b64503e 826 my $filename = config_file($vmid);
1e3baf05
DM
827
828 die "configuration file '$filename' already exists\n" if -f $filename;
19672434 829
1e3baf05
DM
830 my $defaults = load_defaults();
831
832 $settings->{name} = "vm$vmid" if !$settings->{name};
833 $settings->{memory} = $defaults->{memory} if !$settings->{memory};
834
835 my $data = '';
836 foreach my $opt (keys %$settings) {
837 next if !$confdesc->{$opt};
838
839 my $value = $settings->{$opt};
840 next if !$value;
841
842 $data .= "$opt: $value\n";
843 }
844
845 PVE::Tools::file_set_contents($filename, $data);
846}
847
f36ed4f4
DM
848my $parse_size = sub {
849 my ($value) = @_;
850
9bf371a6 851 return undef if $value !~ m/^(\d+(\.\d+)?)([KMG])?$/;
f36ed4f4
DM
852 my ($size, $unit) = ($1, $3);
853 if ($unit) {
854 if ($unit eq 'K') {
855 $size = $size * 1024;
856 } elsif ($unit eq 'M') {
857 $size = $size * 1024 * 1024;
858 } elsif ($unit eq 'G') {
859 $size = $size * 1024 * 1024 * 1024;
860 }
861 }
862 return int($size);
863};
864
865my $format_size = sub {
866 my ($size) = @_;
867
868 $size = int($size);
869
870 my $kb = int($size/1024);
871 return $size if $kb*1024 != $size;
872
873 my $mb = int($kb/1024);
874 return "${kb}K" if $mb*1024 != $kb;
875
876 my $gb = int($mb/1024);
877 return "${mb}M" if $gb*1024 != $mb;
878
879 return "${gb}G";
880};
881
1e3baf05
DM
882# ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]]
883# [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no]
036e0e2b 884# [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
8d87f8aa 885# [,aio=native|threads][,discard=ignore|on]
1e3baf05
DM
886
887sub parse_drive {
888 my ($key, $data) = @_;
889
890 my $res = {};
19672434 891
1e3baf05
DM
892 # $key may be undefined - used to verify JSON parameters
893 if (!defined($key)) {
894 $res->{interface} = 'unknown'; # should not harm when used to verify parameters
895 $res->{index} = 0;
896 } elsif ($key =~ m/^([^\d]+)(\d+)$/) {
897 $res->{interface} = $1;
898 $res->{index} = $2;
899 } else {
900 return undef;
901 }
902
903 foreach my $p (split (/,/, $data)) {
904 next if $p =~ m/^\s*$/;
905
74edd76b 906 if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio|bps|mbps|mbps_max|bps_rd|mbps_rd|mbps_rd_max|bps_wr|mbps_wr|mbps_wr_max|iops|iops_max|iops_rd|iops_rd_max|iops_wr|iops_wr_max|size|discard)=(.+)$/) {
1e3baf05
DM
907 my ($k, $v) = ($1, $2);
908
909 $k = 'file' if $k eq 'volume';
910
911 return undef if defined $res->{$k};
19672434 912
9bf371a6
DM
913 if ($k eq 'bps' || $k eq 'bps_rd' || $k eq 'bps_wr') {
914 return undef if !$v || $v !~ m/^\d+/;
915 $k = "m$k";
916 $v = sprintf("%.3f", $v / (1024*1024));
917 }
1e3baf05
DM
918 $res->{$k} = $v;
919 } else {
920 if (!$res->{file} && $p !~ m/=/) {
921 $res->{file} = $p;
922 } else {
923 return undef;
924 }
925 }
926 }
927
928 return undef if !$res->{file};
929
bdf3f362
AD
930 if($res->{file} =~ m/\.(raw|cow|qcow|qcow2|vmdk|cloop)$/){
931 $res->{format} = $1;
932 }
933
19672434 934 return undef if $res->{cache} &&
e482cec3 935 $res->{cache} !~ m/^(off|none|writethrough|writeback|unsafe|directsync)$/;
1e3baf05
DM
936 return undef if $res->{snapshot} && $res->{snapshot} !~ m/^(on|off)$/;
937 return undef if $res->{cyls} && $res->{cyls} !~ m/^\d+$/;
938 return undef if $res->{heads} && $res->{heads} !~ m/^\d+$/;
939 return undef if $res->{secs} && $res->{secs} !~ m/^\d+$/;
940 return undef if $res->{media} && $res->{media} !~ m/^(disk|cdrom)$/;
941 return undef if $res->{trans} && $res->{trans} !~ m/^(none|lba|auto)$/;
942 return undef if $res->{format} && $res->{format} !~ m/^(raw|cow|qcow|qcow2|vmdk|cloop)$/;
943 return undef if $res->{rerror} && $res->{rerror} !~ m/^(ignore|report|stop)$/;
944 return undef if $res->{werror} && $res->{werror} !~ m/^(enospc|ignore|report|stop)$/;
945 return undef if $res->{backup} && $res->{backup} !~ m/^(yes|no)$/;
946 return undef if $res->{aio} && $res->{aio} !~ m/^(native|threads)$/;
8d87f8aa 947 return undef if $res->{discard} && $res->{discard} !~ m/^(ignore|on)$/;
be190583 948
9bf371a6
DM
949 return undef if $res->{mbps_rd} && $res->{mbps};
950 return undef if $res->{mbps_wr} && $res->{mbps};
951
952 return undef if $res->{mbps} && $res->{mbps} !~ m/^\d+(\.\d+)?$/;
74edd76b 953 return undef if $res->{mbps_max} && $res->{mbps_max} !~ m/^\d+(\.\d+)?$/;
9bf371a6 954 return undef if $res->{mbps_rd} && $res->{mbps_rd} !~ m/^\d+(\.\d+)?$/;
74edd76b 955 return undef if $res->{mbps_rd_max} && $res->{mbps_rd_max} !~ m/^\d+(\.\d+)?$/;
9bf371a6 956 return undef if $res->{mbps_wr} && $res->{mbps_wr} !~ m/^\d+(\.\d+)?$/;
74edd76b 957 return undef if $res->{mbps_wr_max} && $res->{mbps_wr_max} !~ m/^\d+(\.\d+)?$/;
9bf371a6 958
affd2f88
AD
959 return undef if $res->{iops_rd} && $res->{iops};
960 return undef if $res->{iops_wr} && $res->{iops};
74edd76b
AD
961
962
affd2f88 963 return undef if $res->{iops} && $res->{iops} !~ m/^\d+$/;
74edd76b 964 return undef if $res->{iops_max} && $res->{iops_max} !~ m/^\d+$/;
affd2f88 965 return undef if $res->{iops_rd} && $res->{iops_rd} !~ m/^\d+$/;
74edd76b 966 return undef if $res->{iops_rd_max} && $res->{iops_rd_max} !~ m/^\d+$/;
affd2f88 967 return undef if $res->{iops_wr} && $res->{iops_wr} !~ m/^\d+$/;
74edd76b 968 return undef if $res->{iops_wr_max} && $res->{iops_wr_max} !~ m/^\d+$/;
affd2f88
AD
969
970
24afaca0 971 if ($res->{size}) {
be190583 972 return undef if !defined($res->{size} = &$parse_size($res->{size}));
24afaca0
DM
973 }
974
1e3baf05
DM
975 if ($res->{media} && ($res->{media} eq 'cdrom')) {
976 return undef if $res->{snapshot} || $res->{trans} || $res->{format};
19672434 977 return undef if $res->{heads} || $res->{secs} || $res->{cyls};
1e3baf05
DM
978 return undef if $res->{interface} eq 'virtio';
979 }
980
981 # rerror does not work with scsi drives
982 if ($res->{rerror}) {
983 return undef if $res->{interface} eq 'scsi';
984 }
985
986 return $res;
987}
988
74edd76b 989my @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
990
991sub print_drive {
992 my ($vmid, $drive) = @_;
993
994 my $opts = '';
74edd76b 995 foreach my $o (@qemu_drive_options, 'mbps', 'mbps_rd', 'mbps_wr', 'mbps_max', 'mbps_rd_max', 'mbps_wr_max', 'backup') {
1e3baf05
DM
996 $opts .= ",$o=$drive->{$o}" if $drive->{$o};
997 }
998
24afaca0
DM
999 if ($drive->{size}) {
1000 $opts .= ",size=" . &$format_size($drive->{size});
1001 }
1002
1e3baf05
DM
1003 return "$drive->{file}$opts";
1004}
1005
28ef82d3
DM
1006sub scsi_inquiry {
1007 my($fh, $noerr) = @_;
1008
1009 my $SG_IO = 0x2285;
1010 my $SG_GET_VERSION_NUM = 0x2282;
1011
1012 my $versionbuf = "\x00" x 8;
1013 my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf);
1014 if (!$ret) {
1015 die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr;
1016 return undef;
1017 }
97d62eb7 1018 my $version = unpack("I", $versionbuf);
28ef82d3
DM
1019 if ($version < 30000) {
1020 die "scsi generic interface too old\n" if !$noerr;
1021 return undef;
1022 }
97d62eb7 1023
28ef82d3
DM
1024 my $buf = "\x00" x 36;
1025 my $sensebuf = "\x00" x 8;
f334aa3e 1026 my $cmd = pack("C x3 C x1", 0x12, 36);
97d62eb7 1027
28ef82d3
DM
1028 # see /usr/include/scsi/sg.h
1029 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";
1030
97d62eb7
DM
1031 my $packet = pack($sg_io_hdr_t, ord('S'), -3, length($cmd),
1032 length($sensebuf), 0, length($buf), $buf,
28ef82d3
DM
1033 $cmd, $sensebuf, 6000);
1034
1035 $ret = ioctl($fh, $SG_IO, $packet);
1036 if (!$ret) {
1037 die "scsi ioctl SG_IO failed - $!\n" if !$noerr;
1038 return undef;
1039 }
97d62eb7 1040
28ef82d3
DM
1041 my @res = unpack($sg_io_hdr_t, $packet);
1042 if ($res[17] || $res[18]) {
1043 die "scsi ioctl SG_IO status error - $!\n" if !$noerr;
1044 return undef;
1045 }
1046
1047 my $res = {};
09984754 1048 (my $byte0, my $byte1, $res->{vendor},
28ef82d3
DM
1049 $res->{product}, $res->{revision}) = unpack("C C x6 A8 A16 A4", $buf);
1050
09984754
DM
1051 $res->{removable} = $byte1 & 128 ? 1 : 0;
1052 $res->{type} = $byte0 & 31;
1053
28ef82d3
DM
1054 return $res;
1055}
1056
1057sub path_is_scsi {
1058 my ($path) = @_;
1059
1060 my $fh = IO::File->new("+<$path") || return undef;
1061 my $res = scsi_inquiry($fh, 1);
1062 close($fh);
1063
1064 return $res;
1065}
1066
db656e5f
DM
1067sub machine_type_is_q35 {
1068 my ($conf) = @_;
1069
1070 return $conf->{machine} && ($conf->{machine} =~ m/q35/) ? 1 : 0;
1071}
1072
1073sub print_tabletdevice_full {
1074 my ($conf) = @_;
1075
1076 my $q35 = machine_type_is_q35($conf);
1077
1078 # we use uhci for old VMs because tablet driver was buggy in older qemu
1079 my $usbbus = $q35 ? "ehci" : "uhci";
1080
1081 return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
1082}
1083
ca916ecc 1084sub print_drivedevice_full {
5bdcf937 1085 my ($storecfg, $conf, $vmid, $drive, $bridges) = @_;
ca916ecc
DA
1086
1087 my $device = '';
1088 my $maxdev = 0;
19672434 1089
ca916ecc 1090 if ($drive->{interface} eq 'virtio') {
5bdcf937 1091 my $pciaddr = print_pci_addr("$drive->{interface}$drive->{index}", $bridges);
2ed36a41
DM
1092 $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
1093 } elsif ($drive->{interface} eq 'scsi') {
5b952ff5 1094 $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
2ed36a41
DM
1095 my $controller = int($drive->{index} / $maxdev);
1096 my $unit = $drive->{index} % $maxdev;
1097 my $devicetype = 'hd';
231f2e13
DA
1098 my $path = '';
1099 if (drive_is_cdrom($drive)) {
1100 $devicetype = 'cd';
29b19529 1101 } else {
231f2e13
DA
1102 if ($drive->{file} =~ m|^/|) {
1103 $path = $drive->{file};
1104 } else {
1105 $path = PVE::Storage::path($storecfg, $drive->{file});
1106 }
d454d040
AD
1107
1108 if($path =~ m/^iscsi\:\/\//){
29b19529
DM
1109 $devicetype = 'generic';
1110 } else {
09984754
DM
1111 if (my $info = path_is_scsi($path)) {
1112 if ($info->{type} == 0) {
1113 $devicetype = 'block';
1114 } elsif ($info->{type} == 1) { # tape
1115 $devicetype = 'generic';
1116 }
1117 }
d454d040 1118 }
231f2e13 1119 }
ca916ecc 1120
5b952ff5
DM
1121 if (!$conf->{scsihw} || ($conf->{scsihw} =~ m/^lsi/)){
1122 $device = "scsi-$devicetype,bus=scsihw$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
cdd20088
AD
1123 } else {
1124 $device = "scsi-$devicetype,bus=scsihw$controller.0,channel=0,scsi-id=0,lun=$drive->{index},drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
1125 }
1126
2ed36a41
DM
1127 } elsif ($drive->{interface} eq 'ide'){
1128 $maxdev = 2;
1129 my $controller = int($drive->{index} / $maxdev);
1130 my $unit = $drive->{index} % $maxdev;
1131 my $devicetype = ($drive->{media} && $drive->{media} eq 'cdrom') ? "cd" : "hd";
1132
7ebe888a 1133 $device = "ide-$devicetype,bus=ide.$controller,unit=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
cdb0931f
DA
1134 } elsif ($drive->{interface} eq 'sata'){
1135 my $controller = int($drive->{index} / $MAX_SATA_DISKS);
1136 my $unit = $drive->{index} % $MAX_SATA_DISKS;
1137 $device = "ide-drive,bus=ahci$controller.$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
2ed36a41
DM
1138 } elsif ($drive->{interface} eq 'usb') {
1139 die "implement me";
1140 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
1141 } else {
1142 die "unsupported interface type";
ca916ecc
DA
1143 }
1144
3b408e82
DM
1145 $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex};
1146
ca916ecc
DA
1147 return $device;
1148}
1149
15b21acc 1150sub get_initiator_name {
46f58b5f 1151 my $initiator;
15b21acc 1152
46f58b5f
DM
1153 my $fh = IO::File->new('/etc/iscsi/initiatorname.iscsi') || return undef;
1154 while (defined(my $line = <$fh>)) {
1155 next if $line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/;
15b21acc
MR
1156 $initiator = $1;
1157 last;
1158 }
46f58b5f
DM
1159 $fh->close();
1160
15b21acc
MR
1161 return $initiator;
1162}
1163
1e3baf05
DM
1164sub print_drive_full {
1165 my ($storecfg, $vmid, $drive) = @_;
1166
1167 my $opts = '';
1168 foreach my $o (@qemu_drive_options) {
3b408e82 1169 next if $o eq 'bootindex';
1e3baf05 1170 $opts .= ",$o=$drive->{$o}" if $drive->{$o};
19672434 1171 }
1e3baf05 1172
9bf371a6
DM
1173 foreach my $o (qw(bps bps_rd bps_wr)) {
1174 my $v = $drive->{"m$o"};
1175 $opts .= ",$o=" . int($v*1024*1024) if $v;
1176 }
1177
1e3baf05 1178 # use linux-aio by default (qemu default is threads)
19672434 1179 $opts .= ",aio=native" if !$drive->{aio};
1e3baf05
DM
1180
1181 my $path;
1182 my $volid = $drive->{file};
6b64503e
DM
1183 if (drive_is_cdrom($drive)) {
1184 $path = get_iso_path($storecfg, $vmid, $volid);
1e3baf05
DM
1185 } else {
1186 if ($volid =~ m|^/|) {
1187 $path = $volid;
1188 } else {
6b64503e 1189 $path = PVE::Storage::path($storecfg, $volid);
1e3baf05
DM
1190 }
1191 }
1192
ef86170e 1193 $opts .= ",cache=none" if !$drive->{cache} && !drive_is_cdrom($drive);
11490cf2 1194
1e3baf05
DM
1195 my $pathinfo = $path ? "file=$path," : '';
1196
3ebfcc86 1197 return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
1e3baf05
DM
1198}
1199
cc4d6182 1200sub print_netdevice_full {
5bdcf937 1201 my ($vmid, $conf, $net, $netid, $bridges) = @_;
cc4d6182
DA
1202
1203 my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
1204
1205 my $device = $net->{model};
1206 if ($net->{model} eq 'virtio') {
1207 $device = 'virtio-net-pci';
1208 };
1209
1210 # qemu > 0.15 always try to boot from network - we disable that by
1211 # not loading the pxe rom file
1212 my $extra = ($bootorder !~ m/n/) ? "romfile=," : '';
5bdcf937 1213 my $pciaddr = print_pci_addr("$netid", $bridges);
cc4d6182 1214 my $tmpstr = "$device,${extra}mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
a9410357
AD
1215 if ($net->{queues} && $net->{queues} > 1 && $net->{model} eq 'virtio'){
1216 #Consider we have N queues, the number of vectors needed is 2*N + 2 (plus one config interrupt and control vq)
1217 my $vectors = $net->{queues} * 2 + 2;
1218 $tmpstr .= ",vectors=$vectors,mq=on";
1219 }
cc4d6182
DA
1220 $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex} ;
1221 return $tmpstr;
1222}
1223
1224sub print_netdev_full {
1225 my ($vmid, $conf, $net, $netid) = @_;
1226
1227 my $i = '';
1228 if ($netid =~ m/^net(\d+)$/) {
1229 $i = int($1);
1230 }
1231
1232 die "got strange net id '$i'\n" if $i >= ${MAX_NETS};
1233
1234 my $ifname = "tap${vmid}i$i";
1235
1236 # kvm uses TUNSETIFF ioctl, and that limits ifname length
1237 die "interface name '$ifname' is too long (max 15 character)\n"
1238 if length($ifname) >= 16;
1239
1240 my $vhostparam = '';
1241 $vhostparam = ',vhost=on' if $kernel_has_vhost_net && $net->{model} eq 'virtio';
1242
1243 my $vmname = $conf->{name} || "vm$vmid";
1244
a9410357
AD
1245 my $netdev = "";
1246
cc4d6182 1247 if ($net->{bridge}) {
a9410357 1248 $netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/pve-bridge,downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
cc4d6182 1249 } else {
a9410357 1250 $netdev = "type=user,id=$netid,hostname=$vmname";
cc4d6182 1251 }
a9410357
AD
1252
1253 $netdev .= ",queues=$net->{queues}" if ($net->{queues} && $net->{model} eq 'virtio');
1254
1255 return $netdev;
cc4d6182 1256}
1e3baf05
DM
1257
1258sub drive_is_cdrom {
1259 my ($drive) = @_;
1260
1261 return $drive && $drive->{media} && ($drive->{media} eq 'cdrom');
1262
1263}
1264
040b06b7
DA
1265sub parse_hostpci {
1266 my ($value) = @_;
1267
1268 return undef if !$value;
1269
0cea6a01
DM
1270
1271 my @list = split(/,/, $value);
1272 my $found;
1273
040b06b7 1274 my $res = {};
0cea6a01 1275 foreach my $kv (@list) {
040b06b7 1276
4543ecf0 1277 if ($kv =~ m/^(host=)?([a-f0-9]{2}:[a-f0-9]{2})(\.([a-f0-9]))?$/) {
0cea6a01 1278 $found = 1;
4543ecf0
AD
1279 if(defined($4)){
1280 push @{$res->{pciid}}, { id => $2 , function => $4};
1281
1282 }else{
1283 my $pcidevices = lspci($2);
1284 $res->{pciid} = $pcidevices->{$2};
1285 }
0cea6a01
DM
1286 } elsif ($kv =~ m/^driver=(kvm|vfio)$/) {
1287 $res->{driver} = $1;
1288 } elsif ($kv =~ m/^rombar=(on|off)$/) {
1289 $res->{rombar} = $1;
2e3b7e2a
AD
1290 } elsif ($kv =~ m/^x-vga=(on|off)$/) {
1291 $res->{'x-vga'} = $1;
1292 } elsif ($kv =~ m/^pcie=(\d+)$/) {
1293 $res->{pcie} = 1 if $1 == 1;
0cea6a01
DM
1294 } else {
1295 warn "unknown hostpci setting '$kv'\n";
1296 }
040b06b7
DA
1297 }
1298
0cea6a01
DM
1299 return undef if !$found;
1300
040b06b7
DA
1301 return $res;
1302}
1303
1e3baf05
DM
1304# netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
1305sub parse_net {
1306 my ($data) = @_;
1307
1308 my $res = {};
1309
6b64503e 1310 foreach my $kvp (split(/,/, $data)) {
1e3baf05 1311
e4c6e0b8 1312 if ($kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er|vmxnet3)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i) {
6b64503e 1313 my $model = lc($1);
92f0fedc 1314 my $mac = defined($3) ? uc($3) : PVE::Tools::random_ether_addr();
1e3baf05
DM
1315 $res->{model} = $model;
1316 $res->{macaddr} = $mac;
1317 } elsif ($kvp =~ m/^bridge=(\S+)$/) {
1318 $res->{bridge} = $1;
a9410357
AD
1319 } elsif ($kvp =~ m/^queues=(\d+)$/) {
1320 $res->{queues} = $1;
1e3baf05
DM
1321 } elsif ($kvp =~ m/^rate=(\d+(\.\d+)?)$/) {
1322 $res->{rate} = $1;
5070f384
DA
1323 } elsif ($kvp =~ m/^tag=(\d+)$/) {
1324 $res->{tag} = $1;
2dd4aa4c
AD
1325 } elsif ($kvp =~ m/^firewall=(\d+)$/) {
1326 $res->{firewall} = $1;
1e3baf05
DM
1327 } else {
1328 return undef;
1329 }
19672434 1330
1e3baf05
DM
1331 }
1332
1333 return undef if !$res->{model};
1334
1335 return $res;
1336}
1337
1338sub print_net {
1339 my $net = shift;
1340
1341 my $res = "$net->{model}";
1342 $res .= "=$net->{macaddr}" if $net->{macaddr};
1343 $res .= ",bridge=$net->{bridge}" if $net->{bridge};
1344 $res .= ",rate=$net->{rate}" if $net->{rate};
18744ba3 1345 $res .= ",tag=$net->{tag}" if $net->{tag};
28138e9a 1346 $res .= ",firewall=$net->{firewall}" if $net->{firewall};
1e3baf05
DM
1347
1348 return $res;
1349}
1350
1351sub add_random_macs {
1352 my ($settings) = @_;
1353
1354 foreach my $opt (keys %$settings) {
1355 next if $opt !~ m/^net(\d+)$/;
1356 my $net = parse_net($settings->{$opt});
1357 next if !$net;
1358 $settings->{$opt} = print_net($net);
1359 }
1360}
1361
1362sub add_unused_volume {
1858638f 1363 my ($config, $volid) = @_;
1e3baf05
DM
1364
1365 my $key;
1366 for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
1367 my $test = "unused$ind";
1368 if (my $vid = $config->{$test}) {
1369 return if $vid eq $volid; # do not add duplicates
1370 } else {
1371 $key = $test;
19672434 1372 }
1e3baf05
DM
1373 }
1374
1375 die "To many unused volume - please delete them first.\n" if !$key;
97d62eb7 1376
1858638f 1377 $config->{$key} = $volid;
1e3baf05 1378
1858638f 1379 return $key;
1e3baf05
DM
1380}
1381
2796e7d5
DM
1382my $valid_smbios1_options = {
1383 manufacturer => '\S+',
1384 product => '\S+',
1385 version => '\S+',
1386 serial => '\S+',
1387 uuid => '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
1388 sku => '\S+',
1389 family => '\S+',
1390};
1391
1392# smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str]
1393sub parse_smbios1 {
1394 my ($data) = @_;
1395
1396 my $res = {};
1397
1398 foreach my $kvp (split(/,/, $data)) {
1399 return undef if $kvp !~ m/^(\S+)=(.+)$/;
1400 my ($k, $v) = split(/=/, $kvp);
1401 return undef if !defined($k) || !defined($v);
1402 return undef if !$valid_smbios1_options->{$k};
1403 return undef if $v !~ m/^$valid_smbios1_options->{$k}$/;
1404 $res->{$k} = $v;
1405 }
1406
1407 return $res;
1408}
1409
1410PVE::JSONSchema::register_format('pve-qm-smbios1', \&verify_smbios1);
1411sub verify_smbios1 {
1412 my ($value, $noerr) = @_;
1413
1414 return $value if parse_smbios1($value);
1415
1416 return undef if $noerr;
1417
1418 die "unable to parse smbios (type 1) options\n";
1419}
1420
1e3baf05
DM
1421PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk);
1422sub verify_bootdisk {
1423 my ($value, $noerr) = @_;
1424
19672434 1425 return $value if valid_drivename($value);
1e3baf05
DM
1426
1427 return undef if $noerr;
1428
1429 die "invalid boot disk '$value'\n";
1430}
1431
1432PVE::JSONSchema::register_format('pve-qm-net', \&verify_net);
1433sub verify_net {
1434 my ($value, $noerr) = @_;
1435
1436 return $value if parse_net($value);
1437
1438 return undef if $noerr;
19672434 1439
1e3baf05
DM
1440 die "unable to parse network options\n";
1441}
1442
1443PVE::JSONSchema::register_format('pve-qm-drive', \&verify_drive);
1444sub verify_drive {
1445 my ($value, $noerr) = @_;
1446
6b64503e 1447 return $value if parse_drive(undef, $value);
1e3baf05
DM
1448
1449 return undef if $noerr;
19672434 1450
1e3baf05
DM
1451 die "unable to parse drive options\n";
1452}
1453
1454PVE::JSONSchema::register_format('pve-qm-hostpci', \&verify_hostpci);
1455sub verify_hostpci {
1456 my ($value, $noerr) = @_;
1457
040b06b7
DA
1458 return $value if parse_hostpci($value);
1459
1460 return undef if $noerr;
1461
1462 die "unable to parse pci id\n";
1e3baf05
DM
1463}
1464
0ea9541d
DM
1465PVE::JSONSchema::register_format('pve-qm-watchdog', \&verify_watchdog);
1466sub verify_watchdog {
1467 my ($value, $noerr) = @_;
1468
1469 return $value if parse_watchdog($value);
1470
1471 return undef if $noerr;
19672434 1472
0ea9541d
DM
1473 die "unable to parse watchdog options\n";
1474}
1475
1476sub parse_watchdog {
1477 my ($value) = @_;
1478
1479 return undef if !$value;
1480
1481 my $res = {};
1482
6b64503e 1483 foreach my $p (split(/,/, $value)) {
0ea9541d
DM
1484 next if $p =~ m/^\s*$/;
1485
1486 if ($p =~ m/^(model=)?(i6300esb|ib700)$/) {
1487 $res->{model} = $2;
1488 } elsif ($p =~ m/^(action=)?(reset|shutdown|poweroff|pause|debug|none)$/) {
1489 $res->{action} = $2;
1490 } else {
1491 return undef;
1492 }
1493 }
1494
1495 return $res;
1496}
1497
59411c4e
DM
1498PVE::JSONSchema::register_format('pve-qm-startup', \&verify_startup);
1499sub verify_startup {
1500 my ($value, $noerr) = @_;
1501
1502 return $value if parse_startup($value);
1503
1504 return undef if $noerr;
1505
1506 die "unable to parse startup options\n";
1507}
1508
1509sub parse_startup {
1510 my ($value) = @_;
1511
1512 return undef if !$value;
1513
1514 my $res = {};
1515
1516 foreach my $p (split(/,/, $value)) {
1517 next if $p =~ m/^\s*$/;
1518
1519 if ($p =~ m/^(order=)?(\d+)$/) {
1520 $res->{order} = $2;
1521 } elsif ($p =~ m/^up=(\d+)$/) {
1522 $res->{up} = $1;
1523 } elsif ($p =~ m/^down=(\d+)$/) {
1524 $res->{down} = $1;
1525 } else {
1526 return undef;
1527 }
1528 }
1529
1530 return $res;
1531}
1532
1e3baf05
DM
1533sub parse_usb_device {
1534 my ($value) = @_;
1535
1536 return undef if !$value;
1537
6b64503e 1538 my @dl = split(/,/, $value);
1e3baf05
DM
1539 my $found;
1540
1541 my $res = {};
1542 foreach my $v (@dl) {
036e0e2b 1543 if ($v =~ m/^host=(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/) {
1e3baf05 1544 $found = 1;
036e0e2b
DM
1545 $res->{vendorid} = $2;
1546 $res->{productid} = $4;
1e3baf05
DM
1547 } elsif ($v =~ m/^host=(\d+)\-(\d+(\.\d+)*)$/) {
1548 $found = 1;
1549 $res->{hostbus} = $1;
1550 $res->{hostport} = $2;
80401dd8
DM
1551 } elsif ($v =~ m/^spice$/) {
1552 $found = 1;
1553 $res->{spice} = 1;
1e3baf05
DM
1554 } else {
1555 return undef;
1556 }
1557 }
1558 return undef if !$found;
1559
1560 return $res;
1561}
19672434 1562
1e3baf05
DM
1563PVE::JSONSchema::register_format('pve-qm-usb-device', \&verify_usb_device);
1564sub verify_usb_device {
1565 my ($value, $noerr) = @_;
1566
1567 return $value if parse_usb_device($value);
1568
1569 return undef if $noerr;
19672434 1570
1e3baf05
DM
1571 die "unable to parse usb device\n";
1572}
1573
1e3baf05
DM
1574# add JSON properties for create and set function
1575sub json_config_properties {
1576 my $prop = shift;
1577
1578 foreach my $opt (keys %$confdesc) {
18bfb361 1579 next if $opt eq 'parent' || $opt eq 'snaptime' || $opt eq 'vmstate';
1e3baf05
DM
1580 $prop->{$opt} = $confdesc->{$opt};
1581 }
1582
1583 return $prop;
1584}
1585
1586sub check_type {
1587 my ($key, $value) = @_;
1588
1589 die "unknown setting '$key'\n" if !$confdesc->{$key};
1590
1591 my $type = $confdesc->{$key}->{type};
1592
6b64503e 1593 if (!defined($value)) {
1e3baf05
DM
1594 die "got undefined value\n";
1595 }
1596
1597 if ($value =~ m/[\n\r]/) {
1598 die "property contains a line feed\n";
1599 }
1600
1601 if ($type eq 'boolean') {
19672434
DM
1602 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1603 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1604 die "type check ('boolean') failed - got '$value'\n";
1e3baf05
DM
1605 } elsif ($type eq 'integer') {
1606 return int($1) if $value =~ m/^(\d+)$/;
1607 die "type check ('integer') failed - got '$value'\n";
04432191
AD
1608 } elsif ($type eq 'number') {
1609 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
1610 die "type check ('number') failed - got '$value'\n";
1e3baf05
DM
1611 } elsif ($type eq 'string') {
1612 if (my $fmt = $confdesc->{$key}->{format}) {
1613 if ($fmt eq 'pve-qm-drive') {
1614 # special case - we need to pass $key to parse_drive()
6b64503e 1615 my $drive = parse_drive($key, $value);
1e3baf05
DM
1616 return $value if $drive;
1617 die "unable to parse drive options\n";
1618 }
1619 PVE::JSONSchema::check_format($fmt, $value);
19672434
DM
1620 return $value;
1621 }
1e3baf05 1622 $value =~ s/^\"(.*)\"$/$1/;
19672434 1623 return $value;
1e3baf05
DM
1624 } else {
1625 die "internal error"
1626 }
1627}
1628
191435c6
DM
1629sub lock_config_full {
1630 my ($vmid, $timeout, $code, @param) = @_;
1e3baf05 1631
6b64503e 1632 my $filename = config_file_lock($vmid);
1e3baf05 1633
191435c6 1634 my $res = lock_file($filename, $timeout, $code, @param);
1e3baf05
DM
1635
1636 die $@ if $@;
5fdbe4f0
DM
1637
1638 return $res;
1e3baf05
DM
1639}
1640
4e4f83fe
DM
1641sub lock_config_mode {
1642 my ($vmid, $timeout, $shared, $code, @param) = @_;
6116f729
DM
1643
1644 my $filename = config_file_lock($vmid);
1645
4e4f83fe 1646 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
6116f729
DM
1647
1648 die $@ if $@;
1649
1650 return $res;
1651}
1652
191435c6
DM
1653sub lock_config {
1654 my ($vmid, $code, @param) = @_;
1655
1656 return lock_config_full($vmid, 10, $code, @param);
1657}
1658
1e3baf05 1659sub cfs_config_path {
a78ccf26 1660 my ($vmid, $node) = @_;
1e3baf05 1661
a78ccf26
DM
1662 $node = $nodename if !$node;
1663 return "nodes/$node/qemu-server/$vmid.conf";
1e3baf05
DM
1664}
1665
040b06b7
DA
1666sub check_iommu_support{
1667 #fixme : need to check IOMMU support
1668 #http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
1669
1670 my $iommu=1;
1671 return $iommu;
1672
1673}
1674
1e3baf05 1675sub config_file {
a78ccf26 1676 my ($vmid, $node) = @_;
1e3baf05 1677
a78ccf26 1678 my $cfspath = cfs_config_path($vmid, $node);
1e3baf05
DM
1679 return "/etc/pve/$cfspath";
1680}
1681
1682sub config_file_lock {
1683 my ($vmid) = @_;
1684
1685 return "$lock_dir/lock-$vmid.conf";
1686}
1687
1688sub touch_config {
1689 my ($vmid) = @_;
1690
6b64503e 1691 my $conf = config_file($vmid);
1e3baf05
DM
1692 utime undef, undef, $conf;
1693}
1694
1e3baf05 1695sub destroy_vm {
a6af7b3e 1696 my ($storecfg, $vmid, $keep_empty_config) = @_;
1e3baf05 1697
6b64503e 1698 my $conffile = config_file($vmid);
1e3baf05 1699
6b64503e 1700 my $conf = load_config($vmid);
1e3baf05 1701
6b64503e 1702 check_lock($conf);
1e3baf05 1703
19672434 1704 # only remove disks owned by this VM
1e3baf05
DM
1705 foreach_drive($conf, sub {
1706 my ($ds, $drive) = @_;
1707
6b64503e 1708 return if drive_is_cdrom($drive);
1e3baf05
DM
1709
1710 my $volid = $drive->{file};
ed221350 1711
ff1a2432 1712 return if !$volid || $volid =~ m|^/|;
1e3baf05 1713
6b64503e 1714 my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
ff1a2432 1715 return if !$path || !$owner || ($owner != $vmid);
1e3baf05 1716
6b64503e 1717 PVE::Storage::vdisk_free($storecfg, $volid);
1e3baf05 1718 });
19672434 1719
a6af7b3e 1720 if ($keep_empty_config) {
9c502e26 1721 PVE::Tools::file_set_contents($conffile, "memory: 128\n");
a6af7b3e
DM
1722 } else {
1723 unlink $conffile;
1724 }
1e3baf05
DM
1725
1726 # also remove unused disk
1727 eval {
6b64503e 1728 my $dl = PVE::Storage::vdisk_list($storecfg, undef, $vmid);
1e3baf05
DM
1729
1730 eval {
6b64503e 1731 PVE::Storage::foreach_volid($dl, sub {
1e3baf05 1732 my ($volid, $sid, $volname, $d) = @_;
6b64503e 1733 PVE::Storage::vdisk_free($storecfg, $volid);
1e3baf05
DM
1734 });
1735 };
1736 warn $@ if $@;
1737
1738 };
1739 warn $@ if $@;
1740}
1741
1e3baf05 1742sub load_config {
7e8dcf2c 1743 my ($vmid, $node) = @_;
1e3baf05 1744
7e8dcf2c 1745 my $cfspath = cfs_config_path($vmid, $node);
1e3baf05
DM
1746
1747 my $conf = PVE::Cluster::cfs_read_file($cfspath);
1748
1749 die "no such VM ('$vmid')\n" if !defined($conf);
1750
1751 return $conf;
19672434 1752}
1e3baf05
DM
1753
1754sub parse_vm_config {
1755 my ($filename, $raw) = @_;
1756
1757 return undef if !defined($raw);
1758
554ac7e7 1759 my $res = {
fc1ddcdc 1760 digest => Digest::SHA::sha1_hex($raw),
0d18dcfc 1761 snapshots => {},
554ac7e7 1762 };
1e3baf05 1763
19672434 1764 $filename =~ m|/qemu-server/(\d+)\.conf$|
1e3baf05
DM
1765 || die "got strange filename '$filename'";
1766
1767 my $vmid = $1;
1768
0d18dcfc 1769 my $conf = $res;
0581fe4f
DM
1770 my $descr = '';
1771
0d18dcfc
DM
1772 my @lines = split(/\n/, $raw);
1773 foreach my $line (@lines) {
1e3baf05 1774 next if $line =~ m/^\s*$/;
be190583 1775
0d18dcfc
DM
1776 if ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
1777 my $snapname = $1;
1778 $conf->{description} = $descr if $descr;
782f4f75 1779 $descr = '';
be190583 1780 $conf = $res->{snapshots}->{$snapname} = {};
0d18dcfc
DM
1781 next;
1782 }
1e3baf05 1783
0581fe4f
DM
1784 if ($line =~ m/^\#(.*)\s*$/) {
1785 $descr .= PVE::Tools::decode_text($1) . "\n";
1786 next;
1787 }
1788
1e3baf05 1789 if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
0581fe4f 1790 $descr .= PVE::Tools::decode_text($2);
0d18dcfc
DM
1791 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
1792 $conf->{snapstate} = $1;
1e3baf05
DM
1793 } elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
1794 my $key = $1;
1795 my $value = $2;
0d18dcfc 1796 $conf->{$key} = $value;
1e3baf05
DM
1797 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(\S+)\s*$/) {
1798 my $key = $1;
1799 my $value = $2;
1800 eval { $value = check_type($key, $value); };
1801 if ($@) {
1802 warn "vm $vmid - unable to parse value of '$key' - $@";
1803 } else {
1804 my $fmt = $confdesc->{$key}->{format};
1805 if ($fmt && $fmt eq 'pve-qm-drive') {
1806 my $v = parse_drive($key, $value);
1807 if (my $volid = filename_to_volume_id($vmid, $v->{file}, $v->{media})) {
1808 $v->{file} = $volid;
6b64503e 1809 $value = print_drive($vmid, $v);
1e3baf05
DM
1810 } else {
1811 warn "vm $vmid - unable to parse value of '$key'\n";
1812 next;
1813 }
1814 }
1815
1816 if ($key eq 'cdrom') {
0d18dcfc 1817 $conf->{ide2} = $value;
1e3baf05 1818 } else {
0d18dcfc 1819 $conf->{$key} = $value;
1e3baf05
DM
1820 }
1821 }
1822 }
1823 }
1824
0d18dcfc 1825 $conf->{description} = $descr if $descr;
0581fe4f 1826
0d18dcfc 1827 delete $res->{snapstate}; # just to be sure
1e3baf05
DM
1828
1829 return $res;
1830}
1831
1858638f
DM
1832sub write_vm_config {
1833 my ($filename, $conf) = @_;
1e3baf05 1834
0d18dcfc
DM
1835 delete $conf->{snapstate}; # just to be sure
1836
1858638f
DM
1837 if ($conf->{cdrom}) {
1838 die "option ide2 conflicts with cdrom\n" if $conf->{ide2};
1839 $conf->{ide2} = $conf->{cdrom};
1840 delete $conf->{cdrom};
1841 }
1e3baf05
DM
1842
1843 # we do not use 'smp' any longer
1858638f
DM
1844 if ($conf->{sockets}) {
1845 delete $conf->{smp};
1846 } elsif ($conf->{smp}) {
1847 $conf->{sockets} = $conf->{smp};
1848 delete $conf->{cores};
1849 delete $conf->{smp};
1e3baf05
DM
1850 }
1851
264e519f 1852 if ($conf->{maxcpus} && $conf->{sockets}) {
3bd18e48
AD
1853 delete $conf->{sockets};
1854 }
264e519f 1855
ee2f90b1 1856 my $used_volids = {};
0d18dcfc 1857
ee2f90b1 1858 my $cleanup_config = sub {
a8e2f942 1859 my ($cref, $snapname) = @_;
1858638f 1860
ee2f90b1
DM
1861 foreach my $key (keys %$cref) {
1862 next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
1863 $key eq 'snapstate';
1864 my $value = $cref->{$key};
1865 eval { $value = check_type($key, $value); };
1866 die "unable to parse value of '$key' - $@" if $@;
1858638f 1867
ee2f90b1
DM
1868 $cref->{$key} = $value;
1869
a8e2f942 1870 if (!$snapname && valid_drivename($key)) {
ed221350 1871 my $drive = parse_drive($key, $value);
ee2f90b1
DM
1872 $used_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
1873 }
1e3baf05 1874 }
ee2f90b1
DM
1875 };
1876
1877 &$cleanup_config($conf);
1878 foreach my $snapname (keys %{$conf->{snapshots}}) {
a8e2f942 1879 &$cleanup_config($conf->{snapshots}->{$snapname}, $snapname);
1e3baf05
DM
1880 }
1881
1858638f
DM
1882 # remove 'unusedX' settings if we re-add a volume
1883 foreach my $key (keys %$conf) {
1884 my $value = $conf->{$key};
ee2f90b1 1885 if ($key =~ m/^unused/ && $used_volids->{$value}) {
1858638f 1886 delete $conf->{$key};
1e3baf05 1887 }
1858638f 1888 }
be190583 1889
0d18dcfc
DM
1890 my $generate_raw_config = sub {
1891 my ($conf) = @_;
0581fe4f 1892
0d18dcfc
DM
1893 my $raw = '';
1894
1895 # add description as comment to top of file
1896 my $descr = $conf->{description} || '';
1897 foreach my $cl (split(/\n/, $descr)) {
1898 $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
1899 }
1900
1901 foreach my $key (sort keys %$conf) {
1902 next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots';
1903 $raw .= "$key: $conf->{$key}\n";
1904 }
1905 return $raw;
1906 };
0581fe4f 1907
0d18dcfc
DM
1908 my $raw = &$generate_raw_config($conf);
1909 foreach my $snapname (sort keys %{$conf->{snapshots}}) {
1910 $raw .= "\n[$snapname]\n";
1911 $raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
1858638f 1912 }
1e3baf05 1913
1858638f
DM
1914 return $raw;
1915}
1e3baf05 1916
1858638f
DM
1917sub update_config_nolock {
1918 my ($vmid, $conf, $skiplock) = @_;
1e3baf05 1919
1858638f 1920 check_lock($conf) if !$skiplock;
97d62eb7 1921
1858638f 1922 my $cfspath = cfs_config_path($vmid);
1e3baf05 1923
1858638f
DM
1924 PVE::Cluster::cfs_write_file($cfspath, $conf);
1925}
1e3baf05 1926
1858638f
DM
1927sub update_config {
1928 my ($vmid, $conf, $skiplock) = @_;
1e3baf05 1929
1858638f 1930 lock_config($vmid, &update_config_nolock, $conf, $skiplock);
1e3baf05
DM
1931}
1932
19672434 1933sub load_defaults {
1e3baf05
DM
1934
1935 my $res = {};
1936
1937 # we use static defaults from our JSON schema configuration
1938 foreach my $key (keys %$confdesc) {
1939 if (defined(my $default = $confdesc->{$key}->{default})) {
1940 $res->{$key} = $default;
1941 }
1942 }
19672434 1943
1e3baf05
DM
1944 my $conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
1945 $res->{keyboard} = $conf->{keyboard} if $conf->{keyboard};
1946
1947 return $res;
1948}
1949
1950sub config_list {
1951 my $vmlist = PVE::Cluster::get_vmlist();
1952 my $res = {};
1953 return $res if !$vmlist || !$vmlist->{ids};
1954 my $ids = $vmlist->{ids};
1955
1e3baf05
DM
1956 foreach my $vmid (keys %$ids) {
1957 my $d = $ids->{$vmid};
1958 next if !$d->{node} || $d->{node} ne $nodename;
5ee957cc 1959 next if !$d->{type} || $d->{type} ne 'qemu';
1e3baf05
DM
1960 $res->{$vmid}->{exists} = 1;
1961 }
1962 return $res;
1963}
1964
64e13401
DM
1965# test if VM uses local resources (to prevent migration)
1966sub check_local_resources {
1967 my ($conf, $noerr) = @_;
1968
1969 my $loc_res = 0;
19672434 1970
e0ab7331
DM
1971 $loc_res = 1 if $conf->{hostusb}; # old syntax
1972 $loc_res = 1 if $conf->{hostpci}; # old syntax
64e13401 1973
0d29ab3b 1974 foreach my $k (keys %$conf) {
49ca581d 1975 next if $k =~ m/^usb/ && ($conf->{$k} eq 'spice');
2fe1a152 1976 $loc_res = 1 if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
64e13401
DM
1977 }
1978
1979 die "VM uses local resources\n" if $loc_res && !$noerr;
1980
1981 return $loc_res;
1982}
1983
719893a9 1984# check if used storages are available on all nodes (use by migrate)
47152e2e
DM
1985sub check_storage_availability {
1986 my ($storecfg, $conf, $node) = @_;
1987
1988 foreach_drive($conf, sub {
1989 my ($ds, $drive) = @_;
1990
1991 my $volid = $drive->{file};
1992 return if !$volid;
1993
1994 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
1995 return if !$sid;
1996
1997 # check if storage is available on both nodes
1998 my $scfg = PVE::Storage::storage_check_node($storecfg, $sid);
1999 PVE::Storage::storage_check_node($storecfg, $sid, $node);
2000 });
2001}
2002
719893a9
DM
2003# list nodes where all VM images are available (used by has_feature API)
2004sub shared_nodes {
2005 my ($conf, $storecfg) = @_;
2006
2007 my $nodelist = PVE::Cluster::get_nodelist();
2008 my $nodehash = { map { $_ => 1 } @$nodelist };
2009 my $nodename = PVE::INotify::nodename();
be190583 2010
719893a9
DM
2011 foreach_drive($conf, sub {
2012 my ($ds, $drive) = @_;
2013
2014 my $volid = $drive->{file};
2015 return if !$volid;
2016
2017 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
2018 if ($storeid) {
2019 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
2020 if ($scfg->{disable}) {
2021 $nodehash = {};
2022 } elsif (my $avail = $scfg->{nodes}) {
2023 foreach my $node (keys %$nodehash) {
2024 delete $nodehash->{$node} if !$avail->{$node};
2025 }
2026 } elsif (!$scfg->{shared}) {
2027 foreach my $node (keys %$nodehash) {
2028 delete $nodehash->{$node} if $node ne $nodename
2029 }
2030 }
2031 }
2032 });
2033
2034 return $nodehash
2035}
2036
1e3baf05
DM
2037sub check_lock {
2038 my ($conf) = @_;
2039
2040 die "VM is locked ($conf->{lock})\n" if $conf->{lock};
2041}
2042
2043sub check_cmdline {
2044 my ($pidfile, $pid) = @_;
2045
6b64503e
DM
2046 my $fh = IO::File->new("/proc/$pid/cmdline", "r");
2047 if (defined($fh)) {
1e3baf05
DM
2048 my $line = <$fh>;
2049 $fh->close;
2050 return undef if !$line;
6b64503e 2051 my @param = split(/\0/, $line);
1e3baf05
DM
2052
2053 my $cmd = $param[0];
06094efd 2054 return if !$cmd || ($cmd !~ m|kvm$| && $cmd !~ m|qemu-system-x86_64$|);
1e3baf05
DM
2055
2056 for (my $i = 0; $i < scalar (@param); $i++) {
2057 my $p = $param[$i];
2058 next if !$p;
2059 if (($p eq '-pidfile') || ($p eq '--pidfile')) {
2060 my $p = $param[$i+1];
2061 return 1 if $p && ($p eq $pidfile);
2062 return undef;
2063 }
2064 }
2065 }
2066 return undef;
2067}
2068
2069sub check_running {
7e8dcf2c 2070 my ($vmid, $nocheck, $node) = @_;
1e3baf05 2071
7e8dcf2c 2072 my $filename = config_file($vmid, $node);
1e3baf05
DM
2073
2074 die "unable to find configuration file for VM $vmid - no such machine\n"
e6c3b671 2075 if !$nocheck && ! -f $filename;
1e3baf05 2076
e6c3b671 2077 my $pidfile = pidfile_name($vmid);
1e3baf05 2078
e6c3b671
DM
2079 if (my $fd = IO::File->new("<$pidfile")) {
2080 my $st = stat($fd);
1e3baf05 2081 my $line = <$fd>;
6b64503e 2082 close($fd);
1e3baf05
DM
2083
2084 my $mtime = $st->mtime;
2085 if ($mtime > time()) {
2086 warn "file '$filename' modified in future\n";
2087 }
2088
2089 if ($line =~ m/^(\d+)$/) {
2090 my $pid = $1;
e6c3b671
DM
2091 if (check_cmdline($pidfile, $pid)) {
2092 if (my $pinfo = PVE::ProcFSTools::check_process_running($pid)) {
2093 return $pid;
2094 }
2095 }
1e3baf05
DM
2096 }
2097 }
2098
2099 return undef;
2100}
2101
2102sub vzlist {
19672434 2103
1e3baf05
DM
2104 my $vzlist = config_list();
2105
6b64503e 2106 my $fd = IO::Dir->new($var_run_tmpdir) || return $vzlist;
1e3baf05 2107
19672434 2108 while (defined(my $de = $fd->read)) {
1e3baf05
DM
2109 next if $de !~ m/^(\d+)\.pid$/;
2110 my $vmid = $1;
6b64503e
DM
2111 next if !defined($vzlist->{$vmid});
2112 if (my $pid = check_running($vmid)) {
1e3baf05
DM
2113 $vzlist->{$vmid}->{pid} = $pid;
2114 }
2115 }
2116
2117 return $vzlist;
2118}
2119
1e3baf05
DM
2120sub disksize {
2121 my ($storecfg, $conf) = @_;
2122
2123 my $bootdisk = $conf->{bootdisk};
2124 return undef if !$bootdisk;
2125 return undef if !valid_drivename($bootdisk);
2126
2127 return undef if !$conf->{$bootdisk};
2128
2129 my $drive = parse_drive($bootdisk, $conf->{$bootdisk});
2130 return undef if !defined($drive);
2131
2132 return undef if drive_is_cdrom($drive);
2133
2134 my $volid = $drive->{file};
2135 return undef if !$volid;
2136
24afaca0 2137 return $drive->{size};
1e3baf05
DM
2138}
2139
2140my $last_proc_pid_stat;
2141
03a33f30
DM
2142# get VM status information
2143# This must be fast and should not block ($full == false)
2144# We only query KVM using QMP if $full == true (this can be slow)
1e3baf05 2145sub vmstatus {
03a33f30 2146 my ($opt_vmid, $full) = @_;
1e3baf05
DM
2147
2148 my $res = {};
2149
19672434 2150 my $storecfg = PVE::Storage::config();
1e3baf05
DM
2151
2152 my $list = vzlist();
694fcad4 2153 my ($uptime) = PVE::ProcFSTools::read_proc_uptime(1);
1e3baf05 2154
ae4915a2
DM
2155 my $cpucount = $cpuinfo->{cpus} || 1;
2156
1e3baf05
DM
2157 foreach my $vmid (keys %$list) {
2158 next if $opt_vmid && ($vmid ne $opt_vmid);
2159
2160 my $cfspath = cfs_config_path($vmid);
2161 my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
2162
2163 my $d = {};
2164 $d->{pid} = $list->{$vmid}->{pid};
2165
2166 # fixme: better status?
2167 $d->{status} = $list->{$vmid}->{pid} ? 'running' : 'stopped';
2168
af990afe
DM
2169 my $size = disksize($storecfg, $conf);
2170 if (defined($size)) {
2171 $d->{disk} = 0; # no info available
1e3baf05
DM
2172 $d->{maxdisk} = $size;
2173 } else {
2174 $d->{disk} = 0;
2175 $d->{maxdisk} = 0;
2176 }
2177
2178 $d->{cpus} = ($conf->{sockets} || 1) * ($conf->{cores} || 1);
ae4915a2
DM
2179 $d->{cpus} = $cpucount if $d->{cpus} > $cpucount;
2180
1e3baf05 2181 $d->{name} = $conf->{name} || "VM $vmid";
19672434 2182 $d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024) : 0;
1e3baf05 2183
8b1accf7 2184 if ($conf->{balloon}) {
4bdb0514 2185 $d->{balloon_min} = $conf->{balloon}*(1024*1024);
074e01c8 2186 $d->{shares} = defined($conf->{shares}) ? $conf->{shares} : 1000;
8b1accf7
DM
2187 }
2188
1e3baf05
DM
2189 $d->{uptime} = 0;
2190 $d->{cpu} = 0;
1e3baf05
DM
2191 $d->{mem} = 0;
2192
2193 $d->{netout} = 0;
2194 $d->{netin} = 0;
2195
2196 $d->{diskread} = 0;
2197 $d->{diskwrite} = 0;
2198
4d8c851b
AD
2199 $d->{template} = is_template($conf);
2200
1e3baf05
DM
2201 $res->{$vmid} = $d;
2202 }
2203
2204 my $netdev = PVE::ProcFSTools::read_proc_net_dev();
2205 foreach my $dev (keys %$netdev) {
2206 next if $dev !~ m/^tap([1-9]\d*)i/;
2207 my $vmid = $1;
2208 my $d = $res->{$vmid};
2209 next if !$d;
19672434 2210
1e3baf05
DM
2211 $d->{netout} += $netdev->{$dev}->{receive};
2212 $d->{netin} += $netdev->{$dev}->{transmit};
2213 }
2214
1e3baf05
DM
2215 my $ctime = gettimeofday;
2216
2217 foreach my $vmid (keys %$list) {
2218
2219 my $d = $res->{$vmid};
2220 my $pid = $d->{pid};
2221 next if !$pid;
2222
694fcad4
DM
2223 my $pstat = PVE::ProcFSTools::read_proc_pid_stat($pid);
2224 next if !$pstat; # not running
19672434 2225
694fcad4 2226 my $used = $pstat->{utime} + $pstat->{stime};
1e3baf05 2227
694fcad4 2228 $d->{uptime} = int(($uptime - $pstat->{starttime})/$cpuinfo->{user_hz});
1e3baf05 2229
694fcad4 2230 if ($pstat->{vsize}) {
6b64503e 2231 $d->{mem} = int(($pstat->{rss}/$pstat->{vsize})*$d->{maxmem});
1e3baf05
DM
2232 }
2233
2234 my $old = $last_proc_pid_stat->{$pid};
2235 if (!$old) {
19672434
DM
2236 $last_proc_pid_stat->{$pid} = {
2237 time => $ctime,
1e3baf05
DM
2238 used => $used,
2239 cpu => 0,
1e3baf05
DM
2240 };
2241 next;
2242 }
2243
7f0b5beb 2244 my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz};
1e3baf05
DM
2245
2246 if ($dtime > 1000) {
2247 my $dutime = $used - $old->{used};
2248
ae4915a2 2249 $d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus};
1e3baf05 2250 $last_proc_pid_stat->{$pid} = {
19672434 2251 time => $ctime,
1e3baf05
DM
2252 used => $used,
2253 cpu => $d->{cpu},
1e3baf05
DM
2254 };
2255 } else {
2256 $d->{cpu} = $old->{cpu};
1e3baf05
DM
2257 }
2258 }
2259
f5eb281a 2260 return $res if !$full;
03a33f30
DM
2261
2262 my $qmpclient = PVE::QMPClient->new();
2263
64e7fcf2
DM
2264 my $ballooncb = sub {
2265 my ($vmid, $resp) = @_;
2266
2267 my $info = $resp->{'return'};
2268 return if !$info->{max_mem};
be190583 2269
64e7fcf2
DM
2270 my $d = $res->{$vmid};
2271
2272 # use memory assigned to VM
2273 $d->{maxmem} = $info->{max_mem};
2274 $d->{balloon} = $info->{actual};
be190583 2275
64e7fcf2
DM
2276 if (defined($info->{total_mem}) && defined($info->{free_mem})) {
2277 $d->{mem} = $info->{total_mem} - $info->{free_mem};
2278 $d->{freemem} = $info->{free_mem};
2279 }
2280
2281 };
2282
03a33f30
DM
2283 my $blockstatscb = sub {
2284 my ($vmid, $resp) = @_;
2285 my $data = $resp->{'return'} || [];
2286 my $totalrdbytes = 0;
2287 my $totalwrbytes = 0;
2288 for my $blockstat (@$data) {
2289 $totalrdbytes = $totalrdbytes + $blockstat->{stats}->{rd_bytes};
2290 $totalwrbytes = $totalwrbytes + $blockstat->{stats}->{wr_bytes};
2291 }
2292 $res->{$vmid}->{diskread} = $totalrdbytes;
2293 $res->{$vmid}->{diskwrite} = $totalwrbytes;
2294 };
2295
2296 my $statuscb = sub {
2297 my ($vmid, $resp) = @_;
64e7fcf2 2298
03a33f30 2299 $qmpclient->queue_cmd($vmid, $blockstatscb, 'query-blockstats');
64e7fcf2
DM
2300 # this fails if ballon driver is not loaded, so this must be
2301 # the last commnand (following command are aborted if this fails).
2302 $qmpclient->queue_cmd($vmid, $ballooncb, 'query-balloon');
03a33f30
DM
2303
2304 my $status = 'unknown';
2305 if (!defined($status = $resp->{'return'}->{status})) {
2306 warn "unable to get VM status\n";
2307 return;
2308 }
2309
2310 $res->{$vmid}->{qmpstatus} = $resp->{'return'}->{status};
2311 };
2312
2313 foreach my $vmid (keys %$list) {
2314 next if $opt_vmid && ($vmid ne $opt_vmid);
2315 next if !$res->{$vmid}->{pid}; # not running
2316 $qmpclient->queue_cmd($vmid, $statuscb, 'query-status');
2317 }
2318
2319 $qmpclient->queue_execute();
2320
2321 foreach my $vmid (keys %$list) {
2322 next if $opt_vmid && ($vmid ne $opt_vmid);
2323 $res->{$vmid}->{qmpstatus} = $res->{$vmid}->{status} if !$res->{$vmid}->{qmpstatus};
2324 }
2325
1e3baf05
DM
2326 return $res;
2327}
2328
2329sub foreach_drive {
2330 my ($conf, $func) = @_;
2331
2332 foreach my $ds (keys %$conf) {
2333 next if !valid_drivename($ds);
2334
6b64503e 2335 my $drive = parse_drive($ds, $conf->{$ds});
1e3baf05
DM
2336 next if !$drive;
2337
2338 &$func($ds, $drive);
2339 }
2340}
2341
d5769dc2
DM
2342sub foreach_volid {
2343 my ($conf, $func) = @_;
be190583 2344
d5769dc2
DM
2345 my $volhash = {};
2346
2347 my $test_volid = sub {
2348 my ($volid, $is_cdrom) = @_;
2349
2350 return if !$volid;
be190583 2351
d5769dc2
DM
2352 $volhash->{$volid} = $is_cdrom || 0;
2353 };
2354
ed221350 2355 foreach_drive($conf, sub {
d5769dc2
DM
2356 my ($ds, $drive) = @_;
2357 &$test_volid($drive->{file}, drive_is_cdrom($drive));
2358 });
2359
2360 foreach my $snapname (keys %{$conf->{snapshots}}) {
2361 my $snap = $conf->{snapshots}->{$snapname};
2362 &$test_volid($snap->{vmstate}, 0);
ed221350 2363 foreach_drive($snap, sub {
d5769dc2
DM
2364 my ($ds, $drive) = @_;
2365 &$test_volid($drive->{file}, drive_is_cdrom($drive));
2366 });
2367 }
2368
2369 foreach my $volid (keys %$volhash) {
be190583 2370 &$func($volid, $volhash->{$volid});
d5769dc2
DM
2371 }
2372}
2373
86b8228b
DM
2374sub vga_conf_has_spice {
2375 my ($vga) = @_;
2376
590e698c
DM
2377 return 0 if !$vga || $vga !~ m/^qxl([234])?$/;
2378
2379 return $1 || 1;
86b8228b
DM
2380}
2381
1e3baf05 2382sub config_to_command {
952958bc 2383 my ($storecfg, $vmid, $conf, $defaults, $forcemachine) = @_;
1e3baf05
DM
2384
2385 my $cmd = [];
8c559505
DM
2386 my $globalFlags = [];
2387 my $machineFlags = [];
2388 my $rtcFlags = [];
519ed28c 2389 my $cpuFlags = [];
5bdcf937 2390 my $devices = [];
b78ebef7 2391 my $pciaddr = '';
5bdcf937 2392 my $bridges = {};
1e3baf05
DM
2393 my $kvmver = kvm_user_version();
2394 my $vernum = 0; # unknown
a3c52213
DM
2395 if ($kvmver =~ m/^(\d+)\.(\d+)$/) {
2396 $vernum = $1*1000000+$2*1000;
2397 } elsif ($kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/) {
1e3baf05
DM
2398 $vernum = $1*1000000+$2*1000+$3;
2399 }
2400
a3c52213 2401 die "detected old qemu-kvm binary ($kvmver)\n" if $vernum < 15000;
1e3baf05
DM
2402
2403 my $have_ovz = -f '/proc/vz/vestat';
2404
db656e5f
DM
2405 my $q35 = machine_type_is_q35($conf);
2406
1e3baf05
DM
2407 push @$cmd, '/usr/bin/kvm';
2408
2409 push @$cmd, '-id', $vmid;
2410
2411 my $use_virtio = 0;
2412
c971c4f2
AD
2413 my $qmpsocket = qmp_socket($vmid);
2414 push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server,nowait";
2415 push @$cmd, '-mon', "chardev=qmp,mode=control";
2416
7b7c6d1b 2417 my $socket = vnc_socket($vmid);
1e3baf05
DM
2418 push @$cmd, '-vnc', "unix:$socket,x509,password";
2419
6b64503e 2420 push @$cmd, '-pidfile' , pidfile_name($vmid);
19672434 2421
1e3baf05
DM
2422 push @$cmd, '-daemonize';
2423
2796e7d5
DM
2424 if ($conf->{smbios1}) {
2425 push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
2426 }
2427
db656e5f
DM
2428 if ($q35) {
2429 # the q35 chipset support native usb2, so we enable usb controller
2430 # by default for this machine type
f8e83f05 2431 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
db656e5f 2432 } else {
f8e83f05
AD
2433 $pciaddr = print_pci_addr("piix3", $bridges);
2434 push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
24f0d39a 2435
f8e83f05 2436 my $use_usb2 = 0;
db656e5f
DM
2437 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
2438 next if !$conf->{"usb$i"};
2439 $use_usb2 = 1;
2440 }
2441 # include usb device config
2442 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
fcc573ab 2443 }
19672434 2444
5acbfe9e 2445 my $vga = $conf->{vga};
2fa3151e 2446
590e698c
DM
2447 my $qxlnum = vga_conf_has_spice($vga);
2448 $vga = 'qxl' if $qxlnum;
2fa3151e 2449
5acbfe9e 2450 if (!$vga) {
264e519f
DM
2451 if ($conf->{ostype} && ($conf->{ostype} eq 'win8' ||
2452 $conf->{ostype} eq 'win7' ||
5acbfe9e
DM
2453 $conf->{ostype} eq 'w2k8')) {
2454 $vga = 'std';
2455 } else {
2456 $vga = 'cirrus';
2457 }
2458 }
2459
1e3baf05 2460 # enable absolute mouse coordinates (needed by vnc)
5acbfe9e
DM
2461 my $tablet;
2462 if (defined($conf->{tablet})) {
2463 $tablet = $conf->{tablet};
2464 } else {
2465 $tablet = $defaults->{tablet};
590e698c 2466 $tablet = 0 if $qxlnum; # disable for spice because it is not needed
ef5e2be2 2467 $tablet = 0 if $vga =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
5acbfe9e
DM
2468 }
2469
db656e5f 2470 push @$devices, '-device', print_tabletdevice_full($conf) if $tablet;
90404354 2471
1e3baf05 2472 # host pci devices
040b06b7 2473 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
2e3b7e2a
AD
2474 my $d = parse_hostpci($conf->{"hostpci$i"});
2475 next if !$d;
2476
2477 my $pcie = $d->{pcie};
2478 if($pcie){
2479 die "q35 machine model is not enabled" if !$q35;
2480 $pciaddr = print_pcie_addr("hostpci$i");
2481 }else{
2482 $pciaddr = print_pci_addr("hostpci$i", $bridges);
2483 }
2484
2485 my $rombar = $d->{rombar} && $d->{rombar} eq 'off' ? ",rombar=0" : "";
2486 my $driver = $d->{driver} && $d->{driver} eq 'vfio' ? "vfio-pci" : "pci-assign";
2487 my $xvga = $d->{'x-vga'} && $d->{'x-vga'} eq 'on' ? ",x-vga=on" : "";
90404354
AD
2488 push @$cpuFlags, 'kvm=off' if $xvga && $xvga ne '';
2489
2e3b7e2a 2490 $driver = "vfio-pci" if $xvga ne '';
4543ecf0
AD
2491 my $pcidevices = $d->{pciid};
2492 my $multifunction = 1 if @$pcidevices > 1;
2e3b7e2a 2493
4543ecf0
AD
2494 my $j=0;
2495 foreach my $pcidevice (@$pcidevices) {
2e3b7e2a 2496
4543ecf0
AD
2497 my $id = "hostpci$i";
2498 $id .= ".$j" if $multifunction;
2499 my $addr = $pciaddr;
2500 $addr .= ".$j" if $multifunction;
2501 my $devicestr = "$driver,host=$pcidevice->{id}.$pcidevice->{function},id=$id$addr";
2502
2503 if($j == 0){
2504 $devicestr .= "$rombar$xvga";
2505 $devicestr .= ",multifunction=on" if $multifunction;
2506 }
2507
2508 push @$devices, '-device', $devicestr;
2509 $j++;
2510 }
1e3baf05
DM
2511 }
2512
2513 # usb devices
2514 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
2515 my $d = parse_usb_device($conf->{"usb$i"});
2516 next if !$d;
2517 if ($d->{vendorid} && $d->{productid}) {
5bdcf937 2518 push @$devices, '-device', "usb-host,vendorid=0x$d->{vendorid},productid=0x$d->{productid}";
1e3baf05 2519 } elsif (defined($d->{hostbus}) && defined($d->{hostport})) {
5bdcf937 2520 push @$devices, '-device', "usb-host,hostbus=$d->{hostbus},hostport=$d->{hostport}";
80401dd8
DM
2521 } elsif ($d->{spice}) {
2522 # usb redir support for spice
2523 push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
2524 push @$devices, '-device', "usb-redir,chardev=usbredirchardev$i,id=usbredirdev$i,bus=ehci.0";
1e3baf05
DM
2525 }
2526 }
2527
1e3baf05 2528 # serial devices
bae179aa 2529 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
34978be3 2530 if (my $path = $conf->{"serial$i"}) {
9f9d2fb2
DM
2531 if ($path eq 'socket') {
2532 my $socket = "/var/run/qemu-server/${vmid}.serial$i";
2533 push @$devices, '-chardev', "socket,id=serial$i,path=$socket,server,nowait";
2534 push @$devices, '-device', "isa-serial,chardev=serial$i";
2535 } else {
2536 die "no such serial device\n" if ! -c $path;
2537 push @$devices, '-chardev', "tty,id=serial$i,path=$path";
2538 push @$devices, '-device', "isa-serial,chardev=serial$i";
2539 }
34978be3 2540 }
1e3baf05
DM
2541 }
2542
2543 # parallel devices
1989a89c 2544 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
34978be3 2545 if (my $path = $conf->{"parallel$i"}) {
19672434 2546 die "no such parallel device\n" if ! -c $path;
32e69805 2547 my $devtype = $path =~ m!^/dev/usb/lp! ? 'tty' : 'parport';
4c5dbaf6 2548 push @$devices, '-chardev', "$devtype,id=parallel$i,path=$path";
5bdcf937 2549 push @$devices, '-device', "isa-parallel,chardev=parallel$i";
34978be3 2550 }
1e3baf05
DM
2551 }
2552
2553 my $vmname = $conf->{name} || "vm$vmid";
2554
2555 push @$cmd, '-name', $vmname;
19672434 2556
1e3baf05
DM
2557 my $sockets = 1;
2558 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
2559 $sockets = $conf->{sockets} if $conf->{sockets};
2560
2561 my $cores = $conf->{cores} || 1;
3bd18e48
AD
2562 my $maxcpus = $conf->{maxcpus} if $conf->{maxcpus};
2563
264e519f 2564 if ($maxcpus) {
3bd18e48 2565 push @$cmd, '-smp', "cpus=$cores,maxcpus=$maxcpus";
264e519f 2566 } else {
3bd18e48
AD
2567 push @$cmd, '-smp', "sockets=$sockets,cores=$cores";
2568 }
1e3baf05 2569
1e3baf05
DM
2570 push @$cmd, '-nodefaults';
2571
32baffb4 2572 my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
3b408e82 2573
0888fdce
DM
2574 my $bootindex_hash = {};
2575 my $i = 1;
2576 foreach my $o (split(//, $bootorder)) {
2577 $bootindex_hash->{$o} = $i*100;
2578 $i++;
afdb31d5 2579 }
3b408e82
DM
2580
2581 push @$cmd, '-boot', "menu=on";
1e3baf05 2582
6b64503e 2583 push @$cmd, '-no-acpi' if defined($conf->{acpi}) && $conf->{acpi} == 0;
1e3baf05 2584
6b64503e 2585 push @$cmd, '-no-reboot' if defined($conf->{reboot}) && $conf->{reboot} == 0;
1e3baf05 2586
ef5e2be2 2587 push @$cmd, '-vga', $vga if $vga && $vga !~ m/^serial\d+$/; # for kvm 77 and later
1e3baf05
DM
2588
2589 # time drift fix
6b64503e 2590 my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
1e3baf05 2591
6b64503e 2592 my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
8c559505 2593 my $useLocaltime = $conf->{localtime};
1e3baf05
DM
2594
2595 if (my $ost = $conf->{ostype}) {
6b9d84cf 2596 # other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26, solaris
1e3baf05
DM
2597
2598 if ($ost =~ m/^w/) { # windows
8c559505 2599 $useLocaltime = 1 if !defined($conf->{localtime});
1e3baf05 2600
8c559505 2601 # use time drift fix when acpi is enabled
6b64503e 2602 if (!(defined($conf->{acpi}) && $conf->{acpi} == 0)) {
8c559505 2603 $tdf = 1 if !defined($conf->{tdf});
1e3baf05
DM
2604 }
2605 }
2606
be190583 2607 if ($ost eq 'win7' || $ost eq 'win8' || $ost eq 'w2k8' ||
a70ebde3 2608 $ost eq 'wvista') {
8c559505 2609 push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
b7e0c8bf 2610 push @$cmd, '-no-hpet';
462e8d19
AD
2611 #push @$cpuFlags , 'hv_vapic" if !$nokvm; #fixme, my win2008R2 hang at boot with this
2612 push @$cpuFlags , 'hv_spinlocks=0xffff' if !$nokvm;
2613 }
2614
2615 if ($ost eq 'win7' || $ost eq 'win8') {
2616 push @$cpuFlags , 'hv_relaxed' if !$nokvm;
b7e0c8bf 2617 }
1e3baf05
DM
2618 }
2619
8c559505
DM
2620 push @$rtcFlags, 'driftfix=slew' if $tdf;
2621
7f0b5beb 2622 if ($nokvm) {
8c559505 2623 push @$machineFlags, 'accel=tcg';
7f0b5beb
DM
2624 } else {
2625 die "No accelerator found!\n" if !$cpuinfo->{hvm};
2626 }
1e3baf05 2627
952958bc
DM
2628 my $machine_type = $forcemachine || $conf->{machine};
2629 if ($machine_type) {
2630 push @$machineFlags, "type=${machine_type}";
3bafc510
DM
2631 }
2632
8c559505
DM
2633 if ($conf->{startdate}) {
2634 push @$rtcFlags, "base=$conf->{startdate}";
2635 } elsif ($useLocaltime) {
2636 push @$rtcFlags, 'base=localtime';
2637 }
1e3baf05 2638
519ed28c
AD
2639 my $cpu = $nokvm ? "qemu64" : "kvm64";
2640 $cpu = $conf->{cpu} if $conf->{cpu};
2641
4dc339e7
AD
2642 push @$cpuFlags , '+lahf_lm' if $cpu eq 'kvm64';
2643
6b9d84cf
AD
2644 push @$cpuFlags , '+x2apic' if !$nokvm && $conf->{ostype} ne 'solaris';
2645
2646 push @$cpuFlags , '-x2apic' if $conf->{ostype} eq 'solaris';
519ed28c 2647
2e1a5389
AD
2648 push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
2649
be190583 2650 $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
519ed28c 2651
c0efd8cd
DM
2652 # Note: enforce needs kernel 3.10, so we do not use it for now
2653 # push @$cmd, '-cpu', "$cpu,enforce";
2654 push @$cmd, '-cpu', $cpu;
519ed28c 2655
1e3baf05
DM
2656 push @$cmd, '-S' if $conf->{freeze};
2657
2658 # set keyboard layout
2659 my $kb = $conf->{keyboard} || $defaults->{keyboard};
2660 push @$cmd, '-k', $kb if $kb;
2661
2662 # enable sound
2663 #my $soundhw = $conf->{soundhw} || $defaults->{soundhw};
2664 #push @$cmd, '-soundhw', 'es1370';
2665 #push @$cmd, '-soundhw', $soundhw if $soundhw;
ab6a046f 2666
bc84dcca 2667 if($conf->{agent}) {
ab6a046f
AD
2668 my $qgasocket = qga_socket($vmid);
2669 my $pciaddr = print_pci_addr("qga0", $bridges);
2670 push @$devices, '-chardev', "socket,path=$qgasocket,server,nowait,id=qga0";
2671 push @$devices, '-device', "virtio-serial,id=qga0$pciaddr";
2672 push @$devices, '-device', 'virtserialport,chardev=qga0,name=org.qemu.guest_agent.0';
2673 }
2674
1d794448 2675 my $spice_port;
2fa3151e 2676
590e698c
DM
2677 if ($qxlnum) {
2678 if ($qxlnum > 1) {
2679 if ($conf->{ostype} && $conf->{ostype} =~ m/^w/){
2680 for(my $i = 1; $i < $qxlnum; $i++){
2681 my $pciaddr = print_pci_addr("vga$i", $bridges);
2682 push @$cmd, '-device', "qxl,id=vga$i,ram_size=67108864,vram_size=33554432$pciaddr";
2683 }
2684 } else {
2685 # assume other OS works like Linux
2686 push @$cmd, '-global', 'qxl-vga.ram_size=134217728';
2687 push @$cmd, '-global', 'qxl-vga.vram_size=67108864';
2fa3151e
AD
2688 }
2689 }
2690
1011b570 2691 my $pciaddr = print_pci_addr("spice", $bridges);
95a4b4a9 2692
cd339d1f 2693 $spice_port = PVE::Tools::next_spice_port();
943340a6 2694
1d794448 2695 push @$cmd, '-spice', "tls-port=${spice_port},addr=127.0.0.1,tls-ciphers=DES-CBC3-SHA,seamless-migration=on";
1011b570 2696
1011b570
DM
2697 push @$cmd, '-device', "virtio-serial,id=spice$pciaddr";
2698 push @$cmd, '-chardev', "spicevmc,id=vdagent,name=vdagent";
2699 push @$cmd, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
2700 }
2701
8d9ae0d2
DM
2702 # enable balloon by default, unless explicitly disabled
2703 if (!defined($conf->{balloon}) || $conf->{balloon}) {
2704 $pciaddr = print_pci_addr("balloon0", $bridges);
2705 push @$devices, '-device', "virtio-balloon-pci,id=balloon0$pciaddr";
2706 }
1e3baf05 2707
0ea9541d
DM
2708 if ($conf->{watchdog}) {
2709 my $wdopts = parse_watchdog($conf->{watchdog});
5bdcf937 2710 $pciaddr = print_pci_addr("watchdog", $bridges);
0a40e8ea 2711 my $watchdog = $wdopts->{model} || 'i6300esb';
5bdcf937
AD
2712 push @$devices, '-device', "$watchdog$pciaddr";
2713 push @$devices, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
0ea9541d
DM
2714 }
2715
1e3baf05 2716 my $vollist = [];
941e0c42 2717 my $scsicontroller = {};
26ee04b6 2718 my $ahcicontroller = {};
cdd20088 2719 my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
1e3baf05 2720
5881b913
DM
2721 # Add iscsi initiator name if available
2722 if (my $initiator = get_initiator_name()) {
2723 push @$devices, '-iscsi', "initiator-name=$initiator";
2724 }
2725
1e3baf05
DM
2726 foreach_drive($conf, sub {
2727 my ($ds, $drive) = @_;
2728
ff1a2432 2729 if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
1e3baf05 2730 push @$vollist, $drive->{file};
ff1a2432 2731 }
afdb31d5 2732
1e3baf05 2733 $use_virtio = 1 if $ds =~ m/^virtio/;
3b408e82
DM
2734
2735 if (drive_is_cdrom ($drive)) {
2736 if ($bootindex_hash->{d}) {
2737 $drive->{bootindex} = $bootindex_hash->{d};
2738 $bootindex_hash->{d} += 1;
2739 }
2740 } else {
2741 if ($bootindex_hash->{c}) {
2742 $drive->{bootindex} = $bootindex_hash->{c} if $conf->{bootdisk} && ($conf->{bootdisk} eq $ds);
2743 $bootindex_hash->{c} += 1;
2744 }
2745 }
2746
941e0c42 2747 if ($drive->{interface} eq 'scsi') {
cdd20088 2748
5b952ff5 2749 my $maxdev = ($scsihw !~ m/^lsi/) ? 256 : 7;
cdd20088 2750 my $controller = int($drive->{index} / $maxdev);
5bdcf937
AD
2751 $pciaddr = print_pci_addr("scsihw$controller", $bridges);
2752 push @$devices, '-device', "$scsihw,id=scsihw$controller$pciaddr" if !$scsicontroller->{$controller};
cdd20088 2753 $scsicontroller->{$controller}=1;
941e0c42 2754 }
3b408e82 2755
26ee04b6
DA
2756 if ($drive->{interface} eq 'sata') {
2757 my $controller = int($drive->{index} / $MAX_SATA_DISKS);
5bdcf937
AD
2758 $pciaddr = print_pci_addr("ahci$controller", $bridges);
2759 push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr" if !$ahcicontroller->{$controller};
26ee04b6
DA
2760 $ahcicontroller->{$controller}=1;
2761 }
46f58b5f 2762
15b21acc
MR
2763 my $drive_cmd = print_drive_full($storecfg, $vmid, $drive);
2764 push @$devices, '-drive',$drive_cmd;
46f58b5f 2765 push @$devices, '-device', print_drivedevice_full($storecfg, $conf, $vmid, $drive, $bridges);
1e3baf05
DM
2766 });
2767
2768 push @$cmd, '-m', $conf->{memory} || $defaults->{memory};
19672434 2769
cc4d6182 2770 for (my $i = 0; $i < $MAX_NETS; $i++) {
5f0c4c32 2771 next if !$conf->{"net$i"};
cc4d6182
DA
2772 my $d = parse_net($conf->{"net$i"});
2773 next if !$d;
1e3baf05 2774
cc4d6182 2775 $use_virtio = 1 if $d->{model} eq 'virtio';
1e3baf05 2776
cc4d6182
DA
2777 if ($bootindex_hash->{n}) {
2778 $d->{bootindex} = $bootindex_hash->{n};
2779 $bootindex_hash->{n} += 1;
2780 }
1e3baf05 2781
cc4d6182 2782 my $netdevfull = print_netdev_full($vmid,$conf,$d,"net$i");
5bdcf937
AD
2783 push @$devices, '-netdev', $netdevfull;
2784
2785 my $netdevicefull = print_netdevice_full($vmid,$conf,$d,"net$i",$bridges);
2786 push @$devices, '-device', $netdevicefull;
2787 }
1e3baf05 2788
db656e5f
DM
2789 if (!$q35) {
2790 # add pci bridges
f8e83f05
AD
2791 while (my ($k, $v) = each %$bridges) {
2792 $pciaddr = print_pci_addr("pci.$k");
2793 unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
2794 }
19672434
DM
2795 }
2796
1e3baf05
DM
2797 # hack: virtio with fairsched is unreliable, so we do not use fairsched
2798 # when the VM uses virtio devices.
19672434
DM
2799 if (!$use_virtio && $have_ovz) {
2800
6b64503e 2801 my $cpuunits = defined($conf->{cpuunits}) ?
1e3baf05
DM
2802 $conf->{cpuunits} : $defaults->{cpuunits};
2803
2804 push @$cmd, '-cpuunits', $cpuunits if $cpuunits;
2805
2806 # fixme: cpulimit is currently ignored
2807 #push @$cmd, '-cpulimit', $conf->{cpulimit} if $conf->{cpulimit};
2808 }
2809
2810 # add custom args
2811 if ($conf->{args}) {
3ada46c9 2812 my $aa = PVE::Tools::split_args($conf->{args});
1e3baf05
DM
2813 push @$cmd, @$aa;
2814 }
2815
5bdcf937 2816 push @$cmd, @$devices;
be190583 2817 push @$cmd, '-rtc', join(',', @$rtcFlags)
8c559505 2818 if scalar(@$rtcFlags);
be190583 2819 push @$cmd, '-machine', join(',', @$machineFlags)
8c559505
DM
2820 if scalar(@$machineFlags);
2821 push @$cmd, '-global', join(',', @$globalFlags)
2822 if scalar(@$globalFlags);
2823
1d794448 2824 return wantarray ? ($cmd, $vollist, $spice_port) : $cmd;
1e3baf05 2825}
19672434 2826
1e3baf05
DM
2827sub vnc_socket {
2828 my ($vmid) = @_;
2829 return "${var_run_tmpdir}/$vmid.vnc";
2830}
2831
943340a6 2832sub spice_port {
1011b570 2833 my ($vmid) = @_;
943340a6 2834
1d794448 2835 my $res = vm_mon_cmd($vmid, 'query-spice');
943340a6
DM
2836
2837 return $res->{'tls-port'} || $res->{'port'} || die "no spice port\n";
1011b570
DM
2838}
2839
c971c4f2
AD
2840sub qmp_socket {
2841 my ($vmid) = @_;
2842 return "${var_run_tmpdir}/$vmid.qmp";
2843}
2844
ab6a046f
AD
2845sub qga_socket {
2846 my ($vmid) = @_;
2847 return "${var_run_tmpdir}/$vmid.qga";
2848}
2849
1e3baf05
DM
2850sub pidfile_name {
2851 my ($vmid) = @_;
2852 return "${var_run_tmpdir}/$vmid.pid";
2853}
2854
86fdcfb2
DA
2855sub vm_devices_list {
2856 my ($vmid) = @_;
2857
ceea9078
DM
2858 my $res = vm_mon_cmd($vmid, 'query-pci');
2859
2860 my $devices = {};
2861 foreach my $pcibus (@$res) {
2862 foreach my $device (@{$pcibus->{devices}}) {
2863 next if !$device->{'qdev_id'};
2864 $devices->{$device->{'qdev_id'}} = $device;
1dc4f496
DM
2865 }
2866 }
86fdcfb2 2867
1dc4f496 2868 return $devices;
86fdcfb2
DA
2869}
2870
ec21aa11 2871sub vm_deviceplug {
f19d1c47 2872 my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
ae57f6b3 2873
cd6ecb89 2874 return 1 if !check_running($vmid);
db656e5f
DM
2875
2876 my $q35 = machine_type_is_q35($conf);
2877
cd6ecb89 2878 if ($deviceid eq 'tablet') {
db656e5f 2879 qemu_deviceadd($vmid, print_tabletdevice_full($conf));
cd6ecb89
AD
2880 return 1;
2881 }
2882
2dbe827e 2883 return 1 if !$conf->{hotplug};
afdb31d5 2884
95d6343b
DA
2885 my $devices_list = vm_devices_list($vmid);
2886 return 1 if defined($devices_list->{$deviceid});
2887
40f28a9f
AD
2888 qemu_bridgeadd($storecfg, $conf, $vmid, $deviceid); #add bridge if we need it for the device
2889
5e5dcb73
DA
2890 if ($deviceid =~ m/^(virtio)(\d+)$/) {
2891 return undef if !qemu_driveadd($storecfg, $vmid, $device);
cdd20088 2892 my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
5e5dcb73
DA
2893 qemu_deviceadd($vmid, $devicefull);
2894 if(!qemu_deviceaddverify($vmid, $deviceid)) {
2895 qemu_drivedel($vmid, $deviceid);
2896 return undef;
2897 }
f19d1c47 2898 }
cfc817c7 2899
cdd20088
AD
2900 if ($deviceid =~ m/^(scsihw)(\d+)$/) {
2901 my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
cfc817c7 2902 my $pciaddr = print_pci_addr($deviceid);
cdd20088 2903 my $devicefull = "$scsihw,id=$deviceid$pciaddr";
cfc817c7
DA
2904 qemu_deviceadd($vmid, $devicefull);
2905 return undef if(!qemu_deviceaddverify($vmid, $deviceid));
2906 }
2907
a4f091a0 2908 if ($deviceid =~ m/^(scsi)(\d+)$/) {
5b952ff5 2909 return 1 if ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)); #virtio-scsi not yet support hotplug
cdd20088 2910 return undef if !qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
a4f091a0 2911 return undef if !qemu_driveadd($storecfg, $vmid, $device);
cdd20088 2912 my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
a4f091a0
DA
2913 if(!qemu_deviceadd($vmid, $devicefull)) {
2914 qemu_drivedel($vmid, $deviceid);
2915 return undef;
2916 }
2917 }
2918
2630d2a9
DA
2919 if ($deviceid =~ m/^(net)(\d+)$/) {
2920 return undef if !qemu_netdevadd($vmid, $conf, $device, $deviceid);
2921 my $netdevicefull = print_netdevice_full($vmid, $conf, $device, $deviceid);
2922 qemu_deviceadd($vmid, $netdevicefull);
2923 if(!qemu_deviceaddverify($vmid, $deviceid)) {
2924 qemu_netdevdel($vmid, $deviceid);
2925 return undef;
2926 }
2927 }
2928
f8e83f05
AD
2929
2930 if (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
40f28a9f
AD
2931 my $bridgeid = $2;
2932 my $pciaddr = print_pci_addr($deviceid);
2933 my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
2934 qemu_deviceadd($vmid, $devicefull);
2935 return undef if !qemu_deviceaddverify($vmid, $deviceid);
2936 }
2937
5e5dcb73 2938 return 1;
a4dea331
DA
2939}
2940
ec21aa11 2941sub vm_deviceunplug {
f19d1c47 2942 my ($vmid, $conf, $deviceid) = @_;
873c2d69 2943
cd6ecb89
AD
2944 return 1 if !check_running ($vmid);
2945
2946 if ($deviceid eq 'tablet') {
2947 qemu_devicedel($vmid, $deviceid);
2948 return 1;
2949 }
2950
2dbe827e 2951 return 1 if !$conf->{hotplug};
873c2d69 2952
95d6343b
DA
2953 my $devices_list = vm_devices_list($vmid);
2954 return 1 if !defined($devices_list->{$deviceid});
2955
ae57f6b3 2956 die "can't unplug bootdisk" if $conf->{bootdisk} && $conf->{bootdisk} eq $deviceid;
f19d1c47 2957
5e5dcb73 2958 if ($deviceid =~ m/^(virtio)(\d+)$/) {
5e5dcb73
DA
2959 qemu_devicedel($vmid, $deviceid);
2960 return undef if !qemu_devicedelverify($vmid, $deviceid);
1f219ef5 2961 return undef if !qemu_drivedel($vmid, $deviceid);
5e5dcb73 2962 }
cfc817c7
DA
2963
2964 if ($deviceid =~ m/^(lsi)(\d+)$/) {
2965 return undef if !qemu_devicedel($vmid, $deviceid);
2966 }
2967
a4f091a0
DA
2968 if ($deviceid =~ m/^(scsi)(\d+)$/) {
2969 return undef if !qemu_devicedel($vmid, $deviceid);
2970 return undef if !qemu_drivedel($vmid, $deviceid);
2971 }
2972
2630d2a9 2973 if ($deviceid =~ m/^(net)(\d+)$/) {
2630d2a9
DA
2974 qemu_devicedel($vmid, $deviceid);
2975 return undef if !qemu_devicedelverify($vmid, $deviceid);
750886f8 2976 return undef if !qemu_netdevdel($vmid, $deviceid);
2630d2a9
DA
2977 }
2978
5e5dcb73
DA
2979 return 1;
2980}
2981
2982sub qemu_deviceadd {
2983 my ($vmid, $devicefull) = @_;
873c2d69 2984
d695b5b7
AD
2985 $devicefull = "driver=".$devicefull;
2986 my %options = split(/[=,]/, $devicefull);
f19d1c47 2987
d695b5b7
AD
2988 vm_mon_cmd($vmid, "device_add" , %options);
2989 return 1;
5e5dcb73 2990}
afdb31d5 2991
5e5dcb73
DA
2992sub qemu_devicedel {
2993 my($vmid, $deviceid) = @_;
5a77d8c1
AD
2994 my $ret = vm_mon_cmd($vmid, "device_del", id => $deviceid);
2995 return 1;
5e5dcb73
DA
2996}
2997
2998sub qemu_driveadd {
2999 my($storecfg, $vmid, $device) = @_;
3000
3001 my $drive = print_drive_full($storecfg, $vmid, $device);
7b7c6d1b 3002 my $ret = vm_human_monitor_command($vmid, "drive_add auto $drive");
5e5dcb73
DA
3003 # If the command succeeds qemu prints: "OK"
3004 if ($ret !~ m/OK/s) {
3005 syslog("err", "adding drive failed: $ret");
3006 return undef;
f19d1c47 3007 }
5e5dcb73
DA
3008 return 1;
3009}
afdb31d5 3010
5e5dcb73
DA
3011sub qemu_drivedel {
3012 my($vmid, $deviceid) = @_;
873c2d69 3013
7b7c6d1b 3014 my $ret = vm_human_monitor_command($vmid, "drive_del drive-$deviceid");
5e5dcb73
DA
3015 $ret =~ s/^\s+//;
3016 if ($ret =~ m/Device \'.*?\' not found/s) {
afdb31d5 3017 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
5e5dcb73
DA
3018 }
3019 elsif ($ret ne "") {
3020 syslog("err", "deleting drive $deviceid failed : $ret");
3021 return undef;
873c2d69 3022 }
5e5dcb73
DA
3023 return 1;
3024}
f19d1c47 3025
5e5dcb73
DA
3026sub qemu_deviceaddverify {
3027 my ($vmid,$deviceid) = @_;
873c2d69 3028
5e5dcb73
DA
3029 for (my $i = 0; $i <= 5; $i++) {
3030 my $devices_list = vm_devices_list($vmid);
3031 return 1 if defined($devices_list->{$deviceid});
3032 sleep 1;
afdb31d5 3033 }
5e5dcb73
DA
3034 syslog("err", "error on hotplug device $deviceid");
3035 return undef;
3036}
afdb31d5 3037
5e5dcb73
DA
3038
3039sub qemu_devicedelverify {
3040 my ($vmid,$deviceid) = @_;
3041
3042 #need to verify the device is correctly remove as device_del is async and empty return is not reliable
3043 for (my $i = 0; $i <= 5; $i++) {
3044 my $devices_list = vm_devices_list($vmid);
3045 return 1 if !defined($devices_list->{$deviceid});
3046 sleep 1;
afdb31d5 3047 }
5e5dcb73
DA
3048 syslog("err", "error on hot-unplugging device $deviceid");
3049 return undef;
873c2d69
DA
3050}
3051
cdd20088 3052sub qemu_findorcreatescsihw {
cfc817c7
DA
3053 my ($storecfg, $conf, $vmid, $device) = @_;
3054
5b952ff5 3055 my $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
cfc817c7 3056 my $controller = int($device->{index} / $maxdev);
cdd20088 3057 my $scsihwid="scsihw$controller";
cfc817c7
DA
3058 my $devices_list = vm_devices_list($vmid);
3059
cdd20088
AD
3060 if(!defined($devices_list->{$scsihwid})) {
3061 return undef if !vm_deviceplug($storecfg, $conf, $vmid, $scsihwid);
cfc817c7
DA
3062 }
3063 return 1;
3064}
3065
40f28a9f
AD
3066sub qemu_bridgeadd {
3067 my ($storecfg, $conf, $vmid, $device) = @_;
3068
3069 my $bridges = {};
3070 my $bridgeid = undef;
3071 print_pci_addr($device, $bridges);
3072
3073 while (my ($k, $v) = each %$bridges) {
3074 $bridgeid = $k;
3075 }
0e616534 3076 return if !$bridgeid || $bridgeid < 1;
40f28a9f
AD
3077 my $bridge = "pci.$bridgeid";
3078 my $devices_list = vm_devices_list($vmid);
3079
3080 if(!defined($devices_list->{$bridge})) {
3081 return undef if !vm_deviceplug($storecfg, $conf, $vmid, $bridge);
3082 }
3083 return 1;
3084}
3085
2630d2a9
DA
3086sub qemu_netdevadd {
3087 my ($vmid, $conf, $device, $deviceid) = @_;
3088
3089 my $netdev = print_netdev_full($vmid, $conf, $device, $deviceid);
73aa03b8 3090 my %options = split(/[=,]/, $netdev);
2630d2a9 3091
73aa03b8
AD
3092 vm_mon_cmd($vmid, "netdev_add", %options);
3093 return 1;
2630d2a9
DA
3094}
3095
3096sub qemu_netdevdel {
3097 my ($vmid, $deviceid) = @_;
3098
89c1e0f4
AD
3099 vm_mon_cmd($vmid, "netdev_del", id => $deviceid);
3100 return 1;
2630d2a9
DA
3101}
3102
838776ab
AD
3103sub qemu_cpu_hotplug {
3104 my ($vmid, $conf, $cores) = @_;
3105
3106 die "new cores config is not defined" if !$cores;
264e519f
DM
3107 die "you can't add more cores than maxcpus"
3108 if $conf->{maxcpus} && ($cores > $conf->{maxcpus});
838776ab
AD
3109 return if !check_running($vmid);
3110
3111 my $currentcores = $conf->{cores} if $conf->{cores};
3112 die "current cores is not defined" if !$currentcores;
3113 die "maxcpus is not defined" if !$conf->{maxcpus};
264e519f
DM
3114 raise_param_exc({ 'cores' => "online cpu unplug is not yet possible" })
3115 if($cores < $currentcores);
838776ab
AD
3116
3117 my $currentrunningcores = vm_mon_cmd($vmid, "query-cpus");
264e519f
DM
3118 raise_param_exc({ 'cores' => "cores number if running vm is different than configuration" })
3119 if scalar (@{$currentrunningcores}) != $currentcores;
838776ab 3120
264e519f 3121 for(my $i = $currentcores; $i < $cores; $i++) {
838776ab
AD
3122 vm_mon_cmd($vmid, "cpu-add", id => int($i));
3123 }
3124}
3125
affd2f88
AD
3126sub qemu_block_set_io_throttle {
3127 my ($vmid, $deviceid, $bps, $bps_rd, $bps_wr, $iops, $iops_rd, $iops_wr) = @_;
3128
f3f323a3
AD
3129 return if !check_running($vmid) ;
3130
f3f323a3
AD
3131 vm_mon_cmd($vmid, "block_set_io_throttle", device => $deviceid, bps => int($bps), bps_rd => int($bps_rd), bps_wr => int($bps_wr), iops => int($iops), iops_rd => int($iops_rd), iops_wr => int($iops_wr));
3132
affd2f88
AD
3133}
3134
f5eb281a 3135# old code, only used to shutdown old VM after update
dab36e1e
DM
3136sub __read_avail {
3137 my ($fh, $timeout) = @_;
3138
3139 my $sel = new IO::Select;
3140 $sel->add($fh);
3141
3142 my $res = '';
3143 my $buf;
3144
3145 my @ready;
3146 while (scalar (@ready = $sel->can_read($timeout))) {
3147 my $count;
3148 if ($count = $fh->sysread($buf, 8192)) {
3149 if ($buf =~ /^(.*)\(qemu\) $/s) {
3150 $res .= $1;
3151 last;
3152 } else {
3153 $res .= $buf;
3154 }
3155 } else {
3156 if (!defined($count)) {
3157 die "$!\n";
3158 }
3159 last;
3160 }
3161 }
3162
3163 die "monitor read timeout\n" if !scalar(@ready);
f5eb281a 3164
dab36e1e
DM
3165 return $res;
3166}
3167
f5eb281a 3168# old code, only used to shutdown old VM after update
dab36e1e
DM
3169sub vm_monitor_command {
3170 my ($vmid, $cmdstr, $nocheck) = @_;
f5eb281a 3171
dab36e1e
DM
3172 my $res;
3173
3174 eval {
3175 die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
3176
3177 my $sname = "${var_run_tmpdir}/$vmid.mon";
3178
3179 my $sock = IO::Socket::UNIX->new( Peer => $sname ) ||
3180 die "unable to connect to VM $vmid socket - $!\n";
3181
3182 my $timeout = 3;
3183
3184 # hack: migrate sometime blocks the monitor (when migrate_downtime
3185 # is set)
3186 if ($cmdstr =~ m/^(info\s+migrate|migrate\s)/) {
3187 $timeout = 60*60; # 1 hour
3188 }
3189
3190 # read banner;
3191 my $data = __read_avail($sock, $timeout);
3192
3193 if ($data !~ m/^QEMU\s+(\S+)\s+monitor\s/) {
3194 die "got unexpected qemu monitor banner\n";
3195 }
3196
3197 my $sel = new IO::Select;
3198 $sel->add($sock);
3199
3200 if (!scalar(my @ready = $sel->can_write($timeout))) {
3201 die "monitor write error - timeout";
3202 }
3203
3204 my $fullcmd = "$cmdstr\r";
3205
3206 # syslog('info', "VM $vmid monitor command: $cmdstr");
3207
3208 my $b;
3209 if (!($b = $sock->syswrite($fullcmd)) || ($b != length($fullcmd))) {
3210 die "monitor write error - $!";
3211 }
3212
3213 return if ($cmdstr eq 'q') || ($cmdstr eq 'quit');
3214
3215 $timeout = 20;
3216
3217 if ($cmdstr =~ m/^(info\s+migrate|migrate\s)/) {
3218 $timeout = 60*60; # 1 hour
3219 } elsif ($cmdstr =~ m/^(eject|change)/) {
3220 $timeout = 60; # note: cdrom mount command is slow
3221 }
3222 if ($res = __read_avail($sock, $timeout)) {
3223
3224 my @lines = split("\r?\n", $res);
f5eb281a 3225
dab36e1e 3226 shift @lines if $lines[0] !~ m/^unknown command/; # skip echo
f5eb281a 3227
dab36e1e
DM
3228 $res = join("\n", @lines);
3229 $res .= "\n";
3230 }
3231 };
3232
3233 my $err = $@;
3234
3235 if ($err) {
3236 syslog("err", "VM $vmid monitor command failed - $err");
3237 die $err;
3238 }
f5eb281a 3239
dab36e1e
DM
3240 return $res;
3241}
3242
c1175c92
AD
3243sub qemu_block_resize {
3244 my ($vmid, $deviceid, $storecfg, $volid, $size) = @_;
3245
ed221350 3246 my $running = check_running($vmid);
c1175c92
AD
3247
3248 return if !PVE::Storage::volume_resize($storecfg, $volid, $size, $running);
3249
3250 return if !$running;
3251
3252 vm_mon_cmd($vmid, "block_resize", device => $deviceid, size => int($size));
3253
3254}
3255
1ab0057c
AD
3256sub qemu_volume_snapshot {
3257 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
3258
ed221350 3259 my $running = check_running($vmid);
1ab0057c
AD
3260
3261 return if !PVE::Storage::volume_snapshot($storecfg, $volid, $snap, $running);
3262
3263 return if !$running;
3264
3265 vm_mon_cmd($vmid, "snapshot-drive", device => $deviceid, name => $snap);
3266
3267}
3268
fc46aff9
AD
3269sub qemu_volume_snapshot_delete {
3270 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
3271
ed221350 3272 my $running = check_running($vmid);
fc46aff9
AD
3273
3274 return if !PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
3275
3276 return if !$running;
3277
18bfb361 3278 vm_mon_cmd($vmid, "delete-drive-snapshot", device => $deviceid, name => $snap);
fc46aff9
AD
3279}
3280
3d5149c9
AD
3281sub qga_freezefs {
3282 my ($vmid) = @_;
3283
3284 #need to impplement call to qemu-ga
3285}
3286
e8f3f18e
AD
3287sub qga_unfreezefs {
3288 my ($vmid) = @_;
3289
3290 #need to impplement call to qemu-ga
3291}
3292
264e519f
DM
3293sub set_migration_caps {
3294 my ($vmid) = @_;
a89fded1 3295
8b8345f3 3296 my $cap_ref = [];
a89fded1
AD
3297
3298 my $enabled_cap = {
8b8345f3
DM
3299 "auto-converge" => 1,
3300 "xbzrle" => 0,
3301 "x-rdma-pin-all" => 0,
3302 "zero-blocks" => 0,
a89fded1
AD
3303 };
3304
8b8345f3 3305 my $supported_capabilities = vm_mon_cmd_nocheck($vmid, "query-migrate-capabilities");
a89fded1 3306
8b8345f3 3307 for my $supported_capability (@$supported_capabilities) {
b463a3ce
SP
3308 push @$cap_ref, {
3309 capability => $supported_capability->{capability},
22430fa2
DM
3310 state => $enabled_cap->{$supported_capability->{capability}} ? JSON::true : JSON::false,
3311 };
a89fded1
AD
3312 }
3313
8b8345f3
DM
3314 vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => $cap_ref);
3315}
a89fded1 3316
1e3baf05 3317sub vm_start {
1d794448 3318 my ($storecfg, $vmid, $statefile, $skiplock, $migratedfrom, $paused, $forcemachine, $spice_ticket) = @_;
1e3baf05 3319
6b64503e 3320 lock_config($vmid, sub {
7e8dcf2c 3321 my $conf = load_config($vmid, $migratedfrom);
1e3baf05 3322
8b43bc11 3323 die "you can't start a vm if it's a template\n" if is_template($conf);
3dcb98d5 3324
6b64503e 3325 check_lock($conf) if !$skiplock;
1e3baf05 3326
7e8dcf2c 3327 die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
1e3baf05 3328
6c47d546
DM
3329 my $defaults = load_defaults();
3330
3331 # set environment variable useful inside network script
3332 $ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
3333
1d794448 3334 my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
6c47d546 3335
1e3baf05 3336 my $migrate_port = 0;
5bc1e039 3337 my $migrate_uri;
1e3baf05
DM
3338 if ($statefile) {
3339 if ($statefile eq 'tcp') {
5bc1e039
SP
3340 my $localip = "localhost";
3341 my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
3342 if ($datacenterconf->{migration_unsecure}) {
3343 my $nodename = PVE::INotify::nodename();
3344 $localip = PVE::Cluster::remote_node_ip($nodename, 1);
3345 }
f9a971e0 3346 $migrate_port = PVE::Tools::next_migrate_port();
5bc1e039 3347 $migrate_uri = "tcp:${localip}:${migrate_port}";
6c47d546
DM
3348 push @$cmd, '-incoming', $migrate_uri;
3349 push @$cmd, '-S';
1e3baf05 3350 } else {
6c47d546 3351 push @$cmd, '-loadstate', $statefile;
1e3baf05 3352 }
91bd6c90
DM
3353 } elsif ($paused) {
3354 push @$cmd, '-S';
1e3baf05
DM
3355 }
3356
1e3baf05 3357 # host pci devices
040b06b7
DA
3358 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
3359 my $d = parse_hostpci($conf->{"hostpci$i"});
3360 next if !$d;
b1f72af6
AD
3361 my $pcidevices = $d->{pciid};
3362 foreach my $pcidevice (@$pcidevices) {
3363 my $pciid = $pcidevice->{id}.".".$pcidevice->{function};
000fc0a2 3364
b1f72af6
AD
3365 my $info = pci_device_info("0000:$pciid");
3366 die "IOMMU not present\n" if !check_iommu_support();
3367 die "no pci device info for device '$pciid'\n" if !$info;
000fc0a2 3368
b1f72af6
AD
3369 if ($d->{driver} && $d->{driver} eq "vfio") {
3370 die "can't unbind/bind pci group to vfio '$pciid'\n" if !pci_dev_group_bind_to_vfio($pciid);
3371 } else {
3372 die "can't unbind/bind to stub pci device '$pciid'\n" if !pci_dev_bind_to_stub($info);
3373 }
3374
8f3e88af 3375 die "can't reset pci device '$pciid'\n" if $info->{has_fl_reset} and !pci_dev_reset($info);
b1f72af6 3376 }
040b06b7 3377 }
1e3baf05
DM
3378
3379 PVE::Storage::activate_volumes($storecfg, $vollist);
3380
585b6e28
DM
3381 eval { run_command($cmd, timeout => $statefile ? undef : 30,
3382 umask => 0077); };
1e3baf05 3383 my $err = $@;
ff1a2432 3384 die "start failed: $err" if $err;
1e3baf05 3385
5bc1e039 3386 print "migration listens on $migrate_uri\n" if $migrate_uri;
afdb31d5 3387
8c609afd 3388 if ($statefile && $statefile ne 'tcp') {
95381ce0 3389 eval { vm_mon_cmd_nocheck($vmid, "cont"); };
8c609afd 3390 warn $@ if $@;
62de2cbd
DM
3391 }
3392
1d794448 3393 if ($migratedfrom) {
a89fded1
AD
3394
3395 eval {
3396 PVE::QemuServer::set_migration_caps($vmid);
3397 };
1d794448 3398 warn $@ if $@;
a89fded1 3399
1d794448
DM
3400 if ($spice_port) {
3401 print "spice listens on port $spice_port\n";
3402 if ($spice_ticket) {
3403 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "set_password", protocol => 'spice', password => $spice_ticket);
3404 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "expire_password", protocol => 'spice', time => "+30");
95a4b4a9
AD
3405 }
3406 }
3407
1d794448 3408 } else {
4ec05c4c 3409
15b1fc93 3410 if (!$statefile && (!defined($conf->{balloon}) || $conf->{balloon})) {
be190583 3411 vm_mon_cmd_nocheck($vmid, "balloon", value => $conf->{balloon}*1024*1024)
4ec05c4c 3412 if $conf->{balloon};
be190583
DM
3413 vm_mon_cmd_nocheck($vmid, 'qom-set',
3414 path => "machine/peripheral/balloon0",
3415 property => "guest-stats-polling-interval",
4ec05c4c
AD
3416 value => 2);
3417 }
e18b0b99 3418 }
1e3baf05
DM
3419 });
3420}
3421
0eedc444
AD
3422sub vm_mon_cmd {
3423 my ($vmid, $execute, %params) = @_;
3424
26f11676
DM
3425 my $cmd = { execute => $execute, arguments => \%params };
3426 vm_qmp_command($vmid, $cmd);
0eedc444
AD
3427}
3428
3429sub vm_mon_cmd_nocheck {
3430 my ($vmid, $execute, %params) = @_;
3431
26f11676
DM
3432 my $cmd = { execute => $execute, arguments => \%params };
3433 vm_qmp_command($vmid, $cmd, 1);
0eedc444
AD
3434}
3435
c971c4f2 3436sub vm_qmp_command {
d967756b 3437 my ($vmid, $cmd, $nocheck) = @_;
97d62eb7 3438
c971c4f2 3439 my $res;
26f11676 3440
14db5366
DM
3441 my $timeout;
3442 if ($cmd->{arguments} && $cmd->{arguments}->{timeout}) {
3443 $timeout = $cmd->{arguments}->{timeout};
3444 delete $cmd->{arguments}->{timeout};
3445 }
be190583 3446
c971c4f2
AD
3447 eval {
3448 die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
ed221350 3449 my $sname = qmp_socket($vmid);
f5eb281a 3450 if (-e $sname) {
dab36e1e
DM
3451 my $qmpclient = PVE::QMPClient->new();
3452
14db5366 3453 $res = $qmpclient->cmd($vmid, $cmd, $timeout);
dab36e1e
DM
3454 } elsif (-e "${var_run_tmpdir}/$vmid.mon") {
3455 die "can't execute complex command on old monitor - stop/start your vm to fix the problem\n"
3456 if scalar(%{$cmd->{arguments}});
3457 vm_monitor_command($vmid, $cmd->{execute}, $nocheck);
3458 } else {
3459 die "unable to open monitor socket\n";
3460 }
c971c4f2 3461 };
26f11676 3462 if (my $err = $@) {
c971c4f2
AD
3463 syslog("err", "VM $vmid qmp command failed - $err");
3464 die $err;
3465 }
3466
3467 return $res;
3468}
3469
9df5cbcc
DM
3470sub vm_human_monitor_command {
3471 my ($vmid, $cmdline) = @_;
3472
3473 my $res;
3474
f5eb281a 3475 my $cmd = {
9df5cbcc
DM
3476 execute => 'human-monitor-command',
3477 arguments => { 'command-line' => $cmdline},
3478 };
3479
3480 return vm_qmp_command($vmid, $cmd);
3481}
3482
1e3baf05
DM
3483sub vm_commandline {
3484 my ($storecfg, $vmid) = @_;
3485
6b64503e 3486 my $conf = load_config($vmid);
1e3baf05
DM
3487
3488 my $defaults = load_defaults();
3489
6b64503e 3490 my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults);
1e3baf05 3491
6b64503e 3492 return join(' ', @$cmd);
1e3baf05
DM
3493}
3494
3495sub vm_reset {
3496 my ($vmid, $skiplock) = @_;
3497
6b64503e 3498 lock_config($vmid, sub {
1e3baf05 3499
6b64503e 3500 my $conf = load_config($vmid);
1e3baf05 3501
6b64503e 3502 check_lock($conf) if !$skiplock;
1e3baf05 3503
816e2c4a 3504 vm_mon_cmd($vmid, "system_reset");
ff1a2432
DM
3505 });
3506}
3507
3508sub get_vm_volumes {
3509 my ($conf) = @_;
1e3baf05 3510
ff1a2432 3511 my $vollist = [];
d5769dc2
DM
3512 foreach_volid($conf, sub {
3513 my ($volid, $is_cdrom) = @_;
ff1a2432 3514
d5769dc2 3515 return if $volid =~ m|^/|;
ff1a2432 3516
d5769dc2
DM
3517 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
3518 return if !$sid;
ff1a2432
DM
3519
3520 push @$vollist, $volid;
1e3baf05 3521 });
ff1a2432
DM
3522
3523 return $vollist;
3524}
3525
3526sub vm_stop_cleanup {
254575e9 3527 my ($storecfg, $vmid, $conf, $keepActive) = @_;
ff1a2432 3528
745fed70
DM
3529 eval {
3530 fairsched_rmnod($vmid); # try to destroy group
ff1a2432 3531
254575e9
DM
3532 if (!$keepActive) {
3533 my $vollist = get_vm_volumes($conf);
3534 PVE::Storage::deactivate_volumes($storecfg, $vollist);
3535 }
961bfcb2 3536
ab6a046f 3537 foreach my $ext (qw(mon qmp pid vnc qga)) {
961bfcb2
DM
3538 unlink "/var/run/qemu-server/${vmid}.$ext";
3539 }
745fed70
DM
3540 };
3541 warn $@ if $@; # avoid errors - just warn
1e3baf05
DM
3542}
3543
e6c3b671 3544# Note: use $nockeck to skip tests if VM configuration file exists.
254575e9
DM
3545# We need that when migration VMs to other nodes (files already moved)
3546# Note: we set $keepActive in vzdump stop mode - volumes need to stay active
1e3baf05 3547sub vm_stop {
af30308f 3548 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
9269013a 3549
9269013a 3550 $force = 1 if !defined($force) && !$shutdown;
1e3baf05 3551
af30308f
DM
3552 if ($migratedfrom){
3553 my $pid = check_running($vmid, $nocheck, $migratedfrom);
3554 kill 15, $pid if $pid;
3555 my $conf = load_config($vmid, $migratedfrom);
3556 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive);
3557 return;
3558 }
3559
e6c3b671 3560 lock_config($vmid, sub {
1e3baf05 3561
e6c3b671 3562 my $pid = check_running($vmid, $nocheck);
ff1a2432 3563 return if !$pid;
1e3baf05 3564
ff1a2432 3565 my $conf;
e6c3b671 3566 if (!$nocheck) {
ff1a2432 3567 $conf = load_config($vmid);
e6c3b671 3568 check_lock($conf) if !$skiplock;
7f4a5b5a
DM
3569 if (!defined($timeout) && $shutdown && $conf->{startup}) {
3570 my $opts = parse_startup($conf->{startup});
3571 $timeout = $opts->{down} if $opts->{down};
3572 }
e6c3b671 3573 }
19672434 3574
7f4a5b5a
DM
3575 $timeout = 60 if !defined($timeout);
3576
9269013a
DM
3577 eval {
3578 if ($shutdown) {
988903ca 3579 $nocheck ? vm_mon_cmd_nocheck($vmid, "system_powerdown") : vm_mon_cmd($vmid, "system_powerdown");
bcb7c9cf 3580
9269013a 3581 } else {
988903ca 3582 $nocheck ? vm_mon_cmd_nocheck($vmid, "quit") : vm_mon_cmd($vmid, "quit");
afdb31d5 3583 }
9269013a 3584 };
1e3baf05
DM
3585 my $err = $@;
3586
3587 if (!$err) {
1e3baf05 3588 my $count = 0;
e6c3b671 3589 while (($count < $timeout) && check_running($vmid, $nocheck)) {
1e3baf05
DM
3590 $count++;
3591 sleep 1;
3592 }
3593
3594 if ($count >= $timeout) {
9269013a
DM
3595 if ($force) {
3596 warn "VM still running - terminating now with SIGTERM\n";
3597 kill 15, $pid;
3598 } else {
3599 die "VM quit/powerdown failed - got timeout\n";
3600 }
3601 } else {
254575e9 3602 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive) if $conf;
9269013a 3603 return;
1e3baf05
DM
3604 }
3605 } else {
9269013a
DM
3606 if ($force) {
3607 warn "VM quit/powerdown failed - terminating now with SIGTERM\n";
3608 kill 15, $pid;
3609 } else {
afdb31d5 3610 die "VM quit/powerdown failed\n";
9269013a 3611 }
1e3baf05
DM
3612 }
3613
3614 # wait again
ff1a2432 3615 $timeout = 10;
1e3baf05
DM
3616
3617 my $count = 0;
e6c3b671 3618 while (($count < $timeout) && check_running($vmid, $nocheck)) {
1e3baf05
DM
3619 $count++;
3620 sleep 1;
3621 }
3622
3623 if ($count >= $timeout) {
ff1a2432 3624 warn "VM still running - terminating now with SIGKILL\n";
1e3baf05 3625 kill 9, $pid;
ff1a2432 3626 sleep 1;
1e3baf05
DM
3627 }
3628
254575e9 3629 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive) if $conf;
ff1a2432 3630 });
1e3baf05
DM
3631}
3632
3633sub vm_suspend {
3634 my ($vmid, $skiplock) = @_;
3635
6b64503e 3636 lock_config($vmid, sub {
1e3baf05 3637
6b64503e 3638 my $conf = load_config($vmid);
1e3baf05 3639
051347aa 3640 check_lock($conf) if !($skiplock || ($conf->{lock} && $conf->{lock} eq 'backup'));
bcb7c9cf 3641
f77f91f3 3642 vm_mon_cmd($vmid, "stop");
1e3baf05
DM
3643 });
3644}
3645
3646sub vm_resume {
3647 my ($vmid, $skiplock) = @_;
3648
6b64503e 3649 lock_config($vmid, sub {
1e3baf05 3650
6b64503e 3651 my $conf = load_config($vmid);
1e3baf05 3652
051347aa 3653 check_lock($conf) if !($skiplock || ($conf->{lock} && $conf->{lock} eq 'backup'));
1e3baf05 3654
12060fe8 3655 vm_mon_cmd($vmid, "cont");
1e3baf05
DM
3656 });
3657}
3658
5fdbe4f0
DM
3659sub vm_sendkey {
3660 my ($vmid, $skiplock, $key) = @_;
1e3baf05 3661
6b64503e 3662 lock_config($vmid, sub {
1e3baf05 3663
6b64503e 3664 my $conf = load_config($vmid);
f5eb281a 3665
7b7c6d1b
DM
3666 # there is no qmp command, so we use the human monitor command
3667 vm_human_monitor_command($vmid, "sendkey $key");
1e3baf05
DM
3668 });
3669}
3670
3671sub vm_destroy {
3672 my ($storecfg, $vmid, $skiplock) = @_;
3673
6b64503e 3674 lock_config($vmid, sub {
1e3baf05 3675
6b64503e 3676 my $conf = load_config($vmid);
1e3baf05 3677
6b64503e 3678 check_lock($conf) if !$skiplock;
1e3baf05 3679
ff1a2432
DM
3680 if (!check_running($vmid)) {
3681 fairsched_rmnod($vmid); # try to destroy group
3682 destroy_vm($storecfg, $vmid);
3683 } else {
3684 die "VM $vmid is running - destroy failed\n";
1e3baf05
DM
3685 }
3686 });
3687}
3688
1e3baf05
DM
3689# pci helpers
3690
3691sub file_write {
3692 my ($filename, $buf) = @_;
3693
6b64503e 3694 my $fh = IO::File->new($filename, "w");
1e3baf05
DM
3695 return undef if !$fh;
3696
3697 my $res = print $fh $buf;
3698
3699 $fh->close();
3700
3701 return $res;
3702}
3703
3704sub pci_device_info {
3705 my ($name) = @_;
3706
3707 my $res;
3708
3709 return undef if $name !~ m/^([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])$/;
3710 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
3711
3712 my $irq = file_read_firstline("$pcisysfs/devices/$name/irq");
3713 return undef if !defined($irq) || $irq !~ m/^\d+$/;
3714
3715 my $vendor = file_read_firstline("$pcisysfs/devices/$name/vendor");
3716 return undef if !defined($vendor) || $vendor !~ s/^0x//;
3717
3718 my $product = file_read_firstline("$pcisysfs/devices/$name/device");
3719 return undef if !defined($product) || $product !~ s/^0x//;
3720
3721 $res = {
3722 name => $name,
3723 vendor => $vendor,
3724 product => $product,
3725 domain => $domain,
3726 bus => $bus,
3727 slot => $slot,
3728 func => $func,
3729 irq => $irq,
3730 has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0,
3731 };
3732
3733 return $res;
3734}
3735
3736sub pci_dev_reset {
3737 my ($dev) = @_;
3738
3739 my $name = $dev->{name};
3740
3741 my $fn = "$pcisysfs/devices/$name/reset";
3742
6b64503e 3743 return file_write($fn, "1");
1e3baf05
DM
3744}
3745
3746sub pci_dev_bind_to_stub {
3747 my ($dev) = @_;
3748
3749 my $name = $dev->{name};
3750
3751 my $testdir = "$pcisysfs/drivers/pci-stub/$name";
3752 return 1 if -d $testdir;
3753
3754 my $data = "$dev->{vendor} $dev->{product}";
6b64503e 3755 return undef if !file_write("$pcisysfs/drivers/pci-stub/new_id", $data);
1e3baf05
DM
3756
3757 my $fn = "$pcisysfs/devices/$name/driver/unbind";
6b64503e 3758 if (!file_write($fn, $name)) {
1e3baf05
DM
3759 return undef if -f $fn;
3760 }
3761
3762 $fn = "$pcisysfs/drivers/pci-stub/bind";
3763 if (! -d $testdir) {
6b64503e 3764 return undef if !file_write($fn, $name);
1e3baf05
DM
3765 }
3766
3767 return -d $testdir;
3768}
3769
000fc0a2
SP
3770sub pci_dev_bind_to_vfio {
3771 my ($dev) = @_;
3772
3773 my $name = $dev->{name};
3774
3775 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
3776
3777 if (!-d $vfio_basedir) {
3778 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
3779 }
3780 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
3781
3782 my $testdir = "$vfio_basedir/$name";
3783 return 1 if -d $testdir;
3784
3785 my $data = "$dev->{vendor} $dev->{product}";
3786 return undef if !file_write("$vfio_basedir/new_id", $data);
3787
3788 my $fn = "$pcisysfs/devices/$name/driver/unbind";
3789 if (!file_write($fn, $name)) {
3790 return undef if -f $fn;
3791 }
3792
3793 $fn = "$vfio_basedir/bind";
3794 if (! -d $testdir) {
3795 return undef if !file_write($fn, $name);
3796 }
3797
3798 return -d $testdir;
3799}
3800
3801sub pci_dev_group_bind_to_vfio {
3802 my ($pciid) = @_;
3803
3804 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
3805
3806 if (!-d $vfio_basedir) {
3807 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
3808 }
3809 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
3810
3811 # get IOMMU group devices
3812 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
3813 my @devs = grep /^0000:/, readdir($D);
3814 closedir($D);
3815
3816 foreach my $pciid (@devs) {
3817 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
3818 my $info = pci_device_info($1);
3819 pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
3820 }
3821
3822 return 1;
3823}
3824
afdb31d5 3825sub print_pci_addr {
5bdcf937 3826 my ($id, $bridges) = @_;
6b64503e 3827
72a063e4 3828 my $res = '';
6b64503e 3829 my $devices = {
24f0d39a 3830 piix3 => { bus => 0, addr => 1 },
e5f7f8ed 3831 #addr2 : first videocard
13b5a753 3832 balloon0 => { bus => 0, addr => 3 },
0a40e8ea 3833 watchdog => { bus => 0, addr => 4 },
cdd20088
AD
3834 scsihw0 => { bus => 0, addr => 5 },
3835 scsihw1 => { bus => 0, addr => 6 },
26ee04b6 3836 ahci0 => { bus => 0, addr => 7 },
ab6a046f 3837 qga0 => { bus => 0, addr => 8 },
1011b570 3838 spice => { bus => 0, addr => 9 },
6b64503e
DM
3839 virtio0 => { bus => 0, addr => 10 },
3840 virtio1 => { bus => 0, addr => 11 },
3841 virtio2 => { bus => 0, addr => 12 },
3842 virtio3 => { bus => 0, addr => 13 },
3843 virtio4 => { bus => 0, addr => 14 },
3844 virtio5 => { bus => 0, addr => 15 },
b78ebef7
DA
3845 hostpci0 => { bus => 0, addr => 16 },
3846 hostpci1 => { bus => 0, addr => 17 },
f290f8d9
DA
3847 net0 => { bus => 0, addr => 18 },
3848 net1 => { bus => 0, addr => 19 },
3849 net2 => { bus => 0, addr => 20 },
3850 net3 => { bus => 0, addr => 21 },
3851 net4 => { bus => 0, addr => 22 },
3852 net5 => { bus => 0, addr => 23 },
2fa3151e
AD
3853 vga1 => { bus => 0, addr => 24 },
3854 vga2 => { bus => 0, addr => 25 },
3855 vga3 => { bus => 0, addr => 26 },
e5f7f8ed 3856 #addr29 : usb-host (pve-usb.cfg)
5bdcf937
AD
3857 'pci.1' => { bus => 0, addr => 30 },
3858 'pci.2' => { bus => 0, addr => 31 },
3859 'net6' => { bus => 1, addr => 1 },
3860 'net7' => { bus => 1, addr => 2 },
3861 'net8' => { bus => 1, addr => 3 },
3862 'net9' => { bus => 1, addr => 4 },
3863 'net10' => { bus => 1, addr => 5 },
3864 'net11' => { bus => 1, addr => 6 },
3865 'net12' => { bus => 1, addr => 7 },
3866 'net13' => { bus => 1, addr => 8 },
3867 'net14' => { bus => 1, addr => 9 },
3868 'net15' => { bus => 1, addr => 10 },
3869 'net16' => { bus => 1, addr => 11 },
3870 'net17' => { bus => 1, addr => 12 },
3871 'net18' => { bus => 1, addr => 13 },
3872 'net19' => { bus => 1, addr => 14 },
3873 'net20' => { bus => 1, addr => 15 },
3874 'net21' => { bus => 1, addr => 16 },
3875 'net22' => { bus => 1, addr => 17 },
3876 'net23' => { bus => 1, addr => 18 },
3877 'net24' => { bus => 1, addr => 19 },
3878 'net25' => { bus => 1, addr => 20 },
3879 'net26' => { bus => 1, addr => 21 },
3880 'net27' => { bus => 1, addr => 22 },
3881 'net28' => { bus => 1, addr => 23 },
3882 'net29' => { bus => 1, addr => 24 },
3883 'net30' => { bus => 1, addr => 25 },
3884 'net31' => { bus => 1, addr => 26 },
3885 'virtio6' => { bus => 2, addr => 1 },
3886 'virtio7' => { bus => 2, addr => 2 },
3887 'virtio8' => { bus => 2, addr => 3 },
3888 'virtio9' => { bus => 2, addr => 4 },
3889 'virtio10' => { bus => 2, addr => 5 },
3890 'virtio11' => { bus => 2, addr => 6 },
3891 'virtio12' => { bus => 2, addr => 7 },
3892 'virtio13' => { bus => 2, addr => 8 },
3893 'virtio14' => { bus => 2, addr => 9 },
3894 'virtio15' => { bus => 2, addr => 10 },
6b64503e
DM
3895 };
3896
3897 if (defined($devices->{$id}->{bus}) && defined($devices->{$id}->{addr})) {
72a063e4 3898 my $addr = sprintf("0x%x", $devices->{$id}->{addr});
5bdcf937
AD
3899 my $bus = $devices->{$id}->{bus};
3900 $res = ",bus=pci.$bus,addr=$addr";
98627641 3901 $bridges->{$bus} = 1 if $bridges;
72a063e4
DA
3902 }
3903 return $res;
3904
3905}
3906
2e3b7e2a
AD
3907sub print_pcie_addr {
3908 my ($id) = @_;
3909
3910 my $res = '';
3911 my $devices = {
3912 hostpci0 => { bus => "ich9-pcie-port-1", addr => 0 },
3913 hostpci1 => { bus => "ich9-pcie-port-2", addr => 0 },
3914 hostpci2 => { bus => "ich9-pcie-port-3", addr => 0 },
3915 hostpci3 => { bus => "ich9-pcie-port-4", addr => 0 },
3916 };
3917
3918 if (defined($devices->{$id}->{bus}) && defined($devices->{$id}->{addr})) {
3919 my $addr = sprintf("0x%x", $devices->{$id}->{addr});
3920 my $bus = $devices->{$id}->{bus};
3921 $res = ",bus=$bus,addr=$addr";
3922 }
3923 return $res;
3924
3925}
3926
3e16d5fc
DM
3927# vzdump restore implementaion
3928
ed221350 3929sub tar_archive_read_firstfile {
3e16d5fc 3930 my $archive = shift;
afdb31d5 3931
3e16d5fc
DM
3932 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
3933
3934 # try to detect archive type first
3935 my $pid = open (TMP, "tar tf '$archive'|") ||
3936 die "unable to open file '$archive'\n";
3937 my $firstfile = <TMP>;
3938 kill 15, $pid;
3939 close TMP;
3940
3941 die "ERROR: archive contaions no data\n" if !$firstfile;
3942 chomp $firstfile;
3943
3944 return $firstfile;
3945}
3946
ed221350
DM
3947sub tar_restore_cleanup {
3948 my ($storecfg, $statfile) = @_;
3e16d5fc
DM
3949
3950 print STDERR "starting cleanup\n";
3951
3952 if (my $fd = IO::File->new($statfile, "r")) {
3953 while (defined(my $line = <$fd>)) {
3954 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
3955 my $volid = $2;
3956 eval {
3957 if ($volid =~ m|^/|) {
3958 unlink $volid || die 'unlink failed\n';
3959 } else {
ed221350 3960 PVE::Storage::vdisk_free($storecfg, $volid);
3e16d5fc 3961 }
afdb31d5 3962 print STDERR "temporary volume '$volid' sucessfuly removed\n";
3e16d5fc
DM
3963 };
3964 print STDERR "unable to cleanup '$volid' - $@" if $@;
3965 } else {
3966 print STDERR "unable to parse line in statfile - $line";
afdb31d5 3967 }
3e16d5fc
DM
3968 }
3969 $fd->close();
3970 }
3971}
3972
3973sub restore_archive {
a0d1b1a2 3974 my ($archive, $vmid, $user, $opts) = @_;
3e16d5fc 3975
91bd6c90
DM
3976 my $format = $opts->{format};
3977 my $comp;
3978
3979 if ($archive =~ m/\.tgz$/ || $archive =~ m/\.tar\.gz$/) {
3980 $format = 'tar' if !$format;
3981 $comp = 'gzip';
3982 } elsif ($archive =~ m/\.tar$/) {
3983 $format = 'tar' if !$format;
3984 } elsif ($archive =~ m/.tar.lzo$/) {
3985 $format = 'tar' if !$format;
3986 $comp = 'lzop';
3987 } elsif ($archive =~ m/\.vma$/) {
3988 $format = 'vma' if !$format;
3989 } elsif ($archive =~ m/\.vma\.gz$/) {
3990 $format = 'vma' if !$format;
3991 $comp = 'gzip';
3992 } elsif ($archive =~ m/\.vma\.lzo$/) {
3993 $format = 'vma' if !$format;
3994 $comp = 'lzop';
3995 } else {
3996 $format = 'vma' if !$format; # default
3997 }
3998
3999 # try to detect archive format
4000 if ($format eq 'tar') {
4001 return restore_tar_archive($archive, $vmid, $user, $opts);
4002 } else {
4003 return restore_vma_archive($archive, $vmid, $user, $opts, $comp);
4004 }
4005}
4006
4007sub restore_update_config_line {
4008 my ($outfd, $cookie, $vmid, $map, $line, $unique) = @_;
4009
4010 return if $line =~ m/^\#qmdump\#/;
4011 return if $line =~ m/^\#vzdump\#/;
4012 return if $line =~ m/^lock:/;
4013 return if $line =~ m/^unused\d+:/;
4014 return if $line =~ m/^parent:/;
ca3e4fa4 4015 return if $line =~ m/^template:/; # restored VM is never a template
91bd6c90
DM
4016
4017 if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
4018 # try to convert old 1.X settings
4019 my ($id, $ind, $ethcfg) = ($1, $2, $3);
4020 foreach my $devconfig (PVE::Tools::split_list($ethcfg)) {
4021 my ($model, $macaddr) = split(/\=/, $devconfig);
4022 $macaddr = PVE::Tools::random_ether_addr() if !$macaddr || $unique;
4023 my $net = {
4024 model => $model,
4025 bridge => "vmbr$ind",
4026 macaddr => $macaddr,
4027 };
4028 my $netstr = print_net($net);
4029
4030 print $outfd "net$cookie->{netcount}: $netstr\n";
4031 $cookie->{netcount}++;
4032 }
4033 } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && $unique) {
4034 my ($id, $netstr) = ($1, $2);
4035 my $net = parse_net($netstr);
4036 $net->{macaddr} = PVE::Tools::random_ether_addr() if $net->{macaddr};
4037 $netstr = print_net($net);
4038 print $outfd "$id: $netstr\n";
4039 } elsif ($line =~ m/^((ide|scsi|virtio|sata)\d+):\s*(\S+)\s*$/) {
4040 my $virtdev = $1;
907ea891 4041 my $value = $3;
91bd6c90
DM
4042 if ($line =~ m/backup=no/) {
4043 print $outfd "#$line";
4044 } elsif ($virtdev && $map->{$virtdev}) {
ed221350 4045 my $di = parse_drive($virtdev, $value);
8fd57431 4046 delete $di->{format}; # format can change on restore
91bd6c90 4047 $di->{file} = $map->{$virtdev};
ed221350 4048 $value = print_drive($vmid, $di);
91bd6c90
DM
4049 print $outfd "$virtdev: $value\n";
4050 } else {
4051 print $outfd $line;
4052 }
4053 } else {
4054 print $outfd $line;
4055 }
4056}
4057
4058sub scan_volids {
4059 my ($cfg, $vmid) = @_;
4060
4061 my $info = PVE::Storage::vdisk_list($cfg, undef, $vmid);
4062
4063 my $volid_hash = {};
4064 foreach my $storeid (keys %$info) {
4065 foreach my $item (@{$info->{$storeid}}) {
4066 next if !($item->{volid} && $item->{size});
5996a936 4067 $item->{path} = PVE::Storage::path($cfg, $item->{volid});
91bd6c90
DM
4068 $volid_hash->{$item->{volid}} = $item;
4069 }
4070 }
4071
4072 return $volid_hash;
4073}
4074
a8e2f942
DM
4075sub get_used_paths {
4076 my ($vmid, $storecfg, $conf, $scan_snapshots, $skip_drive) = @_;
4077
4078 my $used_path = {};
4079
4080 my $scan_config = sub {
4081 my ($cref, $snapname) = @_;
4082
4083 foreach my $key (keys %$cref) {
4084 my $value = $cref->{$key};
4085 if (valid_drivename($key)) {
4086 next if $skip_drive && $key eq $skip_drive;
4087 my $drive = parse_drive($key, $value);
4088 next if !$drive || !$drive->{file} || drive_is_cdrom($drive);
4089 if ($drive->{file} =~ m!^/!) {
4090 $used_path->{$drive->{file}}++; # = 1;
4091 } else {
4092 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
4093 next if !$storeid;
4094 my $scfg = PVE::Storage::storage_config($storecfg, $storeid, 1);
4095 next if !$scfg;
4096 my $path = PVE::Storage::path($storecfg, $drive->{file}, $snapname);
4097 $used_path->{$path}++; # = 1;
4098 }
4099 }
4100 }
4101 };
4102
4103 &$scan_config($conf);
4104
4105 undef $skip_drive;
4106
4107 if ($scan_snapshots) {
4108 foreach my $snapname (keys %{$conf->{snapshots}}) {
4109 &$scan_config($conf->{snapshots}->{$snapname}, $snapname);
4110 }
4111 }
4112
4113 return $used_path;
4114}
4115
91bd6c90
DM
4116sub update_disksize {
4117 my ($vmid, $conf, $volid_hash) = @_;
be190583 4118
91bd6c90
DM
4119 my $changes;
4120
4121 my $used = {};
4122
5996a936
DM
4123 # Note: it is allowed to define multiple storages with same path (alias), so
4124 # we need to check both 'volid' and real 'path' (two different volid can point
4125 # to the same path).
4126
4127 my $usedpath = {};
be190583 4128
91bd6c90
DM
4129 # update size info
4130 foreach my $opt (keys %$conf) {
ed221350
DM
4131 if (valid_drivename($opt)) {
4132 my $drive = parse_drive($opt, $conf->{$opt});
91bd6c90
DM
4133 my $volid = $drive->{file};
4134 next if !$volid;
4135
4136 $used->{$volid} = 1;
be190583 4137 if ($volid_hash->{$volid} &&
5996a936
DM
4138 (my $path = $volid_hash->{$volid}->{path})) {
4139 $usedpath->{$path} = 1;
4140 }
91bd6c90 4141
ed221350 4142 next if drive_is_cdrom($drive);
91bd6c90
DM
4143 next if !$volid_hash->{$volid};
4144
4145 $drive->{size} = $volid_hash->{$volid}->{size};
7a907ce6
DM
4146 my $new = print_drive($vmid, $drive);
4147 if ($new ne $conf->{$opt}) {
4148 $changes = 1;
4149 $conf->{$opt} = $new;
4150 }
91bd6c90
DM
4151 }
4152 }
4153
5996a936
DM
4154 # remove 'unusedX' entry if volume is used
4155 foreach my $opt (keys %$conf) {
4156 next if $opt !~ m/^unused\d+$/;
4157 my $volid = $conf->{$opt};
4158 my $path = $volid_hash->{$volid}->{path} if $volid_hash->{$volid};
be190583 4159 if ($used->{$volid} || ($path && $usedpath->{$path})) {
5996a936
DM
4160 $changes = 1;
4161 delete $conf->{$opt};
4162 }
4163 }
4164
91bd6c90
DM
4165 foreach my $volid (sort keys %$volid_hash) {
4166 next if $volid =~ m/vm-$vmid-state-/;
4167 next if $used->{$volid};
5996a936
DM
4168 my $path = $volid_hash->{$volid}->{path};
4169 next if !$path; # just to be sure
4170 next if $usedpath->{$path};
91bd6c90 4171 $changes = 1;
ed221350 4172 add_unused_volume($conf, $volid);
05937a14 4173 $usedpath->{$path} = 1; # avoid to add more than once (aliases)
91bd6c90
DM
4174 }
4175
4176 return $changes;
4177}
4178
4179sub rescan {
4180 my ($vmid, $nolock) = @_;
4181
4182 my $cfg = PVE::Cluster::cfs_read_file("storage.cfg");
4183
4184 my $volid_hash = scan_volids($cfg, $vmid);
4185
4186 my $updatefn = sub {
4187 my ($vmid) = @_;
4188
ed221350 4189 my $conf = load_config($vmid);
be190583 4190
ed221350 4191 check_lock($conf);
91bd6c90 4192
03da3f0d
DM
4193 my $vm_volids = {};
4194 foreach my $volid (keys %$volid_hash) {
4195 my $info = $volid_hash->{$volid};
4196 $vm_volids->{$volid} = $info if $info->{vmid} && $info->{vmid} == $vmid;
4197 }
4198
4199 my $changes = update_disksize($vmid, $conf, $vm_volids);
91bd6c90 4200
ed221350 4201 update_config_nolock($vmid, $conf, 1) if $changes;
91bd6c90
DM
4202 };
4203
4204 if (defined($vmid)) {
4205 if ($nolock) {
4206 &$updatefn($vmid);
4207 } else {
ed221350 4208 lock_config($vmid, $updatefn, $vmid);
91bd6c90
DM
4209 }
4210 } else {
4211 my $vmlist = config_list();
4212 foreach my $vmid (keys %$vmlist) {
4213 if ($nolock) {
4214 &$updatefn($vmid);
4215 } else {
ed221350 4216 lock_config($vmid, $updatefn, $vmid);
be190583 4217 }
91bd6c90
DM
4218 }
4219 }
4220}
4221
4222sub restore_vma_archive {
4223 my ($archive, $vmid, $user, $opts, $comp) = @_;
4224
4225 my $input = $archive eq '-' ? "<&STDIN" : undef;
4226 my $readfrom = $archive;
4227
4228 my $uncomp = '';
4229 if ($comp) {
4230 $readfrom = '-';
4231 my $qarchive = PVE::Tools::shellquote($archive);
4232 if ($comp eq 'gzip') {
4233 $uncomp = "zcat $qarchive|";
4234 } elsif ($comp eq 'lzop') {
4235 $uncomp = "lzop -d -c $qarchive|";
4236 } else {
4237 die "unknown compression method '$comp'\n";
4238 }
be190583 4239
91bd6c90
DM
4240 }
4241
4242 my $tmpdir = "/var/tmp/vzdumptmp$$";
4243 rmtree $tmpdir;
4244
4245 # disable interrupts (always do cleanups)
4246 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub {
4247 warn "got interrupt - ignored\n";
4248 };
4249
4250 my $mapfifo = "/var/tmp/vzdumptmp$$.fifo";
4251 POSIX::mkfifo($mapfifo, 0600);
4252 my $fifofh;
4253
4254 my $openfifo = sub {
4255 open($fifofh, '>', $mapfifo) || die $!;
4256 };
4257
4258 my $cmd = "${uncomp}vma extract -v -r $mapfifo $readfrom $tmpdir";
4259
4260 my $oldtimeout;
4261 my $timeout = 5;
4262
4263 my $devinfo = {};
4264
4265 my $rpcenv = PVE::RPCEnvironment::get();
4266
ed221350 4267 my $conffile = config_file($vmid);
91bd6c90
DM
4268 my $tmpfn = "$conffile.$$.tmp";
4269
ed221350
DM
4270 # Note: $oldconf is undef if VM does not exists
4271 my $oldconf = PVE::Cluster::cfs_read_file(cfs_config_path($vmid));
4272
91bd6c90
DM
4273 my $print_devmap = sub {
4274 my $virtdev_hash = {};
4275
4276 my $cfgfn = "$tmpdir/qemu-server.conf";
4277
4278 # we can read the config - that is already extracted
4279 my $fh = IO::File->new($cfgfn, "r") ||
4280 "unable to read qemu-server.conf - $!\n";
4281
4282 while (defined(my $line = <$fh>)) {
4283 if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
4284 my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
4285 die "archive does not contain data for drive '$virtdev'\n"
4286 if !$devinfo->{$devname};
4287 if (defined($opts->{storage})) {
4288 $storeid = $opts->{storage} || 'local';
4289 } elsif (!$storeid) {
4290 $storeid = 'local';
4291 }
4292 $format = 'raw' if !$format;
4293 $devinfo->{$devname}->{devname} = $devname;
4294 $devinfo->{$devname}->{virtdev} = $virtdev;
4295 $devinfo->{$devname}->{format} = $format;
4296 $devinfo->{$devname}->{storeid} = $storeid;
4297
be190583 4298 # check permission on storage
91bd6c90
DM
4299 my $pool = $opts->{pool}; # todo: do we need that?
4300 if ($user ne 'root@pam') {
4301 $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace']);
4302 }
4303
4304 $virtdev_hash->{$virtdev} = $devinfo->{$devname};
4305 }
4306 }
4307
4308 foreach my $devname (keys %$devinfo) {
be190583
DM
4309 die "found no device mapping information for device '$devname'\n"
4310 if !$devinfo->{$devname}->{virtdev};
91bd6c90
DM
4311 }
4312
91bd6c90 4313 my $cfg = cfs_read_file('storage.cfg');
ed221350
DM
4314
4315 # create empty/temp config
be190583 4316 if ($oldconf) {
ed221350
DM
4317 PVE::Tools::file_set_contents($conffile, "memory: 128\n");
4318 foreach_drive($oldconf, sub {
4319 my ($ds, $drive) = @_;
4320
4321 return if drive_is_cdrom($drive);
4322
4323 my $volid = $drive->{file};
4324
4325 return if !$volid || $volid =~ m|^/|;
4326
4327 my ($path, $owner) = PVE::Storage::path($cfg, $volid);
4328 return if !$path || !$owner || ($owner != $vmid);
4329
4330 # Note: only delete disk we want to restore
4331 # other volumes will become unused
4332 if ($virtdev_hash->{$ds}) {
4333 PVE::Storage::vdisk_free($cfg, $volid);
4334 }
4335 });
4336 }
4337
4338 my $map = {};
91bd6c90
DM
4339 foreach my $virtdev (sort keys %$virtdev_hash) {
4340 my $d = $virtdev_hash->{$virtdev};
4341 my $alloc_size = int(($d->{size} + 1024 - 1)/1024);
4342 my $scfg = PVE::Storage::storage_config($cfg, $d->{storeid});
8fd57431
DM
4343
4344 # test if requested format is supported
4345 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($cfg, $d->{storeid});
4346 my $supported = grep { $_ eq $d->{format} } @$validFormats;
4347 $d->{format} = $defFormat if !$supported;
4348
91bd6c90
DM
4349 my $volid = PVE::Storage::vdisk_alloc($cfg, $d->{storeid}, $vmid,
4350 $d->{format}, undef, $alloc_size);
4351 print STDERR "new volume ID is '$volid'\n";
4352 $d->{volid} = $volid;
4353 my $path = PVE::Storage::path($cfg, $volid);
4354
4355 my $write_zeros = 1;
4356 # fixme: what other storages types initialize volumes with zero?
244f2577 4357 if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs' || $scfg->{type} eq 'glusterfs' ||
013d5275 4358 $scfg->{type} eq 'sheepdog' || $scfg->{type} eq 'rbd') {
91bd6c90
DM
4359 $write_zeros = 0;
4360 }
4361
4362 print $fifofh "${write_zeros}:$d->{devname}=$path\n";
4363
4364 print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros})\n";
4365 $map->{$virtdev} = $volid;
4366 }
4367
4368 $fh->seek(0, 0) || die "seek failed - $!\n";
4369
4370 my $outfd = new IO::File ($tmpfn, "w") ||
4371 die "unable to write config for VM $vmid\n";
4372
4373 my $cookie = { netcount => 0 };
4374 while (defined(my $line = <$fh>)) {
be190583 4375 restore_update_config_line($outfd, $cookie, $vmid, $map, $line, $opts->{unique});
91bd6c90
DM
4376 }
4377
4378 $fh->close();
4379 $outfd->close();
4380 };
4381
4382 eval {
4383 # enable interrupts
4384 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
4385 die "interrupted by signal\n";
4386 };
4387 local $SIG{ALRM} = sub { die "got timeout\n"; };
4388
4389 $oldtimeout = alarm($timeout);
4390
4391 my $parser = sub {
4392 my $line = shift;
4393
4394 print "$line\n";
4395
4396 if ($line =~ m/^DEV:\sdev_id=(\d+)\ssize:\s(\d+)\sdevname:\s(\S+)$/) {
4397 my ($dev_id, $size, $devname) = ($1, $2, $3);
4398 $devinfo->{$devname} = { size => $size, dev_id => $dev_id };
4399 } elsif ($line =~ m/^CTIME: /) {
46f58b5f 4400 # we correctly received the vma config, so we can disable
3cf90d7a
DM
4401 # the timeout now for disk allocation (set to 10 minutes, so
4402 # that we always timeout if something goes wrong)
4403 alarm(600);
91bd6c90
DM
4404 &$print_devmap();
4405 print $fifofh "done\n";
4406 my $tmp = $oldtimeout || 0;
4407 $oldtimeout = undef;
4408 alarm($tmp);
4409 close($fifofh);
4410 }
4411 };
be190583 4412
91bd6c90
DM
4413 print "restore vma archive: $cmd\n";
4414 run_command($cmd, input => $input, outfunc => $parser, afterfork => $openfifo);
4415 };
4416 my $err = $@;
4417
4418 alarm($oldtimeout) if $oldtimeout;
4419
4420 unlink $mapfifo;
4421
4422 if ($err) {
4423 rmtree $tmpdir;
4424 unlink $tmpfn;
4425
4426 my $cfg = cfs_read_file('storage.cfg');
4427 foreach my $devname (keys %$devinfo) {
4428 my $volid = $devinfo->{$devname}->{volid};
4429 next if !$volid;
4430 eval {
4431 if ($volid =~ m|^/|) {
4432 unlink $volid || die 'unlink failed\n';
4433 } else {
4434 PVE::Storage::vdisk_free($cfg, $volid);
4435 }
4436 print STDERR "temporary volume '$volid' sucessfuly removed\n";
4437 };
4438 print STDERR "unable to cleanup '$volid' - $@" if $@;
4439 }
4440 die $err;
4441 }
4442
4443 rmtree $tmpdir;
ed221350
DM
4444
4445 rename($tmpfn, $conffile) ||
91bd6c90
DM
4446 die "unable to commit configuration file '$conffile'\n";
4447
ed221350
DM
4448 PVE::Cluster::cfs_update(); # make sure we read new file
4449
91bd6c90
DM
4450 eval { rescan($vmid, 1); };
4451 warn $@ if $@;
4452}
4453
4454sub restore_tar_archive {
4455 my ($archive, $vmid, $user, $opts) = @_;
4456
9c502e26 4457 if ($archive ne '-') {
ed221350 4458 my $firstfile = tar_archive_read_firstfile($archive);
9c502e26
DM
4459 die "ERROR: file '$archive' dos not lock like a QemuServer vzdump backup\n"
4460 if $firstfile ne 'qemu-server.conf';
4461 }
3e16d5fc 4462
ed221350 4463 my $storecfg = cfs_read_file('storage.cfg');
ebb55558 4464
ed221350 4465 # destroy existing data - keep empty config
ebb55558
DM
4466 my $vmcfgfn = PVE::QemuServer::config_file($vmid);
4467 destroy_vm($storecfg, $vmid, 1) if -f $vmcfgfn;
ed221350 4468
3e16d5fc
DM
4469 my $tocmd = "/usr/lib/qemu-server/qmextract";
4470
2415a446 4471 $tocmd .= " --storage " . PVE::Tools::shellquote($opts->{storage}) if $opts->{storage};
a0d1b1a2 4472 $tocmd .= " --pool " . PVE::Tools::shellquote($opts->{pool}) if $opts->{pool};
3e16d5fc
DM
4473 $tocmd .= ' --prealloc' if $opts->{prealloc};
4474 $tocmd .= ' --info' if $opts->{info};
4475
a0d1b1a2 4476 # tar option "xf" does not autodetect compression when read from STDIN,
9c502e26 4477 # so we pipe to zcat
2415a446
DM
4478 my $cmd = "zcat -f|tar xf " . PVE::Tools::shellquote($archive) . " " .
4479 PVE::Tools::shellquote("--to-command=$tocmd");
3e16d5fc
DM
4480
4481 my $tmpdir = "/var/tmp/vzdumptmp$$";
4482 mkpath $tmpdir;
4483
4484 local $ENV{VZDUMP_TMPDIR} = $tmpdir;
4485 local $ENV{VZDUMP_VMID} = $vmid;
a0d1b1a2 4486 local $ENV{VZDUMP_USER} = $user;
3e16d5fc 4487
ed221350 4488 my $conffile = config_file($vmid);
3e16d5fc
DM
4489 my $tmpfn = "$conffile.$$.tmp";
4490
4491 # disable interrupts (always do cleanups)
4492 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub {
4493 print STDERR "got interrupt - ignored\n";
4494 };
4495
afdb31d5 4496 eval {
3e16d5fc
DM
4497 # enable interrupts
4498 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
4499 die "interrupted by signal\n";
4500 };
4501
9c502e26
DM
4502 if ($archive eq '-') {
4503 print "extracting archive from STDIN\n";
4504 run_command($cmd, input => "<&STDIN");
4505 } else {
4506 print "extracting archive '$archive'\n";
4507 run_command($cmd);
4508 }
3e16d5fc
DM
4509
4510 return if $opts->{info};
4511
4512 # read new mapping
4513 my $map = {};
4514 my $statfile = "$tmpdir/qmrestore.stat";
4515 if (my $fd = IO::File->new($statfile, "r")) {
4516 while (defined (my $line = <$fd>)) {
4517 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
4518 $map->{$1} = $2 if $1;
4519 } else {
4520 print STDERR "unable to parse line in statfile - $line\n";
4521 }
4522 }
4523 $fd->close();
4524 }
4525
4526 my $confsrc = "$tmpdir/qemu-server.conf";
4527
4528 my $srcfd = new IO::File($confsrc, "r") ||
4529 die "unable to open file '$confsrc'\n";
4530
4531 my $outfd = new IO::File ($tmpfn, "w") ||
4532 die "unable to write config for VM $vmid\n";
4533
91bd6c90 4534 my $cookie = { netcount => 0 };
3e16d5fc 4535 while (defined (my $line = <$srcfd>)) {
be190583 4536 restore_update_config_line($outfd, $cookie, $vmid, $map, $line, $opts->{unique});
3e16d5fc
DM
4537 }
4538
4539 $srcfd->close();
4540 $outfd->close();
4541 };
4542 my $err = $@;
4543
afdb31d5 4544 if ($err) {
3e16d5fc
DM
4545
4546 unlink $tmpfn;
4547
ed221350 4548 tar_restore_cleanup($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info};
afdb31d5 4549
3e16d5fc 4550 die $err;
afdb31d5 4551 }
3e16d5fc
DM
4552
4553 rmtree $tmpdir;
4554
4555 rename $tmpfn, $conffile ||
4556 die "unable to commit configuration file '$conffile'\n";
91bd6c90 4557
ed221350
DM
4558 PVE::Cluster::cfs_update(); # make sure we read new file
4559
91bd6c90
DM
4560 eval { rescan($vmid, 1); };
4561 warn $@ if $@;
3e16d5fc
DM
4562};
4563
0d18dcfc
DM
4564
4565# Internal snapshots
4566
4567# NOTE: Snapshot create/delete involves several non-atomic
4568# action, and can take a long time.
4569# So we try to avoid locking the file and use 'lock' variable
4570# inside the config file instead.
4571
ef59d1ca
DM
4572my $snapshot_copy_config = sub {
4573 my ($source, $dest) = @_;
4574
4575 foreach my $k (keys %$source) {
4576 next if $k eq 'snapshots';
982c7f12
DM
4577 next if $k eq 'snapstate';
4578 next if $k eq 'snaptime';
18bfb361 4579 next if $k eq 'vmstate';
ef59d1ca
DM
4580 next if $k eq 'lock';
4581 next if $k eq 'digest';
db7c26e5 4582 next if $k eq 'description';
ef59d1ca 4583 next if $k =~ m/^unused\d+$/;
be190583 4584
ef59d1ca
DM
4585 $dest->{$k} = $source->{$k};
4586 }
4587};
4588
4589my $snapshot_apply_config = sub {
4590 my ($conf, $snap) = @_;
4591
4592 # copy snapshot list
4593 my $newconf = {
4594 snapshots => $conf->{snapshots},
4595 };
4596
db7c26e5 4597 # keep description and list of unused disks
ef59d1ca 4598 foreach my $k (keys %$conf) {
db7c26e5 4599 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
ef59d1ca
DM
4600 $newconf->{$k} = $conf->{$k};
4601 }
4602
4603 &$snapshot_copy_config($snap, $newconf);
4604
4605 return $newconf;
4606};
4607
18bfb361
DM
4608sub foreach_writable_storage {
4609 my ($conf, $func) = @_;
4610
4611 my $sidhash = {};
4612
4613 foreach my $ds (keys %$conf) {
4614 next if !valid_drivename($ds);
4615
4616 my $drive = parse_drive($ds, $conf->{$ds});
4617 next if !$drive;
4618 next if drive_is_cdrom($drive);
4619
4620 my $volid = $drive->{file};
4621
4622 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
be190583 4623 $sidhash->{$sid} = $sid if $sid;
18bfb361
DM
4624 }
4625
4626 foreach my $sid (sort keys %$sidhash) {
4627 &$func($sid);
4628 }
4629}
4630
4631my $alloc_vmstate_volid = sub {
4632 my ($storecfg, $vmid, $conf, $snapname) = @_;
be190583 4633
18bfb361
DM
4634 # Note: we try to be smart when selecting a $target storage
4635
4636 my $target;
4637
4638 # search shared storage first
4639 foreach_writable_storage($conf, sub {
4640 my ($sid) = @_;
4641 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
4642 return if !$scfg->{shared};
4643
4644 $target = $sid if !$target || $scfg->{path}; # prefer file based storage
4645 });
4646
4647 if (!$target) {
4648 # now search local storage
4649 foreach_writable_storage($conf, sub {
4650 my ($sid) = @_;
4651 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
4652 return if $scfg->{shared};
4653
4654 $target = $sid if !$target || $scfg->{path}; # prefer file based storage;
4655 });
4656 }
4657
4658 $target = 'local' if !$target;
4659
fe6249f4
DM
4660 my $driver_state_size = 500; # assume 32MB is enough to safe all driver state;
4661 # we abort live save after $conf->{memory}, so we need at max twice that space
4662 my $size = $conf->{memory}*2 + $driver_state_size;
18bfb361
DM
4663
4664 my $name = "vm-$vmid-state-$snapname";
4665 my $scfg = PVE::Storage::storage_config($storecfg, $target);
4666 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
4667 my $volid = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
4668
4669 return $volid;
4670};
4671
0d18dcfc 4672my $snapshot_prepare = sub {
18bfb361 4673 my ($vmid, $snapname, $save_vmstate, $comment) = @_;
22c377f0
DM
4674
4675 my $snap;
0d18dcfc
DM
4676
4677 my $updatefn = sub {
4678
4679 my $conf = load_config($vmid);
4680
be190583 4681 die "you can't take a snapshot if it's a template\n"
5295b23d
DM
4682 if is_template($conf);
4683
0d18dcfc
DM
4684 check_lock($conf);
4685
22c377f0
DM
4686 $conf->{lock} = 'snapshot';
4687
be190583
DM
4688 die "snapshot name '$snapname' already used\n"
4689 if defined($conf->{snapshots}->{$snapname});
0d18dcfc 4690
ee2f90b1 4691 my $storecfg = PVE::Storage::config();
7ea975ef 4692 die "snapshot feature is not available" if !has_feature('snapshot', $conf, $storecfg);
18bfb361 4693
782f4f75 4694 $snap = $conf->{snapshots}->{$snapname} = {};
0d18dcfc 4695
18bfb361
DM
4696 if ($save_vmstate && check_running($vmid)) {
4697 $snap->{vmstate} = &$alloc_vmstate_volid($storecfg, $vmid, $conf, $snapname);
4698 }
4699
ef59d1ca 4700 &$snapshot_copy_config($conf, $snap);
0d18dcfc 4701
782f4f75
DM
4702 $snap->{snapstate} = "prepare";
4703 $snap->{snaptime} = time();
4704 $snap->{description} = $comment if $comment;
4705
4b15803d
DM
4706 # always overwrite machine if we save vmstate. This makes sure we
4707 # can restore it later using correct machine type
4708 $snap->{machine} = get_current_qemu_machine($vmid) if $snap->{vmstate};
4709
0d18dcfc
DM
4710 update_config_nolock($vmid, $conf, 1);
4711 };
4712
4713 lock_config($vmid, $updatefn);
22c377f0
DM
4714
4715 return $snap;
0d18dcfc
DM
4716};
4717
4718my $snapshot_commit = sub {
4719 my ($vmid, $snapname) = @_;
4720
4721 my $updatefn = sub {
4722
4723 my $conf = load_config($vmid);
4724
be190583
DM
4725 die "missing snapshot lock\n"
4726 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
0d18dcfc
DM
4727
4728 my $snap = $conf->{snapshots}->{$snapname};
4729
be190583
DM
4730 die "snapshot '$snapname' does not exist\n" if !defined($snap);
4731
4732 die "wrong snapshot state\n"
4733 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
0d18dcfc 4734
0d18dcfc 4735 delete $snap->{snapstate};
ee2f90b1 4736 delete $conf->{lock};
0d18dcfc 4737
ef59d1ca 4738 my $newconf = &$snapshot_apply_config($conf, $snap);
0d18dcfc 4739
05e5ad3f
DM
4740 $newconf->{parent} = $snapname;
4741
0d18dcfc
DM
4742 update_config_nolock($vmid, $newconf, 1);
4743 };
4744
4745 lock_config($vmid, $updatefn);
4746};
4747
22c377f0
DM
4748sub snapshot_rollback {
4749 my ($vmid, $snapname) = @_;
4750
4751 my $snap;
4752
4753 my $prepare = 1;
4754
a3222b91 4755 my $storecfg = PVE::Storage::config();
be190583 4756
22c377f0
DM
4757 my $updatefn = sub {
4758
4759 my $conf = load_config($vmid);
4760
8b43bc11 4761 die "you can't rollback if vm is a template\n" if is_template($conf);
90b0c6b3 4762
ab33a7c2
DM
4763 $snap = $conf->{snapshots}->{$snapname};
4764
be190583 4765 die "snapshot '$snapname' does not exist\n" if !defined($snap);
ab33a7c2 4766
be190583 4767 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
ab33a7c2
DM
4768 if $snap->{snapstate};
4769
a3222b91
DM
4770 if ($prepare) {
4771 check_lock($conf);
4772 vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
4773 }
22c377f0
DM
4774
4775 die "unable to rollback vm $vmid: vm is running\n"
4776 if check_running($vmid);
4777
4778 if ($prepare) {
4779 $conf->{lock} = 'rollback';
4780 } else {
4781 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
4782 delete $conf->{lock};
4783 }
4784
4b15803d
DM
4785 my $forcemachine;
4786
22c377f0 4787 if (!$prepare) {
4b15803d
DM
4788 my $has_machine_config = defined($conf->{machine});
4789
22c377f0 4790 # copy snapshot config to current config
ef59d1ca
DM
4791 $conf = &$snapshot_apply_config($conf, $snap);
4792 $conf->{parent} = $snapname;
4b15803d 4793
d8b916fd
DM
4794 # Note: old code did not store 'machine', so we try to be smart
4795 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
4796 $forcemachine = $conf->{machine} || 'pc-i440fx-1.4';
be190583 4797 # we remove the 'machine' configuration if not explicitly specified
4b15803d
DM
4798 # in the original config.
4799 delete $conf->{machine} if $snap->{vmstate} && !$has_machine_config;
22c377f0
DM
4800 }
4801
4802 update_config_nolock($vmid, $conf, 1);
a3222b91
DM
4803
4804 if (!$prepare && $snap->{vmstate}) {
4805 my $statefile = PVE::Storage::path($storecfg, $snap->{vmstate});
4b15803d 4806 vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine);
a3222b91 4807 }
22c377f0
DM
4808 };
4809
4810 lock_config($vmid, $updatefn);
be190583 4811
22c377f0
DM
4812 foreach_drive($snap, sub {
4813 my ($ds, $drive) = @_;
4814
4815 return if drive_is_cdrom($drive);
4816
4817 my $volid = $drive->{file};
4818 my $device = "drive-$ds";
4819
79e57b29 4820 PVE::Storage::volume_snapshot_rollback($storecfg, $volid, $snapname);
22c377f0
DM
4821 });
4822
4823 $prepare = 0;
4824 lock_config($vmid, $updatefn);
4825}
4826
9dcf4909
DM
4827my $savevm_wait = sub {
4828 my ($vmid) = @_;
4829
4830 for(;;) {
ed221350 4831 my $stat = vm_mon_cmd_nocheck($vmid, "query-savevm");
9dcf4909
DM
4832 if (!$stat->{status}) {
4833 die "savevm not active\n";
4834 } elsif ($stat->{status} eq 'active') {
4835 sleep(1);
4836 next;
4837 } elsif ($stat->{status} eq 'completed') {
4838 last;
4839 } else {
4840 die "query-savevm returned status '$stat->{status}'\n";
4841 }
4842 }
4843};
4844
0d18dcfc 4845sub snapshot_create {
18bfb361 4846 my ($vmid, $snapname, $save_vmstate, $freezefs, $comment) = @_;
0d18dcfc 4847
18bfb361 4848 my $snap = &$snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
0d18dcfc 4849
18bfb361 4850 $freezefs = $save_vmstate = 0 if !$snap->{vmstate}; # vm is not running
030dd626 4851
3ee28e38
DM
4852 my $drivehash = {};
4853
18bfb361
DM
4854 my $running = check_running($vmid);
4855
0d18dcfc
DM
4856 eval {
4857 # create internal snapshots of all drives
22c377f0
DM
4858
4859 my $storecfg = PVE::Storage::config();
a3222b91
DM
4860
4861 if ($running) {
4862 if ($snap->{vmstate}) {
be190583 4863 my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
9dcf4909
DM
4864 vm_mon_cmd($vmid, "savevm-start", statefile => $path);
4865 &$savevm_wait($vmid);
a3222b91 4866 } else {
9dcf4909 4867 vm_mon_cmd($vmid, "savevm-start");
a3222b91
DM
4868 }
4869 };
4870
4871 qga_freezefs($vmid) if $running && $freezefs;
be190583 4872
22c377f0
DM
4873 foreach_drive($snap, sub {
4874 my ($ds, $drive) = @_;
4875
4876 return if drive_is_cdrom($drive);
0d18dcfc 4877
22c377f0
DM
4878 my $volid = $drive->{file};
4879 my $device = "drive-$ds";
4880
4881 qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
3ee28e38 4882 $drivehash->{$ds} = 1;
22c377f0 4883 });
0d18dcfc 4884 };
22c377f0
DM
4885 my $err = $@;
4886
1a71fa73 4887 eval { qga_unfreezefs($vmid) if $running && $freezefs; };
22c377f0
DM
4888 warn $@ if $@;
4889
9dcf4909 4890 eval { vm_mon_cmd($vmid, "savevm-end") if $running; };
22c377f0
DM
4891 warn $@ if $@;
4892
4893 if ($err) {
0d18dcfc 4894 warn "snapshot create failed: starting cleanup\n";
3ee28e38 4895 eval { snapshot_delete($vmid, $snapname, 0, $drivehash); };
0d18dcfc
DM
4896 warn $@ if $@;
4897 die $err;
4898 }
4899
4900 &$snapshot_commit($vmid, $snapname);
4901}
4902
3ee28e38 4903# Note: $drivehash is only set when called from snapshot_create.
0d18dcfc 4904sub snapshot_delete {
3ee28e38 4905 my ($vmid, $snapname, $force, $drivehash) = @_;
0d18dcfc
DM
4906
4907 my $prepare = 1;
4908
22c377f0 4909 my $snap;
ee2f90b1 4910 my $unused = [];
0d18dcfc 4911
6cb1a8cf
DM
4912 my $unlink_parent = sub {
4913 my ($confref, $new_parent) = @_;
4914
4915 if ($confref->{parent} && $confref->{parent} eq $snapname) {
4916 if ($new_parent) {
4917 $confref->{parent} = $new_parent;
4918 } else {
4919 delete $confref->{parent};
4920 }
4921 }
4922 };
be190583 4923
0d18dcfc 4924 my $updatefn = sub {
2009f324 4925 my ($remove_drive) = @_;
0d18dcfc 4926
22c377f0 4927 my $conf = load_config($vmid);
0d18dcfc 4928
5295b23d
DM
4929 if (!$drivehash) {
4930 check_lock($conf);
be190583 4931 die "you can't delete a snapshot if vm is a template\n"
5295b23d
DM
4932 if is_template($conf);
4933 }
0d18dcfc 4934
22c377f0 4935 $snap = $conf->{snapshots}->{$snapname};
0d18dcfc 4936
be190583 4937 die "snapshot '$snapname' does not exist\n" if !defined($snap);
0d18dcfc
DM
4938
4939 # remove parent refs
8fd882a4
SP
4940 if (!$prepare) {
4941 &$unlink_parent($conf, $snap->{parent});
4942 foreach my $sn (keys %{$conf->{snapshots}}) {
4943 next if $sn eq $snapname;
4944 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
4945 }
0d18dcfc
DM
4946 }
4947
2009f324 4948 if ($remove_drive) {
18bfb361
DM
4949 if ($remove_drive eq 'vmstate') {
4950 delete $snap->{$remove_drive};
4951 } else {
4952 my $drive = parse_drive($remove_drive, $snap->{$remove_drive});
4953 my $volid = $drive->{file};
4954 delete $snap->{$remove_drive};
4955 add_unused_volume($conf, $volid);
4956 }
2009f324
DM
4957 }
4958
0d18dcfc
DM
4959 if ($prepare) {
4960 $snap->{snapstate} = 'delete';
4961 } else {
4962 delete $conf->{snapshots}->{$snapname};
3ee28e38 4963 delete $conf->{lock} if $drivehash;
ee2f90b1
DM
4964 foreach my $volid (@$unused) {
4965 add_unused_volume($conf, $volid);
4966 }
0d18dcfc
DM
4967 }
4968
4969 update_config_nolock($vmid, $conf, 1);
4970 };
4971
4972 lock_config($vmid, $updatefn);
4973
18bfb361 4974 # now remove vmstate file
0d18dcfc 4975
22c377f0
DM
4976 my $storecfg = PVE::Storage::config();
4977
18bfb361
DM
4978 if ($snap->{vmstate}) {
4979 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
4980 if (my $err = $@) {
4981 die $err if !$force;
4982 warn $err;
4983 }
4984 # save changes (remove vmstate from snapshot)
4985 lock_config($vmid, $updatefn, 'vmstate') if !$force;
4986 };
4987
4988 # now remove all internal snapshots
4989 foreach_drive($snap, sub {
22c377f0
DM
4990 my ($ds, $drive) = @_;
4991
4992 return if drive_is_cdrom($drive);
3ee28e38 4993
22c377f0
DM
4994 my $volid = $drive->{file};
4995 my $device = "drive-$ds";
4996
2009f324
DM
4997 if (!$drivehash || $drivehash->{$ds}) {
4998 eval { qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname); };
4999 if (my $err = $@) {
5000 die $err if !$force;
5001 warn $err;
5002 }
3ee28e38 5003 }
2009f324
DM
5004
5005 # save changes (remove drive fron snapshot)
5006 lock_config($vmid, $updatefn, $ds) if !$force;
ee2f90b1 5007 push @$unused, $volid;
22c377f0 5008 });
0d18dcfc
DM
5009
5010 # now cleanup config
5011 $prepare = 0;
5012 lock_config($vmid, $updatefn);
5013}
5014
9cd07842 5015sub has_feature {
7ea975ef
AD
5016 my ($feature, $conf, $storecfg, $snapname, $running) = @_;
5017
719893a9 5018 my $err;
7ea975ef
AD
5019 foreach_drive($conf, sub {
5020 my ($ds, $drive) = @_;
5021
5022 return if drive_is_cdrom($drive);
5023 my $volid = $drive->{file};
5024 $err = 1 if !PVE::Storage::volume_has_feature($storecfg, $feature, $volid, $snapname, $running);
5025 });
5026
719893a9 5027 return $err ? 0 : 1;
7ea975ef 5028}
04a69bb4
AD
5029
5030sub template_create {
5031 my ($vmid, $conf, $disk) = @_;
5032
04a69bb4 5033 my $storecfg = PVE::Storage::config();
04a69bb4 5034
9cd07842
DM
5035 foreach_drive($conf, sub {
5036 my ($ds, $drive) = @_;
5037
5038 return if drive_is_cdrom($drive);
5039 return if $disk && $ds ne $disk;
5040
5041 my $volid = $drive->{file};
bbd56097 5042 return if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid);
9cd07842 5043
04a69bb4
AD
5044 my $voliddst = PVE::Storage::vdisk_create_base($storecfg, $volid);
5045 $drive->{file} = $voliddst;
152fe752
DM
5046 $conf->{$ds} = print_drive($vmid, $drive);
5047 update_config_nolock($vmid, $conf, 1);
04a69bb4 5048 });
04a69bb4
AD
5049}
5050
624361b3
AD
5051sub is_template {
5052 my ($conf) = @_;
5053
96d695c0 5054 return 1 if defined $conf->{template} && $conf->{template} == 1;
624361b3
AD
5055}
5056
5133de42
AD
5057sub qemu_img_convert {
5058 my ($src_volid, $dst_volid, $size, $snapname) = @_;
5059
5060 my $storecfg = PVE::Storage::config();
5061 my ($src_storeid, $src_volname) = PVE::Storage::parse_volume_id($src_volid, 1);
5062 my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid, 1);
5063
5064 if ($src_storeid && $dst_storeid) {
5065 my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
5066 my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
5067
5068 my $src_format = qemu_img_format($src_scfg, $src_volname);
5069 my $dst_format = qemu_img_format($dst_scfg, $dst_volname);
5070
5071 my $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
5072 my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
5073
5074 my $cmd = [];
71ddbff9 5075 push @$cmd, '/usr/bin/qemu-img', 'convert', '-t', 'writeback', '-p', '-n';
5133de42
AD
5076 push @$cmd, '-s', $snapname if($snapname && $src_format eq "qcow2");
5077 push @$cmd, '-f', $src_format, '-O', $dst_format, $src_path, $dst_path;
5078
5079 my $parser = sub {
5080 my $line = shift;
5081 if($line =~ m/\((\S+)\/100\%\)/){
5082 my $percent = $1;
5083 my $transferred = int($size * $percent / 100);
5084 my $remaining = $size - $transferred;
5085
5086 print "transferred: $transferred bytes remaining: $remaining bytes total: $size bytes progression: $percent %\n";
5087 }
5088
5089 };
5090
5091 eval { run_command($cmd, timeout => undef, outfunc => $parser); };
5092 my $err = $@;
5093 die "copy failed: $err" if $err;
5094 }
5095}
5096
5097sub qemu_img_format {
5098 my ($scfg, $volname) = @_;
5099
ccb5c001 5100 if ($scfg->{path} && $volname =~ m/\.(raw|qcow2|qed|vmdk)$/) {
5133de42 5101 return $1;
ccb5c001 5102 } elsif ($scfg->{type} eq 'iscsi') {
5133de42 5103 return "host_device";
be190583 5104 } else {
5133de42 5105 return "raw";
5133de42
AD
5106 }
5107}
5108
cfad42af
AD
5109sub qemu_drive_mirror {
5110 my ($vmid, $drive, $dst_volid, $vmiddst, $maxwait) = @_;
5111
5112 my $count = 1;
5113 my $old_len = 0;
5114 my $frozen = undef;
5115
5116 my $storecfg = PVE::Storage::config();
5117 my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid, 1);
5118
5119 if ($dst_storeid) {
5120 my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
5121
152fe752 5122 my $format;
cfad42af
AD
5123 if ($dst_volname =~ m/\.(raw|qcow2)$/){
5124 $format = $1;
5125 }
5126
5127 my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
5128
152fe752 5129 if ($format) {
be190583 5130 #fixme : sometime drive-mirror timeout, but works fine after.
152fe752 5131 # (I have see the problem with big volume > 200GB), so we need to eval
be190583 5132 eval { vm_mon_cmd($vmid, "drive-mirror", timeout => 10, device => "drive-$drive", mode => "existing",
152fe752
DM
5133 sync => "full", target => $dst_path, format => $format); };
5134 } else {
be190583 5135 eval { vm_mon_cmd($vmid, "drive-mirror", timeout => 10, device => "drive-$drive", mode => "existing",
152fe752 5136 sync => "full", target => $dst_path); };
cfad42af 5137 }
152fe752
DM
5138
5139 eval {
cfad42af 5140 while (1) {
152fe752 5141 my $stats = vm_mon_cmd($vmid, "query-block-jobs");
cfad42af 5142 my $stat = @$stats[0];
f6ab3bdb
AD
5143 die "mirroring job seem to have die. Maybe do you have bad sectors?" if !$stat;
5144 die "error job is not mirroring" if $stat->{type} ne "mirror";
5145
cfad42af
AD
5146 my $transferred = $stat->{offset};
5147 my $total = $stat->{len};
5148 my $remaining = $total - $transferred;
5149 my $percent = sprintf "%.2f", ($transferred * 100 / $total);
cfad42af
AD
5150
5151 print "transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent %\n";
5152
5153 last if ($stat->{len} == $stat->{offset});
5154 if ($old_len == $stat->{offset}) {
5155 if ($maxwait && $count > $maxwait) {
5156 # if writes to disk occurs the disk needs to be freezed
5157 # to be able to complete the migration
5158 vm_suspend($vmid,1);
5159 $count = 0;
5160 $frozen = 1;
152fe752 5161 } else {
cfad42af
AD
5162 $count++ unless $frozen;
5163 }
152fe752
DM
5164 } elsif ($frozen) {
5165 vm_resume($vmid,1);
5166 $count = 0;
cfad42af
AD
5167 }
5168 $old_len = $stat->{offset};
5169 sleep 1;
5170 }
be190583 5171
2fc6bc17 5172 if ($vmiddst == $vmid) {
be190583 5173 # switch the disk if source and destination are on the same guest
2fc6bc17
DM
5174 vm_mon_cmd($vmid, "block-job-complete", device => "drive-$drive");
5175 }
cfad42af 5176 };
4fca0153 5177 if (my $err = $@) {
152fe752 5178 eval { vm_mon_cmd($vmid, "block-job-cancel", device => "drive-$drive"); };
4fca0153 5179 die "mirroring error: $err";
cfad42af
AD
5180 }
5181
2fc6bc17
DM
5182 if ($vmiddst != $vmid) {
5183 # if we clone a disk for a new target vm, we don't switch the disk
152fe752 5184 vm_mon_cmd($vmid, "block-job-cancel", device => "drive-$drive");
cfad42af
AD
5185 }
5186 }
5187}
5188
152fe752 5189sub clone_disk {
be190583 5190 my ($storecfg, $vmid, $running, $drivename, $drive, $snapname,
152fe752
DM
5191 $newvmid, $storage, $format, $full, $newvollist) = @_;
5192
5193 my $newvolid;
5194
5195 if (!$full) {
5196 print "create linked clone of drive $drivename ($drive->{file})\n";
258e646c 5197 $newvolid = PVE::Storage::vdisk_clone($storecfg, $drive->{file}, $newvmid, $snapname);
152fe752
DM
5198 push @$newvollist, $newvolid;
5199 } else {
5200 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
5201 $storeid = $storage if $storage;
5202
1377d7b0
DM
5203 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
5204 if (!$format) {
5205 $format = $drive->{format} || $defFormat;
152fe752
DM
5206 }
5207
1377d7b0
DM
5208 # test if requested format is supported - else use default
5209 my $supported = grep { $_ eq $format } @$validFormats;
5210 $format = $defFormat if !$supported;
5211
152fe752
DM
5212 my ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 3);
5213
5214 print "create full clone of drive $drivename ($drive->{file})\n";
5215 $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newvmid, $format, undef, ($size/1024));
5216 push @$newvollist, $newvolid;
5217
5218 if (!$running || $snapname) {
5219 qemu_img_convert($drive->{file}, $newvolid, $size, $snapname);
5220 } else {
5221 qemu_drive_mirror($vmid, $drivename, $newvolid, $newvmid);
be190583 5222 }
152fe752
DM
5223 }
5224
5225 my ($size) = PVE::Storage::volume_size_info($storecfg, $newvolid, 3);
5226
5227 my $disk = $drive;
5228 $disk->{format} = undef;
5229 $disk->{file} = $newvolid;
5230 $disk->{size} = $size;
5231
5232 return $disk;
5233}
5234
ff556cf2
DM
5235# this only works if VM is running
5236sub get_current_qemu_machine {
5237 my ($vmid) = @_;
5238
5239 my $cmd = { execute => 'query-machines', arguments => {} };
be190583 5240 my $res = PVE::QemuServer::vm_qmp_command($vmid, $cmd);
ff556cf2
DM
5241
5242 my ($current, $default);
5243 foreach my $e (@$res) {
5244 $default = $e->{name} if $e->{'is-default'};
5245 $current = $e->{name} if $e->{'is-current'};
5246 }
5247
5248 # fallback to the default machine if current is not supported by qemu
5249 return $current || $default || 'pc';
5250}
5251
4543ecf0
AD
5252sub lspci {
5253
5254 my $devices = {};
5255
5256 dir_glob_foreach("$pcisysfs/devices", '[a-f0-9]{4}:([a-f0-9]{2}:[a-f0-9]{2})\.([0-9])', sub {
5257 my (undef, $id, $function) = @_;
5258 my $res = { id => $id, function => $function};
5259 push @{$devices->{$id}}, $res;
5260 });
5261
5262 return $devices;
5263}
5264
1e3baf05 52651;