2011-08-23 09:47:04 +04:00
package PVE::QemuServer ;
use strict ;
use POSIX ;
use IO::Handle ;
use IO::Select ;
use IO::File ;
use IO::Dir ;
use IO::Socket::UNIX ;
use File::Basename ;
use File::Path ;
use File::stat ;
use Getopt::Long ;
use Digest::SHA1 ;
use Fcntl ':flock' ;
use Cwd 'abs_path' ;
use IPC::Open3 ;
use Fcntl ;
use PVE::SafeSyslog ;
use Storable qw( dclone ) ;
use PVE::Exception qw( raise raise_param_exc ) ;
use PVE::Storage ;
use PVE::Tools qw( run_command lock_file file_read_firstline ) ;
use PVE::Cluster qw( cfs_register_file cfs_read_file cfs_write_file cfs_lock_file ) ;
use PVE::INotify ;
use PVE::ProcFSTools ;
2011-09-15 11:11:27 +04:00
use Time::HiRes qw( gettimeofday ) ;
2011-08-23 09:47:04 +04:00
2011-09-12 13:03:14 +04:00
my $ cpuinfo = PVE::ProcFSTools:: read_cpuinfo ( ) ;
2011-08-23 09:47:04 +04:00
2011-09-12 14:26:00 +04:00
# Note about locking: we use flock on the config file protect
2011-08-23 09:47:04 +04:00
# against concurent actions.
# Aditionaly, we have a 'lock' setting in the config file. This
# can be set to 'migrate' or 'backup'. Most actions are not
# allowed when such lock is set. But you can ignore this kind of
# lock with the --skiplock flag.
2012-02-02 17:01:08 +04:00
cfs_register_file ( '/qemu-server/' ,
\ & parse_vm_config ,
\ & write_vm_config ) ;
2011-08-23 09:47:04 +04:00
2011-09-14 14:02:08 +04:00
PVE::JSONSchema:: register_standard_option ( 'skiplock' , {
description = > "Ignore locks - only root is allowed to use this option." ,
2012-01-27 12:35:26 +04:00
type = > 'boolean' ,
2011-09-14 14:02:08 +04:00
optional = > 1 ,
} ) ;
PVE::JSONSchema:: register_standard_option ( 'pve-qm-stateuri' , {
description = > "Some command save/restore state from this location." ,
type = > 'string' ,
maxLength = > 128 ,
optional = > 1 ,
} ) ;
2011-08-23 09:47:04 +04:00
#no warnings 'redefine';
unless ( defined ( & _VZSYSCALLS_H_ ) ) {
eval 'sub _VZSYSCALLS_H_ () {1;}' unless defined ( & _VZSYSCALLS_H_ ) ;
require 'sys/syscall.ph' ;
if ( defined ( & __x86_64__ ) ) {
eval 'sub __NR_fairsched_vcpus () {499;}' unless defined ( & __NR_fairsched_vcpus ) ;
eval 'sub __NR_fairsched_mknod () {504;}' unless defined ( & __NR_fairsched_mknod ) ;
eval 'sub __NR_fairsched_rmnod () {505;}' unless defined ( & __NR_fairsched_rmnod ) ;
eval 'sub __NR_fairsched_chwt () {506;}' unless defined ( & __NR_fairsched_chwt ) ;
eval 'sub __NR_fairsched_mvpr () {507;}' unless defined ( & __NR_fairsched_mvpr ) ;
eval 'sub __NR_fairsched_rate () {508;}' unless defined ( & __NR_fairsched_rate ) ;
eval 'sub __NR_setluid () {501;}' unless defined ( & __NR_setluid ) ;
eval 'sub __NR_setublimit () {502;}' unless defined ( & __NR_setublimit ) ;
}
elsif ( defined ( & __i386__ ) ) {
eval 'sub __NR_fairsched_mknod () {500;}' unless defined ( & __NR_fairsched_mknod ) ;
eval 'sub __NR_fairsched_rmnod () {501;}' unless defined ( & __NR_fairsched_rmnod ) ;
eval 'sub __NR_fairsched_chwt () {502;}' unless defined ( & __NR_fairsched_chwt ) ;
eval 'sub __NR_fairsched_mvpr () {503;}' unless defined ( & __NR_fairsched_mvpr ) ;
eval 'sub __NR_fairsched_rate () {504;}' unless defined ( & __NR_fairsched_rate ) ;
eval 'sub __NR_fairsched_vcpus () {505;}' unless defined ( & __NR_fairsched_vcpus ) ;
eval 'sub __NR_setluid () {511;}' unless defined ( & __NR_setluid ) ;
eval 'sub __NR_setublimit () {512;}' unless defined ( & __NR_setublimit ) ;
} else {
die ( "no fairsched syscall for this arch" ) ;
}
require 'asm/ioctl.ph' ;
eval 'sub KVM_GET_API_VERSION () { &_IO(0xAE, 0x);}' unless defined ( & KVM_GET_API_VERSION ) ;
}
sub fairsched_mknod {
my ( $ parent , $ weight , $ desired ) = @ _ ;
2011-09-15 11:11:27 +04:00
return syscall ( & __NR_fairsched_mknod , int ( $ parent ) , int ( $ weight ) , int ( $ desired ) ) ;
2011-08-23 09:47:04 +04:00
}
sub fairsched_rmnod {
my ( $ id ) = @ _ ;
2011-09-15 11:11:27 +04:00
return syscall ( & __NR_fairsched_rmnod , int ( $ id ) ) ;
2011-08-23 09:47:04 +04:00
}
sub fairsched_mvpr {
my ( $ pid , $ newid ) = @ _ ;
2011-09-15 11:11:27 +04:00
return syscall ( & __NR_fairsched_mvpr , int ( $ pid ) , int ( $ newid ) ) ;
2011-08-23 09:47:04 +04:00
}
sub fairsched_vcpus {
my ( $ id , $ vcpus ) = @ _ ;
2011-09-15 11:11:27 +04:00
return syscall ( & __NR_fairsched_vcpus , int ( $ id ) , int ( $ vcpus ) ) ;
2011-08-23 09:47:04 +04:00
}
sub fairsched_rate {
my ( $ id , $ op , $ rate ) = @ _ ;
2011-09-15 11:11:27 +04:00
return syscall ( & __NR_fairsched_rate , int ( $ id ) , int ( $ op ) , int ( $ rate ) ) ;
2011-08-23 09:47:04 +04:00
}
use constant FAIRSCHED_SET_RATE = > 0 ;
use constant FAIRSCHED_DROP_RATE = > 1 ;
use constant FAIRSCHED_GET_RATE = > 2 ;
sub fairsched_cpulimit {
my ( $ id , $ limit ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ cpulim1024 = int ( $ limit * 1024 / 100 ) ;
2011-08-23 09:47:04 +04:00
my $ op = $ cpulim1024 ? FAIRSCHED_SET_RATE : FAIRSCHED_DROP_RATE ;
2011-09-15 11:11:27 +04:00
return fairsched_rate ( $ id , $ op , $ cpulim1024 ) ;
2011-08-23 09:47:04 +04:00
}
my $ nodename = PVE::INotify:: nodename ( ) ;
mkdir "/etc/pve/nodes/$nodename" ;
my $ confdir = "/etc/pve/nodes/$nodename/qemu-server" ;
mkdir $ confdir ;
my $ var_run_tmpdir = "/var/run/qemu-server" ;
mkdir $ var_run_tmpdir ;
my $ lock_dir = "/var/lock/qemu-server" ;
mkdir $ lock_dir ;
my $ pcisysfs = "/sys/bus/pci" ;
my $ confdesc = {
onboot = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Specifies whether a VM will be started during system bootup." ,
default = > 0 ,
} ,
autostart = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Automatic restart after crash (currently ignored)." ,
default = > 0 ,
} ,
2011-10-10 18:46:53 +04:00
hotplug = > {
optional = > 1 ,
2011-10-11 15:20:59 +04:00
type = > 'boolean' ,
2011-10-10 18:46:53 +04:00
description = > "Activate hotplug for disk and network device" ,
default = > 0 ,
} ,
2011-08-23 09:47:04 +04:00
reboot = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Allow reboot. If set to '0' the VM exit on reboot." ,
default = > 1 ,
} ,
lock = > {
optional = > 1 ,
type = > 'string' ,
description = > "Lock/unlock the VM." ,
enum = > [ qw( migrate backup ) ] ,
} ,
cpulimit = > {
optional = > 1 ,
type = > 'integer' ,
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." ,
minimum = > 0 ,
default = > 0 ,
} ,
cpuunits = > {
optional = > 1 ,
type = > 'integer' ,
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." ,
minimum = > 0 ,
maximum = > 500000 ,
default = > 1000 ,
} ,
memory = > {
optional = > 1 ,
type = > 'integer' ,
2011-09-16 08:57:54 +04:00
description = > "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device." ,
2011-08-23 09:47:04 +04:00
minimum = > 16 ,
default = > 512 ,
} ,
2011-09-16 06:46:26 +04:00
balloon = > {
optional = > 1 ,
type = > 'integer' ,
description = > "Amount of target RAM for the VM in MB." ,
2011-09-16 08:57:54 +04:00
minimum = > 16 ,
2011-09-16 06:46:26 +04:00
} ,
2011-08-23 09:47:04 +04:00
keyboard = > {
optional = > 1 ,
type = > 'string' ,
description = > "Keybord layout for vnc server. Default is read from the datacenter configuration file." ,
2012-01-09 14:25:25 +04:00
enum = > PVE::Tools:: kvmkeymaplist ( ) ,
2011-08-23 09:47:04 +04:00
default = > 'en-us' ,
} ,
name = > {
optional = > 1 ,
type = > 'string' ,
description = > "Set a name for the VM. Only used on the configuration web interface." ,
} ,
description = > {
optional = > 1 ,
type = > 'string' ,
description = > "Description for the VM. Only used on the configuration web interface." ,
} ,
ostype = > {
optional = > 1 ,
type = > 'string' ,
enum = > [ qw( other wxp w2k w2k3 w2k8 wvista win7 l24 l26 ) ] ,
description = > << EODESC ,
Used to enable special optimization / features for specific
operating systems:
other = > unspecified OS
wxp = > Microsoft Windows XP
w2k = > Microsoft Windows 2000
w2k3 = > Microsoft Windows 2003
w2k8 = > Microsoft Windows 2008
wvista = > Microsoft Windows Vista
win7 = > Microsoft Windows 7
l24 = > Linux 2.4 Kernel
l26 = > Linux 2.6 / 3 . X Kernel
other | l24 | l26 ... no special behaviour
wxp | w2k | w2k3 | w2k8 | wvista | win7 ... use - - localtime switch
EODESC
} ,
boot = > {
optional = > 1 ,
type = > 'string' ,
description = > "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)." ,
pattern = > '[acdn]{1,4}' ,
2011-09-29 09:43:05 +04:00
default = > 'cdn' ,
2011-08-23 09:47:04 +04:00
} ,
bootdisk = > {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-bootdisk' ,
description = > "Enable booting from specified disk." ,
pattern = > '(ide|scsi|virtio)\d+' ,
} ,
smp = > {
optional = > 1 ,
type = > 'integer' ,
description = > "The number of CPUs. Please use option -sockets instead." ,
minimum = > 1 ,
default = > 1 ,
} ,
sockets = > {
optional = > 1 ,
type = > 'integer' ,
description = > "The number of CPU sockets." ,
minimum = > 1 ,
default = > 1 ,
} ,
cores = > {
optional = > 1 ,
type = > 'integer' ,
description = > "The number of cores per socket." ,
minimum = > 1 ,
default = > 1 ,
} ,
acpi = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable ACPI." ,
default = > 1 ,
} ,
kvm = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable KVM hardware virtualization." ,
default = > 1 ,
} ,
tdf = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable time drift fix." ,
default = > 1 ,
} ,
2011-09-12 14:26:00 +04:00
localtime = > {
2011-08-23 09:47:04 +04:00
optional = > 1 ,
type = > 'boolean' ,
description = > "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS." ,
} ,
freeze = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Freeze CPU at startup (use 'c' monitor command to start execution)." ,
} ,
vga = > {
optional = > 1 ,
type = > 'string' ,
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" ,
enum = > [ qw( std cirrus vmware ) ] ,
} ,
2011-09-08 13:39:56 +04:00
watchdog = > {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-watchdog' ,
typetext = > '[[model=]i6300esb|ib700] [,[action=]reset|shutdown|poweroff|pause|debug|none]' ,
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)" ,
} ,
2011-08-23 09:47:04 +04:00
startdate = > {
optional = > 1 ,
2011-09-12 14:26:00 +04:00
type = > 'string' ,
2011-08-23 09:47:04 +04:00
typetext = > "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)" ,
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'." ,
pattern = > '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)' ,
default = > 'now' ,
} ,
args = > {
optional = > 1 ,
type = > 'string' ,
description = > << EODESCR ,
Note: this option is for experts only . It allows you to pass arbitrary arguments to kvm , for example:
args: - no - reboot - no - hpet
EODESCR
} ,
tablet = > {
optional = > 1 ,
type = > 'boolean' ,
default = > 1 ,
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." ,
} ,
migrate_speed = > {
optional = > 1 ,
type = > 'integer' ,
description = > "Set maximum speed (in MB/s) for migrations. Value 0 is no limit." ,
minimum = > 0 ,
default = > 0 ,
} ,
migrate_downtime = > {
optional = > 1 ,
type = > 'integer' ,
description = > "Set maximum tolerated downtime (in seconds) for migrations." ,
minimum = > 0 ,
default = > 1 ,
} ,
cdrom = > {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-drive' ,
typetext = > 'volume' ,
description = > "This is an alias for option -ide2" ,
} ,
cpu = > {
optional = > 1 ,
description = > "Emulated CPU type." ,
type = > 'string' ,
enum = > [ qw( 486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom host ) ] ,
default = > 'qemu64' ,
} ,
} ;
# what about other qemu settings ?
#cpu => 'string',
#machine => 'string',
#fda => 'file',
#fdb => 'file',
#mtdblock => 'file',
#sd => 'file',
#pflash => 'file',
#snapshot => 'bool',
#bootp => 'file',
##tftp => 'dir',
##smb => 'dir',
#kernel => 'file',
#append => 'string',
#initrd => 'file',
##soundhw => 'string',
while ( my ( $ k , $ v ) = each %$ confdesc ) {
PVE::JSONSchema:: register_standard_option ( "pve-qm-$k" , $ v ) ;
}
my $ MAX_IDE_DISKS = 4 ;
2011-09-07 17:34:36 +04:00
my $ MAX_SCSI_DISKS = 14 ;
my $ MAX_VIRTIO_DISKS = 6 ;
2012-02-01 16:25:19 +04:00
my $ MAX_SATA_DISKS = 6 ;
2011-08-23 09:47:04 +04:00
my $ MAX_USB_DEVICES = 5 ;
2011-09-07 17:34:36 +04:00
my $ MAX_NETS = 6 ;
2011-08-23 09:47:04 +04:00
my $ MAX_UNUSED_DISKS = 8 ;
2011-09-11 10:59:59 +04:00
my $ MAX_HOSTPCI_DEVICES = 2 ;
2011-09-11 11:00:00 +04:00
my $ MAX_SERIAL_PORTS = 4 ;
2011-09-11 11:00:01 +04:00
my $ MAX_PARALLEL_PORTS = 3 ;
2011-08-23 09:47:04 +04:00
my $ nic_model_list = [ 'rtl8139' , 'ne2k_pci' , 'e1000' , 'pcnet' , 'virtio' ,
'ne2k_isa' , 'i82551' , 'i82557b' , 'i82559er' ] ;
2011-09-15 11:11:27 +04:00
my $ nic_model_list_txt = join ( ' ' , sort @$ nic_model_list ) ;
2011-08-23 09:47:04 +04:00
# fixme:
my $ netdesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-net' ,
typetext = > "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>]" ,
description = > << EODESCR ,
2011-09-12 14:26:00 +04:00
Specify network devices .
2011-08-23 09:47:04 +04:00
MODEL is one of: $ nic_model_list_txt
2011-09-12 14:26:00 +04:00
XX:XX:XX:XX:XX:XX should be an unique MAC address . This is
2011-08-23 09:47:04 +04:00
automatically generated if not specified .
The bridge parameter can be used to automatically add the interface to a bridge device . The Proxmox VE standard bridge is called 'vmbr0' .
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' .
If you specify no bridge , we create a kvm 'user' ( NATed ) network device , which provides DHCP and DNS services . The following addresses are used:
10.0 .2 .2 Gateway
10.0 .2 .3 DNS Server
10.0 .2 .4 SMB Server
The DHCP server assign addresses to the guest starting from 10.0 .2 .15 .
EODESCR
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-net" , $ netdesc ) ;
for ( my $ i = 0 ; $ i < $ MAX_NETS ; $ i + + ) {
$ confdesc - > { "net$i" } = $ netdesc ;
}
my $ drivename_hash ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
my $ idedesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-drive' ,
2011-11-18 08:48:52 +04:00
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]' ,
2011-08-23 09:47:04 +04:00
description = > "Use volume as IDE hard disk or CD-ROM (n is 0 to 3)." ,
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-ide" , $ idedesc ) ;
my $ scsidesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-drive' ,
2011-12-13 12:59:28 +04:00
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]' ,
2011-09-12 09:08:01 +04:00
description = > "Use volume as SCSI hard disk or CD-ROM (n is 0 to 13)." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-scsi" , $ scsidesc ) ;
2012-02-01 16:25:19 +04:00
my $ satadesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-drive' ,
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]' ,
description = > "Use volume as SATA hard disk or CD-ROM (n is 0 to 5)." ,
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-sata" , $ satadesc ) ;
2011-08-23 09:47:04 +04:00
my $ virtiodesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-drive' ,
2011-11-18 08:48:52 +04:00
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]' ,
2011-09-12 09:08:01 +04:00
description = > "Use volume as VIRTIO hard disk (n is 0 to 5)." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-virtio" , $ virtiodesc ) ;
my $ usbdesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-usb-device' ,
typetext = > 'host=HOSTUSBDEVICE' ,
description = > << EODESCR ,
2011-09-12 09:08:01 +04:00
Configure an USB device ( n is 0 to 4 ) . This can be used to
2011-08-23 09:47:04 +04:00
pass - through usb devices to the guest . HOSTUSBDEVICE syntax is:
2011-09-12 14:26:00 +04:00
'bus-port(.port)*' ( decimal numbers ) or
2011-08-23 09:47:04 +04:00
'vendor_id:product_id' ( hexadeciaml numbers )
2011-09-12 14:26:00 +04:00
You can use the 'lsusb -t' command to list existing usb devices .
2011-08-23 09:47:04 +04:00
Note: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
EODESCR
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-usb" , $ usbdesc ) ;
2011-09-11 10:59:59 +04:00
my $ hostpcidesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-hostpci' ,
typetext = > "HOSTPCIDEVICE" ,
description = > << EODESCR ,
Map host pci devices . HOSTPCIDEVICE syntax is:
'bus:dev.func' ( hexadecimal numbers )
You can us the 'lspci' command to list existing pci devices .
Note: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
Experimental: user reported problems with this option .
EODESCR
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-hostpci" , $ hostpcidesc ) ;
2011-09-11 11:00:00 +04:00
my $ serialdesc = {
optional = > 1 ,
2011-09-12 09:44:02 +04:00
type = > 'string' ,
2011-09-12 09:08:01 +04:00
pattern = > '/dev/ttyS\d+' ,
2011-09-11 11:00:00 +04:00
description = > << EODESCR ,
2011-09-12 14:26:00 +04:00
Map host serial devices ( n is 0 to 3 ) .
2011-09-11 11:00:00 +04:00
Note: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
Experimental: user reported problems with this option .
EODESCR
} ;
2011-09-11 11:00:01 +04:00
my $ paralleldesc = {
optional = > 1 ,
2011-09-12 09:44:02 +04:00
type = > 'string' ,
2011-09-12 09:08:01 +04:00
pattern = > '/dev/parport\d+' ,
2011-09-11 11:00:01 +04:00
description = > << EODESCR ,
2011-09-12 14:26:00 +04:00
Map host parallel devices ( n is 0 to 2 ) .
2011-09-11 11:00:01 +04:00
Note: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
Experimental: user reported problems with this option .
EODESCR
} ;
for ( my $ i = 0 ; $ i < $ MAX_PARALLEL_PORTS ; $ i + + ) {
$ confdesc - > { "parallel$i" } = $ paralleldesc ;
}
2011-09-11 11:00:00 +04:00
for ( my $ i = 0 ; $ i < $ MAX_SERIAL_PORTS ; $ i + + ) {
$ confdesc - > { "serial$i" } = $ serialdesc ;
}
2011-09-11 10:59:59 +04:00
for ( my $ i = 0 ; $ i < $ MAX_HOSTPCI_DEVICES ; $ i + + ) {
$ confdesc - > { "hostpci$i" } = $ hostpcidesc ;
}
2011-08-23 09:47:04 +04:00
for ( my $ i = 0 ; $ i < $ MAX_IDE_DISKS ; $ i + + ) {
$ drivename_hash - > { "ide$i" } = 1 ;
$ confdesc - > { "ide$i" } = $ idedesc ;
}
2012-02-01 16:25:19 +04:00
for ( my $ i = 0 ; $ i < $ MAX_SATA_DISKS ; $ i + + ) {
$ drivename_hash - > { "sata$i" } = 1 ;
$ confdesc - > { "sata$i" } = $ satadesc ;
}
2011-08-23 09:47:04 +04:00
for ( my $ i = 0 ; $ i < $ MAX_SCSI_DISKS ; $ i + + ) {
$ drivename_hash - > { "scsi$i" } = 1 ;
$ confdesc - > { "scsi$i" } = $ scsidesc ;
}
for ( my $ i = 0 ; $ i < $ MAX_VIRTIO_DISKS ; $ i + + ) {
$ drivename_hash - > { "virtio$i" } = 1 ;
$ confdesc - > { "virtio$i" } = $ virtiodesc ;
}
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
$ confdesc - > { "usb$i" } = $ usbdesc ;
}
my $ unuseddesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-volume-id' ,
description = > "Reference to unused volumes." ,
} ;
for ( my $ i = 0 ; $ i < $ MAX_UNUSED_DISKS ; $ i + + ) {
$ confdesc - > { "unused$i" } = $ unuseddesc ;
}
my $ kvm_api_version = 0 ;
sub kvm_version {
return $ kvm_api_version if $ kvm_api_version ;
2011-09-15 11:11:27 +04:00
my $ fh = IO::File - > new ( "</dev/kvm" ) ||
2011-08-23 09:47:04 +04:00
return 0 ;
2011-09-15 11:11:27 +04:00
if ( my $ v = $ fh - > ioctl ( KVM_GET_API_VERSION ( ) , 0 ) ) {
2011-08-23 09:47:04 +04:00
$ kvm_api_version = $ v ;
}
$ fh - > close ( ) ;
return $ kvm_api_version ;
}
my $ kvm_user_version ;
sub kvm_user_version {
return $ kvm_user_version if $ kvm_user_version ;
$ kvm_user_version = 'unknown' ;
my $ tmp = `kvm -help 2>/dev/null` ;
2011-09-12 14:26:00 +04:00
2011-12-07 14:16:57 +04:00
if ( $ tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?) / ) {
2011-08-23 09:47:04 +04:00
$ kvm_user_version = $ 2 ;
}
return $ kvm_user_version ;
}
my $ kernel_has_vhost_net = - c '/dev/vhost-net' ;
sub disknames {
# order is important - used to autoselect boot disk
2011-09-12 14:26:00 +04:00
return ( ( map { "ide$_" } ( 0 .. ( $ MAX_IDE_DISKS - 1 ) ) ) ,
2011-08-23 09:47:04 +04:00
( map { "scsi$_" } ( 0 .. ( $ MAX_SCSI_DISKS - 1 ) ) ) ,
2012-02-01 16:25:19 +04:00
( map { "virtio$_" } ( 0 .. ( $ MAX_VIRTIO_DISKS - 1 ) ) ) ,
( map { "sata$_" } ( 0 .. ( $ MAX_SATA_DISKS - 1 ) ) ) ) ;
2011-08-23 09:47:04 +04:00
}
sub valid_drivename {
my $ dev = shift ;
2011-09-15 11:11:27 +04:00
return defined ( $ drivename_hash - > { $ dev } ) ;
2011-08-23 09:47:04 +04:00
}
sub option_exists {
my $ key = shift ;
return defined ( $ confdesc - > { $ key } ) ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
sub nic_models {
return $ nic_model_list ;
}
sub os_list_description {
return {
other = > 'Other' ,
wxp = > 'Windows XP' ,
w2k = > 'Windows 2000' ,
w2k3 = > , 'Windows 2003' ,
w2k8 = > 'Windows 2008' ,
wvista = > 'Windows Vista' ,
win7 = > 'Windows 7' ,
l24 = > 'Linux 2.4' ,
l26 = > 'Linux 2.6' ,
2011-09-12 14:26:00 +04:00
} ;
2011-08-23 09:47:04 +04:00
}
sub disk_devive_info {
my $ dev = shift ;
die "unknown disk device format '$dev'" if $ dev !~ m/^(ide|scsi|virtio)(\d+)$/ ;
my $ bus = $ 1 ;
my $ index = $ 2 ;
my $ maxdev = 1024 ;
if ( $ bus eq 'ide' ) {
$ maxdev = 2 ;
} elsif ( $ bus eq 'scsi' ) {
2011-09-07 17:34:36 +04:00
$ maxdev = 7 ;
2011-08-23 09:47:04 +04:00
}
2011-09-15 11:11:27 +04:00
my $ controller = int ( $ index / $ maxdev ) ;
2011-08-23 09:47:04 +04:00
my $ unit = $ index % $ maxdev ;
return { bus = > $ bus , desc = > uc ( $ bus ) . " $controller:$unit" ,
controller = > $ controller , unit = > $ unit , index = > $ index } ;
}
sub qemu_drive_name {
2011-09-12 14:26:00 +04:00
my ( $ dev , $ media ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ info = disk_devive_info ( $ dev ) ;
2011-08-23 09:47:04 +04:00
my $ mediastr = '' ;
if ( ( $ info - > { bus } eq 'ide' ) || ( $ info - > { bus } eq 'scsi' ) ) {
$ mediastr = ( $ media eq 'cdrom' ) ? "-cd" : "-hd" ;
2011-09-12 14:26:00 +04:00
return sprintf ( "%s%i%s%i" , $ info - > { bus } , $ info - > { controller } ,
2011-08-23 09:47:04 +04:00
$ mediastr , $ info - > { unit } ) ;
} else {
2011-09-12 14:26:00 +04:00
return sprintf ( "%s%i" , $ info - > { bus } , $ info - > { index } ) ;
2011-08-23 09:47:04 +04:00
}
}
my $ cdrom_path ;
sub get_cdrom_path {
return $ cdrom_path if $ cdrom_path ;
return $ cdrom_path = "/dev/cdrom" if - l "/dev/cdrom" ;
return $ cdrom_path = "/dev/cdrom1" if - l "/dev/cdrom1" ;
return $ cdrom_path = "/dev/cdrom2" if - l "/dev/cdrom2" ;
}
sub get_iso_path {
my ( $ storecfg , $ vmid , $ cdrom ) = @ _ ;
if ( $ cdrom eq 'cdrom' ) {
return get_cdrom_path ( ) ;
} elsif ( $ cdrom eq 'none' ) {
return '' ;
} elsif ( $ cdrom =~ m | ^ / | ) {
return $ cdrom ;
} else {
2011-09-15 11:11:27 +04:00
return PVE::Storage:: path ( $ storecfg , $ cdrom ) ;
2011-08-23 09:47:04 +04:00
}
}
# try to convert old style file names to volume IDs
sub filename_to_volume_id {
my ( $ vmid , $ file , $ media ) = @ _ ;
if ( ! ( $ file eq 'none' || $ file eq 'cdrom' ||
$ file =~ m | ^ /dev/ . + | || $ file =~ m/^([^:]+):(.+)$/ ) ) {
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
return undef if $ file =~ m | / | ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
if ( $ media && $ media eq 'cdrom' ) {
$ file = "local:iso/$file" ;
} else {
$ file = "local:$vmid/$file" ;
}
}
return $ file ;
}
sub verify_media_type {
my ( $ opt , $ vtype , $ media ) = @ _ ;
return if ! $ media ;
my $ etype ;
if ( $ media eq 'disk' ) {
$ etype = 'image' ;
} elsif ( $ media eq 'cdrom' ) {
$ etype = 'iso' ;
} else {
die "internal error" ;
}
return if ( $ vtype eq $ etype ) ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
raise_param_exc ( { $ opt = > "unexpected media type ($vtype != $etype)" } ) ;
}
sub cleanup_drive_path {
my ( $ opt , $ storecfg , $ drive ) = @ _ ;
# try to convert filesystem paths to volume IDs
if ( ( $ drive - > { file } !~ m/^(cdrom|none)$/ ) &&
( $ drive - > { file } !~ m | ^ /dev/ . + | ) &&
( $ drive - > { file } !~ m/^([^:]+):(.+)$/ ) &&
2011-09-12 14:26:00 +04:00
( $ drive - > { file } !~ m/^\d+$/ ) ) {
2011-08-23 09:47:04 +04:00
my ( $ vtype , $ volid ) = PVE::Storage:: path_to_volume_id ( $ storecfg , $ drive - > { file } ) ;
raise_param_exc ( { $ opt = > "unable to associate path '$drive->{file}' to any storage" } ) if ! $ vtype ;
$ drive - > { media } = 'cdrom' if ! $ drive - > { media } && $ vtype eq 'iso' ;
verify_media_type ( $ opt , $ vtype , $ drive - > { media } ) ;
$ drive - > { file } = $ volid ;
}
$ drive - > { media } = 'cdrom' if ! $ drive - > { media } && $ drive - > { file } =~ m/^(cdrom|none)$/ ;
}
sub create_conf_nolock {
my ( $ vmid , $ settings ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ filename = config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
die "configuration file '$filename' already exists\n" if - f $ filename ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
my $ defaults = load_defaults ( ) ;
$ settings - > { name } = "vm$vmid" if ! $ settings - > { name } ;
$ settings - > { memory } = $ defaults - > { memory } if ! $ settings - > { memory } ;
my $ data = '' ;
foreach my $ opt ( keys %$ settings ) {
next if ! $ confdesc - > { $ opt } ;
my $ value = $ settings - > { $ opt } ;
next if ! $ value ;
$ data . = "$opt: $value\n" ;
}
PVE::Tools:: file_set_contents ( $ filename , $ data ) ;
}
# ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]]
# [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no]
# [,aio=native|threads]
sub parse_drive {
my ( $ key , $ data ) = @ _ ;
my $ res = { } ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
# $key may be undefined - used to verify JSON parameters
if ( ! defined ( $ key ) ) {
$ res - > { interface } = 'unknown' ; # should not harm when used to verify parameters
$ res - > { index } = 0 ;
} elsif ( $ key =~ m/^([^\d]+)(\d+)$/ ) {
$ res - > { interface } = $ 1 ;
$ res - > { index } = $ 2 ;
} else {
return undef ;
}
foreach my $ p ( split ( /,/ , $ data ) ) {
next if $ p =~ m/^\s*$/ ;
if ( $ p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio)=(.+)$/ ) {
my ( $ k , $ v ) = ( $ 1 , $ 2 ) ;
$ k = 'file' if $ k eq 'volume' ;
return undef if defined $ res - > { $ k } ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
$ res - > { $ k } = $ v ;
} else {
if ( ! $ res - > { file } && $ p !~ m/=/ ) {
$ res - > { file } = $ p ;
} else {
return undef ;
}
}
}
return undef if ! $ res - > { file } ;
2011-09-12 14:26:00 +04:00
return undef if $ res - > { cache } &&
2011-11-18 08:48:52 +04:00
$ res - > { cache } !~ m/^(off|none|writethrough|writeback|unsafe)$/ ;
2011-08-23 09:47:04 +04:00
return undef if $ res - > { snapshot } && $ res - > { snapshot } !~ m/^(on|off)$/ ;
return undef if $ res - > { cyls } && $ res - > { cyls } !~ m/^\d+$/ ;
return undef if $ res - > { heads } && $ res - > { heads } !~ m/^\d+$/ ;
return undef if $ res - > { secs } && $ res - > { secs } !~ m/^\d+$/ ;
return undef if $ res - > { media } && $ res - > { media } !~ m/^(disk|cdrom)$/ ;
return undef if $ res - > { trans } && $ res - > { trans } !~ m/^(none|lba|auto)$/ ;
return undef if $ res - > { format } && $ res - > { format } !~ m/^(raw|cow|qcow|qcow2|vmdk|cloop)$/ ;
return undef if $ res - > { rerror } && $ res - > { rerror } !~ m/^(ignore|report|stop)$/ ;
return undef if $ res - > { werror } && $ res - > { werror } !~ m/^(enospc|ignore|report|stop)$/ ;
return undef if $ res - > { backup } && $ res - > { backup } !~ m/^(yes|no)$/ ;
return undef if $ res - > { aio } && $ res - > { aio } !~ m/^(native|threads)$/ ;
if ( $ res - > { media } && ( $ res - > { media } eq 'cdrom' ) ) {
return undef if $ res - > { snapshot } || $ res - > { trans } || $ res - > { format } ;
2011-09-12 14:26:00 +04:00
return undef if $ res - > { heads } || $ res - > { secs } || $ res - > { cyls } ;
2011-08-23 09:47:04 +04:00
return undef if $ res - > { interface } eq 'virtio' ;
}
# rerror does not work with scsi drives
if ( $ res - > { rerror } ) {
return undef if $ res - > { interface } eq 'scsi' ;
}
return $ res ;
}
my @ qemu_drive_options = qw( heads secs cyls trans media format cache snapshot rerror werror aio ) ;
sub print_drive {
my ( $ vmid , $ drive ) = @ _ ;
my $ opts = '' ;
foreach my $ o ( @ qemu_drive_options , 'backup' ) {
$ opts . = ",$o=$drive->{$o}" if $ drive - > { $ o } ;
}
return "$drive->{file}$opts" ;
}
2011-09-07 17:34:38 +04:00
sub print_drivedevice_full {
my ( $ storecfg , $ vmid , $ drive ) = @ _ ;
my $ device = '' ;
my $ maxdev = 0 ;
2011-09-12 14:26:00 +04:00
2011-09-07 17:34:38 +04:00
if ( $ drive - > { interface } eq 'virtio' ) {
2011-12-07 14:41:27 +04:00
my $ pciaddr = print_pci_addr ( "$drive->{interface}$drive->{index}" ) ;
$ device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr" ;
} elsif ( $ drive - > { interface } eq 'scsi' ) {
$ maxdev = 7 ;
my $ controller = int ( $ drive - > { index } / $ maxdev ) ;
my $ unit = $ drive - > { index } % $ maxdev ;
my $ devicetype = 'hd' ;
2011-12-13 12:59:28 +04:00
my $ path = '' ;
if ( drive_is_cdrom ( $ drive ) ) {
$ devicetype = 'cd' ;
} else {
if ( $ drive - > { file } =~ m | ^ / | ) {
$ path = $ drive - > { file } ;
} else {
$ path = PVE::Storage:: path ( $ storecfg , $ drive - > { file } ) ;
}
if ( $ path =~ m | ^ /dev/ | ) {
$ devicetype = 'block' ;
}
}
2011-09-07 17:34:38 +04:00
2012-01-20 14:42:04 +04:00
$ device = "scsi-$devicetype,bus=lsi$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}" ;
2011-12-07 14:41:27 +04:00
} elsif ( $ drive - > { interface } eq 'ide' ) {
$ maxdev = 2 ;
my $ controller = int ( $ drive - > { index } / $ maxdev ) ;
my $ unit = $ drive - > { index } % $ maxdev ;
my $ devicetype = ( $ drive - > { media } && $ drive - > { media } eq 'cdrom' ) ? "cd" : "hd" ;
2012-01-20 14:42:04 +04:00
$ device = "ide-$devicetype,bus=ide.$controller,unit=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}" ;
2012-02-01 16:25:19 +04:00
} elsif ( $ drive - > { interface } eq 'sata' ) {
my $ controller = int ( $ drive - > { index } / $ MAX_SATA_DISKS ) ;
my $ unit = $ drive - > { index } % $ MAX_SATA_DISKS ;
$ device = "ide-drive,bus=ahci$controller.$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}" ;
2011-12-07 14:41:27 +04:00
} elsif ( $ drive - > { interface } eq 'usb' ) {
die "implement me" ;
# -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
} else {
die "unsupported interface type" ;
2011-09-07 17:34:38 +04:00
}
2011-12-07 14:54:31 +04:00
$ device . = ",bootindex=$drive->{bootindex}" if $ drive - > { bootindex } ;
2011-09-07 17:34:38 +04:00
return $ device ;
}
2011-08-23 09:47:04 +04:00
sub print_drive_full {
my ( $ storecfg , $ vmid , $ drive ) = @ _ ;
my $ opts = '' ;
foreach my $ o ( @ qemu_drive_options ) {
2011-12-07 14:54:31 +04:00
next if $ o eq 'bootindex' ;
2011-08-23 09:47:04 +04:00
$ opts . = ",$o=$drive->{$o}" if $ drive - > { $ o } ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
# use linux-aio by default (qemu default is threads)
2011-09-12 14:26:00 +04:00
$ opts . = ",aio=native" if ! $ drive - > { aio } ;
2011-08-23 09:47:04 +04:00
my $ path ;
my $ volid = $ drive - > { file } ;
2011-09-15 11:11:27 +04:00
if ( drive_is_cdrom ( $ drive ) ) {
$ path = get_iso_path ( $ storecfg , $ vmid , $ volid ) ;
2011-08-23 09:47:04 +04:00
} else {
if ( $ volid =~ m | ^ / | ) {
$ path = $ volid ;
} else {
2011-09-15 11:11:27 +04:00
$ path = PVE::Storage:: path ( $ storecfg , $ volid ) ;
2011-08-23 09:47:04 +04:00
}
2011-12-07 15:02:34 +04:00
if ( ! $ drive - > { cache } && ( $ path =~ m | ^ /dev/ | || $ path =~ m | \ . raw $| ) ) {
$ opts . = ",cache=none" ;
}
2011-08-23 09:47:04 +04:00
}
my $ pathinfo = $ path ? "file=$path," : '' ;
2011-09-07 17:34:37 +04:00
return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts" ;
2011-08-23 09:47:04 +04:00
}
2012-01-28 14:02:28 +04:00
sub print_netdevice_full {
my ( $ vmid , $ conf , $ net , $ netid ) = @ _ ;
my $ bootorder = $ conf - > { boot } || $ confdesc - > { boot } - > { default } ;
my $ device = $ net - > { model } ;
if ( $ net - > { model } eq 'virtio' ) {
$ device = 'virtio-net-pci' ;
} ;
# qemu > 0.15 always try to boot from network - we disable that by
# not loading the pxe rom file
my $ extra = ( $ bootorder !~ m/n/ ) ? "romfile=," : '' ;
my $ pciaddr = print_pci_addr ( "$netid" ) ;
my $ tmpstr = "$device,${extra}mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid" ;
$ tmpstr . = ",bootindex=$net->{bootindex}" if $ net - > { bootindex } ;
return $ tmpstr ;
}
sub print_netdev_full {
my ( $ vmid , $ conf , $ net , $ netid ) = @ _ ;
my $ i = '' ;
if ( $ netid =~ m/^net(\d+)$/ ) {
$ i = int ( $ 1 ) ;
}
die "got strange net id '$i'\n" if $ i >= $ { MAX_NETS } ;
my $ ifname = "tap${vmid}i$i" ;
# kvm uses TUNSETIFF ioctl, and that limits ifname length
die "interface name '$ifname' is too long (max 15 character)\n"
if length ( $ ifname ) >= 16 ;
my $ vhostparam = '' ;
$ vhostparam = ',vhost=on' if $ kernel_has_vhost_net && $ net - > { model } eq 'virtio' ;
my $ vmname = $ conf - > { name } || "vm$vmid" ;
if ( $ net - > { bridge } ) {
return "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/pve-bridge$vhostparam" ;
} else {
return "type=user,id=$netid,hostname=$vmname" ;
}
}
2011-08-23 09:47:04 +04:00
sub drive_is_cdrom {
my ( $ drive ) = @ _ ;
return $ drive && $ drive - > { media } && ( $ drive - > { media } eq 'cdrom' ) ;
}
2011-09-11 10:59:59 +04:00
sub parse_hostpci {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
my $ res = { } ;
if ( $ value =~ m/^[a-f0-9]{2}:[a-f0-9]{2}\.[a-f0-9]$/ ) {
$ res - > { pciid } = $ value ;
} else {
return undef ;
}
return $ res ;
}
2011-08-23 09:47:04 +04:00
# netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
sub parse_net {
my ( $ data ) = @ _ ;
my $ res = { } ;
2011-09-15 11:11:27 +04:00
foreach my $ kvp ( split ( /,/ , $ data ) ) {
2011-08-23 09:47:04 +04:00
if ( $ kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i ) {
2011-09-15 11:11:27 +04:00
my $ model = lc ( $ 1 ) ;
2011-10-05 11:24:49 +04:00
my $ mac = uc ( $ 3 ) || PVE::Tools:: random_ether_addr ( ) ;
2011-08-23 09:47:04 +04:00
$ res - > { model } = $ model ;
$ res - > { macaddr } = $ mac ;
} elsif ( $ kvp =~ m/^bridge=(\S+)$/ ) {
$ res - > { bridge } = $ 1 ;
} elsif ( $ kvp =~ m/^rate=(\d+(\.\d+)?)$/ ) {
$ res - > { rate } = $ 1 ;
} else {
return undef ;
}
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
}
return undef if ! $ res - > { model } ;
return $ res ;
}
sub print_net {
my $ net = shift ;
my $ res = "$net->{model}" ;
$ res . = "=$net->{macaddr}" if $ net - > { macaddr } ;
$ res . = ",bridge=$net->{bridge}" if $ net - > { bridge } ;
$ res . = ",rate=$net->{rate}" if $ net - > { rate } ;
return $ res ;
}
sub add_random_macs {
my ( $ settings ) = @ _ ;
foreach my $ opt ( keys %$ settings ) {
next if $ opt !~ m/^net(\d+)$/ ;
my $ net = parse_net ( $ settings - > { $ opt } ) ;
next if ! $ net ;
$ settings - > { $ opt } = print_net ( $ net ) ;
}
}
sub add_unused_volume {
2012-02-02 17:01:08 +04:00
my ( $ config , $ volid ) = @ _ ;
2011-08-23 09:47:04 +04:00
my $ key ;
for ( my $ ind = $ MAX_UNUSED_DISKS - 1 ; $ ind >= 0 ; $ ind - - ) {
my $ test = "unused$ind" ;
if ( my $ vid = $ config - > { $ test } ) {
return if $ vid eq $ volid ; # do not add duplicates
} else {
$ key = $ test ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
}
die "To many unused volume - please delete them first.\n" if ! $ key ;
2012-02-02 17:01:08 +04:00
$ config - > { $ key } = $ volid ;
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
return $ key ;
2011-08-23 09:47:04 +04:00
}
# fixme: remove all thos $noerr parameters?
PVE::JSONSchema:: register_format ( 'pve-qm-bootdisk' , \ & verify_bootdisk ) ;
sub verify_bootdisk {
my ( $ value , $ noerr ) = @ _ ;
2011-09-12 14:26:00 +04:00
return $ value if valid_drivename ( $ value ) ;
2011-08-23 09:47:04 +04:00
return undef if $ noerr ;
die "invalid boot disk '$value'\n" ;
}
PVE::JSONSchema:: register_format ( 'pve-qm-net' , \ & verify_net ) ;
sub verify_net {
my ( $ value , $ noerr ) = @ _ ;
return $ value if parse_net ( $ value ) ;
return undef if $ noerr ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
die "unable to parse network options\n" ;
}
PVE::JSONSchema:: register_format ( 'pve-qm-drive' , \ & verify_drive ) ;
sub verify_drive {
my ( $ value , $ noerr ) = @ _ ;
2011-09-15 11:11:27 +04:00
return $ value if parse_drive ( undef , $ value ) ;
2011-08-23 09:47:04 +04:00
return undef if $ noerr ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
die "unable to parse drive options\n" ;
}
PVE::JSONSchema:: register_format ( 'pve-qm-hostpci' , \ & verify_hostpci ) ;
sub verify_hostpci {
my ( $ value , $ noerr ) = @ _ ;
2011-09-11 10:59:59 +04:00
return $ value if parse_hostpci ( $ value ) ;
return undef if $ noerr ;
die "unable to parse pci id\n" ;
2011-08-23 09:47:04 +04:00
}
2011-09-08 13:39:56 +04:00
PVE::JSONSchema:: register_format ( 'pve-qm-watchdog' , \ & verify_watchdog ) ;
sub verify_watchdog {
my ( $ value , $ noerr ) = @ _ ;
return $ value if parse_watchdog ( $ value ) ;
return undef if $ noerr ;
2011-09-12 14:26:00 +04:00
2011-09-08 13:39:56 +04:00
die "unable to parse watchdog options\n" ;
}
sub parse_watchdog {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
my $ res = { } ;
2011-09-15 11:11:27 +04:00
foreach my $ p ( split ( /,/ , $ value ) ) {
2011-09-08 13:39:56 +04:00
next if $ p =~ m/^\s*$/ ;
if ( $ p =~ m/^(model=)?(i6300esb|ib700)$/ ) {
$ res - > { model } = $ 2 ;
} elsif ( $ p =~ m/^(action=)?(reset|shutdown|poweroff|pause|debug|none)$/ ) {
$ res - > { action } = $ 2 ;
} else {
return undef ;
}
}
return $ res ;
}
2011-08-23 09:47:04 +04:00
sub parse_usb_device {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
2011-09-15 11:11:27 +04:00
my @ dl = split ( /,/ , $ value ) ;
2011-08-23 09:47:04 +04:00
my $ found ;
my $ res = { } ;
foreach my $ v ( @ dl ) {
if ( $ v =~ m/^host=([0-9A-Fa-f]{4}):([0-9A-Fa-f]{4})$/ ) {
$ found = 1 ;
$ res - > { vendorid } = $ 1 ;
$ res - > { productid } = $ 2 ;
} elsif ( $ v =~ m/^host=(\d+)\-(\d+(\.\d+)*)$/ ) {
$ found = 1 ;
$ res - > { hostbus } = $ 1 ;
$ res - > { hostport } = $ 2 ;
} else {
return undef ;
}
}
return undef if ! $ found ;
return $ res ;
}
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
PVE::JSONSchema:: register_format ( 'pve-qm-usb-device' , \ & verify_usb_device ) ;
sub verify_usb_device {
my ( $ value , $ noerr ) = @ _ ;
return $ value if parse_usb_device ( $ value ) ;
return undef if $ noerr ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
die "unable to parse usb device\n" ;
}
# add JSON properties for create and set function
sub json_config_properties {
my $ prop = shift ;
foreach my $ opt ( keys %$ confdesc ) {
$ prop - > { $ opt } = $ confdesc - > { $ opt } ;
}
return $ prop ;
}
sub check_type {
my ( $ key , $ value ) = @ _ ;
die "unknown setting '$key'\n" if ! $ confdesc - > { $ key } ;
my $ type = $ confdesc - > { $ key } - > { type } ;
2011-09-15 11:11:27 +04:00
if ( ! defined ( $ value ) ) {
2011-08-23 09:47:04 +04:00
die "got undefined value\n" ;
}
if ( $ value =~ m/[\n\r]/ ) {
die "property contains a line feed\n" ;
}
if ( $ type eq 'boolean' ) {
2011-09-12 14:26:00 +04:00
return 1 if ( $ value eq '1' ) || ( $ value =~ m/^(on|yes|true)$/i ) ;
return 0 if ( $ value eq '0' ) || ( $ value =~ m/^(off|no|false)$/i ) ;
die "type check ('boolean') failed - got '$value'\n" ;
2011-08-23 09:47:04 +04:00
} elsif ( $ type eq 'integer' ) {
return int ( $ 1 ) if $ value =~ m/^(\d+)$/ ;
die "type check ('integer') failed - got '$value'\n" ;
} elsif ( $ type eq 'string' ) {
if ( my $ fmt = $ confdesc - > { $ key } - > { format } ) {
if ( $ fmt eq 'pve-qm-drive' ) {
# special case - we need to pass $key to parse_drive()
2011-09-15 11:11:27 +04:00
my $ drive = parse_drive ( $ key , $ value ) ;
2011-08-23 09:47:04 +04:00
return $ value if $ drive ;
die "unable to parse drive options\n" ;
}
PVE::JSONSchema:: check_format ( $ fmt , $ value ) ;
2011-09-12 14:26:00 +04:00
return $ value ;
}
2011-08-23 09:47:04 +04:00
$ value =~ s/^\"(.*)\"$/$1/ ;
2011-09-12 14:26:00 +04:00
return $ value ;
2011-08-23 09:47:04 +04:00
} else {
die "internal error"
}
}
sub lock_config {
my ( $ vmid , $ code , @ param ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ filename = config_file_lock ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-10-10 15:17:40 +04:00
my $ res = lock_file ( $ filename , 10 , $ code , @ param ) ;
2011-08-23 09:47:04 +04:00
die $@ if $@ ;
2011-10-10 15:17:40 +04:00
return $ res ;
2011-08-23 09:47:04 +04:00
}
sub cfs_config_path {
2011-09-09 11:47:54 +04:00
my ( $ vmid , $ node ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-09 11:47:54 +04:00
$ node = $ nodename if ! $ node ;
return "nodes/$node/qemu-server/$vmid.conf" ;
2011-08-23 09:47:04 +04:00
}
2011-09-11 10:59:59 +04:00
sub check_iommu_support {
#fixme : need to check IOMMU support
#http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
my $ iommu = 1 ;
return $ iommu ;
}
2011-08-23 09:47:04 +04:00
sub config_file {
2011-09-09 11:47:54 +04:00
my ( $ vmid , $ node ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-09 11:47:54 +04:00
my $ cfspath = cfs_config_path ( $ vmid , $ node ) ;
2011-08-23 09:47:04 +04:00
return "/etc/pve/$cfspath" ;
}
sub config_file_lock {
my ( $ vmid ) = @ _ ;
return "$lock_dir/lock-$vmid.conf" ;
}
sub touch_config {
my ( $ vmid ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ conf = config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
utime undef , undef , $ conf ;
}
sub destroy_vm {
2011-10-18 09:42:29 +04:00
my ( $ storecfg , $ vmid , $ keep_empty_config ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conffile = config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) ;
2011-08-23 09:47:04 +04:00
2011-09-12 14:26:00 +04:00
# only remove disks owned by this VM
2011-08-23 09:47:04 +04:00
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
2011-09-15 11:11:27 +04:00
return if drive_is_cdrom ( $ drive ) ;
2011-08-23 09:47:04 +04:00
my $ volid = $ drive - > { file } ;
2011-11-25 11:05:36 +04:00
return if ! $ volid || $ volid =~ m | ^ / | ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my ( $ path , $ owner ) = PVE::Storage:: path ( $ storecfg , $ volid ) ;
2011-11-25 11:05:36 +04:00
return if ! $ path || ! $ owner || ( $ owner != $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
PVE::Storage:: vdisk_free ( $ storecfg , $ volid ) ;
2011-08-23 09:47:04 +04:00
} ) ;
2011-09-12 14:26:00 +04:00
2011-10-18 09:42:29 +04:00
if ( $ keep_empty_config ) {
2011-10-19 13:27:42 +04:00
PVE::Tools:: file_set_contents ( $ conffile , "memory: 128\n" ) ;
2011-10-18 09:42:29 +04:00
} else {
unlink $ conffile ;
}
2011-08-23 09:47:04 +04:00
# also remove unused disk
eval {
2011-09-15 11:11:27 +04:00
my $ dl = PVE::Storage:: vdisk_list ( $ storecfg , undef , $ vmid ) ;
2011-08-23 09:47:04 +04:00
eval {
2011-09-15 11:11:27 +04:00
PVE::Storage:: foreach_volid ( $ dl , sub {
2011-08-23 09:47:04 +04:00
my ( $ volid , $ sid , $ volname , $ d ) = @ _ ;
2011-09-15 11:11:27 +04:00
PVE::Storage:: vdisk_free ( $ storecfg , $ volid ) ;
2011-08-23 09:47:04 +04:00
} ) ;
} ;
warn $@ if $@ ;
} ;
warn $@ if $@ ;
}
# fixme: remove?
sub load_diskinfo_old {
my ( $ storecfg , $ vmid , $ conf ) = @ _ ;
my $ info = { } ;
my $ res = { } ;
my $ vollist ;
foreach_drive ( $ conf , sub {
my ( $ ds , $ di ) = @ _ ;
$ res - > { $ ds } = $ di ;
2011-09-15 11:11:27 +04:00
return if drive_is_cdrom ( $ di ) ;
2011-08-23 09:47:04 +04:00
if ( $ di - > { file } =~ m | ^ /dev/ . + | ) {
2011-09-15 11:11:27 +04:00
$ info - > { $ di - > { file } } - > { size } = PVE::Storage:: file_size_info ( $ di - > { file } ) ;
2011-08-23 09:47:04 +04:00
} else {
push @$ vollist , $ di - > { file } ;
}
} ) ;
eval {
2011-09-15 11:11:27 +04:00
my $ dl = PVE::Storage:: vdisk_list ( $ storecfg , undef , $ vmid , $ vollist ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
PVE::Storage:: foreach_volid ( $ dl , sub {
2011-08-23 09:47:04 +04:00
my ( $ volid , $ sid , $ volname , $ d ) = @ _ ;
$ info - > { $ volid } = $ d ;
} ) ;
} ;
warn $@ if $@ ;
foreach my $ ds ( keys %$ res ) {
my $ di = $ res - > { $ ds } ;
2011-09-12 14:26:00 +04:00
$ res - > { $ ds } - > { disksize } = $ info - > { $ di - > { file } } ?
2011-08-23 09:47:04 +04:00
$ info - > { $ di - > { file } } - > { size } / ( 1024 * 1024 ) : 0 ;
}
return $ res ;
}
sub load_config {
my ( $ vmid ) = @ _ ;
my $ cfspath = cfs_config_path ( $ vmid ) ;
my $ conf = PVE::Cluster:: cfs_read_file ( $ cfspath ) ;
die "no such VM ('$vmid')\n" if ! defined ( $ conf ) ;
return $ conf ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
sub parse_vm_config {
my ( $ filename , $ raw ) = @ _ ;
return undef if ! defined ( $ raw ) ;
2011-09-07 13:41:34 +04:00
my $ res = {
digest = > Digest::SHA1:: sha1_hex ( $ raw ) ,
} ;
2011-08-23 09:47:04 +04:00
2011-09-12 14:26:00 +04:00
$ filename =~ m | /qemu-server/ ( \ d + ) \ . conf $|
2011-08-23 09:47:04 +04:00
|| die "got strange filename '$filename'" ;
my $ vmid = $ 1 ;
while ( $ raw && $ raw =~ s/^(.*?)(\n|$)// ) {
my $ line = $ 1 ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
next if $ line =~ m/^\#/ ;
next if $ line =~ m/^\s*$/ ;
if ( $ line =~ m/^(description):\s*(.*\S)\s*$/ ) {
my $ key = $ 1 ;
my $ value = PVE::Tools:: decode_text ( $ 2 ) ;
$ res - > { $ key } = $ value ;
} elsif ( $ line =~ m/^(args):\s*(.*\S)\s*$/ ) {
my $ key = $ 1 ;
my $ value = $ 2 ;
$ res - > { $ key } = $ value ;
} elsif ( $ line =~ m/^([a-z][a-z_]*\d*):\s*(\S+)\s*$/ ) {
my $ key = $ 1 ;
my $ value = $ 2 ;
eval { $ value = check_type ( $ key , $ value ) ; } ;
if ( $@ ) {
warn "vm $vmid - unable to parse value of '$key' - $@" ;
} else {
my $ fmt = $ confdesc - > { $ key } - > { format } ;
if ( $ fmt && $ fmt eq 'pve-qm-drive' ) {
my $ v = parse_drive ( $ key , $ value ) ;
if ( my $ volid = filename_to_volume_id ( $ vmid , $ v - > { file } , $ v - > { media } ) ) {
$ v - > { file } = $ volid ;
2011-09-15 11:11:27 +04:00
$ value = print_drive ( $ vmid , $ v ) ;
2011-08-23 09:47:04 +04:00
} else {
warn "vm $vmid - unable to parse value of '$key'\n" ;
next ;
}
}
if ( $ key eq 'cdrom' ) {
$ res - > { ide2 } = $ value ;
} else {
$ res - > { $ key } = $ value ;
}
}
}
}
# convert old smp to sockets
if ( $ res - > { smp } && ! $ res - > { sockets } ) {
$ res - > { sockets } = $ res - > { smp } ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
delete $ res - > { smp } ;
return $ res ;
}
2012-02-02 17:01:08 +04:00
sub write_vm_config {
my ( $ filename , $ conf ) = @ _ ;
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
if ( $ conf - > { cdrom } ) {
die "option ide2 conflicts with cdrom\n" if $ conf - > { ide2 } ;
$ conf - > { ide2 } = $ conf - > { cdrom } ;
delete $ conf - > { cdrom } ;
}
2011-08-23 09:47:04 +04:00
# we do not use 'smp' any longer
2012-02-02 17:01:08 +04:00
if ( $ conf - > { sockets } ) {
delete $ conf - > { smp } ;
} elsif ( $ conf - > { smp } ) {
$ conf - > { sockets } = $ conf - > { smp } ;
delete $ conf - > { cores } ;
delete $ conf - > { smp } ;
2011-08-23 09:47:04 +04:00
}
my $ new_volids = { } ;
2012-02-02 17:01:08 +04:00
foreach my $ key ( keys %$ conf ) {
2011-09-07 13:41:34 +04:00
next if $ key eq 'digest' ;
2012-02-02 17:01:08 +04:00
my $ value = $ conf - > { $ key } ;
2011-08-23 09:47:04 +04:00
if ( $ key eq 'description' ) {
$ value = PVE::Tools:: encode_text ( $ value ) ;
}
eval { $ value = check_type ( $ key , $ value ) ; } ;
die "unable to parse value of '$key' - $@" if $@ ;
2012-02-02 17:01:08 +04:00
$ conf - > { $ key } = $ value ;
2011-08-23 09:47:04 +04:00
if ( valid_drivename ( $ key ) ) {
my $ drive = PVE::QemuServer:: parse_drive ( $ key , $ value ) ;
$ new_volids - > { $ drive - > { file } } = 1 if $ drive && $ drive - > { file } ;
}
}
2012-02-02 17:01:08 +04:00
# remove 'unusedX' settings if we re-add a volume
foreach my $ key ( keys %$ conf ) {
my $ value = $ conf - > { $ key } ;
if ( $ key =~ m/^unused/ && $ new_volids - > { $ value } ) {
delete $ conf - > { $ key } ;
2011-08-23 09:47:04 +04:00
}
2012-02-02 17:01:08 +04:00
}
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
# gererate RAW data
my $ raw = '' ;
foreach my $ key ( sort keys %$ conf ) {
next if $ key eq 'digest' ;
$ raw . = "$key: $conf->{$key}\n" ;
}
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
return $ raw ;
}
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
sub update_config_nolock {
my ( $ vmid , $ conf , $ skiplock ) = @ _ ;
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
my $ cfspath = cfs_config_path ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
PVE::Cluster:: cfs_write_file ( $ cfspath , $ conf ) ;
}
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
sub update_config {
my ( $ vmid , $ conf , $ skiplock ) = @ _ ;
2011-08-23 09:47:04 +04:00
2012-02-02 17:01:08 +04:00
lock_config ( $ vmid , & update_config_nolock , $ conf , $ skiplock ) ;
2011-08-23 09:47:04 +04:00
}
2011-09-12 14:26:00 +04:00
sub load_defaults {
2011-08-23 09:47:04 +04:00
my $ res = { } ;
# we use static defaults from our JSON schema configuration
foreach my $ key ( keys %$ confdesc ) {
if ( defined ( my $ default = $ confdesc - > { $ key } - > { default } ) ) {
$ res - > { $ key } = $ default ;
}
}
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
my $ conf = PVE::Cluster:: cfs_read_file ( 'datacenter.cfg' ) ;
$ res - > { keyboard } = $ conf - > { keyboard } if $ conf - > { keyboard } ;
return $ res ;
}
sub config_list {
my $ vmlist = PVE::Cluster:: get_vmlist ( ) ;
my $ res = { } ;
return $ res if ! $ vmlist || ! $ vmlist - > { ids } ;
my $ ids = $ vmlist - > { ids } ;
foreach my $ vmid ( keys %$ ids ) {
my $ d = $ ids - > { $ vmid } ;
next if ! $ d - > { node } || $ d - > { node } ne $ nodename ;
2011-09-26 14:20:05 +04:00
next if ! $ d - > { type } || $ d - > { type } ne 'qemu' ;
2011-08-23 09:47:04 +04:00
$ res - > { $ vmid } - > { exists } = 1 ;
}
return $ res ;
}
2011-09-09 14:13:21 +04:00
# test if VM uses local resources (to prevent migration)
sub check_local_resources {
my ( $ conf , $ noerr ) = @ _ ;
my $ loc_res = 0 ;
2011-09-12 14:26:00 +04:00
2011-09-12 10:41:28 +04:00
$ loc_res = 1 if $ conf - > { hostusb } ; # old syntax
$ loc_res = 1 if $ conf - > { hostpci } ; # old syntax
2011-09-09 14:13:21 +04:00
2011-09-09 16:18:11 +04:00
foreach my $ k ( keys %$ conf ) {
2011-09-12 09:08:01 +04:00
$ loc_res = 1 if $ k =~ m/^(usb|hostpci|serial|parallel)\d+$/ ;
2011-09-09 14:13:21 +04:00
}
die "VM uses local resources\n" if $ loc_res && ! $ noerr ;
return $ loc_res ;
}
2011-08-23 09:47:04 +04:00
sub check_lock {
my ( $ conf ) = @ _ ;
die "VM is locked ($conf->{lock})\n" if $ conf - > { lock } ;
}
sub check_cmdline {
my ( $ pidfile , $ pid ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ fh = IO::File - > new ( "/proc/$pid/cmdline" , "r" ) ;
if ( defined ( $ fh ) ) {
2011-08-23 09:47:04 +04:00
my $ line = <$fh> ;
$ fh - > close ;
return undef if ! $ line ;
2011-09-15 11:11:27 +04:00
my @ param = split ( /\0/ , $ line ) ;
2011-08-23 09:47:04 +04:00
my $ cmd = $ param [ 0 ] ;
return if ! $ cmd || ( $ cmd !~ m | kvm $| ) ;
for ( my $ i = 0 ; $ i < scalar ( @ param ) ; $ i + + ) {
my $ p = $ param [ $ i ] ;
next if ! $ p ;
if ( ( $ p eq '-pidfile' ) || ( $ p eq '--pidfile' ) ) {
my $ p = $ param [ $ i + 1 ] ;
return 1 if $ p && ( $ p eq $ pidfile ) ;
return undef ;
}
}
}
return undef ;
}
sub check_running {
2011-09-15 10:21:32 +04:00
my ( $ vmid , $ nocheck ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
my $ filename = config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
die "unable to find configuration file for VM $vmid - no such machine\n"
2011-09-15 10:21:32 +04:00
if ! $ nocheck && ! - f $ filename ;
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
my $ pidfile = pidfile_name ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
if ( my $ fd = IO::File - > new ( "<$pidfile" ) ) {
my $ st = stat ( $ fd ) ;
2011-08-23 09:47:04 +04:00
my $ line = <$fd> ;
2011-09-15 11:11:27 +04:00
close ( $ fd ) ;
2011-08-23 09:47:04 +04:00
my $ mtime = $ st - > mtime ;
if ( $ mtime > time ( ) ) {
warn "file '$filename' modified in future\n" ;
}
if ( $ line =~ m/^(\d+)$/ ) {
my $ pid = $ 1 ;
2011-09-15 10:21:32 +04:00
if ( check_cmdline ( $ pidfile , $ pid ) ) {
if ( my $ pinfo = PVE::ProcFSTools:: check_process_running ( $ pid ) ) {
return $ pid ;
}
}
2011-08-23 09:47:04 +04:00
}
}
return undef ;
}
sub vzlist {
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
my $ vzlist = config_list ( ) ;
2011-09-15 11:11:27 +04:00
my $ fd = IO::Dir - > new ( $ var_run_tmpdir ) || return $ vzlist ;
2011-08-23 09:47:04 +04:00
2011-09-12 14:26:00 +04:00
while ( defined ( my $ de = $ fd - > read ) ) {
2011-08-23 09:47:04 +04:00
next if $ de !~ m/^(\d+)\.pid$/ ;
my $ vmid = $ 1 ;
2011-09-15 11:11:27 +04:00
next if ! defined ( $ vzlist - > { $ vmid } ) ;
if ( my $ pid = check_running ( $ vmid ) ) {
2011-08-23 09:47:04 +04:00
$ vzlist - > { $ vmid } - > { pid } = $ pid ;
}
}
return $ vzlist ;
}
my $ storage_timeout_hash = { } ;
sub disksize {
my ( $ storecfg , $ conf ) = @ _ ;
my $ bootdisk = $ conf - > { bootdisk } ;
return undef if ! $ bootdisk ;
return undef if ! valid_drivename ( $ bootdisk ) ;
return undef if ! $ conf - > { $ bootdisk } ;
my $ drive = parse_drive ( $ bootdisk , $ conf - > { $ bootdisk } ) ;
return undef if ! defined ( $ drive ) ;
return undef if drive_is_cdrom ( $ drive ) ;
my $ volid = $ drive - > { file } ;
return undef if ! $ volid ;
my $ path ;
my $ storeid ;
my $ timeoutid ;
if ( $ volid =~ m | ^ / | ) {
$ path = $ timeoutid = $ volid ;
} else {
2012-01-17 16:12:40 +04:00
eval {
$ storeid = $ timeoutid = PVE::Storage:: parse_volume_id ( $ volid ) ;
$ path = PVE::Storage:: path ( $ storecfg , $ volid ) ;
} ;
if ( my $ err = $@ ) {
warn $ err ;
return undef ;
}
2011-08-23 09:47:04 +04:00
}
my $ last_timeout = $ storage_timeout_hash - > { $ timeoutid } ;
if ( $ last_timeout ) {
if ( ( time ( ) - $ last_timeout ) < 30 ) {
# skip storage with errors
return undef ;
}
delete $ storage_timeout_hash - > { $ timeoutid } ;
}
my ( $ size , $ format , $ used ) ;
( $ size , $ format , $ used ) = PVE::Storage:: file_size_info ( $ path , 1 ) ;
if ( ! defined ( $ format ) ) {
# got timeout
$ storage_timeout_hash - > { $ timeoutid } = time ( ) ;
return undef ;
}
return wantarray ? ( $ size , $ used ) : $ size ;
}
my $ last_proc_pid_stat ;
sub vmstatus {
my ( $ opt_vmid ) = @ _ ;
my $ res = { } ;
2011-09-12 14:26:00 +04:00
my $ storecfg = PVE::Storage:: config ( ) ;
2011-08-23 09:47:04 +04:00
my $ list = vzlist ( ) ;
2011-09-14 09:55:34 +04:00
my ( $ uptime ) = PVE::ProcFSTools:: read_proc_uptime ( 1 ) ;
2011-08-23 09:47:04 +04:00
2011-11-18 12:35:32 +04:00
my $ cpucount = $ cpuinfo - > { cpus } || 1 ;
2011-08-23 09:47:04 +04:00
foreach my $ vmid ( keys %$ list ) {
next if $ opt_vmid && ( $ vmid ne $ opt_vmid ) ;
my $ cfspath = cfs_config_path ( $ vmid ) ;
my $ conf = PVE::Cluster:: cfs_read_file ( $ cfspath ) || { } ;
my $ d = { } ;
$ d - > { pid } = $ list - > { $ vmid } - > { pid } ;
# fixme: better status?
$ d - > { status } = $ list - > { $ vmid } - > { pid } ? 'running' : 'stopped' ;
my ( $ size , $ used ) = disksize ( $ storecfg , $ conf ) ;
if ( defined ( $ size ) && defined ( $ used ) ) {
$ d - > { disk } = $ used ;
$ d - > { maxdisk } = $ size ;
} else {
$ d - > { disk } = 0 ;
$ d - > { maxdisk } = 0 ;
}
$ d - > { cpus } = ( $ conf - > { sockets } || 1 ) * ( $ conf - > { cores } || 1 ) ;
2011-11-18 12:35:32 +04:00
$ d - > { cpus } = $ cpucount if $ d - > { cpus } > $ cpucount ;
2011-08-23 09:47:04 +04:00
$ d - > { name } = $ conf - > { name } || "VM $vmid" ;
2011-09-12 14:26:00 +04:00
$ d - > { maxmem } = $ conf - > { memory } ? $ conf - > { memory } * ( 1024 * 1024 ) : 0 ;
2011-08-23 09:47:04 +04:00
$ d - > { uptime } = 0 ;
$ d - > { cpu } = 0 ;
$ d - > { mem } = 0 ;
$ d - > { netout } = 0 ;
$ d - > { netin } = 0 ;
$ d - > { diskread } = 0 ;
$ d - > { diskwrite } = 0 ;
$ res - > { $ vmid } = $ d ;
}
my $ netdev = PVE::ProcFSTools:: read_proc_net_dev ( ) ;
foreach my $ dev ( keys %$ netdev ) {
next if $ dev !~ m/^tap([1-9]\d*)i/ ;
my $ vmid = $ 1 ;
my $ d = $ res - > { $ vmid } ;
next if ! $ d ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
$ d - > { netout } += $ netdev - > { $ dev } - > { receive } ;
$ d - > { netin } += $ netdev - > { $ dev } - > { transmit } ;
}
my $ ctime = gettimeofday ;
foreach my $ vmid ( keys %$ list ) {
my $ d = $ res - > { $ vmid } ;
my $ pid = $ d - > { pid } ;
next if ! $ pid ;
if ( my $ fh = IO::File - > new ( "/proc/$pid/io" , "r" ) ) {
my $ data = { } ;
2011-09-15 11:11:27 +04:00
while ( defined ( my $ line = <$fh> ) ) {
2011-08-23 09:47:04 +04:00
if ( $ line =~ m/^([rw]char):\s+(\d+)$/ ) {
$ data - > { $ 1 } = $ 2 ;
}
}
close ( $ fh ) ;
$ d - > { diskread } = $ data - > { rchar } || 0 ;
$ d - > { diskwrite } = $ data - > { wchar } || 0 ;
}
2011-09-14 09:55:34 +04:00
my $ pstat = PVE::ProcFSTools:: read_proc_pid_stat ( $ pid ) ;
next if ! $ pstat ; # not running
2011-09-12 14:26:00 +04:00
2011-09-14 09:55:34 +04:00
my $ used = $ pstat - > { utime } + $ pstat - > { stime } ;
2011-08-23 09:47:04 +04:00
2011-09-14 09:55:34 +04:00
$ d - > { uptime } = int ( ( $ uptime - $ pstat - > { starttime } ) / $ cpuinfo - > { user_hz } ) ;
2011-08-23 09:47:04 +04:00
2011-09-14 09:55:34 +04:00
if ( $ pstat - > { vsize } ) {
2011-09-15 11:11:27 +04:00
$ d - > { mem } = int ( ( $ pstat - > { rss } / $ pstat - > { vsize } ) * $ d - > { maxmem } ) ;
2011-08-23 09:47:04 +04:00
}
my $ old = $ last_proc_pid_stat - > { $ pid } ;
if ( ! $ old ) {
2011-09-12 14:26:00 +04:00
$ last_proc_pid_stat - > { $ pid } = {
time = > $ ctime ,
2011-08-23 09:47:04 +04:00
used = > $ used ,
cpu = > 0 ,
} ;
next ;
}
2011-09-12 13:03:14 +04:00
my $ dtime = ( $ ctime - $ old - > { time } ) * $ cpucount * $ cpuinfo - > { user_hz } ;
2011-08-23 09:47:04 +04:00
if ( $ dtime > 1000 ) {
my $ dutime = $ used - $ old - > { used } ;
2011-11-18 12:35:32 +04:00
$ d - > { cpu } = ( ( $ dutime /$dtime)* $cpucount) / $ d - > { cpus } ;
2011-08-23 09:47:04 +04:00
$ last_proc_pid_stat - > { $ pid } = {
2011-09-12 14:26:00 +04:00
time = > $ ctime ,
2011-08-23 09:47:04 +04:00
used = > $ used ,
cpu = > $ d - > { cpu } ,
} ;
} else {
$ d - > { cpu } = $ old - > { cpu } ;
}
}
return $ res ;
}
sub foreach_drive {
my ( $ conf , $ func ) = @ _ ;
foreach my $ ds ( keys %$ conf ) {
next if ! valid_drivename ( $ ds ) ;
2011-09-15 11:11:27 +04:00
my $ drive = parse_drive ( $ ds , $ conf - > { $ ds } ) ;
2011-08-23 09:47:04 +04:00
next if ! $ drive ;
& $ func ( $ ds , $ drive ) ;
}
}
sub config_to_command {
my ( $ storecfg , $ vmid , $ conf , $ defaults , $ migrate_uri ) = @ _ ;
my $ cmd = [] ;
2011-10-03 16:53:09 +04:00
my $ pciaddr = '' ;
2011-08-23 09:47:04 +04:00
my $ kvmver = kvm_user_version ( ) ;
my $ vernum = 0 ; # unknown
2011-12-07 14:16:57 +04:00
if ( $ kvmver =~ m/^(\d+)\.(\d+)$/ ) {
$ vernum = $ 1 * 1000000 + $ 2 * 1000 ;
} elsif ( $ kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/ ) {
2011-08-23 09:47:04 +04:00
$ vernum = $ 1 * 1000000 + $ 2 * 1000 + $ 3 ;
}
2011-12-07 14:16:57 +04:00
die "detected old qemu-kvm binary ($kvmver)\n" if $ vernum < 15000 ;
2011-08-23 09:47:04 +04:00
my $ have_ovz = - f '/proc/vz/vestat' ;
push @$ cmd , '/usr/bin/kvm' ;
push @$ cmd , '-id' , $ vmid ;
my $ use_virtio = 0 ;
2011-09-15 11:11:27 +04:00
my $ socket = monitor_socket ( $ vmid ) ;
2011-09-07 17:34:41 +04:00
push @$ cmd , '-chardev' , "socket,id=monitor,path=$socket,server,nowait" ;
push @$ cmd , '-mon' , "chardev=monitor,mode=readline" ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
$ socket = vnc_socket ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
push @$ cmd , '-vnc' , "unix:$socket,x509,password" ;
2011-09-15 11:11:27 +04:00
push @$ cmd , '-pidfile' , pidfile_name ( $ vmid ) ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
push @$ cmd , '-daemonize' ;
push @$ cmd , '-incoming' , $ migrate_uri if $ migrate_uri ;
2011-12-08 13:24:56 +04:00
my $ use_usb2 = 0 ;
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
next if ! $ conf - > { "usb$i" } ;
$ use_usb2 = 1 ;
}
2011-08-23 09:47:04 +04:00
# include usb device config
2011-12-08 13:24:56 +04:00
push @$ cmd , '-readconfig' , '/usr/share/qemu-server/pve-usb.cfg' if $ use_usb2 ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
# enable absolute mouse coordinates (needed by vnc)
2011-09-15 11:11:27 +04:00
my $ tablet = defined ( $ conf - > { tablet } ) ? $ conf - > { tablet } : $ defaults - > { tablet } ;
2011-12-08 13:24:56 +04:00
if ( $ tablet ) {
if ( $ use_usb2 ) {
push @$ cmd , '-device' , 'usb-tablet,bus=ehci.0,port=6' ;
} else {
push @$ cmd , '-usbdevice' , 'tablet' ;
}
}
2011-08-23 09:47:04 +04:00
# host pci devices
2011-09-11 10:59:59 +04:00
for ( my $ i = 0 ; $ i < $ MAX_HOSTPCI_DEVICES ; $ i + + ) {
my $ d = parse_hostpci ( $ conf - > { "hostpci$i" } ) ;
next if ! $ d ;
2011-10-03 16:53:09 +04:00
$ pciaddr = print_pci_addr ( "hostpci$i" ) ;
push @$ cmd , '-device' , "pci-assign,host=$d->{pciid},id=hostpci$i$pciaddr" ;
2011-08-23 09:47:04 +04:00
}
# usb devices
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
my $ d = parse_usb_device ( $ conf - > { "usb$i" } ) ;
next if ! $ d ;
if ( $ d - > { vendorid } && $ d - > { productid } ) {
push @$ cmd , '-device' , "usb-host,vendorid=$d->{vendorid},productid=$d->{productid}" ;
} elsif ( defined ( $ d - > { hostbus } ) && defined ( $ d - > { hostport } ) ) {
push @$ cmd , '-device' , "usb-host,hostbus=$d->{hostbus},hostport=$d->{hostport}" ;
}
}
# serial devices
2011-09-11 11:00:00 +04:00
for ( my $ i = 0 ; $ i < $ MAX_SERIAL_PORTS ; $ i + + ) {
2011-09-12 10:59:05 +04:00
if ( my $ path = $ conf - > { "serial$i" } ) {
2011-09-12 14:26:00 +04:00
die "no such serial device\n" if ! - c $ path ;
2011-09-12 10:59:05 +04:00
push @$ cmd , '-chardev' , "tty,id=serial$i,path=$path" ;
push @$ cmd , '-device' , "isa-serial,chardev=serial$i" ;
}
2011-08-23 09:47:04 +04:00
}
# parallel devices
2011-09-11 11:00:01 +04:00
for ( my $ i = 0 ; $ i < $ MAX_PARALLEL_PORTS ; $ i + + ) {
2011-09-12 10:59:05 +04:00
if ( my $ path = $ conf - > { "parallel$i" } ) {
2011-09-12 14:26:00 +04:00
die "no such parallel device\n" if ! - c $ path ;
2011-09-12 10:59:05 +04:00
push @$ cmd , '-chardev' , "parport,id=parallel$i,path=$path" ;
push @$ cmd , '-device' , "isa-parallel,chardev=parallel$i" ;
}
2011-08-23 09:47:04 +04:00
}
my $ vmname = $ conf - > { name } || "vm$vmid" ;
push @$ cmd , '-name' , $ vmname ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
my $ sockets = 1 ;
$ sockets = $ conf - > { smp } if $ conf - > { smp } ; # old style - no longer iused
$ sockets = $ conf - > { sockets } if $ conf - > { sockets } ;
my $ cores = $ conf - > { cores } || 1 ;
push @$ cmd , '-smp' , "sockets=$sockets,cores=$cores" ;
push @$ cmd , '-cpu' , $ conf - > { cpu } if $ conf - > { cpu } ;
push @$ cmd , '-nodefaults' ;
2011-09-29 09:43:05 +04:00
my $ bootorder = $ conf - > { boot } || $ confdesc - > { boot } - > { default } ;
2011-12-07 14:54:31 +04:00
2011-12-08 14:41:58 +04:00
my $ bootindex_hash = { } ;
my $ i = 1 ;
foreach my $ o ( split ( // , $ bootorder ) ) {
$ bootindex_hash - > { $ o } = $ i * 100 ;
$ i + + ;
2012-01-27 12:35:26 +04:00
}
2011-12-07 14:54:31 +04:00
push @$ cmd , '-boot' , "menu=on" ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
push @$ cmd , '-no-acpi' if defined ( $ conf - > { acpi } ) && $ conf - > { acpi } == 0 ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
push @$ cmd , '-no-reboot' if defined ( $ conf - > { reboot } ) && $ conf - > { reboot } == 0 ;
2011-08-23 09:47:04 +04:00
my $ vga = $ conf - > { vga } ;
if ( ! $ vga ) {
if ( $ conf - > { ostype } && ( $ conf - > { ostype } eq 'win7' || $ conf - > { ostype } eq 'w2k8' ) ) {
$ vga = 'std' ;
} else {
$ vga = 'cirrus' ;
}
}
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
push @$ cmd , '-vga' , $ vga if $ vga ; # for kvm 77 and later
# time drift fix
2011-09-15 11:11:27 +04:00
my $ tdf = defined ( $ conf - > { tdf } ) ? $ conf - > { tdf } : $ defaults - > { tdf } ;
2011-08-23 09:47:04 +04:00
push @$ cmd , '-tdf' if $ tdf ;
2011-09-15 11:11:27 +04:00
my $ nokvm = defined ( $ conf - > { kvm } ) && $ conf - > { kvm } == 0 ? 1 : 0 ;
2011-08-23 09:47:04 +04:00
if ( my $ ost = $ conf - > { ostype } ) {
# other, wxp, w2k, w2k3, w2k8, wvista, win7, l24, l26
if ( $ ost =~ m/^w/ ) { # windows
2011-09-15 11:11:27 +04:00
push @$ cmd , '-localtime' if ! defined ( $ conf - > { localtime } ) ;
2011-08-23 09:47:04 +04:00
# use rtc-td-hack when acpi is enabled
2011-09-15 11:11:27 +04:00
if ( ! ( defined ( $ conf - > { acpi } ) && $ conf - > { acpi } == 0 ) ) {
2011-08-23 09:47:04 +04:00
push @$ cmd , '-rtc-td-hack' ;
}
}
# -tdf ?
2011-09-12 14:26:00 +04:00
# -no-acpi
# -no-kvm
2011-08-23 09:47:04 +04:00
# -win2k-hack ?
}
2011-09-12 13:03:14 +04:00
if ( $ nokvm ) {
push @$ cmd , '-no-kvm' ;
} else {
die "No accelerator found!\n" if ! $ cpuinfo - > { hvm } ;
}
2011-08-23 09:47:04 +04:00
push @$ cmd , '-localtime' if $ conf - > { localtime } ;
push @$ cmd , '-startdate' , $ conf - > { startdate } if $ conf - > { startdate } ;
push @$ cmd , '-S' if $ conf - > { freeze } ;
# set keyboard layout
my $ kb = $ conf - > { keyboard } || $ defaults - > { keyboard } ;
push @$ cmd , '-k' , $ kb if $ kb ;
# enable sound
#my $soundhw = $conf->{soundhw} || $defaults->{soundhw};
#push @$cmd, '-soundhw', 'es1370';
#push @$cmd, '-soundhw', $soundhw if $soundhw;
2011-10-03 16:53:09 +04:00
$ pciaddr = print_pci_addr ( "balloon0" ) ;
2011-09-30 15:10:27 +04:00
push @$ cmd , '-device' , "virtio-balloon-pci,id=balloon0$pciaddr" if $ conf - > { balloon } ;
2011-08-23 09:47:04 +04:00
2011-09-08 13:39:56 +04:00
if ( $ conf - > { watchdog } ) {
my $ wdopts = parse_watchdog ( $ conf - > { watchdog } ) ;
2011-10-03 16:53:10 +04:00
$ pciaddr = print_pci_addr ( "watchdog" ) ;
my $ watchdog = $ wdopts - > { model } || 'i6300esb' ;
push @$ cmd , '-device' , "$watchdog$pciaddr" ;
2011-09-08 13:39:56 +04:00
push @$ cmd , '-watchdog-action' , $ wdopts - > { action } if $ wdopts - > { action } ;
}
2011-08-23 09:47:04 +04:00
my $ vollist = [] ;
2011-09-09 12:27:21 +04:00
my $ scsicontroller = { } ;
2012-02-01 16:25:20 +04:00
my $ ahcicontroller = { } ;
2011-08-23 09:47:04 +04:00
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
2011-11-25 11:05:36 +04:00
if ( PVE::Storage:: parse_volume_id ( $ drive - > { file } , 1 ) ) {
2011-08-23 09:47:04 +04:00
push @$ vollist , $ drive - > { file } ;
2011-11-25 11:05:36 +04:00
}
2012-01-27 12:35:26 +04:00
2011-08-23 09:47:04 +04:00
$ use_virtio = 1 if $ ds =~ m/^virtio/ ;
2011-12-07 14:54:31 +04:00
if ( drive_is_cdrom ( $ drive ) ) {
if ( $ bootindex_hash - > { d } ) {
$ drive - > { bootindex } = $ bootindex_hash - > { d } ;
$ bootindex_hash - > { d } += 1 ;
}
} else {
if ( $ bootindex_hash - > { c } ) {
$ drive - > { bootindex } = $ bootindex_hash - > { c } if $ conf - > { bootdisk } && ( $ conf - > { bootdisk } eq $ ds ) ;
$ bootindex_hash - > { c } += 1 ;
}
}
2011-09-09 12:27:21 +04:00
if ( $ drive - > { interface } eq 'scsi' ) {
my $ maxdev = 7 ;
2011-09-15 11:11:27 +04:00
my $ controller = int ( $ drive - > { index } / $ maxdev ) ;
2012-01-20 14:42:02 +04:00
$ pciaddr = print_pci_addr ( "lsi$controller" ) ;
push @$ cmd , '-device' , "lsi,id=lsi$controller$pciaddr" if ! $ scsicontroller - > { $ controller } ;
2011-10-19 07:54:37 +04:00
$ scsicontroller - > { $ controller } = 1 ;
2011-09-09 12:27:21 +04:00
}
2011-12-07 14:54:31 +04:00
2012-02-01 16:25:20 +04:00
if ( $ drive - > { interface } eq 'sata' ) {
my $ controller = int ( $ drive - > { index } / $ MAX_SATA_DISKS ) ;
$ pciaddr = print_pci_addr ( "ahci$controller" ) ;
push @$ cmd , '-device' , "ahci,id=ahci$controller,multifunction=on$pciaddr" if ! $ ahcicontroller - > { $ controller } ;
$ ahcicontroller - > { $ controller } = 1 ;
}
push @$ cmd , '-drive' , print_drive_full ( $ storecfg , $ vmid , $ drive ) ;
2011-09-15 11:11:27 +04:00
push @$ cmd , '-device' , print_drivedevice_full ( $ storecfg , $ vmid , $ drive ) ;
2011-08-23 09:47:04 +04:00
} ) ;
push @$ cmd , '-m' , $ conf - > { memory } || $ defaults - > { memory } ;
2011-09-12 14:26:00 +04:00
2012-01-28 14:02:28 +04:00
for ( my $ i = 0 ; $ i < $ MAX_NETS ; $ i + + ) {
2012-01-28 20:07:33 +04:00
next if ! $ conf - > { "net$i" } ;
2012-01-28 14:02:28 +04:00
my $ d = parse_net ( $ conf - > { "net$i" } ) ;
next if ! $ d ;
2011-08-23 09:47:04 +04:00
2012-01-28 14:02:28 +04:00
$ use_virtio = 1 if $ d - > { model } eq 'virtio' ;
2011-08-23 09:47:04 +04:00
2012-01-28 14:02:28 +04:00
if ( $ bootindex_hash - > { n } ) {
$ d - > { bootindex } = $ bootindex_hash - > { n } ;
$ bootindex_hash - > { n } += 1 ;
}
2011-08-23 09:47:04 +04:00
2012-01-28 14:02:28 +04:00
my $ netdevfull = print_netdev_full ( $ vmid , $ conf , $ d , "net$i" ) ;
push @$ cmd , '-netdev' , $ netdevfull ;
2011-08-23 09:47:04 +04:00
2012-01-28 14:02:28 +04:00
my $ netdevicefull = print_netdevice_full ( $ vmid , $ conf , $ d , "net$i" ) ;
push @$ cmd , '-device' , $ netdevicefull ;
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
# hack: virtio with fairsched is unreliable, so we do not use fairsched
# when the VM uses virtio devices.
2011-09-12 14:26:00 +04:00
if ( ! $ use_virtio && $ have_ovz ) {
2011-09-15 11:11:27 +04:00
my $ cpuunits = defined ( $ conf - > { cpuunits } ) ?
2011-08-23 09:47:04 +04:00
$ conf - > { cpuunits } : $ defaults - > { cpuunits } ;
push @$ cmd , '-cpuunits' , $ cpuunits if $ cpuunits ;
# fixme: cpulimit is currently ignored
#push @$cmd, '-cpulimit', $conf->{cpulimit} if $conf->{cpulimit};
}
# add custom args
if ( $ conf - > { args } ) {
2011-10-25 14:19:36 +04:00
my $ aa = PVE::Tools:: split_args ( $ conf - > { args } ) ;
2011-08-23 09:47:04 +04:00
push @$ cmd , @$ aa ;
}
return wantarray ? ( $ cmd , $ vollist ) : $ cmd ;
}
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
sub vnc_socket {
my ( $ vmid ) = @ _ ;
return "${var_run_tmpdir}/$vmid.vnc" ;
}
sub monitor_socket {
my ( $ vmid ) = @ _ ;
return "${var_run_tmpdir}/$vmid.mon" ;
}
sub pidfile_name {
my ( $ vmid ) = @ _ ;
return "${var_run_tmpdir}/$vmid.pid" ;
}
sub next_migrate_port {
for ( my $ p = 60000 ; $ p < 60010 ; $ p + + ) {
2011-09-15 11:11:27 +04:00
my $ sock = IO::Socket::INET - > new ( Listen = > 5 ,
LocalAddr = > 'localhost' ,
LocalPort = > $ p ,
ReuseAddr = > 1 ,
Proto = > 0 ) ;
2011-08-23 09:47:04 +04:00
if ( $ sock ) {
2011-09-15 11:11:27 +04:00
close ( $ sock ) ;
2011-08-23 09:47:04 +04:00
return $ p ;
}
}
die "unable to find free migration port" ;
}
2011-10-10 18:46:54 +04:00
sub vm_devices_list {
my ( $ vmid ) = @ _ ;
2011-11-25 11:05:36 +04:00
my $ res = vm_monitor_command ( $ vmid , "info pci" ) ;
2011-10-11 15:28:50 +04:00
my @ lines = split ( "\n" , $ res ) ;
my $ devices ;
my $ bus ;
my $ addr ;
my $ id ;
2012-01-27 12:35:26 +04:00
2011-10-11 15:28:50 +04:00
foreach my $ line ( @ lines ) {
$ line =~ s/^\s+// ;
if ( $ line =~ m/^Bus (\d+), device (\d+), function (\d+):$/ ) {
$ bus = $ 1 ;
$ addr = $ 2 ;
}
if ( $ line =~ m/^id "([a-z][a-z_\-]*\d*)"$/ ) {
2011-10-10 18:46:54 +04:00
$ id = $ 1 ;
$ devices - > { $ id } - > { bus } = $ bus ;
$ devices - > { $ id } - > { addr } = $ addr ;
2011-10-11 15:28:50 +04:00
}
}
2011-10-10 18:46:54 +04:00
2011-10-11 15:28:50 +04:00
return $ devices ;
2011-10-10 18:46:54 +04:00
}
2012-01-20 14:42:03 +04:00
sub vm_deviceplug {
2011-10-14 12:14:11 +04:00
my ( $ storecfg , $ conf , $ vmid , $ deviceid , $ device ) = @ _ ;
2012-02-03 13:23:50 +04:00
2012-01-28 14:02:29 +04:00
return 1 if ! check_running ( $ vmid ) || ! $ conf - > { hotplug } ;
2012-01-27 12:35:26 +04:00
2012-02-05 17:19:06 +04:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if defined ( $ devices_list - > { $ deviceid } ) ;
2012-01-20 14:42:05 +04:00
if ( $ deviceid =~ m/^(virtio)(\d+)$/ ) {
return undef if ! qemu_driveadd ( $ storecfg , $ vmid , $ device ) ;
2011-10-11 15:28:50 +04:00
my $ devicefull = print_drivedevice_full ( $ storecfg , $ vmid , $ device ) ;
2012-01-20 14:42:05 +04:00
qemu_deviceadd ( $ vmid , $ devicefull ) ;
if ( ! qemu_deviceaddverify ( $ vmid , $ deviceid ) ) {
qemu_drivedel ( $ vmid , $ deviceid ) ;
return undef ;
}
2011-10-14 12:14:11 +04:00
}
2012-01-20 14:42:06 +04:00
if ( $ deviceid =~ m/^(lsi)(\d+)$/ ) {
my $ pciaddr = print_pci_addr ( $ deviceid ) ;
my $ devicefull = "lsi,id=$deviceid$pciaddr" ;
qemu_deviceadd ( $ vmid , $ devicefull ) ;
return undef if ( ! qemu_deviceaddverify ( $ vmid , $ deviceid ) ) ;
}
2012-01-20 14:42:07 +04:00
if ( $ deviceid =~ m/^(scsi)(\d+)$/ ) {
return undef if ! qemu_findorcreatelsi ( $ storecfg , $ conf , $ vmid , $ device ) ;
return undef if ! qemu_driveadd ( $ storecfg , $ vmid , $ device ) ;
my $ devicefull = print_drivedevice_full ( $ storecfg , $ vmid , $ device ) ;
if ( ! qemu_deviceadd ( $ vmid , $ devicefull ) ) {
qemu_drivedel ( $ vmid , $ deviceid ) ;
return undef ;
}
}
2012-01-28 14:02:29 +04:00
if ( $ deviceid =~ m/^(net)(\d+)$/ ) {
return undef if ! qemu_netdevadd ( $ vmid , $ conf , $ device , $ deviceid ) ;
my $ netdevicefull = print_netdevice_full ( $ vmid , $ conf , $ device , $ deviceid ) ;
qemu_deviceadd ( $ vmid , $ netdevicefull ) ;
if ( ! qemu_deviceaddverify ( $ vmid , $ deviceid ) ) {
qemu_netdevdel ( $ vmid , $ deviceid ) ;
return undef ;
}
}
2012-01-20 14:42:05 +04:00
return 1 ;
2011-10-10 18:46:55 +04:00
}
2012-01-20 14:42:03 +04:00
sub vm_deviceunplug {
2011-10-14 12:14:11 +04:00
my ( $ vmid , $ conf , $ deviceid ) = @ _ ;
2011-10-10 18:46:56 +04:00
2012-01-20 14:42:05 +04:00
return 1 if ! check_running ( $ vmid ) || ! $ conf - > { hotplug } ;
2011-10-10 18:46:56 +04:00
2012-02-05 17:19:06 +04:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if ! defined ( $ devices_list - > { $ deviceid } ) ;
2012-02-03 13:23:50 +04:00
die "can't unplug bootdisk" if $ conf - > { bootdisk } && $ conf - > { bootdisk } eq $ deviceid ;
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
if ( $ deviceid =~ m/^(virtio)(\d+)$/ ) {
return undef if ! qemu_drivedel ( $ vmid , $ deviceid ) ;
qemu_devicedel ( $ vmid , $ deviceid ) ;
return undef if ! qemu_devicedelverify ( $ vmid , $ deviceid ) ;
}
2012-01-20 14:42:06 +04:00
if ( $ deviceid =~ m/^(lsi)(\d+)$/ ) {
return undef if ! qemu_devicedel ( $ vmid , $ deviceid ) ;
}
2012-01-20 14:42:07 +04:00
if ( $ deviceid =~ m/^(scsi)(\d+)$/ ) {
return undef if ! qemu_devicedel ( $ vmid , $ deviceid ) ;
return undef if ! qemu_drivedel ( $ vmid , $ deviceid ) ;
}
2012-01-28 14:02:29 +04:00
if ( $ deviceid =~ m/^(net)(\d+)$/ ) {
return undef if ! qemu_netdevdel ( $ vmid , $ deviceid ) ;
qemu_devicedel ( $ vmid , $ deviceid ) ;
return undef if ! qemu_devicedelverify ( $ vmid , $ deviceid ) ;
}
2012-01-20 14:42:05 +04:00
return 1 ;
}
sub qemu_deviceadd {
my ( $ vmid , $ devicefull ) = @ _ ;
2011-10-10 18:46:56 +04:00
2012-01-20 14:42:05 +04:00
my $ ret = vm_monitor_command ( $ vmid , "device_add $devicefull" ) ;
$ ret =~ s/^\s+// ;
2012-01-27 12:35:26 +04:00
# Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
2012-01-20 14:42:05 +04:00
return 1 if $ ret eq "" ;
syslog ( "err" , "error on hotplug device : $ret" ) ;
return undef ;
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
}
2012-01-27 12:35:26 +04:00
2012-01-20 14:42:05 +04:00
sub qemu_devicedel {
my ( $ vmid , $ deviceid ) = @ _ ;
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
my $ ret = vm_monitor_command ( $ vmid , "device_del $deviceid" ) ;
$ ret =~ s/^\s+// ;
return 1 if $ ret eq "" ;
syslog ( "err" , "detaching device $deviceid failed : $ret" ) ;
return undef ;
}
sub qemu_driveadd {
my ( $ storecfg , $ vmid , $ device ) = @ _ ;
my $ drive = print_drive_full ( $ storecfg , $ vmid , $ device ) ;
my $ ret = vm_monitor_command ( $ vmid , "drive_add auto $drive" ) ;
# If the command succeeds qemu prints: "OK"
if ( $ ret !~ m/OK/s ) {
syslog ( "err" , "adding drive failed: $ret" ) ;
return undef ;
2011-10-14 12:14:11 +04:00
}
2012-01-20 14:42:05 +04:00
return 1 ;
}
2012-01-27 12:35:26 +04:00
2012-01-20 14:42:05 +04:00
sub qemu_drivedel {
my ( $ vmid , $ deviceid ) = @ _ ;
2011-10-10 18:46:56 +04:00
2012-01-20 14:42:05 +04:00
my $ ret = vm_monitor_command ( $ vmid , "drive_del drive-$deviceid" ) ;
$ ret =~ s/^\s+// ;
if ( $ ret =~ m/Device \'.*?\' not found/s ) {
2012-01-27 12:35:26 +04:00
# NB: device not found errors mean the drive was auto-deleted and we ignore the error
2012-01-20 14:42:05 +04:00
}
elsif ( $ ret ne "" ) {
syslog ( "err" , "deleting drive $deviceid failed : $ret" ) ;
return undef ;
2011-10-10 18:46:56 +04:00
}
2012-01-20 14:42:05 +04:00
return 1 ;
}
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
sub qemu_deviceaddverify {
my ( $ vmid , $ deviceid ) = @ _ ;
2011-10-10 18:46:56 +04:00
2012-01-20 14:42:05 +04:00
for ( my $ i = 0 ; $ i <= 5 ; $ i + + ) {
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if defined ( $ devices_list - > { $ deviceid } ) ;
sleep 1 ;
2012-01-27 12:35:26 +04:00
}
2012-01-20 14:42:05 +04:00
syslog ( "err" , "error on hotplug device $deviceid" ) ;
return undef ;
}
2012-01-27 12:35:26 +04:00
2012-01-20 14:42:05 +04:00
sub qemu_devicedelverify {
my ( $ vmid , $ deviceid ) = @ _ ;
#need to verify the device is correctly remove as device_del is async and empty return is not reliable
for ( my $ i = 0 ; $ i <= 5 ; $ i + + ) {
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if ! defined ( $ devices_list - > { $ deviceid } ) ;
sleep 1 ;
2012-01-27 12:35:26 +04:00
}
2012-01-20 14:42:05 +04:00
syslog ( "err" , "error on hot-unplugging device $deviceid" ) ;
return undef ;
2011-10-10 18:46:56 +04:00
}
2012-01-20 14:42:06 +04:00
sub qemu_findorcreatelsi {
my ( $ storecfg , $ conf , $ vmid , $ device ) = @ _ ;
my $ maxdev = 7 ;
my $ controller = int ( $ device - > { index } / $ maxdev ) ;
my $ lsiid = "lsi$controller" ;
my $ devices_list = vm_devices_list ( $ vmid ) ;
if ( ! defined ( $ devices_list - > { $ lsiid } ) ) {
return undef if ! vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ lsiid ) ;
}
return 1 ;
}
2012-01-28 14:02:29 +04:00
sub qemu_netdevadd {
my ( $ vmid , $ conf , $ device , $ deviceid ) = @ _ ;
my $ netdev = print_netdev_full ( $ vmid , $ conf , $ device , $ deviceid ) ;
my $ ret = vm_monitor_command ( $ vmid , "netdev_add $netdev" ) ;
$ ret =~ s/^\s+// ;
#if the command succeeds, no output is sent. So any non-empty string shows an error
return 1 if $ ret eq "" ;
syslog ( "err" , "adding netdev failed: $ret" ) ;
return undef ;
}
sub qemu_netdevdel {
my ( $ vmid , $ deviceid ) = @ _ ;
my $ ret = vm_monitor_command ( $ vmid , "netdev_del $deviceid" ) ;
$ ret =~ s/^\s+// ;
#if the command succeeds, no output is sent. So any non-empty string shows an error
return 1 if $ ret eq "" ;
syslog ( "err" , "deleting netdev failed: $ret" ) ;
return undef ;
}
2011-08-23 09:47:04 +04:00
sub vm_start {
my ( $ storecfg , $ vmid , $ statefile , $ skiplock ) = @ _ ;
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
die "VM $vmid already running\n" if check_running ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
my $ migrate_uri ;
my $ migrate_port = 0 ;
if ( $ statefile ) {
if ( $ statefile eq 'tcp' ) {
$ migrate_port = next_migrate_port ( ) ;
$ migrate_uri = "tcp:localhost:${migrate_port}" ;
} else {
if ( - f $ statefile ) {
$ migrate_uri = "exec:cat $statefile" ;
} else {
warn "state file '$statefile' does not exist - doing normal startup\n" ;
}
}
}
my $ defaults = load_defaults ( ) ;
2011-09-15 10:21:32 +04:00
my ( $ cmd , $ vollist ) = config_to_command ( $ storecfg , $ vmid , $ conf , $ defaults , $ migrate_uri ) ;
2011-08-23 09:47:04 +04:00
# host pci devices
2011-09-11 10:59:59 +04:00
for ( my $ i = 0 ; $ i < $ MAX_HOSTPCI_DEVICES ; $ i + + ) {
my $ d = parse_hostpci ( $ conf - > { "hostpci$i" } ) ;
next if ! $ d ;
my $ info = pci_device_info ( "0000:$d->{pciid}" ) ;
die "IOMMU not present\n" if ! check_iommu_support ( ) ;
die "no pci device info for device '$d->{pciid}'\n" if ! $ info ;
die "can't unbind pci device '$d->{pciid}'\n" if ! pci_dev_bind_to_stub ( $ info ) ;
die "can't reset pci device '$d->{pciid}'\n" if ! pci_dev_reset ( $ info ) ;
}
2011-08-23 09:47:04 +04:00
PVE::Storage:: activate_volumes ( $ storecfg , $ vollist ) ;
2011-09-15 11:11:27 +04:00
eval { run_command ( $ cmd , timeout = > $ migrate_uri ? undef : 30 ) ; } ;
2011-08-23 09:47:04 +04:00
my $ err = $@ ;
2011-11-25 11:05:36 +04:00
die "start failed: $err" if $ err ;
2011-08-23 09:47:04 +04:00
if ( $ statefile ) {
if ( $ statefile eq 'tcp' ) {
print "migration listens on port $migrate_port\n" ;
} else {
unlink $ statefile ;
# fixme: send resume - is that necessary ?
2011-11-25 11:05:36 +04:00
eval { vm_monitor_command ( $ vmid , "cont" ) ; } ;
2011-08-23 09:47:04 +04:00
}
}
2012-01-27 12:35:26 +04:00
2011-11-23 12:11:07 +04:00
# always set migrate speed (overwrite kvm default of 32m)
# we set a very hight default of 8192m which is basically unlimited
my $ migrate_speed = $ defaults - > { migrate_speed } || 8192 ;
$ migrate_speed = $ conf - > { migrate_speed } || $ migrate_speed ;
2012-01-27 12:35:26 +04:00
eval {
2011-08-23 09:47:04 +04:00
my $ cmd = "migrate_set_speed ${migrate_speed}m" ;
2012-01-27 12:35:26 +04:00
vm_monitor_command ( $ vmid , $ cmd ) ;
2011-11-23 12:11:07 +04:00
} ;
2011-08-23 09:47:04 +04:00
2011-09-12 14:26:00 +04:00
if ( my $ migrate_downtime =
2011-08-23 09:47:04 +04:00
$ conf - > { migrate_downtime } || $ defaults - > { migrate_downtime } ) {
my $ cmd = "migrate_set_downtime ${migrate_downtime}" ;
2011-11-25 11:05:36 +04:00
eval { vm_monitor_command ( $ vmid , $ cmd ) ; } ;
2011-08-23 09:47:04 +04:00
}
2011-09-15 10:21:32 +04:00
2011-09-16 08:57:54 +04:00
vm_balloonset ( $ vmid , $ conf - > { balloon } ) if $ conf - > { balloon } ;
2011-08-23 09:47:04 +04:00
} ) ;
}
sub __read_avail {
my ( $ fh , $ timeout ) = @ _ ;
my $ sel = new IO:: Select ;
2011-09-15 11:11:27 +04:00
$ sel - > add ( $ fh ) ;
2011-08-23 09:47:04 +04:00
my $ res = '' ;
my $ buf ;
my @ ready ;
2011-09-15 11:11:27 +04:00
while ( scalar ( @ ready = $ sel - > can_read ( $ timeout ) ) ) {
2011-08-23 09:47:04 +04:00
my $ count ;
2011-09-15 11:11:27 +04:00
if ( $ count = $ fh - > sysread ( $ buf , 8192 ) ) {
2011-08-23 09:47:04 +04:00
if ( $ buf =~ /^(.*)\(qemu\) $/s ) {
$ res . = $ 1 ;
last ;
} else {
$ res . = $ buf ;
}
} else {
2011-09-15 11:11:27 +04:00
if ( ! defined ( $ count ) ) {
2011-08-23 09:47:04 +04:00
die "$!\n" ;
}
last ;
}
}
2011-09-15 11:11:27 +04:00
die "monitor read timeout\n" if ! scalar ( @ ready ) ;
2011-08-23 09:47:04 +04:00
return $ res ;
}
sub vm_monitor_command {
2011-11-25 11:05:36 +04:00
my ( $ vmid , $ cmdstr , $ nocheck ) = @ _ ;
2011-08-23 09:47:04 +04:00
my $ res ;
eval {
2011-11-25 11:05:36 +04:00
die "VM $vmid not running\n" if ! check_running ( $ vmid , $ nocheck ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
my $ sname = monitor_socket ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ sock = IO::Socket::UNIX - > new ( Peer = > $ sname ) ||
2011-08-23 09:47:04 +04:00
die "unable to connect to VM $vmid socket - $!\n" ;
my $ timeout = 3 ;
2011-09-12 14:26:00 +04:00
# hack: migrate sometime blocks the monitor (when migrate_downtime
2011-08-23 09:47:04 +04:00
# is set)
if ( $ cmdstr =~ m/^(info\s+migrate|migrate\s)/ ) {
$ timeout = 60 * 60 ; # 1 hour
}
# read banner;
2011-09-15 11:11:27 +04:00
my $ data = __read_avail ( $ sock , $ timeout ) ;
2011-09-12 14:26:00 +04:00
2011-08-23 09:47:04 +04:00
if ( $ data !~ m/^QEMU\s+(\S+)\s+monitor\s/ ) {
die "got unexpected qemu monitor banner\n" ;
}
my $ sel = new IO:: Select ;
2011-09-15 11:11:27 +04:00
$ sel - > add ( $ sock ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
if ( ! scalar ( my @ ready = $ sel - > can_write ( $ timeout ) ) ) {
2011-08-23 09:47:04 +04:00
die "monitor write error - timeout" ;
}
my $ fullcmd = "$cmdstr\r" ;
2012-02-03 13:23:50 +04:00
# syslog('info', "VM $vmid monitor command: $cmdstr");
2011-08-23 09:47:04 +04:00
my $ b ;
2011-09-15 11:11:27 +04:00
if ( ! ( $ b = $ sock - > syswrite ( $ fullcmd ) ) || ( $ b != length ( $ fullcmd ) ) ) {
2011-08-23 09:47:04 +04:00
die "monitor write error - $!" ;
}
return if ( $ cmdstr eq 'q' ) || ( $ cmdstr eq 'quit' ) ;
2011-09-12 14:26:00 +04:00
$ timeout = 20 ;
2011-08-23 09:47:04 +04:00
if ( $ cmdstr =~ m/^(info\s+migrate|migrate\s)/ ) {
$ timeout = 60 * 60 ; # 1 hour
} elsif ( $ cmdstr =~ m/^(eject|change)/ ) {
$ timeout = 60 ; # note: cdrom mount command is slow
}
2011-09-15 11:11:27 +04:00
if ( $ res = __read_avail ( $ sock , $ timeout ) ) {
2011-09-12 14:26:00 +04:00
2011-09-15 11:11:27 +04:00
my @ lines = split ( "\r?\n" , $ res ) ;
2011-08-23 09:47:04 +04:00
shift @ lines if $ lines [ 0 ] !~ m/^unknown command/ ; # skip echo
2011-09-12 14:26:00 +04:00
2011-09-15 11:11:27 +04:00
$ res = join ( "\n" , @ lines ) ;
2011-08-23 09:47:04 +04:00
$ res . = "\n" ;
}
} ;
my $ err = $@ ;
if ( $ err ) {
2011-09-15 11:11:27 +04:00
syslog ( "err" , "VM $vmid monitor command failed - $err" ) ;
2011-08-23 09:47:04 +04:00
die $ err ;
}
return $ res ;
}
sub vm_commandline {
my ( $ storecfg , $ vmid ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
my $ defaults = load_defaults ( ) ;
2011-09-15 11:11:27 +04:00
my $ cmd = config_to_command ( $ storecfg , $ vmid , $ conf , $ defaults ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
return join ( ' ' , @$ cmd ) ;
2011-08-23 09:47:04 +04:00
}
sub vm_reset {
my ( $ vmid , $ skiplock ) = @ _ ;
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
vm_monitor_command ( $ vmid , "system_reset" ) ;
} ) ;
}
sub get_vm_volumes {
my ( $ conf ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
my $ vollist = [] ;
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
my ( $ sid , $ volname ) = PVE::Storage:: parse_volume_id ( $ drive - > { file } , 1 ) ;
return if ! $ sid ;
my $ volid = $ drive - > { file } ;
return if ! $ volid || $ volid =~ m | ^ / | ;
push @$ vollist , $ volid ;
2011-08-23 09:47:04 +04:00
} ) ;
2011-11-25 11:05:36 +04:00
return $ vollist ;
}
sub vm_stop_cleanup {
2012-01-17 14:56:56 +04:00
my ( $ storecfg , $ vmid , $ conf , $ keepActive ) = @ _ ;
2011-11-25 11:05:36 +04:00
2011-12-16 09:34:35 +04:00
eval {
fairsched_rmnod ( $ vmid ) ; # try to destroy group
2011-11-25 11:05:36 +04:00
2012-01-17 14:56:56 +04:00
if ( ! $ keepActive ) {
my $ vollist = get_vm_volumes ( $ conf ) ;
PVE::Storage:: deactivate_volumes ( $ storecfg , $ vollist ) ;
}
2011-12-16 09:34:35 +04:00
} ;
warn $@ if $@ ; # avoid errors - just warn
2011-08-23 09:47:04 +04:00
}
2011-09-15 10:21:32 +04:00
# Note: use $nockeck to skip tests if VM configuration file exists.
2012-01-17 14:56:56 +04:00
# We need that when migration VMs to other nodes (files already moved)
# Note: we set $keepActive in vzdump stop mode - volumes need to stay active
2011-08-23 09:47:04 +04:00
sub vm_stop {
2012-01-17 14:56:56 +04:00
my ( $ storecfg , $ vmid , $ skiplock , $ nocheck , $ timeout , $ shutdown , $ force , $ keepActive ) = @ _ ;
2011-12-15 15:47:39 +04:00
$ timeout = 60 if ! defined ( $ timeout ) ;
2011-11-25 11:05:36 +04:00
2011-12-15 15:47:39 +04:00
$ force = 1 if ! defined ( $ force ) && ! $ shutdown ;
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 10:21:32 +04:00
my $ pid = check_running ( $ vmid , $ nocheck ) ;
2011-11-25 11:05:36 +04:00
return if ! $ pid ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
my $ conf ;
2011-09-15 10:21:32 +04:00
if ( ! $ nocheck ) {
2011-11-25 11:05:36 +04:00
$ conf = load_config ( $ vmid ) ;
2011-09-15 10:21:32 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
}
2011-09-12 14:26:00 +04:00
2011-12-15 15:47:39 +04:00
eval {
if ( $ shutdown ) {
vm_monitor_command ( $ vmid , "system_powerdown" , $ nocheck ) ;
} else {
vm_monitor_command ( $ vmid , "quit" , $ nocheck ) ;
2012-01-27 12:35:26 +04:00
}
2011-12-15 15:47:39 +04:00
} ;
2011-08-23 09:47:04 +04:00
my $ err = $@ ;
if ( ! $ err ) {
my $ count = 0 ;
2011-09-15 10:21:32 +04:00
while ( ( $ count < $ timeout ) && check_running ( $ vmid , $ nocheck ) ) {
2011-08-23 09:47:04 +04:00
$ count + + ;
sleep 1 ;
}
if ( $ count >= $ timeout ) {
2011-12-15 15:47:39 +04:00
if ( $ force ) {
warn "VM still running - terminating now with SIGTERM\n" ;
kill 15 , $ pid ;
} else {
die "VM quit/powerdown failed - got timeout\n" ;
}
} else {
2012-01-17 14:56:56 +04:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf , $ keepActive ) if $ conf ;
2011-12-15 15:47:39 +04:00
return ;
2011-08-23 09:47:04 +04:00
}
} else {
2011-12-15 15:47:39 +04:00
if ( $ force ) {
warn "VM quit/powerdown failed - terminating now with SIGTERM\n" ;
kill 15 , $ pid ;
} else {
2012-01-27 12:35:26 +04:00
die "VM quit/powerdown failed\n" ;
2011-12-15 15:47:39 +04:00
}
2011-08-23 09:47:04 +04:00
}
# wait again
2011-11-25 11:05:36 +04:00
$ timeout = 10 ;
2011-08-23 09:47:04 +04:00
my $ count = 0 ;
2011-09-15 10:21:32 +04:00
while ( ( $ count < $ timeout ) && check_running ( $ vmid , $ nocheck ) ) {
2011-08-23 09:47:04 +04:00
$ count + + ;
sleep 1 ;
}
if ( $ count >= $ timeout ) {
2011-11-25 11:05:36 +04:00
warn "VM still running - terminating now with SIGKILL\n" ;
2011-08-23 09:47:04 +04:00
kill 9 , $ pid ;
2011-11-25 11:05:36 +04:00
sleep 1 ;
2011-08-23 09:47:04 +04:00
}
2012-01-17 14:56:56 +04:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf , $ keepActive ) if $ conf ;
2011-11-25 11:05:36 +04:00
} ) ;
2011-08-23 09:47:04 +04:00
}
sub vm_suspend {
my ( $ vmid , $ skiplock ) = @ _ ;
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
vm_monitor_command ( $ vmid , "stop" ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
sub vm_resume {
my ( $ vmid , $ skiplock ) = @ _ ;
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
vm_monitor_command ( $ vmid , "cont" ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
2011-10-10 15:17:40 +04:00
sub vm_sendkey {
my ( $ vmid , $ skiplock , $ key ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
vm_monitor_command ( $ vmid , "sendkey $key" ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
sub vm_destroy {
my ( $ storecfg , $ vmid , $ skiplock ) = @ _ ;
2011-09-15 11:11:27 +04:00
lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
my $ conf = load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-09-15 11:11:27 +04:00
check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
if ( ! check_running ( $ vmid ) ) {
fairsched_rmnod ( $ vmid ) ; # try to destroy group
destroy_vm ( $ storecfg , $ vmid ) ;
} else {
die "VM $vmid is running - destroy failed\n" ;
2011-08-23 09:47:04 +04:00
}
} ) ;
}
sub vm_stopall {
2011-11-25 11:05:36 +04:00
my ( $ storecfg , $ timeout ) = @ _ ;
2011-08-23 09:47:04 +04:00
$ timeout = 3 * 60 if ! $ timeout ;
2011-11-25 11:05:36 +04:00
my $ cleanuphash = { } ;
2011-08-23 09:47:04 +04:00
my $ vzlist = vzlist ( ) ;
my $ count = 0 ;
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
$ count + + ;
2011-11-25 11:05:36 +04:00
$ cleanuphash - > { $ vmid } = 1 ;
2011-08-23 09:47:04 +04:00
}
2011-11-25 11:05:36 +04:00
return if ! $ count ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
my $ msg = "Stopping Qemu Server - sending shutdown requests to all VMs\n" ;
syslog ( 'info' , $ msg ) ;
warn $ msg ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
2011-11-29 14:14:26 +04:00
eval { vm_monitor_command ( $ vmid , "system_powerdown" ) ; } ;
warn $@ if $@ ;
2011-11-25 11:05:36 +04:00
}
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
my $ wt = 5 ;
my $ maxtries = int ( ( $ timeout + $ wt - 1 ) / $ wt ) ;
my $ try = 0 ;
while ( ( $ try < $ maxtries ) && $ count ) {
$ try + + ;
sleep $ wt ;
2012-01-27 12:35:26 +04:00
2011-11-25 11:05:36 +04:00
$ vzlist = vzlist ( ) ;
$ count = 0 ;
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
$ count + + ;
2011-08-23 09:47:04 +04:00
}
2011-11-25 11:05:36 +04:00
last if ! $ count ;
}
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
if ( $ count ) {
2011-08-23 09:47:04 +04:00
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
2011-09-12 14:26:00 +04:00
2011-11-25 11:05:36 +04:00
warn "VM $vmid still running - sending stop now\n" ;
eval { vm_monitor_command ( $ vmid , "quit" ) ; } ;
warn $@ if $@ ;
2011-08-23 09:47:04 +04:00
}
$ timeout = 30 ;
2011-09-15 11:11:27 +04:00
$ maxtries = int ( ( $ timeout + $ wt - 1 ) / $ wt ) ;
2011-08-23 09:47:04 +04:00
$ try = 0 ;
while ( ( $ try < $ maxtries ) && $ count ) {
$ try + + ;
sleep $ wt ;
2012-01-27 12:35:26 +04:00
2011-08-23 09:47:04 +04:00
$ vzlist = vzlist ( ) ;
$ count = 0 ;
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
$ count + + ;
}
last if ! $ count ;
}
2011-11-25 11:05:36 +04:00
if ( $ count ) {
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
foreach my $ vmid ( keys %$ vzlist ) {
next if ! $ vzlist - > { $ vmid } - > { pid } ;
2011-09-12 14:26:00 +04:00
2011-11-25 11:05:36 +04:00
warn "VM $vmid still running - terminating now with SIGTERM\n" ;
kill 15 , $ vzlist - > { $ vmid } - > { pid } ;
}
sleep 1 ;
2011-08-23 09:47:04 +04:00
}
# this is called by system shotdown scripts, so remaining
# processes gets killed anyways (no need to send kill -9 here)
2011-11-25 11:05:36 +04:00
}
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
$ vzlist = vzlist ( ) ;
foreach my $ vmid ( keys %$ cleanuphash ) {
next if $ vzlist - > { $ vmid } - > { pid } ;
2012-01-27 12:35:26 +04:00
eval {
2011-11-25 11:05:36 +04:00
my $ conf = load_config ( $ vmid ) ;
2012-01-27 12:35:26 +04:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf ) ;
2011-11-25 11:05:36 +04:00
} ;
warn $@ if $@ ;
2011-08-23 09:47:04 +04:00
}
2011-11-25 11:05:36 +04:00
$ msg = "Qemu Server stopped\n" ;
syslog ( 'info' , $ msg ) ;
print $ msg ;
2011-08-23 09:47:04 +04:00
}
# pci helpers
sub file_write {
my ( $ filename , $ buf ) = @ _ ;
2011-09-15 11:11:27 +04:00
my $ fh = IO::File - > new ( $ filename , "w" ) ;
2011-08-23 09:47:04 +04:00
return undef if ! $ fh ;
my $ res = print $ fh $ buf ;
$ fh - > close ( ) ;
return $ res ;
}
sub pci_device_info {
my ( $ name ) = @ _ ;
my $ res ;
return undef if $ name !~ m/^([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])$/ ;
my ( $ domain , $ bus , $ slot , $ func ) = ( $ 1 , $ 2 , $ 3 , $ 4 ) ;
my $ irq = file_read_firstline ( "$pcisysfs/devices/$name/irq" ) ;
return undef if ! defined ( $ irq ) || $ irq !~ m/^\d+$/ ;
my $ vendor = file_read_firstline ( "$pcisysfs/devices/$name/vendor" ) ;
return undef if ! defined ( $ vendor ) || $ vendor !~ s/^0x// ;
my $ product = file_read_firstline ( "$pcisysfs/devices/$name/device" ) ;
return undef if ! defined ( $ product ) || $ product !~ s/^0x// ;
$ res = {
name = > $ name ,
vendor = > $ vendor ,
product = > $ product ,
domain = > $ domain ,
bus = > $ bus ,
slot = > $ slot ,
func = > $ func ,
irq = > $ irq ,
has_fl_reset = > - f "$pcisysfs/devices/$name/reset" || 0 ,
} ;
return $ res ;
}
sub pci_dev_reset {
my ( $ dev ) = @ _ ;
my $ name = $ dev - > { name } ;
my $ fn = "$pcisysfs/devices/$name/reset" ;
2011-09-15 11:11:27 +04:00
return file_write ( $ fn , "1" ) ;
2011-08-23 09:47:04 +04:00
}
sub pci_dev_bind_to_stub {
my ( $ dev ) = @ _ ;
my $ name = $ dev - > { name } ;
my $ testdir = "$pcisysfs/drivers/pci-stub/$name" ;
return 1 if - d $ testdir ;
my $ data = "$dev->{vendor} $dev->{product}" ;
2011-09-15 11:11:27 +04:00
return undef if ! file_write ( "$pcisysfs/drivers/pci-stub/new_id" , $ data ) ;
2011-08-23 09:47:04 +04:00
my $ fn = "$pcisysfs/devices/$name/driver/unbind" ;
2011-09-15 11:11:27 +04:00
if ( ! file_write ( $ fn , $ name ) ) {
2011-08-23 09:47:04 +04:00
return undef if - f $ fn ;
}
$ fn = "$pcisysfs/drivers/pci-stub/bind" ;
if ( ! - d $ testdir ) {
2011-09-15 11:11:27 +04:00
return undef if ! file_write ( $ fn , $ name ) ;
2011-08-23 09:47:04 +04:00
}
return - d $ testdir ;
}
2012-01-27 12:35:26 +04:00
sub print_pci_addr {
2011-09-15 08:47:39 +04:00
my ( $ id ) = @ _ ;
2011-09-15 11:11:27 +04:00
2011-09-15 08:47:39 +04:00
my $ res = '' ;
2011-09-15 11:11:27 +04:00
my $ devices = {
2011-10-05 05:15:08 +04:00
#addr1 : ide,parallel,serial (motherboard)
#addr2 : first videocard
2011-09-30 15:10:27 +04:00
balloon0 = > { bus = > 0 , addr = > 3 } ,
2011-10-03 16:53:10 +04:00
watchdog = > { bus = > 0 , addr = > 4 } ,
2012-01-20 14:42:02 +04:00
lsi0 = > { bus = > 0 , addr = > 5 } ,
lsi1 = > { bus = > 0 , addr = > 6 } ,
2012-02-01 16:25:20 +04:00
ahci0 = > { bus = > 0 , addr = > 7 } ,
2011-09-15 11:11:27 +04:00
virtio0 = > { bus = > 0 , addr = > 10 } ,
virtio1 = > { bus = > 0 , addr = > 11 } ,
virtio2 = > { bus = > 0 , addr = > 12 } ,
virtio3 = > { bus = > 0 , addr = > 13 } ,
virtio4 = > { bus = > 0 , addr = > 14 } ,
virtio5 = > { bus = > 0 , addr = > 15 } ,
2011-10-03 16:53:09 +04:00
hostpci0 = > { bus = > 0 , addr = > 16 } ,
hostpci1 = > { bus = > 0 , addr = > 17 } ,
2011-10-04 10:25:37 +04:00
net0 = > { bus = > 0 , addr = > 18 } ,
net1 = > { bus = > 0 , addr = > 19 } ,
net2 = > { bus = > 0 , addr = > 20 } ,
net3 = > { bus = > 0 , addr = > 21 } ,
net4 = > { bus = > 0 , addr = > 22 } ,
net5 = > { bus = > 0 , addr = > 23 } ,
2011-10-05 05:15:08 +04:00
#addr29 : usb-host (pve-usb.cfg)
2011-09-15 11:11:27 +04:00
} ;
if ( defined ( $ devices - > { $ id } - > { bus } ) && defined ( $ devices - > { $ id } - > { addr } ) ) {
2011-09-15 08:47:39 +04:00
my $ addr = sprintf ( "0x%x" , $ devices - > { $ id } - > { addr } ) ;
$ res = ",bus=pci.$devices->{$id}->{bus},addr=$addr" ;
}
return $ res ;
}
2011-09-16 06:46:26 +04:00
sub vm_balloonset {
2011-09-16 08:57:54 +04:00
my ( $ vmid , $ value ) = @ _ ;
2011-09-16 06:46:26 +04:00
2011-11-25 11:05:36 +04:00
vm_monitor_command ( $ vmid , "balloon $value" ) ;
2011-09-16 06:46:26 +04:00
}
2011-10-17 15:49:48 +04:00
# vzdump restore implementaion
sub archive_read_firstfile {
my $ archive = shift ;
2012-01-27 12:35:26 +04:00
2011-10-17 15:49:48 +04:00
die "ERROR: file '$archive' does not exist\n" if ! - f $ archive ;
# try to detect archive type first
my $ pid = open ( TMP , "tar tf '$archive'|" ) ||
die "unable to open file '$archive'\n" ;
my $ firstfile = <TMP> ;
kill 15 , $ pid ;
close TMP ;
die "ERROR: archive contaions no data\n" if ! $ firstfile ;
chomp $ firstfile ;
return $ firstfile ;
}
sub restore_cleanup {
my $ statfile = shift ;
print STDERR "starting cleanup\n" ;
if ( my $ fd = IO::File - > new ( $ statfile , "r" ) ) {
while ( defined ( my $ line = <$fd> ) ) {
if ( $ line =~ m/vzdump:([^\s:]*):(\S+)$/ ) {
my $ volid = $ 2 ;
eval {
if ( $ volid =~ m | ^ / | ) {
unlink $ volid || die 'unlink failed\n' ;
} else {
my $ cfg = cfs_read_file ( 'storage.cfg' ) ;
PVE::Storage:: vdisk_free ( $ cfg , $ volid ) ;
}
2012-01-27 12:35:26 +04:00
print STDERR "temporary volume '$volid' sucessfuly removed\n" ;
2011-10-17 15:49:48 +04:00
} ;
print STDERR "unable to cleanup '$volid' - $@" if $@ ;
} else {
print STDERR "unable to parse line in statfile - $line" ;
2012-01-27 12:35:26 +04:00
}
2011-10-17 15:49:48 +04:00
}
$ fd - > close ( ) ;
}
}
sub restore_archive {
2012-02-02 09:39:38 +04:00
my ( $ archive , $ vmid , $ user , $ opts ) = @ _ ;
2011-10-17 15:49:48 +04:00
2011-10-19 13:27:42 +04:00
if ( $ archive ne '-' ) {
my $ firstfile = archive_read_firstfile ( $ archive ) ;
die "ERROR: file '$archive' dos not lock like a QemuServer vzdump backup\n"
if $ firstfile ne 'qemu-server.conf' ;
}
2011-10-17 15:49:48 +04:00
my $ tocmd = "/usr/lib/qemu-server/qmextract" ;
2011-10-25 13:37:56 +04:00
$ tocmd . = " --storage " . PVE::Tools:: shellquote ( $ opts - > { storage } ) if $ opts - > { storage } ;
2012-02-02 09:39:38 +04:00
$ tocmd . = " --pool " . PVE::Tools:: shellquote ( $ opts - > { pool } ) if $ opts - > { pool } ;
2011-10-17 15:49:48 +04:00
$ tocmd . = ' --prealloc' if $ opts - > { prealloc } ;
$ tocmd . = ' --info' if $ opts - > { info } ;
2012-02-02 09:39:38 +04:00
# tar option "xf" does not autodetect compression when read from STDIN,
2011-10-19 13:27:42 +04:00
# so we pipe to zcat
2011-10-25 13:37:56 +04:00
my $ cmd = "zcat -f|tar xf " . PVE::Tools:: shellquote ( $ archive ) . " " .
PVE::Tools:: shellquote ( "--to-command=$tocmd" ) ;
2011-10-17 15:49:48 +04:00
my $ tmpdir = "/var/tmp/vzdumptmp$$" ;
mkpath $ tmpdir ;
local $ ENV { VZDUMP_TMPDIR } = $ tmpdir ;
local $ ENV { VZDUMP_VMID } = $ vmid ;
2012-02-02 09:39:38 +04:00
local $ ENV { VZDUMP_USER } = $ user ;
2011-10-17 15:49:48 +04:00
my $ conffile = PVE::QemuServer:: config_file ( $ vmid ) ;
my $ tmpfn = "$conffile.$$.tmp" ;
# disable interrupts (always do cleanups)
local $ SIG { INT } = $ SIG { TERM } = $ SIG { QUIT } = $ SIG { HUP } = sub {
print STDERR "got interrupt - ignored\n" ;
} ;
2012-01-27 12:35:26 +04:00
eval {
2011-10-17 15:49:48 +04:00
# enable interrupts
local $ SIG { INT } = $ SIG { TERM } = $ SIG { QUIT } = $ SIG { HUP } = $ SIG { PIPE } = sub {
die "interrupted by signal\n" ;
} ;
2011-10-19 13:27:42 +04:00
if ( $ archive eq '-' ) {
print "extracting archive from STDIN\n" ;
run_command ( $ cmd , input = > "<&STDIN" ) ;
} else {
print "extracting archive '$archive'\n" ;
run_command ( $ cmd ) ;
}
2011-10-17 15:49:48 +04:00
return if $ opts - > { info } ;
# read new mapping
my $ map = { } ;
my $ statfile = "$tmpdir/qmrestore.stat" ;
if ( my $ fd = IO::File - > new ( $ statfile , "r" ) ) {
while ( defined ( my $ line = <$fd> ) ) {
if ( $ line =~ m/vzdump:([^\s:]*):(\S+)$/ ) {
$ map - > { $ 1 } = $ 2 if $ 1 ;
} else {
print STDERR "unable to parse line in statfile - $line\n" ;
}
}
$ fd - > close ( ) ;
}
my $ confsrc = "$tmpdir/qemu-server.conf" ;
my $ srcfd = new IO:: File ( $ confsrc , "r" ) ||
die "unable to open file '$confsrc'\n" ;
my $ outfd = new IO:: File ( $ tmpfn , "w" ) ||
die "unable to write config for VM $vmid\n" ;
2011-10-18 11:14:05 +04:00
my $ netcount = 0 ;
2011-10-17 15:49:48 +04:00
while ( defined ( my $ line = <$srcfd> ) ) {
next if $ line =~ m/^\#vzdump\#/ ;
next if $ line =~ m/^lock:/ ;
next if $ line =~ m/^unused\d+:/ ;
2011-10-18 11:14:05 +04:00
if ( ( $ line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/ ) ) {
# try to convert old 1.X settings
my ( $ id , $ ind , $ ethcfg ) = ( $ 1 , $ 2 , $ 3 ) ;
foreach my $ devconfig ( PVE::Tools:: split_list ( $ ethcfg ) ) {
my ( $ model , $ macaddr ) = split ( /\=/ , $ devconfig ) ;
$ macaddr = PVE::Tools:: random_ether_addr ( ) if ! $ macaddr || $ opts - > { unique } ;
my $ net = {
model = > $ model ,
bridge = > "vmbr$ind" ,
macaddr = > $ macaddr ,
} ;
my $ netstr = print_net ( $ net ) ;
print $ outfd "net${netcount}: $netstr\n" ;
$ netcount + + ;
}
} elsif ( ( $ line =~ m/^(net\d+):\s*(\S+)\s*$/ ) && ( $ opts - > { unique } ) ) {
my ( $ id , $ netstr ) = ( $ 1 , $ 2 ) ;
my $ net = parse_net ( $ netstr ) ;
$ net - > { macaddr } = PVE::Tools:: random_ether_addr ( ) if $ net - > { macaddr } ;
$ netstr = print_net ( $ net ) ;
2012-01-27 12:35:26 +04:00
print $ outfd "$id: $netstr\n" ;
2011-10-18 11:14:05 +04:00
} elsif ( $ line =~ m/^((ide|scsi|virtio)\d+):\s*(\S+)\s*$/ ) {
2011-10-17 15:49:48 +04:00
my $ virtdev = $ 1 ;
my $ value = $ 2 ;
if ( $ line =~ m/backup=no/ ) {
print $ outfd "#$line" ;
} elsif ( $ virtdev && $ map - > { $ virtdev } ) {
my $ di = PVE::QemuServer:: parse_drive ( $ virtdev , $ value ) ;
$ di - > { file } = $ map - > { $ virtdev } ;
$ value = PVE::QemuServer:: print_drive ( $ vmid , $ di ) ;
print $ outfd "$virtdev: $value\n" ;
} else {
print $ outfd $ line ;
}
} else {
print $ outfd $ line ;
}
}
$ srcfd - > close ( ) ;
$ outfd - > close ( ) ;
} ;
my $ err = $@ ;
2012-01-27 12:35:26 +04:00
if ( $ err ) {
2011-10-17 15:49:48 +04:00
unlink $ tmpfn ;
restore_cleanup ( "$tmpdir/qmrestore.stat" ) if ! $ opts - > { info } ;
2012-01-27 12:35:26 +04:00
2011-10-17 15:49:48 +04:00
die $ err ;
2012-01-27 12:35:26 +04:00
}
2011-10-17 15:49:48 +04:00
rmtree $ tmpdir ;
rename $ tmpfn , $ conffile ||
die "unable to commit configuration file '$conffile'\n" ;
} ;
2011-08-23 09:47:04 +04:00
1 ;