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