]>
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 | 318 | my $delayed_mp_param = {}; |
015740e6 | 319 | PVE::LXC::Config->foreach_volume($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}); | |
015740e6 | 374 | PVE::LXC::Config->foreach_volume($mp_param, sub { |
db18c1e4 FG |
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', | |
796e8eee TL |
624 | description => "Remove container from all related configurations." |
625 | ." For example, backup jobs, replication jobs or HA." | |
626 | ." Related ACLs and Firewall entries will *always* be removed.", | |
627 | default => 0, | |
c5a09704 CE |
628 | optional => 1, |
629 | }, | |
f76a2828 DM |
630 | }, |
631 | }, | |
5c752bbf | 632 | returns => { |
f76a2828 DM |
633 | type => 'string', |
634 | }, | |
635 | code => sub { | |
636 | my ($param) = @_; | |
637 | ||
638 | my $rpcenv = PVE::RPCEnvironment::get(); | |
f76a2828 | 639 | my $authuser = $rpcenv->get_user(); |
f76a2828 DM |
640 | my $vmid = $param->{vmid}; |
641 | ||
642 | # test if container exists | |
67afe46e | 643 | my $conf = PVE::LXC::Config->load_config($vmid); |
611fe3aa | 644 | my $storage_cfg = cfs_read_file("storage.cfg"); |
67afe46e | 645 | PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid"); |
7e806596 | 646 | |
3207b81a | 647 | my $ha_managed = PVE::HA::Config::service_is_configured("ct:$vmid"); |
b6e0b774 | 648 | |
c5a09704 | 649 | if (!$param->{purge}) { |
3207b81a TL |
650 | die "unable to remove CT $vmid - used in HA resources and purge parameter not set.\n" |
651 | if $ha_managed; | |
652 | ||
c5a09704 CE |
653 | # do not allow destroy if there are replication jobs without purge |
654 | my $repl_conf = PVE::ReplicationConfig->new(); | |
655 | $repl_conf->check_for_existing_jobs($vmid); | |
656 | } | |
c5a8e04f | 657 | |
d607c17d DM |
658 | my $running_error_msg = "unable to destroy CT $vmid - container is running\n"; |
659 | ||
660 | die $running_error_msg if PVE::LXC::check_running($vmid); # check early | |
661 | ||
611fe3aa | 662 | my $code = sub { |
673cf209 | 663 | # reload config after lock |
67afe46e FG |
664 | $conf = PVE::LXC::Config->load_config($vmid); |
665 | PVE::LXC::Config->check_lock($conf); | |
673cf209 | 666 | |
d607c17d DM |
667 | die $running_error_msg if PVE::LXC::check_running($vmid); |
668 | ||
7fc1d9eb TL |
669 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf, { lock => 'destroyed' }); |
670 | ||
be5fc936 | 671 | PVE::AccessControl::remove_vm_access($vmid); |
2f9b5ead | 672 | PVE::Firewall::remove_vmfw_conf($vmid); |
c5a09704 | 673 | if ($param->{purge}) { |
3207b81a | 674 | print "purging CT $vmid from related configurations..\n"; |
c5a09704 CE |
675 | PVE::ReplicationConfig::remove_vmid_jobs($vmid); |
676 | PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid); | |
3207b81a TL |
677 | |
678 | if ($ha_managed) { | |
679 | PVE::HA::Config::delete_service_from_config("ct:$vmid"); | |
680 | print "NOTE: removed CT $vmid from HA resource configuration.\n"; | |
681 | } | |
c5a09704 | 682 | } |
7fc1d9eb TL |
683 | |
684 | # only now remove the zombie config, else we can have reuse race | |
685 | PVE::LXC::Config->destroy_config($vmid); | |
f76a2828 DM |
686 | }; |
687 | ||
67afe46e | 688 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
351bc0c4 | 689 | |
f76a2828 DM |
690 | return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd); |
691 | }}); | |
692 | ||
fff3a342 DM |
693 | my $sslcert; |
694 | ||
695 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
696 | name => 'vncproxy', |
697 | path => '{vmid}/vncproxy', | |
fff3a342 DM |
698 | method => 'POST', |
699 | protected => 1, | |
700 | permissions => { | |
701 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
702 | }, | |
703 | description => "Creates a TCP VNC proxy connections.", | |
704 | parameters => { | |
705 | additionalProperties => 0, | |
706 | properties => { | |
707 | node => get_standard_option('pve-node'), | |
708 | vmid => get_standard_option('pve-vmid'), | |
709 | websocket => { | |
710 | optional => 1, | |
711 | type => 'boolean', | |
712 | description => "use websocket instead of standard VNC.", | |
713 | }, | |
bd0f4d6d DC |
714 | width => { |
715 | optional => 1, | |
716 | description => "sets the width of the console in pixels.", | |
717 | type => 'integer', | |
718 | minimum => 16, | |
719 | maximum => 4096, | |
720 | }, | |
721 | height => { | |
722 | optional => 1, | |
723 | description => "sets the height of the console in pixels.", | |
724 | type => 'integer', | |
725 | minimum => 16, | |
726 | maximum => 2160, | |
727 | }, | |
fff3a342 DM |
728 | }, |
729 | }, | |
5b4657d0 | 730 | returns => { |
fff3a342 DM |
731 | additionalProperties => 0, |
732 | properties => { | |
733 | user => { type => 'string' }, | |
734 | ticket => { type => 'string' }, | |
735 | cert => { type => 'string' }, | |
736 | port => { type => 'integer' }, | |
737 | upid => { type => 'string' }, | |
738 | }, | |
739 | }, | |
740 | code => sub { | |
741 | my ($param) = @_; | |
742 | ||
743 | my $rpcenv = PVE::RPCEnvironment::get(); | |
744 | ||
745 | my $authuser = $rpcenv->get_user(); | |
746 | ||
747 | my $vmid = $param->{vmid}; | |
748 | my $node = $param->{node}; | |
749 | ||
750 | my $authpath = "/vms/$vmid"; | |
751 | ||
752 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
753 | ||
754 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
755 | if !$sslcert; | |
756 | ||
ec2c57eb | 757 | my ($remip, $family); |
5b4657d0 | 758 | |
fff3a342 | 759 | if ($node ne PVE::INotify::nodename()) { |
85ae6211 | 760 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); |
ec2c57eb WB |
761 | } else { |
762 | $family = PVE::Tools::get_host_address_family($node); | |
fff3a342 DM |
763 | } |
764 | ||
ec2c57eb WB |
765 | my $port = PVE::Tools::next_vnc_port($family); |
766 | ||
fff3a342 DM |
767 | # NOTE: vncterm VNC traffic is already TLS encrypted, |
768 | # so we select the fastest chipher here (or 'none'?) | |
5b4657d0 | 769 | my $remcmd = $remip ? |
806eae70 | 770 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : []; |
fff3a342 | 771 | |
67afe46e | 772 | my $conf = PVE::LXC::Config->load_config($vmid, $node); |
65213b67 | 773 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
aca816ad | 774 | |
5b4657d0 DM |
775 | my $shcmd = [ '/usr/bin/dtach', '-A', |
776 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 777 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
778 | |
779 | my $realcmd = sub { | |
780 | my $upid = shift; | |
781 | ||
5b4657d0 | 782 | syslog ('info', "starting lxc vnc proxy $upid\n"); |
fff3a342 | 783 | |
5b4657d0 | 784 | my $timeout = 10; |
fff3a342 DM |
785 | |
786 | my $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
5b4657d0 | 787 | '-timeout', $timeout, '-authpath', $authpath, |
fff3a342 DM |
788 | '-perm', 'VM.Console']; |
789 | ||
bd0f4d6d DC |
790 | if ($param->{width}) { |
791 | push @$cmd, '-width', $param->{width}; | |
792 | } | |
793 | ||
794 | if ($param->{height}) { | |
795 | push @$cmd, '-height', $param->{height}; | |
796 | } | |
797 | ||
fff3a342 | 798 | if ($param->{websocket}) { |
5b4657d0 | 799 | $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm |
fff3a342 DM |
800 | push @$cmd, '-notls', '-listen', 'localhost'; |
801 | } | |
802 | ||
803 | push @$cmd, '-c', @$remcmd, @$shcmd; | |
804 | ||
37634d3d | 805 | run_command($cmd, keeplocale => 1); |
fff3a342 DM |
806 | |
807 | return; | |
808 | }; | |
809 | ||
810 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
811 | ||
812 | PVE::Tools::wait_for_vnc_port($port); | |
813 | ||
814 | return { | |
815 | user => $authuser, | |
816 | ticket => $ticket, | |
5b4657d0 DM |
817 | port => $port, |
818 | upid => $upid, | |
819 | cert => $sslcert, | |
fff3a342 DM |
820 | }; |
821 | }}); | |
822 | ||
a0226a00 DC |
823 | __PACKAGE__->register_method ({ |
824 | name => 'termproxy', | |
825 | path => '{vmid}/termproxy', | |
826 | method => 'POST', | |
827 | protected => 1, | |
828 | permissions => { | |
829 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
830 | }, | |
831 | description => "Creates a TCP proxy connection.", | |
832 | parameters => { | |
833 | additionalProperties => 0, | |
834 | properties => { | |
835 | node => get_standard_option('pve-node'), | |
836 | vmid => get_standard_option('pve-vmid'), | |
837 | }, | |
838 | }, | |
839 | returns => { | |
840 | additionalProperties => 0, | |
841 | properties => { | |
842 | user => { type => 'string' }, | |
843 | ticket => { type => 'string' }, | |
844 | port => { type => 'integer' }, | |
845 | upid => { type => 'string' }, | |
846 | }, | |
847 | }, | |
848 | code => sub { | |
849 | my ($param) = @_; | |
850 | ||
851 | my $rpcenv = PVE::RPCEnvironment::get(); | |
852 | ||
853 | my $authuser = $rpcenv->get_user(); | |
854 | ||
855 | my $vmid = $param->{vmid}; | |
856 | my $node = $param->{node}; | |
857 | ||
858 | my $authpath = "/vms/$vmid"; | |
859 | ||
860 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
861 | ||
862 | my ($remip, $family); | |
863 | ||
864 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
865 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
866 | } else { | |
867 | $family = PVE::Tools::get_host_address_family($node); | |
868 | } | |
869 | ||
870 | my $port = PVE::Tools::next_vnc_port($family); | |
871 | ||
872 | my $remcmd = $remip ? | |
873 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : []; | |
874 | ||
875 | my $conf = PVE::LXC::Config->load_config($vmid, $node); | |
65213b67 | 876 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
a0226a00 DC |
877 | |
878 | my $shcmd = [ '/usr/bin/dtach', '-A', | |
879 | "/var/run/dtach/vzctlconsole$vmid", | |
880 | '-r', 'winch', '-z', @$concmd]; | |
881 | ||
882 | my $realcmd = sub { | |
883 | my $upid = shift; | |
884 | ||
885 | syslog ('info', "starting lxc termproxy $upid\n"); | |
886 | ||
887 | my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, | |
888 | '--perm', 'VM.Console', '--']; | |
889 | push @$cmd, @$remcmd, @$shcmd; | |
890 | ||
891 | PVE::Tools::run_command($cmd); | |
892 | }; | |
893 | ||
894 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); | |
895 | ||
896 | PVE::Tools::wait_for_vnc_port($port); | |
897 | ||
898 | return { | |
899 | user => $authuser, | |
900 | ticket => $ticket, | |
901 | port => $port, | |
902 | upid => $upid, | |
903 | }; | |
904 | }}); | |
905 | ||
fff3a342 DM |
906 | __PACKAGE__->register_method({ |
907 | name => 'vncwebsocket', | |
908 | path => '{vmid}/vncwebsocket', | |
909 | method => 'GET', | |
5b4657d0 | 910 | permissions => { |
fff3a342 DM |
911 | description => "You also need to pass a valid ticket (vncticket).", |
912 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
913 | }, | |
914 | description => "Opens a weksocket for VNC traffic.", | |
915 | parameters => { | |
916 | additionalProperties => 0, | |
917 | properties => { | |
918 | node => get_standard_option('pve-node'), | |
919 | vmid => get_standard_option('pve-vmid'), | |
920 | vncticket => { | |
921 | description => "Ticket from previous call to vncproxy.", | |
922 | type => 'string', | |
923 | maxLength => 512, | |
924 | }, | |
925 | port => { | |
926 | description => "Port number returned by previous vncproxy call.", | |
927 | type => 'integer', | |
928 | minimum => 5900, | |
929 | maximum => 5999, | |
930 | }, | |
931 | }, | |
932 | }, | |
933 | returns => { | |
934 | type => "object", | |
935 | properties => { | |
936 | port => { type => 'string' }, | |
937 | }, | |
938 | }, | |
939 | code => sub { | |
940 | my ($param) = @_; | |
941 | ||
942 | my $rpcenv = PVE::RPCEnvironment::get(); | |
943 | ||
944 | my $authuser = $rpcenv->get_user(); | |
945 | ||
946 | my $authpath = "/vms/$param->{vmid}"; | |
947 | ||
948 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
949 | ||
950 | my $port = $param->{port}; | |
5b4657d0 | 951 | |
fff3a342 DM |
952 | return { port => $port }; |
953 | }}); | |
954 | ||
955 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
956 | name => 'spiceproxy', |
957 | path => '{vmid}/spiceproxy', | |
fff3a342 DM |
958 | method => 'POST', |
959 | protected => 1, | |
960 | proxyto => 'node', | |
961 | permissions => { | |
962 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
963 | }, | |
964 | description => "Returns a SPICE configuration to connect to the CT.", | |
965 | parameters => { | |
966 | additionalProperties => 0, | |
967 | properties => { | |
968 | node => get_standard_option('pve-node'), | |
969 | vmid => get_standard_option('pve-vmid'), | |
970 | proxy => get_standard_option('spice-proxy', { optional => 1 }), | |
971 | }, | |
972 | }, | |
973 | returns => get_standard_option('remote-viewer-config'), | |
974 | code => sub { | |
975 | my ($param) = @_; | |
976 | ||
977 | my $vmid = $param->{vmid}; | |
978 | my $node = $param->{node}; | |
979 | my $proxy = $param->{proxy}; | |
980 | ||
981 | my $authpath = "/vms/$vmid"; | |
982 | my $permissions = 'VM.Console'; | |
983 | ||
67afe46e | 984 | my $conf = PVE::LXC::Config->load_config($vmid); |
da4db334 TL |
985 | |
986 | die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid); | |
987 | ||
aca816ad DM |
988 | my $concmd = PVE::LXC::get_console_command($vmid, $conf); |
989 | ||
5b4657d0 DM |
990 | my $shcmd = ['/usr/bin/dtach', '-A', |
991 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 992 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
993 | |
994 | my $title = "CT $vmid"; | |
995 | ||
996 | return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd); | |
997 | }}); | |
5c752bbf | 998 | |
5c752bbf DM |
999 | |
1000 | __PACKAGE__->register_method({ | |
52389a07 DM |
1001 | name => 'migrate_vm', |
1002 | path => '{vmid}/migrate', | |
5c752bbf DM |
1003 | method => 'POST', |
1004 | protected => 1, | |
1005 | proxyto => 'node', | |
52389a07 | 1006 | description => "Migrate the container to another node. Creates a new migration task.", |
5c752bbf | 1007 | permissions => { |
52389a07 | 1008 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], |
5c752bbf DM |
1009 | }, |
1010 | parameters => { | |
1011 | additionalProperties => 0, | |
1012 | properties => { | |
1013 | node => get_standard_option('pve-node'), | |
68e8f3c5 DM |
1014 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
1015 | target => get_standard_option('pve-node', { | |
1016 | description => "Target node.", | |
39bb88df | 1017 | completion => \&PVE::Cluster::complete_migration_target, |
68e8f3c5 | 1018 | }), |
52389a07 DM |
1019 | online => { |
1020 | type => 'boolean', | |
1021 | description => "Use online/live migration.", | |
1022 | optional => 1, | |
1023 | }, | |
00d8cdc0 DC |
1024 | restart => { |
1025 | type => 'boolean', | |
1026 | description => "Use restart migration", | |
1027 | optional => 1, | |
1028 | }, | |
1029 | timeout => { | |
1030 | type => 'integer', | |
1031 | description => "Timeout in seconds for shutdown for restart migration", | |
1032 | optional => 1, | |
1033 | default => 180, | |
1034 | }, | |
9746c095 FG |
1035 | force => { |
1036 | type => 'boolean', | |
1037 | description => "Force migration despite local bind / device" . | |
552e168f | 1038 | " mounts. NOTE: deprecated, use 'shared' property of mount point instead.", |
9746c095 FG |
1039 | optional => 1, |
1040 | }, | |
8ead60ef SI |
1041 | bwlimit => { |
1042 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1043 | optional => 1, | |
1044 | type => 'number', | |
1045 | minimum => '0', | |
41e9b65c | 1046 | default => 'migrate limit from datacenter or storage config', |
8ead60ef | 1047 | }, |
5c752bbf DM |
1048 | }, |
1049 | }, | |
1050 | returns => { | |
1051 | type => 'string', | |
52389a07 | 1052 | description => "the task ID.", |
5c752bbf DM |
1053 | }, |
1054 | code => sub { | |
1055 | my ($param) = @_; | |
1056 | ||
1057 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1058 | ||
1059 | my $authuser = $rpcenv->get_user(); | |
1060 | ||
52389a07 | 1061 | my $target = extract_param($param, 'target'); |
bb1ac2de | 1062 | |
52389a07 DM |
1063 | my $localnode = PVE::INotify::nodename(); |
1064 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
bb1ac2de | 1065 | |
52389a07 | 1066 | PVE::Cluster::check_cfs_quorum(); |
5c752bbf | 1067 | |
52389a07 | 1068 | PVE::Cluster::check_node_exists($target); |
27916659 | 1069 | |
52389a07 | 1070 | my $targetip = PVE::Cluster::remote_node_ip($target); |
5c752bbf | 1071 | |
52389a07 | 1072 | my $vmid = extract_param($param, 'vmid'); |
5c752bbf | 1073 | |
52389a07 | 1074 | # test if VM exists |
67afe46e | 1075 | PVE::LXC::Config->load_config($vmid); |
5c752bbf | 1076 | |
52389a07 DM |
1077 | # try to detect errors early |
1078 | if (PVE::LXC::check_running($vmid)) { | |
00d8cdc0 DC |
1079 | die "can't migrate running container without --online or --restart\n" |
1080 | if !$param->{online} && !$param->{restart}; | |
5c752bbf | 1081 | } |
5c752bbf DM |
1082 | |
1083 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
1084 | ||
1085 | my $hacmd = sub { | |
1086 | my $upid = shift; | |
1087 | ||
1088 | my $service = "ct:$vmid"; | |
1089 | ||
52389a07 | 1090 | my $cmd = ['ha-manager', 'migrate', $service, $target]; |
5c752bbf | 1091 | |
545dd4e1 | 1092 | print "Requesting HA migration for CT $vmid to node $target\n"; |
5c752bbf DM |
1093 | |
1094 | PVE::Tools::run_command($cmd); | |
1095 | ||
1096 | return; | |
1097 | }; | |
1098 | ||
52389a07 | 1099 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); |
5c752bbf DM |
1100 | |
1101 | } else { | |
1102 | ||
22557519 DM |
1103 | my $realcmd = sub { |
1104 | PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param); | |
1105 | }; | |
56462053 | 1106 | |
22557519 DM |
1107 | my $worker = sub { |
1108 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
5c752bbf DM |
1109 | }; |
1110 | ||
22557519 | 1111 | return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker); |
5c752bbf DM |
1112 | } |
1113 | }}); | |
1114 | ||
cc5392c8 WL |
1115 | __PACKAGE__->register_method({ |
1116 | name => 'vm_feature', | |
1117 | path => '{vmid}/feature', | |
1118 | method => 'GET', | |
1119 | proxyto => 'node', | |
1120 | protected => 1, | |
1121 | description => "Check if feature for virtual machine is available.", | |
1122 | permissions => { | |
1123 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1124 | }, | |
1125 | parameters => { | |
1126 | additionalProperties => 0, | |
1127 | properties => { | |
1128 | node => get_standard_option('pve-node'), | |
1129 | vmid => get_standard_option('pve-vmid'), | |
1130 | feature => { | |
1131 | description => "Feature to check.", | |
1132 | type => 'string', | |
d53e5c58 | 1133 | enum => [ 'snapshot', 'clone', 'copy' ], |
cc5392c8 | 1134 | }, |
5ec3949c | 1135 | snapname => get_standard_option('pve-snapshot-name', { |
cc5392c8 WL |
1136 | optional => 1, |
1137 | }), | |
1138 | }, | |
1139 | }, | |
1140 | returns => { | |
1141 | type => "object", | |
1142 | properties => { | |
1143 | hasFeature => { type => 'boolean' }, | |
1144 | #nodes => { | |
1145 | #type => 'array', | |
1146 | #items => { type => 'string' }, | |
1147 | #} | |
1148 | }, | |
1149 | }, | |
1150 | code => sub { | |
1151 | my ($param) = @_; | |
1152 | ||
1153 | my $node = extract_param($param, 'node'); | |
1154 | ||
1155 | my $vmid = extract_param($param, 'vmid'); | |
1156 | ||
1157 | my $snapname = extract_param($param, 'snapname'); | |
1158 | ||
1159 | my $feature = extract_param($param, 'feature'); | |
1160 | ||
67afe46e | 1161 | my $conf = PVE::LXC::Config->load_config($vmid); |
cc5392c8 WL |
1162 | |
1163 | if($snapname){ | |
1164 | my $snap = $conf->{snapshots}->{$snapname}; | |
1165 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
1166 | $conf = $snap; | |
1167 | } | |
ef241384 | 1168 | my $storage_cfg = PVE::Storage::config(); |
cc5392c8 | 1169 | #Maybe include later |
ef241384 | 1170 | #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg); |
4518000b | 1171 | my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname); |
cc5392c8 WL |
1172 | |
1173 | return { | |
1174 | hasFeature => $hasFeature, | |
1175 | #nodes => [ keys %$nodelist ], | |
1176 | }; | |
1177 | }}); | |
bb1ac2de DM |
1178 | |
1179 | __PACKAGE__->register_method({ | |
1180 | name => 'template', | |
1181 | path => '{vmid}/template', | |
1182 | method => 'POST', | |
1183 | protected => 1, | |
1184 | proxyto => 'node', | |
1185 | description => "Create a Template.", | |
1186 | permissions => { | |
1187 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
1188 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1189 | }, | |
1190 | parameters => { | |
1191 | additionalProperties => 0, | |
1192 | properties => { | |
1193 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 1194 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
bb1ac2de DM |
1195 | }, |
1196 | }, | |
1197 | returns => { type => 'null'}, | |
1198 | code => sub { | |
1199 | my ($param) = @_; | |
1200 | ||
1201 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1202 | ||
1203 | my $authuser = $rpcenv->get_user(); | |
1204 | ||
1205 | my $node = extract_param($param, 'node'); | |
1206 | ||
1207 | my $vmid = extract_param($param, 'vmid'); | |
1208 | ||
1209 | my $updatefn = sub { | |
1210 | ||
67afe46e FG |
1211 | my $conf = PVE::LXC::Config->load_config($vmid); |
1212 | PVE::LXC::Config->check_lock($conf); | |
bb1ac2de DM |
1213 | |
1214 | die "unable to create template, because CT contains snapshots\n" | |
1215 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
1216 | ||
1217 | die "you can't convert a template to a template\n" | |
67afe46e | 1218 | if PVE::LXC::Config->is_template($conf); |
bb1ac2de DM |
1219 | |
1220 | die "you can't convert a CT to template if the CT is running\n" | |
1221 | if PVE::LXC::check_running($vmid); | |
1222 | ||
1223 | my $realcmd = sub { | |
1224 | PVE::LXC::template_create($vmid, $conf); | |
bb1ac2de | 1225 | |
c2182c49 | 1226 | $conf->{template} = 1; |
bb1ac2de | 1227 | |
c2182c49 WL |
1228 | PVE::LXC::Config->write_config($vmid, $conf); |
1229 | # and remove lxc config | |
1230 | PVE::LXC::update_lxc_config($vmid, $conf); | |
1231 | }; | |
bb1ac2de DM |
1232 | |
1233 | return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd); | |
1234 | }; | |
1235 | ||
67afe46e | 1236 | PVE::LXC::Config->lock_config($vmid, $updatefn); |
bb1ac2de DM |
1237 | |
1238 | return undef; | |
1239 | }}); | |
1240 | ||
c4a33727 DM |
1241 | __PACKAGE__->register_method({ |
1242 | name => 'clone_vm', | |
1243 | path => '{vmid}/clone', | |
1244 | method => 'POST', | |
1245 | protected => 1, | |
1246 | proxyto => 'node', | |
1247 | description => "Create a container clone/copy", | |
1248 | permissions => { | |
1249 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, " . | |
1250 | "and 'VM.Allocate' permissions " . | |
1251 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
1252 | "'Datastore.AllocateSpace' on any used storage.", | |
1253 | check => | |
1254 | [ 'and', | |
1255 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
1256 | [ 'or', | |
1257 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
1258 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
1259 | ], | |
1260 | ] | |
1261 | }, | |
1262 | parameters => { | |
1263 | additionalProperties => 0, | |
1264 | properties => { | |
1265 | node => get_standard_option('pve-node'), | |
1266 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1267 | newid => get_standard_option('pve-vmid', { | |
1268 | completion => \&PVE::Cluster::complete_next_vmid, | |
1269 | description => 'VMID for the clone.' }), | |
1270 | hostname => { | |
1271 | optional => 1, | |
1272 | type => 'string', format => 'dns-name', | |
1273 | description => "Set a hostname for the new CT.", | |
1274 | }, | |
1275 | description => { | |
1276 | optional => 1, | |
1277 | type => 'string', | |
1278 | description => "Description for the new CT.", | |
1279 | }, | |
1280 | pool => { | |
1281 | optional => 1, | |
1282 | type => 'string', format => 'pve-poolid', | |
1283 | description => "Add the new CT to the specified pool.", | |
1284 | }, | |
5ec3949c | 1285 | snapname => get_standard_option('pve-snapshot-name', { |
c4a33727 DM |
1286 | optional => 1, |
1287 | }), | |
1288 | storage => get_standard_option('pve-storage-id', { | |
1289 | description => "Target storage for full clone.", | |
c4a33727 DM |
1290 | optional => 1, |
1291 | }), | |
1292 | full => { | |
1293 | optional => 1, | |
1294 | type => 'boolean', | |
c4b4cb83 | 1295 | description => "Create a full copy of all disks. This is always done when " . |
c4a33727 | 1296 | "you clone a normal CT. For CT templates, we try to create a linked clone by default.", |
c4a33727 | 1297 | }, |
411ae12c DM |
1298 | target => get_standard_option('pve-node', { |
1299 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
1300 | optional => 1, | |
1301 | }), | |
8ead60ef SI |
1302 | bwlimit => { |
1303 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1304 | optional => 1, | |
1305 | type => 'number', | |
1306 | minimum => '0', | |
41e9b65c | 1307 | default => 'clone limit from datacenter or storage config', |
8ead60ef | 1308 | }, |
c4a33727 DM |
1309 | }, |
1310 | }, | |
1311 | returns => { | |
1312 | type => 'string', | |
1313 | }, | |
1314 | code => sub { | |
1315 | my ($param) = @_; | |
1316 | ||
1317 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1318 | ||
1319 | my $authuser = $rpcenv->get_user(); | |
1320 | ||
1321 | my $node = extract_param($param, 'node'); | |
1322 | ||
1323 | my $vmid = extract_param($param, 'vmid'); | |
1324 | ||
1325 | my $newid = extract_param($param, 'newid'); | |
1326 | ||
1327 | my $pool = extract_param($param, 'pool'); | |
1328 | ||
1329 | if (defined($pool)) { | |
1330 | $rpcenv->check_pool_exist($pool); | |
1331 | } | |
1332 | ||
1333 | my $snapname = extract_param($param, 'snapname'); | |
1334 | ||
1335 | my $storage = extract_param($param, 'storage'); | |
1336 | ||
411ae12c DM |
1337 | my $target = extract_param($param, 'target'); |
1338 | ||
c4a33727 DM |
1339 | my $localnode = PVE::INotify::nodename(); |
1340 | ||
411ae12c DM |
1341 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
1342 | ||
1343 | PVE::Cluster::check_node_exists($target) if $target; | |
1344 | ||
c4a33727 DM |
1345 | my $storecfg = PVE::Storage::config(); |
1346 | ||
1347 | if ($storage) { | |
1348 | # check if storage is enabled on local node | |
1349 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
411ae12c DM |
1350 | if ($target) { |
1351 | # check if storage is available on target node | |
1352 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
1353 | # clone only works if target storage is shared | |
1354 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1355 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
1356 | } | |
c4a33727 DM |
1357 | } |
1358 | ||
1359 | PVE::Cluster::check_cfs_quorum(); | |
1360 | ||
db01d674 WB |
1361 | my $conffile; |
1362 | my $newconf = {}; | |
1363 | my $mountpoints = {}; | |
1364 | my $fullclone = {}; | |
1365 | my $vollist = []; | |
411ae12c | 1366 | my $running; |
c4a33727 | 1367 | |
db01d674 WB |
1368 | PVE::LXC::Config->lock_config($vmid, sub { |
1369 | my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk'); | |
e7d553c7 | 1370 | |
411ae12c DM |
1371 | $running = PVE::LXC::check_running($vmid) || 0; |
1372 | ||
e7d553c7 DM |
1373 | my $full = extract_param($param, 'full'); |
1374 | if (!defined($full)) { | |
1375 | $full = !PVE::LXC::Config->is_template($src_conf); | |
1376 | } | |
1377 | die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full; | |
1378 | ||
db01d674 WB |
1379 | eval { |
1380 | die "snapshot '$snapname' does not exist\n" | |
1381 | if $snapname && !defined($src_conf->{snapshots}->{$snapname}); | |
c4a33727 | 1382 | |
c4a33727 | 1383 | |
db01d674 | 1384 | my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf; |
c4a33727 | 1385 | |
db01d674 WB |
1386 | $conffile = PVE::LXC::Config->config_file($newid); |
1387 | die "unable to create CT $newid: config file already exists\n" | |
1388 | if -f $conffile; | |
c4a33727 | 1389 | |
24f9d440 | 1390 | my $sharedvm = 1; |
db01d674 WB |
1391 | foreach my $opt (keys %$src_conf) { |
1392 | next if $opt =~ m/^unused\d+$/; | |
c4a33727 | 1393 | |
db01d674 | 1394 | my $value = $src_conf->{$opt}; |
c4a33727 | 1395 | |
db01d674 | 1396 | if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) { |
e4034859 | 1397 | my $mp = PVE::LXC::Config->parse_volume($opt, $value); |
c4a33727 | 1398 | |
db01d674 WB |
1399 | if ($mp->{type} eq 'volume') { |
1400 | my $volid = $mp->{volume}; | |
09687674 DM |
1401 | |
1402 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1403 | $sid = $storage if defined($storage); | |
1404 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
24f9d440 WB |
1405 | if (!$scfg->{shared}) { |
1406 | $sharedvm = 0; | |
1407 | warn "found non-shared volume: $volid\n" if $target; | |
1408 | } | |
09687674 DM |
1409 | |
1410 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
1411 | ||
e7d553c7 | 1412 | if ($full) { |
db01d674 WB |
1413 | die "Cannot do full clones on a running container without snapshots\n" |
1414 | if $running && !defined($snapname); | |
1415 | $fullclone->{$opt} = 1; | |
1416 | } else { | |
1417 | # not full means clone instead of copy | |
1418 | die "Linked clone feature for '$volid' is not available\n" | |
4a954457 | 1419 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running, {'valid_target_formats' => ['raw', 'subvol']}); |
db01d674 | 1420 | } |
c4a33727 | 1421 | |
db01d674 WB |
1422 | $mountpoints->{$opt} = $mp; |
1423 | push @$vollist, $volid; | |
c4a33727 | 1424 | |
c4a33727 | 1425 | } else { |
db01d674 WB |
1426 | # TODO: allow bind mounts? |
1427 | die "unable to clone mountpint '$opt' (type $mp->{type})\n"; | |
c4a33727 | 1428 | } |
63231f52 TL |
1429 | } elsif ($opt =~ m/^net(\d+)$/) { |
1430 | # always change MAC! address | |
1431 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1432 | my $net = PVE::LXC::Config->parse_lxc_network($value); | |
1433 | $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
1434 | $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net); | |
c4a33727 | 1435 | } else { |
db01d674 WB |
1436 | # copy everything else |
1437 | $newconf->{$opt} = $value; | |
c4a33727 | 1438 | } |
db01d674 | 1439 | } |
24f9d440 WB |
1440 | die "can't clone CT to node '$target' (CT uses local storage)\n" |
1441 | if $target && !$sharedvm; | |
c4a33727 | 1442 | |
db01d674 WB |
1443 | # Replace the 'disk' lock with a 'create' lock. |
1444 | $newconf->{lock} = 'create'; | |
1445 | ||
037dc1e2 | 1446 | delete $newconf->{pending}; |
db01d674 WB |
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.", | |
5e5d76cf | 1574 | enum => [PVE::LXC::Config->valid_volume_keys()], |
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}; | |
e4034859 | 1627 | my $mp = PVE::LXC::Config->parse_volume($disk, $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 | ||
c14508b1 | 1789 | $mpdata = PVE::LXC::Config->parse_volume($mpkey, $conf->{$mpkey}); |
3c0f6806 WB |
1790 | $old_volid = $mpdata->{volume}; |
1791 | ||
1792 | die "you can't move a volume with snapshots and delete the source\n" | |
1793 | if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid); | |
1794 | ||
1795 | PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest}); | |
1796 | ||
1797 | PVE::LXC::Config->set_lock($vmid, $lockname); | |
1798 | }); | |
1799 | ||
1800 | my $realcmd = sub { | |
1801 | eval { | |
1802 | PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage"); | |
1803 | ||
1804 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1805 | my $storage_cfg = PVE::Storage::config(); | |
1806 | ||
1807 | my $new_volid; | |
1808 | ||
1809 | eval { | |
1810 | PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]); | |
8ead60ef SI |
1811 | my $bwlimit = extract_param($param, 'bwlimit'); |
1812 | my $source_storage = PVE::Storage::parse_volume_id($old_volid); | |
1813 | my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$source_storage, $storage], $bwlimit); | |
1814 | $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf, undef, $movelimit); | |
3bfbd900 FE |
1815 | if (PVE::LXC::Config->is_template($conf)) { |
1816 | PVE::Storage::activate_volumes($storage_cfg, [ $new_volid ]); | |
1817 | my $template_volid = PVE::Storage::vdisk_create_base($storage_cfg, $new_volid); | |
1818 | $mpdata->{volume} = $template_volid; | |
1819 | } else { | |
1820 | $mpdata->{volume} = $new_volid; | |
1821 | } | |
3c0f6806 WB |
1822 | |
1823 | PVE::LXC::Config->lock_config($vmid, sub { | |
1824 | my $digest = $conf->{digest}; | |
1825 | $conf = PVE::LXC::Config->load_config($vmid); | |
1826 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1827 | ||
1828 | $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs'); | |
1829 | ||
1830 | PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete}; | |
1831 | ||
1832 | PVE::LXC::Config->write_config($vmid, $conf); | |
1833 | }); | |
1834 | ||
1835 | eval { | |
1836 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
1837 | PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ]) | |
1838 | }; | |
1839 | warn $@ if $@; | |
1840 | }; | |
1841 | if (my $err = $@) { | |
1842 | eval { | |
1843 | PVE::Storage::vdisk_free($storage_cfg, $new_volid) | |
1844 | if defined($new_volid); | |
1845 | }; | |
1846 | warn $@ if $@; | |
1847 | die $err; | |
1848 | } | |
1849 | ||
1850 | if ($param->{delete}) { | |
1851 | eval { | |
1852 | PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]); | |
1853 | PVE::Storage::vdisk_free($storage_cfg, $old_volid); | |
1854 | }; | |
9abb0f8a FE |
1855 | if (my $err = $@) { |
1856 | warn $err; | |
1857 | PVE::LXC::Config->lock_config($vmid, sub { | |
1858 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1859 | PVE::LXC::Config->add_unused_volume($conf, $old_volid); | |
1860 | PVE::LXC::Config->write_config($vmid, $conf); | |
1861 | }); | |
1862 | } | |
3c0f6806 WB |
1863 | } |
1864 | }; | |
1865 | my $err = $@; | |
1866 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1867 | warn $@ if $@; | |
1868 | die $err if $err; | |
1869 | }; | |
1870 | my $task = eval { | |
1871 | $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd); | |
1872 | }; | |
1873 | if (my $err = $@) { | |
1874 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1875 | warn $@ if $@; | |
1876 | die $err; | |
1877 | } | |
1878 | return $task; | |
1879 | }}); | |
1880 | ||
37c2dba8 OB |
1881 | __PACKAGE__->register_method({ |
1882 | name => 'vm_pending', | |
1883 | path => '{vmid}/pending', | |
1884 | method => 'GET', | |
1885 | proxyto => 'node', | |
1886 | description => 'Get container configuration, including pending changes.', | |
1887 | permissions => { | |
1888 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1889 | }, | |
1890 | parameters => { | |
1891 | additionalProperties => 0, | |
1892 | properties => { | |
1893 | node => get_standard_option('pve-node'), | |
1894 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1895 | }, | |
1896 | }, | |
1897 | returns => { | |
1898 | type => "array", | |
1899 | items => { | |
1900 | type => "object", | |
1901 | properties => { | |
1902 | key => { | |
1903 | description => 'Configuration option name.', | |
1904 | type => 'string', | |
1905 | }, | |
1906 | value => { | |
1907 | description => 'Current value.', | |
1908 | type => 'string', | |
1909 | optional => 1, | |
1910 | }, | |
1911 | pending => { | |
1912 | description => 'Pending value.', | |
1913 | type => 'string', | |
1914 | optional => 1, | |
1915 | }, | |
1916 | delete => { | |
1917 | description => "Indicates a pending delete request if present and not 0.", | |
1918 | type => 'integer', | |
1919 | minimum => 0, | |
1920 | maximum => 2, | |
1921 | optional => 1, | |
1922 | }, | |
1923 | }, | |
1924 | }, | |
1925 | }, | |
1926 | code => sub { | |
1927 | my ($param) = @_; | |
1928 | ||
1929 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); | |
1930 | ||
1931 | my $pending_delete_hash = PVE::LXC::Config->parse_pending_delete($conf->{pending}->{delete}); | |
1932 | ||
f622e7dc | 1933 | return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash); |
37c2dba8 OB |
1934 | }}); |
1935 | ||
f76a2828 | 1936 | 1; |