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