]>
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"; | |
443 | if (my $err = check_storage_supports_templates($conf)) { | |
444 | warn $err; | |
445 | warn "Leave restored backup as container instead of converting to template.\n" | |
446 | } else { | |
447 | PVE::LXC::template_create($vmid, $conf); | |
448 | $conf->{template} = 1; | |
449 | } | |
450 | } | |
67afe46e | 451 | PVE::LXC::Config->write_config($vmid, $conf); |
27916659 DM |
452 | }; |
453 | if (my $err = $@) { | |
6c871c36 | 454 | PVE::LXC::destroy_disks($storage_cfg, $vollist); |
4eeb9af1 | 455 | eval { PVE::LXC::Config->destroy_config($vmid) }; |
d7d48be6 | 456 | warn $@ if $@; |
61783321 | 457 | die "$emsg $err"; |
6d098bf4 | 458 | } |
87273b2b | 459 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
9d0225fb TL |
460 | |
461 | PVE::API2::LXC::Status->vm_start({ vmid => $vmid, node => $node }) | |
462 | if $start_after_create; | |
9c2d4ce9 | 463 | }; |
5c752bbf | 464 | |
8a6fc617 | 465 | my $workername = $restore ? 'vzrestore' : 'vzcreate'; |
67afe46e | 466 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
9c2d4ce9 | 467 | |
8a6fc617 | 468 | return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd); |
9c2d4ce9 DM |
469 | }}); |
470 | ||
a2d3e5c3 CE |
471 | sub check_storage_supports_templates { |
472 | my ($conf) = @_; | |
473 | ||
474 | my $scfg = PVE::Storage::config(); | |
475 | eval { | |
476 | PVE::LXC::Config->foreach_mountpoint($conf, sub { | |
477 | my ($ms, $mp) = @_; | |
478 | ||
479 | my ($sid) = PVE::Storage::parse_volume_id($mp->{volume}, 0); | |
480 | die "Warning: Directory storage '$sid' does not support container templates!\n" | |
481 | if $scfg->{ids}->{$sid}->{path}; | |
482 | }); | |
483 | }; | |
484 | return $@ | |
485 | } | |
486 | ||
f76a2828 DM |
487 | __PACKAGE__->register_method({ |
488 | name => 'vmdiridx', | |
5c752bbf | 489 | path => '{vmid}', |
f76a2828 DM |
490 | method => 'GET', |
491 | proxyto => 'node', | |
492 | description => "Directory index", | |
493 | permissions => { | |
494 | user => 'all', | |
495 | }, | |
496 | parameters => { | |
497 | additionalProperties => 0, | |
498 | properties => { | |
499 | node => get_standard_option('pve-node'), | |
500 | vmid => get_standard_option('pve-vmid'), | |
501 | }, | |
502 | }, | |
503 | returns => { | |
504 | type => 'array', | |
505 | items => { | |
506 | type => "object", | |
507 | properties => { | |
508 | subdir => { type => 'string' }, | |
509 | }, | |
510 | }, | |
511 | links => [ { rel => 'child', href => "{subdir}" } ], | |
512 | }, | |
513 | code => sub { | |
514 | my ($param) = @_; | |
515 | ||
516 | # test if VM exists | |
67afe46e | 517 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); |
f76a2828 DM |
518 | |
519 | my $res = [ | |
520 | { subdir => 'config' }, | |
37c2dba8 | 521 | { subdir => 'pending' }, |
fff3a342 DM |
522 | { subdir => 'status' }, |
523 | { subdir => 'vncproxy' }, | |
a0226a00 | 524 | { subdir => 'termproxy' }, |
fff3a342 DM |
525 | { subdir => 'vncwebsocket' }, |
526 | { subdir => 'spiceproxy' }, | |
527 | { subdir => 'migrate' }, | |
c4a33727 | 528 | { subdir => 'clone' }, |
f76a2828 DM |
529 | # { subdir => 'initlog' }, |
530 | { subdir => 'rrd' }, | |
531 | { subdir => 'rrddata' }, | |
532 | { subdir => 'firewall' }, | |
cc5392c8 | 533 | { subdir => 'snapshot' }, |
985b18ed | 534 | { subdir => 'resize' }, |
f76a2828 | 535 | ]; |
5c752bbf | 536 | |
f76a2828 DM |
537 | return $res; |
538 | }}); | |
539 | ||
c4a33727 | 540 | |
f76a2828 | 541 | __PACKAGE__->register_method({ |
5c752bbf DM |
542 | name => 'rrd', |
543 | path => '{vmid}/rrd', | |
f76a2828 DM |
544 | method => 'GET', |
545 | protected => 1, # fixme: can we avoid that? | |
546 | permissions => { | |
547 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
548 | }, | |
549 | description => "Read VM RRD statistics (returns PNG)", | |
550 | parameters => { | |
551 | additionalProperties => 0, | |
552 | properties => { | |
553 | node => get_standard_option('pve-node'), | |
554 | vmid => get_standard_option('pve-vmid'), | |
555 | timeframe => { | |
556 | description => "Specify the time frame you are interested in.", | |
557 | type => 'string', | |
558 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
559 | }, | |
560 | ds => { | |
561 | description => "The list of datasources you want to display.", | |
562 | type => 'string', format => 'pve-configid-list', | |
563 | }, | |
564 | cf => { | |
565 | description => "The RRD consolidation function", | |
566 | type => 'string', | |
567 | enum => [ 'AVERAGE', 'MAX' ], | |
568 | optional => 1, | |
569 | }, | |
570 | }, | |
571 | }, | |
572 | returns => { | |
573 | type => "object", | |
574 | properties => { | |
575 | filename => { type => 'string' }, | |
576 | }, | |
577 | }, | |
578 | code => sub { | |
579 | my ($param) = @_; | |
580 | ||
d949527f | 581 | return PVE::RRD::create_rrd_graph( |
5c752bbf | 582 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
f76a2828 | 583 | $param->{ds}, $param->{cf}); |
5c752bbf | 584 | |
f76a2828 DM |
585 | }}); |
586 | ||
587 | __PACKAGE__->register_method({ | |
5c752bbf DM |
588 | name => 'rrddata', |
589 | path => '{vmid}/rrddata', | |
f76a2828 DM |
590 | method => 'GET', |
591 | protected => 1, # fixme: can we avoid that? | |
592 | permissions => { | |
593 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
594 | }, | |
595 | description => "Read VM RRD statistics", | |
596 | parameters => { | |
597 | additionalProperties => 0, | |
598 | properties => { | |
599 | node => get_standard_option('pve-node'), | |
600 | vmid => get_standard_option('pve-vmid'), | |
601 | timeframe => { | |
602 | description => "Specify the time frame you are interested in.", | |
603 | type => 'string', | |
604 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
605 | }, | |
606 | cf => { | |
607 | description => "The RRD consolidation function", | |
608 | type => 'string', | |
609 | enum => [ 'AVERAGE', 'MAX' ], | |
610 | optional => 1, | |
611 | }, | |
612 | }, | |
613 | }, | |
614 | returns => { | |
615 | type => "array", | |
616 | items => { | |
617 | type => "object", | |
618 | properties => {}, | |
619 | }, | |
620 | }, | |
621 | code => sub { | |
622 | my ($param) = @_; | |
623 | ||
d949527f | 624 | return PVE::RRD::create_rrd_data( |
f76a2828 DM |
625 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); |
626 | }}); | |
627 | ||
f76a2828 | 628 | __PACKAGE__->register_method({ |
5c752bbf DM |
629 | name => 'destroy_vm', |
630 | path => '{vmid}', | |
f76a2828 DM |
631 | method => 'DELETE', |
632 | protected => 1, | |
633 | proxyto => 'node', | |
634 | description => "Destroy the container (also delete all uses files).", | |
635 | permissions => { | |
636 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
637 | }, | |
638 | parameters => { | |
639 | additionalProperties => 0, | |
640 | properties => { | |
641 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 642 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
c5a09704 CE |
643 | purge => { |
644 | type => 'boolean', | |
645 | description => "Remove vmid from backup cron jobs.", | |
646 | optional => 1, | |
647 | }, | |
f76a2828 DM |
648 | }, |
649 | }, | |
5c752bbf | 650 | returns => { |
f76a2828 DM |
651 | type => 'string', |
652 | }, | |
653 | code => sub { | |
654 | my ($param) = @_; | |
655 | ||
656 | my $rpcenv = PVE::RPCEnvironment::get(); | |
f76a2828 | 657 | my $authuser = $rpcenv->get_user(); |
f76a2828 DM |
658 | my $vmid = $param->{vmid}; |
659 | ||
660 | # test if container exists | |
67afe46e | 661 | my $conf = PVE::LXC::Config->load_config($vmid); |
611fe3aa | 662 | my $storage_cfg = cfs_read_file("storage.cfg"); |
67afe46e | 663 | PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid"); |
7e806596 | 664 | |
9d87e069 | 665 | die "unable to remove CT $vmid - used in HA resources\n" |
b6e0b774 AG |
666 | if PVE::HA::Config::vm_is_ha_managed($vmid); |
667 | ||
c5a09704 CE |
668 | if (!$param->{purge}) { |
669 | # do not allow destroy if there are replication jobs without purge | |
670 | my $repl_conf = PVE::ReplicationConfig->new(); | |
671 | $repl_conf->check_for_existing_jobs($vmid); | |
672 | } | |
c5a8e04f | 673 | |
d607c17d DM |
674 | my $running_error_msg = "unable to destroy CT $vmid - container is running\n"; |
675 | ||
676 | die $running_error_msg if PVE::LXC::check_running($vmid); # check early | |
677 | ||
611fe3aa | 678 | my $code = sub { |
673cf209 | 679 | # reload config after lock |
67afe46e FG |
680 | $conf = PVE::LXC::Config->load_config($vmid); |
681 | PVE::LXC::Config->check_lock($conf); | |
673cf209 | 682 | |
d607c17d DM |
683 | die $running_error_msg if PVE::LXC::check_running($vmid); |
684 | ||
7fc1d9eb TL |
685 | PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf, { lock => 'destroyed' }); |
686 | ||
be5fc936 | 687 | PVE::AccessControl::remove_vm_access($vmid); |
2f9b5ead | 688 | PVE::Firewall::remove_vmfw_conf($vmid); |
c5a09704 CE |
689 | if ($param->{purge}) { |
690 | PVE::ReplicationConfig::remove_vmid_jobs($vmid); | |
691 | PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid); | |
692 | } | |
7fc1d9eb TL |
693 | |
694 | # only now remove the zombie config, else we can have reuse race | |
695 | PVE::LXC::Config->destroy_config($vmid); | |
f76a2828 DM |
696 | }; |
697 | ||
67afe46e | 698 | my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); }; |
351bc0c4 | 699 | |
f76a2828 DM |
700 | return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd); |
701 | }}); | |
702 | ||
fff3a342 DM |
703 | my $sslcert; |
704 | ||
705 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
706 | name => 'vncproxy', |
707 | path => '{vmid}/vncproxy', | |
fff3a342 DM |
708 | method => 'POST', |
709 | protected => 1, | |
710 | permissions => { | |
711 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
712 | }, | |
713 | description => "Creates a TCP VNC proxy connections.", | |
714 | parameters => { | |
715 | additionalProperties => 0, | |
716 | properties => { | |
717 | node => get_standard_option('pve-node'), | |
718 | vmid => get_standard_option('pve-vmid'), | |
719 | websocket => { | |
720 | optional => 1, | |
721 | type => 'boolean', | |
722 | description => "use websocket instead of standard VNC.", | |
723 | }, | |
bd0f4d6d DC |
724 | width => { |
725 | optional => 1, | |
726 | description => "sets the width of the console in pixels.", | |
727 | type => 'integer', | |
728 | minimum => 16, | |
729 | maximum => 4096, | |
730 | }, | |
731 | height => { | |
732 | optional => 1, | |
733 | description => "sets the height of the console in pixels.", | |
734 | type => 'integer', | |
735 | minimum => 16, | |
736 | maximum => 2160, | |
737 | }, | |
fff3a342 DM |
738 | }, |
739 | }, | |
5b4657d0 | 740 | returns => { |
fff3a342 DM |
741 | additionalProperties => 0, |
742 | properties => { | |
743 | user => { type => 'string' }, | |
744 | ticket => { type => 'string' }, | |
745 | cert => { type => 'string' }, | |
746 | port => { type => 'integer' }, | |
747 | upid => { type => 'string' }, | |
748 | }, | |
749 | }, | |
750 | code => sub { | |
751 | my ($param) = @_; | |
752 | ||
753 | my $rpcenv = PVE::RPCEnvironment::get(); | |
754 | ||
755 | my $authuser = $rpcenv->get_user(); | |
756 | ||
757 | my $vmid = $param->{vmid}; | |
758 | my $node = $param->{node}; | |
759 | ||
760 | my $authpath = "/vms/$vmid"; | |
761 | ||
762 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
763 | ||
764 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
765 | if !$sslcert; | |
766 | ||
ec2c57eb | 767 | my ($remip, $family); |
5b4657d0 | 768 | |
fff3a342 | 769 | if ($node ne PVE::INotify::nodename()) { |
85ae6211 | 770 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); |
ec2c57eb WB |
771 | } else { |
772 | $family = PVE::Tools::get_host_address_family($node); | |
fff3a342 DM |
773 | } |
774 | ||
ec2c57eb WB |
775 | my $port = PVE::Tools::next_vnc_port($family); |
776 | ||
fff3a342 DM |
777 | # NOTE: vncterm VNC traffic is already TLS encrypted, |
778 | # so we select the fastest chipher here (or 'none'?) | |
5b4657d0 | 779 | my $remcmd = $remip ? |
806eae70 | 780 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : []; |
fff3a342 | 781 | |
67afe46e | 782 | my $conf = PVE::LXC::Config->load_config($vmid, $node); |
65213b67 | 783 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
aca816ad | 784 | |
5b4657d0 DM |
785 | my $shcmd = [ '/usr/bin/dtach', '-A', |
786 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 787 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
788 | |
789 | my $realcmd = sub { | |
790 | my $upid = shift; | |
791 | ||
5b4657d0 | 792 | syslog ('info', "starting lxc vnc proxy $upid\n"); |
fff3a342 | 793 | |
5b4657d0 | 794 | my $timeout = 10; |
fff3a342 DM |
795 | |
796 | my $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
5b4657d0 | 797 | '-timeout', $timeout, '-authpath', $authpath, |
fff3a342 DM |
798 | '-perm', 'VM.Console']; |
799 | ||
bd0f4d6d DC |
800 | if ($param->{width}) { |
801 | push @$cmd, '-width', $param->{width}; | |
802 | } | |
803 | ||
804 | if ($param->{height}) { | |
805 | push @$cmd, '-height', $param->{height}; | |
806 | } | |
807 | ||
fff3a342 | 808 | if ($param->{websocket}) { |
5b4657d0 | 809 | $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm |
fff3a342 DM |
810 | push @$cmd, '-notls', '-listen', 'localhost'; |
811 | } | |
812 | ||
813 | push @$cmd, '-c', @$remcmd, @$shcmd; | |
814 | ||
37634d3d | 815 | run_command($cmd, keeplocale => 1); |
fff3a342 DM |
816 | |
817 | return; | |
818 | }; | |
819 | ||
820 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
821 | ||
822 | PVE::Tools::wait_for_vnc_port($port); | |
823 | ||
824 | return { | |
825 | user => $authuser, | |
826 | ticket => $ticket, | |
5b4657d0 DM |
827 | port => $port, |
828 | upid => $upid, | |
829 | cert => $sslcert, | |
fff3a342 DM |
830 | }; |
831 | }}); | |
832 | ||
a0226a00 DC |
833 | __PACKAGE__->register_method ({ |
834 | name => 'termproxy', | |
835 | path => '{vmid}/termproxy', | |
836 | method => 'POST', | |
837 | protected => 1, | |
838 | permissions => { | |
839 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
840 | }, | |
841 | description => "Creates a TCP proxy connection.", | |
842 | parameters => { | |
843 | additionalProperties => 0, | |
844 | properties => { | |
845 | node => get_standard_option('pve-node'), | |
846 | vmid => get_standard_option('pve-vmid'), | |
847 | }, | |
848 | }, | |
849 | returns => { | |
850 | additionalProperties => 0, | |
851 | properties => { | |
852 | user => { type => 'string' }, | |
853 | ticket => { type => 'string' }, | |
854 | port => { type => 'integer' }, | |
855 | upid => { type => 'string' }, | |
856 | }, | |
857 | }, | |
858 | code => sub { | |
859 | my ($param) = @_; | |
860 | ||
861 | my $rpcenv = PVE::RPCEnvironment::get(); | |
862 | ||
863 | my $authuser = $rpcenv->get_user(); | |
864 | ||
865 | my $vmid = $param->{vmid}; | |
866 | my $node = $param->{node}; | |
867 | ||
868 | my $authpath = "/vms/$vmid"; | |
869 | ||
870 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
871 | ||
872 | my ($remip, $family); | |
873 | ||
874 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
875 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
876 | } else { | |
877 | $family = PVE::Tools::get_host_address_family($node); | |
878 | } | |
879 | ||
880 | my $port = PVE::Tools::next_vnc_port($family); | |
881 | ||
882 | my $remcmd = $remip ? | |
883 | ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : []; | |
884 | ||
885 | my $conf = PVE::LXC::Config->load_config($vmid, $node); | |
65213b67 | 886 | my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1); |
a0226a00 DC |
887 | |
888 | my $shcmd = [ '/usr/bin/dtach', '-A', | |
889 | "/var/run/dtach/vzctlconsole$vmid", | |
890 | '-r', 'winch', '-z', @$concmd]; | |
891 | ||
892 | my $realcmd = sub { | |
893 | my $upid = shift; | |
894 | ||
895 | syslog ('info', "starting lxc termproxy $upid\n"); | |
896 | ||
897 | my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, | |
898 | '--perm', 'VM.Console', '--']; | |
899 | push @$cmd, @$remcmd, @$shcmd; | |
900 | ||
901 | PVE::Tools::run_command($cmd); | |
902 | }; | |
903 | ||
904 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); | |
905 | ||
906 | PVE::Tools::wait_for_vnc_port($port); | |
907 | ||
908 | return { | |
909 | user => $authuser, | |
910 | ticket => $ticket, | |
911 | port => $port, | |
912 | upid => $upid, | |
913 | }; | |
914 | }}); | |
915 | ||
fff3a342 DM |
916 | __PACKAGE__->register_method({ |
917 | name => 'vncwebsocket', | |
918 | path => '{vmid}/vncwebsocket', | |
919 | method => 'GET', | |
5b4657d0 | 920 | permissions => { |
fff3a342 DM |
921 | description => "You also need to pass a valid ticket (vncticket).", |
922 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
923 | }, | |
924 | description => "Opens a weksocket for VNC traffic.", | |
925 | parameters => { | |
926 | additionalProperties => 0, | |
927 | properties => { | |
928 | node => get_standard_option('pve-node'), | |
929 | vmid => get_standard_option('pve-vmid'), | |
930 | vncticket => { | |
931 | description => "Ticket from previous call to vncproxy.", | |
932 | type => 'string', | |
933 | maxLength => 512, | |
934 | }, | |
935 | port => { | |
936 | description => "Port number returned by previous vncproxy call.", | |
937 | type => 'integer', | |
938 | minimum => 5900, | |
939 | maximum => 5999, | |
940 | }, | |
941 | }, | |
942 | }, | |
943 | returns => { | |
944 | type => "object", | |
945 | properties => { | |
946 | port => { type => 'string' }, | |
947 | }, | |
948 | }, | |
949 | code => sub { | |
950 | my ($param) = @_; | |
951 | ||
952 | my $rpcenv = PVE::RPCEnvironment::get(); | |
953 | ||
954 | my $authuser = $rpcenv->get_user(); | |
955 | ||
956 | my $authpath = "/vms/$param->{vmid}"; | |
957 | ||
958 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
959 | ||
960 | my $port = $param->{port}; | |
5b4657d0 | 961 | |
fff3a342 DM |
962 | return { port => $port }; |
963 | }}); | |
964 | ||
965 | __PACKAGE__->register_method ({ | |
5b4657d0 DM |
966 | name => 'spiceproxy', |
967 | path => '{vmid}/spiceproxy', | |
fff3a342 DM |
968 | method => 'POST', |
969 | protected => 1, | |
970 | proxyto => 'node', | |
971 | permissions => { | |
972 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
973 | }, | |
974 | description => "Returns a SPICE configuration to connect to the CT.", | |
975 | parameters => { | |
976 | additionalProperties => 0, | |
977 | properties => { | |
978 | node => get_standard_option('pve-node'), | |
979 | vmid => get_standard_option('pve-vmid'), | |
980 | proxy => get_standard_option('spice-proxy', { optional => 1 }), | |
981 | }, | |
982 | }, | |
983 | returns => get_standard_option('remote-viewer-config'), | |
984 | code => sub { | |
985 | my ($param) = @_; | |
986 | ||
987 | my $vmid = $param->{vmid}; | |
988 | my $node = $param->{node}; | |
989 | my $proxy = $param->{proxy}; | |
990 | ||
991 | my $authpath = "/vms/$vmid"; | |
992 | my $permissions = 'VM.Console'; | |
993 | ||
67afe46e | 994 | my $conf = PVE::LXC::Config->load_config($vmid); |
da4db334 TL |
995 | |
996 | die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid); | |
997 | ||
aca816ad DM |
998 | my $concmd = PVE::LXC::get_console_command($vmid, $conf); |
999 | ||
5b4657d0 DM |
1000 | my $shcmd = ['/usr/bin/dtach', '-A', |
1001 | "/var/run/dtach/vzctlconsole$vmid", | |
aca816ad | 1002 | '-r', 'winch', '-z', @$concmd]; |
fff3a342 DM |
1003 | |
1004 | my $title = "CT $vmid"; | |
1005 | ||
1006 | return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd); | |
1007 | }}); | |
5c752bbf | 1008 | |
5c752bbf DM |
1009 | |
1010 | __PACKAGE__->register_method({ | |
52389a07 DM |
1011 | name => 'migrate_vm', |
1012 | path => '{vmid}/migrate', | |
5c752bbf DM |
1013 | method => 'POST', |
1014 | protected => 1, | |
1015 | proxyto => 'node', | |
52389a07 | 1016 | description => "Migrate the container to another node. Creates a new migration task.", |
5c752bbf | 1017 | permissions => { |
52389a07 | 1018 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], |
5c752bbf DM |
1019 | }, |
1020 | parameters => { | |
1021 | additionalProperties => 0, | |
1022 | properties => { | |
1023 | node => get_standard_option('pve-node'), | |
68e8f3c5 DM |
1024 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), |
1025 | target => get_standard_option('pve-node', { | |
1026 | description => "Target node.", | |
39bb88df | 1027 | completion => \&PVE::Cluster::complete_migration_target, |
68e8f3c5 | 1028 | }), |
52389a07 DM |
1029 | online => { |
1030 | type => 'boolean', | |
1031 | description => "Use online/live migration.", | |
1032 | optional => 1, | |
1033 | }, | |
00d8cdc0 DC |
1034 | restart => { |
1035 | type => 'boolean', | |
1036 | description => "Use restart migration", | |
1037 | optional => 1, | |
1038 | }, | |
1039 | timeout => { | |
1040 | type => 'integer', | |
1041 | description => "Timeout in seconds for shutdown for restart migration", | |
1042 | optional => 1, | |
1043 | default => 180, | |
1044 | }, | |
9746c095 FG |
1045 | force => { |
1046 | type => 'boolean', | |
1047 | description => "Force migration despite local bind / device" . | |
552e168f | 1048 | " mounts. NOTE: deprecated, use 'shared' property of mount point instead.", |
9746c095 FG |
1049 | optional => 1, |
1050 | }, | |
8ead60ef SI |
1051 | bwlimit => { |
1052 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1053 | optional => 1, | |
1054 | type => 'number', | |
1055 | minimum => '0', | |
41e9b65c | 1056 | default => 'migrate limit from datacenter or storage config', |
8ead60ef | 1057 | }, |
5c752bbf DM |
1058 | }, |
1059 | }, | |
1060 | returns => { | |
1061 | type => 'string', | |
52389a07 | 1062 | description => "the task ID.", |
5c752bbf DM |
1063 | }, |
1064 | code => sub { | |
1065 | my ($param) = @_; | |
1066 | ||
1067 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1068 | ||
1069 | my $authuser = $rpcenv->get_user(); | |
1070 | ||
52389a07 | 1071 | my $target = extract_param($param, 'target'); |
bb1ac2de | 1072 | |
52389a07 DM |
1073 | my $localnode = PVE::INotify::nodename(); |
1074 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
bb1ac2de | 1075 | |
52389a07 | 1076 | PVE::Cluster::check_cfs_quorum(); |
5c752bbf | 1077 | |
52389a07 | 1078 | PVE::Cluster::check_node_exists($target); |
27916659 | 1079 | |
52389a07 | 1080 | my $targetip = PVE::Cluster::remote_node_ip($target); |
5c752bbf | 1081 | |
52389a07 | 1082 | my $vmid = extract_param($param, 'vmid'); |
5c752bbf | 1083 | |
52389a07 | 1084 | # test if VM exists |
67afe46e | 1085 | PVE::LXC::Config->load_config($vmid); |
5c752bbf | 1086 | |
52389a07 DM |
1087 | # try to detect errors early |
1088 | if (PVE::LXC::check_running($vmid)) { | |
00d8cdc0 DC |
1089 | die "can't migrate running container without --online or --restart\n" |
1090 | if !$param->{online} && !$param->{restart}; | |
5c752bbf | 1091 | } |
5c752bbf DM |
1092 | |
1093 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
1094 | ||
1095 | my $hacmd = sub { | |
1096 | my $upid = shift; | |
1097 | ||
1098 | my $service = "ct:$vmid"; | |
1099 | ||
52389a07 | 1100 | my $cmd = ['ha-manager', 'migrate', $service, $target]; |
5c752bbf | 1101 | |
545dd4e1 | 1102 | print "Requesting HA migration for CT $vmid to node $target\n"; |
5c752bbf DM |
1103 | |
1104 | PVE::Tools::run_command($cmd); | |
1105 | ||
1106 | return; | |
1107 | }; | |
1108 | ||
52389a07 | 1109 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); |
5c752bbf DM |
1110 | |
1111 | } else { | |
1112 | ||
22557519 DM |
1113 | my $realcmd = sub { |
1114 | PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param); | |
1115 | }; | |
56462053 | 1116 | |
22557519 DM |
1117 | my $worker = sub { |
1118 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
5c752bbf DM |
1119 | }; |
1120 | ||
22557519 | 1121 | return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker); |
5c752bbf DM |
1122 | } |
1123 | }}); | |
1124 | ||
cc5392c8 WL |
1125 | __PACKAGE__->register_method({ |
1126 | name => 'vm_feature', | |
1127 | path => '{vmid}/feature', | |
1128 | method => 'GET', | |
1129 | proxyto => 'node', | |
1130 | protected => 1, | |
1131 | description => "Check if feature for virtual machine is available.", | |
1132 | permissions => { | |
1133 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1134 | }, | |
1135 | parameters => { | |
1136 | additionalProperties => 0, | |
1137 | properties => { | |
1138 | node => get_standard_option('pve-node'), | |
1139 | vmid => get_standard_option('pve-vmid'), | |
1140 | feature => { | |
1141 | description => "Feature to check.", | |
1142 | type => 'string', | |
d53e5c58 | 1143 | enum => [ 'snapshot', 'clone', 'copy' ], |
cc5392c8 | 1144 | }, |
5ec3949c | 1145 | snapname => get_standard_option('pve-snapshot-name', { |
cc5392c8 WL |
1146 | optional => 1, |
1147 | }), | |
1148 | }, | |
1149 | }, | |
1150 | returns => { | |
1151 | type => "object", | |
1152 | properties => { | |
1153 | hasFeature => { type => 'boolean' }, | |
1154 | #nodes => { | |
1155 | #type => 'array', | |
1156 | #items => { type => 'string' }, | |
1157 | #} | |
1158 | }, | |
1159 | }, | |
1160 | code => sub { | |
1161 | my ($param) = @_; | |
1162 | ||
1163 | my $node = extract_param($param, 'node'); | |
1164 | ||
1165 | my $vmid = extract_param($param, 'vmid'); | |
1166 | ||
1167 | my $snapname = extract_param($param, 'snapname'); | |
1168 | ||
1169 | my $feature = extract_param($param, 'feature'); | |
1170 | ||
67afe46e | 1171 | my $conf = PVE::LXC::Config->load_config($vmid); |
cc5392c8 WL |
1172 | |
1173 | if($snapname){ | |
1174 | my $snap = $conf->{snapshots}->{$snapname}; | |
1175 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
1176 | $conf = $snap; | |
1177 | } | |
ef241384 | 1178 | my $storage_cfg = PVE::Storage::config(); |
cc5392c8 | 1179 | #Maybe include later |
ef241384 | 1180 | #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg); |
4518000b | 1181 | my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname); |
cc5392c8 WL |
1182 | |
1183 | return { | |
1184 | hasFeature => $hasFeature, | |
1185 | #nodes => [ keys %$nodelist ], | |
1186 | }; | |
1187 | }}); | |
bb1ac2de DM |
1188 | |
1189 | __PACKAGE__->register_method({ | |
1190 | name => 'template', | |
1191 | path => '{vmid}/template', | |
1192 | method => 'POST', | |
1193 | protected => 1, | |
1194 | proxyto => 'node', | |
1195 | description => "Create a Template.", | |
1196 | permissions => { | |
1197 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
1198 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1199 | }, | |
1200 | parameters => { | |
1201 | additionalProperties => 0, | |
1202 | properties => { | |
1203 | node => get_standard_option('pve-node'), | |
68e8f3c5 | 1204 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), |
bb1ac2de DM |
1205 | }, |
1206 | }, | |
1207 | returns => { type => 'null'}, | |
1208 | code => sub { | |
1209 | my ($param) = @_; | |
1210 | ||
1211 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1212 | ||
1213 | my $authuser = $rpcenv->get_user(); | |
1214 | ||
1215 | my $node = extract_param($param, 'node'); | |
1216 | ||
1217 | my $vmid = extract_param($param, 'vmid'); | |
1218 | ||
1219 | my $updatefn = sub { | |
1220 | ||
67afe46e FG |
1221 | my $conf = PVE::LXC::Config->load_config($vmid); |
1222 | PVE::LXC::Config->check_lock($conf); | |
bb1ac2de DM |
1223 | |
1224 | die "unable to create template, because CT contains snapshots\n" | |
1225 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
1226 | ||
1227 | die "you can't convert a template to a template\n" | |
67afe46e | 1228 | if PVE::LXC::Config->is_template($conf); |
bb1ac2de DM |
1229 | |
1230 | die "you can't convert a CT to template if the CT is running\n" | |
1231 | if PVE::LXC::check_running($vmid); | |
1232 | ||
a2d3e5c3 CE |
1233 | if (my $err = check_storage_supports_templates($conf)) { |
1234 | die $err; | |
1235 | } | |
e1313b73 | 1236 | |
bb1ac2de DM |
1237 | my $realcmd = sub { |
1238 | PVE::LXC::template_create($vmid, $conf); | |
bb1ac2de | 1239 | |
c2182c49 | 1240 | $conf->{template} = 1; |
bb1ac2de | 1241 | |
c2182c49 WL |
1242 | PVE::LXC::Config->write_config($vmid, $conf); |
1243 | # and remove lxc config | |
1244 | PVE::LXC::update_lxc_config($vmid, $conf); | |
1245 | }; | |
bb1ac2de DM |
1246 | |
1247 | return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd); | |
1248 | }; | |
1249 | ||
67afe46e | 1250 | PVE::LXC::Config->lock_config($vmid, $updatefn); |
bb1ac2de DM |
1251 | |
1252 | return undef; | |
1253 | }}); | |
1254 | ||
c4a33727 DM |
1255 | __PACKAGE__->register_method({ |
1256 | name => 'clone_vm', | |
1257 | path => '{vmid}/clone', | |
1258 | method => 'POST', | |
1259 | protected => 1, | |
1260 | proxyto => 'node', | |
1261 | description => "Create a container clone/copy", | |
1262 | permissions => { | |
1263 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, " . | |
1264 | "and 'VM.Allocate' permissions " . | |
1265 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
1266 | "'Datastore.AllocateSpace' on any used storage.", | |
1267 | check => | |
1268 | [ 'and', | |
1269 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
1270 | [ 'or', | |
1271 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
1272 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
1273 | ], | |
1274 | ] | |
1275 | }, | |
1276 | parameters => { | |
1277 | additionalProperties => 0, | |
1278 | properties => { | |
1279 | node => get_standard_option('pve-node'), | |
1280 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1281 | newid => get_standard_option('pve-vmid', { | |
1282 | completion => \&PVE::Cluster::complete_next_vmid, | |
1283 | description => 'VMID for the clone.' }), | |
1284 | hostname => { | |
1285 | optional => 1, | |
1286 | type => 'string', format => 'dns-name', | |
1287 | description => "Set a hostname for the new CT.", | |
1288 | }, | |
1289 | description => { | |
1290 | optional => 1, | |
1291 | type => 'string', | |
1292 | description => "Description for the new CT.", | |
1293 | }, | |
1294 | pool => { | |
1295 | optional => 1, | |
1296 | type => 'string', format => 'pve-poolid', | |
1297 | description => "Add the new CT to the specified pool.", | |
1298 | }, | |
5ec3949c | 1299 | snapname => get_standard_option('pve-snapshot-name', { |
c4a33727 DM |
1300 | optional => 1, |
1301 | }), | |
1302 | storage => get_standard_option('pve-storage-id', { | |
1303 | description => "Target storage for full clone.", | |
c4a33727 DM |
1304 | optional => 1, |
1305 | }), | |
1306 | full => { | |
1307 | optional => 1, | |
1308 | type => 'boolean', | |
c4b4cb83 | 1309 | description => "Create a full copy of all disks. This is always done when " . |
c4a33727 | 1310 | "you clone a normal CT. For CT templates, we try to create a linked clone by default.", |
c4a33727 | 1311 | }, |
411ae12c DM |
1312 | target => get_standard_option('pve-node', { |
1313 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
1314 | optional => 1, | |
1315 | }), | |
8ead60ef SI |
1316 | bwlimit => { |
1317 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1318 | optional => 1, | |
1319 | type => 'number', | |
1320 | minimum => '0', | |
41e9b65c | 1321 | default => 'clone limit from datacenter or storage config', |
8ead60ef | 1322 | }, |
c4a33727 DM |
1323 | }, |
1324 | }, | |
1325 | returns => { | |
1326 | type => 'string', | |
1327 | }, | |
1328 | code => sub { | |
1329 | my ($param) = @_; | |
1330 | ||
1331 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1332 | ||
1333 | my $authuser = $rpcenv->get_user(); | |
1334 | ||
1335 | my $node = extract_param($param, 'node'); | |
1336 | ||
1337 | my $vmid = extract_param($param, 'vmid'); | |
1338 | ||
1339 | my $newid = extract_param($param, 'newid'); | |
1340 | ||
1341 | my $pool = extract_param($param, 'pool'); | |
1342 | ||
1343 | if (defined($pool)) { | |
1344 | $rpcenv->check_pool_exist($pool); | |
1345 | } | |
1346 | ||
1347 | my $snapname = extract_param($param, 'snapname'); | |
1348 | ||
1349 | my $storage = extract_param($param, 'storage'); | |
1350 | ||
411ae12c DM |
1351 | my $target = extract_param($param, 'target'); |
1352 | ||
c4a33727 DM |
1353 | my $localnode = PVE::INotify::nodename(); |
1354 | ||
411ae12c DM |
1355 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
1356 | ||
1357 | PVE::Cluster::check_node_exists($target) if $target; | |
1358 | ||
c4a33727 DM |
1359 | my $storecfg = PVE::Storage::config(); |
1360 | ||
1361 | if ($storage) { | |
1362 | # check if storage is enabled on local node | |
1363 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
411ae12c DM |
1364 | if ($target) { |
1365 | # check if storage is available on target node | |
1366 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
1367 | # clone only works if target storage is shared | |
1368 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1369 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
1370 | } | |
c4a33727 DM |
1371 | } |
1372 | ||
1373 | PVE::Cluster::check_cfs_quorum(); | |
1374 | ||
db01d674 WB |
1375 | my $conffile; |
1376 | my $newconf = {}; | |
1377 | my $mountpoints = {}; | |
1378 | my $fullclone = {}; | |
1379 | my $vollist = []; | |
411ae12c | 1380 | my $running; |
c4a33727 | 1381 | |
db01d674 WB |
1382 | PVE::LXC::Config->lock_config($vmid, sub { |
1383 | my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk'); | |
e7d553c7 | 1384 | |
411ae12c DM |
1385 | $running = PVE::LXC::check_running($vmid) || 0; |
1386 | ||
e7d553c7 DM |
1387 | my $full = extract_param($param, 'full'); |
1388 | if (!defined($full)) { | |
1389 | $full = !PVE::LXC::Config->is_template($src_conf); | |
1390 | } | |
1391 | die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full; | |
1392 | ||
db01d674 WB |
1393 | eval { |
1394 | die "snapshot '$snapname' does not exist\n" | |
1395 | if $snapname && !defined($src_conf->{snapshots}->{$snapname}); | |
c4a33727 | 1396 | |
c4a33727 | 1397 | |
db01d674 | 1398 | my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf; |
c4a33727 | 1399 | |
db01d674 WB |
1400 | $conffile = PVE::LXC::Config->config_file($newid); |
1401 | die "unable to create CT $newid: config file already exists\n" | |
1402 | if -f $conffile; | |
c4a33727 | 1403 | |
24f9d440 | 1404 | my $sharedvm = 1; |
db01d674 WB |
1405 | foreach my $opt (keys %$src_conf) { |
1406 | next if $opt =~ m/^unused\d+$/; | |
c4a33727 | 1407 | |
db01d674 | 1408 | my $value = $src_conf->{$opt}; |
c4a33727 | 1409 | |
db01d674 WB |
1410 | if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) { |
1411 | my $mp = $opt eq 'rootfs' ? | |
1412 | PVE::LXC::Config->parse_ct_rootfs($value) : | |
1413 | PVE::LXC::Config->parse_ct_mountpoint($value); | |
c4a33727 | 1414 | |
db01d674 WB |
1415 | if ($mp->{type} eq 'volume') { |
1416 | my $volid = $mp->{volume}; | |
09687674 DM |
1417 | |
1418 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1419 | $sid = $storage if defined($storage); | |
1420 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
24f9d440 WB |
1421 | if (!$scfg->{shared}) { |
1422 | $sharedvm = 0; | |
1423 | warn "found non-shared volume: $volid\n" if $target; | |
1424 | } | |
09687674 DM |
1425 | |
1426 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
1427 | ||
e7d553c7 | 1428 | if ($full) { |
db01d674 WB |
1429 | die "Cannot do full clones on a running container without snapshots\n" |
1430 | if $running && !defined($snapname); | |
1431 | $fullclone->{$opt} = 1; | |
1432 | } else { | |
1433 | # not full means clone instead of copy | |
1434 | die "Linked clone feature for '$volid' is not available\n" | |
1435 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running); | |
1436 | } | |
c4a33727 | 1437 | |
db01d674 WB |
1438 | $mountpoints->{$opt} = $mp; |
1439 | push @$vollist, $volid; | |
c4a33727 | 1440 | |
c4a33727 | 1441 | } else { |
db01d674 WB |
1442 | # TODO: allow bind mounts? |
1443 | die "unable to clone mountpint '$opt' (type $mp->{type})\n"; | |
c4a33727 | 1444 | } |
63231f52 TL |
1445 | } elsif ($opt =~ m/^net(\d+)$/) { |
1446 | # always change MAC! address | |
1447 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1448 | my $net = PVE::LXC::Config->parse_lxc_network($value); | |
1449 | $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
1450 | $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net); | |
c4a33727 | 1451 | } else { |
db01d674 WB |
1452 | # copy everything else |
1453 | $newconf->{$opt} = $value; | |
c4a33727 | 1454 | } |
db01d674 | 1455 | } |
24f9d440 WB |
1456 | die "can't clone CT to node '$target' (CT uses local storage)\n" |
1457 | if $target && !$sharedvm; | |
c4a33727 | 1458 | |
db01d674 WB |
1459 | # Replace the 'disk' lock with a 'create' lock. |
1460 | $newconf->{lock} = 'create'; | |
1461 | ||
037dc1e2 | 1462 | delete $newconf->{pending}; |
db01d674 WB |
1463 | delete $newconf->{template}; |
1464 | if ($param->{hostname}) { | |
1465 | $newconf->{hostname} = $param->{hostname}; | |
c4a33727 | 1466 | } |
c4a33727 | 1467 | |
db01d674 WB |
1468 | if ($param->{description}) { |
1469 | $newconf->{description} = $param->{description}; | |
1470 | } | |
c4a33727 | 1471 | |
db01d674 WB |
1472 | # create empty/temp config - this fails if CT already exists on other node |
1473 | PVE::LXC::Config->write_config($newid, $newconf); | |
1474 | }; | |
1475 | if (my $err = $@) { | |
1476 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1477 | warn $@ if $@; | |
1478 | die $err; | |
c4a33727 | 1479 | } |
db01d674 | 1480 | }); |
c4a33727 | 1481 | |
db01d674 WB |
1482 | my $update_conf = sub { |
1483 | my ($key, $value) = @_; | |
1484 | return PVE::LXC::Config->lock_config($newid, sub { | |
1485 | my $conf = PVE::LXC::Config->load_config($newid); | |
1486 | die "Lost 'create' config lock, aborting.\n" | |
1487 | if !PVE::LXC::Config->has_lock($conf, 'create'); | |
1488 | $conf->{$key} = $value; | |
1489 | PVE::LXC::Config->write_config($newid, $conf); | |
1490 | }); | |
1491 | }; | |
c4a33727 | 1492 | |
db01d674 WB |
1493 | my $realcmd = sub { |
1494 | my ($upid) = @_; | |
c4a33727 | 1495 | |
db01d674 | 1496 | my $newvollist = []; |
c4a33727 | 1497 | |
411ae12c DM |
1498 | my $verify_running = PVE::LXC::check_running($vmid) || 0; |
1499 | die "unexpected state change\n" if $verify_running != $running; | |
1500 | ||
db01d674 WB |
1501 | eval { |
1502 | local $SIG{INT} = | |
1503 | local $SIG{TERM} = | |
1504 | local $SIG{QUIT} = | |
1505 | local $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
c4a33727 | 1506 | |
db01d674 | 1507 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); |
8ead60ef | 1508 | my $bwlimit = extract_param($param, 'bwlimit'); |
c4a33727 | 1509 | |
db01d674 WB |
1510 | foreach my $opt (keys %$mountpoints) { |
1511 | my $mp = $mountpoints->{$opt}; | |
1512 | my $volid = $mp->{volume}; | |
c4a33727 | 1513 | |
db01d674 WB |
1514 | my $newvolid; |
1515 | if ($fullclone->{$opt}) { | |
1516 | print "create full clone of mountpoint $opt ($volid)\n"; | |
8ead60ef SI |
1517 | my $source_storage = PVE::Storage::parse_volume_id($volid); |
1518 | my $target_storage = $storage // $source_storage; | |
1519 | my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', [$source_storage, $target_storage], $bwlimit); | |
1520 | $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname, $clonelimit); | |
db01d674 WB |
1521 | } else { |
1522 | print "create linked clone of mount point $opt ($volid)\n"; | |
1523 | $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname); | |
c4a33727 DM |
1524 | } |
1525 | ||
db01d674 WB |
1526 | push @$newvollist, $newvolid; |
1527 | $mp->{volume} = $newvolid; | |
c4a33727 | 1528 | |
db01d674 | 1529 | $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs')); |
c4a33727 DM |
1530 | } |
1531 | ||
db01d674 WB |
1532 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
1533 | PVE::LXC::Config->remove_lock($newid, 'create'); | |
411ae12c DM |
1534 | |
1535 | if ($target) { | |
1536 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
1537 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; | |
1538 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); | |
1539 | ||
1540 | my $newconffile = PVE::LXC::Config->config_file($newid, $target); | |
1541 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
1542 | if !rename($conffile, $newconffile); | |
1543 | } | |
c4a33727 | 1544 | }; |
db01d674 | 1545 | my $err = $@; |
c4a33727 | 1546 | |
db01d674 WB |
1547 | # Unlock the source config in any case: |
1548 | eval { PVE::LXC::Config->remove_lock($vmid, 'disk') }; | |
1549 | warn $@ if $@; | |
c4a33727 | 1550 | |
db01d674 WB |
1551 | if ($err) { |
1552 | # Now cleanup the config & disks: | |
1553 | unlink $conffile; | |
c4a33727 | 1554 | |
db01d674 WB |
1555 | sleep 1; # some storages like rbd need to wait before release volume - really? |
1556 | ||
1557 | foreach my $volid (@$newvollist) { | |
1558 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
1559 | warn $@ if $@; | |
1560 | } | |
1561 | die "clone failed: $err"; | |
1562 | } | |
1563 | ||
1564 | return; | |
c4a33727 DM |
1565 | }; |
1566 | ||
db01d674 WB |
1567 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); |
1568 | return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd); | |
c4a33727 DM |
1569 | }}); |
1570 | ||
1571 | ||
985b18ed WL |
1572 | __PACKAGE__->register_method({ |
1573 | name => 'resize_vm', | |
1574 | path => '{vmid}/resize', | |
1575 | method => 'PUT', | |
1576 | protected => 1, | |
1577 | proxyto => 'node', | |
235dbdf3 | 1578 | description => "Resize a container mount point.", |
985b18ed WL |
1579 | permissions => { |
1580 | check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1], | |
1581 | }, | |
1582 | parameters => { | |
1583 | additionalProperties => 0, | |
b8c5a95f WB |
1584 | properties => { |
1585 | node => get_standard_option('pve-node'), | |
1586 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1587 | disk => { | |
1588 | type => 'string', | |
1589 | description => "The disk you want to resize.", | |
d250604f | 1590 | enum => [PVE::LXC::Config->mountpoint_names()], |
b8c5a95f WB |
1591 | }, |
1592 | size => { | |
1593 | type => 'string', | |
1594 | pattern => '\+?\d+(\.\d+)?[KMGT]?', | |
1595 | 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.", | |
1596 | }, | |
1597 | digest => { | |
1598 | type => 'string', | |
1599 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1600 | maxLength => 40, | |
1601 | optional => 1, | |
1602 | } | |
1603 | }, | |
985b18ed | 1604 | }, |
9f3f7963 WL |
1605 | returns => { |
1606 | type => 'string', | |
1607 | description => "the task ID.", | |
1608 | }, | |
985b18ed WL |
1609 | code => sub { |
1610 | my ($param) = @_; | |
1611 | ||
1612 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1613 | ||
1614 | my $authuser = $rpcenv->get_user(); | |
1615 | ||
1616 | my $node = extract_param($param, 'node'); | |
1617 | ||
1618 | my $vmid = extract_param($param, 'vmid'); | |
1619 | ||
1620 | my $digest = extract_param($param, 'digest'); | |
1621 | ||
1622 | my $sizestr = extract_param($param, 'size'); | |
1623 | my $ext = ($sizestr =~ s/^\+//); | |
1624 | my $newsize = PVE::JSONSchema::parse_size($sizestr); | |
1625 | die "invalid size string" if !defined($newsize); | |
1626 | ||
1627 | die "no options specified\n" if !scalar(keys %$param); | |
1628 | ||
f1ba1a4b | 1629 | PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []); |
985b18ed WL |
1630 | |
1631 | my $storage_cfg = cfs_read_file("storage.cfg"); | |
1632 | ||
985b18ed WL |
1633 | my $code = sub { |
1634 | ||
67afe46e FG |
1635 | my $conf = PVE::LXC::Config->load_config($vmid); |
1636 | PVE::LXC::Config->check_lock($conf); | |
985b18ed WL |
1637 | |
1638 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1639 | ||
1640 | my $running = PVE::LXC::check_running($vmid); | |
1641 | ||
1642 | my $disk = $param->{disk}; | |
1b4cf758 FG |
1643 | my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) : |
1644 | PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk}); | |
44a9face | 1645 | |
985b18ed WL |
1646 | my $volid = $mp->{volume}; |
1647 | ||
1648 | my (undef, undef, $owner, undef, undef, undef, $format) = | |
1649 | PVE::Storage::parse_volname($storage_cfg, $volid); | |
1650 | ||
235dbdf3 | 1651 | die "can't resize mount point owned by another container ($owner)" |
985b18ed WL |
1652 | if $vmid != $owner; |
1653 | ||
1654 | die "can't resize volume: $disk if snapshot exists\n" | |
1655 | if %{$conf->{snapshots}} && $format eq 'qcow2'; | |
1656 | ||
1657 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
1658 | ||
1659 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
1660 | ||
4e1e1791 WB |
1661 | PVE::Storage::activate_volumes($storage_cfg, [$volid]); |
1662 | ||
985b18ed | 1663 | my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5); |
2e8bcfef | 1664 | |
16f6c7c6 | 1665 | die "Could not determine current size of volume '$volid'\n" if !defined($size); |
2e8bcfef | 1666 | |
985b18ed WL |
1667 | $newsize += $size if $ext; |
1668 | $newsize = int($newsize); | |
1669 | ||
1670 | die "unable to shrink disk size\n" if $newsize < $size; | |
1671 | ||
1672 | return if $size == $newsize; | |
1673 | ||
1674 | PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr"); | |
9f3f7963 | 1675 | my $realcmd = sub { |
e72c8b0e DM |
1676 | # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so |
1677 | # we pass 0 here (parameter only makes sense for qemu) | |
9f3f7963 WL |
1678 | PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0); |
1679 | ||
1680 | $mp->{size} = $newsize; | |
1b4cf758 | 1681 | $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs'); |
9f3f7963 | 1682 | |
67afe46e | 1683 | PVE::LXC::Config->write_config($vmid, $conf); |
9f3f7963 WL |
1684 | |
1685 | if ($format eq 'raw') { | |
46e62d2d TL |
1686 | # we need to ensure that the volume is mapped, if not needed this is a NOP |
1687 | my $path = PVE::Storage::map_volume($storage_cfg, $volid); | |
1688 | $path = PVE::Storage::path($storage_cfg, $volid) if !defined($path); | |
9f3f7963 | 1689 | if ($running) { |
2a993004 WL |
1690 | |
1691 | $mp->{mp} = '/'; | |
1692 | my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1]; | |
21f292ff | 1693 | $path = PVE::LXC::query_loopdev($path) if $use_loopdev; |
235dbdf3 | 1694 | die "internal error: CT running but mount point not attached to a loop device" |
2a993004 WL |
1695 | if !$path; |
1696 | PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev; | |
9f3f7963 WL |
1697 | |
1698 | # In order for resize2fs to know that we need online-resizing a mountpoint needs | |
1699 | # to be visible to it in its namespace. | |
1700 | # To not interfere with the rest of the system we unshare the current mount namespace, | |
1701 | # mount over /tmp and then run resize2fs. | |
1702 | ||
1703 | # interestingly we don't need to e2fsck on mounted systems... | |
1704 | my $quoted = PVE::Tools::shellquote($path); | |
1705 | my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted"; | |
ce9e6458 WB |
1706 | eval { |
1707 | PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]); | |
1708 | }; | |
1709 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
9f3f7963 | 1710 | } else { |
ce9e6458 WB |
1711 | eval { |
1712 | PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]); | |
1713 | PVE::Tools::run_command(['resize2fs', $path]); | |
1714 | }; | |
1715 | warn "Failed to update the container's filesystem: $@\n" if $@; | |
80440faa | 1716 | |
46ea6368 TL |
1717 | # always un-map if not running, this is a NOP if not needed |
1718 | PVE::Storage::unmap_volume($storage_cfg, $volid); | |
9f3f7963 | 1719 | } |
985b18ed | 1720 | } |
9f3f7963 | 1721 | }; |
985b18ed | 1722 | |
9f3f7963 WL |
1723 | return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd); |
1724 | }; | |
985b18ed | 1725 | |
67afe46e | 1726 | return PVE::LXC::Config->lock_config($vmid, $code);; |
985b18ed WL |
1727 | }}); |
1728 | ||
3c0f6806 WB |
1729 | __PACKAGE__->register_method({ |
1730 | name => 'move_volume', | |
1731 | path => '{vmid}/move_volume', | |
1732 | method => 'POST', | |
1733 | protected => 1, | |
1734 | proxyto => 'node', | |
1735 | description => "Move a rootfs-/mp-volume to a different storage", | |
1736 | permissions => { | |
1737 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . | |
1738 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
1739 | check => | |
1740 | [ 'and', | |
1741 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
1742 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
1743 | ], | |
1744 | }, | |
1745 | parameters => { | |
1746 | additionalProperties => 0, | |
1747 | properties => { | |
1748 | node => get_standard_option('pve-node'), | |
1749 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1750 | volume => { | |
1751 | type => 'string', | |
1752 | enum => [ PVE::LXC::Config->mountpoint_names() ], | |
1753 | description => "Volume which will be moved.", | |
1754 | }, | |
1755 | storage => get_standard_option('pve-storage-id', { | |
1756 | description => "Target Storage.", | |
1757 | completion => \&PVE::Storage::complete_storage_enabled, | |
1758 | }), | |
1759 | delete => { | |
1760 | type => 'boolean', | |
1761 | description => "Delete the original volume after successful copy. By default the original is kept as an unused volume entry.", | |
1762 | optional => 1, | |
1763 | default => 0, | |
1764 | }, | |
1765 | digest => { | |
1766 | type => 'string', | |
1767 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1768 | maxLength => 40, | |
1769 | optional => 1, | |
8ead60ef SI |
1770 | }, |
1771 | bwlimit => { | |
1772 | description => "Override I/O bandwidth limit (in KiB/s).", | |
1773 | optional => 1, | |
1774 | type => 'number', | |
1775 | minimum => '0', | |
41e9b65c | 1776 | default => 'clone limit from datacenter or storage config', |
8ead60ef | 1777 | }, |
3c0f6806 WB |
1778 | }, |
1779 | }, | |
1780 | returns => { | |
1781 | type => 'string', | |
1782 | }, | |
1783 | code => sub { | |
1784 | my ($param) = @_; | |
1785 | ||
1786 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1787 | ||
1788 | my $authuser = $rpcenv->get_user(); | |
1789 | ||
1790 | my $vmid = extract_param($param, 'vmid'); | |
1791 | ||
1792 | my $storage = extract_param($param, 'storage'); | |
1793 | ||
1794 | my $mpkey = extract_param($param, 'volume'); | |
1795 | ||
1796 | my $lockname = 'disk'; | |
1797 | ||
1798 | my ($mpdata, $old_volid); | |
1799 | ||
1800 | PVE::LXC::Config->lock_config($vmid, sub { | |
1801 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1802 | PVE::LXC::Config->check_lock($conf); | |
1803 | ||
1804 | die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid); | |
1805 | ||
1806 | if ($mpkey eq 'rootfs') { | |
1807 | $mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey}); | |
1808 | } elsif ($mpkey =~ m/mp\d+/) { | |
1809 | $mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey}); | |
1810 | } else { | |
1811 | die "Can't parse $mpkey\n"; | |
1812 | } | |
1813 | $old_volid = $mpdata->{volume}; | |
1814 | ||
1815 | die "you can't move a volume with snapshots and delete the source\n" | |
1816 | if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid); | |
1817 | ||
1818 | PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest}); | |
1819 | ||
1820 | PVE::LXC::Config->set_lock($vmid, $lockname); | |
1821 | }); | |
1822 | ||
1823 | my $realcmd = sub { | |
1824 | eval { | |
1825 | PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage"); | |
1826 | ||
1827 | my $conf = PVE::LXC::Config->load_config($vmid); | |
1828 | my $storage_cfg = PVE::Storage::config(); | |
1829 | ||
1830 | my $new_volid; | |
1831 | ||
1832 | eval { | |
1833 | PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]); | |
8ead60ef SI |
1834 | my $bwlimit = extract_param($param, 'bwlimit'); |
1835 | my $source_storage = PVE::Storage::parse_volume_id($old_volid); | |
1836 | my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$source_storage, $storage], $bwlimit); | |
1837 | $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf, undef, $movelimit); | |
3c0f6806 WB |
1838 | $mpdata->{volume} = $new_volid; |
1839 | ||
1840 | PVE::LXC::Config->lock_config($vmid, sub { | |
1841 | my $digest = $conf->{digest}; | |
1842 | $conf = PVE::LXC::Config->load_config($vmid); | |
1843 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); | |
1844 | ||
1845 | $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs'); | |
1846 | ||
1847 | PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete}; | |
1848 | ||
1849 | PVE::LXC::Config->write_config($vmid, $conf); | |
1850 | }); | |
1851 | ||
1852 | eval { | |
1853 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
1854 | PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ]) | |
1855 | }; | |
1856 | warn $@ if $@; | |
1857 | }; | |
1858 | if (my $err = $@) { | |
1859 | eval { | |
1860 | PVE::Storage::vdisk_free($storage_cfg, $new_volid) | |
1861 | if defined($new_volid); | |
1862 | }; | |
1863 | warn $@ if $@; | |
1864 | die $err; | |
1865 | } | |
1866 | ||
1867 | if ($param->{delete}) { | |
1868 | eval { | |
1869 | PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]); | |
1870 | PVE::Storage::vdisk_free($storage_cfg, $old_volid); | |
1871 | }; | |
1872 | warn $@ if $@; | |
1873 | } | |
1874 | }; | |
1875 | my $err = $@; | |
1876 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1877 | warn $@ if $@; | |
1878 | die $err if $err; | |
1879 | }; | |
1880 | my $task = eval { | |
1881 | $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd); | |
1882 | }; | |
1883 | if (my $err = $@) { | |
1884 | eval { PVE::LXC::Config->remove_lock($vmid, $lockname) }; | |
1885 | warn $@ if $@; | |
1886 | die $err; | |
1887 | } | |
1888 | return $task; | |
1889 | }}); | |
1890 | ||
37c2dba8 OB |
1891 | __PACKAGE__->register_method({ |
1892 | name => 'vm_pending', | |
1893 | path => '{vmid}/pending', | |
1894 | method => 'GET', | |
1895 | proxyto => 'node', | |
1896 | description => 'Get container configuration, including pending changes.', | |
1897 | permissions => { | |
1898 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1899 | }, | |
1900 | parameters => { | |
1901 | additionalProperties => 0, | |
1902 | properties => { | |
1903 | node => get_standard_option('pve-node'), | |
1904 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
1905 | }, | |
1906 | }, | |
1907 | returns => { | |
1908 | type => "array", | |
1909 | items => { | |
1910 | type => "object", | |
1911 | properties => { | |
1912 | key => { | |
1913 | description => 'Configuration option name.', | |
1914 | type => 'string', | |
1915 | }, | |
1916 | value => { | |
1917 | description => 'Current value.', | |
1918 | type => 'string', | |
1919 | optional => 1, | |
1920 | }, | |
1921 | pending => { | |
1922 | description => 'Pending value.', | |
1923 | type => 'string', | |
1924 | optional => 1, | |
1925 | }, | |
1926 | delete => { | |
1927 | description => "Indicates a pending delete request if present and not 0.", | |
1928 | type => 'integer', | |
1929 | minimum => 0, | |
1930 | maximum => 2, | |
1931 | optional => 1, | |
1932 | }, | |
1933 | }, | |
1934 | }, | |
1935 | }, | |
1936 | code => sub { | |
1937 | my ($param) = @_; | |
1938 | ||
1939 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); | |
1940 | ||
1941 | my $pending_delete_hash = PVE::LXC::Config->parse_pending_delete($conf->{pending}->{delete}); | |
1942 | ||
f622e7dc | 1943 | return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash); |
37c2dba8 OB |
1944 | }}); |
1945 | ||
f76a2828 | 1946 | 1; |