vm_start: split out lock + initial checks

to start breaking up vm_start before extending parts for new migration
features like storage and network mapping.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2020-03-30 13:41:31 +02:00 committed by Thomas Lamprecht
parent 0c498cca36
commit 3898a56343

View File

@ -4709,12 +4709,34 @@ sub vmconfig_update_disk {
vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive, $arch, $machine_type);
}
# see vm_start_nolock for parameters
sub vm_start {
my ($storecfg, $vmid, $params, $migrate_opts) = @_;
PVE::QemuConfig->lock_config($vmid, sub {
my $conf = PVE::QemuConfig->load_config($vmid, $migrate_opts->{migratedfrom});
die "you can't start a vm if it's a template\n" if PVE::QemuConfig->is_template($conf);
$params->{resume} = PVE::QemuConfig->has_lock($conf, 'suspended');
PVE::QemuConfig->check_lock($conf)
if !($params->{skiplock} || $params->{resume});
die "VM $vmid already running\n" if check_running($vmid, undef, $migrate_opts->{migratedfrom});
vm_start_nolock($storecfg, $vmid, $conf, $params, $migrate_opts);
});
}
# params:
# statefile => 'tcp', 'unix' for migration or path/volid for RAM state
# skiplock => 0/1, skip checking for config lock
# forcemachine => to force Qemu machine (rollback/migration)
# timeout => in seconds
# paused => start VM in paused state (backup)
# resume => resume from hibernation
# migrate_opts:
# migratedfrom => source node
# spice_ticket => used for spice migration, passed via tunnel/stdin
@ -4723,27 +4745,16 @@ sub vmconfig_update_disk {
# targetstorage = storageid/'1' - target storage for disks migrated over NBD
# nbd_proto_version => int, 0 for TCP, 1 for UNIX
# replicated_volumes = which volids should be re-used with bitmaps for nbd migration
sub vm_start {
my ($storecfg, $vmid, $params, $migrate_opts) = @_;
sub vm_start_nolock {
my ($storecfg, $vmid, $conf, $params, $migrate_opts) = @_;
PVE::QemuConfig->lock_config($vmid, sub {
my $statefile = $params->{statefile};
my $resume = $params->{resume};
my $migratedfrom = $migrate_opts->{migratedfrom};
my $migration_type = $migrate_opts->{type};
my $targetstorage = $migrate_opts->{targetstorage};
my $conf = PVE::QemuConfig->load_config($vmid, $migratedfrom);
die "you can't start a vm if it's a template\n" if PVE::QemuConfig->is_template($conf);
my $is_suspended = PVE::QemuConfig->has_lock($conf, 'suspended');
PVE::QemuConfig->check_lock($conf)
if !($params->{skiplock} || $is_suspended);
die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
# clean up leftover reboot request files
eval { clear_reboot_request($vmid); };
warn $@ if $@;
@ -4820,7 +4831,7 @@ sub vm_start {
PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
my $forcemachine = $params->{forcemachine};
if ($is_suspended) {
if ($resume) {
# enforce machine type on suspended vm to ensure HW compatibility
$forcemachine = $conf->{runningmachine};
print "Resuming suspended VM\n";
@ -4945,7 +4956,7 @@ sub vm_start {
my $cpuunits = defined($conf->{cpuunits}) ? $conf->{cpuunits}
: $defaults->{cpuunits};
my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $is_suspended);
my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $resume);
my %run_params = (
timeout => $statefile ? undef : $start_timeout,
umask => 0077,
@ -5073,7 +5084,7 @@ sub vm_start {
property => "guest-stats-polling-interval",
value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
if ($is_suspended) {
if ($resume) {
print "Resumed VM, removing state\n";
if (my $vmstate = $conf->{vmstate}) {
PVE::Storage::deactivate_volumes($storecfg, [$vmstate]);
@ -5084,7 +5095,6 @@ sub vm_start {
}
PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
});
}
sub vm_commandline {