X-Git-Url: https://git.proxmox.com/?p=qemu-server.git;a=blobdiff_plain;f=PVE%2FCLI%2Fqm.pm;h=f4bacd696121d94e973130ef67f3b2911906575c;hp=76fda4dc5aae83a412a233160a823f3705b50369;hb=b14477e718969468a9a5ef639533b9ff620e84ec;hpb=34e4c0aa6c1394faa8fde44929ef9b5c7b64d38d diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm index 76fda4d..f4bacd6 100755 --- a/PVE/CLI/qm.pm +++ b/PVE/CLI/qm.pm @@ -18,6 +18,7 @@ use PVE::SafeSyslog; use PVE::INotify; use PVE::RPCEnvironment; use PVE::Exception qw(raise_param_exc); +use PVE::Network; use PVE::QemuServer; use PVE::QemuServer::ImportDisk; use PVE::QemuServer::OVF; @@ -126,7 +127,15 @@ __PACKAGE__->register_method ({ type => 'boolean', optional => 1, default => 0, - } + }, + snapshot => get_standard_option('pve-snapshot-name', { + description => "Fetch config values from given snapshot.", + optional => 1, + completion => sub { + my ($cmd, $pname, $cur, $args) = @_; + PVE::QemuConfig->snapshot_list($args->[0]); + } + }), }, }, returns => { type => 'null'}, @@ -134,7 +143,7 @@ __PACKAGE__->register_method ({ my ($param) = @_; my $storecfg = PVE::Storage::config(); - my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}); + my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}, $param->{snapshot}); $cmdline =~ s/ -/ \\\n -/g if $param->{pretty}; @@ -410,7 +419,7 @@ __PACKAGE__->register_method ({ type => 'boolean', optional => 1, default => 0, - description => 'Do not actually write changes out to conifg.', + description => 'Do not actually write changes out to VM config(s).', }, }, }, @@ -621,6 +630,7 @@ __PACKAGE__->register_method ({ eval { # order matters, as do_import() will load_config() internally + $conf->{vmgenid} = PVE::QemuServer::generate_uuid(); $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); PVE::QemuConfig->write_config($vmid, $conf); @@ -721,6 +731,68 @@ __PACKAGE__->register_method({ return { result => $res }; }}); +__PACKAGE__->register_method({ + name => 'cleanup', + path => 'cleanup', + method => 'POST', + protected => 1, + description => "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.", + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + vmid => get_standard_option('pve-vmid', { + completion => \&PVE::QemuServer::complete_vmid_running }), + 'clean-shutdown' => { + type => 'boolean', + description => "Indicates if qemu shutdown cleanly.", + }, + 'guest-requested' => { + type => 'boolean', + description => "Indicates if the shutdown was requested by the guest or via qmp.", + }, + }, + }, + returns => { type => 'null', }, + code => sub { + my ($param) = @_; + + my $vmid = $param->{vmid}; + my $clean = $param->{'clean-shutdown'}; + my $guest = $param->{'guest-requested'}; + + # return if we do not have the config anymore + return if !-f PVE::QemuConfig->config_file($vmid); + + my $storecfg = PVE::Storage::config(); + warn "Starting cleanup for $vmid\n"; + + PVE::QemuConfig->lock_config($vmid, sub { + my $conf = PVE::QemuConfig->load_config ($vmid); + my $pid = PVE::QemuServer::check_running ($vmid); + die "vm still running\n" if $pid; + + if (!$clean) { + # we have to cleanup the tap devices after a crash + + foreach my $opt (keys %$conf) { + next if $opt !~ m/^net(\d)+$/; + my $interface = $1; + PVE::Network::tap_unplug("tap${vmid}i${interface}"); + } + } + + if (!$clean || $guest) { + # vm was shutdown from inside the guest or crashed, doing api cleanup + PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0); + } + }); + + warn "Finished cleanup for $vmid\n"; + + return undef; + }}); + my $print_agent_result = sub { my ($data) = @_; @@ -884,10 +956,10 @@ our $cmddef = { monitor => [ __PACKAGE__, 'monitor', ['vmid']], - agent => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], - { node => $nodename }, $print_agent_result ], + agent => { alias => 'guest cmd' }, guest => { + cmd => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { node => $nodename }, $print_agent_result ], passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { node => $nodename }], exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { node => $nodename }, $print_agent_result], 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { node => $nodename }, $print_agent_result], @@ -903,6 +975,8 @@ our $cmddef = { importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']], + cleanup => [ __PACKAGE__, 'cleanup', ['vmid', 'clean-shutdown', 'guest-requested'], { node => $nodename }], + }; 1;