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 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
57 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
62 # Note: $pool is only needed when creating a VM, because pool permissions
63 # are automatically inherited if VM already exists inside a pool.
64 my $create_disks = sub {
65 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
70 PVE
::QemuServer
::foreach_drive
($settings, sub {
73 my $volid = $disk->{file
};
75 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
76 $res->{$ds} = $settings->{$ds};
77 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
78 my ($storeid, $size) = ($2 || $default_storage, $3);
79 die "no storage ID specified (and no default storage)\n" if !$storeid;
80 my $defformat = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
81 my $fmt = $disk->{format
} || $defformat;
82 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid,
83 $fmt, undef, $size*1024*1024);
84 $disk->{file
} = $volid;
85 push @$vollist, $volid;
86 delete $disk->{format
}; # no longer needed
87 $res->{$ds} = PVE
::QemuServer
::print_drive
($vmid, $disk);
89 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
90 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path));
91 $res->{$ds} = $settings->{$ds};
95 # free allocated images on error
97 syslog
('err', "VM $vmid creating disks failed");
98 foreach my $volid (@$vollist) {
99 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
105 # modify vm config if everything went well
106 foreach my $ds (keys %$res) {
107 $conf->{$ds} = $res->{$ds};
113 my $check_vm_modify_config_perm = sub {
114 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
116 return 1 if $authuser eq 'root@pam';
118 foreach my $opt (@$key_list) {
119 # disk checks need to be done somewhere else
120 next if PVE
::QemuServer
::valid_drivename
($opt);
122 if ($opt eq 'sockets' || $opt eq 'cores' ||
123 $opt eq 'cpu' || $opt eq 'smp' ||
124 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
125 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
126 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
127 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
128 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
129 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
130 } elsif ($opt eq 'args' || $opt eq 'lock') {
131 die "only root can set '$opt' config\n";
132 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
133 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
134 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
135 } elsif ($opt =~ m/^net\d+$/) {
136 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
138 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
145 __PACKAGE__-
>register_method({
149 description
=> "Virtual machine index (per node).",
151 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
155 protected
=> 1, # qemu pid files are only readable by root
157 additionalProperties
=> 0,
159 node
=> get_standard_option
('pve-node'),
168 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
173 my $rpcenv = PVE
::RPCEnvironment
::get
();
174 my $authuser = $rpcenv->get_user();
176 my $vmstatus = PVE
::QemuServer
::vmstatus
();
179 foreach my $vmid (keys %$vmstatus) {
180 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
182 my $data = $vmstatus->{$vmid};
183 $data->{vmid
} = $vmid;
190 __PACKAGE__-
>register_method({
194 description
=> "Create or restore a virtual machine.",
196 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.",
198 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
199 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
205 additionalProperties
=> 0,
206 properties
=> PVE
::QemuServer
::json_config_properties
(
208 node
=> get_standard_option
('pve-node'),
209 vmid
=> get_standard_option
('pve-vmid'),
211 description
=> "The backup file.",
216 storage
=> get_standard_option
('pve-storage-id', {
217 description
=> "Default storage.",
223 description
=> "Allow to overwrite existing VM.",
224 requires
=> 'archive',
229 description
=> "Assign a unique random ethernet address.",
230 requires
=> 'archive',
234 type
=> 'string', format
=> 'pve-poolid',
235 description
=> "Add the VM to the specified pool.",
245 my $rpcenv = PVE
::RPCEnvironment
::get
();
247 my $authuser = $rpcenv->get_user();
249 my $node = extract_param
($param, 'node');
251 my $vmid = extract_param
($param, 'vmid');
253 my $archive = extract_param
($param, 'archive');
255 my $storage = extract_param
($param, 'storage');
257 my $force = extract_param
($param, 'force');
259 my $unique = extract_param
($param, 'unique');
261 my $pool = extract_param
($param, 'pool');
263 my $filename = PVE
::QemuServer
::config_file
($vmid);
265 my $storecfg = PVE
::Storage
::config
();
267 PVE
::Cluster
::check_cfs_quorum
();
269 if (defined($pool)) {
270 $rpcenv->check_pool_exist($pool);
271 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
274 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
275 if defined($storage);
278 &$resolve_cdrom_alias($param);
280 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
282 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
284 foreach my $opt (keys %$param) {
285 if (PVE
::QemuServer
::valid_drivename
($opt)) {
286 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
287 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
289 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
290 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
294 PVE
::QemuServer
::add_random_macs
($param);
296 my $keystr = join(' ', keys %$param);
297 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
299 if ($archive eq '-') {
300 die "pipe requires cli environment\n"
301 && $rpcenv->{type
} ne 'cli';
303 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
304 die "can't find archive file '$archive'\n" if !($path && -f
$path);
309 my $addVMtoPoolFn = sub {
310 my $usercfg = cfs_read_file
("user.cfg");
311 if (my $data = $usercfg->{pools
}->{$pool}) {
312 $data->{vms
}->{$vmid} = 1;
313 $usercfg->{vms
}->{$vmid} = $pool;
314 cfs_write_file
("user.cfg", $usercfg);
318 my $restorefn = sub {
321 die "unable to restore vm $vmid: config file already exists\n"
324 die "unable to restore vm $vmid: vm is running\n"
325 if PVE
::QemuServer
::check_running
($vmid);
327 # destroy existing data - keep empty config
328 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
332 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
335 unique
=> $unique });
337 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
340 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
345 # second test (after locking test is accurate)
346 die "unable to create vm $vmid: config file already exists\n"
357 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
359 # try to be smart about bootdisk
360 my @disks = PVE
::QemuServer
::disknames
();
362 foreach my $ds (reverse @disks) {
363 next if !$conf->{$ds};
364 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
365 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
369 if (!$conf->{bootdisk
} && $firstdisk) {
370 $conf->{bootdisk
} = $firstdisk;
373 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
379 foreach my $volid (@$vollist) {
380 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
383 die "create failed - $err";
386 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
389 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
392 return PVE
::QemuServer
::lock_config
($vmid, $archive ?
$restorefn : $createfn);
395 __PACKAGE__-
>register_method({
400 description
=> "Directory index",
405 additionalProperties
=> 0,
407 node
=> get_standard_option
('pve-node'),
408 vmid
=> get_standard_option
('pve-vmid'),
416 subdir
=> { type
=> 'string' },
419 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
425 { subdir
=> 'config' },
426 { subdir
=> 'status' },
427 { subdir
=> 'unlink' },
428 { subdir
=> 'vncproxy' },
429 { subdir
=> 'migrate' },
431 { subdir
=> 'rrddata' },
432 { subdir
=> 'monitor' },
438 __PACKAGE__-
>register_method({
440 path
=> '{vmid}/rrd',
442 protected
=> 1, # fixme: can we avoid that?
444 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
446 description
=> "Read VM RRD statistics (returns PNG)",
448 additionalProperties
=> 0,
450 node
=> get_standard_option
('pve-node'),
451 vmid
=> get_standard_option
('pve-vmid'),
453 description
=> "Specify the time frame you are interested in.",
455 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
458 description
=> "The list of datasources you want to display.",
459 type
=> 'string', format
=> 'pve-configid-list',
462 description
=> "The RRD consolidation function",
464 enum
=> [ 'AVERAGE', 'MAX' ],
472 filename
=> { type
=> 'string' },
478 return PVE
::Cluster
::create_rrd_graph
(
479 "pve2-vm/$param->{vmid}", $param->{timeframe
},
480 $param->{ds
}, $param->{cf
});
484 __PACKAGE__-
>register_method({
486 path
=> '{vmid}/rrddata',
488 protected
=> 1, # fixme: can we avoid that?
490 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
492 description
=> "Read VM RRD statistics",
494 additionalProperties
=> 0,
496 node
=> get_standard_option
('pve-node'),
497 vmid
=> get_standard_option
('pve-vmid'),
499 description
=> "Specify the time frame you are interested in.",
501 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
504 description
=> "The RRD consolidation function",
506 enum
=> [ 'AVERAGE', 'MAX' ],
521 return PVE
::Cluster
::create_rrd_data
(
522 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
526 __PACKAGE__-
>register_method({
528 path
=> '{vmid}/config',
531 description
=> "Get virtual machine configuration.",
533 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
536 additionalProperties
=> 0,
538 node
=> get_standard_option
('pve-node'),
539 vmid
=> get_standard_option
('pve-vmid'),
547 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
554 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
559 my $vm_is_volid_owner = sub {
560 my ($storecfg, $vmid, $volid) =@_;
562 if ($volid !~ m
|^/|) {
564 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
565 if ($owner && ($owner == $vmid)) {
573 my $test_deallocate_drive = sub {
574 my ($storecfg, $vmid, $key, $drive, $force) = @_;
576 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
577 my $volid = $drive->{file
};
578 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
579 if ($force || $key =~ m/^unused/) {
580 my $sid = PVE
::Storage
::parse_volume_id
($volid);
589 my $delete_drive = sub {
590 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
592 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
593 my $volid = $drive->{file
};
594 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
595 if ($force || $key =~ m/^unused/) {
596 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
599 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
604 delete $conf->{$key};
607 my $vmconfig_delete_option = sub {
608 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
610 return if !defined($conf->{$opt});
612 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
615 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
617 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
618 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
619 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
623 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
626 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
627 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
629 delete $conf->{$opt};
632 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
635 my $vmconfig_update_disk = sub {
636 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
638 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
640 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
641 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
643 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
648 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
650 my $media = $drive->{media
} || 'disk';
651 my $oldmedia = $old_drive->{media
} || 'disk';
652 die "unable to change media type\n" if $media ne $oldmedia;
654 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
655 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
657 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
658 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
663 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
664 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
666 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
667 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
669 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
671 if (PVE
::QemuServer
::check_running
($vmid)) {
672 if ($drive->{file
} eq 'none') {
673 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0);
675 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
676 PVE
::QemuServer
::vm_monitor_command
($vmid, "eject -f drive-$opt", 0); #force eject if locked
677 PVE
::QemuServer
::vm_monitor_command
($vmid, "change drive-$opt \"$path\"", 0) if $path;
681 } else { # hotplug new disks
683 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
687 my $vmconfig_update_net = sub {
688 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
691 #if online update, then unplug first
692 die "error hot-unplug
$opt for update
" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
695 $conf->{$opt} = $value;
696 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
697 $conf = PVE::QemuServer::load_config($vmid); # update/reload
699 my $net = PVE::QemuServer::parse_net($conf->{$opt});
701 die "error hotplug
$opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
704 my $vm_config_perm_list = [
714 __PACKAGE__->register_method({
716 path => '{vmid}/config',
720 description => "Set virtual machine options
.",
722 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
725 additionalProperties => 0,
726 properties => PVE::QemuServer::json_config_properties(
728 node => get_standard_option('pve-node'),
729 vmid => get_standard_option('pve-vmid'),
730 skiplock => get_standard_option('skiplock'),
732 type => 'string', format => 'pve-configid-list',
733 description => "A list of settings you want to
delete.",
738 description => $opt_force_description,
740 requires => 'delete',
744 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
750 returns => { type => 'null'},
754 my $rpcenv = PVE::RPCEnvironment::get();
756 my $authuser = $rpcenv->get_user();
758 my $node = extract_param($param, 'node');
760 my $vmid = extract_param($param, 'vmid');
762 my $digest = extract_param($param, 'digest');
764 my @paramarr = (); # used for log message
765 foreach my $key (keys %$param) {
766 push @paramarr, "-$key", $param->{$key};
769 my $skiplock = extract_param($param, 'skiplock');
770 raise_param_exc({ skiplock => "Only root may
use this option
." })
771 if $skiplock && $authuser ne 'root@pam';
773 my $delete_str = extract_param($param, 'delete');
775 my $force = extract_param($param, 'force');
777 die "no options specified
\n" if !$delete_str && !scalar(keys %$param);
779 my $storecfg = PVE::Storage::config();
781 &$resolve_cdrom_alias($param);
783 # now try to verify all parameters
786 foreach my $opt (PVE::Tools::split_list($delete_str)) {
787 $opt = 'ide2' if $opt eq 'cdrom';
788 raise_param_exc({ delete => "you can
't use '-$opt' and " .
789 "-delete $opt' at the same
time" })
790 if defined($param->{$opt});
792 if (!PVE::QemuServer::option_exists($opt)) {
793 raise_param_exc({ delete => "unknown option
'$opt'" });
799 foreach my $opt (keys %$param) {
800 if (PVE::QemuServer::valid_drivename($opt)) {
802 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
803 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
804 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
805 } elsif ($opt =~ m/^net(\d+)$/) {
807 my $net = PVE::QemuServer::parse_net($param->{$opt});
808 $param->{$opt} = PVE::QemuServer::print_net($net);
812 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
814 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
816 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
820 my $conf = PVE::QemuServer::load_config($vmid);
822 die "checksum missmatch
(file change by other user?
)\n"
823 if $digest && $digest ne $conf->{digest};
825 PVE::QemuServer::check_lock($conf) if !$skiplock;
827 PVE::Cluster::log_msg('info', $authuser, "update VM
$vmid: " . join (' ', @paramarr));
829 foreach my $opt (@delete) { # delete
830 $conf = PVE::QemuServer::load_config($vmid); # update/reload
831 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
834 foreach my $opt (keys %$param) { # add/change
836 $conf = PVE::QemuServer::load_config($vmid); # update/reload
838 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
840 if (PVE::QemuServer::valid_drivename($opt)) {
842 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
843 $opt, $param->{$opt}, $force);
845 } elsif ($opt =~ m/^net(\d+)$/) { #nics
847 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
848 $opt, $param->{$opt});
852 $conf->{$opt} = $param->{$opt};
853 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
858 PVE::QemuServer::lock_config($vmid, $updatefn);
864 __PACKAGE__->register_method({
865 name => 'destroy_vm',
870 description => "Destroy the vm
(also
delete all used
/owned volumes
).",
872 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
875 additionalProperties => 0,
877 node => get_standard_option('pve-node'),
878 vmid => get_standard_option('pve-vmid'),
879 skiplock => get_standard_option('skiplock'),
888 my $rpcenv = PVE::RPCEnvironment::get();
890 my $authuser = $rpcenv->get_user();
892 my $vmid = $param->{vmid};
894 my $skiplock = $param->{skiplock};
895 raise_param_exc({ skiplock => "Only root may
use this option
." })
896 if $skiplock && $authuser ne 'root@pam';
899 my $conf = PVE::QemuServer::load_config($vmid);
901 my $storecfg = PVE::Storage::config();
903 my $delVMfromPoolFn = sub {
904 my $usercfg = cfs_read_file("user
.cfg
");
905 my $pool = $usercfg->{vms}->{$vmid};
906 if (my $data = $usercfg->{pools}->{$pool}) {
907 delete $data->{vms}->{$vmid};
908 delete $usercfg->{vms}->{$vmid};
909 cfs_write_file("user
.cfg
", $usercfg);
916 syslog('info', "destroy VM
$vmid: $upid\n");
918 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
920 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed
");
923 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
926 __PACKAGE__->register_method({
928 path => '{vmid}/unlink',
932 description => "Unlink
/delete disk images
.",
934 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
937 additionalProperties => 0,
939 node => get_standard_option('pve-node'),
940 vmid => get_standard_option('pve-vmid'),
942 type => 'string', format => 'pve-configid-list',
943 description => "A list of disk IDs you want to
delete.",
947 description => $opt_force_description,
952 returns => { type => 'null'},
956 $param->{delete} = extract_param($param, 'idlist');
958 __PACKAGE__->update_vm($param);
965 __PACKAGE__->register_method({
967 path => '{vmid}/vncproxy',
971 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
973 description => "Creates a TCP VNC proxy connections
.",
975 additionalProperties => 0,
977 node => get_standard_option('pve-node'),
978 vmid => get_standard_option('pve-vmid'),
982 additionalProperties => 0,
984 user => { type => 'string' },
985 ticket => { type => 'string' },
986 cert => { type => 'string' },
987 port => { type => 'integer' },
988 upid => { type => 'string' },
994 my $rpcenv = PVE::RPCEnvironment::get();
996 my $authuser = $rpcenv->get_user();
998 my $vmid = $param->{vmid};
999 my $node = $param->{node};
1001 my $authpath = "/vms/$vmid";
1003 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1005 $sslcert = PVE::Tools::file_get_contents("/etc/pve
/pve-root-ca
.pem
", 8192)
1008 my $port = PVE::Tools::next_vnc_port();
1012 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1013 $remip = PVE::Cluster::remote_node_ip($node);
1016 # NOTE: kvm VNC traffic is already TLS encrypted,
1017 # so we select the fastest chipher here (or 'none'?)
1018 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1019 '-c', 'blowfish-cbc', $remip] : [];
1026 syslog('info', "starting vnc proxy
$upid\n");
1028 my $qmcmd = [@$remcmd, "/usr/sbin
/qm
", 'vncproxy', $vmid];
1030 my $qmstr = join(' ', @$qmcmd);
1032 # also redirect stderr (else we get RFB protocol errors)
1033 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null
"];
1035 PVE::Tools::run_command($cmd);
1040 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1051 __PACKAGE__->register_method({
1053 path => '{vmid}/status',
1056 description => "Directory
index",
1061 additionalProperties => 0,
1063 node => get_standard_option('pve-node'),
1064 vmid => get_standard_option('pve-vmid'),
1072 subdir => { type => 'string' },
1075 links => [ { rel => 'child', href => "{subdir
}" } ],
1081 my $conf = PVE::QemuServer::load_config($param->{vmid});
1084 { subdir => 'current' },
1085 { subdir => 'start' },
1086 { subdir => 'stop' },
1092 __PACKAGE__->register_method({
1093 name => 'vm_status',
1094 path => '{vmid}/status/current',
1097 protected => 1, # qemu pid files are only readable by root
1098 description => "Get virtual machine status
.",
1100 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1103 additionalProperties => 0,
1105 node => get_standard_option('pve-node'),
1106 vmid => get_standard_option('pve-vmid'),
1109 returns => { type => 'object' },
1114 my $conf = PVE::QemuServer::load_config($param->{vmid});
1116 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1117 my $status = $vmstatus->{$param->{vmid}};
1119 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1120 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1129 __PACKAGE__->register_method({
1131 path => '{vmid}/status/start',
1135 description => "Start virtual machine
.",
1137 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1140 additionalProperties => 0,
1142 node => get_standard_option('pve-node'),
1143 vmid => get_standard_option('pve-vmid'),
1144 skiplock => get_standard_option('skiplock'),
1145 stateuri => get_standard_option('pve-qm-stateuri'),
1154 my $rpcenv = PVE::RPCEnvironment::get();
1156 my $authuser = $rpcenv->get_user();
1158 my $node = extract_param($param, 'node');
1160 my $vmid = extract_param($param, 'vmid');
1162 my $stateuri = extract_param($param, 'stateuri');
1163 raise_param_exc({ stateuri => "Only root may
use this option
." })
1164 if $stateuri && $authuser ne 'root@pam';
1166 my $skiplock = extract_param($param, 'skiplock');
1167 raise_param_exc({ skiplock => "Only root may
use this option
." })
1168 if $skiplock && $authuser ne 'root@pam';
1170 my $storecfg = PVE::Storage::config();
1175 syslog('info', "start VM
$vmid: $upid\n");
1177 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1182 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1185 __PACKAGE__->register_method({
1187 path => '{vmid}/status/stop',
1191 description => "Stop virtual machine
.",
1193 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1196 additionalProperties => 0,
1198 node => get_standard_option('pve-node'),
1199 vmid => get_standard_option('pve-vmid'),
1200 skiplock => get_standard_option('skiplock'),
1202 description => "Wait maximal timeout seconds
.",
1208 description => "Do
not decativate storage volumes
.",
1221 my $rpcenv = PVE::RPCEnvironment::get();
1223 my $authuser = $rpcenv->get_user();
1225 my $node = extract_param($param, 'node');
1227 my $vmid = extract_param($param, 'vmid');
1229 my $skiplock = extract_param($param, 'skiplock');
1230 raise_param_exc({ skiplock => "Only root may
use this option
." })
1231 if $skiplock && $authuser ne 'root@pam';
1233 my $keepActive = extract_param($param, 'keepActive');
1234 raise_param_exc({ keepActive => "Only root may
use this option
." })
1235 if $keepActive && $authuser ne 'root@pam';
1237 my $storecfg = PVE::Storage::config();
1242 syslog('info', "stop VM
$vmid: $upid\n");
1244 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1245 $param->{timeout}, 0, 1, $keepActive);
1250 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1253 __PACKAGE__->register_method({
1255 path => '{vmid}/status/reset',
1259 description => "Reset virtual machine
.",
1261 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1264 additionalProperties => 0,
1266 node => get_standard_option('pve-node'),
1267 vmid => get_standard_option('pve-vmid'),
1268 skiplock => get_standard_option('skiplock'),
1277 my $rpcenv = PVE::RPCEnvironment::get();
1279 my $authuser = $rpcenv->get_user();
1281 my $node = extract_param($param, 'node');
1283 my $vmid = extract_param($param, 'vmid');
1285 my $skiplock = extract_param($param, 'skiplock');
1286 raise_param_exc({ skiplock => "Only root may
use this option
." })
1287 if $skiplock && $authuser ne 'root@pam';
1289 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1294 PVE::QemuServer::vm_reset($vmid, $skiplock);
1299 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1302 __PACKAGE__->register_method({
1303 name => 'vm_shutdown',
1304 path => '{vmid}/status/shutdown',
1308 description => "Shutdown virtual machine
.",
1310 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1313 additionalProperties => 0,
1315 node => get_standard_option('pve-node'),
1316 vmid => get_standard_option('pve-vmid'),
1317 skiplock => get_standard_option('skiplock'),
1319 description => "Wait maximal timeout seconds
.",
1325 description => "Make sure the VM stops
.",
1331 description => "Do
not decativate storage volumes
.",
1344 my $rpcenv = PVE::RPCEnvironment::get();
1346 my $authuser = $rpcenv->get_user();
1348 my $node = extract_param($param, 'node');
1350 my $vmid = extract_param($param, 'vmid');
1352 my $skiplock = extract_param($param, 'skiplock');
1353 raise_param_exc({ skiplock => "Only root may
use this option
." })
1354 if $skiplock && $authuser ne 'root@pam';
1356 my $keepActive = extract_param($param, 'keepActive');
1357 raise_param_exc({ keepActive => "Only root may
use this option
." })
1358 if $keepActive && $authuser ne 'root@pam';
1360 my $storecfg = PVE::Storage::config();
1365 syslog('info', "shutdown VM
$vmid: $upid\n");
1367 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1368 1, $param->{forceStop}, $keepActive);
1373 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1376 __PACKAGE__->register_method({
1377 name => 'vm_suspend',
1378 path => '{vmid}/status/suspend',
1382 description => "Suspend virtual machine
.",
1384 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1387 additionalProperties => 0,
1389 node => get_standard_option('pve-node'),
1390 vmid => get_standard_option('pve-vmid'),
1391 skiplock => get_standard_option('skiplock'),
1400 my $rpcenv = PVE::RPCEnvironment::get();
1402 my $authuser = $rpcenv->get_user();
1404 my $node = extract_param($param, 'node');
1406 my $vmid = extract_param($param, 'vmid');
1408 my $skiplock = extract_param($param, 'skiplock');
1409 raise_param_exc({ skiplock => "Only root may
use this option
." })
1410 if $skiplock && $authuser ne 'root@pam';
1412 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1417 syslog('info', "suspend VM
$vmid: $upid\n");
1419 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1424 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1427 __PACKAGE__->register_method({
1428 name => 'vm_resume',
1429 path => '{vmid}/status/resume',
1433 description => "Resume virtual machine
.",
1435 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1438 additionalProperties => 0,
1440 node => get_standard_option('pve-node'),
1441 vmid => get_standard_option('pve-vmid'),
1442 skiplock => get_standard_option('skiplock'),
1451 my $rpcenv = PVE::RPCEnvironment::get();
1453 my $authuser = $rpcenv->get_user();
1455 my $node = extract_param($param, 'node');
1457 my $vmid = extract_param($param, 'vmid');
1459 my $skiplock = extract_param($param, 'skiplock');
1460 raise_param_exc({ skiplock => "Only root may
use this option
." })
1461 if $skiplock && $authuser ne 'root@pam';
1463 die "VM
$vmid not running
\n" if !PVE::QemuServer::check_running($vmid);
1468 syslog('info', "resume VM
$vmid: $upid\n");
1470 PVE::QemuServer::vm_resume($vmid, $skiplock);
1475 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1478 __PACKAGE__->register_method({
1479 name => 'vm_sendkey',
1480 path => '{vmid}/sendkey',
1484 description => "Send key event to virtual machine
.",
1486 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1489 additionalProperties => 0,
1491 node => get_standard_option('pve-node'),
1492 vmid => get_standard_option('pve-vmid'),
1493 skiplock => get_standard_option('skiplock'),
1495 description => "The key
(qemu monitor encoding
).",
1500 returns => { type => 'null'},
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 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1521 __PACKAGE__->register_method({
1522 name => 'migrate_vm',
1523 path => '{vmid}/migrate',
1527 description => "Migrate virtual machine
. Creates a new migration task
.",
1529 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1532 additionalProperties => 0,
1534 node => get_standard_option('pve-node'),
1535 vmid => get_standard_option('pve-vmid'),
1536 target => get_standard_option('pve-node', { description => "Target node
." }),
1539 description => "Use online
/live migration
.",
1544 description => "Allow to migrate VMs which
use local devices
. Only root may
use this option
.",
1551 description => "the task ID
.",
1556 my $rpcenv = PVE::RPCEnvironment::get();
1558 my $authuser = $rpcenv->get_user();
1560 my $target = extract_param($param, 'target');
1562 my $localnode = PVE::INotify::nodename();
1563 raise_param_exc({ target => "target
is local node
."}) if $target eq $localnode;
1565 PVE::Cluster::check_cfs_quorum();
1567 PVE::Cluster::check_node_exists($target);
1569 my $targetip = PVE::Cluster::remote_node_ip($target);
1571 my $vmid = extract_param($param, 'vmid');
1573 raise_param_exc({ force => "Only root may
use this option
." })
1574 if $param->{force} && $authuser ne 'root@pam';
1577 my $conf = PVE::QemuServer::load_config($vmid);
1579 # try to detect errors early
1581 PVE::QemuServer::check_lock($conf);
1583 if (PVE::QemuServer::check_running($vmid)) {
1584 die "cant migrate running VM without
--online
\n"
1585 if !$param->{online};
1591 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1594 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1599 __PACKAGE__->register_method({
1601 path => '{vmid}/monitor',
1605 description => "Execute Qemu monitor commands
.",
1607 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1610 additionalProperties => 0,
1612 node => get_standard_option('pve-node'),
1613 vmid => get_standard_option('pve-vmid'),
1616 description => "The monitor command
.",
1620 returns => { type => 'string'},
1624 my $vmid = $param->{vmid};
1626 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1630 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1632 $res = "ERROR
: $@" if $@;