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);
458 my $conf = PVE
::QemuServer
::load_config
($vmid);
460 die "checksum missmatch (file change by other user?)\n"
461 if $digest && $digest ne $conf->{digest
};
463 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
465 PVE
::Cluster
::log_msg
('info', $user, "update VM $vmid: " . join (' ', @paramarr));
468 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
470 $opt = 'ide2' if $opt eq 'cdrom';
471 die "you can't use '-$opt' and '-delete $opt' at the same time\n"
472 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); };
514 PVE
::QemuServer
::change_config_nolock
($vmid, {}, $unset, 1);
520 foreach my $opt (keys %$param) {
523 if (PVE
::QemuServer
::valid_drivename
($opt)) {
524 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
525 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
527 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
528 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
531 if (PVE
::QemuServer
::drive_is_cdrom
($drive) && PVE
::QemuServer
::check_running
($vmid)) {
532 if ($drive->{file
} eq 'none') {
533 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
534 #delete $param->{$opt};
537 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
538 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
539 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
546 my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
547 if ($drive->{file} ne $old_drive->{file} && !PVE::QemuServer::drive_is_cdrom($old_drive)) {
550 eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
551 if ($owner && ($owner == $vmid)) {
552 die "error hot-unplug
$opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
553 PVE::QemuServer::add_unused_volume($conf, $old_drive->{file}, $vmid);
557 my $settings = { $opt => $param->{$opt} };
558 PVE::QemuServer::create_disks($storecfg, $vmid, $settings, $conf);
559 $param->{$opt} = $settings->{$opt};
561 if(!PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive)) {
562 PVE::QemuServer::add_unused_volume($conf,$drive->{file},$vmid);
563 PVE::QemuServer::change_config_nolock($vmid, {}, { $opt => 1 }, 1);
564 die "error hotplug
$opt - put disk
in unused
";
569 if ($opt =~ m/^net(\d+)$/) {
570 my $net = PVE::QemuServer::parse_net($param->{$opt});
571 $param->{$opt} = PVE::QemuServer::print_net($net);
574 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
581 __PACKAGE__->register_method({
582 name => 'destroy_vm',
587 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
589 additionalProperties => 0,
591 node => get_standard_option('pve-node'),
592 vmid => get_standard_option('pve-vmid'),
593 skiplock => get_standard_option('skiplock'),
602 my $rpcenv = PVE::RPCEnvironment::get();
604 my $user = $rpcenv->get_user();
606 my $vmid = $param->{vmid};
608 my $skiplock = $param->{skiplock};
609 raise_param_exc({ skiplock => "Only root may
use this option
." })
610 if $skiplock && $user ne 'root@pam';
613 my $conf = PVE::QemuServer::load_config($vmid);
615 my $storecfg = PVE::Storage::config();
620 syslog('info', "destroy VM
$vmid: $upid\n");
622 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
625 return $rpcenv->fork_worker('qmdestroy', $vmid, $user, $realcmd);
628 __PACKAGE__->register_method({
630 path => '{vmid}/unlink',
634 description => "Unlink
/delete disk images
.",
636 additionalProperties => 0,
638 node => get_standard_option('pve-node'),
639 vmid => get_standard_option('pve-vmid'),
641 type => 'string', format => 'pve-configid-list',
642 description => "A list of disk IDs you want to
delete.",
646 description => $opt_force_description,
651 returns => { type => 'null'},
655 $param->{delete} = extract_param($param, 'idlist');
657 __PACKAGE__->update_vm($param);
664 __PACKAGE__->register_method({
666 path => '{vmid}/vncproxy',
670 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
672 description => "Creates a TCP VNC proxy connections
.",
674 additionalProperties => 0,
676 node => get_standard_option('pve-node'),
677 vmid => get_standard_option('pve-vmid'),
681 additionalProperties => 0,
683 user => { type => 'string' },
684 ticket => { type => 'string' },
685 cert => { type => 'string' },
686 port => { type => 'integer' },
687 upid => { type => 'string' },
693 my $rpcenv = PVE::RPCEnvironment::get();
695 my $user = $rpcenv->get_user();
697 my $vmid = $param->{vmid};
698 my $node = $param->{node};
700 my $authpath = "/vms/$vmid";
702 my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath);
704 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
707 my $port = PVE::Tools::next_vnc_port();
711 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
712 $remip = PVE::Cluster::remote_node_ip($node);
715 # NOTE: kvm VNC traffic is already TLS encrypted,
716 # so we select the fastest chipher here (or 'none'?)
717 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
718 '-c', 'blowfish-cbc', $remip] : [];
725 syslog('info', "starting vnc proxy
$upid\n");
727 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
729 my $qmstr = join(' ', @$qmcmd);
731 # also redirect stderr (else we get RFB protocol errors)
732 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
734 PVE::Tools::run_command($cmd);
739 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $user, $realcmd);
750 __PACKAGE__->register_method({
752 path => '{vmid}/status',
755 description => "Directory
index",
757 additionalProperties => 0,
759 node => get_standard_option('pve-node'),
760 vmid => get_standard_option('pve-vmid'),
768 subdir => { type => 'string' },
771 links => [ { rel => 'child', href => "{subdir
}" } ],
777 my $conf = PVE::QemuServer::load_config($param->{vmid});
780 { subdir => 'current' },
781 { subdir => 'start' },
782 { subdir => 'stop' },
788 __PACKAGE__->register_method({
790 path => '{vmid}/status/current',
793 protected => 1, # qemu pid files are only readable by root
794 description => "Get virtual machine status
.",
796 additionalProperties => 0,
798 node => get_standard_option('pve-node'),
799 vmid => get_standard_option('pve-vmid'),
802 returns => { type => 'object' },
807 my $conf = PVE::QemuServer::load_config($param->{vmid});
809 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
810 my $status = $vmstatus->{$param->{vmid}};
812 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
813 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
822 __PACKAGE__->register_method({
824 path => '{vmid}/status/start',
828 description => "Start virtual machine
.",
830 additionalProperties => 0,
832 node => get_standard_option('pve-node'),
833 vmid => get_standard_option('pve-vmid'),
834 skiplock => get_standard_option('skiplock'),
835 stateuri => get_standard_option('pve-qm-stateuri'),
844 my $rpcenv = PVE::RPCEnvironment::get();
846 my $user = $rpcenv->get_user();
848 my $node = extract_param($param, 'node');
850 my $vmid = extract_param($param, 'vmid');
852 my $stateuri = extract_param($param, 'stateuri');
853 raise_param_exc({ stateuri => "Only root may
use this option
." })
854 if $stateuri && $user ne 'root@pam';
856 my $skiplock = extract_param($param, 'skiplock');
857 raise_param_exc({ skiplock => "Only root may
use this option
." })
858 if $skiplock && $user ne 'root@pam';
860 my $storecfg = PVE::Storage::config();
865 syslog('info', "start VM
$vmid: $upid\n");
867 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
872 return $rpcenv->fork_worker('qmstart', $vmid, $user, $realcmd);
875 __PACKAGE__->register_method({
877 path => '{vmid}/status/stop',
881 description => "Stop virtual machine
.",
883 additionalProperties => 0,
885 node => get_standard_option('pve-node'),
886 vmid => get_standard_option('pve-vmid'),
887 skiplock => get_standard_option('skiplock'),
889 description => "Wait maximal timeout seconds
.",
895 description => "Do
not decativate storage volumes
.",
908 my $rpcenv = PVE::RPCEnvironment::get();
910 my $user = $rpcenv->get_user();
912 my $node = extract_param($param, 'node');
914 my $vmid = extract_param($param, 'vmid');
916 my $skiplock = extract_param($param, 'skiplock');
917 raise_param_exc({ skiplock => "Only root may
use this option
." })
918 if $skiplock && $user ne 'root@pam';
920 my $keepActive = extract_param($param, 'keepActive');
921 raise_param_exc({ keepActive => "Only root may
use this option
." })
922 if $keepActive && $user ne 'root@pam';
924 my $storecfg = PVE::Storage::config();
929 syslog('info', "stop VM
$vmid: $upid\n");
931 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
932 $param->{timeout}, 0, 1, $keepActive);
937 return $rpcenv->fork_worker('qmstop', $vmid, $user, $realcmd);
940 __PACKAGE__->register_method({
942 path => '{vmid}/status/reset',
946 description => "Reset virtual machine
.",
948 additionalProperties => 0,
950 node => get_standard_option('pve-node'),
951 vmid => get_standard_option('pve-vmid'),
952 skiplock => get_standard_option('skiplock'),
961 my $rpcenv = PVE::RPCEnvironment::get();
963 my $user = $rpcenv->get_user();
965 my $node = extract_param($param, 'node');
967 my $vmid = extract_param($param, 'vmid');
969 my $skiplock = extract_param($param, 'skiplock');
970 raise_param_exc({ skiplock => "Only root may
use this option
." })
971 if $skiplock && $user ne 'root@pam';
973 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
978 PVE::QemuServer::vm_reset($vmid, $skiplock);
983 return $rpcenv->fork_worker('qmreset', $vmid, $user, $realcmd);
986 __PACKAGE__->register_method({
987 name => 'vm_shutdown',
988 path => '{vmid}/status/shutdown',
992 description => "Shutdown virtual machine
.",
994 additionalProperties => 0,
996 node => get_standard_option('pve-node'),
997 vmid => get_standard_option('pve-vmid'),
998 skiplock => get_standard_option('skiplock'),
1000 description => "Wait maximal timeout seconds
.",
1006 description => "Make sure the VM stops
.",
1012 description => "Do
not decativate storage volumes
.",
1025 my $rpcenv = PVE::RPCEnvironment::get();
1027 my $user = $rpcenv->get_user();
1029 my $node = extract_param($param, 'node');
1031 my $vmid = extract_param($param, 'vmid');
1033 my $skiplock = extract_param($param, 'skiplock');
1034 raise_param_exc({ skiplock => "Only root may
use this option
." })
1035 if $skiplock && $user ne 'root@pam';
1037 my $keepActive = extract_param($param, 'keepActive');
1038 raise_param_exc({ keepActive => "Only root may
use this option
." })
1039 if $keepActive && $user ne 'root@pam';
1041 my $storecfg = PVE::Storage::config();
1046 syslog('info', "shutdown VM
$vmid: $upid\n");
1048 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1049 1, $param->{forceStop}, $keepActive);
1054 return $rpcenv->fork_worker('qmshutdown', $vmid, $user, $realcmd);
1057 __PACKAGE__->register_method({
1058 name => 'vm_suspend',
1059 path => '{vmid}/status/suspend',
1063 description => "Suspend virtual machine
.",
1065 additionalProperties => 0,
1067 node => get_standard_option('pve-node'),
1068 vmid => get_standard_option('pve-vmid'),
1069 skiplock => get_standard_option('skiplock'),
1078 my $rpcenv = PVE::RPCEnvironment::get();
1080 my $user = $rpcenv->get_user();
1082 my $node = extract_param($param, 'node');
1084 my $vmid = extract_param($param, 'vmid');
1086 my $skiplock = extract_param($param, 'skiplock');
1087 raise_param_exc({ skiplock => "Only root may
use this option
." })
1088 if $skiplock && $user ne 'root@pam';
1090 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1095 syslog('info', "suspend VM
$vmid: $upid\n");
1097 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1102 return $rpcenv->fork_worker('qmsuspend', $vmid, $user, $realcmd);
1105 __PACKAGE__->register_method({
1106 name => 'vm_resume',
1107 path => '{vmid}/status/resume',
1111 description => "Resume virtual machine
.",
1113 additionalProperties => 0,
1115 node => get_standard_option('pve-node'),
1116 vmid => get_standard_option('pve-vmid'),
1117 skiplock => get_standard_option('skiplock'),
1126 my $rpcenv = PVE::RPCEnvironment::get();
1128 my $user = $rpcenv->get_user();
1130 my $node = extract_param($param, 'node');
1132 my $vmid = extract_param($param, 'vmid');
1134 my $skiplock = extract_param($param, 'skiplock');
1135 raise_param_exc({ skiplock => "Only root may
use this option
." })
1136 if $skiplock && $user ne 'root@pam';
1138 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1143 syslog('info', "resume VM
$vmid: $upid\n");
1145 PVE::QemuServer::vm_resume($vmid, $skiplock);
1150 return $rpcenv->fork_worker('qmresume', $vmid, $user, $realcmd);
1153 __PACKAGE__->register_method({
1154 name => 'vm_sendkey',
1155 path => '{vmid}/sendkey',
1159 description => "Send key event to virtual machine
.",
1161 additionalProperties => 0,
1163 node => get_standard_option('pve-node'),
1164 vmid => get_standard_option('pve-vmid'),
1165 skiplock => get_standard_option('skiplock'),
1167 description => "The key
(qemu monitor encoding
).",
1172 returns => { type => 'null'},
1176 my $rpcenv = PVE::RPCEnvironment::get();
1178 my $user = $rpcenv->get_user();
1180 my $node = extract_param($param, 'node');
1182 my $vmid = extract_param($param, 'vmid');
1184 my $skiplock = extract_param($param, 'skiplock');
1185 raise_param_exc({ skiplock => "Only root may
use this option
." })
1186 if $skiplock && $user ne 'root@pam';
1188 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1193 __PACKAGE__->register_method({
1194 name => 'migrate_vm',
1195 path => '{vmid}/migrate',
1199 description => "Migrate virtual machine
. Creates a new migration task
.",
1201 additionalProperties => 0,
1203 node => get_standard_option('pve-node'),
1204 vmid => get_standard_option('pve-vmid'),
1205 target => get_standard_option('pve-node', { description => "Target node
." }),
1208 description => "Use online
/live migration
.",
1213 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1220 description => "the task ID
.",
1225 my $rpcenv = PVE::RPCEnvironment::get();
1227 my $user = $rpcenv->get_user();
1229 my $target = extract_param($param, 'target');
1231 my $localnode = PVE::INotify::nodename();
1232 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1234 PVE::Cluster::check_cfs_quorum();
1236 PVE::Cluster::check_node_exists($target);
1238 my $targetip = PVE::Cluster::remote_node_ip($target);
1240 my $vmid = extract_param($param, 'vmid');
1242 raise_param_exc({ force => "Only root may
use this option
." })
1243 if $param->{force} && $user ne 'root@pam';
1246 my $conf = PVE::QemuServer::load_config($vmid);
1248 # try to detect errors early
1250 PVE::QemuServer::check_lock($conf);
1252 if (PVE::QemuServer::check_running($vmid)) {
1253 die "cant migrate running VM without
--online
\n"
1254 if !$param->{online};
1260 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1263 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd);
1268 __PACKAGE__->register_method({
1270 path => '{vmid}/monitor',
1274 description => "Execute Qemu monitor commands
.",
1276 additionalProperties => 0,
1278 node => get_standard_option('pve-node'),
1279 vmid => get_standard_option('pve-vmid'),
1282 description => "The monitor command
.",
1286 returns => { type => 'string'},
1290 my $vmid = $param->{vmid};
1292 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1296 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1298 $res = "ERROR
: $@" if $@;