]> git.proxmox.com Git - pve-container.git/blame - src/PVE/CLI/pct.pm
pct: use CGroup submodule and new CpuSet methods
[pve-container.git] / src / PVE / CLI / pct.pm
CommitLineData
9beee926 1package PVE::CLI::pct;
179b842e 2
9beee926
DM
3use strict;
4use warnings;
5
b95bf64b
WB
6use POSIX;
7use Fcntl;
8use File::Copy 'copy';
9
f9fc67e9 10use PVE::GuestHelpers;
9beee926
DM
11use PVE::SafeSyslog;
12use PVE::Tools qw(extract_param);
c87b1505 13use PVE::CpuSet;
9beee926
DM
14use PVE::Cluster;
15use PVE::INotify;
16use PVE::RPCEnvironment;
17use PVE::JSONSchema qw(get_standard_option);
18use PVE::CLIHandler;
19use PVE::API2::LXC;
20use PVE::API2::LXC::Config;
21use PVE::API2::LXC::Status;
22use PVE::API2::LXC::Snapshot;
fdc5f2ad 23use PVE::LXC::CGroup;
9beee926 24
9beee926
DM
25use base qw(PVE::CLIHandler);
26
27my $nodename = PVE::INotify::nodename();
28
29my $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
35sub 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 78sub 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
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
d8cde2e8
TL
239 die "cannot run fsck when container is running\n"
240 if PVE::LXC::check_running($vmid);
241
f8327ed5
EK
242 my $volid = $mount_point->{volume};
243
244 my $path;
245 my $storage_id = PVE::Storage::parse_volume_id($volid, 1);
246
247 if ($storage_id) {
f8327ed5 248 my (undef, undef, undef, undef, undef, undef, $format) =
da9e487b 249 PVE::Storage::parse_volname($storage_cfg, $volid);
f8327ed5 250
da9e487b
DM
251 die "unable to run fsck for '$volid' (format == $format)\n"
252 if $format ne 'raw';
f8327ed5 253
bcf1163d 254 $path = PVE::Storage::map_volume($storage_cfg, $volid);
f8327ed5
EK
255
256 } else {
da9e487b 257 if (($volid =~ m|^/.+|) && (-b $volid)) {
f8327ed5
EK
258 # pass block devices directly
259 $path = $volid;
260 } else {
da9e487b 261 die "path '$volid' does not point to a block device\n";
f8327ed5
EK
262 }
263 }
264
265 push(@$command, $path);
f8327ed5 266 PVE::Tools::run_command($command);
d8cde2e8 267
bcf1163d 268 PVE::Storage::unmap_volume($storage_cfg, $volid) if $storage_id;
f8327ed5
EK
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.
419sub 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
6827e44d
AA
472__PACKAGE__->register_method ({
473 name => 'rescan',
474 path => 'rescan',
475 method => 'POST',
476 description => "Rescan all storages and update disk sizes and unused disk images.",
477 parameters => {
478 additionalProperties => 0,
479 properties => {
480 vmid => get_standard_option('pve-vmid', {
481 optional => 1,
482 completion => \&PVE::LXC::complete_ctid,
483 }),
484 dryrun => {
485 type => 'boolean',
486 optional => 1,
487 default => 0,
488 description => 'Do not actually write changes out to conifg.',
489 },
490 },
491 },
492 returns => { type => 'null'},
493 code => sub {
494 my ($param) = @_;
495
496 my $dryrun = $param->{dryrun};
497
498 print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun;
499
500 PVE::LXC::rescan($param->{vmid}, 0, $dryrun);
501
502 return undef;
503 }});
504
b95bf64b
WB
505__PACKAGE__->register_method({
506 name => 'pull',
507 path => 'pull',
508 method => 'PUT',
509 description => "Copy a file from the container to the local system.",
510 parameters => {
511 additionalProperties => 0,
512 properties => {
513 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
514 path => {
515 type => 'string',
516 description => "Path to a file inside the container to pull.",
517 },
518 destination => {
519 type => 'string',
520 description => "Destination",
521 },
522 user => {
523 type => 'string',
524 description => 'Owner user name or id.',
525 optional => 1,
526 },
527 group => {
528 type => 'string',
529 description => 'Owner group name or id.',
530 optional => 1,
531 },
532 perms => {
533 type => 'string',
893e40de 534 description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
b95bf64b
WB
535 optional => 1,
536 },
537 },
538 },
539 returns => {
540 type => 'string',
541 description => "the task ID.",
542 },
543 code => sub {
544 my ($param) = @_;
545
546 my $rpcenv = PVE::RPCEnvironment::get();
547
548 my $vmid = extract_param($param, 'vmid');
549 my $path = extract_param($param, 'path');
550 my $dest = extract_param($param, 'destination');
551
552 my $perms = extract_param($param, 'perms');
fa0ab908
DM
553 # assume octal as default
554 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
b95bf64b
WB
555 my $user = extract_param($param, 'user');
556 my $group = extract_param($param, 'group');
557
558 my $code = sub {
559 my $running = PVE::LXC::check_running($vmid);
560 die "can only pull files from a running VM" if !$running;
561
562 my $realcmd = sub {
563 my $pid = PVE::LXC::find_lxc_pid($vmid);
564 # Avoid symlink issues by opening the files from inside the
565 # corresponding namespaces.
566 my $destfd = create_file($dest, $perms, $user, $group);
567
568 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
569 or die "failed to open the container's mount namespace\n";
570 PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS)
571 or die "failed to enter the container's mount namespace\n";
572 close($mntnsfd);
573 chdir('/') or die "failed to change to container root directory\n";
574
575 open my $srcfd, '<', $path
576 or die "failed to open $path: $!\n";
577
578 copy($srcfd, $destfd);
579 };
580
581 # This avoids having to setns() back to our namespace.
582 return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
583 };
584
67afe46e 585 return PVE::LXC::Config->lock_config($vmid, $code);
b95bf64b
WB
586 }});
587
588__PACKAGE__->register_method({
589 name => 'push',
590 path => 'push',
591 method => 'PUT',
592 description => "Copy a local file to the container.",
593 parameters => {
594 additionalProperties => 0,
595 properties => {
596 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
597 file => {
598 type => 'string',
599 description => "Path to a local file.",
600 },
601 destination => {
602 type => 'string',
603 description => "Destination inside the container to write to.",
604 },
605 user => {
606 type => 'string',
607 description => 'Owner user name or id. When using a name it must exist inside the container.',
608 optional => 1,
609 },
610 group => {
611 type => 'string',
612 description => 'Owner group name or id. When using a name it must exist inside the container.',
613 optional => 1,
614 },
615 perms => {
616 type => 'string',
893e40de 617 description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
b95bf64b
WB
618 optional => 1,
619 },
620 },
621 },
622 returns => {
623 type => 'string',
624 description => "the task ID.",
625 },
626 code => sub {
627 my ($param) = @_;
628
629 my $rpcenv = PVE::RPCEnvironment::get();
630
631 my $vmid = extract_param($param, 'vmid');
632 my $file = extract_param($param, 'file');
633 my $dest = extract_param($param, 'destination');
634
635 my $perms = extract_param($param, 'perms');
fa0ab908
DM
636 # assume octal as default
637 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
b95bf64b
WB
638 my $user = extract_param($param, 'user');
639 my $group = extract_param($param, 'group');
640
641 my $code = sub {
642 my $running = PVE::LXC::check_running($vmid);
5a883bff 643 die "can only push files to a running CT\n" if !$running;
b95bf64b 644
67afe46e 645 my $conf = PVE::LXC::Config->load_config($vmid);
b95bf64b
WB
646 my $unprivileged = $conf->{unprivileged};
647
648 my $realcmd = sub {
649 my $pid = PVE::LXC::find_lxc_pid($vmid);
650 # We open the file then enter the container's mount - and for
651 # unprivileged containers - user namespace and then create the
652 # file. This avoids symlink attacks as a symlink cannot point
653 # outside the namespace and our own access is equivalent to the
654 # container-local's root user. Also the user-passed -user and
655 # -group parameters will use the container-local's user and
656 # group names.
657 sysopen my $srcfd, $file, O_RDONLY
658 or die "failed to open $file for reading\n";
659
660 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
661 or die "failed to open the container's mount namespace\n";
662 my $usernsfd;
663 if ($unprivileged) {
664 sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY
665 or die "failed to open the container's user namespace\n";
666 }
667
668 PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS)
669 or die "failed to enter the container's mount namespace\n";
670 close($mntnsfd);
671 chdir('/') or die "failed to change to container root directory\n";
672
673 if ($unprivileged) {
674 PVE::Tools::setns(fileno($usernsfd), PVE::Tools::CLONE_NEWUSER)
675 or die "failed to enter the container's user namespace\n";
676 close($usernsfd);
677 POSIX::setgid(0) or die "setgid failed: $!\n";
678 POSIX::setuid(0) or die "setuid failed: $!\n";
679 }
680
681 my $destfd = create_file($dest, $perms, $user, $group);
682 copy($srcfd, $destfd);
683 };
684
685 # This avoids having to setns() back to our namespace.
686 return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
687 };
688
67afe46e 689 return PVE::LXC::Config->lock_config($vmid, $code);
b95bf64b
WB
690 }});
691
c87b1505
DM
692__PACKAGE__->register_method ({
693 name => 'cpusets',
694 path => 'cpusets',
695 method => 'GET',
696 description => "Print the list of assigned CPU sets.",
697 parameters => {
698 additionalProperties => 0,
699 properties => {},
700 },
701 returns => { type => 'null'},
702 code => sub {
703 my ($param) = @_;
704
705 my $ctlist = PVE::LXC::config_list();
706
707 my $len = 0;
708 my $id_len = 0;
709 my $res = {};
710
711 foreach my $vmid (sort keys %$ctlist) {
fdc5f2ad 712 my $cgroup = PVE::LXC::CGroup->new($vmid);
c87b1505 713
fdc5f2ad
WB
714 my ($cpuset, $path);
715 if (defined($path = $cgroup->get_path('cpuset'))) {
716 $cpuset = eval { PVE::CpuSet->new_from_path($path); };
717 } elsif (defined($path = $cgroup->get_path())) {
718 $cpuset = eval { PVE::CpuSet->new_from_path($path); };
719 } else {
720 # Container not running.
721 next;
722 }
c87b1505
DM
723 if (my $err = $@) {
724 warn $err;
725 next;
726 }
fdc5f2ad 727
c87b1505
DM
728 my @cpuset_members = $cpuset->members();
729
730 my $line = ': ';
731
732 my $last = $cpuset_members[-1];
733
734 for (my $id = 0; $id <= $last; $id++) {
735 my $empty = ' ' x length("$id");
736 $line .= ' ' . ($cpuset->has($id) ? $id : $empty);
737 }
738 $len = length($line) if length($line) > $len;
739 $id_len = length($vmid) if length($vmid) > $id_len;
740
741 $res->{$vmid} = $line;
742 }
743
486fe362 744 my @vmlist = sort keys %$res;
c87b1505 745
486fe362
DM
746 if (scalar(@vmlist)) {
747 my $header = '-' x ($len + $id_len) . "\n";
748
749 print $header;
750 foreach my $vmid (@vmlist) {
751 print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid});
752 }
753 print $header;
754
755 } else {
756 print "no running containers\n";
c87b1505 757 }
c87b1505
DM
758
759 return undef;
760 }});
761
e08ac1a0
OB
762__PACKAGE__->register_method ({
763 name => 'fstrim',
764 path => 'fstrim',
765 method => 'POST',
766 description => "Run fstrim on a chosen CT and its mountpoints.",
767 parameters => {
768 additionalProperties => 0,
769 properties => {
770 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
771 },
772 },
773 returns => { type => 'null' },
774 code => sub {
775
776 my ($param) = @_;
777 my $vmid = $param->{'vmid'};
778
779 my $rootdir = "/var/lib/lxc/$vmid/rootfs";
780
781 my $storecfg = PVE::Storage::config();
782 my $conf = PVE::LXC::Config->set_lock($vmid, 'fstrim');
e08ac1a0
OB
783 eval {
784 my $path = "";
a179d3a7 785 PVE::LXC::mount_all($vmid, $storecfg, $conf);
e08ac1a0
OB
786 PVE::LXC::Config->foreach_mountpoint($conf, sub {
787 my ($name, $mp) = @_;
788 $path = $mp->{mp};
789 my $cmd = ["fstrim", "-v", "$rootdir$path"];
790 PVE::Tools::run_command($cmd);
791 });
792 };
a179d3a7 793 warn $@ if $@;
e08ac1a0
OB
794
795 PVE::LXC::umount_all($vmid, $storecfg, $conf, 0);
796 PVE::LXC::Config->remove_lock($vmid, 'fstrim');
797
798 return undef;
799 }});
800
9beee926
DM
801our $cmddef = {
802 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
803 my $res = shift;
804 return if !scalar(@$res);
d0226204
WB
805 my $format = "%-10s %-10s %-12s %-20s\n";
806 printf($format, 'VMID', 'Status', 'Lock', 'Name');
9beee926 807 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
d0226204 808 printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name});
9beee926
DM
809 }
810 }],
811 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
812 { node => $nodename }, sub {
813 my $config = shift;
814 foreach my $k (sort (keys %$config)) {
815 next if $k eq 'digest';
8a778340 816 next if $k eq 'lxc';
9beee926
DM
817 my $v = $config->{$k};
818 if ($k eq 'description') {
819 $v = PVE::Tools::encode_text($v);
820 }
821 print "$k: $v\n";
822 }
8a778340
FG
823 if (defined($config->{'lxc'})) {
824 my $lxc_list = $config->{'lxc'};
825 foreach my $lxc_opt (@$lxc_list) {
826 print "$lxc_opt->[0]: $lxc_opt->[1]\n"
827 }
828 }
9beee926 829 }],
f9fc67e9
OB
830
831 pending => [ "PVE::API2::LXC", "vm_pending", ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::format_pending ],
9beee926 832 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
40626217 833
985b18ed 834 resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
9beee926
DM
835
836 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
837 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
838
839 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
840 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
841 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
842 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
843 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
f5fe1125
OB
844 reboot => [ 'PVE::API2::LXC::Status', 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit],
845
c4a33727 846 clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
9beee926 847 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
3c0f6806 848 move_volume => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage'], { node => $nodename }, $upid_exit ],
9beee926 849
699ee9b2 850 status => [ __PACKAGE__, 'status', ['vmid']],
9beee926
DM
851 console => [ __PACKAGE__, 'console', ['vmid']],
852 enter => [ __PACKAGE__, 'enter', ['vmid']],
c72bd0ba 853 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
9beee926 854 exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
f8327ed5 855 fsck => [ __PACKAGE__, 'fsck', ['vmid']],
b95bf64b 856
c93be6ed
WB
857 mount => [ __PACKAGE__, 'mount', ['vmid']],
858 unmount => [ __PACKAGE__, 'unmount', ['vmid']],
b95bf64b
WB
859 push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']],
860 pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],
c6b59656
WB
861
862 df => [ __PACKAGE__, 'df', ['vmid']],
6827e44d 863 rescan => [ __PACKAGE__, 'rescan', []],
c6b59656 864
9beee926
DM
865 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
866 { node => $nodename }, $upid_exit ],
867
868 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
869 { node => $nodename } , $upid_exit ],
870
871 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
872
3ac73b44 873 listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::print_snapshot_tree ],
9beee926
DM
874
875 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
876
877 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
c87b1505
DM
878
879 cpusets => [ __PACKAGE__, 'cpusets', []],
880
e08ac1a0
OB
881 fstrim => [ __PACKAGE__, 'fstrim', ['vmid']],
882
9beee926
DM
883};
884
885
8861;