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 path
=> '/vms/{vmid}',
282 privs
=> [ 'VM.Audit' ],
284 description
=> "Read VM RRD statistics (returns PNG)",
286 additionalProperties
=> 0,
288 node
=> get_standard_option
('pve-node'),
289 vmid
=> get_standard_option
('pve-vmid'),
291 description
=> "Specify the time frame you are interested in.",
293 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
296 description
=> "The list of datasources you want to display.",
297 type
=> 'string', format
=> 'pve-configid-list',
300 description
=> "The RRD consolidation function",
302 enum
=> [ 'AVERAGE', 'MAX' ],
310 filename
=> { type
=> 'string' },
316 return PVE
::Cluster
::create_rrd_graph
(
317 "pve2-vm/$param->{vmid}", $param->{timeframe
},
318 $param->{ds
}, $param->{cf
});
322 __PACKAGE__-
>register_method({
324 path
=> '{vmid}/rrddata',
326 protected
=> 1, # fixme: can we avoid that?
328 path
=> '/vms/{vmid}',
329 privs
=> [ 'VM.Audit' ],
331 description
=> "Read VM RRD statistics",
333 additionalProperties
=> 0,
335 node
=> get_standard_option
('pve-node'),
336 vmid
=> get_standard_option
('pve-vmid'),
338 description
=> "Specify the time frame you are interested in.",
340 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
343 description
=> "The RRD consolidation function",
345 enum
=> [ 'AVERAGE', 'MAX' ],
360 return PVE
::Cluster
::create_rrd_data
(
361 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
365 __PACKAGE__-
>register_method({
367 path
=> '{vmid}/config',
370 description
=> "Get virtual machine configuration.",
372 additionalProperties
=> 0,
374 node
=> get_standard_option
('pve-node'),
375 vmid
=> get_standard_option
('pve-vmid'),
383 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
390 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
395 __PACKAGE__-
>register_method({
397 path
=> '{vmid}/config',
401 description
=> "Set virtual machine options.",
403 additionalProperties
=> 0,
404 properties
=> PVE
::QemuServer
::json_config_properties
(
406 node
=> get_standard_option
('pve-node'),
407 vmid
=> get_standard_option
('pve-vmid'),
408 skiplock
=> get_standard_option
('skiplock'),
410 type
=> 'string', format
=> 'pve-configid-list',
411 description
=> "A list of settings you want to delete.",
416 description
=> $opt_force_description,
418 requires
=> 'delete',
422 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
428 returns
=> { type
=> 'null'},
432 my $rpcenv = PVE
::RPCEnvironment
::get
();
434 my $user = $rpcenv->get_user();
436 my $node = extract_param
($param, 'node');
438 my $vmid = extract_param
($param, 'vmid');
440 my $digest = extract_param
($param, 'digest');
443 my @paramarr = (); # used for log message
444 foreach my $key (keys %$param) {
445 push @paramarr, "-$key", $param->{$key};
448 my $skiplock = extract_param
($param, 'skiplock');
449 raise_param_exc
({ skiplock
=> "Only root may use this option." })
450 if $skiplock && $user ne 'root@pam';
452 my $delete = extract_param
($param, 'delete');
453 my $force = extract_param
($param, 'force');
455 die "no options specified\n" if !$delete && !scalar(keys %$param);
457 my $storecfg = PVE
::Storage
::config
();
459 &$resolve_cdrom_alias($param);
464 foreach my $opt (keys %$param) {
465 if (PVE
::QemuServer
::valid_drivename
($opt)) {
466 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
467 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
468 if ($drive->{file
} eq 'eject') {
470 delete $param->{$opt};
474 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
475 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
477 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) {
478 $cdchange->{$opt} = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
483 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
484 $opt = 'ide2' if $opt eq 'cdrom';
485 die "you can't use '-$opt' and '-delete $opt' at the same time\n"
486 if defined($param->{$opt});
489 PVE
::QemuServer
::add_random_macs
($param);
495 my $conf = PVE
::QemuServer
::load_config
($vmid);
497 die "checksum missmatch (file change by other user?)\n"
498 if $digest && $digest ne $conf->{digest
};
500 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
502 PVE
::Cluster
::log_msg
('info', $user, "update VM $vmid: " . join (' ', @paramarr));
505 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
506 if(PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt)) {
507 push(@newdelete, $opt);
510 push(@hotplugerr, $opt);
513 $delete = join(',', @newdelete) if scalar(@newdelete) > 0;
515 foreach my $opt (keys %$eject) {
517 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
518 $cdchange->{$opt} = undef if PVE
::QemuServer
::drive_is_cdrom
($drive);
520 raise_param_exc
({ $opt => "eject failed - drive does not exist." });
524 foreach my $opt (keys %$param) {
525 next if !PVE
::QemuServer
::valid_drivename
($opt);
526 next if !$conf->{$opt};
527 my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
528 next if PVE
::QemuServer
::drive_is_cdrom
($old_drive);
529 my $new_drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
530 if ($new_drive->{file
} ne $old_drive->{file
}) {
532 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $old_drive->{file
}); };
533 if ($owner && ($owner == $vmid)) {
534 PVE
::QemuServer
::add_unused_volume
($conf, $param, $old_drive->{file
});
541 foreach my $opt (PVE
::Tools
::split_list
($delete)) {
542 $opt = 'ide2' if $opt eq 'cdrom';
543 if (!PVE
::QemuServer
::option_exists
($opt)) {
544 raise_param_exc
({ delete => "unknown option '$opt'" });
546 next if !defined($conf->{$opt});
547 if (PVE
::QemuServer
::valid_drivename
($opt)) {
548 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
549 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) {
550 $cdchange->{$opt} = undef;
552 my $volid = $drive->{file
};
554 if ($volid !~ m
|^/|) {
556 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
557 if ($owner && ($owner == $vmid)) {
559 push @$vollist, $volid;
561 PVE
::QemuServer
::add_unused_volume
($conf, $param, $volid);
566 } elsif ($opt =~ m/^unused/) {
567 push @$vollist, $conf->{$opt};
573 PVE
::QemuServer
::create_disks
($storecfg, $vmid, $param, $conf);
576 foreach my $opt (keys %$param) {
577 if($opt =~ m/^(scsi|virtio)(\d+)$/) {
578 my $device = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
579 if(!PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $device)) {
581 PVE
::QemuServer
::add_unused_volume
($param, $device->{file
});
582 push(@hotplugerr, $opt);
587 PVE
::QemuServer
::change_config_nolock
($vmid, $param, $unset, 1);
589 return if !PVE
::QemuServer
::check_running
($vmid);
591 foreach my $opt (keys %$cdchange) {
592 my $qdn = PVE
::QemuServer
::qemu_drive_name
($opt, 'cdrom');
593 my $path = $cdchange->{$opt};
594 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject $qdn", 0);
595 PVE
::QemuServer
::vm_monitor_command
($vmid, "change $qdn \"$path\"", 0) if $path;
599 PVE::QemuServer::lock_config($vmid, $updatefn);
601 foreach my $volid (@$vollist) {
602 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
607 raise_param_exc({ hotplug => "error hotplug
/unplug
".join(',', @hotplugerr)})
608 if scalar(@hotplugerr) > 0;
614 __PACKAGE__->register_method({
615 name => 'destroy_vm',
620 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
622 additionalProperties => 0,
624 node => get_standard_option('pve-node'),
625 vmid => get_standard_option('pve-vmid'),
626 skiplock => get_standard_option('skiplock'),
635 my $rpcenv = PVE::RPCEnvironment::get();
637 my $user = $rpcenv->get_user();
639 my $vmid = $param->{vmid};
641 my $skiplock = $param->{skiplock};
642 raise_param_exc({ skiplock => "Only root may
use this option
." })
643 if $skiplock && $user ne 'root@pam';
646 my $conf = PVE::QemuServer::load_config($vmid);
648 my $storecfg = PVE::Storage::config();
653 syslog('info', "destroy VM
$vmid: $upid\n");
655 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
658 return $rpcenv->fork_worker('qmdestroy', $vmid, $user, $realcmd);
661 __PACKAGE__->register_method({
663 path => '{vmid}/unlink',
667 description => "Unlink
/delete disk images
.",
669 additionalProperties => 0,
671 node => get_standard_option('pve-node'),
672 vmid => get_standard_option('pve-vmid'),
674 type => 'string', format => 'pve-configid-list',
675 description => "A list of disk IDs you want to
delete.",
679 description => $opt_force_description,
684 returns => { type => 'null'},
688 $param->{delete} = extract_param($param, 'idlist');
690 __PACKAGE__->update_vm($param);
697 __PACKAGE__->register_method({
699 path => '{vmid}/vncproxy',
703 path => '/vms/{vmid}',
704 privs => [ 'VM.Console' ],
706 description => "Creates a TCP VNC proxy connections
.",
708 additionalProperties => 0,
710 node => get_standard_option('pve-node'),
711 vmid => get_standard_option('pve-vmid'),
715 additionalProperties => 0,
717 user => { type => 'string' },
718 ticket => { type => 'string' },
719 cert => { type => 'string' },
720 port => { type => 'integer' },
721 upid => { type => 'string' },
727 my $rpcenv = PVE::RPCEnvironment::get();
729 my $user = $rpcenv->get_user();
731 my $vmid = $param->{vmid};
732 my $node = $param->{node};
734 my $authpath = "/vms/$vmid";
736 my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath);
738 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
741 my $port = PVE::Tools::next_vnc_port();
745 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
746 $remip = PVE::Cluster::remote_node_ip($node);
749 # NOTE: kvm VNC traffic is already TLS encrypted,
750 # so we select the fastest chipher here (or 'none'?)
751 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
752 '-c', 'blowfish-cbc', $remip] : [];
759 syslog('info', "starting vnc proxy
$upid\n");
761 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
763 my $qmstr = join(' ', @$qmcmd);
765 # also redirect stderr (else we get RFB protocol errors)
766 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
768 PVE::Tools::run_command($cmd);
773 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $user, $realcmd);
784 __PACKAGE__->register_method({
786 path => '{vmid}/status',
789 description => "Directory
index",
791 additionalProperties => 0,
793 node => get_standard_option('pve-node'),
794 vmid => get_standard_option('pve-vmid'),
802 subdir => { type => 'string' },
805 links => [ { rel => 'child', href => "{subdir
}" } ],
811 my $conf = PVE::QemuServer::load_config($param->{vmid});
814 { subdir => 'current' },
815 { subdir => 'start' },
816 { subdir => 'stop' },
822 __PACKAGE__->register_method({
824 path => '{vmid}/status/current',
827 protected => 1, # qemu pid files are only readable by root
828 description => "Get virtual machine status
.",
830 additionalProperties => 0,
832 node => get_standard_option('pve-node'),
833 vmid => get_standard_option('pve-vmid'),
836 returns => { type => 'object' },
841 my $conf = PVE::QemuServer::load_config($param->{vmid});
843 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
844 my $status = $vmstatus->{$param->{vmid}};
846 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
847 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
856 __PACKAGE__->register_method({
858 path => '{vmid}/status/start',
862 description => "Start virtual machine
.",
864 additionalProperties => 0,
866 node => get_standard_option('pve-node'),
867 vmid => get_standard_option('pve-vmid'),
868 skiplock => get_standard_option('skiplock'),
869 stateuri => get_standard_option('pve-qm-stateuri'),
878 my $rpcenv = PVE::RPCEnvironment::get();
880 my $user = $rpcenv->get_user();
882 my $node = extract_param($param, 'node');
884 my $vmid = extract_param($param, 'vmid');
886 my $stateuri = extract_param($param, 'stateuri');
887 raise_param_exc({ stateuri => "Only root may
use this option
." })
888 if $stateuri && $user ne 'root@pam';
890 my $skiplock = extract_param($param, 'skiplock');
891 raise_param_exc({ skiplock => "Only root may
use this option
." })
892 if $skiplock && $user ne 'root@pam';
894 my $storecfg = PVE::Storage::config();
899 syslog('info', "start VM
$vmid: $upid\n");
901 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
906 return $rpcenv->fork_worker('qmstart', $vmid, $user, $realcmd);
909 __PACKAGE__->register_method({
911 path => '{vmid}/status/stop',
915 description => "Stop virtual machine
.",
917 additionalProperties => 0,
919 node => get_standard_option('pve-node'),
920 vmid => get_standard_option('pve-vmid'),
921 skiplock => get_standard_option('skiplock'),
923 description => "Wait maximal timeout seconds
.",
929 description => "Do
not decativate storage volumes
.",
942 my $rpcenv = PVE::RPCEnvironment::get();
944 my $user = $rpcenv->get_user();
946 my $node = extract_param($param, 'node');
948 my $vmid = extract_param($param, 'vmid');
950 my $skiplock = extract_param($param, 'skiplock');
951 raise_param_exc({ skiplock => "Only root may
use this option
." })
952 if $skiplock && $user ne 'root@pam';
954 my $keepActive = extract_param($param, 'keepActive');
955 raise_param_exc({ keepActive => "Only root may
use this option
." })
956 if $keepActive && $user ne 'root@pam';
958 my $storecfg = PVE::Storage::config();
963 syslog('info', "stop VM
$vmid: $upid\n");
965 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
966 $param->{timeout}, 0, 1, $keepActive);
971 return $rpcenv->fork_worker('qmstop', $vmid, $user, $realcmd);
974 __PACKAGE__->register_method({
976 path => '{vmid}/status/reset',
980 description => "Reset virtual machine
.",
982 additionalProperties => 0,
984 node => get_standard_option('pve-node'),
985 vmid => get_standard_option('pve-vmid'),
986 skiplock => get_standard_option('skiplock'),
995 my $rpcenv = PVE::RPCEnvironment::get();
997 my $user = $rpcenv->get_user();
999 my $node = extract_param($param, 'node');
1001 my $vmid = extract_param($param, 'vmid');
1003 my $skiplock = extract_param($param, 'skiplock');
1004 raise_param_exc({ skiplock => "Only root may
use this option
." })
1005 if $skiplock && $user ne 'root@pam';
1007 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1012 PVE::QemuServer::vm_reset($vmid, $skiplock);
1017 return $rpcenv->fork_worker('qmreset', $vmid, $user, $realcmd);
1020 __PACKAGE__->register_method({
1021 name => 'vm_shutdown',
1022 path => '{vmid}/status/shutdown',
1026 description => "Shutdown virtual machine
.",
1028 additionalProperties => 0,
1030 node => get_standard_option('pve-node'),
1031 vmid => get_standard_option('pve-vmid'),
1032 skiplock => get_standard_option('skiplock'),
1034 description => "Wait maximal timeout seconds
.",
1040 description => "Make sure the VM stops
.",
1046 description => "Do
not decativate storage volumes
.",
1059 my $rpcenv = PVE::RPCEnvironment::get();
1061 my $user = $rpcenv->get_user();
1063 my $node = extract_param($param, 'node');
1065 my $vmid = extract_param($param, 'vmid');
1067 my $skiplock = extract_param($param, 'skiplock');
1068 raise_param_exc({ skiplock => "Only root may
use this option
." })
1069 if $skiplock && $user ne 'root@pam';
1071 my $keepActive = extract_param($param, 'keepActive');
1072 raise_param_exc({ keepActive => "Only root may
use this option
." })
1073 if $keepActive && $user ne 'root@pam';
1075 my $storecfg = PVE::Storage::config();
1080 syslog('info', "shutdown VM
$vmid: $upid\n");
1082 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1083 1, $param->{forceStop}, $keepActive);
1088 return $rpcenv->fork_worker('qmshutdown', $vmid, $user, $realcmd);
1091 __PACKAGE__->register_method({
1092 name => 'vm_suspend',
1093 path => '{vmid}/status/suspend',
1097 description => "Suspend virtual machine
.",
1099 additionalProperties => 0,
1101 node => get_standard_option('pve-node'),
1102 vmid => get_standard_option('pve-vmid'),
1103 skiplock => get_standard_option('skiplock'),
1112 my $rpcenv = PVE::RPCEnvironment::get();
1114 my $user = $rpcenv->get_user();
1116 my $node = extract_param($param, 'node');
1118 my $vmid = extract_param($param, 'vmid');
1120 my $skiplock = extract_param($param, 'skiplock');
1121 raise_param_exc({ skiplock => "Only root may
use this option
." })
1122 if $skiplock && $user ne 'root@pam';
1124 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1129 syslog('info', "suspend VM
$vmid: $upid\n");
1131 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1136 return $rpcenv->fork_worker('qmsuspend', $vmid, $user, $realcmd);
1139 __PACKAGE__->register_method({
1140 name => 'vm_resume',
1141 path => '{vmid}/status/resume',
1145 description => "Resume virtual machine
.",
1147 additionalProperties => 0,
1149 node => get_standard_option('pve-node'),
1150 vmid => get_standard_option('pve-vmid'),
1151 skiplock => get_standard_option('skiplock'),
1160 my $rpcenv = PVE::RPCEnvironment::get();
1162 my $user = $rpcenv->get_user();
1164 my $node = extract_param($param, 'node');
1166 my $vmid = extract_param($param, 'vmid');
1168 my $skiplock = extract_param($param, 'skiplock');
1169 raise_param_exc({ skiplock => "Only root may
use this option
." })
1170 if $skiplock && $user ne 'root@pam';
1172 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1177 syslog('info', "resume VM
$vmid: $upid\n");
1179 PVE::QemuServer::vm_resume($vmid, $skiplock);
1184 return $rpcenv->fork_worker('qmresume', $vmid, $user, $realcmd);
1187 __PACKAGE__->register_method({
1188 name => 'vm_sendkey',
1189 path => '{vmid}/sendkey',
1193 description => "Send key event to virtual machine
.",
1195 additionalProperties => 0,
1197 node => get_standard_option('pve-node'),
1198 vmid => get_standard_option('pve-vmid'),
1199 skiplock => get_standard_option('skiplock'),
1201 description => "The key
(qemu monitor encoding
).",
1206 returns => { type => 'null'},
1210 my $rpcenv = PVE::RPCEnvironment::get();
1212 my $user = $rpcenv->get_user();
1214 my $node = extract_param($param, 'node');
1216 my $vmid = extract_param($param, 'vmid');
1218 my $skiplock = extract_param($param, 'skiplock');
1219 raise_param_exc({ skiplock => "Only root may
use this option
." })
1220 if $skiplock && $user ne 'root@pam';
1222 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1227 __PACKAGE__->register_method({
1228 name => 'migrate_vm',
1229 path => '{vmid}/migrate',
1233 description => "Migrate virtual machine
. Creates a new migration task
.",
1235 additionalProperties => 0,
1237 node => get_standard_option('pve-node'),
1238 vmid => get_standard_option('pve-vmid'),
1239 target => get_standard_option('pve-node', { description => "Target node
." }),
1242 description => "Use online
/live migration
.",
1247 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1254 description => "the task ID
.",
1259 my $rpcenv = PVE::RPCEnvironment::get();
1261 my $user = $rpcenv->get_user();
1263 my $target = extract_param($param, 'target');
1265 my $localnode = PVE::INotify::nodename();
1266 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1268 PVE::Cluster::check_cfs_quorum();
1270 PVE::Cluster::check_node_exists($target);
1272 my $targetip = PVE::Cluster::remote_node_ip($target);
1274 my $vmid = extract_param($param, 'vmid');
1276 raise_param_exc({ force => "Only root may
use this option
." })
1277 if $param->{force} && $user ne 'root@pam';
1280 my $conf = PVE::QemuServer::load_config($vmid);
1282 # try to detect errors early
1284 PVE::QemuServer::check_lock($conf);
1286 if (PVE::QemuServer::check_running($vmid)) {
1287 die "cant migrate running VM without
--online
\n"
1288 if !$param->{online};
1294 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1297 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd);
1302 __PACKAGE__->register_method({
1304 path => '{vmid}/monitor',
1308 description => "Execute Qemu monitor commands
.",
1310 additionalProperties => 0,
1312 node => get_standard_option('pve-node'),
1313 vmid => get_standard_option('pve-vmid'),
1316 description => "The monitor command
.",
1320 returns => { type => 'string'},
1324 my $vmid = $param->{vmid};
1326 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1330 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1332 $res = "ERROR
: $@" if $@;