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, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
63 # check permissions first
66 foreach_drive
($settings, sub {
69 return if 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 $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"
349 $vollist = &$create_disks($rpcenv, $authuser, $storecfg, $vmid, $pool, $param, $storage);
351 # try to be smart about bootdisk
352 my @disks = PVE
::QemuServer
::disknames
();
354 foreach my $ds (reverse @disks) {
355 next if !$param->{$ds};
356 my $disk = PVE
::QemuServer
::parse_drive
($ds, $param->{$ds});
357 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
361 if (!$param->{bootdisk
} && $firstdisk) {
362 $param->{bootdisk
} = $firstdisk;
365 PVE
::QemuServer
::create_conf_nolock
($vmid, $param);
370 foreach my $volid (@$vollist) {
371 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
374 die "create failed - $err";
378 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
381 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
384 __PACKAGE__-
>register_method({
389 description
=> "Directory index",
394 additionalProperties
=> 0,
396 node
=> get_standard_option
('pve-node'),
397 vmid
=> get_standard_option
('pve-vmid'),
405 subdir
=> { type
=> 'string' },
408 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
414 { subdir
=> 'config' },
415 { subdir
=> 'status' },
416 { subdir
=> 'unlink' },
417 { subdir
=> 'vncproxy' },
418 { subdir
=> 'migrate' },
420 { subdir
=> 'rrddata' },
421 { subdir
=> 'monitor' },
427 __PACKAGE__-
>register_method({
429 path
=> '{vmid}/rrd',
431 protected
=> 1, # fixme: can we avoid that?
433 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
435 description
=> "Read VM RRD statistics (returns PNG)",
437 additionalProperties
=> 0,
439 node
=> get_standard_option
('pve-node'),
440 vmid
=> get_standard_option
('pve-vmid'),
442 description
=> "Specify the time frame you are interested in.",
444 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
447 description
=> "The list of datasources you want to display.",
448 type
=> 'string', format
=> 'pve-configid-list',
451 description
=> "The RRD consolidation function",
453 enum
=> [ 'AVERAGE', 'MAX' ],
461 filename
=> { type
=> 'string' },
467 return PVE
::Cluster
::create_rrd_graph
(
468 "pve2-vm/$param->{vmid}", $param->{timeframe
},
469 $param->{ds
}, $param->{cf
});
473 __PACKAGE__-
>register_method({
475 path
=> '{vmid}/rrddata',
477 protected
=> 1, # fixme: can we avoid that?
479 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
481 description
=> "Read VM RRD statistics",
483 additionalProperties
=> 0,
485 node
=> get_standard_option
('pve-node'),
486 vmid
=> get_standard_option
('pve-vmid'),
488 description
=> "Specify the time frame you are interested in.",
490 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
493 description
=> "The RRD consolidation function",
495 enum
=> [ 'AVERAGE', 'MAX' ],
510 return PVE
::Cluster
::create_rrd_data
(
511 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
515 __PACKAGE__-
>register_method({
517 path
=> '{vmid}/config',
520 description
=> "Get virtual machine configuration.",
522 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
525 additionalProperties
=> 0,
527 node
=> get_standard_option
('pve-node'),
528 vmid
=> get_standard_option
('pve-vmid'),
536 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
543 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
548 my $delete_drive = sub {
549 my ($conf, $storecfg, $vmid, $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, $changes);
575 my $vm_config_perm_list = [
585 __PACKAGE__-
>register_method({
587 path
=> '{vmid}/config',
591 description
=> "Set virtual machine options.",
593 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
596 additionalProperties
=> 0,
597 properties
=> PVE
::QemuServer
::json_config_properties
(
599 node
=> get_standard_option
('pve-node'),
600 vmid
=> get_standard_option
('pve-vmid'),
601 skiplock
=> get_standard_option
('skiplock'),
603 type
=> 'string', format
=> 'pve-configid-list',
604 description
=> "A list of settings you want to delete.",
609 description
=> $opt_force_description,
611 requires
=> 'delete',
615 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
621 returns
=> { type
=> 'null'},
625 my $rpcenv = PVE
::RPCEnvironment
::get
();
627 my $authuser = $rpcenv->get_user();
629 my $node = extract_param
($param, 'node');
631 my $vmid = extract_param
($param, 'vmid');
633 my $digest = extract_param
($param, 'digest');
635 my @paramarr = (); # used for log message
636 foreach my $key (keys %$param) {
637 push @paramarr, "-$key", $param->{$key};
640 my $skiplock = extract_param
($param, 'skiplock');
641 raise_param_exc
({ skiplock
=> "Only root may use this option." })
642 if $skiplock && $authuser ne 'root@pam';
644 my $delete_str = extract_param
($param, 'delete');
646 my $force = extract_param
($param, 'force');
648 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
650 my $storecfg = PVE
::Storage
::config
();
652 &$resolve_cdrom_alias($param);
654 # now try to verify all parameters
657 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
658 $opt = 'ide2' if $opt eq 'cdrom';
659 raise_param_exc
({ delete => "you can't use '-$opt' and " .
660 "-delete $opt' at the same time" })
661 if defined($param->{$opt});
663 if (!PVE
::QemuServer
::option_exists
($opt)) {
664 raise_param_exc
({ delete => "unknown option '$opt'" });
671 foreach my $opt (keys %$param) {
672 if (PVE
::QemuServer
::valid_drivename
($opt)) {
674 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
675 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
676 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
677 $drive_hash->{$opt} = $drive;
678 } elsif ($opt =~ m/^net(\d+)$/) {
680 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
681 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
682 $net_hash->{$opt} = $net;
689 my $conf = PVE
::QemuServer
::load_config
($vmid);
691 die "checksum missmatch (file change by other user?)\n"
692 if $digest && $digest ne $conf->{digest
};
694 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
696 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
698 foreach my $opt (@delete) { # delete
700 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
702 next if !defined($conf->{$opt});
704 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
708 if (my $drive = $drive_hash->{$opt}) { # drives
709 $changes = &$delete_drive($conf, $storecfg, $vmid, $drive, $force);
710 } elsif ($opt =~ m/^unused/) {
711 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
712 my $volid = $drive->{file
};
713 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
717 PVE
::QemuServer
::change_config_nolock
($vmid, $changes, { $opt => 1 }, 1);
720 foreach my $opt (keys %$param) { # add/change
722 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
724 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
726 if (my $drive = $drive_hash->{$opt}) { # drives
728 my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt}) if $conf->{$opt};
730 if ($old_drive && ($drive->{file
} ne $old_drive->{file
}) &&
731 !PVE
::QemuServer
::drive_is_cdrom
($old_drive)) { # delete old disks
733 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
735 my $changes = &$delete_drive($conf, $storecfg, $vmid, $old_drive, 0);
736 PVE
::QemuServer
::change_config_nolock
($vmid, $changes, { $opt => 1 }, 1);
737 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
740 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
742 if (PVE
::QemuServer
::check_running
($vmid)) {
743 if ($drive->{file
} eq 'none') {
744 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
746 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
747 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
748 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
752 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
756 my $settings = { $opt => $param->{$opt} };
757 &$create_disks($rpcenv, $authuser, $storecfg, $vmid, undef, $settings);
758 PVE::QemuServer::change_config_nolock($vmid, { $opt => $settings->{$opt} }, {}, 1);
759 $conf = PVE::QemuServer::load_config($vmid); # update/reload
761 my $newdrive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
764 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newdrive);
767 } elsif (my $net = $net_hash->{$opt}) { #nics
769 #if online update, then unplug first
770 die "error hot-unplug
$opt for update
" if $conf->{$opt} &&
771 !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
773 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
774 $conf = PVE::QemuServer::load_config($vmid); # update/reload
776 my $newnet = PVE::QemuServer::parse_net($conf->{$opt});
777 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
781 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
786 PVE::QemuServer::lock_config($vmid, $updatefn);
792 __PACKAGE__->register_method({
793 name => 'destroy_vm',
798 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
800 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
803 additionalProperties => 0,
805 node => get_standard_option('pve-node'),
806 vmid => get_standard_option('pve-vmid'),
807 skiplock => get_standard_option('skiplock'),
816 my $rpcenv = PVE::RPCEnvironment::get();
818 my $authuser = $rpcenv->get_user();
820 my $vmid = $param->{vmid};
822 my $skiplock = $param->{skiplock};
823 raise_param_exc({ skiplock => "Only root may
use this option
." })
824 if $skiplock && $authuser ne 'root@pam';
827 my $conf = PVE::QemuServer::load_config($vmid);
829 my $storecfg = PVE::Storage::config();
834 syslog('info', "destroy VM
$vmid: $upid\n");
836 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
839 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
842 __PACKAGE__->register_method({
844 path => '{vmid}/unlink',
848 description => "Unlink
/delete disk images
.",
850 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
853 additionalProperties => 0,
855 node => get_standard_option('pve-node'),
856 vmid => get_standard_option('pve-vmid'),
858 type => 'string', format => 'pve-configid-list',
859 description => "A list of disk IDs you want to
delete.",
863 description => $opt_force_description,
868 returns => { type => 'null'},
872 $param->{delete} = extract_param($param, 'idlist');
874 __PACKAGE__->update_vm($param);
881 __PACKAGE__->register_method({
883 path => '{vmid}/vncproxy',
887 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
889 description => "Creates a TCP VNC proxy connections
.",
891 additionalProperties => 0,
893 node => get_standard_option('pve-node'),
894 vmid => get_standard_option('pve-vmid'),
898 additionalProperties => 0,
900 user => { type => 'string' },
901 ticket => { type => 'string' },
902 cert => { type => 'string' },
903 port => { type => 'integer' },
904 upid => { type => 'string' },
910 my $rpcenv = PVE::RPCEnvironment::get();
912 my $authuser = $rpcenv->get_user();
914 my $vmid = $param->{vmid};
915 my $node = $param->{node};
917 my $authpath = "/vms/$vmid";
919 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
921 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
924 my $port = PVE::Tools::next_vnc_port();
928 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
929 $remip = PVE::Cluster::remote_node_ip($node);
932 # NOTE: kvm VNC traffic is already TLS encrypted,
933 # so we select the fastest chipher here (or 'none'?)
934 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
935 '-c', 'blowfish-cbc', $remip] : [];
942 syslog('info', "starting vnc proxy
$upid\n");
944 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
946 my $qmstr = join(' ', @$qmcmd);
948 # also redirect stderr (else we get RFB protocol errors)
949 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
951 PVE::Tools::run_command($cmd);
956 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
967 __PACKAGE__->register_method({
969 path => '{vmid}/status',
972 description => "Directory
index",
977 additionalProperties => 0,
979 node => get_standard_option('pve-node'),
980 vmid => get_standard_option('pve-vmid'),
988 subdir => { type => 'string' },
991 links => [ { rel => 'child', href => "{subdir
}" } ],
997 my $conf = PVE::QemuServer::load_config($param->{vmid});
1000 { subdir => 'current' },
1001 { subdir => 'start' },
1002 { subdir => 'stop' },
1008 __PACKAGE__->register_method({
1009 name => 'vm_status',
1010 path => '{vmid}/status/current',
1013 protected => 1, # qemu pid files are only readable by root
1014 description => "Get virtual machine status
.",
1016 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1019 additionalProperties => 0,
1021 node => get_standard_option('pve-node'),
1022 vmid => get_standard_option('pve-vmid'),
1025 returns => { type => 'object' },
1030 my $conf = PVE::QemuServer::load_config($param->{vmid});
1032 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1033 my $status = $vmstatus->{$param->{vmid}};
1035 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1036 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1045 __PACKAGE__->register_method({
1047 path => '{vmid}/status/start',
1051 description => "Start virtual machine
.",
1053 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1056 additionalProperties => 0,
1058 node => get_standard_option('pve-node'),
1059 vmid => get_standard_option('pve-vmid'),
1060 skiplock => get_standard_option('skiplock'),
1061 stateuri => get_standard_option('pve-qm-stateuri'),
1070 my $rpcenv = PVE::RPCEnvironment::get();
1072 my $authuser = $rpcenv->get_user();
1074 my $node = extract_param($param, 'node');
1076 my $vmid = extract_param($param, 'vmid');
1078 my $stateuri = extract_param($param, 'stateuri');
1079 raise_param_exc({ stateuri => "Only root may
use this option
." })
1080 if $stateuri && $authuser ne 'root@pam';
1082 my $skiplock = extract_param($param, 'skiplock');
1083 raise_param_exc({ skiplock => "Only root may
use this option
." })
1084 if $skiplock && $authuser ne 'root@pam';
1086 my $storecfg = PVE::Storage::config();
1091 syslog('info', "start VM
$vmid: $upid\n");
1093 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1098 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1101 __PACKAGE__->register_method({
1103 path => '{vmid}/status/stop',
1107 description => "Stop virtual machine
.",
1109 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1112 additionalProperties => 0,
1114 node => get_standard_option('pve-node'),
1115 vmid => get_standard_option('pve-vmid'),
1116 skiplock => get_standard_option('skiplock'),
1118 description => "Wait maximal timeout seconds
.",
1124 description => "Do
not decativate storage volumes
.",
1137 my $rpcenv = PVE::RPCEnvironment::get();
1139 my $authuser = $rpcenv->get_user();
1141 my $node = extract_param($param, 'node');
1143 my $vmid = extract_param($param, 'vmid');
1145 my $skiplock = extract_param($param, 'skiplock');
1146 raise_param_exc({ skiplock => "Only root may
use this option
." })
1147 if $skiplock && $authuser ne 'root@pam';
1149 my $keepActive = extract_param($param, 'keepActive');
1150 raise_param_exc({ keepActive => "Only root may
use this option
." })
1151 if $keepActive && $authuser ne 'root@pam';
1153 my $storecfg = PVE::Storage::config();
1158 syslog('info', "stop VM
$vmid: $upid\n");
1160 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1161 $param->{timeout}, 0, 1, $keepActive);
1166 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1169 __PACKAGE__->register_method({
1171 path => '{vmid}/status/reset',
1175 description => "Reset virtual machine
.",
1177 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1180 additionalProperties => 0,
1182 node => get_standard_option('pve-node'),
1183 vmid => get_standard_option('pve-vmid'),
1184 skiplock => get_standard_option('skiplock'),
1193 my $rpcenv = PVE::RPCEnvironment::get();
1195 my $authuser = $rpcenv->get_user();
1197 my $node = extract_param($param, 'node');
1199 my $vmid = extract_param($param, 'vmid');
1201 my $skiplock = extract_param($param, 'skiplock');
1202 raise_param_exc({ skiplock => "Only root may
use this option
." })
1203 if $skiplock && $authuser ne 'root@pam';
1205 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1210 PVE::QemuServer::vm_reset($vmid, $skiplock);
1215 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1218 __PACKAGE__->register_method({
1219 name => 'vm_shutdown',
1220 path => '{vmid}/status/shutdown',
1224 description => "Shutdown virtual machine
.",
1226 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1229 additionalProperties => 0,
1231 node => get_standard_option('pve-node'),
1232 vmid => get_standard_option('pve-vmid'),
1233 skiplock => get_standard_option('skiplock'),
1235 description => "Wait maximal timeout seconds
.",
1241 description => "Make sure the VM stops
.",
1247 description => "Do
not decativate storage volumes
.",
1260 my $rpcenv = PVE::RPCEnvironment::get();
1262 my $authuser = $rpcenv->get_user();
1264 my $node = extract_param($param, 'node');
1266 my $vmid = extract_param($param, 'vmid');
1268 my $skiplock = extract_param($param, 'skiplock');
1269 raise_param_exc({ skiplock => "Only root may
use this option
." })
1270 if $skiplock && $authuser ne 'root@pam';
1272 my $keepActive = extract_param($param, 'keepActive');
1273 raise_param_exc({ keepActive => "Only root may
use this option
." })
1274 if $keepActive && $authuser ne 'root@pam';
1276 my $storecfg = PVE::Storage::config();
1281 syslog('info', "shutdown VM
$vmid: $upid\n");
1283 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1284 1, $param->{forceStop}, $keepActive);
1289 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1292 __PACKAGE__->register_method({
1293 name => 'vm_suspend',
1294 path => '{vmid}/status/suspend',
1298 description => "Suspend virtual machine
.",
1300 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1303 additionalProperties => 0,
1305 node => get_standard_option('pve-node'),
1306 vmid => get_standard_option('pve-vmid'),
1307 skiplock => get_standard_option('skiplock'),
1316 my $rpcenv = PVE::RPCEnvironment::get();
1318 my $authuser = $rpcenv->get_user();
1320 my $node = extract_param($param, 'node');
1322 my $vmid = extract_param($param, 'vmid');
1324 my $skiplock = extract_param($param, 'skiplock');
1325 raise_param_exc({ skiplock => "Only root may
use this option
." })
1326 if $skiplock && $authuser ne 'root@pam';
1328 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1333 syslog('info', "suspend VM
$vmid: $upid\n");
1335 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1340 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1343 __PACKAGE__->register_method({
1344 name => 'vm_resume',
1345 path => '{vmid}/status/resume',
1349 description => "Resume virtual machine
.",
1351 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1354 additionalProperties => 0,
1356 node => get_standard_option('pve-node'),
1357 vmid => get_standard_option('pve-vmid'),
1358 skiplock => get_standard_option('skiplock'),
1367 my $rpcenv = PVE::RPCEnvironment::get();
1369 my $authuser = $rpcenv->get_user();
1371 my $node = extract_param($param, 'node');
1373 my $vmid = extract_param($param, 'vmid');
1375 my $skiplock = extract_param($param, 'skiplock');
1376 raise_param_exc({ skiplock => "Only root may
use this option
." })
1377 if $skiplock && $authuser ne 'root@pam';
1379 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1384 syslog('info', "resume VM
$vmid: $upid\n");
1386 PVE::QemuServer::vm_resume($vmid, $skiplock);
1391 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1394 __PACKAGE__->register_method({
1395 name => 'vm_sendkey',
1396 path => '{vmid}/sendkey',
1400 description => "Send key event to virtual machine
.",
1402 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1405 additionalProperties => 0,
1407 node => get_standard_option('pve-node'),
1408 vmid => get_standard_option('pve-vmid'),
1409 skiplock => get_standard_option('skiplock'),
1411 description => "The key
(qemu monitor encoding
).",
1416 returns => { type => 'null'},
1420 my $rpcenv = PVE::RPCEnvironment::get();
1422 my $authuser = $rpcenv->get_user();
1424 my $node = extract_param($param, 'node');
1426 my $vmid = extract_param($param, 'vmid');
1428 my $skiplock = extract_param($param, 'skiplock');
1429 raise_param_exc({ skiplock => "Only root may
use this option
." })
1430 if $skiplock && $authuser ne 'root@pam';
1432 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1437 __PACKAGE__->register_method({
1438 name => 'migrate_vm',
1439 path => '{vmid}/migrate',
1443 description => "Migrate virtual machine
. Creates a new migration task
.",
1445 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1448 additionalProperties => 0,
1450 node => get_standard_option('pve-node'),
1451 vmid => get_standard_option('pve-vmid'),
1452 target => get_standard_option('pve-node', { description => "Target node
." }),
1455 description => "Use online
/live migration
.",
1460 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1467 description => "the task ID
.",
1472 my $rpcenv = PVE::RPCEnvironment::get();
1474 my $authuser = $rpcenv->get_user();
1476 my $target = extract_param($param, 'target');
1478 my $localnode = PVE::INotify::nodename();
1479 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1481 PVE::Cluster::check_cfs_quorum();
1483 PVE::Cluster::check_node_exists($target);
1485 my $targetip = PVE::Cluster::remote_node_ip($target);
1487 my $vmid = extract_param($param, 'vmid');
1489 raise_param_exc({ force => "Only root may
use this option
." })
1490 if $param->{force} && $authuser ne 'root@pam';
1493 my $conf = PVE::QemuServer::load_config($vmid);
1495 # try to detect errors early
1497 PVE::QemuServer::check_lock($conf);
1499 if (PVE::QemuServer::check_running($vmid)) {
1500 die "cant migrate running VM without
--online
\n"
1501 if !$param->{online};
1507 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1510 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1515 __PACKAGE__->register_method({
1517 path => '{vmid}/monitor',
1521 description => "Execute Qemu monitor commands
.",
1523 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1526 additionalProperties => 0,
1528 node => get_standard_option('pve-node'),
1529 vmid => get_standard_option('pve-vmid'),
1532 description => "The monitor command
.",
1536 returns => { type => 'string'},
1540 my $vmid = $param->{vmid};
1542 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1546 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1548 $res = "ERROR
: $@" if $@;