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 my $check_storage_access = sub {
59 my ($rpcenv, $authuser, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
61 PVE
::QemuServer
::foreach_drive
($settings, sub {
62 my ($ds, $drive) = @_;
64 my $isCDROM = PVE
::QemuServer
::drive_is_cdrom
($drive);
66 my $volid = $drive->{file
};
68 if (!$volid || $volid eq 'none') {
70 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
71 my ($storeid, $size) = ($2 || $default_storage, $3);
72 die "no storage ID specified (and no default storage)\n" if !$storeid;
73 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storeid, [ 'Datastore.AllocateSpace' ]);
75 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool);
76 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
81 # Note: $pool is only needed when creating a VM, because pool permissions
82 # are automatically inherited if VM already exists inside a pool.
83 my $create_disks = sub {
84 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
89 PVE
::QemuServer
::foreach_drive
($settings, sub {
92 my $volid = $disk->{file
};
94 if (!$volid || $volid eq 'none') {
95 $res->{$ds} = $settings->{$ds};
96 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
97 my ($storeid, $size) = ($2 || $default_storage, $3);
98 die "no storage ID specified (and no default storage)\n" if !$storeid;
99 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
100 my $fmt = $disk->{format
} || $defformat;
101 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
102 $fmt, undef, $size*1024*1024);
103 $disk->{file
} = $volid;
104 push @$vollist, $volid;
105 delete $disk->{format
}; # no longer needed
106 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
108 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool);
109 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
110 $res->{$ds} = $settings->{$ds};
114 # free allocated images on error
116 syslog
('err', "VM $vmid creating disks failed");
117 foreach my $volid (@$vollist) {
118 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
124 # modify vm config if everything went well
125 foreach my $ds (keys %$res) {
126 $conf->{$ds} = $res->{$ds};
132 my $check_vm_modify_config_perm = sub {
133 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
135 return 1 if $authuser ne 'root@pam';
137 foreach my $opt (@$key_list) {
138 # disk checks need to be done somewhere else
139 next if PVE
::QemuServer
::valid_drivename
($opt);
141 if ($opt eq 'sockets' || $opt eq 'cores' ||
142 $opt eq 'cpu' || $opt eq 'smp' ||
143 $opt eq 'cpuimit' || $opt eq 'cpuunits') {
144 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
145 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
146 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
147 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
148 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
149 } elsif ($opt eq 'args' || $opt eq 'lock') {
150 die "only root can set '$opt' config\n";
151 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
152 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
153 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
154 } elsif ($opt =~ m/^net\d+$/) {
155 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
157 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
164 __PACKAGE__-
>register_method({
168 description
=> "Virtual machine index (per node).",
170 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
174 protected
=> 1, # qemu pid files are only readable by root
176 additionalProperties
=> 0,
178 node
=> get_standard_option
('pve-node'),
187 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
192 my $rpcenv = PVE
::RPCEnvironment
::get
();
193 my $authuser = $rpcenv->get_user();
195 my $vmstatus = PVE
::QemuServer
::vmstatus
();
198 foreach my $vmid (keys %$vmstatus) {
199 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
201 my $data = $vmstatus->{$vmid};
202 $data->{vmid
} = $vmid;
209 __PACKAGE__-
>register_method({
213 description
=> "Create or restore a virtual machine.",
215 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.",
217 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
218 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
224 additionalProperties
=> 0,
225 properties
=> PVE
::QemuServer
::json_config_properties
(
227 node
=> get_standard_option
('pve-node'),
228 vmid
=> get_standard_option
('pve-vmid'),
230 description
=> "The backup file.",
235 storage
=> get_standard_option
('pve-storage-id', {
236 description
=> "Default storage.",
242 description
=> "Allow to overwrite existing VM.",
243 requires
=> 'archive',
248 description
=> "Assign a unique random ethernet address.",
249 requires
=> 'archive',
253 type
=> 'string', format
=> 'pve-poolid',
254 description
=> "Add the VM to the specified pool.",
264 my $rpcenv = PVE
::RPCEnvironment
::get
();
266 my $authuser = $rpcenv->get_user();
268 my $node = extract_param
($param, 'node');
270 my $vmid = extract_param
($param, 'vmid');
272 my $archive = extract_param
($param, 'archive');
274 my $storage = extract_param
($param, 'storage');
276 my $force = extract_param
($param, 'force');
278 my $unique = extract_param
($param, 'unique');
280 my $pool = extract_param
($param, 'pool');
282 my $filename = PVE
::QemuServer
::config_file
($vmid);
284 my $storecfg = PVE
::Storage
::config
();
286 PVE
::Cluster
::check_cfs_quorum
();
288 if (defined($pool)) {
289 $rpcenv->check_pool_exist($pool);
290 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
293 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storage, [ 'Datastore.AllocateSpace' ])
294 if defined($storage);
297 &$resolve_cdrom_alias($param);
299 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $pool, $param, $storage);
301 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
303 foreach my $opt (keys %$param) {
304 if (PVE
::QemuServer
::valid_drivename
($opt)) {
305 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
306 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
308 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
309 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
313 PVE
::QemuServer
::add_random_macs
($param);
315 my $keystr = join(' ', keys %$param);
316 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
318 if ($archive eq '-') {
319 die "pipe requires cli environment\n"
320 && $rpcenv->{type
} ne 'cli';
322 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $archive, $pool);
323 die "can't find archive file '$archive'\n" if !($path && -f
$path);
328 my $restorefn = sub {
331 die "unable to restore vm $vmid: config file already exists\n"
334 die "unable to restore vm $vmid: vm is running\n"
335 if PVE
::QemuServer
::check_running
($vmid);
337 # destroy existing data - keep empty config
338 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
342 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
345 unique
=> $unique });
348 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
353 # second test (after locking test is accurate)
354 die "unable to create vm $vmid: config file already exists\n"
365 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
367 # try to be smart about bootdisk
368 my @disks = PVE
::QemuServer
::disknames
();
370 foreach my $ds (reverse @disks) {
371 next if !$conf->{$ds};
372 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
373 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
377 if (!$conf->{bootdisk
} && $firstdisk) {
378 $conf->{bootdisk
} = $firstdisk;
381 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
387 foreach my $volid (@$vollist) {
388 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
391 die "create failed - $err";
395 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
398 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
401 __PACKAGE__-
>register_method({
406 description
=> "Directory index",
411 additionalProperties
=> 0,
413 node
=> get_standard_option
('pve-node'),
414 vmid
=> get_standard_option
('pve-vmid'),
422 subdir
=> { type
=> 'string' },
425 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
431 { subdir
=> 'config' },
432 { subdir
=> 'status' },
433 { subdir
=> 'unlink' },
434 { subdir
=> 'vncproxy' },
435 { subdir
=> 'migrate' },
437 { subdir
=> 'rrddata' },
438 { subdir
=> 'monitor' },
444 __PACKAGE__-
>register_method({
446 path
=> '{vmid}/rrd',
448 protected
=> 1, # fixme: can we avoid that?
450 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
452 description
=> "Read VM RRD statistics (returns PNG)",
454 additionalProperties
=> 0,
456 node
=> get_standard_option
('pve-node'),
457 vmid
=> get_standard_option
('pve-vmid'),
459 description
=> "Specify the time frame you are interested in.",
461 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
464 description
=> "The list of datasources you want to display.",
465 type
=> 'string', format
=> 'pve-configid-list',
468 description
=> "The RRD consolidation function",
470 enum
=> [ 'AVERAGE', 'MAX' ],
478 filename
=> { type
=> 'string' },
484 return PVE
::Cluster
::create_rrd_graph
(
485 "pve2-vm/$param->{vmid}", $param->{timeframe
},
486 $param->{ds
}, $param->{cf
});
490 __PACKAGE__-
>register_method({
492 path
=> '{vmid}/rrddata',
494 protected
=> 1, # fixme: can we avoid that?
496 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
498 description
=> "Read VM RRD statistics",
500 additionalProperties
=> 0,
502 node
=> get_standard_option
('pve-node'),
503 vmid
=> get_standard_option
('pve-vmid'),
505 description
=> "Specify the time frame you are interested in.",
507 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
510 description
=> "The RRD consolidation function",
512 enum
=> [ 'AVERAGE', 'MAX' ],
527 return PVE
::Cluster
::create_rrd_data
(
528 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
532 __PACKAGE__-
>register_method({
534 path
=> '{vmid}/config',
537 description
=> "Get virtual machine configuration.",
539 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
542 additionalProperties
=> 0,
544 node
=> get_standard_option
('pve-node'),
545 vmid
=> get_standard_option
('pve-vmid'),
553 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
560 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
565 my $vm_is_volid_owner = sub {
566 my ($storecfg, $vmid, $volid) =@_;
568 if ($volid !~ m
|^/|) {
570 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
571 if ($owner && ($owner == $vmid)) {
579 my $test_deallocate_drive = sub {
580 my ($storecfg, $vmid, $key, $drive, $force) = @_;
582 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
583 my $volid = $drive->{file
};
584 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
585 if ($force || $key =~ m/^unused/) {
586 my $sid = PVE
::Storage
::parse_volume_id
($volid);
595 my $delete_drive = sub {
596 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
598 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
599 my $volid = $drive->{file
};
600 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
601 if ($force || $key =~ m/^unused/) {
602 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
605 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
607 delete $conf->{$key};
612 my $vmconfig_delete_option = sub {
613 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
615 return if !defined($conf->{$opt});
617 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
620 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
622 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
623 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
624 $rpcenv->check_storage_perm($authuser, $vmid, undef, $sid, [ 'Datastore.Allocate' ]);
628 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
631 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
632 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
634 delete $conf->{$opt};
637 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
640 my $vmconfig_update_disk = sub {
641 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
643 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
645 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
646 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
648 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
653 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
655 my $media = $drive->{media
} || 'disk';
656 my $oldmedia = $old_drive->{media
} || 'disk';
657 die "unable to change media type\n" if $media ne $oldmedia;
659 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
660 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
662 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
663 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
668 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
669 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
671 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
672 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
674 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
676 if (PVE
::QemuServer
::check_running
($vmid)) {
677 if ($drive->{file
} eq 'none') {
678 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
680 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
681 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
682 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
686 } else { # hotplug new disks
688 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
692 my $vmconfig_update_net = sub {
693 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
696 #if online update, then unplug first
697 die "error hot-unplug
$opt for update
" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
700 $conf->{$opt} = $value;
701 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
702 $conf = PVE::QemuServer::load_config($vmid); # update/reload
704 my $net = PVE::QemuServer::parse_net($conf->{$opt});
706 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
709 my $vm_config_perm_list = [
719 __PACKAGE__->register_method({
721 path => '{vmid}/config',
725 description => "Set virtual machine options
.",
727 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
730 additionalProperties => 0,
731 properties => PVE::QemuServer::json_config_properties(
733 node => get_standard_option('pve-node'),
734 vmid => get_standard_option('pve-vmid'),
735 skiplock => get_standard_option('skiplock'),
737 type => 'string', format => 'pve-configid-list',
738 description => "A list of settings you want to
delete.",
743 description => $opt_force_description,
745 requires => 'delete',
749 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
755 returns => { type => 'null'},
759 my $rpcenv = PVE::RPCEnvironment::get();
761 my $authuser = $rpcenv->get_user();
763 my $node = extract_param($param, 'node');
765 my $vmid = extract_param($param, 'vmid');
767 my $digest = extract_param($param, 'digest');
769 my @paramarr = (); # used for log message
770 foreach my $key (keys %$param) {
771 push @paramarr, "-$key", $param->{$key};
774 my $skiplock = extract_param($param, 'skiplock');
775 raise_param_exc({ skiplock => "Only root may
use this option
." })
776 if $skiplock && $authuser ne 'root@pam';
778 my $delete_str = extract_param($param, 'delete');
780 my $force = extract_param($param, 'force');
782 die "no options specified
\n" if !$delete_str && !scalar(keys %$param);
784 my $storecfg = PVE::Storage::config();
786 &$resolve_cdrom_alias($param);
788 # now try to verify all parameters
791 foreach my $opt (PVE::Tools::split_list($delete_str)) {
792 $opt = 'ide2' if $opt eq 'cdrom';
793 raise_param_exc({ delete => "you can
't use '-$opt' and " .
794 "-delete $opt' at the same
time" })
795 if defined($param->{$opt});
797 if (!PVE::QemuServer::option_exists($opt)) {
798 raise_param_exc({ delete => "unknown option
'$opt'" });
804 foreach my $opt (keys %$param) {
805 if (PVE::QemuServer::valid_drivename($opt)) {
807 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
808 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
809 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
810 } elsif ($opt =~ m/^net(\d+)$/) {
812 my $net = PVE::QemuServer::parse_net($param->{$opt});
813 $param->{$opt} = PVE::QemuServer::print_net($net);
817 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
819 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
821 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, undef, $param);
825 my $conf = PVE::QemuServer::load_config($vmid);
827 die "checksum missmatch
(file change by other user?
)\n"
828 if $digest && $digest ne $conf->{digest};
830 PVE::QemuServer::check_lock($conf) if !$skiplock;
832 PVE::Cluster::log_msg('info', $authuser, "update VM
$vmid: " . join (' ', @paramarr));
834 foreach my $opt (@delete) { # delete
835 $conf = PVE::QemuServer::load_config($vmid); # update/reload
836 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
839 foreach my $opt (keys %$param) { # add/change
841 $conf = PVE::QemuServer::load_config($vmid); # update/reload
843 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
845 if (PVE::QemuServer::valid_drivename($opt)) {
847 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
848 $opt, $param->{$opt}, $force);
850 } elsif ($opt =~ m/^net(\d+)$/) { #nics
852 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
853 $opt, $param->{$opt});
857 $conf->{$opt} = $param->{$opt};
858 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
863 PVE::QemuServer::lock_config($vmid, $updatefn);
869 __PACKAGE__->register_method({
870 name => 'destroy_vm',
875 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
877 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
880 additionalProperties => 0,
882 node => get_standard_option('pve-node'),
883 vmid => get_standard_option('pve-vmid'),
884 skiplock => get_standard_option('skiplock'),
893 my $rpcenv = PVE::RPCEnvironment::get();
895 my $authuser = $rpcenv->get_user();
897 my $vmid = $param->{vmid};
899 my $skiplock = $param->{skiplock};
900 raise_param_exc({ skiplock => "Only root may
use this option
." })
901 if $skiplock && $authuser ne 'root@pam';
904 my $conf = PVE::QemuServer::load_config($vmid);
906 my $storecfg = PVE::Storage::config();
911 syslog('info', "destroy VM
$vmid: $upid\n");
913 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
916 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
919 __PACKAGE__->register_method({
921 path => '{vmid}/unlink',
925 description => "Unlink
/delete disk images
.",
927 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
930 additionalProperties => 0,
932 node => get_standard_option('pve-node'),
933 vmid => get_standard_option('pve-vmid'),
935 type => 'string', format => 'pve-configid-list',
936 description => "A list of disk IDs you want to
delete.",
940 description => $opt_force_description,
945 returns => { type => 'null'},
949 $param->{delete} = extract_param($param, 'idlist');
951 __PACKAGE__->update_vm($param);
958 __PACKAGE__->register_method({
960 path => '{vmid}/vncproxy',
964 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
966 description => "Creates a TCP VNC proxy connections
.",
968 additionalProperties => 0,
970 node => get_standard_option('pve-node'),
971 vmid => get_standard_option('pve-vmid'),
975 additionalProperties => 0,
977 user => { type => 'string' },
978 ticket => { type => 'string' },
979 cert => { type => 'string' },
980 port => { type => 'integer' },
981 upid => { type => 'string' },
987 my $rpcenv = PVE::RPCEnvironment::get();
989 my $authuser = $rpcenv->get_user();
991 my $vmid = $param->{vmid};
992 my $node = $param->{node};
994 my $authpath = "/vms/$vmid";
996 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
998 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
1001 my $port = PVE::Tools::next_vnc_port();
1005 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1006 $remip = PVE::Cluster::remote_node_ip($node);
1009 # NOTE: kvm VNC traffic is already TLS encrypted,
1010 # so we select the fastest chipher here (or 'none'?)
1011 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1012 '-c', 'blowfish-cbc', $remip] : [];
1019 syslog('info', "starting vnc proxy
$upid\n");
1021 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
1023 my $qmstr = join(' ', @$qmcmd);
1025 # also redirect stderr (else we get RFB protocol errors)
1026 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
1028 PVE::Tools::run_command($cmd);
1033 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1044 __PACKAGE__->register_method({
1046 path => '{vmid}/status',
1049 description => "Directory
index",
1054 additionalProperties => 0,
1056 node => get_standard_option('pve-node'),
1057 vmid => get_standard_option('pve-vmid'),
1065 subdir => { type => 'string' },
1068 links => [ { rel => 'child', href => "{subdir
}" } ],
1074 my $conf = PVE::QemuServer::load_config($param->{vmid});
1077 { subdir => 'current' },
1078 { subdir => 'start' },
1079 { subdir => 'stop' },
1085 __PACKAGE__->register_method({
1086 name => 'vm_status',
1087 path => '{vmid}/status/current',
1090 protected => 1, # qemu pid files are only readable by root
1091 description => "Get virtual machine status
.",
1093 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1096 additionalProperties => 0,
1098 node => get_standard_option('pve-node'),
1099 vmid => get_standard_option('pve-vmid'),
1102 returns => { type => 'object' },
1107 my $conf = PVE::QemuServer::load_config($param->{vmid});
1109 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1110 my $status = $vmstatus->{$param->{vmid}};
1112 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1113 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1122 __PACKAGE__->register_method({
1124 path => '{vmid}/status/start',
1128 description => "Start virtual machine
.",
1130 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1133 additionalProperties => 0,
1135 node => get_standard_option('pve-node'),
1136 vmid => get_standard_option('pve-vmid'),
1137 skiplock => get_standard_option('skiplock'),
1138 stateuri => get_standard_option('pve-qm-stateuri'),
1147 my $rpcenv = PVE::RPCEnvironment::get();
1149 my $authuser = $rpcenv->get_user();
1151 my $node = extract_param($param, 'node');
1153 my $vmid = extract_param($param, 'vmid');
1155 my $stateuri = extract_param($param, 'stateuri');
1156 raise_param_exc({ stateuri => "Only root may
use this option
." })
1157 if $stateuri && $authuser ne 'root@pam';
1159 my $skiplock = extract_param($param, 'skiplock');
1160 raise_param_exc({ skiplock => "Only root may
use this option
." })
1161 if $skiplock && $authuser ne 'root@pam';
1163 my $storecfg = PVE::Storage::config();
1168 syslog('info', "start VM
$vmid: $upid\n");
1170 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1175 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1178 __PACKAGE__->register_method({
1180 path => '{vmid}/status/stop',
1184 description => "Stop virtual machine
.",
1186 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1189 additionalProperties => 0,
1191 node => get_standard_option('pve-node'),
1192 vmid => get_standard_option('pve-vmid'),
1193 skiplock => get_standard_option('skiplock'),
1195 description => "Wait maximal timeout seconds
.",
1201 description => "Do
not decativate storage volumes
.",
1214 my $rpcenv = PVE::RPCEnvironment::get();
1216 my $authuser = $rpcenv->get_user();
1218 my $node = extract_param($param, 'node');
1220 my $vmid = extract_param($param, 'vmid');
1222 my $skiplock = extract_param($param, 'skiplock');
1223 raise_param_exc({ skiplock => "Only root may
use this option
." })
1224 if $skiplock && $authuser ne 'root@pam';
1226 my $keepActive = extract_param($param, 'keepActive');
1227 raise_param_exc({ keepActive => "Only root may
use this option
." })
1228 if $keepActive && $authuser ne 'root@pam';
1230 my $storecfg = PVE::Storage::config();
1235 syslog('info', "stop VM
$vmid: $upid\n");
1237 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1238 $param->{timeout}, 0, 1, $keepActive);
1243 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1246 __PACKAGE__->register_method({
1248 path => '{vmid}/status/reset',
1252 description => "Reset virtual machine
.",
1254 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1257 additionalProperties => 0,
1259 node => get_standard_option('pve-node'),
1260 vmid => get_standard_option('pve-vmid'),
1261 skiplock => get_standard_option('skiplock'),
1270 my $rpcenv = PVE::RPCEnvironment::get();
1272 my $authuser = $rpcenv->get_user();
1274 my $node = extract_param($param, 'node');
1276 my $vmid = extract_param($param, 'vmid');
1278 my $skiplock = extract_param($param, 'skiplock');
1279 raise_param_exc({ skiplock => "Only root may
use this option
." })
1280 if $skiplock && $authuser ne 'root@pam';
1282 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1287 PVE::QemuServer::vm_reset($vmid, $skiplock);
1292 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1295 __PACKAGE__->register_method({
1296 name => 'vm_shutdown',
1297 path => '{vmid}/status/shutdown',
1301 description => "Shutdown virtual machine
.",
1303 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1306 additionalProperties => 0,
1308 node => get_standard_option('pve-node'),
1309 vmid => get_standard_option('pve-vmid'),
1310 skiplock => get_standard_option('skiplock'),
1312 description => "Wait maximal timeout seconds
.",
1318 description => "Make sure the VM stops
.",
1324 description => "Do
not decativate storage volumes
.",
1337 my $rpcenv = PVE::RPCEnvironment::get();
1339 my $authuser = $rpcenv->get_user();
1341 my $node = extract_param($param, 'node');
1343 my $vmid = extract_param($param, 'vmid');
1345 my $skiplock = extract_param($param, 'skiplock');
1346 raise_param_exc({ skiplock => "Only root may
use this option
." })
1347 if $skiplock && $authuser ne 'root@pam';
1349 my $keepActive = extract_param($param, 'keepActive');
1350 raise_param_exc({ keepActive => "Only root may
use this option
." })
1351 if $keepActive && $authuser ne 'root@pam';
1353 my $storecfg = PVE::Storage::config();
1358 syslog('info', "shutdown VM
$vmid: $upid\n");
1360 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1361 1, $param->{forceStop}, $keepActive);
1366 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1369 __PACKAGE__->register_method({
1370 name => 'vm_suspend',
1371 path => '{vmid}/status/suspend',
1375 description => "Suspend virtual machine
.",
1377 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1380 additionalProperties => 0,
1382 node => get_standard_option('pve-node'),
1383 vmid => get_standard_option('pve-vmid'),
1384 skiplock => get_standard_option('skiplock'),
1393 my $rpcenv = PVE::RPCEnvironment::get();
1395 my $authuser = $rpcenv->get_user();
1397 my $node = extract_param($param, 'node');
1399 my $vmid = extract_param($param, 'vmid');
1401 my $skiplock = extract_param($param, 'skiplock');
1402 raise_param_exc({ skiplock => "Only root may
use this option
." })
1403 if $skiplock && $authuser ne 'root@pam';
1405 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1410 syslog('info', "suspend VM
$vmid: $upid\n");
1412 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1417 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1420 __PACKAGE__->register_method({
1421 name => 'vm_resume',
1422 path => '{vmid}/status/resume',
1426 description => "Resume virtual machine
.",
1428 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1431 additionalProperties => 0,
1433 node => get_standard_option('pve-node'),
1434 vmid => get_standard_option('pve-vmid'),
1435 skiplock => get_standard_option('skiplock'),
1444 my $rpcenv = PVE::RPCEnvironment::get();
1446 my $authuser = $rpcenv->get_user();
1448 my $node = extract_param($param, 'node');
1450 my $vmid = extract_param($param, 'vmid');
1452 my $skiplock = extract_param($param, 'skiplock');
1453 raise_param_exc({ skiplock => "Only root may
use this option
." })
1454 if $skiplock && $authuser ne 'root@pam';
1456 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1461 syslog('info', "resume VM
$vmid: $upid\n");
1463 PVE::QemuServer::vm_resume($vmid, $skiplock);
1468 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1471 __PACKAGE__->register_method({
1472 name => 'vm_sendkey',
1473 path => '{vmid}/sendkey',
1477 description => "Send key event to virtual machine
.",
1479 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1482 additionalProperties => 0,
1484 node => get_standard_option('pve-node'),
1485 vmid => get_standard_option('pve-vmid'),
1486 skiplock => get_standard_option('skiplock'),
1488 description => "The key
(qemu monitor encoding
).",
1493 returns => { type => 'null'},
1497 my $rpcenv = PVE::RPCEnvironment::get();
1499 my $authuser = $rpcenv->get_user();
1501 my $node = extract_param($param, 'node');
1503 my $vmid = extract_param($param, 'vmid');
1505 my $skiplock = extract_param($param, 'skiplock');
1506 raise_param_exc({ skiplock => "Only root may
use this option
." })
1507 if $skiplock && $authuser ne 'root@pam';
1509 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1514 __PACKAGE__->register_method({
1515 name => 'migrate_vm',
1516 path => '{vmid}/migrate',
1520 description => "Migrate virtual machine
. Creates a new migration task
.",
1522 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1525 additionalProperties => 0,
1527 node => get_standard_option('pve-node'),
1528 vmid => get_standard_option('pve-vmid'),
1529 target => get_standard_option('pve-node', { description => "Target node
." }),
1532 description => "Use online
/live migration
.",
1537 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1544 description => "the task ID
.",
1549 my $rpcenv = PVE::RPCEnvironment::get();
1551 my $authuser = $rpcenv->get_user();
1553 my $target = extract_param($param, 'target');
1555 my $localnode = PVE::INotify::nodename();
1556 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1558 PVE::Cluster::check_cfs_quorum();
1560 PVE::Cluster::check_node_exists($target);
1562 my $targetip = PVE::Cluster::remote_node_ip($target);
1564 my $vmid = extract_param($param, 'vmid');
1566 raise_param_exc({ force => "Only root may
use this option
." })
1567 if $param->{force} && $authuser ne 'root@pam';
1570 my $conf = PVE::QemuServer::load_config($vmid);
1572 # try to detect errors early
1574 PVE::QemuServer::check_lock($conf);
1576 if (PVE::QemuServer::check_running($vmid)) {
1577 die "cant migrate running VM without
--online
\n"
1578 if !$param->{online};
1584 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1587 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1592 __PACKAGE__->register_method({
1594 path => '{vmid}/monitor',
1598 description => "Execute Qemu monitor commands
.",
1600 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1603 additionalProperties => 0,
1605 node => get_standard_option('pve-node'),
1606 vmid => get_standard_option('pve-vmid'),
1609 description => "The monitor command
.",
1613 returns => { type => 'string'},
1617 my $vmid = $param->{vmid};
1619 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1623 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1625 $res = "ERROR
: $@" if $@;