]>
Commit | Line | Data |
---|---|---|
9beee926 DM |
1 | package PVE::CLI::pct; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
b95bf64b WB |
6 | use POSIX; |
7 | use Fcntl; | |
8 | use File::Copy 'copy'; | |
70f2d702 | 9 | use Term::ReadLine; |
b95bf64b | 10 | |
9beee926 DM |
11 | use PVE::SafeSyslog; |
12 | use PVE::Tools qw(extract_param); | |
c87b1505 | 13 | use PVE::CpuSet; |
9beee926 DM |
14 | use PVE::Cluster; |
15 | use PVE::INotify; | |
16 | use PVE::RPCEnvironment; | |
17 | use PVE::JSONSchema qw(get_standard_option); | |
18 | use PVE::CLIHandler; | |
19 | use PVE::API2::LXC; | |
20 | use PVE::API2::LXC::Config; | |
21 | use PVE::API2::LXC::Status; | |
22 | use PVE::API2::LXC::Snapshot; | |
23 | ||
24 | use Data::Dumper; | |
25 | ||
26 | use base qw(PVE::CLIHandler); | |
27 | ||
28 | my $nodename = PVE::INotify::nodename(); | |
29 | ||
30 | my $upid_exit = sub { | |
31 | my $upid = shift; | |
32 | my $status = PVE::Tools::upid_read_status($upid); | |
33 | exit($status eq 'OK' ? 0 : -1); | |
34 | }; | |
35 | ||
52e1f0cb DM |
36 | sub setup_environment { |
37 | PVE::RPCEnvironment->setup_default_cli_env(); | |
38 | } | |
39 | ||
699ee9b2 DC |
40 | __PACKAGE__->register_method ({ |
41 | name => 'status', | |
42 | path => 'status', | |
43 | method => 'GET', | |
44 | description => "Show CT status.", | |
45 | parameters => { | |
46 | additionalProperties => 0, | |
47 | properties => { | |
48 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
49 | verbose => { | |
50 | description => "Verbose output format", | |
51 | type => 'boolean', | |
52 | optional => 1, | |
53 | } | |
54 | }, | |
55 | }, | |
56 | returns => { type => 'null'}, | |
57 | code => sub { | |
58 | my ($param) = @_; | |
59 | ||
60 | # test if CT exists | |
61 | my $conf = PVE::LXC::Config->load_config ($param->{vmid}); | |
62 | ||
63 | my $vmstatus = PVE::LXC::vmstatus($param->{vmid}); | |
64 | my $stat = $vmstatus->{$param->{vmid}}; | |
65 | if ($param->{verbose}) { | |
66 | foreach my $k (sort (keys %$stat)) { | |
67 | my $v = $stat->{$k}; | |
68 | next if !defined($v); | |
69 | print "$k: $v\n"; | |
70 | } | |
71 | } else { | |
72 | my $status = $stat->{status} || 'unknown'; | |
73 | print "status: $status\n"; | |
74 | } | |
75 | ||
76 | return undef; | |
77 | }}); | |
78 | ||
70f2d702 FG |
79 | sub read_password { |
80 | my $term = new Term::ReadLine ('pct'); | |
81 | my $attribs = $term->Attribs; | |
82 | $attribs->{redisplay_function} = $attribs->{shadow_redisplay}; | |
83 | my $input = $term->readline('Enter password: '); | |
84 | my $conf = $term->readline('Retype password: '); | |
85 | die "Passwords do not match.\n" if ($input ne $conf); | |
86 | return $input; | |
87 | } | |
88 | ||
34ddbf08 FG |
89 | sub string_param_file_mapping { |
90 | my ($name) = @_; | |
91 | ||
92 | my $mapping = { | |
93 | 'create_vm' => ['ssh-public-keys'], | |
94 | }; | |
95 | ||
96 | return defined($mapping->{$name}) ? $mapping->{$name} : []; | |
97 | } | |
98 | ||
c72bd0ba WL |
99 | __PACKAGE__->register_method ({ |
100 | name => 'unlock', | |
101 | path => 'unlock', | |
102 | method => 'PUT', | |
103 | description => "Unlock the VM.", | |
104 | parameters => { | |
105 | additionalProperties => 0, | |
106 | properties => { | |
107 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
108 | }, | |
109 | }, | |
110 | returns => { type => 'null'}, | |
111 | code => sub { | |
112 | my ($param) = @_; | |
113 | ||
114 | my $vmid = $param->{vmid}; | |
115 | ||
ad408fe1 | 116 | PVE::LXC::Config->remove_lock($vmid); |
c72bd0ba WL |
117 | |
118 | return undef; | |
119 | }}); | |
120 | ||
9beee926 DM |
121 | __PACKAGE__->register_method ({ |
122 | name => 'console', | |
123 | path => 'console', | |
124 | method => 'GET', | |
125 | description => "Launch a console for the specified container.", | |
126 | parameters => { | |
127 | additionalProperties => 0, | |
128 | properties => { | |
129 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }), | |
130 | }, | |
131 | }, | |
132 | returns => { type => 'null' }, | |
133 | ||
134 | code => sub { | |
135 | my ($param) = @_; | |
136 | ||
137 | # test if container exists on this node | |
67afe46e | 138 | my $conf = PVE::LXC::Config->load_config($param->{vmid}); |
9beee926 DM |
139 | |
140 | my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf); | |
141 | exec(@$cmd); | |
142 | }}); | |
143 | ||
144 | __PACKAGE__->register_method ({ | |
145 | name => 'enter', | |
146 | path => 'enter', | |
147 | method => 'GET', | |
148 | description => "Launch a shell for the specified container.", | |
149 | parameters => { | |
150 | additionalProperties => 0, | |
151 | properties => { | |
152 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }), | |
153 | }, | |
154 | }, | |
155 | returns => { type => 'null' }, | |
156 | ||
157 | code => sub { | |
158 | my ($param) = @_; | |
159 | ||
d4b3eead TL |
160 | my $vmid = $param->{vmid}; |
161 | ||
9beee926 | 162 | # test if container exists on this node |
67afe46e | 163 | PVE::LXC::Config->load_config($vmid); |
d4b3eead TL |
164 | |
165 | die "Error: container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid); | |
9beee926 | 166 | |
d4b3eead | 167 | exec('lxc-attach', '-n', $vmid); |
9beee926 DM |
168 | }}); |
169 | ||
170 | __PACKAGE__->register_method ({ | |
171 | name => 'exec', | |
172 | path => 'exec', | |
173 | method => 'GET', | |
174 | description => "Launch a command inside the specified container.", | |
175 | parameters => { | |
176 | additionalProperties => 0, | |
177 | properties => { | |
4b09527c | 178 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }), |
9beee926 DM |
179 | 'extra-args' => get_standard_option('extra-args'), |
180 | }, | |
181 | }, | |
182 | returns => { type => 'null' }, | |
183 | ||
184 | code => sub { | |
185 | my ($param) = @_; | |
186 | ||
187 | # test if container exists on this node | |
67afe46e | 188 | PVE::LXC::Config->load_config($param->{vmid}); |
9beee926 DM |
189 | |
190 | if (!@{$param->{'extra-args'}}) { | |
191 | die "missing command"; | |
192 | } | |
193 | exec('lxc-attach', '-n', $param->{vmid}, '--', @{$param->{'extra-args'}}); | |
194 | }}); | |
195 | ||
5ef9e3d1 | 196 | __PACKAGE__->register_method ({ |
f8327ed5 EK |
197 | name => 'fsck', |
198 | path => 'fsck', | |
199 | method => 'PUT', | |
5ef9e3d1 | 200 | description => "Run a filesystem check (fsck) on a container volume.", |
f8327ed5 EK |
201 | parameters => { |
202 | additionalProperties => 0, | |
203 | properties => { | |
204 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), | |
205 | force => { | |
206 | optional => 1, | |
207 | type => 'boolean', | |
208 | description => "Force checking, even if the filesystem seems clean", | |
209 | default => 0, | |
210 | }, | |
211 | device => { | |
212 | optional => 1, | |
213 | type => 'string', | |
214 | description => "A volume on which to run the filesystem check", | |
d250604f | 215 | enum => [PVE::LXC::Config->mountpoint_names()], |
f8327ed5 EK |
216 | }, |
217 | }, | |
218 | }, | |
219 | returns => { type => 'null' }, | |
220 | code => sub { | |
221 | ||
222 | my ($param) = @_; | |
223 | my $vmid = $param->{'vmid'}; | |
224 | my $device = defined($param->{'device'}) ? $param->{'device'} : 'rootfs'; | |
225 | ||
226 | my $command = ['fsck', '-a', '-l']; | |
227 | push(@$command, '-f') if $param->{force}; | |
228 | ||
229 | # critical path: all of this will be done while the container is locked | |
230 | my $do_fsck = sub { | |
231 | ||
67afe46e | 232 | my $conf = PVE::LXC::Config->load_config($vmid); |
f8327ed5 EK |
233 | my $storage_cfg = PVE::Storage::config(); |
234 | ||
235dbdf3 | 235 | defined($conf->{$device}) || die "cannot run command on non-existing mount point $device\n"; |
f8327ed5 | 236 | |
1b4cf758 FG |
237 | my $mount_point = $device eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$device}) : |
238 | PVE::LXC::Config->parse_ct_mountpoint($conf->{$device}); | |
44a9face | 239 | |
f8327ed5 EK |
240 | my $volid = $mount_point->{volume}; |
241 | ||
242 | my $path; | |
243 | my $storage_id = PVE::Storage::parse_volume_id($volid, 1); | |
244 | ||
245 | if ($storage_id) { | |
f8327ed5 | 246 | my (undef, undef, undef, undef, undef, undef, $format) = |
da9e487b | 247 | PVE::Storage::parse_volname($storage_cfg, $volid); |
f8327ed5 | 248 | |
da9e487b DM |
249 | die "unable to run fsck for '$volid' (format == $format)\n" |
250 | if $format ne 'raw'; | |
f8327ed5 EK |
251 | |
252 | $path = PVE::Storage::path($storage_cfg, $volid); | |
253 | ||
254 | } else { | |
da9e487b | 255 | if (($volid =~ m|^/.+|) && (-b $volid)) { |
f8327ed5 EK |
256 | # pass block devices directly |
257 | $path = $volid; | |
258 | } else { | |
da9e487b | 259 | die "path '$volid' does not point to a block device\n"; |
f8327ed5 EK |
260 | } |
261 | } | |
262 | ||
263 | push(@$command, $path); | |
264 | ||
da9e487b DM |
265 | PVE::LXC::check_running($vmid) && |
266 | die "cannot run fsck on active container\n"; | |
267 | ||
f8327ed5 EK |
268 | PVE::Tools::run_command($command); |
269 | }; | |
270 | ||
67afe46e | 271 | PVE::LXC::Config->lock_config($vmid, $do_fsck); |
f8327ed5 EK |
272 | return undef; |
273 | }}); | |
274 | ||
c93be6ed WB |
275 | __PACKAGE__->register_method({ |
276 | name => 'mount', | |
277 | path => 'mount', | |
278 | method => 'POST', | |
279 | description => "Mount the container's filesystem on the host. " . | |
280 | "This will hold a lock on the container and is meant for emergency maintenance only " . | |
281 | "as it will prevent further operations on the container other than start and stop.", | |
282 | parameters => { | |
283 | additionalProperties => 0, | |
284 | properties => { | |
285 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
286 | }, | |
287 | }, | |
288 | returns => { type => 'null' }, | |
289 | code => sub { | |
290 | my ($param) = @_; | |
291 | ||
292 | my $rpcenv = PVE::RPCEnvironment::get(); | |
293 | ||
294 | my $vmid = extract_param($param, 'vmid'); | |
295 | my $storecfg = PVE::Storage::config(); | |
67afe46e FG |
296 | PVE::LXC::Config->lock_config($vmid, sub { |
297 | my $conf = PVE::LXC::Config->set_lock($vmid, 'mounted'); | |
c93be6ed WB |
298 | PVE::LXC::mount_all($vmid, $storecfg, $conf); |
299 | }); | |
8632d8eb TL |
300 | |
301 | print "mounted CT $vmid in '/var/lib/lxc/$vmid/rootfs'\n"; | |
c93be6ed WB |
302 | return undef; |
303 | }}); | |
304 | ||
305 | __PACKAGE__->register_method({ | |
306 | name => 'unmount', | |
307 | path => 'unmount', | |
308 | method => 'POST', | |
309 | description => "Unmount the container's filesystem.", | |
310 | parameters => { | |
311 | additionalProperties => 0, | |
312 | properties => { | |
313 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
314 | }, | |
315 | }, | |
316 | returns => { type => 'null' }, | |
317 | code => sub { | |
318 | my ($param) = @_; | |
319 | ||
320 | my $rpcenv = PVE::RPCEnvironment::get(); | |
321 | ||
322 | my $vmid = extract_param($param, 'vmid'); | |
323 | my $storecfg = PVE::Storage::config(); | |
67afe46e FG |
324 | PVE::LXC::Config->lock_config($vmid, sub { |
325 | my $conf = PVE::LXC::Config->load_config($vmid); | |
c93be6ed | 326 | PVE::LXC::umount_all($vmid, $storecfg, $conf, 0); |
67afe46e | 327 | PVE::LXC::Config->remove_lock($vmid, 'mounted'); |
c93be6ed WB |
328 | }); |
329 | return undef; | |
330 | }}); | |
331 | ||
c6b59656 WB |
332 | __PACKAGE__->register_method({ |
333 | name => 'df', | |
334 | path => 'df', | |
335 | method => 'GET', | |
336 | description => "Get the container's current disk usage.", | |
337 | parameters => { | |
338 | additionalProperties => 0, | |
339 | properties => { | |
340 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
341 | }, | |
342 | }, | |
343 | returns => { type => 'null' }, | |
344 | code => sub { | |
345 | my ($param) = @_; | |
346 | ||
347 | my $rpcenv = PVE::RPCEnvironment::get(); | |
348 | ||
349 | # JSONSchema's format_size is exact, this uses floating point numbers | |
350 | my $format = sub { | |
351 | my ($size) = @_; | |
352 | return $size if $size < 1024.; | |
353 | $size /= 1024.; | |
354 | return sprintf('%.1fK', ${size}) if $size < 1024.; | |
355 | $size /= 1024.; | |
356 | return sprintf('%.1fM', ${size}) if $size < 1024.; | |
357 | $size /= 1024.; | |
358 | return sprintf('%.1fG', ${size}) if $size < 1024.; | |
359 | $size /= 1024.; | |
360 | return sprintf('%.1fT', ${size}) if $size < 1024.; | |
361 | }; | |
362 | ||
363 | my $vmid = extract_param($param, 'vmid'); | |
364 | PVE::LXC::Config->lock_config($vmid, sub { | |
365 | my $pid = eval { PVE::LXC::find_lxc_pid($vmid) }; | |
366 | my ($conf, $rootdir, $storecfg, $mounted); | |
367 | if ($@ || !$pid) { | |
368 | $conf = PVE::LXC::Config->set_lock($vmid, 'mounted'); | |
369 | $rootdir = "/var/lib/lxc/$vmid/rootfs"; | |
370 | $storecfg = PVE::Storage::config(); | |
371 | PVE::LXC::mount_all($vmid, $storecfg, $conf); | |
372 | $mounted = 1; | |
373 | } else { | |
374 | $conf = PVE::LXC::Config->load_config($vmid); | |
375 | $rootdir = "/proc/$pid/root"; | |
376 | } | |
377 | ||
378 | my @list = [qw(MP Volume Size Used Avail Use% Path)]; | |
379 | my @len = map { length($_) } @{$list[0]}; | |
380 | ||
381 | eval { | |
382 | PVE::LXC::Config->foreach_mountpoint($conf, sub { | |
383 | my ($name, $mp) = @_; | |
384 | my $path = $mp->{mp}; | |
385 | ||
386 | my $df = PVE::Tools::df("$rootdir/$path", 3); | |
387 | my $total = $format->($df->{total}); | |
388 | my $used = $format->($df->{used}); | |
389 | my $avail = $format->($df->{avail}); | |
390 | ||
391 | my $pc = sprintf('%.1f', $df->{used}/$df->{total}); | |
392 | ||
393 | my $entry = [ $name, $mp->{volume}, $total, $used, $avail, $pc, $path ]; | |
394 | push @list, $entry; | |
395 | ||
396 | foreach my $i (0..5) { | |
397 | $len[$i] = length($entry->[$i]) | |
398 | if $len[$i] < length($entry->[$i]); | |
399 | } | |
400 | }); | |
401 | ||
402 | my $format = "%-$len[0]s %-$len[1]s %$len[2]s %$len[3]s %$len[4]s %$len[5]s %s\n"; | |
403 | printf($format, @$_) foreach @list; | |
404 | }; | |
405 | warn $@ if $@; | |
406 | ||
407 | if ($mounted) { | |
408 | PVE::LXC::umount_all($vmid, $storecfg, $conf, 0); | |
409 | PVE::LXC::Config->remove_lock($vmid, 'mounted'); | |
410 | } | |
411 | }); | |
412 | return undef; | |
413 | }}); | |
414 | ||
b95bf64b WB |
415 | # File creation with specified ownership and permissions. |
416 | # User and group can be names or decimal numbers. | |
417 | # Permissions are explicit (not affected by umask) and can be numeric with the | |
418 | # usual 0/0x prefixes for octal/hex. | |
419 | sub create_file { | |
420 | my ($path, $perms, $user, $group) = @_; | |
421 | my ($uid, $gid); | |
422 | if (defined($user)) { | |
423 | if ($user =~ /^\d+$/) { | |
424 | $uid = int($user); | |
425 | } else { | |
426 | $uid = getpwnam($user) or die "failed to get uid for: $user\n" | |
427 | } | |
428 | } | |
429 | if (defined($group)) { | |
430 | if ($group =~ /^\d+$/) { | |
431 | $gid = int($group); | |
432 | } else { | |
433 | $gid = getgrnam($group) or die "failed to get gid for: $group\n" | |
434 | } | |
435 | } | |
436 | ||
437 | if (defined($perms)) { | |
438 | $! = 0; | |
439 | my ($mode, $unparsed) = POSIX::strtoul($perms, 0); | |
440 | die "invalid mode: '$perms'\n" if $perms eq '' || $unparsed > 0 || $!; | |
441 | $perms = $mode; | |
442 | } | |
443 | ||
444 | my $fd; | |
445 | if (sysopen($fd, $path, O_WRONLY | O_CREAT | O_EXCL, 0)) { | |
446 | $perms = 0666 & ~umask if !defined($perms); | |
447 | } else { | |
448 | # If the path previously existed then we do not care about left-over | |
449 | # file descriptors even if the permissions/ownership is changed. | |
450 | sysopen($fd, $path, O_WRONLY | O_CREAT | O_TRUNC) | |
451 | or die "failed to create file: $path: $!\n"; | |
452 | } | |
453 | ||
454 | my $trunc = 0; | |
455 | ||
456 | if (defined($perms)) { | |
457 | $trunc = 1; | |
458 | chmod($perms, $fd); | |
459 | } | |
460 | ||
461 | if (defined($uid) || defined($gid)) { | |
462 | $trunc = 1; | |
463 | my ($fuid, $fgid) = (stat($fd))[4,5] if !defined($uid) || !defined($gid); | |
464 | $uid = $fuid if !defined($uid); | |
465 | $gid = $fgid if !defined($gid); | |
466 | chown($uid, $gid, $fd) | |
467 | or die "failed to change file owner: $!\n"; | |
468 | } | |
469 | return $fd; | |
470 | } | |
471 | ||
472 | __PACKAGE__->register_method({ | |
473 | name => 'pull', | |
474 | path => 'pull', | |
475 | method => 'PUT', | |
476 | description => "Copy a file from the container to the local system.", | |
477 | parameters => { | |
478 | additionalProperties => 0, | |
479 | properties => { | |
480 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
481 | path => { | |
482 | type => 'string', | |
483 | description => "Path to a file inside the container to pull.", | |
484 | }, | |
485 | destination => { | |
486 | type => 'string', | |
487 | description => "Destination", | |
488 | }, | |
489 | user => { | |
490 | type => 'string', | |
491 | description => 'Owner user name or id.', | |
492 | optional => 1, | |
493 | }, | |
494 | group => { | |
495 | type => 'string', | |
496 | description => 'Owner group name or id.', | |
497 | optional => 1, | |
498 | }, | |
499 | perms => { | |
500 | type => 'string', | |
893e40de | 501 | description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).", |
b95bf64b WB |
502 | optional => 1, |
503 | }, | |
504 | }, | |
505 | }, | |
506 | returns => { | |
507 | type => 'string', | |
508 | description => "the task ID.", | |
509 | }, | |
510 | code => sub { | |
511 | my ($param) = @_; | |
512 | ||
513 | my $rpcenv = PVE::RPCEnvironment::get(); | |
514 | ||
515 | my $vmid = extract_param($param, 'vmid'); | |
516 | my $path = extract_param($param, 'path'); | |
517 | my $dest = extract_param($param, 'destination'); | |
518 | ||
519 | my $perms = extract_param($param, 'perms'); | |
fa0ab908 DM |
520 | # assume octal as default |
521 | $perms = "0$perms" if defined($perms) && $perms !~m/^0/; | |
b95bf64b WB |
522 | my $user = extract_param($param, 'user'); |
523 | my $group = extract_param($param, 'group'); | |
524 | ||
525 | my $code = sub { | |
526 | my $running = PVE::LXC::check_running($vmid); | |
527 | die "can only pull files from a running VM" if !$running; | |
528 | ||
529 | my $realcmd = sub { | |
530 | my $pid = PVE::LXC::find_lxc_pid($vmid); | |
531 | # Avoid symlink issues by opening the files from inside the | |
532 | # corresponding namespaces. | |
533 | my $destfd = create_file($dest, $perms, $user, $group); | |
534 | ||
535 | sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY | |
536 | or die "failed to open the container's mount namespace\n"; | |
537 | PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS) | |
538 | or die "failed to enter the container's mount namespace\n"; | |
539 | close($mntnsfd); | |
540 | chdir('/') or die "failed to change to container root directory\n"; | |
541 | ||
542 | open my $srcfd, '<', $path | |
543 | or die "failed to open $path: $!\n"; | |
544 | ||
545 | copy($srcfd, $destfd); | |
546 | }; | |
547 | ||
548 | # This avoids having to setns() back to our namespace. | |
549 | return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd); | |
550 | }; | |
551 | ||
67afe46e | 552 | return PVE::LXC::Config->lock_config($vmid, $code); |
b95bf64b WB |
553 | }}); |
554 | ||
555 | __PACKAGE__->register_method({ | |
556 | name => 'push', | |
557 | path => 'push', | |
558 | method => 'PUT', | |
559 | description => "Copy a local file to the container.", | |
560 | parameters => { | |
561 | additionalProperties => 0, | |
562 | properties => { | |
563 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
564 | file => { | |
565 | type => 'string', | |
566 | description => "Path to a local file.", | |
567 | }, | |
568 | destination => { | |
569 | type => 'string', | |
570 | description => "Destination inside the container to write to.", | |
571 | }, | |
572 | user => { | |
573 | type => 'string', | |
574 | description => 'Owner user name or id. When using a name it must exist inside the container.', | |
575 | optional => 1, | |
576 | }, | |
577 | group => { | |
578 | type => 'string', | |
579 | description => 'Owner group name or id. When using a name it must exist inside the container.', | |
580 | optional => 1, | |
581 | }, | |
582 | perms => { | |
583 | type => 'string', | |
893e40de | 584 | description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).", |
b95bf64b WB |
585 | optional => 1, |
586 | }, | |
587 | }, | |
588 | }, | |
589 | returns => { | |
590 | type => 'string', | |
591 | description => "the task ID.", | |
592 | }, | |
593 | code => sub { | |
594 | my ($param) = @_; | |
595 | ||
596 | my $rpcenv = PVE::RPCEnvironment::get(); | |
597 | ||
598 | my $vmid = extract_param($param, 'vmid'); | |
599 | my $file = extract_param($param, 'file'); | |
600 | my $dest = extract_param($param, 'destination'); | |
601 | ||
602 | my $perms = extract_param($param, 'perms'); | |
fa0ab908 DM |
603 | # assume octal as default |
604 | $perms = "0$perms" if defined($perms) && $perms !~m/^0/; | |
b95bf64b WB |
605 | my $user = extract_param($param, 'user'); |
606 | my $group = extract_param($param, 'group'); | |
607 | ||
608 | my $code = sub { | |
609 | my $running = PVE::LXC::check_running($vmid); | |
5a883bff | 610 | die "can only push files to a running CT\n" if !$running; |
b95bf64b | 611 | |
67afe46e | 612 | my $conf = PVE::LXC::Config->load_config($vmid); |
b95bf64b WB |
613 | my $unprivileged = $conf->{unprivileged}; |
614 | ||
615 | my $realcmd = sub { | |
616 | my $pid = PVE::LXC::find_lxc_pid($vmid); | |
617 | # We open the file then enter the container's mount - and for | |
618 | # unprivileged containers - user namespace and then create the | |
619 | # file. This avoids symlink attacks as a symlink cannot point | |
620 | # outside the namespace and our own access is equivalent to the | |
621 | # container-local's root user. Also the user-passed -user and | |
622 | # -group parameters will use the container-local's user and | |
623 | # group names. | |
624 | sysopen my $srcfd, $file, O_RDONLY | |
625 | or die "failed to open $file for reading\n"; | |
626 | ||
627 | sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY | |
628 | or die "failed to open the container's mount namespace\n"; | |
629 | my $usernsfd; | |
630 | if ($unprivileged) { | |
631 | sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY | |
632 | or die "failed to open the container's user namespace\n"; | |
633 | } | |
634 | ||
635 | PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS) | |
636 | or die "failed to enter the container's mount namespace\n"; | |
637 | close($mntnsfd); | |
638 | chdir('/') or die "failed to change to container root directory\n"; | |
639 | ||
640 | if ($unprivileged) { | |
641 | PVE::Tools::setns(fileno($usernsfd), PVE::Tools::CLONE_NEWUSER) | |
642 | or die "failed to enter the container's user namespace\n"; | |
643 | close($usernsfd); | |
644 | POSIX::setgid(0) or die "setgid failed: $!\n"; | |
645 | POSIX::setuid(0) or die "setuid failed: $!\n"; | |
646 | } | |
647 | ||
648 | my $destfd = create_file($dest, $perms, $user, $group); | |
649 | copy($srcfd, $destfd); | |
650 | }; | |
651 | ||
652 | # This avoids having to setns() back to our namespace. | |
653 | return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd); | |
654 | }; | |
655 | ||
67afe46e | 656 | return PVE::LXC::Config->lock_config($vmid, $code); |
b95bf64b WB |
657 | }}); |
658 | ||
c87b1505 DM |
659 | __PACKAGE__->register_method ({ |
660 | name => 'cpusets', | |
661 | path => 'cpusets', | |
662 | method => 'GET', | |
663 | description => "Print the list of assigned CPU sets.", | |
664 | parameters => { | |
665 | additionalProperties => 0, | |
666 | properties => {}, | |
667 | }, | |
668 | returns => { type => 'null'}, | |
669 | code => sub { | |
670 | my ($param) = @_; | |
671 | ||
672 | my $ctlist = PVE::LXC::config_list(); | |
673 | ||
674 | my $len = 0; | |
675 | my $id_len = 0; | |
676 | my $res = {}; | |
677 | ||
678 | foreach my $vmid (sort keys %$ctlist) { | |
679 | next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid"; | |
680 | ||
681 | my $cpuset = eval { PVE::CpuSet->new_from_cgroup("lxc/$vmid"); }; | |
682 | if (my $err = $@) { | |
683 | warn $err; | |
684 | next; | |
685 | } | |
686 | my @cpuset_members = $cpuset->members(); | |
687 | ||
688 | my $line = ': '; | |
689 | ||
690 | my $last = $cpuset_members[-1]; | |
691 | ||
692 | for (my $id = 0; $id <= $last; $id++) { | |
693 | my $empty = ' ' x length("$id"); | |
694 | $line .= ' ' . ($cpuset->has($id) ? $id : $empty); | |
695 | } | |
696 | $len = length($line) if length($line) > $len; | |
697 | $id_len = length($vmid) if length($vmid) > $id_len; | |
698 | ||
699 | $res->{$vmid} = $line; | |
700 | } | |
701 | ||
486fe362 | 702 | my @vmlist = sort keys %$res; |
c87b1505 | 703 | |
486fe362 DM |
704 | if (scalar(@vmlist)) { |
705 | my $header = '-' x ($len + $id_len) . "\n"; | |
706 | ||
707 | print $header; | |
708 | foreach my $vmid (@vmlist) { | |
709 | print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid}); | |
710 | } | |
711 | print $header; | |
712 | ||
713 | } else { | |
714 | print "no running containers\n"; | |
c87b1505 | 715 | } |
c87b1505 DM |
716 | |
717 | return undef; | |
718 | }}); | |
719 | ||
9beee926 DM |
720 | our $cmddef = { |
721 | list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub { | |
722 | my $res = shift; | |
723 | return if !scalar(@$res); | |
d0226204 WB |
724 | my $format = "%-10s %-10s %-12s %-20s\n"; |
725 | printf($format, 'VMID', 'Status', 'Lock', 'Name'); | |
9beee926 | 726 | foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) { |
d0226204 | 727 | printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name}); |
9beee926 DM |
728 | } |
729 | }], | |
730 | config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], | |
731 | { node => $nodename }, sub { | |
732 | my $config = shift; | |
733 | foreach my $k (sort (keys %$config)) { | |
734 | next if $k eq 'digest'; | |
8a778340 | 735 | next if $k eq 'lxc'; |
9beee926 DM |
736 | my $v = $config->{$k}; |
737 | if ($k eq 'description') { | |
738 | $v = PVE::Tools::encode_text($v); | |
739 | } | |
740 | print "$k: $v\n"; | |
741 | } | |
8a778340 FG |
742 | if (defined($config->{'lxc'})) { |
743 | my $lxc_list = $config->{'lxc'}; | |
744 | foreach my $lxc_opt (@$lxc_list) { | |
745 | print "$lxc_opt->[0]: $lxc_opt->[1]\n" | |
746 | } | |
747 | } | |
9beee926 DM |
748 | }], |
749 | set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }], | |
40626217 | 750 | |
985b18ed | 751 | resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], |
9beee926 DM |
752 | |
753 | create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ], | |
754 | restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ], | |
755 | ||
756 | start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit], | |
757 | suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit], | |
758 | resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit], | |
759 | shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit], | |
760 | stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit], | |
761 | ||
c4a33727 | 762 | clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], |
9beee926 DM |
763 | migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit], |
764 | ||
699ee9b2 | 765 | status => [ __PACKAGE__, 'status', ['vmid']], |
9beee926 DM |
766 | console => [ __PACKAGE__, 'console', ['vmid']], |
767 | enter => [ __PACKAGE__, 'enter', ['vmid']], | |
c72bd0ba | 768 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], |
9beee926 | 769 | exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']], |
f8327ed5 | 770 | fsck => [ __PACKAGE__, 'fsck', ['vmid']], |
b95bf64b | 771 | |
c93be6ed WB |
772 | mount => [ __PACKAGE__, 'mount', ['vmid']], |
773 | unmount => [ __PACKAGE__, 'unmount', ['vmid']], | |
b95bf64b WB |
774 | push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']], |
775 | pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']], | |
c6b59656 WB |
776 | |
777 | df => [ __PACKAGE__, 'df', ['vmid']], | |
778 | ||
9beee926 DM |
779 | destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], |
780 | { node => $nodename }, $upid_exit ], | |
781 | ||
782 | snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'], | |
783 | { node => $nodename } , $upid_exit ], | |
784 | ||
785 | delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
786 | ||
787 | listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, | |
788 | sub { | |
789 | my $res = shift; | |
790 | foreach my $e (@$res) { | |
791 | my $headline = $e->{description} || 'no-description'; | |
792 | $headline =~ s/\n.*//sg; | |
793 | my $parent = $e->{parent} // 'no-parent'; | |
794 | printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline); | |
795 | } | |
796 | }], | |
797 | ||
798 | rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
799 | ||
800 | template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }], | |
c87b1505 DM |
801 | |
802 | cpusets => [ __PACKAGE__, 'cpusets', []], | |
803 | ||
9beee926 DM |
804 | }; |
805 | ||
806 | ||
807 | 1; |