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