]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer.pm
move test for block device to vmtar.c
[qemu-server.git] / PVE / QemuServer.pm
CommitLineData
1e3baf05
DM
1package PVE::QemuServer;
2
3use strict;
4use POSIX;
5use IO::Handle;
6use IO::Select;
7use IO::File;
8use IO::Dir;
9use IO::Socket::UNIX;
10use File::Basename;
11use File::Path;
12use File::stat;
13use Getopt::Long;
14use Digest::SHA1;
15use Fcntl ':flock';
16use Cwd 'abs_path';
17use IPC::Open3;
18use Fcntl;
19use PVE::SafeSyslog;
20use Storable qw(dclone);
21use PVE::Exception qw(raise raise_param_exc);
22use PVE::Storage;
23use PVE::Tools qw(run_command lock_file file_read_firstline);
24use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
25use PVE::INotify;
26use PVE::ProcFSTools;
6b64503e 27use Time::HiRes qw(gettimeofday);
1e3baf05 28
7f0b5beb 29my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
1e3baf05 30
19672434 31# Note about locking: we use flock on the config file protect
1e3baf05
DM
32# against concurent actions.
33# Aditionaly, we have a 'lock' setting in the config file. This
34# can be set to 'migrate' or 'backup'. Most actions are not
35# allowed when such lock is set. But you can ignore this kind of
36# lock with the --skiplock flag.
37
1858638f
DM
38cfs_register_file('/qemu-server/',
39 \&parse_vm_config,
40 \&write_vm_config);
1e3baf05 41
3ea94c60
DM
42PVE::JSONSchema::register_standard_option('skiplock', {
43 description => "Ignore locks - only root is allowed to use this option.",
afdb31d5 44 type => 'boolean',
3ea94c60
DM
45 optional => 1,
46});
47
48PVE::JSONSchema::register_standard_option('pve-qm-stateuri', {
49 description => "Some command save/restore state from this location.",
50 type => 'string',
51 maxLength => 128,
52 optional => 1,
53});
54
1e3baf05
DM
55#no warnings 'redefine';
56
57unless(defined(&_VZSYSCALLS_H_)) {
58 eval 'sub _VZSYSCALLS_H_ () {1;}' unless defined(&_VZSYSCALLS_H_);
59 require 'sys/syscall.ph';
60 if(defined(&__x86_64__)) {
61 eval 'sub __NR_fairsched_vcpus () {499;}' unless defined(&__NR_fairsched_vcpus);
62 eval 'sub __NR_fairsched_mknod () {504;}' unless defined(&__NR_fairsched_mknod);
63 eval 'sub __NR_fairsched_rmnod () {505;}' unless defined(&__NR_fairsched_rmnod);
64 eval 'sub __NR_fairsched_chwt () {506;}' unless defined(&__NR_fairsched_chwt);
65 eval 'sub __NR_fairsched_mvpr () {507;}' unless defined(&__NR_fairsched_mvpr);
66 eval 'sub __NR_fairsched_rate () {508;}' unless defined(&__NR_fairsched_rate);
67 eval 'sub __NR_setluid () {501;}' unless defined(&__NR_setluid);
68 eval 'sub __NR_setublimit () {502;}' unless defined(&__NR_setublimit);
69 }
70 elsif(defined( &__i386__) ) {
71 eval 'sub __NR_fairsched_mknod () {500;}' unless defined(&__NR_fairsched_mknod);
72 eval 'sub __NR_fairsched_rmnod () {501;}' unless defined(&__NR_fairsched_rmnod);
73 eval 'sub __NR_fairsched_chwt () {502;}' unless defined(&__NR_fairsched_chwt);
74 eval 'sub __NR_fairsched_mvpr () {503;}' unless defined(&__NR_fairsched_mvpr);
75 eval 'sub __NR_fairsched_rate () {504;}' unless defined(&__NR_fairsched_rate);
76 eval 'sub __NR_fairsched_vcpus () {505;}' unless defined(&__NR_fairsched_vcpus);
77 eval 'sub __NR_setluid () {511;}' unless defined(&__NR_setluid);
78 eval 'sub __NR_setublimit () {512;}' unless defined(&__NR_setublimit);
79 } else {
80 die("no fairsched syscall for this arch");
81 }
82 require 'asm/ioctl.ph';
83 eval 'sub KVM_GET_API_VERSION () { &_IO(0xAE, 0x);}' unless defined(&KVM_GET_API_VERSION);
84}
85
86sub fairsched_mknod {
87 my ($parent, $weight, $desired) = @_;
88
6b64503e 89 return syscall(&__NR_fairsched_mknod, int($parent), int($weight), int($desired));
1e3baf05
DM
90}
91
92sub fairsched_rmnod {
93 my ($id) = @_;
94
6b64503e 95 return syscall(&__NR_fairsched_rmnod, int($id));
1e3baf05
DM
96}
97
98sub fairsched_mvpr {
99 my ($pid, $newid) = @_;
100
6b64503e 101 return syscall(&__NR_fairsched_mvpr, int($pid), int($newid));
1e3baf05
DM
102}
103
104sub fairsched_vcpus {
105 my ($id, $vcpus) = @_;
106
6b64503e 107 return syscall(&__NR_fairsched_vcpus, int($id), int($vcpus));
1e3baf05
DM
108}
109
110sub fairsched_rate {
111 my ($id, $op, $rate) = @_;
112
6b64503e 113 return syscall(&__NR_fairsched_rate, int($id), int($op), int($rate));
1e3baf05
DM
114}
115
116use constant FAIRSCHED_SET_RATE => 0;
117use constant FAIRSCHED_DROP_RATE => 1;
118use constant FAIRSCHED_GET_RATE => 2;
119
120sub fairsched_cpulimit {
121 my ($id, $limit) = @_;
122
6b64503e 123 my $cpulim1024 = int($limit * 1024 / 100);
1e3baf05
DM
124 my $op = $cpulim1024 ? FAIRSCHED_SET_RATE : FAIRSCHED_DROP_RATE;
125
6b64503e 126 return fairsched_rate($id, $op, $cpulim1024);
1e3baf05
DM
127}
128
129my $nodename = PVE::INotify::nodename();
130
131mkdir "/etc/pve/nodes/$nodename";
132my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
133mkdir $confdir;
134
135my $var_run_tmpdir = "/var/run/qemu-server";
136mkdir $var_run_tmpdir;
137
138my $lock_dir = "/var/lock/qemu-server";
139mkdir $lock_dir;
140
141my $pcisysfs = "/sys/bus/pci";
142
1e3baf05
DM
143my $confdesc = {
144 onboot => {
145 optional => 1,
146 type => 'boolean',
147 description => "Specifies whether a VM will be started during system bootup.",
148 default => 0,
149 },
150 autostart => {
151 optional => 1,
152 type => 'boolean',
153 description => "Automatic restart after crash (currently ignored).",
154 default => 0,
155 },
2ff09f52
DA
156 hotplug => {
157 optional => 1,
e8b9c17c 158 type => 'boolean',
2ff09f52
DA
159 description => "Activate hotplug for disk and network device",
160 default => 0,
161 },
1e3baf05
DM
162 reboot => {
163 optional => 1,
164 type => 'boolean',
165 description => "Allow reboot. If set to '0' the VM exit on reboot.",
166 default => 1,
167 },
168 lock => {
169 optional => 1,
170 type => 'string',
171 description => "Lock/unlock the VM.",
172 enum => [qw(migrate backup)],
173 },
174 cpulimit => {
175 optional => 1,
176 type => 'integer',
177 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.",
178 minimum => 0,
179 default => 0,
180 },
181 cpuunits => {
182 optional => 1,
183 type => 'integer',
184 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.",
185 minimum => 0,
186 maximum => 500000,
187 default => 1000,
188 },
189 memory => {
190 optional => 1,
191 type => 'integer',
7878afeb 192 description => "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device.",
1e3baf05
DM
193 minimum => 16,
194 default => 512,
195 },
13a48620
DA
196 balloon => {
197 optional => 1,
198 type => 'integer',
199 description => "Amount of target RAM for the VM in MB.",
7878afeb 200 minimum => 16,
13a48620 201 },
1e3baf05
DM
202 keyboard => {
203 optional => 1,
204 type => 'string',
205 description => "Keybord layout for vnc server. Default is read from the datacenter configuration file.",
e95fe75f 206 enum => PVE::Tools::kvmkeymaplist(),
1e3baf05
DM
207 default => 'en-us',
208 },
209 name => {
210 optional => 1,
211 type => 'string',
212 description => "Set a name for the VM. Only used on the configuration web interface.",
213 },
214 description => {
215 optional => 1,
216 type => 'string',
217 description => "Description for the VM. Only used on the configuration web interface.",
218 },
219 ostype => {
220 optional => 1,
221 type => 'string',
222 enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 l24 l26)],
223 description => <<EODESC,
224Used to enable special optimization/features for specific
225operating systems:
226
227other => unspecified OS
228wxp => Microsoft Windows XP
229w2k => Microsoft Windows 2000
230w2k3 => Microsoft Windows 2003
231w2k8 => Microsoft Windows 2008
232wvista => Microsoft Windows Vista
233win7 => Microsoft Windows 7
234l24 => Linux 2.4 Kernel
235l26 => Linux 2.6/3.X Kernel
236
237other|l24|l26 ... no special behaviour
238wxp|w2k|w2k3|w2k8|wvista|win7 ... use --localtime switch
239EODESC
240 },
241 boot => {
242 optional => 1,
243 type => 'string',
244 description => "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n).",
245 pattern => '[acdn]{1,4}',
32baffb4 246 default => 'cdn',
1e3baf05
DM
247 },
248 bootdisk => {
249 optional => 1,
250 type => 'string', format => 'pve-qm-bootdisk',
251 description => "Enable booting from specified disk.",
252 pattern => '(ide|scsi|virtio)\d+',
253 },
254 smp => {
255 optional => 1,
256 type => 'integer',
257 description => "The number of CPUs. Please use option -sockets instead.",
258 minimum => 1,
259 default => 1,
260 },
261 sockets => {
262 optional => 1,
263 type => 'integer',
264 description => "The number of CPU sockets.",
265 minimum => 1,
266 default => 1,
267 },
268 cores => {
269 optional => 1,
270 type => 'integer',
271 description => "The number of cores per socket.",
272 minimum => 1,
273 default => 1,
274 },
275 acpi => {
276 optional => 1,
277 type => 'boolean',
278 description => "Enable/disable ACPI.",
279 default => 1,
280 },
281 kvm => {
282 optional => 1,
283 type => 'boolean',
284 description => "Enable/disable KVM hardware virtualization.",
285 default => 1,
286 },
287 tdf => {
288 optional => 1,
289 type => 'boolean',
290 description => "Enable/disable time drift fix.",
291 default => 1,
292 },
19672434 293 localtime => {
1e3baf05
DM
294 optional => 1,
295 type => 'boolean',
296 description => "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS.",
297 },
298 freeze => {
299 optional => 1,
300 type => 'boolean',
301 description => "Freeze CPU at startup (use 'c' monitor command to start execution).",
302 },
303 vga => {
304 optional => 1,
305 type => 'string',
306 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 win7/w2k8, and 'cirrur' for other OS types",
307 enum => [qw(std cirrus vmware)],
308 },
0ea9541d
DM
309 watchdog => {
310 optional => 1,
311 type => 'string', format => 'pve-qm-watchdog',
312 typetext => '[[model=]i6300esb|ib700] [,[action=]reset|shutdown|poweroff|pause|debug|none]',
313 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)",
314 },
1e3baf05
DM
315 startdate => {
316 optional => 1,
19672434 317 type => 'string',
1e3baf05
DM
318 typetext => "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)",
319 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'.",
320 pattern => '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)',
321 default => 'now',
322 },
323 args => {
324 optional => 1,
325 type => 'string',
326 description => <<EODESCR,
327Note: this option is for experts only. It allows you to pass arbitrary arguments to kvm, for example:
328
329args: -no-reboot -no-hpet
330EODESCR
331 },
332 tablet => {
333 optional => 1,
334 type => 'boolean',
335 default => 1,
336 description => "Enable/disable the usb tablet device. This device is usually needed to allow absolute mouse positioning. 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.",
337 },
338 migrate_speed => {
339 optional => 1,
340 type => 'integer',
341 description => "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
342 minimum => 0,
343 default => 0,
344 },
345 migrate_downtime => {
346 optional => 1,
347 type => 'integer',
348 description => "Set maximum tolerated downtime (in seconds) for migrations.",
349 minimum => 0,
350 default => 1,
351 },
352 cdrom => {
353 optional => 1,
354 type => 'string', format => 'pve-qm-drive',
355 typetext => 'volume',
356 description => "This is an alias for option -ide2",
357 },
358 cpu => {
359 optional => 1,
360 description => "Emulated CPU type.",
361 type => 'string',
362 enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom host) ],
363 default => 'qemu64',
364 },
365};
366
367# what about other qemu settings ?
368#cpu => 'string',
369#machine => 'string',
370#fda => 'file',
371#fdb => 'file',
372#mtdblock => 'file',
373#sd => 'file',
374#pflash => 'file',
375#snapshot => 'bool',
376#bootp => 'file',
377##tftp => 'dir',
378##smb => 'dir',
379#kernel => 'file',
380#append => 'string',
381#initrd => 'file',
382##soundhw => 'string',
383
384while (my ($k, $v) = each %$confdesc) {
385 PVE::JSONSchema::register_standard_option("pve-qm-$k", $v);
386}
387
388my $MAX_IDE_DISKS = 4;
f62db2a4
DA
389my $MAX_SCSI_DISKS = 14;
390my $MAX_VIRTIO_DISKS = 6;
cdb0931f 391my $MAX_SATA_DISKS = 6;
1e3baf05 392my $MAX_USB_DEVICES = 5;
f62db2a4 393my $MAX_NETS = 6;
1e3baf05 394my $MAX_UNUSED_DISKS = 8;
040b06b7 395my $MAX_HOSTPCI_DEVICES = 2;
bae179aa 396my $MAX_SERIAL_PORTS = 4;
1989a89c 397my $MAX_PARALLEL_PORTS = 3;
1e3baf05
DM
398
399my $nic_model_list = ['rtl8139', 'ne2k_pci', 'e1000', 'pcnet', 'virtio',
400 'ne2k_isa', 'i82551', 'i82557b', 'i82559er'];
6b64503e 401my $nic_model_list_txt = join(' ', sort @$nic_model_list);
1e3baf05
DM
402
403# fixme:
404my $netdesc = {
405 optional => 1,
406 type => 'string', format => 'pve-qm-net',
407 typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>]",
408 description => <<EODESCR,
19672434 409Specify network devices.
1e3baf05
DM
410
411MODEL is one of: $nic_model_list_txt
412
19672434 413XX:XX:XX:XX:XX:XX should be an unique MAC address. This is
1e3baf05
DM
414automatically generated if not specified.
415
416The bridge parameter can be used to automatically add the interface to a bridge device. The Proxmox VE standard bridge is called 'vmbr0'.
417
418Option 'rate' is used to limit traffic bandwidth from and to this interface. It is specified as floating point number, unit is 'Megabytes per second'.
419
420If you specify no bridge, we create a kvm 'user' (NATed) network device, which provides DHCP and DNS services. The following addresses are used:
421
42210.0.2.2 Gateway
42310.0.2.3 DNS Server
42410.0.2.4 SMB Server
425
426The DHCP server assign addresses to the guest starting from 10.0.2.15.
427
428EODESCR
429};
430PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
431
432for (my $i = 0; $i < $MAX_NETS; $i++) {
433 $confdesc->{"net$i"} = $netdesc;
434}
435
436my $drivename_hash;
19672434 437
1e3baf05
DM
438my $idedesc = {
439 optional => 1,
440 type => 'string', format => 'pve-qm-drive',
5534dd1a 441 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe] [,format=f] [,backup=yes|no] [,aio=native|threads]',
1e3baf05
DM
442 description => "Use volume as IDE hard disk or CD-ROM (n is 0 to 3).",
443};
444PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
445
446my $scsidesc = {
447 optional => 1,
448 type => 'string', format => 'pve-qm-drive',
231f2e13 449 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe] [,format=f] [,backup=yes|no] [,aio=native|threads]',
2fe1a152 450 description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to 13).",
1e3baf05
DM
451};
452PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc);
453
cdb0931f
DA
454my $satadesc = {
455 optional => 1,
456 type => 'string', format => 'pve-qm-drive',
457 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe] [,format=f] [,backup=yes|no] [,aio=native|threads]',
458 description => "Use volume as SATA hard disk or CD-ROM (n is 0 to 5).",
459};
460PVE::JSONSchema::register_standard_option("pve-qm-sata", $satadesc);
461
1e3baf05
DM
462my $virtiodesc = {
463 optional => 1,
464 type => 'string', format => 'pve-qm-drive',
5534dd1a 465 typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe] [,format=f] [,backup=yes|no] [,aio=native|threads]',
2fe1a152 466 description => "Use volume as VIRTIO hard disk (n is 0 to 5).",
1e3baf05
DM
467};
468PVE::JSONSchema::register_standard_option("pve-qm-virtio", $virtiodesc);
469
470my $usbdesc = {
471 optional => 1,
472 type => 'string', format => 'pve-qm-usb-device',
473 typetext => 'host=HOSTUSBDEVICE',
474 description => <<EODESCR,
2fe1a152 475Configure an USB device (n is 0 to 4). This can be used to
1e3baf05
DM
476pass-through usb devices to the guest. HOSTUSBDEVICE syntax is:
477
19672434 478'bus-port(.port)*' (decimal numbers) or
1e3baf05
DM
479'vendor_id:product_id' (hexadeciaml numbers)
480
19672434 481You can use the 'lsusb -t' command to list existing usb devices.
1e3baf05
DM
482
483Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
484
485EODESCR
486};
487PVE::JSONSchema::register_standard_option("pve-qm-usb", $usbdesc);
488
040b06b7
DA
489my $hostpcidesc = {
490 optional => 1,
491 type => 'string', format => 'pve-qm-hostpci',
492 typetext => "HOSTPCIDEVICE",
493 description => <<EODESCR,
494Map host pci devices. HOSTPCIDEVICE syntax is:
495
496'bus:dev.func' (hexadecimal numbers)
497
498You can us the 'lspci' command to list existing pci devices.
499
500Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
501
502Experimental: user reported problems with this option.
503EODESCR
504};
505PVE::JSONSchema::register_standard_option("pve-qm-hostpci", $hostpcidesc);
506
bae179aa
DA
507my $serialdesc = {
508 optional => 1,
ca0cef26 509 type => 'string',
2fe1a152 510 pattern => '/dev/ttyS\d+',
bae179aa 511 description => <<EODESCR,
19672434 512Map host serial devices (n is 0 to 3).
bae179aa
DA
513
514Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
515
516Experimental: user reported problems with this option.
517EODESCR
518};
bae179aa 519
1989a89c
DA
520my $paralleldesc= {
521 optional => 1,
ca0cef26 522 type => 'string',
2fe1a152 523 pattern => '/dev/parport\d+',
1989a89c 524 description => <<EODESCR,
19672434 525Map host parallel devices (n is 0 to 2).
1989a89c
DA
526
527Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
528
529Experimental: user reported problems with this option.
530EODESCR
531};
1989a89c
DA
532
533for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
534 $confdesc->{"parallel$i"} = $paralleldesc;
535}
536
bae179aa
DA
537for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
538 $confdesc->{"serial$i"} = $serialdesc;
539}
540
040b06b7
DA
541for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
542 $confdesc->{"hostpci$i"} = $hostpcidesc;
543}
1e3baf05
DM
544
545for (my $i = 0; $i < $MAX_IDE_DISKS; $i++) {
546 $drivename_hash->{"ide$i"} = 1;
547 $confdesc->{"ide$i"} = $idedesc;
548}
549
cdb0931f
DA
550for (my $i = 0; $i < $MAX_SATA_DISKS; $i++) {
551 $drivename_hash->{"sata$i"} = 1;
552 $confdesc->{"sata$i"} = $satadesc;
553}
554
1e3baf05
DM
555for (my $i = 0; $i < $MAX_SCSI_DISKS; $i++) {
556 $drivename_hash->{"scsi$i"} = 1;
557 $confdesc->{"scsi$i"} = $scsidesc ;
558}
559
560for (my $i = 0; $i < $MAX_VIRTIO_DISKS; $i++) {
561 $drivename_hash->{"virtio$i"} = 1;
562 $confdesc->{"virtio$i"} = $virtiodesc;
563}
564
565for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
566 $confdesc->{"usb$i"} = $usbdesc;
567}
568
569my $unuseddesc = {
570 optional => 1,
571 type => 'string', format => 'pve-volume-id',
572 description => "Reference to unused volumes.",
573};
574
575for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
576 $confdesc->{"unused$i"} = $unuseddesc;
577}
578
579my $kvm_api_version = 0;
580
581sub kvm_version {
582
583 return $kvm_api_version if $kvm_api_version;
584
6b64503e 585 my $fh = IO::File->new("</dev/kvm") ||
1e3baf05
DM
586 return 0;
587
6b64503e 588 if (my $v = $fh->ioctl(KVM_GET_API_VERSION(), 0)) {
1e3baf05
DM
589 $kvm_api_version = $v;
590 }
591
592 $fh->close();
593
594 return $kvm_api_version;
595}
596
597my $kvm_user_version;
598
599sub kvm_user_version {
600
601 return $kvm_user_version if $kvm_user_version;
602
603 $kvm_user_version = 'unknown';
604
605 my $tmp = `kvm -help 2>/dev/null`;
19672434 606
a3c52213 607 if ($tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?) /) {
1e3baf05
DM
608 $kvm_user_version = $2;
609 }
610
611 return $kvm_user_version;
612
613}
614
615my $kernel_has_vhost_net = -c '/dev/vhost-net';
616
617sub disknames {
618 # order is important - used to autoselect boot disk
19672434 619 return ((map { "ide$_" } (0 .. ($MAX_IDE_DISKS - 1))),
1e3baf05 620 (map { "scsi$_" } (0 .. ($MAX_SCSI_DISKS - 1))),
cdb0931f
DA
621 (map { "virtio$_" } (0 .. ($MAX_VIRTIO_DISKS - 1))),
622 (map { "sata$_" } (0 .. ($MAX_SATA_DISKS - 1))));
1e3baf05
DM
623}
624
625sub valid_drivename {
626 my $dev = shift;
627
6b64503e 628 return defined($drivename_hash->{$dev});
1e3baf05
DM
629}
630
631sub option_exists {
632 my $key = shift;
633 return defined($confdesc->{$key});
19672434 634}
1e3baf05
DM
635
636sub nic_models {
637 return $nic_model_list;
638}
639
640sub os_list_description {
641
642 return {
643 other => 'Other',
644 wxp => 'Windows XP',
645 w2k => 'Windows 2000',
646 w2k3 =>, 'Windows 2003',
647 w2k8 => 'Windows 2008',
648 wvista => 'Windows Vista',
649 win7 => 'Windows 7',
650 l24 => 'Linux 2.4',
651 l26 => 'Linux 2.6',
19672434 652 };
1e3baf05
DM
653}
654
1e3baf05
DM
655sub disk_devive_info {
656 my $dev = shift;
657
658 die "unknown disk device format '$dev'" if $dev !~ m/^(ide|scsi|virtio)(\d+)$/;
659
660 my $bus = $1;
661 my $index = $2;
662 my $maxdev = 1024;
663
664 if ($bus eq 'ide') {
665 $maxdev = 2;
666 } elsif ($bus eq 'scsi') {
f62db2a4 667 $maxdev = 7;
1e3baf05
DM
668 }
669
6b64503e 670 my $controller = int($index / $maxdev);
1e3baf05
DM
671 my $unit = $index % $maxdev;
672
673
674 return { bus => $bus, desc => uc($bus) . " $controller:$unit",
675 controller => $controller, unit => $unit, index => $index };
676
677}
678
679sub qemu_drive_name {
19672434 680 my ($dev, $media) = @_;
1e3baf05 681
6b64503e 682 my $info = disk_devive_info($dev);
1e3baf05
DM
683 my $mediastr = '';
684
685 if (($info->{bus} eq 'ide') || ($info->{bus} eq 'scsi')) {
686 $mediastr = ($media eq 'cdrom') ? "-cd" : "-hd";
19672434 687 return sprintf("%s%i%s%i", $info->{bus}, $info->{controller},
1e3baf05
DM
688 $mediastr, $info->{unit});
689 } else {
19672434 690 return sprintf("%s%i", $info->{bus}, $info->{index});
1e3baf05
DM
691 }
692}
693
694my $cdrom_path;
695
696sub get_cdrom_path {
697
698 return $cdrom_path if $cdrom_path;
699
700 return $cdrom_path = "/dev/cdrom" if -l "/dev/cdrom";
701 return $cdrom_path = "/dev/cdrom1" if -l "/dev/cdrom1";
702 return $cdrom_path = "/dev/cdrom2" if -l "/dev/cdrom2";
703}
704
705sub get_iso_path {
706 my ($storecfg, $vmid, $cdrom) = @_;
707
708 if ($cdrom eq 'cdrom') {
709 return get_cdrom_path();
710 } elsif ($cdrom eq 'none') {
711 return '';
712 } elsif ($cdrom =~ m|^/|) {
713 return $cdrom;
714 } else {
6b64503e 715 return PVE::Storage::path($storecfg, $cdrom);
1e3baf05
DM
716 }
717}
718
719# try to convert old style file names to volume IDs
720sub filename_to_volume_id {
721 my ($vmid, $file, $media) = @_;
722
723 if (!($file eq 'none' || $file eq 'cdrom' ||
724 $file =~ m|^/dev/.+| || $file =~ m/^([^:]+):(.+)$/)) {
19672434 725
1e3baf05 726 return undef if $file =~ m|/|;
19672434 727
1e3baf05
DM
728 if ($media && $media eq 'cdrom') {
729 $file = "local:iso/$file";
730 } else {
731 $file = "local:$vmid/$file";
732 }
733 }
734
735 return $file;
736}
737
738sub verify_media_type {
739 my ($opt, $vtype, $media) = @_;
740
741 return if !$media;
742
743 my $etype;
744 if ($media eq 'disk') {
745 $etype = 'image';
746 } elsif ($media eq 'cdrom') {
747 $etype = 'iso';
748 } else {
749 die "internal error";
750 }
751
752 return if ($vtype eq $etype);
19672434 753
1e3baf05
DM
754 raise_param_exc({ $opt => "unexpected media type ($vtype != $etype)" });
755}
756
757sub cleanup_drive_path {
758 my ($opt, $storecfg, $drive) = @_;
759
760 # try to convert filesystem paths to volume IDs
761
762 if (($drive->{file} !~ m/^(cdrom|none)$/) &&
763 ($drive->{file} !~ m|^/dev/.+|) &&
764 ($drive->{file} !~ m/^([^:]+):(.+)$/) &&
19672434 765 ($drive->{file} !~ m/^\d+$/)) {
1e3baf05
DM
766 my ($vtype, $volid) = PVE::Storage::path_to_volume_id($storecfg, $drive->{file});
767 raise_param_exc({ $opt => "unable to associate path '$drive->{file}' to any storage"}) if !$vtype;
768 $drive->{media} = 'cdrom' if !$drive->{media} && $vtype eq 'iso';
769 verify_media_type($opt, $vtype, $drive->{media});
770 $drive->{file} = $volid;
771 }
772
773 $drive->{media} = 'cdrom' if !$drive->{media} && $drive->{file} =~ m/^(cdrom|none)$/;
774}
775
776sub create_conf_nolock {
777 my ($vmid, $settings) = @_;
778
6b64503e 779 my $filename = config_file($vmid);
1e3baf05
DM
780
781 die "configuration file '$filename' already exists\n" if -f $filename;
19672434 782
1e3baf05
DM
783 my $defaults = load_defaults();
784
785 $settings->{name} = "vm$vmid" if !$settings->{name};
786 $settings->{memory} = $defaults->{memory} if !$settings->{memory};
787
788 my $data = '';
789 foreach my $opt (keys %$settings) {
790 next if !$confdesc->{$opt};
791
792 my $value = $settings->{$opt};
793 next if !$value;
794
795 $data .= "$opt: $value\n";
796 }
797
798 PVE::Tools::file_set_contents($filename, $data);
799}
800
801# ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]]
802# [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no]
803# [,aio=native|threads]
804
805sub parse_drive {
806 my ($key, $data) = @_;
807
808 my $res = {};
19672434 809
1e3baf05
DM
810 # $key may be undefined - used to verify JSON parameters
811 if (!defined($key)) {
812 $res->{interface} = 'unknown'; # should not harm when used to verify parameters
813 $res->{index} = 0;
814 } elsif ($key =~ m/^([^\d]+)(\d+)$/) {
815 $res->{interface} = $1;
816 $res->{index} = $2;
817 } else {
818 return undef;
819 }
820
821 foreach my $p (split (/,/, $data)) {
822 next if $p =~ m/^\s*$/;
823
824 if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio)=(.+)$/) {
825 my ($k, $v) = ($1, $2);
826
827 $k = 'file' if $k eq 'volume';
828
829 return undef if defined $res->{$k};
19672434 830
1e3baf05
DM
831 $res->{$k} = $v;
832 } else {
833 if (!$res->{file} && $p !~ m/=/) {
834 $res->{file} = $p;
835 } else {
836 return undef;
837 }
838 }
839 }
840
841 return undef if !$res->{file};
842
19672434 843 return undef if $res->{cache} &&
5534dd1a 844 $res->{cache} !~ m/^(off|none|writethrough|writeback|unsafe)$/;
1e3baf05
DM
845 return undef if $res->{snapshot} && $res->{snapshot} !~ m/^(on|off)$/;
846 return undef if $res->{cyls} && $res->{cyls} !~ m/^\d+$/;
847 return undef if $res->{heads} && $res->{heads} !~ m/^\d+$/;
848 return undef if $res->{secs} && $res->{secs} !~ m/^\d+$/;
849 return undef if $res->{media} && $res->{media} !~ m/^(disk|cdrom)$/;
850 return undef if $res->{trans} && $res->{trans} !~ m/^(none|lba|auto)$/;
851 return undef if $res->{format} && $res->{format} !~ m/^(raw|cow|qcow|qcow2|vmdk|cloop)$/;
852 return undef if $res->{rerror} && $res->{rerror} !~ m/^(ignore|report|stop)$/;
853 return undef if $res->{werror} && $res->{werror} !~ m/^(enospc|ignore|report|stop)$/;
854 return undef if $res->{backup} && $res->{backup} !~ m/^(yes|no)$/;
855 return undef if $res->{aio} && $res->{aio} !~ m/^(native|threads)$/;
856
857 if ($res->{media} && ($res->{media} eq 'cdrom')) {
858 return undef if $res->{snapshot} || $res->{trans} || $res->{format};
19672434 859 return undef if $res->{heads} || $res->{secs} || $res->{cyls};
1e3baf05
DM
860 return undef if $res->{interface} eq 'virtio';
861 }
862
863 # rerror does not work with scsi drives
864 if ($res->{rerror}) {
865 return undef if $res->{interface} eq 'scsi';
866 }
867
868 return $res;
869}
870
871my @qemu_drive_options = qw(heads secs cyls trans media format cache snapshot rerror werror aio);
872
873sub print_drive {
874 my ($vmid, $drive) = @_;
875
876 my $opts = '';
877 foreach my $o (@qemu_drive_options, 'backup') {
878 $opts .= ",$o=$drive->{$o}" if $drive->{$o};
879 }
880
881 return "$drive->{file}$opts";
882}
883
ca916ecc
DA
884sub print_drivedevice_full {
885 my ($storecfg, $vmid, $drive) = @_;
886
887 my $device = '';
888 my $maxdev = 0;
19672434 889
ca916ecc 890 if ($drive->{interface} eq 'virtio') {
2ed36a41
DM
891 my $pciaddr = print_pci_addr("$drive->{interface}$drive->{index}");
892 $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
893 } elsif ($drive->{interface} eq 'scsi') {
894 $maxdev = 7;
895 my $controller = int($drive->{index} / $maxdev);
896 my $unit = $drive->{index} % $maxdev;
897 my $devicetype = 'hd';
231f2e13
DA
898 my $path = '';
899 if (drive_is_cdrom($drive)) {
900 $devicetype = 'cd';
901 } else {
902 if ($drive->{file} =~ m|^/|) {
903 $path = $drive->{file};
904 } else {
905 $path = PVE::Storage::path($storecfg, $drive->{file});
906 }
907 if ($path =~ m|^/dev/| ) {
908 $devicetype = 'block';
909 }
910 }
ca916ecc 911
7ebe888a 912 $device = "scsi-$devicetype,bus=lsi$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
2ed36a41
DM
913 } elsif ($drive->{interface} eq 'ide'){
914 $maxdev = 2;
915 my $controller = int($drive->{index} / $maxdev);
916 my $unit = $drive->{index} % $maxdev;
917 my $devicetype = ($drive->{media} && $drive->{media} eq 'cdrom') ? "cd" : "hd";
918
7ebe888a 919 $device = "ide-$devicetype,bus=ide.$controller,unit=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
cdb0931f
DA
920 } elsif ($drive->{interface} eq 'sata'){
921 my $controller = int($drive->{index} / $MAX_SATA_DISKS);
922 my $unit = $drive->{index} % $MAX_SATA_DISKS;
923 $device = "ide-drive,bus=ahci$controller.$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
2ed36a41
DM
924 } elsif ($drive->{interface} eq 'usb') {
925 die "implement me";
926 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
927 } else {
928 die "unsupported interface type";
ca916ecc
DA
929 }
930
3b408e82
DM
931 $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex};
932
ca916ecc
DA
933 return $device;
934}
935
1e3baf05
DM
936sub print_drive_full {
937 my ($storecfg, $vmid, $drive) = @_;
938
939 my $opts = '';
940 foreach my $o (@qemu_drive_options) {
3b408e82 941 next if $o eq 'bootindex';
1e3baf05 942 $opts .= ",$o=$drive->{$o}" if $drive->{$o};
19672434 943 }
1e3baf05
DM
944
945 # use linux-aio by default (qemu default is threads)
19672434 946 $opts .= ",aio=native" if !$drive->{aio};
1e3baf05
DM
947
948 my $path;
949 my $volid = $drive->{file};
6b64503e
DM
950 if (drive_is_cdrom($drive)) {
951 $path = get_iso_path($storecfg, $vmid, $volid);
1e3baf05
DM
952 } else {
953 if ($volid =~ m|^/|) {
954 $path = $volid;
955 } else {
6b64503e 956 $path = PVE::Storage::path($storecfg, $volid);
1e3baf05 957 }
2b556977
DM
958 if (!$drive->{cache} && ($path =~ m|^/dev/| || $path =~ m|\.raw$|)) {
959 $opts .= ",cache=none";
960 }
1e3baf05
DM
961 }
962
963 my $pathinfo = $path ? "file=$path," : '';
964
3ebfcc86 965 return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
1e3baf05
DM
966}
967
cc4d6182
DA
968sub print_netdevice_full {
969 my ($vmid, $conf, $net, $netid) = @_;
970
971 my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
972
973 my $device = $net->{model};
974 if ($net->{model} eq 'virtio') {
975 $device = 'virtio-net-pci';
976 };
977
978 # qemu > 0.15 always try to boot from network - we disable that by
979 # not loading the pxe rom file
980 my $extra = ($bootorder !~ m/n/) ? "romfile=," : '';
981 my $pciaddr = print_pci_addr("$netid");
982 my $tmpstr = "$device,${extra}mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
983 $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex} ;
984 return $tmpstr;
985}
986
987sub print_netdev_full {
988 my ($vmid, $conf, $net, $netid) = @_;
989
990 my $i = '';
991 if ($netid =~ m/^net(\d+)$/) {
992 $i = int($1);
993 }
994
995 die "got strange net id '$i'\n" if $i >= ${MAX_NETS};
996
997 my $ifname = "tap${vmid}i$i";
998
999 # kvm uses TUNSETIFF ioctl, and that limits ifname length
1000 die "interface name '$ifname' is too long (max 15 character)\n"
1001 if length($ifname) >= 16;
1002
1003 my $vhostparam = '';
1004 $vhostparam = ',vhost=on' if $kernel_has_vhost_net && $net->{model} eq 'virtio';
1005
1006 my $vmname = $conf->{name} || "vm$vmid";
1007
1008 if ($net->{bridge}) {
1009 return "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/pve-bridge$vhostparam";
1010 } else {
1011 return "type=user,id=$netid,hostname=$vmname";
1012 }
1013}
1e3baf05
DM
1014
1015sub drive_is_cdrom {
1016 my ($drive) = @_;
1017
1018 return $drive && $drive->{media} && ($drive->{media} eq 'cdrom');
1019
1020}
1021
040b06b7
DA
1022sub parse_hostpci {
1023 my ($value) = @_;
1024
1025 return undef if !$value;
1026
1027 my $res = {};
1028
1029 if ($value =~ m/^[a-f0-9]{2}:[a-f0-9]{2}\.[a-f0-9]$/) {
1030 $res->{pciid} = $value;
1031 } else {
1032 return undef;
1033 }
1034
1035 return $res;
1036}
1037
1e3baf05
DM
1038# netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
1039sub parse_net {
1040 my ($data) = @_;
1041
1042 my $res = {};
1043
6b64503e 1044 foreach my $kvp (split(/,/, $data)) {
1e3baf05
DM
1045
1046 if ($kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i) {
6b64503e 1047 my $model = lc($1);
9f91ff02 1048 my $mac = uc($3) || PVE::Tools::random_ether_addr();
1e3baf05
DM
1049 $res->{model} = $model;
1050 $res->{macaddr} = $mac;
1051 } elsif ($kvp =~ m/^bridge=(\S+)$/) {
1052 $res->{bridge} = $1;
1053 } elsif ($kvp =~ m/^rate=(\d+(\.\d+)?)$/) {
1054 $res->{rate} = $1;
1055 } else {
1056 return undef;
1057 }
19672434 1058
1e3baf05
DM
1059 }
1060
1061 return undef if !$res->{model};
1062
1063 return $res;
1064}
1065
1066sub print_net {
1067 my $net = shift;
1068
1069 my $res = "$net->{model}";
1070 $res .= "=$net->{macaddr}" if $net->{macaddr};
1071 $res .= ",bridge=$net->{bridge}" if $net->{bridge};
1072 $res .= ",rate=$net->{rate}" if $net->{rate};
1073
1074 return $res;
1075}
1076
1077sub add_random_macs {
1078 my ($settings) = @_;
1079
1080 foreach my $opt (keys %$settings) {
1081 next if $opt !~ m/^net(\d+)$/;
1082 my $net = parse_net($settings->{$opt});
1083 next if !$net;
1084 $settings->{$opt} = print_net($net);
1085 }
1086}
1087
1088sub add_unused_volume {
1858638f 1089 my ($config, $volid) = @_;
1e3baf05
DM
1090
1091 my $key;
1092 for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
1093 my $test = "unused$ind";
1094 if (my $vid = $config->{$test}) {
1095 return if $vid eq $volid; # do not add duplicates
1096 } else {
1097 $key = $test;
19672434 1098 }
1e3baf05
DM
1099 }
1100
1101 die "To many unused volume - please delete them first.\n" if !$key;
1858638f
DM
1102
1103 $config->{$key} = $volid;
1e3baf05 1104
1858638f 1105 return $key;
1e3baf05
DM
1106}
1107
1108# fixme: remove all thos $noerr parameters?
1109
1110PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk);
1111sub verify_bootdisk {
1112 my ($value, $noerr) = @_;
1113
19672434 1114 return $value if valid_drivename($value);
1e3baf05
DM
1115
1116 return undef if $noerr;
1117
1118 die "invalid boot disk '$value'\n";
1119}
1120
1121PVE::JSONSchema::register_format('pve-qm-net', \&verify_net);
1122sub verify_net {
1123 my ($value, $noerr) = @_;
1124
1125 return $value if parse_net($value);
1126
1127 return undef if $noerr;
19672434 1128
1e3baf05
DM
1129 die "unable to parse network options\n";
1130}
1131
1132PVE::JSONSchema::register_format('pve-qm-drive', \&verify_drive);
1133sub verify_drive {
1134 my ($value, $noerr) = @_;
1135
6b64503e 1136 return $value if parse_drive(undef, $value);
1e3baf05
DM
1137
1138 return undef if $noerr;
19672434 1139
1e3baf05
DM
1140 die "unable to parse drive options\n";
1141}
1142
1143PVE::JSONSchema::register_format('pve-qm-hostpci', \&verify_hostpci);
1144sub verify_hostpci {
1145 my ($value, $noerr) = @_;
1146
040b06b7
DA
1147 return $value if parse_hostpci($value);
1148
1149 return undef if $noerr;
1150
1151 die "unable to parse pci id\n";
1e3baf05
DM
1152}
1153
0ea9541d
DM
1154PVE::JSONSchema::register_format('pve-qm-watchdog', \&verify_watchdog);
1155sub verify_watchdog {
1156 my ($value, $noerr) = @_;
1157
1158 return $value if parse_watchdog($value);
1159
1160 return undef if $noerr;
19672434 1161
0ea9541d
DM
1162 die "unable to parse watchdog options\n";
1163}
1164
1165sub parse_watchdog {
1166 my ($value) = @_;
1167
1168 return undef if !$value;
1169
1170 my $res = {};
1171
6b64503e 1172 foreach my $p (split(/,/, $value)) {
0ea9541d
DM
1173 next if $p =~ m/^\s*$/;
1174
1175 if ($p =~ m/^(model=)?(i6300esb|ib700)$/) {
1176 $res->{model} = $2;
1177 } elsif ($p =~ m/^(action=)?(reset|shutdown|poweroff|pause|debug|none)$/) {
1178 $res->{action} = $2;
1179 } else {
1180 return undef;
1181 }
1182 }
1183
1184 return $res;
1185}
1186
1e3baf05
DM
1187sub parse_usb_device {
1188 my ($value) = @_;
1189
1190 return undef if !$value;
1191
6b64503e 1192 my @dl = split(/,/, $value);
1e3baf05
DM
1193 my $found;
1194
1195 my $res = {};
1196 foreach my $v (@dl) {
1197 if ($v =~ m/^host=([0-9A-Fa-f]{4}):([0-9A-Fa-f]{4})$/) {
1198 $found = 1;
1199 $res->{vendorid} = $1;
1200 $res->{productid} = $2;
1201 } elsif ($v =~ m/^host=(\d+)\-(\d+(\.\d+)*)$/) {
1202 $found = 1;
1203 $res->{hostbus} = $1;
1204 $res->{hostport} = $2;
1205 } else {
1206 return undef;
1207 }
1208 }
1209 return undef if !$found;
1210
1211 return $res;
1212}
19672434 1213
1e3baf05
DM
1214PVE::JSONSchema::register_format('pve-qm-usb-device', \&verify_usb_device);
1215sub verify_usb_device {
1216 my ($value, $noerr) = @_;
1217
1218 return $value if parse_usb_device($value);
1219
1220 return undef if $noerr;
19672434 1221
1e3baf05
DM
1222 die "unable to parse usb device\n";
1223}
1224
1e3baf05
DM
1225# add JSON properties for create and set function
1226sub json_config_properties {
1227 my $prop = shift;
1228
1229 foreach my $opt (keys %$confdesc) {
1230 $prop->{$opt} = $confdesc->{$opt};
1231 }
1232
1233 return $prop;
1234}
1235
1236sub check_type {
1237 my ($key, $value) = @_;
1238
1239 die "unknown setting '$key'\n" if !$confdesc->{$key};
1240
1241 my $type = $confdesc->{$key}->{type};
1242
6b64503e 1243 if (!defined($value)) {
1e3baf05
DM
1244 die "got undefined value\n";
1245 }
1246
1247 if ($value =~ m/[\n\r]/) {
1248 die "property contains a line feed\n";
1249 }
1250
1251 if ($type eq 'boolean') {
19672434
DM
1252 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1253 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1254 die "type check ('boolean') failed - got '$value'\n";
1e3baf05
DM
1255 } elsif ($type eq 'integer') {
1256 return int($1) if $value =~ m/^(\d+)$/;
1257 die "type check ('integer') failed - got '$value'\n";
1258 } elsif ($type eq 'string') {
1259 if (my $fmt = $confdesc->{$key}->{format}) {
1260 if ($fmt eq 'pve-qm-drive') {
1261 # special case - we need to pass $key to parse_drive()
6b64503e 1262 my $drive = parse_drive($key, $value);
1e3baf05
DM
1263 return $value if $drive;
1264 die "unable to parse drive options\n";
1265 }
1266 PVE::JSONSchema::check_format($fmt, $value);
19672434
DM
1267 return $value;
1268 }
1e3baf05 1269 $value =~ s/^\"(.*)\"$/$1/;
19672434 1270 return $value;
1e3baf05
DM
1271 } else {
1272 die "internal error"
1273 }
1274}
1275
1276sub lock_config {
1277 my ($vmid, $code, @param) = @_;
1278
6b64503e 1279 my $filename = config_file_lock($vmid);
1e3baf05 1280
5fdbe4f0 1281 my $res = lock_file($filename, 10, $code, @param);
1e3baf05
DM
1282
1283 die $@ if $@;
5fdbe4f0
DM
1284
1285 return $res;
1e3baf05
DM
1286}
1287
1288sub cfs_config_path {
a78ccf26 1289 my ($vmid, $node) = @_;
1e3baf05 1290
a78ccf26
DM
1291 $node = $nodename if !$node;
1292 return "nodes/$node/qemu-server/$vmid.conf";
1e3baf05
DM
1293}
1294
040b06b7
DA
1295sub check_iommu_support{
1296 #fixme : need to check IOMMU support
1297 #http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
1298
1299 my $iommu=1;
1300 return $iommu;
1301
1302}
1303
1e3baf05 1304sub config_file {
a78ccf26 1305 my ($vmid, $node) = @_;
1e3baf05 1306
a78ccf26 1307 my $cfspath = cfs_config_path($vmid, $node);
1e3baf05
DM
1308 return "/etc/pve/$cfspath";
1309}
1310
1311sub config_file_lock {
1312 my ($vmid) = @_;
1313
1314 return "$lock_dir/lock-$vmid.conf";
1315}
1316
1317sub touch_config {
1318 my ($vmid) = @_;
1319
6b64503e 1320 my $conf = config_file($vmid);
1e3baf05
DM
1321 utime undef, undef, $conf;
1322}
1323
1e3baf05 1324sub destroy_vm {
a6af7b3e 1325 my ($storecfg, $vmid, $keep_empty_config) = @_;
1e3baf05 1326
6b64503e 1327 my $conffile = config_file($vmid);
1e3baf05 1328
6b64503e 1329 my $conf = load_config($vmid);
1e3baf05 1330
6b64503e 1331 check_lock($conf);
1e3baf05 1332
19672434 1333 # only remove disks owned by this VM
1e3baf05
DM
1334 foreach_drive($conf, sub {
1335 my ($ds, $drive) = @_;
1336
6b64503e 1337 return if drive_is_cdrom($drive);
1e3baf05
DM
1338
1339 my $volid = $drive->{file};
ff1a2432 1340 return if !$volid || $volid =~ m|^/|;
1e3baf05 1341
6b64503e 1342 my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
ff1a2432 1343 return if !$path || !$owner || ($owner != $vmid);
1e3baf05 1344
6b64503e 1345 PVE::Storage::vdisk_free($storecfg, $volid);
1e3baf05 1346 });
19672434 1347
a6af7b3e 1348 if ($keep_empty_config) {
9c502e26 1349 PVE::Tools::file_set_contents($conffile, "memory: 128\n");
a6af7b3e
DM
1350 } else {
1351 unlink $conffile;
1352 }
1e3baf05
DM
1353
1354 # also remove unused disk
1355 eval {
6b64503e 1356 my $dl = PVE::Storage::vdisk_list($storecfg, undef, $vmid);
1e3baf05
DM
1357
1358 eval {
6b64503e 1359 PVE::Storage::foreach_volid($dl, sub {
1e3baf05 1360 my ($volid, $sid, $volname, $d) = @_;
6b64503e 1361 PVE::Storage::vdisk_free($storecfg, $volid);
1e3baf05
DM
1362 });
1363 };
1364 warn $@ if $@;
1365
1366 };
1367 warn $@ if $@;
1368}
1369
1370# fixme: remove?
1371sub load_diskinfo_old {
1372 my ($storecfg, $vmid, $conf) = @_;
1373
1374 my $info = {};
1375 my $res = {};
1376 my $vollist;
1377
1378 foreach_drive($conf, sub {
1379 my ($ds, $di) = @_;
1380
1381 $res->{$ds} = $di;
1382
6b64503e 1383 return if drive_is_cdrom($di);
1e3baf05
DM
1384
1385 if ($di->{file} =~ m|^/dev/.+|) {
6b64503e 1386 $info->{$di->{file}}->{size} = PVE::Storage::file_size_info($di->{file});
1e3baf05
DM
1387 } else {
1388 push @$vollist, $di->{file};
1389 }
1390 });
1391
1392 eval {
6b64503e 1393 my $dl = PVE::Storage::vdisk_list($storecfg, undef, $vmid, $vollist);
1e3baf05 1394
6b64503e 1395 PVE::Storage::foreach_volid($dl, sub {
1e3baf05
DM
1396 my ($volid, $sid, $volname, $d) = @_;
1397 $info->{$volid} = $d;
1398 });
1399 };
1400 warn $@ if $@;
1401
1402 foreach my $ds (keys %$res) {
1403 my $di = $res->{$ds};
1404
19672434 1405 $res->{$ds}->{disksize} = $info->{$di->{file}} ?
1e3baf05
DM
1406 $info->{$di->{file}}->{size} / (1024*1024) : 0;
1407 }
1408
1409 return $res;
1410}
1411
1412sub load_config {
1413 my ($vmid) = @_;
1414
1415 my $cfspath = cfs_config_path($vmid);
1416
1417 my $conf = PVE::Cluster::cfs_read_file($cfspath);
1418
1419 die "no such VM ('$vmid')\n" if !defined($conf);
1420
1421 return $conf;
19672434 1422}
1e3baf05
DM
1423
1424sub parse_vm_config {
1425 my ($filename, $raw) = @_;
1426
1427 return undef if !defined($raw);
1428
554ac7e7
DM
1429 my $res = {
1430 digest => Digest::SHA1::sha1_hex($raw),
1431 };
1e3baf05 1432
19672434 1433 $filename =~ m|/qemu-server/(\d+)\.conf$|
1e3baf05
DM
1434 || die "got strange filename '$filename'";
1435
1436 my $vmid = $1;
1437
1438 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
1439 my $line = $1;
19672434 1440
1e3baf05
DM
1441 next if $line =~ m/^\#/;
1442
1443 next if $line =~ m/^\s*$/;
1444
1445 if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
1446 my $key = $1;
1447 my $value = PVE::Tools::decode_text($2);
1448 $res->{$key} = $value;
1449 } elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
1450 my $key = $1;
1451 my $value = $2;
1452 $res->{$key} = $value;
1453 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(\S+)\s*$/) {
1454 my $key = $1;
1455 my $value = $2;
1456 eval { $value = check_type($key, $value); };
1457 if ($@) {
1458 warn "vm $vmid - unable to parse value of '$key' - $@";
1459 } else {
1460 my $fmt = $confdesc->{$key}->{format};
1461 if ($fmt && $fmt eq 'pve-qm-drive') {
1462 my $v = parse_drive($key, $value);
1463 if (my $volid = filename_to_volume_id($vmid, $v->{file}, $v->{media})) {
1464 $v->{file} = $volid;
6b64503e 1465 $value = print_drive($vmid, $v);
1e3baf05
DM
1466 } else {
1467 warn "vm $vmid - unable to parse value of '$key'\n";
1468 next;
1469 }
1470 }
1471
1472 if ($key eq 'cdrom') {
1473 $res->{ide2} = $value;
1474 } else {
1475 $res->{$key} = $value;
1476 }
1477 }
1478 }
1479 }
1480
1481 # convert old smp to sockets
1482 if ($res->{smp} && !$res->{sockets}) {
1483 $res->{sockets} = $res->{smp};
19672434 1484 }
1e3baf05
DM
1485 delete $res->{smp};
1486
1487 return $res;
1488}
1489
1858638f
DM
1490sub write_vm_config {
1491 my ($filename, $conf) = @_;
1e3baf05 1492
1858638f
DM
1493 if ($conf->{cdrom}) {
1494 die "option ide2 conflicts with cdrom\n" if $conf->{ide2};
1495 $conf->{ide2} = $conf->{cdrom};
1496 delete $conf->{cdrom};
1497 }
1e3baf05
DM
1498
1499 # we do not use 'smp' any longer
1858638f
DM
1500 if ($conf->{sockets}) {
1501 delete $conf->{smp};
1502 } elsif ($conf->{smp}) {
1503 $conf->{sockets} = $conf->{smp};
1504 delete $conf->{cores};
1505 delete $conf->{smp};
1e3baf05
DM
1506 }
1507
1508 my $new_volids = {};
1858638f 1509 foreach my $key (keys %$conf) {
554ac7e7 1510 next if $key eq 'digest';
1858638f 1511 my $value = $conf->{$key};
1e3baf05
DM
1512 if ($key eq 'description') {
1513 $value = PVE::Tools::encode_text($value);
1514 }
1515 eval { $value = check_type($key, $value); };
1516 die "unable to parse value of '$key' - $@" if $@;
1858638f
DM
1517
1518 $conf->{$key} = $value;
1519
1e3baf05
DM
1520 if (valid_drivename($key)) {
1521 my $drive = PVE::QemuServer::parse_drive($key, $value);
1522 $new_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
1523 }
1524 }
1525
1858638f
DM
1526 # remove 'unusedX' settings if we re-add a volume
1527 foreach my $key (keys %$conf) {
1528 my $value = $conf->{$key};
1529 if ($key =~ m/^unused/ && $new_volids->{$value}) {
1530 delete $conf->{$key};
1e3baf05 1531 }
1858638f 1532 }
1e3baf05 1533
1858638f
DM
1534 # gererate RAW data
1535 my $raw = '';
1536 foreach my $key (sort keys %$conf) {
1537 next if $key eq 'digest';
1538 $raw .= "$key: $conf->{$key}\n";
1539 }
1e3baf05 1540
1858638f
DM
1541 return $raw;
1542}
1e3baf05 1543
1858638f
DM
1544sub update_config_nolock {
1545 my ($vmid, $conf, $skiplock) = @_;
1e3baf05 1546
1858638f
DM
1547 check_lock($conf) if !$skiplock;
1548
1549 my $cfspath = cfs_config_path($vmid);
1e3baf05 1550
1858638f
DM
1551 PVE::Cluster::cfs_write_file($cfspath, $conf);
1552}
1e3baf05 1553
1858638f
DM
1554sub update_config {
1555 my ($vmid, $conf, $skiplock) = @_;
1e3baf05 1556
1858638f 1557 lock_config($vmid, &update_config_nolock, $conf, $skiplock);
1e3baf05
DM
1558}
1559
19672434 1560sub load_defaults {
1e3baf05
DM
1561
1562 my $res = {};
1563
1564 # we use static defaults from our JSON schema configuration
1565 foreach my $key (keys %$confdesc) {
1566 if (defined(my $default = $confdesc->{$key}->{default})) {
1567 $res->{$key} = $default;
1568 }
1569 }
19672434 1570
1e3baf05
DM
1571 my $conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
1572 $res->{keyboard} = $conf->{keyboard} if $conf->{keyboard};
1573
1574 return $res;
1575}
1576
1577sub config_list {
1578 my $vmlist = PVE::Cluster::get_vmlist();
1579 my $res = {};
1580 return $res if !$vmlist || !$vmlist->{ids};
1581 my $ids = $vmlist->{ids};
1582
1e3baf05
DM
1583 foreach my $vmid (keys %$ids) {
1584 my $d = $ids->{$vmid};
1585 next if !$d->{node} || $d->{node} ne $nodename;
5ee957cc 1586 next if !$d->{type} || $d->{type} ne 'qemu';
1e3baf05
DM
1587 $res->{$vmid}->{exists} = 1;
1588 }
1589 return $res;
1590}
1591
64e13401
DM
1592# test if VM uses local resources (to prevent migration)
1593sub check_local_resources {
1594 my ($conf, $noerr) = @_;
1595
1596 my $loc_res = 0;
19672434 1597
e0ab7331
DM
1598 $loc_res = 1 if $conf->{hostusb}; # old syntax
1599 $loc_res = 1 if $conf->{hostpci}; # old syntax
64e13401 1600
0d29ab3b 1601 foreach my $k (keys %$conf) {
2fe1a152 1602 $loc_res = 1 if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
64e13401
DM
1603 }
1604
1605 die "VM uses local resources\n" if $loc_res && !$noerr;
1606
1607 return $loc_res;
1608}
1609
1e3baf05
DM
1610sub check_lock {
1611 my ($conf) = @_;
1612
1613 die "VM is locked ($conf->{lock})\n" if $conf->{lock};
1614}
1615
1616sub check_cmdline {
1617 my ($pidfile, $pid) = @_;
1618
6b64503e
DM
1619 my $fh = IO::File->new("/proc/$pid/cmdline", "r");
1620 if (defined($fh)) {
1e3baf05
DM
1621 my $line = <$fh>;
1622 $fh->close;
1623 return undef if !$line;
6b64503e 1624 my @param = split(/\0/, $line);
1e3baf05
DM
1625
1626 my $cmd = $param[0];
1627 return if !$cmd || ($cmd !~ m|kvm$|);
1628
1629 for (my $i = 0; $i < scalar (@param); $i++) {
1630 my $p = $param[$i];
1631 next if !$p;
1632 if (($p eq '-pidfile') || ($p eq '--pidfile')) {
1633 my $p = $param[$i+1];
1634 return 1 if $p && ($p eq $pidfile);
1635 return undef;
1636 }
1637 }
1638 }
1639 return undef;
1640}
1641
1642sub check_running {
e6c3b671 1643 my ($vmid, $nocheck) = @_;
1e3baf05 1644
e6c3b671 1645 my $filename = config_file($vmid);
1e3baf05
DM
1646
1647 die "unable to find configuration file for VM $vmid - no such machine\n"
e6c3b671 1648 if !$nocheck && ! -f $filename;
1e3baf05 1649
e6c3b671 1650 my $pidfile = pidfile_name($vmid);
1e3baf05 1651
e6c3b671
DM
1652 if (my $fd = IO::File->new("<$pidfile")) {
1653 my $st = stat($fd);
1e3baf05 1654 my $line = <$fd>;
6b64503e 1655 close($fd);
1e3baf05
DM
1656
1657 my $mtime = $st->mtime;
1658 if ($mtime > time()) {
1659 warn "file '$filename' modified in future\n";
1660 }
1661
1662 if ($line =~ m/^(\d+)$/) {
1663 my $pid = $1;
e6c3b671
DM
1664 if (check_cmdline($pidfile, $pid)) {
1665 if (my $pinfo = PVE::ProcFSTools::check_process_running($pid)) {
1666 return $pid;
1667 }
1668 }
1e3baf05
DM
1669 }
1670 }
1671
1672 return undef;
1673}
1674
1675sub vzlist {
19672434 1676
1e3baf05
DM
1677 my $vzlist = config_list();
1678
6b64503e 1679 my $fd = IO::Dir->new($var_run_tmpdir) || return $vzlist;
1e3baf05 1680
19672434 1681 while (defined(my $de = $fd->read)) {
1e3baf05
DM
1682 next if $de !~ m/^(\d+)\.pid$/;
1683 my $vmid = $1;
6b64503e
DM
1684 next if !defined($vzlist->{$vmid});
1685 if (my $pid = check_running($vmid)) {
1e3baf05
DM
1686 $vzlist->{$vmid}->{pid} = $pid;
1687 }
1688 }
1689
1690 return $vzlist;
1691}
1692
1693my $storage_timeout_hash = {};
1694
1695sub disksize {
1696 my ($storecfg, $conf) = @_;
1697
1698 my $bootdisk = $conf->{bootdisk};
1699 return undef if !$bootdisk;
1700 return undef if !valid_drivename($bootdisk);
1701
1702 return undef if !$conf->{$bootdisk};
1703
1704 my $drive = parse_drive($bootdisk, $conf->{$bootdisk});
1705 return undef if !defined($drive);
1706
1707 return undef if drive_is_cdrom($drive);
1708
1709 my $volid = $drive->{file};
1710 return undef if !$volid;
1711
1712 my $path;
1713 my $storeid;
1714 my $timeoutid;
1715
1716 if ($volid =~ m|^/|) {
1717 $path = $timeoutid = $volid;
1718 } else {
f0cab979
DM
1719 eval {
1720 $storeid = $timeoutid = PVE::Storage::parse_volume_id($volid);
1721 $path = PVE::Storage::path($storecfg, $volid);
1722 };
1723 if (my $err = $@) {
1724 warn $err;
1725 return undef;
1726 }
1e3baf05
DM
1727 }
1728
1729 my $last_timeout = $storage_timeout_hash->{$timeoutid};
1730 if ($last_timeout) {
1731 if ((time() - $last_timeout) < 30) {
1732 # skip storage with errors
1733 return undef ;
1734 }
1735 delete $storage_timeout_hash->{$timeoutid};
1736 }
1737
1738 my ($size, $format, $used);
1739
1740 ($size, $format, $used) = PVE::Storage::file_size_info($path, 1);
1741
1742 if (!defined($format)) {
1743 # got timeout
1744 $storage_timeout_hash->{$timeoutid} = time();
1745 return undef;
1746 }
1747
1748 return wantarray ? ($size, $used) : $size;
1749}
1750
1751my $last_proc_pid_stat;
1752
1753sub vmstatus {
1754 my ($opt_vmid) = @_;
1755
1756 my $res = {};
1757
19672434 1758 my $storecfg = PVE::Storage::config();
1e3baf05
DM
1759
1760 my $list = vzlist();
694fcad4 1761 my ($uptime) = PVE::ProcFSTools::read_proc_uptime(1);
1e3baf05 1762
ae4915a2
DM
1763 my $cpucount = $cpuinfo->{cpus} || 1;
1764
1e3baf05
DM
1765 foreach my $vmid (keys %$list) {
1766 next if $opt_vmid && ($vmid ne $opt_vmid);
1767
1768 my $cfspath = cfs_config_path($vmid);
1769 my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
1770
1771 my $d = {};
1772 $d->{pid} = $list->{$vmid}->{pid};
1773
1774 # fixme: better status?
1775 $d->{status} = $list->{$vmid}->{pid} ? 'running' : 'stopped';
1776
1777 my ($size, $used) = disksize($storecfg, $conf);
1778 if (defined($size) && defined($used)) {
1779 $d->{disk} = $used;
1780 $d->{maxdisk} = $size;
1781 } else {
1782 $d->{disk} = 0;
1783 $d->{maxdisk} = 0;
1784 }
1785
1786 $d->{cpus} = ($conf->{sockets} || 1) * ($conf->{cores} || 1);
ae4915a2
DM
1787 $d->{cpus} = $cpucount if $d->{cpus} > $cpucount;
1788
1e3baf05 1789 $d->{name} = $conf->{name} || "VM $vmid";
19672434 1790 $d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024) : 0;
1e3baf05 1791
1e3baf05
DM
1792 $d->{uptime} = 0;
1793 $d->{cpu} = 0;
1e3baf05
DM
1794 $d->{mem} = 0;
1795
1796 $d->{netout} = 0;
1797 $d->{netin} = 0;
1798
1799 $d->{diskread} = 0;
1800 $d->{diskwrite} = 0;
1801
1802 $res->{$vmid} = $d;
1803 }
1804
1805 my $netdev = PVE::ProcFSTools::read_proc_net_dev();
1806 foreach my $dev (keys %$netdev) {
1807 next if $dev !~ m/^tap([1-9]\d*)i/;
1808 my $vmid = $1;
1809 my $d = $res->{$vmid};
1810 next if !$d;
19672434 1811
1e3baf05
DM
1812 $d->{netout} += $netdev->{$dev}->{receive};
1813 $d->{netin} += $netdev->{$dev}->{transmit};
1814 }
1815
1e3baf05
DM
1816 my $ctime = gettimeofday;
1817
1818 foreach my $vmid (keys %$list) {
1819
1820 my $d = $res->{$vmid};
1821 my $pid = $d->{pid};
1822 next if !$pid;
1823
1824 if (my $fh = IO::File->new("/proc/$pid/io", "r")) {
1825 my $data = {};
6b64503e 1826 while (defined(my $line = <$fh>)) {
1e3baf05
DM
1827 if ($line =~ m/^([rw]char):\s+(\d+)$/) {
1828 $data->{$1} = $2;
1829 }
1830 }
1831 close($fh);
1832 $d->{diskread} = $data->{rchar} || 0;
1833 $d->{diskwrite} = $data->{wchar} || 0;
1834 }
1835
694fcad4
DM
1836 my $pstat = PVE::ProcFSTools::read_proc_pid_stat($pid);
1837 next if !$pstat; # not running
19672434 1838
694fcad4 1839 my $used = $pstat->{utime} + $pstat->{stime};
1e3baf05 1840
694fcad4 1841 $d->{uptime} = int(($uptime - $pstat->{starttime})/$cpuinfo->{user_hz});
1e3baf05 1842
694fcad4 1843 if ($pstat->{vsize}) {
6b64503e 1844 $d->{mem} = int(($pstat->{rss}/$pstat->{vsize})*$d->{maxmem});
1e3baf05
DM
1845 }
1846
1847 my $old = $last_proc_pid_stat->{$pid};
1848 if (!$old) {
19672434
DM
1849 $last_proc_pid_stat->{$pid} = {
1850 time => $ctime,
1e3baf05
DM
1851 used => $used,
1852 cpu => 0,
1e3baf05
DM
1853 };
1854 next;
1855 }
1856
7f0b5beb 1857 my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz};
1e3baf05
DM
1858
1859 if ($dtime > 1000) {
1860 my $dutime = $used - $old->{used};
1861
ae4915a2 1862 $d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus};
1e3baf05 1863 $last_proc_pid_stat->{$pid} = {
19672434 1864 time => $ctime,
1e3baf05
DM
1865 used => $used,
1866 cpu => $d->{cpu},
1e3baf05
DM
1867 };
1868 } else {
1869 $d->{cpu} = $old->{cpu};
1e3baf05
DM
1870 }
1871 }
1872
1873 return $res;
1874}
1875
1876sub foreach_drive {
1877 my ($conf, $func) = @_;
1878
1879 foreach my $ds (keys %$conf) {
1880 next if !valid_drivename($ds);
1881
6b64503e 1882 my $drive = parse_drive($ds, $conf->{$ds});
1e3baf05
DM
1883 next if !$drive;
1884
1885 &$func($ds, $drive);
1886 }
1887}
1888
1889sub config_to_command {
1890 my ($storecfg, $vmid, $conf, $defaults, $migrate_uri) = @_;
1891
1892 my $cmd = [];
b78ebef7 1893 my $pciaddr = '';
1e3baf05
DM
1894 my $kvmver = kvm_user_version();
1895 my $vernum = 0; # unknown
a3c52213
DM
1896 if ($kvmver =~ m/^(\d+)\.(\d+)$/) {
1897 $vernum = $1*1000000+$2*1000;
1898 } elsif ($kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/) {
1e3baf05
DM
1899 $vernum = $1*1000000+$2*1000+$3;
1900 }
1901
a3c52213 1902 die "detected old qemu-kvm binary ($kvmver)\n" if $vernum < 15000;
1e3baf05
DM
1903
1904 my $have_ovz = -f '/proc/vz/vestat';
1905
1906 push @$cmd, '/usr/bin/kvm';
1907
1908 push @$cmd, '-id', $vmid;
1909
1910 my $use_virtio = 0;
1911
6b64503e 1912 my $socket = monitor_socket($vmid);
abb39b66
DA
1913 push @$cmd, '-chardev', "socket,id=monitor,path=$socket,server,nowait";
1914 push @$cmd, '-mon', "chardev=monitor,mode=readline";
1e3baf05 1915
6b64503e 1916 $socket = vnc_socket($vmid);
1e3baf05
DM
1917 push @$cmd, '-vnc', "unix:$socket,x509,password";
1918
6b64503e 1919 push @$cmd, '-pidfile' , pidfile_name($vmid);
19672434 1920
1e3baf05
DM
1921 push @$cmd, '-daemonize';
1922
1923 push @$cmd, '-incoming', $migrate_uri if $migrate_uri;
1924
1c060867
DM
1925 my $use_usb2 = 0;
1926 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
1927 next if !$conf->{"usb$i"};
1928 $use_usb2 = 1;
1929 }
1e3baf05 1930 # include usb device config
1c060867 1931 push @$cmd, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
19672434 1932
1e3baf05 1933 # enable absolute mouse coordinates (needed by vnc)
6b64503e 1934 my $tablet = defined($conf->{tablet}) ? $conf->{tablet} : $defaults->{tablet};
1c060867
DM
1935 if ($tablet) {
1936 if ($use_usb2) {
1937 push @$cmd, '-device', 'usb-tablet,bus=ehci.0,port=6';
1938 } else {
1939 push @$cmd, '-usbdevice', 'tablet';
1940 }
1941 }
1e3baf05
DM
1942
1943 # host pci devices
040b06b7
DA
1944 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
1945 my $d = parse_hostpci($conf->{"hostpci$i"});
1946 next if !$d;
b78ebef7
DA
1947 $pciaddr = print_pci_addr("hostpci$i");
1948 push @$cmd, '-device', "pci-assign,host=$d->{pciid},id=hostpci$i$pciaddr";
1e3baf05
DM
1949 }
1950
1951 # usb devices
1952 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
1953 my $d = parse_usb_device($conf->{"usb$i"});
1954 next if !$d;
1955 if ($d->{vendorid} && $d->{productid}) {
1956 push @$cmd, '-device', "usb-host,vendorid=$d->{vendorid},productid=$d->{productid}";
1957 } elsif (defined($d->{hostbus}) && defined($d->{hostport})) {
1958 push @$cmd, '-device', "usb-host,hostbus=$d->{hostbus},hostport=$d->{hostport}";
1959 }
1960 }
1961
1e3baf05 1962 # serial devices
bae179aa 1963 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
34978be3 1964 if (my $path = $conf->{"serial$i"}) {
19672434 1965 die "no such serial device\n" if ! -c $path;
34978be3
DM
1966 push @$cmd, '-chardev', "tty,id=serial$i,path=$path";
1967 push @$cmd, '-device', "isa-serial,chardev=serial$i";
1968 }
1e3baf05
DM
1969 }
1970
1971 # parallel devices
1989a89c 1972 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
34978be3 1973 if (my $path = $conf->{"parallel$i"}) {
19672434 1974 die "no such parallel device\n" if ! -c $path;
34978be3
DM
1975 push @$cmd, '-chardev', "parport,id=parallel$i,path=$path";
1976 push @$cmd, '-device', "isa-parallel,chardev=parallel$i";
1977 }
1e3baf05
DM
1978 }
1979
1980 my $vmname = $conf->{name} || "vm$vmid";
1981
1982 push @$cmd, '-name', $vmname;
19672434 1983
1e3baf05
DM
1984 my $sockets = 1;
1985 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
1986 $sockets = $conf->{sockets} if $conf->{sockets};
1987
1988 my $cores = $conf->{cores} || 1;
1989
1e3baf05
DM
1990 push @$cmd, '-smp', "sockets=$sockets,cores=$cores";
1991
1992 push @$cmd, '-cpu', $conf->{cpu} if $conf->{cpu};
1993
1e3baf05
DM
1994 push @$cmd, '-nodefaults';
1995
32baffb4 1996 my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
3b408e82 1997
0888fdce
DM
1998 my $bootindex_hash = {};
1999 my $i = 1;
2000 foreach my $o (split(//, $bootorder)) {
2001 $bootindex_hash->{$o} = $i*100;
2002 $i++;
afdb31d5 2003 }
3b408e82
DM
2004
2005 push @$cmd, '-boot', "menu=on";
1e3baf05 2006
6b64503e 2007 push @$cmd, '-no-acpi' if defined($conf->{acpi}) && $conf->{acpi} == 0;
1e3baf05 2008
6b64503e 2009 push @$cmd, '-no-reboot' if defined($conf->{reboot}) && $conf->{reboot} == 0;
1e3baf05
DM
2010
2011 my $vga = $conf->{vga};
2012 if (!$vga) {
2013 if ($conf->{ostype} && ($conf->{ostype} eq 'win7' || $conf->{ostype} eq 'w2k8')) {
2014 $vga = 'std';
2015 } else {
2016 $vga = 'cirrus';
2017 }
2018 }
19672434 2019
1e3baf05
DM
2020 push @$cmd, '-vga', $vga if $vga; # for kvm 77 and later
2021
2022 # time drift fix
6b64503e 2023 my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
1e3baf05
DM
2024 push @$cmd, '-tdf' if $tdf;
2025
6b64503e 2026 my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
1e3baf05
DM
2027
2028 if (my $ost = $conf->{ostype}) {
2029 # other, wxp, w2k, w2k3, w2k8, wvista, win7, l24, l26
2030
2031 if ($ost =~ m/^w/) { # windows
6b64503e 2032 push @$cmd, '-localtime' if !defined($conf->{localtime});
1e3baf05
DM
2033
2034 # use rtc-td-hack when acpi is enabled
6b64503e 2035 if (!(defined($conf->{acpi}) && $conf->{acpi} == 0)) {
1e3baf05
DM
2036 push @$cmd, '-rtc-td-hack';
2037 }
2038 }
2039
2040 # -tdf ?
19672434
DM
2041 # -no-acpi
2042 # -no-kvm
1e3baf05
DM
2043 # -win2k-hack ?
2044 }
2045
7f0b5beb
DM
2046 if ($nokvm) {
2047 push @$cmd, '-no-kvm';
2048 } else {
2049 die "No accelerator found!\n" if !$cpuinfo->{hvm};
2050 }
1e3baf05
DM
2051
2052 push @$cmd, '-localtime' if $conf->{localtime};
2053
2054 push @$cmd, '-startdate', $conf->{startdate} if $conf->{startdate};
2055
2056 push @$cmd, '-S' if $conf->{freeze};
2057
2058 # set keyboard layout
2059 my $kb = $conf->{keyboard} || $defaults->{keyboard};
2060 push @$cmd, '-k', $kb if $kb;
2061
2062 # enable sound
2063 #my $soundhw = $conf->{soundhw} || $defaults->{soundhw};
2064 #push @$cmd, '-soundhw', 'es1370';
2065 #push @$cmd, '-soundhw', $soundhw if $soundhw;
b78ebef7 2066 $pciaddr = print_pci_addr("balloon0");
13b5a753 2067 push @$cmd, '-device', "virtio-balloon-pci,id=balloon0$pciaddr" if $conf->{balloon};
1e3baf05 2068
0ea9541d
DM
2069 if ($conf->{watchdog}) {
2070 my $wdopts = parse_watchdog($conf->{watchdog});
0a40e8ea
DA
2071 $pciaddr = print_pci_addr("watchdog");
2072 my $watchdog = $wdopts->{model} || 'i6300esb';
2073 push @$cmd, '-device', "$watchdog$pciaddr";
0ea9541d
DM
2074 push @$cmd, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
2075 }
2076
1e3baf05 2077 my $vollist = [];
941e0c42 2078 my $scsicontroller = {};
26ee04b6 2079 my $ahcicontroller = {};
1e3baf05
DM
2080
2081 foreach_drive($conf, sub {
2082 my ($ds, $drive) = @_;
2083
ff1a2432 2084 if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
1e3baf05 2085 push @$vollist, $drive->{file};
ff1a2432 2086 }
afdb31d5 2087
1e3baf05 2088 $use_virtio = 1 if $ds =~ m/^virtio/;
3b408e82
DM
2089
2090 if (drive_is_cdrom ($drive)) {
2091 if ($bootindex_hash->{d}) {
2092 $drive->{bootindex} = $bootindex_hash->{d};
2093 $bootindex_hash->{d} += 1;
2094 }
2095 } else {
2096 if ($bootindex_hash->{c}) {
2097 $drive->{bootindex} = $bootindex_hash->{c} if $conf->{bootdisk} && ($conf->{bootdisk} eq $ds);
2098 $bootindex_hash->{c} += 1;
2099 }
2100 }
2101
941e0c42
DA
2102 if ($drive->{interface} eq 'scsi') {
2103 my $maxdev = 7;
6b64503e 2104 my $controller = int($drive->{index} / $maxdev);
69b6ae0c
DA
2105 $pciaddr = print_pci_addr("lsi$controller");
2106 push @$cmd, '-device', "lsi,id=lsi$controller$pciaddr" if !$scsicontroller->{$controller};
58dc808d 2107 $scsicontroller->{$controller}=1;
941e0c42 2108 }
3b408e82 2109
26ee04b6
DA
2110 if ($drive->{interface} eq 'sata') {
2111 my $controller = int($drive->{index} / $MAX_SATA_DISKS);
2112 $pciaddr = print_pci_addr("ahci$controller");
2113 push @$cmd, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr" if !$ahcicontroller->{$controller};
2114 $ahcicontroller->{$controller}=1;
2115 }
2116
2117 push @$cmd, '-drive',print_drive_full($storecfg, $vmid, $drive);
6b64503e 2118 push @$cmd, '-device',print_drivedevice_full($storecfg,$vmid, $drive);
1e3baf05
DM
2119 });
2120
2121 push @$cmd, '-m', $conf->{memory} || $defaults->{memory};
19672434 2122
cc4d6182 2123 for (my $i = 0; $i < $MAX_NETS; $i++) {
5f0c4c32 2124 next if !$conf->{"net$i"};
cc4d6182
DA
2125 my $d = parse_net($conf->{"net$i"});
2126 next if !$d;
1e3baf05 2127
cc4d6182 2128 $use_virtio = 1 if $d->{model} eq 'virtio';
1e3baf05 2129
cc4d6182
DA
2130 if ($bootindex_hash->{n}) {
2131 $d->{bootindex} = $bootindex_hash->{n};
2132 $bootindex_hash->{n} += 1;
2133 }
1e3baf05 2134
cc4d6182
DA
2135 my $netdevfull = print_netdev_full($vmid,$conf,$d,"net$i");
2136 push @$cmd, '-netdev', $netdevfull;
1e3baf05 2137
cc4d6182
DA
2138 my $netdevicefull = print_netdevice_full($vmid,$conf,$d,"net$i");
2139 push @$cmd, '-device', $netdevicefull;
19672434
DM
2140 }
2141
1e3baf05
DM
2142
2143 # hack: virtio with fairsched is unreliable, so we do not use fairsched
2144 # when the VM uses virtio devices.
19672434
DM
2145 if (!$use_virtio && $have_ovz) {
2146
6b64503e 2147 my $cpuunits = defined($conf->{cpuunits}) ?
1e3baf05
DM
2148 $conf->{cpuunits} : $defaults->{cpuunits};
2149
2150 push @$cmd, '-cpuunits', $cpuunits if $cpuunits;
2151
2152 # fixme: cpulimit is currently ignored
2153 #push @$cmd, '-cpulimit', $conf->{cpulimit} if $conf->{cpulimit};
2154 }
2155
2156 # add custom args
2157 if ($conf->{args}) {
3ada46c9 2158 my $aa = PVE::Tools::split_args($conf->{args});
1e3baf05
DM
2159 push @$cmd, @$aa;
2160 }
2161
2162 return wantarray ? ($cmd, $vollist) : $cmd;
2163}
19672434 2164
1e3baf05
DM
2165sub vnc_socket {
2166 my ($vmid) = @_;
2167 return "${var_run_tmpdir}/$vmid.vnc";
2168}
2169
2170sub monitor_socket {
2171 my ($vmid) = @_;
2172 return "${var_run_tmpdir}/$vmid.mon";
2173}
2174
2175sub pidfile_name {
2176 my ($vmid) = @_;
2177 return "${var_run_tmpdir}/$vmid.pid";
2178}
2179
1e3baf05
DM
2180sub next_migrate_port {
2181
2182 for (my $p = 60000; $p < 60010; $p++) {
2183
6b64503e
DM
2184 my $sock = IO::Socket::INET->new(Listen => 5,
2185 LocalAddr => 'localhost',
2186 LocalPort => $p,
2187 ReuseAddr => 1,
2188 Proto => 0);
1e3baf05
DM
2189
2190 if ($sock) {
6b64503e 2191 close($sock);
1e3baf05
DM
2192 return $p;
2193 }
2194 }
2195
2196 die "unable to find free migration port";
2197}
2198
86fdcfb2
DA
2199sub vm_devices_list {
2200 my ($vmid) = @_;
2201
ff1a2432 2202 my $res = vm_monitor_command ($vmid, "info pci");
1dc4f496
DM
2203
2204 my @lines = split ("\n", $res);
2205 my $devices;
2206 my $bus;
2207 my $addr;
2208 my $id;
afdb31d5 2209
1dc4f496
DM
2210 foreach my $line (@lines) {
2211 $line =~ s/^\s+//;
2212 if ($line =~ m/^Bus (\d+), device (\d+), function (\d+):$/) {
2213 $bus=$1;
2214 $addr=$2;
2215 }
2216 if ($line =~ m/^id "([a-z][a-z_\-]*\d*)"$/) {
86fdcfb2
DA
2217 $id=$1;
2218 $devices->{$id}->{bus}=$bus;
2219 $devices->{$id}->{addr}=$addr;
1dc4f496
DM
2220 }
2221 }
86fdcfb2 2222
1dc4f496 2223 return $devices;
86fdcfb2
DA
2224}
2225
ec21aa11 2226sub vm_deviceplug {
f19d1c47 2227 my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
ae57f6b3 2228
2630d2a9 2229 return 1 if !check_running($vmid) || !$conf->{hotplug};
afdb31d5 2230
95d6343b
DA
2231 my $devices_list = vm_devices_list($vmid);
2232 return 1 if defined($devices_list->{$deviceid});
2233
5e5dcb73
DA
2234 if ($deviceid =~ m/^(virtio)(\d+)$/) {
2235 return undef if !qemu_driveadd($storecfg, $vmid, $device);
1dc4f496 2236 my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
5e5dcb73
DA
2237 qemu_deviceadd($vmid, $devicefull);
2238 if(!qemu_deviceaddverify($vmid, $deviceid)) {
2239 qemu_drivedel($vmid, $deviceid);
2240 return undef;
2241 }
f19d1c47 2242 }
cfc817c7
DA
2243
2244 if ($deviceid =~ m/^(lsi)(\d+)$/) {
2245 my $pciaddr = print_pci_addr($deviceid);
2246 my $devicefull = "lsi,id=$deviceid$pciaddr";
2247 qemu_deviceadd($vmid, $devicefull);
2248 return undef if(!qemu_deviceaddverify($vmid, $deviceid));
2249 }
2250
a4f091a0
DA
2251 if ($deviceid =~ m/^(scsi)(\d+)$/) {
2252 return undef if !qemu_findorcreatelsi($storecfg,$conf, $vmid, $device);
2253 return undef if !qemu_driveadd($storecfg, $vmid, $device);
2254 my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
2255 if(!qemu_deviceadd($vmid, $devicefull)) {
2256 qemu_drivedel($vmid, $deviceid);
2257 return undef;
2258 }
2259 }
2260
2630d2a9
DA
2261 if ($deviceid =~ m/^(net)(\d+)$/) {
2262 return undef if !qemu_netdevadd($vmid, $conf, $device, $deviceid);
2263 my $netdevicefull = print_netdevice_full($vmid, $conf, $device, $deviceid);
2264 qemu_deviceadd($vmid, $netdevicefull);
2265 if(!qemu_deviceaddverify($vmid, $deviceid)) {
2266 qemu_netdevdel($vmid, $deviceid);
2267 return undef;
2268 }
2269 }
2270
5e5dcb73 2271 return 1;
a4dea331
DA
2272}
2273
ec21aa11 2274sub vm_deviceunplug {
f19d1c47 2275 my ($vmid, $conf, $deviceid) = @_;
873c2d69 2276
5e5dcb73 2277 return 1 if !check_running ($vmid) || !$conf->{hotplug};
873c2d69 2278
95d6343b
DA
2279 my $devices_list = vm_devices_list($vmid);
2280 return 1 if !defined($devices_list->{$deviceid});
2281
ae57f6b3 2282 die "can't unplug bootdisk" if $conf->{bootdisk} && $conf->{bootdisk} eq $deviceid;
f19d1c47 2283
5e5dcb73
DA
2284 if ($deviceid =~ m/^(virtio)(\d+)$/) {
2285 return undef if !qemu_drivedel($vmid, $deviceid);
2286 qemu_devicedel($vmid, $deviceid);
2287 return undef if !qemu_devicedelverify($vmid, $deviceid);
2288 }
cfc817c7
DA
2289
2290 if ($deviceid =~ m/^(lsi)(\d+)$/) {
2291 return undef if !qemu_devicedel($vmid, $deviceid);
2292 }
2293
a4f091a0
DA
2294 if ($deviceid =~ m/^(scsi)(\d+)$/) {
2295 return undef if !qemu_devicedel($vmid, $deviceid);
2296 return undef if !qemu_drivedel($vmid, $deviceid);
2297 }
2298
2630d2a9
DA
2299 if ($deviceid =~ m/^(net)(\d+)$/) {
2300 return undef if !qemu_netdevdel($vmid, $deviceid);
2301 qemu_devicedel($vmid, $deviceid);
2302 return undef if !qemu_devicedelverify($vmid, $deviceid);
2303 }
2304
5e5dcb73
DA
2305 return 1;
2306}
2307
2308sub qemu_deviceadd {
2309 my ($vmid, $devicefull) = @_;
873c2d69 2310
5e5dcb73
DA
2311 my $ret = vm_monitor_command($vmid, "device_add $devicefull");
2312 $ret =~ s/^\s+//;
afdb31d5 2313 # Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
5e5dcb73
DA
2314 return 1 if $ret eq "";
2315 syslog("err", "error on hotplug device : $ret");
2316 return undef;
f19d1c47 2317
5e5dcb73 2318}
afdb31d5 2319
5e5dcb73
DA
2320sub qemu_devicedel {
2321 my($vmid, $deviceid) = @_;
f19d1c47 2322
5e5dcb73
DA
2323 my $ret = vm_monitor_command($vmid, "device_del $deviceid");
2324 $ret =~ s/^\s+//;
2325 return 1 if $ret eq "";
2326 syslog("err", "detaching device $deviceid failed : $ret");
2327 return undef;
2328}
2329
2330sub qemu_driveadd {
2331 my($storecfg, $vmid, $device) = @_;
2332
2333 my $drive = print_drive_full($storecfg, $vmid, $device);
2334 my $ret = vm_monitor_command($vmid, "drive_add auto $drive");
2335 # If the command succeeds qemu prints: "OK"
2336 if ($ret !~ m/OK/s) {
2337 syslog("err", "adding drive failed: $ret");
2338 return undef;
f19d1c47 2339 }
5e5dcb73
DA
2340 return 1;
2341}
afdb31d5 2342
5e5dcb73
DA
2343sub qemu_drivedel {
2344 my($vmid, $deviceid) = @_;
873c2d69 2345
5e5dcb73
DA
2346 my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid");
2347 $ret =~ s/^\s+//;
2348 if ($ret =~ m/Device \'.*?\' not found/s) {
afdb31d5 2349 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
5e5dcb73
DA
2350 }
2351 elsif ($ret ne "") {
2352 syslog("err", "deleting drive $deviceid failed : $ret");
2353 return undef;
873c2d69 2354 }
5e5dcb73
DA
2355 return 1;
2356}
f19d1c47 2357
5e5dcb73
DA
2358sub qemu_deviceaddverify {
2359 my ($vmid,$deviceid) = @_;
873c2d69 2360
5e5dcb73
DA
2361 for (my $i = 0; $i <= 5; $i++) {
2362 my $devices_list = vm_devices_list($vmid);
2363 return 1 if defined($devices_list->{$deviceid});
2364 sleep 1;
afdb31d5 2365 }
5e5dcb73
DA
2366 syslog("err", "error on hotplug device $deviceid");
2367 return undef;
2368}
afdb31d5 2369
5e5dcb73
DA
2370
2371sub qemu_devicedelverify {
2372 my ($vmid,$deviceid) = @_;
2373
2374 #need to verify the device is correctly remove as device_del is async and empty return is not reliable
2375 for (my $i = 0; $i <= 5; $i++) {
2376 my $devices_list = vm_devices_list($vmid);
2377 return 1 if !defined($devices_list->{$deviceid});
2378 sleep 1;
afdb31d5 2379 }
5e5dcb73
DA
2380 syslog("err", "error on hot-unplugging device $deviceid");
2381 return undef;
873c2d69
DA
2382}
2383
cfc817c7
DA
2384sub qemu_findorcreatelsi {
2385 my ($storecfg, $conf, $vmid, $device) = @_;
2386
2387 my $maxdev = 7;
2388 my $controller = int($device->{index} / $maxdev);
2389 my $lsiid="lsi$controller";
2390 my $devices_list = vm_devices_list($vmid);
2391
2392 if(!defined($devices_list->{$lsiid})) {
2393 return undef if !vm_deviceplug($storecfg, $conf, $vmid, $lsiid);
2394 }
2395 return 1;
2396}
2397
2630d2a9
DA
2398sub qemu_netdevadd {
2399 my ($vmid, $conf, $device, $deviceid) = @_;
2400
2401 my $netdev = print_netdev_full($vmid, $conf, $device, $deviceid);
2402 my $ret = vm_monitor_command($vmid, "netdev_add $netdev");
2403 $ret =~ s/^\s+//;
2404
2405 #if the command succeeds, no output is sent. So any non-empty string shows an error
2406 return 1 if $ret eq "";
2407 syslog("err", "adding netdev failed: $ret");
2408 return undef;
2409}
2410
2411sub qemu_netdevdel {
2412 my ($vmid, $deviceid) = @_;
2413
2414 my $ret = vm_monitor_command($vmid, "netdev_del $deviceid");
2415 $ret =~ s/^\s+//;
2416 #if the command succeeds, no output is sent. So any non-empty string shows an error
2417 return 1 if $ret eq "";
2418 syslog("err", "deleting netdev failed: $ret");
2419 return undef;
2420}
2421
1e3baf05
DM
2422sub vm_start {
2423 my ($storecfg, $vmid, $statefile, $skiplock) = @_;
2424
6b64503e
DM
2425 lock_config($vmid, sub {
2426 my $conf = load_config($vmid);
1e3baf05 2427
6b64503e 2428 check_lock($conf) if !$skiplock;
1e3baf05 2429
ff1a2432 2430 die "VM $vmid already running\n" if check_running($vmid);
1e3baf05
DM
2431
2432 my $migrate_uri;
2433 my $migrate_port = 0;
2434
2435 if ($statefile) {
2436 if ($statefile eq 'tcp') {
2437 $migrate_port = next_migrate_port();
2438 $migrate_uri = "tcp:localhost:${migrate_port}";
2439 } else {
2440 if (-f $statefile) {
2441 $migrate_uri = "exec:cat $statefile";
2442 } else {
2443 warn "state file '$statefile' does not exist - doing normal startup\n";
2444 }
2445 }
2446 }
2447
2448 my $defaults = load_defaults();
2449
e6c3b671 2450 my ($cmd, $vollist) = config_to_command($storecfg, $vmid, $conf, $defaults, $migrate_uri);
1e3baf05 2451 # host pci devices
040b06b7
DA
2452 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
2453 my $d = parse_hostpci($conf->{"hostpci$i"});
2454 next if !$d;
2455 my $info = pci_device_info("0000:$d->{pciid}");
2456 die "IOMMU not present\n" if !check_iommu_support();
2457 die "no pci device info for device '$d->{pciid}'\n" if !$info;
2458 die "can't unbind pci device '$d->{pciid}'\n" if !pci_dev_bind_to_stub($info);
2459 die "can't reset pci device '$d->{pciid}'\n" if !pci_dev_reset($info);
2460 }
1e3baf05
DM
2461
2462 PVE::Storage::activate_volumes($storecfg, $vollist);
2463
6b64503e 2464 eval { run_command($cmd, timeout => $migrate_uri ? undef : 30); };
1e3baf05 2465 my $err = $@;
ff1a2432 2466 die "start failed: $err" if $err;
1e3baf05
DM
2467
2468 if ($statefile) {
2469
2470 if ($statefile eq 'tcp') {
2471 print "migration listens on port $migrate_port\n";
2472 } else {
2473 unlink $statefile;
2474 # fixme: send resume - is that necessary ?
ff1a2432 2475 eval { vm_monitor_command($vmid, "cont"); };
1e3baf05
DM
2476 }
2477 }
afdb31d5 2478
48e1a963
DM
2479 # always set migrate speed (overwrite kvm default of 32m)
2480 # we set a very hight default of 8192m which is basically unlimited
2481 my $migrate_speed = $defaults->{migrate_speed} || 8192;
2482 $migrate_speed = $conf->{migrate_speed} || $migrate_speed;
afdb31d5 2483 eval {
1e3baf05 2484 my $cmd = "migrate_set_speed ${migrate_speed}m";
afdb31d5 2485 vm_monitor_command($vmid, $cmd);
48e1a963 2486 };
1e3baf05 2487
19672434 2488 if (my $migrate_downtime =
1e3baf05
DM
2489 $conf->{migrate_downtime} || $defaults->{migrate_downtime}) {
2490 my $cmd = "migrate_set_downtime ${migrate_downtime}";
ff1a2432 2491 eval { vm_monitor_command($vmid, $cmd); };
1e3baf05 2492 }
e6c3b671 2493
7878afeb 2494 vm_balloonset($vmid, $conf->{balloon}) if $conf->{balloon};
1e3baf05
DM
2495 });
2496}
2497
2498sub __read_avail {
2499 my ($fh, $timeout) = @_;
2500
2501 my $sel = new IO::Select;
6b64503e 2502 $sel->add($fh);
1e3baf05
DM
2503
2504 my $res = '';
2505 my $buf;
2506
2507 my @ready;
6b64503e 2508 while (scalar (@ready = $sel->can_read($timeout))) {
1e3baf05 2509 my $count;
6b64503e 2510 if ($count = $fh->sysread($buf, 8192)) {
1e3baf05
DM
2511 if ($buf =~ /^(.*)\(qemu\) $/s) {
2512 $res .= $1;
2513 last;
2514 } else {
2515 $res .= $buf;
2516 }
2517 } else {
6b64503e 2518 if (!defined($count)) {
1e3baf05
DM
2519 die "$!\n";
2520 }
2521 last;
2522 }
2523 }
2524
6b64503e 2525 die "monitor read timeout\n" if !scalar(@ready);
1e3baf05
DM
2526
2527 return $res;
2528}
2529
2530sub vm_monitor_command {
ff1a2432 2531 my ($vmid, $cmdstr, $nocheck) = @_;
1e3baf05
DM
2532
2533 my $res;
2534
1e3baf05 2535 eval {
ff1a2432 2536 die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
1e3baf05 2537
e6c3b671 2538 my $sname = monitor_socket($vmid);
1e3baf05 2539
6b64503e 2540 my $sock = IO::Socket::UNIX->new( Peer => $sname ) ||
1e3baf05
DM
2541 die "unable to connect to VM $vmid socket - $!\n";
2542
2543 my $timeout = 3;
2544
19672434 2545 # hack: migrate sometime blocks the monitor (when migrate_downtime
1e3baf05
DM
2546 # is set)
2547 if ($cmdstr =~ m/^(info\s+migrate|migrate\s)/) {
2548 $timeout = 60*60; # 1 hour
2549 }
2550
2551 # read banner;
6b64503e 2552 my $data = __read_avail($sock, $timeout);
19672434 2553
1e3baf05
DM
2554 if ($data !~ m/^QEMU\s+(\S+)\s+monitor\s/) {
2555 die "got unexpected qemu monitor banner\n";
2556 }
2557
2558 my $sel = new IO::Select;
6b64503e 2559 $sel->add($sock);
1e3baf05 2560
6b64503e 2561 if (!scalar(my @ready = $sel->can_write($timeout))) {
1e3baf05
DM
2562 die "monitor write error - timeout";
2563 }
2564
2565 my $fullcmd = "$cmdstr\r";
2566
ae57f6b3
DM
2567 # syslog('info', "VM $vmid monitor command: $cmdstr");
2568
1e3baf05 2569 my $b;
6b64503e 2570 if (!($b = $sock->syswrite($fullcmd)) || ($b != length($fullcmd))) {
1e3baf05
DM
2571 die "monitor write error - $!";
2572 }
2573
2574 return if ($cmdstr eq 'q') || ($cmdstr eq 'quit');
2575
19672434 2576 $timeout = 20;
1e3baf05
DM
2577
2578 if ($cmdstr =~ m/^(info\s+migrate|migrate\s)/) {
2579 $timeout = 60*60; # 1 hour
2580 } elsif ($cmdstr =~ m/^(eject|change)/) {
2581 $timeout = 60; # note: cdrom mount command is slow
2582 }
6b64503e 2583 if ($res = __read_avail($sock, $timeout)) {
19672434 2584
6b64503e 2585 my @lines = split("\r?\n", $res);
1e3baf05
DM
2586
2587 shift @lines if $lines[0] !~ m/^unknown command/; # skip echo
19672434 2588
6b64503e 2589 $res = join("\n", @lines);
1e3baf05
DM
2590 $res .= "\n";
2591 }
2592 };
2593
2594 my $err = $@;
2595
2596 if ($err) {
6b64503e 2597 syslog("err", "VM $vmid monitor command failed - $err");
1e3baf05
DM
2598 die $err;
2599 }
2600
2601 return $res;
2602}
2603
2604sub vm_commandline {
2605 my ($storecfg, $vmid) = @_;
2606
6b64503e 2607 my $conf = load_config($vmid);
1e3baf05
DM
2608
2609 my $defaults = load_defaults();
2610
6b64503e 2611 my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults);
1e3baf05 2612
6b64503e 2613 return join(' ', @$cmd);
1e3baf05
DM
2614}
2615
2616sub vm_reset {
2617 my ($vmid, $skiplock) = @_;
2618
6b64503e 2619 lock_config($vmid, sub {
1e3baf05 2620
6b64503e 2621 my $conf = load_config($vmid);
1e3baf05 2622
6b64503e 2623 check_lock($conf) if !$skiplock;
1e3baf05 2624
ff1a2432
DM
2625 vm_monitor_command($vmid, "system_reset");
2626 });
2627}
2628
2629sub get_vm_volumes {
2630 my ($conf) = @_;
1e3baf05 2631
ff1a2432
DM
2632 my $vollist = [];
2633 foreach_drive($conf, sub {
2634 my ($ds, $drive) = @_;
2635
2636 my ($sid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
2637 return if !$sid;
2638
2639 my $volid = $drive->{file};
2640 return if !$volid || $volid =~ m|^/|;
2641
2642 push @$vollist, $volid;
1e3baf05 2643 });
ff1a2432
DM
2644
2645 return $vollist;
2646}
2647
2648sub vm_stop_cleanup {
254575e9 2649 my ($storecfg, $vmid, $conf, $keepActive) = @_;
ff1a2432 2650
745fed70
DM
2651 eval {
2652 fairsched_rmnod($vmid); # try to destroy group
ff1a2432 2653
254575e9
DM
2654 if (!$keepActive) {
2655 my $vollist = get_vm_volumes($conf);
2656 PVE::Storage::deactivate_volumes($storecfg, $vollist);
2657 }
745fed70
DM
2658 };
2659 warn $@ if $@; # avoid errors - just warn
1e3baf05
DM
2660}
2661
e6c3b671 2662# Note: use $nockeck to skip tests if VM configuration file exists.
254575e9
DM
2663# We need that when migration VMs to other nodes (files already moved)
2664# Note: we set $keepActive in vzdump stop mode - volumes need to stay active
1e3baf05 2665sub vm_stop {
254575e9 2666 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive) = @_;
9269013a
DM
2667
2668 $timeout = 60 if !defined($timeout);
ff1a2432 2669
9269013a 2670 $force = 1 if !defined($force) && !$shutdown;
1e3baf05 2671
e6c3b671 2672 lock_config($vmid, sub {
1e3baf05 2673
e6c3b671 2674 my $pid = check_running($vmid, $nocheck);
ff1a2432 2675 return if !$pid;
1e3baf05 2676
ff1a2432 2677 my $conf;
e6c3b671 2678 if (!$nocheck) {
ff1a2432 2679 $conf = load_config($vmid);
e6c3b671
DM
2680 check_lock($conf) if !$skiplock;
2681 }
19672434 2682
9269013a
DM
2683 eval {
2684 if ($shutdown) {
2685 vm_monitor_command($vmid, "system_powerdown", $nocheck);
2686 } else {
2687 vm_monitor_command($vmid, "quit", $nocheck);
afdb31d5 2688 }
9269013a 2689 };
1e3baf05
DM
2690 my $err = $@;
2691
2692 if (!$err) {
1e3baf05 2693 my $count = 0;
e6c3b671 2694 while (($count < $timeout) && check_running($vmid, $nocheck)) {
1e3baf05
DM
2695 $count++;
2696 sleep 1;
2697 }
2698
2699 if ($count >= $timeout) {
9269013a
DM
2700 if ($force) {
2701 warn "VM still running - terminating now with SIGTERM\n";
2702 kill 15, $pid;
2703 } else {
2704 die "VM quit/powerdown failed - got timeout\n";
2705 }
2706 } else {
254575e9 2707 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive) if $conf;
9269013a 2708 return;
1e3baf05
DM
2709 }
2710 } else {
9269013a
DM
2711 if ($force) {
2712 warn "VM quit/powerdown failed - terminating now with SIGTERM\n";
2713 kill 15, $pid;
2714 } else {
afdb31d5 2715 die "VM quit/powerdown failed\n";
9269013a 2716 }
1e3baf05
DM
2717 }
2718
2719 # wait again
ff1a2432 2720 $timeout = 10;
1e3baf05
DM
2721
2722 my $count = 0;
e6c3b671 2723 while (($count < $timeout) && check_running($vmid, $nocheck)) {
1e3baf05
DM
2724 $count++;
2725 sleep 1;
2726 }
2727
2728 if ($count >= $timeout) {
ff1a2432 2729 warn "VM still running - terminating now with SIGKILL\n";
1e3baf05 2730 kill 9, $pid;
ff1a2432 2731 sleep 1;
1e3baf05
DM
2732 }
2733
254575e9 2734 vm_stop_cleanup($storecfg, $vmid, $conf, $keepActive) if $conf;
ff1a2432 2735 });
1e3baf05
DM
2736}
2737
2738sub vm_suspend {
2739 my ($vmid, $skiplock) = @_;
2740
6b64503e 2741 lock_config($vmid, sub {
1e3baf05 2742
6b64503e 2743 my $conf = load_config($vmid);
1e3baf05 2744
6b64503e 2745 check_lock($conf) if !$skiplock;
1e3baf05 2746
ff1a2432 2747 vm_monitor_command($vmid, "stop");
1e3baf05
DM
2748 });
2749}
2750
2751sub vm_resume {
2752 my ($vmid, $skiplock) = @_;
2753
6b64503e 2754 lock_config($vmid, sub {
1e3baf05 2755
6b64503e 2756 my $conf = load_config($vmid);
1e3baf05 2757
6b64503e 2758 check_lock($conf) if !$skiplock;
1e3baf05 2759
ff1a2432 2760 vm_monitor_command($vmid, "cont");
1e3baf05
DM
2761 });
2762}
2763
5fdbe4f0
DM
2764sub vm_sendkey {
2765 my ($vmid, $skiplock, $key) = @_;
1e3baf05 2766
6b64503e 2767 lock_config($vmid, sub {
1e3baf05 2768
6b64503e 2769 my $conf = load_config($vmid);
1e3baf05 2770
ff1a2432 2771 vm_monitor_command($vmid, "sendkey $key");
1e3baf05
DM
2772 });
2773}
2774
2775sub vm_destroy {
2776 my ($storecfg, $vmid, $skiplock) = @_;
2777
6b64503e 2778 lock_config($vmid, sub {
1e3baf05 2779
6b64503e 2780 my $conf = load_config($vmid);
1e3baf05 2781
6b64503e 2782 check_lock($conf) if !$skiplock;
1e3baf05 2783
ff1a2432
DM
2784 if (!check_running($vmid)) {
2785 fairsched_rmnod($vmid); # try to destroy group
2786 destroy_vm($storecfg, $vmid);
2787 } else {
2788 die "VM $vmid is running - destroy failed\n";
1e3baf05
DM
2789 }
2790 });
2791}
2792
2793sub vm_stopall {
ff1a2432 2794 my ($storecfg, $timeout) = @_;
1e3baf05
DM
2795
2796 $timeout = 3*60 if !$timeout;
2797
ff1a2432
DM
2798 my $cleanuphash = {};
2799
1e3baf05
DM
2800 my $vzlist = vzlist();
2801 my $count = 0;
2802 foreach my $vmid (keys %$vzlist) {
2803 next if !$vzlist->{$vmid}->{pid};
2804 $count++;
ff1a2432 2805 $cleanuphash->{$vmid} = 1;
1e3baf05
DM
2806 }
2807
ff1a2432 2808 return if !$count;
1e3baf05 2809
ff1a2432
DM
2810 my $msg = "Stopping Qemu Server - sending shutdown requests to all VMs\n";
2811 syslog('info', $msg);
2812 warn $msg;
1e3baf05 2813
ff1a2432
DM
2814 foreach my $vmid (keys %$vzlist) {
2815 next if !$vzlist->{$vmid}->{pid};
bbbe5146
DM
2816 eval { vm_monitor_command($vmid, "system_powerdown"); };
2817 warn $@ if $@;
ff1a2432 2818 }
1e3baf05 2819
ff1a2432
DM
2820 my $wt = 5;
2821 my $maxtries = int(($timeout + $wt -1)/$wt);
2822 my $try = 0;
2823 while (($try < $maxtries) && $count) {
2824 $try++;
2825 sleep $wt;
afdb31d5 2826
ff1a2432
DM
2827 $vzlist = vzlist();
2828 $count = 0;
2829 foreach my $vmid (keys %$vzlist) {
2830 next if !$vzlist->{$vmid}->{pid};
2831 $count++;
1e3baf05 2832 }
ff1a2432
DM
2833 last if !$count;
2834 }
1e3baf05 2835
ff1a2432 2836 if ($count) {
1e3baf05
DM
2837
2838 foreach my $vmid (keys %$vzlist) {
2839 next if !$vzlist->{$vmid}->{pid};
19672434 2840
ff1a2432
DM
2841 warn "VM $vmid still running - sending stop now\n";
2842 eval { vm_monitor_command($vmid, "quit"); };
2843 warn $@ if $@;
1e3baf05
DM
2844 }
2845
2846 $timeout = 30;
6b64503e 2847 $maxtries = int(($timeout + $wt -1)/$wt);
1e3baf05
DM
2848 $try = 0;
2849 while (($try < $maxtries) && $count) {
2850 $try++;
2851 sleep $wt;
afdb31d5 2852
1e3baf05
DM
2853 $vzlist = vzlist();
2854 $count = 0;
2855 foreach my $vmid (keys %$vzlist) {
2856 next if !$vzlist->{$vmid}->{pid};
2857 $count++;
2858 }
2859 last if !$count;
2860 }
2861
ff1a2432 2862 if ($count) {
1e3baf05 2863
ff1a2432
DM
2864 foreach my $vmid (keys %$vzlist) {
2865 next if !$vzlist->{$vmid}->{pid};
19672434 2866
ff1a2432
DM
2867 warn "VM $vmid still running - terminating now with SIGTERM\n";
2868 kill 15, $vzlist->{$vmid}->{pid};
2869 }
2870 sleep 1;
1e3baf05
DM
2871 }
2872
2873 # this is called by system shotdown scripts, so remaining
2874 # processes gets killed anyways (no need to send kill -9 here)
ff1a2432 2875 }
1e3baf05 2876
ff1a2432
DM
2877 $vzlist = vzlist();
2878 foreach my $vmid (keys %$cleanuphash) {
2879 next if $vzlist->{$vmid}->{pid};
afdb31d5 2880 eval {
ff1a2432 2881 my $conf = load_config($vmid);
afdb31d5 2882 vm_stop_cleanup($storecfg, $vmid, $conf);
ff1a2432
DM
2883 };
2884 warn $@ if $@;
1e3baf05 2885 }
ff1a2432
DM
2886
2887 $msg = "Qemu Server stopped\n";
2888 syslog('info', $msg);
2889 print $msg;
1e3baf05
DM
2890}
2891
2892# pci helpers
2893
2894sub file_write {
2895 my ($filename, $buf) = @_;
2896
6b64503e 2897 my $fh = IO::File->new($filename, "w");
1e3baf05
DM
2898 return undef if !$fh;
2899
2900 my $res = print $fh $buf;
2901
2902 $fh->close();
2903
2904 return $res;
2905}
2906
2907sub pci_device_info {
2908 my ($name) = @_;
2909
2910 my $res;
2911
2912 return undef if $name !~ m/^([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])$/;
2913 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
2914
2915 my $irq = file_read_firstline("$pcisysfs/devices/$name/irq");
2916 return undef if !defined($irq) || $irq !~ m/^\d+$/;
2917
2918 my $vendor = file_read_firstline("$pcisysfs/devices/$name/vendor");
2919 return undef if !defined($vendor) || $vendor !~ s/^0x//;
2920
2921 my $product = file_read_firstline("$pcisysfs/devices/$name/device");
2922 return undef if !defined($product) || $product !~ s/^0x//;
2923
2924 $res = {
2925 name => $name,
2926 vendor => $vendor,
2927 product => $product,
2928 domain => $domain,
2929 bus => $bus,
2930 slot => $slot,
2931 func => $func,
2932 irq => $irq,
2933 has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0,
2934 };
2935
2936 return $res;
2937}
2938
2939sub pci_dev_reset {
2940 my ($dev) = @_;
2941
2942 my $name = $dev->{name};
2943
2944 my $fn = "$pcisysfs/devices/$name/reset";
2945
6b64503e 2946 return file_write($fn, "1");
1e3baf05
DM
2947}
2948
2949sub pci_dev_bind_to_stub {
2950 my ($dev) = @_;
2951
2952 my $name = $dev->{name};
2953
2954 my $testdir = "$pcisysfs/drivers/pci-stub/$name";
2955 return 1 if -d $testdir;
2956
2957 my $data = "$dev->{vendor} $dev->{product}";
6b64503e 2958 return undef if !file_write("$pcisysfs/drivers/pci-stub/new_id", $data);
1e3baf05
DM
2959
2960 my $fn = "$pcisysfs/devices/$name/driver/unbind";
6b64503e 2961 if (!file_write($fn, $name)) {
1e3baf05
DM
2962 return undef if -f $fn;
2963 }
2964
2965 $fn = "$pcisysfs/drivers/pci-stub/bind";
2966 if (! -d $testdir) {
6b64503e 2967 return undef if !file_write($fn, $name);
1e3baf05
DM
2968 }
2969
2970 return -d $testdir;
2971}
2972
afdb31d5 2973sub print_pci_addr {
72a063e4 2974 my ($id) = @_;
6b64503e 2975
72a063e4 2976 my $res = '';
6b64503e 2977 my $devices = {
e5f7f8ed
DA
2978 #addr1 : ide,parallel,serial (motherboard)
2979 #addr2 : first videocard
13b5a753 2980 balloon0 => { bus => 0, addr => 3 },
0a40e8ea 2981 watchdog => { bus => 0, addr => 4 },
69b6ae0c
DA
2982 lsi0 => { bus => 0, addr => 5 },
2983 lsi1 => { bus => 0, addr => 6 },
26ee04b6 2984 ahci0 => { bus => 0, addr => 7 },
6b64503e
DM
2985 virtio0 => { bus => 0, addr => 10 },
2986 virtio1 => { bus => 0, addr => 11 },
2987 virtio2 => { bus => 0, addr => 12 },
2988 virtio3 => { bus => 0, addr => 13 },
2989 virtio4 => { bus => 0, addr => 14 },
2990 virtio5 => { bus => 0, addr => 15 },
b78ebef7
DA
2991 hostpci0 => { bus => 0, addr => 16 },
2992 hostpci1 => { bus => 0, addr => 17 },
f290f8d9
DA
2993 net0 => { bus => 0, addr => 18 },
2994 net1 => { bus => 0, addr => 19 },
2995 net2 => { bus => 0, addr => 20 },
2996 net3 => { bus => 0, addr => 21 },
2997 net4 => { bus => 0, addr => 22 },
2998 net5 => { bus => 0, addr => 23 },
e5f7f8ed 2999 #addr29 : usb-host (pve-usb.cfg)
6b64503e
DM
3000 };
3001
3002 if (defined($devices->{$id}->{bus}) && defined($devices->{$id}->{addr})) {
72a063e4
DA
3003 my $addr = sprintf("0x%x", $devices->{$id}->{addr});
3004 $res = ",bus=pci.$devices->{$id}->{bus},addr=$addr";
3005 }
3006 return $res;
3007
3008}
3009
13a48620 3010sub vm_balloonset {
7878afeb 3011 my ($vmid, $value) = @_;
13a48620 3012
ff1a2432 3013 vm_monitor_command($vmid, "balloon $value");
13a48620
DA
3014}
3015
3e16d5fc
DM
3016# vzdump restore implementaion
3017
3018sub archive_read_firstfile {
3019 my $archive = shift;
afdb31d5 3020
3e16d5fc
DM
3021 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
3022
3023 # try to detect archive type first
3024 my $pid = open (TMP, "tar tf '$archive'|") ||
3025 die "unable to open file '$archive'\n";
3026 my $firstfile = <TMP>;
3027 kill 15, $pid;
3028 close TMP;
3029
3030 die "ERROR: archive contaions no data\n" if !$firstfile;
3031 chomp $firstfile;
3032
3033 return $firstfile;
3034}
3035
3036sub restore_cleanup {
3037 my $statfile = shift;
3038
3039 print STDERR "starting cleanup\n";
3040
3041 if (my $fd = IO::File->new($statfile, "r")) {
3042 while (defined(my $line = <$fd>)) {
3043 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
3044 my $volid = $2;
3045 eval {
3046 if ($volid =~ m|^/|) {
3047 unlink $volid || die 'unlink failed\n';
3048 } else {
3049 my $cfg = cfs_read_file('storage.cfg');
3050 PVE::Storage::vdisk_free($cfg, $volid);
3051 }
afdb31d5 3052 print STDERR "temporary volume '$volid' sucessfuly removed\n";
3e16d5fc
DM
3053 };
3054 print STDERR "unable to cleanup '$volid' - $@" if $@;
3055 } else {
3056 print STDERR "unable to parse line in statfile - $line";
afdb31d5 3057 }
3e16d5fc
DM
3058 }
3059 $fd->close();
3060 }
3061}
3062
3063sub restore_archive {
a0d1b1a2 3064 my ($archive, $vmid, $user, $opts) = @_;
3e16d5fc 3065
9c502e26
DM
3066 if ($archive ne '-') {
3067 my $firstfile = archive_read_firstfile($archive);
3068 die "ERROR: file '$archive' dos not lock like a QemuServer vzdump backup\n"
3069 if $firstfile ne 'qemu-server.conf';
3070 }
3e16d5fc
DM
3071
3072 my $tocmd = "/usr/lib/qemu-server/qmextract";
3073
2415a446 3074 $tocmd .= " --storage " . PVE::Tools::shellquote($opts->{storage}) if $opts->{storage};
a0d1b1a2 3075 $tocmd .= " --pool " . PVE::Tools::shellquote($opts->{pool}) if $opts->{pool};
3e16d5fc
DM
3076 $tocmd .= ' --prealloc' if $opts->{prealloc};
3077 $tocmd .= ' --info' if $opts->{info};
3078
a0d1b1a2 3079 # tar option "xf" does not autodetect compression when read from STDIN,
9c502e26 3080 # so we pipe to zcat
2415a446
DM
3081 my $cmd = "zcat -f|tar xf " . PVE::Tools::shellquote($archive) . " " .
3082 PVE::Tools::shellquote("--to-command=$tocmd");
3e16d5fc
DM
3083
3084 my $tmpdir = "/var/tmp/vzdumptmp$$";
3085 mkpath $tmpdir;
3086
3087 local $ENV{VZDUMP_TMPDIR} = $tmpdir;
3088 local $ENV{VZDUMP_VMID} = $vmid;
a0d1b1a2 3089 local $ENV{VZDUMP_USER} = $user;
3e16d5fc
DM
3090
3091 my $conffile = PVE::QemuServer::config_file($vmid);
3092 my $tmpfn = "$conffile.$$.tmp";
3093
3094 # disable interrupts (always do cleanups)
3095 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub {
3096 print STDERR "got interrupt - ignored\n";
3097 };
3098
afdb31d5 3099 eval {
3e16d5fc
DM
3100 # enable interrupts
3101 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
3102 die "interrupted by signal\n";
3103 };
3104
9c502e26
DM
3105 if ($archive eq '-') {
3106 print "extracting archive from STDIN\n";
3107 run_command($cmd, input => "<&STDIN");
3108 } else {
3109 print "extracting archive '$archive'\n";
3110 run_command($cmd);
3111 }
3e16d5fc
DM
3112
3113 return if $opts->{info};
3114
3115 # read new mapping
3116 my $map = {};
3117 my $statfile = "$tmpdir/qmrestore.stat";
3118 if (my $fd = IO::File->new($statfile, "r")) {
3119 while (defined (my $line = <$fd>)) {
3120 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
3121 $map->{$1} = $2 if $1;
3122 } else {
3123 print STDERR "unable to parse line in statfile - $line\n";
3124 }
3125 }
3126 $fd->close();
3127 }
3128
3129 my $confsrc = "$tmpdir/qemu-server.conf";
3130
3131 my $srcfd = new IO::File($confsrc, "r") ||
3132 die "unable to open file '$confsrc'\n";
3133
3134 my $outfd = new IO::File ($tmpfn, "w") ||
3135 die "unable to write config for VM $vmid\n";
3136
51586c3a
DM
3137 my $netcount = 0;
3138
3e16d5fc
DM
3139 while (defined (my $line = <$srcfd>)) {
3140 next if $line =~ m/^\#vzdump\#/;
3141 next if $line =~ m/^lock:/;
3142 next if $line =~ m/^unused\d+:/;
3143
51586c3a
DM
3144 if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
3145 # try to convert old 1.X settings
3146 my ($id, $ind, $ethcfg) = ($1, $2, $3);
3147 foreach my $devconfig (PVE::Tools::split_list($ethcfg)) {
3148 my ($model, $macaddr) = split(/\=/, $devconfig);
3149 $macaddr = PVE::Tools::random_ether_addr() if !$macaddr || $opts->{unique};
3150 my $net = {
3151 model => $model,
3152 bridge => "vmbr$ind",
3153 macaddr => $macaddr,
3154 };
3155 my $netstr = print_net($net);
3156 print $outfd "net${netcount}: $netstr\n";
3157 $netcount++;
3158 }
3159 } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && ($opts->{unique})) {
3160 my ($id, $netstr) = ($1, $2);
3161 my $net = parse_net($netstr);
3162 $net->{macaddr} = PVE::Tools::random_ether_addr() if $net->{macaddr};
3163 $netstr = print_net($net);
afdb31d5 3164 print $outfd "$id: $netstr\n";
51586c3a 3165 } elsif ($line =~ m/^((ide|scsi|virtio)\d+):\s*(\S+)\s*$/) {
3e16d5fc
DM
3166 my $virtdev = $1;
3167 my $value = $2;
3168 if ($line =~ m/backup=no/) {
3169 print $outfd "#$line";
3170 } elsif ($virtdev && $map->{$virtdev}) {
3171 my $di = PVE::QemuServer::parse_drive($virtdev, $value);
3172 $di->{file} = $map->{$virtdev};
3173 $value = PVE::QemuServer::print_drive($vmid, $di);
3174 print $outfd "$virtdev: $value\n";
3175 } else {
3176 print $outfd $line;
3177 }
3178 } else {
3179 print $outfd $line;
3180 }
3181 }
3182
3183 $srcfd->close();
3184 $outfd->close();
3185 };
3186 my $err = $@;
3187
afdb31d5 3188 if ($err) {
3e16d5fc
DM
3189
3190 unlink $tmpfn;
3191
3192 restore_cleanup("$tmpdir/qmrestore.stat") if !$opts->{info};
afdb31d5 3193
3e16d5fc 3194 die $err;
afdb31d5 3195 }
3e16d5fc
DM
3196
3197 rmtree $tmpdir;
3198
3199 rename $tmpfn, $conffile ||
3200 die "unable to commit configuration file '$conffile'\n";
3201};
3202
1e3baf05 32031;