1 package PVE
::API2
::Qemu
;
7 use PVE
::Cluster qw
(cfs_read_file cfs_write_file
);;
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
};
37 my $check_storage_access = sub {
38 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
40 PVE
::QemuServer
::foreach_drive
($settings, sub {
41 my ($ds, $drive) = @_;
43 my $isCDROM = PVE
::QemuServer
::drive_is_cdrom
($drive);
45 my $volid = $drive->{file
};
47 if (!$volid || $volid eq 'none') {
49 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
50 my ($storeid, $size) = ($2 || $default_storage, $3);
51 die "no storage ID specified (and no default storage)\n" if !$storeid;
52 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
54 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
55 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
60 # Note: $pool is only needed when creating a VM, because pool permissions
61 # are automatically inherited if VM already exists inside a pool.
62 my $create_disks = sub {
63 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
68 PVE
::QemuServer
::foreach_drive
($settings, sub {
71 my $volid = $disk->{file
};
73 if (!$volid || $volid eq 'none') {
74 $res->{$ds} = $settings->{$ds};
75 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
76 my ($storeid, $size) = ($2 || $default_storage, $3);
77 die "no storage ID specified (and no default storage)\n" if !$storeid;
78 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
79 my $fmt = $disk->{format
} || $defformat;
80 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
81 $fmt, undef, $size*1024*1024);
82 $disk->{file
} = $volid;
83 push @$vollist, $volid;
84 delete $disk->{format
}; # no longer needed
85 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
87 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
88 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
89 $res->{$ds} = $settings->{$ds};
93 # free allocated images on error
95 syslog
('err', "VM $vmid creating disks failed");
96 foreach my $volid (@$vollist) {
97 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
103 # modify vm config if everything went well
104 foreach my $ds (keys %$res) {
105 $conf->{$ds} = $res->{$ds};
111 my $check_vm_modify_config_perm = sub {
112 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
114 return 1 if $authuser ne 'root@pam';
116 foreach my $opt (@$key_list) {
117 # disk checks need to be done somewhere else
118 next if PVE
::QemuServer
::valid_drivename
($opt);
120 if ($opt eq 'sockets' || $opt eq 'cores' ||
121 $opt eq 'cpu' || $opt eq 'smp' ||
122 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
123 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
124 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
125 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
126 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
127 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
128 } elsif ($opt eq 'args' || $opt eq 'lock') {
129 die "only root can set '$opt' config\n";
130 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
131 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
132 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
133 } elsif ($opt =~ m/^net\d+$/) {
134 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
136 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
143 __PACKAGE__-
>register_method({
147 description
=> "Virtual machine index (per node).",
149 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
153 protected
=> 1, # qemu pid files are only readable by root
155 additionalProperties
=> 0,
157 node
=> get_standard_option
('pve-node'),
166 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
171 my $rpcenv = PVE
::RPCEnvironment
::get
();
172 my $authuser = $rpcenv->get_user();
174 my $vmstatus = PVE
::QemuServer
::vmstatus
();
177 foreach my $vmid (keys %$vmstatus) {
178 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
180 my $data = $vmstatus->{$vmid};
181 $data->{vmid
} = $vmid;
188 __PACKAGE__-
>register_method({
192 description
=> "Create or restore a virtual machine.",
194 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.",
196 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
197 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
203 additionalProperties
=> 0,
204 properties
=> PVE
::QemuServer
::json_config_properties
(
206 node
=> get_standard_option
('pve-node'),
207 vmid
=> get_standard_option
('pve-vmid'),
209 description
=> "The backup file.",
214 storage
=> get_standard_option
('pve-storage-id', {
215 description
=> "Default storage.",
221 description
=> "Allow to overwrite existing VM.",
222 requires
=> 'archive',
227 description
=> "Assign a unique random ethernet address.",
228 requires
=> 'archive',
232 type
=> 'string', format
=> 'pve-poolid',
233 description
=> "Add the VM to the specified pool.",
243 my $rpcenv = PVE
::RPCEnvironment
::get
();
245 my $authuser = $rpcenv->get_user();
247 my $node = extract_param
($param, 'node');
249 my $vmid = extract_param
($param, 'vmid');
251 my $archive = extract_param
($param, 'archive');
253 my $storage = extract_param
($param, 'storage');
255 my $force = extract_param
($param, 'force');
257 my $unique = extract_param
($param, 'unique');
259 my $pool = extract_param
($param, 'pool');
261 my $filename = PVE
::QemuServer
::config_file
($vmid);
263 my $storecfg = PVE
::Storage
::config
();
265 PVE
::Cluster
::check_cfs_quorum
();
267 if (defined($pool)) {
268 $rpcenv->check_pool_exist($pool);
269 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
272 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
273 if defined($storage);
276 &$resolve_cdrom_alias($param);
278 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
280 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
282 foreach my $opt (keys %$param) {
283 if (PVE
::QemuServer
::valid_drivename
($opt)) {
284 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
285 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
287 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
288 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
292 PVE
::QemuServer
::add_random_macs
($param);
294 my $keystr = join(' ', keys %$param);
295 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
297 if ($archive eq '-') {
298 die "pipe requires cli environment\n"
299 && $rpcenv->{type
} ne 'cli';
301 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
302 die "can't find archive file '$archive'\n" if !($path && -f
$path);
307 my $addVMtoPoolFn = sub {
308 my $usercfg = cfs_read_file
("user.cfg");
309 if (my $data = $usercfg->{pools
}->{$pool}) {
310 $data->{vms
}->{$vmid} = 1;
311 $usercfg->{vms
}->{$vmid} = $pool;
312 cfs_write_file
("user.cfg", $usercfg);
316 my $restorefn = sub {
319 die "unable to restore vm $vmid: config file already exists\n"
322 die "unable to restore vm $vmid: vm is running\n"
323 if PVE
::QemuServer
::check_running
($vmid);
325 # destroy existing data - keep empty config
326 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
330 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
333 unique
=> $unique });
335 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
338 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
343 # second test (after locking test is accurate)
344 die "unable to create vm $vmid: config file already exists\n"
355 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
357 # try to be smart about bootdisk
358 my @disks = PVE
::QemuServer
::disknames
();
360 foreach my $ds (reverse @disks) {
361 next if !$conf->{$ds};
362 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
363 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
367 if (!$conf->{bootdisk
} && $firstdisk) {
368 $conf->{bootdisk
} = $firstdisk;
371 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
377 foreach my $volid (@$vollist) {
378 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
381 die "create failed - $err";
384 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
387 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
390 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
393 __PACKAGE__-
>register_method({
398 description
=> "Directory index",
403 additionalProperties
=> 0,
405 node
=> get_standard_option
('pve-node'),
406 vmid
=> get_standard_option
('pve-vmid'),
414 subdir
=> { type
=> 'string' },
417 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
423 { subdir
=> 'config' },
424 { subdir
=> 'status' },
425 { subdir
=> 'unlink' },
426 { subdir
=> 'vncproxy' },
427 { subdir
=> 'migrate' },
429 { subdir
=> 'rrddata' },
430 { subdir
=> 'monitor' },
436 __PACKAGE__-
>register_method({
438 path
=> '{vmid}/rrd',
440 protected
=> 1, # fixme: can we avoid that?
442 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
444 description
=> "Read VM RRD statistics (returns PNG)",
446 additionalProperties
=> 0,
448 node
=> get_standard_option
('pve-node'),
449 vmid
=> get_standard_option
('pve-vmid'),
451 description
=> "Specify the time frame you are interested in.",
453 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
456 description
=> "The list of datasources you want to display.",
457 type
=> 'string', format
=> 'pve-configid-list',
460 description
=> "The RRD consolidation function",
462 enum
=> [ 'AVERAGE', 'MAX' ],
470 filename
=> { type
=> 'string' },
476 return PVE
::Cluster
::create_rrd_graph
(
477 "pve2-vm/$param->{vmid}", $param->{timeframe
},
478 $param->{ds
}, $param->{cf
});
482 __PACKAGE__-
>register_method({
484 path
=> '{vmid}/rrddata',
486 protected
=> 1, # fixme: can we avoid that?
488 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
490 description
=> "Read VM RRD statistics",
492 additionalProperties
=> 0,
494 node
=> get_standard_option
('pve-node'),
495 vmid
=> get_standard_option
('pve-vmid'),
497 description
=> "Specify the time frame you are interested in.",
499 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
502 description
=> "The RRD consolidation function",
504 enum
=> [ 'AVERAGE', 'MAX' ],
519 return PVE
::Cluster
::create_rrd_data
(
520 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
524 __PACKAGE__-
>register_method({
526 path
=> '{vmid}/config',
529 description
=> "Get virtual machine configuration.",
531 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
534 additionalProperties
=> 0,
536 node
=> get_standard_option
('pve-node'),
537 vmid
=> get_standard_option
('pve-vmid'),
545 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
552 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
557 my $vm_is_volid_owner = sub {
558 my ($storecfg, $vmid, $volid) =@_;
560 if ($volid !~ m
|^/|) {
562 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
563 if ($owner && ($owner == $vmid)) {
571 my $test_deallocate_drive = sub {
572 my ($storecfg, $vmid, $key, $drive, $force) = @_;
574 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
575 my $volid = $drive->{file
};
576 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
577 if ($force || $key =~ m/^unused/) {
578 my $sid = PVE
::Storage
::parse_volume_id
($volid);
587 my $delete_drive = sub {
588 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
590 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
591 my $volid = $drive->{file
};
592 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
593 if ($force || $key =~ m/^unused/) {
594 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
597 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
599 delete $conf->{$key};
604 my $vmconfig_delete_option = sub {
605 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
607 return if !defined($conf->{$opt});
609 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
612 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
614 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
615 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
616 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
620 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
623 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
624 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
626 delete $conf->{$opt};
629 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
632 my $vmconfig_update_disk = sub {
633 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
635 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
637 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
638 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
640 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
645 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
647 my $media = $drive->{media
} || 'disk';
648 my $oldmedia = $old_drive->{media
} || 'disk';
649 die "unable to change media type\n" if $media ne $oldmedia;
651 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
652 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
654 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
655 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
660 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
661 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
663 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
664 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
666 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
668 if (PVE
::QemuServer
::check_running
($vmid)) {
669 if ($drive->{file
} eq 'none') {
670 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
672 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
673 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
674 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
678 } else { # hotplug new disks
680 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
684 my $vmconfig_update_net = sub {
685 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
688 #if online update, then unplug first
689 die "error hot-unplug
$opt for update
" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
692 $conf->{$opt} = $value;
693 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
694 $conf = PVE::QemuServer::load_config($vmid); # update/reload
696 my $net = PVE::QemuServer::parse_net($conf->{$opt});
698 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
701 my $vm_config_perm_list = [
711 __PACKAGE__->register_method({
713 path => '{vmid}/config',
717 description => "Set virtual machine options
.",
719 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
722 additionalProperties => 0,
723 properties => PVE::QemuServer::json_config_properties(
725 node => get_standard_option('pve-node'),
726 vmid => get_standard_option('pve-vmid'),
727 skiplock => get_standard_option('skiplock'),
729 type => 'string', format => 'pve-configid-list',
730 description => "A list of settings you want to
delete.",
735 description => $opt_force_description,
737 requires => 'delete',
741 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
747 returns => { type => 'null'},
751 my $rpcenv = PVE::RPCEnvironment::get();
753 my $authuser = $rpcenv->get_user();
755 my $node = extract_param($param, 'node');
757 my $vmid = extract_param($param, 'vmid');
759 my $digest = extract_param($param, 'digest');
761 my @paramarr = (); # used for log message
762 foreach my $key (keys %$param) {
763 push @paramarr, "-$key", $param->{$key};
766 my $skiplock = extract_param($param, 'skiplock');
767 raise_param_exc({ skiplock => "Only root may
use this option
." })
768 if $skiplock && $authuser ne 'root@pam';
770 my $delete_str = extract_param($param, 'delete');
772 my $force = extract_param($param, 'force');
774 die "no options specified
\n" if !$delete_str && !scalar(keys %$param);
776 my $storecfg = PVE::Storage::config();
778 &$resolve_cdrom_alias($param);
780 # now try to verify all parameters
783 foreach my $opt (PVE::Tools::split_list($delete_str)) {
784 $opt = 'ide2' if $opt eq 'cdrom';
785 raise_param_exc({ delete => "you can
't use '-$opt' and " .
786 "-delete $opt' at the same
time" })
787 if defined($param->{$opt});
789 if (!PVE::QemuServer::option_exists($opt)) {
790 raise_param_exc({ delete => "unknown option
'$opt'" });
796 foreach my $opt (keys %$param) {
797 if (PVE::QemuServer::valid_drivename($opt)) {
799 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
800 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
801 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
802 } elsif ($opt =~ m/^net(\d+)$/) {
804 my $net = PVE::QemuServer::parse_net($param->{$opt});
805 $param->{$opt} = PVE::QemuServer::print_net($net);
809 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
811 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
813 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
817 my $conf = PVE::QemuServer::load_config($vmid);
819 die "checksum missmatch
(file change by other user?
)\n"
820 if $digest && $digest ne $conf->{digest};
822 PVE::QemuServer::check_lock($conf) if !$skiplock;
824 PVE::Cluster::log_msg('info', $authuser, "update VM
$vmid: " . join (' ', @paramarr));
826 foreach my $opt (@delete) { # delete
827 $conf = PVE::QemuServer::load_config($vmid); # update/reload
828 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
831 foreach my $opt (keys %$param) { # add/change
833 $conf = PVE::QemuServer::load_config($vmid); # update/reload
835 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
837 if (PVE::QemuServer::valid_drivename($opt)) {
839 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
840 $opt, $param->{$opt}, $force);
842 } elsif ($opt =~ m/^net(\d+)$/) { #nics
844 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
845 $opt, $param->{$opt});
849 $conf->{$opt} = $param->{$opt};
850 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
855 PVE::QemuServer::lock_config($vmid, $updatefn);
861 __PACKAGE__->register_method({
862 name => 'destroy_vm',
867 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
869 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
872 additionalProperties => 0,
874 node => get_standard_option('pve-node'),
875 vmid => get_standard_option('pve-vmid'),
876 skiplock => get_standard_option('skiplock'),
885 my $rpcenv = PVE::RPCEnvironment::get();
887 my $authuser = $rpcenv->get_user();
889 my $vmid = $param->{vmid};
891 my $skiplock = $param->{skiplock};
892 raise_param_exc({ skiplock => "Only root may
use this option
." })
893 if $skiplock && $authuser ne 'root@pam';
896 my $conf = PVE::QemuServer::load_config($vmid);
898 my $storecfg = PVE::Storage::config();
900 my $delVMfromPoolFn = sub {
901 my $usercfg = cfs_read_file("user
.cfg
");
902 my $pool = $usercfg->{vms}->{$vmid};
903 if (my $data = $usercfg->{pools}->{$pool}) {
904 delete $data->{vms}->{$vmid};
905 delete $usercfg->{vms}->{$vmid};
906 cfs_write_file("user
.cfg
", $usercfg);
913 syslog('info', "destroy VM
$vmid: $upid\n");
915 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
917 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed
");
920 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
923 __PACKAGE__->register_method({
925 path => '{vmid}/unlink',
929 description => "Unlink
/delete disk images
.",
931 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
934 additionalProperties => 0,
936 node => get_standard_option('pve-node'),
937 vmid => get_standard_option('pve-vmid'),
939 type => 'string', format => 'pve-configid-list',
940 description => "A list of disk IDs you want to
delete.",
944 description => $opt_force_description,
949 returns => { type => 'null'},
953 $param->{delete} = extract_param($param, 'idlist');
955 __PACKAGE__->update_vm($param);
962 __PACKAGE__->register_method({
964 path => '{vmid}/vncproxy',
968 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
970 description => "Creates a TCP VNC proxy connections
.",
972 additionalProperties => 0,
974 node => get_standard_option('pve-node'),
975 vmid => get_standard_option('pve-vmid'),
979 additionalProperties => 0,
981 user => { type => 'string' },
982 ticket => { type => 'string' },
983 cert => { type => 'string' },
984 port => { type => 'integer' },
985 upid => { type => 'string' },
991 my $rpcenv = PVE::RPCEnvironment::get();
993 my $authuser = $rpcenv->get_user();
995 my $vmid = $param->{vmid};
996 my $node = $param->{node};
998 my $authpath = "/vms/$vmid";
1000 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1002 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
1005 my $port = PVE::Tools::next_vnc_port();
1009 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1010 $remip = PVE::Cluster::remote_node_ip($node);
1013 # NOTE: kvm VNC traffic is already TLS encrypted,
1014 # so we select the fastest chipher here (or 'none'?)
1015 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1016 '-c', 'blowfish-cbc', $remip] : [];
1023 syslog('info', "starting vnc proxy
$upid\n");
1025 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
1027 my $qmstr = join(' ', @$qmcmd);
1029 # also redirect stderr (else we get RFB protocol errors)
1030 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
1032 PVE::Tools::run_command($cmd);
1037 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1048 __PACKAGE__->register_method({
1050 path => '{vmid}/status',
1053 description => "Directory
index",
1058 additionalProperties => 0,
1060 node => get_standard_option('pve-node'),
1061 vmid => get_standard_option('pve-vmid'),
1069 subdir => { type => 'string' },
1072 links => [ { rel => 'child', href => "{subdir
}" } ],
1078 my $conf = PVE::QemuServer::load_config($param->{vmid});
1081 { subdir => 'current' },
1082 { subdir => 'start' },
1083 { subdir => 'stop' },
1089 __PACKAGE__->register_method({
1090 name => 'vm_status',
1091 path => '{vmid}/status/current',
1094 protected => 1, # qemu pid files are only readable by root
1095 description => "Get virtual machine status
.",
1097 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1100 additionalProperties => 0,
1102 node => get_standard_option('pve-node'),
1103 vmid => get_standard_option('pve-vmid'),
1106 returns => { type => 'object' },
1111 my $conf = PVE::QemuServer::load_config($param->{vmid});
1113 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1114 my $status = $vmstatus->{$param->{vmid}};
1116 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1117 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1126 __PACKAGE__->register_method({
1128 path => '{vmid}/status/start',
1132 description => "Start virtual machine
.",
1134 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1137 additionalProperties => 0,
1139 node => get_standard_option('pve-node'),
1140 vmid => get_standard_option('pve-vmid'),
1141 skiplock => get_standard_option('skiplock'),
1142 stateuri => get_standard_option('pve-qm-stateuri'),
1151 my $rpcenv = PVE::RPCEnvironment::get();
1153 my $authuser = $rpcenv->get_user();
1155 my $node = extract_param($param, 'node');
1157 my $vmid = extract_param($param, 'vmid');
1159 my $stateuri = extract_param($param, 'stateuri');
1160 raise_param_exc({ stateuri => "Only root may
use this option
." })
1161 if $stateuri && $authuser ne 'root@pam';
1163 my $skiplock = extract_param($param, 'skiplock');
1164 raise_param_exc({ skiplock => "Only root may
use this option
." })
1165 if $skiplock && $authuser ne 'root@pam';
1167 my $storecfg = PVE::Storage::config();
1172 syslog('info', "start VM
$vmid: $upid\n");
1174 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1179 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1182 __PACKAGE__->register_method({
1184 path => '{vmid}/status/stop',
1188 description => "Stop virtual machine
.",
1190 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1193 additionalProperties => 0,
1195 node => get_standard_option('pve-node'),
1196 vmid => get_standard_option('pve-vmid'),
1197 skiplock => get_standard_option('skiplock'),
1199 description => "Wait maximal timeout seconds
.",
1205 description => "Do
not decativate storage volumes
.",
1218 my $rpcenv = PVE::RPCEnvironment::get();
1220 my $authuser = $rpcenv->get_user();
1222 my $node = extract_param($param, 'node');
1224 my $vmid = extract_param($param, 'vmid');
1226 my $skiplock = extract_param($param, 'skiplock');
1227 raise_param_exc({ skiplock => "Only root may
use this option
." })
1228 if $skiplock && $authuser ne 'root@pam';
1230 my $keepActive = extract_param($param, 'keepActive');
1231 raise_param_exc({ keepActive => "Only root may
use this option
." })
1232 if $keepActive && $authuser ne 'root@pam';
1234 my $storecfg = PVE::Storage::config();
1239 syslog('info', "stop VM
$vmid: $upid\n");
1241 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1242 $param->{timeout}, 0, 1, $keepActive);
1247 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1250 __PACKAGE__->register_method({
1252 path => '{vmid}/status/reset',
1256 description => "Reset virtual machine
.",
1258 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1261 additionalProperties => 0,
1263 node => get_standard_option('pve-node'),
1264 vmid => get_standard_option('pve-vmid'),
1265 skiplock => get_standard_option('skiplock'),
1274 my $rpcenv = PVE::RPCEnvironment::get();
1276 my $authuser = $rpcenv->get_user();
1278 my $node = extract_param($param, 'node');
1280 my $vmid = extract_param($param, 'vmid');
1282 my $skiplock = extract_param($param, 'skiplock');
1283 raise_param_exc({ skiplock => "Only root may
use this option
." })
1284 if $skiplock && $authuser ne 'root@pam';
1286 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1291 PVE::QemuServer::vm_reset($vmid, $skiplock);
1296 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1299 __PACKAGE__->register_method({
1300 name => 'vm_shutdown',
1301 path => '{vmid}/status/shutdown',
1305 description => "Shutdown virtual machine
.",
1307 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1310 additionalProperties => 0,
1312 node => get_standard_option('pve-node'),
1313 vmid => get_standard_option('pve-vmid'),
1314 skiplock => get_standard_option('skiplock'),
1316 description => "Wait maximal timeout seconds
.",
1322 description => "Make sure the VM stops
.",
1328 description => "Do
not decativate storage volumes
.",
1341 my $rpcenv = PVE::RPCEnvironment::get();
1343 my $authuser = $rpcenv->get_user();
1345 my $node = extract_param($param, 'node');
1347 my $vmid = extract_param($param, 'vmid');
1349 my $skiplock = extract_param($param, 'skiplock');
1350 raise_param_exc({ skiplock => "Only root may
use this option
." })
1351 if $skiplock && $authuser ne 'root@pam';
1353 my $keepActive = extract_param($param, 'keepActive');
1354 raise_param_exc({ keepActive => "Only root may
use this option
." })
1355 if $keepActive && $authuser ne 'root@pam';
1357 my $storecfg = PVE::Storage::config();
1362 syslog('info', "shutdown VM
$vmid: $upid\n");
1364 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1365 1, $param->{forceStop}, $keepActive);
1370 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1373 __PACKAGE__->register_method({
1374 name => 'vm_suspend',
1375 path => '{vmid}/status/suspend',
1379 description => "Suspend virtual machine
.",
1381 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1384 additionalProperties => 0,
1386 node => get_standard_option('pve-node'),
1387 vmid => get_standard_option('pve-vmid'),
1388 skiplock => get_standard_option('skiplock'),
1397 my $rpcenv = PVE::RPCEnvironment::get();
1399 my $authuser = $rpcenv->get_user();
1401 my $node = extract_param($param, 'node');
1403 my $vmid = extract_param($param, 'vmid');
1405 my $skiplock = extract_param($param, 'skiplock');
1406 raise_param_exc({ skiplock => "Only root may
use this option
." })
1407 if $skiplock && $authuser ne 'root@pam';
1409 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1414 syslog('info', "suspend VM
$vmid: $upid\n");
1416 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1421 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1424 __PACKAGE__->register_method({
1425 name => 'vm_resume',
1426 path => '{vmid}/status/resume',
1430 description => "Resume virtual machine
.",
1432 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1435 additionalProperties => 0,
1437 node => get_standard_option('pve-node'),
1438 vmid => get_standard_option('pve-vmid'),
1439 skiplock => get_standard_option('skiplock'),
1448 my $rpcenv = PVE::RPCEnvironment::get();
1450 my $authuser = $rpcenv->get_user();
1452 my $node = extract_param($param, 'node');
1454 my $vmid = extract_param($param, 'vmid');
1456 my $skiplock = extract_param($param, 'skiplock');
1457 raise_param_exc({ skiplock => "Only root may
use this option
." })
1458 if $skiplock && $authuser ne 'root@pam';
1460 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1465 syslog('info', "resume VM
$vmid: $upid\n");
1467 PVE::QemuServer::vm_resume($vmid, $skiplock);
1472 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1475 __PACKAGE__->register_method({
1476 name => 'vm_sendkey',
1477 path => '{vmid}/sendkey',
1481 description => "Send key event to virtual machine
.",
1483 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1486 additionalProperties => 0,
1488 node => get_standard_option('pve-node'),
1489 vmid => get_standard_option('pve-vmid'),
1490 skiplock => get_standard_option('skiplock'),
1492 description => "The key
(qemu monitor encoding
).",
1497 returns => { type => 'null'},
1501 my $rpcenv = PVE::RPCEnvironment::get();
1503 my $authuser = $rpcenv->get_user();
1505 my $node = extract_param($param, 'node');
1507 my $vmid = extract_param($param, 'vmid');
1509 my $skiplock = extract_param($param, 'skiplock');
1510 raise_param_exc({ skiplock => "Only root may
use this option
." })
1511 if $skiplock && $authuser ne 'root@pam';
1513 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1518 __PACKAGE__->register_method({
1519 name => 'migrate_vm',
1520 path => '{vmid}/migrate',
1524 description => "Migrate virtual machine
. Creates a new migration task
.",
1526 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1529 additionalProperties => 0,
1531 node => get_standard_option('pve-node'),
1532 vmid => get_standard_option('pve-vmid'),
1533 target => get_standard_option('pve-node', { description => "Target node
." }),
1536 description => "Use online
/live migration
.",
1541 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1548 description => "the task ID
.",
1553 my $rpcenv = PVE::RPCEnvironment::get();
1555 my $authuser = $rpcenv->get_user();
1557 my $target = extract_param($param, 'target');
1559 my $localnode = PVE::INotify::nodename();
1560 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1562 PVE::Cluster::check_cfs_quorum();
1564 PVE::Cluster::check_node_exists($target);
1566 my $targetip = PVE::Cluster::remote_node_ip($target);
1568 my $vmid = extract_param($param, 'vmid');
1570 raise_param_exc({ force => "Only root may
use this option
." })
1571 if $param->{force} && $authuser ne 'root@pam';
1574 my $conf = PVE::QemuServer::load_config($vmid);
1576 # try to detect errors early
1578 PVE::QemuServer::check_lock($conf);
1580 if (PVE::QemuServer::check_running($vmid)) {
1581 die "cant migrate running VM without
--online
\n"
1582 if !$param->{online};
1588 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1591 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1596 __PACKAGE__->register_method({
1598 path => '{vmid}/monitor',
1602 description => "Execute Qemu monitor commands
.",
1604 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1607 additionalProperties => 0,
1609 node => get_standard_option('pve-node'),
1610 vmid => get_standard_option('pve-vmid'),
1613 description => "The monitor command
.",
1617 returns => { type => 'string'},
1621 my $vmid = $param->{vmid};
1623 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1627 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1629 $res = "ERROR
: $@" if $@;