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