]>
Commit | Line | Data |
---|---|---|
f76a2828 DM |
1 | #!/usr/bin/perl -T |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use lib qw(. ..); | |
6 | ||
7 | use PVE::SafeSyslog; | |
148d1cb4 | 8 | use PVE::Tools qw(extract_param); |
f76a2828 DM |
9 | use PVE::Cluster; |
10 | use PVE::INotify; | |
11 | use PVE::RPCEnvironment; | |
12 | use PVE::JSONSchema qw(get_standard_option); | |
13 | use PVE::CLIHandler; | |
14 | use PVE::API2::LXC; | |
52389a07 DM |
15 | use PVE::API2::LXC::Config; |
16 | use PVE::API2::LXC::Status; | |
17 | use PVE::API2::LXC::Snapshot; | |
f76a2828 DM |
18 | |
19 | use Data::Dumper; | |
20 | ||
21 | use base qw(PVE::CLIHandler); | |
22 | ||
23 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
24 | ||
25 | initlog ('pct'); | |
26 | ||
27 | die "please run as root\n" if $> != 0; | |
28 | ||
29 | PVE::INotify::inotify_init(); | |
30 | ||
31 | my $rpcenv = PVE::RPCEnvironment->init('cli'); | |
32 | $rpcenv->init_request(); | |
33 | $rpcenv->set_language($ENV{LANG}); | |
34 | $rpcenv->set_user('root@pam'); | |
35 | ||
36 | my $nodename = PVE::INotify::nodename(); | |
37 | ||
38 | my $upid_exit = sub { | |
39 | my $upid = shift; | |
40 | my $status = PVE::Tools::upid_read_status($upid); | |
41 | exit($status eq 'OK' ? 0 : -1); | |
42 | }; | |
43 | ||
44 | __PACKAGE__->register_method ({ | |
cf52aa57 DM |
45 | name => 'console', |
46 | path => 'console', | |
f76a2828 | 47 | method => 'GET', |
cf52aa57 | 48 | description => "Launch a console for the specified container.", |
f76a2828 DM |
49 | parameters => { |
50 | additionalProperties => 0, | |
51 | properties => { | |
68e8f3c5 | 52 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }), |
f76a2828 DM |
53 | }, |
54 | }, | |
55 | returns => { type => 'null' }, | |
56 | ||
57 | code => sub { | |
58 | my ($param) = @_; | |
59 | ||
cf52aa57 | 60 | # test if container exists on this node |
aca816ad | 61 | my $conf = PVE::LXC::load_config($param->{vmid}); |
cf52aa57 | 62 | |
aca816ad DM |
63 | my $cmd = PVE::LXC::get_console_command($param->{vmid}, $conf); |
64 | exec(@$cmd); | |
cf52aa57 DM |
65 | }}); |
66 | ||
67 | __PACKAGE__->register_method ({ | |
68 | name => 'enter', | |
69 | path => 'enter', | |
70 | method => 'GET', | |
71 | description => "Launch a shell for the specified container.", | |
72 | parameters => { | |
73 | additionalProperties => 0, | |
74 | properties => { | |
68e8f3c5 | 75 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }), |
cf52aa57 DM |
76 | }, |
77 | }, | |
78 | returns => { type => 'null' }, | |
79 | ||
80 | code => sub { | |
81 | my ($param) = @_; | |
82 | ||
83 | # test if container exists on this node | |
84 | PVE::LXC::load_config($param->{vmid}); | |
85 | ||
86 | exec('lxc-attach', '-n', $param->{vmid}); | |
f76a2828 DM |
87 | }}); |
88 | ||
0b699324 WB |
89 | __PACKAGE__->register_method ({ |
90 | name => 'exec', | |
91 | path => 'exec', | |
92 | method => 'GET', | |
93 | description => "Launch a command inside the specified container.", | |
94 | parameters => { | |
95 | additionalProperties => 0, | |
96 | properties => { | |
97 | vmid => get_standard_option('pve-vmid'), | |
98 | 'extra-args' => get_standard_option('extra-args'), | |
99 | }, | |
100 | }, | |
101 | returns => { type => 'null' }, | |
102 | ||
103 | code => sub { | |
104 | my ($param) = @_; | |
105 | ||
106 | # test if container exists on this node | |
107 | PVE::LXC::load_config($param->{vmid}); | |
108 | ||
109 | if (!@{$param->{'extra-args'}}) { | |
110 | die "missing command"; | |
111 | } | |
112 | exec('lxc-attach', '-n', $param->{vmid}, '--', @{$param->{'extra-args'}}); | |
113 | }}); | |
114 | ||
f76a2828 | 115 | my $cmddef = { |
9c2d4ce9 | 116 | #test => [ __PACKAGE__, 'test', [], {}, sub {} ], |
f76a2828 DM |
117 | list=> [ 'PVE::API2::LXC', 'vmlist', [], { node => $nodename }, sub { |
118 | my $res = shift; | |
2d0261f6 DM |
119 | return if !scalar(@$res); |
120 | my $format = "%-10s %-10s %-20s\n"; | |
121 | printf($format, 'VMID', 'Status', 'Name'); | |
122 | foreach my $d (sort {$a->{vmid} <=> $b->{vmid} } @$res) { | |
123 | printf($format, $d->{vmid}, $d->{status}, $d->{name}); | |
124 | } | |
125 | }], | |
52389a07 | 126 | config => [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'], |
f76a2828 DM |
127 | { node => $nodename }, sub { |
128 | my $config = shift; | |
129 | foreach my $k (sort (keys %$config)) { | |
130 | next if $k eq 'digest'; | |
131 | my $v = $config->{$k}; | |
132 | if ($k eq 'description') { | |
133 | $v = PVE::Tools::encode_text($v); | |
134 | } | |
135 | print "$k: $v\n"; | |
136 | } | |
137 | }], | |
52389a07 | 138 | set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }], |
ec52ac21 | 139 | |
9c2d4ce9 | 140 | create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ], |
148d1cb4 | 141 | restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ], |
f76a2828 | 142 | |
52389a07 DM |
143 | start => [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node => $nodename }, $upid_exit], |
144 | suspend => [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit], | |
145 | resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit], | |
146 | shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit], | |
147 | stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit], | |
148 | ||
cf52aa57 DM |
149 | migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit], |
150 | ||
151 | console => [ __PACKAGE__, 'console', ['vmid']], | |
152 | enter => [ __PACKAGE__, 'enter', ['vmid']], | |
0b699324 | 153 | exec => [ __PACKAGE__, 'exec', ['vmid', 'extra-args']], |
cf52aa57 | 154 | |
f76a2828 DM |
155 | destroy => [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'], |
156 | { node => $nodename }, $upid_exit ], | |
157 | ||
52389a07 | 158 | snapshot => [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'], |
489e960d | 159 | { node => $nodename } , $upid_exit ], |
57ccb3f8 | 160 | |
52389a07 | 161 | delsnapshot => [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], |
723157f6 | 162 | |
56ec6fef DM |
163 | listsnapshot => [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node => $nodename }, |
164 | sub { | |
165 | my $res = shift; | |
166 | foreach my $e (@$res) { | |
167 | my $headline = $e->{description} || 'no-description'; | |
168 | $headline =~ s/\n.*//sg; | |
169 | my $parent = $e->{parent} // 'no-parent'; | |
170 | printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline); | |
171 | } | |
172 | }], | |
173 | ||
52389a07 | 174 | rollback => [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], |
bb1ac2de DM |
175 | |
176 | template => [ "PVE::API2::LXC", 'template', ['vmid'], { node => $nodename }], | |
f76a2828 DM |
177 | }; |
178 | ||
179 | my $cmd = shift; | |
180 | ||
181 | PVE::CLIHandler::handle_cmd($cmddef, "pct", $cmd, \@ARGV, undef, $0); | |
182 | ||
183 | exit 0; | |
184 | ||
185 | __END__ | |
186 | ||
187 | =head1 NAME | |
188 | ||
081850e1 | 189 | pct - Tool to manage Linux Containers (LXC) on Proxmox VE |
f76a2828 DM |
190 | |
191 | =head1 SYNOPSIS | |
192 | ||
193 | =include synopsis | |
194 | ||
195 | =head1 DESCRIPTION | |
196 | ||
081850e1 EK |
197 | pct is a tool to manages Linux Containers (LXC). You can create |
198 | and destroy containers, and control execution | |
199 | (start/stop/suspend/resume). Besides that, you can use pct to set | |
200 | parameters in the associated config file, like network configuration or | |
201 | memory. | |
202 | ||
203 | =head1 EXAMPLES | |
204 | ||
205 | Create a container based on a Debian template | |
206 | (provided you downloaded the template via the webgui before) | |
207 | ||
208 | pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz | |
209 | ||
210 | Start a container | |
211 | ||
212 | pct start 100 | |
213 | ||
214 | Start a login session via getty | |
215 | ||
216 | pct console 100 | |
217 | ||
218 | Enter the lxc namespace and run a shell as root user | |
219 | ||
220 | pct enter 100 | |
221 | ||
222 | Display the configuration | |
223 | ||
224 | pct config 100 | |
225 | ||
226 | Add a network interface called eth0, bridged to the host bridge vmbr0, | |
227 | set the address and gateway, while it's running | |
228 | ||
229 | pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1 | |
230 | ||
231 | Reduce the memory of the container to 512MB | |
232 | ||
233 | pct set -memory 512 100 | |
234 | ||
235 | =head1 FILES | |
236 | ||
237 | /etc/pve/lxc/<vmid>.conf | |
238 | ||
239 | Configuration file for the container <vmid> | |
240 | ||
241 | =head1 SEE ALSO | |
242 | ||
243 | L<B<qm(1)>>, L<B<pvesh(1)>> | |
f76a2828 DM |
244 | |
245 | =include pve_copyright |