12 use PVE
::Tools
qw(extract_param);
15 use PVE
::RPCEnvironment
;
16 use PVE
::JSONSchema
qw(get_standard_option);
19 use PVE
::API2
::LXC
::Config
;
20 use PVE
::API2
::LXC
::Status
;
21 use PVE
::API2
::LXC
::Snapshot
;
25 use base
qw(PVE::CLIHandler);
27 my $nodename = PVE
::INotify
::nodename
();
31 my $status = PVE
::Tools
::upid_read_status
($upid);
32 exit($status eq 'OK' ?
0 : -1);
36 my $term = new Term
::ReadLine
('pct');
37 my $attribs = $term->Attribs;
38 $attribs->{redisplay_function
} = $attribs->{shadow_redisplay
};
39 my $input = $term->readline('Enter password: ');
40 my $conf = $term->readline('Retype password: ');
41 die "Passwords do not match.\n" if ($input ne $conf);
45 sub string_param_file_mapping
{
49 'create_vm' => ['ssh-public-keys'],
52 return defined($mapping->{$name}) ?
$mapping->{$name} : [];
55 __PACKAGE__-
>register_method ({
59 description
=> "Unlock the VM.",
61 additionalProperties
=> 0,
63 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
66 returns
=> { type
=> 'null'},
70 my $vmid = $param->{vmid
};
72 PVE
::LXC
::Config-
>remove_lock($vmid);
77 __PACKAGE__-
>register_method ({
81 description
=> "Launch a console for the specified container.",
83 additionalProperties
=> 0,
85 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
88 returns
=> { type
=> 'null' },
93 # test if container exists on this node
94 my $conf = PVE
::LXC
::Config-
>load_config($param->{vmid
});
96 my $cmd = PVE
::LXC
::get_console_command
($param->{vmid
}, $conf);
100 __PACKAGE__-
>register_method ({
104 description
=> "Launch a shell for the specified container.",
106 additionalProperties
=> 0,
108 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
111 returns
=> { type
=> 'null' },
116 my $vmid = $param->{vmid
};
118 # test if container exists on this node
119 PVE
::LXC
::Config-
>load_config($vmid);
121 die "Error: container '$vmid' not running!\n" if !PVE
::LXC
::check_running
($vmid);
123 exec('lxc-attach', '-n', $vmid);
126 __PACKAGE__-
>register_method ({
130 description
=> "Launch a command inside the specified container.",
132 additionalProperties
=> 0,
134 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
135 'extra-args' => get_standard_option
('extra-args'),
138 returns
=> { type
=> 'null' },
143 # test if container exists on this node
144 PVE
::LXC
::Config-
>load_config($param->{vmid
});
146 if (!@{$param->{'extra-args'}}) {
147 die "missing command";
149 exec('lxc-attach', '-n', $param->{vmid
}, '--', @{$param->{'extra-args'}});
152 __PACKAGE__-
>register_method ({
156 description
=> "Run a filesystem check (fsck) on a container volume.",
158 additionalProperties
=> 0,
160 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
164 description
=> "Force checking, even if the filesystem seems clean",
170 description
=> "A volume on which to run the filesystem check",
171 enum
=> [PVE
::LXC
::Config-
>mountpoint_names()],
175 returns
=> { type
=> 'null' },
179 my $vmid = $param->{'vmid'};
180 my $device = defined($param->{'device'}) ?
$param->{'device'} : 'rootfs';
182 my $command = ['fsck', '-a', '-l'];
183 push(@$command, '-f') if $param->{force
};
185 # critical path: all of this will be done while the container is locked
188 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
189 my $storage_cfg = PVE
::Storage
::config
();
191 defined($conf->{$device}) || die "cannot run command on unexisting mountpoint $device\n";
193 my $mount_point = $device eq 'rootfs' ? PVE
::LXC
::Config-
>parse_ct_rootfs($conf->{$device}) :
194 PVE
::LXC
::Config-
>parse_ct_mountpoint($conf->{$device});
196 my $volid = $mount_point->{volume
};
199 my $storage_id = PVE
::Storage
::parse_volume_id
($volid, 1);
202 my (undef, undef, undef, undef, undef, undef, $format) =
203 PVE
::Storage
::parse_volname
($storage_cfg, $volid);
205 die "unable to run fsck for '$volid' (format == $format)\n"
208 $path = PVE
::Storage
::path
($storage_cfg, $volid);
211 if (($volid =~ m
|^/.+|) && (-b
$volid)) {
212 # pass block devices directly
215 die "path '$volid' does not point to a block device\n";
219 push(@$command, $path);
221 PVE
::LXC
::check_running
($vmid) &&
222 die "cannot run fsck on active container\n";
224 PVE
::Tools
::run_command
($command);
227 PVE
::LXC
::Config-
>lock_config($vmid, $do_fsck);
231 __PACKAGE__-
>register_method({
235 description
=> "Mount the container's filesystem on the host. " .
236 "This will hold a lock on the container and is meant for emergency maintenance only " .
237 "as it will prevent further operations on the container other than start and stop.",
239 additionalProperties
=> 0,
241 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
244 returns
=> { type
=> 'null' },
248 my $rpcenv = PVE
::RPCEnvironment
::get
();
250 my $vmid = extract_param
($param, 'vmid');
251 my $storecfg = PVE
::Storage
::config
();
252 PVE
::LXC
::Config-
>lock_config($vmid, sub {
253 my $conf = PVE
::LXC
::Config-
>set_lock($vmid, 'mounted');
254 PVE
::LXC
::mount_all
($vmid, $storecfg, $conf);
259 __PACKAGE__-
>register_method({
263 description
=> "Unmount the container's filesystem.",
265 additionalProperties
=> 0,
267 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
270 returns
=> { type
=> 'null' },
274 my $rpcenv = PVE
::RPCEnvironment
::get
();
276 my $vmid = extract_param
($param, 'vmid');
277 my $storecfg = PVE
::Storage
::config
();
278 PVE
::LXC
::Config-
>lock_config($vmid, sub {
279 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
280 PVE
::LXC
::umount_all
($vmid, $storecfg, $conf, 0);
281 PVE
::LXC
::Config-
>remove_lock($vmid, 'mounted');
286 __PACKAGE__-
>register_method({
290 description
=> "Get the container's current disk usage.",
292 additionalProperties
=> 0,
294 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
297 returns
=> { type
=> 'null' },
301 my $rpcenv = PVE
::RPCEnvironment
::get
();
303 # JSONSchema's format_size is exact, this uses floating point numbers
306 return $size if $size < 1024.;
308 return sprintf('%.1fK', ${size
}) if $size < 1024.;
310 return sprintf('%.1fM', ${size
}) if $size < 1024.;
312 return sprintf('%.1fG', ${size
}) if $size < 1024.;
314 return sprintf('%.1fT', ${size
}) if $size < 1024.;
317 my $vmid = extract_param
($param, 'vmid');
318 PVE
::LXC
::Config-
>lock_config($vmid, sub {
319 my $pid = eval { PVE
::LXC
::find_lxc_pid
($vmid) };
320 my ($conf, $rootdir, $storecfg, $mounted);
322 $conf = PVE
::LXC
::Config-
>set_lock($vmid, 'mounted');
323 $rootdir = "/var/lib/lxc/$vmid/rootfs";
324 $storecfg = PVE
::Storage
::config
();
325 PVE
::LXC
::mount_all
($vmid, $storecfg, $conf);
328 $conf = PVE
::LXC
::Config-
>load_config($vmid);
329 $rootdir = "/proc/$pid/root";
332 my @list = [qw(MP Volume Size Used Avail Use% Path)];
333 my @len = map { length($_) } @{$list[0]};
336 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
337 my ($name, $mp) = @_;
338 my $path = $mp->{mp
};
340 my $df = PVE
::Tools
::df
("$rootdir/$path", 3);
341 my $total = $format->($df->{total
});
342 my $used = $format->($df->{used
});
343 my $avail = $format->($df->{avail
});
345 my $pc = sprintf('%.1f', $df->{used
}/$df->{total
});
347 my $entry = [ $name, $mp->{volume
}, $total, $used, $avail, $pc, $path ];
350 foreach my $i (0..5) {
351 $len[$i] = length($entry->[$i])
352 if $len[$i] < length($entry->[$i]);
356 my $format = "%-$len[0]s %-$len[1]s %$len[2]s %$len[3]s %$len[4]s %$len[5]s %s\n";
357 printf($format, @$_) foreach @list;
362 PVE
::LXC
::umount_all
($vmid, $storecfg, $conf, 0);
363 PVE
::LXC
::Config-
>remove_lock($vmid, 'mounted');
369 # File creation with specified ownership and permissions.
370 # User and group can be names or decimal numbers.
371 # Permissions are explicit (not affected by umask) and can be numeric with the
372 # usual 0/0x prefixes for octal/hex.
374 my ($path, $perms, $user, $group) = @_;
376 if (defined($user)) {
377 if ($user =~ /^\d+$/) {
380 $uid = getpwnam($user) or die "failed to get uid for: $user\n"
383 if (defined($group)) {
384 if ($group =~ /^\d+$/) {
387 $gid = getgrnam($group) or die "failed to get gid for: $group\n"
391 if (defined($perms)) {
393 my ($mode, $unparsed) = POSIX
::strtoul
($perms, 0);
394 die "invalid mode: '$perms'\n" if $perms eq '' || $unparsed > 0 || $!;
399 if (sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) {
400 $perms = 0666 & ~umask if !defined($perms);
402 # If the path previously existed then we do not care about left-over
403 # file descriptors even if the permissions/ownership is changed.
404 sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_TRUNC
)
405 or die "failed to create file: $path: $!\n";
410 if (defined($perms)) {
415 if (defined($uid) || defined($gid)) {
417 my ($fuid, $fgid) = (stat($fd))[4,5] if !defined($uid) || !defined($gid);
418 $uid = $fuid if !defined($uid);
419 $gid = $fgid if !defined($gid);
420 chown($uid, $gid, $fd)
421 or die "failed to change file owner: $!\n";
426 __PACKAGE__-
>register_method({
430 description
=> "Copy a file from the container to the local system.",
432 additionalProperties
=> 0,
434 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
437 description
=> "Path to a file inside the container to pull.",
441 description
=> "Destination",
445 description
=> 'Owner user name or id.',
450 description
=> 'Owner group name or id.',
455 description
=> "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
462 description
=> "the task ID.",
467 my $rpcenv = PVE
::RPCEnvironment
::get
();
469 my $vmid = extract_param
($param, 'vmid');
470 my $path = extract_param
($param, 'path');
471 my $dest = extract_param
($param, 'destination');
473 my $perms = extract_param
($param, 'perms');
474 # assume octal as default
475 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
476 my $user = extract_param
($param, 'user');
477 my $group = extract_param
($param, 'group');
480 my $running = PVE
::LXC
::check_running
($vmid);
481 die "can only pull files from a running VM" if !$running;
484 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
485 # Avoid symlink issues by opening the files from inside the
486 # corresponding namespaces.
487 my $destfd = create_file
($dest, $perms, $user, $group);
489 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
490 or die "failed to open the container's mount namespace\n";
491 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
492 or die "failed to enter the container's mount namespace\n";
494 chdir('/') or die "failed to change to container root directory\n";
496 open my $srcfd, '<', $path
497 or die "failed to open $path: $!\n";
499 copy
($srcfd, $destfd);
502 # This avoids having to setns() back to our namespace.
503 return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
506 return PVE
::LXC
::Config-
>lock_config($vmid, $code);
509 __PACKAGE__-
>register_method({
513 description
=> "Copy a local file to the container.",
515 additionalProperties
=> 0,
517 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
520 description
=> "Path to a local file.",
524 description
=> "Destination inside the container to write to.",
528 description
=> 'Owner user name or id. When using a name it must exist inside the container.',
533 description
=> 'Owner group name or id. When using a name it must exist inside the container.',
538 description
=> "File permissions to use (octal by default, prefix with '0x' for hexadecimal).",
545 description
=> "the task ID.",
550 my $rpcenv = PVE
::RPCEnvironment
::get
();
552 my $vmid = extract_param
($param, 'vmid');
553 my $file = extract_param
($param, 'file');
554 my $dest = extract_param
($param, 'destination');
556 my $perms = extract_param
($param, 'perms');
557 # assume octal as default
558 $perms = "0$perms" if defined($perms) && $perms !~m/^0/;
559 my $user = extract_param
($param, 'user');
560 my $group = extract_param
($param, 'group');
563 my $running = PVE
::LXC
::check_running
($vmid);
564 die "can only push files to a running VM" if !$running;
566 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
567 my $unprivileged = $conf->{unprivileged
};
570 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
571 # We open the file then enter the container's mount - and for
572 # unprivileged containers - user namespace and then create the
573 # file. This avoids symlink attacks as a symlink cannot point
574 # outside the namespace and our own access is equivalent to the
575 # container-local's root user. Also the user-passed -user and
576 # -group parameters will use the container-local's user and
578 sysopen my $srcfd, $file, O_RDONLY
579 or die "failed to open $file for reading\n";
581 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
582 or die "failed to open the container's mount namespace\n";
585 sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY
586 or die "failed to open the container's user namespace\n";
589 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
590 or die "failed to enter the container's mount namespace\n";
592 chdir('/') or die "failed to change to container root directory\n";
595 PVE
::Tools
::setns
(fileno($usernsfd), PVE
::Tools
::CLONE_NEWUSER
)
596 or die "failed to enter the container's user namespace\n";
598 POSIX
::setgid
(0) or die "setgid failed: $!\n";
599 POSIX
::setuid
(0) or die "setuid failed: $!\n";
602 my $destfd = create_file
($dest, $perms, $user, $group);
603 copy
($srcfd, $destfd);
606 # This avoids having to setns() back to our namespace.
607 return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
610 return PVE
::LXC
::Config-
>lock_config($vmid, $code);
614 list
=> [ 'PVE::API2::LXC', 'vmlist', [], { node
=> $nodename }, sub {
616 return if !scalar(@$res);
617 my $format = "%-10s %-10s %-12s %-20s\n";
618 printf($format, 'VMID', 'Status', 'Lock', 'Name');
619 foreach my $d (sort {$a->{vmid
} <=> $b->{vmid
} } @$res) {
620 printf($format, $d->{vmid
}, $d->{status
}, $d->{lock}, $d->{name
});
623 config
=> [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
624 { node
=> $nodename }, sub {
626 foreach my $k (sort (keys %$config)) {
627 next if $k eq 'digest';
628 my $v = $config->{$k};
629 if ($k eq 'description') {
630 $v = PVE
::Tools
::encode_text
($v);
635 set
=> [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node
=> $nodename }],
637 resize
=> [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
639 create
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename }, $upid_exit ],
640 restore
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename, restore
=> 1 }, $upid_exit ],
642 start
=> [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node
=> $nodename }, $upid_exit],
643 suspend
=> [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit],
644 resume
=> [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit],
645 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit],
646 stop
=> [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit],
648 clone
=> [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
649 migrate
=> [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit],
651 console
=> [ __PACKAGE__
, 'console', ['vmid']],
652 enter
=> [ __PACKAGE__
, 'enter', ['vmid']],
653 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
654 exec => [ __PACKAGE__
, 'exec', ['vmid', 'extra-args']],
655 fsck
=> [ __PACKAGE__
, 'fsck', ['vmid']],
657 mount
=> [ __PACKAGE__
, 'mount', ['vmid']],
658 unmount
=> [ __PACKAGE__
, 'unmount', ['vmid']],
659 push => [ __PACKAGE__
, 'push', ['vmid', 'file', 'destination']],
660 pull
=> [ __PACKAGE__
, 'pull', ['vmid', 'path', 'destination']],
662 df
=> [ __PACKAGE__
, 'df', ['vmid']],
664 destroy
=> [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
665 { node
=> $nodename }, $upid_exit ],
667 snapshot
=> [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
668 { node
=> $nodename } , $upid_exit ],
670 delsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
672 listsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node
=> $nodename },
675 foreach my $e (@$res) {
676 my $headline = $e->{description
} || 'no-description';
677 $headline =~ s/\n.*//sg;
678 my $parent = $e->{parent
} // 'no-parent';
679 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
683 rollback
=> [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
685 template
=> [ "PVE::API2::LXC", 'template', ['vmid'], { node
=> $nodename }],
695 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
703 pct is a tool to manages Linux Containers (LXC). You can create
704 and destroy containers, and control execution
705 (start/stop/suspend/resume). Besides that, you can use pct to set
706 parameters in the associated config file, like network configuration or
711 Create a container based on a Debian template
712 (provided you downloaded the template via the webgui before)
714 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
720 Start a login session via getty
724 Enter the lxc namespace and run a shell as root user
728 Display the configuration
732 Add a network interface called eth0, bridged to the host bridge vmbr0,
733 set the address and gateway, while it's running
735 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
737 Reduce the memory of the container to 512MB
739 pct set -memory 512 100
743 /etc/pve/lxc/<vmid>.conf
745 Configuration file for the container <vmid>
749 L<B<qm(1)>>, L<B<pvesh(1)>>
751 =include pve_copyright