]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/CLI/pct.pm
11 use PVE
::Tools
qw(extract_param);
14 use PVE
::RPCEnvironment
;
15 use PVE
::JSONSchema
qw(get_standard_option);
18 use PVE
::API2
::LXC
::Config
;
19 use PVE
::API2
::LXC
::Status
;
20 use PVE
::API2
::LXC
::Snapshot
;
24 use base
qw(PVE::CLIHandler);
26 my $nodename = PVE
::INotify
::nodename
();
30 my $status = PVE
::Tools
::upid_read_status
($upid);
31 exit($status eq 'OK' ?
0 : -1);
34 __PACKAGE__-
>register_method ({
38 description
=> "Unlock the VM.",
40 additionalProperties
=> 0,
42 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
45 returns
=> { type
=> 'null'},
49 my $vmid = $param->{vmid
};
51 PVE
::LXC
::lock_config
($vmid, sub {
52 my $conf = PVE
::LXC
::load_config
($vmid);
54 PVE
::LXC
::write_config
($vmid, $conf);
60 __PACKAGE__-
>register_method ({
64 description
=> "Launch a console for the specified container.",
66 additionalProperties
=> 0,
68 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
71 returns
=> { type
=> 'null' },
76 # test if container exists on this node
77 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
79 my $cmd = PVE
::LXC
::get_console_command
($param->{vmid
}, $conf);
83 __PACKAGE__-
>register_method ({
87 description
=> "Launch a shell for the specified container.",
89 additionalProperties
=> 0,
91 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
94 returns
=> { type
=> 'null' },
99 my $vmid = $param->{vmid
};
101 # test if container exists on this node
102 PVE
::LXC
::load_config
($vmid);
104 die "Error: container '$vmid' not running!\n" if !PVE
::LXC
::check_running
($vmid);
106 exec('lxc-attach', '-n', $vmid);
109 __PACKAGE__-
>register_method ({
113 description
=> "Launch a command inside the specified container.",
115 additionalProperties
=> 0,
117 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
118 'extra-args' => get_standard_option
('extra-args'),
121 returns
=> { type
=> 'null' },
126 # test if container exists on this node
127 PVE
::LXC
::load_config
($param->{vmid
});
129 if (!@{$param->{'extra-args'}}) {
130 die "missing command";
132 exec('lxc-attach', '-n', $param->{vmid
}, '--', @{$param->{'extra-args'}});
135 __PACKAGE__-
>register_method ({
139 description
=> "Run a filesystem check (fsck) on a container volume.",
141 additionalProperties
=> 0,
143 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
147 description
=> "Force checking, even if the filesystem seems clean",
153 description
=> "A volume on which to run the filesystem check",
154 enum
=> [PVE
::LXC
::mountpoint_names
()],
158 returns
=> { type
=> 'null' },
162 my $vmid = $param->{'vmid'};
163 my $device = defined($param->{'device'}) ?
$param->{'device'} : 'rootfs';
165 my $command = ['fsck', '-a', '-l'];
166 push(@$command, '-f') if $param->{force
};
168 # critical path: all of this will be done while the container is locked
171 my $conf = PVE
::LXC
::load_config
($vmid);
172 my $storage_cfg = PVE
::Storage
::config
();
174 defined($conf->{$device}) || die "cannot run command on unexisting mountpoint $device\n";
176 my $mount_point = $device eq 'rootfs' ? PVE
::LXC
::parse_ct_rootfs
($conf->{$device}) :
177 PVE
::LXC
::parse_ct_mountpoint
($conf->{$device});
179 my $volid = $mount_point->{volume
};
182 my $storage_id = PVE
::Storage
::parse_volume_id
($volid, 1);
185 my (undef, undef, undef, undef, undef, undef, $format) =
186 PVE
::Storage
::parse_volname
($storage_cfg, $volid);
188 die "unable to run fsck for '$volid' (format == $format)\n"
191 $path = PVE
::Storage
::path
($storage_cfg, $volid);
194 if (($volid =~ m
|^/.+|) && (-b
$volid)) {
195 # pass block devices directly
198 die "path '$volid' does not point to a block device\n";
202 push(@$command, $path);
204 PVE
::LXC
::check_running
($vmid) &&
205 die "cannot run fsck on active container\n";
207 PVE
::Tools
::run_command
($command);
210 PVE
::LXC
::lock_config
($vmid, $do_fsck);
214 __PACKAGE__-
>register_method({
218 description
=> "Mount the container's filesystem on the host. " .
219 "This will hold a lock on the container and is meant for emergency maintenance only " .
220 "as it will prevent further operations on the container other than start and stop.",
222 additionalProperties
=> 0,
224 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
227 returns
=> { type
=> 'null' },
231 my $rpcenv = PVE
::RPCEnvironment
::get
();
233 my $vmid = extract_param
($param, 'vmid');
234 my $storecfg = PVE
::Storage
::config
();
235 PVE
::LXC
::lock_config
($vmid, sub {
236 my $conf = PVE
::LXC
::set_lock
($vmid, 'mounted');
237 PVE
::LXC
::mount_all
($vmid, $storecfg, $conf);
242 __PACKAGE__-
>register_method({
246 description
=> "Unmount the container's filesystem.",
248 additionalProperties
=> 0,
250 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
253 returns
=> { type
=> 'null' },
257 my $rpcenv = PVE
::RPCEnvironment
::get
();
259 my $vmid = extract_param
($param, 'vmid');
260 my $storecfg = PVE
::Storage
::config
();
261 PVE
::LXC
::lock_config
($vmid, sub {
262 my $conf = PVE
::LXC
::load_config
($vmid);
263 PVE
::LXC
::umount_all
($vmid, $storecfg, $conf, 0);
264 PVE
::LXC
::remove_lock
($vmid, 'mounted');
269 # File creation with specified ownership and permissions.
270 # User and group can be names or decimal numbers.
271 # Permissions are explicit (not affected by umask) and can be numeric with the
272 # usual 0/0x prefixes for octal/hex.
274 my ($path, $perms, $user, $group) = @_;
276 if (defined($user)) {
277 if ($user =~ /^\d+$/) {
280 $uid = getpwnam($user) or die "failed to get uid for: $user\n"
283 if (defined($group)) {
284 if ($group =~ /^\d+$/) {
287 $gid = getgrnam($group) or die "failed to get gid for: $group\n"
291 if (defined($perms)) {
293 my ($mode, $unparsed) = POSIX
::strtoul
($perms, 0);
294 die "invalid mode: '$perms'\n" if $perms eq '' || $unparsed > 0 || $!;
299 if (sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) {
300 $perms = 0666 & ~umask if !defined($perms);
302 # If the path previously existed then we do not care about left-over
303 # file descriptors even if the permissions/ownership is changed.
304 sysopen($fd, $path, O_WRONLY
| O_CREAT
| O_TRUNC
)
305 or die "failed to create file: $path: $!\n";
310 if (defined($perms)) {
315 if (defined($uid) || defined($gid)) {
317 my ($fuid, $fgid) = (stat($fd))[4,5] if !defined($uid) || !defined($gid);
318 $uid = $fuid if !defined($uid);
319 $gid = $fgid if !defined($gid);
320 chown($uid, $gid, $fd)
321 or die "failed to change file owner: $!\n";
326 __PACKAGE__-
>register_method({
330 description
=> "Copy a file from the container to the local system.",
332 additionalProperties
=> 0,
334 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
337 description
=> "Path to a file inside the container to pull.",
341 description
=> "Destination",
345 description
=> 'Owner user name or id.',
350 description
=> 'Owner group name or id.',
355 description
=> 'File permissions to use.',
362 description
=> "the task ID.",
367 my $rpcenv = PVE
::RPCEnvironment
::get
();
369 my $vmid = extract_param
($param, 'vmid');
370 my $path = extract_param
($param, 'path');
371 my $dest = extract_param
($param, 'destination');
373 my $perms = extract_param
($param, 'perms');
374 my $user = extract_param
($param, 'user');
375 my $group = extract_param
($param, 'group');
378 my $running = PVE
::LXC
::check_running
($vmid);
379 die "can only pull files from a running VM" if !$running;
382 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
383 # Avoid symlink issues by opening the files from inside the
384 # corresponding namespaces.
385 my $destfd = create_file
($dest, $perms, $user, $group);
387 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
388 or die "failed to open the container's mount namespace\n";
389 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
390 or die "failed to enter the container's mount namespace\n";
392 chdir('/') or die "failed to change to container root directory\n";
394 open my $srcfd, '<', $path
395 or die "failed to open $path: $!\n";
397 copy
($srcfd, $destfd);
400 # This avoids having to setns() back to our namespace.
401 return $rpcenv->fork_worker('pull_file', $vmid, undef, $realcmd);
404 return PVE
::LXC
::lock_config
($vmid, $code);
407 __PACKAGE__-
>register_method({
411 description
=> "Copy a local file to the container.",
413 additionalProperties
=> 0,
415 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
418 description
=> "Path to a local file.",
422 description
=> "Destination inside the container to write to.",
426 description
=> 'Owner user name or id. When using a name it must exist inside the container.',
431 description
=> 'Owner group name or id. When using a name it must exist inside the container.',
436 description
=> 'File permissions to use.',
443 description
=> "the task ID.",
448 my $rpcenv = PVE
::RPCEnvironment
::get
();
450 my $vmid = extract_param
($param, 'vmid');
451 my $file = extract_param
($param, 'file');
452 my $dest = extract_param
($param, 'destination');
454 my $perms = extract_param
($param, 'perms');
455 my $user = extract_param
($param, 'user');
456 my $group = extract_param
($param, 'group');
459 my $running = PVE
::LXC
::check_running
($vmid);
460 die "can only push files to a running VM" if !$running;
462 my $conf = PVE
::LXC
::load_config
($vmid);
463 my $unprivileged = $conf->{unprivileged
};
466 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
467 # We open the file then enter the container's mount - and for
468 # unprivileged containers - user namespace and then create the
469 # file. This avoids symlink attacks as a symlink cannot point
470 # outside the namespace and our own access is equivalent to the
471 # container-local's root user. Also the user-passed -user and
472 # -group parameters will use the container-local's user and
474 sysopen my $srcfd, $file, O_RDONLY
475 or die "failed to open $file for reading\n";
477 sysopen my $mntnsfd, "/proc/$pid/ns/mnt", O_RDONLY
478 or die "failed to open the container's mount namespace\n";
481 sysopen $usernsfd, "/proc/$pid/ns/user", O_RDONLY
482 or die "failed to open the container's user namespace\n";
485 PVE
::Tools
::setns
(fileno($mntnsfd), PVE
::Tools
::CLONE_NEWNS
)
486 or die "failed to enter the container's mount namespace\n";
488 chdir('/') or die "failed to change to container root directory\n";
491 PVE
::Tools
::setns
(fileno($usernsfd), PVE
::Tools
::CLONE_NEWUSER
)
492 or die "failed to enter the container's user namespace\n";
494 POSIX
::setgid
(0) or die "setgid failed: $!\n";
495 POSIX
::setuid
(0) or die "setuid failed: $!\n";
498 my $destfd = create_file
($dest, $perms, $user, $group);
499 copy
($srcfd, $destfd);
502 # This avoids having to setns() back to our namespace.
503 return $rpcenv->fork_worker('push_file', $vmid, undef, $realcmd);
506 return PVE
::LXC
::lock_config
($vmid, $code);
510 list
=> [ 'PVE::API2::LXC', 'vmlist', [], { node
=> $nodename }, sub {
512 return if !scalar(@$res);
513 my $format = "%-10s %-10s %-20s\n";
514 printf($format, 'VMID', 'Status', 'Name');
515 foreach my $d (sort {$a->{vmid
} <=> $b->{vmid
} } @$res) {
516 printf($format, $d->{vmid
}, $d->{status
}, $d->{name
});
519 config
=> [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
520 { node
=> $nodename }, sub {
522 foreach my $k (sort (keys %$config)) {
523 next if $k eq 'digest';
524 my $v = $config->{$k};
525 if ($k eq 'description') {
526 $v = PVE
::Tools
::encode_text
($v);
531 set
=> [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node
=> $nodename }],
533 resize
=> [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
535 create
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename }, $upid_exit ],
536 restore
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename, restore
=> 1 }, $upid_exit ],
538 start
=> [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node
=> $nodename }, $upid_exit],
539 suspend
=> [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit],
540 resume
=> [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit],
541 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit],
542 stop
=> [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit],
544 clone
=> [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
545 migrate
=> [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit],
547 console
=> [ __PACKAGE__
, 'console', ['vmid']],
548 enter
=> [ __PACKAGE__
, 'enter', ['vmid']],
549 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
550 exec => [ __PACKAGE__
, 'exec', ['vmid', 'extra-args']],
551 fsck
=> [ __PACKAGE__
, 'fsck', ['vmid']],
553 mount
=> [ __PACKAGE__
, 'mount', ['vmid']],
554 unmount
=> [ __PACKAGE__
, 'unmount', ['vmid']],
555 push => [ __PACKAGE__
, 'push', ['vmid', 'file', 'destination']],
556 pull
=> [ __PACKAGE__
, 'pull', ['vmid', 'path', 'destination']],
558 destroy
=> [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
559 { node
=> $nodename }, $upid_exit ],
561 snapshot
=> [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
562 { node
=> $nodename } , $upid_exit ],
564 delsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
566 listsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node
=> $nodename },
569 foreach my $e (@$res) {
570 my $headline = $e->{description
} || 'no-description';
571 $headline =~ s/\n.*//sg;
572 my $parent = $e->{parent
} // 'no-parent';
573 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
577 rollback
=> [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
579 template
=> [ "PVE::API2::LXC", 'template', ['vmid'], { node
=> $nodename }],
589 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
597 pct is a tool to manages Linux Containers (LXC). You can create
598 and destroy containers, and control execution
599 (start/stop/suspend/resume). Besides that, you can use pct to set
600 parameters in the associated config file, like network configuration or
605 Create a container based on a Debian template
606 (provided you downloaded the template via the webgui before)
608 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
614 Start a login session via getty
618 Enter the lxc namespace and run a shell as root user
622 Display the configuration
626 Add a network interface called eth0, bridged to the host bridge vmbr0,
627 set the address and gateway, while it's running
629 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
631 Reduce the memory of the container to 512MB
633 pct set -memory 512 100
637 /etc/pve/lxc/<vmid>.conf
639 Configuration file for the container <vmid>
643 L<B<qm(1)>>, L<B<pvesh(1)>>
645 =include pve_copyright