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 # File creation with specified ownership and permissions.
287 # User and group can be names or decimal numbers.
288 # Permissions are explicit (not affected by umask) and can be numeric with the
289 # usual 0/0x prefixes for octal/hex.
291 my ($path, $perms, $user, $group) = @_;
293 if (defined($user)) {
294 if ($user =~ /^\d+$/) {
297 $uid = getpwnam($user) or die "failed to get uid for: $user\n"
300 if (defined($group)) {
301 if ($group =~ /^\d+$/) {
304 $gid = getgrnam($group) or die "failed to get gid for: $group\n"
308 if (defined($perms)) {
310 my ($mode, $unparsed) = POSIX
::strtoul
($perms, 0);
311 die "invalid mode: '$perms'\n" if $perms eq '' || $unparsed > 0 || $!;
316 if (sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) {
317 $perms = 0666 & ~umask if !defined($perms);
319 # If the path previously existed then we do not care about left-over
320 # file descriptors even if the permissions/ownership is changed.
321 sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_TRUNC
)
322 or die "failed to create file: $path: $!\n";
327 if (defined($perms)) {
332 if (defined($uid) || defined($gid)) {
334 my ($fuid, $fgid) = (stat($fd))[4,5] if !defined($uid) || !defined($gid);
335 $uid = $fuid if !defined($uid);
336 $gid = $fgid if !defined($gid);
337 chown($uid, $gid, $fd)
338 or die "failed to change file owner: $!\n";
343 __PACKAGE__-
>register_method({
347 description
=> "Copy a file from the container to the local system.",
349 additionalProperties
=> 0,
351 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
354 description
=> "Path to a file inside the container to pull.",
358 description
=> "Destination",
362 description
=> 'Owner user name or id.',
367 description
=> 'Owner group name or id.',
372 description
=> 'File permissions to use.',
379 description
=> "the task ID.",
384 my $rpcenv = PVE
::RPCEnvironment
::get
();
386 my $vmid = extract_param
($param, 'vmid');
387 my $path = extract_param
($param, 'path');
388 my $dest = extract_param
($param, 'destination');
390 my $perms = extract_param
($param, 'perms');
391 my $user = extract_param
($param, 'user');
392 my $group = extract_param
($param, 'group');
395 my $running = PVE
::LXC
::check_running
($vmid);
396 die "can only pull files from a running VM" if !$running;
399 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
400 # Avoid symlink issues by opening the files from inside the
401 # corresponding namespaces.
402 my $destfd = create_file
($dest, $perms, $user, $group);
404 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
405 or die "failed to open the container's mount namespace\n";
406 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
407 or die "failed to enter the container's mount namespace\n";
409 chdir('/') or die "failed to change to container root directory\n";
411 open my $srcfd, '<', $path
412 or die "failed to open $path: $!\n";
414 copy
($srcfd, $destfd);
417 # This avoids having to setns() back to our namespace.
418 return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
421 return PVE
::LXC
::Config-
>lock_config($vmid, $code);
424 __PACKAGE__-
>register_method({
428 description
=> "Copy a local file to the container.",
430 additionalProperties
=> 0,
432 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
435 description
=> "Path to a local file.",
439 description
=> "Destination inside the container to write to.",
443 description
=> 'Owner user name or id. When using a name it must exist inside the container.',
448 description
=> 'Owner group name or id. When using a name it must exist inside the container.',
453 description
=> 'File permissions to use.',
460 description
=> "the task ID.",
465 my $rpcenv = PVE
::RPCEnvironment
::get
();
467 my $vmid = extract_param
($param, 'vmid');
468 my $file = extract_param
($param, 'file');
469 my $dest = extract_param
($param, 'destination');
471 my $perms = extract_param
($param, 'perms');
472 my $user = extract_param
($param, 'user');
473 my $group = extract_param
($param, 'group');
476 my $running = PVE
::LXC
::check_running
($vmid);
477 die "can only push files to a running VM" if !$running;
479 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
480 my $unprivileged = $conf->{unprivileged
};
483 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
484 # We open the file then enter the container's mount - and for
485 # unprivileged containers - user namespace and then create the
486 # file. This avoids symlink attacks as a symlink cannot point
487 # outside the namespace and our own access is equivalent to the
488 # container-local's root user. Also the user-passed -user and
489 # -group parameters will use the container-local's user and
491 sysopen my $srcfd, $file, O_RDONLY
492 or die "failed to open $file for reading\n";
494 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
495 or die "failed to open the container's mount namespace\n";
498 sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY
499 or die "failed to open the container's user namespace\n";
502 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
503 or die "failed to enter the container's mount namespace\n";
505 chdir('/') or die "failed to change to container root directory\n";
508 PVE
::Tools
::setns
(fileno($usernsfd), PVE
::Tools
::CLONE_NEWUSER
)
509 or die "failed to enter the container's user namespace\n";
511 POSIX
::setgid
(0) or die "setgid failed: $!\n";
512 POSIX
::setuid
(0) or die "setuid failed: $!\n";
515 my $destfd = create_file
($dest, $perms, $user, $group);
516 copy
($srcfd, $destfd);
519 # This avoids having to setns() back to our namespace.
520 return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
523 return PVE
::LXC
::Config-
>lock_config($vmid, $code);
527 list
=> [ 'PVE::API2::LXC', 'vmlist', [], { node
=> $nodename }, sub {
529 return if !scalar(@$res);
530 my $format = "%-10s %-10s %-12s %-20s\n";
531 printf($format, 'VMID', 'Status', 'Lock', 'Name');
532 foreach my $d (sort {$a->{vmid
} <=> $b->{vmid
} } @$res) {
533 printf($format, $d->{vmid
}, $d->{status
}, $d->{lock}, $d->{name
});
536 config
=> [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
537 { node
=> $nodename }, sub {
539 foreach my $k (sort (keys %$config)) {
540 next if $k eq 'digest';
541 my $v = $config->{$k};
542 if ($k eq 'description') {
543 $v = PVE
::Tools
::encode_text
($v);
548 set
=> [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node
=> $nodename }],
550 resize
=> [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
552 create
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename }, $upid_exit ],
553 restore
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename, restore
=> 1 }, $upid_exit ],
555 start
=> [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node
=> $nodename }, $upid_exit],
556 suspend
=> [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit],
557 resume
=> [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit],
558 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit],
559 stop
=> [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit],
561 clone
=> [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
562 migrate
=> [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit],
564 console
=> [ __PACKAGE__
, 'console', ['vmid']],
565 enter
=> [ __PACKAGE__
, 'enter', ['vmid']],
566 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
567 exec => [ __PACKAGE__
, 'exec', ['vmid', 'extra-args']],
568 fsck
=> [ __PACKAGE__
, 'fsck', ['vmid']],
570 mount
=> [ __PACKAGE__
, 'mount', ['vmid']],
571 unmount
=> [ __PACKAGE__
, 'unmount', ['vmid']],
572 push => [ __PACKAGE__
, 'push', ['vmid', 'file', 'destination']],
573 pull
=> [ __PACKAGE__
, 'pull', ['vmid', 'path', 'destination']],
575 destroy
=> [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
576 { node
=> $nodename }, $upid_exit ],
578 snapshot
=> [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
579 { node
=> $nodename } , $upid_exit ],
581 delsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
583 listsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node
=> $nodename },
586 foreach my $e (@$res) {
587 my $headline = $e->{description
} || 'no-description';
588 $headline =~ s/\n.*//sg;
589 my $parent = $e->{parent
} // 'no-parent';
590 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
594 rollback
=> [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
596 template
=> [ "PVE::API2::LXC", 'template', ['vmid'], { node
=> $nodename }],
606 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
614 pct is a tool to manages Linux Containers (LXC). You can create
615 and destroy containers, and control execution
616 (start/stop/suspend/resume). Besides that, you can use pct to set
617 parameters in the associated config file, like network configuration or
622 Create a container based on a Debian template
623 (provided you downloaded the template via the webgui before)
625 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
631 Start a login session via getty
635 Enter the lxc namespace and run a shell as root user
639 Display the configuration
643 Add a network interface called eth0, bridged to the host bridge vmbr0,
644 set the address and gateway, while it's running
646 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
648 Reduce the memory of the container to 512MB
650 pct set -memory 512 100
654 /etc/pve/lxc/<vmid>.conf
656 Configuration file for the container <vmid>
660 L<B<qm(1)>>, L<B<pvesh(1)>>
662 =include pve_copyright