]>
Commit | Line | Data |
---|---|---|
9beee926 | 1 | package PVE::CLI::pct; |
179b842e | 2 | |
9beee926 DM |
3 | use strict; |
4 | use warnings; | |
5 | ||
b95bf64b WB |
6 | use POSIX; |
7 | use Fcntl; | |
8 | use File::Copy 'copy'; | |
9 | ||
f9fc67e9 | 10 | use PVE::GuestHelpers; |
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; | |
fdc5f2ad | 23 | use PVE::LXC::CGroup; |
9beee926 | 24 | |
9beee926 DM |
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", | |
5e5d76cf | 214 | enum => [PVE::LXC::Config->valid_volume_keys()], |
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 | |
e4034859 | 236 | my $mount_point = PVE::LXC::Config->parse_volume($device, $conf->{$device}); |
44a9face | 237 | |
d8cde2e8 TL |
238 | die "cannot run fsck when container is running\n" |
239 | if PVE::LXC::check_running($vmid); | |
240 | ||
f8327ed5 EK |
241 | my $volid = $mount_point->{volume}; |
242 | ||
243 | my $path; | |
244 | my $storage_id = PVE::Storage::parse_volume_id($volid, 1); | |
245 | ||
246 | if ($storage_id) { | |
f8327ed5 | 247 | my (undef, undef, undef, undef, undef, undef, $format) = |
da9e487b | 248 | PVE::Storage::parse_volname($storage_cfg, $volid); |
f8327ed5 | 249 | |
da9e487b DM |
250 | die "unable to run fsck for '$volid' (format == $format)\n" |
251 | if $format ne 'raw'; | |
f8327ed5 | 252 | |
bcf1163d | 253 | $path = PVE::Storage::map_volume($storage_cfg, $volid); |
f8327ed5 EK |
254 | |
255 | } else { | |
da9e487b | 256 | if (($volid =~ m|^/.+|) && (-b $volid)) { |
f8327ed5 EK |
257 | # pass block devices directly |
258 | $path = $volid; | |
259 | } else { | |
da9e487b | 260 | die "path '$volid' does not point to a block device\n"; |
f8327ed5 EK |
261 | } |
262 | } | |
263 | ||
264 | push(@$command, $path); | |
f8327ed5 | 265 | PVE::Tools::run_command($command); |
d8cde2e8 | 266 | |
bcf1163d | 267 | PVE::Storage::unmap_volume($storage_cfg, $volid) if $storage_id; |
f8327ed5 EK |
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 { | |
015740e6 | 381 | PVE::LXC::Config->foreach_volume($conf, sub { |
c6b59656 WB |
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 | ||
704 | my $ctlist = PVE::LXC::config_list(); | |
705 | ||
706 | my $len = 0; | |
707 | my $id_len = 0; | |
708 | my $res = {}; | |
709 | ||
710 | foreach my $vmid (sort keys %$ctlist) { | |
fdc5f2ad | 711 | my $cgroup = PVE::LXC::CGroup->new($vmid); |
c87b1505 | 712 | |
fdc5f2ad WB |
713 | my ($cpuset, $path); |
714 | if (defined($path = $cgroup->get_path('cpuset'))) { | |
715 | $cpuset = eval { PVE::CpuSet->new_from_path($path); }; | |
716 | } elsif (defined($path = $cgroup->get_path())) { | |
717 | $cpuset = eval { PVE::CpuSet->new_from_path($path); }; | |
718 | } else { | |
719 | # Container not running. | |
720 | next; | |
721 | } | |
c87b1505 DM |
722 | if (my $err = $@) { |
723 | warn $err; | |
724 | next; | |
725 | } | |
fdc5f2ad | 726 | |
c87b1505 DM |
727 | my @cpuset_members = $cpuset->members(); |
728 | ||
729 | my $line = ': '; | |
730 | ||
731 | my $last = $cpuset_members[-1]; | |
732 | ||
733 | for (my $id = 0; $id <= $last; $id++) { | |
734 | my $empty = ' ' x length("$id"); | |
735 | $line .= ' ' . ($cpuset->has($id) ? $id : $empty); | |
736 | } | |
737 | $len = length($line) if length($line) > $len; | |
738 | $id_len = length($vmid) if length($vmid) > $id_len; | |
739 | ||
740 | $res->{$vmid} = $line; | |
741 | } | |
742 | ||
486fe362 | 743 | my @vmlist = sort keys %$res; |
c87b1505 | 744 | |
486fe362 DM |
745 | if (scalar(@vmlist)) { |
746 | my $header = '-' x ($len + $id_len) . "\n"; | |
747 | ||
748 | print $header; | |
749 | foreach my $vmid (@vmlist) { | |
750 | print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid}); | |
751 | } | |
752 | print $header; | |
753 | ||
754 | } else { | |
755 | print "no running containers\n"; | |
c87b1505 | 756 | } |
c87b1505 DM |
757 | |
758 | return undef; | |
759 | }}); | |
760 | ||
e08ac1a0 OB |
761 | __PACKAGE__->register_method ({ |
762 | name => 'fstrim', | |
763 | path => 'fstrim', | |
764 | method => 'POST', | |
765 | description => "Run fstrim on a chosen CT and its mountpoints.", | |
766 | parameters => { | |
767 | additionalProperties => 0, | |
768 | properties => { | |
769 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }), | |
4590b2e4 | 770 | 'ignore-mountpoints' => { |
8a4db933 | 771 | description => 'Skip all mountpoints, only do fstrim on the container root.', |
4590b2e4 OB |
772 | optional => 1, |
773 | type => 'boolean', | |
774 | }, | |
e08ac1a0 OB |
775 | }, |
776 | }, | |
777 | returns => { type => 'null' }, | |
778 | code => sub { | |
779 | ||
780 | my ($param) = @_; | |
781 | my $vmid = $param->{'vmid'}; | |
782 | ||
783 | my $rootdir = "/var/lib/lxc/$vmid/rootfs"; | |
784 | ||
785 | my $storecfg = PVE::Storage::config(); | |
786 | my $conf = PVE::LXC::Config->set_lock($vmid, 'fstrim'); | |
e08ac1a0 OB |
787 | eval { |
788 | my $path = ""; | |
a179d3a7 | 789 | PVE::LXC::mount_all($vmid, $storecfg, $conf); |
015740e6 | 790 | PVE::LXC::Config->foreach_volume($conf, sub { |
e08ac1a0 OB |
791 | my ($name, $mp) = @_; |
792 | $path = $mp->{mp}; | |
4590b2e4 | 793 | return if $param->{'ignore-mountpoints'} && $name =~ /^mp\d+/; |
e08ac1a0 OB |
794 | my $cmd = ["fstrim", "-v", "$rootdir$path"]; |
795 | PVE::Tools::run_command($cmd); | |
796 | }); | |
797 | }; | |
a179d3a7 | 798 | warn $@ if $@; |
e08ac1a0 OB |
799 | |
800 | PVE::LXC::umount_all($vmid, $storecfg, $conf, 0); | |
801 | PVE::LXC::Config->remove_lock($vmid, 'fstrim'); | |
802 | ||
803 | return undef; | |
804 | }}); | |
805 | ||
9beee926 DM |
806 | our $cmddef = { |
807 | list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub { | |
808 | my $res = shift; | |
809 | return if !scalar(@$res); | |
d0226204 WB |
810 | my $format = "%-10s %-10s %-12s %-20s\n"; |
811 | printf($format, 'VMID', 'Status', 'Lock', 'Name'); | |
9beee926 | 812 | foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) { |
d0226204 | 813 | printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name}); |
9beee926 DM |
814 | } |
815 | }], | |
816 | config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], | |
817 | { node => $nodename }, sub { | |
818 | my $config = shift; | |
819 | foreach my $k (sort (keys %$config)) { | |
820 | next if $k eq 'digest'; | |
8a778340 | 821 | next if $k eq 'lxc'; |
9beee926 DM |
822 | my $v = $config->{$k}; |
823 | if ($k eq 'description') { | |
824 | $v = PVE::Tools::encode_text($v); | |
825 | } | |
826 | print "$k: $v\n"; | |
827 | } | |
8a778340 FG |
828 | if (defined($config->{'lxc'})) { |
829 | my $lxc_list = $config->{'lxc'}; | |
830 | foreach my $lxc_opt (@$lxc_list) { | |
831 | print "$lxc_opt->[0]: $lxc_opt->[1]\n" | |
832 | } | |
833 | } | |
9beee926 | 834 | }], |
f9fc67e9 OB |
835 | |
836 | pending => [ "PVE::API2::LXC", "vm_pending", ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::format_pending ], | |
9beee926 | 837 | set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }], |
40626217 | 838 | |
985b18ed | 839 | resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], |
721b6d63 | 840 | |
9beee926 DM |
841 | create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ], |
842 | restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ], | |
721b6d63 | 843 | destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], |
9beee926 DM |
844 | |
845 | start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit], | |
846 | suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit], | |
847 | resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit], | |
848 | shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit], | |
849 | stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit], | |
f5fe1125 OB |
850 | reboot => [ 'PVE::API2::LXC::Status', 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit], |
851 | ||
c4a33727 | 852 | clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], |
9beee926 | 853 | migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit], |
3c0f6806 | 854 | move_volume => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage'], { node => $nodename }, $upid_exit ], |
721b6d63 TL |
855 | |
856 | snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
857 | delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
858 | listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::print_snapshot_tree ], | |
859 | rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
860 | template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }], | |
861 | ||
699ee9b2 | 862 | status => [ __PACKAGE__, 'status', ['vmid']], |
9beee926 DM |
863 | console => [ __PACKAGE__, 'console', ['vmid']], |
864 | enter => [ __PACKAGE__, 'enter', ['vmid']], | |
c72bd0ba | 865 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], |
9beee926 | 866 | exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']], |
f8327ed5 | 867 | fsck => [ __PACKAGE__, 'fsck', ['vmid']], |
b95bf64b | 868 | |
c93be6ed WB |
869 | mount => [ __PACKAGE__, 'mount', ['vmid']], |
870 | unmount => [ __PACKAGE__, 'unmount', ['vmid']], | |
b95bf64b WB |
871 | push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']], |
872 | pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']], | |
c6b59656 WB |
873 | |
874 | df => [ __PACKAGE__, 'df', ['vmid']], | |
6827e44d | 875 | rescan => [ __PACKAGE__, 'rescan', []], |
c87b1505 | 876 | cpusets => [ __PACKAGE__, 'cpusets', []], |
e08ac1a0 | 877 | fstrim => [ __PACKAGE__, 'fstrim', ['vmid']], |
9beee926 DM |
878 | }; |
879 | ||
880 | ||
881 | 1; |