1 package PVE
::API2
::Qemu
;
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
};
36 __PACKAGE__-
>register_method({
40 description
=> "Virtual machine index (per node).",
42 protected
=> 1, # qemu pid files are only readable by root
44 additionalProperties
=> 0,
46 node
=> get_standard_option
('pve-node'),
55 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
60 my $vmstatus = PVE
::QemuServer
::vmstatus
();
62 return PVE
::RESTHandler
::hash_to_array
($vmstatus, 'vmid');
66 __PACKAGE__-
>register_method({
70 description
=> "Create or restore a virtual machine.",
74 additionalProperties
=> 0,
75 properties
=> PVE
::QemuServer
::json_config_properties
(
77 node
=> get_standard_option
('pve-node'),
78 vmid
=> get_standard_option
('pve-vmid'),
80 description
=> "The backup file.",
85 storage
=> get_standard_option
('pve-storage-id', {
86 description
=> "Default storage.",
92 description
=> "Allow to overwrite existing VM.",
93 requires
=> 'archive',
98 description
=> "Assign a unique random ethernet address.",
99 requires
=> 'archive',
109 my $rpcenv = PVE
::RPCEnvironment
::get
();
111 my $user = $rpcenv->get_user();
113 my $node = extract_param
($param, 'node');
115 my $vmid = extract_param
($param, 'vmid');
117 my $archive = extract_param
($param, 'archive');
119 my $storage = extract_param
($param, 'storage');
121 my $force = extract_param
($param, 'force');
123 my $unique = extract_param
($param, 'unique');
125 my $filename = PVE
::QemuServer
::config_file
($vmid);
127 my $storecfg = PVE
::Storage
::config
();
129 PVE
::Cluster
::check_cfs_quorum
();
132 &$resolve_cdrom_alias($param);
134 foreach my $opt (keys %$param) {
135 if (PVE
::QemuServer
::valid_drivename
($opt)) {
136 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
137 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
139 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
140 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
144 PVE
::QemuServer
::add_random_macs
($param);
146 my $keystr = join(' ', keys %$param);
147 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
149 if ($archive eq '-') {
150 die "pipe requires cli environment\n"
151 && $rpcenv->{type
} ne 'cli';
154 if (PVE
::Storage
::parse_volume_id
($archive, 1)) {
155 $path = PVE
::Storage
::path
($storecfg, $archive);
157 raise_param_exc
({ archive
=> "Only root can pass arbitrary paths." })
158 if $user ne 'root@pam';
160 $path = abs_path
($archive);
162 die "can't find archive file '$archive'\n" if !($path && -f
$path);
167 my $restorefn = sub {
170 die "unable to restore vm $vmid: config file already exists\n"
173 die "unable to restore vm $vmid: vm is running\n"
174 if PVE
::QemuServer
::check_running
($vmid);
176 # destroy existing data - keep empty config
177 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
181 PVE
::QemuServer
::restore_archive
($archive, $vmid, {
183 unique
=> $unique });
186 return $rpcenv->fork_worker('qmrestore', $vmid, $user, $realcmd);
191 # second test (after locking test is accurate)
192 die "unable to create vm $vmid: config file already exists\n"
200 $vollist = PVE
::QemuServer
::create_disks
($storecfg, $vmid, $param, $storage);
202 # try to be smart about bootdisk
203 my @disks = PVE
::QemuServer
::disknames
();
205 foreach my $ds (reverse @disks) {
206 next if !$param->{$ds};
207 my $disk = PVE
::QemuServer
::parse_drive
($ds, $param->{$ds});
208 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
212 if (!$param->{bootdisk
} && $firstdisk) {
213 $param->{bootdisk
} = $firstdisk;
216 PVE
::QemuServer
::create_conf_nolock
($vmid, $param);
221 foreach my $volid (@$vollist) {
222 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
225 die "create failed - $err";
229 return $rpcenv->fork_worker('qmcreate', $vmid, $user, $realcmd);
232 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
235 __PACKAGE__-
>register_method({
240 description
=> "Directory index",
242 additionalProperties
=> 0,
244 node
=> get_standard_option
('pve-node'),
245 vmid
=> get_standard_option
('pve-vmid'),
253 subdir
=> { type
=> 'string' },
256 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
262 { subdir
=> 'config' },
263 { subdir
=> 'status' },
264 { subdir
=> 'unlink' },
265 { subdir
=> 'vncproxy' },
266 { subdir
=> 'migrate' },
268 { subdir
=> 'rrddata' },
269 { subdir
=> 'monitor' },
275 __PACKAGE__-
>register_method({
277 path
=> '{vmid}/rrd',
279 protected
=> 1, # fixme: can we avoid that?
281 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
283 description
=> "Read VM RRD statistics (returns PNG)",
285 additionalProperties
=> 0,
287 node
=> get_standard_option
('pve-node'),
288 vmid
=> get_standard_option
('pve-vmid'),
290 description
=> "Specify the time frame you are interested in.",
292 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
295 description
=> "The list of datasources you want to display.",
296 type
=> 'string', format
=> 'pve-configid-list',
299 description
=> "The RRD consolidation function",
301 enum
=> [ 'AVERAGE', 'MAX' ],
309 filename
=> { type
=> 'string' },
315 return PVE
::Cluster
::create_rrd_graph
(
316 "pve2-vm/$param->{vmid}", $param->{timeframe
},
317 $param->{ds
}, $param->{cf
});
321 __PACKAGE__-
>register_method({
323 path
=> '{vmid}/rrddata',
325 protected
=> 1, # fixme: can we avoid that?
327 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
329 description
=> "Read VM RRD statistics",
331 additionalProperties
=> 0,
333 node
=> get_standard_option
('pve-node'),
334 vmid
=> get_standard_option
('pve-vmid'),
336 description
=> "Specify the time frame you are interested in.",
338 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
341 description
=> "The RRD consolidation function",
343 enum
=> [ 'AVERAGE', 'MAX' ],
358 return PVE
::Cluster
::create_rrd_data
(
359 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
363 __PACKAGE__-
>register_method({
365 path
=> '{vmid}/config',
368 description
=> "Get virtual machine configuration.",
370 additionalProperties
=> 0,
372 node
=> get_standard_option
('pve-node'),
373 vmid
=> get_standard_option
('pve-vmid'),
381 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
388 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
393 __PACKAGE__-
>register_method({
395 path
=> '{vmid}/config',
399 description
=> "Set virtual machine options.",
401 additionalProperties
=> 0,
402 properties
=> PVE
::QemuServer
::json_config_properties
(
404 node
=> get_standard_option
('pve-node'),
405 vmid
=> get_standard_option
('pve-vmid'),
406 skiplock
=> get_standard_option
('skiplock'),
408 type
=> 'string', format
=> 'pve-configid-list',
409 description
=> "A list of settings you want to delete.",
414 description
=> $opt_force_description,
416 requires
=> 'delete',
420 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
426 returns
=> { type
=> 'null'},
430 my $rpcenv = PVE
::RPCEnvironment
::get
();
432 my $user = $rpcenv->get_user();
434 my $node = extract_param
($param, 'node');
436 my $vmid = extract_param
($param, 'vmid');
438 my $digest = extract_param
($param, 'digest');
440 my @paramarr = (); # used for log message
441 foreach my $key (keys %$param) {
442 push @paramarr, "-$key", $param->{$key};
445 my $skiplock = extract_param
($param, 'skiplock');
446 raise_param_exc
({ skiplock
=> "Only root may use this option." })
447 if $skiplock && $user ne 'root@pam';
449 my $delete = extract_param
($param, 'delete');
450 my $force = extract_param
($param, 'force');
452 die "no options specified\n" if !$delete && !scalar(keys %$param);
454 my $storecfg = PVE
::Storage
::config
();
456 &$resolve_cdrom_alias($param);
460 my $conf = PVE
::QemuServer
::load_config
($vmid);
462 die "checksum missmatch (file change by other user?)\n"
463 if $digest && $digest ne $conf->{digest
};
465 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
467 PVE
::Cluster
::log_msg
('info', $user, "update VM $vmid: " . join (' ', @paramarr));
470 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
472 $opt = 'ide2' if $opt eq 'cdrom';
473 die "you can't use '-$opt' and '-delete $opt' at the same time\n"
474 if defined($param->{$opt});
476 if (!PVE
::QemuServer
::option_exists
($opt)) {
477 raise_param_exc
({ delete => "unknown option '$opt'" });
480 next if !defined($conf->{$opt});
482 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
485 if (PVE
::QemuServer
::valid_drivename
($opt)) {
486 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
488 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
489 my $volid = $drive->{file
};
491 if ($volid !~ m
|^/|) {
493 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
494 if ($owner && ($owner == $vmid)) {
496 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
500 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
505 } elsif ($opt =~ m/^unused/) {
506 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
507 my $volid = $drive->{file
};
508 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
513 PVE
::QemuServer
::change_config_nolock
($vmid, {}, { $opt => 1 }, 1);
517 foreach my $opt (keys %$param) {
520 if (PVE
::QemuServer
::valid_drivename
($opt)) {
521 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
522 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
524 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
525 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
528 if (PVE
::QemuServer
::drive_is_cdrom
($drive) && PVE
::QemuServer
::check_running
($vmid)) {
529 if ($drive->{file
} eq 'none') {
530 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
531 #delete $param->{$opt};
534 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
535 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
536 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
543 my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
544 if ($drive->{file} ne $old_drive->{file} && !PVE::QemuServer::drive_is_cdrom($old_drive)) {
547 eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
548 if ($owner && ($owner == $vmid)) {
549 die "error hot-unplug
$opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
550 PVE::QemuServer::add_unused_volume($conf, $old_drive->{file}, $vmid);
554 my $settings = { $opt => $param->{$opt} };
555 PVE::QemuServer::create_disks($storecfg, $vmid, $settings, $conf);
556 $param->{$opt} = $settings->{$opt};
558 if(!PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive)) {
559 PVE::QemuServer::add_unused_volume($conf,$drive->{file},$vmid);
560 PVE::QemuServer::change_config_nolock($vmid, {}, { $opt => 1 }, 1);
561 die "error hotplug
$opt - put disk
in unused
";
567 if ($opt =~ m/^net(\d+)$/) {
568 $net = PVE::QemuServer::parse_net($param->{$opt});
569 $param->{$opt} = PVE::QemuServer::print_net($net);
570 #if online update, then unplug first
571 die "error hot-unplug
$opt for update
" if $conf->{$opt} && !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
574 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
576 #nic hotplug after config write as we need it for pve-bridge script
577 if (defined ($net)) {
578 if(!PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net)) {
579 #rewrite conf to remove nic if hotplug fail
580 PVE::QemuServer::change_config_nolock($vmid, {}, { $opt => 1 }, 1);
581 die "error hotplug
$opt";
587 PVE::QemuServer::lock_config($vmid, $updatefn);
593 __PACKAGE__->register_method({
594 name => 'destroy_vm',
599 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
601 additionalProperties => 0,
603 node => get_standard_option('pve-node'),
604 vmid => get_standard_option('pve-vmid'),
605 skiplock => get_standard_option('skiplock'),
614 my $rpcenv = PVE::RPCEnvironment::get();
616 my $user = $rpcenv->get_user();
618 my $vmid = $param->{vmid};
620 my $skiplock = $param->{skiplock};
621 raise_param_exc({ skiplock => "Only root may
use this option
." })
622 if $skiplock && $user ne 'root@pam';
625 my $conf = PVE::QemuServer::load_config($vmid);
627 my $storecfg = PVE::Storage::config();
632 syslog('info', "destroy VM
$vmid: $upid\n");
634 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
637 return $rpcenv->fork_worker('qmdestroy', $vmid, $user, $realcmd);
640 __PACKAGE__->register_method({
642 path => '{vmid}/unlink',
646 description => "Unlink
/delete disk images
.",
648 additionalProperties => 0,
650 node => get_standard_option('pve-node'),
651 vmid => get_standard_option('pve-vmid'),
653 type => 'string', format => 'pve-configid-list',
654 description => "A list of disk IDs you want to
delete.",
658 description => $opt_force_description,
663 returns => { type => 'null'},
667 $param->{delete} = extract_param($param, 'idlist');
669 __PACKAGE__->update_vm($param);
676 __PACKAGE__->register_method({
678 path => '{vmid}/vncproxy',
682 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
684 description => "Creates a TCP VNC proxy connections
.",
686 additionalProperties => 0,
688 node => get_standard_option('pve-node'),
689 vmid => get_standard_option('pve-vmid'),
693 additionalProperties => 0,
695 user => { type => 'string' },
696 ticket => { type => 'string' },
697 cert => { type => 'string' },
698 port => { type => 'integer' },
699 upid => { type => 'string' },
705 my $rpcenv = PVE::RPCEnvironment::get();
707 my $user = $rpcenv->get_user();
709 my $vmid = $param->{vmid};
710 my $node = $param->{node};
712 my $authpath = "/vms/$vmid";
714 my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath);
716 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
719 my $port = PVE::Tools::next_vnc_port();
723 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
724 $remip = PVE::Cluster::remote_node_ip($node);
727 # NOTE: kvm VNC traffic is already TLS encrypted,
728 # so we select the fastest chipher here (or 'none'?)
729 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
730 '-c', 'blowfish-cbc', $remip] : [];
737 syslog('info', "starting vnc proxy
$upid\n");
739 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
741 my $qmstr = join(' ', @$qmcmd);
743 # also redirect stderr (else we get RFB protocol errors)
744 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
746 PVE::Tools::run_command($cmd);
751 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $user, $realcmd);
762 __PACKAGE__->register_method({
764 path => '{vmid}/status',
767 description => "Directory
index",
769 additionalProperties => 0,
771 node => get_standard_option('pve-node'),
772 vmid => get_standard_option('pve-vmid'),
780 subdir => { type => 'string' },
783 links => [ { rel => 'child', href => "{subdir
}" } ],
789 my $conf = PVE::QemuServer::load_config($param->{vmid});
792 { subdir => 'current' },
793 { subdir => 'start' },
794 { subdir => 'stop' },
800 __PACKAGE__->register_method({
802 path => '{vmid}/status/current',
805 protected => 1, # qemu pid files are only readable by root
806 description => "Get virtual machine status
.",
808 additionalProperties => 0,
810 node => get_standard_option('pve-node'),
811 vmid => get_standard_option('pve-vmid'),
814 returns => { type => 'object' },
819 my $conf = PVE::QemuServer::load_config($param->{vmid});
821 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
822 my $status = $vmstatus->{$param->{vmid}};
824 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
825 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
834 __PACKAGE__->register_method({
836 path => '{vmid}/status/start',
840 description => "Start virtual machine
.",
842 additionalProperties => 0,
844 node => get_standard_option('pve-node'),
845 vmid => get_standard_option('pve-vmid'),
846 skiplock => get_standard_option('skiplock'),
847 stateuri => get_standard_option('pve-qm-stateuri'),
856 my $rpcenv = PVE::RPCEnvironment::get();
858 my $user = $rpcenv->get_user();
860 my $node = extract_param($param, 'node');
862 my $vmid = extract_param($param, 'vmid');
864 my $stateuri = extract_param($param, 'stateuri');
865 raise_param_exc({ stateuri => "Only root may
use this option
." })
866 if $stateuri && $user ne 'root@pam';
868 my $skiplock = extract_param($param, 'skiplock');
869 raise_param_exc({ skiplock => "Only root may
use this option
." })
870 if $skiplock && $user ne 'root@pam';
872 my $storecfg = PVE::Storage::config();
877 syslog('info', "start VM
$vmid: $upid\n");
879 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
884 return $rpcenv->fork_worker('qmstart', $vmid, $user, $realcmd);
887 __PACKAGE__->register_method({
889 path => '{vmid}/status/stop',
893 description => "Stop virtual machine
.",
895 additionalProperties => 0,
897 node => get_standard_option('pve-node'),
898 vmid => get_standard_option('pve-vmid'),
899 skiplock => get_standard_option('skiplock'),
901 description => "Wait maximal timeout seconds
.",
907 description => "Do
not decativate storage volumes
.",
920 my $rpcenv = PVE::RPCEnvironment::get();
922 my $user = $rpcenv->get_user();
924 my $node = extract_param($param, 'node');
926 my $vmid = extract_param($param, 'vmid');
928 my $skiplock = extract_param($param, 'skiplock');
929 raise_param_exc({ skiplock => "Only root may
use this option
." })
930 if $skiplock && $user ne 'root@pam';
932 my $keepActive = extract_param($param, 'keepActive');
933 raise_param_exc({ keepActive => "Only root may
use this option
." })
934 if $keepActive && $user ne 'root@pam';
936 my $storecfg = PVE::Storage::config();
941 syslog('info', "stop VM
$vmid: $upid\n");
943 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
944 $param->{timeout}, 0, 1, $keepActive);
949 return $rpcenv->fork_worker('qmstop', $vmid, $user, $realcmd);
952 __PACKAGE__->register_method({
954 path => '{vmid}/status/reset',
958 description => "Reset virtual machine
.",
960 additionalProperties => 0,
962 node => get_standard_option('pve-node'),
963 vmid => get_standard_option('pve-vmid'),
964 skiplock => get_standard_option('skiplock'),
973 my $rpcenv = PVE::RPCEnvironment::get();
975 my $user = $rpcenv->get_user();
977 my $node = extract_param($param, 'node');
979 my $vmid = extract_param($param, 'vmid');
981 my $skiplock = extract_param($param, 'skiplock');
982 raise_param_exc({ skiplock => "Only root may
use this option
." })
983 if $skiplock && $user ne 'root@pam';
985 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
990 PVE::QemuServer::vm_reset($vmid, $skiplock);
995 return $rpcenv->fork_worker('qmreset', $vmid, $user, $realcmd);
998 __PACKAGE__->register_method({
999 name => 'vm_shutdown',
1000 path => '{vmid}/status/shutdown',
1004 description => "Shutdown virtual machine
.",
1006 additionalProperties => 0,
1008 node => get_standard_option('pve-node'),
1009 vmid => get_standard_option('pve-vmid'),
1010 skiplock => get_standard_option('skiplock'),
1012 description => "Wait maximal timeout seconds
.",
1018 description => "Make sure the VM stops
.",
1024 description => "Do
not decativate storage volumes
.",
1037 my $rpcenv = PVE::RPCEnvironment::get();
1039 my $user = $rpcenv->get_user();
1041 my $node = extract_param($param, 'node');
1043 my $vmid = extract_param($param, 'vmid');
1045 my $skiplock = extract_param($param, 'skiplock');
1046 raise_param_exc({ skiplock => "Only root may
use this option
." })
1047 if $skiplock && $user ne 'root@pam';
1049 my $keepActive = extract_param($param, 'keepActive');
1050 raise_param_exc({ keepActive => "Only root may
use this option
." })
1051 if $keepActive && $user ne 'root@pam';
1053 my $storecfg = PVE::Storage::config();
1058 syslog('info', "shutdown VM
$vmid: $upid\n");
1060 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1061 1, $param->{forceStop}, $keepActive);
1066 return $rpcenv->fork_worker('qmshutdown', $vmid, $user, $realcmd);
1069 __PACKAGE__->register_method({
1070 name => 'vm_suspend',
1071 path => '{vmid}/status/suspend',
1075 description => "Suspend virtual machine
.",
1077 additionalProperties => 0,
1079 node => get_standard_option('pve-node'),
1080 vmid => get_standard_option('pve-vmid'),
1081 skiplock => get_standard_option('skiplock'),
1090 my $rpcenv = PVE::RPCEnvironment::get();
1092 my $user = $rpcenv->get_user();
1094 my $node = extract_param($param, 'node');
1096 my $vmid = extract_param($param, 'vmid');
1098 my $skiplock = extract_param($param, 'skiplock');
1099 raise_param_exc({ skiplock => "Only root may
use this option
." })
1100 if $skiplock && $user ne 'root@pam';
1102 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1107 syslog('info', "suspend VM
$vmid: $upid\n");
1109 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1114 return $rpcenv->fork_worker('qmsuspend', $vmid, $user, $realcmd);
1117 __PACKAGE__->register_method({
1118 name => 'vm_resume',
1119 path => '{vmid}/status/resume',
1123 description => "Resume virtual machine
.",
1125 additionalProperties => 0,
1127 node => get_standard_option('pve-node'),
1128 vmid => get_standard_option('pve-vmid'),
1129 skiplock => get_standard_option('skiplock'),
1138 my $rpcenv = PVE::RPCEnvironment::get();
1140 my $user = $rpcenv->get_user();
1142 my $node = extract_param($param, 'node');
1144 my $vmid = extract_param($param, 'vmid');
1146 my $skiplock = extract_param($param, 'skiplock');
1147 raise_param_exc({ skiplock => "Only root may
use this option
." })
1148 if $skiplock && $user ne 'root@pam';
1150 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1155 syslog('info', "resume VM
$vmid: $upid\n");
1157 PVE::QemuServer::vm_resume($vmid, $skiplock);
1162 return $rpcenv->fork_worker('qmresume', $vmid, $user, $realcmd);
1165 __PACKAGE__->register_method({
1166 name => 'vm_sendkey',
1167 path => '{vmid}/sendkey',
1171 description => "Send key event to virtual machine
.",
1173 additionalProperties => 0,
1175 node => get_standard_option('pve-node'),
1176 vmid => get_standard_option('pve-vmid'),
1177 skiplock => get_standard_option('skiplock'),
1179 description => "The key
(qemu monitor encoding
).",
1184 returns => { type => 'null'},
1188 my $rpcenv = PVE::RPCEnvironment::get();
1190 my $user = $rpcenv->get_user();
1192 my $node = extract_param($param, 'node');
1194 my $vmid = extract_param($param, 'vmid');
1196 my $skiplock = extract_param($param, 'skiplock');
1197 raise_param_exc({ skiplock => "Only root may
use this option
." })
1198 if $skiplock && $user ne 'root@pam';
1200 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1205 __PACKAGE__->register_method({
1206 name => 'migrate_vm',
1207 path => '{vmid}/migrate',
1211 description => "Migrate virtual machine
. Creates a new migration task
.",
1213 additionalProperties => 0,
1215 node => get_standard_option('pve-node'),
1216 vmid => get_standard_option('pve-vmid'),
1217 target => get_standard_option('pve-node', { description => "Target node
." }),
1220 description => "Use online
/live migration
.",
1225 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1232 description => "the task ID
.",
1237 my $rpcenv = PVE::RPCEnvironment::get();
1239 my $user = $rpcenv->get_user();
1241 my $target = extract_param($param, 'target');
1243 my $localnode = PVE::INotify::nodename();
1244 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1246 PVE::Cluster::check_cfs_quorum();
1248 PVE::Cluster::check_node_exists($target);
1250 my $targetip = PVE::Cluster::remote_node_ip($target);
1252 my $vmid = extract_param($param, 'vmid');
1254 raise_param_exc({ force => "Only root may
use this option
." })
1255 if $param->{force} && $user ne 'root@pam';
1258 my $conf = PVE::QemuServer::load_config($vmid);
1260 # try to detect errors early
1262 PVE::QemuServer::check_lock($conf);
1264 if (PVE::QemuServer::check_running($vmid)) {
1265 die "cant migrate running VM without
--online
\n"
1266 if !$param->{online};
1272 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1275 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd);
1280 __PACKAGE__->register_method({
1282 path => '{vmid}/monitor',
1286 description => "Execute Qemu monitor commands
.",
1288 additionalProperties => 0,
1290 node => get_standard_option('pve-node'),
1291 vmid => get_standard_option('pve-vmid'),
1294 description => "The monitor command
.",
1298 returns => { type => 'string'},
1302 my $vmid = $param->{vmid};
1304 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1308 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1310 $res = "ERROR
: $@" if $@;