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