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