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
680 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
681 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
683 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
684 $drive = PVE
::QemuServer
::parse_drive
($opt, $conf->{$opt});
686 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) { # cdrom
688 if (PVE
::QemuServer
::check_running
($vmid)) {
689 if ($drive->{file
} eq 'none') {
690 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt");
692 my $path = PVE
::QemuServer
::get_iso_path
($storecfg, $vmid, $drive->{file
});
693 PVE
::QemuServer
::vm_mon_cmd
($vmid, "eject",force
=> JSON
::true
,device
=> "drive-$opt"); #force eject if locked
694 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change",device
=> "drive-$opt",target
=> "$path") if $path;
698 } else { # hotplug new disks
700 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $drive);
704 my $vmconfig_update_net = sub {
705 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
708 #if online update, then unplug first
709 die "error hot-unplug $opt for update" if !PVE
::QemuServer
::vm_deviceunplug
($vmid, $conf, $opt);
712 $conf->{$opt} = $value;
713 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
714 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
716 my $net = PVE
::QemuServer
::parse_net
($conf->{$opt});
718 die "error hotplug $opt" if !PVE
::QemuServer
::vm_deviceplug
($storecfg, $conf, $vmid, $opt, $net);
721 my $vm_config_perm_list = [
731 __PACKAGE__-
>register_method({
733 path
=> '{vmid}/config',
737 description
=> "Set virtual machine options.",
739 check
=> ['perm', '/vms/{vmid}', $vm_config_perm_list, any
=> 1],
742 additionalProperties
=> 0,
743 properties
=> PVE
::QemuServer
::json_config_properties
(
745 node
=> get_standard_option
('pve-node'),
746 vmid
=> get_standard_option
('pve-vmid'),
747 skiplock
=> get_standard_option
('skiplock'),
749 type
=> 'string', format
=> 'pve-configid-list',
750 description
=> "A list of settings you want to delete.",
755 description
=> $opt_force_description,
757 requires
=> 'delete',
761 description
=> 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
767 returns
=> { type
=> 'null'},
771 my $rpcenv = PVE
::RPCEnvironment
::get
();
773 my $authuser = $rpcenv->get_user();
775 my $node = extract_param
($param, 'node');
777 my $vmid = extract_param
($param, 'vmid');
779 my $digest = extract_param
($param, 'digest');
781 my @paramarr = (); # used for log message
782 foreach my $key (keys %$param) {
783 push @paramarr, "-$key", $param->{$key};
786 my $skiplock = extract_param
($param, 'skiplock');
787 raise_param_exc
({ skiplock
=> "Only root may use this option." })
788 if $skiplock && $authuser ne 'root@pam';
790 my $delete_str = extract_param
($param, 'delete');
792 my $force = extract_param
($param, 'force');
794 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
796 my $storecfg = PVE
::Storage
::config
();
798 &$resolve_cdrom_alias($param);
800 # now try to verify all parameters
803 foreach my $opt (PVE
::Tools
::split_list
($delete_str)) {
804 $opt = 'ide2' if $opt eq 'cdrom';
805 raise_param_exc
({ delete => "you can't use '-$opt' and " .
806 "-delete $opt' at the same time" })
807 if defined($param->{$opt});
809 if (!PVE
::QemuServer
::option_exists
($opt)) {
810 raise_param_exc
({ delete => "unknown option '$opt'" });
816 foreach my $opt (keys %$param) {
817 if (PVE
::QemuServer
::valid_drivename
($opt)) {
819 my $drive = PVE
::QemuServer
::parse_drive
($opt, $param->{$opt});
820 PVE
::QemuServer
::cleanup_drive_path
($opt, $storecfg, $drive);
821 $param->{$opt} = PVE
::QemuServer
::print_drive
($vmid, $drive);
822 } elsif ($opt =~ m/^net(\d+)$/) {
824 my $net = PVE
::QemuServer
::parse_net
($param->{$opt});
825 $param->{$opt} = PVE
::QemuServer
::print_net
($net);
829 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
831 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
833 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
837 my $conf = PVE
::QemuServer
::load_config
($vmid);
839 die "checksum missmatch (file change by other user?)\n"
840 if $digest && $digest ne $conf->{digest
};
842 PVE
::QemuServer
::check_lock
($conf) if !$skiplock;
844 PVE
::Cluster
::log_msg
('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
846 foreach my $opt (@delete) { # delete
847 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
848 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
851 foreach my $opt (keys %$param) { # add/change
853 $conf = PVE
::QemuServer
::load_config
($vmid); # update/reload
855 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
857 if (PVE
::QemuServer
::valid_drivename
($opt)) {
859 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
860 $opt, $param->{$opt}, $force);
862 } elsif ($opt =~ m/^net(\d+)$/) { #nics
864 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
865 $opt, $param->{$opt});
869 $conf->{$opt} = $param->{$opt};
870 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
875 PVE
::QemuServer
::lock_config
($vmid, $updatefn);
881 __PACKAGE__-
>register_method({
882 name
=> 'destroy_vm',
887 description
=> "Destroy the vm (also delete all used/owned volumes).",
889 check
=> [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
892 additionalProperties
=> 0,
894 node
=> get_standard_option
('pve-node'),
895 vmid
=> get_standard_option
('pve-vmid'),
896 skiplock
=> get_standard_option
('skiplock'),
905 my $rpcenv = PVE
::RPCEnvironment
::get
();
907 my $authuser = $rpcenv->get_user();
909 my $vmid = $param->{vmid
};
911 my $skiplock = $param->{skiplock
};
912 raise_param_exc
({ skiplock
=> "Only root may use this option." })
913 if $skiplock && $authuser ne 'root@pam';
916 my $conf = PVE
::QemuServer
::load_config
($vmid);
918 my $storecfg = PVE
::Storage
::config
();
920 my $delVMfromPoolFn = sub {
921 my $usercfg = cfs_read_file
("user.cfg");
922 if (my $pool = $usercfg->{vms
}->{$vmid}) {
923 if (my $data = $usercfg->{pools
}->{$pool}) {
924 delete $data->{vms
}->{$vmid};
925 delete $usercfg->{vms
}->{$vmid};
926 cfs_write_file
("user.cfg", $usercfg);
934 syslog
('info', "destroy VM $vmid: $upid\n");
936 PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock);
938 PVE
::AccessControl
::lock_user_config
($delVMfromPoolFn, "pool cleanup failed");
941 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
944 __PACKAGE__-
>register_method({
946 path
=> '{vmid}/unlink',
950 description
=> "Unlink/delete disk images.",
952 check
=> [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
955 additionalProperties
=> 0,
957 node
=> get_standard_option
('pve-node'),
958 vmid
=> get_standard_option
('pve-vmid'),
960 type
=> 'string', format
=> 'pve-configid-list',
961 description
=> "A list of disk IDs you want to delete.",
965 description
=> $opt_force_description,
970 returns
=> { type
=> 'null'},
974 $param->{delete} = extract_param
($param, 'idlist');
976 __PACKAGE__-
>update_vm($param);
983 __PACKAGE__-
>register_method({
985 path
=> '{vmid}/vncproxy',
989 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
991 description
=> "Creates a TCP VNC proxy connections.",
993 additionalProperties
=> 0,
995 node
=> get_standard_option
('pve-node'),
996 vmid
=> get_standard_option
('pve-vmid'),
1000 additionalProperties
=> 0,
1002 user
=> { type
=> 'string' },
1003 ticket
=> { type
=> 'string' },
1004 cert
=> { type
=> 'string' },
1005 port
=> { type
=> 'integer' },
1006 upid
=> { type
=> 'string' },
1012 my $rpcenv = PVE
::RPCEnvironment
::get
();
1014 my $authuser = $rpcenv->get_user();
1016 my $vmid = $param->{vmid
};
1017 my $node = $param->{node
};
1019 my $authpath = "/vms/$vmid";
1021 my $ticket = PVE
::AccessControl
::assemble_vnc_ticket
($authuser, $authpath);
1023 $sslcert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192)
1026 my $port = PVE
::Tools
::next_vnc_port
();
1030 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
1031 $remip = PVE
::Cluster
::remote_node_ip
($node);
1034 # NOTE: kvm VNC traffic is already TLS encrypted,
1035 # so we select the fastest chipher here (or 'none'?)
1036 my $remcmd = $remip ?
['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1037 '-c', 'blowfish-cbc', $remip] : [];
1044 syslog
('info', "starting vnc proxy $upid\n");
1046 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1048 my $qmstr = join(' ', @$qmcmd);
1050 # also redirect stderr (else we get RFB protocol errors)
1051 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1053 PVE
::Tools
::run_command
($cmd);
1058 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1069 __PACKAGE__-
>register_method({
1071 path
=> '{vmid}/status',
1074 description
=> "Directory index",
1079 additionalProperties
=> 0,
1081 node
=> get_standard_option
('pve-node'),
1082 vmid
=> get_standard_option
('pve-vmid'),
1090 subdir
=> { type
=> 'string' },
1093 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
1099 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1102 { subdir
=> 'current' },
1103 { subdir
=> 'start' },
1104 { subdir
=> 'stop' },
1110 my $vm_is_ha_managed = sub {
1113 my $cc = PVE
::Cluster
::cfs_read_file
('cluster.conf');
1114 if (PVE
::Cluster
::cluster_conf_lookup_pvevm
($cc, 0, $vmid, 1)) {
1120 __PACKAGE__-
>register_method({
1121 name
=> 'vm_status',
1122 path
=> '{vmid}/status/current',
1125 protected
=> 1, # qemu pid files are only readable by root
1126 description
=> "Get virtual machine status.",
1128 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1131 additionalProperties
=> 0,
1133 node
=> get_standard_option
('pve-node'),
1134 vmid
=> get_standard_option
('pve-vmid'),
1137 returns
=> { type
=> 'object' },
1142 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
1144 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
});
1145 my $status = $vmstatus->{$param->{vmid
}};
1147 $status->{ha
} = &$vm_is_ha_managed($param->{vmid
});
1152 __PACKAGE__-
>register_method({
1154 path
=> '{vmid}/status/start',
1158 description
=> "Start virtual machine.",
1160 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1163 additionalProperties
=> 0,
1165 node
=> get_standard_option
('pve-node'),
1166 vmid
=> get_standard_option
('pve-vmid'),
1167 skiplock
=> get_standard_option
('skiplock'),
1168 stateuri
=> get_standard_option
('pve-qm-stateuri'),
1177 my $rpcenv = PVE
::RPCEnvironment
::get
();
1179 my $authuser = $rpcenv->get_user();
1181 my $node = extract_param
($param, 'node');
1183 my $vmid = extract_param
($param, 'vmid');
1185 my $stateuri = extract_param
($param, 'stateuri');
1186 raise_param_exc
({ stateuri
=> "Only root may use this option." })
1187 if $stateuri && $authuser ne 'root@pam';
1189 my $skiplock = extract_param
($param, 'skiplock');
1190 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1191 if $skiplock && $authuser ne 'root@pam';
1193 my $storecfg = PVE
::Storage
::config
();
1195 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1196 $rpcenv->{type
} ne 'ha') {
1201 my $service = "pvevm:$vmid";
1203 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1205 print "Executing HA start for VM $vmid\n";
1207 PVE
::Tools
::run_command
($cmd);
1212 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1219 syslog
('info', "start VM $vmid: $upid\n");
1221 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $stateuri, $skiplock);
1226 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1230 __PACKAGE__-
>register_method({
1232 path
=> '{vmid}/status/stop',
1236 description
=> "Stop virtual machine.",
1238 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1241 additionalProperties
=> 0,
1243 node
=> get_standard_option
('pve-node'),
1244 vmid
=> get_standard_option
('pve-vmid'),
1245 skiplock
=> get_standard_option
('skiplock'),
1247 description
=> "Wait maximal timeout seconds.",
1253 description
=> "Do not decativate storage volumes.",
1266 my $rpcenv = PVE
::RPCEnvironment
::get
();
1268 my $authuser = $rpcenv->get_user();
1270 my $node = extract_param
($param, 'node');
1272 my $vmid = extract_param
($param, 'vmid');
1274 my $skiplock = extract_param
($param, 'skiplock');
1275 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1276 if $skiplock && $authuser ne 'root@pam';
1278 my $keepActive = extract_param
($param, 'keepActive');
1279 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1280 if $keepActive && $authuser ne 'root@pam';
1282 my $storecfg = PVE
::Storage
::config
();
1284 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1289 my $service = "pvevm:$vmid";
1291 my $cmd = ['clusvcadm', '-d', $service];
1293 print "Executing HA stop for VM $vmid\n";
1295 PVE
::Tools
::run_command
($cmd);
1300 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1306 syslog
('info', "stop VM $vmid: $upid\n");
1308 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0,
1309 $param->{timeout
}, 0, 1, $keepActive);
1314 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1318 __PACKAGE__-
>register_method({
1320 path
=> '{vmid}/status/reset',
1324 description
=> "Reset virtual machine.",
1326 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1329 additionalProperties
=> 0,
1331 node
=> get_standard_option
('pve-node'),
1332 vmid
=> get_standard_option
('pve-vmid'),
1333 skiplock
=> get_standard_option
('skiplock'),
1342 my $rpcenv = PVE
::RPCEnvironment
::get
();
1344 my $authuser = $rpcenv->get_user();
1346 my $node = extract_param
($param, 'node');
1348 my $vmid = extract_param
($param, 'vmid');
1350 my $skiplock = extract_param
($param, 'skiplock');
1351 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1352 if $skiplock && $authuser ne 'root@pam';
1354 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1359 PVE
::QemuServer
::vm_reset
($vmid, $skiplock);
1364 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1367 __PACKAGE__-
>register_method({
1368 name
=> 'vm_shutdown',
1369 path
=> '{vmid}/status/shutdown',
1373 description
=> "Shutdown virtual machine.",
1375 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1378 additionalProperties
=> 0,
1380 node
=> get_standard_option
('pve-node'),
1381 vmid
=> get_standard_option
('pve-vmid'),
1382 skiplock
=> get_standard_option
('skiplock'),
1384 description
=> "Wait maximal timeout seconds.",
1390 description
=> "Make sure the VM stops.",
1396 description
=> "Do not decativate storage volumes.",
1409 my $rpcenv = PVE
::RPCEnvironment
::get
();
1411 my $authuser = $rpcenv->get_user();
1413 my $node = extract_param
($param, 'node');
1415 my $vmid = extract_param
($param, 'vmid');
1417 my $skiplock = extract_param
($param, 'skiplock');
1418 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1419 if $skiplock && $authuser ne 'root@pam';
1421 my $keepActive = extract_param
($param, 'keepActive');
1422 raise_param_exc
({ keepActive
=> "Only root may use this option." })
1423 if $keepActive && $authuser ne 'root@pam';
1425 my $storecfg = PVE
::Storage
::config
();
1430 syslog
('info', "shutdown VM $vmid: $upid\n");
1432 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, $skiplock, 0, $param->{timeout
},
1433 1, $param->{forceStop
}, $keepActive);
1438 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1441 __PACKAGE__-
>register_method({
1442 name
=> 'vm_suspend',
1443 path
=> '{vmid}/status/suspend',
1447 description
=> "Suspend virtual machine.",
1449 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1452 additionalProperties
=> 0,
1454 node
=> get_standard_option
('pve-node'),
1455 vmid
=> get_standard_option
('pve-vmid'),
1456 skiplock
=> get_standard_option
('skiplock'),
1465 my $rpcenv = PVE
::RPCEnvironment
::get
();
1467 my $authuser = $rpcenv->get_user();
1469 my $node = extract_param
($param, 'node');
1471 my $vmid = extract_param
($param, 'vmid');
1473 my $skiplock = extract_param
($param, 'skiplock');
1474 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1475 if $skiplock && $authuser ne 'root@pam';
1477 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1482 syslog
('info', "suspend VM $vmid: $upid\n");
1484 PVE
::QemuServer
::vm_suspend
($vmid, $skiplock);
1489 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1492 __PACKAGE__-
>register_method({
1493 name
=> 'vm_resume',
1494 path
=> '{vmid}/status/resume',
1498 description
=> "Resume virtual machine.",
1500 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1503 additionalProperties
=> 0,
1505 node
=> get_standard_option
('pve-node'),
1506 vmid
=> get_standard_option
('pve-vmid'),
1507 skiplock
=> get_standard_option
('skiplock'),
1516 my $rpcenv = PVE
::RPCEnvironment
::get
();
1518 my $authuser = $rpcenv->get_user();
1520 my $node = extract_param
($param, 'node');
1522 my $vmid = extract_param
($param, 'vmid');
1524 my $skiplock = extract_param
($param, 'skiplock');
1525 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1526 if $skiplock && $authuser ne 'root@pam';
1528 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
1533 syslog
('info', "resume VM $vmid: $upid\n");
1535 PVE
::QemuServer
::vm_resume
($vmid, $skiplock);
1540 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1543 __PACKAGE__-
>register_method({
1544 name
=> 'vm_sendkey',
1545 path
=> '{vmid}/sendkey',
1549 description
=> "Send key event to virtual machine.",
1551 check
=> ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1554 additionalProperties
=> 0,
1556 node
=> get_standard_option
('pve-node'),
1557 vmid
=> get_standard_option
('pve-vmid'),
1558 skiplock
=> get_standard_option
('skiplock'),
1560 description
=> "The key (qemu monitor encoding).",
1565 returns
=> { type
=> 'null'},
1569 my $rpcenv = PVE
::RPCEnvironment
::get
();
1571 my $authuser = $rpcenv->get_user();
1573 my $node = extract_param
($param, 'node');
1575 my $vmid = extract_param
($param, 'vmid');
1577 my $skiplock = extract_param
($param, 'skiplock');
1578 raise_param_exc
({ skiplock
=> "Only root may use this option." })
1579 if $skiplock && $authuser ne 'root@pam';
1581 PVE
::QemuServer
::vm_sendkey
($vmid, $skiplock, $param->{key
});
1586 __PACKAGE__-
>register_method({
1587 name
=> 'migrate_vm',
1588 path
=> '{vmid}/migrate',
1592 description
=> "Migrate virtual machine. Creates a new migration task.",
1594 check
=> ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1597 additionalProperties
=> 0,
1599 node
=> get_standard_option
('pve-node'),
1600 vmid
=> get_standard_option
('pve-vmid'),
1601 target
=> get_standard_option
('pve-node', { description
=> "Target node." }),
1604 description
=> "Use online/live migration.",
1609 description
=> "Allow to migrate VMs which use local devices. Only root may use this option.",
1616 description
=> "the task ID.",
1621 my $rpcenv = PVE
::RPCEnvironment
::get
();
1623 my $authuser = $rpcenv->get_user();
1625 my $target = extract_param
($param, 'target');
1627 my $localnode = PVE
::INotify
::nodename
();
1628 raise_param_exc
({ target
=> "target is local node."}) if $target eq $localnode;
1630 PVE
::Cluster
::check_cfs_quorum
();
1632 PVE
::Cluster
::check_node_exists
($target);
1634 my $targetip = PVE
::Cluster
::remote_node_ip
($target);
1636 my $vmid = extract_param
($param, 'vmid');
1638 raise_param_exc
({ force
=> "Only root may use this option." })
1639 if $param->{force
} && $authuser ne 'root@pam';
1642 my $conf = PVE
::QemuServer
::load_config
($vmid);
1644 # try to detect errors early
1646 PVE
::QemuServer
::check_lock
($conf);
1648 if (PVE
::QemuServer
::check_running
($vmid)) {
1649 die "cant migrate running VM without --online\n"
1650 if !$param->{online
};
1653 my $storecfg = PVE
::Storage
::config
();
1654 PVE
::QemuServer
::check_storage_availability
($storecfg, $conf, $target);
1656 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type
} ne 'ha') {
1661 my $service = "pvevm:$vmid";
1663 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1665 print "Executing HA migrate for VM $vmid to node $target\n";
1667 PVE
::Tools
::run_command
($cmd);
1672 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1679 PVE
::QemuMigrate-
>migrate($target, $targetip, $vmid, $param);
1682 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1687 __PACKAGE__-
>register_method({
1689 path
=> '{vmid}/monitor',
1693 description
=> "Execute Qemu monitor commands.",
1695 check
=> ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1698 additionalProperties
=> 0,
1700 node
=> get_standard_option
('pve-node'),
1701 vmid
=> get_standard_option
('pve-vmid'),
1704 description
=> "The monitor command.",
1708 returns
=> { type
=> 'string'},
1712 my $vmid = $param->{vmid
};
1714 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
1718 $res = PVE
::QemuServer
::vm_human_monitor_command
($vmid, $param->{command
});
1720 $res = "ERROR: $@" if $@;