-
Notifications
You must be signed in to change notification settings - Fork 2
/
lxc.pl
executable file
·335 lines (244 loc) · 7.69 KB
/
lxc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env perl
#
# lxc - Wrapper around lxc utils to make managing containers easier
# Copyright © 2011 Shoptime Software
#
# This package is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
use warnings;
use strict;
use 5.010;
use Carp;
use Config::IniFiles;
use FindBin;
use lib "$FindBin::Bin/lib";
use Getopt::Long qw(GetOptions);
use LXC::Commands;
use Path::Class;
use Pod::Usage;
use Sysadm::Install qw(ask);
our $VERSION = '0.1.0';
my (%opt);
# If running the 'exec' command, hide the command from our option parsing
my @exec_args;
if ( scalar @ARGV > 2 && $ARGV[1] eq 'exec' ) {
@exec_args = splice(@ARGV, 2);
}
if (!GetOptions(\%opt,
'help|?',
'version',
# Other options will go here, as individual commands want them
# Only used by 'create'
'a|autostart',
'm|mirror=s',
'n|no-start',
't|template=s',
'u|user-from-host',
)) {
pod2usage(-exitval => 1, -verbose => 0);
}
push @ARGV, @exec_args;
# Actions that don't involve a command
pod2usage(-exitval => 0, -verbose => 1) if $opt{help};
if ( $opt{version} ) {
system('lxc-version');
print "lxc control script: $VERSION\n";
exit 0;
}
# Configure app
my $cfg;
my @searchpath = (
'/etc/lxc/lxc-simple.ini',
"$FindBin::Bin/lxc-simple.ini",
);
foreach my $filename ( @searchpath ) {
if ( -f $filename ) {
$cfg = Config::IniFiles->new(-file => $filename);
last;
}
}
my $app = LXC::Commands->new(
lxc_dir => Path::Class::dir($cfg ? $cfg->val('paths', 'lxc_dir') : '/var/lib/lxc'),
);
# Run command!
my $name = shift;
my $command = shift;
if ( defined $command ) {
pod2usage(-exitval => 0, -verbose => 0) unless $name;
}
else {
# For commands that don't have to operate on containers (e.g. 'status')
$command = $name;
$name = undef;
}
unless ($> == 0 || $< == 0) { die "You must be root\n" }
given ( $command ) {
when ( 'create' ) {
$app->create(
name => $name,
autostart => $opt{a},
install_user => $opt{u},
mirror => $opt{m},
start => !$opt{n},
template => $opt{t},
);
}
when ( 'destroy' ) {
$app->check_valid_container($name);
my $input = ask("Are you sure you want to destroy '$name'?", 'n');
die "Aborted\n" unless $input =~ m{^y}i;
$app->destroy(
name => $name,
);
}
when ( 'start' ) {
$app->start(
name => $name,
);
}
when ( 'stop' ) {
$app->stop(
name => $name,
);
}
when ( 'restart' ) {
$app->restart(
name => $name,
);
}
when ( 'enter' ) {
$app->enter(
name => $name,
);
}
when ( 'console' ) {
$app->console(
name => $name,
);
}
when ( 'exec' ) {
$app->enter(
name => $name,
command => \@ARGV,
);
}
when ( 'status' ) {
$app->status(
name => $name,
);
}
when ( 'autostart' ) {
$app->autostart;
}
when ( 'stopall' ) {
$app->stopall;
}
default {
die "No such command.\n\nTry $0 --help\n";
}
}
__END__
=head1 NAME
lxc - Wrapper around lxc utils to make managing containers easier
=head1 SYNOPSIS
lxc [name] [command] # When operating on a container
lxc [command] # For some commands
Commands:
lxc [name] create [-u] --template=[lucid|maverick|etc...]
lxc [name] destroy
lxc [name] start|stop|restart
lxc [name] enter
lxc [name] exec command [args]
lxc [name] console
lxc [name] status
lxc status
=head1 DESCRIPTION
C<lxc> wraps around the low-level commands for controlling linux containers, to
make it easier to manage containers for the common case - which is creating
containers that work in a similar fashion to vservers or jails.
=head1 OPTIONS
=over 4
=item B<-h|--help>
Display this documentation.
=item B<--version>
Display the version of this script, and the version of C<lxc> installed on your
system.
=back
=head1 COMMANDS
=head2 lxc [name] create
Creates a new container. Will also start it unless you pass C<-n>.
You will probably want to use the template option to specify the distribution
your container should be.
Take note of C<-u> - it can be useful if you want to set up a container for
developing software in.
=head3 Options
=over 4
=item B<-a|--autostart>
Flag this container as one that should be automatically started on boot.
=item B<-m|--mirror>
Specify an apt mirror to use inside the container (regretfully, not used for
downloading the container yet - upstream needs to offer this feature).
=item B<-n|--no-start>
Don't start the container once created (the default is to start it).
=item B<-t|--template>
Specify an LXC template to use to create the container with. This is passed
through to C<lxc-create>.
=item B<-u|--user-from-host>
If you invoke C<create> via sudo and use this option, it will bind mount /home
into the container and create a user account for you.
The user account will have the same password as your account on the host.
This option is useful when you want to create a container for developing
software in. You can use your IDE/editor setup/VCS that you have already
configured on the host, and the bind mount means the container can see all your
code.
=back
=head2 lxc [name] destroy
Destroys a container. You'll be asked to confirm first. This operation cannot
be undone!
=head2 lxc [name] start
Starts a container. It waits until networking is up in the container, which
means C<enter> will work.
=head2 lxc [name] stop
Gracefully shuts down a container (unlike the rather brutal L<lxc-stop>
command).
=head2 lxc [name] restart
Stops a container, if it's running, then starts it.
=head2 lxc [name] enter
Gives you a shell inside the container.
Under the hood, this is currently implemented with ssh, until kernels with
L<lxc-attach> support are widely available.
=head2 lxc [name] exec command [args]
Executes the command in the container.
=head2 lxc [name] console
Connects you to C<tty1> in the container.
Note that you can only do this from one place at a time, however this command
is just a layer over L<lxc-console>, so you could get more if you wanted.
However, in most cases, you'll just want to use L<lxc [name] enter> instead.
The one time this is useful is if networking is down inside the container.
=head2 lxc [name] status
Tells you the status of the container.
Currently, this is limited to whether it's running or not. This is just a
wrapper around L<lxc-info>.
=head2 lxc status
Tells you the status of all containers.
=head2 lxc autostart
Starts all containers that have a file called 'autostart' in their lxc config
directory (usually /var/lib/lxc/[name]).
This is most useful for starting containers automatically on boot.
=head2 lxc stopall
Stops all containers.
This is most useful for stopping containers automatically on shutdown.
=head1 AUTHOR
Shoptime Software and others; see CREDITS
=cut