]> git.proxmox.com Git - pve-container.git/blame - src/PVE/CLI/pct.pm
Fix: fsck: rbd volume not mapped
[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;
23
9beee926
DM
24use base qw(PVE::CLIHandler);
25
26my $nodename = PVE::INotify::nodename();
27
28my $upid_exit = sub {
29 my $upid = shift;
30 my $status = PVE::Tools::upid_read_status($upid);
31 exit($status eq 'OK' ? 0 : -1);
32};
33
52e1f0cb
DM
34sub setup_environment {
35 PVE::RPCEnvironment->setup_default_cli_env();
36}
37
699ee9b2
DC
38__PACKAGE__->register_method ({
39 name => 'status',
40 path => 'status',
41 method => 'GET',
42 description => "Show CT status.",
43 parameters => {
44 additionalProperties => 0,
45 properties => {
46 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
47 verbose => {
48 description => "Verbose output format",
49 type => 'boolean',
50 optional => 1,
51 }
52 },
53 },
54 returns => { type => 'null'},
55 code => sub {
56 my ($param) = @_;
57
58 # test if CT exists
59 my $conf = PVE::LXC::Config->load_config ($param->{vmid});
60
61 my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
62 my $stat = $vmstatus->{$param->{vmid}};
63 if ($param->{verbose}) {
64 foreach my $k (sort (keys %$stat)) {
65 my $v = $stat->{$k};
66 next if !defined($v);
67 print "$k: $v\n";
68 }
69 } else {
70 my $status = $stat->{status} || 'unknown';
71 print "status: $status\n";
72 }
73
74 return undef;
75 }});
76
6085e45b 77sub param_mapping {
34ddbf08
FG
78 my ($name) = @_;
79
80 my $mapping = {
6085e45b
DC
81 'create_vm' => [
82 PVE::CLIHandler::get_standard_mapping('pve-password'),
83 'ssh-public-keys',
84 ],
34ddbf08
FG
85 };
86
6085e45b 87 return $mapping->{$name};
34ddbf08
FG
88}
89
c72bd0ba
WL
90__PACKAGE__->register_method ({
91 name => 'unlock',
92 path => 'unlock',
93 method => 'PUT',
94 description => "Unlock the VM.",
95 parameters => {
96 additionalProperties => 0,
97 properties => {
98 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
99 },
100 },
101 returns => { type => 'null'},
102 code => sub {
103 my ($param) = @_;
104
105 my $vmid = $param->{vmid};
106
ad408fe1 107 PVE::LXC::Config->remove_lock($vmid);
c72bd0ba
WL
108
109 return undef;
110 }});
111
9beee926
DM
112__PACKAGE__->register_method ({
113 name => 'console',
114 path => 'console',
115 method => 'GET',
116 description => "Launch a console for the specified container.",
117 parameters => {
118 additionalProperties => 0,
119 properties => {
120 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
65213b67
TM
121 escape => {
122 description => "Escape sequence prefix. For example to use <Ctrl+b q> as the escape sequence pass '^b'.",
123 default => '^a',
124 type => 'string',
125 pattern => '\^?[a-z]',
126 optional => 1,
127 },
9beee926
DM
128 },
129 },
130 returns => { type => 'null' },
131
132 code => sub {
133 my ($param) = @_;
134
135 # test if container exists on this node
67afe46e 136 my $conf = PVE::LXC::Config->load_config($param->{vmid});
9beee926 137
65213b67 138 my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf, $param->{escape});
9beee926
DM
139 exec(@$cmd);
140 }});
141
142__PACKAGE__->register_method ({
143 name => 'enter',
144 path => 'enter',
145 method => 'GET',
146 description => "Launch a shell for the specified container.",
147 parameters => {
148 additionalProperties => 0,
149 properties => {
150 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
151 },
152 },
153 returns => { type => 'null' },
154
155 code => sub {
156 my ($param) = @_;
157
d4b3eead
TL
158 my $vmid = $param->{vmid};
159
9beee926 160 # test if container exists on this node
67afe46e 161 PVE::LXC::Config->load_config($vmid);
d4b3eead
TL
162
163 die "Error: container '$vmid' not running!\n" if !PVE::LXC::check_running($vmid);
9beee926 164
d4b3eead 165 exec('lxc-attach', '-n', $vmid);
9beee926
DM
166 }});
167
168__PACKAGE__->register_method ({
169 name => 'exec',
170 path => 'exec',
171 method => 'GET',
172 description => "Launch a command inside the specified container.",
173 parameters => {
174 additionalProperties => 0,
175 properties => {
4b09527c 176 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
9beee926
DM
177 'extra-args' => get_standard_option('extra-args'),
178 },
179 },
180 returns => { type => 'null' },
181
182 code => sub {
183 my ($param) = @_;
184
185 # test if container exists on this node
67afe46e 186 PVE::LXC::Config->load_config($param->{vmid});
9beee926
DM
187
188 if (!@{$param->{'extra-args'}}) {
189 die "missing command";
190 }
191 exec('lxc-attach', '-n', $param->{vmid}, '--', @{$param->{'extra-args'}});
192 }});
193
5ef9e3d1 194__PACKAGE__->register_method ({
f8327ed5
EK
195 name => 'fsck',
196 path => 'fsck',
197 method => 'PUT',
5ef9e3d1 198 description => "Run a filesystem check (fsck) on a container volume.",
f8327ed5
EK
199 parameters => {
200 additionalProperties => 0,
201 properties => {
202 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
203 force => {
204 optional => 1,
205 type => 'boolean',
206 description => "Force checking, even if the filesystem seems clean",
207 default => 0,
208 },
209 device => {
210 optional => 1,
211 type => 'string',
212 description => "A volume on which to run the filesystem check",
d250604f 213 enum => [PVE::LXC::Config->mountpoint_names()],
f8327ed5
EK
214 },
215 },
216 },
217 returns => { type => 'null' },
218 code => sub {
219
220 my ($param) = @_;
221 my $vmid = $param->{'vmid'};
222 my $device = defined($param->{'device'}) ? $param->{'device'} : 'rootfs';
223
224 my $command = ['fsck', '-a', '-l'];
225 push(@$command, '-f') if $param->{force};
226
227 # critical path: all of this will be done while the container is locked
228 my $do_fsck = sub {
229
67afe46e 230 my $conf = PVE::LXC::Config->load_config($vmid);
f8327ed5
EK
231 my $storage_cfg = PVE::Storage::config();
232
235dbdf3 233 defined($conf->{$device}) || die "cannot run command on non-existing mount point $device\n";
f8327ed5 234
1b4cf758
FG
235 my $mount_point = $device eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$device}) :
236 PVE::LXC::Config->parse_ct_mountpoint($conf->{$device});
44a9face 237
f8327ed5
EK
238 my $volid = $mount_point->{volume};
239
240 my $path;
241 my $storage_id = PVE::Storage::parse_volume_id($volid, 1);
242
243 if ($storage_id) {
f8327ed5 244 my (undef, undef, undef, undef, undef, undef, $format) =
da9e487b 245 PVE::Storage::parse_volname($storage_cfg, $volid);
f8327ed5 246
da9e487b
DM
247 die "unable to run fsck for '$volid' (format == $format)\n"
248 if $format ne 'raw';
f8327ed5 249
bcf1163d 250 $path = PVE::Storage::map_volume($storage_cfg, $volid);
f8327ed5
EK
251
252 } else {
da9e487b 253 if (($volid =~ m|^/.+|) && (-b $volid)) {
f8327ed5
EK
254 # pass block devices directly
255 $path = $volid;
256 } else {
da9e487b 257 die "path '$volid' does not point to a block device\n";
f8327ed5
EK
258 }
259 }
260
261 push(@$command, $path);
262
da9e487b
DM
263 PVE::LXC::check_running($vmid) &&
264 die "cannot run fsck on active container\n";
265
f8327ed5 266 PVE::Tools::run_command($command);
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 {
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.
418sub 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');
e08ac1a0
OB
779 eval {
780 my $path = "";
a179d3a7 781 PVE::LXC::mount_all($vmid, $storecfg, $conf);
e08ac1a0
OB
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 };
a179d3a7 789 warn $@ if $@;
e08ac1a0
OB
790
791 PVE::LXC::umount_all($vmid, $storecfg, $conf, 0);
792 PVE::LXC::Config->remove_lock($vmid, 'fstrim');
793
794 return undef;
795 }});
796
9beee926
DM
797our $cmddef = {
798 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
799 my $res = shift;
800 return if !scalar(@$res);
d0226204
WB
801 my $format = "%-10s %-10s %-12s %-20s\n";
802 printf($format, 'VMID', 'Status', 'Lock', 'Name');
9beee926 803 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
d0226204 804 printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name});
9beee926
DM
805 }
806 }],
807 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
808 { node => $nodename }, sub {
809 my $config = shift;
810 foreach my $k (sort (keys %$config)) {
811 next if $k eq 'digest';
8a778340 812 next if $k eq 'lxc';
9beee926
DM
813 my $v = $config->{$k};
814 if ($k eq 'description') {
815 $v = PVE::Tools::encode_text($v);
816 }
817 print "$k: $v\n";
818 }
8a778340
FG
819 if (defined($config->{'lxc'})) {
820 my $lxc_list = $config->{'lxc'};
821 foreach my $lxc_opt (@$lxc_list) {
822 print "$lxc_opt->[0]: $lxc_opt->[1]\n"
823 }
824 }
9beee926 825 }],
f9fc67e9
OB
826
827 pending => [ "PVE::API2::LXC", "vm_pending", ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::format_pending ],
9beee926 828 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
40626217 829
985b18ed 830 resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
9beee926
DM
831
832 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
833 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
834
835 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
836 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
837 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
838 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
839 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
f5fe1125
OB
840 reboot => [ 'PVE::API2::LXC::Status', 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit],
841
c4a33727 842 clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
9beee926 843 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
3c0f6806 844 move_volume => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage'], { node => $nodename }, $upid_exit ],
9beee926 845
699ee9b2 846 status => [ __PACKAGE__, 'status', ['vmid']],
9beee926
DM
847 console => [ __PACKAGE__, 'console', ['vmid']],
848 enter => [ __PACKAGE__, 'enter', ['vmid']],
c72bd0ba 849 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
9beee926 850 exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
f8327ed5 851 fsck => [ __PACKAGE__, 'fsck', ['vmid']],
b95bf64b 852
c93be6ed
WB
853 mount => [ __PACKAGE__, 'mount', ['vmid']],
854 unmount => [ __PACKAGE__, 'unmount', ['vmid']],
b95bf64b
WB
855 push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']],
856 pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],
c6b59656
WB
857
858 df => [ __PACKAGE__, 'df', ['vmid']],
6827e44d 859 rescan => [ __PACKAGE__, 'rescan', []],
c6b59656 860
9beee926
DM
861 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
862 { node => $nodename }, $upid_exit ],
863
864 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
865 { node => $nodename } , $upid_exit ],
866
867 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
868
3ac73b44 869 listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::print_snapshot_tree ],
9beee926
DM
870
871 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
872
873 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
c87b1505
DM
874
875 cpusets => [ __PACKAGE__, 'cpusets', []],
876
e08ac1a0
OB
877 fstrim => [ __PACKAGE__, 'fstrim', ['vmid']],
878
9beee926
DM
879};
880
881
8821;