]> git.proxmox.com Git - pve-container.git/blob - src/PVE/CLI/pct.pm
Refactor lock_container into lock_config_[xx]
[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_config($vmid, 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 = $device eq 'rootfs' ? PVE::LXC::parse_ct_rootfs($conf->{$device}) :
169 PVE::LXC::parse_ct_mountpoint($conf->{$device});
170
171 my $volid = $mount_point->{volume};
172
173 my $path;
174 my $storage_id = PVE::Storage::parse_volume_id($volid, 1);
175
176 if ($storage_id) {
177 my (undef, undef, undef, undef, undef, undef, $format) =
178 PVE::Storage::parse_volname($storage_cfg, $volid);
179
180 die "unable to run fsck for '$volid' (format == $format)\n"
181 if $format ne 'raw';
182
183 $path = PVE::Storage::path($storage_cfg, $volid);
184
185 } else {
186 if (($volid =~ m|^/.+|) && (-b $volid)) {
187 # pass block devices directly
188 $path = $volid;
189 } else {
190 die "path '$volid' does not point to a block device\n";
191 }
192 }
193
194 push(@$command, $path);
195
196 PVE::LXC::check_running($vmid) &&
197 die "cannot run fsck on active container\n";
198
199 PVE::Tools::run_command($command);
200 };
201
202 PVE::LXC::lock_config($vmid, $do_fsck);
203 return undef;
204 }});
205
206 our $cmddef = {
207 list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub {
208 my $res = shift;
209 return if !scalar(@$res);
210 my $format = "%-10s %-10s %-20s\n";
211 printf($format, 'VMID', 'Status', 'Name');
212 foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) {
213 printf($format, $d->{vmid}, $d->{status}, $d->{name});
214 }
215 }],
216 config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
217 { node => $nodename }, sub {
218 my $config = shift;
219 foreach my $k (sort (keys %$config)) {
220 next if $k eq 'digest';
221 my $v = $config->{$k};
222 if ($k eq 'description') {
223 $v = PVE::Tools::encode_text($v);
224 }
225 print "$k: $v\n";
226 }
227 }],
228 set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
229
230 resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
231
232 create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
233 restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],
234
235 start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit],
236 suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit],
237 resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
238 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
239 stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
240
241 clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
242 migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
243
244 console => [ __PACKAGE__, 'console', ['vmid']],
245 enter => [ __PACKAGE__, 'enter', ['vmid']],
246 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
247 exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']],
248 fsck => [ __PACKAGE__, 'fsck', ['vmid']],
249
250 destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
251 { node => $nodename }, $upid_exit ],
252
253 snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
254 { node => $nodename } , $upid_exit ],
255
256 delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
257
258 listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename },
259 sub {
260 my $res = shift;
261 foreach my $e (@$res) {
262 my $headline = $e->{description} || 'no-description';
263 $headline =~ s/\n.*//sg;
264 my $parent = $e->{parent} // 'no-parent';
265 printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
266 }
267 }],
268
269 rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
270
271 template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }],
272 };
273
274
275 1;
276
277 __END__
278
279 =head1 NAME
280
281 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
282
283 =head1 SYNOPSIS
284
285 =include synopsis
286
287 =head1 DESCRIPTION
288
289 pct is a tool to manages Linux Containers (LXC). You can create
290 and destroy containers, and control execution
291 (start/stop/suspend/resume). Besides that, you can use pct to set
292 parameters in the associated config file, like network configuration or
293 memory.
294
295 =head1 EXAMPLES
296
297 Create a container based on a Debian template
298 (provided you downloaded the template via the webgui before)
299
300 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
301
302 Start a container
303
304 pct start 100
305
306 Start a login session via getty
307
308 pct console 100
309
310 Enter the lxc namespace and run a shell as root user
311
312 pct enter 100
313
314 Display the configuration
315
316 pct config 100
317
318 Add a network interface called eth0, bridged to the host bridge vmbr0,
319 set the address and gateway, while it's running
320
321 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
322
323 Reduce the memory of the container to 512MB
324
325 pct set -memory 512 100
326
327 =head1 FILES
328
329 /etc/pve/lxc/<vmid>.conf
330
331 Configuration file for the container <vmid>
332
333 =head1 SEE ALSO
334
335 L<B<qm(1)>>, L<B<pvesh(1)>>
336
337 =include pve_copyright