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);
92 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
93 my $dl = PVE
::Storage
::vdisk_list
($storecfg, $storeid, undef);
94 my $foundvolid = undef;
96 PVE
::Storage
::foreach_volid
($dl, sub {
98 if($volumeid eq $volid) {
104 die "image '$path' does not exists\n" if (!(-f
$path || -b
$path || $foundvolid));
105 $res->{$ds} = $settings->{$ds};
109 # free allocated images on error
111 syslog
('err', "VM $vmid creating disks failed");
112 foreach my $volid (@$vollist) {
113 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
119 # modify vm config if everything went well
120 foreach my $ds (keys %$res) {
121 $conf->{$ds} = $res->{$ds};
127 my $check_vm_modify_config_perm = sub {
128 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
130 return 1 if $authuser eq 'root@pam';
132 foreach my $opt (@$key_list) {
133 # disk checks need to be done somewhere else
134 next if PVE
::QemuServer
::valid_drivename
($opt);
136 if ($opt eq 'sockets' || $opt eq 'cores' ||
137 $opt eq 'cpu' || $opt eq 'smp' ||
138 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
139 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
140 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
141 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
142 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
143 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
144 } elsif ($opt eq 'args' || $opt eq 'lock') {
145 die "only root can set '$opt' config\n";
146 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
147 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
148 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
149 } elsif ($opt =~ m/^net\d+$/) {
150 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
152 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
159 __PACKAGE__-
>register_method({
163 description
=> "Virtual machine index (per node).",
165 description
=> "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
169 protected
=> 1, # qemu pid files are only readable by root
171 additionalProperties
=> 0,
173 node
=> get_standard_option
('pve-node'),
182 links
=> [ { rel
=> 'child', href
=> "{vmid}" } ],
187 my $rpcenv = PVE
::RPCEnvironment
::get
();
188 my $authuser = $rpcenv->get_user();
190 my $vmstatus = PVE
::QemuServer
::vmstatus
();
193 foreach my $vmid (keys %$vmstatus) {
194 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
196 my $data = $vmstatus->{$vmid};
197 $data->{vmid
} = $vmid;
204 __PACKAGE__-
>register_method({
208 description
=> "Create or restore a virtual machine.",
210 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.",
212 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
213 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param
=> 'pool'],
219 additionalProperties
=> 0,
220 properties
=> PVE
::QemuServer
::json_config_properties
(
222 node
=> get_standard_option
('pve-node'),
223 vmid
=> get_standard_option
('pve-vmid'),
225 description
=> "The backup file.",
230 storage
=> get_standard_option
('pve-storage-id', {
231 description
=> "Default storage.",
237 description
=> "Allow to overwrite existing VM.",
238 requires
=> 'archive',
243 description
=> "Assign a unique random ethernet address.",
244 requires
=> 'archive',
248 type
=> 'string', format
=> 'pve-poolid',
249 description
=> "Add the VM to the specified pool.",
259 my $rpcenv = PVE
::RPCEnvironment
::get
();
261 my $authuser = $rpcenv->get_user();
263 my $node = extract_param
($param, 'node');
265 my $vmid = extract_param
($param, 'vmid');
267 my $archive = extract_param
($param, 'archive');
269 my $storage = extract_param
($param, 'storage');
271 my $force = extract_param
($param, 'force');
273 my $unique = extract_param
($param, 'unique');
275 my $pool = extract_param
($param, 'pool');
277 my $filename = PVE
::QemuServer
::config_file
($vmid);
279 my $storecfg = PVE
::Storage
::config
();
281 PVE
::Cluster
::check_cfs_quorum
();
283 if (defined($pool)) {
284 $rpcenv->check_pool_exist($pool);
287 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
288 if defined($storage);
291 &$resolve_cdrom_alias($param);
293 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
295 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
297 foreach my $opt (keys %$param) {
298 if (PVE
::QemuServer
::valid_drivename
($opt)) {
299 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
300 raise_param_exc
({ $opt => "unable to parse drive options" }) if !$drive;
302 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
303 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
307 PVE
::QemuServer
::add_random_macs
($param);
309 my $keystr = join(' ', keys %$param);
310 raise_param_exc
({ archive
=> "option conflicts with other options ($keystr)"}) if $keystr;
312 if ($archive eq '-') {
313 die "pipe requires cli environment\n"
314 if $rpcenv->{type
} ne 'cli';
316 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
318 PVE
::Storage
::activate_volumes
($storecfg, [ $archive ])
319 if PVE
::Storage
::parse_volume_id
($archive, 1);
321 die "can't find archive file '$archive'\n" if !($path && -f
$path);
326 my $addVMtoPoolFn = sub {
327 my $usercfg = cfs_read_file
("user.cfg");
328 if (my $data = $usercfg->{pools
}->{$pool}) {
329 $data->{vms
}->{$vmid} = 1;
330 $usercfg->{vms
}->{$vmid} = $pool;
331 cfs_write_file
("user.cfg", $usercfg);
335 my $restorefn = sub {
338 die "unable to restore vm $vmid: config file already exists\n"
341 die "unable to restore vm $vmid: vm is running\n"
342 if PVE
::QemuServer
::check_running
($vmid);
344 # destroy existing data - keep empty config
345 PVE
::QemuServer
::destroy_vm
($storecfg, $vmid, 1);
349 PVE
::QemuServer
::restore_archive
($archive, $vmid, $authuser, {
352 unique
=> $unique });
354 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
357 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
363 die "unable to create vm $vmid: config file already exists\n"
374 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
376 # try to be smart about bootdisk
377 my @disks = PVE
::QemuServer
::disknames
();
379 foreach my $ds (reverse @disks) {
380 next if !$conf->{$ds};
381 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
382 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
386 if (!$conf->{bootdisk
} && $firstdisk) {
387 $conf->{bootdisk
} = $firstdisk;
390 PVE
::QemuServer
::update_config_nolock
($vmid, $conf);
396 foreach my $volid (@$vollist) {
397 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
400 die "create failed - $err";
403 PVE
::AccessControl
::lock_user_config
($addVMtoPoolFn, "can't add VM to pool") if $pool;
406 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
409 return PVE
::QemuServer
::lock_config_full
($vmid, 1, $archive ?
$restorefn : $createfn);
412 __PACKAGE__-
>register_method({
417 description
=> "Directory index",
422 additionalProperties
=> 0,
424 node
=> get_standard_option
('pve-node'),
425 vmid
=> get_standard_option
('pve-vmid'),
433 subdir
=> { type
=> 'string' },
436 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
442 { subdir
=> 'config' },
443 { subdir
=> 'status' },
444 { subdir
=> 'unlink' },
445 { subdir
=> 'vncproxy' },
446 { subdir
=> 'migrate' },
448 { subdir
=> 'rrddata' },
449 { subdir
=> 'monitor' },
455 __PACKAGE__-
>register_method({
457 path
=> '{vmid}/rrd',
459 protected
=> 1, # fixme: can we avoid that?
461 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
463 description
=> "Read VM RRD statistics (returns PNG)",
465 additionalProperties
=> 0,
467 node
=> get_standard_option
('pve-node'),
468 vmid
=> get_standard_option
('pve-vmid'),
470 description
=> "Specify the time frame you are interested in.",
472 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
475 description
=> "The list of datasources you want to display.",
476 type
=> 'string', format
=> 'pve-configid-list',
479 description
=> "The RRD consolidation function",
481 enum
=> [ 'AVERAGE', 'MAX' ],
489 filename
=> { type
=> 'string' },
495 return PVE
::Cluster
::create_rrd_graph
(
496 "pve2-vm/$param->{vmid}", $param->{timeframe
},
497 $param->{ds
}, $param->{cf
});
501 __PACKAGE__-
>register_method({
503 path
=> '{vmid}/rrddata',
505 protected
=> 1, # fixme: can we avoid that?
507 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
509 description
=> "Read VM RRD statistics",
511 additionalProperties
=> 0,
513 node
=> get_standard_option
('pve-node'),
514 vmid
=> get_standard_option
('pve-vmid'),
516 description
=> "Specify the time frame you are interested in.",
518 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
521 description
=> "The RRD consolidation function",
523 enum
=> [ 'AVERAGE', 'MAX' ],
538 return PVE
::Cluster
::create_rrd_data
(
539 "pve2-vm/$param->{vmid}", $param->{timeframe
}, $param->{cf
});
543 __PACKAGE__-
>register_method({
545 path
=> '{vmid}/config',
548 description
=> "Get virtual machine configuration.",
550 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
553 additionalProperties
=> 0,
555 node
=> get_standard_option
('pve-node'),
556 vmid
=> get_standard_option
('pve-vmid'),
564 description
=> 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
571 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
576 my $vm_is_volid_owner = sub {
577 my ($storecfg, $vmid, $volid) =@_;
579 if ($volid !~ m
|^/|) {
581 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
582 if ($owner && ($owner == $vmid)) {
590 my $test_deallocate_drive = sub {
591 my ($storecfg, $vmid, $key, $drive, $force) = @_;
593 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
594 my $volid = $drive->{file
};
595 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
596 if ($force || $key =~ m/^unused/) {
597 my $sid = PVE
::Storage
::parse_volume_id
($volid);
606 my $delete_drive = sub {
607 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
609 if (!PVE
::QemuServer
::drive_is_cdrom
($drive)) {
610 my $volid = $drive->{file
};
611 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
612 if ($force || $key =~ m/^unused/) {
613 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
616 PVE
::QemuServer
::add_unused_volume
($conf, $volid, $vmid);
621 delete $conf->{$key};
624 my $vmconfig_delete_option = sub {
625 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
627 return if !defined($conf->{$opt});
629 my $isDisk = PVE
::QemuServer
::valid_drivename
($opt)|| ($opt =~ m/^unused/);
632 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
634 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
635 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
636 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
640 die "error hot-unplug $opt" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
643 my $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
644 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
646 delete $conf->{$opt};
649 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
652 my $vmconfig_update_disk = sub {
653 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
655 my $drive = PVE
::QemuServer
::parse_drive
($opt, $value);
657 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { #cdrom
658 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
660 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
665 if (my $old_drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt})) {
667 my $media = $drive->{media
} || 'disk';
668 my $oldmedia = $old_drive->{media
} || 'disk';
669 die "unable to change media type\n" if $media ne $oldmedia;
671 if (!PVE
::QemuServer
::drive_is_cdrom
($old_drive) &&
672 ($drive->{file
} ne $old_drive->{file
})) { # delete old disks
674 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
675 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
678 if($drive->{bps
} != $old_drive->{bps
} ||
679 $drive->{bps_rd
} != $old_drive->{bps_rd
} ||
680 $drive->{bps_wr
} != $old_drive->{bps_wr
} ||
681 $drive->{iops
} != $old_drive->{iops
} ||
682 $drive->{iops_rd
} != $old_drive->{iops_rd
} ||
683 $drive->{iops_wr
} != $old_drive->{iops_wr
} ) {
684 PVE
::QemuServer
::qemu_block_set_io_throttle
($vmid,"drive-$opt",$drive->{bps
}, $drive->{bps_rd
}, $drive->{bps_wr
}, $drive->{iops
}, $drive->{iops_rd
}, $drive->{iops_wr
}) if !PVE
::QemuServer
::drive_is_cdrom
($drive);
689 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
690 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
692 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
693 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
695 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
697 if (PVE
::QemuServer
::check_running
($vmid)) {
698 if ($drive->{file
} eq 'none') {
699 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt");
701 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
702 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt"); #force eject if locked
703 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change",device
=> "drive-$opt",target
=> "$path") if $path;
707 } else { # hotplug new disks
709 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $drive);
713 my $vmconfig_update_net = sub {
714 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
717 #if online update, then unplug first
718 die "error hot-unplug $opt for update" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
721 $conf->{$opt} = $value;
722 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
723 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
725 my $net = PVE
::QemuServer
::parse_net
($conf->{$opt});
727 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $net);
730 my $vm_config_perm_list = [
740 __PACKAGE__-
>register_method({
742 path
=> '{vmid}/config',
746 description
=> "Set virtual machine options.",
748 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
751 additionalProperties
=> 0,
752 properties
=> PVE
::QemuServer
::json_config_properties
(
754 node
=> get_standard_option
('pve-node'),
755 vmid
=> get_standard_option
('pve-vmid'),
756 skiplock
=> get_standard_option
('skiplock'),
758 type
=> 'string', format
=> 'pve-configid-list',
759 description
=> "A list of settings you want to delete.",
764 description
=> $opt_force_description,
766 requires
=> 'delete',
770 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
776 returns
=> { type
=> 'null'},
780 my $rpcenv = PVE
::RPCEnvironment
::get
();
782 my $authuser = $rpcenv->get_user();
784 my $node = extract_param
($param, 'node');
786 my $vmid = extract_param
($param, 'vmid');
788 my $digest = extract_param
($param, 'digest');
790 my @paramarr = (); # used for log message
791 foreach my $key (keys %$param) {
792 push @paramarr, "-$key", $param->{$key};
795 my $skiplock = extract_param
($param, 'skiplock');
796 raise_param_exc
({ skiplock
=> "Only root may use this option." })
797 if $skiplock && $authuser ne 'root@pam';
799 my $delete_str = extract_param
($param, 'delete');
801 my $force = extract_param
($param, 'force');
803 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
805 my $storecfg = PVE
::Storage
::config
();
807 &$resolve_cdrom_alias($param);
809 # now try to verify all parameters
812 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
813 $opt = 'ide2' if $opt eq 'cdrom';
814 raise_param_exc
({ delete => "you can't use '-$opt' and " .
815 "-delete $opt' at the same time" })
816 if defined($param->{$opt});
818 if (!PVE
::QemuServer
::option_exists
($opt)) {
819 raise_param_exc
({ delete => "unknown option '$opt'" });
825 foreach my $opt (keys %$param) {
826 if (PVE
::QemuServer
::valid_drivename
($opt)) {
828 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
829 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
830 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
831 } elsif ($opt =~ m/^net(\d+)$/) {
833 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
834 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
838 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
840 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
842 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
846 my $conf = PVE
::QemuServer
::load_config
($vmid);
848 die "checksum missmatch (file change by other user?)\n"
849 if $digest && $digest ne $conf->{digest
};
851 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
853 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
855 foreach my $opt (@delete) { # delete
856 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
857 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
860 foreach my $opt (keys %$param) { # add/change
862 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
864 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
866 if (PVE
::QemuServer
::valid_drivename
($opt)) {
868 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
869 $opt, $param->{$opt}, $force);
871 } elsif ($opt =~ m/^net(\d+)$/) { #nics
873 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
874 $opt, $param->{$opt});
878 $conf->{$opt} = $param->{$opt};
879 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
884 PVE
::QemuServer
::lock_config
($vmid, $updatefn);
890 __PACKAGE__-
>register_method({
891 name
=> 'destroy_vm',
896 description
=> "Destroy the vm (also delete all used/owned volumes).",
898 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
901 additionalProperties
=> 0,
903 node
=> get_standard_option
('pve-node'),
904 vmid
=> get_standard_option
('pve-vmid'),
905 skiplock
=> get_standard_option
('skiplock'),
914 my $rpcenv = PVE
::RPCEnvironment
::get
();
916 my $authuser = $rpcenv->get_user();
918 my $vmid = $param->{vmid
};
920 my $skiplock = $param->{skiplock
};
921 raise_param_exc
({ skiplock
=> "Only root may use this option." })
922 if $skiplock && $authuser ne 'root@pam';
925 my $conf = PVE
::QemuServer
::load_config
($vmid);
927 my $storecfg = PVE
::Storage
::config
();
929 my $delVMfromPoolFn = sub {
930 my $usercfg = cfs_read_file
("user.cfg");
931 if (my $pool = $usercfg->{vms
}->{$vmid}) {
932 if (my $data = $usercfg->{pools
}->{$pool}) {
933 delete $data->{vms
}->{$vmid};
934 delete $usercfg->{vms
}->{$vmid};
935 cfs_write_file
("user.cfg", $usercfg);
943 syslog
('info', "destroy VM $vmid: $upid\n");
945 PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock);
947 PVE
::AccessControl
::lock_user_config
($delVMfromPoolFn, "pool cleanup failed");
950 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
953 __PACKAGE__-
>register_method({
955 path
=> '{vmid}/unlink',
959 description
=> "Unlink/delete disk images.",
961 check
=> [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
964 additionalProperties
=> 0,
966 node
=> get_standard_option
('pve-node'),
967 vmid
=> get_standard_option
('pve-vmid'),
969 type
=> 'string', format
=> 'pve-configid-list',
970 description
=> "A list of disk IDs you want to delete.",
974 description
=> $opt_force_description,
979 returns
=> { type
=> 'null'},
983 $param->{delete} = extract_param
($param, 'idlist');
985 __PACKAGE__-
>update_vm($param);
992 __PACKAGE__-
>register_method({
994 path
=> '{vmid}/vncproxy',
998 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1000 description
=> "Creates a TCP VNC proxy connections.",
1002 additionalProperties
=> 0,
1004 node
=> get_standard_option
('pve-node'),
1005 vmid
=> get_standard_option
('pve-vmid'),
1009 additionalProperties
=> 0,
1011 user
=> { type
=> 'string' },
1012 ticket
=> { type
=> 'string' },
1013 cert
=> { type
=> 'string' },
1014 port
=> { type
=> 'integer' },
1015 upid
=> { type
=> 'string' },
1021 my $rpcenv = PVE
::RPCEnvironment
::get
();
1023 my $authuser = $rpcenv->get_user();
1025 my $vmid = $param->{vmid
};
1026 my $node = $param->{node
};
1028 my $authpath = "/vms/$vmid";
1030 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
1032 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
1035 my $port = PVE
::Tools
::next_vnc_port
();
1039 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
1040 $remip = PVE
::Cluster
::remote_node_ip
($node);
1043 # NOTE: kvm VNC traffic is already TLS encrypted,
1044 # so we select the fastest chipher here (or 'none'?)
1045 my $remcmd = $remip ?
['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1046 '-c', 'blowfish-cbc', $remip] : [];
1053 syslog
('info', "starting vnc proxy $upid\n");
1055 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1057 my $qmstr = join(' ', @$qmcmd);
1059 # also redirect stderr (else we get RFB protocol errors)
1060 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1062 PVE
::Tools
::run_command
($cmd);
1067 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1078 __PACKAGE__-
>register_method({
1080 path
=> '{vmid}/status',
1083 description
=> "Directory index",
1088 additionalProperties
=> 0,
1090 node
=> get_standard_option
('pve-node'),
1091 vmid
=> get_standard_option
('pve-vmid'),
1099 subdir
=> { type
=> 'string' },
1102 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
1108 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1111 { subdir
=> 'current' },
1112 { subdir
=> 'start' },
1113 { subdir
=> 'stop' },
1119 my $vm_is_ha_managed = sub {
1122 my $cc = PVE
::Cluster
::cfs_read_file
('cluster.conf');
1123 if (PVE
::Cluster
::cluster_conf_lookup_pvevm
($cc, 0, $vmid, 1)) {
1129 __PACKAGE__-
>register_method({
1130 name
=> 'vm_status',
1131 path
=> '{vmid}/status/current',
1134 protected
=> 1, # qemu pid files are only readable by root
1135 description
=> "Get virtual machine status.",
1137 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1140 additionalProperties
=> 0,
1142 node
=> get_standard_option
('pve-node'),
1143 vmid
=> get_standard_option
('pve-vmid'),
1146 returns
=> { type
=> 'object' },
1151 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1153 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
1154 my $status = $vmstatus->{$param->{vmid
}};
1156 $status->{ha
} = &$vm_is_ha_managed($param->{vmid
});
1161 __PACKAGE__-
>register_method({
1163 path
=> '{vmid}/status/start',
1167 description
=> "Start virtual machine.",
1169 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1172 additionalProperties
=> 0,
1174 node
=> get_standard_option
('pve-node'),
1175 vmid
=> get_standard_option
('pve-vmid'),
1176 skiplock
=> get_standard_option
('skiplock'),
1177 stateuri
=> get_standard_option
('pve-qm-stateuri'),
1186 my $rpcenv = PVE
::RPCEnvironment
::get
();
1188 my $authuser = $rpcenv->get_user();
1190 my $node = extract_param
($param, 'node');
1192 my $vmid = extract_param
($param, 'vmid');
1194 my $stateuri = extract_param
($param, 'stateuri');
1195 raise_param_exc
({ stateuri
=> "Only root may use this option." })
1196 if $stateuri && $authuser ne 'root@pam';
1198 my $skiplock = extract_param
($param, 'skiplock');
1199 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1200 if $skiplock && $authuser ne 'root@pam';
1202 my $storecfg = PVE
::Storage
::config
();
1204 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1205 $rpcenv->{type
} ne 'ha') {
1210 my $service = "pvevm:$vmid";
1212 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1214 print "Executing HA start for VM $vmid\n";
1216 PVE
::Tools
::run_command
($cmd);
1221 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1228 syslog
('info', "start VM $vmid: $upid\n");
1230 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $stateuri, $skiplock);
1235 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1239 __PACKAGE__-
>register_method({
1241 path
=> '{vmid}/status/stop',
1245 description
=> "Stop virtual machine.",
1247 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1250 additionalProperties
=> 0,
1252 node
=> get_standard_option
('pve-node'),
1253 vmid
=> get_standard_option
('pve-vmid'),
1254 skiplock
=> get_standard_option
('skiplock'),
1256 description
=> "Wait maximal timeout seconds.",
1262 description
=> "Do not decativate storage volumes.",
1275 my $rpcenv = PVE
::RPCEnvironment
::get
();
1277 my $authuser = $rpcenv->get_user();
1279 my $node = extract_param
($param, 'node');
1281 my $vmid = extract_param
($param, 'vmid');
1283 my $skiplock = extract_param
($param, 'skiplock');
1284 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1285 if $skiplock && $authuser ne 'root@pam';
1287 my $keepActive = extract_param
($param, 'keepActive');
1288 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1289 if $keepActive && $authuser ne 'root@pam';
1291 my $storecfg = PVE
::Storage
::config
();
1293 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1298 my $service = "pvevm:$vmid";
1300 my $cmd = ['clusvcadm', '-d', $service];
1302 print "Executing HA stop for VM $vmid\n";
1304 PVE
::Tools
::run_command
($cmd);
1309 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1315 syslog
('info', "stop VM $vmid: $upid\n");
1317 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0,
1318 $param->{timeout
}, 0, 1, $keepActive);
1323 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1327 __PACKAGE__-
>register_method({
1329 path
=> '{vmid}/status/reset',
1333 description
=> "Reset virtual machine.",
1335 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1338 additionalProperties
=> 0,
1340 node
=> get_standard_option
('pve-node'),
1341 vmid
=> get_standard_option
('pve-vmid'),
1342 skiplock
=> get_standard_option
('skiplock'),
1351 my $rpcenv = PVE
::RPCEnvironment
::get
();
1353 my $authuser = $rpcenv->get_user();
1355 my $node = extract_param
($param, 'node');
1357 my $vmid = extract_param
($param, 'vmid');
1359 my $skiplock = extract_param
($param, 'skiplock');
1360 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1361 if $skiplock && $authuser ne 'root@pam';
1363 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1368 PVE
::QemuServer
::vm_reset
($vmid, $skiplock);
1373 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1376 __PACKAGE__-
>register_method({
1377 name
=> 'vm_shutdown',
1378 path
=> '{vmid}/status/shutdown',
1382 description
=> "Shutdown 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'),
1393 description
=> "Wait maximal timeout seconds.",
1399 description
=> "Make sure the VM stops.",
1405 description
=> "Do not decativate storage volumes.",
1418 my $rpcenv = PVE
::RPCEnvironment
::get
();
1420 my $authuser = $rpcenv->get_user();
1422 my $node = extract_param
($param, 'node');
1424 my $vmid = extract_param
($param, 'vmid');
1426 my $skiplock = extract_param
($param, 'skiplock');
1427 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1428 if $skiplock && $authuser ne 'root@pam';
1430 my $keepActive = extract_param
($param, 'keepActive');
1431 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1432 if $keepActive && $authuser ne 'root@pam';
1434 my $storecfg = PVE
::Storage
::config
();
1439 syslog
('info', "shutdown VM $vmid: $upid\n");
1441 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0, $param->{timeout
},
1442 1, $param->{forceStop
}, $keepActive);
1447 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1450 __PACKAGE__-
>register_method({
1451 name
=> 'vm_suspend',
1452 path
=> '{vmid}/status/suspend',
1456 description
=> "Suspend virtual machine.",
1458 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1461 additionalProperties
=> 0,
1463 node
=> get_standard_option
('pve-node'),
1464 vmid
=> get_standard_option
('pve-vmid'),
1465 skiplock
=> get_standard_option
('skiplock'),
1474 my $rpcenv = PVE
::RPCEnvironment
::get
();
1476 my $authuser = $rpcenv->get_user();
1478 my $node = extract_param
($param, 'node');
1480 my $vmid = extract_param
($param, 'vmid');
1482 my $skiplock = extract_param
($param, 'skiplock');
1483 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1484 if $skiplock && $authuser ne 'root@pam';
1486 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1491 syslog
('info', "suspend VM $vmid: $upid\n");
1493 PVE
::QemuServer
::vm_suspend
($vmid, $skiplock);
1498 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1501 __PACKAGE__-
>register_method({
1502 name
=> 'vm_resume',
1503 path
=> '{vmid}/status/resume',
1507 description
=> "Resume virtual machine.",
1509 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1512 additionalProperties
=> 0,
1514 node
=> get_standard_option
('pve-node'),
1515 vmid
=> get_standard_option
('pve-vmid'),
1516 skiplock
=> get_standard_option
('skiplock'),
1525 my $rpcenv = PVE
::RPCEnvironment
::get
();
1527 my $authuser = $rpcenv->get_user();
1529 my $node = extract_param
($param, 'node');
1531 my $vmid = extract_param
($param, 'vmid');
1533 my $skiplock = extract_param
($param, 'skiplock');
1534 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1535 if $skiplock && $authuser ne 'root@pam';
1537 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1542 syslog
('info', "resume VM $vmid: $upid\n");
1544 PVE
::QemuServer
::vm_resume
($vmid, $skiplock);
1549 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1552 __PACKAGE__-
>register_method({
1553 name
=> 'vm_sendkey',
1554 path
=> '{vmid}/sendkey',
1558 description
=> "Send key event to virtual machine.",
1560 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1563 additionalProperties
=> 0,
1565 node
=> get_standard_option
('pve-node'),
1566 vmid
=> get_standard_option
('pve-vmid'),
1567 skiplock
=> get_standard_option
('skiplock'),
1569 description
=> "The key (qemu monitor encoding).",
1574 returns
=> { type
=> 'null'},
1578 my $rpcenv = PVE
::RPCEnvironment
::get
();
1580 my $authuser = $rpcenv->get_user();
1582 my $node = extract_param
($param, 'node');
1584 my $vmid = extract_param
($param, 'vmid');
1586 my $skiplock = extract_param
($param, 'skiplock');
1587 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1588 if $skiplock && $authuser ne 'root@pam';
1590 PVE
::QemuServer
::vm_sendkey
($vmid, $skiplock, $param->{key
});
1595 __PACKAGE__-
>register_method({
1596 name
=> 'migrate_vm',
1597 path
=> '{vmid}/migrate',
1601 description
=> "Migrate virtual machine. Creates a new migration task.",
1603 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1606 additionalProperties
=> 0,
1608 node
=> get_standard_option
('pve-node'),
1609 vmid
=> get_standard_option
('pve-vmid'),
1610 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1613 description
=> "Use online/live migration.",
1618 description
=> "Allow to migrate VMs which use local devices. Only root may use this option.",
1625 description
=> "the task ID.",
1630 my $rpcenv = PVE
::RPCEnvironment
::get
();
1632 my $authuser = $rpcenv->get_user();
1634 my $target = extract_param
($param, 'target');
1636 my $localnode = PVE
::INotify
::nodename
();
1637 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1639 PVE
::Cluster
::check_cfs_quorum
();
1641 PVE
::Cluster
::check_node_exists
($target);
1643 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1645 my $vmid = extract_param
($param, 'vmid');
1647 raise_param_exc
({ force
=> "Only root may use this option." })
1648 if $param->{force
} && $authuser ne 'root@pam';
1651 my $conf = PVE
::QemuServer
::load_config
($vmid);
1653 # try to detect errors early
1655 PVE
::QemuServer
::check_lock
($conf);
1657 if (PVE
::QemuServer
::check_running
($vmid)) {
1658 die "cant migrate running VM without --online\n"
1659 if !$param->{online
};
1662 my $storecfg = PVE
::Storage
::config
();
1663 PVE
::QemuServer
::check_storage_availability
($storecfg, $conf, $target);
1665 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1670 my $service = "pvevm:$vmid";
1672 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1674 print "Executing HA migrate for VM $vmid to node $target\n";
1676 PVE
::Tools
::run_command
($cmd);
1681 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1688 PVE
::QemuMigrate-
>migrate($target, $targetip, $vmid, $param);
1691 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1696 __PACKAGE__-
>register_method({
1698 path
=> '{vmid}/monitor',
1702 description
=> "Execute Qemu monitor commands.",
1704 check
=> ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1707 additionalProperties
=> 0,
1709 node
=> get_standard_option
('pve-node'),
1710 vmid
=> get_standard_option
('pve-vmid'),
1713 description
=> "The monitor command.",
1717 returns
=> { type
=> 'string'},
1721 my $vmid = $param->{vmid
};
1723 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
1727 $res = PVE
::QemuServer
::vm_human_monitor_command
($vmid, $param->{command
});
1729 $res = "ERROR: $@" if $@;