use strict;
use warnings;
-use POSIX;
-use IO::Handle;
-use IO::Select;
-use IO::File;
-use IO::Dir;
-use IO::Socket::UNIX;
+use Cwd 'abs_path';
+use Digest::SHA;
+use Fcntl ':flock';
+use Fcntl;
use File::Basename;
+use File::Copy qw(copy);
use File::Path;
use File::stat;
use Getopt::Long;
-use Digest::SHA;
-use Fcntl ':flock';
-use Cwd 'abs_path';
+use IO::Dir;
+use IO::File;
+use IO::Handle;
+use IO::Select;
+use IO::Socket::UNIX;
use IPC::Open3;
use JSON;
-use Fcntl;
-use PVE::SafeSyslog;
-use Storable qw(dclone);
use MIME::Base64;
-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 $IPV6RE);
-use PVE::JSONSchema qw(get_standard_option);
+use POSIX;
+use Storable qw(dclone);
+use Time::HiRes qw(gettimeofday);
+use URI::Escape;
+use UUID;
+
use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
+use PVE::Exception qw(raise raise_param_exc);
+use PVE::GuestHelpers;
use PVE::INotify;
+use PVE::JSONSchema qw(get_standard_option);
use PVE::ProcFSTools;
-use PVE::QemuConfig;
-use PVE::QMPClient;
use PVE::RPCEnvironment;
-use PVE::GuestHelpers;
-use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port);
-use PVE::QemuServer::Memory;
-use PVE::QemuServer::USB qw(parse_usb_device);
-use PVE::QemuServer::Cloudinit;
+use PVE::SafeSyslog;
+use PVE::Storage;
use PVE::SysFSTools;
use PVE::Systemd;
-use Time::HiRes qw(gettimeofday);
-use File::Copy qw(copy);
-use URI::Escape;
+use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach get_host_arch $IPV6RE);
+
+use PVE::QMPClient;
+use PVE::QemuConfig;
+use PVE::QemuServer::Cloudinit;
+use PVE::QemuServer::Memory;
+use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port);
+use PVE::QemuServer::USB qw(parse_usb_device);
my $EDK2_FW_BASE = '/usr/share/pve-edk2-firmware/';
my $OVMF = {
PVE::JSONSchema::register_standard_option('pve-qemu-machine', {
description => "Specifies the Qemu machine type.",
type => 'string',
- pattern => '(pc|pc(-i440fx)?-\d+\.\d+(\.pxe)?|q35|pc-q35-\d+\.\d+(\.pxe)?|virt(?:-\d+\.\d+)?)',
+ pattern => '(pc|pc(-i440fx)?-\d+(\.\d+)+(\.pxe)?|q35|pc-q35-\d+(\.\d+)+(\.pxe)?|virt(?:-\d+(\.\d+)+)?)',
maxLength => 40,
optional => 1,
});
'Skylake-Client-IBRS' => 'GenuineIntel',
'Skylake-Server' => 'GenuineIntel',
'Skylake-Server-IBRS' => 'GenuineIntel',
+ 'Cascadelake-Server' => 'GenuineIntel',
+ KnightsMill => 'GenuineIntel',
+
# AMD CPUs
athlon => 'AuthenticAMD',
usb3 => {
optional => 1,
type => 'boolean',
- 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).",
+ description => "Specifies whether if given host option is a USB3 device or port.",
default => 0,
},
};
$path = PVE::Storage::path($storecfg, $drive->{file});
}
- if($path =~ m/^iscsi\:\/\//){
+ # for compatibility only, we prefer scsi-hd (#2408, #2355, #2380)
+ if ($path =~ m/^iscsi\:\/\// &&
+ !qemu_machine_feature_enabled($machine_type, undef, 4, 1)) {
$devicetype = 'generic';
}
}
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) = @_;
}
}
-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][,base64=bool]
my $smbios1_fmt = {
uuid => {
}
}
-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;
# check if any base image is still used by a linked clone
foreach_drive($conf, sub {
my ($ds, $drive) = @_;
-
return if drive_is_cdrom($drive);
my $volid = $drive->{file};
-
return if !$volid || $volid =~ m|^/|;
die "base volume '$volid' is still in use by linked cloned\n"
# only remove disks owned by this VM
foreach_drive($conf, sub {
my ($ds, $drive) = @_;
-
return if drive_is_cdrom($drive, 1);
my $volid = $drive->{file};
-
return if !$volid || $volid =~ m|^/|;
my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
return if !$path || !$owner || ($owner != $vmid);
- eval {
- PVE::Storage::vdisk_free($storecfg, $volid);
- };
+ eval { PVE::Storage::vdisk_free($storecfg, $volid) };
warn "Could not remove disk '$volid', check manually: $@" if $@;
+ });
+ # also remove unused disk
+ my $vmdisks = PVE::Storage::vdisk_list($storecfg, undef, $vmid);
+ PVE::Storage::foreach_volid($vmdisks, sub {
+ my ($volid, $sid, $volname, $d) = @_;
+ eval { PVE::Storage::vdisk_free($storecfg, $volid) };
+ warn $@ if $@;
});
if ($keep_empty_config) {
- PVE::Tools::file_set_contents($conffile, "memory: 128\n");
+ PVE::QemuConfig->write_config($vmid, { memory => 128 });
} else {
- unlink $conffile;
+ PVE::QemuConfig->destroy_config($vmid);
}
-
- # 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 {
push @loc_res, "ivshmem" if $conf->{ivshmem};
foreach my $k (keys %$conf) {
- next if $k =~ m/^usb/ && ($conf->{$k} eq 'spice');
+ next if $k =~ m/^usb/ && ($conf->{$k} =~ m/^spice(?![^,])/);
# sockets are safe: they will recreated be on the target side post-migrate
next if $k =~ m/^serial/ && ($conf->{$k} eq 'socket');
push @loc_res, $k if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
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 $conf = PVE::QemuConfig->load_config($vmid);
my $d = { vmid => $vmid };
$d->{pid} = $list->{$vmid}->{pid};
return $1 || 1;
}
-my $host_arch; # FIXME: fix PVE::Tools::get_host_arch
-sub get_host_arch() {
- $host_arch = (POSIX::uname())[4] if !$host_arch;
- return $host_arch;
-}
-
sub is_native($) {
my ($arch) = @_;
return get_host_arch() eq $arch;
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 $use_old_bios_files = undef;
}
# usb devices
- my @usbdevices = PVE::QemuServer::USB::get_usb_devices($conf, $usbdesc->{format}, $MAX_USB_DEVICES);
+ my $usb_dev_features = {};
+ $usb_dev_features->{spice_usb3} = 1 if qemu_machine_feature_enabled($machine_type, $kvmver, 4, 0);
+
+ my @usbdevices = PVE::QemuServer::USB::get_usb_devices($conf, $usbdesc->{format}, $MAX_USB_DEVICES, $usb_dev_features);
push @$devices, @usbdevices if @usbdevices;
# serial devices
for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
my $pfamily = PVE::Tools::get_host_address_family($nodename);
my @nodeaddrs = PVE::Tools::getaddrinfo_all('localhost', family => $pfamily);
die "failed to get an ip address of type $pfamily for 'localhost'\n" if !@nodeaddrs;
+
+ 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";
+
my $localhost = PVE::Network::addr_to_ip($nodeaddrs[0]->{addr});
$spice_port = PVE::Tools::next_spice_port($pfamily, $localhost);
my $spice_opts = "tls-port=${spice_port},addr=$localhost,tls-ciphers=HIGH,seamless-migration=on";
$spice_opts .= ",streaming-video=$spice_enhancement->{videostreaming}" if $spice_enhancement->{videostreaming};
push @$devices, '-spice', "$spice_opts";
-
- 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
$bridges->{3} = 1 if $scsihw =~ m/^virtio-scsi-single/;
- while (my ($k, $v) = each %$bridges) {
+ for my $k (sort {$b cmp $a} keys %$bridges) {
$pciaddr = print_pci_addr("pci.$k", undef, $arch, $machine_type);
unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
}
if (my $vmstate = $conf->{vmstate}) {
my $statepath = PVE::Storage::path($storecfg, $vmstate);
- PVE::Storage::activate_volumes($storecfg, [$vmstate]);
+ push @$vollist, $vmstate;
push @$cmd, '-loadstate', $statepath;
}
}
sub qmp_socket {
- my ($vmid, $qga, $name) = @_;
+ my ($vmid, $qga) = @_;
my $sockettype = $qga ? 'qga' : 'qmp';
- my $ext = $name ? '-'.$name : '';
- return "${var_run_tmpdir}/$vmid$ext.$sockettype";
+ return "${var_run_tmpdir}/$vmid.$sockettype";
}
sub pidfile_name {
my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
- my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
- while (my ($opt, $force) = each %$pending_delete_hash) {
+ my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
+ foreach my $opt (sort keys %$pending_delete_hash) {
next if $selection && !$selection->{$opt};
+ my $force = $pending_delete_hash->{$opt}->{force};
eval {
if ($opt eq 'hotplug') {
die "skip\n" if ($conf->{hotplug} =~ /memory/);
} else {
# save new config if hotplug was successful
delete $conf->{$opt};
- vmconfig_undelete_pending_option($conf, $opt);
+ PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
PVE::QemuConfig->write_config($vmid, $conf);
$conf = PVE::QemuConfig->load_config($vmid); # update/reload
}
}
}
+
+
sub vmconfig_apply_pending {
my ($vmid, $conf, $storecfg) = @_;
# cold plug
- my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
- while (my ($opt, $force) = each %$pending_delete_hash) {
+ my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
+ foreach my $opt (sort keys %$pending_delete_hash) {
die "internal error" if $opt =~ m/^unused/;
+ my $force = $pending_delete_hash->{$opt}->{force};
$conf = PVE::QemuConfig->load_config($vmid); # update/reload
if (!defined($conf->{$opt})) {
- vmconfig_undelete_pending_option($conf, $opt);
+ PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
PVE::QemuConfig->write_config($vmid, $conf);
} elsif (is_valid_drivename($opt)) {
vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
- vmconfig_undelete_pending_option($conf, $opt);
+ PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
delete $conf->{$opt};
PVE::QemuConfig->write_config($vmid, $conf);
} else {
- vmconfig_undelete_pending_option($conf, $opt);
+ PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
delete $conf->{$opt};
PVE::QemuConfig->write_config($vmid, $conf);
}
my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
- my $migrate_port = 0;
my $migrate_uri;
if ($statefile) {
if ($statefile eq 'tcp') {
}
my $pfamily = PVE::Tools::get_host_address_family($nodename);
- $migrate_port = PVE::Tools::next_migrate_port($pfamily);
+ my $migrate_port = PVE::Tools::next_migrate_port($pfamily);
$migrate_uri = "tcp:${localip}:${migrate_port}";
push @$cmd, '-incoming', $migrate_uri;
push @$cmd, '-S';
push @$cmd, '-incoming', $migrate_uri;
push @$cmd, '-S';
- } else {
+ } elsif (-e $statefile) {
push @$cmd, '-loadstate', $statefile;
+ } else {
+ my $statepath = PVE::Storage::path($storecfg, $statefile);
+ push @$vollist, $statefile;
+ push @$cmd, '-loadstate', $statepath;
}
} elsif ($paused) {
push @$cmd, '-S';
my $migrate_network_addr = PVE::Cluster::get_local_migration_ip($migration_network);
my $localip = $migrate_network_addr ? $migrate_network_addr : PVE::Cluster::remote_node_ip($nodename, 1);
my $pfamily = PVE::Tools::get_host_address_family($nodename);
- $migrate_port = PVE::Tools::next_migrate_port($pfamily);
+ my $storage_migrate_port = PVE::Tools::next_migrate_port($pfamily);
- vm_mon_cmd_nocheck($vmid, "nbd-server-start", addr => { type => 'inet', data => { host => "${localip}", port => "${migrate_port}" } } );
+ vm_mon_cmd_nocheck($vmid, "nbd-server-start", addr => { type => 'inet', data => { host => "${localip}", port => "${storage_migrate_port}" } } );
$localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
foreach my $opt (sort keys %$local_volumes) {
my $volid = $local_volumes->{$opt};
vm_mon_cmd_nocheck($vmid, "nbd-server-add", device => "drive-$opt", writable => JSON::true );
- my $migrate_storage_uri = "nbd:${localip}:${migrate_port}:exportname=drive-$opt";
+ my $migrate_storage_uri = "nbd:${localip}:${storage_migrate_port}:exportname=drive-$opt";
print "storage migration listens on $migrate_storage_uri volume:$volid\n";
}
}
});
}
-sub vm_destroy {
- my ($storecfg, $vmid, $skiplock) = @_;
-
- PVE::QemuConfig->lock_config($vmid, sub {
-
- my $conf = PVE::QemuConfig->load_config($vmid);
-
- if (!check_running($vmid)) {
- destroy_vm($storecfg, $vmid, undef, $skiplock);
- } else {
- die "VM $vmid is running - destroy failed\n";
- }
- });
-}
-
# vzdump restore implementaion
sub tar_archive_read_firstfile {
foreach_drive($oldconf, sub {
my ($ds, $drive) = @_;
- return if !$drive->{is_cloudinit} && drive_is_cdrom($drive);
+ return if drive_is_cdrom($drive, 1);
my $volid = $drive->{file};
return if !$volid || $volid =~ m|^/|;
my $storecfg = PVE::Storage::config();
- # destroy existing data - keep empty config
+ # avoid zombie disks when restoring over an existing VM -> cleanup first
+ # pass keep_empty_config=1 to keep the config (thus VMID) reserved for us
+ # skiplock=1 because qmrestore has set the 'create' lock itself already
my $vmcfgfn = PVE::QemuConfig->config_file($vmid);
- destroy_vm($storecfg, $vmid, 1) if -f $vmcfgfn;
+ destroy_vm($storecfg, $vmid, 1, 1) if -f $vmcfgfn;
my $tocmd = "/usr/lib/qemu-server/qmextract";
$srcfd->close();
$outfd->close();
};
- my $err = $@;
-
- if ($err) {
-
+ if (my $err = $@) {
unlink $tmpfn;
-
tar_restore_cleanup($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info};
-
die $err;
}
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) {
+ die "destination '$dst_volid' is not a valid volid form qemu-img convert\n" if !$dst_storeid;
- PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
+ my $cachemode;
+ my $src_path;
+ my $src_is_iscsi = 0;
+ my $src_format = 'raw';
+ if ($src_storeid) {
+ PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
- my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
+ $src_format = qemu_img_format($src_scfg, $src_volname);
+ $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
+ $src_is_iscsi = ($src_path =~ m|^iscsi://|);
+ $cachemode = 'none' if $src_scfg->{type} eq 'zfspool';
+ } elsif (-f $src_volid) {
+ $src_path = $src_volid;
+ if ($src_path =~ m/\.($QEMU_FORMAT_RE)$/) {
+ $src_format = $1;
+ }
+ }
- my $src_format = qemu_img_format($src_scfg, $src_volname);
- my $dst_format = qemu_img_format($dst_scfg, $dst_volname);
+ die "source '$src_volid' is not a valid volid nor path for qemu-img convert\n" if !$src_path;
- my $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
- my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
+ my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
+ my $dst_format = qemu_img_format($dst_scfg, $dst_volname);
+ my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
+ my $dst_is_iscsi = ($dst_path =~ m|^iscsi://|);
- my $src_is_iscsi = ($src_path =~ m|^iscsi://|);
- my $dst_is_iscsi = ($dst_path =~ m|^iscsi://|);
+ my $cmd = [];
+ push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-n';
+ push @$cmd, '-l', "snapshot.name=$snapname" if($snapname && $src_format eq "qcow2");
+ push @$cmd, '-t', 'none' if $dst_scfg->{type} eq 'zfspool';
+ push @$cmd, '-T', $cachemode if defined($cachemode);
+
+ if ($src_is_iscsi) {
+ push @$cmd, '--image-opts';
+ $src_path = convert_iscsi_path($src_path);
+ } else {
+ push @$cmd, '-f', $src_format;
+ }
- my $cmd = [];
- push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-n';
- push @$cmd, '-l', "snapshot.name=$snapname" if($snapname && $src_format eq "qcow2");
- push @$cmd, '-t', 'none' if $dst_scfg->{type} eq 'zfspool';
- push @$cmd, '-T', 'none' if $src_scfg->{type} eq 'zfspool';
+ if ($dst_is_iscsi) {
+ push @$cmd, '--target-image-opts';
+ $dst_path = convert_iscsi_path($dst_path);
+ } else {
+ push @$cmd, '-O', $dst_format;
+ }
- if ($src_is_iscsi) {
- push @$cmd, '--image-opts';
- $src_path = convert_iscsi_path($src_path);
- } else {
- push @$cmd, '-f', $src_format;
- }
+ push @$cmd, $src_path;
- if ($dst_is_iscsi) {
- push @$cmd, '--target-image-opts';
- $dst_path = convert_iscsi_path($dst_path);
- } else {
- push @$cmd, '-O', $dst_format;
- }
+ if (!$dst_is_iscsi && $is_zero_initialized) {
+ push @$cmd, "zeroinit:$dst_path";
+ } else {
+ push @$cmd, $dst_path;
+ }
- push @$cmd, $src_path;
+ my $parser = sub {
+ my $line = shift;
+ if($line =~ m/\((\S+)\/100\%\)/){
+ my $percent = $1;
+ my $transferred = int($size * $percent / 100);
+ my $remaining = $size - $transferred;
- if (!$dst_is_iscsi && $is_zero_initialized) {
- push @$cmd, "zeroinit:$dst_path";
- } else {
- push @$cmd, $dst_path;
+ print "transferred: $transferred bytes remaining: $remaining bytes total: $size bytes progression: $percent %\n";
}
- 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;
- }
+ eval { run_command($cmd, timeout => undef, outfunc => $parser); };
+ my $err = $@;
+ die "copy failed: $err" if $err;
}
sub qemu_img_format {
print "create full clone of drive $drivename ($drive->{file})\n";
my $name = undef;
- if (drive_is_cloudinit($drive)) {
- $name = "vm-$newvmid-cloudinit";
- $snapname = undef;
- # we only get here if it's supported by QEMU_FORMAT_RE, so just accept
- if ($dst_format ne 'raw') {
- $name .= ".$dst_format";
- }
- }
$newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newvmid, $dst_format, $name, ($size/1024));
push @$newvollist, $newvolid;
$current_minor = $2;
}
- return 1 if $current_major > $version_major ||
- ($current_major == $version_major &&
- $current_minor >= $version_minor);
+ return 1 if version_cmp($current_major, $version_major, $current_minor, $version_minor) >= 0;
+}
+
+# gets in pairs the versions you want to compares, i.e.:
+# ($a-major, $b-major, $a-minor, $b-minor, $a-extra, $b-extra, ...)
+# returns 0 if same, -1 if $a is older than $b, +1 if $a is newer than $b
+sub version_cmp {
+ my @versions = @_;
+
+ my $size = scalar(@versions);
+
+ return 0 if $size == 0;
+ die "cannot compare odd count of versions" if $size & 1;
+
+ for (my $i = 0; $i < $size; $i += 2) {
+ my ($a, $b) = splice(@versions, 0, 2);
+ $a //= 0;
+ $b //= 0;
+
+ return 1 if $a > $b;
+ return -1 if $a < $b;
+ }
+ return 0;
+}
+
+# dies if a) VM not running or not exisiting b) Version query failed
+# So, any defined return value is valid, any invalid state can be caught by eval
+sub runs_at_least_qemu_version {
+ my ($vmid, $major, $minor, $extra) = @_;
+
+ my $v = vm_qmp_command($vmid, { execute => 'query-version' });
+ die "could not query currently running version for VM $vmid\n" if !defined($v);
+ $v = $v->{qemu};
+
+ return version_cmp($v->{major}, $major, $v->{minor}, $minor, $v->{micro}, $extra) >= 0;
}
sub qemu_machine_pxe {
my (undef, $ovmf_vars) = get_ovmf_files($arch);
die "EFI vars default image not found\n" if ! -f $ovmf_vars;
- my $vars_size = PVE::Tools::convert_size(-s $ovmf_vars, 'b' => 'kb');
+ my $vars_size_b = -s $ovmf_vars;
+ my $vars_size = PVE::Tools::convert_size($vars_size_b, 'b' => 'kb');
my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
PVE::Storage::activate_volumes($storecfg, [$volid]);
- my $path = PVE::Storage::path($storecfg, $volid);
- eval {
- run_command(['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $fmt, $ovmf_vars, $path]);
- };
- die "Copying EFI vars image failed: $@" if $@;
+ qemu_img_convert($ovmf_vars, $volid, $vars_size_b, undef, 0);
return ($volid, $vars_size);
}