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