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