use PVE::SSHInfo;
use PVE::Replication;
use PVE::StorageTunnel;
+use PVE::RESTEnvironment qw(log_warn);
BEGIN {
if (!$ENV{PVE_GENERATING_DOCS}) {
}
};
-my $NEW_DISK_RE = qr!^(([^/:\s]+):)?(\d+(\.\d+)?)$!;
-
my $check_drive_param = sub {
my ($param, $storecfg, $extra_checks) = @_;
raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
if ($drive->{'import-from'}) {
- if ($drive->{file} !~ $NEW_DISK_RE || $3 != 0) {
+ if ($drive->{file} !~ $PVE::QemuServer::Drive::NEW_DISK_RE || $3 != 0) {
raise_param_exc({
$opt => "'import-from' requires special syntax - ".
"use <storage ID>:0,import-from=<source>",
# nothing to check
} elsif ($isCDROM && ($volid eq 'cdrom')) {
$rpcenv->check($authuser, "/", ['Sys.Console']);
- } elsif (!$isCDROM && ($volid =~ $NEW_DISK_RE)) {
+ } elsif (!$isCDROM && ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE)) {
my ($storeid, $size) = ($2 || $default_storage, $3);
die "no storage ID specified (and no default storage)\n" if !$storeid;
$rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
# Note: $pool is only needed when creating a VM, because pool permissions
# are automatically inherited if VM already exists inside a pool.
-my $create_disks = sub {
- my ($rpcenv, $authuser, $conf, $arch, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
+my sub create_disks : prototype($$$$$$$$$$) {
+ my (
+ $rpcenv,
+ $authuser,
+ $conf,
+ $arch,
+ $storecfg,
+ $vmid,
+ $pool,
+ $settings,
+ $default_storage,
+ $is_live_import,
+ ) = @_;
my $vollist = [];
my $res = {};
+ my $live_import_mapping = {};
+
my $code = sub {
my ($ds, $disk) = @_;
delete $disk->{format}; # no longer needed
$res->{$ds} = PVE::QemuServer::print_drive($disk);
print "$ds: successfully created disk '$res->{$ds}'\n";
- } elsif ($volid =~ $NEW_DISK_RE) {
+ } elsif ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
my ($storeid, $size) = ($2 || $default_storage, $3);
die "no storage ID specified (and no default storage)\n" if !$storeid;
+ $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
+
+ my $live_import = $is_live_import && $ds ne 'efidisk0';
+ my $needs_creation = 1;
+
if (my $source = delete $disk->{'import-from'}) {
my $dst_volid;
+ $needs_creation = $live_import;
+
if (PVE::Storage::parse_volume_id($source, 1)) { # PVE-managed volume
- my $dest_info = {
- vmid => $vmid,
- drivename => $ds,
- storage => $storeid,
- format => $disk->{format},
- };
+ if ($live_import && $ds ne 'efidisk0') {
+ my $path = PVE::Storage::path($storecfg, $source)
+ or die "failed to get a path for '$source'\n";
+ $source = $path;
+ ($size, my $source_format) = PVE::Storage::file_size_info($source);
+ die "could not get file size of $source\n" if !$size;
+ $live_import_mapping->{$ds} = {
+ path => $source,
+ format => $source_format,
+ };
+ } else {
+ my $dest_info = {
+ vmid => $vmid,
+ drivename => $ds,
+ storage => $storeid,
+ format => $disk->{format},
+ };
- $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk)
- if $ds eq 'efidisk0';
+ $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk)
+ if $ds eq 'efidisk0';
- ($dst_volid, $size) = eval {
- $import_from_volid->($storecfg, $source, $dest_info, $vollist);
- };
- die "cannot import from '$source' - $@" if $@;
+ ($dst_volid, $size) = eval {
+ $import_from_volid->($storecfg, $source, $dest_info, $vollist);
+ };
+ die "cannot import from '$source' - $@" if $@;
+ }
} else {
$source = PVE::Storage::abs_filesystem_path($storecfg, $source, 1);
- $size = PVE::Storage::file_size_info($source);
+ ($size, my $source_format) = PVE::Storage::file_size_info($source);
die "could not get file size of $source\n" if !$size;
- (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
- $source,
- $vmid,
- $storeid,
- {
- drive_name => $ds,
- format => $disk->{format},
- 'skip-config-update' => 1,
- },
- );
- push @$vollist, $dst_volid;
+ if ($live_import && $ds ne 'efidisk0') {
+ $live_import_mapping->{$ds} = {
+ path => $source,
+ format => $source_format,
+ };
+ } else {
+ (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
+ $source,
+ $vmid,
+ $storeid,
+ {
+ drive_name => $ds,
+ format => $disk->{format},
+ 'skip-config-update' => 1,
+ },
+ );
+ push @$vollist, $dst_volid;
+ }
}
- $disk->{file} = $dst_volid;
- $disk->{size} = $size;
- delete $disk->{format}; # no longer needed
- $res->{$ds} = PVE::QemuServer::print_drive($disk);
- } else {
+ if ($needs_creation) {
+ $size = PVE::Tools::convert_size($size, 'b' => 'kb'); # vdisk_alloc uses kb
+ } else {
+ $disk->{file} = $dst_volid;
+ $disk->{size} = $size;
+ delete $disk->{format}; # no longer needed
+ $res->{$ds} = PVE::QemuServer::print_drive($disk);
+ }
+ }
+
+ if ($needs_creation) {
my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
my $fmt = $disk->{format} || $defformat;
- $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
-
my $volid;
if ($ds eq 'efidisk0') {
my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf);
die $err;
}
- return ($vollist, $res);
+ # don't return empty import mappings
+ $live_import_mapping = undef if !%$live_import_mapping;
+
+ return ($vollist, $res, $live_import_mapping);
};
my $check_cpu_model_access = sub {
return 1;
};
+sub assert_scsi_feature_compatibility {
+ my ($opt, $conf, $storecfg, $drive_attributes) = @_;
+
+ my $drive = PVE::QemuServer::Drive::parse_drive($opt, $drive_attributes, 1);
+
+ my $machine_type = PVE::QemuServer::get_vm_machine($conf, undef, $conf->{arch});
+ my $machine_version = PVE::QemuServer::Machine::extract_version(
+ $machine_type, PVE::QemuServer::kvm_user_version());
+ my $drivetype = PVE::QemuServer::Drive::get_scsi_device_type(
+ $drive, $storecfg, $machine_version);
+
+ if ($drivetype ne 'hd' && $drivetype ne 'cd') {
+ if ($drive->{product}) {
+ raise_param_exc({
+ $opt => "Passing of product information is only supported for 'scsi-hd' and "
+ ."'scsi-cd' devices (e.g. not pass-through).",
+ });
+ }
+ if ($drive->{vendor}) {
+ raise_param_exc({
+ $opt => "Passing of vendor information is only supported for 'scsi-hd' and "
+ ."'scsi-cd' devices (e.g. not pass-through).",
+ });
+ }
+ }
+}
+
__PACKAGE__->register_method({
name => 'vmlist',
path => '',
return $res;
};
-
__PACKAGE__->register_method({
name => 'create_vm',
path => '',
'live-restore' => {
optional => 1,
type => 'boolean',
- description => "Start the VM immediately from the backup and restore in background. PBS only.",
- requires => 'archive',
+ description => "Start the VM immediately while importing or restoring in the background.",
},
pool => {
optional => 1,
eval { PVE::QemuServer::template_create($vmid, $restored_conf) };
warn $@ if $@;
}
+
+ PVE::QemuServer::create_ifaces_ipams_ips($restored_conf, $vmid) if $unique;
};
# ensure no old replication state are exists
};
my $createfn = sub {
+ my $live_import_mapping = {};
+
# ensure no old replication state are exists
PVE::ReplicationState::delete_guest_states($vmid);
my $conf = $param;
my $arch = PVE::QemuServer::get_vm_arch($conf);
+ for my $opt (sort keys $param->%*) {
+ next if $opt !~ m/^scsi\d+$/;
+ assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt});
+ }
+
$conf->{meta} = PVE::QemuServer::new_meta_info_string();
my $vollist = [];
eval {
- ($vollist, my $created_opts) = $create_disks->(
+ ($vollist, my $created_opts, $live_import_mapping) = create_disks(
$rpcenv,
$authuser,
$conf,
$pool,
$param,
$storage,
+ $live_restore,
);
$conf->{$_} = $created_opts->{$_} for keys $created_opts->%*;
$conf->{boot} = PVE::QemuServer::print_bootorder($devs);
}
+ my $vga = PVE::QemuServer::parse_vga($conf->{vga});
+ PVE::QemuServer::assert_clipboard_config($vga);
+
# auto generate uuid if user did not specify smbios1 option
if (!$conf->{smbios1}) {
$conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
$conf->{vmgenid} = PVE::QemuServer::generate_uuid();
}
- my $machine = $conf->{machine};
+ my $machine_conf = PVE::QemuServer::Machine::parse_machine($conf->{machine});
+ my $machine = $machine_conf->{type};
if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
# always pin Windows' machine version on create, they get to easily confused
if (PVE::QemuServer::Helpers::windows_version($conf->{ostype})) {
- $conf->{machine} = PVE::QemuServer::windows_get_pinned_machine_version($machine);
+ $machine_conf->{type} = PVE::QemuServer::windows_get_pinned_machine_version($machine);
+ $conf->{machine} = PVE::QemuServer::Machine::print_machine($machine_conf);
}
}
+ PVE::QemuServer::Machine::assert_valid_machine_property($conf, $machine_conf);
- PVE::QemuConfig->write_config($vmid, $conf);
+ $conf->{lock} = 'import' if $live_import_mapping;
+ PVE::QemuConfig->write_config($vmid, $conf);
};
my $err = $@;
}
PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
+
+ PVE::QemuServer::create_ifaces_ipams_ips($conf, $vmid);
};
PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
- if ($start_after_create) {
+ if ($start_after_create && !$live_restore) {
print "Execute autostart\n";
eval { PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node}) };
warn $@ if $@;
+ return;
+ } else {
+ return $live_import_mapping;
}
};
} else {
$worker_name = 'qmcreate';
$code = sub {
- eval { $createfn->() };
+ # If a live import was requested the create function returns
+ # the mapping for the startup.
+ my $live_import_mapping = eval { $createfn->() };
if (my $err = $@) {
eval {
my $conffile = PVE::QemuConfig->config_file($vmid);
warn $@ if $@;
die $err;
}
+
+ if ($live_import_mapping) {
+ my $import_options = {
+ bwlimit => $bwlimit,
+ live => 1,
+ };
+
+ my $conf = PVE::QemuConfig->load_config($vmid);
+ PVE::QemuServer::live_import_from_files(
+ $live_import_mapping,
+ $vmid,
+ $conf,
+ $import_options,
+ );
+ }
};
}
return if defined($volname) && $volname eq 'cloudinit';
my $format;
- if ($volid =~ $NEW_DISK_RE) {
+ if ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
$storeid = $2;
$format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid);
} else {
PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
if defined($conf->{pending}->{$opt});
- my (undef, $created_opts) = $create_disks->(
+ assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt})
+ if $opt =~ m/^scsi\d+$/;
+
+ my (undef, $created_opts) = create_disks(
$rpcenv,
$authuser,
$conf,
$vmid,
undef,
{$opt => $param->{$opt}},
+ undef,
+ undef,
);
$conf->{pending}->{$_} = $created_opts->{$_} for keys $created_opts->%*;
die "only root can modify '$opt' config for real devices\n";
}
$conf->{pending}->{$opt} = $param->{$opt};
+ } elsif ($opt eq 'vga') {
+ my $vga = PVE::QemuServer::parse_vga($param->{$opt});
+ PVE::QemuServer::assert_clipboard_config($vga);
+ $conf->{pending}->{$opt} = $param->{$opt};
} elsif ($opt =~ m/^usb\d+/) {
if (my $olddevice = $conf->{$opt}) {
check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $conf->{$opt});
);
}
$conf->{pending}->{$opt} = $param->{$opt};
+ } elsif ($opt eq 'machine') {
+ my $machine_conf = PVE::QemuServer::Machine::parse_machine($param->{$opt});
+ PVE::QemuServer::Machine::assert_valid_machine_property($conf, $machine_conf);
+ $conf->{pending}->{$opt} = $param->{$opt};
} else {
$conf->{pending}->{$opt} = $param->{$opt};
type => 'boolean',
optional => 1,
},
+ clipboard => {
+ description => 'Enable a specific clipboard. If not set, depending on'
+ .' the display type the SPICE one will be added.',
+ type => 'string',
+ enum => ['vnc'],
+ optional => 1,
+ },
},
},
code => sub {
my $spice = defined($vga->{type}) && $vga->{type} =~ /^virtio/;
$spice ||= PVE::QemuServer::vga_conf_has_spice($conf->{vga});
$status->{spice} = 1 if $spice;
+ $status->{clipboard} = $vga->{clipboard};
}
$status->{agent} = 1 if PVE::QemuServer::get_qga_key($conf, 'enabled');
# sending a graceful shutdown command to paused VMs runs into timeouts, and even worse, when
# the VM gets resumed later, it still gets the request delivered and powers off
- if (PVE::QemuServer::vm_is_paused($vmid)) {
+ if (PVE::QemuServer::vm_is_paused($vmid, 1)) {
if ($param->{forceStop}) {
warn "VM is paused - stop instead of shutdown\n";
$shutdown = 0;
my $node = extract_param($param, 'node');
my $vmid = extract_param($param, 'vmid');
- die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid);
+ die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid, 1);
die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
PVE::QemuConfig->write_config($newid, $newconf);
+ PVE::QemuServer::create_ifaces_ipams_ips($newconf, $newid);
+
if ($target) {
- # always deactivate volumes - avoid lvm LVs to be active on several nodes
- PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
+ if (!$running) {
+ # always deactivate volumes – avoids that LVM LVs are active on several nodes
+ eval { PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) };
+ # but only warn when that fails (e.g., parallel clones keeping them active)
+ log_warn($@) if $@;
+ }
+
PVE::Storage::deactivate_volumes($storecfg, $newvollist);
my $newconffile = PVE::QemuConfig->config_file($newid, $target);
PVE::QemuServer::check_local_resources($vmconf, 1);
delete $missing_mappings_by_node->{$localnode};
+ my $vga = PVE::QemuServer::parse_vga($vmconf->{vga});
+ if ($res->{running} && $vga->{'clipboard'} && $vga->{'clipboard'} eq 'vnc') {
+ push $local_resources->@*, "clipboard=vnc";
+ }
+
# if vm is not running, return target nodes where local storage/mapped devices are available
# for offline migration
if (!$res->{running}) {