]>
Commit | Line | Data |
---|---|---|
f76a2828 DM |
1 | package PVE::API2::LXC; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Tools qw(extract_param run_command); | |
0620edda | 8 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); |
f76a2828 | 9 | use PVE::INotify; |
9c2d4ce9 | 10 | use PVE::Cluster qw(cfs_read_file); |
f76a2828 | 11 | use PVE::AccessControl; |
2f9b5ead | 12 | use PVE::Firewall; |
f76a2828 DM |
13 | use PVE::Storage; |
14 | use PVE::RESTHandler; | |
15 | use PVE::RPCEnvironment; | |
c5a8e04f | 16 | use PVE::ReplicationConfig; |
f76a2828 | 17 | use PVE::LXC; |
7af97ad5 | 18 | use PVE::LXC::Create; |
6f42807e | 19 | use PVE::LXC::Migrate; |
56462053 | 20 | use PVE::GuestHelpers; |
c5a09704 | 21 | use PVE::VZDump::Plugin; |
52389a07 DM |
22 | use PVE::API2::LXC::Config; |
23 | use PVE::API2::LXC::Status; | |
24 | use PVE::API2::LXC::Snapshot; | |
f76a2828 DM |
25 | use PVE::JSONSchema qw(get_standard_option); |
26 | use base qw(PVE::RESTHandler); | |
27 | ||
6c2e9377 WB |
28 | BEGIN { |
29 | if (!$ENV{PVE_GENERATING_DOCS}) { | |
30 | require PVE::HA::Env::PVE2; | |
31 | import PVE::HA::Env::PVE2; | |
32 | require PVE::HA::Config; | |
33 | import PVE::HA::Config; | |
34 | } | |
35 | } | |
f76a2828 | 36 | |
52389a07 DM |
37 | __PACKAGE__->register_method ({ |
38 | subclass => "PVE::API2::LXC::Config", | |
fa91e46f | 39 | path => '{vmid}/config', |
52389a07 | 40 | }); |
f76a2828 | 41 | |
52389a07 DM |
42 | __PACKAGE__->register_method ({ |
43 | subclass => "PVE::API2::LXC::Status", | |
44 | path => '{vmid}/status', | |
45 | }); | |
f76a2828 | 46 | |
52389a07 DM |
47 | __PACKAGE__->register_method ({ |
48 | subclass => "PVE::API2::LXC::Snapshot", | |
49 | path => '{vmid}/snapshot', | |
50 | }); | |
f76a2828 | 51 | |
52389a07 DM |
52 | __PACKAGE__->register_method ({ |
53 | subclass => "PVE::API2::Firewall::CT", | |
54 | path => '{vmid}/firewall', | |
55 | }); | |
1e6c8d5b | 56 | |
f76a2828 | 57 | __PACKAGE__->register_method({ |
5c752bbf DM |
58 | name => 'vmlist', |
59 | path => '', | |
f76a2828 DM |
60 | method => 'GET', |
61 | description => "LXC container index (per node).", | |
62 | permissions => { | |
63 | description => "Only list CTs where you have VM.Audit permissons on /vms/<vmid>.", | |
64 | user => 'all', | |
65 | }, | |
66 | proxyto => 'node', | |
67 | protected => 1, # /proc files are only readable by root | |
68 | parameters => { | |
69 | additionalProperties => 0, | |
70 | properties => { | |
71 | node => get_standard_option('pve-node'), | |
72 | }, | |
73 | }, | |
74 | returns => { | |
75 | type => 'array', | |
76 | items => { | |
77 | type => "object", | |
8233d33d | 78 | properties => $PVE::LXC::vmstatus_return_properties, |
f76a2828 DM |
79 | }, |
80 | links => [ { rel => 'child', href => "{vmid}" } ], | |
81 | }, | |
82 | code => sub { | |
83 | my ($param) = @_; | |
84 | ||
85 | my $rpcenv = PVE::RPCEnvironment::get(); | |
86 | my $authuser = $rpcenv->get_user(); | |
87 | ||
88 | my $vmstatus = PVE::LXC::vmstatus(); | |
89 | ||
90 | my $res = []; | |
91 | foreach my $vmid (keys %$vmstatus) { | |
92 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
93 | ||
94 | my $data = $vmstatus->{$vmid}; | |
f76a2828 DM |
95 | push @$res, $data; |
96 | } | |
97 | ||
98 | return $res; | |
5c752bbf | 99 | |
f76a2828 DM |
100 | }}); |
101 | ||
9c2d4ce9 | 102 | __PACKAGE__->register_method({ |
5c752bbf DM |
103 | name => 'create_vm', |
104 | path => '', | |
9c2d4ce9 DM |
105 | method => 'POST', |
106 | description => "Create or restore a container.", | |
107 | permissions => { | |
108 | user => 'all', # check inside | |
109 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . | |
110 | "For restore, it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
111 | "You also need 'Datastore.AllocateSpace' permissions on the storage.", | |
112 | }, | |
113 | protected => 1, | |
114 | proxyto => 'node', | |
115 | parameters => { | |
116 | additionalProperties => 0, | |
1b4cf758 | 117 | properties => PVE::LXC::Config->json_config_properties({ |
9c2d4ce9 | 118 | node => get_standard_option('pve-node'), |
781e26b2 | 119 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), |
9c2d4ce9 DM |
120 | ostemplate => { |
121 | description => "The OS template or backup file.", | |
5c752bbf | 122 | type => 'string', |
9c2d4ce9 | 123 | maxLength => 255, |
68e8f3c5 | 124 | completion => \&PVE::LXC::complete_os_templates, |
9c2d4ce9 | 125 | }, |
5c752bbf DM |
126 | password => { |
127 | optional => 1, | |
9c2d4ce9 DM |
128 | type => 'string', |
129 | description => "Sets root password inside container.", | |
168d6b07 | 130 | minLength => 5, |
9c2d4ce9 DM |
131 | }, |
132 | storage => get_standard_option('pve-storage-id', { | |
eb35f9c0 | 133 | description => "Default Storage.", |
9c2d4ce9 DM |
134 | default => 'local', |
135 | optional => 1, | |
c5362cda | 136 | completion => \&PVE::Storage::complete_storage_enabled, |
9c2d4ce9 DM |
137 | }), |
138 | force => { | |
5c752bbf | 139 | optional => 1, |
9c2d4ce9 DM |
140 | type => 'boolean', |
141 | description => "Allow to overwrite existing container.", | |
142 | }, | |
143 | restore => { | |
5c752bbf | 144 | optional => 1, |
9c2d4ce9 DM |
145 | type => 'boolean', |
146 | description => "Mark this as restore task.", | |
147 | }, | |
43912111 CE |
148 | unique => { |
149 | optional => 1, | |
150 | type => 'boolean', | |
151 | description => "Assign a unique random ethernet address.", | |
152 | requires => 'restore', | |
153 | }, | |
5c752bbf | 154 | pool => { |
9c2d4ce9 DM |
155 | optional => 1, |
156 | type => 'string', format => 'pve-poolid', | |
157 | description => "Add the VM to the specified pool.", | |
158 | }, | |
7c78b6cc WB |
159 | 'ignore-unpack-errors' => { |
160 | optional => 1, | |
161 | type => 'boolean', | |
162 | description => "Ignore errors when extracting the template.", | |
163 | }, | |
34ddbf08 FG |
164 | 'ssh-public-keys' => { |
165 | optional => 1, | |
166 | type => 'string', | |
167 | description => "Setup public SSH keys (one key per line, " . | |
168 | "OpenSSH format).", | |
169 | }, | |
f9b4407e | 170 | bwlimit => { |
8ead60ef | 171 | description => "Override I/O bandwidth limit (in KiB/s).", |
f9b4407e WB |
172 | optional => 1, |
173 | type => 'number', | |
174 | minimum => '0', | |
41e9b65c | 175 | default => 'restore limit from datacenter or storage config', |
f9b4407e | 176 | }, |
9d0225fb TL |
177 | start => { |
178 | optional => 1, | |
179 | type => 'boolean', | |
180 | default => 0, | |
181 | description => "Start the CT after its creation finished successfully.", | |
182 | }, | |
9c2d4ce9 DM |
183 | }), |
184 | }, | |
5c752bbf | 185 | returns => { |
9c2d4ce9 DM |
186 | type => 'string', |
187 | }, | |
188 | code => sub { | |
189 | my ($param) = @_; | |
190 | ||
61783321 TL |
191 | PVE::Cluster::check_cfs_quorum(); |
192 | ||
9c2d4ce9 | 193 | my $rpcenv = PVE::RPCEnvironment::get(); |
9c2d4ce9 DM |
194 | my $authuser = $rpcenv->get_user(); |
195 | ||
196 | my $node = extract_param($param, 'node'); | |
9c2d4ce9 | 197 | my $vmid = extract_param($param, 'vmid'); |
7c78b6cc | 198 | my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors'); |
f9b4407e | 199 | my $bwlimit = extract_param($param, 'bwlimit'); |
9d0225fb TL |
200 | my $start_after_create = extract_param($param, 'start'); |
201 | ||
67afe46e | 202 | my $basecfg_fn = PVE::LXC::Config->config_file($vmid); |
9c2d4ce9 DM |
203 | my $same_container_exists = -f $basecfg_fn; |
204 | ||
425b62cb WB |
205 | # 'unprivileged' is read-only, so we can't pass it to update_pct_config |
206 | my $unprivileged = extract_param($param, 'unprivileged'); | |
9c2d4ce9 | 207 | my $restore = extract_param($param, 'restore'); |
43912111 | 208 | my $unique = extract_param($param, 'unique'); |
9c2d4ce9 | 209 | |
39170644 FG |
210 | # used to skip firewall config restore if user lacks permission |
211 | my $skip_fw_config_restore = 0; | |
212 | ||
148d1cb4 DM |
213 | if ($restore) { |
214 | # fixme: limit allowed parameters | |
148d1cb4 | 215 | } |
351bc0c4 | 216 | |
9c2d4ce9 DM |
217 | my $force = extract_param($param, 'force'); |
218 | ||
219 | if (!($same_container_exists && $restore && $force)) { | |
220 | PVE::Cluster::check_vmid_unused($vmid); | |
e22af68f | 221 | } else { |
61783321 | 222 | die "can't overwrite running container\n" if PVE::LXC::check_running($vmid); |
67afe46e FG |
223 | my $conf = PVE::LXC::Config->load_config($vmid); |
224 | PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid"); | |
9c2d4ce9 | 225 | } |
5c752bbf | 226 | |
9c2d4ce9 | 227 | my $password = extract_param($param, 'password'); |
34ddbf08 | 228 | my $ssh_keys = extract_param($param, 'ssh-public-keys'); |
2130286f | 229 | PVE::Tools::validate_ssh_public_keys($ssh_keys) if defined($ssh_keys); |
34ddbf08 | 230 | |
27916659 | 231 | my $pool = extract_param($param, 'pool'); |
9c2d4ce9 DM |
232 | if (defined($pool)) { |
233 | $rpcenv->check_pool_exist($pool); | |
234 | $rpcenv->check_perm_modify($authuser, "/pool/$pool"); | |
5c752bbf | 235 | } |
9c2d4ce9 DM |
236 | |
237 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { | |
238 | # OK | |
239 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
240 | # OK | |
241 | } elsif ($restore && $force && $same_container_exists && | |
242 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
243 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
39170644 FG |
244 | |
245 | # we don't want to restore a container-provided FW conf in this case | |
246 | # since the user is lacking permission to configure the container's FW | |
247 | $skip_fw_config_restore = 1; | |
9c2d4ce9 DM |
248 | } else { |
249 | raise_perm_exc(); | |
250 | } | |
251 | ||
9eb69152 | 252 | my $ostemplate = extract_param($param, 'ostemplate'); |
bb6afcb0 DM |
253 | my $storage = extract_param($param, 'storage') // 'local'; |
254 | ||
9eb69152 FG |
255 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []); |
256 | ||
bb6afcb0 | 257 | my $storage_cfg = cfs_read_file("storage.cfg"); |
9c2d4ce9 | 258 | |
9c2d4ce9 | 259 | my $archive; |
9c2d4ce9 | 260 | if ($ostemplate eq '-') { |
351bc0c4 TM |
261 | die "pipe requires cli environment\n" |
262 | if $rpcenv->{type} ne 'cli'; | |
263 | die "pipe can only be used with restore tasks\n" | |
148d1cb4 DM |
264 | if !$restore; |
265 | $archive = '-'; | |
b51a98d4 | 266 | die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs}); |
9c2d4ce9 | 267 | } else { |
f26c66e0 | 268 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, $vmid, $ostemplate); |
9c2d4ce9 DM |
269 | $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate); |
270 | } | |
271 | ||
f9b4407e | 272 | my %used_storages; |
bb6afcb0 DM |
273 | my $check_and_activate_storage = sub { |
274 | my ($sid) = @_; | |
275 | ||
276 | my $scfg = PVE::Storage::storage_check_node($storage_cfg, $sid, $node); | |
277 | ||
278 | raise_param_exc({ storage => "storage '$sid' does not support container directories"}) | |
279 | if !$scfg->{content}->{rootdir}; | |
280 | ||
281 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
282 | ||
283 | PVE::Storage::activate_storage($storage_cfg, $sid); | |
f9b4407e | 284 | $used_storages{$sid} = 1; |
bb6afcb0 DM |
285 | }; |
286 | ||
9c2d4ce9 | 287 | my $conf = {}; |
5b4657d0 | 288 | |
99132ce0 WB |
289 | my $is_root = $authuser eq 'root@pam'; |
290 | ||
b51a98d4 | 291 | my $no_disk_param = {}; |
db18c1e4 FG |
292 | my $mp_param = {}; |
293 | my $storage_only_mode = 1; | |
b51a98d4 | 294 | foreach my $opt (keys %$param) { |
78ccc99b DM |
295 | my $value = $param->{$opt}; |
296 | if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) { | |
297 | # allow to use simple numbers (add default storage in that case) | |
db18c1e4 FG |
298 | if ($value =~ m/^\d+(\.\d+)?$/) { |
299 | $mp_param->{$opt} = "$storage:$value"; | |
300 | } else { | |
301 | $mp_param->{$opt} = $value; | |
302 | } | |
303 | $storage_only_mode = 0; | |
a9dd8015 FG |
304 | } elsif ($opt =~ m/^unused\d+$/) { |
305 | warn "ignoring '$opt', cannot create/restore with unused volume\n"; | |
306 | delete $param->{$opt}; | |
78ccc99b DM |
307 | } else { |
308 | $no_disk_param->{$opt} = $value; | |
309 | } | |
b51a98d4 | 310 | } |
bb6afcb0 | 311 | |
235dbdf3 | 312 | die "mount points configured, but 'rootfs' not set - aborting\n" |
db18c1e4 FG |
313 | if !$storage_only_mode && !defined($mp_param->{rootfs}); |
314 | ||
bb6afcb0 | 315 | # check storage access, activate storage |
db18c1e4 FG |
316 | my $delayed_mp_param = {}; |
317 | PVE::LXC::Config->foreach_mountpoint($mp_param, sub { | |
bb6afcb0 DM |
318 | my ($ms, $mountpoint) = @_; |
319 | ||
320 | my $volid = $mountpoint->{volume}; | |
321 | my $mp = $mountpoint->{mp}; | |
322 | ||
e2007ac2 DM |
323 | if ($mountpoint->{type} ne 'volume') { # bind or device |
324 | die "Only root can pass arbitrary filesystem paths.\n" | |
99132ce0 | 325 | if !$is_root; |
e2007ac2 DM |
326 | } else { |
327 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
328 | &$check_and_activate_storage($sid); | |
329 | } | |
bb6afcb0 DM |
330 | }); |
331 | ||
332 | # check/activate default storage | |
db18c1e4 | 333 | &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs}); |
bb6afcb0 | 334 | |
1b4cf758 | 335 | PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param); |
9c2d4ce9 | 336 | |
425b62cb WB |
337 | $conf->{unprivileged} = 1 if $unprivileged; |
338 | ||
61783321 | 339 | my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -"; |
bccaa371 | 340 | |
61783321 TL |
341 | eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) }; |
342 | die "$emsg $@" if $@; | |
bccaa371 | 343 | |
61783321 TL |
344 | my $code = sub { |
345 | my $old_conf = PVE::LXC::Config->load_config($vmid); | |
a2d3e5c3 | 346 | my $was_template; |
bccaa371 | 347 | |
eb35f9c0 | 348 | my $vollist = []; |
27916659 | 349 | eval { |
5a7a51d0 WB |
350 | my $orig_mp_param; # only used if $restore |
351 | if ($restore) { | |
61783321 | 352 | die "can't overwrite running container\n" if PVE::LXC::check_running($vmid); |
a5246958 | 353 | if ($is_root && $archive ne '-') { |
a48e5562 | 354 | my $orig_conf; |
a5246958 | 355 | ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive); |
a2d3e5c3 | 356 | $was_template = delete $orig_conf->{template}; |
a67908f1 | 357 | # When we're root call 'restore_configuration' with restricted=0, |
5a7a51d0 WB |
358 | # causing it to restore the raw lxc entries, among which there may be |
359 | # 'lxc.idmap' entries. We need to make sure that the extracted contents | |
360 | # of the container match up with the restored configuration afterwards: | |
a67908f1 | 361 | $conf->{lxc} = $orig_conf->{lxc}; |
5a7a51d0 | 362 | } |
f360d7f1 | 363 | } |
db18c1e4 | 364 | if ($storage_only_mode) { |
b51a98d4 | 365 | if ($restore) { |
a48e5562 | 366 | if (!defined($orig_mp_param)) { |
a5246958 OB |
367 | (undef, $orig_mp_param) = PVE::LXC::Create::recover_config($archive); |
368 | } | |
f360d7f1 | 369 | $mp_param = $orig_mp_param; |
db18c1e4 FG |
370 | die "rootfs configuration could not be recovered, please check and specify manually!\n" |
371 | if !defined($mp_param->{rootfs}); | |
372 | PVE::LXC::Config->foreach_mountpoint($mp_param, sub { | |
373 | my ($ms, $mountpoint) = @_; | |
374 | my $type = $mountpoint->{type}; | |
375 | if ($type eq 'volume') { | |
376 | die "unable to detect disk size - please specify $ms (size)\n" | |
377 | if !defined($mountpoint->{size}); | |
378 | my $disksize = $mountpoint->{size} / (1024 * 1024 * 1024); # create_disks expects GB as unit size | |
379 | delete $mountpoint->{size}; | |
380 | $mountpoint->{volume} = "$storage:$disksize"; | |
381 | $mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); | |
382 | } else { | |
15e4d443 | 383 | my $type = $mountpoint->{type}; |
7e0e7f38 FG |
384 | die "restoring rootfs to $type mount is only possible by specifying -rootfs manually!\n" |
385 | if ($ms eq 'rootfs'); | |
20f9339d | 386 | die "restoring '$ms' to $type mount is only possible for root\n" |
99132ce0 | 387 | if !$is_root; |
7e0e7f38 | 388 | |
15e4d443 FG |
389 | if ($mountpoint->{backup}) { |
390 | warn "WARNING - unsupported configuration!\n"; | |
235dbdf3 FG |
391 | warn "backup was enabled for $type mount point $ms ('$mountpoint->{mp}')\n"; |
392 | warn "mount point configuration will be restored after archive extraction!\n"; | |
15e4d443 FG |
393 | warn "contained files will be restored to wrong directory!\n"; |
394 | } | |
136040f4 | 395 | delete $mp_param->{$ms}; # actually delay bind/dev mps |
db18c1e4 FG |
396 | $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs'); |
397 | } | |
398 | }); | |
b51a98d4 | 399 | } else { |
db18c1e4 | 400 | $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB |
b51a98d4 DM |
401 | } |
402 | } | |
403 | ||
db18c1e4 FG |
404 | $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf); |
405 | ||
61783321 TL |
406 | # we always have the 'create' lock so check for more than 1 entry |
407 | if (scalar(keys %$old_conf) > 1) { | |
51665c2d | 408 | # destroy old container volumes |
61783321 | 409 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, { lock => 'create' }); |
51665c2d | 410 | } |
51665c2d FG |
411 | |
412 | eval { | |
413 | my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1); | |
f9b4407e WB |
414 | $bwlimit = PVE::Storage::get_bandwidth_limit('restore', [keys %used_storages], $bwlimit); |
415 | PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit); | |
51665c2d FG |
416 | |
417 | if ($restore) { | |
39170644 | 418 | PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root, $unique, $skip_fw_config_restore); |
4b4bbe55 OB |
419 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); |
420 | $lxc_setup->template_fixup($conf); | |
51665c2d FG |
421 | } else { |
422 | my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS | |
423 | PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection) | |
424 | $lxc_setup->post_create_hook($password, $ssh_keys); | |
425 | } | |
426 | }; | |
427 | my $err = $@; | |
428 | PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0); | |
429 | PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf)); | |
430 | die $err if $err; | |
27916659 DM |
431 | # set some defaults |
432 | $conf->{hostname} ||= "CT$vmid"; | |
433 | $conf->{memory} ||= 512; | |
434 | $conf->{swap} //= 512; | |
db18c1e4 FG |
435 | foreach my $mp (keys %$delayed_mp_param) { |
436 | $conf->{$mp} = $delayed_mp_param->{$mp}; | |
437 | } | |
a2d3e5c3 CE |
438 | # If the template flag was set, we try to convert again to template after restore |
439 | if ($was_template) { | |
440 | print STDERR "Convert restored container to template...\n"; | |
441 | if (my $err = check_storage_supports_templates($conf)) { | |
442 | warn $err; | |
443 | warn "Leave restored backup as container instead of converting to template.\n" | |
444 | } else { | |
445 | PVE::LXC::template_create($vmid, $conf); | |
446 | $conf->{template} = 1; | |
447 | } | |
448 | } | |
67afe46e | 449 | PVE::LXC::Config->write_config($vmid, $conf); |
27916659 DM |
450 | }; |
451 | if (my $err = $@) { | |
6c871c36 | 452 | PVE::LXC::destroy_disks($storage_cfg, $vollist); |
4eeb9af1 | 453 | eval { PVE::LXC::Config->destroy_config($vmid) }; |
d7d48be6 | 454 | warn $@ if $@; |
61783321 | 455 | die "$emsg $err"; |
6d098bf4 | 456 | } |
87273b2b | 457 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
9d0225fb TL |
458 | |
459 | PVE::API2::LXC::Status->vm_start({ vmid => $vmid, node => $node }) | |
460 | if $start_after_create; | |
9c2d4ce9 | 461 | }; |
5c752bbf | 462 | |
8a6fc617 | 463 | my $workername = $restore ? 'vzrestore' : 'vzcreate'; |
67afe46e | 464 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
9c2d4ce9 | 465 | |
8a6fc617 | 466 | return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd); |
9c2d4ce9 DM |
467 | }}); |
468 | ||
a2d3e5c3 CE |
469 | sub check_storage_supports_templates { |
470 | my ($conf) = @_; | |
471 | ||
472 | my $scfg = PVE::Storage::config(); | |
473 | eval { | |
474 | PVE::LXC::Config->foreach_mountpoint($conf, sub { | |
475 | my ($ms, $mp) = @_; | |
476 | ||
477 | my ($sid) = PVE::Storage::parse_volume_id($mp->{volume}, 0); | |
478 | die "Warning: Directory storage '$sid' does not support container templates!\n" | |
479 | if $scfg->{ids}->{$sid}->{path}; | |
480 | }); | |
481 | }; | |
482 | return $@ | |
483 | } | |
484 | ||
f76a2828 DM |
485 | __PACKAGE__->register_method({ |
486 | name => 'vmdiridx', | |
5c752bbf | 487 | path => '{vmid}', |
f76a2828 DM |
488 | method => 'GET', |
489 | proxyto => 'node', | |
490 | description => "Directory index", | |
491 | permissions => { | |
492 | user => 'all', | |
493 | }, | |
494 | parameters => { | |
495 | additionalProperties => 0, | |
496 | properties => { | |
497 | node => get_standard_option('pve-node'), | |
498 | vmid => get_standard_option('pve-vmid'), | |
499 | }, | |
500 | }, | |
501 | returns => { | |
502 | type => 'array', | |
503 | items => { | |
504 | type => "object", | |
505 | properties => { | |
506 | subdir => { type => 'string' }, | |
507 | }, | |
508 | }, | |
509 | links => [ { rel => 'child', href => "{subdir}" } ], | |
510 | }, | |
511 | code => sub { | |
512 | my ($param) = @_; | |
513 | ||
514 | # test if VM exists | |
67afe46e | 515 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); |
f76a2828 DM |
516 | |
517 | my $res = [ | |
518 | { subdir => 'config' }, | |
37c2dba8 | 519 | { subdir => 'pending' }, |
fff3a342 DM |
520 | { subdir => 'status' }, |
521 | { subdir => 'vncproxy' }, | |
a0226a00 | 522 | { subdir => 'termproxy' }, |
fff3a342 DM |
523 | { subdir => 'vncwebsocket' }, |
524 | { subdir => 'spiceproxy' }, | |
525 | { subdir => 'migrate' }, | |
c4a33727 | 526 | { subdir => 'clone' }, |
f76a2828 DM |
527 | # { subdir => 'initlog' }, |
528 | { subdir => 'rrd' }, | |
529 | { subdir => 'rrddata' }, | |
530 | { subdir => 'firewall' }, | |
cc5392c8 | 531 | { subdir => 'snapshot' }, |
985b18ed | 532 | { subdir => 'resize' }, |
f76a2828 | 533 | ]; |
5c752bbf | 534 | |
f76a2828 DM |
535 | return $res; |
536 | }}); | |
537 | ||
c4a33727 | 538 | |
f76a2828 | 539 | __PACKAGE__->register_method({ |
5c752bbf DM |
540 | name => 'rrd', |
541 | path => '{vmid}/rrd', | |
f76a2828 DM |
542 | method => 'GET', |
543 | protected => 1, # fixme: can we avoid that? | |
544 | permissions => { | |
545 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
546 | }, | |
547 | description => "Read VM RRD statistics (returns PNG)", | |
548 | parameters => { | |
549 | additionalProperties => 0, | |
550 | properties => { | |
551 | node => get_standard_option('pve-node'), | |
552 | vmid => get_standard_option('pve-vmid'), | |
553 | timeframe => { | |
554 | description => "Specify the time frame you are interested in.", | |
555 | type => 'string', | |
556 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
557 | }, | |
558 | ds => { | |
559 | description => "The list of datasources you want to display.", | |
560 | type => 'string', format => 'pve-configid-list', | |
561 | }, | |
562 | cf => { | |
563 | description => "The RRD consolidation function", | |
564 | type => 'string', | |
565 | enum => [ 'AVERAGE', 'MAX' ], | |
566 | optional => 1, | |
567 | }, | |
568 | }, | |
569 | }, | |
570 | returns => { | |
571 | type => "object", | |
572 | properties => { | |
573 | filename => { type => 'string' }, | |
574 | }, | |
575 | }, | |
576 | code => sub { | |
577 | my ($param) = @_; | |
578 | ||
579 | return PVE::Cluster::create_rrd_graph( | |
5c752bbf | 580 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
f76a2828 | 581 | $param->{ds}, $param->{cf}); |
5c752bbf | 582 | |
f76a2828 DM |
583 | }}); |
584 | ||
585 | __PACKAGE__->register_method({ | |
5c752bbf DM |
586 | name => 'rrddata', |
587 | path => '{vmid}/rrddata', | |
f76a2828 DM |
588 | method => 'GET', |
589 | protected => 1, # fixme: can we avoid that? | |
590 | permissions => { | |
591 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
592 | }, | |
593 | description => "Read VM RRD statistics", | |
594 | parameters => { | |
595 | additionalProperties => 0, | |
596 | properties => { | |
597 | node => get_standard_option('pve-node'), | |
598 | vmid => get_standard_option('pve-vmid'), | |
599 | timeframe => { | |
600 | description => "Specify the time frame you are interested in.", | |
601 | type => 'string', | |
602 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
603 | }, | |
604 | cf => { | |
605 | description => "The RRD consolidation function", | |
606 | type => 'string', | |
607 | enum => [ 'AVERAGE', 'MAX' ], | |
608 | optional => 1, | |
609 | }, | |
610 | }, | |
611 | }, | |
612 | returns => { | |
613 | type => "array", | |
614 | items => { | |
615 | type => "object", | |
616 | properties => {}, | |
617 | }, | |
618 | }, | |
619 | code => sub { | |
620 | my ($param) = @_; | |
621 | ||
622 | return PVE::Cluster::create_rrd_data( | |
623 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
624 | }}); | |
625 | ||
f76a2828 | 626 | __PACKAGE__->register_method({ |
5c752bbf DM |
627 | name => 'destroy_vm', |
628 | path => '{vmid}', | |
f76a2828 DM |
629 | method => 'DELETE', |
630 | protected => 1, | |
631 | proxyto => 'node', | |
632 | description => "Destroy the container (also delete all uses files).", | |
633 | permissions => { | |
634 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
635 | }, | |
636 | parameters => { | |
637 | additionalProperties => 0, | |
638 | properties => { | |
639 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 640 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
c5a09704 CE |
641 | purge => { |
642 | type => 'boolean', | |
643 | description => "Remove vmid from backup cron jobs.", | |
644 | optional => 1, | |
645 | }, | |
f76a2828 DM |
646 | }, |
647 | }, | |
5c752bbf | 648 | returns => { |
f76a2828 DM |
649 | type => 'string', |
650 | }, | |
651 | code => sub { | |
652 | my ($param) = @_; | |
653 | ||
654 | my $rpcenv = PVE::RPCEnvironment::get(); | |
f76a2828 | 655 | my $authuser = $rpcenv->get_user(); |
f76a2828 DM |
656 | my $vmid = $param->{vmid}; |
657 | ||
658 | # test if container exists | |
67afe46e | 659 | my $conf = PVE::LXC::Config->load_config($vmid); |
611fe3aa | 660 | my $storage_cfg = cfs_read_file("storage.cfg"); |
67afe46e | 661 | PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid"); |
7e806596 | 662 | |
9d87e069 | 663 | die "unable to remove CT $vmid - used in HA resources\n" |
b6e0b774 AG |
664 | if PVE::HA::Config::vm_is_ha_managed($vmid); |
665 | ||
c5a09704 CE |
666 | if (!$param->{purge}) { |
667 | # do not allow destroy if there are replication jobs without purge | |
668 | my $repl_conf = PVE::ReplicationConfig->new(); | |
669 | $repl_conf->check_for_existing_jobs($vmid); | |
670 | } | |
c5a8e04f | 671 | |
d607c17d DM |
672 | my $running_error_msg = "unable to destroy CT $vmid - container is running\n"; |
673 | ||
674 | die $running_error_msg if PVE::LXC::check_running($vmid); # check early | |
675 | ||
611fe3aa | 676 | my $code = sub { |
673cf209 | 677 | # reload config after lock |
67afe46e FG |
678 | $conf = PVE::LXC::Config->load_config($vmid); |
679 | PVE::LXC::Config->check_lock($conf); | |
673cf209 | 680 | |
d607c17d DM |
681 | die $running_error_msg if PVE::LXC::check_running($vmid); |
682 | ||
7fc1d9eb TL |
683 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf, { lock => 'destroyed' }); |
684 | ||
be5fc936 | 685 | PVE::AccessControl::remove_vm_access($vmid); |
2f9b5ead | 686 | PVE::Firewall::remove_vmfw_conf($vmid); |
c5a09704 CE |
687 | if ($param->{purge}) { |
688 | PVE::ReplicationConfig::remove_vmid_jobs($vmid); | |
689 | PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid); | |
690 | } | |
7fc1d9eb TL |
691 | |
692 | # only now remove the zombie config, else we can have reuse race | |
693 | PVE::LXC::Config->destroy_config($vmid); | |
f76a2828 DM |
694 | }; |
695 | ||
67afe46e | 696 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
351bc0c4 | 697 | |
f76a2828 DM |
698 | return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd); |
699 | }}); | |
700 | ||
fff3a342 DM |
701 | my $sslcert; |
702 | ||
703 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
704 | name => 'vncproxy', |
705 | path => '{vmid}/vncproxy', | |
fff3a342 DM |
706 | method => 'POST', |
707 | protected => 1, | |
708 | permissions => { | |
709 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
710 | }, | |
711 | description => "Creates a TCP VNC proxy connections.", | |
712 | parameters => { | |
713 | additionalProperties => 0, | |
714 | properties => { | |
715 | node => get_standard_option('pve-node'), | |
716 | vmid => get_standard_option('pve-vmid'), | |
717 | websocket => { | |
718 | optional => 1, | |
719 | type => 'boolean', | |
720 | description => "use websocket instead of standard VNC.", | |
721 | }, | |
bd0f4d6d DC |
722 | width => { |
723 | optional => 1, | |
724 | description => "sets the width of the console in pixels.", | |
725 | type => 'integer', | |
726 | minimum => 16, | |
727 | maximum => 4096, | |
728 | }, | |
729 | height => { | |
730 | optional => 1, | |
731 | description => "sets the height of the console in pixels.", | |
732 | type => 'integer', | |
733 | minimum => 16, | |
734 | maximum => 2160, | |
735 | }, | |
fff3a342 DM |
736 | }, |
737 | }, | |
5b4657d0 | 738 | returns => { |
fff3a342 DM |
739 | additionalProperties => 0, |
740 | properties => { | |
741 | user => { type => 'string' }, | |
742 | ticket => { type => 'string' }, | |
743 | cert => { type => 'string' }, | |
744 | port => { type => 'integer' }, | |
745 | upid => { type => 'string' }, | |
746 | }, | |
747 | }, | |
748 | code => sub { | |
749 | my ($param) = @_; | |
750 | ||
751 | my $rpcenv = PVE::RPCEnvironment::get(); | |
752 | ||
753 | my $authuser = $rpcenv->get_user(); | |
754 | ||
755 | my $vmid = $param->{vmid}; | |
756 | my $node = $param->{node}; | |
757 | ||
758 | my $authpath = "/vms/$vmid"; | |
759 | ||
760 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
761 | ||
762 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
763 | if !$sslcert; | |
764 | ||
ec2c57eb | 765 | my ($remip, $family); |
5b4657d0 | 766 | |
fff3a342 | 767 | if ($node ne PVE::INotify::nodename()) { |
85ae6211 | 768 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); |
ec2c57eb WB |
769 | } else { |
770 | $family = PVE::Tools::get_host_address_family($node); | |
fff3a342 DM |
771 | } |
772 | ||
ec2c57eb WB |
773 | my $port = PVE::Tools::next_vnc_port($family); |
774 | ||
fff3a342 DM |
775 | # NOTE: vncterm VNC traffic is already TLS encrypted, |
776 | # so we select the fastest chipher here (or 'none'?) | |
5b4657d0 | 777 | my $remcmd = $remip ? |
806eae70 | 778 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : []; |
fff3a342 | 779 | |
67afe46e | 780 | my $conf = PVE::LXC::Config->load_config($vmid, $node); |
65213b67 | 781 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
aca816ad | 782 | |
5b4657d0 DM |
783 | my $shcmd = [ '/usr/bin/dtach', '-A', |
784 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 785 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
786 | |
787 | my $realcmd = sub { | |
788 | my $upid = shift; | |
789 | ||
5b4657d0 | 790 | syslog ('info', "starting lxc vnc proxy $upid\n"); |
fff3a342 | 791 | |
5b4657d0 | 792 | my $timeout = 10; |
fff3a342 DM |
793 | |
794 | my $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
5b4657d0 | 795 | '-timeout', $timeout, '-authpath', $authpath, |
fff3a342 DM |
796 | '-perm', 'VM.Console']; |
797 | ||
bd0f4d6d DC |
798 | if ($param->{width}) { |
799 | push @$cmd, '-width', $param->{width}; | |
800 | } | |
801 | ||
802 | if ($param->{height}) { | |
803 | push @$cmd, '-height', $param->{height}; | |
804 | } | |
805 | ||
fff3a342 | 806 | if ($param->{websocket}) { |
5b4657d0 | 807 | $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm |
fff3a342 DM |
808 | push @$cmd, '-notls', '-listen', 'localhost'; |
809 | } | |
810 | ||
811 | push @$cmd, '-c', @$remcmd, @$shcmd; | |
812 | ||
37634d3d | 813 | run_command($cmd, keeplocale => 1); |
fff3a342 DM |
814 | |
815 | return; | |
816 | }; | |
817 | ||
818 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
819 | ||
820 | PVE::Tools::wait_for_vnc_port($port); | |
821 | ||
822 | return { | |
823 | user => $authuser, | |
824 | ticket => $ticket, | |
5b4657d0 DM |
825 | port => $port, |
826 | upid => $upid, | |
827 | cert => $sslcert, | |
fff3a342 DM |
828 | }; |
829 | }}); | |
830 | ||
a0226a00 DC |
831 | __PACKAGE__->register_method ({ |
832 | name => 'termproxy', | |
833 | path => '{vmid}/termproxy', | |
834 | method => 'POST', | |
835 | protected => 1, | |
836 | permissions => { | |
837 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
838 | }, | |
839 | description => "Creates a TCP proxy connection.", | |
840 | parameters => { | |
841 | additionalProperties => 0, | |
842 | properties => { | |
843 | node => get_standard_option('pve-node'), | |
844 | vmid => get_standard_option('pve-vmid'), | |
845 | }, | |
846 | }, | |
847 | returns => { | |
848 | additionalProperties => 0, | |
849 | properties => { | |
850 | user => { type => 'string' }, | |
851 | ticket => { type => 'string' }, | |
852 | port => { type => 'integer' }, | |
853 | upid => { type => 'string' }, | |
854 | }, | |
855 | }, | |
856 | code => sub { | |
857 | my ($param) = @_; | |
858 | ||
859 | my $rpcenv = PVE::RPCEnvironment::get(); | |
860 | ||
861 | my $authuser = $rpcenv->get_user(); | |
862 | ||
863 | my $vmid = $param->{vmid}; | |
864 | my $node = $param->{node}; | |
865 | ||
866 | my $authpath = "/vms/$vmid"; | |
867 | ||
868 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
869 | ||
870 | my ($remip, $family); | |
871 | ||
872 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
873 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
874 | } else { | |
875 | $family = PVE::Tools::get_host_address_family($node); | |
876 | } | |
877 | ||
878 | my $port = PVE::Tools::next_vnc_port($family); | |
879 | ||
880 | my $remcmd = $remip ? | |
881 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : []; | |
882 | ||
883 | my $conf = PVE::LXC::Config->load_config($vmid, $node); | |
65213b67 | 884 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
a0226a00 DC |
885 | |
886 | my $shcmd = [ '/usr/bin/dtach', '-A', | |
887 | "/var/run/dtach/vzctlconsole$vmid", | |
888 | '-r', 'winch', '-z', @$concmd]; | |
889 | ||
890 | my $realcmd = sub { | |
891 | my $upid = shift; | |
892 | ||
893 | syslog ('info', "starting lxc termproxy $upid\n"); | |
894 | ||
895 | my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, | |
896 | '--perm', 'VM.Console', '--']; | |
897 | push @$cmd, @$remcmd, @$shcmd; | |
898 | ||
899 | PVE::Tools::run_command($cmd); | |
900 | }; | |
901 | ||
902 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); | |
903 | ||
904 | PVE::Tools::wait_for_vnc_port($port); | |
905 | ||
906 | return { | |
907 | user => $authuser, | |
908 | ticket => $ticket, | |
909 | port => $port, | |
910 | upid => $upid, | |
911 | }; | |
912 | }}); | |
913 | ||
fff3a342 DM |
914 | __PACKAGE__->register_method({ |
915 | name => 'vncwebsocket', | |
916 | path => '{vmid}/vncwebsocket', | |
917 | method => 'GET', | |
5b4657d0 | 918 | permissions => { |
fff3a342 DM |
919 | description => "You also need to pass a valid ticket (vncticket).", |
920 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
921 | }, | |
922 | description => "Opens a weksocket for VNC traffic.", | |
923 | parameters => { | |
924 | additionalProperties => 0, | |
925 | properties => { | |
926 | node => get_standard_option('pve-node'), | |
927 | vmid => get_standard_option('pve-vmid'), | |
928 | vncticket => { | |
929 | description => "Ticket from previous call to vncproxy.", | |
930 | type => 'string', | |
931 | maxLength => 512, | |
932 | }, | |
933 | port => { | |
934 | description => "Port number returned by previous vncproxy call.", | |
935 | type => 'integer', | |
936 | minimum => 5900, | |
937 | maximum => 5999, | |
938 | }, | |
939 | }, | |
940 | }, | |
941 | returns => { | |
942 | type => "object", | |
943 | properties => { | |
944 | port => { type => 'string' }, | |
945 | }, | |
946 | }, | |
947 | code => sub { | |
948 | my ($param) = @_; | |
949 | ||
950 | my $rpcenv = PVE::RPCEnvironment::get(); | |
951 | ||
952 | my $authuser = $rpcenv->get_user(); | |
953 | ||
954 | my $authpath = "/vms/$param->{vmid}"; | |
955 | ||
956 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
957 | ||
958 | my $port = $param->{port}; | |
5b4657d0 | 959 | |
fff3a342 DM |
960 | return { port => $port }; |
961 | }}); | |
962 | ||
963 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
964 | name => 'spiceproxy', |
965 | path => '{vmid}/spiceproxy', | |
fff3a342 DM |
966 | method => 'POST', |
967 | protected => 1, | |
968 | proxyto => 'node', | |
969 | permissions => { | |
970 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
971 | }, | |
972 | description => "Returns a SPICE configuration to connect to the CT.", | |
973 | parameters => { | |
974 | additionalProperties => 0, | |
975 | properties => { | |
976 | node => get_standard_option('pve-node'), | |
977 | vmid => get_standard_option('pve-vmid'), | |
978 | proxy => get_standard_option('spice-proxy', { optional => 1 }), | |
979 | }, | |
980 | }, | |
981 | returns => get_standard_option('remote-viewer-config'), | |
982 | code => sub { | |
983 | my ($param) = @_; | |
984 | ||
985 | my $vmid = $param->{vmid}; | |
986 | my $node = $param->{node}; | |
987 | my $proxy = $param->{proxy}; | |
988 | ||
989 | my $authpath = "/vms/$vmid"; | |
990 | my $permissions = 'VM.Console'; | |
991 | ||
67afe46e | 992 | my $conf = PVE::LXC::Config->load_config($vmid); |
da4db334 TL |
993 | |
994 | die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid); | |
995 | ||
aca816ad DM |
996 | my $concmd = PVE::LXC::get_console_command($vmid, $conf); |
997 | ||
5b4657d0 DM |
998 | my $shcmd = ['/usr/bin/dtach', '-A', |
999 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 1000 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
1001 | |
1002 | my $title = "CT $vmid"; | |
1003 | ||
1004 | return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd); | |
1005 | }}); | |
5c752bbf | 1006 | |
5c752bbf DM |
1007 | |
1008 | __PACKAGE__->register_method({ | |
52389a07 DM |
1009 | name => 'migrate_vm', |
1010 | path => '{vmid}/migrate', | |
5c752bbf DM |
1011 | method => 'POST', |
1012 | protected => 1, | |
1013 | proxyto => 'node', | |
52389a07 | 1014 | description => "Migrate the container to another node. Creates a new migration task.", |
5c752bbf | 1015 | permissions => { |
52389a07 | 1016 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], |
5c752bbf DM |
1017 | }, |
1018 | parameters => { | |
1019 | additionalProperties => 0, | |
1020 | properties => { | |
1021 | node => get_standard_option('pve-node'), | |
68e8f3c5 DM |
1022 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
1023 | target => get_standard_option('pve-node', { | |
1024 | description => "Target node.", | |
39bb88df | 1025 | completion => \&PVE::Cluster::complete_migration_target, |
68e8f3c5 | 1026 | }), |
52389a07 DM |
1027 | online => { |
1028 | type => 'boolean', | |
1029 | description => "Use online/live migration.", | |
1030 | optional => 1, | |
1031 | }, | |
00d8cdc0 DC |
1032 | restart => { |
1033 | type => 'boolean', | |
1034 | description => "Use restart migration", | |
1035 | optional => 1, | |
1036 | }, | |
1037 | timeout => { | |
1038 | type => 'integer', | |
1039 | description => "Timeout in seconds for shutdown for restart migration", | |
1040 | optional => 1, | |
1041 | default => 180, | |
1042 | }, | |
9746c095 FG |
1043 | force => { |
1044 | type => 'boolean', | |
1045 | description => "Force migration despite local bind / device" . | |
552e168f | 1046 | " mounts. NOTE: deprecated, use 'shared' property of mount point instead.", |
9746c095 FG |
1047 | optional => 1, |
1048 | }, | |
8ead60ef SI |
1049 | bwlimit => { |
1050 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1051 | optional => 1, | |
1052 | type => 'number', | |
1053 | minimum => '0', | |
41e9b65c | 1054 | default => 'migrate limit from datacenter or storage config', |
8ead60ef | 1055 | }, |
5c752bbf DM |
1056 | }, |
1057 | }, | |
1058 | returns => { | |
1059 | type => 'string', | |
52389a07 | 1060 | description => "the task ID.", |
5c752bbf DM |
1061 | }, |
1062 | code => sub { | |
1063 | my ($param) = @_; | |
1064 | ||
1065 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1066 | ||
1067 | my $authuser = $rpcenv->get_user(); | |
1068 | ||
52389a07 | 1069 | my $target = extract_param($param, 'target'); |
bb1ac2de | 1070 | |
52389a07 DM |
1071 | my $localnode = PVE::INotify::nodename(); |
1072 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
bb1ac2de | 1073 | |
52389a07 | 1074 | PVE::Cluster::check_cfs_quorum(); |
5c752bbf | 1075 | |
52389a07 | 1076 | PVE::Cluster::check_node_exists($target); |
27916659 | 1077 | |
52389a07 | 1078 | my $targetip = PVE::Cluster::remote_node_ip($target); |
5c752bbf | 1079 | |
52389a07 | 1080 | my $vmid = extract_param($param, 'vmid'); |
5c752bbf | 1081 | |
52389a07 | 1082 | # test if VM exists |
67afe46e | 1083 | PVE::LXC::Config->load_config($vmid); |
5c752bbf | 1084 | |
52389a07 DM |
1085 | # try to detect errors early |
1086 | if (PVE::LXC::check_running($vmid)) { | |
00d8cdc0 DC |
1087 | die "can't migrate running container without --online or --restart\n" |
1088 | if !$param->{online} && !$param->{restart}; | |
5c752bbf | 1089 | } |
5c752bbf DM |
1090 | |
1091 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
1092 | ||
1093 | my $hacmd = sub { | |
1094 | my $upid = shift; | |
1095 | ||
1096 | my $service = "ct:$vmid"; | |
1097 | ||
52389a07 | 1098 | my $cmd = ['ha-manager', 'migrate', $service, $target]; |
5c752bbf | 1099 | |
545dd4e1 | 1100 | print "Requesting HA migration for CT $vmid to node $target\n"; |
5c752bbf DM |
1101 | |
1102 | PVE::Tools::run_command($cmd); | |
1103 | ||
1104 | return; | |
1105 | }; | |
1106 | ||
52389a07 | 1107 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); |
5c752bbf DM |
1108 | |
1109 | } else { | |
1110 | ||
22557519 DM |
1111 | my $realcmd = sub { |
1112 | PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param); | |
1113 | }; | |
56462053 | 1114 | |
22557519 DM |
1115 | my $worker = sub { |
1116 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
5c752bbf DM |
1117 | }; |
1118 | ||
22557519 | 1119 | return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker); |
5c752bbf DM |
1120 | } |
1121 | }}); | |
1122 | ||
cc5392c8 WL |
1123 | __PACKAGE__->register_method({ |
1124 | name => 'vm_feature', | |
1125 | path => '{vmid}/feature', | |
1126 | method => 'GET', | |
1127 | proxyto => 'node', | |
1128 | protected => 1, | |
1129 | description => "Check if feature for virtual machine is available.", | |
1130 | permissions => { | |
1131 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1132 | }, | |
1133 | parameters => { | |
1134 | additionalProperties => 0, | |
1135 | properties => { | |
1136 | node => get_standard_option('pve-node'), | |
1137 | vmid => get_standard_option('pve-vmid'), | |
1138 | feature => { | |
1139 | description => "Feature to check.", | |
1140 | type => 'string', | |
d53e5c58 | 1141 | enum => [ 'snapshot', 'clone', 'copy' ], |
cc5392c8 | 1142 | }, |
5ec3949c | 1143 | snapname => get_standard_option('pve-snapshot-name', { |
cc5392c8 WL |
1144 | optional => 1, |
1145 | }), | |
1146 | }, | |
1147 | }, | |
1148 | returns => { | |
1149 | type => "object", | |
1150 | properties => { | |
1151 | hasFeature => { type => 'boolean' }, | |
1152 | #nodes => { | |
1153 | #type => 'array', | |
1154 | #items => { type => 'string' }, | |
1155 | #} | |
1156 | }, | |
1157 | }, | |
1158 | code => sub { | |
1159 | my ($param) = @_; | |
1160 | ||
1161 | my $node = extract_param($param, 'node'); | |
1162 | ||
1163 | my $vmid = extract_param($param, 'vmid'); | |
1164 | ||
1165 | my $snapname = extract_param($param, 'snapname'); | |
1166 | ||
1167 | my $feature = extract_param($param, 'feature'); | |
1168 | ||
67afe46e | 1169 | my $conf = PVE::LXC::Config->load_config($vmid); |
cc5392c8 WL |
1170 | |
1171 | if($snapname){ | |
1172 | my $snap = $conf->{snapshots}->{$snapname}; | |
1173 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
1174 | $conf = $snap; | |
1175 | } | |
ef241384 | 1176 | my $storage_cfg = PVE::Storage::config(); |
cc5392c8 | 1177 | #Maybe include later |
ef241384 | 1178 | #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg); |
4518000b | 1179 | my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname); |
cc5392c8 WL |
1180 | |
1181 | return { | |
1182 | hasFeature => $hasFeature, | |
1183 | #nodes => [ keys %$nodelist ], | |
1184 | }; | |
1185 | }}); | |
bb1ac2de DM |
1186 | |
1187 | __PACKAGE__->register_method({ | |
1188 | name => 'template', | |
1189 | path => '{vmid}/template', | |
1190 | method => 'POST', | |
1191 | protected => 1, | |
1192 | proxyto => 'node', | |
1193 | description => "Create a Template.", | |
1194 | permissions => { | |
1195 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
1196 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1197 | }, | |
1198 | parameters => { | |
1199 | additionalProperties => 0, | |
1200 | properties => { | |
1201 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 1202 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
bb1ac2de DM |
1203 | }, |
1204 | }, | |
1205 | returns => { type => 'null'}, | |
1206 | code => sub { | |
1207 | my ($param) = @_; | |
1208 | ||
1209 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1210 | ||
1211 | my $authuser = $rpcenv->get_user(); | |
1212 | ||
1213 | my $node = extract_param($param, 'node'); | |
1214 | ||
1215 | my $vmid = extract_param($param, 'vmid'); | |
1216 | ||
1217 | my $updatefn = sub { | |
1218 | ||
67afe46e FG |
1219 | my $conf = PVE::LXC::Config->load_config($vmid); |
1220 | PVE::LXC::Config->check_lock($conf); | |
bb1ac2de DM |
1221 | |
1222 | die "unable to create template, because CT contains snapshots\n" | |
1223 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
1224 | ||
1225 | die "you can't convert a template to a template\n" | |
67afe46e | 1226 | if PVE::LXC::Config->is_template($conf); |
bb1ac2de DM |
1227 | |
1228 | die "you can't convert a CT to template if the CT is running\n" | |
1229 | if PVE::LXC::check_running($vmid); | |
1230 | ||
a2d3e5c3 CE |
1231 | if (my $err = check_storage_supports_templates($conf)) { |
1232 | die $err; | |
1233 | } | |
e1313b73 | 1234 | |
bb1ac2de DM |
1235 | my $realcmd = sub { |
1236 | PVE::LXC::template_create($vmid, $conf); | |
bb1ac2de | 1237 | |
c2182c49 | 1238 | $conf->{template} = 1; |
bb1ac2de | 1239 | |
c2182c49 WL |
1240 | PVE::LXC::Config->write_config($vmid, $conf); |
1241 | # and remove lxc config | |
1242 | PVE::LXC::update_lxc_config($vmid, $conf); | |
1243 | }; | |
bb1ac2de DM |
1244 | |
1245 | return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd); | |
1246 | }; | |
1247 | ||
67afe46e | 1248 | PVE::LXC::Config->lock_config($vmid, $updatefn); |
bb1ac2de DM |
1249 | |
1250 | return undef; | |
1251 | }}); | |
1252 | ||
c4a33727 DM |
1253 | __PACKAGE__->register_method({ |
1254 | name => 'clone_vm', | |
1255 | path => '{vmid}/clone', | |
1256 | method => 'POST', | |
1257 | protected => 1, | |
1258 | proxyto => 'node', | |
1259 | description => "Create a container clone/copy", | |
1260 | permissions => { | |
1261 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, " . | |
1262 | "and 'VM.Allocate' permissions " . | |
1263 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
1264 | "'Datastore.AllocateSpace' on any used storage.", | |
1265 | check => | |
1266 | [ 'and', | |
1267 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
1268 | [ 'or', | |
1269 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
1270 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
1271 | ], | |
1272 | ] | |
1273 | }, | |
1274 | parameters => { | |
1275 | additionalProperties => 0, | |
1276 | properties => { | |
1277 | node => get_standard_option('pve-node'), | |
1278 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1279 | newid => get_standard_option('pve-vmid', { | |
1280 | completion => \&PVE::Cluster::complete_next_vmid, | |
1281 | description => 'VMID for the clone.' }), | |
1282 | hostname => { | |
1283 | optional => 1, | |
1284 | type => 'string', format => 'dns-name', | |
1285 | description => "Set a hostname for the new CT.", | |
1286 | }, | |
1287 | description => { | |
1288 | optional => 1, | |
1289 | type => 'string', | |
1290 | description => "Description for the new CT.", | |
1291 | }, | |
1292 | pool => { | |
1293 | optional => 1, | |
1294 | type => 'string', format => 'pve-poolid', | |
1295 | description => "Add the new CT to the specified pool.", | |
1296 | }, | |
5ec3949c | 1297 | snapname => get_standard_option('pve-snapshot-name', { |
c4a33727 DM |
1298 | optional => 1, |
1299 | }), | |
1300 | storage => get_standard_option('pve-storage-id', { | |
1301 | description => "Target storage for full clone.", | |
c4a33727 DM |
1302 | optional => 1, |
1303 | }), | |
1304 | full => { | |
1305 | optional => 1, | |
1306 | type => 'boolean', | |
c4b4cb83 | 1307 | description => "Create a full copy of all disks. This is always done when " . |
c4a33727 | 1308 | "you clone a normal CT. For CT templates, we try to create a linked clone by default.", |
c4a33727 | 1309 | }, |
411ae12c DM |
1310 | target => get_standard_option('pve-node', { |
1311 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
1312 | optional => 1, | |
1313 | }), | |
8ead60ef SI |
1314 | bwlimit => { |
1315 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1316 | optional => 1, | |
1317 | type => 'number', | |
1318 | minimum => '0', | |
41e9b65c | 1319 | default => 'clone limit from datacenter or storage config', |
8ead60ef | 1320 | }, |
c4a33727 DM |
1321 | }, |
1322 | }, | |
1323 | returns => { | |
1324 | type => 'string', | |
1325 | }, | |
1326 | code => sub { | |
1327 | my ($param) = @_; | |
1328 | ||
1329 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1330 | ||
1331 | my $authuser = $rpcenv->get_user(); | |
1332 | ||
1333 | my $node = extract_param($param, 'node'); | |
1334 | ||
1335 | my $vmid = extract_param($param, 'vmid'); | |
1336 | ||
1337 | my $newid = extract_param($param, 'newid'); | |
1338 | ||
1339 | my $pool = extract_param($param, 'pool'); | |
1340 | ||
1341 | if (defined($pool)) { | |
1342 | $rpcenv->check_pool_exist($pool); | |
1343 | } | |
1344 | ||
1345 | my $snapname = extract_param($param, 'snapname'); | |
1346 | ||
1347 | my $storage = extract_param($param, 'storage'); | |
1348 | ||
411ae12c DM |
1349 | my $target = extract_param($param, 'target'); |
1350 | ||
c4a33727 DM |
1351 | my $localnode = PVE::INotify::nodename(); |
1352 | ||
411ae12c DM |
1353 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
1354 | ||
1355 | PVE::Cluster::check_node_exists($target) if $target; | |
1356 | ||
c4a33727 DM |
1357 | my $storecfg = PVE::Storage::config(); |
1358 | ||
1359 | if ($storage) { | |
1360 | # check if storage is enabled on local node | |
1361 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
411ae12c DM |
1362 | if ($target) { |
1363 | # check if storage is available on target node | |
1364 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
1365 | # clone only works if target storage is shared | |
1366 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1367 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
1368 | } | |
c4a33727 DM |
1369 | } |
1370 | ||
1371 | PVE::Cluster::check_cfs_quorum(); | |
1372 | ||
db01d674 WB |
1373 | my $conffile; |
1374 | my $newconf = {}; | |
1375 | my $mountpoints = {}; | |
1376 | my $fullclone = {}; | |
1377 | my $vollist = []; | |
411ae12c | 1378 | my $running; |
c4a33727 | 1379 | |
db01d674 WB |
1380 | PVE::LXC::Config->lock_config($vmid, sub { |
1381 | my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk'); | |
e7d553c7 | 1382 | |
411ae12c DM |
1383 | $running = PVE::LXC::check_running($vmid) || 0; |
1384 | ||
e7d553c7 DM |
1385 | my $full = extract_param($param, 'full'); |
1386 | if (!defined($full)) { | |
1387 | $full = !PVE::LXC::Config->is_template($src_conf); | |
1388 | } | |
1389 | die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full; | |
1390 | ||
db01d674 WB |
1391 | eval { |
1392 | die "snapshot '$snapname' does not exist\n" | |
1393 | if $snapname && !defined($src_conf->{snapshots}->{$snapname}); | |
c4a33727 | 1394 | |
c4a33727 | 1395 | |
db01d674 | 1396 | my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf; |
c4a33727 | 1397 | |
db01d674 WB |
1398 | $conffile = PVE::LXC::Config->config_file($newid); |
1399 | die "unable to create CT $newid: config file already exists\n" | |
1400 | if -f $conffile; | |
c4a33727 | 1401 | |
24f9d440 | 1402 | my $sharedvm = 1; |
db01d674 WB |
1403 | foreach my $opt (keys %$src_conf) { |
1404 | next if $opt =~ m/^unused\d+$/; | |
c4a33727 | 1405 | |
db01d674 | 1406 | my $value = $src_conf->{$opt}; |
c4a33727 | 1407 | |
db01d674 WB |
1408 | if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) { |
1409 | my $mp = $opt eq 'rootfs' ? | |
1410 | PVE::LXC::Config->parse_ct_rootfs($value) : | |
1411 | PVE::LXC::Config->parse_ct_mountpoint($value); | |
c4a33727 | 1412 | |
db01d674 WB |
1413 | if ($mp->{type} eq 'volume') { |
1414 | my $volid = $mp->{volume}; | |
09687674 DM |
1415 | |
1416 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1417 | $sid = $storage if defined($storage); | |
1418 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
24f9d440 WB |
1419 | if (!$scfg->{shared}) { |
1420 | $sharedvm = 0; | |
1421 | warn "found non-shared volume: $volid\n" if $target; | |
1422 | } | |
09687674 DM |
1423 | |
1424 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
1425 | ||
e7d553c7 | 1426 | if ($full) { |
db01d674 WB |
1427 | die "Cannot do full clones on a running container without snapshots\n" |
1428 | if $running && !defined($snapname); | |
1429 | $fullclone->{$opt} = 1; | |
1430 | } else { | |
1431 | # not full means clone instead of copy | |
1432 | die "Linked clone feature for '$volid' is not available\n" | |
1433 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running); | |
1434 | } | |
c4a33727 | 1435 | |
db01d674 WB |
1436 | $mountpoints->{$opt} = $mp; |
1437 | push @$vollist, $volid; | |
c4a33727 | 1438 | |
c4a33727 | 1439 | } else { |
db01d674 WB |
1440 | # TODO: allow bind mounts? |
1441 | die "unable to clone mountpint '$opt' (type $mp->{type})\n"; | |
c4a33727 | 1442 | } |
63231f52 TL |
1443 | } elsif ($opt =~ m/^net(\d+)$/) { |
1444 | # always change MAC! address | |
1445 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1446 | my $net = PVE::LXC::Config->parse_lxc_network($value); | |
1447 | $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
1448 | $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net); | |
c4a33727 | 1449 | } else { |
db01d674 WB |
1450 | # copy everything else |
1451 | $newconf->{$opt} = $value; | |
c4a33727 | 1452 | } |
db01d674 | 1453 | } |
24f9d440 WB |
1454 | die "can't clone CT to node '$target' (CT uses local storage)\n" |
1455 | if $target && !$sharedvm; | |
c4a33727 | 1456 | |
db01d674 WB |
1457 | # Replace the 'disk' lock with a 'create' lock. |
1458 | $newconf->{lock} = 'create'; | |
1459 | ||
037dc1e2 | 1460 | delete $newconf->{pending}; |
db01d674 WB |
1461 | delete $newconf->{template}; |
1462 | if ($param->{hostname}) { | |
1463 | $newconf->{hostname} = $param->{hostname}; | |
c4a33727 | 1464 | } |
c4a33727 | 1465 | |
db01d674 WB |
1466 | if ($param->{description}) { |
1467 | $newconf->{description} = $param->{description}; | |
1468 | } | |
c4a33727 | 1469 | |
db01d674 WB |
1470 | # create empty/temp config - this fails if CT already exists on other node |
1471 | PVE::LXC::Config->write_config($newid, $newconf); | |
1472 | }; | |
1473 | if (my $err = $@) { | |
1474 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1475 | warn $@ if $@; | |
1476 | die $err; | |
c4a33727 | 1477 | } |
db01d674 | 1478 | }); |
c4a33727 | 1479 | |
db01d674 WB |
1480 | my $update_conf = sub { |
1481 | my ($key, $value) = @_; | |
1482 | return PVE::LXC::Config->lock_config($newid, sub { | |
1483 | my $conf = PVE::LXC::Config->load_config($newid); | |
1484 | die "Lost 'create' config lock, aborting.\n" | |
1485 | if !PVE::LXC::Config->has_lock($conf, 'create'); | |
1486 | $conf->{$key} = $value; | |
1487 | PVE::LXC::Config->write_config($newid, $conf); | |
1488 | }); | |
1489 | }; | |
c4a33727 | 1490 | |
db01d674 WB |
1491 | my $realcmd = sub { |
1492 | my ($upid) = @_; | |
c4a33727 | 1493 | |
db01d674 | 1494 | my $newvollist = []; |
c4a33727 | 1495 | |
411ae12c DM |
1496 | my $verify_running = PVE::LXC::check_running($vmid) || 0; |
1497 | die "unexpected state change\n" if $verify_running != $running; | |
1498 | ||
db01d674 WB |
1499 | eval { |
1500 | local $SIG{INT} = | |
1501 | local $SIG{TERM} = | |
1502 | local $SIG{QUIT} = | |
1503 | local $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
c4a33727 | 1504 | |
db01d674 | 1505 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); |
8ead60ef | 1506 | my $bwlimit = extract_param($param, 'bwlimit'); |
c4a33727 | 1507 | |
db01d674 WB |
1508 | foreach my $opt (keys %$mountpoints) { |
1509 | my $mp = $mountpoints->{$opt}; | |
1510 | my $volid = $mp->{volume}; | |
c4a33727 | 1511 | |
db01d674 WB |
1512 | my $newvolid; |
1513 | if ($fullclone->{$opt}) { | |
1514 | print "create full clone of mountpoint $opt ($volid)\n"; | |
8ead60ef SI |
1515 | my $source_storage = PVE::Storage::parse_volume_id($volid); |
1516 | my $target_storage = $storage // $source_storage; | |
1517 | my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', [$source_storage, $target_storage], $bwlimit); | |
1518 | $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname, $clonelimit); | |
db01d674 WB |
1519 | } else { |
1520 | print "create linked clone of mount point $opt ($volid)\n"; | |
1521 | $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname); | |
c4a33727 DM |
1522 | } |
1523 | ||
db01d674 WB |
1524 | push @$newvollist, $newvolid; |
1525 | $mp->{volume} = $newvolid; | |
c4a33727 | 1526 | |
db01d674 | 1527 | $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs')); |
c4a33727 DM |
1528 | } |
1529 | ||
db01d674 WB |
1530 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
1531 | PVE::LXC::Config->remove_lock($newid, 'create'); | |
411ae12c DM |
1532 | |
1533 | if ($target) { | |
1534 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
1535 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; | |
1536 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); | |
1537 | ||
1538 | my $newconffile = PVE::LXC::Config->config_file($newid, $target); | |
1539 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
1540 | if !rename($conffile, $newconffile); | |
1541 | } | |
c4a33727 | 1542 | }; |
db01d674 | 1543 | my $err = $@; |
c4a33727 | 1544 | |
db01d674 WB |
1545 | # Unlock the source config in any case: |
1546 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1547 | warn $@ if $@; | |
c4a33727 | 1548 | |
db01d674 WB |
1549 | if ($err) { |
1550 | # Now cleanup the config & disks: | |
1551 | unlink $conffile; | |
c4a33727 | 1552 | |
db01d674 WB |
1553 | sleep 1; # some storages like rbd need to wait before release volume - really? |
1554 | ||
1555 | foreach my $volid (@$newvollist) { | |
1556 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
1557 | warn $@ if $@; | |
1558 | } | |
1559 | die "clone failed: $err"; | |
1560 | } | |
1561 | ||
1562 | return; | |
c4a33727 DM |
1563 | }; |
1564 | ||
db01d674 WB |
1565 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); |
1566 | return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd); | |
c4a33727 DM |
1567 | }}); |
1568 | ||
1569 | ||
985b18ed WL |
1570 | __PACKAGE__->register_method({ |
1571 | name => 'resize_vm', | |
1572 | path => '{vmid}/resize', | |
1573 | method => 'PUT', | |
1574 | protected => 1, | |
1575 | proxyto => 'node', | |
235dbdf3 | 1576 | description => "Resize a container mount point.", |
985b18ed WL |
1577 | permissions => { |
1578 | check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1], | |
1579 | }, | |
1580 | parameters => { | |
1581 | additionalProperties => 0, | |
b8c5a95f WB |
1582 | properties => { |
1583 | node => get_standard_option('pve-node'), | |
1584 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1585 | disk => { | |
1586 | type => 'string', | |
1587 | description => "The disk you want to resize.", | |
d250604f | 1588 | enum => [PVE::LXC::Config->mountpoint_names()], |
b8c5a95f WB |
1589 | }, |
1590 | size => { | |
1591 | type => 'string', | |
1592 | pattern => '\+?\d+(\.\d+)?[KMGT]?', | |
1593 | 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.", | |
1594 | }, | |
1595 | digest => { | |
1596 | type => 'string', | |
1597 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1598 | maxLength => 40, | |
1599 | optional => 1, | |
1600 | } | |
1601 | }, | |
985b18ed | 1602 | }, |
9f3f7963 WL |
1603 | returns => { |
1604 | type => 'string', | |
1605 | description => "the task ID.", | |
1606 | }, | |
985b18ed WL |
1607 | code => sub { |
1608 | my ($param) = @_; | |
1609 | ||
1610 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1611 | ||
1612 | my $authuser = $rpcenv->get_user(); | |
1613 | ||
1614 | my $node = extract_param($param, 'node'); | |
1615 | ||
1616 | my $vmid = extract_param($param, 'vmid'); | |
1617 | ||
1618 | my $digest = extract_param($param, 'digest'); | |
1619 | ||
1620 | my $sizestr = extract_param($param, 'size'); | |
1621 | my $ext = ($sizestr =~ s/^\+//); | |
1622 | my $newsize = PVE::JSONSchema::parse_size($sizestr); | |
1623 | die "invalid size string" if !defined($newsize); | |
1624 | ||
1625 | die "no options specified\n" if !scalar(keys %$param); | |
1626 | ||
f1ba1a4b | 1627 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []); |
985b18ed WL |
1628 | |
1629 | my $storage_cfg = cfs_read_file("storage.cfg"); | |
1630 | ||
985b18ed WL |
1631 | my $code = sub { |
1632 | ||
67afe46e FG |
1633 | my $conf = PVE::LXC::Config->load_config($vmid); |
1634 | PVE::LXC::Config->check_lock($conf); | |
985b18ed WL |
1635 | |
1636 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1637 | ||
1638 | my $running = PVE::LXC::check_running($vmid); | |
1639 | ||
1640 | my $disk = $param->{disk}; | |
1b4cf758 FG |
1641 | my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) : |
1642 | PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk}); | |
44a9face | 1643 | |
985b18ed WL |
1644 | my $volid = $mp->{volume}; |
1645 | ||
1646 | my (undef, undef, $owner, undef, undef, undef, $format) = | |
1647 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
1648 | ||
235dbdf3 | 1649 | die "can't resize mount point owned by another container ($owner)" |
985b18ed WL |
1650 | if $vmid != $owner; |
1651 | ||
1652 | die "can't resize volume: $disk if snapshot exists\n" | |
1653 | if %{$conf->{snapshots}} && $format eq 'qcow2'; | |
1654 | ||
1655 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1656 | ||
1657 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
1658 | ||
4e1e1791 WB |
1659 | PVE::Storage::activate_volumes($storage_cfg, [$volid]); |
1660 | ||
985b18ed | 1661 | my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5); |
2e8bcfef | 1662 | |
16f6c7c6 | 1663 | die "Could not determine current size of volume '$volid'\n" if !defined($size); |
2e8bcfef | 1664 | |
985b18ed WL |
1665 | $newsize += $size if $ext; |
1666 | $newsize = int($newsize); | |
1667 | ||
1668 | die "unable to shrink disk size\n" if $newsize < $size; | |
1669 | ||
1670 | return if $size == $newsize; | |
1671 | ||
1672 | PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr"); | |
9f3f7963 | 1673 | my $realcmd = sub { |
e72c8b0e DM |
1674 | # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so |
1675 | # we pass 0 here (parameter only makes sense for qemu) | |
9f3f7963 WL |
1676 | PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0); |
1677 | ||
1678 | $mp->{size} = $newsize; | |
1b4cf758 | 1679 | $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs'); |
9f3f7963 | 1680 | |
67afe46e | 1681 | PVE::LXC::Config->write_config($vmid, $conf); |
9f3f7963 WL |
1682 | |
1683 | if ($format eq 'raw') { | |
46e62d2d TL |
1684 | # we need to ensure that the volume is mapped, if not needed this is a NOP |
1685 | my $path = PVE::Storage::map_volume($storage_cfg, $volid); | |
1686 | $path = PVE::Storage::path($storage_cfg, $volid) if !defined($path); | |
9f3f7963 | 1687 | if ($running) { |
2a993004 WL |
1688 | |
1689 | $mp->{mp} = '/'; | |
1690 | my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1]; | |
21f292ff | 1691 | $path = PVE::LXC::query_loopdev($path) if $use_loopdev; |
235dbdf3 | 1692 | die "internal error: CT running but mount point not attached to a loop device" |
2a993004 WL |
1693 | if !$path; |
1694 | PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev; | |
9f3f7963 WL |
1695 | |
1696 | # In order for resize2fs to know that we need online-resizing a mountpoint needs | |
1697 | # to be visible to it in its namespace. | |
1698 | # To not interfere with the rest of the system we unshare the current mount namespace, | |
1699 | # mount over /tmp and then run resize2fs. | |
1700 | ||
1701 | # interestingly we don't need to e2fsck on mounted systems... | |
1702 | my $quoted = PVE::Tools::shellquote($path); | |
1703 | my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted"; | |
ce9e6458 WB |
1704 | eval { |
1705 | PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]); | |
1706 | }; | |
1707 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
9f3f7963 | 1708 | } else { |
ce9e6458 WB |
1709 | eval { |
1710 | PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]); | |
1711 | PVE::Tools::run_command(['resize2fs', $path]); | |
1712 | }; | |
1713 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
80440faa | 1714 | |
46ea6368 TL |
1715 | # always un-map if not running, this is a NOP if not needed |
1716 | PVE::Storage::unmap_volume($storage_cfg, $volid); | |
9f3f7963 | 1717 | } |
985b18ed | 1718 | } |
9f3f7963 | 1719 | }; |
985b18ed | 1720 | |
9f3f7963 WL |
1721 | return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd); |
1722 | }; | |
985b18ed | 1723 | |
67afe46e | 1724 | return PVE::LXC::Config->lock_config($vmid, $code);; |
985b18ed WL |
1725 | }}); |
1726 | ||
3c0f6806 WB |
1727 | __PACKAGE__->register_method({ |
1728 | name => 'move_volume', | |
1729 | path => '{vmid}/move_volume', | |
1730 | method => 'POST', | |
1731 | protected => 1, | |
1732 | proxyto => 'node', | |
1733 | description => "Move a rootfs-/mp-volume to a different storage", | |
1734 | permissions => { | |
1735 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . | |
1736 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
1737 | check => | |
1738 | [ 'and', | |
1739 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
1740 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
1741 | ], | |
1742 | }, | |
1743 | parameters => { | |
1744 | additionalProperties => 0, | |
1745 | properties => { | |
1746 | node => get_standard_option('pve-node'), | |
1747 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1748 | volume => { | |
1749 | type => 'string', | |
1750 | enum => [ PVE::LXC::Config->mountpoint_names() ], | |
1751 | description => "Volume which will be moved.", | |
1752 | }, | |
1753 | storage => get_standard_option('pve-storage-id', { | |
1754 | description => "Target Storage.", | |
1755 | completion => \&PVE::Storage::complete_storage_enabled, | |
1756 | }), | |
1757 | delete => { | |
1758 | type => 'boolean', | |
1759 | description => "Delete the original volume after successful copy. By default the original is kept as an unused volume entry.", | |
1760 | optional => 1, | |
1761 | default => 0, | |
1762 | }, | |
1763 | digest => { | |
1764 | type => 'string', | |
1765 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1766 | maxLength => 40, | |
1767 | optional => 1, | |
8ead60ef SI |
1768 | }, |
1769 | bwlimit => { | |
1770 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1771 | optional => 1, | |
1772 | type => 'number', | |
1773 | minimum => '0', | |
41e9b65c | 1774 | default => 'clone limit from datacenter or storage config', |
8ead60ef | 1775 | }, |
3c0f6806 WB |
1776 | }, |
1777 | }, | |
1778 | returns => { | |
1779 | type => 'string', | |
1780 | }, | |
1781 | code => sub { | |
1782 | my ($param) = @_; | |
1783 | ||
1784 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1785 | ||
1786 | my $authuser = $rpcenv->get_user(); | |
1787 | ||
1788 | my $vmid = extract_param($param, 'vmid'); | |
1789 | ||
1790 | my $storage = extract_param($param, 'storage'); | |
1791 | ||
1792 | my $mpkey = extract_param($param, 'volume'); | |
1793 | ||
1794 | my $lockname = 'disk'; | |
1795 | ||
1796 | my ($mpdata, $old_volid); | |
1797 | ||
1798 | PVE::LXC::Config->lock_config($vmid, sub { | |
1799 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1800 | PVE::LXC::Config->check_lock($conf); | |
1801 | ||
1802 | die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid); | |
1803 | ||
1804 | if ($mpkey eq 'rootfs') { | |
1805 | $mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey}); | |
1806 | } elsif ($mpkey =~ m/mp\d+/) { | |
1807 | $mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey}); | |
1808 | } else { | |
1809 | die "Can't parse $mpkey\n"; | |
1810 | } | |
1811 | $old_volid = $mpdata->{volume}; | |
1812 | ||
1813 | die "you can't move a volume with snapshots and delete the source\n" | |
1814 | if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid); | |
1815 | ||
1816 | PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest}); | |
1817 | ||
1818 | PVE::LXC::Config->set_lock($vmid, $lockname); | |
1819 | }); | |
1820 | ||
1821 | my $realcmd = sub { | |
1822 | eval { | |
1823 | PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage"); | |
1824 | ||
1825 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1826 | my $storage_cfg = PVE::Storage::config(); | |
1827 | ||
1828 | my $new_volid; | |
1829 | ||
1830 | eval { | |
1831 | PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]); | |
8ead60ef SI |
1832 | my $bwlimit = extract_param($param, 'bwlimit'); |
1833 | my $source_storage = PVE::Storage::parse_volume_id($old_volid); | |
1834 | my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$source_storage, $storage], $bwlimit); | |
1835 | $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf, undef, $movelimit); | |
3c0f6806 WB |
1836 | $mpdata->{volume} = $new_volid; |
1837 | ||
1838 | PVE::LXC::Config->lock_config($vmid, sub { | |
1839 | my $digest = $conf->{digest}; | |
1840 | $conf = PVE::LXC::Config->load_config($vmid); | |
1841 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1842 | ||
1843 | $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs'); | |
1844 | ||
1845 | PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete}; | |
1846 | ||
1847 | PVE::LXC::Config->write_config($vmid, $conf); | |
1848 | }); | |
1849 | ||
1850 | eval { | |
1851 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
1852 | PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ]) | |
1853 | }; | |
1854 | warn $@ if $@; | |
1855 | }; | |
1856 | if (my $err = $@) { | |
1857 | eval { | |
1858 | PVE::Storage::vdisk_free($storage_cfg, $new_volid) | |
1859 | if defined($new_volid); | |
1860 | }; | |
1861 | warn $@ if $@; | |
1862 | die $err; | |
1863 | } | |
1864 | ||
1865 | if ($param->{delete}) { | |
1866 | eval { | |
1867 | PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]); | |
1868 | PVE::Storage::vdisk_free($storage_cfg, $old_volid); | |
1869 | }; | |
1870 | warn $@ if $@; | |
1871 | } | |
1872 | }; | |
1873 | my $err = $@; | |
1874 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1875 | warn $@ if $@; | |
1876 | die $err if $err; | |
1877 | }; | |
1878 | my $task = eval { | |
1879 | $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd); | |
1880 | }; | |
1881 | if (my $err = $@) { | |
1882 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1883 | warn $@ if $@; | |
1884 | die $err; | |
1885 | } | |
1886 | return $task; | |
1887 | }}); | |
1888 | ||
37c2dba8 OB |
1889 | __PACKAGE__->register_method({ |
1890 | name => 'vm_pending', | |
1891 | path => '{vmid}/pending', | |
1892 | method => 'GET', | |
1893 | proxyto => 'node', | |
1894 | description => 'Get container configuration, including pending changes.', | |
1895 | permissions => { | |
1896 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1897 | }, | |
1898 | parameters => { | |
1899 | additionalProperties => 0, | |
1900 | properties => { | |
1901 | node => get_standard_option('pve-node'), | |
1902 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1903 | }, | |
1904 | }, | |
1905 | returns => { | |
1906 | type => "array", | |
1907 | items => { | |
1908 | type => "object", | |
1909 | properties => { | |
1910 | key => { | |
1911 | description => 'Configuration option name.', | |
1912 | type => 'string', | |
1913 | }, | |
1914 | value => { | |
1915 | description => 'Current value.', | |
1916 | type => 'string', | |
1917 | optional => 1, | |
1918 | }, | |
1919 | pending => { | |
1920 | description => 'Pending value.', | |
1921 | type => 'string', | |
1922 | optional => 1, | |
1923 | }, | |
1924 | delete => { | |
1925 | description => "Indicates a pending delete request if present and not 0.", | |
1926 | type => 'integer', | |
1927 | minimum => 0, | |
1928 | maximum => 2, | |
1929 | optional => 1, | |
1930 | }, | |
1931 | }, | |
1932 | }, | |
1933 | }, | |
1934 | code => sub { | |
1935 | my ($param) = @_; | |
1936 | ||
1937 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); | |
1938 | ||
1939 | my $pending_delete_hash = PVE::LXC::Config->parse_pending_delete($conf->{pending}->{delete}); | |
1940 | ||
f622e7dc | 1941 | return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash); |
37c2dba8 OB |
1942 | }}); |
1943 | ||
f76a2828 | 1944 | 1; |