foreach my $opt (@delete) {
$modified->{$opt} = 1;
$conf = PVE::QemuConfig->load_config($vmid); # update/reload
- if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
+
+ # value of what we want to delete, independent if pending or not
+ my $val = $conf->{$opt} // $conf->{pending}->{$opt};
+ if (!defined($val)) {
warn "cannot delete '$opt' - not set in current configuration!\n";
$modified->{$opt} = 0;
next;
}
+ my $is_pending_val = defined($conf->{pending}->{$opt});
if ($opt =~ m/^unused/) {
- my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+ my $drive = PVE::QemuServer::parse_drive($opt, $val);
PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
} elsif (PVE::QemuServer::is_valid_drivename($opt)) {
PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
- PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
- if defined($conf->{pending}->{$opt});
+ PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
+ if $is_pending_val;
PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
PVE::QemuConfig->write_config($vmid, $conf);
} elsif ($opt =~ m/^serial\d+$/) {
- if ($conf->{$opt} eq 'socket') {
+ if ($val eq 'socket') {
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
} elsif ($authuser ne 'root@pam') {
die "only root can delete '$opt' config for real devices\n";
PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
PVE::QemuConfig->write_config($vmid, $conf);
} elsif ($opt =~ m/^usb\d+$/) {
- if ($conf->{$opt} =~ m/spice/) {
+ if ($val =~ m/spice/) {
$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
} elsif ($authuser ne 'root@pam') {
die "only root can delete '$opt' config for real devices\n";
}
});
-
__PACKAGE__->register_method({
name => 'destroy_vm',
path => '{vmid}',
my ($param) = @_;
my $rpcenv = PVE::RPCEnvironment::get();
-
my $authuser = $rpcenv->get_user();
-
my $vmid = $param->{vmid};
my $skiplock = $param->{skiplock};
# test if VM exists
my $conf = PVE::QemuConfig->load_config($vmid);
-
my $storecfg = PVE::Storage::config();
-
PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
-
die "unable to remove VM $vmid - used in HA resources\n"
if PVE::HA::Config::vm_is_ha_managed($vmid);
my $upid = shift;
syslog('info', "destroy VM $vmid: $upid\n");
-
PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
-
PVE::AccessControl::remove_vm_access($vmid);
-
PVE::Firewall::remove_vmfw_conf($vmid);
};
{ subdir => 'current' },
{ subdir => 'start' },
{ subdir => 'stop' },
+ { subdir => 'reset' },
+ { subdir => 'shutdown' },
+ { subdir => 'suspend' },
+ { subdir => 'reboot' },
];
return $res;
# read spice ticket from STDIN
my $spice_ticket;
- if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
+ if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
if (defined(my $line = <STDIN>)) {
chomp $line;
$spice_ticket = $line;
}
}});
+__PACKAGE__->register_method({
+ name => 'vm_reboot',
+ path => '{vmid}/status/reboot',
+ method => 'POST',
+ protected => 1,
+ proxyto => 'node',
+ description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.",
+ permissions => {
+ check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ vmid => get_standard_option('pve-vmid',
+ { completion => \&PVE::QemuServer::complete_vmid_running }),
+ timeout => {
+ description => "Wait maximal timeout seconds for the shutdown.",
+ type => 'integer',
+ minimum => 0,
+ optional => 1,
+ },
+ },
+ },
+ returns => {
+ type => 'string',
+ },
+ code => sub {
+ my ($param) = @_;
+
+ my $rpcenv = PVE::RPCEnvironment::get();
+ my $authuser = $rpcenv->get_user();
+
+ my $node = extract_param($param, 'node');
+ my $vmid = extract_param($param, 'vmid');
+
+ my $qmpstatus = eval {
+ PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
+ };
+ my $err = $@ if $@;
+
+ if (!$err && $qmpstatus->{status} eq "paused") {
+ die "VM is paused - cannot shutdown\n";
+ }
+
+ die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
+ my $realcmd = sub {
+ my $upid = shift;
+
+ syslog('info', "requesting reboot of VM $vmid: $upid\n");
+ PVE::QemuServer::vm_reboot($vmid, $param->{timeout});
+ return;
+ };
+
+ return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd);
+ }});
+
__PACKAGE__->register_method({
name => 'vm_suspend',
path => '{vmid}/status/suspend',
}),
online => {
type => 'boolean',
- description => "Use online/live migration.",
+ description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
optional => 1,
},
force => {
my $vmid = extract_param($param, 'vmid');
- raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
- if !$param->{online} && $param->{targetstorage};
-
raise_param_exc({ force => "Only root may use this option." })
if $param->{force} && $authuser ne 'root@pam';
PVE::QemuConfig->check_lock($conf);
if (PVE::QemuServer::check_running($vmid)) {
- die "cant migrate running VM without --online\n"
- if !$param->{online};
+ die "can't migrate running VM without --online\n" if !$param->{online};
+ } else {
+ warn "VM isn't running. Doing offline migration instead\n." if $param->{online};
+ $param->{online} = 0;
}
+ raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
+ if !$param->{online} && $param->{targetstorage};
+
my $storecfg = PVE::Storage::config();
if( $param->{targetstorage}) {
PVE::Storage::activate_volumes($storecfg, [$volid]);
my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
+ die "Could not determine current size of volume '$volid'\n" if !defined($size);
+
die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
my ($ext, $newsize, $unit) = ($1, $2, $4);
if ($unit) {