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