]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | package PVE::API2::Qemu; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5b9d692a | 5 | use Cwd 'abs_path'; |
451b2b81 | 6 | use Net::SSLeay; |
47314bf5 | 7 | use UUID; |
1e3baf05 | 8 | |
502d18a2 | 9 | use PVE::Cluster qw (cfs_read_file cfs_write_file);; |
1e3baf05 DM |
10 | use PVE::SafeSyslog; |
11 | use PVE::Tools qw(extract_param); | |
f9bfceef | 12 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); |
1e3baf05 DM |
13 | use PVE::Storage; |
14 | use PVE::JSONSchema qw(get_standard_option); | |
15 | use PVE::RESTHandler; | |
ffda963f | 16 | use PVE::QemuConfig; |
1e3baf05 | 17 | use PVE::QemuServer; |
3ea94c60 | 18 | use PVE::QemuMigrate; |
1e3baf05 DM |
19 | use PVE::RPCEnvironment; |
20 | use PVE::AccessControl; | |
21 | use PVE::INotify; | |
de8f60b2 | 22 | use PVE::Network; |
e9abcde6 | 23 | use PVE::Firewall; |
228a998b | 24 | use PVE::API2::Firewall::VM; |
0dbcc8c9 | 25 | use PVE::HA::Env::PVE2; |
2003f0f8 | 26 | use PVE::HA::Config; |
1e3baf05 DM |
27 | |
28 | use Data::Dumper; # fixme: remove | |
29 | ||
30 | use base qw(PVE::RESTHandler); | |
31 | ||
32 | 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."; | |
33 | ||
34 | my $resolve_cdrom_alias = sub { | |
35 | my $param = shift; | |
36 | ||
37 | if (my $value = $param->{cdrom}) { | |
38 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
39 | $param->{ide2} = $value; | |
40 | delete $param->{cdrom}; | |
41 | } | |
42 | }; | |
43 | ||
ae57f6b3 | 44 | my $check_storage_access = sub { |
fcbb753e | 45 | my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_; |
a0d1b1a2 | 46 | |
ae57f6b3 DM |
47 | PVE::QemuServer::foreach_drive($settings, sub { |
48 | my ($ds, $drive) = @_; | |
a0d1b1a2 | 49 | |
ae57f6b3 | 50 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); |
a0d1b1a2 | 51 | |
ae57f6b3 | 52 | my $volid = $drive->{file}; |
a0d1b1a2 | 53 | |
09d0ee64 DM |
54 | if (!$volid || $volid eq 'none') { |
55 | # nothing to check | |
f5782fd0 DM |
56 | } elsif ($isCDROM && ($volid eq 'cdrom')) { |
57 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
09d0ee64 | 58 | } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) { |
a0d1b1a2 DM |
59 | my ($storeid, $size) = ($2 || $default_storage, $3); |
60 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
fcbb753e | 61 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); |
a0d1b1a2 | 62 | } else { |
8b192abf | 63 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); |
a0d1b1a2 DM |
64 | } |
65 | }); | |
ae57f6b3 | 66 | }; |
a0d1b1a2 | 67 | |
9418baad | 68 | my $check_storage_access_clone = sub { |
81f043eb | 69 | my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_; |
6116f729 | 70 | |
55173c6b DM |
71 | my $sharedvm = 1; |
72 | ||
6116f729 DM |
73 | PVE::QemuServer::foreach_drive($conf, sub { |
74 | my ($ds, $drive) = @_; | |
75 | ||
76 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
77 | ||
78 | my $volid = $drive->{file}; | |
79 | ||
80 | return if !$volid || $volid eq 'none'; | |
81 | ||
82 | if ($isCDROM) { | |
83 | if ($volid eq 'cdrom') { | |
84 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
85 | } else { | |
75466c4f | 86 | # we simply allow access |
55173c6b DM |
87 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
88 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
89 | $sharedvm = 0 if !$scfg->{shared}; | |
90 | ||
6116f729 DM |
91 | } |
92 | } else { | |
55173c6b DM |
93 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
94 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
95 | $sharedvm = 0 if !$scfg->{shared}; | |
96 | ||
81f043eb | 97 | $sid = $storage if $storage; |
6116f729 DM |
98 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); |
99 | } | |
100 | }); | |
55173c6b DM |
101 | |
102 | return $sharedvm; | |
6116f729 DM |
103 | }; |
104 | ||
ae57f6b3 DM |
105 | # Note: $pool is only needed when creating a VM, because pool permissions |
106 | # are automatically inherited if VM already exists inside a pool. | |
107 | my $create_disks = sub { | |
108 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
a0d1b1a2 DM |
109 | |
110 | my $vollist = []; | |
a0d1b1a2 | 111 | |
ae57f6b3 DM |
112 | my $res = {}; |
113 | PVE::QemuServer::foreach_drive($settings, sub { | |
114 | my ($ds, $disk) = @_; | |
115 | ||
116 | my $volid = $disk->{file}; | |
117 | ||
f5782fd0 | 118 | if (!$volid || $volid eq 'none' || $volid eq 'cdrom') { |
628e9a2b AD |
119 | delete $disk->{size}; |
120 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
09d0ee64 | 121 | } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) { |
ae57f6b3 DM |
122 | my ($storeid, $size) = ($2 || $default_storage, $3); |
123 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
124 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); | |
125 | my $fmt = $disk->{format} || $defformat; | |
1a35631a DC |
126 | |
127 | my $volid; | |
128 | if ($ds eq 'efidisk0') { | |
129 | # handle efidisk | |
130 | my $ovmfvars = '/usr/share/kvm/OVMF_VARS-pure-efi.fd'; | |
131 | die "uefi vars image not found\n" if ! -f $ovmfvars; | |
132 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, | |
133 | $fmt, undef, 128); | |
134 | $disk->{file} = $volid; | |
135 | $disk->{size} = 128*1024; | |
136 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
137 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); | |
138 | my $qemufmt = PVE::QemuServer::qemu_img_format($scfg, $volname); | |
139 | my $path = PVE::Storage::path($storecfg, $volid); | |
02daf96a | 140 | my $efidiskcmd = ['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $qemufmt]; |
1a35631a DC |
141 | push @$efidiskcmd, $ovmfvars; |
142 | push @$efidiskcmd, $path; | |
02daf96a DC |
143 | |
144 | PVE::Storage::activate_volumes($storecfg, [$volid]); | |
145 | ||
1a35631a DC |
146 | eval { PVE::Tools::run_command($efidiskcmd); }; |
147 | my $err = $@; | |
148 | die "Copying of EFI Vars image failed: $err" if $err; | |
149 | } else { | |
150 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, | |
151 | $fmt, undef, $size*1024*1024); | |
152 | $disk->{file} = $volid; | |
153 | $disk->{size} = $size*1024*1024*1024; | |
154 | } | |
a0d1b1a2 | 155 | push @$vollist, $volid; |
ae57f6b3 DM |
156 | delete $disk->{format}; # no longer needed |
157 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
158 | } else { | |
eabe0da0 | 159 | |
c9928b3d | 160 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); |
75466c4f | 161 | |
7043d946 | 162 | my $volid_is_new = 1; |
35cb731c | 163 | |
7043d946 DM |
164 | if ($conf->{$ds}) { |
165 | my $olddrive = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
166 | $volid_is_new = undef if $olddrive->{file} && $olddrive->{file} eq $volid; | |
eabe0da0 | 167 | } |
75466c4f | 168 | |
d52b8b77 | 169 | if ($volid_is_new) { |
09a89895 | 170 | |
7043d946 DM |
171 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); |
172 | ||
09a89895 | 173 | PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid; |
d52b8b77 DM |
174 | |
175 | my $size = PVE::Storage::volume_size_info($storecfg, $volid); | |
176 | ||
177 | die "volume $volid does not exists\n" if !$size; | |
178 | ||
179 | $disk->{size} = $size; | |
09a89895 | 180 | } |
24afaca0 | 181 | |
24afaca0 | 182 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); |
a0d1b1a2 | 183 | } |
ae57f6b3 | 184 | }); |
a0d1b1a2 DM |
185 | |
186 | # free allocated images on error | |
187 | if (my $err = $@) { | |
188 | syslog('err', "VM $vmid creating disks failed"); | |
189 | foreach my $volid (@$vollist) { | |
190 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
191 | warn $@ if $@; | |
192 | } | |
193 | die $err; | |
194 | } | |
195 | ||
196 | # modify vm config if everything went well | |
ae57f6b3 DM |
197 | foreach my $ds (keys %$res) { |
198 | $conf->{$ds} = $res->{$ds}; | |
a0d1b1a2 DM |
199 | } |
200 | ||
201 | return $vollist; | |
202 | }; | |
203 | ||
58cb690b DC |
204 | my $cpuoptions = { |
205 | 'cores' => 1, | |
206 | 'cpu' => 1, | |
207 | 'cpulimit' => 1, | |
208 | 'cpuunits' => 1, | |
209 | 'numa' => 1, | |
210 | 'smp' => 1, | |
211 | 'sockets' => 1, | |
84b31f48 | 212 | 'vcpus' => 1, |
58cb690b DC |
213 | }; |
214 | ||
215 | my $memoryoptions = { | |
216 | 'memory' => 1, | |
217 | 'balloon' => 1, | |
218 | 'shares' => 1, | |
219 | }; | |
220 | ||
221 | my $hwtypeoptions = { | |
222 | 'acpi' => 1, | |
223 | 'hotplug' => 1, | |
224 | 'kvm' => 1, | |
225 | 'machine' => 1, | |
226 | 'scsihw' => 1, | |
227 | 'smbios1' => 1, | |
228 | 'tablet' => 1, | |
229 | 'vga' => 1, | |
230 | 'watchdog' => 1, | |
231 | }; | |
232 | ||
6bcacc21 | 233 | my $generaloptions = { |
58cb690b DC |
234 | 'agent' => 1, |
235 | 'autostart' => 1, | |
236 | 'bios' => 1, | |
237 | 'description' => 1, | |
238 | 'keyboard' => 1, | |
239 | 'localtime' => 1, | |
240 | 'migrate_downtime' => 1, | |
241 | 'migrate_speed' => 1, | |
242 | 'name' => 1, | |
243 | 'onboot' => 1, | |
244 | 'ostype' => 1, | |
245 | 'protection' => 1, | |
246 | 'reboot' => 1, | |
247 | 'startdate' => 1, | |
248 | 'startup' => 1, | |
249 | 'tdf' => 1, | |
250 | 'template' => 1, | |
251 | }; | |
252 | ||
253 | my $vmpoweroptions = { | |
254 | 'freeze' => 1, | |
255 | }; | |
256 | ||
257 | my $diskoptions = { | |
258 | 'boot' => 1, | |
259 | 'bootdisk' => 1, | |
260 | }; | |
261 | ||
a0d1b1a2 | 262 | my $check_vm_modify_config_perm = sub { |
ae57f6b3 | 263 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; |
a0d1b1a2 | 264 | |
6e5c4da7 | 265 | return 1 if $authuser eq 'root@pam'; |
a0d1b1a2 | 266 | |
ae57f6b3 | 267 | foreach my $opt (@$key_list) { |
a0d1b1a2 | 268 | # disk checks need to be done somewhere else |
74479ee9 | 269 | next if PVE::QemuServer::is_valid_drivename($opt); |
58cb690b | 270 | next if $opt eq 'cdrom'; |
63d269d7 | 271 | next if $opt =~ m/^unused\d+$/; |
a0d1b1a2 | 272 | |
6bcacc21 | 273 | if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) { |
a0d1b1a2 | 274 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); |
58cb690b | 275 | } elsif ($memoryoptions->{$opt}) { |
a0d1b1a2 | 276 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); |
58cb690b | 277 | } elsif ($hwtypeoptions->{$opt}) { |
a0d1b1a2 | 278 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); |
6bcacc21 | 279 | } elsif ($generaloptions->{$opt}) { |
58cb690b DC |
280 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); |
281 | # special case for startup since it changes host behaviour | |
282 | if ($opt eq 'startup') { | |
283 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']); | |
284 | } | |
285 | } elsif ($vmpoweroptions->{$opt}) { | |
286 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']); | |
287 | } elsif ($diskoptions->{$opt}) { | |
288 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
a0d1b1a2 DM |
289 | } elsif ($opt =~ m/^net\d+$/) { |
290 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
291 | } else { | |
58cb690b DC |
292 | # catches usb\d+, hostpci\d+, args, lock, etc. |
293 | # new options will be checked here | |
294 | die "only root can set '$opt' config\n"; | |
a0d1b1a2 DM |
295 | } |
296 | } | |
297 | ||
298 | return 1; | |
299 | }; | |
300 | ||
1e3baf05 | 301 | __PACKAGE__->register_method({ |
afdb31d5 DM |
302 | name => 'vmlist', |
303 | path => '', | |
1e3baf05 DM |
304 | method => 'GET', |
305 | description => "Virtual machine index (per node).", | |
a0d1b1a2 DM |
306 | permissions => { |
307 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
308 | user => 'all', | |
309 | }, | |
1e3baf05 DM |
310 | proxyto => 'node', |
311 | protected => 1, # qemu pid files are only readable by root | |
312 | parameters => { | |
313 | additionalProperties => 0, | |
314 | properties => { | |
315 | node => get_standard_option('pve-node'), | |
12612b09 WB |
316 | full => { |
317 | type => 'boolean', | |
318 | optional => 1, | |
319 | description => "Determine the full status of active VMs.", | |
320 | }, | |
1e3baf05 DM |
321 | }, |
322 | }, | |
323 | returns => { | |
324 | type => 'array', | |
325 | items => { | |
326 | type => "object", | |
327 | properties => {}, | |
328 | }, | |
329 | links => [ { rel => 'child', href => "{vmid}" } ], | |
330 | }, | |
331 | code => sub { | |
332 | my ($param) = @_; | |
333 | ||
a0d1b1a2 DM |
334 | my $rpcenv = PVE::RPCEnvironment::get(); |
335 | my $authuser = $rpcenv->get_user(); | |
336 | ||
12612b09 | 337 | my $vmstatus = PVE::QemuServer::vmstatus(undef, $param->{full}); |
1e3baf05 | 338 | |
a0d1b1a2 DM |
339 | my $res = []; |
340 | foreach my $vmid (keys %$vmstatus) { | |
341 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
342 | ||
343 | my $data = $vmstatus->{$vmid}; | |
184955dc | 344 | $data->{vmid} = int($vmid); |
a0d1b1a2 DM |
345 | push @$res, $data; |
346 | } | |
1e3baf05 | 347 | |
a0d1b1a2 | 348 | return $res; |
1e3baf05 DM |
349 | }}); |
350 | ||
d703d4c0 | 351 | |
d703d4c0 | 352 | |
1e3baf05 | 353 | __PACKAGE__->register_method({ |
afdb31d5 DM |
354 | name => 'create_vm', |
355 | path => '', | |
1e3baf05 | 356 | method => 'POST', |
3e16d5fc | 357 | description => "Create or restore a virtual machine.", |
a0d1b1a2 | 358 | permissions => { |
f9bfceef DM |
359 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . |
360 | "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
361 | "If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
362 | user => 'all', # check inside | |
a0d1b1a2 | 363 | }, |
1e3baf05 DM |
364 | protected => 1, |
365 | proxyto => 'node', | |
366 | parameters => { | |
367 | additionalProperties => 0, | |
368 | properties => PVE::QemuServer::json_config_properties( | |
369 | { | |
370 | node => get_standard_option('pve-node'), | |
65e866e5 | 371 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), |
3e16d5fc DM |
372 | archive => { |
373 | description => "The backup file.", | |
374 | type => 'string', | |
375 | optional => 1, | |
376 | maxLength => 255, | |
65e866e5 | 377 | completion => \&PVE::QemuServer::complete_backup_archives, |
3e16d5fc DM |
378 | }, |
379 | storage => get_standard_option('pve-storage-id', { | |
380 | description => "Default storage.", | |
381 | optional => 1, | |
335af808 | 382 | completion => \&PVE::QemuServer::complete_storage, |
3e16d5fc DM |
383 | }), |
384 | force => { | |
afdb31d5 | 385 | optional => 1, |
3e16d5fc DM |
386 | type => 'boolean', |
387 | description => "Allow to overwrite existing VM.", | |
51586c3a DM |
388 | requires => 'archive', |
389 | }, | |
390 | unique => { | |
afdb31d5 | 391 | optional => 1, |
51586c3a DM |
392 | type => 'boolean', |
393 | description => "Assign a unique random ethernet address.", | |
394 | requires => 'archive', | |
3e16d5fc | 395 | }, |
75466c4f | 396 | pool => { |
a0d1b1a2 DM |
397 | optional => 1, |
398 | type => 'string', format => 'pve-poolid', | |
399 | description => "Add the VM to the specified pool.", | |
400 | }, | |
1e3baf05 DM |
401 | }), |
402 | }, | |
afdb31d5 | 403 | returns => { |
5fdbe4f0 DM |
404 | type => 'string', |
405 | }, | |
1e3baf05 DM |
406 | code => sub { |
407 | my ($param) = @_; | |
408 | ||
5fdbe4f0 DM |
409 | my $rpcenv = PVE::RPCEnvironment::get(); |
410 | ||
a0d1b1a2 | 411 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 | 412 | |
1e3baf05 DM |
413 | my $node = extract_param($param, 'node'); |
414 | ||
1e3baf05 DM |
415 | my $vmid = extract_param($param, 'vmid'); |
416 | ||
3e16d5fc DM |
417 | my $archive = extract_param($param, 'archive'); |
418 | ||
419 | my $storage = extract_param($param, 'storage'); | |
420 | ||
51586c3a DM |
421 | my $force = extract_param($param, 'force'); |
422 | ||
423 | my $unique = extract_param($param, 'unique'); | |
75466c4f | 424 | |
a0d1b1a2 | 425 | my $pool = extract_param($param, 'pool'); |
51586c3a | 426 | |
ffda963f | 427 | my $filename = PVE::QemuConfig->config_file($vmid); |
afdb31d5 DM |
428 | |
429 | my $storecfg = PVE::Storage::config(); | |
1e3baf05 | 430 | |
3e16d5fc | 431 | PVE::Cluster::check_cfs_quorum(); |
1e3baf05 | 432 | |
a0d1b1a2 DM |
433 | if (defined($pool)) { |
434 | $rpcenv->check_pool_exist($pool); | |
75466c4f | 435 | } |
a0d1b1a2 | 436 | |
fcbb753e | 437 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']) |
a0d1b1a2 DM |
438 | if defined($storage); |
439 | ||
f9bfceef DM |
440 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { |
441 | # OK | |
442 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
443 | # OK | |
444 | } elsif ($archive && $force && (-f $filename) && | |
445 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
446 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
447 | } else { | |
448 | raise_perm_exc(); | |
449 | } | |
450 | ||
afdb31d5 | 451 | if (!$archive) { |
3e16d5fc | 452 | &$resolve_cdrom_alias($param); |
1e3baf05 | 453 | |
fcbb753e | 454 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); |
ae57f6b3 DM |
455 | |
456 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
457 | ||
3e16d5fc | 458 | foreach my $opt (keys %$param) { |
74479ee9 | 459 | if (PVE::QemuServer::is_valid_drivename($opt)) { |
3e16d5fc DM |
460 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); |
461 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
afdb31d5 | 462 | |
3e16d5fc DM |
463 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); |
464 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
465 | } | |
1e3baf05 | 466 | } |
3e16d5fc DM |
467 | |
468 | PVE::QemuServer::add_random_macs($param); | |
51586c3a DM |
469 | } else { |
470 | my $keystr = join(' ', keys %$param); | |
bc4dcb99 DM |
471 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; |
472 | ||
5b9d692a | 473 | if ($archive eq '-') { |
afdb31d5 | 474 | die "pipe requires cli environment\n" |
d7810bc1 | 475 | if $rpcenv->{type} ne 'cli'; |
5b9d692a | 476 | } else { |
c9928b3d DM |
477 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive); |
478 | $archive = PVE::Storage::abs_filesystem_path($storecfg, $archive); | |
971f27c4 | 479 | } |
1e3baf05 DM |
480 | } |
481 | ||
3e16d5fc | 482 | my $restorefn = sub { |
4d8d55f1 | 483 | my $vmlist = PVE::Cluster::get_vmlist(); |
4d8d55f1 | 484 | if ($vmlist->{ids}->{$vmid}) { |
0152058a DM |
485 | my $current_node = $vmlist->{ids}->{$vmid}->{node}; |
486 | if ($current_node eq $node) { | |
ffda963f | 487 | my $conf = PVE::QemuConfig->load_config($vmid); |
3e16d5fc | 488 | |
ffda963f | 489 | PVE::QemuConfig->check_protection($conf, "unable to restore VM $vmid"); |
3e16d5fc | 490 | |
4d8d55f1 AG |
491 | die "unable to restore vm $vmid - config file already exists\n" |
492 | if !$force; | |
493 | ||
494 | die "unable to restore vm $vmid - vm is running\n" | |
495 | if PVE::QemuServer::check_running($vmid); | |
3a07a8a9 FG |
496 | |
497 | die "unable to restore vm $vmid - vm is a template\n" | |
498 | if PVE::QemuConfig->is_template($conf); | |
499 | ||
4d8d55f1 | 500 | } else { |
0152058a | 501 | die "unable to restore vm $vmid - already existing on cluster node '$current_node'\n"; |
4d8d55f1 | 502 | } |
3e16d5fc DM |
503 | } |
504 | ||
505 | my $realcmd = sub { | |
a0d1b1a2 | 506 | PVE::QemuServer::restore_archive($archive, $vmid, $authuser, { |
51586c3a | 507 | storage => $storage, |
a0d1b1a2 | 508 | pool => $pool, |
51586c3a | 509 | unique => $unique }); |
502d18a2 | 510 | |
be517049 | 511 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
3e16d5fc DM |
512 | }; |
513 | ||
a0d1b1a2 | 514 | return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); |
3e16d5fc | 515 | }; |
1e3baf05 | 516 | |
1e3baf05 DM |
517 | my $createfn = sub { |
518 | ||
191435c6 | 519 | # test after locking |
5f20325f | 520 | PVE::Cluster::check_vmid_unused($vmid); |
1e3baf05 | 521 | |
5fdbe4f0 | 522 | my $realcmd = sub { |
1e3baf05 | 523 | |
5fdbe4f0 | 524 | my $vollist = []; |
1e3baf05 | 525 | |
1858638f DM |
526 | my $conf = $param; |
527 | ||
5fdbe4f0 | 528 | eval { |
ae57f6b3 | 529 | |
1858638f | 530 | $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage); |
1e3baf05 | 531 | |
5fdbe4f0 | 532 | # try to be smart about bootdisk |
74479ee9 | 533 | my @disks = PVE::QemuServer::valid_drive_names(); |
5fdbe4f0 DM |
534 | my $firstdisk; |
535 | foreach my $ds (reverse @disks) { | |
1858638f DM |
536 | next if !$conf->{$ds}; |
537 | my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
5fdbe4f0 DM |
538 | next if PVE::QemuServer::drive_is_cdrom($disk); |
539 | $firstdisk = $ds; | |
540 | } | |
1e3baf05 | 541 | |
1858638f DM |
542 | if (!$conf->{bootdisk} && $firstdisk) { |
543 | $conf->{bootdisk} = $firstdisk; | |
5fdbe4f0 | 544 | } |
1e3baf05 | 545 | |
47314bf5 DM |
546 | # auto generate uuid if user did not specify smbios1 option |
547 | if (!$conf->{smbios1}) { | |
548 | my ($uuid, $uuid_str); | |
549 | UUID::generate($uuid); | |
550 | UUID::unparse($uuid, $uuid_str); | |
551 | $conf->{smbios1} = "uuid=$uuid_str"; | |
552 | } | |
553 | ||
ffda963f | 554 | PVE::QemuConfig->write_config($vmid, $conf); |
ae9ca91d | 555 | |
5fdbe4f0 DM |
556 | }; |
557 | my $err = $@; | |
1e3baf05 | 558 | |
5fdbe4f0 DM |
559 | if ($err) { |
560 | foreach my $volid (@$vollist) { | |
561 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
562 | warn $@ if $@; | |
563 | } | |
564 | die "create failed - $err"; | |
565 | } | |
502d18a2 | 566 | |
be517049 | 567 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
5fdbe4f0 DM |
568 | }; |
569 | ||
a0d1b1a2 | 570 | return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
571 | }; |
572 | ||
ffda963f | 573 | return PVE::QemuConfig->lock_config_full($vmid, 1, $archive ? $restorefn : $createfn); |
1e3baf05 DM |
574 | }}); |
575 | ||
576 | __PACKAGE__->register_method({ | |
577 | name => 'vmdiridx', | |
afdb31d5 | 578 | path => '{vmid}', |
1e3baf05 DM |
579 | method => 'GET', |
580 | proxyto => 'node', | |
581 | description => "Directory index", | |
a0d1b1a2 DM |
582 | permissions => { |
583 | user => 'all', | |
584 | }, | |
1e3baf05 DM |
585 | parameters => { |
586 | additionalProperties => 0, | |
587 | properties => { | |
588 | node => get_standard_option('pve-node'), | |
589 | vmid => get_standard_option('pve-vmid'), | |
590 | }, | |
591 | }, | |
592 | returns => { | |
593 | type => 'array', | |
594 | items => { | |
595 | type => "object", | |
596 | properties => { | |
597 | subdir => { type => 'string' }, | |
598 | }, | |
599 | }, | |
600 | links => [ { rel => 'child', href => "{subdir}" } ], | |
601 | }, | |
602 | code => sub { | |
603 | my ($param) = @_; | |
604 | ||
605 | my $res = [ | |
606 | { subdir => 'config' }, | |
df2a2dbb | 607 | { subdir => 'pending' }, |
1e3baf05 DM |
608 | { subdir => 'status' }, |
609 | { subdir => 'unlink' }, | |
610 | { subdir => 'vncproxy' }, | |
3ea94c60 | 611 | { subdir => 'migrate' }, |
2f48a4f5 | 612 | { subdir => 'resize' }, |
586bfa78 | 613 | { subdir => 'move' }, |
1e3baf05 DM |
614 | { subdir => 'rrd' }, |
615 | { subdir => 'rrddata' }, | |
91c94f0a | 616 | { subdir => 'monitor' }, |
7e7d7b61 | 617 | { subdir => 'snapshot' }, |
288eeea8 | 618 | { subdir => 'spiceproxy' }, |
7aa608d6 | 619 | { subdir => 'sendkey' }, |
228a998b | 620 | { subdir => 'firewall' }, |
1e3baf05 | 621 | ]; |
afdb31d5 | 622 | |
1e3baf05 DM |
623 | return $res; |
624 | }}); | |
625 | ||
228a998b | 626 | __PACKAGE__->register_method ({ |
f34ebd52 | 627 | subclass => "PVE::API2::Firewall::VM", |
228a998b DM |
628 | path => '{vmid}/firewall', |
629 | }); | |
630 | ||
1e3baf05 | 631 | __PACKAGE__->register_method({ |
afdb31d5 DM |
632 | name => 'rrd', |
633 | path => '{vmid}/rrd', | |
1e3baf05 DM |
634 | method => 'GET', |
635 | protected => 1, # fixme: can we avoid that? | |
636 | permissions => { | |
378b359e | 637 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
638 | }, |
639 | description => "Read VM RRD statistics (returns PNG)", | |
640 | parameters => { | |
641 | additionalProperties => 0, | |
642 | properties => { | |
643 | node => get_standard_option('pve-node'), | |
644 | vmid => get_standard_option('pve-vmid'), | |
645 | timeframe => { | |
646 | description => "Specify the time frame you are interested in.", | |
647 | type => 'string', | |
648 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
649 | }, | |
650 | ds => { | |
651 | description => "The list of datasources you want to display.", | |
652 | type => 'string', format => 'pve-configid-list', | |
653 | }, | |
654 | cf => { | |
655 | description => "The RRD consolidation function", | |
656 | type => 'string', | |
657 | enum => [ 'AVERAGE', 'MAX' ], | |
658 | optional => 1, | |
659 | }, | |
660 | }, | |
661 | }, | |
662 | returns => { | |
663 | type => "object", | |
664 | properties => { | |
665 | filename => { type => 'string' }, | |
666 | }, | |
667 | }, | |
668 | code => sub { | |
669 | my ($param) = @_; | |
670 | ||
671 | return PVE::Cluster::create_rrd_graph( | |
afdb31d5 | 672 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
1e3baf05 | 673 | $param->{ds}, $param->{cf}); |
afdb31d5 | 674 | |
1e3baf05 DM |
675 | }}); |
676 | ||
677 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
678 | name => 'rrddata', |
679 | path => '{vmid}/rrddata', | |
1e3baf05 DM |
680 | method => 'GET', |
681 | protected => 1, # fixme: can we avoid that? | |
682 | permissions => { | |
378b359e | 683 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
684 | }, |
685 | description => "Read VM RRD statistics", | |
686 | parameters => { | |
687 | additionalProperties => 0, | |
688 | properties => { | |
689 | node => get_standard_option('pve-node'), | |
690 | vmid => get_standard_option('pve-vmid'), | |
691 | timeframe => { | |
692 | description => "Specify the time frame you are interested in.", | |
693 | type => 'string', | |
694 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
695 | }, | |
696 | cf => { | |
697 | description => "The RRD consolidation function", | |
698 | type => 'string', | |
699 | enum => [ 'AVERAGE', 'MAX' ], | |
700 | optional => 1, | |
701 | }, | |
702 | }, | |
703 | }, | |
704 | returns => { | |
705 | type => "array", | |
706 | items => { | |
707 | type => "object", | |
708 | properties => {}, | |
709 | }, | |
710 | }, | |
711 | code => sub { | |
712 | my ($param) = @_; | |
713 | ||
714 | return PVE::Cluster::create_rrd_data( | |
715 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
716 | }}); | |
717 | ||
718 | ||
719 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
720 | name => 'vm_config', |
721 | path => '{vmid}/config', | |
1e3baf05 DM |
722 | method => 'GET', |
723 | proxyto => 'node', | |
1e7f2726 | 724 | description => "Get current virtual machine configuration. This does not include pending configuration changes (see 'pending' API).", |
a0d1b1a2 DM |
725 | permissions => { |
726 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
727 | }, | |
1e3baf05 DM |
728 | parameters => { |
729 | additionalProperties => 0, | |
730 | properties => { | |
731 | node => get_standard_option('pve-node'), | |
335af808 | 732 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
6d89b548 DM |
733 | current => { |
734 | description => "Get current values (instead of pending values).", | |
735 | optional => 1, | |
736 | default => 0, | |
737 | type => 'boolean', | |
738 | }, | |
1e3baf05 DM |
739 | }, |
740 | }, | |
afdb31d5 | 741 | returns => { |
1e3baf05 | 742 | type => "object", |
554ac7e7 DM |
743 | properties => { |
744 | digest => { | |
745 | type => 'string', | |
746 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
747 | } | |
748 | }, | |
1e3baf05 DM |
749 | }, |
750 | code => sub { | |
751 | my ($param) = @_; | |
752 | ||
ffda963f | 753 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
1e3baf05 | 754 | |
22c377f0 | 755 | delete $conf->{snapshots}; |
6d89b548 DM |
756 | |
757 | if (!$param->{current}) { | |
025e1d90 | 758 | foreach my $opt (keys %{$conf->{pending}}) { |
6d89b548 DM |
759 | next if $opt eq 'delete'; |
760 | my $value = $conf->{pending}->{$opt}; | |
761 | next if ref($value); # just to be sure | |
762 | $conf->{$opt} = $value; | |
763 | } | |
1bc483f6 WB |
764 | my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete}); |
765 | foreach my $opt (keys %$pending_delete_hash) { | |
6d89b548 DM |
766 | delete $conf->{$opt} if $conf->{$opt}; |
767 | } | |
768 | } | |
769 | ||
1e7f2726 | 770 | delete $conf->{pending}; |
22c377f0 | 771 | |
1e3baf05 DM |
772 | return $conf; |
773 | }}); | |
774 | ||
1e7f2726 DM |
775 | __PACKAGE__->register_method({ |
776 | name => 'vm_pending', | |
777 | path => '{vmid}/pending', | |
778 | method => 'GET', | |
779 | proxyto => 'node', | |
780 | description => "Get virtual machine configuration, including pending changes.", | |
781 | permissions => { | |
782 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
783 | }, | |
784 | parameters => { | |
785 | additionalProperties => 0, | |
786 | properties => { | |
787 | node => get_standard_option('pve-node'), | |
335af808 | 788 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
1e7f2726 DM |
789 | }, |
790 | }, | |
791 | returns => { | |
792 | type => "array", | |
793 | items => { | |
794 | type => "object", | |
795 | properties => { | |
796 | key => { | |
797 | description => "Configuration option name.", | |
798 | type => 'string', | |
799 | }, | |
800 | value => { | |
801 | description => "Current value.", | |
802 | type => 'string', | |
803 | optional => 1, | |
804 | }, | |
805 | pending => { | |
806 | description => "Pending value.", | |
807 | type => 'string', | |
808 | optional => 1, | |
809 | }, | |
810 | delete => { | |
1bc483f6 WB |
811 | description => "Indicates a pending delete request if present and not 0. " . |
812 | "The value 2 indicates a force-delete request.", | |
813 | type => 'integer', | |
814 | minimum => 0, | |
815 | maximum => 2, | |
1e7f2726 DM |
816 | optional => 1, |
817 | }, | |
818 | }, | |
819 | }, | |
820 | }, | |
821 | code => sub { | |
822 | my ($param) = @_; | |
823 | ||
ffda963f | 824 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
1e7f2726 | 825 | |
1bc483f6 | 826 | my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete}); |
1e7f2726 DM |
827 | |
828 | my $res = []; | |
829 | ||
025e1d90 | 830 | foreach my $opt (keys %$conf) { |
1e7f2726 DM |
831 | next if ref($conf->{$opt}); |
832 | my $item = { key => $opt }; | |
833 | $item->{value} = $conf->{$opt} if defined($conf->{$opt}); | |
834 | $item->{pending} = $conf->{pending}->{$opt} if defined($conf->{pending}->{$opt}); | |
1bc483f6 | 835 | $item->{delete} = ($pending_delete_hash->{$opt} ? 2 : 1) if exists $pending_delete_hash->{$opt}; |
1e7f2726 DM |
836 | push @$res, $item; |
837 | } | |
838 | ||
025e1d90 | 839 | foreach my $opt (keys %{$conf->{pending}}) { |
1e7f2726 DM |
840 | next if $opt eq 'delete'; |
841 | next if ref($conf->{pending}->{$opt}); # just to be sure | |
0e54e1c8 | 842 | next if defined($conf->{$opt}); |
1e7f2726 DM |
843 | my $item = { key => $opt }; |
844 | $item->{pending} = $conf->{pending}->{$opt}; | |
845 | push @$res, $item; | |
846 | } | |
847 | ||
1bc483f6 | 848 | while (my ($opt, $force) = each %$pending_delete_hash) { |
1e7f2726 DM |
849 | next if $conf->{pending}->{$opt}; # just to be sure |
850 | next if $conf->{$opt}; | |
1bc483f6 | 851 | my $item = { key => $opt, delete => ($force ? 2 : 1)}; |
1e7f2726 DM |
852 | push @$res, $item; |
853 | } | |
854 | ||
855 | return $res; | |
856 | }}); | |
857 | ||
5555edea DM |
858 | # POST/PUT {vmid}/config implementation |
859 | # | |
860 | # The original API used PUT (idempotent) an we assumed that all operations | |
861 | # are fast. But it turned out that almost any configuration change can | |
862 | # involve hot-plug actions, or disk alloc/free. Such actions can take long | |
863 | # time to complete and have side effects (not idempotent). | |
864 | # | |
7043d946 | 865 | # The new implementation uses POST and forks a worker process. We added |
5555edea | 866 | # a new option 'background_delay'. If specified we wait up to |
7043d946 | 867 | # 'background_delay' second for the worker task to complete. It returns null |
5555edea | 868 | # if the task is finished within that time, else we return the UPID. |
7043d946 | 869 | |
5555edea DM |
870 | my $update_vm_api = sub { |
871 | my ($param, $sync) = @_; | |
a0d1b1a2 | 872 | |
5555edea | 873 | my $rpcenv = PVE::RPCEnvironment::get(); |
1e3baf05 | 874 | |
5555edea | 875 | my $authuser = $rpcenv->get_user(); |
1e3baf05 | 876 | |
5555edea | 877 | my $node = extract_param($param, 'node'); |
1e3baf05 | 878 | |
5555edea | 879 | my $vmid = extract_param($param, 'vmid'); |
1e3baf05 | 880 | |
5555edea | 881 | my $digest = extract_param($param, 'digest'); |
1e3baf05 | 882 | |
5555edea | 883 | my $background_delay = extract_param($param, 'background_delay'); |
1e3baf05 | 884 | |
5555edea DM |
885 | my @paramarr = (); # used for log message |
886 | foreach my $key (keys %$param) { | |
c13e17d0 | 887 | push @paramarr, "-$key", $param->{$key}; |
5555edea | 888 | } |
0532bc63 | 889 | |
5555edea DM |
890 | my $skiplock = extract_param($param, 'skiplock'); |
891 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
892 | if $skiplock && $authuser ne 'root@pam'; | |
1e3baf05 | 893 | |
5555edea | 894 | my $delete_str = extract_param($param, 'delete'); |
0532bc63 | 895 | |
d3df8cf3 DM |
896 | my $revert_str = extract_param($param, 'revert'); |
897 | ||
5555edea | 898 | my $force = extract_param($param, 'force'); |
1e68cb19 | 899 | |
d3df8cf3 | 900 | die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param); |
7bfdeb5f | 901 | |
5555edea | 902 | my $storecfg = PVE::Storage::config(); |
1e68cb19 | 903 | |
5555edea | 904 | my $defaults = PVE::QemuServer::load_defaults(); |
1e68cb19 | 905 | |
5555edea | 906 | &$resolve_cdrom_alias($param); |
0532bc63 | 907 | |
5555edea | 908 | # now try to verify all parameters |
ae57f6b3 | 909 | |
d3df8cf3 DM |
910 | my $revert = {}; |
911 | foreach my $opt (PVE::Tools::split_list($revert_str)) { | |
912 | if (!PVE::QemuServer::option_exists($opt)) { | |
913 | raise_param_exc({ revert => "unknown option '$opt'" }); | |
914 | } | |
915 | ||
916 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
917 | "-revert $opt' at the same time" }) | |
918 | if defined($param->{$opt}); | |
919 | ||
920 | $revert->{$opt} = 1; | |
921 | } | |
922 | ||
5555edea DM |
923 | my @delete = (); |
924 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
925 | $opt = 'ide2' if $opt eq 'cdrom'; | |
d3df8cf3 | 926 | |
5555edea DM |
927 | raise_param_exc({ delete => "you can't use '-$opt' and " . |
928 | "-delete $opt' at the same time" }) | |
929 | if defined($param->{$opt}); | |
7043d946 | 930 | |
d3df8cf3 DM |
931 | raise_param_exc({ revert => "you can't use '-delete $opt' and " . |
932 | "-revert $opt' at the same time" }) | |
933 | if $revert->{$opt}; | |
934 | ||
5555edea DM |
935 | if (!PVE::QemuServer::option_exists($opt)) { |
936 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
0532bc63 | 937 | } |
1e3baf05 | 938 | |
5555edea DM |
939 | push @delete, $opt; |
940 | } | |
941 | ||
942 | foreach my $opt (keys %$param) { | |
74479ee9 | 943 | if (PVE::QemuServer::is_valid_drivename($opt)) { |
5555edea DM |
944 | # cleanup drive path |
945 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
f9091201 | 946 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; |
5555edea DM |
947 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); |
948 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
949 | } elsif ($opt =~ m/^net(\d+)$/) { | |
950 | # add macaddr | |
951 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
952 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
1e68cb19 | 953 | } |
5555edea | 954 | } |
1e3baf05 | 955 | |
5555edea | 956 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); |
ae57f6b3 | 957 | |
5555edea | 958 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); |
ae57f6b3 | 959 | |
5555edea | 960 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); |
1e3baf05 | 961 | |
5555edea | 962 | my $updatefn = sub { |
1e3baf05 | 963 | |
ffda963f | 964 | my $conf = PVE::QemuConfig->load_config($vmid); |
1e3baf05 | 965 | |
5555edea DM |
966 | die "checksum missmatch (file change by other user?)\n" |
967 | if $digest && $digest ne $conf->{digest}; | |
968 | ||
ffda963f | 969 | PVE::QemuConfig->check_lock($conf) if !$skiplock; |
7043d946 | 970 | |
d3df8cf3 DM |
971 | foreach my $opt (keys %$revert) { |
972 | if (defined($conf->{$opt})) { | |
973 | $param->{$opt} = $conf->{$opt}; | |
974 | } elsif (defined($conf->{pending}->{$opt})) { | |
975 | push @delete, $opt; | |
976 | } | |
977 | } | |
978 | ||
5555edea | 979 | if ($param->{memory} || defined($param->{balloon})) { |
6ca8b698 DM |
980 | my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory}; |
981 | my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon}; | |
7043d946 | 982 | |
5555edea DM |
983 | die "balloon value too large (must be smaller than assigned memory)\n" |
984 | if $balloon && $balloon > $maxmem; | |
985 | } | |
1e3baf05 | 986 | |
5555edea | 987 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); |
1e3baf05 | 988 | |
5555edea | 989 | my $worker = sub { |
7bfdeb5f | 990 | |
5555edea | 991 | print "update VM $vmid: " . join (' ', @paramarr) . "\n"; |
c2a64aa7 | 992 | |
202d1f45 DM |
993 | # write updates to pending section |
994 | ||
3a11fadb DM |
995 | my $modified = {}; # record what $option we modify |
996 | ||
202d1f45 | 997 | foreach my $opt (@delete) { |
3a11fadb | 998 | $modified->{$opt} = 1; |
ffda963f | 999 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
202d1f45 | 1000 | if ($opt =~ m/^unused/) { |
202d1f45 | 1001 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); |
ffda963f | 1002 | PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'"); |
4d8d55f1 | 1003 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); |
3dc38fbb WB |
1004 | if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) { |
1005 | delete $conf->{$opt}; | |
ffda963f | 1006 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1007 | } |
74479ee9 | 1008 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { |
ffda963f | 1009 | PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'"); |
202d1f45 | 1010 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); |
055d554d | 1011 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) |
202d1f45 | 1012 | if defined($conf->{pending}->{$opt}); |
3dc38fbb | 1013 | PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); |
ffda963f | 1014 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1015 | } else { |
3dc38fbb | 1016 | PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); |
ffda963f | 1017 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1018 | } |
5d39a182 | 1019 | } |
1e3baf05 | 1020 | |
202d1f45 | 1021 | foreach my $opt (keys %$param) { # add/change |
3a11fadb | 1022 | $modified->{$opt} = 1; |
ffda963f | 1023 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
202d1f45 DM |
1024 | next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed |
1025 | ||
74479ee9 | 1026 | if (PVE::QemuServer::is_valid_drivename($opt)) { |
202d1f45 DM |
1027 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); |
1028 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM | |
1029 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
1030 | } else { | |
1031 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
1032 | } | |
055d554d | 1033 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) |
202d1f45 DM |
1034 | if defined($conf->{pending}->{$opt}); |
1035 | ||
1036 | &$create_disks($rpcenv, $authuser, $conf->{pending}, $storecfg, $vmid, undef, {$opt => $param->{$opt}}); | |
1037 | } else { | |
1038 | $conf->{pending}->{$opt} = $param->{$opt}; | |
1039 | } | |
055d554d | 1040 | PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt); |
ffda963f | 1041 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 DM |
1042 | } |
1043 | ||
1044 | # remove pending changes when nothing changed | |
ffda963f | 1045 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
c750e90a | 1046 | my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf); |
ffda963f | 1047 | PVE::QemuConfig->write_config($vmid, $conf) if $changes; |
202d1f45 DM |
1048 | |
1049 | return if !scalar(keys %{$conf->{pending}}); | |
1050 | ||
7bfdeb5f | 1051 | my $running = PVE::QemuServer::check_running($vmid); |
39001640 DM |
1052 | |
1053 | # apply pending changes | |
1054 | ||
ffda963f | 1055 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
39001640 | 1056 | |
3a11fadb DM |
1057 | if ($running) { |
1058 | my $errors = {}; | |
1059 | PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors); | |
1060 | raise_param_exc($errors) if scalar(keys %$errors); | |
1061 | } else { | |
1062 | PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running); | |
1063 | } | |
1e68cb19 | 1064 | |
915d3481 | 1065 | return; |
5d39a182 DM |
1066 | }; |
1067 | ||
5555edea DM |
1068 | if ($sync) { |
1069 | &$worker(); | |
1070 | return undef; | |
1071 | } else { | |
1072 | my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker); | |
fcdb0117 | 1073 | |
5555edea DM |
1074 | if ($background_delay) { |
1075 | ||
1076 | # Note: It would be better to do that in the Event based HTTPServer | |
7043d946 | 1077 | # to avoid blocking call to sleep. |
5555edea DM |
1078 | |
1079 | my $end_time = time() + $background_delay; | |
1080 | ||
1081 | my $task = PVE::Tools::upid_decode($upid); | |
1082 | ||
1083 | my $running = 1; | |
1084 | while (time() < $end_time) { | |
1085 | $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}); | |
1086 | last if !$running; | |
1087 | sleep(1); # this gets interrupted when child process ends | |
1088 | } | |
1089 | ||
1090 | if (!$running) { | |
1091 | my $status = PVE::Tools::upid_read_status($upid); | |
1092 | return undef if $status eq 'OK'; | |
1093 | die $status; | |
1094 | } | |
7043d946 | 1095 | } |
5555edea DM |
1096 | |
1097 | return $upid; | |
1098 | } | |
1099 | }; | |
1100 | ||
ffda963f | 1101 | return PVE::QemuConfig->lock_config($vmid, $updatefn); |
5555edea DM |
1102 | }; |
1103 | ||
1104 | my $vm_config_perm_list = [ | |
1105 | 'VM.Config.Disk', | |
1106 | 'VM.Config.CDROM', | |
1107 | 'VM.Config.CPU', | |
1108 | 'VM.Config.Memory', | |
1109 | 'VM.Config.Network', | |
1110 | 'VM.Config.HWType', | |
1111 | 'VM.Config.Options', | |
1112 | ]; | |
1113 | ||
1114 | __PACKAGE__->register_method({ | |
1115 | name => 'update_vm_async', | |
1116 | path => '{vmid}/config', | |
1117 | method => 'POST', | |
1118 | protected => 1, | |
1119 | proxyto => 'node', | |
1120 | description => "Set virtual machine options (asynchrounous API).", | |
1121 | permissions => { | |
1122 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1123 | }, | |
1124 | parameters => { | |
1125 | additionalProperties => 0, | |
1126 | properties => PVE::QemuServer::json_config_properties( | |
1127 | { | |
1128 | node => get_standard_option('pve-node'), | |
1129 | vmid => get_standard_option('pve-vmid'), | |
1130 | skiplock => get_standard_option('skiplock'), | |
1131 | delete => { | |
1132 | type => 'string', format => 'pve-configid-list', | |
1133 | description => "A list of settings you want to delete.", | |
1134 | optional => 1, | |
1135 | }, | |
4c8365fa DM |
1136 | revert => { |
1137 | type => 'string', format => 'pve-configid-list', | |
1138 | description => "Revert a pending change.", | |
1139 | optional => 1, | |
1140 | }, | |
5555edea DM |
1141 | force => { |
1142 | type => 'boolean', | |
1143 | description => $opt_force_description, | |
1144 | optional => 1, | |
1145 | requires => 'delete', | |
1146 | }, | |
1147 | digest => { | |
1148 | type => 'string', | |
1149 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1150 | maxLength => 40, | |
1151 | optional => 1, | |
1152 | }, | |
1153 | background_delay => { | |
1154 | type => 'integer', | |
1155 | description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.", | |
1156 | minimum => 1, | |
1157 | maximum => 30, | |
1158 | optional => 1, | |
1159 | }, | |
1160 | }), | |
1161 | }, | |
1162 | returns => { | |
1163 | type => 'string', | |
1164 | optional => 1, | |
1165 | }, | |
1166 | code => $update_vm_api, | |
1167 | }); | |
1168 | ||
1169 | __PACKAGE__->register_method({ | |
1170 | name => 'update_vm', | |
1171 | path => '{vmid}/config', | |
1172 | method => 'PUT', | |
1173 | protected => 1, | |
1174 | proxyto => 'node', | |
1175 | description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.", | |
1176 | permissions => { | |
1177 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1178 | }, | |
1179 | parameters => { | |
1180 | additionalProperties => 0, | |
1181 | properties => PVE::QemuServer::json_config_properties( | |
1182 | { | |
1183 | node => get_standard_option('pve-node'), | |
335af808 | 1184 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
5555edea DM |
1185 | skiplock => get_standard_option('skiplock'), |
1186 | delete => { | |
1187 | type => 'string', format => 'pve-configid-list', | |
1188 | description => "A list of settings you want to delete.", | |
1189 | optional => 1, | |
1190 | }, | |
4c8365fa DM |
1191 | revert => { |
1192 | type => 'string', format => 'pve-configid-list', | |
1193 | description => "Revert a pending change.", | |
1194 | optional => 1, | |
1195 | }, | |
5555edea DM |
1196 | force => { |
1197 | type => 'boolean', | |
1198 | description => $opt_force_description, | |
1199 | optional => 1, | |
1200 | requires => 'delete', | |
1201 | }, | |
1202 | digest => { | |
1203 | type => 'string', | |
1204 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1205 | maxLength => 40, | |
1206 | optional => 1, | |
1207 | }, | |
1208 | }), | |
1209 | }, | |
1210 | returns => { type => 'null' }, | |
1211 | code => sub { | |
1212 | my ($param) = @_; | |
1213 | &$update_vm_api($param, 1); | |
1e3baf05 | 1214 | return undef; |
5555edea DM |
1215 | } |
1216 | }); | |
1e3baf05 DM |
1217 | |
1218 | ||
1219 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1220 | name => 'destroy_vm', |
1221 | path => '{vmid}', | |
1e3baf05 DM |
1222 | method => 'DELETE', |
1223 | protected => 1, | |
1224 | proxyto => 'node', | |
1225 | description => "Destroy the vm (also delete all used/owned volumes).", | |
a0d1b1a2 DM |
1226 | permissions => { |
1227 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1228 | }, | |
1e3baf05 DM |
1229 | parameters => { |
1230 | additionalProperties => 0, | |
1231 | properties => { | |
1232 | node => get_standard_option('pve-node'), | |
335af808 | 1233 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), |
3ea94c60 | 1234 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
1235 | }, |
1236 | }, | |
afdb31d5 | 1237 | returns => { |
5fdbe4f0 DM |
1238 | type => 'string', |
1239 | }, | |
1e3baf05 DM |
1240 | code => sub { |
1241 | my ($param) = @_; | |
1242 | ||
1243 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1244 | ||
a0d1b1a2 | 1245 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1246 | |
1247 | my $vmid = $param->{vmid}; | |
1248 | ||
1249 | my $skiplock = $param->{skiplock}; | |
afdb31d5 | 1250 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1251 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1252 | |
5fdbe4f0 | 1253 | # test if VM exists |
ffda963f | 1254 | my $conf = PVE::QemuConfig->load_config($vmid); |
5fdbe4f0 | 1255 | |
afdb31d5 | 1256 | my $storecfg = PVE::Storage::config(); |
1e3baf05 | 1257 | |
ffda963f | 1258 | PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid"); |
cb0e4540 | 1259 | |
952e3ac3 DM |
1260 | die "unable to remove VM $vmid - used in HA resources\n" |
1261 | if PVE::HA::Config::vm_is_ha_managed($vmid); | |
e9f2f8e5 | 1262 | |
db593da2 DM |
1263 | # early tests (repeat after locking) |
1264 | die "VM $vmid is running - destroy failed\n" | |
1265 | if PVE::QemuServer::check_running($vmid); | |
1266 | ||
5fdbe4f0 | 1267 | my $realcmd = sub { |
ff1a2432 DM |
1268 | my $upid = shift; |
1269 | ||
1270 | syslog('info', "destroy VM $vmid: $upid\n"); | |
1271 | ||
5fdbe4f0 | 1272 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); |
502d18a2 | 1273 | |
37f43805 | 1274 | PVE::AccessControl::remove_vm_access($vmid); |
e9abcde6 AG |
1275 | |
1276 | PVE::Firewall::remove_vmfw_conf($vmid); | |
5fdbe4f0 | 1277 | }; |
1e3baf05 | 1278 | |
a0d1b1a2 | 1279 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1280 | }}); |
1281 | ||
1282 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1283 | name => 'unlink', |
1284 | path => '{vmid}/unlink', | |
1e3baf05 DM |
1285 | method => 'PUT', |
1286 | protected => 1, | |
1287 | proxyto => 'node', | |
1288 | description => "Unlink/delete disk images.", | |
a0d1b1a2 DM |
1289 | permissions => { |
1290 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1291 | }, | |
1e3baf05 DM |
1292 | parameters => { |
1293 | additionalProperties => 0, | |
1294 | properties => { | |
1295 | node => get_standard_option('pve-node'), | |
335af808 | 1296 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
1e3baf05 DM |
1297 | idlist => { |
1298 | type => 'string', format => 'pve-configid-list', | |
1299 | description => "A list of disk IDs you want to delete.", | |
1300 | }, | |
1301 | force => { | |
1302 | type => 'boolean', | |
1303 | description => $opt_force_description, | |
1304 | optional => 1, | |
1305 | }, | |
1306 | }, | |
1307 | }, | |
1308 | returns => { type => 'null'}, | |
1309 | code => sub { | |
1310 | my ($param) = @_; | |
1311 | ||
1312 | $param->{delete} = extract_param($param, 'idlist'); | |
1313 | ||
1314 | __PACKAGE__->update_vm($param); | |
1315 | ||
1316 | return undef; | |
1317 | }}); | |
1318 | ||
1319 | my $sslcert; | |
1320 | ||
1321 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1322 | name => 'vncproxy', |
1323 | path => '{vmid}/vncproxy', | |
1e3baf05 DM |
1324 | method => 'POST', |
1325 | protected => 1, | |
1326 | permissions => { | |
378b359e | 1327 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1e3baf05 DM |
1328 | }, |
1329 | description => "Creates a TCP VNC proxy connections.", | |
1330 | parameters => { | |
1331 | additionalProperties => 0, | |
1332 | properties => { | |
1333 | node => get_standard_option('pve-node'), | |
1334 | vmid => get_standard_option('pve-vmid'), | |
b4d5c000 SP |
1335 | websocket => { |
1336 | optional => 1, | |
1337 | type => 'boolean', | |
1338 | description => "starts websockify instead of vncproxy", | |
1339 | }, | |
1e3baf05 DM |
1340 | }, |
1341 | }, | |
afdb31d5 | 1342 | returns => { |
1e3baf05 DM |
1343 | additionalProperties => 0, |
1344 | properties => { | |
1345 | user => { type => 'string' }, | |
1346 | ticket => { type => 'string' }, | |
1347 | cert => { type => 'string' }, | |
1348 | port => { type => 'integer' }, | |
1349 | upid => { type => 'string' }, | |
1350 | }, | |
1351 | }, | |
1352 | code => sub { | |
1353 | my ($param) = @_; | |
1354 | ||
1355 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1356 | ||
a0d1b1a2 | 1357 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1358 | |
1359 | my $vmid = $param->{vmid}; | |
1360 | my $node = $param->{node}; | |
983d4582 | 1361 | my $websocket = $param->{websocket}; |
1e3baf05 | 1362 | |
ffda963f | 1363 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists |
ef5e2be2 | 1364 | |
b6f39da2 DM |
1365 | my $authpath = "/vms/$vmid"; |
1366 | ||
a0d1b1a2 | 1367 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); |
b6f39da2 | 1368 | |
1e3baf05 DM |
1369 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
1370 | if !$sslcert; | |
1371 | ||
af0eba7e | 1372 | my ($remip, $family); |
ef5e2be2 | 1373 | my $remcmd = []; |
afdb31d5 | 1374 | |
4f1be36c | 1375 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
af0eba7e | 1376 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); |
b4d5c000 | 1377 | # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure |
ef5e2be2 | 1378 | $remcmd = ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip]; |
af0eba7e WB |
1379 | } else { |
1380 | $family = PVE::Tools::get_host_address_family($node); | |
1e3baf05 DM |
1381 | } |
1382 | ||
af0eba7e WB |
1383 | my $port = PVE::Tools::next_vnc_port($family); |
1384 | ||
afdb31d5 | 1385 | my $timeout = 10; |
1e3baf05 DM |
1386 | |
1387 | my $realcmd = sub { | |
1388 | my $upid = shift; | |
1389 | ||
1390 | syslog('info', "starting vnc proxy $upid\n"); | |
1391 | ||
ef5e2be2 | 1392 | my $cmd; |
1e3baf05 | 1393 | |
2dc23d72 | 1394 | if ($conf->{vga} && ($conf->{vga} =~ m/^serial\d+$/)) { |
ef5e2be2 | 1395 | |
983d4582 | 1396 | die "Websocket mode is not supported in vga serial mode!" if $websocket; |
b4d5c000 | 1397 | |
ef5e2be2 DM |
1398 | my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $conf->{vga} ]; |
1399 | #my $termcmd = "/usr/bin/qm terminal -iface $conf->{vga}"; | |
1400 | $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
fa8ea931 | 1401 | '-timeout', $timeout, '-authpath', $authpath, |
ef5e2be2 DM |
1402 | '-perm', 'Sys.Console', '-c', @$remcmd, @$termcmd]; |
1403 | } else { | |
1e3baf05 | 1404 | |
3e7567e0 DM |
1405 | $ENV{LC_PVE_TICKET} = $ticket if $websocket; # set ticket with "qm vncproxy" |
1406 | ||
ef5e2be2 DM |
1407 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; |
1408 | ||
1409 | my $qmstr = join(' ', @$qmcmd); | |
1410 | ||
1411 | # also redirect stderr (else we get RFB protocol errors) | |
d483fa01 | 1412 | $cmd = ['/bin/nc6', '-l', '-p', $port, '-w', $timeout, '-e', "$qmstr 2>/dev/null"]; |
ef5e2be2 | 1413 | } |
1e3baf05 | 1414 | |
be62c45c | 1415 | PVE::Tools::run_command($cmd); |
1e3baf05 DM |
1416 | |
1417 | return; | |
1418 | }; | |
1419 | ||
a0d1b1a2 | 1420 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); |
1e3baf05 | 1421 | |
3da85107 DM |
1422 | PVE::Tools::wait_for_vnc_port($port); |
1423 | ||
1e3baf05 | 1424 | return { |
a0d1b1a2 | 1425 | user => $authuser, |
1e3baf05 | 1426 | ticket => $ticket, |
afdb31d5 DM |
1427 | port => $port, |
1428 | upid => $upid, | |
1429 | cert => $sslcert, | |
1e3baf05 DM |
1430 | }; |
1431 | }}); | |
1432 | ||
3e7567e0 DM |
1433 | __PACKAGE__->register_method({ |
1434 | name => 'vncwebsocket', | |
1435 | path => '{vmid}/vncwebsocket', | |
1436 | method => 'GET', | |
3e7567e0 | 1437 | permissions => { |
c422ce93 | 1438 | description => "You also need to pass a valid ticket (vncticket).", |
3e7567e0 DM |
1439 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1440 | }, | |
4d00f52f | 1441 | description => "Opens a weksocket for VNC traffic.", |
3e7567e0 DM |
1442 | parameters => { |
1443 | additionalProperties => 0, | |
1444 | properties => { | |
1445 | node => get_standard_option('pve-node'), | |
1446 | vmid => get_standard_option('pve-vmid'), | |
c422ce93 DM |
1447 | vncticket => { |
1448 | description => "Ticket from previous call to vncproxy.", | |
1449 | type => 'string', | |
1450 | maxLength => 512, | |
1451 | }, | |
3e7567e0 DM |
1452 | port => { |
1453 | description => "Port number returned by previous vncproxy call.", | |
1454 | type => 'integer', | |
1455 | minimum => 5900, | |
1456 | maximum => 5999, | |
1457 | }, | |
1458 | }, | |
1459 | }, | |
1460 | returns => { | |
1461 | type => "object", | |
1462 | properties => { | |
1463 | port => { type => 'string' }, | |
1464 | }, | |
1465 | }, | |
1466 | code => sub { | |
1467 | my ($param) = @_; | |
1468 | ||
1469 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1470 | ||
1471 | my $authuser = $rpcenv->get_user(); | |
1472 | ||
1473 | my $vmid = $param->{vmid}; | |
1474 | my $node = $param->{node}; | |
1475 | ||
c422ce93 DM |
1476 | my $authpath = "/vms/$vmid"; |
1477 | ||
1478 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
1479 | ||
ffda963f | 1480 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ? |
3e7567e0 DM |
1481 | |
1482 | # Note: VNC ports are acessible from outside, so we do not gain any | |
1483 | # security if we verify that $param->{port} belongs to VM $vmid. This | |
1484 | # check is done by verifying the VNC ticket (inside VNC protocol). | |
1485 | ||
1486 | my $port = $param->{port}; | |
f34ebd52 | 1487 | |
3e7567e0 DM |
1488 | return { port => $port }; |
1489 | }}); | |
1490 | ||
288eeea8 DM |
1491 | __PACKAGE__->register_method({ |
1492 | name => 'spiceproxy', | |
1493 | path => '{vmid}/spiceproxy', | |
78252ce7 | 1494 | method => 'POST', |
288eeea8 | 1495 | protected => 1, |
78252ce7 | 1496 | proxyto => 'node', |
288eeea8 DM |
1497 | permissions => { |
1498 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1499 | }, | |
1500 | description => "Returns a SPICE configuration to connect to the VM.", | |
1501 | parameters => { | |
1502 | additionalProperties => 0, | |
1503 | properties => { | |
1504 | node => get_standard_option('pve-node'), | |
1505 | vmid => get_standard_option('pve-vmid'), | |
dd25eecf | 1506 | proxy => get_standard_option('spice-proxy', { optional => 1 }), |
288eeea8 DM |
1507 | }, |
1508 | }, | |
dd25eecf | 1509 | returns => get_standard_option('remote-viewer-config'), |
288eeea8 DM |
1510 | code => sub { |
1511 | my ($param) = @_; | |
1512 | ||
1513 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1514 | ||
1515 | my $authuser = $rpcenv->get_user(); | |
1516 | ||
1517 | my $vmid = $param->{vmid}; | |
1518 | my $node = $param->{node}; | |
fb6c7260 | 1519 | my $proxy = $param->{proxy}; |
288eeea8 | 1520 | |
ffda963f | 1521 | my $conf = PVE::QemuConfig->load_config($vmid, $node); |
7f9e28e4 TL |
1522 | my $title = "VM $vmid"; |
1523 | $title .= " - ". $conf->{name} if $conf->{name}; | |
288eeea8 | 1524 | |
943340a6 | 1525 | my $port = PVE::QemuServer::spice_port($vmid); |
dd25eecf | 1526 | |
f34ebd52 | 1527 | my ($ticket, undef, $remote_viewer_config) = |
dd25eecf | 1528 | PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port); |
f34ebd52 | 1529 | |
288eeea8 DM |
1530 | PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket); |
1531 | PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30"); | |
f34ebd52 | 1532 | |
dd25eecf | 1533 | return $remote_viewer_config; |
288eeea8 DM |
1534 | }}); |
1535 | ||
5fdbe4f0 DM |
1536 | __PACKAGE__->register_method({ |
1537 | name => 'vmcmdidx', | |
afdb31d5 | 1538 | path => '{vmid}/status', |
5fdbe4f0 DM |
1539 | method => 'GET', |
1540 | proxyto => 'node', | |
1541 | description => "Directory index", | |
a0d1b1a2 DM |
1542 | permissions => { |
1543 | user => 'all', | |
1544 | }, | |
5fdbe4f0 DM |
1545 | parameters => { |
1546 | additionalProperties => 0, | |
1547 | properties => { | |
1548 | node => get_standard_option('pve-node'), | |
1549 | vmid => get_standard_option('pve-vmid'), | |
1550 | }, | |
1551 | }, | |
1552 | returns => { | |
1553 | type => 'array', | |
1554 | items => { | |
1555 | type => "object", | |
1556 | properties => { | |
1557 | subdir => { type => 'string' }, | |
1558 | }, | |
1559 | }, | |
1560 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1561 | }, | |
1562 | code => sub { | |
1563 | my ($param) = @_; | |
1564 | ||
1565 | # test if VM exists | |
ffda963f | 1566 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
5fdbe4f0 DM |
1567 | |
1568 | my $res = [ | |
1569 | { subdir => 'current' }, | |
1570 | { subdir => 'start' }, | |
1571 | { subdir => 'stop' }, | |
1572 | ]; | |
afdb31d5 | 1573 | |
5fdbe4f0 DM |
1574 | return $res; |
1575 | }}); | |
1576 | ||
1e3baf05 | 1577 | __PACKAGE__->register_method({ |
afdb31d5 | 1578 | name => 'vm_status', |
5fdbe4f0 | 1579 | path => '{vmid}/status/current', |
1e3baf05 DM |
1580 | method => 'GET', |
1581 | proxyto => 'node', | |
1582 | protected => 1, # qemu pid files are only readable by root | |
1583 | description => "Get virtual machine status.", | |
a0d1b1a2 DM |
1584 | permissions => { |
1585 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1586 | }, | |
1e3baf05 DM |
1587 | parameters => { |
1588 | additionalProperties => 0, | |
1589 | properties => { | |
1590 | node => get_standard_option('pve-node'), | |
1591 | vmid => get_standard_option('pve-vmid'), | |
1592 | }, | |
1593 | }, | |
1594 | returns => { type => 'object' }, | |
1595 | code => sub { | |
1596 | my ($param) = @_; | |
1597 | ||
1598 | # test if VM exists | |
ffda963f | 1599 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
1e3baf05 | 1600 | |
03a33f30 | 1601 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); |
8610701a | 1602 | my $status = $vmstatus->{$param->{vmid}}; |
1e3baf05 | 1603 | |
4d2a734e | 1604 | $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}"); |
8610701a | 1605 | |
86b8228b | 1606 | $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga}); |
46246f04 | 1607 | |
8610701a | 1608 | return $status; |
1e3baf05 DM |
1609 | }}); |
1610 | ||
1611 | __PACKAGE__->register_method({ | |
afdb31d5 | 1612 | name => 'vm_start', |
5fdbe4f0 DM |
1613 | path => '{vmid}/status/start', |
1614 | method => 'POST', | |
1e3baf05 DM |
1615 | protected => 1, |
1616 | proxyto => 'node', | |
5fdbe4f0 | 1617 | description => "Start virtual machine.", |
a0d1b1a2 DM |
1618 | permissions => { |
1619 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1620 | }, | |
1e3baf05 DM |
1621 | parameters => { |
1622 | additionalProperties => 0, | |
1623 | properties => { | |
1624 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
1625 | vmid => get_standard_option('pve-vmid', |
1626 | { completion => \&PVE::QemuServer::complete_vmid_stopped }), | |
3ea94c60 DM |
1627 | skiplock => get_standard_option('skiplock'), |
1628 | stateuri => get_standard_option('pve-qm-stateuri'), | |
7e8dcf2c | 1629 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), |
2de2d6f7 TL |
1630 | migration_type => { |
1631 | type => 'string', | |
1632 | enum => ['secure', 'insecure'], | |
1633 | description => "Migration traffic is encrypted using an SSH " . | |
1634 | "tunnel by default. On secure, completely private networks " . | |
1635 | "this can be disabled to increase performance.", | |
1636 | optional => 1, | |
1637 | }, | |
1638 | migration_network => { | |
29ddbe70 | 1639 | type => 'string', format => 'CIDR', |
2de2d6f7 TL |
1640 | description => "CIDR of the (sub) network that is used for migration.", |
1641 | optional => 1, | |
1642 | }, | |
952958bc | 1643 | machine => get_standard_option('pve-qm-machine'), |
1e3baf05 DM |
1644 | }, |
1645 | }, | |
afdb31d5 | 1646 | returns => { |
5fdbe4f0 DM |
1647 | type => 'string', |
1648 | }, | |
1e3baf05 DM |
1649 | code => sub { |
1650 | my ($param) = @_; | |
1651 | ||
1652 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1653 | ||
a0d1b1a2 | 1654 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1655 | |
1656 | my $node = extract_param($param, 'node'); | |
1657 | ||
1e3baf05 DM |
1658 | my $vmid = extract_param($param, 'vmid'); |
1659 | ||
952958bc DM |
1660 | my $machine = extract_param($param, 'machine'); |
1661 | ||
3ea94c60 | 1662 | my $stateuri = extract_param($param, 'stateuri'); |
afdb31d5 | 1663 | raise_param_exc({ stateuri => "Only root may use this option." }) |
a0d1b1a2 | 1664 | if $stateuri && $authuser ne 'root@pam'; |
3ea94c60 | 1665 | |
1e3baf05 | 1666 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 1667 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1668 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1669 | |
7e8dcf2c AD |
1670 | my $migratedfrom = extract_param($param, 'migratedfrom'); |
1671 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1672 | if $migratedfrom && $authuser ne 'root@pam'; | |
1673 | ||
2de2d6f7 TL |
1674 | my $migration_type = extract_param($param, 'migration_type'); |
1675 | raise_param_exc({ migration_type => "Only root may use this option." }) | |
1676 | if $migration_type && $authuser ne 'root@pam'; | |
1677 | ||
1678 | my $migration_network = extract_param($param, 'migration_network'); | |
1679 | raise_param_exc({ migration_network => "Only root may use this option." }) | |
1680 | if $migration_network && $authuser ne 'root@pam'; | |
1681 | ||
7c14dcae DM |
1682 | # read spice ticket from STDIN |
1683 | my $spice_ticket; | |
1684 | if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) { | |
a64d6146 | 1685 | if (defined(my $line = <>)) { |
760fb3c8 DM |
1686 | chomp $line; |
1687 | $spice_ticket = $line; | |
1688 | } | |
7c14dcae DM |
1689 | } |
1690 | ||
98cbd0f4 WB |
1691 | PVE::Cluster::check_cfs_quorum(); |
1692 | ||
afdb31d5 | 1693 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 | 1694 | |
2003f0f8 | 1695 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri && |
cce37749 | 1696 | $rpcenv->{type} ne 'ha') { |
5fdbe4f0 | 1697 | |
88fc87b4 DM |
1698 | my $hacmd = sub { |
1699 | my $upid = shift; | |
5fdbe4f0 | 1700 | |
c44291cd | 1701 | my $service = "vm:$vmid"; |
5fdbe4f0 | 1702 | |
2a7e2b82 | 1703 | my $cmd = ['ha-manager', 'set', $service, '--state', 'started']; |
88fc87b4 DM |
1704 | |
1705 | print "Executing HA start for VM $vmid\n"; | |
1706 | ||
1707 | PVE::Tools::run_command($cmd); | |
1708 | ||
1709 | return; | |
1710 | }; | |
1711 | ||
1712 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
1713 | ||
1714 | } else { | |
1715 | ||
1716 | my $realcmd = sub { | |
1717 | my $upid = shift; | |
1718 | ||
1719 | syslog('info', "start VM $vmid: $upid\n"); | |
1720 | ||
fa8ea931 | 1721 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef, |
2de2d6f7 | 1722 | $machine, $spice_ticket, $migration_network, $migration_type); |
88fc87b4 DM |
1723 | |
1724 | return; | |
1725 | }; | |
5fdbe4f0 | 1726 | |
88fc87b4 DM |
1727 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); |
1728 | } | |
5fdbe4f0 DM |
1729 | }}); |
1730 | ||
1731 | __PACKAGE__->register_method({ | |
afdb31d5 | 1732 | name => 'vm_stop', |
5fdbe4f0 DM |
1733 | path => '{vmid}/status/stop', |
1734 | method => 'POST', | |
1735 | protected => 1, | |
1736 | proxyto => 'node', | |
346130b2 | 1737 | description => "Stop virtual machine. The qemu process will exit immediately. This" . |
d6c747ff | 1738 | "is akin to pulling the power plug of a running computer and may damage the VM data", |
a0d1b1a2 DM |
1739 | permissions => { |
1740 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1741 | }, | |
5fdbe4f0 DM |
1742 | parameters => { |
1743 | additionalProperties => 0, | |
1744 | properties => { | |
1745 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
1746 | vmid => get_standard_option('pve-vmid', |
1747 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 1748 | skiplock => get_standard_option('skiplock'), |
debe8882 | 1749 | migratedfrom => get_standard_option('pve-node', { optional => 1 }), |
c6bb9502 DM |
1750 | timeout => { |
1751 | description => "Wait maximal timeout seconds.", | |
1752 | type => 'integer', | |
1753 | minimum => 0, | |
1754 | optional => 1, | |
254575e9 DM |
1755 | }, |
1756 | keepActive => { | |
94a17e1d | 1757 | description => "Do not deactivate storage volumes.", |
254575e9 DM |
1758 | type => 'boolean', |
1759 | optional => 1, | |
1760 | default => 0, | |
c6bb9502 | 1761 | } |
5fdbe4f0 DM |
1762 | }, |
1763 | }, | |
afdb31d5 | 1764 | returns => { |
5fdbe4f0 DM |
1765 | type => 'string', |
1766 | }, | |
1767 | code => sub { | |
1768 | my ($param) = @_; | |
1769 | ||
1770 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1771 | ||
a0d1b1a2 | 1772 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1773 | |
1774 | my $node = extract_param($param, 'node'); | |
1775 | ||
1776 | my $vmid = extract_param($param, 'vmid'); | |
1777 | ||
1778 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1779 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1780 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1781 | |
254575e9 | 1782 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1783 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1784 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1785 | |
af30308f DM |
1786 | my $migratedfrom = extract_param($param, 'migratedfrom'); |
1787 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1788 | if $migratedfrom && $authuser ne 'root@pam'; | |
1789 | ||
1790 | ||
ff1a2432 DM |
1791 | my $storecfg = PVE::Storage::config(); |
1792 | ||
2003f0f8 | 1793 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) { |
5fdbe4f0 | 1794 | |
88fc87b4 DM |
1795 | my $hacmd = sub { |
1796 | my $upid = shift; | |
5fdbe4f0 | 1797 | |
c44291cd | 1798 | my $service = "vm:$vmid"; |
c6bb9502 | 1799 | |
e0feef86 | 1800 | my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped']; |
88fc87b4 DM |
1801 | |
1802 | print "Executing HA stop for VM $vmid\n"; | |
1803 | ||
1804 | PVE::Tools::run_command($cmd); | |
5fdbe4f0 | 1805 | |
88fc87b4 DM |
1806 | return; |
1807 | }; | |
1808 | ||
1809 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1810 | ||
1811 | } else { | |
1812 | my $realcmd = sub { | |
1813 | my $upid = shift; | |
1814 | ||
1815 | syslog('info', "stop VM $vmid: $upid\n"); | |
1816 | ||
1817 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
af30308f | 1818 | $param->{timeout}, 0, 1, $keepActive, $migratedfrom); |
88fc87b4 DM |
1819 | |
1820 | return; | |
1821 | }; | |
1822 | ||
1823 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
1824 | } | |
5fdbe4f0 DM |
1825 | }}); |
1826 | ||
1827 | __PACKAGE__->register_method({ | |
afdb31d5 | 1828 | name => 'vm_reset', |
5fdbe4f0 DM |
1829 | path => '{vmid}/status/reset', |
1830 | method => 'POST', | |
1831 | protected => 1, | |
1832 | proxyto => 'node', | |
1833 | description => "Reset virtual machine.", | |
a0d1b1a2 DM |
1834 | permissions => { |
1835 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1836 | }, | |
5fdbe4f0 DM |
1837 | parameters => { |
1838 | additionalProperties => 0, | |
1839 | properties => { | |
1840 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
1841 | vmid => get_standard_option('pve-vmid', |
1842 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 DM |
1843 | skiplock => get_standard_option('skiplock'), |
1844 | }, | |
1845 | }, | |
afdb31d5 | 1846 | returns => { |
5fdbe4f0 DM |
1847 | type => 'string', |
1848 | }, | |
1849 | code => sub { | |
1850 | my ($param) = @_; | |
1851 | ||
1852 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1853 | ||
a0d1b1a2 | 1854 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1855 | |
1856 | my $node = extract_param($param, 'node'); | |
1857 | ||
1858 | my $vmid = extract_param($param, 'vmid'); | |
1859 | ||
1860 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1861 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1862 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1863 | |
ff1a2432 DM |
1864 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1865 | ||
5fdbe4f0 DM |
1866 | my $realcmd = sub { |
1867 | my $upid = shift; | |
1868 | ||
1e3baf05 | 1869 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
1870 | |
1871 | return; | |
1872 | }; | |
1873 | ||
a0d1b1a2 | 1874 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1875 | }}); |
1876 | ||
1877 | __PACKAGE__->register_method({ | |
afdb31d5 | 1878 | name => 'vm_shutdown', |
5fdbe4f0 DM |
1879 | path => '{vmid}/status/shutdown', |
1880 | method => 'POST', | |
1881 | protected => 1, | |
1882 | proxyto => 'node', | |
d6c747ff EK |
1883 | description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." . |
1884 | "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.", | |
a0d1b1a2 DM |
1885 | permissions => { |
1886 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1887 | }, | |
5fdbe4f0 DM |
1888 | parameters => { |
1889 | additionalProperties => 0, | |
1890 | properties => { | |
1891 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
1892 | vmid => get_standard_option('pve-vmid', |
1893 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 1894 | skiplock => get_standard_option('skiplock'), |
c6bb9502 DM |
1895 | timeout => { |
1896 | description => "Wait maximal timeout seconds.", | |
1897 | type => 'integer', | |
1898 | minimum => 0, | |
1899 | optional => 1, | |
9269013a DM |
1900 | }, |
1901 | forceStop => { | |
1902 | description => "Make sure the VM stops.", | |
1903 | type => 'boolean', | |
1904 | optional => 1, | |
1905 | default => 0, | |
254575e9 DM |
1906 | }, |
1907 | keepActive => { | |
94a17e1d | 1908 | description => "Do not deactivate storage volumes.", |
254575e9 DM |
1909 | type => 'boolean', |
1910 | optional => 1, | |
1911 | default => 0, | |
c6bb9502 | 1912 | } |
5fdbe4f0 DM |
1913 | }, |
1914 | }, | |
afdb31d5 | 1915 | returns => { |
5fdbe4f0 DM |
1916 | type => 'string', |
1917 | }, | |
1918 | code => sub { | |
1919 | my ($param) = @_; | |
1920 | ||
1921 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1922 | ||
a0d1b1a2 | 1923 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1924 | |
1925 | my $node = extract_param($param, 'node'); | |
1926 | ||
1927 | my $vmid = extract_param($param, 'vmid'); | |
1928 | ||
1929 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1930 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1931 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1932 | |
254575e9 | 1933 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1934 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1935 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1936 | |
02d07cf5 DM |
1937 | my $storecfg = PVE::Storage::config(); |
1938 | ||
89897367 DC |
1939 | my $shutdown = 1; |
1940 | ||
1941 | # if vm is paused, do not shutdown (but stop if forceStop = 1) | |
1942 | # otherwise, we will infer a shutdown command, but run into the timeout, | |
1943 | # then when the vm is resumed, it will instantly shutdown | |
1944 | # | |
1945 | # checking the qmp status here to get feedback to the gui/cli/api | |
1946 | # and the status query should not take too long | |
1947 | my $qmpstatus; | |
1948 | eval { | |
1949 | $qmpstatus = PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0); | |
1950 | }; | |
1951 | my $err = $@ if $@; | |
1952 | ||
1953 | if (!$err && $qmpstatus->{status} eq "paused") { | |
1954 | if ($param->{forceStop}) { | |
1955 | warn "VM is paused - stop instead of shutdown\n"; | |
1956 | $shutdown = 0; | |
1957 | } else { | |
1958 | die "VM is paused - cannot shutdown\n"; | |
1959 | } | |
1960 | } | |
1961 | ||
ae849692 DM |
1962 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && |
1963 | ($rpcenv->{type} ne 'ha')) { | |
5fdbe4f0 | 1964 | |
ae849692 DM |
1965 | my $hacmd = sub { |
1966 | my $upid = shift; | |
5fdbe4f0 | 1967 | |
ae849692 | 1968 | my $service = "vm:$vmid"; |
c6bb9502 | 1969 | |
ae849692 DM |
1970 | my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped']; |
1971 | ||
1972 | print "Executing HA stop for VM $vmid\n"; | |
1973 | ||
1974 | PVE::Tools::run_command($cmd); | |
1975 | ||
1976 | return; | |
1977 | }; | |
1978 | ||
1979 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1980 | ||
1981 | } else { | |
1982 | ||
1983 | my $realcmd = sub { | |
1984 | my $upid = shift; | |
1985 | ||
1986 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
5fdbe4f0 | 1987 | |
ae849692 DM |
1988 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, |
1989 | $shutdown, $param->{forceStop}, $keepActive); | |
1990 | ||
1991 | return; | |
1992 | }; | |
1993 | ||
1994 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); | |
1995 | } | |
5fdbe4f0 DM |
1996 | }}); |
1997 | ||
1998 | __PACKAGE__->register_method({ | |
afdb31d5 | 1999 | name => 'vm_suspend', |
5fdbe4f0 DM |
2000 | path => '{vmid}/status/suspend', |
2001 | method => 'POST', | |
2002 | protected => 1, | |
2003 | proxyto => 'node', | |
2004 | description => "Suspend virtual machine.", | |
a0d1b1a2 DM |
2005 | permissions => { |
2006 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2007 | }, | |
5fdbe4f0 DM |
2008 | parameters => { |
2009 | additionalProperties => 0, | |
2010 | properties => { | |
2011 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2012 | vmid => get_standard_option('pve-vmid', |
2013 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 DM |
2014 | skiplock => get_standard_option('skiplock'), |
2015 | }, | |
2016 | }, | |
afdb31d5 | 2017 | returns => { |
5fdbe4f0 DM |
2018 | type => 'string', |
2019 | }, | |
2020 | code => sub { | |
2021 | my ($param) = @_; | |
2022 | ||
2023 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2024 | ||
a0d1b1a2 | 2025 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2026 | |
2027 | my $node = extract_param($param, 'node'); | |
2028 | ||
2029 | my $vmid = extract_param($param, 'vmid'); | |
2030 | ||
2031 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2032 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2033 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2034 | |
ff1a2432 DM |
2035 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
2036 | ||
5fdbe4f0 DM |
2037 | my $realcmd = sub { |
2038 | my $upid = shift; | |
2039 | ||
2040 | syslog('info', "suspend VM $vmid: $upid\n"); | |
2041 | ||
1e3baf05 | 2042 | PVE::QemuServer::vm_suspend($vmid, $skiplock); |
5fdbe4f0 DM |
2043 | |
2044 | return; | |
2045 | }; | |
2046 | ||
a0d1b1a2 | 2047 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
2048 | }}); |
2049 | ||
2050 | __PACKAGE__->register_method({ | |
afdb31d5 | 2051 | name => 'vm_resume', |
5fdbe4f0 DM |
2052 | path => '{vmid}/status/resume', |
2053 | method => 'POST', | |
2054 | protected => 1, | |
2055 | proxyto => 'node', | |
2056 | description => "Resume virtual machine.", | |
a0d1b1a2 DM |
2057 | permissions => { |
2058 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2059 | }, | |
5fdbe4f0 DM |
2060 | parameters => { |
2061 | additionalProperties => 0, | |
2062 | properties => { | |
2063 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2064 | vmid => get_standard_option('pve-vmid', |
2065 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 2066 | skiplock => get_standard_option('skiplock'), |
289e0b85 AD |
2067 | nocheck => { type => 'boolean', optional => 1 }, |
2068 | ||
5fdbe4f0 DM |
2069 | }, |
2070 | }, | |
afdb31d5 | 2071 | returns => { |
5fdbe4f0 DM |
2072 | type => 'string', |
2073 | }, | |
2074 | code => sub { | |
2075 | my ($param) = @_; | |
2076 | ||
2077 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2078 | ||
a0d1b1a2 | 2079 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2080 | |
2081 | my $node = extract_param($param, 'node'); | |
2082 | ||
2083 | my $vmid = extract_param($param, 'vmid'); | |
2084 | ||
2085 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2086 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2087 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2088 | |
289e0b85 AD |
2089 | my $nocheck = extract_param($param, 'nocheck'); |
2090 | ||
2091 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid, $nocheck); | |
ff1a2432 | 2092 | |
5fdbe4f0 DM |
2093 | my $realcmd = sub { |
2094 | my $upid = shift; | |
2095 | ||
2096 | syslog('info', "resume VM $vmid: $upid\n"); | |
2097 | ||
289e0b85 | 2098 | PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck); |
1e3baf05 | 2099 | |
5fdbe4f0 DM |
2100 | return; |
2101 | }; | |
2102 | ||
a0d1b1a2 | 2103 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
2104 | }}); |
2105 | ||
2106 | __PACKAGE__->register_method({ | |
afdb31d5 | 2107 | name => 'vm_sendkey', |
5fdbe4f0 DM |
2108 | path => '{vmid}/sendkey', |
2109 | method => 'PUT', | |
2110 | protected => 1, | |
2111 | proxyto => 'node', | |
2112 | description => "Send key event to virtual machine.", | |
a0d1b1a2 DM |
2113 | permissions => { |
2114 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
2115 | }, | |
5fdbe4f0 DM |
2116 | parameters => { |
2117 | additionalProperties => 0, | |
2118 | properties => { | |
2119 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2120 | vmid => get_standard_option('pve-vmid', |
2121 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 DM |
2122 | skiplock => get_standard_option('skiplock'), |
2123 | key => { | |
2124 | description => "The key (qemu monitor encoding).", | |
2125 | type => 'string' | |
2126 | } | |
2127 | }, | |
2128 | }, | |
2129 | returns => { type => 'null'}, | |
2130 | code => sub { | |
2131 | my ($param) = @_; | |
2132 | ||
2133 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2134 | ||
a0d1b1a2 | 2135 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2136 | |
2137 | my $node = extract_param($param, 'node'); | |
2138 | ||
2139 | my $vmid = extract_param($param, 'vmid'); | |
2140 | ||
2141 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2142 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2143 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 DM |
2144 | |
2145 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
2146 | ||
2147 | return; | |
1e3baf05 DM |
2148 | }}); |
2149 | ||
1ac0d2ee AD |
2150 | __PACKAGE__->register_method({ |
2151 | name => 'vm_feature', | |
2152 | path => '{vmid}/feature', | |
2153 | method => 'GET', | |
2154 | proxyto => 'node', | |
75466c4f | 2155 | protected => 1, |
1ac0d2ee AD |
2156 | description => "Check if feature for virtual machine is available.", |
2157 | permissions => { | |
2158 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2159 | }, | |
2160 | parameters => { | |
2161 | additionalProperties => 0, | |
2162 | properties => { | |
2163 | node => get_standard_option('pve-node'), | |
2164 | vmid => get_standard_option('pve-vmid'), | |
2165 | feature => { | |
2166 | description => "Feature to check.", | |
2167 | type => 'string', | |
7758ce86 | 2168 | enum => [ 'snapshot', 'clone', 'copy' ], |
1ac0d2ee AD |
2169 | }, |
2170 | snapname => get_standard_option('pve-snapshot-name', { | |
2171 | optional => 1, | |
2172 | }), | |
2173 | }, | |
1ac0d2ee AD |
2174 | }, |
2175 | returns => { | |
719893a9 DM |
2176 | type => "object", |
2177 | properties => { | |
2178 | hasFeature => { type => 'boolean' }, | |
7043d946 | 2179 | nodes => { |
719893a9 DM |
2180 | type => 'array', |
2181 | items => { type => 'string' }, | |
2182 | } | |
2183 | }, | |
1ac0d2ee AD |
2184 | }, |
2185 | code => sub { | |
2186 | my ($param) = @_; | |
2187 | ||
2188 | my $node = extract_param($param, 'node'); | |
2189 | ||
2190 | my $vmid = extract_param($param, 'vmid'); | |
2191 | ||
2192 | my $snapname = extract_param($param, 'snapname'); | |
2193 | ||
2194 | my $feature = extract_param($param, 'feature'); | |
2195 | ||
2196 | my $running = PVE::QemuServer::check_running($vmid); | |
2197 | ||
ffda963f | 2198 | my $conf = PVE::QemuConfig->load_config($vmid); |
1ac0d2ee AD |
2199 | |
2200 | if($snapname){ | |
2201 | my $snap = $conf->{snapshots}->{$snapname}; | |
2202 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
2203 | $conf = $snap; | |
2204 | } | |
2205 | my $storecfg = PVE::Storage::config(); | |
2206 | ||
719893a9 | 2207 | my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg); |
b2c9558d | 2208 | my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running); |
7043d946 | 2209 | |
719893a9 DM |
2210 | return { |
2211 | hasFeature => $hasFeature, | |
2212 | nodes => [ keys %$nodelist ], | |
7043d946 | 2213 | }; |
1ac0d2ee AD |
2214 | }}); |
2215 | ||
6116f729 | 2216 | __PACKAGE__->register_method({ |
9418baad DM |
2217 | name => 'clone_vm', |
2218 | path => '{vmid}/clone', | |
6116f729 DM |
2219 | method => 'POST', |
2220 | protected => 1, | |
2221 | proxyto => 'node', | |
37329185 | 2222 | description => "Create a copy of virtual machine/template.", |
6116f729 | 2223 | permissions => { |
9418baad | 2224 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " . |
6116f729 DM |
2225 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . |
2226 | "'Datastore.AllocateSpace' on any used storage.", | |
75466c4f DM |
2227 | check => |
2228 | [ 'and', | |
9418baad | 2229 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], |
75466c4f | 2230 | [ 'or', |
6116f729 DM |
2231 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], |
2232 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
2233 | ], | |
2234 | ] | |
2235 | }, | |
2236 | parameters => { | |
2237 | additionalProperties => 0, | |
2238 | properties => { | |
6116f729 | 2239 | node => get_standard_option('pve-node'), |
335af808 | 2240 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
9418baad | 2241 | newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }), |
a60ab1a6 DM |
2242 | name => { |
2243 | optional => 1, | |
2244 | type => 'string', format => 'dns-name', | |
2245 | description => "Set a name for the new VM.", | |
2246 | }, | |
2247 | description => { | |
2248 | optional => 1, | |
2249 | type => 'string', | |
2250 | description => "Description for the new VM.", | |
2251 | }, | |
75466c4f | 2252 | pool => { |
6116f729 DM |
2253 | optional => 1, |
2254 | type => 'string', format => 'pve-poolid', | |
2255 | description => "Add the new VM to the specified pool.", | |
2256 | }, | |
9076d880 | 2257 | snapname => get_standard_option('pve-snapshot-name', { |
9076d880 DM |
2258 | optional => 1, |
2259 | }), | |
81f043eb | 2260 | storage => get_standard_option('pve-storage-id', { |
9418baad | 2261 | description => "Target storage for full clone.", |
4e4f83fe | 2262 | requires => 'full', |
81f043eb AD |
2263 | optional => 1, |
2264 | }), | |
55173c6b | 2265 | 'format' => { |
42a19c87 AD |
2266 | description => "Target format for file storage.", |
2267 | requires => 'full', | |
2268 | type => 'string', | |
2269 | optional => 1, | |
55173c6b | 2270 | enum => [ 'raw', 'qcow2', 'vmdk'], |
42a19c87 | 2271 | }, |
6116f729 DM |
2272 | full => { |
2273 | optional => 1, | |
55173c6b DM |
2274 | type => 'boolean', |
2275 | description => "Create a full copy of all disk. This is always done when " . | |
9418baad | 2276 | "you clone a normal VM. For VM templates, we try to create a linked clone by default.", |
6116f729 DM |
2277 | default => 0, |
2278 | }, | |
75466c4f | 2279 | target => get_standard_option('pve-node', { |
55173c6b DM |
2280 | description => "Target node. Only allowed if the original VM is on shared storage.", |
2281 | optional => 1, | |
2282 | }), | |
2283 | }, | |
6116f729 DM |
2284 | }, |
2285 | returns => { | |
2286 | type => 'string', | |
2287 | }, | |
2288 | code => sub { | |
2289 | my ($param) = @_; | |
2290 | ||
2291 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2292 | ||
55173c6b | 2293 | my $authuser = $rpcenv->get_user(); |
6116f729 DM |
2294 | |
2295 | my $node = extract_param($param, 'node'); | |
2296 | ||
2297 | my $vmid = extract_param($param, 'vmid'); | |
2298 | ||
2299 | my $newid = extract_param($param, 'newid'); | |
2300 | ||
6116f729 DM |
2301 | my $pool = extract_param($param, 'pool'); |
2302 | ||
2303 | if (defined($pool)) { | |
2304 | $rpcenv->check_pool_exist($pool); | |
2305 | } | |
2306 | ||
55173c6b | 2307 | my $snapname = extract_param($param, 'snapname'); |
9076d880 | 2308 | |
81f043eb AD |
2309 | my $storage = extract_param($param, 'storage'); |
2310 | ||
42a19c87 AD |
2311 | my $format = extract_param($param, 'format'); |
2312 | ||
55173c6b DM |
2313 | my $target = extract_param($param, 'target'); |
2314 | ||
2315 | my $localnode = PVE::INotify::nodename(); | |
2316 | ||
751cc556 | 2317 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
55173c6b DM |
2318 | |
2319 | PVE::Cluster::check_node_exists($target) if $target; | |
2320 | ||
6116f729 DM |
2321 | my $storecfg = PVE::Storage::config(); |
2322 | ||
4a5a2590 DM |
2323 | if ($storage) { |
2324 | # check if storage is enabled on local node | |
2325 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
2326 | if ($target) { | |
2327 | # check if storage is available on target node | |
2328 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
2329 | # clone only works if target storage is shared | |
2330 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
2331 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
2332 | } | |
2333 | } | |
2334 | ||
55173c6b | 2335 | PVE::Cluster::check_cfs_quorum(); |
6116f729 | 2336 | |
4e4f83fe DM |
2337 | my $running = PVE::QemuServer::check_running($vmid) || 0; |
2338 | ||
4e4f83fe DM |
2339 | # exclusive lock if VM is running - else shared lock is enough; |
2340 | my $shared_lock = $running ? 0 : 1; | |
2341 | ||
9418baad | 2342 | my $clonefn = sub { |
6116f729 | 2343 | |
829967a9 DM |
2344 | # do all tests after lock |
2345 | # we also try to do all tests before we fork the worker | |
2346 | ||
ffda963f | 2347 | my $conf = PVE::QemuConfig->load_config($vmid); |
6116f729 | 2348 | |
ffda963f | 2349 | PVE::QemuConfig->check_lock($conf); |
6116f729 | 2350 | |
4e4f83fe | 2351 | my $verify_running = PVE::QemuServer::check_running($vmid) || 0; |
6116f729 | 2352 | |
4e4f83fe | 2353 | die "unexpected state change\n" if $verify_running != $running; |
6116f729 | 2354 | |
75466c4f DM |
2355 | die "snapshot '$snapname' does not exist\n" |
2356 | if $snapname && !defined( $conf->{snapshots}->{$snapname}); | |
6116f729 | 2357 | |
75466c4f | 2358 | my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf; |
9076d880 | 2359 | |
9418baad | 2360 | my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage); |
6116f729 | 2361 | |
9418baad | 2362 | die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm; |
75466c4f | 2363 | |
ffda963f | 2364 | my $conffile = PVE::QemuConfig->config_file($newid); |
6116f729 DM |
2365 | |
2366 | die "unable to create VM $newid: config file already exists\n" | |
2367 | if -f $conffile; | |
2368 | ||
9418baad | 2369 | my $newconf = { lock => 'clone' }; |
829967a9 | 2370 | my $drives = {}; |
34456bf0 | 2371 | my $fullclone = {}; |
829967a9 DM |
2372 | my $vollist = []; |
2373 | ||
2374 | foreach my $opt (keys %$oldconf) { | |
2375 | my $value = $oldconf->{$opt}; | |
2376 | ||
2377 | # do not copy snapshot related info | |
2378 | next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' || | |
2379 | $opt eq 'vmstate' || $opt eq 'snapstate'; | |
2380 | ||
a78ea5df WL |
2381 | # no need to copy unused images, because VMID(owner) changes anyways |
2382 | next if $opt =~ m/^unused\d+$/; | |
2383 | ||
829967a9 DM |
2384 | # always change MAC! address |
2385 | if ($opt =~ m/^net(\d+)$/) { | |
2386 | my $net = PVE::QemuServer::parse_net($value); | |
b5b99790 WB |
2387 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); |
2388 | $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
829967a9 | 2389 | $newconf->{$opt} = PVE::QemuServer::print_net($net); |
74479ee9 | 2390 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { |
1f1412d1 DM |
2391 | my $drive = PVE::QemuServer::parse_drive($opt, $value); |
2392 | die "unable to parse drive options for '$opt'\n" if !$drive; | |
829967a9 DM |
2393 | if (PVE::QemuServer::drive_is_cdrom($drive)) { |
2394 | $newconf->{$opt} = $value; # simply copy configuration | |
2395 | } else { | |
64ff6fe4 | 2396 | if ($param->{full}) { |
2dd53043 | 2397 | die "Full clone feature is not available" |
dba198b0 | 2398 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running); |
34456bf0 | 2399 | $fullclone->{$opt} = 1; |
64ff6fe4 SP |
2400 | } else { |
2401 | # not full means clone instead of copy | |
2402 | die "Linked clone feature is not available" | |
2403 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running); | |
dba198b0 | 2404 | } |
829967a9 DM |
2405 | $drives->{$opt} = $drive; |
2406 | push @$vollist, $drive->{file}; | |
2407 | } | |
2408 | } else { | |
2409 | # copy everything else | |
2410 | $newconf->{$opt} = $value; | |
2411 | } | |
2412 | } | |
2413 | ||
cd11416f DM |
2414 | # auto generate a new uuid |
2415 | my ($uuid, $uuid_str); | |
2416 | UUID::generate($uuid); | |
2417 | UUID::unparse($uuid, $uuid_str); | |
2418 | my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || ''); | |
f34ebd52 | 2419 | $smbios1->{uuid} = $uuid_str; |
cd11416f DM |
2420 | $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1); |
2421 | ||
829967a9 DM |
2422 | delete $newconf->{template}; |
2423 | ||
2424 | if ($param->{name}) { | |
2425 | $newconf->{name} = $param->{name}; | |
2426 | } else { | |
c55fee03 DM |
2427 | if ($oldconf->{name}) { |
2428 | $newconf->{name} = "Copy-of-$oldconf->{name}"; | |
2429 | } else { | |
2430 | $newconf->{name} = "Copy-of-VM-$vmid"; | |
2431 | } | |
829967a9 | 2432 | } |
2dd53043 | 2433 | |
829967a9 DM |
2434 | if ($param->{description}) { |
2435 | $newconf->{description} = $param->{description}; | |
2436 | } | |
2437 | ||
6116f729 | 2438 | # create empty/temp config - this fails if VM already exists on other node |
9418baad | 2439 | PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); |
6116f729 DM |
2440 | |
2441 | my $realcmd = sub { | |
2442 | my $upid = shift; | |
2443 | ||
b83e0181 | 2444 | my $newvollist = []; |
6116f729 | 2445 | |
b83e0181 | 2446 | eval { |
829967a9 | 2447 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; |
75466c4f | 2448 | |
eb15b9f0 | 2449 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); |
6116f729 | 2450 | |
829967a9 DM |
2451 | foreach my $opt (keys %$drives) { |
2452 | my $drive = $drives->{$opt}; | |
2dd53043 | 2453 | |
152fe752 | 2454 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname, |
34456bf0 | 2455 | $newid, $storage, $format, $fullclone->{$opt}, $newvollist); |
00b095ca | 2456 | |
152fe752 | 2457 | $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive); |
2dd53043 | 2458 | |
ffda963f | 2459 | PVE::QemuConfig->write_config($newid, $newconf); |
829967a9 | 2460 | } |
b83e0181 DM |
2461 | |
2462 | delete $newconf->{lock}; | |
ffda963f | 2463 | PVE::QemuConfig->write_config($newid, $newconf); |
55173c6b DM |
2464 | |
2465 | if ($target) { | |
baca276d | 2466 | # always deactivate volumes - avoid lvm LVs to be active on several nodes |
51eefb7e | 2467 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; |
32acc380 | 2468 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); |
baca276d | 2469 | |
ffda963f | 2470 | my $newconffile = PVE::QemuConfig->config_file($newid, $target); |
55173c6b DM |
2471 | die "Failed to move config to node '$target' - rename failed: $!\n" |
2472 | if !rename($conffile, $newconffile); | |
2473 | } | |
d703d4c0 | 2474 | |
be517049 | 2475 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
6116f729 | 2476 | }; |
75466c4f | 2477 | if (my $err = $@) { |
6116f729 DM |
2478 | unlink $conffile; |
2479 | ||
b83e0181 DM |
2480 | sleep 1; # some storage like rbd need to wait before release volume - really? |
2481 | ||
2482 | foreach my $volid (@$newvollist) { | |
2483 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2484 | warn $@ if $@; | |
2485 | } | |
9418baad | 2486 | die "clone failed: $err"; |
6116f729 DM |
2487 | } |
2488 | ||
2489 | return; | |
2490 | }; | |
2491 | ||
457010cc AG |
2492 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); |
2493 | ||
9418baad | 2494 | return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd); |
6116f729 DM |
2495 | }; |
2496 | ||
ffda963f | 2497 | return PVE::QemuConfig->lock_config_mode($vmid, 1, $shared_lock, sub { |
6116f729 | 2498 | # Aquire exclusive lock lock for $newid |
ffda963f | 2499 | return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn); |
6116f729 DM |
2500 | }); |
2501 | ||
2502 | }}); | |
2503 | ||
586bfa78 | 2504 | __PACKAGE__->register_method({ |
43bc02a9 DM |
2505 | name => 'move_vm_disk', |
2506 | path => '{vmid}/move_disk', | |
e2cd75fa | 2507 | method => 'POST', |
586bfa78 AD |
2508 | protected => 1, |
2509 | proxyto => 'node', | |
2510 | description => "Move volume to different storage.", | |
2511 | permissions => { | |
e2cd75fa DM |
2512 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . |
2513 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
7043d946 | 2514 | check => |
e2cd75fa DM |
2515 | [ 'and', |
2516 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2517 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
2518 | ], | |
586bfa78 AD |
2519 | }, |
2520 | parameters => { | |
2521 | additionalProperties => 0, | |
2522 | properties => { | |
2523 | node => get_standard_option('pve-node'), | |
335af808 | 2524 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
586bfa78 AD |
2525 | disk => { |
2526 | type => 'string', | |
2527 | description => "The disk you want to move.", | |
74479ee9 | 2528 | enum => [ PVE::QemuServer::valid_drive_names() ], |
586bfa78 | 2529 | }, |
335af808 DM |
2530 | storage => get_standard_option('pve-storage-id', { |
2531 | description => "Target storage.", | |
2532 | completion => \&PVE::QemuServer::complete_storage, | |
2533 | }), | |
635c3c44 | 2534 | 'format' => { |
586bfa78 AD |
2535 | type => 'string', |
2536 | description => "Target Format.", | |
2537 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
2538 | optional => 1, | |
2539 | }, | |
70d45e33 DM |
2540 | delete => { |
2541 | type => 'boolean', | |
2542 | description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.", | |
2543 | optional => 1, | |
2544 | default => 0, | |
2545 | }, | |
586bfa78 AD |
2546 | digest => { |
2547 | type => 'string', | |
2548 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2549 | maxLength => 40, | |
2550 | optional => 1, | |
2551 | }, | |
2552 | }, | |
2553 | }, | |
e2cd75fa DM |
2554 | returns => { |
2555 | type => 'string', | |
2556 | description => "the task ID.", | |
2557 | }, | |
586bfa78 AD |
2558 | code => sub { |
2559 | my ($param) = @_; | |
2560 | ||
2561 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2562 | ||
2563 | my $authuser = $rpcenv->get_user(); | |
2564 | ||
2565 | my $node = extract_param($param, 'node'); | |
2566 | ||
2567 | my $vmid = extract_param($param, 'vmid'); | |
2568 | ||
2569 | my $digest = extract_param($param, 'digest'); | |
2570 | ||
2571 | my $disk = extract_param($param, 'disk'); | |
2572 | ||
2573 | my $storeid = extract_param($param, 'storage'); | |
2574 | ||
2575 | my $format = extract_param($param, 'format'); | |
2576 | ||
586bfa78 AD |
2577 | my $storecfg = PVE::Storage::config(); |
2578 | ||
2579 | my $updatefn = sub { | |
2580 | ||
ffda963f | 2581 | my $conf = PVE::QemuConfig->load_config($vmid); |
586bfa78 | 2582 | |
dcce9b46 FG |
2583 | PVE::QemuConfig->check_lock($conf); |
2584 | ||
586bfa78 AD |
2585 | die "checksum missmatch (file change by other user?)\n" |
2586 | if $digest && $digest ne $conf->{digest}; | |
586bfa78 AD |
2587 | |
2588 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2589 | ||
2590 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2591 | ||
70d45e33 | 2592 | my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n"; |
586bfa78 AD |
2593 | |
2594 | die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2595 | ||
e2cd75fa | 2596 | my $oldfmt; |
70d45e33 | 2597 | my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid); |
586bfa78 AD |
2598 | if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){ |
2599 | $oldfmt = $1; | |
2600 | } | |
2601 | ||
7043d946 | 2602 | die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid && |
e2cd75fa | 2603 | (!$format || !$oldfmt || $oldfmt eq $format); |
586bfa78 | 2604 | |
9dbf9b54 FG |
2605 | # this only checks snapshots because $disk is passed! |
2606 | my $snapshotted = PVE::QemuServer::is_volume_in_use($storecfg, $conf, $disk, $old_volid); | |
2607 | die "you can't move a disk with snapshots and delete the source\n" | |
2608 | if $snapshotted && $param->{delete}; | |
2609 | ||
586bfa78 AD |
2610 | PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid"); |
2611 | ||
2612 | my $running = PVE::QemuServer::check_running($vmid); | |
e2cd75fa DM |
2613 | |
2614 | PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]); | |
2615 | ||
586bfa78 AD |
2616 | my $realcmd = sub { |
2617 | ||
2618 | my $newvollist = []; | |
2619 | ||
2620 | eval { | |
2621 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2622 | ||
9dbf9b54 FG |
2623 | warn "moving disk with snapshots, snapshots will not be moved!\n" |
2624 | if $snapshotted; | |
2625 | ||
e2cd75fa DM |
2626 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef, |
2627 | $vmid, $storeid, $format, 1, $newvollist); | |
2628 | ||
2629 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2630 | ||
8793d495 | 2631 | PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete}; |
7043d946 | 2632 | |
ffda963f | 2633 | PVE::QemuConfig->write_config($vmid, $conf); |
73272365 | 2634 | |
f34ebd52 | 2635 | eval { |
73272365 | 2636 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes |
f34ebd52 | 2637 | PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ]) |
73272365 DM |
2638 | if !$running; |
2639 | }; | |
2640 | warn $@ if $@; | |
586bfa78 AD |
2641 | }; |
2642 | if (my $err = $@) { | |
2643 | ||
2644 | foreach my $volid (@$newvollist) { | |
2645 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2646 | warn $@ if $@; | |
2647 | } | |
2648 | die "storage migration failed: $err"; | |
2649 | } | |
70d45e33 DM |
2650 | |
2651 | if ($param->{delete}) { | |
a3d0bafb FG |
2652 | eval { |
2653 | PVE::Storage::deactivate_volumes($storecfg, [$old_volid]); | |
2654 | PVE::Storage::vdisk_free($storecfg, $old_volid); | |
2655 | }; | |
2656 | warn $@ if $@; | |
70d45e33 | 2657 | } |
586bfa78 AD |
2658 | }; |
2659 | ||
2660 | return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd); | |
2661 | }; | |
e2cd75fa | 2662 | |
ffda963f | 2663 | return PVE::QemuConfig->lock_config($vmid, $updatefn); |
586bfa78 AD |
2664 | }}); |
2665 | ||
3ea94c60 | 2666 | __PACKAGE__->register_method({ |
afdb31d5 | 2667 | name => 'migrate_vm', |
3ea94c60 DM |
2668 | path => '{vmid}/migrate', |
2669 | method => 'POST', | |
2670 | protected => 1, | |
2671 | proxyto => 'node', | |
2672 | description => "Migrate virtual machine. Creates a new migration task.", | |
a0d1b1a2 DM |
2673 | permissions => { |
2674 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
2675 | }, | |
3ea94c60 DM |
2676 | parameters => { |
2677 | additionalProperties => 0, | |
2678 | properties => { | |
2679 | node => get_standard_option('pve-node'), | |
335af808 DM |
2680 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
2681 | target => get_standard_option('pve-node', { | |
2682 | description => "Target node.", | |
2683 | completion => \&PVE::Cluster::complete_migration_target, | |
2684 | }), | |
3ea94c60 DM |
2685 | online => { |
2686 | type => 'boolean', | |
2687 | description => "Use online/live migration.", | |
2688 | optional => 1, | |
2689 | }, | |
2690 | force => { | |
2691 | type => 'boolean', | |
2692 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
2693 | optional => 1, | |
2694 | }, | |
2de2d6f7 TL |
2695 | migration_type => { |
2696 | type => 'string', | |
2697 | enum => ['secure', 'insecure'], | |
2698 | description => "Migration traffic is encrypted using an SSH " . | |
2699 | "tunnel by default. On secure, completely private networks " . | |
2700 | "this can be disabled to increase performance.", | |
2701 | optional => 1, | |
2702 | }, | |
2703 | migration_network => { | |
2704 | type => 'string', | |
2705 | format => 'CIDR', | |
2706 | description => "CIDR of the (sub) network that is used for migration.", | |
2707 | optional => 1, | |
2708 | }, | |
3ea94c60 DM |
2709 | }, |
2710 | }, | |
afdb31d5 | 2711 | returns => { |
3ea94c60 DM |
2712 | type => 'string', |
2713 | description => "the task ID.", | |
2714 | }, | |
2715 | code => sub { | |
2716 | my ($param) = @_; | |
2717 | ||
2718 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2719 | ||
a0d1b1a2 | 2720 | my $authuser = $rpcenv->get_user(); |
3ea94c60 DM |
2721 | |
2722 | my $target = extract_param($param, 'target'); | |
2723 | ||
2724 | my $localnode = PVE::INotify::nodename(); | |
2725 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
2726 | ||
2727 | PVE::Cluster::check_cfs_quorum(); | |
2728 | ||
2729 | PVE::Cluster::check_node_exists($target); | |
2730 | ||
2731 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
2732 | ||
2733 | my $vmid = extract_param($param, 'vmid'); | |
2734 | ||
afdb31d5 | 2735 | raise_param_exc({ force => "Only root may use this option." }) |
a0d1b1a2 | 2736 | if $param->{force} && $authuser ne 'root@pam'; |
3ea94c60 | 2737 | |
2de2d6f7 TL |
2738 | raise_param_exc({ migration_type => "Only root may use this option." }) |
2739 | if $param->{migration_type} && $authuser ne 'root@pam'; | |
2740 | ||
2741 | # allow root only until better network permissions are available | |
2742 | raise_param_exc({ migration_network => "Only root may use this option." }) | |
2743 | if $param->{migration_network} && $authuser ne 'root@pam'; | |
2744 | ||
3ea94c60 | 2745 | # test if VM exists |
ffda963f | 2746 | my $conf = PVE::QemuConfig->load_config($vmid); |
3ea94c60 DM |
2747 | |
2748 | # try to detect errors early | |
a5ed42d3 | 2749 | |
ffda963f | 2750 | PVE::QemuConfig->check_lock($conf); |
a5ed42d3 | 2751 | |
3ea94c60 | 2752 | if (PVE::QemuServer::check_running($vmid)) { |
afdb31d5 | 2753 | die "cant migrate running VM without --online\n" |
3ea94c60 DM |
2754 | if !$param->{online}; |
2755 | } | |
2756 | ||
47152e2e | 2757 | my $storecfg = PVE::Storage::config(); |
22d646a7 | 2758 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); |
47152e2e | 2759 | |
2003f0f8 | 2760 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
3ea94c60 | 2761 | |
88fc87b4 DM |
2762 | my $hacmd = sub { |
2763 | my $upid = shift; | |
3ea94c60 | 2764 | |
c44291cd | 2765 | my $service = "vm:$vmid"; |
88fc87b4 | 2766 | |
2003f0f8 | 2767 | my $cmd = ['ha-manager', 'migrate', $service, $target]; |
88fc87b4 DM |
2768 | |
2769 | print "Executing HA migrate for VM $vmid to node $target\n"; | |
2770 | ||
2771 | PVE::Tools::run_command($cmd); | |
2772 | ||
2773 | return; | |
2774 | }; | |
2775 | ||
2776 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
2777 | ||
2778 | } else { | |
2779 | ||
2780 | my $realcmd = sub { | |
2781 | my $upid = shift; | |
2782 | ||
2783 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); | |
2784 | }; | |
2785 | ||
2786 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); | |
2787 | } | |
3ea94c60 | 2788 | |
3ea94c60 | 2789 | }}); |
1e3baf05 | 2790 | |
91c94f0a | 2791 | __PACKAGE__->register_method({ |
afdb31d5 DM |
2792 | name => 'monitor', |
2793 | path => '{vmid}/monitor', | |
91c94f0a DM |
2794 | method => 'POST', |
2795 | protected => 1, | |
2796 | proxyto => 'node', | |
2797 | description => "Execute Qemu monitor commands.", | |
a0d1b1a2 | 2798 | permissions => { |
a8f2f427 | 2799 | description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')", |
a0d1b1a2 DM |
2800 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], |
2801 | }, | |
91c94f0a DM |
2802 | parameters => { |
2803 | additionalProperties => 0, | |
2804 | properties => { | |
2805 | node => get_standard_option('pve-node'), | |
2806 | vmid => get_standard_option('pve-vmid'), | |
2807 | command => { | |
2808 | type => 'string', | |
2809 | description => "The monitor command.", | |
2810 | } | |
2811 | }, | |
2812 | }, | |
2813 | returns => { type => 'string'}, | |
2814 | code => sub { | |
2815 | my ($param) = @_; | |
2816 | ||
a8f2f427 FG |
2817 | my $rpcenv = PVE::RPCEnvironment::get(); |
2818 | my $authuser = $rpcenv->get_user(); | |
2819 | ||
2820 | my $is_ro = sub { | |
2821 | my $command = shift; | |
2822 | return $command =~ m/^\s*info(\s+|$)/ | |
2823 | || $command =~ m/^\s*help\s*$/; | |
2824 | }; | |
2825 | ||
2826 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']) | |
2827 | if !&$is_ro($param->{command}); | |
2828 | ||
91c94f0a DM |
2829 | my $vmid = $param->{vmid}; |
2830 | ||
ffda963f | 2831 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists |
91c94f0a DM |
2832 | |
2833 | my $res = ''; | |
2834 | eval { | |
7b7c6d1b | 2835 | $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command}); |
91c94f0a DM |
2836 | }; |
2837 | $res = "ERROR: $@" if $@; | |
2838 | ||
2839 | return $res; | |
2840 | }}); | |
2841 | ||
0d02881c AD |
2842 | __PACKAGE__->register_method({ |
2843 | name => 'resize_vm', | |
614e3941 | 2844 | path => '{vmid}/resize', |
0d02881c AD |
2845 | method => 'PUT', |
2846 | protected => 1, | |
2847 | proxyto => 'node', | |
2f48a4f5 | 2848 | description => "Extend volume size.", |
0d02881c | 2849 | permissions => { |
3b2773f6 | 2850 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], |
0d02881c AD |
2851 | }, |
2852 | parameters => { | |
2853 | additionalProperties => 0, | |
2f48a4f5 DM |
2854 | properties => { |
2855 | node => get_standard_option('pve-node'), | |
335af808 | 2856 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
2f48a4f5 DM |
2857 | skiplock => get_standard_option('skiplock'), |
2858 | disk => { | |
2859 | type => 'string', | |
2860 | description => "The disk you want to resize.", | |
74479ee9 | 2861 | enum => [PVE::QemuServer::valid_drive_names()], |
2f48a4f5 DM |
2862 | }, |
2863 | size => { | |
2864 | type => 'string', | |
f91b2e45 | 2865 | pattern => '\+?\d+(\.\d+)?[KMGT]?', |
2f48a4f5 DM |
2866 | description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.", |
2867 | }, | |
2868 | digest => { | |
2869 | type => 'string', | |
2870 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2871 | maxLength => 40, | |
2872 | optional => 1, | |
2873 | }, | |
2874 | }, | |
0d02881c AD |
2875 | }, |
2876 | returns => { type => 'null'}, | |
2877 | code => sub { | |
2878 | my ($param) = @_; | |
2879 | ||
2880 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2881 | ||
2882 | my $authuser = $rpcenv->get_user(); | |
2883 | ||
2884 | my $node = extract_param($param, 'node'); | |
2885 | ||
2886 | my $vmid = extract_param($param, 'vmid'); | |
2887 | ||
2888 | my $digest = extract_param($param, 'digest'); | |
2889 | ||
2f48a4f5 | 2890 | my $disk = extract_param($param, 'disk'); |
75466c4f | 2891 | |
2f48a4f5 | 2892 | my $sizestr = extract_param($param, 'size'); |
0d02881c | 2893 | |
f91b2e45 | 2894 | my $skiplock = extract_param($param, 'skiplock'); |
0d02881c AD |
2895 | raise_param_exc({ skiplock => "Only root may use this option." }) |
2896 | if $skiplock && $authuser ne 'root@pam'; | |
2897 | ||
0d02881c AD |
2898 | my $storecfg = PVE::Storage::config(); |
2899 | ||
0d02881c AD |
2900 | my $updatefn = sub { |
2901 | ||
ffda963f | 2902 | my $conf = PVE::QemuConfig->load_config($vmid); |
0d02881c AD |
2903 | |
2904 | die "checksum missmatch (file change by other user?)\n" | |
2905 | if $digest && $digest ne $conf->{digest}; | |
ffda963f | 2906 | PVE::QemuConfig->check_lock($conf) if !$skiplock; |
0d02881c | 2907 | |
f91b2e45 DM |
2908 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; |
2909 | ||
2910 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2911 | ||
d662790a WL |
2912 | my (undef, undef, undef, undef, undef, undef, $format) = |
2913 | PVE::Storage::parse_volname($storecfg, $drive->{file}); | |
2914 | ||
2915 | die "can't resize volume: $disk if snapshot exists\n" | |
2916 | if %{$conf->{snapshots}} && $format eq 'qcow2'; | |
2917 | ||
f91b2e45 DM |
2918 | my $volid = $drive->{file}; |
2919 | ||
2920 | die "disk '$disk' has no associated volume\n" if !$volid; | |
2921 | ||
2922 | die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2923 | ||
2924 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
2925 | ||
2926 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
2927 | ||
b572a606 | 2928 | PVE::Storage::activate_volumes($storecfg, [$volid]); |
f91b2e45 DM |
2929 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5); |
2930 | ||
2931 | die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/; | |
2932 | my ($ext, $newsize, $unit) = ($1, $2, $4); | |
2933 | if ($unit) { | |
2934 | if ($unit eq 'K') { | |
2935 | $newsize = $newsize * 1024; | |
2936 | } elsif ($unit eq 'M') { | |
2937 | $newsize = $newsize * 1024 * 1024; | |
2938 | } elsif ($unit eq 'G') { | |
2939 | $newsize = $newsize * 1024 * 1024 * 1024; | |
2940 | } elsif ($unit eq 'T') { | |
2941 | $newsize = $newsize * 1024 * 1024 * 1024 * 1024; | |
2942 | } | |
2943 | } | |
2944 | $newsize += $size if $ext; | |
2945 | $newsize = int($newsize); | |
2946 | ||
2947 | die "unable to skrink disk size\n" if $newsize < $size; | |
2948 | ||
2949 | return if $size == $newsize; | |
2950 | ||
2f48a4f5 | 2951 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr"); |
0d02881c | 2952 | |
f91b2e45 | 2953 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize); |
75466c4f | 2954 | |
f91b2e45 DM |
2955 | $drive->{size} = $newsize; |
2956 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive); | |
2957 | ||
ffda963f | 2958 | PVE::QemuConfig->write_config($vmid, $conf); |
f91b2e45 | 2959 | }; |
0d02881c | 2960 | |
ffda963f | 2961 | PVE::QemuConfig->lock_config($vmid, $updatefn); |
0d02881c AD |
2962 | return undef; |
2963 | }}); | |
2964 | ||
9dbd1ee4 | 2965 | __PACKAGE__->register_method({ |
7e7d7b61 | 2966 | name => 'snapshot_list', |
9dbd1ee4 | 2967 | path => '{vmid}/snapshot', |
7e7d7b61 DM |
2968 | method => 'GET', |
2969 | description => "List all snapshots.", | |
2970 | permissions => { | |
2971 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2972 | }, | |
2973 | proxyto => 'node', | |
2974 | protected => 1, # qemu pid files are only readable by root | |
2975 | parameters => { | |
2976 | additionalProperties => 0, | |
2977 | properties => { | |
e261de40 | 2978 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
7e7d7b61 DM |
2979 | node => get_standard_option('pve-node'), |
2980 | }, | |
2981 | }, | |
2982 | returns => { | |
2983 | type => 'array', | |
2984 | items => { | |
2985 | type => "object", | |
2986 | properties => {}, | |
2987 | }, | |
2988 | links => [ { rel => 'child', href => "{name}" } ], | |
2989 | }, | |
2990 | code => sub { | |
2991 | my ($param) = @_; | |
2992 | ||
6aa4651b DM |
2993 | my $vmid = $param->{vmid}; |
2994 | ||
ffda963f | 2995 | my $conf = PVE::QemuConfig->load_config($vmid); |
7e7d7b61 DM |
2996 | my $snaphash = $conf->{snapshots} || {}; |
2997 | ||
2998 | my $res = []; | |
2999 | ||
3000 | foreach my $name (keys %$snaphash) { | |
0ea6bc69 | 3001 | my $d = $snaphash->{$name}; |
75466c4f DM |
3002 | my $item = { |
3003 | name => $name, | |
3004 | snaptime => $d->{snaptime} || 0, | |
6aa4651b | 3005 | vmstate => $d->{vmstate} ? 1 : 0, |
982c7f12 DM |
3006 | description => $d->{description} || '', |
3007 | }; | |
0ea6bc69 | 3008 | $item->{parent} = $d->{parent} if $d->{parent}; |
3ee28e38 | 3009 | $item->{snapstate} = $d->{snapstate} if $d->{snapstate}; |
0ea6bc69 DM |
3010 | push @$res, $item; |
3011 | } | |
3012 | ||
6aa4651b DM |
3013 | my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; |
3014 | my $current = { name => 'current', digest => $conf->{digest}, running => $running }; | |
d1914468 DM |
3015 | $current->{parent} = $conf->{parent} if $conf->{parent}; |
3016 | ||
3017 | push @$res, $current; | |
7e7d7b61 DM |
3018 | |
3019 | return $res; | |
3020 | }}); | |
3021 | ||
3022 | __PACKAGE__->register_method({ | |
3023 | name => 'snapshot', | |
3024 | path => '{vmid}/snapshot', | |
3025 | method => 'POST', | |
9dbd1ee4 AD |
3026 | protected => 1, |
3027 | proxyto => 'node', | |
3028 | description => "Snapshot a VM.", | |
3029 | permissions => { | |
f1baf1df | 3030 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
9dbd1ee4 AD |
3031 | }, |
3032 | parameters => { | |
3033 | additionalProperties => 0, | |
3034 | properties => { | |
3035 | node => get_standard_option('pve-node'), | |
335af808 | 3036 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 3037 | snapname => get_standard_option('pve-snapshot-name'), |
9dbd1ee4 AD |
3038 | vmstate => { |
3039 | optional => 1, | |
3040 | type => 'boolean', | |
3041 | description => "Save the vmstate", | |
3042 | }, | |
782f4f75 DM |
3043 | description => { |
3044 | optional => 1, | |
3045 | type => 'string', | |
3046 | description => "A textual description or comment.", | |
3047 | }, | |
9dbd1ee4 AD |
3048 | }, |
3049 | }, | |
7e7d7b61 DM |
3050 | returns => { |
3051 | type => 'string', | |
3052 | description => "the task ID.", | |
3053 | }, | |
9dbd1ee4 AD |
3054 | code => sub { |
3055 | my ($param) = @_; | |
3056 | ||
3057 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3058 | ||
3059 | my $authuser = $rpcenv->get_user(); | |
3060 | ||
3061 | my $node = extract_param($param, 'node'); | |
3062 | ||
3063 | my $vmid = extract_param($param, 'vmid'); | |
3064 | ||
9dbd1ee4 AD |
3065 | my $snapname = extract_param($param, 'snapname'); |
3066 | ||
d1914468 DM |
3067 | die "unable to use snapshot name 'current' (reserved name)\n" |
3068 | if $snapname eq 'current'; | |
3069 | ||
7e7d7b61 | 3070 | my $realcmd = sub { |
22c377f0 | 3071 | PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname"); |
b2c9558d | 3072 | PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate}, |
af9110dd | 3073 | $param->{description}); |
7e7d7b61 DM |
3074 | }; |
3075 | ||
3076 | return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd); | |
3077 | }}); | |
3078 | ||
154ccdcd DM |
3079 | __PACKAGE__->register_method({ |
3080 | name => 'snapshot_cmd_idx', | |
3081 | path => '{vmid}/snapshot/{snapname}', | |
3082 | description => '', | |
3083 | method => 'GET', | |
3084 | permissions => { | |
3085 | user => 'all', | |
3086 | }, | |
3087 | parameters => { | |
3088 | additionalProperties => 0, | |
3089 | properties => { | |
3090 | vmid => get_standard_option('pve-vmid'), | |
3091 | node => get_standard_option('pve-node'), | |
8abd398b | 3092 | snapname => get_standard_option('pve-snapshot-name'), |
154ccdcd DM |
3093 | }, |
3094 | }, | |
3095 | returns => { | |
3096 | type => 'array', | |
3097 | items => { | |
3098 | type => "object", | |
3099 | properties => {}, | |
3100 | }, | |
3101 | links => [ { rel => 'child', href => "{cmd}" } ], | |
3102 | }, | |
3103 | code => sub { | |
3104 | my ($param) = @_; | |
3105 | ||
3106 | my $res = []; | |
3107 | ||
3108 | push @$res, { cmd => 'rollback' }; | |
d788cea6 | 3109 | push @$res, { cmd => 'config' }; |
154ccdcd DM |
3110 | |
3111 | return $res; | |
3112 | }}); | |
3113 | ||
d788cea6 DM |
3114 | __PACKAGE__->register_method({ |
3115 | name => 'update_snapshot_config', | |
3116 | path => '{vmid}/snapshot/{snapname}/config', | |
3117 | method => 'PUT', | |
3118 | protected => 1, | |
3119 | proxyto => 'node', | |
3120 | description => "Update snapshot metadata.", | |
3121 | permissions => { | |
3122 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3123 | }, | |
3124 | parameters => { | |
3125 | additionalProperties => 0, | |
3126 | properties => { | |
3127 | node => get_standard_option('pve-node'), | |
3128 | vmid => get_standard_option('pve-vmid'), | |
3129 | snapname => get_standard_option('pve-snapshot-name'), | |
3130 | description => { | |
3131 | optional => 1, | |
3132 | type => 'string', | |
3133 | description => "A textual description or comment.", | |
3134 | }, | |
3135 | }, | |
3136 | }, | |
3137 | returns => { type => 'null' }, | |
3138 | code => sub { | |
3139 | my ($param) = @_; | |
3140 | ||
3141 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3142 | ||
3143 | my $authuser = $rpcenv->get_user(); | |
3144 | ||
3145 | my $vmid = extract_param($param, 'vmid'); | |
3146 | ||
3147 | my $snapname = extract_param($param, 'snapname'); | |
3148 | ||
3149 | return undef if !defined($param->{description}); | |
3150 | ||
3151 | my $updatefn = sub { | |
3152 | ||
ffda963f | 3153 | my $conf = PVE::QemuConfig->load_config($vmid); |
d788cea6 | 3154 | |
ffda963f | 3155 | PVE::QemuConfig->check_lock($conf); |
d788cea6 DM |
3156 | |
3157 | my $snap = $conf->{snapshots}->{$snapname}; | |
3158 | ||
75466c4f DM |
3159 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
3160 | ||
d788cea6 DM |
3161 | $snap->{description} = $param->{description} if defined($param->{description}); |
3162 | ||
ffda963f | 3163 | PVE::QemuConfig->write_config($vmid, $conf); |
d788cea6 DM |
3164 | }; |
3165 | ||
ffda963f | 3166 | PVE::QemuConfig->lock_config($vmid, $updatefn); |
d788cea6 DM |
3167 | |
3168 | return undef; | |
3169 | }}); | |
3170 | ||
3171 | __PACKAGE__->register_method({ | |
3172 | name => 'get_snapshot_config', | |
3173 | path => '{vmid}/snapshot/{snapname}/config', | |
3174 | method => 'GET', | |
3175 | proxyto => 'node', | |
3176 | description => "Get snapshot configuration", | |
3177 | permissions => { | |
3178 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3179 | }, | |
3180 | parameters => { | |
3181 | additionalProperties => 0, | |
3182 | properties => { | |
3183 | node => get_standard_option('pve-node'), | |
3184 | vmid => get_standard_option('pve-vmid'), | |
3185 | snapname => get_standard_option('pve-snapshot-name'), | |
3186 | }, | |
3187 | }, | |
3188 | returns => { type => "object" }, | |
3189 | code => sub { | |
3190 | my ($param) = @_; | |
3191 | ||
3192 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3193 | ||
3194 | my $authuser = $rpcenv->get_user(); | |
3195 | ||
3196 | my $vmid = extract_param($param, 'vmid'); | |
3197 | ||
3198 | my $snapname = extract_param($param, 'snapname'); | |
3199 | ||
ffda963f | 3200 | my $conf = PVE::QemuConfig->load_config($vmid); |
d788cea6 DM |
3201 | |
3202 | my $snap = $conf->{snapshots}->{$snapname}; | |
3203 | ||
75466c4f DM |
3204 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
3205 | ||
d788cea6 DM |
3206 | return $snap; |
3207 | }}); | |
3208 | ||
7e7d7b61 DM |
3209 | __PACKAGE__->register_method({ |
3210 | name => 'rollback', | |
154ccdcd | 3211 | path => '{vmid}/snapshot/{snapname}/rollback', |
7e7d7b61 DM |
3212 | method => 'POST', |
3213 | protected => 1, | |
3214 | proxyto => 'node', | |
3215 | description => "Rollback VM state to specified snapshot.", | |
3216 | permissions => { | |
f1baf1df | 3217 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
7e7d7b61 DM |
3218 | }, |
3219 | parameters => { | |
3220 | additionalProperties => 0, | |
3221 | properties => { | |
3222 | node => get_standard_option('pve-node'), | |
335af808 | 3223 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 3224 | snapname => get_standard_option('pve-snapshot-name'), |
7e7d7b61 DM |
3225 | }, |
3226 | }, | |
3227 | returns => { | |
3228 | type => 'string', | |
3229 | description => "the task ID.", | |
3230 | }, | |
3231 | code => sub { | |
3232 | my ($param) = @_; | |
3233 | ||
3234 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3235 | ||
3236 | my $authuser = $rpcenv->get_user(); | |
3237 | ||
3238 | my $node = extract_param($param, 'node'); | |
3239 | ||
3240 | my $vmid = extract_param($param, 'vmid'); | |
3241 | ||
3242 | my $snapname = extract_param($param, 'snapname'); | |
3243 | ||
7e7d7b61 | 3244 | my $realcmd = sub { |
22c377f0 | 3245 | PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname"); |
b2c9558d | 3246 | PVE::QemuConfig->snapshot_rollback($vmid, $snapname); |
7e7d7b61 DM |
3247 | }; |
3248 | ||
3249 | return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd); | |
3250 | }}); | |
3251 | ||
3252 | __PACKAGE__->register_method({ | |
3253 | name => 'delsnapshot', | |
3254 | path => '{vmid}/snapshot/{snapname}', | |
3255 | method => 'DELETE', | |
3256 | protected => 1, | |
3257 | proxyto => 'node', | |
3258 | description => "Delete a VM snapshot.", | |
3259 | permissions => { | |
f1baf1df | 3260 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
7e7d7b61 DM |
3261 | }, |
3262 | parameters => { | |
3263 | additionalProperties => 0, | |
3264 | properties => { | |
3265 | node => get_standard_option('pve-node'), | |
335af808 | 3266 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 3267 | snapname => get_standard_option('pve-snapshot-name'), |
3ee28e38 DM |
3268 | force => { |
3269 | optional => 1, | |
3270 | type => 'boolean', | |
3271 | description => "For removal from config file, even if removing disk snapshots fails.", | |
3272 | }, | |
7e7d7b61 DM |
3273 | }, |
3274 | }, | |
3275 | returns => { | |
3276 | type => 'string', | |
3277 | description => "the task ID.", | |
3278 | }, | |
3279 | code => sub { | |
3280 | my ($param) = @_; | |
3281 | ||
3282 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3283 | ||
3284 | my $authuser = $rpcenv->get_user(); | |
3285 | ||
3286 | my $node = extract_param($param, 'node'); | |
3287 | ||
3288 | my $vmid = extract_param($param, 'vmid'); | |
3289 | ||
3290 | my $snapname = extract_param($param, 'snapname'); | |
3291 | ||
7e7d7b61 | 3292 | my $realcmd = sub { |
22c377f0 | 3293 | PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); |
b2c9558d | 3294 | PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force}); |
7e7d7b61 | 3295 | }; |
9dbd1ee4 | 3296 | |
7b2257a8 | 3297 | return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); |
9dbd1ee4 AD |
3298 | }}); |
3299 | ||
04a69bb4 AD |
3300 | __PACKAGE__->register_method({ |
3301 | name => 'template', | |
3302 | path => '{vmid}/template', | |
3303 | method => 'POST', | |
3304 | protected => 1, | |
3305 | proxyto => 'node', | |
3306 | description => "Create a Template.", | |
b02691d8 | 3307 | permissions => { |
7af0a6c8 DM |
3308 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", |
3309 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
b02691d8 | 3310 | }, |
04a69bb4 AD |
3311 | parameters => { |
3312 | additionalProperties => 0, | |
3313 | properties => { | |
3314 | node => get_standard_option('pve-node'), | |
335af808 | 3315 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), |
04a69bb4 AD |
3316 | disk => { |
3317 | optional => 1, | |
3318 | type => 'string', | |
3319 | description => "If you want to convert only 1 disk to base image.", | |
74479ee9 | 3320 | enum => [PVE::QemuServer::valid_drive_names()], |
04a69bb4 AD |
3321 | }, |
3322 | ||
3323 | }, | |
3324 | }, | |
3325 | returns => { type => 'null'}, | |
3326 | code => sub { | |
3327 | my ($param) = @_; | |
3328 | ||
3329 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3330 | ||
3331 | my $authuser = $rpcenv->get_user(); | |
3332 | ||
3333 | my $node = extract_param($param, 'node'); | |
3334 | ||
3335 | my $vmid = extract_param($param, 'vmid'); | |
3336 | ||
3337 | my $disk = extract_param($param, 'disk'); | |
3338 | ||
3339 | my $updatefn = sub { | |
3340 | ||
ffda963f | 3341 | my $conf = PVE::QemuConfig->load_config($vmid); |
04a69bb4 | 3342 | |
ffda963f | 3343 | PVE::QemuConfig->check_lock($conf); |
04a69bb4 | 3344 | |
75466c4f | 3345 | die "unable to create template, because VM contains snapshots\n" |
b91c2aae | 3346 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); |
0402a80b | 3347 | |
75466c4f | 3348 | die "you can't convert a template to a template\n" |
ffda963f | 3349 | if PVE::QemuConfig->is_template($conf) && !$disk; |
0402a80b | 3350 | |
75466c4f | 3351 | die "you can't convert a VM to template if VM is running\n" |
218cab9a | 3352 | if PVE::QemuServer::check_running($vmid); |
35c5fdef | 3353 | |
04a69bb4 AD |
3354 | my $realcmd = sub { |
3355 | PVE::QemuServer::template_create($vmid, $conf, $disk); | |
3356 | }; | |
04a69bb4 | 3357 | |
75e7e997 | 3358 | $conf->{template} = 1; |
ffda963f | 3359 | PVE::QemuConfig->write_config($vmid, $conf); |
75e7e997 DM |
3360 | |
3361 | return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd); | |
04a69bb4 AD |
3362 | }; |
3363 | ||
ffda963f | 3364 | PVE::QemuConfig->lock_config($vmid, $updatefn); |
04a69bb4 AD |
3365 | return undef; |
3366 | }}); | |
3367 | ||
1e3baf05 | 3368 | 1; |