]> git.proxmox.com Git - pve-container.git/blob - src/PVE/CLI/pct.pm
cc610a57c21d27e591c87715d51ea35bdfcffd30
[pve-container.git] / src / PVE / CLI / pct.pm
1 package PVE::CLI::pct;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param);
8 use PVE::Cluster;
9 use PVE::INotify;
10 use PVE::RPCEnvironment;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::CLIHandler;
13 use PVE::API2::LXC;
14 use PVE::API2::LXC::Config;
15 use PVE::API2::LXC::Status;
16 use PVE::API2::LXC::Snapshot;
17
18 use Data::Dumper;
19
20 use base qw(PVE::CLIHandler);
21
22 my $nodename = PVE::INotify::nodename();
23
24 my $upid_exit = sub {
25 my $upid = shift;
26 my $status = PVE::Tools::upid_read_status($upid);
27 exit($status eq 'OK' ? 0 : -1);
28 };
29
30 __PACKAGE__->register_method ({
31 name => 'unlock',
32 path => 'unlock',
33 method => 'PUT',
34 description => "Unlock the VM.",
35 parameters => {
36 additionalProperties => 0,
37 properties => {
38 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
39 },
40 },
41 returns => { type => 'null'},
42 code => sub {
43 my ($param) = @_;
44
45 my $vmid = $param->{vmid};
46
47 PVE::LXC::lock_container($vmid, 5, sub {
48 my $conf = PVE::LXC::load_config($vmid);
49 delete $conf->{lock};
50 PVE::LXC::write_config($vmid, $conf);
51 });
52
53 return undef;
54 }});
55
56 __PACKAGE__->register_method ({
57 name => 'console',
58 path => 'console',
59 method => 'GET',
60 description => "Launch a console for the specified container.",
61 parameters => {
62 additionalProperties => 0,
63 properties => {
64 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
65 },
66 },
67 returns => { type => 'null' },
68
69 code => sub {
70 my ($param) = @_;
71
72 # test if container exists on this node
73 my $conf = PVE::LXC::load_config($param->{vmid});
74
75 my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf);
76 exec(@$cmd);
77 }});
78
79 __PACKAGE__->register_method ({
80 name => 'enter',
81 path => 'enter',
82 method => 'GET',
83 description => "Launch a shell for the specified container.",
84 parameters => {
85 additionalProperties => 0,
86 properties => {
87 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
88 },
89 },
90 returns => { type => 'null' },
91
92 code => sub {
93 my ($param) = @_;
94
95 # test if container exists on this node
96 PVE::LXC::load_config($param->{vmid});
97
98 exec('lxc-attach', '-n', $param->{vmid});
99 }});
100
101 __PACKAGE__->register_method ({
102 name => 'exec',
103 path => 'exec',
104 method => 'GET',
105 description => "Launch a command inside the specified container.",
106 parameters => {
107 additionalProperties => 0,
108 properties => {
109 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
110 'extra-args' => get_standard_option('extra-args'),
111 },
112 },
113 returns => { type => 'null' },
114
115 code => sub {
116 my ($param) = @_;
117
118 # test if container exists on this node
119 PVE::LXC::load_config($param->{vmid});
120
121 if (!@{$param->{'extra-args'}}) {
122 die "missing command";
123 }
124 exec('lxc-attach', '-n', $param->{vmid}, '--', @{$param->{'extra-args'}});
125 }});
126
127 __PACKAGE__->register_method ({
128 name => 'fsck',
129 path => 'fsck',
130 method => 'PUT',
131 description => "Run a filesystem check (fsck) on a container volume.",
132 parameters => {
133 additionalProperties => 0,
134 properties => {
135 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
136 force => {
137 optional => 1,
138 type => 'boolean',
139 description => "Force checking, even if the filesystem seems clean",
140 default => 0,
141 },
142 device => {
143 optional => 1,
144 type => 'string',
145 description => "A volume on which to run the filesystem check",
146 enum => [PVE::LXC::mountpoint_names()],
147 },
148 },
149 },
150 returns => { type => 'null' },
151 code => sub {
152
153 my ($param) = @_;
154 my $vmid = $param->{'vmid'};
155 my $device = defined($param->{'device'}) ? $param->{'device'} : 'rootfs';
156
157 my $command = ['fsck', '-a', '-l'];
158 push(@$command, '-f') if $param->{force};
159
160 # critical path: all of this will be done while the container is locked
161 my $do_fsck = sub {
162
163 my $conf = PVE::LXC::load_config($vmid);
164 my $storage_cfg = PVE::Storage::config();
165
166 defined($conf->{$device}) || die "cannot run command on unexisting mountpoint $device\n";
167
168 my $mount_point = PVE::LXC::parse_ct_mountpoint($conf->{$device});
169 my $volid = $mount_point->{volume};
170
171 my $path;
172 my $storage_id = PVE::Storage::parse_volume_id($volid, 1);
173
174 if ($storage_id) {
175 # skip zfs datasets
176 my (undef, undef, undef, undef, undef, undef, $format) =
177 PVE::Storage::parse_volname($storage_cfg, $volid);
178 my $storage_id_cfg = PVE::Storage::storage_config($storage_cfg, $storage_id);
179
180 if (($storage_id_cfg->{'type'} eq 'zfspool') && ($format eq 'subvol')) {
181 die "cannot run command, $device does not point to a block device or volume\n";
182 }
183
184 $path = PVE::Storage::path($storage_cfg, $volid);
185
186 } else {
187 # todo: move both checks to utility function
188 # skip bind mounts
189 if ($volid =~ m|^/.+| && $volid !~ m|^/dev/.+|) {
190 die "cannot run command, $device does not point to a block device or volume\n";
191 } elsif ($volid =~ m|^/dev/.+|) {
192 # pass block devices directly
193 $path = $volid;
194 } else {
195 die "unknown storage configuration";
196 }
197 }
198
199 push(@$command, $path);
200
201 PVE::LXC::check_running($vmid) && die "cannot run command on active container\n";
202 PVE::Tools::run_command($command);
203 };
204
205 PVE::LXC::lock_container($vmid, undef, $do_fsck);
206 return undef;
207 }});
208
209 our $cmddef = {
210 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
211 my $res = shift;
212 return if !scalar(@$res);
213 my $format = "%-10s %-10s %-20s\n";
214 printf($format, 'VMID', 'Status', 'Name');
215 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
216 printf($format, $d->{vmid}, $d->{status}, $d->{name});
217 }
218 }],
219 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
220 { node => $nodename }, sub {
221 my $config = shift;
222 foreach my $k (sort (keys %$config)) {
223 next if $k eq 'digest';
224 my $v = $config->{$k};
225 if ($k eq 'description') {
226 $v = PVE::Tools::encode_text($v);
227 }
228 print "$k: $v\n";
229 }
230 }],
231 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
232
233 resize => [ "PVE::API2::LXC::Config", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
234
235 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
236 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
237
238 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
239 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
240 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
241 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
242 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
243
244 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
245
246 console => [ __PACKAGE__, 'console', ['vmid']],
247 enter => [ __PACKAGE__, 'enter', ['vmid']],
248 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
249 exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
250 fsck => [ __PACKAGE__, 'fsck', ['vmid']],
251
252 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
253 { node => $nodename }, $upid_exit ],
254
255 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
256 { node => $nodename } , $upid_exit ],
257
258 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
259
260 listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename },
261 sub {
262 my $res = shift;
263 foreach my $e (@$res) {
264 my $headline = $e->{description} || 'no-description';
265 $headline =~ s/\n.*//sg;
266 my $parent = $e->{parent} // 'no-parent';
267 printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
268 }
269 }],
270
271 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
272
273 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
274 };
275
276
277 1;
278
279 __END__
280
281 =head1 NAME
282
283 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
284
285 =head1 SYNOPSIS
286
287 =include synopsis
288
289 =head1 DESCRIPTION
290
291 pct is a tool to manages Linux Containers (LXC). You can create
292 and destroy containers, and control execution
293 (start/stop/suspend/resume). Besides that, you can use pct to set
294 parameters in the associated config file, like network configuration or
295 memory.
296
297 =head1 EXAMPLES
298
299 Create a container based on a Debian template
300 (provided you downloaded the template via the webgui before)
301
302 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
303
304 Start a container
305
306 pct start 100
307
308 Start a login session via getty
309
310 pct console 100
311
312 Enter the lxc namespace and run a shell as root user
313
314 pct enter 100
315
316 Display the configuration
317
318 pct config 100
319
320 Add a network interface called eth0, bridged to the host bridge vmbr0,
321 set the address and gateway, while it's running
322
323 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
324
325 Reduce the memory of the container to 512MB
326
327 pct set -memory 512 100
328
329 =head1 FILES
330
331 /etc/pve/lxc/<vmid>.conf
332
333 Configuration file for the container <vmid>
334
335 =head1 SEE ALSO
336
337 L<B<qm(1)>>, L<B<pvesh(1)>>
338
339 =include pve_copyright