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 eq 'cdrom')) {
50 $rpcenv->check($authuser, "/", ['Sys.Console']);
51 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
52 my ($storeid, $size) = ($2 || $default_storage, $3);
53 die "no storage ID specified (and no default storage)\n" if !$storeid;
54 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
56 $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
61 # Note: $pool is only needed when creating a VM, because pool permissions
62 # are automatically inherited if VM already exists inside a pool.
63 my $create_disks = sub {
64 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
69 PVE
::QemuServer
::foreach_drive
($settings, sub {
72 my $volid = $disk->{file
};
74 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
75 $res->{$ds} = $settings->{$ds};
76 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
77 my ($storeid, $size) = ($2 || $default_storage, $3);
78 die "no storage ID specified (and no default storage)\n" if !$storeid;
79 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
80 my $fmt = $disk->{format
} || $defformat;
81 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
82 $fmt, undef, $size*1024*1024);
83 $disk->{file
} = $volid;
84 push @$vollist, $volid;
85 delete $disk->{format
}; # no longer needed
86 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
88 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
89 PVE
::Storage
::activate_volumes
($storecfg, [ $volid ])
90 if PVE
::Storage
::parse_volume_id
($volid, 1);
91 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
92 $res->{$ds} = $settings->{$ds};
96 # free allocated images on error
98 syslog
('err', "VM $vmid creating disks failed");
99 foreach my $volid (@$vollist) {
100 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
106 # modify vm config if everything went well
107 foreach my $ds (keys %$res) {
108 $conf->{$ds} = $res->{$ds};
114 my $check_vm_modify_config_perm = sub {
115 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
117 return 1 if $authuser eq 'root@pam';
119 foreach my $opt (@$key_list) {
120 # disk checks need to be done somewhere else
121 next if PVE
::QemuServer
::valid_drivename
($opt);
123 if ($opt eq 'sockets' || $opt eq 'cores' ||
124 $opt eq 'cpu' || $opt eq 'smp' ||
125 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
126 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
127 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
128 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
129 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
130 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
131 } elsif ($opt eq 'args' || $opt eq 'lock') {
132 die "only root can set '$opt' config\n";
133 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
134 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
135 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
136 } elsif ($opt =~ m/^net\d+$/) {
137 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
139 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
146 __PACKAGE__-
>register_method({
150 description
=> "Virtual machine index (per node).",
152 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
156 protected
=> 1, # qemu pid files are only readable by root
158 additionalProperties
=> 0,
160 node
=> get_standard_option
('pve-node'),
169 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
174 my $rpcenv = PVE
::RPCEnvironment
::get
();
175 my $authuser = $rpcenv->get_user();
177 my $vmstatus = PVE
::QemuServer
::vmstatus
();
180 foreach my $vmid (keys %$vmstatus) {
181 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
183 my $data = $vmstatus->{$vmid};
184 $data->{vmid
} = $vmid;
191 __PACKAGE__-
>register_method({
195 description
=> "Create or restore a virtual machine.",
197 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.",
199 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
200 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
206 additionalProperties
=> 0,
207 properties
=> PVE
::QemuServer
::json_config_properties
(
209 node
=> get_standard_option
('pve-node'),
210 vmid
=> get_standard_option
('pve-vmid'),
212 description
=> "The backup file.",
217 storage
=> get_standard_option
('pve-storage-id', {
218 description
=> "Default storage.",
224 description
=> "Allow to overwrite existing VM.",
225 requires
=> 'archive',
230 description
=> "Assign a unique random ethernet address.",
231 requires
=> 'archive',
235 type
=> 'string', format
=> 'pve-poolid',
236 description
=> "Add the VM to the specified pool.",
246 my $rpcenv = PVE
::RPCEnvironment
::get
();
248 my $authuser = $rpcenv->get_user();
250 my $node = extract_param
($param, 'node');
252 my $vmid = extract_param
($param, 'vmid');
254 my $archive = extract_param
($param, 'archive');
256 my $storage = extract_param
($param, 'storage');
258 my $force = extract_param
($param, 'force');
260 my $unique = extract_param
($param, 'unique');
262 my $pool = extract_param
($param, 'pool');
264 my $filename = PVE
::QemuServer
::config_file
($vmid);
266 my $storecfg = PVE
::Storage
::config
();
268 PVE
::Cluster
::check_cfs_quorum
();
270 if (defined($pool)) {
271 $rpcenv->check_pool_exist($pool);
272 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
275 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
276 if defined($storage);
279 &$resolve_cdrom_alias($param);
281 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
283 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
285 foreach my $opt (keys %$param) {
286 if (PVE
::QemuServer
::valid_drivename
($opt)) {
287 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
288 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
290 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
291 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
295 PVE
::QemuServer
::add_random_macs
($param);
297 my $keystr = join(' ', keys %$param);
298 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
300 if ($archive eq '-') {
301 die "pipe requires cli environment\n"
302 if $rpcenv->{type
} ne 'cli';
304 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
306 PVE
::Storage
::activate_volumes
($storecfg, [ $archive ])
307 if PVE
::Storage
::parse_volume_id
($archive, 1);
309 die "can't find archive file '$archive'\n" if !($path && -f
$path);
314 my $addVMtoPoolFn = sub {
315 my $usercfg = cfs_read_file
("user.cfg");
316 if (my $data = $usercfg->{pools
}->{$pool}) {
317 $data->{vms
}->{$vmid} = 1;
318 $usercfg->{vms
}->{$vmid} = $pool;
319 cfs_write_file
("user.cfg", $usercfg);
323 my $restorefn = sub {
326 die "unable to restore vm $vmid: config file already exists\n"
329 die "unable to restore vm $vmid: vm is running\n"
330 if PVE
::QemuServer
::check_running
($vmid);
332 # destroy existing data - keep empty config
333 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
337 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
340 unique
=> $unique });
342 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
345 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
351 die "unable to create vm $vmid: config file already exists\n"
362 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
364 # try to be smart about bootdisk
365 my @disks = PVE
::QemuServer
::disknames
();
367 foreach my $ds (reverse @disks) {
368 next if !$conf->{$ds};
369 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
370 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
374 if (!$conf->{bootdisk
} && $firstdisk) {
375 $conf->{bootdisk
} = $firstdisk;
378 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
384 foreach my $volid (@$vollist) {
385 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
388 die "create failed - $err";
391 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
394 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
397 return PVE
::QemuServer
::lock_config_full
($vmid, 1, $archive ?
$restorefn : $createfn);
400 __PACKAGE__-
>register_method({
405 description
=> "Directory index",
410 additionalProperties
=> 0,
412 node
=> get_standard_option
('pve-node'),
413 vmid
=> get_standard_option
('pve-vmid'),
421 subdir
=> { type
=> 'string' },
424 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
430 { subdir
=> 'config' },
431 { subdir
=> 'status' },
432 { subdir
=> 'unlink' },
433 { subdir
=> 'vncproxy' },
434 { subdir
=> 'migrate' },
436 { subdir
=> 'rrddata' },
437 { subdir
=> 'monitor' },
443 __PACKAGE__-
>register_method({
445 path
=> '{vmid}/rrd',
447 protected
=> 1, # fixme: can we avoid that?
449 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
451 description
=> "Read VM RRD statistics (returns PNG)",
453 additionalProperties
=> 0,
455 node
=> get_standard_option
('pve-node'),
456 vmid
=> get_standard_option
('pve-vmid'),
458 description
=> "Specify the time frame you are interested in.",
460 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
463 description
=> "The list of datasources you want to display.",
464 type
=> 'string', format
=> 'pve-configid-list',
467 description
=> "The RRD consolidation function",
469 enum
=> [ 'AVERAGE', 'MAX' ],
477 filename
=> { type
=> 'string' },
483 return PVE
::Cluster
::create_rrd_graph
(
484 "pve2-vm/$param->{vmid}", $param->{timeframe
},
485 $param->{ds
}, $param->{cf
});
489 __PACKAGE__-
>register_method({
491 path
=> '{vmid}/rrddata',
493 protected
=> 1, # fixme: can we avoid that?
495 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
497 description
=> "Read VM RRD statistics",
499 additionalProperties
=> 0,
501 node
=> get_standard_option
('pve-node'),
502 vmid
=> get_standard_option
('pve-vmid'),
504 description
=> "Specify the time frame you are interested in.",
506 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
509 description
=> "The RRD consolidation function",
511 enum
=> [ 'AVERAGE', 'MAX' ],
526 return PVE
::Cluster
::create_rrd_data
(
527 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
531 __PACKAGE__-
>register_method({
533 path
=> '{vmid}/config',
536 description
=> "Get virtual machine configuration.",
538 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
541 additionalProperties
=> 0,
543 node
=> get_standard_option
('pve-node'),
544 vmid
=> get_standard_option
('pve-vmid'),
552 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
559 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
564 my $vm_is_volid_owner = sub {
565 my ($storecfg, $vmid, $volid) =@_;
567 if ($volid !~ m
|^/|) {
569 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
570 if ($owner && ($owner == $vmid)) {
578 my $test_deallocate_drive = sub {
579 my ($storecfg, $vmid, $key, $drive, $force) = @_;
581 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
582 my $volid = $drive->{file
};
583 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
584 if ($force || $key =~ m/^unused/) {
585 my $sid = PVE
::Storage
::parse_volume_id
($volid);
594 my $delete_drive = sub {
595 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
597 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
598 my $volid = $drive->{file
};
599 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
600 if ($force || $key =~ m/^unused/) {
601 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
604 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
609 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($authuser, "/storage/$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, $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();
908 my $delVMfromPoolFn = sub {
909 my $usercfg = cfs_read_file("user
.cfg
");
910 if (my $pool = $usercfg->{vms}->{$vmid}) {
911 if (my $data = $usercfg->{pools}->{$pool}) {
912 delete $data->{vms}->{$vmid};
913 delete $usercfg->{vms}->{$vmid};
914 cfs_write_file("user
.cfg
", $usercfg);
922 syslog('info', "destroy VM
$vmid: $upid\n");
924 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
926 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed
");
929 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
932 __PACKAGE__->register_method({
934 path => '{vmid}/unlink',
938 description => "Unlink
/delete disk images
.",
940 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
943 additionalProperties => 0,
945 node => get_standard_option('pve-node'),
946 vmid => get_standard_option('pve-vmid'),
948 type => 'string', format => 'pve-configid-list',
949 description => "A list of disk IDs you want to
delete.",
953 description => $opt_force_description,
958 returns => { type => 'null'},
962 $param->{delete} = extract_param($param, 'idlist');
964 __PACKAGE__->update_vm($param);
971 __PACKAGE__->register_method({
973 path => '{vmid}/vncproxy',
977 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
979 description => "Creates a TCP VNC proxy connections
.",
981 additionalProperties => 0,
983 node => get_standard_option('pve-node'),
984 vmid => get_standard_option('pve-vmid'),
988 additionalProperties => 0,
990 user => { type => 'string' },
991 ticket => { type => 'string' },
992 cert => { type => 'string' },
993 port => { type => 'integer' },
994 upid => { type => 'string' },
1000 my $rpcenv = PVE::RPCEnvironment::get();
1002 my $authuser = $rpcenv->get_user();
1004 my $vmid = $param->{vmid};
1005 my $node = $param->{node};
1007 my $authpath = "/vms/$vmid";
1009 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1011 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
1014 my $port = PVE::Tools::next_vnc_port();
1018 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1019 $remip = PVE::Cluster::remote_node_ip($node);
1022 # NOTE: kvm VNC traffic is already TLS encrypted,
1023 # so we select the fastest chipher here (or 'none'?)
1024 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1025 '-c', 'blowfish-cbc', $remip] : [];
1032 syslog('info', "starting vnc proxy
$upid\n");
1034 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
1036 my $qmstr = join(' ', @$qmcmd);
1038 # also redirect stderr (else we get RFB protocol errors)
1039 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
1041 PVE::Tools::run_command($cmd);
1046 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1057 __PACKAGE__->register_method({
1059 path => '{vmid}/status',
1062 description => "Directory
index",
1067 additionalProperties => 0,
1069 node => get_standard_option('pve-node'),
1070 vmid => get_standard_option('pve-vmid'),
1078 subdir => { type => 'string' },
1081 links => [ { rel => 'child', href => "{subdir
}" } ],
1087 my $conf = PVE::QemuServer::load_config($param->{vmid});
1090 { subdir => 'current' },
1091 { subdir => 'start' },
1092 { subdir => 'stop' },
1098 my $vm_is_ha_managed = sub {
1101 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1102 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) {
1108 __PACKAGE__->register_method({
1109 name => 'vm_status',
1110 path => '{vmid}/status/current',
1113 protected => 1, # qemu pid files are only readable by root
1114 description => "Get virtual machine status
.",
1116 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1119 additionalProperties => 0,
1121 node => get_standard_option('pve-node'),
1122 vmid => get_standard_option('pve-vmid'),
1125 returns => { type => 'object' },
1130 my $conf = PVE::QemuServer::load_config($param->{vmid});
1132 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1133 my $status = $vmstatus->{$param->{vmid}};
1135 $status->{ha} = &$vm_is_ha_managed($param->{vmid});
1140 __PACKAGE__->register_method({
1142 path => '{vmid}/status/start',
1146 description => "Start virtual machine
.",
1148 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1151 additionalProperties => 0,
1153 node => get_standard_option('pve-node'),
1154 vmid => get_standard_option('pve-vmid'),
1155 skiplock => get_standard_option('skiplock'),
1156 stateuri => get_standard_option('pve-qm-stateuri'),
1165 my $rpcenv = PVE::RPCEnvironment::get();
1167 my $authuser = $rpcenv->get_user();
1169 my $node = extract_param($param, 'node');
1171 my $vmid = extract_param($param, 'vmid');
1173 my $stateuri = extract_param($param, 'stateuri');
1174 raise_param_exc({ stateuri => "Only root may
use this option
." })
1175 if $stateuri && $authuser ne 'root@pam';
1177 my $skiplock = extract_param($param, 'skiplock');
1178 raise_param_exc({ skiplock => "Only root may
use this option
." })
1179 if $skiplock && $authuser ne 'root@pam';
1181 my $storecfg = PVE::Storage::config();
1183 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1184 $rpcenv->{type} ne 'ha') {
1189 my $service = "pvevm
:$vmid";
1191 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1193 print "Executing HA start
for VM
$vmid\n";
1195 PVE::Tools::run_command($cmd);
1200 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1207 syslog('info', "start VM
$vmid: $upid\n");
1209 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1214 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1218 __PACKAGE__->register_method({
1220 path => '{vmid}/status/stop',
1224 description => "Stop 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 => "Do
not decativate storage volumes
.",
1254 my $rpcenv = PVE::RPCEnvironment::get();
1256 my $authuser = $rpcenv->get_user();
1258 my $node = extract_param($param, 'node');
1260 my $vmid = extract_param($param, 'vmid');
1262 my $skiplock = extract_param($param, 'skiplock');
1263 raise_param_exc({ skiplock => "Only root may
use this option
." })
1264 if $skiplock && $authuser ne 'root@pam';
1266 my $keepActive = extract_param($param, 'keepActive');
1267 raise_param_exc({ keepActive => "Only root may
use this option
." })
1268 if $keepActive && $authuser ne 'root@pam';
1270 my $storecfg = PVE::Storage::config();
1272 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1277 my $service = "pvevm
:$vmid";
1279 my $cmd = ['clusvcadm', '-d', $service];
1281 print "Executing HA stop
for VM
$vmid\n";
1283 PVE::Tools::run_command($cmd);
1288 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1294 syslog('info', "stop VM
$vmid: $upid\n");
1296 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1297 $param->{timeout}, 0, 1, $keepActive);
1302 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1306 __PACKAGE__->register_method({
1308 path => '{vmid}/status/reset',
1312 description => "Reset virtual machine
.",
1314 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1317 additionalProperties => 0,
1319 node => get_standard_option('pve-node'),
1320 vmid => get_standard_option('pve-vmid'),
1321 skiplock => get_standard_option('skiplock'),
1330 my $rpcenv = PVE::RPCEnvironment::get();
1332 my $authuser = $rpcenv->get_user();
1334 my $node = extract_param($param, 'node');
1336 my $vmid = extract_param($param, 'vmid');
1338 my $skiplock = extract_param($param, 'skiplock');
1339 raise_param_exc({ skiplock => "Only root may
use this option
." })
1340 if $skiplock && $authuser ne 'root@pam';
1342 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1347 PVE::QemuServer::vm_reset($vmid, $skiplock);
1352 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1355 __PACKAGE__->register_method({
1356 name => 'vm_shutdown',
1357 path => '{vmid}/status/shutdown',
1361 description => "Shutdown virtual machine
.",
1363 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1366 additionalProperties => 0,
1368 node => get_standard_option('pve-node'),
1369 vmid => get_standard_option('pve-vmid'),
1370 skiplock => get_standard_option('skiplock'),
1372 description => "Wait maximal timeout seconds
.",
1378 description => "Make sure the VM stops
.",
1384 description => "Do
not decativate storage volumes
.",
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 my $keepActive = extract_param($param, 'keepActive');
1410 raise_param_exc({ keepActive => "Only root may
use this option
." })
1411 if $keepActive && $authuser ne 'root@pam';
1413 my $storecfg = PVE::Storage::config();
1418 syslog('info', "shutdown VM
$vmid: $upid\n");
1420 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1421 1, $param->{forceStop}, $keepActive);
1426 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1429 __PACKAGE__->register_method({
1430 name => 'vm_suspend',
1431 path => '{vmid}/status/suspend',
1435 description => "Suspend virtual machine
.",
1437 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1440 additionalProperties => 0,
1442 node => get_standard_option('pve-node'),
1443 vmid => get_standard_option('pve-vmid'),
1444 skiplock => get_standard_option('skiplock'),
1453 my $rpcenv = PVE::RPCEnvironment::get();
1455 my $authuser = $rpcenv->get_user();
1457 my $node = extract_param($param, 'node');
1459 my $vmid = extract_param($param, 'vmid');
1461 my $skiplock = extract_param($param, 'skiplock');
1462 raise_param_exc({ skiplock => "Only root may
use this option
." })
1463 if $skiplock && $authuser ne 'root@pam';
1465 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1470 syslog('info', "suspend VM
$vmid: $upid\n");
1472 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1477 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1480 __PACKAGE__->register_method({
1481 name => 'vm_resume',
1482 path => '{vmid}/status/resume',
1486 description => "Resume virtual machine
.",
1488 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1491 additionalProperties => 0,
1493 node => get_standard_option('pve-node'),
1494 vmid => get_standard_option('pve-vmid'),
1495 skiplock => get_standard_option('skiplock'),
1504 my $rpcenv = PVE::RPCEnvironment::get();
1506 my $authuser = $rpcenv->get_user();
1508 my $node = extract_param($param, 'node');
1510 my $vmid = extract_param($param, 'vmid');
1512 my $skiplock = extract_param($param, 'skiplock');
1513 raise_param_exc({ skiplock => "Only root may
use this option
." })
1514 if $skiplock && $authuser ne 'root@pam';
1516 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1521 syslog('info', "resume VM
$vmid: $upid\n");
1523 PVE::QemuServer::vm_resume($vmid, $skiplock);
1528 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1531 __PACKAGE__->register_method({
1532 name => 'vm_sendkey',
1533 path => '{vmid}/sendkey',
1537 description => "Send key event to virtual machine
.",
1539 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1542 additionalProperties => 0,
1544 node => get_standard_option('pve-node'),
1545 vmid => get_standard_option('pve-vmid'),
1546 skiplock => get_standard_option('skiplock'),
1548 description => "The key
(qemu monitor encoding
).",
1553 returns => { type => 'null'},
1557 my $rpcenv = PVE::RPCEnvironment::get();
1559 my $authuser = $rpcenv->get_user();
1561 my $node = extract_param($param, 'node');
1563 my $vmid = extract_param($param, 'vmid');
1565 my $skiplock = extract_param($param, 'skiplock');
1566 raise_param_exc({ skiplock => "Only root may
use this option
." })
1567 if $skiplock && $authuser ne 'root@pam';
1569 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1574 __PACKAGE__->register_method({
1575 name => 'migrate_vm',
1576 path => '{vmid}/migrate',
1580 description => "Migrate virtual machine
. Creates a new migration task
.",
1582 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1585 additionalProperties => 0,
1587 node => get_standard_option('pve-node'),
1588 vmid => get_standard_option('pve-vmid'),
1589 target => get_standard_option('pve-node', { description => "Target node
." }),
1592 description => "Use online
/live migration
.",
1597 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1604 description => "the task ID
.",
1609 my $rpcenv = PVE::RPCEnvironment::get();
1611 my $authuser = $rpcenv->get_user();
1613 my $target = extract_param($param, 'target');
1615 my $localnode = PVE::INotify::nodename();
1616 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1618 PVE::Cluster::check_cfs_quorum();
1620 PVE::Cluster::check_node_exists($target);
1622 my $targetip = PVE::Cluster::remote_node_ip($target);
1624 my $vmid = extract_param($param, 'vmid');
1626 raise_param_exc({ force => "Only root may
use this option
." })
1627 if $param->{force} && $authuser ne 'root@pam';
1630 my $conf = PVE::QemuServer::load_config($vmid);
1632 # try to detect errors early
1634 PVE::QemuServer::check_lock($conf);
1636 if (PVE::QemuServer::check_running($vmid)) {
1637 die "cant migrate running VM without
--online
\n"
1638 if !$param->{online};
1641 my $storecfg = PVE::Storage::config();
1642 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
1644 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1649 my $service = "pvevm
:$vmid";
1651 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1653 print "Executing HA migrate
for VM
$vmid to node
$target\n";
1655 PVE::Tools::run_command($cmd);
1660 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1667 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1670 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1675 __PACKAGE__->register_method({
1677 path => '{vmid}/monitor',
1681 description => "Execute Qemu monitor commands
.",
1683 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1686 additionalProperties => 0,
1688 node => get_standard_option('pve-node'),
1689 vmid => get_standard_option('pve-vmid'),
1692 description => "The monitor command
.",
1696 returns => { type => 'string'},
1700 my $vmid = $param->{vmid};
1702 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1706 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1708 $res = "ERROR
: $@" if $@;