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 my $check_volume_access = sub {
37 my ($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool) = @_;
40 if (my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1)) {
41 my ($ownervm, $vtype);
42 ($path, $ownervm, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
43 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
44 # we simply allow access
45 } elsif (!$ownervm || ($ownervm != $vmid)) {
46 # allow if we are Datastore administrator
47 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $sid, [ 'Datastore.Allocate' ]);
50 die "Only root can pass arbitrary filesystem paths."
51 if $authuser ne 'root@pam';
53 $path = abs_path
($volid);
58 # Note: $pool is only needed when creating a VM, because pool permissions
59 # are automatically inherited if VM already exists inside a pool.
60 my $create_disks = sub {
61 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
63 # check permissions first
66 PVE
::QemuServer
::foreach_drive
($settings, sub {
69 return if PVE
::QemuServer
::drive_is_cdrom
($disk);
71 my $volid = $disk->{file
};
73 if ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
74 my ($storeid, $size) = ($2 || $default_storage, $3);
75 die "no storage ID specified (and no default storage)\n" if !$storeid;
76 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storeid, [ 'Datastore.AllocateSpace' ]);
77 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
78 my $fmt = $disk->{format
} || $defformat;
79 push @$alloc, [$ds, $disk, $storeid, $size, $fmt];
81 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool);
82 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
86 # now try to allocate everything
90 foreach my $task (@$alloc) {
91 my ($ds, $disk, $storeid, $size, $fmt) = @$task;
93 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
94 $fmt, undef, $size*1024*1024);
96 $disk->{file
} = $volid;
97 push @$vollist, $volid;
101 # free allocated images on error
103 syslog
('err', "VM $vmid creating disks failed");
104 foreach my $volid (@$vollist) {
105 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
111 # modify vm config if everything went well
112 foreach my $task (@$alloc) {
113 my ($ds, $disk) = @$task;
114 delete $disk->{format
}; # no longer needed
115 $conf->{$ds} = $settings->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
121 my $check_vm_modify_config_perm = sub {
122 my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
124 return 1 if $authuser ne 'root@pam';
126 foreach my $opt (keys %$param) {
127 # disk checks need to be done somewhere else
128 next if PVE
::QemuServer
::valid_drivename
($opt);
130 if ($opt eq 'sockets' || $opt eq 'cores' ||
131 $opt eq 'cpu' || $opt eq 'smp' ||
132 $opt eq 'cpuimit' || $opt eq 'cpuunits') {
133 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
134 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
135 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
136 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
137 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
138 } elsif ($opt eq 'args' || $opt eq 'lock') {
139 die "only root can set '$opt' config\n";
140 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
141 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
142 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
143 } elsif ($opt =~ m/^net\d+$/) {
144 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
146 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
153 __PACKAGE__-
>register_method({
157 description
=> "Virtual machine index (per node).",
159 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
163 protected
=> 1, # qemu pid files are only readable by root
165 additionalProperties
=> 0,
167 node
=> get_standard_option
('pve-node'),
176 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
181 my $rpcenv = PVE
::RPCEnvironment
::get
();
182 my $authuser = $rpcenv->get_user();
184 my $vmstatus = PVE
::QemuServer
::vmstatus
();
187 foreach my $vmid (keys %$vmstatus) {
188 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
190 my $data = $vmstatus->{$vmid};
191 $data->{vmid
} = $vmid;
198 __PACKAGE__-
>register_method({
202 description
=> "Create or restore a virtual machine.",
204 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.",
206 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
207 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
213 additionalProperties
=> 0,
214 properties
=> PVE
::QemuServer
::json_config_properties
(
216 node
=> get_standard_option
('pve-node'),
217 vmid
=> get_standard_option
('pve-vmid'),
219 description
=> "The backup file.",
224 storage
=> get_standard_option
('pve-storage-id', {
225 description
=> "Default storage.",
231 description
=> "Allow to overwrite existing VM.",
232 requires
=> 'archive',
237 description
=> "Assign a unique random ethernet address.",
238 requires
=> 'archive',
242 type
=> 'string', format
=> 'pve-poolid',
243 description
=> "Add the VM to the specified pool.",
253 my $rpcenv = PVE
::RPCEnvironment
::get
();
255 my $authuser = $rpcenv->get_user();
257 my $node = extract_param
($param, 'node');
259 my $vmid = extract_param
($param, 'vmid');
261 my $archive = extract_param
($param, 'archive');
263 my $storage = extract_param
($param, 'storage');
265 my $force = extract_param
($param, 'force');
267 my $unique = extract_param
($param, 'unique');
269 my $pool = extract_param
($param, 'pool');
271 my $filename = PVE
::QemuServer
::config_file
($vmid);
273 my $storecfg = PVE
::Storage
::config
();
275 PVE
::Cluster
::check_cfs_quorum
();
277 if (defined($pool)) {
278 $rpcenv->check_pool_exist($pool);
279 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
282 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storage, [ 'Datastore.AllocateSpace' ])
283 if defined($storage);
286 &$resolve_cdrom_alias($param);
288 foreach my $opt (keys %$param) {
289 if (PVE
::QemuServer
::valid_drivename
($opt)) {
290 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
291 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
293 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
294 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
298 PVE
::QemuServer
::add_random_macs
($param);
300 my $keystr = join(' ', keys %$param);
301 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
303 if ($archive eq '-') {
304 die "pipe requires cli environment\n"
305 && $rpcenv->{type
} ne 'cli';
307 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $archive, $pool);
308 die "can't find archive file '$archive'\n" if !($path && -f
$path);
313 my $restorefn = sub {
316 die "unable to restore vm $vmid: config file already exists\n"
319 die "unable to restore vm $vmid: vm is running\n"
320 if PVE
::QemuServer
::check_running
($vmid);
322 # destroy existing data - keep empty config
323 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
327 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
330 unique
=> $unique });
333 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
336 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param);
340 # second test (after locking test is accurate)
341 die "unable to create vm $vmid: config file already exists\n"
351 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
353 # try to be smart about bootdisk
354 my @disks = PVE
::QemuServer
::disknames
();
356 foreach my $ds (reverse @disks) {
357 next if !$conf->{$ds};
358 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
359 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
363 if (!$conf->{bootdisk
} && $firstdisk) {
364 $conf->{bootdisk
} = $firstdisk;
367 PVE
::QemuServer
::update_conf_nolock
($vmid, $conf);
372 foreach my $volid (@$vollist) {
373 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
376 die "create failed - $err";
380 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
383 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
386 __PACKAGE__-
>register_method({
391 description
=> "Directory index",
396 additionalProperties
=> 0,
398 node
=> get_standard_option
('pve-node'),
399 vmid
=> get_standard_option
('pve-vmid'),
407 subdir
=> { type
=> 'string' },
410 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
416 { subdir
=> 'config' },
417 { subdir
=> 'status' },
418 { subdir
=> 'unlink' },
419 { subdir
=> 'vncproxy' },
420 { subdir
=> 'migrate' },
422 { subdir
=> 'rrddata' },
423 { subdir
=> 'monitor' },
429 __PACKAGE__-
>register_method({
431 path
=> '{vmid}/rrd',
433 protected
=> 1, # fixme: can we avoid that?
435 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
437 description
=> "Read VM RRD statistics (returns PNG)",
439 additionalProperties
=> 0,
441 node
=> get_standard_option
('pve-node'),
442 vmid
=> get_standard_option
('pve-vmid'),
444 description
=> "Specify the time frame you are interested in.",
446 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
449 description
=> "The list of datasources you want to display.",
450 type
=> 'string', format
=> 'pve-configid-list',
453 description
=> "The RRD consolidation function",
455 enum
=> [ 'AVERAGE', 'MAX' ],
463 filename
=> { type
=> 'string' },
469 return PVE
::Cluster
::create_rrd_graph
(
470 "pve2-vm/$param->{vmid}", $param->{timeframe
},
471 $param->{ds
}, $param->{cf
});
475 __PACKAGE__-
>register_method({
477 path
=> '{vmid}/rrddata',
479 protected
=> 1, # fixme: can we avoid that?
481 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
483 description
=> "Read VM RRD statistics",
485 additionalProperties
=> 0,
487 node
=> get_standard_option
('pve-node'),
488 vmid
=> get_standard_option
('pve-vmid'),
490 description
=> "Specify the time frame you are interested in.",
492 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
495 description
=> "The RRD consolidation function",
497 enum
=> [ 'AVERAGE', 'MAX' ],
512 return PVE
::Cluster
::create_rrd_data
(
513 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
517 __PACKAGE__-
>register_method({
519 path
=> '{vmid}/config',
522 description
=> "Get virtual machine configuration.",
524 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
527 additionalProperties
=> 0,
529 node
=> get_standard_option
('pve-node'),
530 vmid
=> get_standard_option
('pve-vmid'),
538 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
545 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
550 my $delete_drive = sub {
551 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
553 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
554 my $volid = $drive->{file
};
556 if ($volid !~ m
|^/|) {
558 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
559 if ($owner && ($owner == $vmid)) {
561 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
564 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
566 delete $conf->{$key};
574 my $vm_config_perm_list = [
584 __PACKAGE__-
>register_method({
586 path
=> '{vmid}/config',
590 description
=> "Set virtual machine options.",
592 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
595 additionalProperties
=> 0,
596 properties
=> PVE
::QemuServer
::json_config_properties
(
598 node
=> get_standard_option
('pve-node'),
599 vmid
=> get_standard_option
('pve-vmid'),
600 skiplock
=> get_standard_option
('skiplock'),
602 type
=> 'string', format
=> 'pve-configid-list',
603 description
=> "A list of settings you want to delete.",
608 description
=> $opt_force_description,
610 requires
=> 'delete',
614 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
620 returns
=> { type
=> 'null'},
624 my $rpcenv = PVE
::RPCEnvironment
::get
();
626 my $authuser = $rpcenv->get_user();
628 my $node = extract_param
($param, 'node');
630 my $vmid = extract_param
($param, 'vmid');
632 my $digest = extract_param
($param, 'digest');
634 my @paramarr = (); # used for log message
635 foreach my $key (keys %$param) {
636 push @paramarr, "-$key", $param->{$key};
639 my $skiplock = extract_param
($param, 'skiplock');
640 raise_param_exc
({ skiplock
=> "Only root may use this option." })
641 if $skiplock && $authuser ne 'root@pam';
643 my $delete_str = extract_param
($param, 'delete');
645 my $force = extract_param
($param, 'force');
647 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
649 my $storecfg = PVE
::Storage
::config
();
651 &$resolve_cdrom_alias($param);
653 # now try to verify all parameters
656 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
657 $opt = 'ide2' if $opt eq 'cdrom';
658 raise_param_exc
({ delete => "you can't use '-$opt' and " .
659 "-delete $opt' at the same time" })
660 if defined($param->{$opt});
662 if (!PVE
::QemuServer
::option_exists
($opt)) {
663 raise_param_exc
({ delete => "unknown option '$opt'" });
670 foreach my $opt (keys %$param) {
671 if (PVE
::QemuServer
::valid_drivename
($opt)) {
673 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
674 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
675 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
676 $drive_hash->{$opt} = $drive;
677 } elsif ($opt =~ m/^net(\d+)$/) {
679 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
680 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
681 $net_hash->{$opt} = $net;
688 my $conf = PVE
::QemuServer
::load_config
($vmid);
690 die "checksum missmatch (file change by other user?)\n"
691 if $digest && $digest ne $conf->{digest
};
693 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
695 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
697 foreach my $opt (@delete) { # delete
699 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
701 next if !defined($conf->{$opt});
703 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
705 if (PVE
::QemuServer
::valid_drivename
($opt)) {
706 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
707 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
708 } elsif ($opt =~ m/^unused/) {
709 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
710 my $volid = $drive->{file
};
711 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
713 delete $conf->{$opt};
715 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
718 foreach my $opt (keys %$param) { # add/change
720 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
722 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
724 if (my $drive = $drive_hash->{$opt}) { # drives
726 my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt}) if $conf->{$opt};
728 if ($old_drive && ($drive->{file
} ne $old_drive->{file
}) &&
729 !PVE
::QemuServer
::drive_is_cdrom
($old_drive)) { # delete old disks
731 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
733 &$delete_drive($conf, $storecfg, $vmid, $opt, $old_drive, 0);
734 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
735 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
738 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
740 if (PVE
::QemuServer
::check_running
($vmid)) {
741 if ($drive->{file
} eq 'none') {
742 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
744 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
745 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
746 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
750 $conf->{$opt} = $param->{$opt};
751 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
755 my $settings = { $opt => $param->{$opt} };
756 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, $settings);
757 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
758 $conf = PVE::QemuServer::load_config($vmid); # update/reload
760 my $newdrive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
763 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newdrive);
766 } elsif (my $net = $net_hash->{$opt}) { #nics
768 #if online update, then unplug first
769 die "error hot-unplug
$opt for update
" if $conf->{$opt} &&
770 !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
772 $conf->{$opt} = $param->{$opt};
773 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
774 $conf = PVE::QemuServer::load_config($vmid); # update/reload
776 my $newnet = PVE::QemuServer::parse_net($conf->{$opt});
778 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
782 $conf->{$opt} = $param->{$opt};
783 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
788 PVE::QemuServer::lock_config($vmid, $updatefn);
794 __PACKAGE__->register_method({
795 name => 'destroy_vm',
800 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
802 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
805 additionalProperties => 0,
807 node => get_standard_option('pve-node'),
808 vmid => get_standard_option('pve-vmid'),
809 skiplock => get_standard_option('skiplock'),
818 my $rpcenv = PVE::RPCEnvironment::get();
820 my $authuser = $rpcenv->get_user();
822 my $vmid = $param->{vmid};
824 my $skiplock = $param->{skiplock};
825 raise_param_exc({ skiplock => "Only root may
use this option
." })
826 if $skiplock && $authuser ne 'root@pam';
829 my $conf = PVE::QemuServer::load_config($vmid);
831 my $storecfg = PVE::Storage::config();
836 syslog('info', "destroy VM
$vmid: $upid\n");
838 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
841 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
844 __PACKAGE__->register_method({
846 path => '{vmid}/unlink',
850 description => "Unlink
/delete disk images
.",
852 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
855 additionalProperties => 0,
857 node => get_standard_option('pve-node'),
858 vmid => get_standard_option('pve-vmid'),
860 type => 'string', format => 'pve-configid-list',
861 description => "A list of disk IDs you want to
delete.",
865 description => $opt_force_description,
870 returns => { type => 'null'},
874 $param->{delete} = extract_param($param, 'idlist');
876 __PACKAGE__->update_vm($param);
883 __PACKAGE__->register_method({
885 path => '{vmid}/vncproxy',
889 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
891 description => "Creates a TCP VNC proxy connections
.",
893 additionalProperties => 0,
895 node => get_standard_option('pve-node'),
896 vmid => get_standard_option('pve-vmid'),
900 additionalProperties => 0,
902 user => { type => 'string' },
903 ticket => { type => 'string' },
904 cert => { type => 'string' },
905 port => { type => 'integer' },
906 upid => { type => 'string' },
912 my $rpcenv = PVE::RPCEnvironment::get();
914 my $authuser = $rpcenv->get_user();
916 my $vmid = $param->{vmid};
917 my $node = $param->{node};
919 my $authpath = "/vms/$vmid";
921 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
923 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
926 my $port = PVE::Tools::next_vnc_port();
930 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
931 $remip = PVE::Cluster::remote_node_ip($node);
934 # NOTE: kvm VNC traffic is already TLS encrypted,
935 # so we select the fastest chipher here (or 'none'?)
936 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
937 '-c', 'blowfish-cbc', $remip] : [];
944 syslog('info', "starting vnc proxy
$upid\n");
946 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
948 my $qmstr = join(' ', @$qmcmd);
950 # also redirect stderr (else we get RFB protocol errors)
951 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
953 PVE::Tools::run_command($cmd);
958 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
969 __PACKAGE__->register_method({
971 path => '{vmid}/status',
974 description => "Directory
index",
979 additionalProperties => 0,
981 node => get_standard_option('pve-node'),
982 vmid => get_standard_option('pve-vmid'),
990 subdir => { type => 'string' },
993 links => [ { rel => 'child', href => "{subdir
}" } ],
999 my $conf = PVE::QemuServer::load_config($param->{vmid});
1002 { subdir => 'current' },
1003 { subdir => 'start' },
1004 { subdir => 'stop' },
1010 __PACKAGE__->register_method({
1011 name => 'vm_status',
1012 path => '{vmid}/status/current',
1015 protected => 1, # qemu pid files are only readable by root
1016 description => "Get virtual machine status
.",
1018 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1021 additionalProperties => 0,
1023 node => get_standard_option('pve-node'),
1024 vmid => get_standard_option('pve-vmid'),
1027 returns => { type => 'object' },
1032 my $conf = PVE::QemuServer::load_config($param->{vmid});
1034 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1035 my $status = $vmstatus->{$param->{vmid}};
1037 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1038 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1047 __PACKAGE__->register_method({
1049 path => '{vmid}/status/start',
1053 description => "Start virtual machine
.",
1055 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1058 additionalProperties => 0,
1060 node => get_standard_option('pve-node'),
1061 vmid => get_standard_option('pve-vmid'),
1062 skiplock => get_standard_option('skiplock'),
1063 stateuri => get_standard_option('pve-qm-stateuri'),
1072 my $rpcenv = PVE::RPCEnvironment::get();
1074 my $authuser = $rpcenv->get_user();
1076 my $node = extract_param($param, 'node');
1078 my $vmid = extract_param($param, 'vmid');
1080 my $stateuri = extract_param($param, 'stateuri');
1081 raise_param_exc({ stateuri => "Only root may
use this option
." })
1082 if $stateuri && $authuser ne 'root@pam';
1084 my $skiplock = extract_param($param, 'skiplock');
1085 raise_param_exc({ skiplock => "Only root may
use this option
." })
1086 if $skiplock && $authuser ne 'root@pam';
1088 my $storecfg = PVE::Storage::config();
1093 syslog('info', "start VM
$vmid: $upid\n");
1095 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1100 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1103 __PACKAGE__->register_method({
1105 path => '{vmid}/status/stop',
1109 description => "Stop virtual machine
.",
1111 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1114 additionalProperties => 0,
1116 node => get_standard_option('pve-node'),
1117 vmid => get_standard_option('pve-vmid'),
1118 skiplock => get_standard_option('skiplock'),
1120 description => "Wait maximal timeout seconds
.",
1126 description => "Do
not decativate storage volumes
.",
1139 my $rpcenv = PVE::RPCEnvironment::get();
1141 my $authuser = $rpcenv->get_user();
1143 my $node = extract_param($param, 'node');
1145 my $vmid = extract_param($param, 'vmid');
1147 my $skiplock = extract_param($param, 'skiplock');
1148 raise_param_exc({ skiplock => "Only root may
use this option
." })
1149 if $skiplock && $authuser ne 'root@pam';
1151 my $keepActive = extract_param($param, 'keepActive');
1152 raise_param_exc({ keepActive => "Only root may
use this option
." })
1153 if $keepActive && $authuser ne 'root@pam';
1155 my $storecfg = PVE::Storage::config();
1160 syslog('info', "stop VM
$vmid: $upid\n");
1162 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1163 $param->{timeout}, 0, 1, $keepActive);
1168 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1171 __PACKAGE__->register_method({
1173 path => '{vmid}/status/reset',
1177 description => "Reset virtual machine
.",
1179 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1182 additionalProperties => 0,
1184 node => get_standard_option('pve-node'),
1185 vmid => get_standard_option('pve-vmid'),
1186 skiplock => get_standard_option('skiplock'),
1195 my $rpcenv = PVE::RPCEnvironment::get();
1197 my $authuser = $rpcenv->get_user();
1199 my $node = extract_param($param, 'node');
1201 my $vmid = extract_param($param, 'vmid');
1203 my $skiplock = extract_param($param, 'skiplock');
1204 raise_param_exc({ skiplock => "Only root may
use this option
." })
1205 if $skiplock && $authuser ne 'root@pam';
1207 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1212 PVE::QemuServer::vm_reset($vmid, $skiplock);
1217 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1220 __PACKAGE__->register_method({
1221 name => 'vm_shutdown',
1222 path => '{vmid}/status/shutdown',
1226 description => "Shutdown virtual machine
.",
1228 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1231 additionalProperties => 0,
1233 node => get_standard_option('pve-node'),
1234 vmid => get_standard_option('pve-vmid'),
1235 skiplock => get_standard_option('skiplock'),
1237 description => "Wait maximal timeout seconds
.",
1243 description => "Make sure the VM stops
.",
1249 description => "Do
not decativate storage volumes
.",
1262 my $rpcenv = PVE::RPCEnvironment::get();
1264 my $authuser = $rpcenv->get_user();
1266 my $node = extract_param($param, 'node');
1268 my $vmid = extract_param($param, 'vmid');
1270 my $skiplock = extract_param($param, 'skiplock');
1271 raise_param_exc({ skiplock => "Only root may
use this option
." })
1272 if $skiplock && $authuser ne 'root@pam';
1274 my $keepActive = extract_param($param, 'keepActive');
1275 raise_param_exc({ keepActive => "Only root may
use this option
." })
1276 if $keepActive && $authuser ne 'root@pam';
1278 my $storecfg = PVE::Storage::config();
1283 syslog('info', "shutdown VM
$vmid: $upid\n");
1285 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1286 1, $param->{forceStop}, $keepActive);
1291 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1294 __PACKAGE__->register_method({
1295 name => 'vm_suspend',
1296 path => '{vmid}/status/suspend',
1300 description => "Suspend virtual machine
.",
1302 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1305 additionalProperties => 0,
1307 node => get_standard_option('pve-node'),
1308 vmid => get_standard_option('pve-vmid'),
1309 skiplock => get_standard_option('skiplock'),
1318 my $rpcenv = PVE::RPCEnvironment::get();
1320 my $authuser = $rpcenv->get_user();
1322 my $node = extract_param($param, 'node');
1324 my $vmid = extract_param($param, 'vmid');
1326 my $skiplock = extract_param($param, 'skiplock');
1327 raise_param_exc({ skiplock => "Only root may
use this option
." })
1328 if $skiplock && $authuser ne 'root@pam';
1330 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1335 syslog('info', "suspend VM
$vmid: $upid\n");
1337 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1342 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1345 __PACKAGE__->register_method({
1346 name => 'vm_resume',
1347 path => '{vmid}/status/resume',
1351 description => "Resume virtual machine
.",
1353 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1356 additionalProperties => 0,
1358 node => get_standard_option('pve-node'),
1359 vmid => get_standard_option('pve-vmid'),
1360 skiplock => get_standard_option('skiplock'),
1369 my $rpcenv = PVE::RPCEnvironment::get();
1371 my $authuser = $rpcenv->get_user();
1373 my $node = extract_param($param, 'node');
1375 my $vmid = extract_param($param, 'vmid');
1377 my $skiplock = extract_param($param, 'skiplock');
1378 raise_param_exc({ skiplock => "Only root may
use this option
." })
1379 if $skiplock && $authuser ne 'root@pam';
1381 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1386 syslog('info', "resume VM
$vmid: $upid\n");
1388 PVE::QemuServer::vm_resume($vmid, $skiplock);
1393 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1396 __PACKAGE__->register_method({
1397 name => 'vm_sendkey',
1398 path => '{vmid}/sendkey',
1402 description => "Send key event to virtual machine
.",
1404 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1407 additionalProperties => 0,
1409 node => get_standard_option('pve-node'),
1410 vmid => get_standard_option('pve-vmid'),
1411 skiplock => get_standard_option('skiplock'),
1413 description => "The key
(qemu monitor encoding
).",
1418 returns => { type => 'null'},
1422 my $rpcenv = PVE::RPCEnvironment::get();
1424 my $authuser = $rpcenv->get_user();
1426 my $node = extract_param($param, 'node');
1428 my $vmid = extract_param($param, 'vmid');
1430 my $skiplock = extract_param($param, 'skiplock');
1431 raise_param_exc({ skiplock => "Only root may
use this option
." })
1432 if $skiplock && $authuser ne 'root@pam';
1434 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1439 __PACKAGE__->register_method({
1440 name => 'migrate_vm',
1441 path => '{vmid}/migrate',
1445 description => "Migrate virtual machine
. Creates a new migration task
.",
1447 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1450 additionalProperties => 0,
1452 node => get_standard_option('pve-node'),
1453 vmid => get_standard_option('pve-vmid'),
1454 target => get_standard_option('pve-node', { description => "Target node
." }),
1457 description => "Use online
/live migration
.",
1462 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1469 description => "the task ID
.",
1474 my $rpcenv = PVE::RPCEnvironment::get();
1476 my $authuser = $rpcenv->get_user();
1478 my $target = extract_param($param, 'target');
1480 my $localnode = PVE::INotify::nodename();
1481 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1483 PVE::Cluster::check_cfs_quorum();
1485 PVE::Cluster::check_node_exists($target);
1487 my $targetip = PVE::Cluster::remote_node_ip($target);
1489 my $vmid = extract_param($param, 'vmid');
1491 raise_param_exc({ force => "Only root may
use this option
." })
1492 if $param->{force} && $authuser ne 'root@pam';
1495 my $conf = PVE::QemuServer::load_config($vmid);
1497 # try to detect errors early
1499 PVE::QemuServer::check_lock($conf);
1501 if (PVE::QemuServer::check_running($vmid)) {
1502 die "cant migrate running VM without
--online
\n"
1503 if !$param->{online};
1509 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1512 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1517 __PACKAGE__->register_method({
1519 path => '{vmid}/monitor',
1523 description => "Execute Qemu monitor commands
.",
1525 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1528 additionalProperties => 0,
1530 node => get_standard_option('pve-node'),
1531 vmid => get_standard_option('pve-vmid'),
1534 description => "The monitor command
.",
1538 returns => { type => 'string'},
1542 my $vmid = $param->{vmid};
1544 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1548 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1550 $res = "ERROR
: $@" if $@;