]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer.pm
1 package PVE
:: QemuServer
;
20 use Storable
qw(dclone) ;
21 use PVE
:: Exception
qw(raise raise_param_exc) ;
23 use PVE
:: Tools
qw(run_command lock_file file_read_firstline) ;
24 use PVE
:: Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file) ;
27 use Time
:: HiRes
qw(gettimeofday) ;
29 my $cpuinfo = PVE
:: ProcFSTools
:: read_cpuinfo
();
31 # Note about locking: we use flock on the config file protect
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.
38 cfs_register_file
( '/qemu-server/' ,
42 PVE
:: JSONSchema
:: register_standard_option
( 'skiplock' , {
43 description
=> "Ignore locks - only root is allowed to use this option." ,
48 PVE
:: JSONSchema
:: register_standard_option
( 'pve-qm-stateuri' , {
49 description
=> "Some command save/restore state from this location." ,
55 #no warnings 'redefine';
57 unless ( 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
);
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
);
80 die ( "no fairsched syscall for this arch" );
82 require 'asm/ioctl.ph' ;
83 eval 'sub KVM_GET_API_VERSION () { &_IO(0xAE, 0x);}' unless defined (& KVM_GET_API_VERSION
);
87 my ( $parent, $weight, $desired ) = @_ ;
89 return syscall (& __NR_fairsched_mknod
, int ( $parent ), int ( $weight ), int ( $desired ));
95 return syscall (& __NR_fairsched_rmnod
, int ( $id ));
99 my ( $pid, $newid ) = @_ ;
101 return syscall (& __NR_fairsched_mvpr
, int ( $pid ), int ( $newid ));
104 sub fairsched_vcpus
{
105 my ( $id, $vcpus ) = @_ ;
107 return syscall (& __NR_fairsched_vcpus
, int ( $id ), int ( $vcpus ));
111 my ( $id, $op, $rate ) = @_ ;
113 return syscall (& __NR_fairsched_rate
, int ( $id ), int ( $op ), int ( $rate ));
116 use constant FAIRSCHED_SET_RATE
=> 0 ;
117 use constant FAIRSCHED_DROP_RATE
=> 1 ;
118 use constant FAIRSCHED_GET_RATE
=> 2 ;
120 sub fairsched_cpulimit
{
121 my ( $id, $limit ) = @_ ;
123 my $cpulim1024 = int ( $limit * 1024 / 100 );
124 my $op = $cpulim1024 ? FAIRSCHED_SET_RATE
: FAIRSCHED_DROP_RATE
;
126 return fairsched_rate
( $id, $op, $cpulim1024 );
129 my $nodename = PVE
:: INotify
:: nodename
();
131 mkdir "/etc/pve/nodes/ $nodename " ;
132 my $confdir = "/etc/pve/nodes/ $nodename/qemu -server" ;
135 my $var_run_tmpdir = "/var/run/qemu-server" ;
136 mkdir $var_run_tmpdir ;
138 my $lock_dir = "/var/lock/qemu-server" ;
141 my $pcisysfs = "/sys/bus/pci" ;
147 description
=> "Specifies whether a VM will be started during system bootup." ,
153 description
=> "Automatic restart after crash (currently ignored)." ,
159 description
=> "Activate hotplug for disk and network device" ,
165 description
=> "Allow reboot. If set to '0' the VM exit on reboot." ,
171 description
=> "Lock/unlock the VM." ,
172 enum
=> [ qw(migrate backup) ],
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\n NOTE: This option is currently ignored." ,
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\n NOTE: You can disable fair-scheduler configuration by setting this to 0." ,
192 description
=> "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device." ,
199 description
=> "Amount of target RAM for the VM in MB." ,
205 description
=> "Keybord layout for vnc server. Default is read from the datacenter configuration file." ,
206 enum
=> PVE
:: Tools
:: kvmkeymaplist
(),
211 type
=> 'string' , format
=> 'dns-name' ,
212 description
=> "Set a name for the VM. Only used on the configuration web interface." ,
217 description
=> "Description for the VM. Only used on the configuration web interface. This is saved as comment inside the configuration file." ,
222 enum
=> [ qw(other wxp w2k w2k3 w2k8 wvista win7 l24 l26) ],
223 description
=> <<EODESC,
224 Used to enable special optimization/features for specific
227 other => unspecified OS
228 wxp => Microsoft Windows XP
229 w2k => Microsoft Windows 2000
230 w2k3 => Microsoft Windows 2003
231 w2k8 => Microsoft Windows 2008
232 wvista => Microsoft Windows Vista
233 win7 => Microsoft Windows 7
234 l24 => Linux 2.4 Kernel
235 l26 => Linux 2.6/3.X Kernel
237 other|l24|l26 ... no special behaviour
238 wxp|w2k|w2k3|w2k8|wvista|win7 ... use --localtime switch
244 description
=> "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)." ,
245 pattern
=> '[acdn]{1,4}' ,
250 type
=> 'string' , format
=> 'pve-qm-bootdisk' ,
251 description
=> "Enable booting from specified disk." ,
252 pattern
=> '(ide|scsi|virtio)\d+' ,
257 description
=> "The number of CPUs. Please use option -sockets instead." ,
264 description
=> "The number of CPU sockets." ,
271 description
=> "The number of cores per socket." ,
278 description
=> "Enable/disable ACPI." ,
284 description
=> "Enable/disable KVM hardware virtualization." ,
290 description
=> "Enable/disable time drift fix. This is ignored for kvm versions newer that 1.0 (not needed anymore)." ,
296 description
=> "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS." ,
301 description
=> "Freeze CPU at startup (use 'c' monitor command to start execution)." ,
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) ],
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)" ,
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})?)' ,
326 description
=> <<EODESCR,
327 Note: this option is for experts only. It allows you to pass arbitrary arguments to kvm, for example:
329 args: -no-reboot -no-hpet
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." ,
341 description
=> "Set maximum speed (in MB/s) for migrations. Value 0 is no limit." ,
345 migrate_downtime
=> {
348 description
=> "Set maximum tolerated downtime (in seconds) for migrations." ,
354 type
=> 'string' , format
=> 'pve-qm-drive' ,
355 typetext
=> 'volume' ,
356 description
=> "This is an alias for option -ide2" ,
360 description
=> "Emulated CPU type." ,
362 enum
=> [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom cpu64-rhel6 cpu64-rhel5 Conroe Penryn Nehalem Westmere Opteron_G1 Opteron_G2 Opteron_G3 host) ],
367 # what about other qemu settings ?
369 #machine => 'string',
382 ##soundhw => 'string',
384 while ( my ( $k, $v ) = each %$confdesc ) {
385 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm- $k " , $v );
388 my $MAX_IDE_DISKS = 4 ;
389 my $MAX_SCSI_DISKS = 14 ;
390 my $MAX_VIRTIO_DISKS = 6 ;
391 my $MAX_SATA_DISKS = 6 ;
392 my $MAX_USB_DEVICES = 5 ;
394 my $MAX_UNUSED_DISKS = 8 ;
395 my $MAX_HOSTPCI_DEVICES = 2 ;
396 my $MAX_SERIAL_PORTS = 4 ;
397 my $MAX_PARALLEL_PORTS = 3 ;
399 my $nic_model_list = [ 'rtl8139' , 'ne2k_pci' , 'e1000' , 'pcnet' , 'virtio' ,
400 'ne2k_isa' , 'i82551' , 'i82557b' , 'i82559er' ];
401 my $nic_model_list_txt = join ( ' ' , sort @$nic_model_list );
406 type
=> 'string' , format
=> 'pve-qm-net' ,
407 typetext
=> "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>][,tag=<vlanid>]" ,
408 description
=> <<EODESCR,
409 Specify network devices.
411 MODEL is one of: $nic_model_list_txt
413 XX:XX:XX:XX:XX:XX should be an unique MAC address. This is
414 automatically generated if not specified.
416 The bridge parameter can be used to automatically add the interface to a bridge device. The Proxmox VE standard bridge is called 'vmbr0'.
418 Option 'rate' is used to limit traffic bandwidth from and to this interface. It is specified as floating point number, unit is 'Megabytes per second'.
420 If you specify no bridge, we create a kvm 'user' (NATed) network device, which provides DHCP and DNS services. The following addresses are used:
426 The DHCP server assign addresses to the guest starting from 10.0.2.15.
430 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-net" , $netdesc );
432 for ( my $i = 0 ; $i < $MAX_NETS ; $i++ ) {
433 $confdesc ->{ "net $i " } = $netdesc ;
440 type
=> 'string' , format
=> 'pve-qm-drive' ,
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] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]' ,
442 description
=> "Use volume as IDE hard disk or CD-ROM (n is 0 to 3)." ,
444 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-ide" , $idedesc );
448 type
=> 'string' , format
=> 'pve-qm-drive' ,
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] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]' ,
450 description
=> "Use volume as SCSI hard disk or CD-ROM (n is 0 to 13)." ,
452 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-scsi" , $scsidesc );
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] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]' ,
458 description
=> "Use volume as SATA hard disk or CD-ROM (n is 0 to 5)." ,
460 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-sata" , $satadesc );
464 type
=> 'string' , format
=> 'pve-qm-drive' ,
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] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]' ,
466 description
=> "Use volume as VIRTIO hard disk (n is 0 to 5)." ,
468 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-virtio" , $virtiodesc );
472 type
=> 'string' , format
=> 'pve-qm-usb-device' ,
473 typetext
=> 'host=HOSTUSBDEVICE' ,
474 description
=> <<EODESCR,
475 Configure an USB device (n is 0 to 4). This can be used to
476 pass-through usb devices to the guest. HOSTUSBDEVICE syntax is:
478 'bus-port(.port)*' (decimal numbers) or
479 'vendor_id:product_id' (hexadeciaml numbers)
481 You can use the 'lsusb -t' command to list existing usb devices.
483 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
487 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-usb" , $usbdesc );
491 type
=> 'string' , format
=> 'pve-qm-hostpci' ,
492 typetext
=> "HOSTPCIDEVICE" ,
493 description
=> <<EODESCR,
494 Map host pci devices. HOSTPCIDEVICE syntax is:
496 'bus:dev.func' (hexadecimal numbers)
498 You can us the 'lspci' command to list existing pci devices.
500 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
502 Experimental: user reported problems with this option.
505 PVE
:: JSONSchema
:: register_standard_option
( "pve-qm-hostpci" , $hostpcidesc );
510 pattern
=> '/dev/ttyS\d+' ,
511 description
=> <<EODESCR,
512 Map host serial devices (n is 0 to 3).
514 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
516 Experimental: user reported problems with this option.
523 pattern
=> '/dev/parport\d+' ,
524 description
=> <<EODESCR,
525 Map host parallel devices (n is 0 to 2).
527 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
529 Experimental: user reported problems with this option.
533 for ( my $i = 0 ; $i < $MAX_PARALLEL_PORTS ; $i++ ) {
534 $confdesc ->{ "parallel $i " } = $paralleldesc ;
537 for ( my $i = 0 ; $i < $MAX_SERIAL_PORTS ; $i++ ) {
538 $confdesc ->{ "serial $i " } = $serialdesc ;
541 for ( my $i = 0 ; $i < $MAX_HOSTPCI_DEVICES ; $i++ ) {
542 $confdesc ->{ "hostpci $i " } = $hostpcidesc ;
545 for ( my $i = 0 ; $i < $MAX_IDE_DISKS ; $i++ ) {
546 $drivename_hash ->{ "ide $i " } = 1 ;
547 $confdesc ->{ "ide $i " } = $idedesc ;
550 for ( my $i = 0 ; $i < $MAX_SATA_DISKS ; $i++ ) {
551 $drivename_hash ->{ "sata $i " } = 1 ;
552 $confdesc ->{ "sata $i " } = $satadesc ;
555 for ( my $i = 0 ; $i < $MAX_SCSI_DISKS ; $i++ ) {
556 $drivename_hash ->{ "scsi $i " } = 1 ;
557 $confdesc ->{ "scsi $i " } = $scsidesc ;
560 for ( my $i = 0 ; $i < $MAX_VIRTIO_DISKS ; $i++ ) {
561 $drivename_hash ->{ "virtio $i " } = 1 ;
562 $confdesc ->{ "virtio $i " } = $virtiodesc ;
565 for ( my $i = 0 ; $i < $MAX_USB_DEVICES ; $i++ ) {
566 $confdesc ->{ "usb $i " } = $usbdesc ;
571 type
=> 'string' , format
=> 'pve-volume-id' ,
572 description
=> "Reference to unused volumes." ,
575 for ( my $i = 0 ; $i < $MAX_UNUSED_DISKS ; $i++ ) {
576 $confdesc ->{ "unused $i " } = $unuseddesc ;
579 my $kvm_api_version = 0 ;
583 return $kvm_api_version if $kvm_api_version ;
585 my $fh = IO
:: File-
> new ( "</dev/kvm" ) ||
588 if ( my $v = $fh -> ioctl ( KVM_GET_API_VERSION
(), 0 )) {
589 $kvm_api_version = $v ;
594 return $kvm_api_version ;
597 my $kvm_user_version ;
599 sub kvm_user_version
{
601 return $kvm_user_version if $kvm_user_version ;
603 $kvm_user_version = 'unknown' ;
605 my $tmp = `kvm -help 2>/dev/null` ;
607 if ( $tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?) / ) {
608 $kvm_user_version = $2 ;
611 return $kvm_user_version ;
615 my $kernel_has_vhost_net = - c
'/dev/vhost-net' ;
618 # order is important - used to autoselect boot disk
619 return (( map { "ide $_ " } ( 0 .. ( $MAX_IDE_DISKS - 1 ))),
620 ( map { "scsi $_ " } ( 0 .. ( $MAX_SCSI_DISKS - 1 ))),
621 ( map { "virtio $_ " } ( 0 .. ( $MAX_VIRTIO_DISKS - 1 ))),
622 ( map { "sata $_ " } ( 0 .. ( $MAX_SATA_DISKS - 1 ))));
625 sub valid_drivename
{
628 return defined ( $drivename_hash ->{ $dev });
633 return defined ( $confdesc ->{ $key });
637 return $nic_model_list ;
640 sub os_list_description
{
645 w2k
=> 'Windows 2000' ,
646 w2k3
=>, 'Windows 2003' ,
647 w2k8
=> 'Windows 2008' ,
648 wvista
=> 'Windows Vista' ,
655 sub disk_devive_info
{
658 die "unknown disk device format ' $dev '" if $dev !~ m/^(ide|scsi|virtio)(\d+)$/ ;
666 } elsif ( $bus eq 'scsi' ) {
670 my $controller = int ( $index / $maxdev );
671 my $unit = $index % $maxdev ;
674 return { bus
=> $bus, desc
=> uc ( $bus ) . " $controller : $unit " ,
675 controller
=> $controller, unit
=> $unit, index => $index };
679 sub qemu_drive_name
{
680 my ( $dev, $media ) = @_ ;
682 my $info = disk_devive_info
( $dev );
685 if (( $info ->{ bus
} eq 'ide' ) || ( $info ->{ bus
} eq 'scsi' )) {
686 $mediastr = ( $media eq 'cdrom' ) ?
"-cd" : "-hd" ;
687 return sprintf ( " %s%i%s%i " , $info ->{ bus
}, $info ->{ controller
},
688 $mediastr, $info ->{ unit
});
690 return sprintf ( " %s%i " , $info ->{ bus
}, $info ->{ index });
698 return $cdrom_path if $cdrom_path ;
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" ;
706 my ( $storecfg, $vmid, $cdrom ) = @_ ;
708 if ( $cdrom eq 'cdrom' ) {
709 return get_cdrom_path
();
710 } elsif ( $cdrom eq 'none' ) {
712 } elsif ( $cdrom =~ m
|^/|) {
715 return PVE
:: Storage
:: path
( $storecfg, $cdrom );
719 # try to convert old style file names to volume IDs
720 sub filename_to_volume_id
{
721 my ( $vmid, $file, $media ) = @_ ;
723 if (!( $file eq 'none' || $file eq 'cdrom' ||
724 $file =~ m
|^ /dev/ .+| || $file =~ m/^([^:]+):(.+)$/ )) {
726 return undef if $file =~ m
|/|;
728 if ( $media && $media eq 'cdrom' ) {
729 $file = "local:iso/ $file " ;
731 $file = "local: $vmid/$file " ;
738 sub verify_media_type
{
739 my ( $opt, $vtype, $media ) = @_ ;
744 if ( $media eq 'disk' ) {
746 } elsif ( $media eq 'cdrom' ) {
749 die "internal error" ;
752 return if ( $vtype eq $etype );
754 raise_param_exc
({ $opt => "unexpected media type ( $vtype != $etype )" });
757 sub cleanup_drive_path
{
758 my ( $opt, $storecfg, $drive ) = @_ ;
760 # try to convert filesystem paths to volume IDs
762 if (( $drive ->{ file
} !~ m/^(cdrom|none)$/ ) &&
763 ( $drive ->{ file
} !~ m
|^ /dev/ .+|) &&
764 ( $drive ->{ file
} !~ m/^([^:]+):(.+)$/ ) &&
765 ( $drive ->{ file
} !~ m/^\d+$/ )) {
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 ;
773 $drive ->{ media
} = 'cdrom' if ! $drive ->{ media
} && $drive ->{ file
} =~ m/^(cdrom|none)$/ ;
776 sub create_conf_nolock
{
777 my ( $vmid, $settings ) = @_ ;
779 my $filename = config_file
( $vmid );
781 die "configuration file ' $filename ' already exists \n " if - f
$filename ;
783 my $defaults = load_defaults
();
785 $settings ->{ name
} = "vm $vmid " if ! $settings ->{ name
};
786 $settings ->{ memory
} = $defaults ->{ memory
} if ! $settings ->{ memory
};
789 foreach my $opt ( keys %$settings ) {
790 next if ! $confdesc ->{ $opt };
792 my $value = $settings ->{ $opt };
795 $data .= " $opt : $value\n " ;
798 PVE
:: Tools
:: file_set_contents
( $filename, $data );
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 # [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
804 # [,aio=native|threads]
807 my ( $key, $data ) = @_ ;
811 # $key may be undefined - used to verify JSON parameters
812 if (! defined ( $key )) {
813 $res ->{ interface
} = 'unknown' ; # should not harm when used to verify parameters
815 } elsif ( $key =~ m/^([^\d]+)(\d+)$/ ) {
816 $res ->{ interface
} = $1 ;
822 foreach my $p ( split ( /,/ , $data )) {
823 next if $p =~ m/^\s*$/ ;
825 if ( $p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio)=(.+)$/ ) {
826 my ( $k, $v ) = ( $1, $2 );
828 $k = 'file' if $k eq 'volume' ;
830 return undef if defined $res ->{ $k };
834 if (! $res ->{ file
} && $p !~ m/=/ ) {
842 return undef if ! $res ->{ file
};
844 return undef if $res ->{ cache
} &&
845 $res ->{ cache
} !~ m/^(off|none|writethrough|writeback|unsafe)$/ ;
846 return undef if $res ->{ snapshot
} && $res ->{ snapshot
} !~ m/^(on|off)$/ ;
847 return undef if $res ->{ cyls
} && $res ->{ cyls
} !~ m/^\d+$/ ;
848 return undef if $res ->{ heads
} && $res ->{ heads
} !~ m/^\d+$/ ;
849 return undef if $res ->{ secs
} && $res ->{ secs
} !~ m/^\d+$/ ;
850 return undef if $res ->{ media
} && $res ->{ media
} !~ m/^(disk|cdrom)$/ ;
851 return undef if $res ->{ trans
} && $res ->{ trans
} !~ m/^(none|lba|auto)$/ ;
852 return undef if $res ->{ format
} && $res ->{ format
} !~ m/^(raw|cow|qcow|qcow2|vmdk|cloop)$/ ;
853 return undef if $res ->{ rerror
} && $res ->{ rerror
} !~ m/^(ignore|report|stop)$/ ;
854 return undef if $res ->{ werror
} && $res ->{ werror
} !~ m/^(enospc|ignore|report|stop)$/ ;
855 return undef if $res ->{ backup
} && $res ->{ backup
} !~ m/^(yes|no)$/ ;
856 return undef if $res ->{ aio
} && $res ->{ aio
} !~ m/^(native|threads)$/ ;
858 if ( $res ->{ media
} && ( $res ->{ media
} eq 'cdrom' )) {
859 return undef if $res ->{ snapshot
} || $res ->{ trans
} || $res ->{ format
};
860 return undef if $res ->{ heads
} || $res ->{ secs
} || $res ->{ cyls
};
861 return undef if $res ->{ interface
} eq 'virtio' ;
864 # rerror does not work with scsi drives
865 if ( $res ->{ rerror
}) {
866 return undef if $res ->{ interface
} eq 'scsi' ;
872 my @qemu_drive_options = qw(heads secs cyls trans media format cache snapshot rerror werror aio) ;
875 my ( $vmid, $drive ) = @_ ;
878 foreach my $o ( @qemu_drive_options, 'backup' ) {
879 $opts .= ", $o = $drive ->{ $o }" if $drive ->{ $o };
882 return " $drive ->{file} $opts " ;
886 my ( $fh, $noerr ) = @_ ;
889 my $SG_GET_VERSION_NUM = 0x2282 ;
891 my $versionbuf = " \x00 " x
8 ;
892 my $ret = ioctl ( $fh, $SG_GET_VERSION_NUM, $versionbuf );
894 die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n " if ! $noerr ;
897 my $version = unpack ( "I" , $versionbuf );
898 if ( $version < 30000 ) {
899 die "scsi generic interface too old \n " if ! $noerr ;
903 my $buf = " \x00 " x
36 ;
904 my $sensebuf = " \x00 " x
8 ;
905 my $cmd = pack ( "C x3 C x11" , 0x12 , 36 );
907 # see /usr/include/scsi/sg.h
908 my $sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I" ;
910 my $packet = pack ( $sg_io_hdr_t, ord ( 'S' ), - 3 , length ( $cmd ),
911 length ( $sensebuf ), 0 , length ( $buf ), $buf,
912 $cmd, $sensebuf, 6000 );
914 $ret = ioctl ( $fh, $SG_IO, $packet );
916 die "scsi ioctl SG_IO failed - $!\n " if ! $noerr ;
920 my @res = unpack ( $sg_io_hdr_t, $packet );
921 if ( $res [ 17 ] || $res [ 18 ]) {
922 die "scsi ioctl SG_IO status error - $!\n " if ! $noerr ;
927 ( $res ->{ device
}, $res ->{ removable
}, $res ->{ venodor
},
928 $res ->{ product
}, $res ->{ revision
}) = unpack ( "C C x6 A8 A16 A4" , $buf );
936 my $fh = IO
:: File-
> new ( "+< $path " ) || return undef ;
937 my $res = scsi_inquiry
( $fh, 1 );
943 sub print_drivedevice_full
{
944 my ( $storecfg, $vmid, $drive ) = @_ ;
949 if ( $drive ->{ interface
} eq 'virtio' ) {
950 my $pciaddr = print_pci_addr
( " $drive ->{interface} $drive ->{index}" );
951 $device = "virtio-blk-pci,drive=drive- $drive ->{interface} $drive ->{index},id= $drive ->{interface} $drive ->{index} $pciaddr " ;
952 } elsif ( $drive ->{ interface
} eq 'scsi' ) {
954 my $controller = int ( $drive ->{ index } / $maxdev );
955 my $unit = $drive ->{ index } % $maxdev ;
956 my $devicetype = 'hd' ;
958 if ( drive_is_cdrom
( $drive )) {
961 if ( $drive ->{ file
} =~ m
|^/|) {
962 $path = $drive ->{ file
};
964 $path = PVE
:: Storage
:: path
( $storecfg, $drive ->{ file
});
966 $devicetype = 'block' if path_is_scsi
( $path );
969 $device = "scsi- $devicetype,bus =lsi $controller .0,scsi-id= $unit,drive =drive- $drive ->{interface} $drive ->{index},id= $drive ->{interface} $drive ->{index}" ;
970 } elsif ( $drive ->{ interface
} eq 'ide' ){
972 my $controller = int ( $drive ->{ index } / $maxdev );
973 my $unit = $drive ->{ index } % $maxdev ;
974 my $devicetype = ( $drive ->{ media
} && $drive ->{ media
} eq 'cdrom' ) ?
"cd" : "hd" ;
976 $device = "ide- $devicetype,bus =ide. $controller,unit = $unit,drive =drive- $drive ->{interface} $drive ->{index},id= $drive ->{interface} $drive ->{index}" ;
977 } elsif ( $drive ->{ interface
} eq 'sata' ){
978 my $controller = int ( $drive ->{ index } / $MAX_SATA_DISKS );
979 my $unit = $drive ->{ index } % $MAX_SATA_DISKS ;
980 $device = "ide-drive,bus=ahci $controller . $unit,drive =drive- $drive ->{interface} $drive ->{index},id= $drive ->{interface} $drive ->{index}" ;
981 } elsif ( $drive ->{ interface
} eq 'usb' ) {
983 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
985 die "unsupported interface type" ;
988 $device .= ",bootindex= $drive ->{bootindex}" if $drive ->{ bootindex
};
993 sub print_drive_full
{
994 my ( $storecfg, $vmid, $drive ) = @_ ;
997 foreach my $o ( @qemu_drive_options ) {
998 next if $o eq 'bootindex' ;
999 $opts .= ", $o = $drive ->{ $o }" if $drive ->{ $o };
1002 # use linux-aio by default (qemu default is threads)
1003 $opts .= ",aio=native" if ! $drive ->{ aio
};
1006 my $volid = $drive ->{ file
};
1007 if ( drive_is_cdrom
( $drive )) {
1008 $path = get_iso_path
( $storecfg, $vmid, $volid );
1010 if ( $volid =~ m
|^/|) {
1013 $path = PVE
:: Storage
:: path
( $storecfg, $volid );
1015 if (! $drive ->{ cache
} && ( $path =~ m
|^ /dev/ | || $path =~ m
| \
. raw
$|)) {
1016 $opts .= ",cache=none" ;
1020 my $pathinfo = $path ?
"file= $path, " : '' ;
1022 return "${pathinfo}if=none,id=drive- $drive ->{interface} $drive ->{index} $opts " ;
1025 sub print_netdevice_full
{
1026 my ( $vmid, $conf, $net, $netid ) = @_ ;
1028 my $bootorder = $conf ->{ boot
} || $confdesc ->{ boot
}->{ default };
1030 my $device = $net ->{ model
};
1031 if ( $net ->{ model
} eq 'virtio' ) {
1032 $device = 'virtio-net-pci' ;
1035 # qemu > 0.15 always try to boot from network - we disable that by
1036 # not loading the pxe rom file
1037 my $extra = ( $bootorder !~ m/n/ ) ?
"romfile=," : '' ;
1038 my $pciaddr = print_pci_addr
( " $netid " );
1039 my $tmpstr = " $device,$ {extra}mac= $net ->{macaddr},netdev= $netid$pciaddr,id = $netid " ;
1040 $tmpstr .= ",bootindex= $net ->{bootindex}" if $net ->{ bootindex
} ;
1044 sub print_netdev_full
{
1045 my ( $vmid, $conf, $net, $netid ) = @_ ;
1048 if ( $netid =~ m/^net(\d+)$/ ) {
1052 die "got strange net id ' $i ' \n " if $i >= ${ MAX_NETS
};
1054 my $ifname = "tap${vmid}i $i " ;
1056 # kvm uses TUNSETIFF ioctl, and that limits ifname length
1057 die "interface name ' $ifname ' is too long (max 15 character) \n "
1058 if length ( $ifname ) >= 16 ;
1060 my $vhostparam = '' ;
1061 $vhostparam = ',vhost=on' if $kernel_has_vhost_net && $net ->{ model
} eq 'virtio' ;
1063 my $vmname = $conf ->{ name
} || "vm $vmid " ;
1065 if ( $net ->{ bridge
}) {
1066 return "type=tap,id= $netid,ifname =${ifname},script=/var/lib/qemu-server/pve-bridge $vhostparam " ;
1068 return "type=user,id= $netid,hostname = $vmname " ;
1072 sub drive_is_cdrom
{
1075 return $drive && $drive ->{ media
} && ( $drive ->{ media
} eq 'cdrom' );
1082 return undef if ! $value ;
1086 if ( $value =~ m/^[a-f0-9]{2}:[a-f0-9]{2}\.[a-f0-9]$/ ) {
1087 $res ->{ pciid
} = $value ;
1095 # netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
1101 foreach my $kvp ( split ( /,/ , $data )) {
1103 if ( $kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i ) {
1105 my $mac = uc ( $3 ) || PVE
:: Tools
:: random_ether_addr
();
1106 $res ->{ model
} = $model ;
1107 $res ->{ macaddr
} = $mac ;
1108 } elsif ( $kvp =~ m/^bridge=(\S+)$/ ) {
1109 $res ->{ bridge
} = $1 ;
1110 } elsif ( $kvp =~ m/^rate=(\d+(\.\d+)?)$/ ) {
1112 } elsif ( $kvp =~ m/^tag=(\d+)$/ ) {
1120 return undef if ! $res ->{ model
};
1128 my $res = " $net ->{model}" ;
1129 $res .= "= $net ->{macaddr}" if $net ->{ macaddr
};
1130 $res .= ",bridge= $net ->{bridge}" if $net ->{ bridge
};
1131 $res .= ",rate= $net ->{rate}" if $net ->{ rate
};
1132 $res .= ",tag= $net ->{tag}" if $net ->{ tag
};
1137 sub add_random_macs
{
1138 my ( $settings ) = @_ ;
1140 foreach my $opt ( keys %$settings ) {
1141 next if $opt !~ m/^net(\d+)$/ ;
1142 my $net = parse_net
( $settings ->{ $opt });
1144 $settings ->{ $opt } = print_net
( $net );
1148 sub add_unused_volume
{
1149 my ( $config, $volid ) = @_ ;
1152 for ( my $ind = $MAX_UNUSED_DISKS - 1 ; $ind >= 0 ; $ind --) {
1153 my $test = "unused $ind " ;
1154 if ( my $vid = $config ->{ $test }) {
1155 return if $vid eq $volid ; # do not add duplicates
1161 die "To many unused volume - please delete them first. \n " if ! $key ;
1163 $config ->{ $key } = $volid ;
1168 # fixme: remove all thos $noerr parameters?
1170 PVE
:: JSONSchema
:: register_format
( 'pve-qm-bootdisk' , \
& verify_bootdisk
);
1171 sub verify_bootdisk
{
1172 my ( $value, $noerr ) = @_ ;
1174 return $value if valid_drivename
( $value );
1176 return undef if $noerr ;
1178 die "invalid boot disk ' $value ' \n " ;
1181 PVE
:: JSONSchema
:: register_format
( 'pve-qm-net' , \
& verify_net
);
1183 my ( $value, $noerr ) = @_ ;
1185 return $value if parse_net
( $value );
1187 return undef if $noerr ;
1189 die "unable to parse network options \n " ;
1192 PVE
:: JSONSchema
:: register_format
( 'pve-qm-drive' , \
& verify_drive
);
1194 my ( $value, $noerr ) = @_ ;
1196 return $value if parse_drive
( undef , $value );
1198 return undef if $noerr ;
1200 die "unable to parse drive options \n " ;
1203 PVE
:: JSONSchema
:: register_format
( 'pve-qm-hostpci' , \
& verify_hostpci
);
1204 sub verify_hostpci
{
1205 my ( $value, $noerr ) = @_ ;
1207 return $value if parse_hostpci
( $value );
1209 return undef if $noerr ;
1211 die "unable to parse pci id \n " ;
1214 PVE
:: JSONSchema
:: register_format
( 'pve-qm-watchdog' , \
& verify_watchdog
);
1215 sub verify_watchdog
{
1216 my ( $value, $noerr ) = @_ ;
1218 return $value if parse_watchdog
( $value );
1220 return undef if $noerr ;
1222 die "unable to parse watchdog options \n " ;
1225 sub parse_watchdog
{
1228 return undef if ! $value ;
1232 foreach my $p ( split ( /,/ , $value )) {
1233 next if $p =~ m/^\s*$/ ;
1235 if ( $p =~ m/^(model=)?(i6300esb|ib700)$/ ) {
1237 } elsif ( $p =~ m/^(action=)?(reset|shutdown|poweroff|pause|debug|none)$/ ) {
1238 $res ->{ action
} = $2 ;
1247 sub parse_usb_device
{
1250 return undef if ! $value ;
1252 my @dl = split ( /,/ , $value );
1256 foreach my $v ( @dl ) {
1257 if ( $v =~ m/^host=(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/ ) {
1259 $res ->{ vendorid
} = $2 ;
1260 $res ->{ productid
} = $4 ;
1261 } elsif ( $v =~ m/^host=(\d+)\-(\d+(\.\d+)*)$/ ) {
1263 $res ->{ hostbus
} = $1 ;
1264 $res ->{ hostport
} = $2 ;
1269 return undef if ! $found ;
1274 PVE
:: JSONSchema
:: register_format
( 'pve-qm-usb-device' , \
& verify_usb_device
);
1275 sub verify_usb_device
{
1276 my ( $value, $noerr ) = @_ ;
1278 return $value if parse_usb_device
( $value );
1280 return undef if $noerr ;
1282 die "unable to parse usb device \n " ;
1285 # add JSON properties for create and set function
1286 sub json_config_properties
{
1289 foreach my $opt ( keys %$confdesc ) {
1290 $prop ->{ $opt } = $confdesc ->{ $opt };
1297 my ( $key, $value ) = @_ ;
1299 die "unknown setting ' $key ' \n " if ! $confdesc ->{ $key };
1301 my $type = $confdesc ->{ $key }->{ type
};
1303 if (! defined ( $value )) {
1304 die "got undefined value \n " ;
1307 if ( $value =~ m/[\n\r]/ ) {
1308 die "property contains a line feed \n " ;
1311 if ( $type eq 'boolean' ) {
1312 return 1 if ( $value eq '1' ) || ( $value =~ m/^(on|yes|true)$/i );
1313 return 0 if ( $value eq '0' ) || ( $value =~ m/^(off|no|false)$/i );
1314 die "type check ('boolean') failed - got ' $value ' \n " ;
1315 } elsif ( $type eq 'integer' ) {
1316 return int ( $1 ) if $value =~ m/^(\d+)$/ ;
1317 die "type check ('integer') failed - got ' $value ' \n " ;
1318 } elsif ( $type eq 'string' ) {
1319 if ( my $fmt = $confdesc ->{ $key }->{ format
}) {
1320 if ( $fmt eq 'pve-qm-drive' ) {
1321 # special case - we need to pass $key to parse_drive()
1322 my $drive = parse_drive
( $key, $value );
1323 return $value if $drive ;
1324 die "unable to parse drive options \n " ;
1326 PVE
:: JSONSchema
:: check_format
( $fmt, $value );
1329 $value =~ s/^\"(.*)\"$/$1/ ;
1332 die "internal error"
1336 sub lock_config_full
{
1337 my ( $vmid, $timeout, $code, @param ) = @_ ;
1339 my $filename = config_file_lock
( $vmid );
1341 my $res = lock_file
( $filename, $timeout, $code, @param );
1349 my ( $vmid, $code, @param ) = @_ ;
1351 return lock_config_full
( $vmid, 10 , $code, @param );
1354 sub cfs_config_path
{
1355 my ( $vmid, $node ) = @_ ;
1357 $node = $nodename if ! $node ;
1358 return "nodes/ $node/qemu -server/ $vmid .conf" ;
1361 sub check_iommu_support
{
1362 #fixme : need to check IOMMU support
1363 #http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
1371 my ( $vmid, $node ) = @_ ;
1373 my $cfspath = cfs_config_path
( $vmid, $node );
1374 return "/etc/pve/ $cfspath " ;
1377 sub config_file_lock
{
1380 return " $lock_dir/lock - $vmid .conf" ;
1386 my $conf = config_file
( $vmid );
1387 utime undef , undef , $conf ;
1391 my ( $storecfg, $vmid, $keep_empty_config ) = @_ ;
1393 my $conffile = config_file
( $vmid );
1395 my $conf = load_config
( $vmid );
1399 # only remove disks owned by this VM
1400 foreach_drive
( $conf, sub {
1401 my ( $ds, $drive ) = @_ ;
1403 return if drive_is_cdrom
( $drive );
1405 my $volid = $drive ->{ file
};
1406 return if ! $volid || $volid =~ m
|^/|;
1408 my ( $path, $owner ) = PVE
:: Storage
:: path
( $storecfg, $volid );
1409 return if ! $path || ! $owner || ( $owner != $vmid );
1411 PVE
:: Storage
:: vdisk_free
( $storecfg, $volid );
1414 if ( $keep_empty_config ) {
1415 PVE
:: Tools
:: file_set_contents
( $conffile, "memory: 128 \n " );
1420 # also remove unused disk
1422 my $dl = PVE
:: Storage
:: vdisk_list
( $storecfg, undef , $vmid );
1425 PVE
:: Storage
:: foreach_volid
( $dl, sub {
1426 my ( $volid, $sid, $volname, $d ) = @_ ;
1427 PVE
:: Storage
:: vdisk_free
( $storecfg, $volid );
1437 sub load_diskinfo_old
{
1438 my ( $storecfg, $vmid, $conf ) = @_ ;
1444 foreach_drive
( $conf, sub {
1449 return if drive_is_cdrom
( $di );
1451 if ( $di ->{ file
} =~ m
|^ /dev/ .+|) {
1452 $info ->{ $di ->{ file
}}->{ size
} = PVE
:: Storage
:: file_size_info
( $di ->{ file
});
1454 push @$vollist, $di ->{ file
};
1459 my $dl = PVE
:: Storage
:: vdisk_list
( $storecfg, undef , $vmid, $vollist );
1461 PVE
:: Storage
:: foreach_volid
( $dl, sub {
1462 my ( $volid, $sid, $volname, $d ) = @_ ;
1463 $info ->{ $volid } = $d ;
1468 foreach my $ds ( keys %$res ) {
1469 my $di = $res ->{ $ds };
1471 $res ->{ $ds }->{ disksize
} = $info ->{ $di ->{ file
}} ?
1472 $info ->{ $di ->{ file
}}->{ size
} / ( 1024 * 1024 ) : 0 ;
1481 my $cfspath = cfs_config_path
( $vmid );
1483 my $conf = PVE
:: Cluster
:: cfs_read_file
( $cfspath );
1485 die "no such VM (' $vmid ') \n " if ! defined ( $conf );
1490 sub parse_vm_config
{
1491 my ( $filename, $raw ) = @_ ;
1493 return undef if ! defined ( $raw );
1496 digest
=> Digest
:: SHA
:: sha1_hex
( $raw ),
1499 $filename =~ m
| /qemu-server/ ( \d
+) \
. conf
$|
1500 || die "got strange filename ' $filename '" ;
1506 while ( $raw && $raw =~ s/^(.*?)(\n|$)// ) {
1509 next if $line =~ m/^\s*$/ ;
1511 if ( $line =~ m/^\#(.*)\s*$/ ) {
1512 $descr .= PVE
:: Tools
:: decode_text
( $1 ) . " \n " ;
1516 if ( $line =~ m/^(description):\s*(.*\S)\s*$/ ) {
1517 $descr .= PVE
:: Tools
:: decode_text
( $2 );
1518 } elsif ( $line =~ m/^(args):\s*(.*\S)\s*$/ ) {
1521 $res ->{ $key } = $value ;
1522 } elsif ( $line =~ m/^([a-z][a-z_]*\d*):\s*(\S+)\s*$/ ) {
1525 eval { $value = check_type
( $key, $value ); };
1527 warn "vm $vmid - unable to parse value of ' $key ' - $@ " ;
1529 my $fmt = $confdesc ->{ $key }->{ format
};
1530 if ( $fmt && $fmt eq 'pve-qm-drive' ) {
1531 my $v = parse_drive
( $key, $value );
1532 if ( my $volid = filename_to_volume_id
( $vmid, $v ->{ file
}, $v ->{ media
})) {
1533 $v ->{ file
} = $volid ;
1534 $value = print_drive
( $vmid, $v );
1536 warn "vm $vmid - unable to parse value of ' $key ' \n " ;
1541 if ( $key eq 'cdrom' ) {
1542 $res ->{ ide2
} = $value ;
1544 $res ->{ $key } = $value ;
1550 $res ->{ description
} = $descr if $descr ;
1552 # convert old smp to sockets
1553 if ( $res ->{ smp
} && ! $res ->{ sockets
}) {
1554 $res ->{ sockets
} = $res ->{ smp
};
1561 sub write_vm_config
{
1562 my ( $filename, $conf ) = @_ ;
1564 if ( $conf ->{ cdrom
}) {
1565 die "option ide2 conflicts with cdrom \n " if $conf ->{ ide2
};
1566 $conf ->{ ide2
} = $conf ->{ cdrom
};
1567 delete $conf ->{ cdrom
};
1570 # we do not use 'smp' any longer
1571 if ( $conf ->{ sockets
}) {
1572 delete $conf ->{ smp
};
1573 } elsif ( $conf ->{ smp
}) {
1574 $conf ->{ sockets
} = $conf ->{ smp
};
1575 delete $conf ->{ cores
};
1576 delete $conf ->{ smp
};
1579 my $new_volids = {};
1580 foreach my $key ( keys %$conf ) {
1581 next if $key eq 'digest' || $key eq 'description' ;
1582 my $value = $conf ->{ $key };
1583 eval { $value = check_type
( $key, $value ); };
1584 die "unable to parse value of ' $key ' - $@ " if $@ ;
1586 $conf ->{ $key } = $value ;
1588 if ( valid_drivename
( $key )) {
1589 my $drive = PVE
:: QemuServer
:: parse_drive
( $key, $value );
1590 $new_volids ->{ $drive ->{ file
}} = 1 if $drive && $drive ->{ file
};
1594 # remove 'unusedX' settings if we re-add a volume
1595 foreach my $key ( keys %$conf ) {
1596 my $value = $conf ->{ $key };
1597 if ( $key =~ m/^unused/ && $new_volids ->{ $value }) {
1598 delete $conf ->{ $key };
1605 # add description as comment to top of file
1606 my $descr = $conf ->{ description
} || '' ;
1607 foreach my $cl ( split ( /\n/ , $descr )) {
1608 $raw .= '#' . PVE
:: Tools
:: encode_text
( $cl ) . " \n " ;
1611 foreach my $key ( sort keys %$conf ) {
1612 next if $key eq 'digest' || $key eq 'description' ;
1613 $raw .= " $key : $conf ->{ $key } \n " ;
1619 sub update_config_nolock
{
1620 my ( $vmid, $conf, $skiplock ) = @_ ;
1622 check_lock
( $conf ) if ! $skiplock ;
1624 my $cfspath = cfs_config_path
( $vmid );
1626 PVE
:: Cluster
:: cfs_write_file
( $cfspath, $conf );
1630 my ( $vmid, $conf, $skiplock ) = @_ ;
1632 lock_config
( $vmid, & update_config_nolock
, $conf, $skiplock );
1639 # we use static defaults from our JSON schema configuration
1640 foreach my $key ( keys %$confdesc ) {
1641 if ( defined ( my $default = $confdesc ->{ $key }->{ default })) {
1642 $res ->{ $key } = $default ;
1646 my $conf = PVE
:: Cluster
:: cfs_read_file
( 'datacenter.cfg' );
1647 $res ->{ keyboard
} = $conf ->{ keyboard
} if $conf ->{ keyboard
};
1653 my $vmlist = PVE
:: Cluster
:: get_vmlist
();
1655 return $res if ! $vmlist || ! $vmlist ->{ ids
};
1656 my $ids = $vmlist ->{ ids
};
1658 foreach my $vmid ( keys %$ids ) {
1659 my $d = $ids ->{ $vmid };
1660 next if ! $d ->{ node
} || $d ->{ node
} ne $nodename ;
1661 next if ! $d ->{ type
} || $d ->{ type
} ne 'qemu' ;
1662 $res ->{ $vmid }->{ exists } = 1 ;
1667 # test if VM uses local resources (to prevent migration)
1668 sub check_local_resources
{
1669 my ( $conf, $noerr ) = @_ ;
1673 $loc_res = 1 if $conf ->{ hostusb
}; # old syntax
1674 $loc_res = 1 if $conf ->{ hostpci
}; # old syntax
1676 foreach my $k ( keys %$conf ) {
1677 $loc_res = 1 if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/ ;
1680 die "VM uses local resources \n " if $loc_res && ! $noerr ;
1688 die "VM is locked ( $conf ->{lock}) \n " if $conf ->{ lock };
1692 my ( $pidfile, $pid ) = @_ ;
1694 my $fh = IO
:: File-
> new ( "/proc/ $pid/cmdline " , "r" );
1698 return undef if ! $line ;
1699 my @param = split ( /\0/ , $line );
1701 my $cmd = $param [ 0 ];
1702 return if ! $cmd || ( $cmd !~ m
| kvm
$|);
1704 for ( my $i = 0 ; $i < scalar ( @param ); $i++ ) {
1707 if (( $p eq '-pidfile' ) || ( $p eq '--pidfile' )) {
1708 my $p = $param [ $i+1 ];
1709 return 1 if $p && ( $p eq $pidfile );
1718 my ( $vmid, $nocheck ) = @_ ;
1720 my $filename = config_file
( $vmid );
1722 die "unable to find configuration file for VM $vmid - no such machine \n "
1723 if ! $nocheck && ! - f
$filename ;
1725 my $pidfile = pidfile_name
( $vmid );
1727 if ( my $fd = IO
:: File-
> new ( "< $pidfile " )) {
1732 my $mtime = $st -> mtime ;
1733 if ( $mtime > time ()) {
1734 warn "file ' $filename ' modified in future \n " ;
1737 if ( $line =~ m/^(\d+)$/ ) {
1739 if ( check_cmdline
( $pidfile, $pid )) {
1740 if ( my $pinfo = PVE
:: ProcFSTools
:: check_process_running
( $pid )) {
1752 my $vzlist = config_list
();
1754 my $fd = IO
:: Dir-
> new ( $var_run_tmpdir ) || return $vzlist ;
1756 while ( defined ( my $de = $fd -> read )) {
1757 next if $de !~ m/^(\d+)\.pid$/ ;
1759 next if ! defined ( $vzlist ->{ $vmid });
1760 if ( my $pid = check_running
( $vmid )) {
1761 $vzlist ->{ $vmid }->{ pid
} = $pid ;
1768 my $storage_timeout_hash = {};
1771 my ( $storecfg, $conf ) = @_ ;
1773 my $bootdisk = $conf ->{ bootdisk
};
1774 return undef if ! $bootdisk ;
1775 return undef if ! valid_drivename
( $bootdisk );
1777 return undef if ! $conf ->{ $bootdisk };
1779 my $drive = parse_drive
( $bootdisk, $conf ->{ $bootdisk });
1780 return undef if ! defined ( $drive );
1782 return undef if drive_is_cdrom
( $drive );
1784 my $volid = $drive ->{ file
};
1785 return undef if ! $volid ;
1791 if ( $volid =~ m
|^/|) {
1792 $path = $timeoutid = $volid ;
1795 $storeid = $timeoutid = PVE
:: Storage
:: parse_volume_id
( $volid );
1796 $path = PVE
:: Storage
:: path
( $storecfg, $volid );
1804 my $last_timeout = $storage_timeout_hash ->{ $timeoutid };
1805 if ( $last_timeout ) {
1806 if (( time () - $last_timeout ) < 30 ) {
1807 # skip storage with errors
1810 delete $storage_timeout_hash ->{ $timeoutid };
1813 my ( $size, $format, $used );
1815 ( $size, $format, $used ) = PVE
:: Storage
:: file_size_info
( $path, 1 );
1817 if (! defined ( $format )) {
1819 $storage_timeout_hash ->{ $timeoutid } = time ();
1823 return wantarray ?
( $size, $used ) : $size ;
1826 my $last_proc_pid_stat ;
1829 my ( $opt_vmid ) = @_ ;
1833 my $storecfg = PVE
:: Storage
:: config
();
1835 my $list = vzlist
();
1836 my ( $uptime ) = PVE
:: ProcFSTools
:: read_proc_uptime
( 1 );
1838 my $cpucount = $cpuinfo ->{ cpus
} || 1 ;
1840 foreach my $vmid ( keys %$list ) {
1841 next if $opt_vmid && ( $vmid ne $opt_vmid );
1843 my $cfspath = cfs_config_path
( $vmid );
1844 my $conf = PVE
:: Cluster
:: cfs_read_file
( $cfspath ) || {};
1847 $d ->{ pid
} = $list ->{ $vmid }->{ pid
};
1849 # fixme: better status?
1850 $d ->{ status
} = $list ->{ $vmid }->{ pid
} ?
'running' : 'stopped' ;
1852 my ( $size, $used ) = disksize
( $storecfg, $conf );
1853 if ( defined ( $size ) && defined ( $used )) {
1855 $d ->{ maxdisk
} = $size ;
1861 $d ->{ cpus
} = ( $conf ->{ sockets
} || 1 ) * ( $conf ->{ cores
} || 1 );
1862 $d ->{ cpus
} = $cpucount if $d ->{ cpus
} > $cpucount ;
1864 $d ->{ name
} = $conf ->{ name
} || "VM $vmid " ;
1865 $d ->{ maxmem
} = $conf ->{ memory
} ?
$conf ->{ memory
}*( 1024 * 1024 ) : 0 ;
1875 $d ->{ diskwrite
} = 0 ;
1880 my $netdev = PVE
:: ProcFSTools
:: read_proc_net_dev
();
1881 foreach my $dev ( keys %$netdev ) {
1882 next if $dev !~ m/^tap([1-9]\d*)i/ ;
1884 my $d = $res ->{ $vmid };
1887 $d ->{ netout
} += $netdev ->{ $dev }->{ receive
};
1888 $d ->{ netin
} += $netdev ->{ $dev }->{ transmit
};
1891 my $ctime = gettimeofday
;
1893 foreach my $vmid ( keys %$list ) {
1895 my $d = $res ->{ $vmid };
1896 my $pid = $d ->{ pid
};
1899 if ( my $fh = IO
:: File-
> new ( "/proc/ $pid/io " , "r" )) {
1901 while ( defined ( my $line = < $fh >)) {
1902 if ( $line =~ m/^([rw]char):\s+(\d+)$/ ) {
1907 $d ->{ diskread
} = $data ->{ rchar
} || 0 ;
1908 $d ->{ diskwrite
} = $data ->{ wchar
} || 0 ;
1911 my $pstat = PVE
:: ProcFSTools
:: read_proc_pid_stat
( $pid );
1912 next if ! $pstat ; # not running
1914 my $used = $pstat ->{ utime } + $pstat ->{ stime
};
1916 $d ->{ uptime
} = int (( $uptime - $pstat ->{ starttime
})/ $cpuinfo ->{ user_hz
});
1918 if ( $pstat ->{ vsize
}) {
1919 $d ->{ mem
} = int (( $pstat ->{ rss
}/ $pstat ->{ vsize
})* $d ->{ maxmem
});
1922 my $old = $last_proc_pid_stat ->{ $pid };
1924 $last_proc_pid_stat ->{ $pid } = {
1932 my $dtime = ( $ctime - $old ->{ time }) * $cpucount * $cpuinfo ->{ user_hz
};
1934 if ( $dtime > 1000 ) {
1935 my $dutime = $used - $old ->{ used
};
1937 $d ->{ cpu
} = (( $dutime/$dtime )* $cpucount ) / $d ->{ cpus
};
1938 $last_proc_pid_stat ->{ $pid } = {
1944 $d ->{ cpu
} = $old ->{ cpu
};
1952 my ( $conf, $func ) = @_ ;
1954 foreach my $ds ( keys %$conf ) {
1955 next if ! valid_drivename
( $ds );
1957 my $drive = parse_drive
( $ds, $conf ->{ $ds });
1960 & $func ( $ds, $drive );
1964 sub config_to_command
{
1965 my ( $storecfg, $vmid, $conf, $defaults, $migrate_uri ) = @_ ;
1969 my $kvmver = kvm_user_version
();
1970 my $vernum = 0 ; # unknown
1971 if ( $kvmver =~ m/^(\d+)\.(\d+)$/ ) {
1972 $vernum = $1*1000000+$2*1000 ;
1973 } elsif ( $kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/ ) {
1974 $vernum = $1*1000000+$2*1000+$3 ;
1977 die "detected old qemu-kvm binary ( $kvmver ) \n " if $vernum < 15000 ;
1979 my $have_ovz = - f
'/proc/vz/vestat' ;
1981 push @$cmd, '/usr/bin/kvm' ;
1983 push @$cmd, '-id' , $vmid ;
1987 my $socket = monitor_socket
( $vmid );
1988 push @$cmd, '-chardev' , "socket,id=monitor,path= $socket,server,nowait " ;
1989 push @$cmd, '-mon' , "chardev=monitor,mode=readline" ;
1991 $socket = vnc_socket
( $vmid );
1992 push @$cmd, '-vnc' , "unix: $socket,x509,password " ;
1994 push @$cmd, '-pidfile' , pidfile_name
( $vmid );
1996 push @$cmd, '-daemonize' ;
1998 push @$cmd, '-incoming' , $migrate_uri if $migrate_uri ;
2001 for ( my $i = 0 ; $i < $MAX_USB_DEVICES ; $i++ ) {
2002 next if ! $conf ->{ "usb $i " };
2005 # include usb device config
2006 push @$cmd, '-readconfig' , '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2 ;
2008 # enable absolute mouse coordinates (needed by vnc)
2009 my $tablet = defined ( $conf ->{ tablet
}) ?
$conf ->{ tablet
} : $defaults ->{ tablet
};
2012 push @$cmd, '-device' , 'usb-tablet,bus=ehci.0,port=6' ;
2014 push @$cmd, '-usbdevice' , 'tablet' ;
2019 for ( my $i = 0 ; $i < $MAX_HOSTPCI_DEVICES ; $i++ ) {
2020 my $d = parse_hostpci
( $conf ->{ "hostpci $i " });
2022 $pciaddr = print_pci_addr
( "hostpci $i " );
2023 push @$cmd, '-device' , "pci-assign,host= $d ->{pciid},id=hostpci $i$pciaddr " ;
2027 for ( my $i = 0 ; $i < $MAX_USB_DEVICES ; $i++ ) {
2028 my $d = parse_usb_device
( $conf ->{ "usb $i " });
2030 if ( $d ->{ vendorid
} && $d ->{ productid
}) {
2031 push @$cmd, '-device' , "usb-host,vendorid=0x $d ->{vendorid},productid=0x $d ->{productid}" ;
2032 } elsif ( defined ( $d ->{ hostbus
}) && defined ( $d ->{ hostport
})) {
2033 push @$cmd, '-device' , "usb-host,hostbus= $d ->{hostbus},hostport= $d ->{hostport}" ;
2038 for ( my $i = 0 ; $i < $MAX_SERIAL_PORTS ; $i++ ) {
2039 if ( my $path = $conf ->{ "serial $i " }) {
2040 die "no such serial device \n " if ! - c
$path ;
2041 push @$cmd, '-chardev' , "tty,id=serial $i,path = $path " ;
2042 push @$cmd, '-device' , "isa-serial,chardev=serial $i " ;
2047 for ( my $i = 0 ; $i < $MAX_PARALLEL_PORTS ; $i++ ) {
2048 if ( my $path = $conf ->{ "parallel $i " }) {
2049 die "no such parallel device \n " if ! - c
$path ;
2050 push @$cmd, '-chardev' , "parport,id=parallel $i,path = $path " ;
2051 push @$cmd, '-device' , "isa-parallel,chardev=parallel $i " ;
2055 my $vmname = $conf ->{ name
} || "vm $vmid " ;
2057 push @$cmd, '-name' , $vmname ;
2060 $sockets = $conf ->{ smp
} if $conf ->{ smp
}; # old style - no longer iused
2061 $sockets = $conf ->{ sockets
} if $conf ->{ sockets
};
2063 my $cores = $conf ->{ cores
} || 1 ;
2065 push @$cmd, '-smp' , "sockets= $sockets,cores = $cores " ;
2067 push @$cmd, '-cpu' , $conf ->{ cpu
} if $conf ->{ cpu
};
2069 push @$cmd, '-nodefaults' ;
2071 my $bootorder = $conf ->{ boot
} || $confdesc ->{ boot
}->{ default };
2073 my $bootindex_hash = {};
2075 foreach my $o ( split ( // , $bootorder )) {
2076 $bootindex_hash ->{ $o } = $i*100 ;
2080 push @$cmd, '-boot' , "menu=on" ;
2082 push @$cmd, '-no-acpi' if defined ( $conf ->{ acpi
}) && $conf ->{ acpi
} == 0 ;
2084 push @$cmd, '-no-reboot' if defined ( $conf ->{ reboot
}) && $conf ->{ reboot
} == 0 ;
2086 my $vga = $conf ->{ vga
};
2088 if ( $conf ->{ ostype
} && ( $conf ->{ ostype
} eq 'win7' || $conf ->{ ostype
} eq 'w2k8' )) {
2095 push @$cmd, '-vga' , $vga if $vga ; # for kvm 77 and later
2098 my $tdf = defined ( $conf ->{ tdf
}) ?
$conf ->{ tdf
} : $defaults ->{ tdf
};
2099 # ignore - no longer supported by newer kvm
2100 # push @$cmd, '-tdf' if $tdf;
2102 my $nokvm = defined ( $conf ->{ kvm
}) && $conf ->{ kvm
} == 0 ?
1 : 0 ;
2104 if ( my $ost = $conf ->{ ostype
}) {
2105 # other, wxp, w2k, w2k3, w2k8, wvista, win7, l24, l26
2107 if ( $ost =~ m/^w/ ) { # windows
2108 push @$cmd, '-localtime' if ! defined ( $conf ->{ localtime });
2110 # use rtc-td-hack when acpi is enabled
2111 if (!( defined ( $conf ->{ acpi
}) && $conf ->{ acpi
} == 0 )) {
2112 push @$cmd, '-rtc-td-hack' ;
2123 push @$cmd, '-no-kvm' ;
2125 die "No accelerator found! \n " if ! $cpuinfo ->{ hvm
};
2128 push @$cmd, '-localtime' if $conf ->{ localtime };
2130 push @$cmd, '-startdate' , $conf ->{ startdate
} if $conf ->{ startdate
};
2132 push @$cmd, '-S' if $conf ->{ freeze
};
2134 # set keyboard layout
2135 my $kb = $conf ->{ keyboard
} || $defaults ->{ keyboard
};
2136 push @$cmd, '-k' , $kb if $kb ;
2139 #my $soundhw = $conf->{soundhw} || $defaults->{soundhw};
2140 #push @$cmd, '-soundhw', 'es1370';
2141 #push @$cmd, '-soundhw', $soundhw if $soundhw;
2142 $pciaddr = print_pci_addr
( "balloon0" );
2143 push @$cmd, '-device' , "virtio-balloon-pci,id=balloon0 $pciaddr " if $conf ->{ balloon
};
2145 if ( $conf ->{ watchdog
}) {
2146 my $wdopts = parse_watchdog
( $conf ->{ watchdog
});
2147 $pciaddr = print_pci_addr
( "watchdog" );
2148 my $watchdog = $wdopts ->{ model
} || 'i6300esb' ;
2149 push @$cmd, '-device' , " $watchdog$pciaddr " ;
2150 push @$cmd, '-watchdog-action' , $wdopts ->{ action
} if $wdopts ->{ action
};
2154 my $scsicontroller = {};
2155 my $ahcicontroller = {};
2157 foreach_drive
( $conf, sub {
2158 my ( $ds, $drive ) = @_ ;
2160 if ( PVE
:: Storage
:: parse_volume_id
( $drive ->{ file
}, 1 )) {
2161 push @$vollist, $drive ->{ file
};
2164 $use_virtio = 1 if $ds =~ m/^virtio/ ;
2166 if ( drive_is_cdrom
( $drive )) {
2167 if ( $bootindex_hash ->{ d
}) {
2168 $drive ->{ bootindex
} = $bootindex_hash ->{ d
};
2169 $bootindex_hash ->{ d
} += 1 ;
2172 if ( $bootindex_hash ->{ c
}) {
2173 $drive ->{ bootindex
} = $bootindex_hash ->{ c
} if $conf ->{ bootdisk
} && ( $conf ->{ bootdisk
} eq $ds );
2174 $bootindex_hash ->{ c
} += 1 ;
2178 if ( $drive ->{ interface
} eq 'scsi' ) {
2180 my $controller = int ( $drive ->{ index } / $maxdev );
2181 $pciaddr = print_pci_addr
( "lsi $controller " );
2182 push @$cmd, '-device' , "lsi,id=lsi $controller$pciaddr " if ! $scsicontroller ->{ $controller };
2183 $scsicontroller ->{ $controller }= 1 ;
2186 if ( $drive ->{ interface
} eq 'sata' ) {
2187 my $controller = int ( $drive ->{ index } / $MAX_SATA_DISKS );
2188 $pciaddr = print_pci_addr
( "ahci $controller " );
2189 push @$cmd, '-device' , "ahci,id=ahci $controller,multifunction =on $pciaddr " if ! $ahcicontroller ->{ $controller };
2190 $ahcicontroller ->{ $controller }= 1 ;
2193 push @$cmd, '-drive' , print_drive_full
( $storecfg, $vmid, $drive );
2194 push @$cmd, '-device' , print_drivedevice_full
( $storecfg,$vmid, $drive );
2197 push @$cmd, '-m' , $conf ->{ memory
} || $defaults ->{ memory
};
2199 for ( my $i = 0 ; $i < $MAX_NETS ; $i++ ) {
2200 next if ! $conf ->{ "net $i " };
2201 my $d = parse_net
( $conf ->{ "net $i " });
2204 $use_virtio = 1 if $d ->{ model
} eq 'virtio' ;
2206 if ( $bootindex_hash ->{ n
}) {
2207 $d ->{ bootindex
} = $bootindex_hash ->{ n
};
2208 $bootindex_hash ->{ n
} += 1 ;
2211 my $netdevfull = print_netdev_full
( $vmid,$conf,$d, "net $i " );
2212 push @$cmd, '-netdev' , $netdevfull ;
2214 my $netdevicefull = print_netdevice_full
( $vmid,$conf,$d, "net $i " );
2215 push @$cmd, '-device' , $netdevicefull ;
2219 # hack: virtio with fairsched is unreliable, so we do not use fairsched
2220 # when the VM uses virtio devices.
2221 if (! $use_virtio && $have_ovz ) {
2223 my $cpuunits = defined ( $conf ->{ cpuunits
}) ?
2224 $conf ->{ cpuunits
} : $defaults ->{ cpuunits
};
2226 push @$cmd, '-cpuunits' , $cpuunits if $cpuunits ;
2228 # fixme: cpulimit is currently ignored
2229 #push @$cmd, '-cpulimit', $conf->{cpulimit} if $conf->{cpulimit};
2233 if ( $conf ->{ args
}) {
2234 my $aa = PVE
:: Tools
:: split_args
( $conf ->{ args
});
2238 return wantarray ?
( $cmd, $vollist ) : $cmd ;
2243 return "${var_run_tmpdir}/ $vmid .vnc" ;
2246 sub monitor_socket
{
2248 return "${var_run_tmpdir}/ $vmid .mon" ;
2253 return "${var_run_tmpdir}/ $vmid .pid" ;
2256 sub next_migrate_port
{
2258 for ( my $p = 60000 ; $p < 60010 ; $p++ ) {
2260 my $sock = IO
:: Socket
:: INET-
> new ( Listen
=> 5 ,
2261 LocalAddr
=> 'localhost' ,
2272 die "unable to find free migration port" ;
2275 sub vm_devices_list
{
2278 my $res = vm_monitor_command
( $vmid, "info pci" );
2280 my @lines = split ( " \n " , $res );
2286 foreach my $line ( @lines ) {
2288 if ( $line =~ m/^Bus (\d+), device (\d+), function (\d+):$/ ) {
2292 if ( $line =~ m/^id "([a-z][a-z_\-]*\d*)"$/ ) {
2294 $devices ->{ $id }->{ bus
}= $bus ;
2295 $devices ->{ $id }->{ addr
}= $addr ;
2303 my ( $storecfg, $conf, $vmid, $deviceid, $device ) = @_ ;
2305 return 1 if ! check_running
( $vmid ) || ! $conf ->{ hotplug
};
2307 my $devices_list = vm_devices_list
( $vmid );
2308 return 1 if defined ( $devices_list ->{ $deviceid });
2310 if ( $deviceid =~ m/^(virtio)(\d+)$/ ) {
2311 return undef if ! qemu_driveadd
( $storecfg, $vmid, $device );
2312 my $devicefull = print_drivedevice_full
( $storecfg, $vmid, $device );
2313 qemu_deviceadd
( $vmid, $devicefull );
2314 if (! qemu_deviceaddverify
( $vmid, $deviceid )) {
2315 qemu_drivedel
( $vmid, $deviceid );
2320 if ( $deviceid =~ m/^(lsi)(\d+)$/ ) {
2321 my $pciaddr = print_pci_addr
( $deviceid );
2322 my $devicefull = "lsi,id= $deviceid$pciaddr " ;
2323 qemu_deviceadd
( $vmid, $devicefull );
2324 return undef if (! qemu_deviceaddverify
( $vmid, $deviceid ));
2327 if ( $deviceid =~ m/^(scsi)(\d+)$/ ) {
2328 return undef if ! qemu_findorcreatelsi
( $storecfg,$conf, $vmid, $device );
2329 return undef if ! qemu_driveadd
( $storecfg, $vmid, $device );
2330 my $devicefull = print_drivedevice_full
( $storecfg, $vmid, $device );
2331 if (! qemu_deviceadd
( $vmid, $devicefull )) {
2332 qemu_drivedel
( $vmid, $deviceid );
2337 if ( $deviceid =~ m/^(net)(\d+)$/ ) {
2338 return undef if ! qemu_netdevadd
( $vmid, $conf, $device, $deviceid );
2339 my $netdevicefull = print_netdevice_full
( $vmid, $conf, $device, $deviceid );
2340 qemu_deviceadd
( $vmid, $netdevicefull );
2341 if (! qemu_deviceaddverify
( $vmid, $deviceid )) {
2342 qemu_netdevdel
( $vmid, $deviceid );
2350 sub vm_deviceunplug
{
2351 my ( $vmid, $conf, $deviceid ) = @_ ;
2353 return 1 if ! check_running
( $vmid ) || ! $conf ->{ hotplug
};
2355 my $devices_list = vm_devices_list
( $vmid );
2356 return 1 if ! defined ( $devices_list ->{ $deviceid });
2358 die "can't unplug bootdisk" if $conf ->{ bootdisk
} && $conf ->{ bootdisk
} eq $deviceid ;
2360 if ( $deviceid =~ m/^(virtio)(\d+)$/ ) {
2361 return undef if ! qemu_drivedel
( $vmid, $deviceid );
2362 qemu_devicedel
( $vmid, $deviceid );
2363 return undef if ! qemu_devicedelverify
( $vmid, $deviceid );
2366 if ( $deviceid =~ m/^(lsi)(\d+)$/ ) {
2367 return undef if ! qemu_devicedel
( $vmid, $deviceid );
2370 if ( $deviceid =~ m/^(scsi)(\d+)$/ ) {
2371 return undef if ! qemu_devicedel
( $vmid, $deviceid );
2372 return undef if ! qemu_drivedel
( $vmid, $deviceid );
2375 if ( $deviceid =~ m/^(net)(\d+)$/ ) {
2376 return undef if ! qemu_netdevdel
( $vmid, $deviceid );
2377 qemu_devicedel
( $vmid, $deviceid );
2378 return undef if ! qemu_devicedelverify
( $vmid, $deviceid );
2384 sub qemu_deviceadd
{
2385 my ( $vmid, $devicefull ) = @_ ;
2387 my $ret = vm_monitor_command
( $vmid, "device_add $devicefull " );
2389 # Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
2390 return 1 if $ret eq "" ;
2391 syslog
( "err" , "error on hotplug device : $ret " );
2396 sub qemu_devicedel
{
2397 my ( $vmid, $deviceid ) = @_ ;
2399 my $ret = vm_monitor_command
( $vmid, "device_del $deviceid " );
2401 return 1 if $ret eq "" ;
2402 syslog
( "err" , "detaching device $deviceid failed : $ret " );
2407 my ( $storecfg, $vmid, $device ) = @_ ;
2409 my $drive = print_drive_full
( $storecfg, $vmid, $device );
2410 my $ret = vm_monitor_command
( $vmid, "drive_add auto $drive " );
2411 # If the command succeeds qemu prints: "OK"
2412 if ( $ret !~ m/OK/s ) {
2413 syslog
( "err" , "adding drive failed: $ret " );
2420 my ( $vmid, $deviceid ) = @_ ;
2422 my $ret = vm_monitor_command
( $vmid, "drive_del drive- $deviceid " );
2424 if ( $ret =~ m/Device \'.*?\' not found/s ) {
2425 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
2427 elsif ( $ret ne "" ) {
2428 syslog
( "err" , "deleting drive $deviceid failed : $ret " );
2434 sub qemu_deviceaddverify
{
2435 my ( $vmid,$deviceid ) = @_ ;
2437 for ( my $i = 0 ; $i <= 5 ; $i++ ) {
2438 my $devices_list = vm_devices_list
( $vmid );
2439 return 1 if defined ( $devices_list ->{ $deviceid });
2442 syslog
( "err" , "error on hotplug device $deviceid " );
2447 sub qemu_devicedelverify
{
2448 my ( $vmid,$deviceid ) = @_ ;
2450 #need to verify the device is correctly remove as device_del is async and empty return is not reliable
2451 for ( my $i = 0 ; $i <= 5 ; $i++ ) {
2452 my $devices_list = vm_devices_list
( $vmid );
2453 return 1 if ! defined ( $devices_list ->{ $deviceid });
2456 syslog
( "err" , "error on hot-unplugging device $deviceid " );
2460 sub qemu_findorcreatelsi
{
2461 my ( $storecfg, $conf, $vmid, $device ) = @_ ;
2464 my $controller = int ( $device ->{ index } / $maxdev );
2465 my $lsiid = "lsi $controller " ;
2466 my $devices_list = vm_devices_list
( $vmid );
2468 if (! defined ( $devices_list ->{ $lsiid })) {
2469 return undef if ! vm_deviceplug
( $storecfg, $conf, $vmid, $lsiid );
2474 sub qemu_netdevadd
{
2475 my ( $vmid, $conf, $device, $deviceid ) = @_ ;
2477 my $netdev = print_netdev_full
( $vmid, $conf, $device, $deviceid );
2478 my $ret = vm_monitor_command
( $vmid, "netdev_add $netdev " );
2481 #if the command succeeds, no output is sent. So any non-empty string shows an error
2482 return 1 if $ret eq "" ;
2483 syslog
( "err" , "adding netdev failed: $ret " );
2487 sub qemu_netdevdel
{
2488 my ( $vmid, $deviceid ) = @_ ;
2490 my $ret = vm_monitor_command
( $vmid, "netdev_del $deviceid " );
2492 #if the command succeeds, no output is sent. So any non-empty string shows an error
2493 return 1 if $ret eq "" ;
2494 syslog
( "err" , "deleting netdev failed: $ret " );
2499 my ( $storecfg, $vmid, $statefile, $skiplock ) = @_ ;
2501 lock_config
( $vmid, sub {
2502 my $conf = load_config
( $vmid );
2504 check_lock
( $conf ) if ! $skiplock ;
2506 die "VM $vmid already running \n " if check_running
( $vmid );
2509 my $migrate_port = 0 ;
2512 if ( $statefile eq 'tcp' ) {
2513 $migrate_port = next_migrate_port
();
2514 $migrate_uri = "tcp:localhost:${migrate_port}" ;
2516 if (- f
$statefile ) {
2517 $migrate_uri = "exec:cat $statefile " ;
2519 warn "state file ' $statefile ' does not exist - doing normal startup \n " ;
2524 my $defaults = load_defaults
();
2526 my ( $cmd, $vollist ) = config_to_command
( $storecfg, $vmid, $conf, $defaults, $migrate_uri );
2528 for ( my $i = 0 ; $i < $MAX_HOSTPCI_DEVICES ; $i++ ) {
2529 my $d = parse_hostpci
( $conf ->{ "hostpci $i " });
2531 my $info = pci_device_info
( "0000: $d ->{pciid}" );
2532 die "IOMMU not present \n " if ! check_iommu_support
();
2533 die "no pci device info for device ' $d ->{pciid}' \n " if ! $info ;
2534 die "can't unbind pci device ' $d ->{pciid}' \n " if ! pci_dev_bind_to_stub
( $info );
2535 die "can't reset pci device ' $d ->{pciid}' \n " if ! pci_dev_reset
( $info );
2538 PVE
:: Storage
:: activate_volumes
( $storecfg, $vollist );
2540 eval { run_command
( $cmd, timeout
=> $migrate_uri ?
undef : 30 ); };
2542 die "start failed: $err " if $err ;
2546 if ( $statefile eq 'tcp' ) {
2547 print "migration listens on port $migrate_port\n " ;
2550 # fixme: send resume - is that necessary ?
2551 eval { vm_monitor_command
( $vmid, "cont" ); };
2555 # always set migrate speed (overwrite kvm default of 32m)
2556 # we set a very hight default of 8192m which is basically unlimited
2557 my $migrate_speed = $defaults ->{ migrate_speed
} || 8192 ;
2558 $migrate_speed = $conf ->{ migrate_speed
} || $migrate_speed ;
2560 my $cmd = "migrate_set_speed ${migrate_speed}m" ;
2561 vm_monitor_command
( $vmid, $cmd );
2564 if ( my $migrate_downtime =
2565 $conf ->{ migrate_downtime
} || $defaults ->{ migrate_downtime
}) {
2566 my $cmd = "migrate_set_downtime ${migrate_downtime}" ;
2567 eval { vm_monitor_command
( $vmid, $cmd ); };
2570 vm_balloonset
( $vmid, $conf ->{ balloon
}) if $conf ->{ balloon
};
2576 my ( $fh, $timeout ) = @_ ;
2578 my $sel = new IO
:: Select
;
2585 while ( scalar ( @ready = $sel -> can_read ( $timeout ))) {
2587 if ( $count = $fh -> sysread ( $buf, 8192 )) {
2588 if ( $buf =~ /^(.*)\(qemu\) $/s ) {
2595 if (! defined ( $count )) {
2602 die "monitor read timeout \n " if ! scalar ( @ready );
2607 sub vm_monitor_command
{
2608 my ( $vmid, $cmdstr, $nocheck ) = @_ ;
2613 die "VM $vmid not running \n " if ! check_running
( $vmid, $nocheck );
2615 my $sname = monitor_socket
( $vmid );
2617 my $sock = IO
:: Socket
:: UNIX-
> new ( Peer
=> $sname ) ||
2618 die "unable to connect to VM $vmid socket - $!\n " ;
2622 # hack: migrate sometime blocks the monitor (when migrate_downtime
2624 if ( $cmdstr =~ m/^(info\s+migrate|migrate\s)/ ) {
2625 $timeout = 60 * 60 ; # 1 hour
2629 my $data = __read_avail
( $sock, $timeout );
2631 if ( $data !~ m/^QEMU\s+(\S+)\s+monitor\s/ ) {
2632 die "got unexpected qemu monitor banner \n " ;
2635 my $sel = new IO
:: Select
;
2638 if (! scalar ( my @ready = $sel -> can_write ( $timeout ))) {
2639 die "monitor write error - timeout" ;
2642 my $fullcmd = " $cmdstr\r " ;
2644 # syslog('info', "VM $vmid monitor command: $cmdstr");
2647 if (!( $b = $sock -> syswrite ( $fullcmd )) || ( $b != length ( $fullcmd ))) {
2648 die "monitor write error - $! " ;
2651 return if ( $cmdstr eq 'q' ) || ( $cmdstr eq 'quit' );
2655 if ( $cmdstr =~ m/^(info\s+migrate|migrate\s)/ ) {
2656 $timeout = 60 * 60 ; # 1 hour
2657 } elsif ( $cmdstr =~ m/^(eject|change)/ ) {
2658 $timeout = 60 ; # note: cdrom mount command is slow
2660 if ( $res = __read_avail
( $sock, $timeout )) {
2662 my @lines = split ( " \r ? \n " , $res );
2664 shift @lines if $lines [ 0 ] !~ m/^unknown command/ ; # skip echo
2666 $res = join ( " \n " , @lines );
2674 syslog
( "err" , "VM $vmid monitor command failed - $err " );
2681 sub vm_commandline
{
2682 my ( $storecfg, $vmid ) = @_ ;
2684 my $conf = load_config
( $vmid );
2686 my $defaults = load_defaults
();
2688 my $cmd = config_to_command
( $storecfg, $vmid, $conf, $defaults );
2690 return join ( ' ' , @$cmd );
2694 my ( $vmid, $skiplock ) = @_ ;
2696 lock_config
( $vmid, sub {
2698 my $conf = load_config
( $vmid );
2700 check_lock
( $conf ) if ! $skiplock ;
2702 vm_monitor_command
( $vmid, "system_reset" );
2706 sub get_vm_volumes
{
2710 foreach_drive
( $conf, sub {
2711 my ( $ds, $drive ) = @_ ;
2713 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $drive ->{ file
}, 1 );
2716 my $volid = $drive ->{ file
};
2717 return if ! $volid || $volid =~ m
|^/|;
2719 push @$vollist, $volid ;
2725 sub vm_stop_cleanup
{
2726 my ( $storecfg, $vmid, $conf, $keepActive ) = @_ ;
2729 fairsched_rmnod
( $vmid ); # try to destroy group
2732 my $vollist = get_vm_volumes
( $conf );
2733 PVE
:: Storage
:: deactivate_volumes
( $storecfg, $vollist );
2736 foreach my $ext ( qw(mon pid vnc) ) {
2737 unlink "/var/run/qemu-server/${vmid}. $ext " ;
2740 warn $@ if $@ ; # avoid errors - just warn
2743 # Note: use $nockeck to skip tests if VM configuration file exists.
2744 # We need that when migration VMs to other nodes (files already moved)
2745 # Note: we set $keepActive in vzdump stop mode - volumes need to stay active
2747 my ( $storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive ) = @_ ;
2749 $timeout = 60 if ! defined ( $timeout );
2751 $force = 1 if ! defined ( $force ) && ! $shutdown ;
2753 lock_config
( $vmid, sub {
2755 my $pid = check_running
( $vmid, $nocheck );
2760 $conf = load_config
( $vmid );
2761 check_lock
( $conf ) if ! $skiplock ;
2766 vm_monitor_command
( $vmid, "system_powerdown" , $nocheck );
2768 vm_monitor_command
( $vmid, "quit" , $nocheck );
2775 while (( $count < $timeout ) && check_running
( $vmid, $nocheck )) {
2780 if ( $count >= $timeout ) {
2782 warn "VM still running - terminating now with SIGTERM \n " ;
2785 die "VM quit/powerdown failed - got timeout \n " ;
2788 vm_stop_cleanup
( $storecfg, $vmid, $conf, $keepActive ) if $conf ;
2793 warn "VM quit/powerdown failed - terminating now with SIGTERM \n " ;
2796 die "VM quit/powerdown failed \n " ;
2804 while (( $count < $timeout ) && check_running
( $vmid, $nocheck )) {
2809 if ( $count >= $timeout ) {
2810 warn "VM still running - terminating now with SIGKILL \n " ;
2815 vm_stop_cleanup
( $storecfg, $vmid, $conf, $keepActive ) if $conf ;
2820 my ( $vmid, $skiplock ) = @_ ;
2822 lock_config
( $vmid, sub {
2824 my $conf = load_config
( $vmid );
2826 check_lock
( $conf ) if ! $skiplock ;
2828 vm_monitor_command
( $vmid, "stop" );
2833 my ( $vmid, $skiplock ) = @_ ;
2835 lock_config
( $vmid, sub {
2837 my $conf = load_config
( $vmid );
2839 check_lock
( $conf ) if ! $skiplock ;
2841 vm_monitor_command
( $vmid, "cont" );
2846 my ( $vmid, $skiplock, $key ) = @_ ;
2848 lock_config
( $vmid, sub {
2850 my $conf = load_config
( $vmid );
2852 vm_monitor_command
( $vmid, "sendkey $key " );
2857 my ( $storecfg, $vmid, $skiplock ) = @_ ;
2859 lock_config
( $vmid, sub {
2861 my $conf = load_config
( $vmid );
2863 check_lock
( $conf ) if ! $skiplock ;
2865 if (! check_running
( $vmid )) {
2866 fairsched_rmnod
( $vmid ); # try to destroy group
2867 destroy_vm
( $storecfg, $vmid );
2869 die "VM $vmid is running - destroy failed \n " ;
2875 my ( $storecfg, $timeout ) = @_ ;
2877 $timeout = 3 * 60 if ! $timeout ;
2879 my $cleanuphash = {};
2881 my $vzlist = vzlist
();
2883 foreach my $vmid ( keys %$vzlist ) {
2884 next if ! $vzlist ->{ $vmid }->{ pid
};
2886 $cleanuphash ->{ $vmid } = 1 ;
2891 my $msg = "Stopping Qemu Server - sending shutdown requests to all VMs \n " ;
2892 syslog
( 'info' , $msg );
2895 foreach my $vmid ( keys %$vzlist ) {
2896 next if ! $vzlist ->{ $vmid }->{ pid
};
2897 eval { vm_monitor_command
( $vmid, "system_powerdown" ); };
2902 my $maxtries = int (( $timeout + $wt - 1 )/ $wt );
2904 while (( $try < $maxtries ) && $count ) {
2910 foreach my $vmid ( keys %$vzlist ) {
2911 next if ! $vzlist ->{ $vmid }->{ pid
};
2919 foreach my $vmid ( keys %$vzlist ) {
2920 next if ! $vzlist ->{ $vmid }->{ pid
};
2922 warn "VM $vmid still running - sending stop now \n " ;
2923 eval { vm_monitor_command
( $vmid, "quit" ); };
2928 $maxtries = int (( $timeout + $wt - 1 )/ $wt );
2930 while (( $try < $maxtries ) && $count ) {
2936 foreach my $vmid ( keys %$vzlist ) {
2937 next if ! $vzlist ->{ $vmid }->{ pid
};
2945 foreach my $vmid ( keys %$vzlist ) {
2946 next if ! $vzlist ->{ $vmid }->{ pid
};
2948 warn "VM $vmid still running - terminating now with SIGTERM \n " ;
2949 kill 15 , $vzlist ->{ $vmid }->{ pid
};
2954 # this is called by system shotdown scripts, so remaining
2955 # processes gets killed anyways (no need to send kill -9 here)
2959 foreach my $vmid ( keys %$cleanuphash ) {
2960 next if $vzlist ->{ $vmid }->{ pid
};
2962 my $conf = load_config
( $vmid );
2963 vm_stop_cleanup
( $storecfg, $vmid, $conf );
2968 $msg = "Qemu Server stopped \n " ;
2969 syslog
( 'info' , $msg );
2976 my ( $filename, $buf ) = @_ ;
2978 my $fh = IO
:: File-
> new ( $filename, "w" );
2979 return undef if ! $fh ;
2981 my $res = print $fh $buf ;
2988 sub pci_device_info
{
2993 return undef if $name !~ m/^([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])$/ ;
2994 my ( $domain, $bus, $slot, $func ) = ( $1, $2, $3, $4 );
2996 my $irq = file_read_firstline
( " $pcisysfs/devices/$name/irq " );
2997 return undef if ! defined ( $irq ) || $irq !~ m/^\d+$/ ;
2999 my $vendor = file_read_firstline
( " $pcisysfs/devices/$name/vendor " );
3000 return undef if ! defined ( $vendor ) || $vendor !~ s/^0x// ;
3002 my $product = file_read_firstline
( " $pcisysfs/devices/$name/device " );
3003 return undef if ! defined ( $product ) || $product !~ s/^0x// ;
3008 product
=> $product,
3014 has_fl_reset
=> - f
" $pcisysfs/devices/$name/reset " || 0 ,
3023 my $name = $dev ->{ name
};
3025 my $fn = " $pcisysfs/devices/$name/reset " ;
3027 return file_write
( $fn, "1" );
3030 sub pci_dev_bind_to_stub
{
3033 my $name = $dev ->{ name
};
3035 my $testdir = " $pcisysfs/drivers/pci -stub/ $name " ;
3036 return 1 if - d
$testdir ;
3038 my $data = " $dev ->{vendor} $dev ->{product}" ;
3039 return undef if ! file_write
( " $pcisysfs/drivers/pci -stub/new_id" , $data );
3041 my $fn = " $pcisysfs/devices/$name/driver/unbind " ;
3042 if (! file_write
( $fn, $name )) {
3043 return undef if - f
$fn ;
3046 $fn = " $pcisysfs/drivers/pci -stub/bind" ;
3047 if (! - d
$testdir ) {
3048 return undef if ! file_write
( $fn, $name );
3054 sub print_pci_addr
{
3059 #addr1 : ide,parallel,serial (motherboard)
3060 #addr2 : first videocard
3061 balloon0
=> { bus
=> 0 , addr
=> 3 },
3062 watchdog
=> { bus
=> 0 , addr
=> 4 },
3063 lsi0
=> { bus
=> 0 , addr
=> 5 },
3064 lsi1
=> { bus
=> 0 , addr
=> 6 },
3065 ahci0
=> { bus
=> 0 , addr
=> 7 },
3066 virtio0
=> { bus
=> 0 , addr
=> 10 },
3067 virtio1
=> { bus
=> 0 , addr
=> 11 },
3068 virtio2
=> { bus
=> 0 , addr
=> 12 },
3069 virtio3
=> { bus
=> 0 , addr
=> 13 },
3070 virtio4
=> { bus
=> 0 , addr
=> 14 },
3071 virtio5
=> { bus
=> 0 , addr
=> 15 },
3072 hostpci0
=> { bus
=> 0 , addr
=> 16 },
3073 hostpci1
=> { bus
=> 0 , addr
=> 17 },
3074 net0
=> { bus
=> 0 , addr
=> 18 },
3075 net1
=> { bus
=> 0 , addr
=> 19 },
3076 net2
=> { bus
=> 0 , addr
=> 20 },
3077 net3
=> { bus
=> 0 , addr
=> 21 },
3078 net4
=> { bus
=> 0 , addr
=> 22 },
3079 net5
=> { bus
=> 0 , addr
=> 23 },
3080 #addr29 : usb-host (pve-usb.cfg)
3083 if ( defined ( $devices ->{ $id }->{ bus
}) && defined ( $devices ->{ $id }->{ addr
})) {
3084 my $addr = sprintf ( "0x %x " , $devices ->{ $id }->{ addr
});
3085 $res = ",bus=pci. $devices ->{ $id }->{bus},addr= $addr " ;
3092 my ( $vmid, $value ) = @_ ;
3094 vm_monitor_command
( $vmid, "balloon $value " );
3097 # vzdump restore implementaion
3099 sub archive_read_firstfile
{
3100 my $archive = shift ;
3102 die "ERROR: file ' $archive ' does not exist \n " if ! - f
$archive ;
3104 # try to detect archive type first
3105 my $pid = open ( TMP
, "tar tf ' $archive '|" ) ||
3106 die "unable to open file ' $archive ' \n " ;
3107 my $firstfile = < TMP
>;
3111 die "ERROR: archive contaions no data \n " if ! $firstfile ;
3117 sub restore_cleanup
{
3118 my $statfile = shift ;
3120 print STDERR
"starting cleanup \n " ;
3122 if ( my $fd = IO
:: File-
> new ( $statfile, "r" )) {
3123 while ( defined ( my $line = < $fd >)) {
3124 if ( $line =~ m/vzdump:([^\s:]*):(\S+)$/ ) {
3127 if ( $volid =~ m
|^/|) {
3128 unlink $volid || die 'unlink failed \n ' ;
3130 my $cfg = cfs_read_file
( 'storage.cfg' );
3131 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
3133 print STDERR
"temporary volume ' $volid ' sucessfuly removed \n " ;
3135 print STDERR
"unable to cleanup ' $volid ' - $@ " if $@ ;
3137 print STDERR
"unable to parse line in statfile - $line " ;
3144 sub restore_archive
{
3145 my ( $archive, $vmid, $user, $opts ) = @_ ;
3147 if ( $archive ne '-' ) {
3148 my $firstfile = archive_read_firstfile
( $archive );
3149 die "ERROR: file ' $archive ' dos not lock like a QemuServer vzdump backup \n "
3150 if $firstfile ne 'qemu-server.conf' ;
3153 my $tocmd = "/usr/lib/qemu-server/qmextract" ;
3155 $tocmd .= " --storage " . PVE
:: Tools
:: shellquote
( $opts ->{ storage
}) if $opts ->{ storage
};
3156 $tocmd .= " --pool " . PVE
:: Tools
:: shellquote
( $opts ->{ pool
}) if $opts ->{ pool
};
3157 $tocmd .= ' --prealloc' if $opts ->{ prealloc
};
3158 $tocmd .= ' --info' if $opts ->{ info
};
3160 # tar option "xf" does not autodetect compression when read from STDIN,
3161 # so we pipe to zcat
3162 my $cmd = "zcat -f|tar xf " . PVE
:: Tools
:: shellquote
( $archive ) . " " .
3163 PVE
:: Tools
:: shellquote
( "--to-command= $tocmd " );
3165 my $tmpdir = "/var/tmp/vzdumptmp $$ " ;
3168 local $ENV { VZDUMP_TMPDIR
} = $tmpdir ;
3169 local $ENV { VZDUMP_VMID
} = $vmid ;
3170 local $ENV { VZDUMP_USER
} = $user ;
3172 my $conffile = PVE
:: QemuServer
:: config_file
( $vmid );
3173 my $tmpfn = " $conffile . $$ .tmp" ;
3175 # disable interrupts (always do cleanups)
3176 local $SIG { INT
} = $SIG { TERM
} = $SIG { QUIT
} = $SIG { HUP
} = sub {
3177 print STDERR
"got interrupt - ignored \n " ;
3182 local $SIG { INT
} = $SIG { TERM
} = $SIG { QUIT
} = $SIG { HUP
} = $SIG { PIPE
} = sub {
3183 die "interrupted by signal \n " ;
3186 if ( $archive eq '-' ) {
3187 print "extracting archive from STDIN \n " ;
3188 run_command
( $cmd, input
=> "<&STDIN" );
3190 print "extracting archive ' $archive ' \n " ;
3194 return if $opts ->{ info
};
3198 my $statfile = " $tmpdir/qmrestore .stat" ;
3199 if ( my $fd = IO
:: File-
> new ( $statfile, "r" )) {
3200 while ( defined ( my $line = < $fd >)) {
3201 if ( $line =~ m/vzdump:([^\s:]*):(\S+)$/ ) {
3202 $map ->{ $1 } = $2 if $1 ;
3204 print STDERR
"unable to parse line in statfile - $line\n " ;
3210 my $confsrc = " $tmpdir/qemu -server.conf" ;
3212 my $srcfd = new IO
:: File
( $confsrc, "r" ) ||
3213 die "unable to open file ' $confsrc ' \n " ;
3215 my $outfd = new IO
:: File
( $tmpfn, "w" ) ||
3216 die "unable to write config for VM $vmid\n " ;
3220 while ( defined ( my $line = < $srcfd >)) {
3221 next if $line =~ m/^\#vzdump\#/ ;
3222 next if $line =~ m/^lock:/ ;
3223 next if $line =~ m/^unused\d+:/ ;
3225 if (( $line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/ )) {
3226 # try to convert old 1.X settings
3227 my ( $id, $ind, $ethcfg ) = ( $1, $2, $3 );
3228 foreach my $devconfig ( PVE
:: Tools
:: split_list
( $ethcfg )) {
3229 my ( $model, $macaddr ) = split ( /\=/ , $devconfig );
3230 $macaddr = PVE
:: Tools
:: random_ether_addr
() if ! $macaddr || $opts ->{ unique
};
3233 bridge
=> "vmbr $ind " ,
3234 macaddr
=> $macaddr,
3236 my $netstr = print_net
( $net );
3237 print $outfd "net${netcount}: $netstr\n " ;
3240 } elsif (( $line =~ m/^(net\d+):\s*(\S+)\s*$/ ) && ( $opts ->{ unique
})) {
3241 my ( $id, $netstr ) = ( $1, $2 );
3242 my $net = parse_net
( $netstr );
3243 $net ->{ macaddr
} = PVE
:: Tools
:: random_ether_addr
() if $net ->{ macaddr
};
3244 $netstr = print_net
( $net );
3245 print $outfd " $id : $netstr\n " ;
3246 } elsif ( $line =~ m/^((ide|scsi|virtio)\d+):\s*(\S+)\s*$/ ) {
3249 if ( $line =~ m/backup=no/ ) {
3250 print $outfd "# $line " ;
3251 } elsif ( $virtdev && $map ->{ $virtdev }) {
3252 my $di = PVE
:: QemuServer
:: parse_drive
( $virtdev, $value );
3253 $di ->{ file
} = $map ->{ $virtdev };
3254 $value = PVE
:: QemuServer
:: print_drive
( $vmid, $di );
3255 print $outfd " $virtdev : $value\n " ;
3273 restore_cleanup
( " $tmpdir/qmrestore .stat" ) if ! $opts ->{ info
};
3280 rename $tmpfn, $conffile ||
3281 die "unable to commit configuration file ' $conffile ' \n " ;