1 package PVE
::API2
::Qemu
;
7 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);;
9 use PVE
::Tools
qw(extract_param);
10 use PVE
::Exception
qw(raise raise_param_exc);
12 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::RPCEnvironment
;
17 use PVE
::AccessControl
;
20 use Data
::Dumper
; # fixme: remove
22 use base
qw(PVE::RESTHandler);
24 my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal.";
26 my $resolve_cdrom_alias = sub {
29 if (my $value = $param->{cdrom
}) {
30 $value .= ",media=cdrom" if $value !~ m/media=/;
31 $param->{ide2
} = $value;
32 delete $param->{cdrom
};
37 my $check_storage_access = sub {
38 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
40 PVE
::QemuServer
::foreach_drive
($settings, sub {
41 my ($ds, $drive) = @_;
43 my $isCDROM = PVE
::QemuServer
::drive_is_cdrom
($drive);
45 my $volid = $drive->{file
};
47 if (!$volid || $volid eq 'none') {
49 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
50 my ($storeid, $size) = ($2 || $default_storage, $3);
51 die "no storage ID specified (and no default storage)\n" if !$storeid;
52 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
54 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
55 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
60 # Note: $pool is only needed when creating a VM, because pool permissions
61 # are automatically inherited if VM already exists inside a pool.
62 my $create_disks = sub {
63 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
68 PVE
::QemuServer
::foreach_drive
($settings, sub {
71 my $volid = $disk->{file
};
73 if (!$volid || $volid eq 'none') {
74 $res->{$ds} = $settings->{$ds};
75 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
76 my ($storeid, $size) = ($2 || $default_storage, $3);
77 die "no storage ID specified (and no default storage)\n" if !$storeid;
78 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
79 my $fmt = $disk->{format
} || $defformat;
80 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
81 $fmt, undef, $size*1024*1024);
82 $disk->{file
} = $volid;
83 push @$vollist, $volid;
84 delete $disk->{format
}; # no longer needed
85 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
87 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
88 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
89 $res->{$ds} = $settings->{$ds};
93 # free allocated images on error
95 syslog
('err', "VM $vmid creating disks failed");
96 foreach my $volid (@$vollist) {
97 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
103 # modify vm config if everything went well
104 foreach my $ds (keys %$res) {
105 $conf->{$ds} = $res->{$ds};
111 my $check_vm_modify_config_perm = sub {
112 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
114 return 1 if $authuser eq 'root@pam';
116 foreach my $opt (@$key_list) {
117 # disk checks need to be done somewhere else
118 next if PVE
::QemuServer
::valid_drivename
($opt);
120 if ($opt eq 'sockets' || $opt eq 'cores' ||
121 $opt eq 'cpu' || $opt eq 'smp' ||
122 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
123 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
124 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
125 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
126 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
127 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
128 } elsif ($opt eq 'args' || $opt eq 'lock') {
129 die "only root can set '$opt' config\n";
130 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
131 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
132 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
133 } elsif ($opt =~ m/^net\d+$/) {
134 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
136 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
143 __PACKAGE__-
>register_method({
147 description
=> "Virtual machine index (per node).",
149 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
153 protected
=> 1, # qemu pid files are only readable by root
155 additionalProperties
=> 0,
157 node
=> get_standard_option
('pve-node'),
166 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
171 my $rpcenv = PVE
::RPCEnvironment
::get
();
172 my $authuser = $rpcenv->get_user();
174 my $vmstatus = PVE
::QemuServer
::vmstatus
();
177 foreach my $vmid (keys %$vmstatus) {
178 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
180 my $data = $vmstatus->{$vmid};
181 $data->{vmid
} = $vmid;
188 __PACKAGE__-
>register_method({
192 description
=> "Create or restore a virtual machine.",
194 description
=> "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
196 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
197 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
203 additionalProperties
=> 0,
204 properties
=> PVE
::QemuServer
::json_config_properties
(
206 node
=> get_standard_option
('pve-node'),
207 vmid
=> get_standard_option
('pve-vmid'),
209 description
=> "The backup file.",
214 storage
=> get_standard_option
('pve-storage-id', {
215 description
=> "Default storage.",
221 description
=> "Allow to overwrite existing VM.",
222 requires
=> 'archive',
227 description
=> "Assign a unique random ethernet address.",
228 requires
=> 'archive',
232 type
=> 'string', format
=> 'pve-poolid',
233 description
=> "Add the VM to the specified pool.",
243 my $rpcenv = PVE
::RPCEnvironment
::get
();
245 my $authuser = $rpcenv->get_user();
247 my $node = extract_param
($param, 'node');
249 my $vmid = extract_param
($param, 'vmid');
251 my $archive = extract_param
($param, 'archive');
253 my $storage = extract_param
($param, 'storage');
255 my $force = extract_param
($param, 'force');
257 my $unique = extract_param
($param, 'unique');
259 my $pool = extract_param
($param, 'pool');
261 my $filename = PVE
::QemuServer
::config_file
($vmid);
263 my $storecfg = PVE
::Storage
::config
();
265 PVE
::Cluster
::check_cfs_quorum
();
267 if (defined($pool)) {
268 $rpcenv->check_pool_exist($pool);
269 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
272 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
273 if defined($storage);
276 &$resolve_cdrom_alias($param);
278 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
280 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
282 foreach my $opt (keys %$param) {
283 if (PVE
::QemuServer
::valid_drivename
($opt)) {
284 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
285 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
287 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
288 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
292 PVE
::QemuServer
::add_random_macs
($param);
294 my $keystr = join(' ', keys %$param);
295 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
297 if ($archive eq '-') {
298 die "pipe requires cli environment\n"
299 && $rpcenv->{type
} ne 'cli';
301 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
302 die "can't find archive file '$archive'\n" if !($path && -f
$path);
307 my $addVMtoPoolFn = sub {
308 my $usercfg = cfs_read_file
("user.cfg");
309 if (my $data = $usercfg->{pools
}->{$pool}) {
310 $data->{vms
}->{$vmid} = 1;
311 $usercfg->{vms
}->{$vmid} = $pool;
312 cfs_write_file
("user.cfg", $usercfg);
316 my $restorefn = sub {
319 die "unable to restore vm $vmid: config file already exists\n"
322 die "unable to restore vm $vmid: vm is running\n"
323 if PVE
::QemuServer
::check_running
($vmid);
325 # destroy existing data - keep empty config
326 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
330 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
333 unique
=> $unique });
335 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
338 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
343 # second test (after locking test is accurate)
344 die "unable to create vm $vmid: config file already exists\n"
355 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
357 # try to be smart about bootdisk
358 my @disks = PVE
::QemuServer
::disknames
();
360 foreach my $ds (reverse @disks) {
361 next if !$conf->{$ds};
362 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
363 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
367 if (!$conf->{bootdisk
} && $firstdisk) {
368 $conf->{bootdisk
} = $firstdisk;
371 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
377 foreach my $volid (@$vollist) {
378 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
381 die "create failed - $err";
384 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
387 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
390 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
393 __PACKAGE__-
>register_method({
398 description
=> "Directory index",
403 additionalProperties
=> 0,
405 node
=> get_standard_option
('pve-node'),
406 vmid
=> get_standard_option
('pve-vmid'),
414 subdir
=> { type
=> 'string' },
417 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
423 { subdir
=> 'config' },
424 { subdir
=> 'status' },
425 { subdir
=> 'unlink' },
426 { subdir
=> 'vncproxy' },
427 { subdir
=> 'migrate' },
429 { subdir
=> 'rrddata' },
430 { subdir
=> 'monitor' },
436 __PACKAGE__-
>register_method({
438 path
=> '{vmid}/rrd',
440 protected
=> 1, # fixme: can we avoid that?
442 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
444 description
=> "Read VM RRD statistics (returns PNG)",
446 additionalProperties
=> 0,
448 node
=> get_standard_option
('pve-node'),
449 vmid
=> get_standard_option
('pve-vmid'),
451 description
=> "Specify the time frame you are interested in.",
453 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
456 description
=> "The list of datasources you want to display.",
457 type
=> 'string', format
=> 'pve-configid-list',
460 description
=> "The RRD consolidation function",
462 enum
=> [ 'AVERAGE', 'MAX' ],
470 filename
=> { type
=> 'string' },
476 return PVE
::Cluster
::create_rrd_graph
(
477 "pve2-vm/$param->{vmid}", $param->{timeframe
},
478 $param->{ds
}, $param->{cf
});
482 __PACKAGE__-
>register_method({
484 path
=> '{vmid}/rrddata',
486 protected
=> 1, # fixme: can we avoid that?
488 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
490 description
=> "Read VM RRD statistics",
492 additionalProperties
=> 0,
494 node
=> get_standard_option
('pve-node'),
495 vmid
=> get_standard_option
('pve-vmid'),
497 description
=> "Specify the time frame you are interested in.",
499 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
502 description
=> "The RRD consolidation function",
504 enum
=> [ 'AVERAGE', 'MAX' ],
519 return PVE
::Cluster
::create_rrd_data
(
520 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
524 __PACKAGE__-
>register_method({
526 path
=> '{vmid}/config',
529 description
=> "Get virtual machine configuration.",
531 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
534 additionalProperties
=> 0,
536 node
=> get_standard_option
('pve-node'),
537 vmid
=> get_standard_option
('pve-vmid'),
545 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
552 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
557 my $vm_is_volid_owner = sub {
558 my ($storecfg, $vmid, $volid) =@_;
560 if ($volid !~ m
|^/|) {
562 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
563 if ($owner && ($owner == $vmid)) {
571 my $test_deallocate_drive = sub {
572 my ($storecfg, $vmid, $key, $drive, $force) = @_;
574 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
575 my $volid = $drive->{file
};
576 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
577 if ($force || $key =~ m/^unused/) {
578 my $sid = PVE
::Storage
::parse_volume_id
($volid);
587 my $delete_drive = sub {
588 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
590 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
591 my $volid = $drive->{file
};
592 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
593 if ($force || $key =~ m/^unused/) {
594 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
597 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
602 delete $conf->{$key};
605 my $vmconfig_delete_option = sub {
606 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
608 return if !defined($conf->{$opt});
610 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
613 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
615 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
616 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
617 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
621 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
624 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
625 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
627 delete $conf->{$opt};
630 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
633 my $vmconfig_update_disk = sub {
634 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
636 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
638 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
639 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
641 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
646 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
648 my $media = $drive->{media
} || 'disk';
649 my $oldmedia = $old_drive->{media
} || 'disk';
650 die "unable to change media type\n" if $media ne $oldmedia;
652 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
653 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
655 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
656 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
661 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
662 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
664 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
665 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
667 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
669 if (PVE
::QemuServer
::check_running
($vmid)) {
670 if ($drive->{file
} eq 'none') {
671 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
673 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
674 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
675 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
679 } else { # hotplug new disks
681 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
685 my $vmconfig_update_net = sub {
686 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
689 #if online update, then unplug first
690 die "error hot-unplug
$opt for update
" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
693 $conf->{$opt} = $value;
694 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
695 $conf = PVE::QemuServer::load_config($vmid); # update/reload
697 my $net = PVE::QemuServer::parse_net($conf->{$opt});
699 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
702 my $vm_config_perm_list = [
712 __PACKAGE__->register_method({
714 path => '{vmid}/config',
718 description => "Set virtual machine options
.",
720 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
723 additionalProperties => 0,
724 properties => PVE::QemuServer::json_config_properties(
726 node => get_standard_option('pve-node'),
727 vmid => get_standard_option('pve-vmid'),
728 skiplock => get_standard_option('skiplock'),
730 type => 'string', format => 'pve-configid-list',
731 description => "A list of settings you want to
delete.",
736 description => $opt_force_description,
738 requires => 'delete',
742 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
748 returns => { type => 'null'},
752 my $rpcenv = PVE::RPCEnvironment::get();
754 my $authuser = $rpcenv->get_user();
756 my $node = extract_param($param, 'node');
758 my $vmid = extract_param($param, 'vmid');
760 my $digest = extract_param($param, 'digest');
762 my @paramarr = (); # used for log message
763 foreach my $key (keys %$param) {
764 push @paramarr, "-$key", $param->{$key};
767 my $skiplock = extract_param($param, 'skiplock');
768 raise_param_exc({ skiplock => "Only root may
use this option
." })
769 if $skiplock && $authuser ne 'root@pam';
771 my $delete_str = extract_param($param, 'delete');
773 my $force = extract_param($param, 'force');
775 die "no options specified
\n" if !$delete_str && !scalar(keys %$param);
777 my $storecfg = PVE::Storage::config();
779 &$resolve_cdrom_alias($param);
781 # now try to verify all parameters
784 foreach my $opt (PVE::Tools::split_list($delete_str)) {
785 $opt = 'ide2' if $opt eq 'cdrom';
786 raise_param_exc({ delete => "you can
't use '-$opt' and " .
787 "-delete $opt' at the same
time" })
788 if defined($param->{$opt});
790 if (!PVE::QemuServer::option_exists($opt)) {
791 raise_param_exc({ delete => "unknown option
'$opt'" });
797 foreach my $opt (keys %$param) {
798 if (PVE::QemuServer::valid_drivename($opt)) {
800 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
801 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
802 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
803 } elsif ($opt =~ m/^net(\d+)$/) {
805 my $net = PVE::QemuServer::parse_net($param->{$opt});
806 $param->{$opt} = PVE::QemuServer::print_net($net);
810 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
812 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
814 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
818 my $conf = PVE::QemuServer::load_config($vmid);
820 die "checksum missmatch
(file change by other user?
)\n"
821 if $digest && $digest ne $conf->{digest};
823 PVE::QemuServer::check_lock($conf) if !$skiplock;
825 PVE::Cluster::log_msg('info', $authuser, "update VM
$vmid: " . join (' ', @paramarr));
827 foreach my $opt (@delete) { # delete
828 $conf = PVE::QemuServer::load_config($vmid); # update/reload
829 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
832 foreach my $opt (keys %$param) { # add/change
834 $conf = PVE::QemuServer::load_config($vmid); # update/reload
836 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
838 if (PVE::QemuServer::valid_drivename($opt)) {
840 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
841 $opt, $param->{$opt}, $force);
843 } elsif ($opt =~ m/^net(\d+)$/) { #nics
845 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
846 $opt, $param->{$opt});
850 $conf->{$opt} = $param->{$opt};
851 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
856 PVE::QemuServer::lock_config($vmid, $updatefn);
862 __PACKAGE__->register_method({
863 name => 'destroy_vm',
868 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
870 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
873 additionalProperties => 0,
875 node => get_standard_option('pve-node'),
876 vmid => get_standard_option('pve-vmid'),
877 skiplock => get_standard_option('skiplock'),
886 my $rpcenv = PVE::RPCEnvironment::get();
888 my $authuser = $rpcenv->get_user();
890 my $vmid = $param->{vmid};
892 my $skiplock = $param->{skiplock};
893 raise_param_exc({ skiplock => "Only root may
use this option
." })
894 if $skiplock && $authuser ne 'root@pam';
897 my $conf = PVE::QemuServer::load_config($vmid);
899 my $storecfg = PVE::Storage::config();
901 my $delVMfromPoolFn = sub {
902 my $usercfg = cfs_read_file("user
.cfg
");
903 my $pool = $usercfg->{vms}->{$vmid};
904 if (my $data = $usercfg->{pools}->{$pool}) {
905 delete $data->{vms}->{$vmid};
906 delete $usercfg->{vms}->{$vmid};
907 cfs_write_file("user
.cfg
", $usercfg);
914 syslog('info', "destroy VM
$vmid: $upid\n");
916 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
918 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed
");
921 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
924 __PACKAGE__->register_method({
926 path => '{vmid}/unlink',
930 description => "Unlink
/delete disk images
.",
932 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
935 additionalProperties => 0,
937 node => get_standard_option('pve-node'),
938 vmid => get_standard_option('pve-vmid'),
940 type => 'string', format => 'pve-configid-list',
941 description => "A list of disk IDs you want to
delete.",
945 description => $opt_force_description,
950 returns => { type => 'null'},
954 $param->{delete} = extract_param($param, 'idlist');
956 __PACKAGE__->update_vm($param);
963 __PACKAGE__->register_method({
965 path => '{vmid}/vncproxy',
969 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
971 description => "Creates a TCP VNC proxy connections
.",
973 additionalProperties => 0,
975 node => get_standard_option('pve-node'),
976 vmid => get_standard_option('pve-vmid'),
980 additionalProperties => 0,
982 user => { type => 'string' },
983 ticket => { type => 'string' },
984 cert => { type => 'string' },
985 port => { type => 'integer' },
986 upid => { type => 'string' },
992 my $rpcenv = PVE::RPCEnvironment::get();
994 my $authuser = $rpcenv->get_user();
996 my $vmid = $param->{vmid};
997 my $node = $param->{node};
999 my $authpath = "/vms/$vmid";
1001 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1003 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
1006 my $port = PVE::Tools::next_vnc_port();
1010 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1011 $remip = PVE::Cluster::remote_node_ip($node);
1014 # NOTE: kvm VNC traffic is already TLS encrypted,
1015 # so we select the fastest chipher here (or 'none'?)
1016 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1017 '-c', 'blowfish-cbc', $remip] : [];
1024 syslog('info', "starting vnc proxy
$upid\n");
1026 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
1028 my $qmstr = join(' ', @$qmcmd);
1030 # also redirect stderr (else we get RFB protocol errors)
1031 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
1033 PVE::Tools::run_command($cmd);
1038 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1049 __PACKAGE__->register_method({
1051 path => '{vmid}/status',
1054 description => "Directory
index",
1059 additionalProperties => 0,
1061 node => get_standard_option('pve-node'),
1062 vmid => get_standard_option('pve-vmid'),
1070 subdir => { type => 'string' },
1073 links => [ { rel => 'child', href => "{subdir
}" } ],
1079 my $conf = PVE::QemuServer::load_config($param->{vmid});
1082 { subdir => 'current' },
1083 { subdir => 'start' },
1084 { subdir => 'stop' },
1090 __PACKAGE__->register_method({
1091 name => 'vm_status',
1092 path => '{vmid}/status/current',
1095 protected => 1, # qemu pid files are only readable by root
1096 description => "Get virtual machine status
.",
1098 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1101 additionalProperties => 0,
1103 node => get_standard_option('pve-node'),
1104 vmid => get_standard_option('pve-vmid'),
1107 returns => { type => 'object' },
1112 my $conf = PVE::QemuServer::load_config($param->{vmid});
1114 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1115 my $status = $vmstatus->{$param->{vmid}};
1117 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1118 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1127 __PACKAGE__->register_method({
1129 path => '{vmid}/status/start',
1133 description => "Start virtual machine
.",
1135 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1138 additionalProperties => 0,
1140 node => get_standard_option('pve-node'),
1141 vmid => get_standard_option('pve-vmid'),
1142 skiplock => get_standard_option('skiplock'),
1143 stateuri => get_standard_option('pve-qm-stateuri'),
1152 my $rpcenv = PVE::RPCEnvironment::get();
1154 my $authuser = $rpcenv->get_user();
1156 my $node = extract_param($param, 'node');
1158 my $vmid = extract_param($param, 'vmid');
1160 my $stateuri = extract_param($param, 'stateuri');
1161 raise_param_exc({ stateuri => "Only root may
use this option
." })
1162 if $stateuri && $authuser ne 'root@pam';
1164 my $skiplock = extract_param($param, 'skiplock');
1165 raise_param_exc({ skiplock => "Only root may
use this option
." })
1166 if $skiplock && $authuser ne 'root@pam';
1168 my $storecfg = PVE::Storage::config();
1173 syslog('info', "start VM
$vmid: $upid\n");
1175 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1180 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1183 __PACKAGE__->register_method({
1185 path => '{vmid}/status/stop',
1189 description => "Stop virtual machine
.",
1191 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1194 additionalProperties => 0,
1196 node => get_standard_option('pve-node'),
1197 vmid => get_standard_option('pve-vmid'),
1198 skiplock => get_standard_option('skiplock'),
1200 description => "Wait maximal timeout seconds
.",
1206 description => "Do
not decativate storage volumes
.",
1219 my $rpcenv = PVE::RPCEnvironment::get();
1221 my $authuser = $rpcenv->get_user();
1223 my $node = extract_param($param, 'node');
1225 my $vmid = extract_param($param, 'vmid');
1227 my $skiplock = extract_param($param, 'skiplock');
1228 raise_param_exc({ skiplock => "Only root may
use this option
." })
1229 if $skiplock && $authuser ne 'root@pam';
1231 my $keepActive = extract_param($param, 'keepActive');
1232 raise_param_exc({ keepActive => "Only root may
use this option
." })
1233 if $keepActive && $authuser ne 'root@pam';
1235 my $storecfg = PVE::Storage::config();
1240 syslog('info', "stop VM
$vmid: $upid\n");
1242 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1243 $param->{timeout}, 0, 1, $keepActive);
1248 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1251 __PACKAGE__->register_method({
1253 path => '{vmid}/status/reset',
1257 description => "Reset virtual machine
.",
1259 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1262 additionalProperties => 0,
1264 node => get_standard_option('pve-node'),
1265 vmid => get_standard_option('pve-vmid'),
1266 skiplock => get_standard_option('skiplock'),
1275 my $rpcenv = PVE::RPCEnvironment::get();
1277 my $authuser = $rpcenv->get_user();
1279 my $node = extract_param($param, 'node');
1281 my $vmid = extract_param($param, 'vmid');
1283 my $skiplock = extract_param($param, 'skiplock');
1284 raise_param_exc({ skiplock => "Only root may
use this option
." })
1285 if $skiplock && $authuser ne 'root@pam';
1287 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1292 PVE::QemuServer::vm_reset($vmid, $skiplock);
1297 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1300 __PACKAGE__->register_method({
1301 name => 'vm_shutdown',
1302 path => '{vmid}/status/shutdown',
1306 description => "Shutdown virtual machine
.",
1308 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1311 additionalProperties => 0,
1313 node => get_standard_option('pve-node'),
1314 vmid => get_standard_option('pve-vmid'),
1315 skiplock => get_standard_option('skiplock'),
1317 description => "Wait maximal timeout seconds
.",
1323 description => "Make sure the VM stops
.",
1329 description => "Do
not decativate storage volumes
.",
1342 my $rpcenv = PVE::RPCEnvironment::get();
1344 my $authuser = $rpcenv->get_user();
1346 my $node = extract_param($param, 'node');
1348 my $vmid = extract_param($param, 'vmid');
1350 my $skiplock = extract_param($param, 'skiplock');
1351 raise_param_exc({ skiplock => "Only root may
use this option
." })
1352 if $skiplock && $authuser ne 'root@pam';
1354 my $keepActive = extract_param($param, 'keepActive');
1355 raise_param_exc({ keepActive => "Only root may
use this option
." })
1356 if $keepActive && $authuser ne 'root@pam';
1358 my $storecfg = PVE::Storage::config();
1363 syslog('info', "shutdown VM
$vmid: $upid\n");
1365 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1366 1, $param->{forceStop}, $keepActive);
1371 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1374 __PACKAGE__->register_method({
1375 name => 'vm_suspend',
1376 path => '{vmid}/status/suspend',
1380 description => "Suspend virtual machine
.",
1382 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1385 additionalProperties => 0,
1387 node => get_standard_option('pve-node'),
1388 vmid => get_standard_option('pve-vmid'),
1389 skiplock => get_standard_option('skiplock'),
1398 my $rpcenv = PVE::RPCEnvironment::get();
1400 my $authuser = $rpcenv->get_user();
1402 my $node = extract_param($param, 'node');
1404 my $vmid = extract_param($param, 'vmid');
1406 my $skiplock = extract_param($param, 'skiplock');
1407 raise_param_exc({ skiplock => "Only root may
use this option
." })
1408 if $skiplock && $authuser ne 'root@pam';
1410 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1415 syslog('info', "suspend VM
$vmid: $upid\n");
1417 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1422 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1425 __PACKAGE__->register_method({
1426 name => 'vm_resume',
1427 path => '{vmid}/status/resume',
1431 description => "Resume virtual machine
.",
1433 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1436 additionalProperties => 0,
1438 node => get_standard_option('pve-node'),
1439 vmid => get_standard_option('pve-vmid'),
1440 skiplock => get_standard_option('skiplock'),
1449 my $rpcenv = PVE::RPCEnvironment::get();
1451 my $authuser = $rpcenv->get_user();
1453 my $node = extract_param($param, 'node');
1455 my $vmid = extract_param($param, 'vmid');
1457 my $skiplock = extract_param($param, 'skiplock');
1458 raise_param_exc({ skiplock => "Only root may
use this option
." })
1459 if $skiplock && $authuser ne 'root@pam';
1461 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1466 syslog('info', "resume VM
$vmid: $upid\n");
1468 PVE::QemuServer::vm_resume($vmid, $skiplock);
1473 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1476 __PACKAGE__->register_method({
1477 name => 'vm_sendkey',
1478 path => '{vmid}/sendkey',
1482 description => "Send key event to virtual machine
.",
1484 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1487 additionalProperties => 0,
1489 node => get_standard_option('pve-node'),
1490 vmid => get_standard_option('pve-vmid'),
1491 skiplock => get_standard_option('skiplock'),
1493 description => "The key
(qemu monitor encoding
).",
1498 returns => { type => 'null'},
1502 my $rpcenv = PVE::RPCEnvironment::get();
1504 my $authuser = $rpcenv->get_user();
1506 my $node = extract_param($param, 'node');
1508 my $vmid = extract_param($param, 'vmid');
1510 my $skiplock = extract_param($param, 'skiplock');
1511 raise_param_exc({ skiplock => "Only root may
use this option
." })
1512 if $skiplock && $authuser ne 'root@pam';
1514 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1519 __PACKAGE__->register_method({
1520 name => 'migrate_vm',
1521 path => '{vmid}/migrate',
1525 description => "Migrate virtual machine
. Creates a new migration task
.",
1527 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1530 additionalProperties => 0,
1532 node => get_standard_option('pve-node'),
1533 vmid => get_standard_option('pve-vmid'),
1534 target => get_standard_option('pve-node', { description => "Target node
." }),
1537 description => "Use online
/live migration
.",
1542 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1549 description => "the task ID
.",
1554 my $rpcenv = PVE::RPCEnvironment::get();
1556 my $authuser = $rpcenv->get_user();
1558 my $target = extract_param($param, 'target');
1560 my $localnode = PVE::INotify::nodename();
1561 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1563 PVE::Cluster::check_cfs_quorum();
1565 PVE::Cluster::check_node_exists($target);
1567 my $targetip = PVE::Cluster::remote_node_ip($target);
1569 my $vmid = extract_param($param, 'vmid');
1571 raise_param_exc({ force => "Only root may
use this option
." })
1572 if $param->{force} && $authuser ne 'root@pam';
1575 my $conf = PVE::QemuServer::load_config($vmid);
1577 # try to detect errors early
1579 PVE::QemuServer::check_lock($conf);
1581 if (PVE::QemuServer::check_running($vmid)) {
1582 die "cant migrate running VM without
--online
\n"
1583 if !$param->{online};
1589 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1592 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1597 __PACKAGE__->register_method({
1599 path => '{vmid}/monitor',
1603 description => "Execute Qemu monitor commands
.",
1605 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1608 additionalProperties => 0,
1610 node => get_standard_option('pve-node'),
1611 vmid => get_standard_option('pve-vmid'),
1614 description => "The monitor command
.",
1618 returns => { type => 'string'},
1622 my $vmid = $param->{vmid};
1624 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1628 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1630 $res = "ERROR
: $@" if $@;