]> git.proxmox.com Git - pve-container.git/blame - src/PVE/CLI/pct.pm
close #1940: pct console: added ability to specify escape sequence
[pve-container.git] / src / PVE / CLI / pct.pm
CommitLineData
9beee926
DM
1package PVE::CLI::pct;
2
3use strict;
4use warnings;
5
b95bf64b
WB
6use POSIX;
7use Fcntl;
8use File::Copy 'copy';
9
9beee926
DM
10use PVE::SafeSyslog;
11use PVE::Tools qw(extract_param);
c87b1505 12use PVE::CpuSet;
9beee926
DM
13use PVE::Cluster;
14use PVE::INotify;
15use PVE::RPCEnvironment;
16use PVE::JSONSchema qw(get_standard_option);
17use PVE::CLIHandler;
18use PVE::API2::LXC;
19use PVE::API2::LXC::Config;
20use PVE::API2::LXC::Status;
21use PVE::API2::LXC::Snapshot;
22
23use Data::Dumper;
24
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",
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.
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
471__PACKAGE__->register_method({
472 name => 'pull',
473 path => 'pull',
474 method => 'PUT',
475 description => "Copy a file from the container to the local system.",
476 parameters => {
477 additionalProperties => 0,
478 properties => {
479 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
480 path => {
481 type => 'string',
482 description => "Path to a file inside the container to pull.",
483 },
484 destination => {
485 type => 'string',
486 description => "Destination",
487 },
488 user => {
489 type => 'string',
490 description => 'Owner user name or id.',
491 optional => 1,
492 },
493 group => {
494 type => 'string',
495 description => 'Owner group name or id.',
496 optional => 1,
497 },
498 perms => {
499 type => 'string',
893e40de 500 description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
b95bf64b
WB
501 optional => 1,
502 },
503 },
504 },
505 returns => {
506 type => 'string',
507 description => "the task ID.",
508 },
509 code => sub {
510 my ($param) = @_;
511
512 my $rpcenv = PVE::RPCEnvironment::get();
513
514 my $vmid = extract_param($param, 'vmid');
515 my $path = extract_param($param, 'path');
516 my $dest = extract_param($param, 'destination');
517
518 my $perms = extract_param($param, 'perms');
fa0ab908
DM
519 # assume octal as default
520 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
b95bf64b
WB
521 my $user = extract_param($param, 'user');
522 my $group = extract_param($param, 'group');
523
524 my $code = sub {
525 my $running = PVE::LXC::check_running($vmid);
526 die "can only pull files from a running VM" if !$running;
527
528 my $realcmd = sub {
529 my $pid = PVE::LXC::find_lxc_pid($vmid);
530 # Avoid symlink issues by opening the files from inside the
531 # corresponding namespaces.
532 my $destfd = create_file($dest, $perms, $user, $group);
533
534 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
535 or die "failed to open the container's mount namespace\n";
536 PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS)
537 or die "failed to enter the container's mount namespace\n";
538 close($mntnsfd);
539 chdir('/') or die "failed to change to container root directory\n";
540
541 open my $srcfd, '<', $path
542 or die "failed to open $path: $!\n";
543
544 copy($srcfd, $destfd);
545 };
546
547 # This avoids having to setns() back to our namespace.
548 return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
549 };
550
67afe46e 551 return PVE::LXC::Config->lock_config($vmid, $code);
b95bf64b
WB
552 }});
553
554__PACKAGE__->register_method({
555 name => 'push',
556 path => 'push',
557 method => 'PUT',
558 description => "Copy a local file to the container.",
559 parameters => {
560 additionalProperties => 0,
561 properties => {
562 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
563 file => {
564 type => 'string',
565 description => "Path to a local file.",
566 },
567 destination => {
568 type => 'string',
569 description => "Destination inside the container to write to.",
570 },
571 user => {
572 type => 'string',
573 description => 'Owner user name or id. When using a name it must exist inside the container.',
574 optional => 1,
575 },
576 group => {
577 type => 'string',
578 description => 'Owner group name or id. When using a name it must exist inside the container.',
579 optional => 1,
580 },
581 perms => {
582 type => 'string',
893e40de 583 description => "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
b95bf64b
WB
584 optional => 1,
585 },
586 },
587 },
588 returns => {
589 type => 'string',
590 description => "the task ID.",
591 },
592 code => sub {
593 my ($param) = @_;
594
595 my $rpcenv = PVE::RPCEnvironment::get();
596
597 my $vmid = extract_param($param, 'vmid');
598 my $file = extract_param($param, 'file');
599 my $dest = extract_param($param, 'destination');
600
601 my $perms = extract_param($param, 'perms');
fa0ab908
DM
602 # assume octal as default
603 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
b95bf64b
WB
604 my $user = extract_param($param, 'user');
605 my $group = extract_param($param, 'group');
606
607 my $code = sub {
608 my $running = PVE::LXC::check_running($vmid);
5a883bff 609 die "can only push files to a running CT\n" if !$running;
b95bf64b 610
67afe46e 611 my $conf = PVE::LXC::Config->load_config($vmid);
b95bf64b
WB
612 my $unprivileged = $conf->{unprivileged};
613
614 my $realcmd = sub {
615 my $pid = PVE::LXC::find_lxc_pid($vmid);
616 # We open the file then enter the container's mount - and for
617 # unprivileged containers - user namespace and then create the
618 # file. This avoids symlink attacks as a symlink cannot point
619 # outside the namespace and our own access is equivalent to the
620 # container-local's root user. Also the user-passed -user and
621 # -group parameters will use the container-local's user and
622 # group names.
623 sysopen my $srcfd, $file, O_RDONLY
624 or die "failed to open $file for reading\n";
625
626 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
627 or die "failed to open the container's mount namespace\n";
628 my $usernsfd;
629 if ($unprivileged) {
630 sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY
631 or die "failed to open the container's user namespace\n";
632 }
633
634 PVE::Tools::setns(fileno($mntnsfd), PVE::Tools::CLONE_NEWNS)
635 or die "failed to enter the container's mount namespace\n";
636 close($mntnsfd);
637 chdir('/') or die "failed to change to container root directory\n";
638
639 if ($unprivileged) {
640 PVE::Tools::setns(fileno($usernsfd), PVE::Tools::CLONE_NEWUSER)
641 or die "failed to enter the container's user namespace\n";
642 close($usernsfd);
643 POSIX::setgid(0) or die "setgid failed: $!\n";
644 POSIX::setuid(0) or die "setuid failed: $!\n";
645 }
646
647 my $destfd = create_file($dest, $perms, $user, $group);
648 copy($srcfd, $destfd);
649 };
650
651 # This avoids having to setns() back to our namespace.
652 return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
653 };
654
67afe46e 655 return PVE::LXC::Config->lock_config($vmid, $code);
b95bf64b
WB
656 }});
657
c87b1505
DM
658__PACKAGE__->register_method ({
659 name => 'cpusets',
660 path => 'cpusets',
661 method => 'GET',
662 description => "Print the list of assigned CPU sets.",
663 parameters => {
664 additionalProperties => 0,
665 properties => {},
666 },
667 returns => { type => 'null'},
668 code => sub {
669 my ($param) = @_;
670
0b6a2f0e
WB
671 my $cgv1 = PVE::LXC::get_cgroup_subsystems();
672 if (!$cgv1->{cpuset}) {
673 print "cpuset cgroup not available\n";
674 return undef;
675 }
676
c87b1505
DM
677 my $ctlist = PVE::LXC::config_list();
678
679 my $len = 0;
680 my $id_len = 0;
681 my $res = {};
682
683 foreach my $vmid (sort keys %$ctlist) {
684 next if ! -d "/sys/fs/cgroup/cpuset/lxc/$vmid";
685
686 my $cpuset = eval { PVE::CpuSet->new_from_cgroup("lxc/$vmid"); };
687 if (my $err = $@) {
688 warn $err;
689 next;
690 }
691 my @cpuset_members = $cpuset->members();
692
693 my $line = ': ';
694
695 my $last = $cpuset_members[-1];
696
697 for (my $id = 0; $id <= $last; $id++) {
698 my $empty = ' ' x length("$id");
699 $line .= ' ' . ($cpuset->has($id) ? $id : $empty);
700 }
701 $len = length($line) if length($line) > $len;
702 $id_len = length($vmid) if length($vmid) > $id_len;
703
704 $res->{$vmid} = $line;
705 }
706
486fe362 707 my @vmlist = sort keys %$res;
c87b1505 708
486fe362
DM
709 if (scalar(@vmlist)) {
710 my $header = '-' x ($len + $id_len) . "\n";
711
712 print $header;
713 foreach my $vmid (@vmlist) {
714 print sprintf("%${id_len}i%s\n", $vmid, $res->{$vmid});
715 }
716 print $header;
717
718 } else {
719 print "no running containers\n";
c87b1505 720 }
c87b1505
DM
721
722 return undef;
723 }});
724
9beee926
DM
725our $cmddef = {
726 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
727 my $res = shift;
728 return if !scalar(@$res);
d0226204
WB
729 my $format = "%-10s %-10s %-12s %-20s\n";
730 printf($format, 'VMID', 'Status', 'Lock', 'Name');
9beee926 731 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
d0226204 732 printf($format, $d->{vmid}, $d->{status}, $d->{lock}, $d->{name});
9beee926
DM
733 }
734 }],
735 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
736 { node => $nodename }, sub {
737 my $config = shift;
738 foreach my $k (sort (keys %$config)) {
739 next if $k eq 'digest';
8a778340 740 next if $k eq 'lxc';
9beee926
DM
741 my $v = $config->{$k};
742 if ($k eq 'description') {
743 $v = PVE::Tools::encode_text($v);
744 }
745 print "$k: $v\n";
746 }
8a778340
FG
747 if (defined($config->{'lxc'})) {
748 my $lxc_list = $config->{'lxc'};
749 foreach my $lxc_opt (@$lxc_list) {
750 print "$lxc_opt->[0]: $lxc_opt->[1]\n"
751 }
752 }
9beee926
DM
753 }],
754 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
40626217 755
985b18ed 756 resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
9beee926
DM
757
758 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
759 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
760
761 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
762 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
763 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
764 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
765 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
766
c4a33727 767 clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
9beee926 768 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
3c0f6806 769 move_volume => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage'], { node => $nodename }, $upid_exit ],
9beee926 770
699ee9b2 771 status => [ __PACKAGE__, 'status', ['vmid']],
9beee926
DM
772 console => [ __PACKAGE__, 'console', ['vmid']],
773 enter => [ __PACKAGE__, 'enter', ['vmid']],
c72bd0ba 774 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
9beee926 775 exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
f8327ed5 776 fsck => [ __PACKAGE__, 'fsck', ['vmid']],
b95bf64b 777
c93be6ed
WB
778 mount => [ __PACKAGE__, 'mount', ['vmid']],
779 unmount => [ __PACKAGE__, 'unmount', ['vmid']],
b95bf64b
WB
780 push => [ __PACKAGE__, 'push', ['vmid', 'file', 'destination']],
781 pull => [ __PACKAGE__, 'pull', ['vmid', 'path', 'destination']],
c6b59656
WB
782
783 df => [ __PACKAGE__, 'df', ['vmid']],
784
9beee926
DM
785 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
786 { node => $nodename }, $upid_exit ],
787
788 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
789 { node => $nodename } , $upid_exit ],
790
791 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
792
793 listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename },
794 sub {
795 my $res = shift;
796 foreach my $e (@$res) {
797 my $headline = $e->{description} || 'no-description';
798 $headline =~ s/\n.*//sg;
799 my $parent = $e->{parent} // 'no-parent';
800 printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
801 }
802 }],
803
804 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
805
806 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
c87b1505
DM
807
808 cpusets => [ __PACKAGE__, 'cpusets', []],
809
9beee926
DM
810};
811
812
8131;