2011-08-23 09:47:04 +04:00
package PVE::QemuServer ;
use strict ;
2013-10-01 15:14:49 +04:00
use warnings ;
2011-08-23 09:47:04 +04:00
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 ;
2012-03-20 15:25:08 +04:00
use Digest::SHA ;
2011-08-23 09:47:04 +04:00
use Fcntl ':flock' ;
use Cwd 'abs_path' ;
use IPC::Open3 ;
2012-05-29 16:01:50 +04:00
use JSON ;
2011-08-23 09:47:04 +04:00
use Fcntl ;
use PVE::SafeSyslog ;
use Storable qw( dclone ) ;
use PVE::Exception qw( raise raise_param_exc ) ;
use PVE::Storage ;
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
use PVE::Tools qw( run_command lock_file lock_file_full file_read_firstline dir_glob_foreach ) ;
2012-09-10 12:15:14 +04:00
use PVE::JSONSchema qw( get_standard_option ) ;
2011-08-23 09:47:04 +04:00
use PVE::Cluster qw( cfs_register_file cfs_read_file cfs_write_file cfs_lock_file ) ;
use PVE::INotify ;
use PVE::ProcFSTools ;
2016-03-07 14:41:12 +03:00
use PVE::QemuConfig ;
2012-07-13 09:06:22 +04:00
use PVE::QMPClient ;
2012-12-12 18:35:26 +04:00
use PVE::RPCEnvironment ;
2016-05-23 10:47:50 +03:00
use PVE::QemuServer::Memory ;
2011-09-15 11:11:27 +04:00
use Time::HiRes qw( gettimeofday ) ;
2015-11-21 10:48:59 +03:00
use File::Copy qw( copy ) ;
2016-03-18 13:42:33 +03:00
use URI::Escape ;
2011-08-23 09:47:04 +04:00
2015-05-06 10:57:34 +03:00
my $ qemu_snap_storage = { rbd = > 1 , sheepdog = > 1 } ;
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
2012-09-07 15:07:23 +04:00
# can be set to 'migrate', 'backup', 'snapshot' or 'rollback'. Most actions are not
2011-08-23 09:47:04 +04:00
# allowed when such lock is set. But you can ignore this kind of
# lock with the --skiplock flag.
2012-05-30 14:08:33 +04:00
cfs_register_file ( '/qemu-server/' ,
2012-02-02 17:01:08 +04:00
\ & 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 ,
} ) ;
2012-09-10 10:55:04 +04:00
PVE::JSONSchema:: register_standard_option ( 'pve-snapshot-name' , {
description = > "The name of the snapshot." ,
type = > 'string' , format = > 'pve-configid' ,
maxLength = > 40 ,
} ) ;
2011-08-23 09:47:04 +04:00
#no warnings 'redefine';
2015-05-28 16:59:22 +03:00
sub cgroups_write {
my ( $ controller , $ vmid , $ option , $ value ) = @ _ ;
2015-05-29 09:23:13 +03:00
my $ path = "/sys/fs/cgroup/$controller/qemu.slice/$vmid.scope/$option" ;
PVE::ProcFSTools:: write_proc_entry ( $ path , $ value ) ;
2015-05-28 16:59:22 +03:00
}
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" ;
2016-05-01 10:24:25 +03:00
my $ cpu_vendor_list = {
# Intel CPUs
486 = > 'GenuineIntel' ,
pentium = > 'GenuineIntel' ,
pentium2 = > 'GenuineIntel' ,
pentium3 = > 'GenuineIntel' ,
coreduo = > 'GenuineIntel' ,
core2duo = > 'GenuineIntel' ,
Conroe = > 'GenuineIntel' ,
Penryn = > 'GenuineIntel' ,
Nehalem = > 'GenuineIntel' ,
Westmere = > 'GenuineIntel' ,
SandyBridge = > 'GenuineIntel' ,
IvyBridge = > 'GenuineIntel' ,
Haswell = > 'GenuineIntel' ,
'Haswell-noTSX' = > 'GenuineIntel' ,
Broadwell = > 'GenuineIntel' ,
'Broadwell-noTSX' = > 'GenuineIntel' ,
# AMD CPUs
athlon = > 'AuthenticAMD' ,
phenom = > 'AuthenticAMD' ,
Opteron_G1 = > 'AuthenticAMD' ,
Opteron_G2 = > 'AuthenticAMD' ,
Opteron_G3 = > 'AuthenticAMD' ,
Opteron_G4 = > 'AuthenticAMD' ,
Opteron_G5 = > 'AuthenticAMD' ,
# generic types, use vendor from host node
host = > 'default' ,
kvm32 = > 'default' ,
kvm64 = > 'default' ,
qemu32 = > 'default' ,
qemu64 = > 'default' ,
} ;
2016-03-30 13:20:07 +03:00
my $ cpu_fmt = {
2016-01-26 12:16:08 +03:00
cputype = > {
description = > "Emulated CPU type." ,
type = > 'string' ,
2016-05-11 11:11:49 +03:00
enum = > [ sort { "\L$a" cmp "\L$b" } keys %$ cpu_vendor_list ] ,
2016-03-30 13:20:08 +03:00
format_description = > 'cputype' ,
2016-01-26 12:16:08 +03:00
default = > 'kvm64' ,
default_key = > 1 ,
} ,
hidden = > {
description = > "Do not identify as a KVM virtual machine." ,
type = > 'boolean' ,
optional = > 1 ,
default = > 0
} ,
} ;
2016-03-30 13:20:13 +03:00
my $ watchdog_fmt = {
model = > {
default_key = > 1 ,
type = > 'string' ,
enum = > [ qw( i6300esb ib700 ) ] ,
description = > "Watchdog type to emulate." ,
default = > 'i6300esb' ,
optional = > 1 ,
} ,
action = > {
type = > 'string' ,
enum = > [ qw( reset shutdown poweroff pause debug none ) ] ,
description = > "The action to perform if after activation the guest fails to poll the watchdog in time." ,
optional = > 1 ,
} ,
} ;
PVE::JSONSchema:: register_format ( 'pve-qm-watchdog' , $ watchdog_fmt ) ;
2011-08-23 09:47:04 +04:00
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 ,
2015-01-27 09:16:22 +03:00
type = > 'string' , format = > 'pve-hotplug-features' ,
description = > "Selectively enable hotplug features. This is a comma separated list of hotplug features: 'network', 'disk', 'cpu', 'memory' and 'usb'. Use '0' to disable hotplug completely. Value '1' is an alias for the default 'network,disk,usb'." ,
default = > 'network,disk,usb' ,
2011-10-10 18:46:53 +04:00
} ,
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." ,
2012-09-07 15:07:23 +04:00
enum = > [ qw( migrate backup snapshot rollback ) ] ,
2011-08-23 09:47:04 +04:00
} ,
cpulimit = > {
optional = > 1 ,
2015-06-02 17:03:25 +03:00
type = > 'number' ,
2016-05-19 14:13:25 +03:00
description = > "Limit of CPU usage." ,
verbose_description = > "Limit of CPU usage.\n\nNOTE: If the computer has 2 CPUs, it has total of '2' CPU time. Value '0' indicates no CPU limit." ,
2011-08-23 09:47:04 +04:00
minimum = > 0 ,
2015-06-02 17:03:25 +03:00
maximum = > 128 ,
2016-05-19 14:13:25 +03:00
default = > 0 ,
2011-08-23 09:47:04 +04:00
} ,
cpuunits = > {
optional = > 1 ,
type = > 'integer' ,
2016-05-19 14:13:25 +03:00
description = > "CPU weight for a VM." ,
verbose_description = > "CPU weight for a VM. Argument is used in the kernel fair scheduler. The larger the number is, the more CPU time this VM gets. Number is relative to weights of all the other running VMs.\n\nNOTE: You can disable fair-scheduler configuration by setting this to 0." ,
2011-08-23 09:47:04 +04:00
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' ,
2012-12-19 10:24:39 +04:00
description = > "Amount of target RAM for the VM in MB. Using zero disables the ballon driver." ,
minimum = > 0 ,
} ,
shares = > {
optional = > 1 ,
type = > 'integer' ,
description = > "Amount of memory shares for auto-ballooning. The larger the number is, the more memory this VM gets. Number is relative to weights of all other running VMs. Using zero disables auto-ballooning" ,
minimum = > 0 ,
maximum = > 50000 ,
default = > 1000 ,
2011-09-16 06:46:26 +04:00
} ,
2011-08-23 09:47:04 +04:00
keyboard = > {
optional = > 1 ,
type = > 'string' ,
2016-05-19 17:27:30 +03:00
description = > "Keybord layout for vnc server. Default is read from the '/etc/pve/datacenter.conf' 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 ,
2012-03-13 10:00:27 +04:00
type = > 'string' , format = > 'dns-name' ,
2011-08-23 09:47:04 +04:00
description = > "Set a name for the VM. Only used on the configuration web interface." ,
} ,
2012-07-30 16:58:40 +04:00
scsihw = > {
optional = > 1 ,
type = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "SCSI controller model" ,
2015-03-27 05:41:52 +03:00
enum = > [ qw( lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi ) ] ,
2012-07-30 16:58:40 +04:00
default = > 'lsi' ,
} ,
2011-08-23 09:47:04 +04:00
description = > {
optional = > 1 ,
type = > 'string' ,
2012-03-01 11:13:14 +04:00
description = > "Description for the VM. Only used on the configuration web interface. This is saved as comment inside the configuration file." ,
2011-08-23 09:47:04 +04:00
} ,
ostype = > {
optional = > 1 ,
type = > 'string' ,
2013-09-23 11:10:32 +04:00
enum = > [ qw( other wxp w2k w2k3 w2k8 wvista win7 win8 l24 l26 solaris ) ] ,
2016-05-19 14:13:25 +03:00
description = > "Specify guest operating system." ,
verbose_description = > << EODESC ,
Specify guest operating system . This is used to enable special
optimization / features for specific operating systems:
[ horizontal ]
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
win8 ; ; Microsoft Windows 8 / 2012
l24 ; ; Linux 2.4 Kernel
l26 ; ; Linux 2.6 / 3 . X Kernel
solaris ; ; Solaris /OpenSolaris/ OpenIndiania kernel
2011-08-23 09:47:04 +04:00
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." ,
2012-08-29 11:19:21 +04:00
pattern = > '(ide|sata|scsi|virtio)\d+' ,
2011-08-23 09:47:04 +04:00
} ,
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 ,
} ,
add numa options v3
This enable numa support inside the guest, and share the memory and cores across the sockets numa nodes.
numa: 0|1
example:
-------
sockets:2
cores:2
memory:4096
numa: 1
qemu command line
-----------------
-object memory-backend-ram,size=2048,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=2048,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:47 +03:00
numa = > {
optional = > 1 ,
type = > 'boolean' ,
2016-03-16 16:53:07 +03:00
description = > "Enable/disable NUMA." ,
add numa options v3
This enable numa support inside the guest, and share the memory and cores across the sockets numa nodes.
numa: 0|1
example:
-------
sockets:2
cores:2
memory:4096
numa: 1
qemu command line
-----------------
-object memory-backend-ram,size=2048,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=2048,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:47 +03:00
default = > 0 ,
} ,
2016-06-15 06:04:02 +03:00
hugepages = > {
optional = > 1 ,
type = > 'string' ,
description = > "Enable/disable hugepages memory." ,
enum = > [ qw( any 2 1024 ) ] ,
} ,
2015-01-09 18:30:35 +03:00
vcpus = > {
2014-01-07 16:32:50 +04:00
optional = > 1 ,
type = > 'integer' ,
2015-01-09 18:30:35 +03:00
description = > "Number of hotplugged vcpus." ,
2014-01-07 16:32:50 +04:00
minimum = > 1 ,
2015-01-09 18:30:35 +03:00
default = > 0 ,
2014-01-07 16:32:50 +04:00
} ,
2011-08-23 09:47:04 +04:00
acpi = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable ACPI." ,
default = > 1 ,
} ,
2012-09-04 08:31:44 +04:00
agent = > {
2012-09-03 11:51:08 +04:00
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable Qemu GuestAgent." ,
2012-09-04 08:46:53 +04:00
default = > 0 ,
2012-09-03 11:51:08 +04:00
} ,
2011-08-23 09:47:04 +04:00
kvm = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable KVM hardware virtualization." ,
default = > 1 ,
} ,
tdf = > {
optional = > 1 ,
type = > 'boolean' ,
2012-09-26 14:42:03 +04:00
description = > "Enable/disable time drift fix." ,
default = > 0 ,
2011-08-23 09:47:04 +04:00
} ,
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' ,
2016-05-19 14:13:25 +03:00
description = > "Select the VGA type." ,
verbose_description = > "Select the VGA type. If you want to use high resolution" .
2016-03-16 16:53:07 +03:00
" modes (>= 1280x1024x16) then you should use the options " .
"'std' or 'vmware'. Default is 'std' for win8/win7/w2k8, and " .
"'cirrus' for other OS types. The 'qxl' option enables the SPICE " .
"display sever. For win* OS you can select how many independent " .
"displays you want, Linux guests can add displays them self. " .
"You can also run without any graphic card, using a serial device" .
" as terminal." ,
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
enum = > [ qw( std cirrus vmware qxl serial0 serial1 serial2 serial3 qxl2 qxl3 qxl4 ) ] ,
2011-08-23 09:47:04 +04:00
} ,
2011-09-08 13:39:56 +04:00
watchdog = > {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-watchdog' ,
2016-05-19 14:13:25 +03:00
description = > "Create a virtual hardware watchdog device." ,
verbose_description = > "Create a virtual hardware watchdog device. Once enabled" .
2016-03-16 16:53:07 +03:00
" (by a guest action), the watchdog must be periodically polled " .
"by an agent inside the guest or else the watchdog will reset " .
"the guest (or execute the respective action specified)" ,
2011-09-08 13:39:56 +04:00
} ,
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' ,
} ,
2015-04-22 11:02:33 +03:00
startup = > get_standard_option ( 'pve-startup-order' ) ,
2013-04-18 19:05:29 +04:00
template = > {
optional = > 1 ,
type = > 'boolean' ,
description = > "Enable/disable Template." ,
default = > 0 ,
} ,
2011-08-23 09:47:04 +04:00
args = > {
optional = > 1 ,
type = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Arbitrary arguments passed to kvm." ,
verbose_description = > << EODESCR ,
2016-04-15 14:12:00 +03:00
Arbitrary arguments passed to kvm , for example:
2011-08-23 09:47:04 +04:00
args: - no - reboot - no - hpet
2016-04-15 14:12:00 +03:00
NOTE: this option is for experts only .
2011-08-23 09:47:04 +04:00
EODESCR
} ,
tablet = > {
optional = > 1 ,
type = > 'boolean' ,
default = > 1 ,
2016-05-19 14:13:25 +03:00
description = > "Enable/disable the USB tablet device." ,
verbose_description = > "Enable/disable the USB tablet device. This device is " .
2016-03-16 16:53:07 +03:00
"usually needed to allow absolute mouse positioning with VNC. " .
"Else the mouse runs out of sync with normal VNC clients. " .
"If you're running lots of console-only guests on one host, " .
"you may consider disabling this to save some context switches. " .
"This is turned off by default if you use spice (-vga=qxl)." ,
2011-08-23 09:47:04 +04:00
} ,
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 ,
2012-12-30 22:03:00 +04:00
type = > 'number' ,
2011-08-23 09:47:04 +04:00
description = > "Set maximum tolerated downtime (in seconds) for migrations." ,
minimum = > 0 ,
2012-12-30 22:03:00 +04:00
default = > 0.1 ,
2011-08-23 09:47:04 +04:00
} ,
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' ,
2016-03-30 13:20:07 +03:00
format = > $ cpu_fmt ,
2011-08-23 09:47:04 +04:00
} ,
2012-09-10 12:15:14 +04:00
parent = > get_standard_option ( 'pve-snapshot-name' , {
optional = > 1 ,
description = > "Parent snapshot name. This is used internally, and should not be modified." ,
} ) ,
2012-09-11 10:45:39 +04:00
snaptime = > {
optional = > 1 ,
description = > "Timestamp for snapshots." ,
type = > 'integer' ,
minimum = > 0 ,
} ,
2012-09-12 13:59:48 +04:00
vmstate = > {
optional = > 1 ,
type = > 'string' , format = > 'pve-volume-id' ,
description = > "Reference to a volume which stores the VM state. This is used internally for snapshots." ,
} ,
2013-06-05 11:18:23 +04:00
machine = > {
description = > "Specific the Qemu machine type." ,
type = > 'string' ,
2015-11-05 21:43:10 +03:00
pattern = > '(pc|pc(-i440fx)?-\d+\.\d+(\.pxe)?|q35|pc-q35-\d+\.\d+(\.pxe)?)' ,
2013-06-05 11:18:23 +04:00
maxLength = > 40 ,
optional = > 1 ,
} ,
2014-06-26 13:12:25 +04:00
smbios1 = > {
description = > "Specify SMBIOS type 1 fields." ,
type = > 'string' , format = > 'pve-qm-smbios1' ,
maxLength = > 256 ,
optional = > 1 ,
} ,
2015-09-03 16:35:37 +03:00
protection = > {
optional = > 1 ,
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > "Sets the protection flag of the VM. This will disable the remove VM and remove disk operations." ,
2015-09-03 16:35:37 +03:00
default = > 0 ,
} ,
2015-12-10 12:48:04 +03:00
bios = > {
2015-11-21 10:48:59 +03:00
optional = > 1 ,
2015-12-10 12:48:04 +03:00
type = > 'string' ,
enum = > [ qw( seabios ovmf ) ] ,
description = > "Select BIOS implementation." ,
default = > 'seabios' ,
2015-11-21 10:48:59 +03:00
} ,
2011-08-23 09:47:04 +04:00
} ;
# 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 ;
2012-08-21 12:03:17 +04:00
my $ MAX_VIRTIO_DISKS = 16 ;
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 ;
2012-08-20 13:10:24 +04:00
my $ MAX_NETS = 32 ;
2011-08-23 09:47:04 +04:00
my $ MAX_UNUSED_DISKS = 8 ;
2014-08-06 09:39:33 +04:00
my $ MAX_HOSTPCI_DEVICES = 4 ;
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 ;
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
my $ MAX_NUMA = 8 ;
2016-03-30 13:20:10 +03:00
my $ numa_fmt = {
cpus = > {
type = > "string" ,
pattern = > qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/ ,
2016-05-19 14:13:25 +03:00
description = > "CPUs accessing this NUMA node." ,
2016-03-30 13:20:10 +03:00
format_description = > "id[-id];..." ,
} ,
memory = > {
type = > "number" ,
2016-05-19 14:13:25 +03:00
description = > "Amount of memory this NUMA node provides." ,
2016-03-30 13:20:10 +03:00
optional = > 1 ,
} ,
hostnodes = > {
type = > "string" ,
pattern = > qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/ ,
2016-05-19 14:13:25 +03:00
description = > "Host NUMA nodes to use." ,
2016-03-30 13:20:10 +03:00
format_description = > "id[-id];..." ,
optional = > 1 ,
} ,
policy = > {
type = > 'string' ,
enum = > [ qw( preferred bind interleave ) ] ,
2016-05-19 14:13:25 +03:00
description = > "NUMA allocation policy." ,
2016-03-30 13:20:10 +03:00
optional = > 1 ,
} ,
} ;
PVE::JSONSchema:: register_format ( 'pve-qm-numanode' , $ numa_fmt ) ;
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
my $ numadesc = {
optional = > 1 ,
2016-03-30 13:20:10 +03:00
type = > 'string' , format = > $ numa_fmt ,
2016-05-19 14:13:25 +03:00
description = > "NUMA topology." ,
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-numanode" , $ numadesc ) ;
for ( my $ i = 0 ; $ i < $ MAX_NUMA ; $ i + + ) {
$ confdesc - > { "numa$i" } = $ numadesc ;
}
2011-08-23 09:47:04 +04:00
my $ nic_model_list = [ 'rtl8139' , 'ne2k_pci' , 'e1000' , 'pcnet' , 'virtio' ,
2015-01-17 15:46:32 +03:00
'ne2k_isa' , 'i82551' , 'i82557b' , 'i82559er' , 'vmxnet3' ,
'e1000-82540em' , 'e1000-82544gc' , 'e1000-82545em' ] ;
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
2016-05-19 14:13:25 +03:00
my $ net_fmt_bridge_descr = << __EOD__ ;
Bridge to attach the network device to . The Proxmox VE standard bridge
is called 'vmbr0' .
If you do not specify a 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 .
__EOD__
2016-03-30 13:20:11 +03:00
my $ net_fmt = {
macaddr = > {
type = > 'string' ,
pattern = > qr/[0-9a-f]{2}(?::[0-9a-f]{2}){5}/ i ,
2016-05-19 14:13:25 +03:00
description = > "MAC address. That address must be unique withing your network. This is automatically generated if not specified." ,
2016-03-30 13:20:11 +03:00
format_description = > "XX:XX:XX:XX:XX:XX" ,
optional = > 1 ,
} ,
2016-05-11 11:11:49 +03:00
model = > {
type = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Network Card Model. The 'virtio' model provides the best performance with very low CPU overhead. If your guest does not support this driver, it is usually best to use 'e1000'." ,
2016-05-11 11:11:49 +03:00
format_description = > 'model' ,
enum = > $ nic_model_list ,
default_key = > 1 ,
} ,
( map { $ _ = > { keyAlias = > 'model' , alias = > 'macaddr' } } @$ nic_model_list ) ,
2016-03-30 13:20:11 +03:00
bridge = > {
type = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > $ net_fmt_bridge_descr ,
2016-03-30 13:20:11 +03:00
format_description = > 'bridge' ,
optional = > 1 ,
} ,
queues = > {
type = > 'integer' ,
minimum = > 0 , maximum = > 16 ,
description = > 'Number of packet queues to be used on the device.' ,
optional = > 1 ,
} ,
rate = > {
type = > 'number' ,
minimum = > 0 ,
2016-05-19 14:13:25 +03:00
description = > "Rate limit in mbps (megabytes per second) as floating point number." ,
2016-03-30 13:20:11 +03:00
optional = > 1 ,
} ,
tag = > {
type = > 'integer' ,
2016-05-18 12:18:28 +03:00
minimum = > 1 , maximum = > 4094 ,
2016-03-30 13:20:11 +03:00
description = > 'VLAN tag to apply to packets on this interface.' ,
optional = > 1 ,
} ,
trunks = > {
type = > 'string' ,
pattern = > qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/ ,
description = > 'VLAN trunks to pass through this interface.' ,
2016-05-11 11:11:49 +03:00
format_description = > 'vlanid[;vlanid...]' ,
2016-03-30 13:20:11 +03:00
optional = > 1 ,
} ,
firewall = > {
type = > 'boolean' ,
description = > 'Whether this interface should be protected by the firewall.' ,
optional = > 1 ,
} ,
link_down = > {
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > 'Whether this interface should be disconnected (like pulling the plug).' ,
2016-03-30 13:20:11 +03:00
optional = > 1 ,
} ,
} ;
2016-05-19 14:13:25 +03:00
2011-08-23 09:47:04 +04:00
my $ netdesc = {
optional = > 1 ,
2016-05-11 11:11:49 +03:00
type = > 'string' , format = > $ net_fmt ,
2016-05-19 14:13:25 +03:00
description = > "Specify network devices." ,
2011-08-23 09:47:04 +04:00
} ;
2016-05-19 14:13:25 +03:00
2011-08-23 09:47:04 +04:00
PVE::JSONSchema:: register_standard_option ( "pve-qm-net" , $ netdesc ) ;
for ( my $ i = 0 ; $ i < $ MAX_NETS ; $ i + + ) {
$ confdesc - > { "net$i" } = $ netdesc ;
}
2016-04-21 12:40:24 +03:00
PVE::JSONSchema:: register_format ( 'pve-volume-id-or-qm-path' , \ & verify_volume_id_or_qm_path ) ;
sub verify_volume_id_or_qm_path {
2016-03-30 13:20:09 +03:00
my ( $ volid , $ noerr ) = @ _ ;
2016-04-21 12:40:24 +03:00
if ( $ volid eq 'none' || $ volid eq 'cdrom' || $ volid =~ m | ^ / | ) {
return $ volid ;
}
# if its neither 'none' nor 'cdrom' nor a path, check if its a volume-id
2016-03-30 13:20:09 +03:00
$ volid = eval { PVE::JSONSchema:: check_format ( 'pve-volume-id' , $ volid , '' ) } ;
if ( $@ ) {
return undef if $ noerr ;
die $@ ;
}
return $ volid ;
}
2011-08-23 09:47:04 +04:00
my $ drivename_hash ;
2011-09-12 14:26:00 +04:00
2016-01-14 15:33:56 +03:00
my % drivedesc_base = (
volume = > { alias = > 'file' } ,
file = > {
2016-03-30 13:20:06 +03:00
type = > 'string' ,
2016-04-21 12:40:24 +03:00
format = > 'pve-volume-id-or-qm-path' ,
2016-01-14 15:33:56 +03:00
default_key = > 1 ,
format_description = > 'volume' ,
description = > "The drive's backing volume." ,
} ,
media = > {
type = > 'string' ,
enum = > [ qw( cdrom disk ) ] ,
description = > "The drive's media type." ,
default = > 'disk' ,
optional = > 1
} ,
cyls = > {
type = > 'integer' ,
description = > "Force the drive's physical geometry to have a specific cylinder count." ,
optional = > 1
} ,
heads = > {
type = > 'integer' ,
description = > "Force the drive's physical geometry to have a specific head count." ,
optional = > 1
} ,
secs = > {
type = > 'integer' ,
description = > "Force the drive's physical geometry to have a specific sector count." ,
optional = > 1
} ,
trans = > {
type = > 'string' ,
enum = > [ qw( none lba auto ) ] ,
description = > "Force disk geometry bios translation mode." ,
optional = > 1 ,
} ,
snapshot = > {
type = > 'boolean' ,
description = > "Whether the drive should be included when making snapshots." ,
optional = > 1 ,
} ,
cache = > {
type = > 'string' ,
enum = > [ qw( none writethrough writeback unsafe directsync ) ] ,
description = > "The drive's cache mode" ,
optional = > 1 ,
} ,
format = > {
type = > 'string' ,
2016-05-11 11:11:49 +03:00
format_description = > 'image format' ,
2016-01-14 15:33:56 +03:00
enum = > [ qw( raw cow qcow qed qcow2 vmdk cloop ) ] ,
description = > "The drive's backing file's data format." ,
optional = > 1 ,
} ,
size = > {
2016-01-29 10:58:59 +03:00
type = > 'string' ,
format = > 'disk-size' ,
2016-05-11 11:11:49 +03:00
format_description = > 'DiskSize' ,
2016-01-14 15:33:56 +03:00
description = > "Disk size. This is purely informational and has no effect." ,
optional = > 1 ,
} ,
backup = > {
type = > 'boolean' ,
description = > "Whether the drive should be included when making backups." ,
optional = > 1 ,
} ,
werror = > {
type = > 'string' ,
enum = > [ qw( enospc ignore report stop ) ] ,
description = > 'Write error action.' ,
optional = > 1 ,
} ,
aio = > {
type = > 'string' ,
enum = > [ qw( native threads ) ] ,
description = > 'AIO type to use.' ,
optional = > 1 ,
} ,
discard = > {
type = > 'string' ,
enum = > [ qw( ignore on ) ] ,
description = > 'Controls whether to pass discard/trim requests to the underlying storage.' ,
optional = > 1 ,
} ,
detect_zeroes = > {
type = > 'boolean' ,
description = > 'Controls whether to detect and try to optimize writes of zeroes.' ,
optional = > 1 ,
} ,
serial = > {
type = > 'string' ,
2016-03-18 13:42:33 +03:00
format = > 'urlencoded' ,
2016-01-14 15:33:56 +03:00
format_description = > 'serial' ,
2016-03-21 12:58:44 +03:00
maxLength = > 20 * 3 , # *3 since it's %xx url enoded
description = > "The drive's reported serial number, url-encoded, up to 20 bytes long." ,
2016-01-14 15:33:56 +03:00
optional = > 1 ,
}
) ;
my % rerror_fmt = (
rerror = > {
type = > 'string' ,
enum = > [ qw( ignore report stop ) ] ,
description = > 'Read error action.' ,
optional = > 1 ,
} ,
) ;
my % iothread_fmt = ( iothread = > {
type = > 'boolean' ,
description = > "Whether to use iothreads for this drive" ,
optional = > 1 ,
} ) ;
my % model_fmt = (
model = > {
type = > 'string' ,
2016-03-18 13:42:33 +03:00
format = > 'urlencoded' ,
2016-01-14 15:33:56 +03:00
format_description = > 'model' ,
2016-03-21 12:58:44 +03:00
maxLength = > 40 * 3 , # *3 since it's %xx url enoded
description = > "The drive's reported model name, url-encoded, up to 40 bytes long." ,
2016-01-14 15:33:56 +03:00
optional = > 1 ,
} ,
) ;
my % queues_fmt = (
queues = > {
type = > 'integer' ,
description = > "Number of queues." ,
minimum = > 2 ,
optional = > 1
}
) ;
my $ add_throttle_desc = sub {
2016-05-11 11:11:49 +03:00
my ( $ key , $ type , $ what , $ unit , $ longunit ) = @ _ ;
2016-01-14 15:33:56 +03:00
$ drivedesc_base { $ key } = {
type = > $ type ,
2016-05-11 11:11:49 +03:00
format_description = > $ unit ,
description = > "Maximum $what speed in $longunit per second." ,
2016-01-14 15:33:56 +03:00
optional = > 1 ,
} ;
} ;
# throughput: (leaky bucket)
$ add_throttle_desc - > ( 'bps' , 'integer' , 'r/w speed' , 'bps' , 'bytes' ) ;
$ add_throttle_desc - > ( 'bps_rd' , 'integer' , 'read speed' , 'bps' , 'bytes' ) ;
$ add_throttle_desc - > ( 'bps_wr' , 'integer' , 'write speed' , 'bps' , 'bytes' ) ;
2016-03-30 13:20:06 +03:00
$ add_throttle_desc - > ( 'mbps' , 'number' , 'r/w speed' , 'mbps' , 'megabytes' ) ;
$ add_throttle_desc - > ( 'mbps_rd' , 'number' , 'read speed' , 'mbps' , 'megabytes' ) ;
$ add_throttle_desc - > ( 'mbps_wr' , 'number' , 'write speed' , 'mbps' , 'megabytes' ) ;
2016-01-14 15:33:56 +03:00
$ add_throttle_desc - > ( 'iops' , 'integer' , 'r/w I/O' , 'iops' , 'operations' ) ;
$ add_throttle_desc - > ( 'iops_rd' , 'integer' , 'read I/O' , 'iops' , 'operations' ) ;
$ add_throttle_desc - > ( 'iops_wr' , 'integer' , 'write I/O' , 'iops' , 'operations' ) ;
# pools: (pool of IO before throttling starts taking effect)
2016-03-30 13:20:06 +03:00
$ add_throttle_desc - > ( 'mbps_max' , 'number' , 'unthrottled r/w pool' , 'mbps' , 'megabytes' ) ;
$ add_throttle_desc - > ( 'mbps_rd_max' , 'number' , 'unthrottled read pool' , 'mbps' , 'megabytes' ) ;
$ add_throttle_desc - > ( 'mbps_wr_max' , 'number' , 'unthrottled write pool' , 'mbps' , 'megabytes' ) ;
2016-01-14 15:33:56 +03:00
$ add_throttle_desc - > ( 'iops_max' , 'integer' , 'unthrottled r/w I/O pool' , 'iops' , 'operations' ) ;
$ add_throttle_desc - > ( 'iops_rd_max' , 'integer' , 'unthrottled read I/O pool' , 'iops' , 'operations' ) ;
$ add_throttle_desc - > ( 'iops_wr_max' , 'integer' , 'unthrottled write I/O pool' , 'iops' , 'operations' ) ;
my $ ide_fmt = {
% drivedesc_base ,
% rerror_fmt ,
% model_fmt ,
} ;
2011-08-23 09:47:04 +04:00
my $ idedesc = {
optional = > 1 ,
2016-01-14 15:33:56 +03:00
type = > 'string' , format = > $ ide_fmt ,
2012-08-20 14:13:15 +04:00
description = > "Use volume as IDE hard disk or CD-ROM (n is 0 to " . ( $ MAX_IDE_DISKS - 1 ) . ")." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-ide" , $ idedesc ) ;
2016-01-14 15:33:56 +03:00
my $ scsi_fmt = {
% drivedesc_base ,
% iothread_fmt ,
% queues_fmt ,
} ;
2011-08-23 09:47:04 +04:00
my $ scsidesc = {
optional = > 1 ,
2016-01-14 15:33:56 +03:00
type = > 'string' , format = > $ scsi_fmt ,
2012-08-20 14:13:15 +04:00
description = > "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ( $ MAX_SCSI_DISKS - 1 ) . ")." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-scsi" , $ scsidesc ) ;
2016-01-14 15:33:56 +03:00
my $ sata_fmt = {
% drivedesc_base ,
% rerror_fmt ,
} ;
2012-02-01 16:25:19 +04:00
my $ satadesc = {
optional = > 1 ,
2016-01-14 15:33:56 +03:00
type = > 'string' , format = > $ sata_fmt ,
2012-08-20 14:13:15 +04:00
description = > "Use volume as SATA hard disk or CD-ROM (n is 0 to " . ( $ MAX_SATA_DISKS - 1 ) . ")." ,
2012-02-01 16:25:19 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-sata" , $ satadesc ) ;
2016-01-14 15:33:56 +03:00
my $ virtio_fmt = {
% drivedesc_base ,
% iothread_fmt ,
% rerror_fmt ,
} ;
2011-08-23 09:47:04 +04:00
my $ virtiodesc = {
optional = > 1 ,
2016-01-14 15:33:56 +03:00
type = > 'string' , format = > $ virtio_fmt ,
2012-08-20 14:13:15 +04:00
description = > "Use volume as VIRTIO hard disk (n is 0 to " . ( $ MAX_VIRTIO_DISKS - 1 ) . ")." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-virtio" , $ virtiodesc ) ;
2016-01-14 15:33:56 +03:00
my $ alldrive_fmt = {
% drivedesc_base ,
% rerror_fmt ,
% iothread_fmt ,
% model_fmt ,
% queues_fmt ,
} ;
2016-03-30 13:20:07 +03:00
my $ usb_fmt = {
2016-02-11 17:49:09 +03:00
host = > {
default_key = > 1 ,
type = > 'string' , format = > 'pve-qm-usb-device' ,
format_description = > 'HOSTUSBDEVICE|spice' ,
2016-05-19 14:13:25 +03:00
description = > << EODESCR ,
The Host USB device or port or the value 'spice' . HOSTUSBDEVICE syntax is:
'bus-port(.port)*' ( decimal numbers ) or
'vendor_id:product_id' ( hexadeciaml numbers ) or
'spice'
You can use the 'lsusb -t' command to list existing usb devices .
NOTE: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
The value 'spice' can be used to add a usb redirection devices for spice .
EODESCR
2016-02-11 17:49:09 +03:00
} ,
usb3 = > {
optional = > 1 ,
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > "Specifies whether if given host option is a USB3 device or port (this does currently not work reliably with spice redirection and is then ignored)." ,
default = > 0 ,
2016-02-11 17:49:09 +03:00
} ,
} ;
2011-08-23 09:47:04 +04:00
my $ usbdesc = {
optional = > 1 ,
2016-03-30 13:20:07 +03:00
type = > 'string' , format = > $ usb_fmt ,
2016-05-19 14:13:25 +03:00
description = > "Configure an USB device (n is 0 to 4)." ,
2011-08-23 09:47:04 +04:00
} ;
PVE::JSONSchema:: register_standard_option ( "pve-qm-usb" , $ usbdesc ) ;
2016-03-30 13:20:12 +03:00
# NOTE: the match-groups of this regex are used in parse_hostpci
my $ PCIRE = qr/([a-f0-9]{2}:[a-f0-9]{2})(?:\.([a-f0-9]))?/ ;
my $ hostpci_fmt = {
host = > {
default_key = > 1 ,
type = > 'string' ,
pattern = > qr/$PCIRE(;$PCIRE)*/ ,
format_description = > 'HOSTPCIID[;HOSTPCIID2...]' ,
2016-05-19 14:13:25 +03:00
description = > << EODESCR ,
Host PCI device pass through . The PCI ID of a host ' s PCI device or a list
of PCI virtual functions of the host . HOSTPCIID syntax is:
'bus:dev.func' ( hexadecimal numbers )
You can us the 'lspci' command to list existing PCI devices .
EODESCR
2016-03-30 13:20:12 +03:00
} ,
rombar = > {
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > "Specify whether or not the device's ROM will be visible in the guest's memory map." ,
2016-03-30 13:20:12 +03:00
optional = > 1 ,
default = > 1 ,
} ,
pcie = > {
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > "Choose the PCI-express bus (needs the 'q35' machine model)." ,
2016-03-30 13:20:12 +03:00
optional = > 1 ,
default = > 0 ,
} ,
'x-vga' = > {
type = > 'boolean' ,
2016-05-19 14:13:25 +03:00
description = > "Enable vfio-vga device support." ,
2016-03-30 13:20:12 +03:00
optional = > 1 ,
default = > 0 ,
} ,
} ;
PVE::JSONSchema:: register_format ( 'pve-qm-hostpci' , $ hostpci_fmt ) ;
2011-09-11 10:59:59 +04:00
my $ hostpcidesc = {
optional = > 1 ,
type = > 'string' , format = > 'pve-qm-hostpci' ,
2016-05-19 14:13:25 +03:00
description = > "Map host PCI devices into guest." ,
2016-05-20 12:59:30 +03:00
verbose_description = > << EODESCR ,
Map host PCI devices into guest .
NOTE: This option allows direct access to host hardware . So it is no longer
possible to migrate such machines - use with special care .
CAUTION: Experimental ! User reported problems with this option .
EODESCR
2011-09-11 10:59:59 +04:00
} ;
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' ,
2014-12-01 11:47:36 +03:00
pattern = > '(/dev/.+|socket)' ,
2016-05-19 14:13:25 +03:00
description = > "Create a serial device inside the VM (n is 0 to 3)" ,
verbose_description = > << EODESCR ,
Create a serial device inside the VM ( n is 0 to 3 ) , and pass through a
host serial device ( i . e . /dev/ ttyS0 ) , or create a unix socket on the
host side ( use 'qm terminal' to open a terminal connection ) .
2011-09-11 11:00:00 +04:00
2016-03-23 12:22:17 +03:00
NOTE: If you pass through a host serial device , it is no longer possible to migrate such machines - use with special care .
2011-09-11 11:00:00 +04:00
2016-05-19 14:13:25 +03:00
CAUTION: Experimental ! User reported problems with this option .
2011-09-11 11:00:00 +04:00
EODESCR
} ;
2011-09-11 11:00:01 +04:00
my $ paralleldesc = {
optional = > 1 ,
2011-09-12 09:44:02 +04:00
type = > 'string' ,
2013-08-14 14:18:54 +04:00
pattern = > '/dev/parport\d+|/dev/usb/lp\d+' ,
2016-05-19 14:13:25 +03:00
description = > "Map host parallel devices (n is 0 to 2)." ,
verbose_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
2016-03-23 12:22:17 +03:00
NOTE: This option allows direct access to host hardware . So it is no longer possible to migrate such machines - use with special care .
2011-09-11 11:00:01 +04:00
2016-05-19 14:13:25 +03:00
CAUTION: Experimental ! User reported problems with this option .
2011-09-11 11:00:01 +04:00
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' ,
2016-05-19 14:13:25 +03:00
description = > "Reference to unused volumes. This is used internally, and should not be modified manually." ,
2011-08-23 09:47:04 +04:00
} ;
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' ;
2016-02-25 16:47:17 +03:00
my $ code = sub {
my $ line = shift ;
if ( $ line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/ ) {
$ kvm_user_version = $ 2 ;
}
} ;
2011-09-12 14:26:00 +04:00
2016-02-25 16:47:17 +03:00
eval { run_command ( "kvm -version" , outfunc = > $ code ) ; } ;
warn $@ if $@ ;
2011-08-23 09:47:04 +04:00
return $ kvm_user_version ;
}
my $ kernel_has_vhost_net = - c '/dev/vhost-net' ;
2016-03-03 17:45:15 +03:00
sub valid_drive_names {
2011-08-23 09:47:04 +04:00
# 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
}
2016-03-03 17:45:15 +03:00
sub is_valid_drivename {
2011-08-23 09:47:04 +04:00
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' ,
2012-10-23 11:36:18 +04:00
win8 = > 'Windows 8/2012' ,
2011-08-23 09:47:04 +04:00
l24 = > 'Linux 2.4' ,
l26 = > 'Linux 2.6' ,
2011-09-12 14:26:00 +04:00
} ;
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' ) {
2012-05-23 09:24:15 +04:00
$ etype = 'images' ;
2011-08-23 09:47:04 +04:00
} 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)$/ ;
}
2015-01-27 09:16:22 +03:00
sub parse_hotplug_features {
my ( $ data ) = @ _ ;
my $ res = { } ;
return $ res if $ data eq '0' ;
2015-03-27 08:16:24 +03:00
2015-01-27 09:16:22 +03:00
$ data = $ confdesc - > { hotplug } - > { default } if $ data eq '1' ;
2015-01-27 13:05:36 +03:00
foreach my $ feature ( PVE::Tools:: split_list ( $ data ) ) {
2015-01-27 09:16:22 +03:00
if ( $ feature =~ m/^(network|disk|cpu|memory|usb)$/ ) {
$ res - > { $ 1 } = 1 ;
} else {
2016-05-30 11:44:37 +03:00
die "invalid hotplug feature '$feature'\n" ;
2015-01-27 09:16:22 +03:00
}
}
return $ res ;
}
PVE::JSONSchema:: register_format ( 'pve-hotplug-features' , \ & pve_verify_hotplug_features ) ;
sub pve_verify_hotplug_features {
my ( $ value , $ noerr ) = @ _ ;
return $ value if parse_hotplug_features ( $ value ) ;
return undef if $ noerr ;
die "unable to parse hotplug option\n" ;
}
2011-08-23 09:47:04 +04:00
# 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]
2012-03-29 09:12:43 +04:00
# [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
2015-12-17 16:58:12 +03:00
# [,aio=native|threads][,discard=ignore|on][,detect_zeroes=on|off]
# [,iothread=on][,serial=serial][,model=model]
2011-08-23 09:47:04 +04:00
sub parse_drive {
my ( $ key , $ data ) = @ _ ;
2016-01-14 15:33:56 +03:00
my ( $ interface , $ index ) ;
2011-09-12 14:26:00 +04:00
2016-01-14 15:33:56 +03:00
if ( $ key =~ m/^([^\d]+)(\d+)$/ ) {
$ interface = $ 1 ;
$ index = $ 2 ;
2011-08-23 09:47:04 +04:00
} else {
return undef ;
}
2016-01-14 15:33:56 +03:00
my $ desc = $ key =~ /^unused\d+$/ ? $ alldrive_fmt
: $ confdesc - > { $ key } - > { format } ;
if ( ! $ desc ) {
warn "invalid drive key: $key\n" ;
return undef ;
}
my $ res = eval { PVE::JSONSchema:: parse_property_string ( $ desc , $ data ) } ;
return undef if ! $ res ;
$ res - > { interface } = $ interface ;
$ res - > { index } = $ index ;
my $ error = 0 ;
foreach my $ opt ( qw( bps bps_rd bps_wr ) ) {
if ( my $ bps = defined ( delete $ res - > { $ opt } ) ) {
if ( defined ( $ res - > { "m$opt" } ) ) {
warn "both $opt and m$opt specified\n" ;
+ + $ error ;
next ;
2011-08-23 09:47:04 +04:00
}
2016-01-14 15:33:56 +03:00
$ res - > { "m$opt" } = sprintf ( "%.3f" , $ bps / ( 1024 * 1024.0 ) ) ;
2011-08-23 09:47:04 +04:00
}
}
2016-01-14 15:33:56 +03:00
return undef if $ error ;
2013-07-15 11:13:31 +04:00
2012-08-29 15:42:41 +04:00
return undef if $ res - > { mbps_rd } && $ res - > { mbps } ;
return undef if $ res - > { mbps_wr } && $ res - > { mbps } ;
2012-05-09 16:29:29 +04:00
return undef if $ res - > { iops_rd } && $ res - > { iops } ;
return undef if $ res - > { iops_wr } && $ res - > { iops } ;
2013-12-04 18:12:36 +04:00
2011-08-23 09:47:04 +04:00
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' ;
}
2016-01-14 15:33:56 +03:00
if ( my $ size = $ res - > { size } ) {
return undef if ! defined ( $ res - > { size } = PVE::JSONSchema:: parse_size ( $ size ) ) ;
2011-08-23 09:47:04 +04:00
}
return $ res ;
}
sub print_drive {
my ( $ vmid , $ drive ) = @ _ ;
2016-01-14 15:33:56 +03:00
my $ data = { %$ drive } ;
delete $ data - > { $ _ } for qw( index interface ) ;
return PVE::JSONSchema:: print_property_string ( $ data , $ alldrive_fmt ) ;
2011-08-23 09:47:04 +04:00
}
2012-03-19 13:32:52 +04:00
sub scsi_inquiry {
my ( $ fh , $ noerr ) = @ _ ;
my $ SG_IO = 0x2285 ;
my $ SG_GET_VERSION_NUM = 0x2282 ;
my $ versionbuf = "\x00" x 8 ;
my $ ret = ioctl ( $ fh , $ SG_GET_VERSION_NUM , $ versionbuf ) ;
if ( ! $ ret ) {
die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if ! $ noerr ;
return undef ;
}
2012-05-30 14:08:33 +04:00
my $ version = unpack ( "I" , $ versionbuf ) ;
2012-03-19 13:32:52 +04:00
if ( $ version < 30000 ) {
die "scsi generic interface too old\n" if ! $ noerr ;
return undef ;
}
2012-05-30 14:08:33 +04:00
2012-03-19 13:32:52 +04:00
my $ buf = "\x00" x 36 ;
my $ sensebuf = "\x00" x 8 ;
2013-07-15 15:12:18 +04:00
my $ cmd = pack ( "C x3 C x1" , 0x12 , 36 ) ;
2012-05-30 14:08:33 +04:00
2012-03-19 13:32:52 +04:00
# see /usr/include/scsi/sg.h
my $ sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I" ;
2012-05-30 14:08:33 +04:00
my $ packet = pack ( $ sg_io_hdr_t , ord ( 'S' ) , - 3 , length ( $ cmd ) ,
length ( $ sensebuf ) , 0 , length ( $ buf ) , $ buf ,
2012-03-19 13:32:52 +04:00
$ cmd , $ sensebuf , 6000 ) ;
$ ret = ioctl ( $ fh , $ SG_IO , $ packet ) ;
if ( ! $ ret ) {
die "scsi ioctl SG_IO failed - $!\n" if ! $ noerr ;
return undef ;
}
2012-05-30 14:08:33 +04:00
2012-03-19 13:32:52 +04:00
my @ res = unpack ( $ sg_io_hdr_t , $ packet ) ;
if ( $ res [ 17 ] || $ res [ 18 ] ) {
die "scsi ioctl SG_IO status error - $!\n" if ! $ noerr ;
return undef ;
}
my $ res = { } ;
2013-07-15 15:19:54 +04:00
( my $ byte0 , my $ byte1 , $ res - > { vendor } ,
2012-03-19 13:32:52 +04:00
$ res - > { product } , $ res - > { revision } ) = unpack ( "C C x6 A8 A16 A4" , $ buf ) ;
2013-07-15 15:19:54 +04:00
$ res - > { removable } = $ byte1 & 128 ? 1 : 0 ;
$ res - > { type } = $ byte0 & 31 ;
2012-03-19 13:32:52 +04:00
return $ res ;
}
sub path_is_scsi {
my ( $ path ) = @ _ ;
my $ fh = IO::File - > new ( "+<$path" ) || return undef ;
my $ res = scsi_inquiry ( $ fh , 1 ) ;
close ( $ fh ) ;
return $ res ;
}
2014-06-18 08:54:45 +04:00
sub machine_type_is_q35 {
my ( $ conf ) = @ _ ;
2014-11-10 08:31:08 +03:00
2014-06-18 08:54:45 +04:00
return $ conf - > { machine } && ( $ conf - > { machine } =~ m/q35/ ) ? 1 : 0 ;
}
sub print_tabletdevice_full {
my ( $ conf ) = @ _ ;
2014-11-10 08:31:08 +03:00
2014-06-18 08:54:45 +04:00
my $ q35 = machine_type_is_q35 ( $ conf ) ;
# we use uhci for old VMs because tablet driver was buggy in older qemu
my $ usbbus = $ q35 ? "ehci" : "uhci" ;
2014-11-10 08:31:08 +03:00
2014-06-18 08:54:45 +04:00
return "usb-tablet,id=tablet,bus=$usbbus.0,port=1" ;
}
2011-09-07 17:34:38 +04:00
sub print_drivedevice_full {
2012-08-20 13:10:24 +04:00
my ( $ storecfg , $ conf , $ vmid , $ drive , $ bridges ) = @ _ ;
2011-09-07 17:34:38 +04:00
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' ) {
2012-08-20 13:10:24 +04:00
my $ pciaddr = print_pci_addr ( "$drive->{interface}$drive->{index}" , $ bridges ) ;
2011-12-07 14:41:27 +04:00
$ device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr" ;
2015-03-19 13:06:11 +03:00
$ device . = ",iothread=iothread-$drive->{interface}$drive->{index}" if $ drive - > { iothread } ;
2011-12-07 14:41:27 +04:00
} elsif ( $ drive - > { interface } eq 'scsi' ) {
2015-03-27 05:41:52 +03:00
2015-03-27 05:41:54 +03:00
my ( $ maxdev , $ controller , $ controller_prefix ) = scsihw_infos ( $ conf , $ drive ) ;
2011-12-07 14:41:27 +04:00
my $ unit = $ drive - > { index } % $ maxdev ;
my $ devicetype = 'hd' ;
2016-02-25 13:43:01 +03:00
my $ path = '' ;
if ( drive_is_cdrom ( $ drive ) ) {
$ devicetype = 'cd' ;
2013-07-15 15:11:28 +04:00
} else {
2016-02-25 13:43:01 +03:00
if ( $ drive - > { file } =~ m | ^ / | ) {
$ path = $ drive - > { file } ;
if ( my $ info = path_is_scsi ( $ path ) ) {
if ( $ info - > { type } == 0 ) {
$ devicetype = 'block' ;
} elsif ( $ info - > { type } == 1 ) { # tape
$ devicetype = 'generic' ;
}
}
} else {
$ path = PVE::Storage:: path ( $ storecfg , $ drive - > { file } ) ;
}
if ( $ path =~ m/^iscsi\:\/\// ) {
$ devicetype = 'generic' ;
}
}
if ( ! $ conf - > { scsihw } || ( $ conf - > { scsihw } =~ m/^lsi/ ) ) {
$ device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}" ;
} else {
$ device = "scsi-$devicetype,bus=$controller_prefix$controller.0,channel=0,scsi-id=0,lun=$drive->{index},drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}" ;
}
2012-07-30 16:58:40 +04:00
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}" ;
2015-09-30 11:23:27 +03:00
if ( $ devicetype eq 'hd' && ( my $ model = $ drive - > { model } ) ) {
2016-03-18 13:42:33 +03:00
$ model = URI::Escape:: uri_unescape ( $ model ) ;
2015-09-30 11:23:27 +03:00
$ device . = ",model=$model" ;
}
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 ;
}
2014-05-13 05:10:40 +04:00
sub get_initiator_name {
2014-05-17 11:07:18 +04:00
my $ initiator ;
2014-05-13 05:10:40 +04:00
2014-05-17 11:07:18 +04:00
my $ fh = IO::File - > new ( '/etc/iscsi/initiatorname.iscsi' ) || return undef ;
while ( defined ( my $ line = <$fh> ) ) {
next if $ line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/ ;
2014-05-13 05:10:40 +04:00
$ initiator = $ 1 ;
last ;
}
2014-05-17 11:07:18 +04:00
$ fh - > close ( ) ;
2014-05-13 05:10:40 +04:00
return $ initiator ;
}
2011-08-23 09:47:04 +04:00
sub print_drive_full {
my ( $ storecfg , $ vmid , $ drive ) = @ _ ;
2015-06-10 11:22:42 +03:00
my $ path ;
my $ volid = $ drive - > { file } ;
my $ format ;
if ( drive_is_cdrom ( $ drive ) ) {
$ path = get_iso_path ( $ storecfg , $ vmid , $ volid ) ;
} else {
my ( $ storeid , $ volname ) = PVE::Storage:: parse_volume_id ( $ volid , 1 ) ;
if ( $ storeid ) {
$ path = PVE::Storage:: path ( $ storecfg , $ volid ) ;
my $ scfg = PVE::Storage:: storage_config ( $ storecfg , $ storeid ) ;
$ format = qemu_img_format ( $ scfg , $ volname ) ;
} else {
$ path = $ volid ;
2016-01-25 17:13:36 +03:00
$ format = "raw" ;
2015-06-10 11:22:42 +03:00
}
}
2011-08-23 09:47:04 +04:00
my $ opts = '' ;
2016-03-18 13:42:33 +03:00
my @ qemu_drive_options = qw( heads secs cyls trans media format cache snapshot rerror werror aio discard iops iops_rd iops_wr iops_max iops_rd_max iops_wr_max ) ;
2011-08-23 09:47:04 +04:00
foreach my $ o ( @ qemu_drive_options ) {
$ opts . = ",$o=$drive->{$o}" if $ drive - > { $ o } ;
2011-09-12 14:26:00 +04:00
}
2016-03-18 13:42:33 +03:00
if ( my $ serial = $ drive - > { serial } ) {
$ serial = URI::Escape:: uri_unescape ( $ serial ) ;
$ opts . = ",serial=$serial" ;
}
2011-08-23 09:47:04 +04:00
2015-06-10 11:22:42 +03:00
$ opts . = ",format=$format" if $ format && ! $ drive - > { format } ;
2012-08-29 15:42:41 +04:00
foreach my $ o ( qw( bps bps_rd bps_wr ) ) {
my $ v = $ drive - > { "m$o" } ;
$ opts . = ",$o=" . int ( $ v * 1024 * 1024 ) if $ v ;
}
2015-06-02 17:04:26 +03:00
my $ cache_direct = 0 ;
if ( my $ cache = $ drive - > { cache } ) {
$ cache_direct = $ cache =~ /^(?:off|none|directsync)$/ ;
} elsif ( ! drive_is_cdrom ( $ drive ) ) {
$ opts . = ",cache=none" ;
$ cache_direct = 1 ;
}
# aio native works only with O_DIRECT
if ( ! $ drive - > { aio } ) {
if ( $ cache_direct ) {
$ opts . = ",aio=native" ;
} else {
$ opts . = ",aio=threads" ;
}
}
2013-02-22 17:35:51 +04:00
2015-12-17 16:58:12 +03:00
if ( ! drive_is_cdrom ( $ drive ) ) {
my $ detectzeroes ;
2016-01-29 12:08:08 +03:00
if ( defined ( $ drive - > { detect_zeroes } ) && ! $ drive - > { detect_zeroes } ) {
2015-12-17 16:58:12 +03:00
$ detectzeroes = 'off' ;
} elsif ( $ drive - > { discard } ) {
$ detectzeroes = $ drive - > { discard } eq 'on' ? 'unmap' : 'on' ;
} else {
# This used to be our default with discard not being specified:
$ detectzeroes = 'on' ;
}
$ opts . = ",detect-zeroes=$detectzeroes" if $ detectzeroes ;
}
2014-10-13 11:45:30 +04:00
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 {
2015-10-23 11:41:53 +03:00
my ( $ vmid , $ conf , $ net , $ netid , $ bridges , $ use_old_bios_files ) = @ _ ;
2012-01-28 14:02:28 +04:00
my $ bootorder = $ conf - > { boot } || $ confdesc - > { boot } - > { default } ;
my $ device = $ net - > { model } ;
if ( $ net - > { model } eq 'virtio' ) {
$ device = 'virtio-net-pci' ;
} ;
2012-08-20 13:10:24 +04:00
my $ pciaddr = print_pci_addr ( "$netid" , $ bridges ) ;
2015-01-09 08:56:14 +03:00
my $ tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid" ;
2014-06-10 10:30:31 +04:00
if ( $ net - > { queues } && $ net - > { queues } > 1 && $ net - > { model } eq 'virtio' ) {
#Consider we have N queues, the number of vectors needed is 2*N + 2 (plus one config interrupt and control vq)
my $ vectors = $ net - > { queues } * 2 + 2 ;
$ tmpstr . = ",vectors=$vectors,mq=on" ;
}
2012-01-28 14:02:28 +04:00
$ tmpstr . = ",bootindex=$net->{bootindex}" if $ net - > { bootindex } ;
2015-10-23 11:41:53 +03:00
if ( $ use_old_bios_files ) {
my $ romfile ;
if ( $ device eq 'virtio-net-pci' ) {
$ romfile = 'pxe-virtio.rom' ;
} elsif ( $ device eq 'e1000' ) {
$ romfile = 'pxe-e1000.rom' ;
} elsif ( $ device eq 'ne2k' ) {
$ romfile = 'pxe-ne2k_pci.rom' ;
} elsif ( $ device eq 'pcnet' ) {
$ romfile = 'pxe-pcnet.rom' ;
} elsif ( $ device eq 'rtl8139' ) {
$ romfile = 'pxe-rtl8139.rom' ;
}
$ tmpstr . = ",romfile=$romfile" if $ romfile ;
}
2012-01-28 14:02:28 +04:00
return $ tmpstr ;
}
sub print_netdev_full {
2015-11-06 17:05:59 +03:00
my ( $ vmid , $ conf , $ net , $ netid , $ hotplug ) = @ _ ;
2012-01-28 14:02:28 +04:00
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" ;
2014-06-10 10:30:31 +04:00
my $ netdev = "" ;
2015-11-06 17:05:59 +03:00
my $ script = $ hotplug ? "pve-bridge-hotplug" : "pve-bridge" ;
2014-06-10 10:30:31 +04:00
2012-01-28 14:02:28 +04:00
if ( $ net - > { bridge } ) {
2015-11-06 17:05:59 +03:00
$ netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/$script,downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam" ;
2012-01-28 14:02:28 +04:00
} else {
2014-06-10 10:30:31 +04:00
$ netdev = "type=user,id=$netid,hostname=$vmname" ;
2012-01-28 14:02:28 +04:00
}
2014-06-10 10:30:31 +04:00
$ netdev . = ",queues=$net->{queues}" if ( $ net - > { queues } && $ net - > { model } eq 'virtio' ) ;
return $ netdev ;
2012-01-28 14:02:28 +04:00
}
2011-08-23 09:47:04 +04:00
sub drive_is_cdrom {
my ( $ drive ) = @ _ ;
return $ drive && $ drive - > { media } && ( $ drive - > { media } eq 'cdrom' ) ;
}
2016-03-30 13:20:10 +03:00
sub parse_number_sets {
my ( $ set ) = @ _ ;
my $ res = [] ;
foreach my $ part ( split ( /;/ , $ set ) ) {
if ( $ part =~ /^\s*(\d+)(?:-(\d+))?\s*$/ ) {
die "invalid range: $part ($2 < $1)\n" if defined ( $ 2 ) && $ 2 < $ 1 ;
push @$ res , [ $ 1 , $ 2 ] ;
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
} else {
2016-03-30 13:20:10 +03:00
die "invalid range: $part\n" ;
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
}
}
2016-03-30 13:20:10 +03:00
return $ res ;
}
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
2016-03-30 13:20:10 +03:00
sub parse_numa {
my ( $ data ) = @ _ ;
my $ res = PVE::JSONSchema:: parse_property_string ( $ numa_fmt , $ data ) ;
$ res - > { cpus } = parse_number_sets ( $ res - > { cpus } ) if defined ( $ res - > { cpus } ) ;
$ res - > { hostnodes } = parse_number_sets ( $ res - > { hostnodes } ) if defined ( $ res - > { hostnodes } ) ;
add custom numa topology support
numaX: cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>][,policy=<preferred|bind|interleave>]]
example:
-------
sockets:4
cores:2
memory:4096
numa: 1
numa0: cpus=0-1,memory=1024,hostnodes=0-1,policy=interleave
numa1: cpus=2-3,memory=3072,hostnodes=2,policy=bind
qemu command line
-----------------
-object memory-backend-ram,size=1024M,policy=interleave,host-nodes=0-1,id=ram-node0
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0
-object memory-backend-ram,size=3072M,policy=bind,host-nodes=2,id=ram-node1
-numa node,nodeid=1,cpus=2-3,memdev=ram-node1
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-12-03 18:23:48 +03:00
return $ res ;
}
2011-09-11 10:59:59 +04:00
sub parse_hostpci {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
2016-03-30 13:20:12 +03:00
my $ res = PVE::JSONSchema:: parse_property_string ( $ hostpci_fmt , $ value ) ;
2013-12-13 14:43:05 +04:00
2016-03-30 13:20:12 +03:00
my @ idlist = split ( /;/ , $ res - > { host } ) ;
delete $ res - > { host } ;
foreach my $ id ( @ idlist ) {
if ( $ id =~ /^$PCIRE$/ ) {
push @ { $ res - > { pciid } } , { id = > $ 1 , function = > ( $ 2 // '0' ) } ;
2013-12-13 14:43:05 +04:00
} else {
2016-03-30 13:20:12 +03:00
# should have been caught by parse_property_string already
die "failed to parse PCI id: $id\n" ;
2013-12-13 14:43:05 +04:00
}
2011-09-11 10:59:59 +04:00
}
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 ) = @ _ ;
2016-03-30 13:20:11 +03:00
my $ res = eval { PVE::JSONSchema:: parse_property_string ( $ net_fmt , $ data ) } ;
if ( $@ ) {
warn $@ ;
return undef ;
2011-08-23 09:47:04 +04:00
}
2016-03-30 13:20:11 +03:00
$ res - > { macaddr } = PVE::Tools:: random_ether_addr ( ) if ! defined ( $ res - > { macaddr } ) ;
2011-08-23 09:47:04 +04:00
return $ res ;
}
sub print_net {
my $ net = shift ;
2016-03-30 13:20:11 +03:00
return PVE::JSONSchema:: print_property_string ( $ net , $ net_fmt ) ;
2011-08-23 09:47:04 +04:00
}
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 ) ;
}
}
2014-11-17 09:08:44 +03:00
sub vm_is_volid_owner {
my ( $ storecfg , $ vmid , $ volid ) = @ _ ;
if ( $ volid !~ m | ^ / | ) {
my ( $ path , $ owner ) ;
eval { ( $ path , $ owner ) = PVE::Storage:: path ( $ storecfg , $ volid ) ; } ;
if ( $ owner && ( $ owner == $ vmid ) ) {
return 1 ;
}
}
return undef ;
}
2015-08-12 14:38:36 +03:00
sub split_flagged_list {
my $ text = shift || '' ;
$ text =~ s/[,;]/ /g ;
$ text =~ s/^\s+// ;
return { map { /^(!?)(.*)$/ && ( $ 2 , $ 1 ) } ( $ text =~ /\S+/g ) } ;
}
sub join_flagged_list {
my ( $ how , $ lst ) = @ _ ;
join $ how , map { $ lst - > { $ _ } . $ _ } keys %$ lst ;
}
2014-11-17 09:08:44 +03:00
sub vmconfig_delete_pending_option {
2015-08-12 14:38:36 +03:00
my ( $ conf , $ key , $ force ) = @ _ ;
2014-11-17 09:08:44 +03:00
delete $ conf - > { pending } - > { $ key } ;
2015-08-12 14:38:36 +03:00
my $ pending_delete_hash = split_flagged_list ( $ conf - > { pending } - > { delete } ) ;
$ pending_delete_hash - > { $ key } = $ force ? '!' : '' ;
$ conf - > { pending } - > { delete } = join_flagged_list ( ',' , $ pending_delete_hash ) ;
2014-11-17 09:08:44 +03:00
}
sub vmconfig_undelete_pending_option {
my ( $ conf , $ key ) = @ _ ;
2015-08-12 14:38:36 +03:00
my $ pending_delete_hash = split_flagged_list ( $ conf - > { pending } - > { delete } ) ;
2014-11-17 09:08:44 +03:00
delete $ pending_delete_hash - > { $ key } ;
2015-08-12 14:38:36 +03:00
if ( %$ pending_delete_hash ) {
$ conf - > { pending } - > { delete } = join_flagged_list ( ',' , $ pending_delete_hash ) ;
2014-11-17 09:08:44 +03:00
} else {
delete $ conf - > { pending } - > { delete } ;
}
}
sub vmconfig_register_unused_drive {
my ( $ storecfg , $ vmid , $ conf , $ drive ) = @ _ ;
if ( ! drive_is_cdrom ( $ drive ) ) {
my $ volid = $ drive - > { file } ;
if ( vm_is_volid_owner ( $ storecfg , $ vmid , $ volid ) ) {
2016-03-07 14:41:14 +03:00
PVE::QemuConfig - > add_unused_volume ( $ conf , $ volid , $ vmid ) ;
2014-11-17 09:08:44 +03:00
}
}
}
2014-11-19 11:20:09 +03:00
sub vmconfig_cleanup_pending {
my ( $ conf ) = @ _ ;
# remove pending changes when nothing changed
my $ changes ;
foreach my $ opt ( keys % { $ conf - > { pending } } ) {
if ( defined ( $ conf - > { $ opt } ) && ( $ conf - > { pending } - > { $ opt } eq $ conf - > { $ opt } ) ) {
$ changes = 1 ;
delete $ conf - > { pending } - > { $ opt } ;
}
}
2015-08-12 14:38:36 +03:00
my $ current_delete_hash = split_flagged_list ( $ conf - > { pending } - > { delete } ) ;
2014-11-19 11:20:09 +03:00
my $ pending_delete_hash = { } ;
2015-08-12 14:38:36 +03:00
while ( my ( $ opt , $ force ) = each %$ current_delete_hash ) {
2014-11-19 11:20:09 +03:00
if ( defined ( $ conf - > { $ opt } ) ) {
2015-08-12 14:38:36 +03:00
$ pending_delete_hash - > { $ opt } = $ force ;
2014-11-19 11:20:09 +03:00
} else {
$ changes = 1 ;
}
}
2015-08-12 14:38:36 +03:00
if ( %$ pending_delete_hash ) {
$ conf - > { pending } - > { delete } = join_flagged_list ( ',' , $ pending_delete_hash ) ;
2014-11-19 11:20:09 +03:00
} else {
delete $ conf - > { pending } - > { delete } ;
}
return $ changes ;
}
2016-01-14 15:33:55 +03:00
# smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str]
2016-03-30 13:20:07 +03:00
my $ smbios1_fmt = {
2016-01-14 15:33:55 +03:00
uuid = > {
type = > 'string' ,
pattern = > '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}' ,
format_description = > 'UUID' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 UUID." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
version = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 version." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
serial = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 serial number." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
manufacturer = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 manufacturer." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
product = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 product ID." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
sku = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 SKU string." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
family = > {
type = > 'string' ,
pattern = > '\S+' ,
2016-05-11 11:11:49 +03:00
format_description = > 'string' ,
2016-05-19 14:13:25 +03:00
description = > "Set SMBIOS1 family string." ,
2016-01-14 15:33:55 +03:00
optional = > 1 ,
} ,
2014-06-26 13:12:25 +04:00
} ;
sub parse_smbios1 {
my ( $ data ) = @ _ ;
2016-03-30 13:20:07 +03:00
my $ res = eval { PVE::JSONSchema:: parse_property_string ( $ smbios1_fmt , $ data ) } ;
2016-01-14 15:33:55 +03:00
warn $@ if $@ ;
2014-06-26 13:12:25 +04:00
return $ res ;
}
2014-08-26 11:20:09 +04:00
sub print_smbios1 {
my ( $ smbios1 ) = @ _ ;
2016-03-30 13:20:07 +03:00
return PVE::JSONSchema:: print_property_string ( $ smbios1 , $ smbios1_fmt ) ;
2014-08-26 11:20:09 +04:00
}
2016-03-30 13:20:07 +03:00
PVE::JSONSchema:: register_format ( 'pve-qm-smbios1' , $ smbios1_fmt ) ;
2014-06-26 13:12:25 +04:00
2011-08-23 09:47:04 +04:00
PVE::JSONSchema:: register_format ( 'pve-qm-bootdisk' , \ & verify_bootdisk ) ;
sub verify_bootdisk {
my ( $ value , $ noerr ) = @ _ ;
2016-03-03 17:45:15 +03:00
return $ value if is_valid_drivename ( $ value ) ;
2011-08-23 09:47:04 +04:00
return undef if $ noerr ;
die "invalid boot disk '$value'\n" ;
}
2011-09-08 13:39:56 +04:00
sub parse_watchdog {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
2016-03-30 13:20:13 +03:00
my $ res = eval { PVE::JSONSchema:: parse_property_string ( $ watchdog_fmt , $ value ) } ;
warn $@ if $@ ;
2011-09-08 13:39:56 +04:00
return $ res ;
}
2011-08-23 09:47:04 +04:00
sub parse_usb_device {
my ( $ value ) = @ _ ;
return undef if ! $ value ;
my $ res = { } ;
2016-02-11 17:49:09 +03:00
if ( $ value =~ m/^(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/ ) {
$ res - > { vendorid } = $ 2 ;
$ res - > { productid } = $ 4 ;
} elsif ( $ value =~ m/^(\d+)\-(\d+(\.\d+)*)$/ ) {
$ res - > { hostbus } = $ 1 ;
$ res - > { hostport } = $ 2 ;
} elsif ( $ value =~ m/^spice$/i ) {
$ res - > { spice } = 1 ;
} else {
return undef ;
2011-08-23 09:47:04 +04:00
}
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 ) {
2012-09-12 13:59:48 +04:00
next if $ opt eq 'parent' || $ opt eq 'snaptime' || $ opt eq 'vmstate' ;
2011-08-23 09:47:04 +04:00
$ 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" ;
2012-12-30 22:03:00 +04:00
} elsif ( $ type eq 'number' ) {
return $ value if $ value =~ m/^(\d+)(\.\d+)?$/ ;
die "type check ('number') failed - got '$value'\n" ;
2011-08-23 09:47:04 +04:00
} 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"
}
}
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 touch_config {
my ( $ vmid ) = @ _ ;
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
utime undef , undef , $ conf ;
}
sub destroy_vm {
2016-02-05 10:46:35 +03:00
my ( $ storecfg , $ vmid , $ keep_empty_config , $ skiplock ) = @ _ ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conffile = PVE::QemuConfig - > config_file ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > check_lock ( $ conf ) if ! $ skiplock ;
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 } ;
2013-01-04 09:57:11 +04:00
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 $@ ;
}
sub parse_vm_config {
my ( $ filename , $ raw ) = @ _ ;
return undef if ! defined ( $ raw ) ;
2011-09-07 13:41:34 +04:00
my $ res = {
2012-03-20 15:25:08 +04:00
digest = > Digest::SHA:: sha1_hex ( $ raw ) ,
2012-09-07 13:51:19 +04:00
snapshots = > { } ,
2014-11-11 08:52:10 +03:00
pending = > { } ,
2011-09-07 13:41:34 +04:00
} ;
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 ;
2012-09-07 13:51:19 +04:00
my $ conf = $ res ;
2015-08-11 12:24:41 +03:00
my $ descr ;
2014-11-11 09:01:01 +03:00
my $ section = '' ;
2012-03-01 11:13:14 +04:00
2012-09-07 13:51:19 +04:00
my @ lines = split ( /\n/ , $ raw ) ;
foreach my $ line ( @ lines ) {
2011-08-23 09:47:04 +04:00
next if $ line =~ m/^\s*$/ ;
2013-07-15 11:13:31 +04:00
2014-10-30 15:40:22 +03:00
if ( $ line =~ m/^\[PENDING\]\s*$/i ) {
2014-11-11 09:01:01 +03:00
$ section = 'pending' ;
2015-08-11 12:24:41 +03:00
if ( defined ( $ descr ) ) {
$ descr =~ s/\s+$// ;
$ conf - > { description } = $ descr ;
}
$ descr = undef ;
2014-11-11 09:01:01 +03:00
$ conf = $ res - > { $ section } = { } ;
2014-10-30 15:40:22 +03:00
next ;
2014-11-11 08:52:10 +03:00
} elsif ( $ line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i ) {
2014-11-11 09:01:01 +03:00
$ section = $ 1 ;
2015-08-11 12:24:41 +03:00
if ( defined ( $ descr ) ) {
$ descr =~ s/\s+$// ;
$ conf - > { description } = $ descr ;
}
$ descr = undef ;
2014-11-11 09:01:01 +03:00
$ conf = $ res - > { snapshots } - > { $ section } = { } ;
2012-09-07 13:51:19 +04:00
next ;
}
2011-08-23 09:47:04 +04:00
2012-03-01 11:13:14 +04:00
if ( $ line =~ m/^\#(.*)\s*$/ ) {
2015-08-11 12:24:41 +03:00
$ descr = '' if ! defined ( $ descr ) ;
2012-03-01 11:13:14 +04:00
$ descr . = PVE::Tools:: decode_text ( $ 1 ) . "\n" ;
next ;
}
2011-08-23 09:47:04 +04:00
if ( $ line =~ m/^(description):\s*(.*\S)\s*$/ ) {
2015-08-11 12:24:41 +03:00
$ descr = '' if ! defined ( $ descr ) ;
2012-03-01 11:13:14 +04:00
$ descr . = PVE::Tools:: decode_text ( $ 2 ) ;
2012-09-07 13:51:19 +04:00
} elsif ( $ line =~ m/snapstate:\s*(prepare|delete)\s*$/ ) {
$ conf - > { snapstate } = $ 1 ;
2011-08-23 09:47:04 +04:00
} elsif ( $ line =~ m/^(args):\s*(.*\S)\s*$/ ) {
my $ key = $ 1 ;
my $ value = $ 2 ;
2012-09-07 13:51:19 +04:00
$ conf - > { $ key } = $ value ;
2014-11-11 09:40:07 +03:00
} elsif ( $ line =~ m/^delete:\s*(.*\S)\s*$/ ) {
2014-11-11 09:01:01 +03:00
my $ value = $ 1 ;
2014-11-11 09:40:07 +03:00
if ( $ section eq 'pending' ) {
$ conf - > { delete } = $ value ; # we parse this later
} else {
warn "vm $vmid - propertry 'delete' is only allowed in [PENDING]\n" ;
2014-10-30 15:40:22 +03:00
}
2011-08-23 09:47:04 +04:00
} 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' ) {
2012-09-07 13:51:19 +04:00
$ conf - > { ide2 } = $ value ;
2011-08-23 09:47:04 +04:00
} else {
2012-09-07 13:51:19 +04:00
$ conf - > { $ key } = $ value ;
2011-08-23 09:47:04 +04:00
}
}
}
}
2015-08-11 12:24:41 +03:00
if ( defined ( $ descr ) ) {
$ descr =~ s/\s+$// ;
$ conf - > { description } = $ descr ;
}
2012-09-07 13:51:19 +04:00
delete $ res - > { snapstate } ; # just to be sure
2011-08-23 09:47:04 +04:00
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-09-07 13:51:19 +04:00
delete $ conf - > { snapstate } ; # just to be sure
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
}
2012-09-10 13:49:32 +04:00
my $ used_volids = { } ;
2012-09-07 13:51:19 +04:00
2012-09-10 13:49:32 +04:00
my $ cleanup_config = sub {
2014-11-11 09:40:07 +03:00
my ( $ cref , $ pending , $ snapname ) = @ _ ;
2012-02-02 17:01:08 +04:00
2012-09-10 13:49:32 +04:00
foreach my $ key ( keys %$ cref ) {
next if $ key eq 'digest' || $ key eq 'description' || $ key eq 'snapshots' ||
2014-11-11 09:40:07 +03:00
$ key eq 'snapstate' || $ key eq 'pending' ;
2012-09-10 13:49:32 +04:00
my $ value = $ cref - > { $ key } ;
2014-11-11 09:40:07 +03:00
if ( $ key eq 'delete' ) {
die "propertry 'delete' is only allowed in [PENDING]\n"
if ! $ pending ;
# fixme: check syntax?
next ;
}
2012-09-10 13:49:32 +04:00
eval { $ value = check_type ( $ key , $ value ) ; } ;
die "unable to parse value of '$key' - $@" if $@ ;
2012-02-02 17:01:08 +04:00
2012-09-10 13:49:32 +04:00
$ cref - > { $ key } = $ value ;
2016-03-03 17:45:15 +03:00
if ( ! $ snapname && is_valid_drivename ( $ key ) ) {
2013-01-04 09:57:11 +04:00
my $ drive = parse_drive ( $ key , $ value ) ;
2012-09-10 13:49:32 +04:00
$ used_volids - > { $ drive - > { file } } = 1 if $ drive && $ drive - > { file } ;
}
2011-08-23 09:47:04 +04:00
}
2012-09-10 13:49:32 +04:00
} ;
& $ cleanup_config ( $ conf ) ;
2014-11-11 09:40:07 +03:00
& $ cleanup_config ( $ conf - > { pending } , 1 ) ;
2012-09-10 13:49:32 +04:00
foreach my $ snapname ( keys % { $ conf - > { snapshots } } ) {
2014-11-11 09:40:07 +03:00
die "internal error" if $ snapname eq 'pending' ;
& $ cleanup_config ( $ conf - > { snapshots } - > { $ snapname } , undef , $ snapname ) ;
2011-08-23 09:47:04 +04:00
}
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 } ;
2012-09-10 13:49:32 +04:00
if ( $ key =~ m/^unused/ && $ used_volids - > { $ value } ) {
2012-02-02 17:01:08 +04:00
delete $ conf - > { $ key } ;
2011-08-23 09:47:04 +04:00
}
2012-02-02 17:01:08 +04:00
}
2013-07-15 11:13:31 +04:00
2012-09-07 13:51:19 +04:00
my $ generate_raw_config = sub {
2015-08-11 12:24:41 +03:00
my ( $ conf , $ pending ) = @ _ ;
2012-03-01 11:13:14 +04:00
2012-09-07 13:51:19 +04:00
my $ raw = '' ;
# add description as comment to top of file
2015-08-11 12:24:41 +03:00
if ( defined ( my $ descr = $ conf - > { description } ) ) {
if ( $ descr ) {
foreach my $ cl ( split ( /\n/ , $ descr ) ) {
$ raw . = '#' . PVE::Tools:: encode_text ( $ cl ) . "\n" ;
}
} else {
$ raw . = "#\n" if $ pending ;
}
2012-09-07 13:51:19 +04:00
}
foreach my $ key ( sort keys %$ conf ) {
2014-11-11 09:40:07 +03:00
next if $ key eq 'digest' || $ key eq 'description' || $ key eq 'pending' || $ key eq 'snapshots' ;
2012-09-07 13:51:19 +04:00
$ raw . = "$key: $conf->{$key}\n" ;
}
return $ raw ;
} ;
2012-03-01 11:13:14 +04:00
2012-09-07 13:51:19 +04:00
my $ raw = & $ generate_raw_config ( $ conf ) ;
2014-11-11 09:40:07 +03:00
if ( scalar ( keys % { $ conf - > { pending } } ) ) {
$ raw . = "\n[PENDING]\n" ;
2015-08-11 12:24:41 +03:00
$ raw . = & $ generate_raw_config ( $ conf - > { pending } , 1 ) ;
2014-11-11 09:40:07 +03:00
}
2012-09-07 13:51:19 +04:00
foreach my $ snapname ( sort keys % { $ conf - > { snapshots } } ) {
$ raw . = "\n[$snapname]\n" ;
$ raw . = & $ generate_raw_config ( $ conf - > { snapshots } - > { $ snapname } ) ;
2012-02-02 17:01:08 +04:00
}
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
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 ) {
2013-11-26 10:37:18 +04:00
next if $ k =~ m/^usb/ && ( $ conf - > { $ k } eq 'spice' ) ;
2015-11-09 13:32:02 +03:00
# sockets are safe: they will recreated be on the target side post-migrate
next if $ k =~ m/^serial/ && ( $ conf - > { $ k } eq 'socket' ) ;
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 ;
}
2013-05-06 10:56:17 +04:00
# check if used storages are available on all nodes (use by migrate)
2012-03-30 11:13:31 +04:00
sub check_storage_availability {
my ( $ storecfg , $ conf , $ node ) = @ _ ;
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
my $ volid = $ drive - > { file } ;
return if ! $ volid ;
my ( $ sid , $ volname ) = PVE::Storage:: parse_volume_id ( $ volid , 1 ) ;
return if ! $ sid ;
# check if storage is available on both nodes
my $ scfg = PVE::Storage:: storage_check_node ( $ storecfg , $ sid ) ;
PVE::Storage:: storage_check_node ( $ storecfg , $ sid , $ node ) ;
} ) ;
}
2013-05-06 10:56:17 +04:00
# list nodes where all VM images are available (used by has_feature API)
sub shared_nodes {
my ( $ conf , $ storecfg ) = @ _ ;
my $ nodelist = PVE::Cluster:: get_nodelist ( ) ;
my $ nodehash = { map { $ _ = > 1 } @$ nodelist } ;
my $ nodename = PVE::INotify:: nodename ( ) ;
2013-07-15 11:13:31 +04:00
2013-05-06 10:56:17 +04:00
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
my $ volid = $ drive - > { file } ;
return if ! $ volid ;
my ( $ storeid , $ volname ) = PVE::Storage:: parse_volume_id ( $ volid , 1 ) ;
if ( $ storeid ) {
my $ scfg = PVE::Storage:: storage_config ( $ storecfg , $ storeid ) ;
if ( $ scfg - > { disable } ) {
$ nodehash = { } ;
} elsif ( my $ avail = $ scfg - > { nodes } ) {
foreach my $ node ( keys %$ nodehash ) {
delete $ nodehash - > { $ node } if ! $ avail - > { $ node } ;
}
} elsif ( ! $ scfg - > { shared } ) {
foreach my $ node ( keys %$ nodehash ) {
delete $ nodehash - > { $ node } if $ node ne $ nodename
}
}
}
} ) ;
return $ nodehash
}
2011-08-23 09:47:04 +04:00
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 ] ;
2013-01-16 16:19:24 +04:00
return if ! $ cmd || ( $ cmd !~ m | kvm $| && $ cmd !~ m | qemu - system - x86_64 $| ) ;
2011-08-23 09:47:04 +04:00
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 {
2012-08-21 14:21:51 +04:00
my ( $ vmid , $ nocheck , $ node ) = @ _ ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ filename = PVE::QemuConfig - > config_file ( $ vmid , $ node ) ;
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 ;
}
sub disksize {
my ( $ storecfg , $ conf ) = @ _ ;
my $ bootdisk = $ conf - > { bootdisk } ;
return undef if ! $ bootdisk ;
2016-03-03 17:45:15 +03:00
return undef if ! is_valid_drivename ( $ bootdisk ) ;
2011-08-23 09:47:04 +04:00
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 ;
2012-08-01 15:22:43 +04:00
return $ drive - > { size } ;
2011-08-23 09:47:04 +04:00
}
my $ last_proc_pid_stat ;
2012-07-13 11:25:58 +04:00
# get VM status information
# This must be fast and should not block ($full == false)
# We only query KVM using QMP if $full == true (this can be slow)
2011-08-23 09:47:04 +04:00
sub vmstatus {
2012-07-13 11:25:58 +04:00
my ( $ opt_vmid , $ full ) = @ _ ;
2011-08-23 09:47:04 +04:00
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 ) ;
2016-03-07 14:41:12 +03:00
my $ cfspath = PVE::QemuConfig - > cfs_config_path ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
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' ;
2012-08-01 15:44:54 +04:00
my $ size = disksize ( $ storecfg , $ conf ) ;
if ( defined ( $ size ) ) {
$ d - > { disk } = 0 ; # no info available
2011-08-23 09:47:04 +04:00
$ 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 ;
2015-03-08 16:07:41 +03:00
$ d - > { cpus } = $ conf - > { vcpus } if $ conf - > { vcpus } ;
2011-11-18 12:35:32 +04:00
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
2012-12-19 10:24:39 +04:00
if ( $ conf - > { balloon } ) {
2012-12-19 12:08:16 +04:00
$ d - > { balloon_min } = $ conf - > { balloon } * ( 1024 * 1024 ) ;
2012-12-28 17:05:58 +04:00
$ d - > { shares } = defined ( $ conf - > { shares } ) ? $ conf - > { shares } : 1000 ;
2012-12-19 10:24:39 +04:00
}
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 ;
2016-03-07 14:41:12 +03:00
$ d - > { template } = PVE::QemuConfig - > is_template ( $ conf ) ;
2013-02-14 14:58:56 +04:00
2011-08-23 09:47:04 +04:00
$ 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 } ;
2015-06-16 13:44:49 +03:00
if ( $ full ) {
$ d - > { nics } - > { $ dev } - > { netout } = $ netdev - > { $ dev } - > { receive } ;
$ d - > { nics } - > { $ dev } - > { netin } = $ netdev - > { $ dev } - > { transmit } ;
}
2011-08-23 09:47:04 +04:00
}
my $ ctime = gettimeofday ;
foreach my $ vmid ( keys %$ list ) {
my $ d = $ res - > { $ vmid } ;
my $ pid = $ d - > { pid } ;
next if ! $ pid ;
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 } ;
}
}
2012-08-23 09:36:48 +04:00
return $ res if ! $ full ;
2012-07-13 11:25:58 +04:00
my $ qmpclient = PVE::QMPClient - > new ( ) ;
2012-12-18 15:36:18 +04:00
my $ ballooncb = sub {
my ( $ vmid , $ resp ) = @ _ ;
my $ info = $ resp - > { 'return' } ;
2015-03-09 10:14:03 +03:00
return if ! $ info - > { max_mem } ;
2012-12-18 15:36:18 +04:00
my $ d = $ res - > { $ vmid } ;
2015-03-09 10:14:03 +03:00
# use memory assigned to VM
$ d - > { maxmem } = $ info - > { max_mem } ;
$ d - > { balloon } = $ info - > { actual } ;
if ( defined ( $ info - > { total_mem } ) && defined ( $ info - > { free_mem } ) ) {
$ d - > { mem } = $ info - > { total_mem } - $ info - > { free_mem } ;
$ d - > { freemem } = $ info - > { free_mem } ;
2012-12-18 15:36:18 +04:00
}
2015-06-16 13:44:49 +03:00
$ d - > { ballooninfo } = $ info ;
2012-12-18 15:36:18 +04:00
} ;
2012-07-13 11:25:58 +04:00
my $ blockstatscb = sub {
my ( $ vmid , $ resp ) = @ _ ;
my $ data = $ resp - > { 'return' } || [] ;
my $ totalrdbytes = 0 ;
my $ totalwrbytes = 0 ;
2015-06-16 13:44:49 +03:00
2012-07-13 11:25:58 +04:00
for my $ blockstat ( @$ data ) {
$ totalrdbytes = $ totalrdbytes + $ blockstat - > { stats } - > { rd_bytes } ;
$ totalwrbytes = $ totalwrbytes + $ blockstat - > { stats } - > { wr_bytes } ;
2015-06-16 13:44:49 +03:00
$ blockstat - > { device } =~ s/drive-// ;
$ res - > { $ vmid } - > { blockstat } - > { $ blockstat - > { device } } = $ blockstat - > { stats } ;
2012-07-13 11:25:58 +04:00
}
$ res - > { $ vmid } - > { diskread } = $ totalrdbytes ;
$ res - > { $ vmid } - > { diskwrite } = $ totalwrbytes ;
} ;
my $ statuscb = sub {
my ( $ vmid , $ resp ) = @ _ ;
2012-12-18 15:36:18 +04:00
2012-07-13 11:25:58 +04:00
$ qmpclient - > queue_cmd ( $ vmid , $ blockstatscb , 'query-blockstats' ) ;
2012-12-18 15:36:18 +04:00
# this fails if ballon driver is not loaded, so this must be
# the last commnand (following command are aborted if this fails).
2015-03-09 10:14:03 +03:00
$ qmpclient - > queue_cmd ( $ vmid , $ ballooncb , 'query-balloon' ) ;
2012-07-13 11:25:58 +04:00
my $ status = 'unknown' ;
if ( ! defined ( $ status = $ resp - > { 'return' } - > { status } ) ) {
warn "unable to get VM status\n" ;
return ;
}
$ res - > { $ vmid } - > { qmpstatus } = $ resp - > { 'return' } - > { status } ;
} ;
foreach my $ vmid ( keys %$ list ) {
next if $ opt_vmid && ( $ vmid ne $ opt_vmid ) ;
next if ! $ res - > { $ vmid } - > { pid } ; # not running
$ qmpclient - > queue_cmd ( $ vmid , $ statuscb , 'query-status' ) ;
}
2014-11-28 12:32:40 +03:00
$ qmpclient - > queue_execute ( undef , 1 ) ;
2012-07-13 11:25:58 +04:00
foreach my $ vmid ( keys %$ list ) {
next if $ opt_vmid && ( $ vmid ne $ opt_vmid ) ;
$ res - > { $ vmid } - > { qmpstatus } = $ res - > { $ vmid } - > { status } if ! $ res - > { $ vmid } - > { qmpstatus } ;
}
2011-08-23 09:47:04 +04:00
return $ res ;
}
sub foreach_drive {
2016-06-17 17:12:25 +03:00
my ( $ conf , $ func , @ param ) = @ _ ;
2011-08-23 09:47:04 +04:00
2016-03-03 17:45:15 +03:00
foreach my $ ds ( valid_drive_names ( ) ) {
next if ! defined ( $ conf - > { $ ds } ) ;
2011-08-23 09:47:04 +04:00
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 ;
2016-06-17 17:12:25 +03:00
& $ func ( $ ds , $ drive , @ param ) ;
2011-08-23 09:47:04 +04:00
}
}
2012-09-25 09:42:01 +04:00
sub foreach_volid {
2016-06-16 12:22:30 +03:00
my ( $ conf , $ func , @ param ) = @ _ ;
2013-07-15 11:13:31 +04:00
2012-09-25 09:42:01 +04:00
my $ volhash = { } ;
my $ test_volid = sub {
my ( $ volid , $ is_cdrom ) = @ _ ;
return if ! $ volid ;
2013-07-15 11:13:31 +04:00
2012-09-25 09:42:01 +04:00
$ volhash - > { $ volid } = $ is_cdrom || 0 ;
} ;
2013-01-04 09:57:11 +04:00
foreach_drive ( $ conf , sub {
2012-09-25 09:42:01 +04:00
my ( $ ds , $ drive ) = @ _ ;
& $ test_volid ( $ drive - > { file } , drive_is_cdrom ( $ drive ) ) ;
} ) ;
foreach my $ snapname ( keys % { $ conf - > { snapshots } } ) {
my $ snap = $ conf - > { snapshots } - > { $ snapname } ;
& $ test_volid ( $ snap - > { vmstate } , 0 ) ;
2013-01-04 09:57:11 +04:00
foreach_drive ( $ snap , sub {
2012-09-25 09:42:01 +04:00
my ( $ ds , $ drive ) = @ _ ;
& $ test_volid ( $ drive - > { file } , drive_is_cdrom ( $ drive ) ) ;
} ) ;
}
foreach my $ volid ( keys %$ volhash ) {
2016-06-16 12:22:30 +03:00
& $ func ( $ volid , $ volhash - > { $ volid } , @ param ) ;
2012-09-25 09:42:01 +04:00
}
}
2013-07-24 13:42:48 +04:00
sub vga_conf_has_spice {
my ( $ vga ) = @ _ ;
2013-10-02 11:11:57 +04:00
return 0 if ! $ vga || $ vga !~ m/^qxl([234])?$/ ;
return $ 1 || 1 ;
2013-07-24 13:42:48 +04:00
}
2011-08-23 09:47:04 +04:00
sub config_to_command {
2015-10-29 09:37:00 +03:00
my ( $ storecfg , $ vmid , $ conf , $ defaults , $ forcemachine ) = @ _ ;
2011-08-23 09:47:04 +04:00
my $ cmd = [] ;
2012-09-26 14:42:03 +04:00
my $ globalFlags = [] ;
my $ machineFlags = [] ;
my $ rtcFlags = [] ;
2013-07-15 10:51:35 +04:00
my $ cpuFlags = [] ;
2012-08-20 13:10:24 +04:00
my $ devices = [] ;
2011-10-03 16:53:09 +04:00
my $ pciaddr = '' ;
2012-08-20 13:10:24 +04:00
my $ bridges = { } ;
2011-08-23 09:47:04 +04:00
my $ kvmver = kvm_user_version ( ) ;
my $ vernum = 0 ; # unknown
2016-02-18 14:54:06 +03:00
my $ ostype = $ conf - > { ostype } ;
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' ;
2014-06-18 08:54:45 +04:00
my $ q35 = machine_type_is_q35 ( $ conf ) ;
memory hotplug patch v10
This patch allow to hotplug memory dimm modules
though a new option : dimm_memory
The dimm modules are generated from a map
dimmid size dimm_memory
dimm0 512 512 100.00 0
dimm1 512 1024 50.00 1
dimm2 512 1536 33.33 2
dimm3 512 2048 25.00 3
dimm4 512 2560 20.00 0
dimm5 512 3072 16.67 1
dimm6 512 3584 14.29 2
dimm7 512 4096 12.50 3
dimm8 512 4608 11.11 0
dimm9 512 5120 10.00 1
dimm10 512 5632 9.09 2
dimm11 512 6144 8.33 3
dimm12 512 6656 7.69 0
dimm13 512 7168 7.14 1
dimm14 512 7680 6.67 2
dimm15 512 8192 6.25 3
dimm16 512 8704 5.88 0
dimm17 512 9216 5.56 1
dimm18 512 9728 5.26 2
dimm19 512 10240 5.00 3
dimm20 512 10752 4.76 0
...
dimm241 65536 3260416 2.01 1
dimm242 65536 3325952 1.97 2
dimm243 65536 3391488 1.93 3
dimm244 65536 3457024 1.90 0
dimm245 65536 3522560 1.86 1
dimm246 65536 3588096 1.83 2
dimm247 65536 3653632 1.79 3
dimm248 65536 3719168 1.76 0
dimm249 65536 3784704 1.73 1
dimm250 65536 3850240 1.70 2
dimm251 65536 3915776 1.67 3
dimm252 65536 3981312 1.65 0
dimm253 65536 4046848 1.62 1
dimm254 65536 4112384 1.59 2
dimm255 65536 4177920 1.57 3
max dimm_memory size is 4TB, which is the current qemu limit
If the dimm_memory value is not aligned on memory module, we align the dimm_memory on the next module.
vmid.conf
---------
memory: 1024
numa:1
hotplug: memmory
when hotplug memory option is enabled, the minimum memory value must be 1GB, and also numa need to be enabled.
we assign the first 1GB as static memory, splitted on each numa nodes.
The remaining memory is assigned on hotpluggable dimm devices.
The static memory need to be also 128MB aligned, to have other dimm devices aligned too.
This 128MB alignment is a linux limitation, windows can align on 2MB size.
Numa need to be aligned, as linux guest don't boot on some setup with multi sockets,
and windows need numa to be able to hotplug memory
hotplug
----
qm set <vmid> -memory X (where X is bigger than current value)
unplug (not yet implemented in qemu)
------
qm set <vmid> -memory X (where X is lower than current value)
linux guest
-----------
-acpi hotplug module should be loaded in guest
-need a recent kernel. (tested with 3.10)
can be enable automaticaly, adding:
/lib/udev/rules.d/80-hotplug-cpu-mem.rules
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", \
ATTR{online}="1"
SUBSYSTEM=="memory", ACTION=="add", TEST=="state", ATTR{state}=="offline", \
ATTR{state}="online"
windows guest
-------------
tested with:
- windows 2012 standard
- windows 2008 enterprise/datacenter
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2015-01-28 08:47:24 +03:00
my $ hotplug_features = parse_hotplug_features ( defined ( $ conf - > { hotplug } ) ? $ conf - > { hotplug } : '1' ) ;
2015-03-16 07:57:41 +03:00
my $ machine_type = $ forcemachine || $ conf - > { machine } ;
2015-11-06 12:27:05 +03:00
my $ use_old_bios_files = undef ;
( $ use_old_bios_files , $ machine_type ) = qemu_use_old_bios_files ( $ machine_type ) ;
2014-06-18 08:54:45 +04:00
2015-05-28 16:59:21 +03:00
my $ cpuunits = defined ( $ conf - > { cpuunits } ) ?
$ conf - > { cpuunits } : $ defaults - > { cpuunits } ;
2011-08-23 09:47:04 +04:00
push @$ cmd , '/usr/bin/kvm' ;
push @$ cmd , '-id' , $ vmid ;
my $ use_virtio = 0 ;
2012-05-29 16:01:50 +04:00
my $ qmpsocket = qmp_socket ( $ vmid ) ;
push @$ cmd , '-chardev' , "socket,id=qmp,path=$qmpsocket,server,nowait" ;
push @$ cmd , '-mon' , "chardev=qmp,mode=control" ;
2011-08-23 09:47:04 +04:00
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' ;
2014-06-26 13:12:25 +04:00
if ( $ conf - > { smbios1 } ) {
push @$ cmd , '-smbios' , "type=1,$conf->{smbios1}" ;
}
2015-12-10 12:48:04 +03:00
if ( $ conf - > { bios } && $ conf - > { bios } eq 'ovmf' ) {
2015-11-21 10:48:59 +03:00
my $ ovmfvar = "OVMF_VARS-pure-efi.fd" ;
my $ ovmfvar_src = "/usr/share/kvm/$ovmfvar" ;
my $ ovmfvar_dst = "/tmp/$vmid-$ovmfvar" ;
2015-12-10 13:11:02 +03:00
PVE::Tools:: file_copy ( $ ovmfvar_src , $ ovmfvar_dst , 256 * 1024 ) ;
2015-11-21 10:48:59 +03:00
push @$ cmd , '-drive' , "if=pflash,format=raw,readonly,file=/usr/share/kvm/OVMF-pure-efi.fd" ;
push @$ cmd , '-drive' , "if=pflash,format=raw,file=$ovmfvar_dst" ;
}
2014-06-18 08:54:45 +04:00
if ( $ q35 ) {
2014-11-10 08:31:08 +03:00
# the q35 chipset support native usb2, so we enable usb controller
2014-06-18 08:54:45 +04:00
# by default for this machine type
2014-05-26 11:49:56 +04:00
push @$ devices , '-readconfig' , '/usr/share/qemu-server/pve-q35.cfg' ;
2014-06-18 08:54:45 +04:00
} else {
2014-05-26 11:49:56 +04:00
$ pciaddr = print_pci_addr ( "piix3" , $ bridges ) ;
push @$ devices , '-device' , "piix3-usb-uhci,id=uhci$pciaddr.0x2" ;
2013-02-22 14:39:13 +04:00
2014-05-26 11:49:56 +04:00
my $ use_usb2 = 0 ;
2014-06-18 08:54:45 +04:00
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
next if ! $ conf - > { "usb$i" } ;
2016-02-11 17:49:09 +03:00
my $ d = eval { PVE::JSONSchema:: parse_property_string ( $ usbdesc - > { format } , $ conf - > { "usb$i" } ) } ;
next if ! $ d || $ d - > { usb3 } ; # do not add usb2 controller if we have only usb3 devices
2014-06-18 08:54:45 +04:00
$ use_usb2 = 1 ;
}
# include usb device config
push @$ devices , '-readconfig' , '/usr/share/qemu-server/pve-usb.cfg' if $ use_usb2 ;
2013-02-22 13:08:10 +04:00
}
2011-09-12 14:26:00 +04:00
2016-02-10 14:52:12 +03:00
# add usb3 controller if needed
my $ use_usb3 = 0 ;
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
next if ! $ conf - > { "usb$i" } ;
2016-02-11 17:49:09 +03:00
my $ d = eval { PVE::JSONSchema:: parse_property_string ( $ usbdesc - > { format } , $ conf - > { "usb$i" } ) } ;
next if ! $ d || ! $ d - > { usb3 } ;
2016-02-10 14:52:12 +03:00
$ use_usb3 = 1 ;
}
$ pciaddr = print_pci_addr ( "xhci" , $ bridges ) ;
push @$ devices , '-device' , "nec-usb-xhci,id=xhci$pciaddr" if $ use_usb3 ;
2013-07-19 11:53:44 +04:00
my $ vga = $ conf - > { vga } ;
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
2013-10-02 11:11:57 +04:00
my $ qxlnum = vga_conf_has_spice ( $ vga ) ;
$ vga = 'qxl' if $ qxlnum ;
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
2013-07-19 11:53:44 +04:00
if ( ! $ vga ) {
2014-01-14 13:33:36 +04:00
if ( $ conf - > { ostype } && ( $ conf - > { ostype } eq 'win8' ||
$ conf - > { ostype } eq 'win7' ||
2013-07-19 11:53:44 +04:00
$ conf - > { ostype } eq 'w2k8' ) ) {
$ vga = 'std' ;
} else {
$ vga = 'cirrus' ;
}
}
2011-08-23 09:47:04 +04:00
# enable absolute mouse coordinates (needed by vnc)
2013-07-19 11:53:44 +04:00
my $ tablet ;
if ( defined ( $ conf - > { tablet } ) ) {
$ tablet = $ conf - > { tablet } ;
} else {
$ tablet = $ defaults - > { tablet } ;
2013-10-02 11:11:57 +04:00
$ tablet = 0 if $ qxlnum ; # disable for spice because it is not needed
2013-07-31 11:19:36 +04:00
$ tablet = 0 if $ vga =~ m/^serial\d+$/ ; # disable if we use serial terminal (no vga card)
2013-07-19 11:53:44 +04:00
}
2014-06-18 08:54:45 +04:00
push @$ devices , '-device' , print_tabletdevice_full ( $ conf ) if $ tablet ;
2014-11-10 08:31:08 +03:00
2016-01-26 12:16:08 +03:00
my $ kvm_off = 0 ;
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 + + ) {
2014-06-23 19:41:53 +04:00
my $ d = parse_hostpci ( $ conf - > { "hostpci$i" } ) ;
next if ! $ d ;
my $ pcie = $ d - > { pcie } ;
if ( $ pcie ) {
die "q35 machine model is not enabled" if ! $ q35 ;
$ pciaddr = print_pcie_addr ( "hostpci$i" ) ;
} else {
$ pciaddr = print_pci_addr ( "hostpci$i" , $ bridges ) ;
}
2016-03-30 13:20:12 +03:00
my $ rombar = defined ( $ d - > { rombar } ) && ! $ d - > { rombar } ? ',rombar=0' : '' ;
my $ xvga = '' ;
if ( $ d - > { 'x-vga' } ) {
$ xvga = ',x-vga=on' ;
2016-01-26 12:16:08 +03:00
$ kvm_off = 1 ;
2014-08-06 09:27:29 +04:00
$ vga = 'none' ;
2016-02-18 14:54:06 +03:00
if ( $ ostype eq 'win7' || $ ostype eq 'win8' || $ ostype eq 'w2k8' ) {
2016-02-18 10:14:42 +03:00
push @$ cpuFlags , 'hv_vendor_id=proxmox' ;
}
2016-01-09 09:27:35 +03:00
if ( $ conf - > { bios } && $ conf - > { bios } eq 'ovmf' ) {
$ xvga = "" ;
}
2014-08-06 09:27:29 +04:00
}
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
my $ pcidevices = $ d - > { pciid } ;
my $ multifunction = 1 if @$ pcidevices > 1 ;
2014-06-23 19:41:53 +04:00
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
my $ j = 0 ;
foreach my $ pcidevice ( @$ pcidevices ) {
2014-06-23 19:41:53 +04:00
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
my $ id = "hostpci$i" ;
$ id . = ".$j" if $ multifunction ;
my $ addr = $ pciaddr ;
$ addr . = ".$j" if $ multifunction ;
2015-09-20 20:23:23 +03:00
my $ devicestr = "vfio-pci,host=$pcidevice->{id}.$pcidevice->{function},id=$id$addr" ;
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
if ( $ j == 0 ) {
$ devicestr . = "$rombar$xvga" ;
$ devicestr . = ",multifunction=on" if $ multifunction ;
}
push @$ devices , '-device' , $ devicestr ;
$ j + + ;
}
2011-08-23 09:47:04 +04:00
}
# usb devices
for ( my $ i = 0 ; $ i < $ MAX_USB_DEVICES ; $ i + + ) {
2016-02-11 17:49:09 +03:00
next if ! $ conf - > { "usb$i" } ;
my $ d = eval { PVE::JSONSchema:: parse_property_string ( $ usbdesc - > { format } , $ conf - > { "usb$i" } ) } ;
2011-08-23 09:47:04 +04:00
next if ! $ d ;
2016-02-10 14:52:12 +03:00
# if it is a usb3 device, attach it to the xhci controller, else omit the bus option
my $ usbbus = '' ;
2016-02-11 17:49:09 +03:00
if ( defined ( $ d - > { usb3 } ) && $ d - > { usb3 } ) {
2016-02-10 14:52:12 +03:00
$ usbbus = ',bus=xhci.0' ;
}
2016-02-11 17:49:09 +03:00
if ( defined ( $ d - > { host } ) ) {
$ d = parse_usb_device ( $ d - > { host } ) ;
if ( defined ( $ d - > { vendorid } ) && defined ( $ d - > { productid } ) ) {
push @$ devices , '-device' , "usb-host$usbbus,vendorid=0x$d->{vendorid},productid=0x$d->{productid}" ;
} elsif ( defined ( $ d - > { hostbus } ) && defined ( $ d - > { hostport } ) ) {
push @$ devices , '-device' , "usb-host$usbbus,hostbus=$d->{hostbus},hostport=$d->{hostport}" ;
} elsif ( defined ( $ d - > { spice } ) && $ d - > { spice } ) {
# usb redir support for spice, currently no usb3
push @$ devices , '-chardev' , "spicevmc,id=usbredirchardev$i,name=usbredir" ;
push @$ devices , '-device' , "usb-redir,chardev=usbredirchardev$i,id=usbredirdev$i,bus=ehci.0" ;
}
2011-08-23 09:47:04 +04:00
}
}
# 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" } ) {
2013-07-31 08:58:26 +04:00
if ( $ path eq 'socket' ) {
my $ socket = "/var/run/qemu-server/${vmid}.serial$i" ;
push @$ devices , '-chardev' , "socket,id=serial$i,path=$socket,server,nowait" ;
push @$ devices , '-device' , "isa-serial,chardev=serial$i" ;
} else {
die "no such serial device\n" if ! - c $ path ;
push @$ devices , '-chardev' , "tty,id=serial$i,path=$path" ;
push @$ devices , '-device' , "isa-serial,chardev=serial$i" ;
}
2011-09-12 10:59:05 +04:00
}
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 ;
2013-08-14 17:55:01 +04:00
my $ devtype = $ path =~ m !^/dev/usb/lp! ? 'tty' : 'parport' ;
2013-08-14 16:22:24 +04:00
push @$ devices , '-chardev' , "$devtype,id=parallel$i,path=$path" ;
2012-08-20 13:10:24 +04:00
push @$ devices , '-device' , "isa-parallel,chardev=parallel$i" ;
2011-09-12 10:59:05 +04:00
}
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 ;
2014-01-07 16:32:50 +04:00
2015-01-09 18:30:35 +03:00
my $ maxcpus = $ sockets * $ cores ;
2014-11-17 11:52:30 +03:00
2015-01-09 18:30:35 +03:00
my $ vcpus = $ conf - > { vcpus } ? $ conf - > { vcpus } : $ maxcpus ;
2014-11-17 11:52:30 +03:00
2015-01-09 18:30:35 +03:00
my $ allowed_vcpus = $ cpuinfo - > { cpus } ;
2015-09-14 09:44:27 +03:00
die "MAX $allowed_vcpus vcpus allowed per VM on this node\n"
2015-01-09 18:30:35 +03:00
if ( $ allowed_vcpus < $ maxcpus ) ;
push @$ cmd , '-smp' , "$vcpus,sockets=$sockets,cores=$cores,maxcpus=$maxcpus" ;
2011-08-23 09:47:04 +04:00
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
2015-01-09 08:56:15 +03:00
push @$ cmd , '-boot' , "menu=on,strict=on,reboot-timeout=1000" ;
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
2013-07-31 11:19:36 +04:00
push @$ cmd , '-vga' , $ vga if $ vga && $ vga !~ m/^serial\d+$/ ; # for kvm 77 and later
2011-08-23 09:47:04 +04:00
2016-02-18 10:14:43 +03:00
if ( $ vga && $ vga !~ m/^serial\d+$/ && $ vga ne 'none' ) {
my $ socket = vnc_socket ( $ vmid ) ;
push @$ cmd , '-vnc' , "unix:$socket,x509,password" ;
} else {
push @$ cmd , '-nographic' ;
}
2011-08-23 09:47:04 +04:00
# 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
2011-09-15 11:11:27 +04:00
my $ nokvm = defined ( $ conf - > { kvm } ) && $ conf - > { kvm } == 0 ? 1 : 0 ;
2012-09-26 14:42:03 +04:00
my $ useLocaltime = $ conf - > { localtime } ;
2011-08-23 09:47:04 +04:00
2016-02-18 14:54:06 +03:00
if ( $ ostype ) {
2013-09-23 11:10:32 +04:00
# other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26, solaris
2011-08-23 09:47:04 +04:00
2016-02-18 14:54:06 +03:00
if ( $ ostype =~ m/^w/ ) { # windows
2012-09-26 14:42:03 +04:00
$ useLocaltime = 1 if ! defined ( $ conf - > { localtime } ) ;
2011-08-23 09:47:04 +04:00
2012-09-26 14:42:03 +04:00
# use time drift fix when acpi is enabled
2011-09-15 11:11:27 +04:00
if ( ! ( defined ( $ conf - > { acpi } ) && $ conf - > { acpi } == 0 ) ) {
2012-09-26 14:42:03 +04:00
$ tdf = 1 if ! defined ( $ conf - > { tdf } ) ;
2011-08-23 09:47:04 +04:00
}
}
2016-02-18 14:54:06 +03:00
if ( $ ostype eq 'win7' || $ ostype eq 'win8' || $ ostype eq 'w2k8' ||
$ ostype eq 'wvista' ) {
2012-09-26 14:42:03 +04:00
push @$ globalFlags , 'kvm-pit.lost_tick_policy=discard' ;
2012-04-05 14:34:33 +04:00
push @$ cmd , '-no-hpet' ;
2015-03-16 07:57:42 +03:00
if ( qemu_machine_feature_enabled ( $ machine_type , $ kvmver , 2 , 3 ) ) {
2016-02-18 10:14:42 +03:00
push @$ cpuFlags , 'hv_spinlocks=0x1fff' if ! $ nokvm ;
push @$ cpuFlags , 'hv_vapic' if ! $ nokvm ;
push @$ cpuFlags , 'hv_time' if ! $ nokvm ;
2015-03-16 07:57:43 +03:00
2016-05-20 11:26:08 +03:00
if ( qemu_machine_feature_enabled ( $ machine_type , $ kvmver , 2 , 6 ) ) {
push @$ cpuFlags , 'hv_reset' if ! $ nokvm ;
push @$ cpuFlags , 'hv_vpindex' if ! $ nokvm ;
push @$ cpuFlags , 'hv_runtime' if ! $ nokvm ;
}
2015-03-27 08:16:24 +03:00
} else {
2016-02-18 10:14:42 +03:00
push @$ cpuFlags , 'hv_spinlocks=0xffff' if ! $ nokvm ;
2015-03-16 07:57:42 +03:00
}
2013-07-15 10:51:37 +04:00
}
2016-02-18 14:54:06 +03:00
if ( $ ostype eq 'win7' || $ ostype eq 'win8' ) {
2016-02-18 10:14:42 +03:00
push @$ cpuFlags , 'hv_relaxed' if ! $ nokvm ;
2012-04-05 14:34:33 +04:00
}
2011-08-23 09:47:04 +04:00
}
2012-09-26 14:42:03 +04:00
push @$ rtcFlags , 'driftfix=slew' if $ tdf ;
2011-09-12 13:03:14 +04:00
if ( $ nokvm ) {
2012-09-26 14:42:03 +04:00
push @$ machineFlags , 'accel=tcg' ;
2011-09-12 13:03:14 +04:00
} else {
die "No accelerator found!\n" if ! $ cpuinfo - > { hvm } ;
}
2011-08-23 09:47:04 +04:00
2013-06-05 11:27:31 +04:00
if ( $ machine_type ) {
push @$ machineFlags , "type=${machine_type}" ;
2013-06-05 11:18:23 +04:00
}
2012-09-26 14:42:03 +04:00
if ( $ conf - > { startdate } ) {
push @$ rtcFlags , "base=$conf->{startdate}" ;
} elsif ( $ useLocaltime ) {
push @$ rtcFlags , 'base=localtime' ;
}
2011-08-23 09:47:04 +04:00
2013-07-15 10:51:35 +04:00
my $ cpu = $ nokvm ? "qemu64" : "kvm64" ;
2016-01-26 12:16:08 +03:00
if ( my $ cputype = $ conf - > { cpu } ) {
2016-03-30 13:20:07 +03:00
my $ cpuconf = PVE::JSONSchema:: parse_property_string ( $ cpu_fmt , $ cputype )
2016-01-26 12:16:08 +03:00
or die "Cannot parse cpu description: $cputype\n" ;
$ cpu = $ cpuconf - > { cputype } ;
$ kvm_off = 1 if $ cpuconf - > { hidden } ;
}
2013-07-15 10:51:35 +04:00
2013-11-28 19:34:17 +04:00
push @$ cpuFlags , '+lahf_lm' if $ cpu eq 'kvm64' ;
2015-08-28 12:10:44 +03:00
push @$ cpuFlags , '-x2apic'
if $ conf - > { ostype } && $ conf - > { ostype } eq 'solaris' ;
2013-07-15 10:51:35 +04:00
2013-07-15 10:51:36 +04:00
push @$ cpuFlags , '+sep' if $ cpu eq 'kvm64' || $ cpu eq 'kvm32' ;
2015-07-08 11:49:06 +03:00
push @$ cpuFlags , '-rdtscp' if $ cpu =~ m/^Opteron/ ;
2015-03-16 07:57:45 +03:00
if ( qemu_machine_feature_enabled ( $ machine_type , $ kvmver , 2 , 3 ) ) {
push @$ cpuFlags , '+kvm_pv_unhalt' if ! $ nokvm ;
2015-03-16 07:57:46 +03:00
push @$ cpuFlags , '+kvm_pv_eoi' if ! $ nokvm ;
2015-03-16 07:57:45 +03:00
}
2015-07-20 13:30:58 +03:00
push @$ cpuFlags , 'enforce' if $ cpu ne 'host' && ! $ nokvm ;
2015-07-08 11:49:07 +03:00
2016-01-26 12:16:08 +03:00
push @$ cpuFlags , 'kvm=off' if $ kvm_off ;
2016-05-01 10:24:25 +03:00
my $ cpu_vendor = $ cpu_vendor_list - > { $ cpu } ||
die "internal error" ; # should not happen
push @$ cpuFlags , "vendor=${cpu_vendor}"
if $ cpu_vendor ne 'default' ;
2013-07-15 11:13:31 +04:00
$ cpu . = "," . join ( ',' , @$ cpuFlags ) if scalar ( @$ cpuFlags ) ;
2013-07-15 10:51:35 +04:00
2015-07-08 11:49:07 +03:00
push @$ cmd , '-cpu' , $ cpu ;
2013-07-15 10:51:35 +04:00
2016-05-23 10:47:52 +03:00
PVE::QemuServer::Memory:: config ( $ conf , $ vmid , $ sockets , $ cores , $ defaults , $ hotplug_features , $ cmd ) ;
2011-08-23 09:47:04 +04:00
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;
2012-09-03 11:51:08 +04:00
2012-09-04 08:31:44 +04:00
if ( $ conf - > { agent } ) {
2014-11-27 13:56:52 +03:00
my $ qgasocket = qmp_socket ( $ vmid , 1 ) ;
2012-09-03 11:51:08 +04:00
my $ pciaddr = print_pci_addr ( "qga0" , $ bridges ) ;
push @$ devices , '-chardev' , "socket,path=$qgasocket,server,nowait,id=qga0" ;
push @$ devices , '-device' , "virtio-serial,id=qga0$pciaddr" ;
push @$ devices , '-device' , 'virtserialport,chardev=qga0,name=org.qemu.guest_agent.0' ;
}
2013-07-24 13:24:20 +04:00
my $ spice_port ;
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
2013-10-02 11:11:57 +04:00
if ( $ qxlnum ) {
if ( $ qxlnum > 1 ) {
if ( $ conf - > { ostype } && $ conf - > { ostype } =~ m/^w/ ) {
for ( my $ i = 1 ; $ i < $ qxlnum ; $ i + + ) {
my $ pciaddr = print_pci_addr ( "vga$i" , $ bridges ) ;
push @$ cmd , '-device' , "qxl,id=vga$i,ram_size=67108864,vram_size=33554432$pciaddr" ;
}
} else {
# assume other OS works like Linux
push @$ cmd , '-global' , 'qxl-vga.ram_size=134217728' ;
push @$ cmd , '-global' , 'qxl-vga.vram_size=67108864' ;
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
}
}
2013-06-25 09:10:42 +04:00
my $ pciaddr = print_pci_addr ( "spice" , $ bridges ) ;
2013-07-24 11:52:33 +04:00
2015-05-12 13:14:03 +03:00
my $ nodename = PVE::INotify:: nodename ( ) ;
my $ pfamily = PVE::Tools:: get_host_address_family ( $ nodename ) ;
$ spice_port = PVE::Tools:: next_spice_port ( $ pfamily ) ;
2013-07-17 13:33:02 +04:00
2015-05-26 17:08:53 +03:00
push @$ devices , '-spice' , "tls-port=${spice_port},addr=localhost,tls-ciphers=DES-CBC3-SHA,seamless-migration=on" ;
2013-06-25 09:10:42 +04:00
2014-08-13 03:04:31 +04:00
push @$ devices , '-device' , "virtio-serial,id=spice$pciaddr" ;
push @$ devices , '-chardev' , "spicevmc,id=vdagent,name=vdagent" ;
push @$ devices , '-device' , "virtserialport,chardev=vdagent,name=com.redhat.spice.0" ;
2013-06-25 09:10:42 +04:00
}
2012-12-19 10:30:34 +04:00
# enable balloon by default, unless explicitly disabled
if ( ! defined ( $ conf - > { balloon } ) || $ conf - > { balloon } ) {
$ pciaddr = print_pci_addr ( "balloon0" , $ bridges ) ;
push @$ devices , '-device' , "virtio-balloon-pci,id=balloon0$pciaddr" ;
}
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 } ) ;
2012-08-20 13:10:24 +04:00
$ pciaddr = print_pci_addr ( "watchdog" , $ bridges ) ;
2011-10-03 16:53:10 +04:00
my $ watchdog = $ wdopts - > { model } || 'i6300esb' ;
2012-08-20 13:10:24 +04:00
push @$ devices , '-device' , "$watchdog$pciaddr" ;
push @$ devices , '-watchdog-action' , $ wdopts - > { action } if $ wdopts - > { action } ;
2011-09-08 13:39:56 +04:00
}
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 = { } ;
2012-07-30 16:58:40 +04:00
my $ scsihw = defined ( $ conf - > { scsihw } ) ? $ conf - > { scsihw } : $ defaults - > { scsihw } ;
2011-08-23 09:47:04 +04:00
2014-05-17 11:14:58 +04:00
# Add iscsi initiator name if available
if ( my $ initiator = get_initiator_name ( ) ) {
push @$ devices , '-iscsi' , "initiator-name=$initiator" ;
}
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 ;
}
}
2015-03-19 13:06:11 +03:00
if ( $ drive - > { interface } eq 'virtio' ) {
push @$ cmd , '-object' , "iothread,id=iothread-$ds" if $ drive - > { iothread } ;
}
2011-09-09 12:27:21 +04:00
if ( $ drive - > { interface } eq 'scsi' ) {
2012-07-30 16:58:40 +04:00
2015-03-27 05:41:54 +03:00
my ( $ maxdev , $ controller , $ controller_prefix ) = scsihw_infos ( $ conf , $ drive ) ;
2015-03-27 05:41:52 +03:00
$ pciaddr = print_pci_addr ( "$controller_prefix$controller" , $ bridges ) ;
2015-03-27 08:16:24 +03:00
my $ scsihw_type = $ scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $ scsihw ;
2015-04-01 06:11:43 +03:00
my $ iothread = '' ;
if ( $ conf - > { scsihw } && $ conf - > { scsihw } eq "virtio-scsi-single" && $ drive - > { iothread } ) {
$ iothread . = ",iothread=iothread-$controller_prefix$controller" ;
push @$ cmd , '-object' , "iothread,id=iothread-$controller_prefix$controller" ;
2016-05-03 15:00:28 +03:00
} elsif ( $ drive - > { iothread } ) {
warn "iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n" ;
2015-04-01 06:11:43 +03:00
}
2015-04-02 07:10:54 +03:00
my $ queues = '' ;
if ( $ conf - > { scsihw } && $ conf - > { scsihw } eq "virtio-scsi-single" && $ drive - > { queues } ) {
$ queues = ",num_queues=$drive->{queues}" ;
}
push @$ devices , '-device' , "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues" if ! $ scsicontroller - > { $ controller } ;
2012-07-30 16:58:40 +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 ) ;
2012-08-20 13:10:24 +04:00
$ pciaddr = print_pci_addr ( "ahci$controller" , $ bridges ) ;
push @$ devices , '-device' , "ahci,id=ahci$controller,multifunction=on$pciaddr" if ! $ ahcicontroller - > { $ controller } ;
2012-02-01 16:25:20 +04:00
$ ahcicontroller - > { $ controller } = 1 ;
}
2014-05-17 11:07:18 +04:00
2014-05-13 05:10:40 +04:00
my $ drive_cmd = print_drive_full ( $ storecfg , $ vmid , $ drive ) ;
push @$ devices , '-drive' , $ drive_cmd ;
2014-05-17 11:07:18 +04:00
push @$ devices , '-device' , print_drivedevice_full ( $ storecfg , $ conf , $ vmid , $ drive , $ bridges ) ;
2011-08-23 09:47:04 +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" ) ;
2012-08-20 13:10:24 +04:00
push @$ devices , '-netdev' , $ netdevfull ;
2015-10-23 11:41:53 +03:00
my $ netdevicefull = print_netdevice_full ( $ vmid , $ conf , $ d , "net$i" , $ bridges , $ use_old_bios_files ) ;
2012-08-20 13:10:24 +04:00
push @$ devices , '-device' , $ netdevicefull ;
}
2011-08-23 09:47:04 +04:00
2014-06-18 08:54:45 +04:00
if ( ! $ q35 ) {
# add pci bridges
2015-03-17 11:47:10 +03:00
if ( qemu_machine_feature_enabled ( $ machine_type , $ kvmver , 2 , 3 ) ) {
$ bridges - > { 1 } = 1 ;
$ bridges - > { 2 } = 1 ;
}
2015-03-27 05:41:52 +03:00
$ bridges - > { 3 } = 1 if $ scsihw =~ m/^virtio-scsi-single/ ;
2014-05-26 11:49:56 +04:00
while ( my ( $ k , $ v ) = each %$ bridges ) {
$ pciaddr = print_pci_addr ( "pci.$k" ) ;
unshift @$ devices , '-device' , "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $ k > 0 ;
}
2011-09-12 14:26:00 +04:00
}
2011-08-23 09:47:04 +04:00
# 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 ;
}
2012-08-20 13:10:24 +04:00
push @$ cmd , @$ devices ;
2013-07-15 11:13:31 +04:00
push @$ cmd , '-rtc' , join ( ',' , @$ rtcFlags )
2012-09-26 14:42:03 +04:00
if scalar ( @$ rtcFlags ) ;
2013-07-15 11:13:31 +04:00
push @$ cmd , '-machine' , join ( ',' , @$ machineFlags )
2012-09-26 14:42:03 +04:00
if scalar ( @$ machineFlags ) ;
push @$ cmd , '-global' , join ( ',' , @$ globalFlags )
if scalar ( @$ globalFlags ) ;
2013-07-24 13:24:20 +04:00
return wantarray ? ( $ cmd , $ vollist , $ spice_port ) : $ cmd ;
2011-08-23 09:47:04 +04:00
}
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" ;
}
2013-07-17 13:33:02 +04:00
sub spice_port {
2013-06-25 09:10:42 +04:00
my ( $ vmid ) = @ _ ;
2013-07-17 13:33:02 +04:00
2013-07-24 13:24:20 +04:00
my $ res = vm_mon_cmd ( $ vmid , 'query-spice' ) ;
2013-07-17 13:33:02 +04:00
return $ res - > { 'tls-port' } || $ res - > { 'port' } || die "no spice port\n" ;
2013-06-25 09:10:42 +04:00
}
2012-05-29 16:01:50 +04:00
sub qmp_socket {
2013-03-17 19:09:01 +04:00
my ( $ vmid , $ qga ) = @ _ ;
my $ sockettype = $ qga ? 'qga' : 'qmp' ;
return "${var_run_tmpdir}/$vmid.$sockettype" ;
2012-05-29 16:01:50 +04:00
}
2011-08-23 09:47:04 +04:00
sub pidfile_name {
my ( $ vmid ) = @ _ ;
return "${var_run_tmpdir}/$vmid.pid" ;
}
2011-10-10 18:46:54 +04:00
sub vm_devices_list {
my ( $ vmid ) = @ _ ;
2012-07-13 10:42:13 +04:00
my $ res = vm_mon_cmd ( $ vmid , 'query-pci' ) ;
my $ devices = { } ;
foreach my $ pcibus ( @$ res ) {
foreach my $ device ( @ { $ pcibus - > { devices } } ) {
2015-02-12 10:30:16 +03:00
next if ! $ device - > { 'qdev_id' } ;
2015-02-10 19:23:09 +03:00
if ( $ device - > { 'pci_bridge' } ) {
$ devices - > { $ device - > { 'qdev_id' } } = 1 ;
foreach my $ bridge_device ( @ { $ device - > { 'pci_bridge' } - > { devices } } ) {
next if ! $ bridge_device - > { 'qdev_id' } ;
$ devices - > { $ bridge_device - > { 'qdev_id' } } = 1 ;
$ devices - > { $ device - > { 'qdev_id' } } + + ;
}
} else {
$ devices - > { $ device - > { 'qdev_id' } } = 1 ;
}
2014-08-29 17:04:15 +04:00
}
}
my $ resblock = vm_mon_cmd ( $ vmid , 'query-block' ) ;
foreach my $ block ( @$ resblock ) {
if ( $ block - > { device } =~ m/^drive-(\S+)/ ) {
$ devices - > { $ 1 } = 1 ;
2011-10-11 15:28:50 +04:00
}
}
2011-10-10 18:46:54 +04:00
2014-11-18 15:29:21 +03:00
my $ resmice = vm_mon_cmd ( $ vmid , 'query-mice' ) ;
foreach my $ mice ( @$ resmice ) {
if ( $ mice - > { name } eq 'QEMU HID Tablet' ) {
$ devices - > { tablet } = 1 ;
last ;
}
}
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
2014-06-18 08:54:45 +04:00
my $ q35 = machine_type_is_q35 ( $ conf ) ;
2012-02-05 17:19:06 +04:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if defined ( $ devices_list - > { $ deviceid } ) ;
2014-11-25 11:13:37 +03:00
qemu_add_pci_bridge ( $ storecfg , $ conf , $ vmid , $ deviceid ) ; # add PCI bridge if we need it for the device
2014-11-18 15:29:21 +03:00
if ( $ deviceid eq 'tablet' ) {
2014-11-25 11:13:37 +03:00
2014-11-18 15:29:21 +03:00
qemu_deviceadd ( $ vmid , print_tabletdevice_full ( $ conf ) ) ;
2014-11-25 11:13:37 +03:00
} elsif ( $ deviceid =~ m/^(virtio)(\d+)$/ ) {
2012-08-20 15:06:59 +04:00
2015-03-19 13:06:12 +03:00
qemu_iothread_add ( $ vmid , $ deviceid , $ device ) ;
2014-11-25 11:13:37 +03:00
qemu_driveadd ( $ storecfg , $ vmid , $ device ) ;
2012-07-30 16:58:40 +04:00
my $ devicefull = print_drivedevice_full ( $ storecfg , $ conf , $ vmid , $ device ) ;
2014-11-25 11:13:37 +03:00
2012-01-20 14:42:05 +04:00
qemu_deviceadd ( $ vmid , $ devicefull ) ;
2014-11-25 11:13:37 +03:00
eval { qemu_deviceaddverify ( $ vmid , $ deviceid ) ; } ;
if ( my $ err = $@ ) {
2014-11-25 10:29:12 +03:00
eval { qemu_drivedel ( $ vmid , $ deviceid ) ; } ;
warn $@ if $@ ;
2014-11-25 11:13:37 +03:00
die $ err ;
2012-01-20 14:42:05 +04:00
}
2012-01-20 14:42:06 +04:00
2015-03-27 05:41:53 +03:00
} elsif ( $ deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/ ) {
2014-11-25 11:13:37 +03:00
2015-04-01 06:11:43 +03:00
2012-07-30 16:58:40 +04:00
my $ scsihw = defined ( $ conf - > { scsihw } ) ? $ conf - > { scsihw } : "lsi" ;
2012-01-20 14:42:06 +04:00
my $ pciaddr = print_pci_addr ( $ deviceid ) ;
2015-03-27 08:16:24 +03:00
my $ scsihw_type = $ scsihw eq 'virtio-scsi-single' ? "virtio-scsi-pci" : $ scsihw ;
2015-03-27 05:41:53 +03:00
my $ devicefull = "$scsihw_type,id=$deviceid$pciaddr" ;
2014-11-25 11:13:37 +03:00
2015-04-01 06:11:43 +03:00
if ( $ deviceid =~ m/^virtioscsi(\d+)$/ && $ device - > { iothread } ) {
qemu_iothread_add ( $ vmid , $ deviceid , $ device ) ;
$ devicefull . = ",iothread=iothread-$deviceid" ;
}
2015-04-02 07:10:54 +03:00
if ( $ deviceid =~ m/^virtioscsi(\d+)$/ && $ device - > { queues } ) {
$ devicefull . = ",num_queues=$device->{queues}" ;
}
2012-01-20 14:42:06 +04:00
qemu_deviceadd ( $ vmid , $ devicefull ) ;
2014-11-25 11:13:37 +03:00
qemu_deviceaddverify ( $ vmid , $ deviceid ) ;
2012-01-20 14:42:06 +04:00
2014-11-25 11:13:37 +03:00
} elsif ( $ deviceid =~ m/^(scsi)(\d+)$/ ) {
qemu_findorcreatescsihw ( $ storecfg , $ conf , $ vmid , $ device ) ;
qemu_driveadd ( $ storecfg , $ vmid , $ device ) ;
2015-03-27 08:16:24 +03:00
2014-11-25 11:13:37 +03:00
my $ devicefull = print_drivedevice_full ( $ storecfg , $ conf , $ vmid , $ device ) ;
eval { qemu_deviceadd ( $ vmid , $ devicefull ) ; } ;
if ( my $ err = $@ ) {
2014-11-25 10:29:12 +03:00
eval { qemu_drivedel ( $ vmid , $ deviceid ) ; } ;
warn $@ if $@ ;
2014-11-25 11:13:37 +03:00
die $ err ;
2012-01-20 14:42:07 +04:00
}
2014-11-25 11:13:37 +03:00
} elsif ( $ deviceid =~ m/^(net)(\d+)$/ ) {
2012-01-28 14:02:29 +04:00
return undef if ! qemu_netdevadd ( $ vmid , $ conf , $ device , $ deviceid ) ;
2015-11-06 12:27:06 +03:00
my $ machine_type = PVE::QemuServer:: qemu_machine_pxe ( $ vmid , $ conf ) ;
my $ use_old_bios_files = undef ;
( $ use_old_bios_files , $ machine_type ) = qemu_use_old_bios_files ( $ machine_type ) ;
my $ netdevicefull = print_netdevice_full ( $ vmid , $ conf , $ device , $ deviceid , undef , $ use_old_bios_files ) ;
2012-01-28 14:02:29 +04:00
qemu_deviceadd ( $ vmid , $ netdevicefull ) ;
2014-11-25 11:13:37 +03:00
eval { qemu_deviceaddverify ( $ vmid , $ deviceid ) ; } ;
if ( my $ err = $@ ) {
eval { qemu_netdevdel ( $ vmid , $ deviceid ) ; } ;
warn $@ if $@ ;
die $ err ;
2012-01-28 14:02:29 +04:00
}
2014-11-25 11:13:37 +03:00
} elsif ( ! $ q35 && $ deviceid =~ m/^(pci\.)(\d+)$/ ) {
2014-11-10 08:31:08 +03:00
2012-08-20 15:06:59 +04:00
my $ bridgeid = $ 2 ;
my $ pciaddr = print_pci_addr ( $ deviceid ) ;
my $ devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr" ;
2015-03-27 08:16:24 +03:00
2012-08-20 15:06:59 +04:00
qemu_deviceadd ( $ vmid , $ devicefull ) ;
2014-11-25 11:13:37 +03:00
qemu_deviceaddverify ( $ vmid , $ deviceid ) ;
} else {
2015-03-27 08:16:24 +03:00
die "can't hotplug device '$deviceid'\n" ;
2012-08-20 15:06:59 +04:00
}
2012-01-20 14:42:05 +04:00
return 1 ;
2011-10-10 18:46:55 +04:00
}
2014-11-25 08:58:33 +03:00
# fixme: this should raise exceptions on error!
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-02-05 17:19:06 +04:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
return 1 if ! defined ( $ devices_list - > { $ deviceid } ) ;
2014-11-25 10:29:12 +03:00
die "can't unplug bootdisk" if $ conf - > { bootdisk } && $ conf - > { bootdisk } eq $ deviceid ;
2014-11-18 15:29:21 +03:00
if ( $ deviceid eq 'tablet' ) {
2014-11-25 10:29:12 +03:00
2014-11-18 15:29:21 +03:00
qemu_devicedel ( $ vmid , $ deviceid ) ;
2014-11-25 10:29:12 +03:00
} elsif ( $ deviceid =~ m/^(virtio)(\d+)$/ ) {
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
qemu_devicedel ( $ vmid , $ deviceid ) ;
2014-11-25 10:29:12 +03:00
qemu_devicedelverify ( $ vmid , $ deviceid ) ;
qemu_drivedel ( $ vmid , $ deviceid ) ;
2015-03-19 13:06:12 +03:00
qemu_iothread_del ( $ conf , $ vmid , $ deviceid ) ;
2015-03-27 05:41:53 +03:00
} elsif ( $ deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/ ) {
2015-03-27 08:16:24 +03:00
2014-11-25 10:29:12 +03:00
qemu_devicedel ( $ vmid , $ deviceid ) ;
2015-03-18 13:08:04 +03:00
qemu_devicedelverify ( $ vmid , $ deviceid ) ;
2015-04-01 06:11:43 +03:00
qemu_iothread_del ( $ conf , $ vmid , $ deviceid ) ;
2015-03-27 08:16:24 +03:00
2014-11-25 10:29:12 +03:00
} elsif ( $ deviceid =~ m/^(scsi)(\d+)$/ ) {
2012-01-20 14:42:06 +04:00
2015-04-01 06:11:44 +03:00
#qemu 2.3 segfault on drive_del with virtioscsi + iothread
my $ device = parse_drive ( $ deviceid , $ conf - > { $ deviceid } ) ;
die "virtioscsi with iothread is not hot-unplugglable currently" if $ device - > { iothread } ;
2014-11-25 10:29:12 +03:00
qemu_devicedel ( $ vmid , $ deviceid ) ;
qemu_drivedel ( $ vmid , $ deviceid ) ;
2015-03-27 08:16:24 +03:00
qemu_deletescsihw ( $ conf , $ vmid , $ deviceid ) ;
2015-03-18 13:08:04 +03:00
2014-11-25 10:29:12 +03:00
} elsif ( $ deviceid =~ m/^(net)(\d+)$/ ) {
2012-01-20 14:42:07 +04:00
2012-01-28 14:02:29 +04:00
qemu_devicedel ( $ vmid , $ deviceid ) ;
2014-11-25 10:29:12 +03:00
qemu_devicedelverify ( $ vmid , $ deviceid ) ;
qemu_netdevdel ( $ vmid , $ deviceid ) ;
} else {
die "can't unplug device '$deviceid'\n" ;
2012-01-28 14:02:29 +04:00
}
2012-01-20 14:42:05 +04:00
return 1 ;
}
sub qemu_deviceadd {
my ( $ vmid , $ devicefull ) = @ _ ;
2011-10-10 18:46:56 +04:00
2013-02-19 13:22:07 +04:00
$ devicefull = "driver=" . $ devicefull ;
my % options = split ( /[=,]/ , $ devicefull ) ;
2011-10-14 12:14:11 +04:00
2013-02-19 13:22:07 +04:00
vm_mon_cmd ( $ vmid , "device_add" , % options ) ;
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 {
2014-11-25 11:13:37 +03:00
my ( $ vmid , $ deviceid ) = @ _ ;
2014-11-25 10:29:12 +03:00
2013-02-19 13:22:06 +04:00
my $ ret = vm_mon_cmd ( $ vmid , "device_del" , id = > $ deviceid ) ;
2012-01-20 14:42:05 +04:00
}
2015-03-19 13:06:12 +03:00
sub qemu_iothread_add {
my ( $ vmid , $ deviceid , $ device ) = @ _ ;
if ( $ device - > { iothread } ) {
my $ iothreads = vm_iothreads_list ( $ vmid ) ;
qemu_objectadd ( $ vmid , "iothread-$deviceid" , "iothread" ) if ! $ iothreads - > { "iothread-$deviceid" } ;
}
}
sub qemu_iothread_del {
my ( $ conf , $ vmid , $ deviceid ) = @ _ ;
my $ device = parse_drive ( $ deviceid , $ conf - > { $ deviceid } ) ;
if ( $ device - > { iothread } ) {
my $ iothreads = vm_iothreads_list ( $ vmid ) ;
qemu_objectdel ( $ vmid , "iothread-$deviceid" ) if $ iothreads - > { "iothread-$deviceid" } ;
}
}
memory hotplug patch v10
This patch allow to hotplug memory dimm modules
though a new option : dimm_memory
The dimm modules are generated from a map
dimmid size dimm_memory
dimm0 512 512 100.00 0
dimm1 512 1024 50.00 1
dimm2 512 1536 33.33 2
dimm3 512 2048 25.00 3
dimm4 512 2560 20.00 0
dimm5 512 3072 16.67 1
dimm6 512 3584 14.29 2
dimm7 512 4096 12.50 3
dimm8 512 4608 11.11 0
dimm9 512 5120 10.00 1
dimm10 512 5632 9.09 2
dimm11 512 6144 8.33 3
dimm12 512 6656 7.69 0
dimm13 512 7168 7.14 1
dimm14 512 7680 6.67 2
dimm15 512 8192 6.25 3
dimm16 512 8704 5.88 0
dimm17 512 9216 5.56 1
dimm18 512 9728 5.26 2
dimm19 512 10240 5.00 3
dimm20 512 10752 4.76 0
...
dimm241 65536 3260416 2.01 1
dimm242 65536 3325952 1.97 2
dimm243 65536 3391488 1.93 3
dimm244 65536 3457024 1.90 0
dimm245 65536 3522560 1.86 1
dimm246 65536 3588096 1.83 2
dimm247 65536 3653632 1.79 3
dimm248 65536 3719168 1.76 0
dimm249 65536 3784704 1.73 1
dimm250 65536 3850240 1.70 2
dimm251 65536 3915776 1.67 3
dimm252 65536 3981312 1.65 0
dimm253 65536 4046848 1.62 1
dimm254 65536 4112384 1.59 2
dimm255 65536 4177920 1.57 3
max dimm_memory size is 4TB, which is the current qemu limit
If the dimm_memory value is not aligned on memory module, we align the dimm_memory on the next module.
vmid.conf
---------
memory: 1024
numa:1
hotplug: memmory
when hotplug memory option is enabled, the minimum memory value must be 1GB, and also numa need to be enabled.
we assign the first 1GB as static memory, splitted on each numa nodes.
The remaining memory is assigned on hotpluggable dimm devices.
The static memory need to be also 128MB aligned, to have other dimm devices aligned too.
This 128MB alignment is a linux limitation, windows can align on 2MB size.
Numa need to be aligned, as linux guest don't boot on some setup with multi sockets,
and windows need numa to be able to hotplug memory
hotplug
----
qm set <vmid> -memory X (where X is bigger than current value)
unplug (not yet implemented in qemu)
------
qm set <vmid> -memory X (where X is lower than current value)
linux guest
-----------
-acpi hotplug module should be loaded in guest
-need a recent kernel. (tested with 3.10)
can be enable automaticaly, adding:
/lib/udev/rules.d/80-hotplug-cpu-mem.rules
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", \
ATTR{online}="1"
SUBSYSTEM=="memory", ACTION=="add", TEST=="state", ATTR{state}=="offline", \
ATTR{state}="online"
windows guest
-------------
tested with:
- windows 2012 standard
- windows 2008 enterprise/datacenter
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2015-01-28 08:47:24 +03:00
sub qemu_objectadd {
my ( $ vmid , $ objectid , $ qomtype ) = @ _ ;
vm_mon_cmd ( $ vmid , "object-add" , id = > $ objectid , "qom-type" = > $ qomtype ) ;
return 1 ;
}
sub qemu_objectdel {
my ( $ vmid , $ objectid ) = @ _ ;
vm_mon_cmd ( $ vmid , "object-del" , id = > $ objectid ) ;
return 1 ;
}
2012-01-20 14:42:05 +04:00
sub qemu_driveadd {
2014-11-25 11:13:37 +03:00
my ( $ storecfg , $ vmid , $ device ) = @ _ ;
2012-01-20 14:42:05 +04:00
my $ drive = print_drive_full ( $ storecfg , $ vmid , $ device ) ;
2015-03-05 12:34:10 +03:00
$ drive =~ s/\\/\\\\/g ;
2015-02-10 14:59:22 +03:00
my $ ret = vm_human_monitor_command ( $ vmid , "drive_add auto \"$drive\"" ) ;
2014-11-25 11:13:37 +03:00
2012-01-20 14:42:05 +04:00
# If the command succeeds qemu prints: "OK"
2014-11-25 11:13:37 +03:00
return 1 if $ ret =~ m/OK/s ;
die "adding drive failed: $ret\n" ;
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_drivedel {
my ( $ vmid , $ deviceid ) = @ _ ;
2011-10-10 18:46:56 +04:00
2012-07-13 10:56:13 +04:00
my $ ret = vm_human_monitor_command ( $ vmid , "drive_del drive-$deviceid" ) ;
2012-01-20 14:42:05 +04:00
$ ret =~ s/^\s+// ;
2015-03-27 08:16:24 +03:00
2014-11-25 10:29:12 +03:00
return 1 if $ ret eq "" ;
2015-03-27 08:16:24 +03:00
2014-11-25 10:29:12 +03:00
# NB: device not found errors mean the drive was auto-deleted and we ignore the error
2015-03-27 08:16:24 +03:00
return 1 if $ ret =~ m/Device \'.*?\' not found/s ;
2014-11-25 10:29:12 +03:00
die "deleting drive $deviceid failed : $ret\n" ;
2012-01-20 14:42:05 +04:00
}
2011-10-14 12:14:11 +04:00
2012-01-20 14:42:05 +04:00
sub qemu_deviceaddverify {
2014-11-25 11:13:37 +03:00
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
}
2014-11-25 11:13:37 +03:00
die "error on hotplug device '$deviceid'\n" ;
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_devicedelverify {
2014-11-25 10:29:12 +03:00
my ( $ vmid , $ deviceid ) = @ _ ;
2015-03-27 08:16:24 +03:00
# need to verify that the device is correctly removed as device_del
2014-11-25 10:29:12 +03:00
# is async and empty return is not reliable
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
}
2014-11-25 10:29:12 +03:00
die "error on hot-unplugging device '$deviceid'\n" ;
2011-10-10 18:46:56 +04:00
}
2012-07-30 16:58:40 +04:00
sub qemu_findorcreatescsihw {
2012-01-20 14:42:06 +04:00
my ( $ storecfg , $ conf , $ vmid , $ device ) = @ _ ;
2015-03-27 05:41:54 +03:00
my ( $ maxdev , $ controller , $ controller_prefix ) = scsihw_infos ( $ conf , $ device ) ;
2015-03-27 05:41:53 +03:00
my $ scsihwid = "$controller_prefix$controller" ;
2012-01-20 14:42:06 +04:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
2012-07-30 16:58:40 +04:00
if ( ! defined ( $ devices_list - > { $ scsihwid } ) ) {
2015-04-01 06:11:43 +03:00
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ scsihwid , $ device ) ;
2012-01-20 14:42:06 +04:00
}
2014-11-25 11:13:37 +03:00
2012-01-20 14:42:06 +04:00
return 1 ;
}
2015-03-18 13:08:04 +03:00
sub qemu_deletescsihw {
my ( $ conf , $ vmid , $ opt ) = @ _ ;
my $ device = parse_drive ( $ opt , $ conf - > { $ opt } ) ;
2015-03-27 08:15:01 +03:00
if ( $ conf - > { scsihw } && ( $ conf - > { scsihw } eq 'virtio-scsi-single' ) ) {
2015-03-27 05:41:53 +03:00
vm_deviceunplug ( $ vmid , $ conf , "virtioscsi$device->{index}" ) ;
return 1 ;
}
2015-03-27 05:41:54 +03:00
my ( $ maxdev , $ controller , $ controller_prefix ) = scsihw_infos ( $ conf , $ device ) ;
2015-03-18 13:08:04 +03:00
my $ devices_list = vm_devices_list ( $ vmid ) ;
foreach my $ opt ( keys % { $ devices_list } ) {
2016-03-03 17:45:15 +03:00
if ( PVE::QemuServer:: is_valid_drivename ( $ opt ) ) {
2015-03-18 13:08:04 +03:00
my $ drive = PVE::QemuServer:: parse_drive ( $ opt , $ conf - > { $ opt } ) ;
if ( $ drive - > { interface } eq 'scsi' && $ drive - > { index } < ( ( $ maxdev - 1 ) * ( $ controller + 1 ) ) ) {
return 1 ;
}
}
}
my $ scsihwid = "scsihw$controller" ;
vm_deviceunplug ( $ vmid , $ conf , $ scsihwid ) ;
return 1 ;
}
2014-11-24 12:13:21 +03:00
sub qemu_add_pci_bridge {
2012-08-20 15:06:59 +04:00
my ( $ storecfg , $ conf , $ vmid , $ device ) = @ _ ;
my $ bridges = { } ;
2014-11-24 12:13:21 +03:00
my $ bridgeid ;
2012-08-20 15:06:59 +04:00
print_pci_addr ( $ device , $ bridges ) ;
while ( my ( $ k , $ v ) = each %$ bridges ) {
$ bridgeid = $ k ;
}
2014-11-25 11:13:37 +03:00
return 1 if ! defined ( $ bridgeid ) || $ bridgeid < 1 ;
2014-11-24 12:13:21 +03:00
2012-08-20 15:06:59 +04:00
my $ bridge = "pci.$bridgeid" ;
my $ devices_list = vm_devices_list ( $ vmid ) ;
2014-11-24 12:13:21 +03:00
if ( ! defined ( $ devices_list - > { $ bridge } ) ) {
2014-11-25 11:13:37 +03:00
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ bridge ) ;
2012-08-20 15:06:59 +04:00
}
2014-11-24 12:13:21 +03:00
2012-08-20 15:06:59 +04:00
return 1 ;
}
2015-01-20 13:47:11 +03:00
sub qemu_set_link_status {
my ( $ vmid , $ device , $ up ) = @ _ ;
2015-03-27 08:16:24 +03:00
vm_mon_cmd ( $ vmid , "set_link" , name = > $ device ,
2015-01-20 13:47:11 +03:00
up = > $ up ? JSON:: true : JSON:: false ) ;
}
2012-01-28 14:02:29 +04:00
sub qemu_netdevadd {
my ( $ vmid , $ conf , $ device , $ deviceid ) = @ _ ;
2015-11-06 17:05:59 +03:00
my $ netdev = print_netdev_full ( $ vmid , $ conf , $ device , $ deviceid , 1 ) ;
2013-02-19 13:22:09 +04:00
my % options = split ( /[=,]/ , $ netdev ) ;
2012-01-28 14:02:29 +04:00
2013-02-19 13:22:09 +04:00
vm_mon_cmd ( $ vmid , "netdev_add" , % options ) ;
return 1 ;
2012-01-28 14:02:29 +04:00
}
sub qemu_netdevdel {
my ( $ vmid , $ deviceid ) = @ _ ;
2013-02-19 13:22:08 +04:00
vm_mon_cmd ( $ vmid , "netdev_del" , id = > $ deviceid ) ;
2012-01-28 14:02:29 +04:00
}
2014-01-07 16:32:51 +04:00
sub qemu_cpu_hotplug {
2015-01-09 18:30:36 +03:00
my ( $ vmid , $ conf , $ vcpus ) = @ _ ;
2014-01-07 16:32:51 +04:00
2015-01-09 18:30:36 +03: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 ;
my $ maxcpus = $ sockets * $ cores ;
2014-01-07 16:32:51 +04:00
2015-01-09 18:30:36 +03:00
$ vcpus = $ maxcpus if ! $ vcpus ;
2014-11-19 14:59:02 +03:00
2015-01-09 18:30:36 +03:00
die "you can't add more vcpus than maxcpus\n"
if $ vcpus > $ maxcpus ;
2014-11-19 14:59:02 +03:00
2015-01-09 18:30:36 +03:00
my $ currentvcpus = $ conf - > { vcpus } || $ maxcpus ;
2014-11-19 14:59:02 +03:00
die "online cpu unplug is not yet possible\n"
2015-01-09 18:30:36 +03:00
if $ vcpus < $ currentvcpus ;
2014-01-07 16:32:51 +04:00
2015-01-09 18:30:36 +03:00
my $ currentrunningvcpus = vm_mon_cmd ( $ vmid , "query-cpus" ) ;
die "vcpus in running vm is different than configuration\n"
if scalar ( @ { $ currentrunningvcpus } ) != $ currentvcpus ;
2014-01-07 16:32:51 +04:00
2015-01-09 18:30:36 +03:00
for ( my $ i = $ currentvcpus ; $ i < $ vcpus ; $ i + + ) {
2014-01-07 16:32:51 +04:00
vm_mon_cmd ( $ vmid , "cpu-add" , id = > int ( $ i ) ) ;
}
}
2012-05-09 16:29:29 +04:00
sub qemu_block_set_io_throttle {
2016-01-11 15:27:46 +03:00
my ( $ vmid , $ deviceid ,
$ bps , $ bps_rd , $ bps_wr , $ iops , $ iops_rd , $ iops_wr ,
$ bps_max , $ bps_rd_max , $ bps_wr_max , $ iops_max , $ iops_rd_max , $ iops_wr_max ) = @ _ ;
2012-05-09 16:29:29 +04:00
2012-07-15 19:19:06 +04:00
return if ! check_running ( $ vmid ) ;
2016-01-11 15:27:46 +03:00
vm_mon_cmd ( $ vmid , "block_set_io_throttle" , device = > $ deviceid ,
bps = > int ( $ bps ) ,
bps_rd = > int ( $ bps_rd ) ,
bps_wr = > int ( $ bps_wr ) ,
iops = > int ( $ iops ) ,
iops_rd = > int ( $ iops_rd ) ,
iops_wr = > int ( $ iops_wr ) ,
bps_max = > int ( $ bps_max ) ,
bps_rd_max = > int ( $ bps_rd_max ) ,
bps_wr_max = > int ( $ bps_wr_max ) ,
iops_max = > int ( $ iops_max ) ,
iops_rd_max = > int ( $ iops_rd_max ) ,
iops_wr_max = > int ( $ iops_wr_max )
) ;
2012-07-15 19:19:06 +04:00
2012-05-09 16:29:29 +04:00
}
2012-08-23 09:36:48 +04:00
# old code, only used to shutdown old VM after update
2012-08-17 12:34:39 +04:00
sub __read_avail {
my ( $ fh , $ timeout ) = @ _ ;
my $ sel = new IO:: Select ;
$ sel - > add ( $ fh ) ;
my $ res = '' ;
my $ buf ;
my @ ready ;
while ( scalar ( @ ready = $ sel - > can_read ( $ timeout ) ) ) {
my $ count ;
if ( $ count = $ fh - > sysread ( $ buf , 8192 ) ) {
if ( $ buf =~ /^(.*)\(qemu\) $/s ) {
$ res . = $ 1 ;
last ;
} else {
$ res . = $ buf ;
}
} else {
if ( ! defined ( $ count ) ) {
die "$!\n" ;
}
last ;
}
}
die "monitor read timeout\n" if ! scalar ( @ ready ) ;
2012-08-23 09:36:48 +04:00
2012-08-17 12:34:39 +04:00
return $ res ;
}
2012-08-23 09:36:48 +04:00
# old code, only used to shutdown old VM after update
2012-08-17 12:34:39 +04:00
sub vm_monitor_command {
my ( $ vmid , $ cmdstr , $ nocheck ) = @ _ ;
2012-08-23 09:36:48 +04:00
2012-08-17 12:34:39 +04:00
my $ res ;
eval {
die "VM $vmid not running\n" if ! check_running ( $ vmid , $ nocheck ) ;
my $ sname = "${var_run_tmpdir}/$vmid.mon" ;
my $ sock = IO::Socket::UNIX - > new ( Peer = > $ sname ) ||
die "unable to connect to VM $vmid socket - $!\n" ;
my $ timeout = 3 ;
# hack: migrate sometime blocks the monitor (when migrate_downtime
# is set)
if ( $ cmdstr =~ m/^(info\s+migrate|migrate\s)/ ) {
$ timeout = 60 * 60 ; # 1 hour
}
# read banner;
my $ data = __read_avail ( $ sock , $ timeout ) ;
if ( $ data !~ m/^QEMU\s+(\S+)\s+monitor\s/ ) {
die "got unexpected qemu monitor banner\n" ;
}
my $ sel = new IO:: Select ;
$ sel - > add ( $ sock ) ;
if ( ! scalar ( my @ ready = $ sel - > can_write ( $ timeout ) ) ) {
die "monitor write error - timeout" ;
}
my $ fullcmd = "$cmdstr\r" ;
# syslog('info', "VM $vmid monitor command: $cmdstr");
my $ b ;
if ( ! ( $ b = $ sock - > syswrite ( $ fullcmd ) ) || ( $ b != length ( $ fullcmd ) ) ) {
die "monitor write error - $!" ;
}
return if ( $ cmdstr eq 'q' ) || ( $ cmdstr eq 'quit' ) ;
$ timeout = 20 ;
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
}
if ( $ res = __read_avail ( $ sock , $ timeout ) ) {
my @ lines = split ( "\r?\n" , $ res ) ;
2012-08-23 09:36:48 +04:00
2012-08-17 12:34:39 +04:00
shift @ lines if $ lines [ 0 ] !~ m/^unknown command/ ; # skip echo
2012-08-23 09:36:48 +04:00
2012-08-17 12:34:39 +04:00
$ res = join ( "\n" , @ lines ) ;
$ res . = "\n" ;
}
} ;
my $ err = $@ ;
if ( $ err ) {
syslog ( "err" , "VM $vmid monitor command failed - $err" ) ;
die $ err ;
}
2012-08-23 09:36:48 +04:00
2012-08-17 12:34:39 +04:00
return $ res ;
}
2012-08-06 13:56:34 +04:00
sub qemu_block_resize {
my ( $ vmid , $ deviceid , $ storecfg , $ volid , $ size ) = @ _ ;
2013-01-04 09:57:11 +04:00
my $ running = check_running ( $ vmid ) ;
2012-08-06 13:56:34 +04:00
return if ! PVE::Storage:: volume_resize ( $ storecfg , $ volid , $ size , $ running ) ;
return if ! $ running ;
vm_mon_cmd ( $ vmid , "block_resize" , device = > $ deviceid , size = > int ( $ size ) ) ;
}
2012-09-06 12:33:34 +04:00
sub qemu_volume_snapshot {
my ( $ vmid , $ deviceid , $ storecfg , $ volid , $ snap ) = @ _ ;
2013-01-04 09:57:11 +04:00
my $ running = check_running ( $ vmid ) ;
2012-09-06 12:33:34 +04:00
2015-05-06 10:57:34 +03:00
if ( $ running && do_snapshots_with_qemu ( $ storecfg , $ volid ) ) {
vm_mon_cmd ( $ vmid , "snapshot-drive" , device = > $ deviceid , name = > $ snap ) ;
} else {
PVE::Storage:: volume_snapshot ( $ storecfg , $ volid , $ snap ) ;
}
2012-09-06 12:33:34 +04:00
}
2012-09-06 12:33:40 +04:00
sub qemu_volume_snapshot_delete {
my ( $ vmid , $ deviceid , $ storecfg , $ volid , $ snap ) = @ _ ;
2013-01-04 09:57:11 +04:00
my $ running = check_running ( $ vmid ) ;
2012-09-06 12:33:40 +04:00
return if ! PVE::Storage:: volume_snapshot_delete ( $ storecfg , $ volid , $ snap , $ running ) ;
return if ! $ running ;
2012-09-12 13:59:48 +04:00
vm_mon_cmd ( $ vmid , "delete-drive-snapshot" , device = > $ deviceid , name = > $ snap ) ;
2012-09-06 12:33:40 +04:00
}
2014-01-14 13:33:36 +04:00
sub set_migration_caps {
my ( $ vmid ) = @ _ ;
2014-01-09 13:58:48 +04:00
2014-01-10 16:09:19 +04:00
my $ cap_ref = [] ;
2014-01-09 13:58:48 +04:00
my $ enabled_cap = {
2014-01-10 16:09:19 +04:00
"auto-converge" = > 1 ,
2015-09-25 14:50:36 +03:00
"xbzrle" = > 1 ,
2014-01-10 16:09:19 +04:00
"x-rdma-pin-all" = > 0 ,
"zero-blocks" = > 0 ,
2015-09-25 14:50:37 +03:00
"compress" = > 0
2014-01-09 13:58:48 +04:00
} ;
2014-01-10 16:09:19 +04:00
my $ supported_capabilities = vm_mon_cmd_nocheck ( $ vmid , "query-migrate-capabilities" ) ;
2014-01-09 13:58:48 +04:00
2014-01-10 16:09:19 +04:00
for my $ supported_capability ( @$ supported_capabilities ) {
2014-02-08 23:20:55 +04:00
push @$ cap_ref , {
capability = > $ supported_capability - > { capability } ,
2014-02-10 11:03:50 +04:00
state = > $ enabled_cap - > { $ supported_capability - > { capability } } ? JSON:: true : JSON:: false ,
} ;
2014-01-09 13:58:48 +04:00
}
2014-01-10 16:09:19 +04:00
vm_mon_cmd_nocheck ( $ vmid , "migrate-set-capabilities" , capabilities = > $ cap_ref ) ;
}
2014-01-09 13:58:48 +04:00
2015-01-07 12:02:32 +03:00
my $ fast_plug_option = {
2015-02-15 11:04:30 +03:00
'lock' = > 1 ,
2015-01-07 12:02:32 +03:00
'name' = > 1 ,
2015-03-27 08:16:24 +03:00
'onboot' = > 1 ,
2015-01-07 12:02:32 +03:00
'shares' = > 1 ,
'startup' = > 1 ,
2015-08-11 12:24:41 +03:00
'description' = > 1 ,
2015-01-07 12:02:32 +03:00
} ;
2014-11-19 14:59:02 +03:00
# hotplug changes in [PENDING]
# $selection hash can be used to only apply specified options, for
# example: { cores => 1 } (only apply changed 'cores')
# $errors ref is used to return error messages
2014-11-17 11:50:31 +03:00
sub vmconfig_hotplug_pending {
2014-11-19 14:59:02 +03:00
my ( $ vmid , $ conf , $ storecfg , $ selection , $ errors ) = @ _ ;
2014-11-17 11:50:31 +03:00
2014-11-19 12:43:42 +03:00
my $ defaults = load_defaults ( ) ;
2014-11-17 11:50:31 +03:00
# commit values which do not have any impact on running VM first
2014-11-19 14:59:02 +03:00
# Note: those option cannot raise errors, we we do not care about
# $selection and always apply them.
my $ add_error = sub {
my ( $ opt , $ msg ) = @ _ ;
$ errors - > { $ opt } = "hotplug problem - $msg" ;
} ;
2014-11-17 11:50:31 +03:00
my $ changes = 0 ;
foreach my $ opt ( keys % { $ conf - > { pending } } ) { # add/change
2015-01-07 12:02:32 +03:00
if ( $ fast_plug_option - > { $ opt } ) {
2014-11-17 11:50:31 +03:00
$ conf - > { $ opt } = $ conf - > { pending } - > { $ opt } ;
delete $ conf - > { pending } - > { $ opt } ;
$ changes = 1 ;
}
}
if ( $ changes ) {
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-17 11:50:31 +03:00
}
2015-01-27 09:16:22 +03:00
my $ hotplug_features = parse_hotplug_features ( defined ( $ conf - > { hotplug } ) ? $ conf - > { hotplug } : '1' ) ;
2014-11-17 11:50:31 +03:00
2015-08-12 14:38:36 +03:00
my $ pending_delete_hash = split_flagged_list ( $ conf - > { pending } - > { delete } ) ;
while ( my ( $ opt , $ force ) = each %$ pending_delete_hash ) {
2014-11-19 14:59:02 +03:00
next if $ selection && ! $ selection - > { $ opt } ;
eval {
2015-02-09 18:47:52 +03:00
if ( $ opt eq 'hotplug' ) {
die "skip\n" if ( $ conf - > { hotplug } =~ /memory/ ) ;
} elsif ( $ opt eq 'tablet' ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { usb } ;
2014-11-19 14:59:02 +03:00
if ( $ defaults - > { tablet } ) {
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ opt ) ;
} else {
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
}
2015-01-09 18:30:36 +03:00
} elsif ( $ opt eq 'vcpus' ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { cpu } ;
2015-01-09 18:30:36 +03:00
qemu_cpu_hotplug ( $ vmid , $ conf , undef ) ;
2015-01-02 17:16:01 +03:00
} elsif ( $ opt eq 'balloon' ) {
2015-01-07 12:02:32 +03:00
# enable balloon device is not hotpluggable
die "skip\n" if ! defined ( $ conf - > { balloon } ) || $ conf - > { balloon } ;
} elsif ( $ fast_plug_option - > { $ opt } ) {
# do nothing
2014-11-25 08:58:33 +03:00
} elsif ( $ opt =~ m/^net(\d+)$/ ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { network } ;
2014-11-25 08:58:33 +03:00
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
2016-03-03 17:45:15 +03:00
} elsif ( is_valid_drivename ( $ opt ) ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { disk } || $ opt =~ m/(ide|sata)(\d+)/ ;
2015-01-02 17:15:58 +03:00
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
2015-08-12 14:38:36 +03:00
vmconfig_delete_or_detach_drive ( $ vmid , $ storecfg , $ conf , $ opt , $ force ) ;
memory hotplug patch v10
This patch allow to hotplug memory dimm modules
though a new option : dimm_memory
The dimm modules are generated from a map
dimmid size dimm_memory
dimm0 512 512 100.00 0
dimm1 512 1024 50.00 1
dimm2 512 1536 33.33 2
dimm3 512 2048 25.00 3
dimm4 512 2560 20.00 0
dimm5 512 3072 16.67 1
dimm6 512 3584 14.29 2
dimm7 512 4096 12.50 3
dimm8 512 4608 11.11 0
dimm9 512 5120 10.00 1
dimm10 512 5632 9.09 2
dimm11 512 6144 8.33 3
dimm12 512 6656 7.69 0
dimm13 512 7168 7.14 1
dimm14 512 7680 6.67 2
dimm15 512 8192 6.25 3
dimm16 512 8704 5.88 0
dimm17 512 9216 5.56 1
dimm18 512 9728 5.26 2
dimm19 512 10240 5.00 3
dimm20 512 10752 4.76 0
...
dimm241 65536 3260416 2.01 1
dimm242 65536 3325952 1.97 2
dimm243 65536 3391488 1.93 3
dimm244 65536 3457024 1.90 0
dimm245 65536 3522560 1.86 1
dimm246 65536 3588096 1.83 2
dimm247 65536 3653632 1.79 3
dimm248 65536 3719168 1.76 0
dimm249 65536 3784704 1.73 1
dimm250 65536 3850240 1.70 2
dimm251 65536 3915776 1.67 3
dimm252 65536 3981312 1.65 0
dimm253 65536 4046848 1.62 1
dimm254 65536 4112384 1.59 2
dimm255 65536 4177920 1.57 3
max dimm_memory size is 4TB, which is the current qemu limit
If the dimm_memory value is not aligned on memory module, we align the dimm_memory on the next module.
vmid.conf
---------
memory: 1024
numa:1
hotplug: memmory
when hotplug memory option is enabled, the minimum memory value must be 1GB, and also numa need to be enabled.
we assign the first 1GB as static memory, splitted on each numa nodes.
The remaining memory is assigned on hotpluggable dimm devices.
The static memory need to be also 128MB aligned, to have other dimm devices aligned too.
This 128MB alignment is a linux limitation, windows can align on 2MB size.
Numa need to be aligned, as linux guest don't boot on some setup with multi sockets,
and windows need numa to be able to hotplug memory
hotplug
----
qm set <vmid> -memory X (where X is bigger than current value)
unplug (not yet implemented in qemu)
------
qm set <vmid> -memory X (where X is lower than current value)
linux guest
-----------
-acpi hotplug module should be loaded in guest
-need a recent kernel. (tested with 3.10)
can be enable automaticaly, adding:
/lib/udev/rules.d/80-hotplug-cpu-mem.rules
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", \
ATTR{online}="1"
SUBSYSTEM=="memory", ACTION=="add", TEST=="state", ATTR{state}=="offline", \
ATTR{state}="online"
windows guest
-------------
tested with:
- windows 2012 standard
- windows 2008 enterprise/datacenter
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2015-01-28 08:47:24 +03:00
} elsif ( $ opt =~ m/^memory$/ ) {
die "skip\n" if ! $ hotplug_features - > { memory } ;
2016-05-23 10:47:51 +03:00
PVE::QemuServer::Memory:: qemu_memory_hotplug ( $ vmid , $ conf , $ defaults , $ opt ) ;
2015-05-28 16:59:22 +03:00
} elsif ( $ opt eq 'cpuunits' ) {
cgroups_write ( "cpu" , $ vmid , "cpu.shares" , $ defaults - > { cpuunits } ) ;
2015-06-02 09:06:45 +03:00
} elsif ( $ opt eq 'cpulimit' ) {
cgroups_write ( "cpu" , $ vmid , "cpu.cfs_quota_us" , - 1 ) ;
2014-11-18 15:29:21 +03:00
} else {
2014-11-24 12:33:51 +03:00
die "skip\n" ;
2014-11-18 15:29:21 +03:00
}
2014-11-19 14:59:02 +03:00
} ;
if ( my $ err = $@ ) {
2014-11-24 12:33:51 +03:00
& $ add_error ( $ opt , $ err ) if $ err ne "skip\n" ;
} else {
2014-11-19 14:59:02 +03:00
# save new config if hotplug was successful
delete $ conf - > { $ opt } ;
vmconfig_undelete_pending_option ( $ conf , $ opt ) ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-18 15:29:21 +03:00
}
}
foreach my $ opt ( keys % { $ conf - > { pending } } ) {
2014-11-19 14:59:02 +03:00
next if $ selection && ! $ selection - > { $ opt } ;
2014-11-18 15:29:21 +03:00
my $ value = $ conf - > { pending } - > { $ opt } ;
2014-11-19 14:59:02 +03:00
eval {
2015-02-09 18:47:52 +03:00
if ( $ opt eq 'hotplug' ) {
die "skip\n" if ( $ value =~ /memory/ ) || ( $ value !~ /memory/ && $ conf - > { hotplug } =~ /memory/ ) ;
} elsif ( $ opt eq 'tablet' ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { usb } ;
2014-11-19 14:59:02 +03:00
if ( $ value == 1 ) {
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ opt ) ;
} elsif ( $ value == 0 ) {
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
}
2015-01-09 18:30:36 +03:00
} elsif ( $ opt eq 'vcpus' ) {
2015-01-27 09:16:22 +03:00
die "skip\n" if ! $ hotplug_features - > { cpu } ;
2014-11-19 14:59:02 +03:00
qemu_cpu_hotplug ( $ vmid , $ conf , $ value ) ;
} elsif ( $ opt eq 'balloon' ) {
2015-01-07 12:02:32 +03:00
# enable/disable balloning device is not hotpluggable
2015-01-12 17:04:31 +03:00
my $ old_balloon_enabled = ! ! ( ! defined ( $ conf - > { balloon } ) || $ conf - > { balloon } ) ;
2015-03-27 08:16:24 +03:00
my $ new_balloon_enabled = ! ! ( ! defined ( $ conf - > { pending } - > { balloon } ) || $ conf - > { pending } - > { balloon } ) ;
2015-01-07 12:02:32 +03:00
die "skip\n" if $ old_balloon_enabled != $ new_balloon_enabled ;
2014-11-19 14:59:02 +03:00
# allow manual ballooning if shares is set to zero
2015-03-02 18:03:22 +03:00
if ( ( defined ( $ conf - > { shares } ) && ( $ conf - > { shares } == 0 ) ) ) {
2015-01-02 17:16:01 +03:00
my $ balloon = $ conf - > { pending } - > { balloon } || $ conf - > { memory } || $ defaults - > { memory } ;
vm_mon_cmd ( $ vmid , "balloon" , value = > $ balloon * 1024 * 1024 ) ;
}
2015-03-27 08:16:24 +03:00
} elsif ( $ opt =~ m/^net(\d+)$/ ) {
2014-11-25 08:58:33 +03:00
# some changes can be done without hotplug
2015-03-27 08:16:24 +03:00
vmconfig_update_net ( $ storecfg , $ conf , $ hotplug_features - > { network } ,
2015-01-27 09:16:22 +03:00
$ vmid , $ opt , $ value ) ;
2016-03-03 17:45:15 +03:00
} elsif ( is_valid_drivename ( $ opt ) ) {
2014-11-25 13:37:37 +03:00
# some changes can be done without hotplug
2015-01-27 09:16:22 +03:00
vmconfig_update_disk ( $ storecfg , $ conf , $ hotplug_features - > { disk } ,
$ vmid , $ opt , $ value , 1 ) ;
memory hotplug patch v10
This patch allow to hotplug memory dimm modules
though a new option : dimm_memory
The dimm modules are generated from a map
dimmid size dimm_memory
dimm0 512 512 100.00 0
dimm1 512 1024 50.00 1
dimm2 512 1536 33.33 2
dimm3 512 2048 25.00 3
dimm4 512 2560 20.00 0
dimm5 512 3072 16.67 1
dimm6 512 3584 14.29 2
dimm7 512 4096 12.50 3
dimm8 512 4608 11.11 0
dimm9 512 5120 10.00 1
dimm10 512 5632 9.09 2
dimm11 512 6144 8.33 3
dimm12 512 6656 7.69 0
dimm13 512 7168 7.14 1
dimm14 512 7680 6.67 2
dimm15 512 8192 6.25 3
dimm16 512 8704 5.88 0
dimm17 512 9216 5.56 1
dimm18 512 9728 5.26 2
dimm19 512 10240 5.00 3
dimm20 512 10752 4.76 0
...
dimm241 65536 3260416 2.01 1
dimm242 65536 3325952 1.97 2
dimm243 65536 3391488 1.93 3
dimm244 65536 3457024 1.90 0
dimm245 65536 3522560 1.86 1
dimm246 65536 3588096 1.83 2
dimm247 65536 3653632 1.79 3
dimm248 65536 3719168 1.76 0
dimm249 65536 3784704 1.73 1
dimm250 65536 3850240 1.70 2
dimm251 65536 3915776 1.67 3
dimm252 65536 3981312 1.65 0
dimm253 65536 4046848 1.62 1
dimm254 65536 4112384 1.59 2
dimm255 65536 4177920 1.57 3
max dimm_memory size is 4TB, which is the current qemu limit
If the dimm_memory value is not aligned on memory module, we align the dimm_memory on the next module.
vmid.conf
---------
memory: 1024
numa:1
hotplug: memmory
when hotplug memory option is enabled, the minimum memory value must be 1GB, and also numa need to be enabled.
we assign the first 1GB as static memory, splitted on each numa nodes.
The remaining memory is assigned on hotpluggable dimm devices.
The static memory need to be also 128MB aligned, to have other dimm devices aligned too.
This 128MB alignment is a linux limitation, windows can align on 2MB size.
Numa need to be aligned, as linux guest don't boot on some setup with multi sockets,
and windows need numa to be able to hotplug memory
hotplug
----
qm set <vmid> -memory X (where X is bigger than current value)
unplug (not yet implemented in qemu)
------
qm set <vmid> -memory X (where X is lower than current value)
linux guest
-----------
-acpi hotplug module should be loaded in guest
-need a recent kernel. (tested with 3.10)
can be enable automaticaly, adding:
/lib/udev/rules.d/80-hotplug-cpu-mem.rules
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", \
ATTR{online}="1"
SUBSYSTEM=="memory", ACTION=="add", TEST=="state", ATTR{state}=="offline", \
ATTR{state}="online"
windows guest
-------------
tested with:
- windows 2012 standard
- windows 2008 enterprise/datacenter
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2015-01-28 08:47:24 +03:00
} elsif ( $ opt =~ m/^memory$/ ) { #dimms
die "skip\n" if ! $ hotplug_features - > { memory } ;
2016-05-23 10:47:51 +03:00
$ value = PVE::QemuServer::Memory:: qemu_memory_hotplug ( $ vmid , $ conf , $ defaults , $ opt , $ value ) ;
2015-05-28 16:59:22 +03:00
} elsif ( $ opt eq 'cpuunits' ) {
cgroups_write ( "cpu" , $ vmid , "cpu.shares" , $ conf - > { pending } - > { $ opt } ) ;
2015-06-02 09:06:45 +03:00
} elsif ( $ opt eq 'cpulimit' ) {
2015-06-02 17:03:25 +03:00
my $ cpulimit = $ conf - > { pending } - > { $ opt } == 0 ? - 1 : int ( $ conf - > { pending } - > { $ opt } * 100000 ) ;
2015-06-02 09:06:45 +03:00
cgroups_write ( "cpu" , $ vmid , "cpu.cfs_quota_us" , $ cpulimit ) ;
2014-11-19 14:59:02 +03:00
} else {
2014-11-24 12:33:51 +03:00
die "skip\n" ; # skip non-hot-pluggable options
2014-11-18 15:29:21 +03:00
}
2014-11-19 14:59:02 +03:00
} ;
if ( my $ err = $@ ) {
2014-11-24 12:33:51 +03:00
& $ add_error ( $ opt , $ err ) if $ err ne "skip\n" ;
} else {
2014-11-19 14:59:02 +03:00
# save new config if hotplug was successful
$ conf - > { $ opt } = $ value ;
delete $ conf - > { pending } - > { $ opt } ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-18 15:29:21 +03:00
}
}
2014-11-17 11:50:31 +03:00
}
2014-11-17 09:08:44 +03:00
2015-08-12 14:38:36 +03:00
sub try_deallocate_drive {
my ( $ storecfg , $ vmid , $ conf , $ key , $ drive , $ rpcenv , $ authuser , $ force ) = @ _ ;
if ( ( $ force || $ key =~ /^unused/ ) && ! drive_is_cdrom ( $ drive , 1 ) ) {
my $ volid = $ drive - > { file } ;
if ( vm_is_volid_owner ( $ storecfg , $ vmid , $ volid ) ) {
my $ sid = PVE::Storage:: parse_volume_id ( $ volid ) ;
$ rpcenv - > check ( $ authuser , "/storage/$sid" , [ 'Datastore.AllocateSpace' ] ) ;
2015-08-13 12:15:56 +03:00
# check if the disk is really unused
die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
2015-12-10 14:07:39 +03:00
if is_volume_in_use ( $ storecfg , $ conf , $ key , $ volid ) ;
2015-08-13 12:15:56 +03:00
PVE::Storage:: vdisk_free ( $ storecfg , $ volid ) ;
2015-08-12 14:38:36 +03:00
return 1 ;
2015-08-20 11:34:59 +03:00
} else {
# If vm is not owner of this disk remove from config
return 1 ;
2015-08-12 14:38:36 +03:00
}
}
return undef ;
}
sub vmconfig_delete_or_detach_drive {
my ( $ vmid , $ storecfg , $ conf , $ opt , $ force ) = @ _ ;
my $ drive = parse_drive ( $ opt , $ conf - > { $ opt } ) ;
my $ rpcenv = PVE::RPCEnvironment:: get ( ) ;
my $ authuser = $ rpcenv - > get_user ( ) ;
if ( $ force ) {
$ rpcenv - > check_vm_perm ( $ authuser , $ vmid , undef , [ 'VM.Config.Disk' ] ) ;
try_deallocate_drive ( $ storecfg , $ vmid , $ conf , $ opt , $ drive , $ rpcenv , $ authuser , $ force ) ;
} else {
vmconfig_register_unused_drive ( $ storecfg , $ vmid , $ conf , $ drive ) ;
}
}
2014-11-17 09:08:44 +03:00
sub vmconfig_apply_pending {
2014-11-19 14:59:02 +03:00
my ( $ vmid , $ conf , $ storecfg ) = @ _ ;
2014-11-17 11:50:31 +03:00
# cold plug
2014-11-17 09:08:44 +03:00
2015-08-12 14:38:36 +03:00
my $ pending_delete_hash = split_flagged_list ( $ conf - > { pending } - > { delete } ) ;
while ( my ( $ opt , $ force ) = each %$ pending_delete_hash ) {
2014-11-17 09:08:44 +03:00
die "internal error" if $ opt =~ m/^unused/ ;
2016-03-07 14:41:12 +03:00
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-17 09:08:44 +03:00
if ( ! defined ( $ conf - > { $ opt } ) ) {
vmconfig_undelete_pending_option ( $ conf , $ opt ) ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
2016-03-03 17:45:15 +03:00
} elsif ( is_valid_drivename ( $ opt ) ) {
2015-08-12 14:38:36 +03:00
vmconfig_delete_or_detach_drive ( $ vmid , $ storecfg , $ conf , $ opt , $ force ) ;
2014-11-17 09:08:44 +03:00
vmconfig_undelete_pending_option ( $ conf , $ opt ) ;
delete $ conf - > { $ opt } ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
2014-11-17 09:08:44 +03:00
} else {
vmconfig_undelete_pending_option ( $ conf , $ opt ) ;
delete $ conf - > { $ opt } ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
2014-11-17 09:08:44 +03:00
}
}
2016-03-07 14:41:12 +03:00
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-17 09:08:44 +03:00
foreach my $ opt ( keys % { $ conf - > { pending } } ) { # add/change
2016-03-07 14:41:12 +03:00
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-17 09:08:44 +03:00
if ( defined ( $ conf - > { $ opt } ) && ( $ conf - > { $ opt } eq $ conf - > { pending } - > { $ opt } ) ) {
# skip if nothing changed
2016-03-03 17:45:15 +03:00
} elsif ( is_valid_drivename ( $ opt ) ) {
2014-11-17 09:08:44 +03:00
vmconfig_register_unused_drive ( $ storecfg , $ vmid , $ conf , parse_drive ( $ opt , $ conf - > { $ opt } ) )
if defined ( $ conf - > { $ opt } ) ;
$ conf - > { $ opt } = $ conf - > { pending } - > { $ opt } ;
} else {
$ conf - > { $ opt } = $ conf - > { pending } - > { $ opt } ;
}
delete $ conf - > { pending } - > { $ opt } ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
2014-11-17 09:08:44 +03:00
}
}
2014-11-25 08:58:33 +03:00
my $ safe_num_ne = sub {
my ( $ a , $ b ) = @ _ ;
return 0 if ! defined ( $ a ) && ! defined ( $ b ) ;
return 1 if ! defined ( $ a ) ;
return 1 if ! defined ( $ b ) ;
return $ a != $ b ;
} ;
my $ safe_string_ne = sub {
my ( $ a , $ b ) = @ _ ;
return 0 if ! defined ( $ a ) && ! defined ( $ b ) ;
return 1 if ! defined ( $ a ) ;
return 1 if ! defined ( $ b ) ;
return $ a ne $ b ;
} ;
sub vmconfig_update_net {
2015-01-27 09:16:22 +03:00
my ( $ storecfg , $ conf , $ hotplug , $ vmid , $ opt , $ value ) = @ _ ;
2014-11-25 08:58:33 +03:00
my $ newnet = parse_net ( $ value ) ;
if ( $ conf - > { $ opt } ) {
my $ oldnet = parse_net ( $ conf - > { $ opt } ) ;
if ( & $ safe_string_ne ( $ oldnet - > { model } , $ newnet - > { model } ) ||
& $ safe_string_ne ( $ oldnet - > { macaddr } , $ newnet - > { macaddr } ) ||
& $ safe_num_ne ( $ oldnet - > { queues } , $ newnet - > { queues } ) ||
! ( $ newnet - > { bridge } && $ oldnet - > { bridge } ) ) { # bridge/nat mode change
# for non online change, we try to hot-unplug
2015-01-21 10:52:37 +03:00
die "skip\n" if ! $ hotplug ;
2014-11-25 08:58:33 +03:00
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
} else {
die "internal error" if $ opt !~ m/net(\d+)/ ;
my $ iface = "tap${vmid}i$1" ;
2015-03-27 08:16:24 +03:00
2015-01-20 13:47:11 +03:00
if ( & $ safe_string_ne ( $ oldnet - > { bridge } , $ newnet - > { bridge } ) ||
& $ safe_num_ne ( $ oldnet - > { tag } , $ newnet - > { tag } ) ||
2016-01-18 11:09:05 +03:00
& $ safe_string_ne ( $ oldnet - > { trunks } , $ newnet - > { trunks } ) ||
2015-01-20 13:47:11 +03:00
& $ safe_num_ne ( $ oldnet - > { firewall } , $ newnet - > { firewall } ) ) {
2014-11-25 08:58:33 +03:00
PVE::Network:: tap_unplug ( $ iface ) ;
2016-03-08 15:55:13 +03:00
PVE::Network:: tap_plug ( $ iface , $ newnet - > { bridge } , $ newnet - > { tag } , $ newnet - > { firewall } , $ newnet - > { trunks } , $ newnet - > { rate } ) ;
} elsif ( & $ safe_num_ne ( $ oldnet - > { rate } , $ newnet - > { rate } ) ) {
# Rate can be applied on its own but any change above needs to
# include the rate in tap_plug since OVS resets everything.
PVE::Network:: tap_rate_limit ( $ iface , $ newnet - > { rate } ) ;
2014-11-25 08:58:33 +03:00
}
2014-11-25 14:07:02 +03:00
2015-01-20 13:47:11 +03:00
if ( & $ safe_string_ne ( $ oldnet - > { link_down } , $ newnet - > { link_down } ) ) {
qemu_set_link_status ( $ vmid , $ opt , ! $ newnet - > { link_down } ) ;
}
2014-11-25 14:07:02 +03:00
return 1 ;
2014-11-25 08:58:33 +03:00
}
}
2015-03-27 08:16:24 +03:00
2015-01-21 10:52:37 +03:00
if ( $ hotplug ) {
2014-11-25 14:07:02 +03:00
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ opt , $ newnet ) ;
} else {
die "skip\n" ;
}
2014-11-25 08:58:33 +03:00
}
2014-11-25 13:37:37 +03:00
sub vmconfig_update_disk {
2015-01-27 09:16:22 +03:00
my ( $ storecfg , $ conf , $ hotplug , $ vmid , $ opt , $ value , $ force ) = @ _ ;
2014-11-25 13:37:37 +03:00
# fixme: do we need force?
my $ drive = parse_drive ( $ opt , $ value ) ;
if ( $ conf - > { $ opt } ) {
if ( my $ old_drive = parse_drive ( $ opt , $ conf - > { $ opt } ) ) {
my $ media = $ drive - > { media } || 'disk' ;
my $ oldmedia = $ old_drive - > { media } || 'disk' ;
die "unable to change media type\n" if $ media ne $ oldmedia ;
if ( ! drive_is_cdrom ( $ old_drive ) ) {
2015-03-27 08:16:24 +03:00
if ( $ drive - > { file } ne $ old_drive - > { file } ) {
2014-11-25 13:37:37 +03:00
2015-01-21 10:52:37 +03:00
die "skip\n" if ! $ hotplug ;
2014-11-25 13:37:37 +03:00
# unplug and register as unused
vm_deviceunplug ( $ vmid , $ conf , $ opt ) ;
vmconfig_register_unused_drive ( $ storecfg , $ vmid , $ conf , $ old_drive )
2015-03-27 08:16:24 +03:00
2014-11-25 13:37:37 +03:00
} else {
# update existing disk
# skip non hotpluggable value
2015-03-27 08:16:24 +03:00
if ( & $ safe_num_ne ( $ drive - > { discard } , $ old_drive - > { discard } ) ||
2015-03-19 13:06:12 +03:00
& $ safe_string_ne ( $ drive - > { iothread } , $ old_drive - > { iothread } ) ||
2015-04-02 07:10:54 +03:00
& $ safe_string_ne ( $ drive - > { queues } , $ old_drive - > { queues } ) ||
2014-11-25 13:37:37 +03:00
& $ safe_string_ne ( $ drive - > { cache } , $ old_drive - > { cache } ) ) {
die "skip\n" ;
}
# apply throttle
if ( & $ safe_num_ne ( $ drive - > { mbps } , $ old_drive - > { mbps } ) ||
& $ safe_num_ne ( $ drive - > { mbps_rd } , $ old_drive - > { mbps_rd } ) ||
& $ safe_num_ne ( $ drive - > { mbps_wr } , $ old_drive - > { mbps_wr } ) ||
& $ safe_num_ne ( $ drive - > { iops } , $ old_drive - > { iops } ) ||
& $ safe_num_ne ( $ drive - > { iops_rd } , $ old_drive - > { iops_rd } ) ||
& $ safe_num_ne ( $ drive - > { iops_wr } , $ old_drive - > { iops_wr } ) ||
& $ safe_num_ne ( $ drive - > { mbps_max } , $ old_drive - > { mbps_max } ) ||
& $ safe_num_ne ( $ drive - > { mbps_rd_max } , $ old_drive - > { mbps_rd_max } ) ||
& $ safe_num_ne ( $ drive - > { mbps_wr_max } , $ old_drive - > { mbps_wr_max } ) ||
& $ safe_num_ne ( $ drive - > { iops_max } , $ old_drive - > { iops_max } ) ||
& $ safe_num_ne ( $ drive - > { iops_rd_max } , $ old_drive - > { iops_rd_max } ) ||
& $ safe_num_ne ( $ drive - > { iops_wr_max } , $ old_drive - > { iops_wr_max } ) ) {
2015-03-27 08:16:24 +03:00
2014-11-25 13:37:37 +03:00
qemu_block_set_io_throttle ( $ vmid , "drive-$opt" ,
( $ drive - > { mbps } || 0 ) * 1024 * 1024 ,
( $ drive - > { mbps_rd } || 0 ) * 1024 * 1024 ,
( $ drive - > { mbps_wr } || 0 ) * 1024 * 1024 ,
$ drive - > { iops } || 0 ,
$ drive - > { iops_rd } || 0 ,
$ drive - > { iops_wr } || 0 ,
( $ drive - > { mbps_max } || 0 ) * 1024 * 1024 ,
( $ drive - > { mbps_rd_max } || 0 ) * 1024 * 1024 ,
( $ drive - > { mbps_wr_max } || 0 ) * 1024 * 1024 ,
$ drive - > { iops_max } || 0 ,
$ drive - > { iops_rd_max } || 0 ,
$ drive - > { iops_wr_max } || 0 ) ;
}
2015-03-27 08:16:24 +03:00
2014-11-25 13:37:37 +03:00
return 1 ;
}
2015-02-12 10:00:14 +03:00
} else { # cdrom
2015-03-27 08:16:24 +03:00
2015-02-12 10:00:14 +03:00
if ( $ drive - > { file } eq 'none' ) {
vm_mon_cmd ( $ vmid , "eject" , force = > JSON:: true , device = > "drive-$opt" ) ;
} else {
my $ path = get_iso_path ( $ storecfg , $ vmid , $ drive - > { file } ) ;
vm_mon_cmd ( $ vmid , "eject" , force = > JSON:: true , device = > "drive-$opt" ) ; # force eject if locked
vm_mon_cmd ( $ vmid , "change" , device = > "drive-$opt" , target = > "$path" ) if $ path ;
}
2015-03-27 08:16:24 +03:00
2015-02-14 11:20:41 +03:00
return 1 ;
2014-11-25 13:37:37 +03:00
}
}
}
2015-03-27 08:16:24 +03:00
die "skip\n" if ! $ hotplug || $ opt =~ m/(ide|sata)(\d+)/ ;
2015-02-12 10:00:14 +03:00
# hotplug new disks
2016-02-04 18:47:56 +03:00
PVE::Storage:: activate_volumes ( $ storecfg , [ $ drive - > { file } ] ) if $ drive - > { file } !~ m | ^ /dev/ . + | ;
2015-02-12 10:00:14 +03:00
vm_deviceplug ( $ storecfg , $ conf , $ vmid , $ opt , $ drive ) ;
2014-11-25 13:37:37 +03:00
}
2011-08-23 09:47:04 +04:00
sub vm_start {
2015-10-23 11:41:53 +03:00
my ( $ storecfg , $ vmid , $ statefile , $ skiplock , $ migratedfrom , $ paused ,
$ forcemachine , $ spice_ticket ) = @ _ ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
my $ conf = PVE::QemuConfig - > load_config ( $ vmid , $ migratedfrom ) ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
die "you can't start a vm if it's a template\n" if PVE::QemuConfig - > is_template ( $ conf ) ;
2013-02-14 14:58:53 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2012-08-21 14:21:51 +04:00
die "VM $vmid already running\n" if check_running ( $ vmid , undef , $ migratedfrom ) ;
2011-08-23 09:47:04 +04:00
2014-11-17 09:08:44 +03:00
if ( ! $ statefile && scalar ( keys % { $ conf - > { pending } } ) ) {
2014-11-19 14:59:02 +03:00
vmconfig_apply_pending ( $ vmid , $ conf , $ storecfg ) ;
2016-03-07 14:41:12 +03:00
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ; # update/reload
2014-11-17 09:08:44 +03:00
}
2012-09-18 11:23:47 +04:00
my $ defaults = load_defaults ( ) ;
# set environment variable useful inside network script
$ ENV { PVE_MIGRATED_FROM } = $ migratedfrom if $ migratedfrom ;
2015-10-29 09:37:00 +03:00
my ( $ cmd , $ vollist , $ spice_port ) = config_to_command ( $ storecfg , $ vmid , $ conf , $ defaults , $ forcemachine ) ;
2012-09-18 11:23:47 +04:00
2011-08-23 09:47:04 +04:00
my $ migrate_port = 0 ;
qemu-server: add support for unsecure migration (setting in datacenter.cfg)
This patch adds support for unsecure migration using a direct tcp connection
KVM <=> KVM instead of an extra SSH tunnel. Without ssh the limit is just the
bandwith and no longer the CPU / one single core.
You can enable this by adding:
migration_unsecure: 1
to datacenter.cfg
Examples using qemu 1.4 as migration with qemu 1.3 still does not work for me:
current default with SSH Tunnel VM uses 2GB mem:
Dec 27 21:10:32 starting migration of VM 105 to node 'cloud1-1202' (10.255.0.20)
Dec 27 21:10:32 copying disk images
Dec 27 21:10:32 starting VM 105 on remote node 'cloud1-1202'
Dec 27 21:10:35 starting ssh migration tunnel
Dec 27 21:10:36 starting online/live migration on localhost:60000
Dec 27 21:10:36 migrate_set_speed: 8589934592
Dec 27 21:10:36 migrate_set_downtime: 1
Dec 27 21:10:38 migration status: active (transferred 152481002, remaining 1938546688), total 2156396544) , expected downtime 0
Dec 27 21:10:40 migration status: active (transferred 279836995, remaining 1811140608), total 2156396544) , expected downtime 0
Dec 27 21:10:42 migration status: active (transferred 421265271, remaining 1669840896), total 2156396544) , expected downtime 0
Dec 27 21:10:44 migration status: active (transferred 570987974, remaining 1520152576), total 2156396544) , expected downtime 0
Dec 27 21:10:46 migration status: active (transferred 721469404, remaining 1369939968), total 2156396544) , expected downtime 0
Dec 27 21:10:48 migration status: active (transferred 875595258, remaining 1216057344), total 2156396544) , expected downtime 0
Dec 27 21:10:50 migration status: active (transferred 1034654822, remaining 1056931840), total 2156396544) , expected downtime 0
Dec 27 21:10:54 migration status: active (transferred 1176288424, remaining 915369984), total 2156396544) , expected downtime 0
Dec 27 21:10:56 migration status: active (transferred 1339734759, remaining 752050176), total 2156396544) , expected downtime 0
Dec 27 21:10:58 migration status: active (transferred 1503743261, remaining 588206080), total 2156396544) , expected downtime 0
Dec 27 21:11:02 migration status: active (transferred 1645097827, remaining 446906368), total 2156396544) , expected downtime 0
Dec 27 21:11:04 migration status: active (transferred 1810562934, remaining 281751552), total 2156396544) , expected downtime 0
Dec 27 21:11:06 migration status: active (transferred 1964377505, remaining 126033920), total 2156396544) , expected downtime 0
Dec 27 21:11:08 migration status: active (transferred 2077930417, remaining 0), total 2156396544) , expected downtime 0
Dec 27 21:11:09 migration speed: 62.06 MB/s - downtime 37 ms
Dec 27 21:11:09 migration status: completed
Dec 27 21:11:13 migration finished successfuly (duration 00:00:41)
TASK OK
with unsecure migration without SSH Tunnel:
Dec 27 22:43:14 starting migration of VM 105 to node 'cloud1-1203' (10.255.0.22)
Dec 27 22:43:14 copying disk images
Dec 27 22:43:14 starting VM 105 on remote node 'cloud1-1203'
Dec 27 22:43:17 starting online/live migration on 10.255.0.22:60000
Dec 27 22:43:17 migrate_set_speed: 8589934592
Dec 27 22:43:17 migrate_set_downtime: 1
Dec 27 22:43:19 migration speed: 1024.00 MB/s - downtime 1100 ms
Dec 27 22:43:19 migration status: completed
Dec 27 22:43:22 migration finished successfuly (duration 00:00:09)
TASK OK
2013-07-26 13:22:58 +04:00
my $ migrate_uri ;
2011-08-23 09:47:04 +04:00
if ( $ statefile ) {
if ( $ statefile eq 'tcp' ) {
qemu-server: add support for unsecure migration (setting in datacenter.cfg)
This patch adds support for unsecure migration using a direct tcp connection
KVM <=> KVM instead of an extra SSH tunnel. Without ssh the limit is just the
bandwith and no longer the CPU / one single core.
You can enable this by adding:
migration_unsecure: 1
to datacenter.cfg
Examples using qemu 1.4 as migration with qemu 1.3 still does not work for me:
current default with SSH Tunnel VM uses 2GB mem:
Dec 27 21:10:32 starting migration of VM 105 to node 'cloud1-1202' (10.255.0.20)
Dec 27 21:10:32 copying disk images
Dec 27 21:10:32 starting VM 105 on remote node 'cloud1-1202'
Dec 27 21:10:35 starting ssh migration tunnel
Dec 27 21:10:36 starting online/live migration on localhost:60000
Dec 27 21:10:36 migrate_set_speed: 8589934592
Dec 27 21:10:36 migrate_set_downtime: 1
Dec 27 21:10:38 migration status: active (transferred 152481002, remaining 1938546688), total 2156396544) , expected downtime 0
Dec 27 21:10:40 migration status: active (transferred 279836995, remaining 1811140608), total 2156396544) , expected downtime 0
Dec 27 21:10:42 migration status: active (transferred 421265271, remaining 1669840896), total 2156396544) , expected downtime 0
Dec 27 21:10:44 migration status: active (transferred 570987974, remaining 1520152576), total 2156396544) , expected downtime 0
Dec 27 21:10:46 migration status: active (transferred 721469404, remaining 1369939968), total 2156396544) , expected downtime 0
Dec 27 21:10:48 migration status: active (transferred 875595258, remaining 1216057344), total 2156396544) , expected downtime 0
Dec 27 21:10:50 migration status: active (transferred 1034654822, remaining 1056931840), total 2156396544) , expected downtime 0
Dec 27 21:10:54 migration status: active (transferred 1176288424, remaining 915369984), total 2156396544) , expected downtime 0
Dec 27 21:10:56 migration status: active (transferred 1339734759, remaining 752050176), total 2156396544) , expected downtime 0
Dec 27 21:10:58 migration status: active (transferred 1503743261, remaining 588206080), total 2156396544) , expected downtime 0
Dec 27 21:11:02 migration status: active (transferred 1645097827, remaining 446906368), total 2156396544) , expected downtime 0
Dec 27 21:11:04 migration status: active (transferred 1810562934, remaining 281751552), total 2156396544) , expected downtime 0
Dec 27 21:11:06 migration status: active (transferred 1964377505, remaining 126033920), total 2156396544) , expected downtime 0
Dec 27 21:11:08 migration status: active (transferred 2077930417, remaining 0), total 2156396544) , expected downtime 0
Dec 27 21:11:09 migration speed: 62.06 MB/s - downtime 37 ms
Dec 27 21:11:09 migration status: completed
Dec 27 21:11:13 migration finished successfuly (duration 00:00:41)
TASK OK
with unsecure migration without SSH Tunnel:
Dec 27 22:43:14 starting migration of VM 105 to node 'cloud1-1203' (10.255.0.22)
Dec 27 22:43:14 copying disk images
Dec 27 22:43:14 starting VM 105 on remote node 'cloud1-1203'
Dec 27 22:43:17 starting online/live migration on 10.255.0.22:60000
Dec 27 22:43:17 migrate_set_speed: 8589934592
Dec 27 22:43:17 migrate_set_downtime: 1
Dec 27 22:43:19 migration speed: 1024.00 MB/s - downtime 1100 ms
Dec 27 22:43:19 migration status: completed
Dec 27 22:43:22 migration finished successfuly (duration 00:00:09)
TASK OK
2013-07-26 13:22:58 +04:00
my $ localip = "localhost" ;
my $ datacenterconf = PVE::Cluster:: cfs_read_file ( 'datacenter.cfg' ) ;
2015-05-12 13:14:03 +03:00
my $ nodename = PVE::INotify:: nodename ( ) ;
qemu-server: add support for unsecure migration (setting in datacenter.cfg)
This patch adds support for unsecure migration using a direct tcp connection
KVM <=> KVM instead of an extra SSH tunnel. Without ssh the limit is just the
bandwith and no longer the CPU / one single core.
You can enable this by adding:
migration_unsecure: 1
to datacenter.cfg
Examples using qemu 1.4 as migration with qemu 1.3 still does not work for me:
current default with SSH Tunnel VM uses 2GB mem:
Dec 27 21:10:32 starting migration of VM 105 to node 'cloud1-1202' (10.255.0.20)
Dec 27 21:10:32 copying disk images
Dec 27 21:10:32 starting VM 105 on remote node 'cloud1-1202'
Dec 27 21:10:35 starting ssh migration tunnel
Dec 27 21:10:36 starting online/live migration on localhost:60000
Dec 27 21:10:36 migrate_set_speed: 8589934592
Dec 27 21:10:36 migrate_set_downtime: 1
Dec 27 21:10:38 migration status: active (transferred 152481002, remaining 1938546688), total 2156396544) , expected downtime 0
Dec 27 21:10:40 migration status: active (transferred 279836995, remaining 1811140608), total 2156396544) , expected downtime 0
Dec 27 21:10:42 migration status: active (transferred 421265271, remaining 1669840896), total 2156396544) , expected downtime 0
Dec 27 21:10:44 migration status: active (transferred 570987974, remaining 1520152576), total 2156396544) , expected downtime 0
Dec 27 21:10:46 migration status: active (transferred 721469404, remaining 1369939968), total 2156396544) , expected downtime 0
Dec 27 21:10:48 migration status: active (transferred 875595258, remaining 1216057344), total 2156396544) , expected downtime 0
Dec 27 21:10:50 migration status: active (transferred 1034654822, remaining 1056931840), total 2156396544) , expected downtime 0
Dec 27 21:10:54 migration status: active (transferred 1176288424, remaining 915369984), total 2156396544) , expected downtime 0
Dec 27 21:10:56 migration status: active (transferred 1339734759, remaining 752050176), total 2156396544) , expected downtime 0
Dec 27 21:10:58 migration status: active (transferred 1503743261, remaining 588206080), total 2156396544) , expected downtime 0
Dec 27 21:11:02 migration status: active (transferred 1645097827, remaining 446906368), total 2156396544) , expected downtime 0
Dec 27 21:11:04 migration status: active (transferred 1810562934, remaining 281751552), total 2156396544) , expected downtime 0
Dec 27 21:11:06 migration status: active (transferred 1964377505, remaining 126033920), total 2156396544) , expected downtime 0
Dec 27 21:11:08 migration status: active (transferred 2077930417, remaining 0), total 2156396544) , expected downtime 0
Dec 27 21:11:09 migration speed: 62.06 MB/s - downtime 37 ms
Dec 27 21:11:09 migration status: completed
Dec 27 21:11:13 migration finished successfuly (duration 00:00:41)
TASK OK
with unsecure migration without SSH Tunnel:
Dec 27 22:43:14 starting migration of VM 105 to node 'cloud1-1203' (10.255.0.22)
Dec 27 22:43:14 copying disk images
Dec 27 22:43:14 starting VM 105 on remote node 'cloud1-1203'
Dec 27 22:43:17 starting online/live migration on 10.255.0.22:60000
Dec 27 22:43:17 migrate_set_speed: 8589934592
Dec 27 22:43:17 migrate_set_downtime: 1
Dec 27 22:43:19 migration speed: 1024.00 MB/s - downtime 1100 ms
Dec 27 22:43:19 migration status: completed
Dec 27 22:43:22 migration finished successfuly (duration 00:00:09)
TASK OK
2013-07-26 13:22:58 +04:00
if ( $ datacenterconf - > { migration_unsecure } ) {
$ localip = PVE::Cluster:: remote_node_ip ( $ nodename , 1 ) ;
2015-11-05 16:09:16 +03:00
$ localip = "[$localip]" if Net::IP:: ip_is_ipv6 ( $ localip ) ;
qemu-server: add support for unsecure migration (setting in datacenter.cfg)
This patch adds support for unsecure migration using a direct tcp connection
KVM <=> KVM instead of an extra SSH tunnel. Without ssh the limit is just the
bandwith and no longer the CPU / one single core.
You can enable this by adding:
migration_unsecure: 1
to datacenter.cfg
Examples using qemu 1.4 as migration with qemu 1.3 still does not work for me:
current default with SSH Tunnel VM uses 2GB mem:
Dec 27 21:10:32 starting migration of VM 105 to node 'cloud1-1202' (10.255.0.20)
Dec 27 21:10:32 copying disk images
Dec 27 21:10:32 starting VM 105 on remote node 'cloud1-1202'
Dec 27 21:10:35 starting ssh migration tunnel
Dec 27 21:10:36 starting online/live migration on localhost:60000
Dec 27 21:10:36 migrate_set_speed: 8589934592
Dec 27 21:10:36 migrate_set_downtime: 1
Dec 27 21:10:38 migration status: active (transferred 152481002, remaining 1938546688), total 2156396544) , expected downtime 0
Dec 27 21:10:40 migration status: active (transferred 279836995, remaining 1811140608), total 2156396544) , expected downtime 0
Dec 27 21:10:42 migration status: active (transferred 421265271, remaining 1669840896), total 2156396544) , expected downtime 0
Dec 27 21:10:44 migration status: active (transferred 570987974, remaining 1520152576), total 2156396544) , expected downtime 0
Dec 27 21:10:46 migration status: active (transferred 721469404, remaining 1369939968), total 2156396544) , expected downtime 0
Dec 27 21:10:48 migration status: active (transferred 875595258, remaining 1216057344), total 2156396544) , expected downtime 0
Dec 27 21:10:50 migration status: active (transferred 1034654822, remaining 1056931840), total 2156396544) , expected downtime 0
Dec 27 21:10:54 migration status: active (transferred 1176288424, remaining 915369984), total 2156396544) , expected downtime 0
Dec 27 21:10:56 migration status: active (transferred 1339734759, remaining 752050176), total 2156396544) , expected downtime 0
Dec 27 21:10:58 migration status: active (transferred 1503743261, remaining 588206080), total 2156396544) , expected downtime 0
Dec 27 21:11:02 migration status: active (transferred 1645097827, remaining 446906368), total 2156396544) , expected downtime 0
Dec 27 21:11:04 migration status: active (transferred 1810562934, remaining 281751552), total 2156396544) , expected downtime 0
Dec 27 21:11:06 migration status: active (transferred 1964377505, remaining 126033920), total 2156396544) , expected downtime 0
Dec 27 21:11:08 migration status: active (transferred 2077930417, remaining 0), total 2156396544) , expected downtime 0
Dec 27 21:11:09 migration speed: 62.06 MB/s - downtime 37 ms
Dec 27 21:11:09 migration status: completed
Dec 27 21:11:13 migration finished successfuly (duration 00:00:41)
TASK OK
with unsecure migration without SSH Tunnel:
Dec 27 22:43:14 starting migration of VM 105 to node 'cloud1-1203' (10.255.0.22)
Dec 27 22:43:14 copying disk images
Dec 27 22:43:14 starting VM 105 on remote node 'cloud1-1203'
Dec 27 22:43:17 starting online/live migration on 10.255.0.22:60000
Dec 27 22:43:17 migrate_set_speed: 8589934592
Dec 27 22:43:17 migrate_set_downtime: 1
Dec 27 22:43:19 migration speed: 1024.00 MB/s - downtime 1100 ms
Dec 27 22:43:19 migration status: completed
Dec 27 22:43:22 migration finished successfuly (duration 00:00:09)
TASK OK
2013-07-26 13:22:58 +04:00
}
2015-05-12 13:14:03 +03:00
my $ pfamily = PVE::Tools:: get_host_address_family ( $ nodename ) ;
$ migrate_port = PVE::Tools:: next_migrate_port ( $ pfamily ) ;
2015-11-05 16:09:16 +03:00
$ migrate_uri = "tcp:${localip}:${migrate_port}" ;
2012-09-18 11:23:47 +04:00
push @$ cmd , '-incoming' , $ migrate_uri ;
push @$ cmd , '-S' ;
migrate: use ssh forwarded UNIX socket tunnel
We cannot guarantee when the SSH forward Tunnel really becomes
ready. The check with the mtunnel API call did not help for this
prolem as it only checked that the SSH connection itself works and
that the destination node has quorum but the forwarded tunnel itself
was not checked.
The Forward tunnel is a different channel in the SSH connection,
independent of the SSH `qm mtunnel` channel, so only if that works
it does not guarantees that our migration tunnel is up and ready.
When the node(s) where under load, or when we did parallel
migrations (migrateall), the migrate command was often started
before a tunnel was open and ready to receive data. This led to
a direct abortion of the migration and is the main cause in why
parallel migrations often leave two thirds or more VMs on the
source node.
The issue was tracked down to SSH after debugging the QEMU
process and enabling debug logging showed that the tunnel became
often to late available and ready, or not at all.
Fixing the TCP forward tunnel is quirky and not straight ahead, the
only way SSH gives as a possibility is to use -N (no command)
-f (background) and -o "ExitOnForwardFailure=yes", then it would
wait in the foreground until the tunnel is ready and only then
background itself. This is not quite the nicest way for our special
use case and our code base.
Waiting for the local port to become open and ready (through
/proc/net/tcp[6]] as a proof of concept is not enough, even if the
port is in the listening state and should theoretically accept
connections this still failed often as the tunnel was not yet fully
ready.
Further another problem would still be open if we tried to patch the
SSH Forward method we currently use - which we solve for free with
the approach of this patch - namely the problem that the method
to get an available port (next_migration_port) has a serious race
condition which could lead to multiple use of the same port on a
parallel migration (I observed this on my many test, seldom but if
it happens its really bad).
So lets now use UNIX sockets, which ssh supports since version 5.7.
The end points are UNIX socket bound to the VMID - thus no port so
no race and also no limitation of available ports (we reserved 50 for
migration).
The endpoints get created in /run/qemu-server/VMID.migrate and as
KVM/QEMU in current versions is able to use UNIX socket just as well
as TCP we have not to change much on the interaction with QEMU.
QEMU is started with the migrate_incoming url at the local
destination endpoint and creates the socket file, we then create
a listening socket on the source side and connect over SSH to the
destination.
Now the migration can be started by issuing the migrate qmp command
with an updated uri.
This breaks live migration from new to old, but *not* from old to
new, so there is a upgrade path.
If a live migration from new to old must be made (for whatever
reason), use the unsecure_migration setting (man datacenter.conf)
to allow this, although that should only be done in trusted network.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2016-06-03 12:32:00 +03:00
} elsif ( $ statefile eq 'unix' ) {
# should be default for secure migrations as a ssh TCP forward
# tunnel is not deterministic reliable ready and fails regurarly
# to set up in time, so use UNIX socket forwards
2016-06-03 16:59:15 +03:00
my $ socket_addr = "/run/qemu-server/$vmid.migrate" ;
unlink $ socket_addr ;
$ migrate_uri = "unix:$socket_addr" ;
migrate: use ssh forwarded UNIX socket tunnel
We cannot guarantee when the SSH forward Tunnel really becomes
ready. The check with the mtunnel API call did not help for this
prolem as it only checked that the SSH connection itself works and
that the destination node has quorum but the forwarded tunnel itself
was not checked.
The Forward tunnel is a different channel in the SSH connection,
independent of the SSH `qm mtunnel` channel, so only if that works
it does not guarantees that our migration tunnel is up and ready.
When the node(s) where under load, or when we did parallel
migrations (migrateall), the migrate command was often started
before a tunnel was open and ready to receive data. This led to
a direct abortion of the migration and is the main cause in why
parallel migrations often leave two thirds or more VMs on the
source node.
The issue was tracked down to SSH after debugging the QEMU
process and enabling debug logging showed that the tunnel became
often to late available and ready, or not at all.
Fixing the TCP forward tunnel is quirky and not straight ahead, the
only way SSH gives as a possibility is to use -N (no command)
-f (background) and -o "ExitOnForwardFailure=yes", then it would
wait in the foreground until the tunnel is ready and only then
background itself. This is not quite the nicest way for our special
use case and our code base.
Waiting for the local port to become open and ready (through
/proc/net/tcp[6]] as a proof of concept is not enough, even if the
port is in the listening state and should theoretically accept
connections this still failed often as the tunnel was not yet fully
ready.
Further another problem would still be open if we tried to patch the
SSH Forward method we currently use - which we solve for free with
the approach of this patch - namely the problem that the method
to get an available port (next_migration_port) has a serious race
condition which could lead to multiple use of the same port on a
parallel migration (I observed this on my many test, seldom but if
it happens its really bad).
So lets now use UNIX sockets, which ssh supports since version 5.7.
The end points are UNIX socket bound to the VMID - thus no port so
no race and also no limitation of available ports (we reserved 50 for
migration).
The endpoints get created in /run/qemu-server/VMID.migrate and as
KVM/QEMU in current versions is able to use UNIX socket just as well
as TCP we have not to change much on the interaction with QEMU.
QEMU is started with the migrate_incoming url at the local
destination endpoint and creates the socket file, we then create
a listening socket on the source side and connect over SSH to the
destination.
Now the migration can be started by issuing the migrate qmp command
with an updated uri.
This breaks live migration from new to old, but *not* from old to
new, so there is a upgrade path.
If a live migration from new to old must be made (for whatever
reason), use the unsecure_migration setting (man datacenter.conf)
to allow this, although that should only be done in trusted network.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2016-06-03 12:32:00 +03:00
push @$ cmd , '-incoming' , $ migrate_uri ;
push @$ cmd , '-S' ;
2011-08-23 09:47:04 +04:00
} else {
2012-09-18 11:23:47 +04:00
push @$ cmd , '-loadstate' , $ statefile ;
2011-08-23 09:47:04 +04:00
}
2012-12-12 18:35:26 +04:00
} elsif ( $ paused ) {
push @$ cmd , '-S' ;
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 ;
2014-06-23 19:41:55 +04:00
my $ pcidevices = $ d - > { pciid } ;
foreach my $ pcidevice ( @$ pcidevices ) {
my $ pciid = $ pcidevice - > { id } . "." . $ pcidevice - > { function } ;
2014-02-14 00:12:29 +04:00
2014-06-23 19:41:55 +04:00
my $ info = pci_device_info ( "0000:$pciid" ) ;
die "IOMMU not present\n" if ! check_iommu_support ( ) ;
die "no pci device info for device '$pciid'\n" if ! $ info ;
2015-09-20 20:23:23 +03:00
die "can't unbind/bind pci group to vfio '$pciid'\n" if ! pci_dev_group_bind_to_vfio ( $ pciid ) ;
2014-06-29 23:41:37 +04:00
die "can't reset pci device '$pciid'\n" if $ info - > { has_fl_reset } and ! pci_dev_reset ( $ info ) ;
2014-06-23 19:41:55 +04:00
}
2011-09-11 10:59:59 +04:00
}
2011-08-23 09:47:04 +04:00
PVE::Storage:: activate_volumes ( $ storecfg , $ vollist ) ;
2016-04-21 16:18:28 +03:00
if ( ! check_running ( $ vmid , 1 ) && - d "/sys/fs/cgroup/systemd/qemu.slice/$vmid.scope" ) {
my $ cmd = [] ;
push @$ cmd , '/bin/systemctl' , 'stop' , "$vmid.scope" ;
eval { run_command ( $ cmd ) ; } ;
}
2016-06-03 12:10:01 +03:00
my $ cpuunits = defined ( $ conf - > { cpuunits } ) ? $ conf - > { cpuunits }
: $ defaults - > { cpuunits } ;
2016-06-15 06:04:02 +03:00
my % run_params = ( timeout = > $ statefile ? undef : 30 , umask = > 0077 ) ;
my % properties = (
Slice = > 'qemu.slice' ,
KillMode = > 'none' ,
CPUShares = > $ cpuunits
) ;
if ( my $ cpulimit = $ conf - > { cpulimit } ) {
$ properties { CPUQuota } = int ( $ cpulimit * 100 ) ;
}
$ properties { timeout } = 10 if $ statefile ; # setting up the scope shoul be quick
if ( $ conf - > { hugepages } ) {
my $ code = sub {
my $ hugepages_topology = PVE::QemuServer::Memory:: hugepages_topology ( $ conf ) ;
my $ hugepages_host_topology = PVE::QemuServer::Memory:: hugepages_host_topology ( ) ;
PVE::QemuServer::Memory:: hugepages_mount ( ) ;
PVE::QemuServer::Memory:: hugepages_allocate ( $ hugepages_topology , $ hugepages_host_topology ) ;
eval {
PVE::Tools:: enter_systemd_scope ( $ vmid , "Proxmox VE VM $vmid" , % properties ) ;
run_command ( $ cmd , % run_params ) ;
} ;
if ( my $ err = $@ ) {
PVE::QemuServer::Memory:: hugepages_reset ( $ hugepages_host_topology ) ;
die $ err ;
}
PVE::QemuServer::Memory:: hugepages_pre_deallocate ( $ hugepages_topology ) ;
} ;
eval { PVE::QemuServer::Memory:: hugepages_update_locked ( $ code ) ; } ;
} else {
eval {
PVE::Tools:: enter_systemd_scope ( $ vmid , "Proxmox VE VM $vmid" , % properties ) ;
run_command ( $ cmd , % run_params ) ;
} ;
}
2016-02-19 13:13:01 +03:00
if ( my $ err = $@ ) {
# deactivate volumes if start fails
eval { PVE::Storage:: deactivate_volumes ( $ storecfg , $ vollist ) ; } ;
die "start failed: $err" ;
}
2011-08-23 09:47:04 +04:00
qemu-server: add support for unsecure migration (setting in datacenter.cfg)
This patch adds support for unsecure migration using a direct tcp connection
KVM <=> KVM instead of an extra SSH tunnel. Without ssh the limit is just the
bandwith and no longer the CPU / one single core.
You can enable this by adding:
migration_unsecure: 1
to datacenter.cfg
Examples using qemu 1.4 as migration with qemu 1.3 still does not work for me:
current default with SSH Tunnel VM uses 2GB mem:
Dec 27 21:10:32 starting migration of VM 105 to node 'cloud1-1202' (10.255.0.20)
Dec 27 21:10:32 copying disk images
Dec 27 21:10:32 starting VM 105 on remote node 'cloud1-1202'
Dec 27 21:10:35 starting ssh migration tunnel
Dec 27 21:10:36 starting online/live migration on localhost:60000
Dec 27 21:10:36 migrate_set_speed: 8589934592
Dec 27 21:10:36 migrate_set_downtime: 1
Dec 27 21:10:38 migration status: active (transferred 152481002, remaining 1938546688), total 2156396544) , expected downtime 0
Dec 27 21:10:40 migration status: active (transferred 279836995, remaining 1811140608), total 2156396544) , expected downtime 0
Dec 27 21:10:42 migration status: active (transferred 421265271, remaining 1669840896), total 2156396544) , expected downtime 0
Dec 27 21:10:44 migration status: active (transferred 570987974, remaining 1520152576), total 2156396544) , expected downtime 0
Dec 27 21:10:46 migration status: active (transferred 721469404, remaining 1369939968), total 2156396544) , expected downtime 0
Dec 27 21:10:48 migration status: active (transferred 875595258, remaining 1216057344), total 2156396544) , expected downtime 0
Dec 27 21:10:50 migration status: active (transferred 1034654822, remaining 1056931840), total 2156396544) , expected downtime 0
Dec 27 21:10:54 migration status: active (transferred 1176288424, remaining 915369984), total 2156396544) , expected downtime 0
Dec 27 21:10:56 migration status: active (transferred 1339734759, remaining 752050176), total 2156396544) , expected downtime 0
Dec 27 21:10:58 migration status: active (transferred 1503743261, remaining 588206080), total 2156396544) , expected downtime 0
Dec 27 21:11:02 migration status: active (transferred 1645097827, remaining 446906368), total 2156396544) , expected downtime 0
Dec 27 21:11:04 migration status: active (transferred 1810562934, remaining 281751552), total 2156396544) , expected downtime 0
Dec 27 21:11:06 migration status: active (transferred 1964377505, remaining 126033920), total 2156396544) , expected downtime 0
Dec 27 21:11:08 migration status: active (transferred 2077930417, remaining 0), total 2156396544) , expected downtime 0
Dec 27 21:11:09 migration speed: 62.06 MB/s - downtime 37 ms
Dec 27 21:11:09 migration status: completed
Dec 27 21:11:13 migration finished successfuly (duration 00:00:41)
TASK OK
with unsecure migration without SSH Tunnel:
Dec 27 22:43:14 starting migration of VM 105 to node 'cloud1-1203' (10.255.0.22)
Dec 27 22:43:14 copying disk images
Dec 27 22:43:14 starting VM 105 on remote node 'cloud1-1203'
Dec 27 22:43:17 starting online/live migration on 10.255.0.22:60000
Dec 27 22:43:17 migrate_set_speed: 8589934592
Dec 27 22:43:17 migrate_set_downtime: 1
Dec 27 22:43:19 migration speed: 1024.00 MB/s - downtime 1100 ms
Dec 27 22:43:19 migration status: completed
Dec 27 22:43:22 migration finished successfuly (duration 00:00:09)
TASK OK
2013-07-26 13:22:58 +04:00
print "migration listens on $migrate_uri\n" if $ migrate_uri ;
2012-01-27 12:35:26 +04:00
2012-09-19 14:37:33 +04:00
if ( $ statefile && $ statefile ne 'tcp' ) {
2012-12-20 15:41:03 +04:00
eval { vm_mon_cmd_nocheck ( $ vmid , "cont" ) ; } ;
2012-09-19 14:37:33 +04:00
warn $@ if $@ ;
2012-09-19 12:40:30 +04:00
}
2013-07-24 13:24:20 +04:00
if ( $ migratedfrom ) {
2014-01-09 13:58:48 +04:00
eval {
2014-11-19 12:43:42 +03:00
set_migration_caps ( $ vmid ) ;
2014-01-09 13:58:48 +04:00
} ;
2013-07-24 13:24:20 +04:00
warn $@ if $@ ;
2014-01-09 13:58:48 +04:00
2013-07-24 13:24:20 +04:00
if ( $ spice_port ) {
print "spice listens on port $spice_port\n" ;
if ( $ spice_ticket ) {
2014-11-19 12:43:42 +03:00
vm_mon_cmd_nocheck ( $ vmid , "set_password" , protocol = > 'spice' , password = > $ spice_ticket ) ;
vm_mon_cmd_nocheck ( $ vmid , "expire_password" , protocol = > 'spice' , time = > "+30" ) ;
2013-07-24 11:52:33 +04:00
}
}
2013-07-24 13:24:20 +04:00
} else {
2013-01-04 14:05:23 +04:00
2013-04-29 16:13:57 +04:00
if ( ! $ statefile && ( ! defined ( $ conf - > { balloon } ) || $ conf - > { balloon } ) ) {
2013-07-15 11:13:31 +04:00
vm_mon_cmd_nocheck ( $ vmid , "balloon" , value = > $ conf - > { balloon } * 1024 * 1024 )
2013-01-04 14:05:23 +04:00
if $ conf - > { balloon } ;
}
2015-01-20 13:47:11 +03:00
foreach my $ opt ( keys %$ conf ) {
next if $ opt !~ m/^net\d+$/ ;
my $ nicconf = parse_net ( $ conf - > { $ opt } ) ;
qemu_set_link_status ( $ vmid , $ opt , 0 ) if $ nicconf - > { link_down } ;
}
2012-08-28 14:46:08 +04:00
}
2015-03-27 08:16:24 +03:00
2015-03-09 10:22:12 +03:00
vm_mon_cmd_nocheck ( $ vmid , 'qom-set' ,
path = > "machine/peripheral/balloon0" ,
property = > "guest-stats-polling-interval" ,
value = > 2 ) if ( ! defined ( $ conf - > { balloon } ) || $ conf - > { balloon } ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
2012-06-25 12:02:47 +04:00
sub vm_mon_cmd {
my ( $ vmid , $ execute , % params ) = @ _ ;
2012-07-13 09:06:22 +04:00
my $ cmd = { execute = > $ execute , arguments = > \ % params } ;
vm_qmp_command ( $ vmid , $ cmd ) ;
2012-06-25 12:02:47 +04:00
}
sub vm_mon_cmd_nocheck {
my ( $ vmid , $ execute , % params ) = @ _ ;
2012-07-13 09:06:22 +04:00
my $ cmd = { execute = > $ execute , arguments = > \ % params } ;
vm_qmp_command ( $ vmid , $ cmd , 1 ) ;
2012-06-25 12:02:47 +04:00
}
2012-05-29 16:01:50 +04:00
sub vm_qmp_command {
2014-11-26 13:11:39 +03:00
my ( $ vmid , $ cmd , $ nocheck ) = @ _ ;
2012-05-30 14:08:33 +04:00
2012-05-29 16:01:50 +04:00
my $ res ;
2012-07-13 09:06:22 +04:00
2012-08-27 15:13:36 +04:00
my $ timeout ;
if ( $ cmd - > { arguments } && $ cmd - > { arguments } - > { timeout } ) {
$ timeout = $ cmd - > { arguments } - > { timeout } ;
delete $ cmd - > { arguments } - > { timeout } ;
}
2013-07-15 11:13:31 +04:00
2012-05-29 16:01:50 +04:00
eval {
die "VM $vmid not running\n" if ! check_running ( $ vmid , $ nocheck ) ;
2014-11-27 13:56:52 +03:00
my $ sname = qmp_socket ( $ vmid ) ;
if ( - e $ sname ) { # test if VM is reasonambe new and supports qmp/qga
2014-11-26 13:11:39 +03:00
my $ qmpclient = PVE::QMPClient - > new ( ) ;
2012-08-17 12:34:39 +04:00
2012-08-27 15:13:36 +04:00
$ res = $ qmpclient - > cmd ( $ vmid , $ cmd , $ timeout ) ;
2014-11-26 13:11:39 +03:00
} elsif ( - e "${var_run_tmpdir}/$vmid.mon" ) {
2012-08-17 12:34:39 +04:00
die "can't execute complex command on old monitor - stop/start your vm to fix the problem\n"
if scalar ( % { $ cmd - > { arguments } } ) ;
vm_monitor_command ( $ vmid , $ cmd - > { execute } , $ nocheck ) ;
} else {
die "unable to open monitor socket\n" ;
}
2012-05-29 16:01:50 +04:00
} ;
2012-07-13 09:06:22 +04:00
if ( my $ err = $@ ) {
2012-05-29 16:01:50 +04:00
syslog ( "err" , "VM $vmid qmp command failed - $err" ) ;
die $ err ;
}
return $ res ;
}
2012-07-13 09:30:13 +04:00
sub vm_human_monitor_command {
my ( $ vmid , $ cmdline ) = @ _ ;
my $ res ;
2012-08-23 09:36:48 +04:00
my $ cmd = {
2012-07-13 09:30:13 +04:00
execute = > 'human-monitor-command' ,
arguments = > { 'command-line' = > $ cmdline } ,
} ;
return vm_qmp_command ( $ vmid , $ cmd ) ;
}
2011-08-23 09:47:04 +04:00
sub vm_commandline {
my ( $ storecfg , $ vmid ) = @ _ ;
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > 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 ) = @ _ ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > check_lock ( $ conf ) if ! $ skiplock ;
2011-08-23 09:47:04 +04:00
2012-06-25 12:02:52 +04:00
vm_mon_cmd ( $ vmid , "system_reset" ) ;
2011-11-25 11:05:36 +04:00
} ) ;
}
sub get_vm_volumes {
my ( $ conf ) = @ _ ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
my $ vollist = [] ;
2012-09-25 09:42:01 +04:00
foreach_volid ( $ conf , sub {
my ( $ volid , $ is_cdrom ) = @ _ ;
2011-11-25 11:05:36 +04:00
2012-09-25 09:42:01 +04:00
return if $ volid =~ m | ^ / | ;
2011-11-25 11:05:36 +04:00
2012-09-25 09:42:01 +04:00
my ( $ sid , $ volname ) = PVE::Storage:: parse_volume_id ( $ volid , 1 ) ;
return if ! $ sid ;
2011-11-25 11:05:36 +04:00
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 {
2015-01-21 12:42:43 +03:00
my ( $ storecfg , $ vmid , $ conf , $ keepActive , $ apply_pending_changes ) = @ _ ;
2011-11-25 11:05:36 +04:00
2011-12-16 09:34:35 +04:00
eval {
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 ) ;
}
2015-03-27 08:16:24 +03:00
2012-09-03 11:51:08 +04:00
foreach my $ ext ( qw( mon qmp pid vnc qga ) ) {
2012-03-01 15:54:06 +04:00
unlink "/var/run/qemu-server/${vmid}.$ext" ;
}
2015-03-27 08:16:24 +03:00
2015-01-21 12:42:43 +03:00
vmconfig_apply_pending ( $ vmid , $ conf , $ storecfg ) if $ apply_pending_changes ;
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-08-23 12:28:41 +04:00
my ( $ storecfg , $ vmid , $ skiplock , $ nocheck , $ timeout , $ shutdown , $ force , $ keepActive , $ migratedfrom ) = @ _ ;
2011-12-15 15:47:39 +04:00
$ force = 1 if ! defined ( $ force ) && ! $ shutdown ;
2011-08-23 09:47:04 +04:00
2012-08-23 12:28:41 +04:00
if ( $ migratedfrom ) {
my $ pid = check_running ( $ vmid , $ nocheck , $ migratedfrom ) ;
kill 15 , $ pid if $ pid ;
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid , $ migratedfrom ) ;
2015-01-21 12:42:43 +03:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf , $ keepActive , 0 ) ;
2012-08-23 12:28:41 +04:00
return ;
}
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > 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 ) {
2016-03-07 14:41:12 +03:00
$ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
PVE::QemuConfig - > check_lock ( $ conf ) if ! $ skiplock ;
2012-08-21 13:39:17 +04:00
if ( ! defined ( $ timeout ) && $ shutdown && $ conf - > { startup } ) {
2015-07-16 14:46:27 +03:00
my $ opts = PVE::JSONSchema:: pve_parse_startup_order ( $ conf - > { startup } ) ;
2012-08-21 13:39:17 +04:00
$ timeout = $ opts - > { down } if $ opts - > { down } ;
}
2011-09-15 10:21:32 +04:00
}
2011-09-12 14:26:00 +04:00
2012-08-21 13:39:17 +04:00
$ timeout = 60 if ! defined ( $ timeout ) ;
2014-12-04 15:07:59 +03:00
2011-12-15 15:47:39 +04:00
eval {
if ( $ shutdown ) {
2014-12-29 12:56:07 +03:00
if ( defined ( $ conf ) && $ conf - > { agent } ) {
2014-11-27 15:29:51 +03:00
vm_qmp_command ( $ vmid , { execute = > "guest-shutdown" } , $ nocheck ) ;
2014-11-26 13:11:40 +03:00
} else {
2014-11-27 15:29:51 +03:00
vm_qmp_command ( $ vmid , { execute = > "system_powerdown" } , $ nocheck ) ;
2014-11-26 13:11:40 +03:00
}
2011-12-15 15:47:39 +04:00
} else {
2014-11-27 15:29:51 +03:00
vm_qmp_command ( $ vmid , { execute = > "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 {
2015-01-21 12:42:43 +03:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf , $ keepActive , 1 ) 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
}
2015-01-21 12:42:43 +03:00
vm_stop_cleanup ( $ storecfg , $ vmid , $ conf , $ keepActive , 1 ) if $ conf ;
2011-11-25 11:05:36 +04:00
} ) ;
2011-08-23 09:47:04 +04:00
}
sub vm_suspend {
my ( $ vmid , $ skiplock ) = @ _ ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2016-03-11 14:11:57 +03:00
PVE::QemuConfig - > check_lock ( $ conf )
if ! ( $ skiplock || PVE::QemuConfig - > has_lock ( $ conf , 'backup' ) ) ;
2012-06-26 08:42:18 +04:00
2012-06-25 12:02:48 +04:00
vm_mon_cmd ( $ vmid , "stop" ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
sub vm_resume {
2015-10-14 12:06:06 +03:00
my ( $ vmid , $ skiplock , $ nocheck ) = @ _ ;
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2015-10-14 12:06:06 +03:00
if ( ! $ nocheck ) {
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2016-03-11 14:11:57 +03:00
PVE::QemuConfig - > check_lock ( $ conf )
if ! ( $ skiplock || PVE::QemuConfig - > has_lock ( $ conf , 'backup' ) ) ;
2015-10-14 12:06:06 +03:00
vm_mon_cmd ( $ vmid , "cont" ) ;
} else {
vm_mon_cmd_nocheck ( $ 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
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2012-08-23 09:36:48 +04:00
2012-07-13 10:56:13 +04:00
# there is no qmp command, so we use the human monitor command
vm_human_monitor_command ( $ vmid , "sendkey $key" ) ;
2011-08-23 09:47:04 +04:00
} ) ;
}
sub vm_destroy {
my ( $ storecfg , $ vmid , $ skiplock ) = @ _ ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , sub {
2011-08-23 09:47:04 +04:00
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2011-08-23 09:47:04 +04:00
2011-11-25 11:05:36 +04:00
if ( ! check_running ( $ vmid ) ) {
2016-02-05 10:46:35 +03:00
destroy_vm ( $ storecfg , $ vmid , undef , $ skiplock ) ;
2011-11-25 11:05:36 +04:00
} else {
die "VM $vmid is running - destroy failed\n" ;
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
}
2014-02-14 00:12:29 +04:00
sub pci_dev_bind_to_vfio {
my ( $ dev ) = @ _ ;
my $ name = $ dev - > { name } ;
my $ vfio_basedir = "$pcisysfs/drivers/vfio-pci" ;
if ( ! - d $ vfio_basedir ) {
system ( "/sbin/modprobe vfio-pci >/dev/null 2>/dev/null" ) ;
}
die "Cannot find vfio-pci module!\n" if ! - d $ vfio_basedir ;
my $ testdir = "$vfio_basedir/$name" ;
return 1 if - d $ testdir ;
my $ data = "$dev->{vendor} $dev->{product}" ;
return undef if ! file_write ( "$vfio_basedir/new_id" , $ data ) ;
my $ fn = "$pcisysfs/devices/$name/driver/unbind" ;
if ( ! file_write ( $ fn , $ name ) ) {
return undef if - f $ fn ;
}
$ fn = "$vfio_basedir/bind" ;
if ( ! - d $ testdir ) {
return undef if ! file_write ( $ fn , $ name ) ;
}
return - d $ testdir ;
}
sub pci_dev_group_bind_to_vfio {
my ( $ pciid ) = @ _ ;
my $ vfio_basedir = "$pcisysfs/drivers/vfio-pci" ;
if ( ! - d $ vfio_basedir ) {
system ( "/sbin/modprobe vfio-pci >/dev/null 2>/dev/null" ) ;
}
die "Cannot find vfio-pci module!\n" if ! - d $ vfio_basedir ;
# get IOMMU group devices
opendir ( my $ D , "$pcisysfs/devices/0000:$pciid/iommu_group/devices/" ) || die "Cannot open iommu_group: $!\n" ;
my @ devs = grep /^0000:/ , readdir ( $ D ) ;
closedir ( $ D ) ;
foreach my $ pciid ( @ devs ) {
$ pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n" ;
2015-02-01 22:46:46 +03:00
# pci bridges, switches or root ports are not supported
# they have a pci_bus subdirectory so skip them
next if ( - e "$pcisysfs/devices/$pciid/pci_bus" ) ;
2014-02-14 00:12:29 +04:00
my $ info = pci_device_info ( $ 1 ) ;
pci_dev_bind_to_vfio ( $ info ) || die "Cannot bind $pciid to vfio\n" ;
}
return 1 ;
}
2012-01-27 12:35:26 +04:00
sub print_pci_addr {
2012-08-20 13:10:24 +04:00
my ( $ id , $ bridges ) = @ _ ;
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 = {
2013-02-22 14:39:13 +04:00
piix3 = > { bus = > 0 , addr = > 1 } ,
2011-10-05 05:15:08 +04:00
#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 } ,
2015-03-27 08:16:24 +03:00
scsihw0 = > { bus = > 0 , addr = > 5 } ,
2015-03-27 05:41:52 +03:00
'pci.3' = > { bus = > 0 , addr = > 5 } , #can also be used for virtio-scsi-single bridge
2012-07-30 16:58:40 +04:00
scsihw1 = > { bus = > 0 , addr = > 6 } ,
2012-02-01 16:25:20 +04:00
ahci0 = > { bus = > 0 , addr = > 7 } ,
2012-09-03 11:51:08 +04:00
qga0 = > { bus = > 0 , addr = > 8 } ,
2013-06-25 09:10:42 +04:00
spice = > { bus = > 0 , addr = > 9 } ,
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 } ,
add multi-monitors spice support
add qxl2 (2monitors),qxl3 (3monitors),qxl4 (4monitors) vga type.
For linux, we only need 1 qxl card with more memory
For windows, we need 1 qxl card by monitor
Original Information from spice-mailing
"
You need to specify multiple devices for Windows VMs. This is what
libvirt gives me (via 'virsh domxml-to-native qemu argv DOMAIN_XML'):
<...> -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=33554432 -device qxl,id=video1,ram_size=67108864,vram_size=33554432 -device qxl,id=video2,ram_size=67108864,vram_size=33554432 -device qxl,id=video3,ram_size=67108864,vram_size=33554432
For Linux VM, just one qxl device is OK but then it's advisable to
increase the available RAM:
<...> -vga qxl -global qxl-vga.ram_size=134217728 -global qxl-vga.vram_size=33554432
If you don't turn off surfaces, then you should increase vram size to
say 64 MB from current default of 32 MB.
"
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2013-09-25 17:33:08 +04:00
vga1 = > { bus = > 0 , addr = > 24 } ,
vga2 = > { bus = > 0 , addr = > 25 } ,
vga3 = > { bus = > 0 , addr = > 26 } ,
2014-08-06 09:39:33 +04:00
hostpci2 = > { bus = > 0 , addr = > 27 } ,
hostpci3 = > { bus = > 0 , addr = > 28 } ,
2011-10-05 05:15:08 +04:00
#addr29 : usb-host (pve-usb.cfg)
2012-08-20 13:10:24 +04:00
'pci.1' = > { bus = > 0 , addr = > 30 } ,
'pci.2' = > { bus = > 0 , addr = > 31 } ,
'net6' = > { bus = > 1 , addr = > 1 } ,
'net7' = > { bus = > 1 , addr = > 2 } ,
'net8' = > { bus = > 1 , addr = > 3 } ,
'net9' = > { bus = > 1 , addr = > 4 } ,
'net10' = > { bus = > 1 , addr = > 5 } ,
'net11' = > { bus = > 1 , addr = > 6 } ,
'net12' = > { bus = > 1 , addr = > 7 } ,
'net13' = > { bus = > 1 , addr = > 8 } ,
'net14' = > { bus = > 1 , addr = > 9 } ,
'net15' = > { bus = > 1 , addr = > 10 } ,
'net16' = > { bus = > 1 , addr = > 11 } ,
'net17' = > { bus = > 1 , addr = > 12 } ,
'net18' = > { bus = > 1 , addr = > 13 } ,
'net19' = > { bus = > 1 , addr = > 14 } ,
'net20' = > { bus = > 1 , addr = > 15 } ,
'net21' = > { bus = > 1 , addr = > 16 } ,
'net22' = > { bus = > 1 , addr = > 17 } ,
'net23' = > { bus = > 1 , addr = > 18 } ,
'net24' = > { bus = > 1 , addr = > 19 } ,
'net25' = > { bus = > 1 , addr = > 20 } ,
'net26' = > { bus = > 1 , addr = > 21 } ,
'net27' = > { bus = > 1 , addr = > 22 } ,
'net28' = > { bus = > 1 , addr = > 23 } ,
'net29' = > { bus = > 1 , addr = > 24 } ,
'net30' = > { bus = > 1 , addr = > 25 } ,
'net31' = > { bus = > 1 , addr = > 26 } ,
2016-02-10 14:52:12 +03:00
'xhci' = > { bus = > 1 , addr = > 27 } ,
2012-08-20 13:10:24 +04:00
'virtio6' = > { bus = > 2 , addr = > 1 } ,
'virtio7' = > { bus = > 2 , addr = > 2 } ,
'virtio8' = > { bus = > 2 , addr = > 3 } ,
'virtio9' = > { bus = > 2 , addr = > 4 } ,
'virtio10' = > { bus = > 2 , addr = > 5 } ,
'virtio11' = > { bus = > 2 , addr = > 6 } ,
'virtio12' = > { bus = > 2 , addr = > 7 } ,
'virtio13' = > { bus = > 2 , addr = > 8 } ,
'virtio14' = > { bus = > 2 , addr = > 9 } ,
'virtio15' = > { bus = > 2 , addr = > 10 } ,
2015-03-27 05:41:52 +03:00
'virtioscsi0' = > { bus = > 3 , addr = > 1 } ,
'virtioscsi1' = > { bus = > 3 , addr = > 2 } ,
'virtioscsi2' = > { bus = > 3 , addr = > 3 } ,
'virtioscsi3' = > { bus = > 3 , addr = > 4 } ,
'virtioscsi4' = > { bus = > 3 , addr = > 5 } ,
'virtioscsi5' = > { bus = > 3 , addr = > 6 } ,
'virtioscsi6' = > { bus = > 3 , addr = > 7 } ,
'virtioscsi7' = > { bus = > 3 , addr = > 8 } ,
'virtioscsi8' = > { bus = > 3 , addr = > 9 } ,
'virtioscsi9' = > { bus = > 3 , addr = > 10 } ,
'virtioscsi10' = > { bus = > 3 , addr = > 11 } ,
'virtioscsi11' = > { bus = > 3 , addr = > 12 } ,
'virtioscsi12' = > { bus = > 3 , addr = > 13 } ,
'virtioscsi13' = > { bus = > 3 , addr = > 14 } ,
'virtioscsi14' = > { bus = > 3 , addr = > 15 } ,
'virtioscsi15' = > { bus = > 3 , addr = > 16 } ,
'virtioscsi16' = > { bus = > 3 , addr = > 17 } ,
'virtioscsi17' = > { bus = > 3 , addr = > 18 } ,
'virtioscsi18' = > { bus = > 3 , addr = > 19 } ,
'virtioscsi19' = > { bus = > 3 , addr = > 20 } ,
'virtioscsi20' = > { bus = > 3 , addr = > 21 } ,
'virtioscsi21' = > { bus = > 3 , addr = > 22 } ,
'virtioscsi22' = > { bus = > 3 , addr = > 23 } ,
'virtioscsi23' = > { bus = > 3 , addr = > 24 } ,
'virtioscsi24' = > { bus = > 3 , addr = > 25 } ,
'virtioscsi25' = > { bus = > 3 , addr = > 26 } ,
'virtioscsi26' = > { bus = > 3 , addr = > 27 } ,
'virtioscsi27' = > { bus = > 3 , addr = > 28 } ,
'virtioscsi28' = > { bus = > 3 , addr = > 29 } ,
'virtioscsi29' = > { bus = > 3 , addr = > 30 } ,
'virtioscsi30' = > { bus = > 3 , addr = > 31 } ,
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 } ) ;
2012-08-20 13:10:24 +04:00
my $ bus = $ devices - > { $ id } - > { bus } ;
$ res = ",bus=pci.$bus,addr=$addr" ;
2012-08-20 15:47:56 +04:00
$ bridges - > { $ bus } = 1 if $ bridges ;
2011-09-15 08:47:39 +04:00
}
return $ res ;
}
2014-06-23 19:41:53 +04:00
sub print_pcie_addr {
my ( $ id ) = @ _ ;
my $ res = '' ;
my $ devices = {
hostpci0 = > { bus = > "ich9-pcie-port-1" , addr = > 0 } ,
hostpci1 = > { bus = > "ich9-pcie-port-2" , addr = > 0 } ,
hostpci2 = > { bus = > "ich9-pcie-port-3" , addr = > 0 } ,
hostpci3 = > { bus = > "ich9-pcie-port-4" , addr = > 0 } ,
} ;
if ( defined ( $ devices - > { $ id } - > { bus } ) && defined ( $ devices - > { $ id } - > { addr } ) ) {
my $ addr = sprintf ( "0x%x" , $ devices - > { $ id } - > { addr } ) ;
my $ bus = $ devices - > { $ id } - > { bus } ;
$ res = ",bus=$bus,addr=$addr" ;
}
return $ res ;
}
2011-10-17 15:49:48 +04:00
# vzdump restore implementaion
2013-01-04 09:57:11 +04:00
sub tar_archive_read_firstfile {
2011-10-17 15:49:48 +04:00
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
2016-06-09 17:54:46 +03:00
my $ pid = open ( my $ fh , '-|' , 'tar' , 'tf' , $ archive ) ||
2011-10-17 15:49:48 +04:00
die "unable to open file '$archive'\n" ;
2016-06-09 17:54:46 +03:00
my $ firstfile = <$fh> ;
2011-10-17 15:49:48 +04:00
kill 15 , $ pid ;
2016-06-09 17:54:46 +03:00
close $ fh ;
2011-10-17 15:49:48 +04:00
die "ERROR: archive contaions no data\n" if ! $ firstfile ;
chomp $ firstfile ;
return $ firstfile ;
}
2013-01-04 09:57:11 +04:00
sub tar_restore_cleanup {
my ( $ storecfg , $ statfile ) = @ _ ;
2011-10-17 15:49:48 +04:00
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 {
2013-01-04 09:57:11 +04:00
PVE::Storage:: vdisk_free ( $ storecfg , $ volid ) ;
2011-10-17 15:49:48 +04:00
}
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
2012-12-12 18:35:26 +04:00
my $ format = $ opts - > { format } ;
my $ comp ;
if ( $ archive =~ m/\.tgz$/ || $ archive =~ m/\.tar\.gz$/ ) {
$ format = 'tar' if ! $ format ;
$ comp = 'gzip' ;
} elsif ( $ archive =~ m/\.tar$/ ) {
$ format = 'tar' if ! $ format ;
} elsif ( $ archive =~ m/.tar.lzo$/ ) {
$ format = 'tar' if ! $ format ;
$ comp = 'lzop' ;
} elsif ( $ archive =~ m/\.vma$/ ) {
$ format = 'vma' if ! $ format ;
} elsif ( $ archive =~ m/\.vma\.gz$/ ) {
$ format = 'vma' if ! $ format ;
$ comp = 'gzip' ;
} elsif ( $ archive =~ m/\.vma\.lzo$/ ) {
$ format = 'vma' if ! $ format ;
$ comp = 'lzop' ;
} else {
$ format = 'vma' if ! $ format ; # default
}
# try to detect archive format
if ( $ format eq 'tar' ) {
return restore_tar_archive ( $ archive , $ vmid , $ user , $ opts ) ;
} else {
return restore_vma_archive ( $ archive , $ vmid , $ user , $ opts , $ comp ) ;
}
}
sub restore_update_config_line {
my ( $ outfd , $ cookie , $ vmid , $ map , $ line , $ unique ) = @ _ ;
return if $ line =~ m/^\#qmdump\#/ ;
return if $ line =~ m/^\#vzdump\#/ ;
return if $ line =~ m/^lock:/ ;
return if $ line =~ m/^unused\d+:/ ;
return if $ line =~ m/^parent:/ ;
2013-05-08 12:19:38 +04:00
return if $ line =~ m/^template:/ ; # restored VM is never a template
2012-12-12 18:35:26 +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 || $ unique ;
my $ net = {
model = > $ model ,
bridge = > "vmbr$ind" ,
macaddr = > $ macaddr ,
} ;
my $ netstr = print_net ( $ net ) ;
print $ outfd "net$cookie->{netcount}: $netstr\n" ;
$ cookie - > { netcount } + + ;
}
} elsif ( ( $ line =~ m/^(net\d+):\s*(\S+)\s*$/ ) && $ 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 ) ;
print $ outfd "$id: $netstr\n" ;
} elsif ( $ line =~ m/^((ide|scsi|virtio|sata)\d+):\s*(\S+)\s*$/ ) {
my $ virtdev = $ 1 ;
2013-01-07 09:49:11 +04:00
my $ value = $ 3 ;
2016-02-10 16:28:18 +03:00
my $ di = parse_drive ( $ virtdev , $ value ) ;
if ( defined ( $ di - > { backup } ) && ! $ di - > { backup } ) {
2012-12-12 18:35:26 +04:00
print $ outfd "#$line" ;
2016-02-10 16:28:19 +03:00
} elsif ( $ map - > { $ virtdev } ) {
2013-05-21 14:02:41 +04:00
delete $ di - > { format } ; # format can change on restore
2012-12-12 18:35:26 +04:00
$ di - > { file } = $ map - > { $ virtdev } ;
2013-01-04 09:57:11 +04:00
$ value = print_drive ( $ vmid , $ di ) ;
2012-12-12 18:35:26 +04:00
print $ outfd "$virtdev: $value\n" ;
} else {
print $ outfd $ line ;
}
} else {
print $ outfd $ line ;
}
}
sub scan_volids {
my ( $ cfg , $ vmid ) = @ _ ;
my $ info = PVE::Storage:: vdisk_list ( $ cfg , undef , $ vmid ) ;
my $ volid_hash = { } ;
foreach my $ storeid ( keys %$ info ) {
foreach my $ item ( @ { $ info - > { $ storeid } } ) {
next if ! ( $ item - > { volid } && $ item - > { size } ) ;
2013-05-27 10:25:39 +04:00
$ item - > { path } = PVE::Storage:: path ( $ cfg , $ item - > { volid } ) ;
2012-12-12 18:35:26 +04:00
$ volid_hash - > { $ item - > { volid } } = $ item ;
}
}
return $ volid_hash ;
}
2015-12-10 14:07:39 +03:00
sub is_volume_in_use {
my ( $ storecfg , $ conf , $ skip_drive , $ volid ) = @ _ ;
2013-05-28 14:02:29 +04:00
2015-12-10 14:07:39 +03:00
my $ path = PVE::Storage:: path ( $ storecfg , $ volid ) ;
2013-05-28 14:02:29 +04:00
my $ scan_config = sub {
my ( $ cref , $ snapname ) = @ _ ;
foreach my $ key ( keys %$ cref ) {
my $ value = $ cref - > { $ key } ;
2016-03-03 17:45:15 +03:00
if ( is_valid_drivename ( $ key ) ) {
2013-05-28 14:02:29 +04:00
next if $ skip_drive && $ key eq $ skip_drive ;
my $ drive = parse_drive ( $ key , $ value ) ;
next if ! $ drive || ! $ drive - > { file } || drive_is_cdrom ( $ drive ) ;
2015-12-10 14:07:39 +03:00
return 1 if $ volid eq $ drive - > { file } ;
2013-05-28 14:02:29 +04:00
if ( $ drive - > { file } =~ m !^/! ) {
2015-12-10 14:07:39 +03:00
return 1 if $ drive - > { file } eq $ path ;
2013-05-28 14:02:29 +04:00
} else {
my ( $ storeid , $ volname ) = PVE::Storage:: parse_volume_id ( $ drive - > { file } , 1 ) ;
next if ! $ storeid ;
my $ scfg = PVE::Storage:: storage_config ( $ storecfg , $ storeid , 1 ) ;
next if ! $ scfg ;
2015-12-10 14:07:39 +03:00
return 1 if $ path eq PVE::Storage:: path ( $ storecfg , $ drive - > { file } , $ snapname ) ;
2013-05-28 14:02:29 +04:00
}
}
}
2015-12-10 14:07:39 +03:00
return 0 ;
2013-05-28 14:02:29 +04:00
} ;
2015-12-10 14:07:39 +03:00
return 1 if & $ scan_config ( $ conf ) ;
2013-05-28 14:02:29 +04:00
undef $ skip_drive ;
2015-12-10 14:07:39 +03:00
foreach my $ snapname ( keys % { $ conf - > { snapshots } } ) {
return 1 if & $ scan_config ( $ conf - > { snapshots } - > { $ snapname } , $ snapname ) ;
2013-05-28 14:02:29 +04:00
}
2015-12-10 14:07:39 +03:00
return 0 ;
2013-05-28 14:02:29 +04:00
}
2012-12-12 18:35:26 +04:00
sub update_disksize {
my ( $ vmid , $ conf , $ volid_hash ) = @ _ ;
2013-07-15 11:13:31 +04:00
2012-12-12 18:35:26 +04:00
my $ changes ;
my $ used = { } ;
2013-05-27 10:25:39 +04:00
# Note: it is allowed to define multiple storages with same path (alias), so
# we need to check both 'volid' and real 'path' (two different volid can point
# to the same path).
my $ usedpath = { } ;
2013-07-15 11:13:31 +04:00
2012-12-12 18:35:26 +04:00
# update size info
foreach my $ opt ( keys %$ conf ) {
2016-03-03 17:45:15 +03:00
if ( is_valid_drivename ( $ opt ) ) {
2013-01-04 09:57:11 +04:00
my $ drive = parse_drive ( $ opt , $ conf - > { $ opt } ) ;
2012-12-12 18:35:26 +04:00
my $ volid = $ drive - > { file } ;
next if ! $ volid ;
$ used - > { $ volid } = 1 ;
2013-07-15 11:13:31 +04:00
if ( $ volid_hash - > { $ volid } &&
2013-05-27 10:25:39 +04:00
( my $ path = $ volid_hash - > { $ volid } - > { path } ) ) {
$ usedpath - > { $ path } = 1 ;
}
2012-12-12 18:35:26 +04:00
2013-01-04 09:57:11 +04:00
next if drive_is_cdrom ( $ drive ) ;
2012-12-12 18:35:26 +04:00
next if ! $ volid_hash - > { $ volid } ;
$ drive - > { size } = $ volid_hash - > { $ volid } - > { size } ;
2013-05-27 11:30:40 +04:00
my $ new = print_drive ( $ vmid , $ drive ) ;
if ( $ new ne $ conf - > { $ opt } ) {
$ changes = 1 ;
$ conf - > { $ opt } = $ new ;
}
2012-12-12 18:35:26 +04:00
}
}
2013-05-27 10:25:39 +04:00
# remove 'unusedX' entry if volume is used
foreach my $ opt ( keys %$ conf ) {
next if $ opt !~ m/^unused\d+$/ ;
my $ volid = $ conf - > { $ opt } ;
my $ path = $ volid_hash - > { $ volid } - > { path } if $ volid_hash - > { $ volid } ;
2013-07-15 11:13:31 +04:00
if ( $ used - > { $ volid } || ( $ path && $ usedpath - > { $ path } ) ) {
2013-05-27 10:25:39 +04:00
$ changes = 1 ;
delete $ conf - > { $ opt } ;
}
}
2012-12-12 18:35:26 +04:00
foreach my $ volid ( sort keys %$ volid_hash ) {
next if $ volid =~ m/vm-$vmid-state-/ ;
next if $ used - > { $ volid } ;
2013-05-27 10:25:39 +04:00
my $ path = $ volid_hash - > { $ volid } - > { path } ;
next if ! $ path ; # just to be sure
next if $ usedpath - > { $ path } ;
2012-12-12 18:35:26 +04:00
$ changes = 1 ;
2016-03-07 14:41:14 +03:00
PVE::QemuConfig - > add_unused_volume ( $ conf , $ volid ) ;
2013-05-28 14:21:14 +04:00
$ usedpath - > { $ path } = 1 ; # avoid to add more than once (aliases)
2012-12-12 18:35:26 +04:00
}
return $ changes ;
}
sub rescan {
my ( $ vmid , $ nolock ) = @ _ ;
2016-03-25 16:01:36 +03:00
my $ cfg = PVE::Storage:: config ( ) ;
2012-12-12 18:35:26 +04:00
my $ volid_hash = scan_volids ( $ cfg , $ vmid ) ;
my $ updatefn = sub {
my ( $ vmid ) = @ _ ;
2016-03-07 14:41:12 +03:00
my $ conf = PVE::QemuConfig - > load_config ( $ vmid ) ;
2013-07-15 11:13:31 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > check_lock ( $ conf ) ;
2012-12-12 18:35:26 +04:00
2013-04-19 12:45:46 +04:00
my $ vm_volids = { } ;
foreach my $ volid ( keys %$ volid_hash ) {
my $ info = $ volid_hash - > { $ volid } ;
$ vm_volids - > { $ volid } = $ info if $ info - > { vmid } && $ info - > { vmid } == $ vmid ;
}
my $ changes = update_disksize ( $ vmid , $ conf , $ vm_volids ) ;
2012-12-12 18:35:26 +04:00
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) if $ changes ;
2012-12-12 18:35:26 +04:00
} ;
if ( defined ( $ vmid ) ) {
if ( $ nolock ) {
& $ updatefn ( $ vmid ) ;
} else {
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , $ updatefn , $ vmid ) ;
2012-12-12 18:35:26 +04:00
}
} else {
my $ vmlist = config_list ( ) ;
foreach my $ vmid ( keys %$ vmlist ) {
if ( $ nolock ) {
& $ updatefn ( $ vmid ) ;
} else {
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > lock_config ( $ vmid , $ updatefn , $ vmid ) ;
2013-07-15 11:13:31 +04:00
}
2012-12-12 18:35:26 +04:00
}
}
}
sub restore_vma_archive {
my ( $ archive , $ vmid , $ user , $ opts , $ comp ) = @ _ ;
my $ input = $ archive eq '-' ? "<&STDIN" : undef ;
my $ readfrom = $ archive ;
my $ uncomp = '' ;
if ( $ comp ) {
$ readfrom = '-' ;
my $ qarchive = PVE::Tools:: shellquote ( $ archive ) ;
if ( $ comp eq 'gzip' ) {
$ uncomp = "zcat $qarchive|" ;
} elsif ( $ comp eq 'lzop' ) {
$ uncomp = "lzop -d -c $qarchive|" ;
} else {
die "unknown compression method '$comp'\n" ;
}
2013-07-15 11:13:31 +04:00
2012-12-12 18:35:26 +04:00
}
my $ tmpdir = "/var/tmp/vzdumptmp$$" ;
rmtree $ tmpdir ;
# disable interrupts (always do cleanups)
local $ SIG { INT } = $ SIG { TERM } = $ SIG { QUIT } = $ SIG { HUP } = sub {
warn "got interrupt - ignored\n" ;
} ;
my $ mapfifo = "/var/tmp/vzdumptmp$$.fifo" ;
POSIX:: mkfifo ( $ mapfifo , 0600 ) ;
my $ fifofh ;
my $ openfifo = sub {
open ( $ fifofh , '>' , $ mapfifo ) || die $! ;
} ;
my $ cmd = "${uncomp}vma extract -v -r $mapfifo $readfrom $tmpdir" ;
my $ oldtimeout ;
my $ timeout = 5 ;
my $ devinfo = { } ;
my $ rpcenv = PVE::RPCEnvironment:: get ( ) ;
2016-03-07 14:41:12 +03:00
my $ conffile = PVE::QemuConfig - > config_file ( $ vmid ) ;
2012-12-12 18:35:26 +04:00
my $ tmpfn = "$conffile.$$.tmp" ;
2013-01-04 09:57:11 +04:00
# Note: $oldconf is undef if VM does not exists
2016-03-07 14:41:12 +03:00
my $ cfs_path = PVE::QemuConfig - > cfs_config_path ( $ vmid ) ;
my $ oldconf = PVE::Cluster:: cfs_read_file ( $ cfs_path ) ;
2013-01-04 09:57:11 +04:00
2012-12-12 18:35:26 +04:00
my $ print_devmap = sub {
my $ virtdev_hash = { } ;
my $ cfgfn = "$tmpdir/qemu-server.conf" ;
# we can read the config - that is already extracted
my $ fh = IO::File - > new ( $ cfgfn , "r" ) ||
"unable to read qemu-server.conf - $!\n" ;
2015-11-25 12:20:04 +03:00
my $ fwcfgfn = "$tmpdir/qemu-server.fw" ;
2016-01-28 11:00:41 +03:00
if ( - f $ fwcfgfn ) {
my $ pve_firewall_dir = '/etc/pve/firewall' ;
mkdir $ pve_firewall_dir ; # make sure the dir exists
PVE::Tools:: file_copy ( $ fwcfgfn , "${pve_firewall_dir}/$vmid.fw" ) ;
}
2015-11-25 12:20:04 +03:00
2012-12-12 18:35:26 +04:00
while ( defined ( my $ line = <$fh> ) ) {
if ( $ line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/ ) {
my ( $ virtdev , $ devname , $ storeid , $ format ) = ( $ 1 , $ 2 , $ 3 , $ 4 ) ;
die "archive does not contain data for drive '$virtdev'\n"
if ! $ devinfo - > { $ devname } ;
if ( defined ( $ opts - > { storage } ) ) {
$ storeid = $ opts - > { storage } || 'local' ;
} elsif ( ! $ storeid ) {
$ storeid = 'local' ;
}
$ format = 'raw' if ! $ format ;
$ devinfo - > { $ devname } - > { devname } = $ devname ;
$ devinfo - > { $ devname } - > { virtdev } = $ virtdev ;
$ devinfo - > { $ devname } - > { format } = $ format ;
$ devinfo - > { $ devname } - > { storeid } = $ storeid ;
2013-07-15 11:13:31 +04:00
# check permission on storage
2012-12-12 18:35:26 +04:00
my $ pool = $ opts - > { pool } ; # todo: do we need that?
if ( $ user ne 'root@pam' ) {
$ rpcenv - > check ( $ user , "/storage/$storeid" , [ 'Datastore.AllocateSpace' ] ) ;
}
$ virtdev_hash - > { $ virtdev } = $ devinfo - > { $ devname } ;
}
}
foreach my $ devname ( keys %$ devinfo ) {
2013-07-15 11:13:31 +04:00
die "found no device mapping information for device '$devname'\n"
if ! $ devinfo - > { $ devname } - > { virtdev } ;
2012-12-12 18:35:26 +04:00
}
2016-03-25 16:01:36 +03:00
my $ cfg = PVE::Storage:: config ( ) ;
2013-01-04 09:57:11 +04:00
# create empty/temp config
2013-07-15 11:13:31 +04:00
if ( $ oldconf ) {
2013-01-04 09:57:11 +04:00
PVE::Tools:: file_set_contents ( $ conffile , "memory: 128\n" ) ;
foreach_drive ( $ oldconf , sub {
my ( $ ds , $ drive ) = @ _ ;
return if drive_is_cdrom ( $ drive ) ;
my $ volid = $ drive - > { file } ;
return if ! $ volid || $ volid =~ m | ^ / | ;
my ( $ path , $ owner ) = PVE::Storage:: path ( $ cfg , $ volid ) ;
return if ! $ path || ! $ owner || ( $ owner != $ vmid ) ;
# Note: only delete disk we want to restore
# other volumes will become unused
if ( $ virtdev_hash - > { $ ds } ) {
PVE::Storage:: vdisk_free ( $ cfg , $ volid ) ;
}
} ) ;
2016-02-16 12:05:41 +03:00
# delete vmstate files
# since after the restore we have no snapshots anymore
foreach my $ snapname ( keys % { $ oldconf - > { snapshots } } ) {
my $ snap = $ oldconf - > { snapshots } - > { $ snapname } ;
if ( $ snap - > { vmstate } ) {
eval { PVE::Storage:: vdisk_free ( $ cfg , $ snap - > { vmstate } ) ; } ;
if ( my $ err = $@ ) {
warn $ err ;
}
}
}
2013-01-04 09:57:11 +04:00
}
my $ map = { } ;
2012-12-12 18:35:26 +04:00
foreach my $ virtdev ( sort keys %$ virtdev_hash ) {
my $ d = $ virtdev_hash - > { $ virtdev } ;
my $ alloc_size = int ( ( $ d - > { size } + 1024 - 1 ) / 1024 ) ;
my $ scfg = PVE::Storage:: storage_config ( $ cfg , $ d - > { storeid } ) ;
2013-05-21 14:02:41 +04:00
# test if requested format is supported
my ( $ defFormat , $ validFormats ) = PVE::Storage:: storage_default_format ( $ cfg , $ d - > { storeid } ) ;
my $ supported = grep { $ _ eq $ d - > { format } } @$ validFormats ;
$ d - > { format } = $ defFormat if ! $ supported ;
2012-12-12 18:35:26 +04:00
my $ volid = PVE::Storage:: vdisk_alloc ( $ cfg , $ d - > { storeid } , $ vmid ,
$ d - > { format } , undef , $ alloc_size ) ;
print STDERR "new volume ID is '$volid'\n" ;
$ d - > { volid } = $ volid ;
my $ path = PVE::Storage:: path ( $ cfg , $ volid ) ;
2015-09-08 17:05:21 +03:00
PVE::Storage:: activate_volumes ( $ cfg , [ $ volid ] ) ;
2012-12-12 18:35:26 +04:00
my $ write_zeros = 1 ;
2016-02-23 14:43:51 +03:00
if ( PVE::Storage:: volume_has_feature ( $ cfg , 'sparseinit' , $ volid ) ) {
2012-12-12 18:35:26 +04:00
$ write_zeros = 0 ;
}
2016-04-28 17:11:44 +03:00
print $ fifofh "format=$d->{format}:${write_zeros}:$d->{devname}=$path\n" ;
2012-12-12 18:35:26 +04:00
print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros})\n" ;
$ map - > { $ virtdev } = $ volid ;
}
$ fh - > seek ( 0 , 0 ) || die "seek failed - $!\n" ;
my $ outfd = new IO:: File ( $ tmpfn , "w" ) ||
die "unable to write config for VM $vmid\n" ;
my $ cookie = { netcount = > 0 } ;
while ( defined ( my $ line = <$fh> ) ) {
2013-07-15 11:13:31 +04:00
restore_update_config_line ( $ outfd , $ cookie , $ vmid , $ map , $ line , $ opts - > { unique } ) ;
2012-12-12 18:35:26 +04:00
}
$ fh - > close ( ) ;
$ outfd - > close ( ) ;
} ;
eval {
# enable interrupts
local $ SIG { INT } = $ SIG { TERM } = $ SIG { QUIT } = $ SIG { HUP } = $ SIG { PIPE } = sub {
die "interrupted by signal\n" ;
} ;
local $ SIG { ALRM } = sub { die "got timeout\n" ; } ;
$ oldtimeout = alarm ( $ timeout ) ;
my $ parser = sub {
my $ line = shift ;
print "$line\n" ;
if ( $ line =~ m/^DEV:\sdev_id=(\d+)\ssize:\s(\d+)\sdevname:\s(\S+)$/ ) {
my ( $ dev_id , $ size , $ devname ) = ( $ 1 , $ 2 , $ 3 ) ;
$ devinfo - > { $ devname } = { size = > $ size , dev_id = > $ dev_id } ;
} elsif ( $ line =~ m/^CTIME: / ) {
2014-05-17 11:07:18 +04:00
# we correctly received the vma config, so we can disable
2014-04-17 12:37:46 +04:00
# the timeout now for disk allocation (set to 10 minutes, so
# that we always timeout if something goes wrong)
alarm ( 600 ) ;
2012-12-12 18:35:26 +04:00
& $ print_devmap ( ) ;
print $ fifofh "done\n" ;
my $ tmp = $ oldtimeout || 0 ;
$ oldtimeout = undef ;
alarm ( $ tmp ) ;
close ( $ fifofh ) ;
}
} ;
2013-07-15 11:13:31 +04:00
2012-12-12 18:35:26 +04:00
print "restore vma archive: $cmd\n" ;
run_command ( $ cmd , input = > $ input , outfunc = > $ parser , afterfork = > $ openfifo ) ;
} ;
my $ err = $@ ;
alarm ( $ oldtimeout ) if $ oldtimeout ;
2015-09-08 17:05:21 +03:00
my $ vollist = [] ;
foreach my $ devname ( keys %$ devinfo ) {
my $ volid = $ devinfo - > { $ devname } - > { volid } ;
push @$ vollist , $ volid if $ volid ;
}
2016-03-25 16:01:36 +03:00
my $ cfg = PVE::Storage:: config ( ) ;
2015-09-08 17:05:21 +03:00
PVE::Storage:: deactivate_volumes ( $ cfg , $ vollist ) ;
2012-12-12 18:35:26 +04:00
unlink $ mapfifo ;
if ( $ err ) {
rmtree $ tmpdir ;
unlink $ tmpfn ;
foreach my $ devname ( keys %$ devinfo ) {
my $ volid = $ devinfo - > { $ devname } - > { volid } ;
next if ! $ volid ;
eval {
if ( $ volid =~ m | ^ / | ) {
unlink $ volid || die 'unlink failed\n' ;
} else {
PVE::Storage:: vdisk_free ( $ cfg , $ volid ) ;
}
print STDERR "temporary volume '$volid' sucessfuly removed\n" ;
} ;
print STDERR "unable to cleanup '$volid' - $@" if $@ ;
}
die $ err ;
}
rmtree $ tmpdir ;
2013-01-04 09:57:11 +04:00
rename ( $ tmpfn , $ conffile ) ||
2012-12-12 18:35:26 +04:00
die "unable to commit configuration file '$conffile'\n" ;
2013-01-04 09:57:11 +04:00
PVE::Cluster:: cfs_update ( ) ; # make sure we read new file
2012-12-12 18:35:26 +04:00
eval { rescan ( $ vmid , 1 ) ; } ;
warn $@ if $@ ;
}
sub restore_tar_archive {
my ( $ archive , $ vmid , $ user , $ opts ) = @ _ ;
2011-10-19 13:27:42 +04:00
if ( $ archive ne '-' ) {
2013-01-04 09:57:11 +04:00
my $ firstfile = tar_archive_read_firstfile ( $ archive ) ;
2011-10-19 13:27:42 +04:00
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
2016-03-25 16:01:36 +03:00
my $ storecfg = PVE::Storage:: config ( ) ;
2013-01-28 12:54:00 +04:00
2013-01-04 09:57:11 +04:00
# destroy existing data - keep empty config
2016-03-07 14:41:12 +03:00
my $ vmcfgfn = PVE::QemuConfig - > config_file ( $ vmid ) ;
2013-01-28 12:54:00 +04:00
destroy_vm ( $ storecfg , $ vmid , 1 ) if - f $ vmcfgfn ;
2013-01-04 09:57:11 +04:00
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
2016-03-07 14:41:12 +03:00
my $ conffile = PVE::QemuConfig - > config_file ( $ vmid ) ;
2011-10-17 15:49:48 +04:00
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" ;
2012-12-12 18:35:26 +04:00
my $ cookie = { netcount = > 0 } ;
2011-10-17 15:49:48 +04:00
while ( defined ( my $ line = <$srcfd> ) ) {
2013-07-15 11:13:31 +04:00
restore_update_config_line ( $ outfd , $ cookie , $ vmid , $ map , $ line , $ opts - > { unique } ) ;
2011-10-17 15:49:48 +04:00
}
$ 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 ;
2013-01-04 09:57:11 +04:00
tar_restore_cleanup ( $ storecfg , "$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" ;
2012-12-12 18:35:26 +04:00
2013-01-04 09:57:11 +04:00
PVE::Cluster:: cfs_update ( ) ; # make sure we read new file
2012-12-12 18:35:26 +04:00
eval { rescan ( $ vmid , 1 ) ; } ;
warn $@ if $@ ;
2011-10-17 15:49:48 +04:00
} ;
2012-09-12 13:59:48 +04:00
sub foreach_writable_storage {
my ( $ conf , $ func ) = @ _ ;
my $ sidhash = { } ;
foreach my $ ds ( keys %$ conf ) {
2016-03-03 17:45:15 +03:00
next if ! is_valid_drivename ( $ ds ) ;
2012-09-12 13:59:48 +04:00
my $ drive = parse_drive ( $ ds , $ conf - > { $ ds } ) ;
next if ! $ drive ;
next if drive_is_cdrom ( $ drive ) ;
my $ volid = $ drive - > { file } ;
my ( $ sid , $ volname ) = PVE::Storage:: parse_volume_id ( $ volid , 1 ) ;
2013-07-15 11:13:31 +04:00
$ sidhash - > { $ sid } = $ sid if $ sid ;
2012-09-12 13:59:48 +04:00
}
foreach my $ sid ( sort keys %$ sidhash ) {
& $ func ( $ sid ) ;
}
}
2015-05-06 10:57:34 +03:00
sub do_snapshots_with_qemu {
my ( $ storecfg , $ volid ) = @ _ ;
my $ storage_name = PVE::Storage:: parse_volume_id ( $ volid ) ;
2015-09-08 13:23:54 +03:00
if ( $ qemu_snap_storage - > { $ storecfg - > { ids } - > { $ storage_name } - > { type } }
&& ! $ storecfg - > { ids } - > { $ storage_name } - > { krbd } ) {
2015-05-06 10:57:34 +03:00
return 1 ;
}
if ( $ volid =~ m/\.(qcow2|qed)$/ ) {
return 1 ;
}
return undef ;
}
2016-02-15 13:45:56 +03:00
sub qga_check_running {
my ( $ vmid ) = @ _ ;
eval { vm_mon_cmd ( $ vmid , "guest-ping" , timeout = > 3 ) ; } ;
if ( $@ ) {
warn "Qemu Guest Agent are not running - $@" ;
return 0 ;
}
return 1 ;
}
2013-02-14 14:58:49 +04:00
sub template_create {
my ( $ vmid , $ conf , $ disk ) = @ _ ;
my $ storecfg = PVE::Storage:: config ( ) ;
2013-02-15 11:44:12 +04:00
foreach_drive ( $ conf , sub {
my ( $ ds , $ drive ) = @ _ ;
return if drive_is_cdrom ( $ drive ) ;
return if $ disk && $ ds ne $ disk ;
my $ volid = $ drive - > { file } ;
2013-04-18 19:05:31 +04:00
return if ! PVE::Storage:: volume_has_feature ( $ storecfg , 'template' , $ volid ) ;
2013-02-15 11:44:12 +04:00
2013-02-14 14:58:49 +04:00
my $ voliddst = PVE::Storage:: vdisk_create_base ( $ storecfg , $ volid ) ;
$ drive - > { file } = $ voliddst ;
2013-05-29 10:32:10 +04:00
$ conf - > { $ ds } = print_drive ( $ vmid , $ drive ) ;
2016-03-07 14:41:12 +03:00
PVE::QemuConfig - > write_config ( $ vmid , $ conf ) ;
2013-02-14 14:58:49 +04:00
} ) ;
}
2013-04-29 10:41:01 +04:00
sub qemu_img_convert {
2016-03-18 14:20:33 +03:00
my ( $ src_volid , $ dst_volid , $ size , $ snapname , $ is_zero_initialized ) = @ _ ;
2013-04-29 10:41:01 +04:00
my $ storecfg = PVE::Storage:: config ( ) ;
my ( $ src_storeid , $ src_volname ) = PVE::Storage:: parse_volume_id ( $ src_volid , 1 ) ;
my ( $ dst_storeid , $ dst_volname ) = PVE::Storage:: parse_volume_id ( $ dst_volid , 1 ) ;
if ( $ src_storeid && $ dst_storeid ) {
2015-11-12 13:16:50 +03:00
PVE::Storage:: activate_volumes ( $ storecfg , [ $ src_volid ] , $ snapname ) ;
2013-04-29 10:41:01 +04:00
my $ src_scfg = PVE::Storage:: storage_config ( $ storecfg , $ src_storeid ) ;
my $ dst_scfg = PVE::Storage:: storage_config ( $ storecfg , $ dst_storeid ) ;
my $ src_format = qemu_img_format ( $ src_scfg , $ src_volname ) ;
my $ dst_format = qemu_img_format ( $ dst_scfg , $ dst_volname ) ;
my $ src_path = PVE::Storage:: path ( $ storecfg , $ src_volid , $ snapname ) ;
my $ dst_path = PVE::Storage:: path ( $ storecfg , $ dst_volid ) ;
my $ cmd = [] ;
2013-11-28 20:17:33 +04:00
push @$ cmd , '/usr/bin/qemu-img' , 'convert' , '-t' , 'writeback' , '-p' , '-n' ;
2013-04-29 10:41:01 +04:00
push @$ cmd , '-s' , $ snapname if ( $ snapname && $ src_format eq "qcow2" ) ;
2016-03-18 14:20:33 +03:00
push @$ cmd , '-f' , $ src_format , '-O' , $ dst_format , $ src_path ;
if ( $ is_zero_initialized ) {
push @$ cmd , "zeroinit:$dst_path" ;
} else {
push @$ cmd , $ dst_path ;
}
2013-04-29 10:41:01 +04:00
my $ parser = sub {
my $ line = shift ;
if ( $ line =~ m/\((\S+)\/100\%\)/ ) {
my $ percent = $ 1 ;
my $ transferred = int ( $ size * $ percent / 100 ) ;
my $ remaining = $ size - $ transferred ;
print "transferred: $transferred bytes remaining: $remaining bytes total: $size bytes progression: $percent %\n" ;
}
} ;
eval { run_command ( $ cmd , timeout = > undef , outfunc = > $ parser ) ; } ;
my $ err = $@ ;
die "copy failed: $err" if $ err ;
}
}
sub qemu_img_format {
my ( $ scfg , $ volname ) = @ _ ;
2015-06-10 11:22:42 +03:00
if ( $ scfg - > { path } && $ volname =~ m/\.(raw|cow|qcow|qcow2|qed|vmdk|cloop)$/ ) {
2013-04-29 10:41:01 +04:00
return $ 1 ;
2013-07-15 11:13:31 +04:00
} else {
2013-04-29 10:41:01 +04:00
return "raw" ;
}
}
2013-05-02 20:18:03 +04:00
sub qemu_drive_mirror {
2016-03-18 14:20:33 +03:00
my ( $ vmid , $ drive , $ dst_volid , $ vmiddst , $ is_zero_initialized ) = @ _ ;
2013-05-02 20:18:03 +04:00
my $ storecfg = PVE::Storage:: config ( ) ;
2014-11-10 09:55:09 +03:00
my ( $ dst_storeid , $ dst_volname ) = PVE::Storage:: parse_volume_id ( $ dst_volid ) ;
2013-05-29 10:32:10 +04:00
2014-11-10 09:55:09 +03:00
my $ dst_scfg = PVE::Storage:: storage_config ( $ storecfg , $ dst_storeid ) ;
2013-05-02 20:18:03 +04:00
2015-06-10 11:22:42 +03:00
my $ format = qemu_img_format ( $ dst_scfg , $ dst_volname ) ;
2014-11-09 17:13:01 +03:00
2014-11-10 09:55:09 +03:00
my $ dst_path = PVE::Storage:: path ( $ storecfg , $ dst_volid ) ;
2014-11-09 17:13:01 +03:00
2016-03-18 14:20:33 +03:00
my $ qemu_target = $ is_zero_initialized ? "zeroinit:$dst_path" : $ dst_path ;
my $ opts = { timeout = > 10 , device = > "drive-$drive" , mode = > "existing" , sync = > "full" , target = > $ qemu_target } ;
2014-11-10 10:18:39 +03:00
$ opts - > { format } = $ format if $ format ;
2015-07-01 07:01:42 +03:00
print "drive mirror is starting (scanning bitmap) : this step can take some minutes/hours, depend of disk size and storage speed\n" ;
2014-11-09 17:13:01 +03:00
2016-04-11 16:19:46 +03:00
my $ finish_job = sub {
while ( 1 ) {
my $ stats = vm_mon_cmd ( $ vmid , "query-block-jobs" ) ;
my $ stat = @$ stats [ 0 ] ;
last if ! $ stat ;
sleep 1 ;
}
} ;
2014-11-10 09:55:09 +03:00
eval {
2015-07-01 07:01:42 +03:00
vm_mon_cmd ( $ vmid , "drive-mirror" , %$ opts ) ;
2014-11-10 09:55:09 +03:00
while ( 1 ) {
my $ stats = vm_mon_cmd ( $ vmid , "query-block-jobs" ) ;
my $ stat = @$ stats [ 0 ] ;
die "mirroring job seem to have die. Maybe do you have bad sectors?" if ! $ stat ;
die "error job is not mirroring" if $ stat - > { type } ne "mirror" ;
my $ busy = $ stat - > { busy } ;
2015-05-11 16:55:21 +03:00
my $ ready = $ stat - > { ready } ;
2014-11-10 09:55:09 +03:00
2014-12-01 11:27:01 +03:00
if ( my $ total = $ stat - > { len } ) {
my $ transferred = $ stat - > { offset } || 0 ;
my $ remaining = $ total - $ transferred ;
my $ percent = sprintf "%.2f" , ( $ transferred * 100 / $ total ) ;
2014-12-04 15:07:59 +03:00
2015-05-11 16:55:21 +03:00
print "transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent % busy: $busy ready: $ready \n" ;
2014-12-01 11:27:01 +03:00
}
2014-11-21 14:31:56 +03:00
2014-11-10 09:55:09 +03:00
2015-05-11 16:55:21 +03:00
if ( $ stat - > { ready } eq 'true' ) {
2014-11-21 14:31:56 +03:00
2015-05-11 16:55:21 +03:00
last if $ vmiddst != $ vmid ;
2014-11-10 08:31:08 +03:00
2015-05-11 16:55:21 +03:00
# try to switch the disk if source and destination are on the same guest
eval { vm_mon_cmd ( $ vmid , "block-job-complete" , device = > "drive-$drive" ) } ;
2016-04-11 16:19:46 +03:00
if ( ! $@ ) {
& $ finish_job ( ) ;
last ;
}
2015-05-11 16:55:21 +03:00
die $@ if $@ !~ m/cannot be completed/ ;
2014-11-10 09:55:09 +03:00
}
sleep 1 ;
2013-05-02 20:18:03 +04:00
}
2014-11-10 09:55:09 +03:00
} ;
2014-11-10 10:18:39 +03:00
my $ err = $@ ;
2014-11-10 09:55:09 +03:00
2014-11-10 10:18:39 +03:00
my $ cancel_job = sub {
2014-11-10 09:55:09 +03:00
vm_mon_cmd ( $ vmid , "block-job-cancel" , device = > "drive-$drive" ) ;
2016-04-11 16:19:46 +03:00
& $ finish_job ( ) ;
2014-11-10 10:18:39 +03:00
} ;
if ( $ err ) {
2014-11-21 14:31:56 +03:00
eval { & $ cancel_job ( ) ; } ;
2014-11-10 10:18:39 +03:00
die "mirroring error: $err" ;
}
if ( $ vmiddst != $ vmid ) {
# if we clone a disk for a new target vm, we don't switch the disk
& $ cancel_job ( ) ; # so we call block-job-cancel
2013-05-02 20:18:03 +04:00
}
}
2013-05-29 10:32:10 +04:00
sub clone_disk {
2013-07-15 11:13:31 +04:00
my ( $ storecfg , $ vmid , $ running , $ drivename , $ drive , $ snapname ,
2013-05-29 10:32:10 +04:00
$ newvmid , $ storage , $ format , $ full , $ newvollist ) = @ _ ;
my $ newvolid ;
if ( ! $ full ) {
print "create linked clone of drive $drivename ($drive->{file})\n" ;
2014-07-04 12:25:47 +04:00
$ newvolid = PVE::Storage:: vdisk_clone ( $ storecfg , $ drive - > { file } , $ newvmid , $ snapname ) ;
2013-05-29 10:32:10 +04:00
push @$ newvollist , $ newvolid ;
} else {
my ( $ storeid , $ volname ) = PVE::Storage:: parse_volume_id ( $ drive - > { file } ) ;
$ storeid = $ storage if $ storage ;
2013-05-29 14:59:21 +04:00
my ( $ defFormat , $ validFormats ) = PVE::Storage:: storage_default_format ( $ storecfg , $ storeid ) ;
if ( ! $ format ) {
2015-06-10 11:22:42 +03:00
my $ scfg = PVE::Storage:: storage_config ( $ storecfg , $ storeid ) ;
$ format = qemu_img_format ( $ scfg , $ volname ) ;
2013-05-29 10:32:10 +04:00
}
2013-05-29 14:59:21 +04:00
# test if requested format is supported - else use default
my $ supported = grep { $ _ eq $ format } @$ validFormats ;
$ format = $ defFormat if ! $ supported ;
2013-05-29 10:32:10 +04:00
my ( $ size ) = PVE::Storage:: volume_size_info ( $ storecfg , $ drive - > { file } , 3 ) ;
print "create full clone of drive $drivename ($drive->{file})\n" ;
$ newvolid = PVE::Storage:: vdisk_alloc ( $ storecfg , $ storeid , $ newvmid , $ format , undef , ( $ size / 1024 ) ) ;
push @$ newvollist , $ newvolid ;
2015-09-03 14:12:13 +03:00
PVE::Storage:: activate_volumes ( $ storecfg , $ newvollist ) ;
2016-03-18 14:20:33 +03:00
my $ sparseinit = PVE::Storage:: volume_has_feature ( $ storecfg , 'sparseinit' , $ newvolid ) ;
2013-05-29 10:32:10 +04:00
if ( ! $ running || $ snapname ) {
2016-03-18 14:20:33 +03:00
qemu_img_convert ( $ drive - > { file } , $ newvolid , $ size , $ snapname , $ sparseinit ) ;
2013-05-29 10:32:10 +04:00
} else {
2016-03-18 14:20:33 +03:00
qemu_drive_mirror ( $ vmid , $ drivename , $ newvolid , $ newvmid , $ sparseinit ) ;
2013-07-15 11:13:31 +04:00
}
2013-05-29 10:32:10 +04:00
}
my ( $ size ) = PVE::Storage:: volume_size_info ( $ storecfg , $ newvolid , 3 ) ;
my $ disk = $ drive ;
$ disk - > { format } = undef ;
$ disk - > { file } = $ newvolid ;
$ disk - > { size } = $ size ;
return $ disk ;
}
2013-06-05 11:52:22 +04:00
# this only works if VM is running
sub get_current_qemu_machine {
my ( $ vmid ) = @ _ ;
my $ cmd = { execute = > 'query-machines' , arguments = > { } } ;
2014-11-19 12:43:42 +03:00
my $ res = vm_qmp_command ( $ vmid , $ cmd ) ;
2013-06-05 11:52:22 +04:00
my ( $ current , $ default ) ;
foreach my $ e ( @$ res ) {
$ default = $ e - > { name } if $ e - > { 'is-default' } ;
$ current = $ e - > { name } if $ e - > { 'is-current' } ;
}
# fallback to the default machine if current is not supported by qemu
return $ current || $ default || 'pc' ;
}
2015-03-16 07:57:41 +03:00
sub qemu_machine_feature_enabled {
my ( $ machine , $ kvmver , $ version_major , $ version_minor ) = @ _ ;
my $ current_major ;
my $ current_minor ;
if ( $ machine && $ machine =~ m/^(pc(-i440fx|-q35)?-(\d+)\.(\d+))/ ) {
$ current_major = $ 3 ;
$ current_minor = $ 4 ;
} elsif ( $ kvmver =~ m/^(\d+)\.(\d+)/ ) {
$ current_major = $ 1 ;
$ current_minor = $ 2 ;
}
return 1 if $ current_major >= $ version_major && $ current_minor >= $ version_minor ;
}
2015-11-06 12:27:04 +03:00
sub qemu_machine_pxe {
my ( $ vmid , $ conf , $ machine ) = @ _ ;
$ machine = PVE::QemuServer:: get_current_qemu_machine ( $ vmid ) if ! $ machine ;
foreach my $ opt ( keys %$ conf ) {
next if $ opt !~ m/^net(\d+)$/ ;
my $ net = PVE::QemuServer:: parse_net ( $ conf - > { $ opt } ) ;
next if ! $ net ;
my $ romfile = PVE::QemuServer:: vm_mon_cmd_nocheck ( $ vmid , 'qom-get' , path = > $ opt , property = > 'romfile' ) ;
return $ machine . ".pxe" if $ romfile =~ m/pxe/ ;
last ;
}
2016-02-22 19:15:13 +03:00
return $ machine ;
2015-11-06 12:27:04 +03:00
}
2015-11-06 12:27:05 +03:00
sub qemu_use_old_bios_files {
my ( $ machine_type ) = @ _ ;
return if ! $ machine_type ;
my $ use_old_bios_files = undef ;
if ( $ machine_type =~ m/^(\S+)\.pxe$/ ) {
$ machine_type = $ 1 ;
$ use_old_bios_files = 1 ;
} else {
2016-02-25 16:47:16 +03:00
my $ kvmver = kvm_user_version ( ) ;
2015-11-06 12:27:05 +03:00
# Note: kvm version < 2.4 use non-efi pxe files, and have problems when we
# load new efi bios files on migration. So this hack is required to allow
# live migration from qemu-2.2 to qemu-2.4, which is sometimes used when
# updrading from proxmox-ve-3.X to proxmox-ve 4.0
2016-02-25 16:47:16 +03:00
$ use_old_bios_files = ! qemu_machine_feature_enabled ( $ machine_type , $ kvmver , 2 , 4 ) ;
2015-11-06 12:27:05 +03:00
}
return ( $ use_old_bios_files , $ machine_type ) ;
}
add pci multifunction support
multifunction device should be define without the .function
hostpci0: 00:00
example
-------
if 00:00.0
00:00.1
00:00.2
exists,
then we generate the multifunction devices
-device (pci-assign|vfio-pci),host=00:00.0,id=hostpci0.0,bus=...,addr=0x0.0,multifunction=on
-device (pci-assign|vfio-pci),host=00:00.1,id=hostpci0.1,bus=...,addr=0x0.1
-device (pci-assign|vfio-pci),host=00:00.2,id=hostpci0.2,bus=...,addr=0x0.2
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
2014-06-23 19:41:54 +04:00
sub lspci {
my $ devices = { } ;
dir_glob_foreach ( "$pcisysfs/devices" , '[a-f0-9]{4}:([a-f0-9]{2}:[a-f0-9]{2})\.([0-9])' , sub {
my ( undef , $ id , $ function ) = @ _ ;
my $ res = { id = > $ id , function = > $ function } ;
push @ { $ devices - > { $ id } } , $ res ;
} ) ;
return $ devices ;
}
2015-03-19 13:06:12 +03:00
sub vm_iothreads_list {
my ( $ vmid ) = @ _ ;
my $ res = vm_mon_cmd ( $ vmid , 'query-iothreads' ) ;
my $ iothreads = { } ;
foreach my $ iothread ( @$ res ) {
$ iothreads - > { $ iothread - > { id } } = $ iothread - > { "thread-id" } ;
}
return $ iothreads ;
}
2015-03-27 05:41:54 +03:00
sub scsihw_infos {
my ( $ conf , $ drive ) = @ _ ;
my $ maxdev = 0 ;
if ( $ conf - > { scsihw } && ( $ conf - > { scsihw } =~ m/^lsi/ ) ) {
$ maxdev = 7 ;
2015-03-27 08:15:01 +03:00
} elsif ( $ conf - > { scsihw } && ( $ conf - > { scsihw } eq 'virtio-scsi-single' ) ) {
2015-03-27 05:41:54 +03:00
$ maxdev = 1 ;
} else {
$ maxdev = 256 ;
}
my $ controller = int ( $ drive - > { index } / $ maxdev ) ;
2015-03-27 08:15:01 +03:00
my $ controller_prefix = ( $ conf - > { scsihw } && $ conf - > { scsihw } eq 'virtio-scsi-single' ) ? "virtioscsi" : "scsihw" ;
2015-03-27 05:41:54 +03:00
return ( $ maxdev , $ controller , $ controller_prefix ) ;
}
2015-03-27 08:15:01 +03:00
2015-09-06 17:01:59 +03:00
# bash completion helper
sub complete_backup_archives {
my ( $ cmdname , $ pname , $ cvalue ) = @ _ ;
my $ cfg = PVE::Storage:: config ( ) ;
my $ storeid ;
if ( $ cvalue =~ m/^([^:]+):/ ) {
$ storeid = $ 1 ;
}
my $ data = PVE::Storage:: template_list ( $ cfg , $ storeid , 'backup' ) ;
my $ res = [] ;
foreach my $ id ( keys %$ data ) {
foreach my $ item ( @ { $ data - > { $ id } } ) {
next if $ item - > { format } !~ m/^vma\.(gz|lzo)$/ ;
push @$ res , $ item - > { volid } if defined ( $ item - > { volid } ) ;
}
}
return $ res ;
}
my $ complete_vmid_full = sub {
my ( $ running ) = @ _ ;
my $ idlist = vmstatus ( ) ;
my $ res = [] ;
foreach my $ id ( keys %$ idlist ) {
my $ d = $ idlist - > { $ id } ;
if ( defined ( $ running ) ) {
next if $ d - > { template } ;
next if $ running && $ d - > { status } ne 'running' ;
next if ! $ running && $ d - > { status } eq 'running' ;
}
push @$ res , $ id ;
}
return $ res ;
} ;
sub complete_vmid {
return & $ complete_vmid_full ( ) ;
}
sub complete_vmid_stopped {
return & $ complete_vmid_full ( 0 ) ;
}
sub complete_vmid_running {
return & $ complete_vmid_full ( 1 ) ;
}
2015-09-07 09:13:07 +03:00
sub complete_storage {
my $ cfg = PVE::Storage:: config ( ) ;
my $ ids = $ cfg - > { ids } ;
my $ res = [] ;
foreach my $ sid ( keys %$ ids ) {
next if ! PVE::Storage:: storage_check_enabled ( $ cfg , $ sid , undef , 1 ) ;
2015-11-09 13:11:47 +03:00
next if ! $ ids - > { $ sid } - > { content } - > { images } ;
2015-09-07 09:13:07 +03:00
push @$ res , $ sid ;
}
return $ res ;
}
2011-08-23 09:47:04 +04:00
1 ;