Permalink
Find file Copy path
6249 lines (4999 sloc) 169 KB
package PVE::QemuServer;
use strict;
use warnings;
use POSIX;
use IO::Handle;
use IO::Select;
use IO::File;
use IO::Dir;
use IO::Socket::UNIX;
use File::Basename;
use File::Path;
use File::stat;
use Getopt::Long;
use Digest::SHA;
use Fcntl ':flock';
use Cwd 'abs_path';
use IPC::Open3;
use JSON;
use Fcntl;
use PVE::SafeSyslog;
use Storable qw(dclone);
use PVE::Exception qw(raise raise_param_exc);
use PVE::Storage;
use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach);
use PVE::JSONSchema qw(get_standard_option);
use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
use PVE::INotify;
use PVE::ProcFSTools;
use PVE::QemuConfig;
use PVE::QMPClient;
use PVE::RPCEnvironment;
use Time::HiRes qw(gettimeofday);
use File::Copy qw(copy);
use URI::Escape;
my $qemu_snap_storage = {rbd => 1, sheepdog => 1};
my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
# Note about locking: we use flock on the config file protect
# against concurent actions.
# Aditionaly, we have a 'lock' setting in the config file. This
# can be set to 'migrate', 'backup', 'snapshot' or 'rollback'. Most actions are not
# allowed when such lock is set. But you can ignore this kind of
# lock with the --skiplock flag.
cfs_register_file('/qemu-server/',
\&parse_vm_config,
\&write_vm_config);
PVE::JSONSchema::register_standard_option('skiplock', {
description => "Ignore locks - only root is allowed to use this option.",
type => 'boolean',
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,
});
PVE::JSONSchema::register_standard_option('pve-snapshot-name', {
description => "The name of the snapshot.",
type => 'string', format => 'pve-configid',
maxLength => 40,
});
#no warnings 'redefine';
sub cgroups_write {
my ($controller, $vmid, $option, $value) = @_;
my $path = "/sys/fs/cgroup/$controller/qemu.slice/$vmid.scope/$option";
PVE::ProcFSTools::write_proc_entry($path, $value);
}
my $nodename = PVE::INotify::nodename();
mkdir "/etc/pve/nodes/$nodename";
my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
mkdir $confdir;
my $var_run_tmpdir = "/var/run/qemu-server";
mkdir $var_run_tmpdir;
my $lock_dir = "/var/lock/qemu-server";
mkdir $lock_dir;
my $pcisysfs = "/sys/bus/pci";
my $cpu_fmt = {
cputype => {
description => "Emulated CPU type.",
type => 'string',
enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge IvyBridge Haswell Haswell-noTSX Broadwell Broadwell-noTSX Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
format_description => 'cputype',
default => 'kvm64',
default_key => 1,
},
hidden => {
description => "Do not identify as a KVM virtual machine.",
type => 'boolean',
optional => 1,
default => 0
},
};
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);
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,
},
hotplug => {
optional => 1,
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',
},
reboot => {
optional => 1,
type => 'boolean',
description => "Allow reboot. If set to '0' the VM exit on reboot.",
default => 1,
},
lock => {
optional => 1,
type => 'string',
description => "Lock/unlock the VM.",
enum => [qw(migrate backup snapshot rollback)],
},
cpulimit => {
optional => 1,
type => 'number',
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.",
minimum => 0,
maximum => 128,
default => 0,
},
cpuunits => {
optional => 1,
type => 'integer',
description => "CPU weight for a VM. Argument is used in the kernel fair scheduler. The larger the number is, the more CPU time this VM gets. Number is relative to weights of all the other running VMs.\n\nNOTE: You can disable fair-scheduler configuration by setting this to 0.",
minimum => 0,
maximum => 500000,
default => 1000,
},
memory => {
optional => 1,
type => 'integer',
description => "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device.",
minimum => 16,
default => 512,
},
balloon => {
optional => 1,
type => 'integer',
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,
},
keyboard => {
optional => 1,
type => 'string',
description => "Keybord layout for vnc server. Default is read from the datacenter configuration file.",
enum => PVE::Tools::kvmkeymaplist(),
default => 'en-us',
},
name => {
optional => 1,
type => 'string', format => 'dns-name',
description => "Set a name for the VM. Only used on the configuration web interface.",
},
scsihw => {
optional => 1,
type => 'string',
description => "scsi controller model",
enum => [qw(lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi)],
default => 'lsi',
},
description => {
optional => 1,
type => 'string',
description => "Description for the VM. Only used on the configuration web interface. This is saved as comment inside the configuration file.",
},
ostype => {
optional => 1,
type => 'string',
enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 l24 l26 solaris)],
description => <<EODESC,
Used to enable special optimization/features for specific
operating systems:
other => unspecified OS
wxp => Microsoft Windows XP
w2k => Microsoft Windows 2000
w2k3 => Microsoft Windows 2003
w2k8 => Microsoft Windows 2008
wvista => Microsoft Windows Vista
win7 => Microsoft Windows 7
win8 => Microsoft Windows 8/2012
l24 => Linux 2.4 Kernel
l26 => Linux 2.6/3.X Kernel
solaris => solaris/opensolaris/openindiania kernel
other|l24|l26|solaris ... no special behaviour
wxp|w2k|w2k3|w2k8|wvista|win7|win8 ... use --localtime switch
EODESC
},
boot => {
optional => 1,
type => 'string',
description => "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n).",
pattern => '[acdn]{1,4}',
default => 'cdn',
},
bootdisk => {
optional => 1,
type => 'string', format => 'pve-qm-bootdisk',
description => "Enable booting from specified disk.",
pattern => '(ide|sata|scsi|virtio)\d+',
},
smp => {
optional => 1,
type => 'integer',
description => "The number of CPUs. Please use option -sockets instead.",
minimum => 1,
default => 1,
},
sockets => {
optional => 1,
type => 'integer',
description => "The number of CPU sockets.",
minimum => 1,
default => 1,
},
cores => {
optional => 1,
type => 'integer',
description => "The number of cores per socket.",
minimum => 1,
default => 1,
},
numa => {
optional => 1,
type => 'boolean',
description => "Enable/disable NUMA.",
default => 0,
},
vcpus => {
optional => 1,
type => 'integer',
description => "Number of hotplugged vcpus.",
minimum => 1,
default => 0,
},
acpi => {
optional => 1,
type => 'boolean',
description => "Enable/disable ACPI.",
default => 1,
},
agent => {
optional => 1,
type => 'boolean',
description => "Enable/disable Qemu GuestAgent.",
default => 0,
},
kvm => {
optional => 1,
type => 'boolean',
description => "Enable/disable KVM hardware virtualization.",
default => 1,
},
tdf => {
optional => 1,
type => 'boolean',
description => "Enable/disable time drift fix.",
default => 0,
},
localtime => {
optional => 1,
type => 'boolean',
description => "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS.",
},
freeze => {
optional => 1,
type => 'boolean',
description => "Freeze CPU at startup (use 'c' monitor command to start execution).",
},
vga => {
optional => 1,
type => 'string',
description => "Select the VGA type. If you want to use high resolution" .
" 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.",
enum => [qw(std cirrus vmware qxl serial0 serial1 serial2 serial3 qxl2 qxl3 qxl4)],
},
watchdog => {
optional => 1,
type => 'string', format => 'pve-qm-watchdog',
description => "Create a virtual hardware watchdog device. Once enabled" .
" (by a guest action), the watchdog must be periodically polled " .
"by an agent inside the guest or else the watchdog will reset " .
"the guest (or execute the respective action specified)",
},
startdate => {
optional => 1,
type => 'string',
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',
},
startup => get_standard_option('pve-startup-order'),
template => {
optional => 1,
type => 'boolean',
description => "Enable/disable Template.",
default => 0,
},
args => {
optional => 1,
type => 'string',
description => <<EODESCR,
Arbitrary arguments passed to kvm, for example:
args: -no-reboot -no-hpet
NOTE: this option is for experts only.
EODESCR
},
tablet => {
optional => 1,
type => 'boolean',
default => 1,
description => "Enable/disable the USB tablet device. This device is " .
"usually needed to allow absolute mouse positioning 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).",
},
migrate_speed => {
optional => 1,
type => 'integer',
description => "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
minimum => 0,
default => 0,
},
migrate_downtime => {
optional => 1,
type => 'number',
description => "Set maximum tolerated downtime (in seconds) for migrations.",
minimum => 0,
default => 0.1,
},
cdrom => {
optional => 1,
type => 'string', format => 'pve-qm-drive',
typetext => 'volume',
description => "This is an alias for option -ide2",
},
cpu => {
optional => 1,
description => "Emulated CPU type.",
type => 'string',
format => $cpu_fmt,
},
parent => get_standard_option('pve-snapshot-name', {
optional => 1,
description => "Parent snapshot name. This is used internally, and should not be modified.",
}),
snaptime => {
optional => 1,
description => "Timestamp for snapshots.",
type => 'integer',
minimum => 0,
},
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.",
},
machine => {
description => "Specific the Qemu machine type.",
type => 'string',
pattern => '(pc|pc(-i440fx)?-\d+\.\d+(\.pxe)?|q35|pc-q35-\d+\.\d+(\.pxe)?)',
maxLength => 40,
optional => 1,
},
smbios1 => {
description => "Specify SMBIOS type 1 fields.",
type => 'string', format => 'pve-qm-smbios1',
maxLength => 256,
optional => 1,
},
protection => {
optional => 1,
type => 'boolean',
description => "Sets the protection flag of the VM. This will prevent the remove operation.",
default => 0,
},
bios => {
optional => 1,
type => 'string',
enum => [ qw(seabios ovmf) ],
description => "Select BIOS implementation.",
default => 'seabios',
},
};
# 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;
my $MAX_SCSI_DISKS = 14;
my $MAX_VIRTIO_DISKS = 16;
my $MAX_SATA_DISKS = 6;
my $MAX_USB_DEVICES = 5;
my $MAX_NETS = 32;
my $MAX_UNUSED_DISKS = 8;
my $MAX_HOSTPCI_DEVICES = 4;
my $MAX_SERIAL_PORTS = 4;
my $MAX_PARALLEL_PORTS = 3;
my $MAX_NUMA = 8;
my $MAX_MEM = 4194304;
my $STATICMEM = 1024;
my $numa_fmt = {
cpus => {
type => "string",
pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
description => "CPUs accessing this numa node.",
format_description => "id[-id];...",
},
memory => {
type => "number",
description => "Amount of memory this numa node provides.",
format_description => "mb",
optional => 1,
},
hostnodes => {
type => "string",
pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
description => "host numa nodes to use",
format_description => "id[-id];...",
optional => 1,
},
policy => {
type => 'string',
enum => [qw(preferred bind interleave)],
format_description => 'preferred|bind|interleave',
description => "numa allocation policy.",
optional => 1,
},
};
PVE::JSONSchema::register_format('pve-qm-numanode', $numa_fmt);
my $numadesc = {
optional => 1,
type => 'string', format => $numa_fmt,
description => "numa topology",
};
PVE::JSONSchema::register_standard_option("pve-qm-numanode", $numadesc);
for (my $i = 0; $i < $MAX_NUMA; $i++) {
$confdesc->{"numa$i"} = $numadesc;
}
my $nic_model_list = ['rtl8139', 'ne2k_pci', 'e1000', 'pcnet', 'virtio',
'ne2k_isa', 'i82551', 'i82557b', 'i82559er', 'vmxnet3',
'e1000-82540em', 'e1000-82544gc', 'e1000-82545em'];
my $nic_model_list_txt = join(' ', sort @$nic_model_list);
my $net_fmt = {
macaddr => {
type => 'string',
pattern => qr/[0-9a-f]{2}(?::[0-9a-f]{2}){5}/i,
description => "MAC address",
format_description => "XX:XX:XX:XX:XX:XX",
optional => 1,
},
model => { alias => 'macaddr', default_key => 1 },
(map { $_ => { group => 'model' } } @$nic_model_list),
bridge => {
type => 'string',
description => 'Bridge to attach the network device to.',
format_description => 'bridge',
optional => 1,
},
queues => {
type => 'integer',
minimum => 0, maximum => 16,
description => 'Number of packet queues to be used on the device.',
format_description => 'number',
optional => 1,
},
rate => {
type => 'number',
minimum => 0,
description => 'Rate limit in mbps as floating point number.',
format_description => 'mbps',
optional => 1,
},
tag => {
type => 'integer',
minimum => 2, maximum => 4094,
description => 'VLAN tag to apply to packets on this interface.',
format_description => 'vlanid',
optional => 1,
},
trunks => {
type => 'string',
pattern => qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
description => 'VLAN trunks to pass through this interface.',
format_description => 'id;id...',
optional => 1,
},
firewall => {
type => 'boolean',
description => 'Whether this interface should be protected by the firewall.',
format_description => '0|1',
optional => 1,
},
link_down => {
type => 'boolean',
description => 'Whether this interface should be DISconnected (like pulling the plug).',
format_description => '0|1',
optional => 1,
},
};
my $netdesc = {
optional => 1,
type => 'string', format => 'pve-qm-net',
description => <<EODESCR,
Specify network devices.
MODEL is one of: $nic_model_list_txt
XX:XX:XX:XX:XX:XX should be an unique MAC address. This is
automatically generated if not specified.
The bridge parameter can be used to automatically add the interface to a bridge device. The Proxmox VE standard bridge is called 'vmbr0'.
Option 'rate' is used to limit traffic bandwidth from and to this interface. It is specified as floating point number, unit is 'Megabytes per second'.
If you specify no bridge, we create a kvm 'user' (NATed) network device, which provides DHCP and DNS services. The following addresses are used:
10.0.2.2 Gateway
10.0.2.3 DNS Server
10.0.2.4 SMB Server
The DHCP server assign addresses to the guest starting from 10.0.2.15.
EODESCR
};
PVE::JSONSchema::register_standard_option("pve-qm-net", $netdesc);
for (my $i = 0; $i < $MAX_NETS; $i++) {
$confdesc->{"net$i"} = $netdesc;
}
PVE::JSONSchema::register_format('pve-volume-id-or-qm-path', \&verify_volume_id_or_qm_path);
sub verify_volume_id_or_qm_path {
my ($volid, $noerr) = @_;
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
$volid = eval { PVE::JSONSchema::check_format('pve-volume-id', $volid, '') };
if ($@) {
return undef if $noerr;
die $@;
}
return $volid;
}
my $drivename_hash;
my %drivedesc_base = (
volume => { alias => 'file' },
file => {
type => 'string',
format => 'pve-volume-id-or-qm-path',
default_key => 1,
format_description => 'volume',
description => "The drive's backing volume.",
},
media => {
type => 'string',
format_description => 'cdrom|disk',
enum => [qw(cdrom disk)],
description => "The drive's media type.",
default => 'disk',
optional => 1
},
cyls => {
type => 'integer',
format_description => 'count',
description => "Force the drive's physical geometry to have a specific cylinder count.",
optional => 1
},
heads => {
type => 'integer',
format_description => 'count',
description => "Force the drive's physical geometry to have a specific head count.",
optional => 1
},
secs => {
type => 'integer',
format_description => 'count',
description => "Force the drive's physical geometry to have a specific sector count.",
optional => 1
},
trans => {
type => 'string',
format_description => 'none|lba|auto',
enum => [qw(none lba auto)],
description => "Force disk geometry bios translation mode.",
optional => 1,
},
snapshot => {
type => 'boolean',
format_description => 'on|off',
description => "Whether the drive should be included when making snapshots.",
optional => 1,
},
cache => {
type => 'string',
format_description => 'none|writethrough|writeback|unsafe|directsync',
enum => [qw(none writethrough writeback unsafe directsync)],
description => "The drive's cache mode",
optional => 1,
},
format => {
type => 'string',
format_description => 'drive format',
enum => [qw(raw cow qcow qed qcow2 vmdk cloop)],
description => "The drive's backing file's data format.",
optional => 1,
},
size => {
type => 'string',
format => 'disk-size',
description => "Disk size. This is purely informational and has no effect.",
optional => 1,
},
backup => {
type => 'boolean',
format_description => 'on|off',
description => "Whether the drive should be included when making backups.",
optional => 1,
},
werror => {
type => 'string',
format_description => 'enospc|ignore|report|stop',
enum => [qw(enospc ignore report stop)],
description => 'Write error action.',
optional => 1,
},
aio => {
type => 'string',
format_description => 'native|threads',
enum => [qw(native threads)],
description => 'AIO type to use.',
optional => 1,
},
discard => {
type => 'string',
format_description => 'ignore|on',
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',
format => 'urlencoded',
format_description => 'serial',
maxLength => 20*3, # *3 since it's %xx url enoded
description => "The drive's reported serial number, url-encoded, up to 20 bytes long.",
optional => 1,
}
);
my %rerror_fmt = (
rerror => {
type => 'string',
format_description => 'ignore|report|stop',
enum => [qw(ignore report stop)],
description => 'Read error action.',
optional => 1,
},
);
my %iothread_fmt = ( iothread => {
type => 'boolean',
format_description => 'off|on',
description => "Whether to use iothreads for this drive",
optional => 1,
});
my %model_fmt = (
model => {
type => 'string',
format => 'urlencoded',
format_description => 'model',
maxLength => 40*3, # *3 since it's %xx url enoded
description => "The drive's reported model name, url-encoded, up to 40 bytes long.",
optional => 1,
},
);
my %queues_fmt = (
queues => {
type => 'integer',
format_description => 'nbqueues',
description => "Number of queues.",
minimum => 2,
optional => 1
}
);
my $add_throttle_desc = sub {
my ($key, $type, $what, $size, $longsize) = @_;
$drivedesc_base{$key} = {
type => $type,
format_description => $size,
description => "Maximum $what speed in $longsize per second.",
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');
$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');
$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)
$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');
$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,
};
my $idedesc = {
optional => 1,
type => 'string', format => $ide_fmt,
description => "Use volume as IDE hard disk or CD-ROM (n is 0 to " .($MAX_IDE_DISKS -1) . ").",
};
PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
my $scsi_fmt = {
%drivedesc_base,
%iothread_fmt,
%queues_fmt,
};
my $scsidesc = {
optional => 1,
type => 'string', format => $scsi_fmt,
description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . ").",
};
PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc);
my $sata_fmt = {
%drivedesc_base,
%rerror_fmt,
};
my $satadesc = {
optional => 1,
type => 'string', format => $sata_fmt,
description => "Use volume as SATA hard disk or CD-ROM (n is 0 to " . ($MAX_SATA_DISKS - 1). ").",
};
PVE::JSONSchema::register_standard_option("pve-qm-sata", $satadesc);
my $virtio_fmt = {
%drivedesc_base,
%iothread_fmt,
%rerror_fmt,
};
my $virtiodesc = {
optional => 1,
type => 'string', format => $virtio_fmt,
description => "Use volume as VIRTIO hard disk (n is 0 to " . ($MAX_VIRTIO_DISKS - 1) . ").",
};
PVE::JSONSchema::register_standard_option("pve-qm-virtio", $virtiodesc);
my $alldrive_fmt = {
%drivedesc_base,
%rerror_fmt,
%iothread_fmt,
%model_fmt,
%queues_fmt,
};
my $usb_fmt = {
host => {
default_key => 1,
type => 'string', format => 'pve-qm-usb-device',
format_description => 'HOSTUSBDEVICE|spice',
description => 'The Host USB device or port or the value spice',
},
usb3 => {
optional => 1,
type => 'boolean',
format_description => 'yes|no',
description => 'Specifies whether if given host option is a USB3 device or port',
},
};
my $usbdesc = {
optional => 1,
type => 'string', format => $usb_fmt,
description => <<EODESCR,
Configure an USB device (n is 0 to 4). This can be used to
pass-through usb devices to the guest. 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.
The 'usb3' option determines whether the device is a USB3 device or not (this does currently not work reliably with spice redirection and is then ignored).
EODESCR
};
PVE::JSONSchema::register_standard_option("pve-qm-usb", $usbdesc);
# 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...]',
description => "The PCI ID of a host's PCI device or a list of PCI virtual functions of the host.",
},
rombar => {
type => 'boolean',
optional => 1,
default => 1,
},
pcie => {
type => 'boolean',
optional => 1,
default => 0,
},
'x-vga' => {
type => 'boolean',
optional => 1,
default => 0,
},
};
PVE::JSONSchema::register_format('pve-qm-hostpci', $hostpci_fmt);
my $hostpcidesc = {
optional => 1,
type => 'string', format => 'pve-qm-hostpci',
description => <<EODESCR,
Map host pci devices. HOSTPCIDEVICE syntax is:
'bus:dev.func' (hexadecimal numbers)
You can us the 'lspci' command to list existing pci devices.
The 'rombar' option determines whether or not the device's ROM will be visible in the guest's memory map (default is 'on').
NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
Experimental: user reported problems with this option.
EODESCR
};
PVE::JSONSchema::register_standard_option("pve-qm-hostpci", $hostpcidesc);
my $serialdesc = {
optional => 1,
type => 'string',
pattern => '(/dev/.+|socket)',
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).
NOTE: If you pass through a host serial device, it is no longer possible to migrate such machines - use with special care.
Experimental: user reported problems with this option.
EODESCR
};
my $paralleldesc= {
optional => 1,
type => 'string',
pattern => '/dev/parport\d+|/dev/usb/lp\d+',
description => <<EODESCR,
Map host parallel devices (n is 0 to 2).
NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
Experimental: user reported problems with this option.
EODESCR
};
for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
$confdesc->{"parallel$i"} = $paralleldesc;
}
for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
$confdesc->{"serial$i"} = $serialdesc;
}
for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
$confdesc->{"hostpci$i"} = $hostpcidesc;
}
for (my $i = 0; $i < $MAX_IDE_DISKS; $i++) {
$drivename_hash->{"ide$i"} = 1;
$confdesc->{"ide$i"} = $idedesc;
}
for (my $i = 0; $i < $MAX_SATA_DISKS; $i++) {
$drivename_hash->{"sata$i"} = 1;
$confdesc->{"sata$i"} = $satadesc;
}
for (my $i = 0; $i < $MAX_SCSI_DISKS; $i++) {
$drivename_hash->{"scsi$i"} = 1;
$confdesc->{"scsi$i"} = $scsidesc ;
}
for (my $i = 0; $i < $MAX_VIRTIO_DISKS; $i++) {
$drivename_hash->{"virtio$i"} = 1;
$confdesc->{"virtio$i"} = $virtiodesc;
}
for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
$confdesc->{"usb$i"} = $usbdesc;
}
my $unuseddesc = {
optional => 1,
type => 'string', format => 'pve-volume-id',
description => "Reference to unused volumes.",
};
for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
$confdesc->{"unused$i"} = $unuseddesc;
}
my $kvm_api_version = 0;
sub kvm_version {
return $kvm_api_version if $kvm_api_version;
my $fh = IO::File->new("</dev/kvm") ||
return 0;
if (my $v = $fh->ioctl(KVM_GET_API_VERSION(), 0)) {
$kvm_api_version = $v;
}
$fh->close();
return $kvm_api_version;
}
my $kvm_user_version;
sub kvm_user_version {
return $kvm_user_version if $kvm_user_version;
$kvm_user_version = 'unknown';
my $code = sub {
my $line = shift;
if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
$kvm_user_version = $2;
}
};
eval { run_command("kvm -version", outfunc => $code); };
warn $@ if $@;
return $kvm_user_version;
}
my $kernel_has_vhost_net = -c '/dev/vhost-net';
sub valid_drive_names {
# order is important - used to autoselect boot disk
return ((map { "ide$_" } (0 .. ($MAX_IDE_DISKS - 1))),
(map { "scsi$_" } (0 .. ($MAX_SCSI_DISKS - 1))),
(map { "virtio$_" } (0 .. ($MAX_VIRTIO_DISKS - 1))),
(map { "sata$_" } (0 .. ($MAX_SATA_DISKS - 1))));
}
sub is_valid_drivename {
my $dev = shift;
return defined($drivename_hash->{$dev});
}
sub option_exists {
my $key = shift;
return defined($confdesc->{$key});
}
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',
win8 => 'Windows 8/2012',
l24 => 'Linux 2.4',
l26 => 'Linux 2.6',
};
}
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 {
return PVE::Storage::path($storecfg, $cdrom);
}
}
# 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/^([^:]+):(.+)$/)) {
return undef if $file =~ m|/|;
if ($media && $media eq 'cdrom') {
$file = "local:iso/$file";
} else {
$file = "local:$vmid/$file";
}
}
return $file;
}
sub verify_media_type {
my ($opt, $vtype, $media) = @_;
return if !$media;
my $etype;
if ($media eq 'disk') {
$etype = 'images';
} elsif ($media eq 'cdrom') {
$etype = 'iso';
} else {
die "internal error";
}
return if ($vtype eq $etype);
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/^([^:]+):(.+)$/) &&
($drive->{file} !~ m/^\d+$/)) {
my ($vtype, $volid) = PVE::Storage::path_to_volume_id($storecfg, $drive->{file});
raise_param_exc({ $opt => "unable to associate path '$drive->{file}' to any storage"}) if !$vtype;
$drive->{media} = 'cdrom' if !$drive->{media} && $vtype eq 'iso';
verify_media_type($opt, $vtype, $drive->{media});
$drive->{file} = $volid;
}
$drive->{media} = 'cdrom' if !$drive->{media} && $drive->{file} =~ m/^(cdrom|none)$/;
}
sub parse_hotplug_features {
my ($data) = @_;
my $res = {};
return $res if $data eq '0';
$data = $confdesc->{hotplug}->{default} if $data eq '1';
foreach my $feature (PVE::Tools::split_list($data)) {
if ($feature =~ m/^(network|disk|cpu|memory|usb)$/) {
$res->{$1} = 1;
} else {
warn "ignoring unknown hotplug feature '$feature'\n";
}
}
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";
}
# 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]
# [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
# [,aio=native|threads][,discard=ignore|on][,detect_zeroes=on|off]
# [,iothread=on][,serial=serial][,model=model]
sub parse_drive {
my ($key, $data) = @_;
my ($interface, $index);
if ($key =~ m/^([^\d]+)(\d+)$/) {
$interface = $1;
$index = $2;
} else {
return undef;
}
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;
}
$res->{"m$opt"} = sprintf("%.3f", $bps / (1024*1024.0));
}
}
return undef if $error;
return undef if $res->{mbps_rd} && $res->{mbps};
return undef if $res->{mbps_wr} && $res->{mbps};
return undef if $res->{iops_rd} && $res->{iops};
return undef if $res->{iops_wr} && $res->{iops};
if ($res->{media} && ($res->{media} eq 'cdrom')) {
return undef if $res->{snapshot} || $res->{trans} || $res->{format};
return undef if $res->{heads} || $res->{secs} || $res->{cyls};
return undef if $res->{interface} eq 'virtio';
}
if (my $size = $res->{size}) {
return undef if !defined($res->{size} = PVE::JSONSchema::parse_size($size));
}
return $res;
}
sub print_drive {
my ($vmid, $drive) = @_;
my $data = { %$drive };
delete $data->{$_} for qw(index interface);
return PVE::JSONSchema::print_property_string($data, $alldrive_fmt);
}
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;
}
my $version = unpack("I", $versionbuf);
if ($version < 30000) {
die "scsi generic interface too old\n" if !$noerr;
return undef;
}
my $buf = "\x00" x 36;
my $sensebuf = "\x00" x 8;
my $cmd = pack("C x3 C x1", 0x12, 36);
# 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";
my $packet = pack($sg_io_hdr_t, ord('S'), -3, length($cmd),
length($sensebuf), 0, length($buf), $buf,
$cmd, $sensebuf, 6000);
$ret = ioctl($fh, $SG_IO, $packet);
if (!$ret) {
die "scsi ioctl SG_IO failed - $!\n" if !$noerr;
return undef;
}
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 = {};
(my $byte0, my $byte1, $res->{vendor},
$res->{product}, $res->{revision}) = unpack("C C x6 A8 A16 A4", $buf);
$res->{removable} = $byte1 & 128 ? 1 : 0;
$res->{type} = $byte0 & 31;
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;
}
sub machine_type_is_q35 {
my ($conf) = @_;
return $conf->{machine} && ($conf->{machine} =~ m/q35/) ? 1 : 0;
}
sub print_tabletdevice_full {
my ($conf) = @_;
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";
return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
}
sub print_drivedevice_full {
my ($storecfg, $conf, $vmid, $drive, $bridges) = @_;
my $device = '';
my $maxdev = 0;
if ($drive->{interface} eq 'virtio') {
my $pciaddr = print_pci_addr("$drive->{interface}$drive->{index}", $bridges);
$device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
$device .= ",iothread=iothread-$drive->{interface}$drive->{index}" if $drive->{iothread};
} elsif ($drive->{interface} eq 'scsi') {
my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
my $unit = $drive->{index} % $maxdev;
my $devicetype = 'hd';
my $path = '';
if (drive_is_cdrom($drive)) {
$devicetype = 'cd';
} else {
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}";
}
} 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";
$device = "ide-$devicetype,bus=ide.$controller,unit=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
if ($devicetype eq 'hd' && (my $model = $drive->{model})) {
$model = URI::Escape::uri_unescape($model);
$device .= ",model=$model";
}
} 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}";
} 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";
}
$device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex};
return $device;
}
sub get_initiator_name {
my $initiator;
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]+)/;
$initiator = $1;
last;
}
$fh->close();
return $initiator;
}
sub print_drive_full {
my ($storecfg, $vmid, $drive) = @_;
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;
$format = "raw";
}
}
my $opts = '';
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);
foreach my $o (@qemu_drive_options) {
$opts .= ",$o=$drive->{$o}" if $drive->{$o};
}
if (my $serial = $drive->{serial}) {
$serial = URI::Escape::uri_unescape($serial);
$opts .= ",serial=$serial";
}
$opts .= ",format=$format" if $format && !$drive->{format};
foreach my $o (qw(bps bps_rd bps_wr)) {
my $v = $drive->{"m$o"};
$opts .= ",$o=" . int($v*1024*1024) if $v;
}
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";
}
}
if (!drive_is_cdrom($drive)) {
my $detectzeroes;
if (defined($drive->{detect_zeroes}) && !$drive->{detect_zeroes}) {
$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;
}
my $pathinfo = $path ? "file=$path," : '';
return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
}
sub print_netdevice_full {
my ($vmid, $conf, $net, $netid, $bridges, $use_old_bios_files) = @_;
my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
my $device = $net->{model};
if ($net->{model} eq 'virtio') {
$device = 'virtio-net-pci';
};
my $pciaddr = print_pci_addr("$netid", $bridges);
my $tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
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";
}
$tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex} ;
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;
}
return $tmpstr;
}
sub print_netdev_full {
my ($vmid, $conf, $net, $netid, $hotplug) = @_;
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";
my $netdev = "";
my $script = $hotplug ? "pve-bridge-hotplug" : "pve-bridge";
if ($net->{bridge}) {
$netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/$script,downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
} else {
$netdev = "type=user,id=$netid,hostname=$vmname";
}
$netdev .= ",queues=$net->{queues}" if ($net->{queues} && $net->{model} eq 'virtio');
return $netdev;
}
sub drive_is_cdrom {
my ($drive) = @_;
return $drive && $drive->{media} && ($drive->{media} eq 'cdrom');
}
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 ];
} else {
die "invalid range: $part\n";
}
}
return $res;
}
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});
return $res;
}
sub parse_hostpci {
my ($value) = @_;
return undef if !$value;
my $res = PVE::JSONSchema::parse_property_string($hostpci_fmt, $value);
my @idlist = split(/;/, $res->{host});
delete $res->{host};
foreach my $id (@idlist) {
if ($id =~ /^$PCIRE$/) {
push @{$res->{pciid}}, { id => $1, function => ($2//'0') };
} else {
# should have been caught by parse_property_string already
die "failed to parse PCI id: $id\n";
}
}
return $res;
}
# netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
sub parse_net {
my ($data) = @_;
my $res = eval { PVE::JSONSchema::parse_property_string($net_fmt, $data) };
if ($@) {
warn $@;
return undef;
}
$res->{macaddr} = PVE::Tools::random_ether_addr() if !defined($res->{macaddr});
return $res;
}
sub print_net {
my $net = shift;
return PVE::JSONSchema::print_property_string($net, $net_fmt);
}
sub add_random_macs {
my ($settings) = @_;
foreach my $opt (keys %$settings) {
next if $opt !~ m/^net(\d+)$/;
my $net = parse_net($settings->{$opt});
next if !$net;
$settings->{$opt} = print_net($net);
}
}
sub 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;
}
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;
}
sub vmconfig_delete_pending_option {
my ($conf, $key, $force) = @_;
delete $conf->{pending}->{$key};
my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
$pending_delete_hash->{$key} = $force ? '!' : '';
$conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
}
sub vmconfig_undelete_pending_option {
my ($conf, $key) = @_;
my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
delete $pending_delete_hash->{$key};
if (%$pending_delete_hash) {
$conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
} 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)) {
PVE::QemuConfig->add_unused_volume($conf, $volid, $vmid);
}
}
}
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};
}
}
my $current_delete_hash = split_flagged_list($conf->{pending}->{delete});
my $pending_delete_hash = {};
while (my ($opt, $force) = each %$current_delete_hash) {
if (defined($conf->{$opt})) {
$pending_delete_hash->{$opt} = $force;
} else {
$changes = 1;
}
}
if (%$pending_delete_hash) {
$conf->{pending}->{delete} = join_flagged_list(',', $pending_delete_hash);
} else {
delete $conf->{pending}->{delete};
}
return $changes;
}
# smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str]
my $smbios1_fmt = {
uuid => {
type => 'string',
pattern => '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
format_description => 'UUID',
optional => 1,
},
version => {
type => 'string',
pattern => '\S+',
format_description => 'str',
optional => 1,
},
serial => {
type => 'string',
pattern => '\S+',
format_description => 'str',
optional => 1,
},
manufacturer => {
type => 'string',
pattern => '\S+',
format_description => 'name',
optional => 1,
},
product => {
type => 'string',
pattern => '\S+',
format_description => 'name',
optional => 1,
},
sku => {
type => 'string',
pattern => '\S+',
format_description => 'str',
optional => 1,
},
family => {
type => 'string',
pattern => '\S+',
format_description => 'str',
optional => 1,
},
};
sub parse_smbios1 {
my ($data) = @_;
my $res = eval { PVE::JSONSchema::parse_property_string($smbios1_fmt, $data) };
warn $@ if $@;
return $res;
}
sub print_smbios1 {
my ($smbios1) = @_;
return PVE::JSONSchema::print_property_string($smbios1, $smbios1_fmt);
}
PVE::JSONSchema::register_format('pve-qm-smbios1', $smbios1_fmt);
PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk);
sub verify_bootdisk {
my ($value, $noerr) = @_;
return $value if is_valid_drivename($value);
return undef if $noerr;
die "invalid boot disk '$value'\n";
}
PVE::JSONSchema::register_format('pve-qm-net', \&verify_net);
sub verify_net {
my ($value, $noerr) = @_;
return $value if parse_net($value);
return undef if $noerr;
die "unable to parse network options\n";
}
sub parse_watchdog {
my ($value) = @_;
return undef if !$value;
my $res = eval { PVE::JSONSchema::parse_property_string($watchdog_fmt, $value) };
warn $@ if $@;
return $res;
}
sub parse_usb_device {
my ($value) = @_;
return undef if !$value;
my $res = {};
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;
}
return $res;
}
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;
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) {
next if $opt eq 'parent' || $opt eq 'snaptime' || $opt eq 'vmstate';
$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};
if (!defined($value)) {
die "got undefined value\n";
}
if ($value =~ m/[\n\r]/) {
die "property contains a line feed\n";
}
if ($type eq 'boolean') {
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";
} elsif ($type eq 'integer') {
return int($1) if $value =~ m/^(\d+)$/;
die "type check ('integer') failed - got '$value'\n";
} elsif ($type eq 'number') {
return $value if $value =~ m/^(\d+)(\.\d+)?$/;
die "type check ('number') failed - got '$value'\n";
} elsif ($type eq 'string') {
if (my $fmt = $confdesc->{$key}->{format}) {
if ($fmt eq 'pve-qm-drive') {
# special case - we need to pass $key to parse_drive()
my $drive = parse_drive($key, $value);
return $value if $drive;
die "unable to parse drive options\n";
}
PVE::JSONSchema::check_format($fmt, $value);
return $value;
}
$value =~ s/^\"(.*)\"$/$1/;
return $value;
} else {
die "internal error"
}
}
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;
}
sub touch_config {
my ($vmid) = @_;
my $conf = PVE::QemuConfig->config_file($vmid);
utime undef, undef, $conf;
}
sub destroy_vm {
my ($storecfg, $vmid, $keep_empty_config, $skiplock) = @_;
my $conffile = PVE::QemuConfig->config_file($vmid);
my $conf = PVE::QemuConfig->load_config($vmid);
PVE::QemuConfig->check_lock($conf) if !$skiplock;
# only remove disks owned by this VM
foreach_drive($conf, 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($storecfg, $volid);
return if !$path || !$owner || ($owner != $vmid);
PVE::Storage::vdisk_free($storecfg, $volid);
});
if ($keep_empty_config) {
PVE::Tools::file_set_contents($conffile, "memory: 128\n");
} else {
unlink $conffile;
}
# also remove unused disk
eval {
my $dl = PVE::Storage::vdisk_list($storecfg, undef, $vmid);
eval {
PVE::Storage::foreach_volid($dl, sub {
my ($volid, $sid, $volname, $d) = @_;
PVE::Storage::vdisk_free($storecfg, $volid);
});
};
warn $@ if $@;
};
warn $@ if $@;
}
sub parse_vm_config {
my ($filename, $raw) = @_;
return undef if !defined($raw);
my $res = {
digest => Digest::SHA::sha1_hex($raw),
snapshots => {},
pending => {},
};
$filename =~ m|/qemu-server/(\d+)\.conf$|
|| die "got strange filename '$filename'";
my $vmid = $1;
my $conf = $res;
my $descr;
my $section = '';
my @lines = split(/\n/, $raw);
foreach my $line (@lines) {
next if $line =~ m/^\s*$/;
if ($line =~ m/^\[PENDING\]\s*$/i) {
$section = 'pending';
if (defined($descr)) {
$descr =~ s/\s+$//;
$conf->{description} = $descr;
}
$descr = undef;
$conf = $res->{$section} = {};
next;
} elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
$section = $1;
if (defined($descr)) {
$descr =~ s/\s+$//;
$conf->{description} = $descr;
}
$descr = undef;
$conf = $res->{snapshots}->{$section} = {};
next;
}
if ($line =~ m/^\#(.*)\s*$/) {
$descr = '' if !defined($descr);
$descr .= PVE::Tools::decode_text($1) . "\n";
next;
}
if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
$descr = '' if !defined($descr);
$descr .= PVE::Tools::decode_text($2);
} elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
$conf->{snapstate} = $1;
} elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
my $key = $1;
my $value = $2;
$conf->{$key} = $value;
} elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
my $value = $1;
if ($section eq 'pending') {
$conf->{delete} = $value; # we parse this later
} else {
warn "vm $vmid - propertry 'delete' is only allowed in [PENDING]\n";
}
} 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;
$value = print_drive($vmid, $v);
} else {
warn "vm $vmid - unable to parse value of '$key'\n";
next;
}
}
if ($key eq 'cdrom') {
$conf->{ide2} = $value;
} else {
$conf->{$key} = $value;
}
}
}
}
if (defined($descr)) {
$descr =~ s/\s+$//;
$conf->{description} = $descr;
}
delete $res->{snapstate}; # just to be sure
return $res;
}
sub write_vm_config {
my ($filename, $conf) = @_;
delete $conf->{snapstate}; # just to be sure
if ($conf->{cdrom}) {
die "option ide2 conflicts with cdrom\n" if $conf->{ide2};
$conf->{ide2} = $conf->{cdrom};
delete $conf->{cdrom};
}
# we do not use 'smp' any longer
if ($conf->{sockets}) {
delete $conf->{smp};
} elsif ($conf->{smp}) {
$conf->{sockets} = $conf->{smp};
delete $conf->{cores};
delete $conf->{smp};
}
my $used_volids = {};
my $cleanup_config = sub {
my ($cref, $pending, $snapname) = @_;
foreach my $key (keys %$cref) {
next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
$key eq 'snapstate' || $key eq 'pending';
my $value = $cref->{$key};
if ($key eq 'delete') {
die "propertry 'delete' is only allowed in [PENDING]\n"
if !$pending;
# fixme: check syntax?
next;
}
eval { $value = check_type($key, $value); };
die "unable to parse value of '$key' - $@" if $@;
$cref->{$key} = $value;
if (!$snapname && is_valid_drivename($key)) {
my $drive = parse_drive($key, $value);
$used_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
}
}
};
&$cleanup_config($conf);
&$cleanup_config($conf->{pending}, 1);
foreach my $snapname (keys %{$conf->{snapshots}}) {
die "internal error" if $snapname eq 'pending';
&$cleanup_config($conf->{snapshots}->{$snapname}, undef, $snapname);
}
# remove 'unusedX' settings if we re-add a volume
foreach my $key (keys %$conf) {
my $value = $conf->{$key};
if ($key =~ m/^unused/ && $used_volids->{$value}) {
delete $conf->{$key};
}
}
my $generate_raw_config = sub {
my ($conf, $pending) = @_;
my $raw = '';
# add description as comment to top of file
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;
}
}
foreach my $key (sort keys %$conf) {
next if $key eq 'digest' || $key eq 'description' || $key eq 'pending' || $key eq 'snapshots';
$raw .= "$key: $conf->{$key}\n";
}
return $raw;
};
my $raw = &$generate_raw_config($conf);
if (scalar(keys %{$conf->{pending}})){
$raw .= "\n[PENDING]\n";
$raw .= &$generate_raw_config($conf->{pending}, 1);
}
foreach my $snapname (sort keys %{$conf->{snapshots}}) {
$raw .= "\n[$snapname]\n";
$raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
}
return $raw;
}
sub load_defaults {
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;
}
}
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;
next if !$d->{type} || $d->{type} ne 'qemu';
$res->{$vmid}->{exists} = 1;
}
return $res;
}
# test if VM uses local resources (to prevent migration)
sub check_local_resources {
my ($conf, $noerr) = @_;
my $loc_res = 0;
$loc_res = 1 if $conf->{hostusb}; # old syntax
$loc_res = 1 if $conf->{hostpci}; # old syntax
foreach my $k (keys %$conf) {
next if $k =~ m/^usb/ && ($conf->{$k} eq 'spice');
# sockets are safe: they will recreated be on the target side post-migrate
next if $k =~ m/^serial/ && ($conf->{$k} eq 'socket');
$loc_res = 1 if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
}
die "VM uses local resources\n" if $loc_res && !$noerr;
return $loc_res;
}
# check if used storages are available on all nodes (use by migrate)
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);
});
}
# 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();
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
}
sub check_cmdline {
my ($pidfile, $pid) = @_;
my $fh = IO::File->new("/proc/$pid/cmdline", "r");
if (defined($fh)) {
my $line = <$fh>;
$fh->close;
return undef if !$line;
my @param = split(/\0/, $line);
my $cmd = $param[0];
return if !$cmd || ($cmd !~ m|kvm$| && $cmd !~ m|qemu-system-x86_64$|);
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 {
my ($vmid, $nocheck, $node) = @_;
my $filename = PVE::QemuConfig->config_file($vmid, $node);
die "unable to find configuration file for VM $vmid - no such machine\n"
if !$nocheck && ! -f $filename;
my $pidfile = pidfile_name($vmid);
if (my $fd = IO::File->new("<$pidfile")) {
my $st = stat($fd);
my $line = <$fd>;
close($fd);
my $mtime = $st->mtime;
if ($mtime > time()) {
warn "file '$filename' modified in future\n";
}
if ($line =~ m/^(\d+)$/) {
my $pid = $1;
if (check_cmdline($pidfile, $pid)) {
if (my $pinfo = PVE::ProcFSTools::check_process_running($pid)) {
return $pid;
}
}
}
}
return undef;
}
sub vzlist {
my $vzlist = config_list();
my $fd = IO::Dir->new($var_run_tmpdir) || return $vzlist;
while (defined(my $de = $fd->read)) {
next if $de !~ m/^(\d+)\.pid$/;
my $vmid = $1;
next if !defined($vzlist->{$vmid});
if (my $pid = check_running($vmid)) {
$vzlist->{$vmid}->{pid} = $pid;
}
}
return $vzlist;
}
sub disksize {
my ($storecfg, $conf) = @_;
my $bootdisk = $conf->{bootdisk};
return undef if !$bootdisk;
return undef if !is_valid_drivename($bootdisk);
return undef if !$conf->{$bootdisk};
my $drive = parse_drive($bootdisk, $conf->{$bootdisk});
return undef if !defined($drive);
return undef if drive_is_cdrom($drive);
my $volid = $drive->{file};
return undef if !$volid;
return $drive->{size};
}
my $last_proc_pid_stat;
# 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)
sub vmstatus {
my ($opt_vmid, $full) = @_;
my $res = {};
my $storecfg = PVE::Storage::config();
my $list = vzlist();
my ($uptime) = PVE::ProcFSTools::read_proc_uptime(1);
my $cpucount = $cpuinfo->{cpus} || 1;
foreach my $vmid (keys %$list) {
next if $opt_vmid && ($vmid ne $opt_vmid);
my $cfspath = PVE::QemuConfig->cfs_config_path($vmid);
my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
my $d = {};
$d->{pid} = $list->{$vmid}->{pid};
# fixme: better status?
$d->{status} = $list->{$vmid}->{pid} ? 'running' : 'stopped';
my $size = disksize($storecfg, $conf);
if (defined($size)) {
$d->{disk} = 0; # no info available
$d->{maxdisk} = $size;
} else {
$d->{disk} = 0;
$d->{maxdisk} = 0;
}
$d->{cpus} = ($conf->{sockets} || 1) * ($conf->{cores} || 1);
$d->{cpus} = $cpucount if $d->{cpus} > $cpucount;
$d->{cpus} = $conf->{vcpus} if $conf->{vcpus};
$d->{name} = $conf->{name} || "VM $vmid";
$d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024) : 0;
if ($conf->{balloon}) {
$d->{balloon_min} = $conf->{balloon}*(1024*1024);
$d->{shares} = defined($conf->{shares}) ? $conf->{shares} : 1000;
}
$d->{uptime} = 0;
$d->{cpu} = 0;
$d->{mem} = 0;
$d->{netout} = 0;
$d->{netin} = 0;
$d->{diskread} = 0;
$d->{diskwrite} = 0;
$d->{template} = PVE::QemuConfig->is_template($conf);
$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;
$d->{netout} += $netdev->{$dev}->{receive};
$d->{netin} += $netdev->{$dev}->{transmit};
if ($full) {
$d->{nics}->{$dev}->{netout} = $netdev->{$dev}->{receive};
$d->{nics}->{$dev}->{netin} = $netdev->{$dev}->{transmit};
}
}
my $ctime = gettimeofday;
foreach my $vmid (keys %$list) {
my $d = $res->{$vmid};
my $pid = $d->{pid};
next if !$pid;
my $pstat = PVE::ProcFSTools::read_proc_pid_stat($pid);
next if !$pstat; # not running
my $used = $pstat->{utime} + $pstat->{stime};
$d->{uptime} = int(($uptime - $pstat->{starttime})/$cpuinfo->{user_hz});
if ($pstat->{vsize}) {
$d->{mem} = int(($pstat->{rss}/$pstat->{vsize})*$d->{maxmem});
}
my $old = $last_proc_pid_stat->{$pid};
if (!$old) {
$last_proc_pid_stat->{$pid} = {
time => $ctime,
used => $used,
cpu => 0,
};
next;
}
my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz};
if ($dtime > 1000) {
my $dutime = $used - $old->{used};
$d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus};
$last_proc_pid_stat->{$pid} = {
time => $ctime,
used => $used,
cpu => $d->{cpu},
};
} else {
$d->{cpu} = $old->{cpu};
}
}
return $res if !$full;
my $qmpclient = PVE::QMPClient->new();
my $ballooncb = sub {
my ($vmid, $resp) = @_;
my $info = $resp->{'return'};
return if !$info->{max_mem};
my $d = $res->{$vmid};
# 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};
}
$d->{ballooninfo} = $info;
};
my $blockstatscb = sub {
my ($vmid, $resp) = @_;
my $data = $resp->{'return'} || [];
my $totalrdbytes = 0;
my $totalwrbytes = 0;
for my $blockstat (@$data) {
$totalrdbytes = $totalrdbytes + $blockstat->{stats}->{rd_bytes};
$totalwrbytes = $totalwrbytes + $blockstat->{stats}->{wr_bytes};
$blockstat->{device} =~ s/drive-//;
$res->{$vmid}->{blockstat}->{$blockstat->{device}} = $blockstat->{stats};
}
$res->{$vmid}->{diskread} = $totalrdbytes;
$res->{$vmid}->{diskwrite} = $totalwrbytes;
};
my $statuscb = sub {
my ($vmid, $resp) = @_;
$qmpclient->queue_cmd($vmid, $blockstatscb, 'query-blockstats');
# this fails if ballon driver is not loaded, so this must be
# the last commnand (following command are aborted if this fails).
$qmpclient->queue_cmd($vmid, $ballooncb, 'query-balloon');
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');
}
$qmpclient->queue_execute(undef, 1);
foreach my $vmid (keys %$list) {
next if $opt_vmid && ($vmid ne $opt_vmid);
$res->{$vmid}->{qmpstatus} = $res->{$vmid}->{status} if !$res->{$vmid}->{qmpstatus};
}
return $res;
}
sub foreach_dimm {
my ($conf, $vmid, $memory, $sockets, $func) = @_;
my $dimm_id = 0;
my $current_size = 1024;
my $dimm_size = 512;
return if $current_size == $memory;
for (my $j = 0; $j < 8; $j++) {
for (my $i = 0; $i < 32; $i++) {
my $name = "dimm${dimm_id}";
$dimm_id++;
my $numanode = $i % $sockets;
$current_size += $dimm_size;
&$func($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory);
return $current_size if $current_size >= $memory;
}
$dimm_size *= 2;
}
}
sub foreach_reverse_dimm {
my ($conf, $vmid, $memory, $sockets, $func) = @_;
my $dimm_id = 253;
my $current_size = 4177920;
my $dimm_size = 65536;
return if $current_size == $memory;
for (my $j = 0; $j < 8; $j++) {
for (my $i = 0; $i < 32; $i++) {
my $name = "dimm${dimm_id}";
$dimm_id--;
my $numanode = $i % $sockets;
$current_size -= $dimm_size;
&$func($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory);
return $current_size if $current_size <= $memory;
}
$dimm_size /= 2;
}
}
sub foreach_drive {
my ($conf, $func) = @_;
foreach my $ds (valid_drive_names()) {
next if !defined($conf->{$ds});
my $drive = parse_drive($ds, $conf->{$ds});
next if !$drive;
&$func($ds, $drive);
}
}
sub foreach_volid {
my ($conf, $func) = @_;
my $volhash = {};
my $test_volid = sub {
my ($volid, $is_cdrom) = @_;
return if !$volid;
$volhash->{$volid} = $is_cdrom || 0;
};
foreach_drive($conf, sub {
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);
foreach_drive($snap, sub {
my ($ds, $drive) = @_;
&$test_volid($drive->{file}, drive_is_cdrom($drive));
});
}
foreach my $volid (keys %$volhash) {
&$func($volid, $volhash->{$volid});
}
}
sub vga_conf_has_spice {
my ($vga) = @_;
return 0 if !$vga || $vga !~ m/^qxl([234])?$/;
return $1 || 1;
}
sub config_to_command {
my ($storecfg, $vmid, $conf, $defaults, $forcemachine) = @_;
my $cmd = [];
my $globalFlags = [];
my $machineFlags = [];
my $rtcFlags = [];
my $cpuFlags = [];
my $devices = [];
my $pciaddr = '';
my $bridges = {};
my $kvmver = kvm_user_version();
my $vernum = 0; # unknown
my $ostype = $conf->{ostype};
if ($kvmver =~ m/^(\d+)\.(\d+)$/) {
$vernum = $1*1000000+$2*1000;
} elsif ($kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/) {
$vernum = $1*1000000+$2*1000+$3;
}
die "detected old qemu-kvm binary ($kvmver)\n" if $vernum < 15000;
my $have_ovz = -f '/proc/vz/vestat';
my $q35 = machine_type_is_q35($conf);
my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
my $machine_type = $forcemachine || $conf->{machine};
my $use_old_bios_files = undef;
($use_old_bios_files, $machine_type) = qemu_use_old_bios_files($machine_type);
my $cpuunits = defined($conf->{cpuunits}) ?
$conf->{cpuunits} : $defaults->{cpuunits};
push @$cmd, '/usr/bin/systemd-run';
push @$cmd, '--scope';
push @$cmd, '--slice', "qemu";
push @$cmd, '--unit', $vmid;
# set KillMode=none, so that systemd don't kill those scopes
# at shutdown (pve-manager service should stop the VMs instead)
push @$cmd, '-p', "KillMode=none";
push @$cmd, '-p', "CPUShares=$cpuunits";
if ($conf->{cpulimit}) {
my $cpulimit = int($conf->{cpulimit} * 100);
push @$cmd, '-p', "CPUQuota=$cpulimit\%";
}
push @$cmd, '/usr/bin/kvm';
push @$cmd, '-id', $vmid;
my $use_virtio = 0;
my $qmpsocket = qmp_socket($vmid);
push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server,nowait";
push @$cmd, '-mon', "chardev=qmp,mode=control";
push @$cmd, '-pidfile' , pidfile_name($vmid);
push @$cmd, '-daemonize';
if ($conf->{smbios1}) {
push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
}
if ($conf->{bios} && $conf->{bios} eq 'ovmf') {
my $ovmfvar = "OVMF_VARS-pure-efi.fd";
my $ovmfvar_src = "/usr/share/kvm/$ovmfvar";
my $ovmfvar_dst = "/tmp/$vmid-$ovmfvar";
PVE::Tools::file_copy($ovmfvar_src, $ovmfvar_dst, 256*1024);
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";
}
if ($q35) {
# the q35 chipset support native usb2, so we enable usb controller
# by default for this machine type
push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
} else {
$pciaddr = print_pci_addr("piix3", $bridges);
push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
my $use_usb2 = 0;
for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
next if !$conf->{"usb$i"};
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
$use_usb2 = 1;
}
# include usb device config
push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
}
# add usb3 controller if needed
my $use_usb3 = 0;
for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
next if !$conf->{"usb$i"};
my $d = eval { PVE::JSONSchema::parse_property_string($usbdesc->{format},$conf->{"usb$i"}) };
next if !$d || !$d->{usb3};
$use_usb3 = 1;
}
$pciaddr = print_pci_addr("xhci", $bridges);
push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr" if $use_usb3;
my $vga = $conf->{vga};
my $qxlnum = vga_conf_has_spice($vga);
$vga = 'qxl' if $qxlnum;
if (!$vga) {
if ($conf->{ostype} && ($conf->{ostype} eq 'win8' ||
$conf->{ostype} eq 'win7' ||
$conf->{ostype} eq 'w2k8')) {
$vga = 'std';
} else {
$vga = 'cirrus';
}
}
# enable absolute mouse coordinates (needed by vnc)
my $tablet;
if (defined($conf->{tablet})) {
$tablet = $conf->{tablet};
} else {
$tablet = $defaults->{tablet};
$tablet = 0 if $qxlnum; # disable for spice because it is not needed
$tablet = 0 if $vga =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
}
push @$devices, '-device', print_tabletdevice_full($conf) if $tablet;
my $kvm_off = 0;
# host pci devices
for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
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);
}
my $rombar = defined($d->{rombar}) && !$d->{rombar} ? ',rombar=0' : '';
my $xvga = '';
if ($d->{'x-vga'}) {
$xvga = ',x-vga=on';
$kvm_off = 1;
$vga = 'none';
if ($ostype eq 'win7' || $ostype eq 'win8' || $ostype eq 'w2k8') {
push @$cpuFlags , 'hv_vendor_id=proxmox';
}
if ($conf->{bios} && $conf->{bios} eq 'ovmf') {
$xvga = "";
}
}
my $pcidevices = $d->{pciid};
my $multifunction = 1 if @$pcidevices > 1;
my $j=0;
foreach my $pcidevice (@$pcidevices) {
my $id = "hostpci$i";
$id .= ".$j" if $multifunction;
my $addr = $pciaddr;
$addr .= ".$j" if $multifunction;
my $devicestr = "vfio-pci,host=$pcidevice->{id}.$pcidevice->{function},id=$id$addr";
if($j == 0){
$devicestr .= "$rombar$xvga";
$devicestr .= ",multifunction=on" if $multifunction;
}
push @$devices, '-device', $devicestr;
$j++;
}
}
# usb devices
for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
next if !$conf->{"usb$i"};
my $d = eval { PVE::JSONSchema::parse_property_string($usbdesc->{format},$conf->{"usb$i"}) };
next if !$d;
# if it is a usb3 device, attach it to the xhci controller, else omit the bus option
my $usbbus = '';
if (defined($d->{usb3}) && $d->{usb3}) {
$usbbus = ',bus=xhci.0';
}
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";
}
}
}
# serial devices
for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
if (my $path = $conf->{"serial$i"}) {
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";
}
}
}
# parallel devices
for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
if (my $path = $conf->{"parallel$i"}) {
die "no such parallel device\n" if ! -c $path;
my $devtype = $path =~ m!^/dev/usb/lp! ? 'tty' : 'parport';
push @$devices, '-chardev', "$devtype,id=parallel$i,path=$path";
push @$devices, '-device', "isa-parallel,chardev=parallel$i";
}
}
my $vmname = $conf->{name} || "vm$vmid";
push @$cmd, '-name', $vmname;
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;
my $vcpus = $conf->{vcpus} ? $conf->{vcpus} : $maxcpus;
my $allowed_vcpus = $cpuinfo->{cpus};
die "MAX $allowed_vcpus vcpus allowed per VM on this node\n"
if ($allowed_vcpus < $maxcpus);
push @$cmd, '-smp', "$vcpus,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
push @$cmd, '-nodefaults';
my $bootorder = $conf->{boot} || $confdesc->{boot}->{default};
my $bootindex_hash = {};
my $i = 1;
foreach my $o (split(//, $bootorder)) {
$bootindex_hash->{$o} = $i*100;
$i++;
}
push @$cmd, '-boot', "menu=on,strict=on,reboot-timeout=1000";
push @$cmd, '-no-acpi' if defined($conf->{acpi}) && $conf->{acpi} == 0;
push @$cmd, '-no-reboot' if defined($conf->{reboot}) && $conf->{reboot} == 0;
push @$cmd, '-vga', $vga if $vga && $vga !~ m/^serial\d+$/; # for kvm 77 and later
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';
}
# time drift fix
my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
my $useLocaltime = $conf->{localtime};
if ($ostype) {
# other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26, solaris
if ($ostype =~ m/^w/) { # windows
$useLocaltime = 1 if !defined($conf->{localtime});
# use time drift fix when acpi is enabled
if (!(defined($conf->{acpi}) && $conf->{acpi} == 0)) {
$tdf = 1 if !defined($conf->{tdf});
}
}
if ($ostype eq 'win7' || $ostype eq 'win8' || $ostype eq 'w2k8' ||
$ostype eq 'wvista') {
push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
push @$cmd, '-no-hpet';
if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
push @$cpuFlags , 'hv_spinlocks=0x1fff' if !$nokvm;
push @$cpuFlags , 'hv_vapic' if !$nokvm;
push @$cpuFlags , 'hv_time' if !$nokvm;
} else {
push @$cpuFlags , 'hv_spinlocks=0xffff' if !$nokvm;
}
}
if ($ostype eq 'win7' || $ostype eq 'win8') {
push @$cpuFlags , 'hv_relaxed' if !$nokvm;
}
}
push @$rtcFlags, 'driftfix=slew' if $tdf;
if ($nokvm) {
push @$machineFlags, 'accel=tcg';
} else {
die "No accelerator found!\n" if !$cpuinfo->{hvm};
}
if ($machine_type) {
push @$machineFlags, "type=${machine_type}";
}
if ($conf->{startdate}) {
push @$rtcFlags, "base=$conf->{startdate}";
} elsif ($useLocaltime) {
push @$rtcFlags, 'base=localtime';
}
my $cpu = $nokvm ? "qemu64" : "kvm64";
if (my $cputype = $conf->{cpu}) {
my $cpuconf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cputype)
or die "Cannot parse cpu description: $cputype\n";
$cpu = $cpuconf->{cputype};
$kvm_off = 1 if $cpuconf->{hidden};
}
push @$cpuFlags , '+lahf_lm' if $cpu eq 'kvm64';
push @$cpuFlags , '-x2apic'
if $conf->{ostype} && $conf->{ostype} eq 'solaris';
push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
push @$cpuFlags, '-rdtscp' if $cpu =~ m/^Opteron/;
if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
push @$cpuFlags , '+kvm_pv_unhalt' if !$nokvm;
push @$cpuFlags , '+kvm_pv_eoi' if !$nokvm;
}
push @$cpuFlags, 'enforce' if $cpu ne 'host' && !$nokvm;
push @$cpuFlags, 'kvm=off' if $kvm_off;
$cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
push @$cmd, '-cpu', $cpu;
my $memory = $conf->{memory} || $defaults->{memory};
my $static_memory = 0;
my $dimm_memory = 0;
if ($hotplug_features->{memory}) {
die "Numa need to be enabled for memory hotplug\n" if !$conf->{numa};
die "Total memory is bigger than ${MAX_MEM}MB\n" if $memory > $MAX_MEM;
$static_memory = $STATICMEM;
die "minimum memory must be ${static_memory}MB\n" if($memory < $static_memory);
$dimm_memory = $memory - $static_memory;
push @$cmd, '-m', "size=${static_memory},slots=255,maxmem=${MAX_MEM}M";
} else {
$static_memory = $memory;
push @$cmd, '-m', $static_memory;
}
if ($conf->{numa}) {
my $numa_totalmemory = undef;
for (my $i = 0; $i < $MAX_NUMA; $i++) {
next if !$conf->{"numa$i"};
my $numa = parse_numa($conf->{"numa$i"});
next if !$numa;
# memory
die "missing numa node$i memory value\n" if !$numa->{memory};
my $numa_memory = $numa->{memory};
$numa_totalmemory += $numa_memory;
my $numa_object = "memory-backend-ram,id=ram-node$i,size=${numa_memory}M";
# cpus
my $cpulists = $numa->{cpus};
die "missing numa node$i cpus\n" if !defined($cpulists);
my $cpus = join(',', map {
my ($start, $end) = @$_;
defined($end) ? "$start-$end" : $start
} @$cpulists);
# hostnodes
my $hostnodelists = $numa->{hostnodes};
if (defined($hostnodelists)) {
my $hostnodes;
foreach my $hostnoderange (@$hostnodelists) {
my ($start, $end) = @$hostnoderange;
$hostnodes .= ',' if $hostnodes;
$hostnodes .= $start;
$hostnodes .= "-$end" if defined($end);
$end //= $start;
for (my $i = $start; $i <= $end; ++$i ) {
die "host numa node$i don't exist\n" if ! -d "/sys/devices/system/node/node$i/";
}
}
# policy
my $policy = $numa->{policy};
die "you need to define a policy for hostnode $hostnodes\n" if !$policy;
$numa_object .= ",host-nodes=$hostnodes,policy=$policy";
}
push @$cmd, '-object', $numa_object;
push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
}
die "total memory for NUMA nodes must be equal to vm static memory\n"
if $numa_totalmemory && $numa_totalmemory != $static_memory;
#if no custom tology, we split memory and cores across numa nodes
if(!$numa_totalmemory) {
my $numa_memory = ($static_memory / $sockets) . "M";
for (my $i = 0; $i < $sockets; $i++) {
my $cpustart = ($cores * $i);
my $cpuend = ($cpustart + $cores - 1) if $cores && $cores > 1;
my $cpus = $cpustart;
$cpus .= "-$cpuend" if $cpuend;
push @$cmd, '-object', "memory-backend-ram,size=$numa_memory,id=ram-node$i";
push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
}
}
}
if ($hotplug_features->{memory}) {
foreach_dimm($conf, $vmid, $memory, $sockets, sub {
my ($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
push @$cmd, "-object" , "memory-backend-ram,id=mem-$name,size=${dimm_size}M";
push @$cmd, "-device", "pc-dimm,id=$name,memdev=mem-$name,node=$numanode";
#if dimm_memory is not aligned to dimm map
if($current_size > $memory) {
$conf->{memory} = $current_size;
PVE::QemuConfig->write_config($vmid, $conf);
}
});
}
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;
if($conf->{agent}) {
my $qgasocket = qmp_socket($vmid, 1);
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';
}
my $spice_port;
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';
}
}
my $pciaddr = print_pci_addr("spice", $bridges);
my $nodename = PVE::INotify::nodename();
my $pfamily = PVE::Tools::get_host_address_family($nodename);
$spice_port = PVE::Tools::next_spice_port($pfamily);
push @$devices, '-spice', "tls-port=${spice_port},addr=localhost,tls-ciphers=DES-CBC3-SHA,seamless-migration=on";
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";
}
# 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";
}
if ($conf->{watchdog}) {
my $wdopts = parse_watchdog($conf->{watchdog});
$pciaddr = print_pci_addr("watchdog", $bridges);
my $watchdog = $wdopts->{model} || 'i6300esb';
push @$devices, '-device', "$watchdog$pciaddr";
push @$devices, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
}
my $vollist = [];
my $scsicontroller = {};
my $ahcicontroller = {};
my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
# Add iscsi initiator name if available
if (my $initiator = get_initiator_name()) {
push @$devices, '-iscsi', "initiator-name=$initiator";
}
foreach_drive($conf, sub {
my ($ds, $drive) = @_;
if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
push @$vollist, $drive->{file};
}
$use_virtio = 1 if $ds =~ m/^virtio/;
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;
}
}
if($drive->{interface} eq 'virtio'){
push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread};
}
if ($drive->{interface} eq 'scsi') {
my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
$pciaddr = print_pci_addr("$controller_prefix$controller", $bridges);
my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
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";
}
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};
$scsicontroller->{$controller}=1;
}
if ($drive->{interface} eq 'sata') {
my $controller = int($drive->{index} / $MAX_SATA_DISKS);
$pciaddr = print_pci_addr("ahci$controller", $bridges);
push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr" if !$ahcicontroller->{$controller};
$ahcicontroller->{$controller}=1;
}
my $drive_cmd = print_drive_full($storecfg, $vmid, $drive);
push @$devices, '-drive',$drive_cmd;
push @$devices, '-device', print_drivedevice_full($storecfg, $conf, $vmid, $drive, $bridges);
});
for (my $i = 0; $i < $MAX_NETS; $i++) {
next if !$conf->{"net$i"};
my $d = parse_net($conf->{"net$i"});
next if !$d;
$use_virtio = 1 if $d->{model} eq 'virtio';
if ($bootindex_hash->{n}) {
$d->{bootindex} = $bootindex_hash->{n};
$bootindex_hash->{n} += 1;
}
my $netdevfull = print_netdev_full($vmid,$conf,$d,"net$i");
push @$devices, '-netdev', $netdevfull;
my $netdevicefull = print_netdevice_full($vmid, $conf, $d, "net$i", $bridges, $use_old_bios_files);
push @$devices, '-device', $netdevicefull;
}
if (!$q35) {
# add pci bridges
if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
$bridges->{1} = 1;
$bridges->{