]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxccontainer.h
api_start: don't get a container reference for the daemonized case
[mirror_lxc.git] / src / lxc / lxccontainer.h
CommitLineData
953e611c
JH
1/*! \file
2 *
3 * liblxcapi
250b1eec
SG
4 *
5 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
6 * Copyright © 2012 Canonical Ltd.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
9be53773
SH
23#ifndef __LXC_CONTAINER_H
24#define __LXC_CONTAINER_H
72d0e1cb 25#include <malloc.h>
95ee490b 26#include <semaphore.h>
72d0e1cb 27#include <stdbool.h>
95ee490b 28#include <stdlib.h>
d659597e 29#include <stdint.h>
72d0e1cb 30
f2363e38
ÇO
31#include <lxc/attach_options.h>
32
579e783e
AM
33#ifdef __cplusplus
34extern "C" {
35#endif
36
953e611c 37#define LXC_CLONE_KEEPNAME (1 << 0) /*!< Do not edit the rootfs to change the hostname */
619256b5
ÇO
38#define LXC_CLONE_KEEPMACADDR (1 << 1) /*!< Do not change the MAC address on network interfaces */
39#define LXC_CLONE_SNAPSHOT (1 << 2) /*!< Snapshot the original filesystem(s) */
0a83cbbb
SH
40#define LXC_CLONE_KEEPBDEVTYPE (1 << 3) /*!< Use the same bdev type */
41#define LXC_CLONE_MAYBE_SNAPSHOT (1 << 4) /*!< Snapshot only if bdev supports it, else copy */
42#define LXC_CLONE_MAXFLAGS (1 << 5) /*!< Number of \c LXC_CLONE_* flags */
953e611c
JH
43#define LXC_CREATE_QUIET (1 << 0) /*!< Redirect \c stdin to \c /dev/zero and \c stdout and \c stderr to \c /dev/null */
44#define LXC_CREATE_MAXFLAGS (1 << 1) /*!< Number of \c LXC_CREATE* flags */
dc23c1c8 45
1897e3bc
SH
46struct bdev_specs;
47
f5dd1d53
SH
48struct lxc_snapshot;
49
95ee490b
SG
50struct lxc_lock;
51
953e611c
JH
52/*!
53 * An LXC container.
54 */
72d0e1cb
SG
55struct lxc_container {
56 // private fields
953e611c
JH
57 /*!
58 * \private
59 * Name of container.
60 */
72d0e1cb 61 char *name;
953e611c
JH
62
63 /*!
64 * \private
65 * Full path to configuration file.
66 */
72d0e1cb 67 char *configfile;
953e611c
JH
68
69 /*!
70 * \private
71 * Container semaphore lock.
72 */
df271a59 73 struct lxc_lock *slock;
953e611c
JH
74
75 /*!
76 * \private
77 * Container private lock.
78 */
df271a59 79 struct lxc_lock *privlock;
953e611c
JH
80
81 /*!
82 * \private
83 * Number of references to this container.
84 * \note protected by privlock.
85 */
86 int numthreads;
87
88 /*!
89 * \private
90 * Container configuration.
91 *
92 * \internal FIXME: do we want the whole lxc_handler?
93 */
94 struct lxc_conf *lxc_conf;
72d0e1cb
SG
95
96 // public fields
953e611c 97 /*! Human-readable string representing last error */
72d0e1cb 98 char *error_string;
953e611c
JH
99
100 /*! Last error number */
72d0e1cb 101 int error_num;
953e611c
JH
102
103 /*! Whether container wishes to be daemonized */
540f932a 104 bool daemonize;
72d0e1cb 105
953e611c 106 /*! Full path to configuration file */
2a59a681
SH
107 char *config_path;
108
953e611c
JH
109 /*!
110 * \brief Determine if \c /var/lib/lxc/$name/config exists.
111 *
112 * \param c Container.
113 *
114 * \return \c true if container is defined, else \c false.
115 */
116 bool (*is_defined)(struct lxc_container *c);
117
118 /*!
119 * \brief Determine state of container.
120 *
121 * \param c Container.
122 *
123 * \return Static upper-case string representing state of container.
124 *
125 * \note Returned string must not be freed.
126 */
72d0e1cb 127 const char *(*state)(struct lxc_container *c);
953e611c
JH
128
129 /*!
130 * \brief Determine if container is running.
131 *
132 * \param c Container.
133 *
134 * \return \c true on success, else \c false.
135 */
136 bool (*is_running)(struct lxc_container *c);
137
138 /*!
139 * \brief Freeze running container.
140 *
141 * \param c Container.
142 *
143 * \return \c true on success, else \c false.
144 */
72d0e1cb 145 bool (*freeze)(struct lxc_container *c);
953e611c
JH
146
147 /*!
148 * \brief Thaw a frozen container.
149 *
150 * \param c Container.
151 *
152 * \return \c true on success, else \c false.
153 */
72d0e1cb 154 bool (*unfreeze)(struct lxc_container *c);
953e611c
JH
155
156 /*!
157 * \brief Determine process ID of the containers init process.
158 *
159 * \param c Container.
f7f1ba77 160 *
953e611c
JH
161 * \return pid of init process as seen from outside the
162 * container.
163 */
72d0e1cb 164 pid_t (*init_pid)(struct lxc_container *c);
953e611c
JH
165
166 /*!
167 * \brief Load the specified configuration for the container.
168 *
169 * \param c Container.
170 * \param alt_file Full path to alternate configuration file, or
171 * \c NULL to use the default configuration file.
172 *
173 * \return \c true on success, else \c false.
174 */
12a50cc6 175 bool (*load_config)(struct lxc_container *c, const char *alt_file);
953e611c
JH
176
177 /*!
178 * \brief Start the container.
179 *
180 * \param c Container.
181 * \param useinit Use lxcinit rather than \c /sbin/init.
182 * \param argv Array of arguments to pass to init.
183 *
184 * \return \c true on success, else \c false.
185 */
12a50cc6 186 bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
953e611c
JH
187
188 /*!
189 * \brief Start the container (list variant).
190 *
191 * \param c Container.
192 * \param useinit Use lxcinit rather than \c /sbin/init.
193 * \param ... Command-line to pass to init (must end in \c NULL).
194 *
195 * \return \c true on success, else \c false.
196 *
197 * \note Identical to \ref start except that that the init
198 * arguments are specified via a list rather than an array of
199 * pointers.
200 */
72d0e1cb 201 bool (*startl)(struct lxc_container *c, int useinit, ...);
953e611c
JH
202
203 /*!
204 * \brief Stop the container.
205 *
206 * \param c Container.
207 *
208 * \return \c true on success, else \c false.
209 */
72d0e1cb 210 bool (*stop)(struct lxc_container *c);
953e611c
JH
211
212 /*!
213 * \brief Determine if the container wants to run disconnected
214 * from the terminal.
215 *
216 * \param c Container.
c9d845b5 217 * \param state Value for the daemonize bit (0 or 1).
953e611c
JH
218 *
219 * \return \c true if container wants to be daemonised, else \c false.
220 */
540f932a 221 bool (*want_daemonize)(struct lxc_container *c, bool state);
953e611c
JH
222
223 /*!
224 * \brief Determine whether container wishes all file descriptors
225 * to be closed on startup.
226 *
227 * \param c Container.
c9d845b5 228 * \param state Value for the close_all_fds bit (0 or 1).
953e611c
JH
229 *
230 * \return \c true if container wants all file descriptors closed,
231 * else \c false.
232 */
540f932a 233 bool (*want_close_all_fds)(struct lxc_container *c, bool state);
953e611c
JH
234
235 /*!
236 * \brief Return current config file name.
237 *
238 * \param c Container.
239 *
240 * \return config file name, or \c NULL on error.
241 *
242 * \note The result is allocated, so the caller must free the result.
243 */
72d0e1cb 244 char *(*config_file_name)(struct lxc_container *c);
953e611c
JH
245
246 /*!
247 * \brief Wait for container to reach a particular state.
248 *
249 * \param c Container.
250 * \param state State to wait for.
251 * \param timeout Timeout in seconds.
252 *
253 * \return \c true if state reached within \p timeout, else \c false.
254 *
255 * \note A \p timeout of \c -1 means wait forever. A \p timeout
256 * of \c 0 means do not wait.
257 */
12a50cc6 258 bool (*wait)(struct lxc_container *c, const char *state, int timeout);
953e611c
JH
259
260 /*!
261 * \brief Set a key/value configuration option.
262 *
263 * \param c Container.
264 * \param key Name of option to set.
265 * \param value Value of \p name to set.
266 *
267 * \return \c true on success, else \c false.
268 */
12a50cc6 269 bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
953e611c
JH
270
271 /*!
272 * \brief Delete the container.
273 *
274 * \param c Container.
275 *
276 * \return \c true on success, else \c false.
277 *
278 * \note Container must be stopped and have no dependent snapshots.
279 */
72d0e1cb 280 bool (*destroy)(struct lxc_container *c);
953e611c
JH
281
282 /*!
283 * \brief Save configuaration to a file.
284 *
285 * \param c Container.
286 * \param alt_file Full path to file to save configuration in.
287 *
288 * \return \c true on success, else \c false.
289 */
12a50cc6 290 bool (*save_config)(struct lxc_container *c, const char *alt_file);
953e611c
JH
291
292 /*!
293 * \brief Create a container.
294 *
295 * \param c Container (with lxcpath, name and a starting
296 * configuration set).
297 * \param t Template to execute to instantiate the root
298 * filesystem and adjust the configuration.
299 * \param bdevtype Backing store type to use (if \c NULL, \c dir will be used).
300 * \param specs Additional parameters for the backing store (for
301 * example LVM volume group to use).
302 * \param flags \c LXC_CREATE_* options (currently only \ref
303 * LXC_CREATE_QUIET is supported).
304 * \param argv Arguments to pass to the template, terminated by \c NULL (if no
305 * arguments are required, just pass \c NULL).
306 *
307 * \return \c true on success, else \c false.
308 */
1897e3bc 309 bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
dc23c1c8 310 struct bdev_specs *specs, int flags, char *const argv[]);
953e611c
JH
311
312 /*!
313 * \brief Create a container (list variant).
314 *
315 * \param c Container (with lxcpath, name and a starting
316 * configuration set).
317 * \param t Template to execute to instantiate the root
318 * filesystem and adjust the configuration.
319 * \param bdevtype Backing store type to use (if \c NULL, \c dir will be used).
320 * \param specs Additional parameters for the backing store (for
321 * example LVM volume group to use).
322 * \param flags \c LXC_CREATE_* options (currently only \ref
323 * LXC_CREATE_QUIET is supported).
324 * \param ... Command-line to pass to init (must end in \c NULL).
325 *
326 * \return \c true on success, else \c false.
327 *
328 * \note Identical to \ref create except that the template
329 * arguments are specified as a list rather than an array of
330 * pointers.
331 */
1897e3bc 332 bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
dc23c1c8 333 struct bdev_specs *specs, int flags, ...);
953e611c 334
06e5650e
ÇO
335 /*!
336 * \brief Rename a container
337 *
338 * \param c Container.
339 * \param newname New name to be used for the container.
340 *
341 * \return \c true on success, else \c false.
342 */
343 bool (*rename)(struct lxc_container *c, const char *newname);
344
953e611c
JH
345 /*!
346 * \brief Request the container reboot by sending it \c SIGINT.
347 *
348 * \param c Container.
349 *
350 * \return \c true if reboot request successful, else \c false.
351 */
3e625e2d 352 bool (*reboot)(struct lxc_container *c);
953e611c
JH
353
354 /*!
355 * \brief Request the container shutdown by sending it \c
356 * SIGPWR.
357 *
358 * \param c Container.
359 * \param timeout Seconds to wait before forcing a hard stop
360 * (value must be >0).
361 *
362 * \return \c true if configuration was loaded successfully, else \c false.
363 *
364 * \note A \p timeout of \c 0 means do not wait.
365 */
72d0e1cb 366 bool (*shutdown)(struct lxc_container *c, int timeout);
953e611c
JH
367
368 /*!
369 * \brief Completely clear the containers in-memory configuration.
370 *
371 * \param c Container.
372 */
4df7f012 373 void (*clear_config)(struct lxc_container *c);
953e611c
JH
374
375 /*!
376 * \brief Clear a configuration item.
377 *
378 * \param c Container.
379 * \param key Name of option to clear.
380 *
381 * \return \c true on success, else \c false.
382 *
383 * \note Analog of \ref set_config_item.
384 */
12a50cc6 385 bool (*clear_config_item)(struct lxc_container *c, const char *key);
953e611c
JH
386
387 /*!
388 * \brief Retrieve the value of a config item.
389 *
390 * \param c Container.
391 * \param key Name of option to get.
392 * \param[out] retv Caller-allocated buffer to write value of \p key
393 * into (or \c NULL to determine length of value).
394 * \param inlen Length of \p retv (may be zero).
395 *
396 * \return Length of config items value, or < 0 on error.
397 *
398 * \note The caller can (and should) determine how large a buffer to allocate for
399 * \p retv by initially passing its value as \c NULL and considering the return value.
400 * This function can then be called again passing a newly-allocated suitably-sized buffer.
401 * \note If \p retv is NULL, \p inlen is ignored.
402 * \note If \p inlen is smaller than required, the value written
403 * to \p retv will be truncated.
404 */
12a50cc6 405 int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
953e611c
JH
406
407 /*!
408 * \brief Retrieve a list of config item keys given a key
409 * prefix.
410 *
411 * \param c Container.
412 * \param key Name of option to get.
413 * \param[out] retv Caller-allocated buffer to write list of keys to
414 * (or \c NULL to determine overall length of keys list).
415 * \param inlen Length of \p retv (may be zero).
416 *
417 * \return Length of keys list, or < 0 on error.
418 *
419 * \note The list values written to \p retv are separated by
420 * a newline character ('\\n').
421 * \note The caller can (and should) determine how large a buffer to allocate for
422 * \p retv by initially passing its value as \c NULL and considering the return value.
423 * This function can then be called again passing a newly-allocated suitably-sized buffer.
424 * \note If \p retv is NULL, \p inlen is ignored.
425 * \note If \p inlen is smaller than required, the value written
426 * to \p retv will be truncated.
427 */
12a50cc6 428 int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
953e611c
JH
429
430 /*!
431 * \brief Obtain a list of network interfaces.
432 * \param c Container.
433 *
434 * \return Newly-allocated array of network interfaces, or \c
435 * NULL on error.
436 *
437 * \note The returned array is allocated, so the caller must free it.
438 * \note The returned array is terminated with a \c NULL entry.
439 */
799f29ab 440 char** (*get_interfaces)(struct lxc_container *c);
953e611c
JH
441
442 /*!
443 * \brief Determine the list of container IP addresses.
444 *
445 * \param c Container.
446 * \param interface Network interface name to consider.
447 * \param family Network family (for example "inet", "inet6").
448 * \param scope IPv6 scope id (ignored if \p family is not "inet6").
449 *
450 * \return Newly-allocated array of network interfaces, or \c
451 * NULL on error.
452 *
453 * \note The returned array is allocated, so the caller must free it.
454 * \note The returned array is terminated with a \c NULL entry.
455 */
f0ca2726 456 char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
953e611c
JH
457
458 /*!
459 * \brief Retrieve the specified cgroup subsystem value for the container.
460 *
461 * \param c Container.
462 * \param subsys cgroup subsystem to retrieve.
463 * \param[out] retv Caller-allocated buffer to write value of \p
464 * subsys into (or \c NULL to determine length of value).
465 * \param inlen length of \p retv (may be zero).
466 *
467 * \return Length of \p subsys value, or < 0 on error.
468 *
469 * \note If \p retv is \c NULL, \p inlen is ignored.
470 * \note If \p inlen is smaller than required, the value written
471 * to \p retv will be truncated.
794dd120
SH
472 */
473 int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
953e611c
JH
474
475 /*!
476 * \brief Set the specified cgroup subsystem value for the container.
477 *
478 * \param c Container.
479 * \param subsys cgroup subsystem to consider.
480 * \param value Value to set for \p subsys.
481 *
482 * \return \c true on success, else \c false.
483 */
794dd120 484 bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
72d0e1cb 485
953e611c
JH
486 /*!
487 * \brief Determine full path to the containers configuration file.
488 * Each container can have a custom configuration path. However
489 * by default it will be set to either the \c LXCPATH configure
490 * variable, or the lxcpath value in the \c LXC_GLOBAL_CONF configuration
491 * file (i.e. \c /etc/lxc/lxc.conf).
492 * The value for a specific container can be changed using
493 * \ref set_config_path. There is no other way to specify this in general at the moment.
494 *
495 * \param c Container.
496 *
497 * \return Static string representing full path to configuration
498 * file.
499 *
500 * \note Returned string must not be freed.
2a59a681
SH
501 */
502 const char *(*get_config_path)(struct lxc_container *c);
953e611c
JH
503
504 /*!
505 * \brief Set the full path to the containers configuration
506 * file.
507 *
508 * \param c Container.
509 * \param path Full path to configuration file.
510 *
511 * \return \c true on success, else \c false.
512 */
2a59a681
SH
513 bool (*set_config_path)(struct lxc_container *c, const char *path);
514
953e611c
JH
515 /*!
516 * \brief Copy a stopped container.
517 *
518 * \param c Original container.
519 * \param newname New name for the container. If \c NULL, the same
520 * name is used and a new lxcpath MUST be specified.
521 * \param lxcpath lxcpath in which to create the new container. If
522 * \c NULL, the original container's lxcpath will be used.
523 * (XXX: should we use the default instead?)
524 * \param flags Additional \c LXC_CLONE* flags to change the cloning behaviour:
525 * - \ref LXC_CLONE_KEEPNAME
953e611c
JH
526 * - \ref LXC_CLONE_KEEPMACADDR
527 * - \ref LXC_CLONE_SNAPSHOT
528 * \param bdevtype Optionally force the cloned bdevtype to a specified plugin.
529 * By default the original is used (subject to snapshot requirements).
530 * \param bdevdata Information about how to create the new storage
531 * (i.e. fstype and fsdata).
cf642e10 532 * \param newsize In case of a block device backing store, an
953e611c
JH
533 * optional size. If \c 0, the original backing store's size will
534 * be used if possible. Note this only applies to the rootfs. For
535 * any other filesystems, the original size will be duplicated.
536 * \param hookargs Additional arguments to pass to the clone hook script.
537 *
538 * \return Newly-allocated copy of container \p c, or \p NULL on
539 * error.
540 *
541 * \note If devtype was not specified, and \p flags contains \ref
542 * LXC_CLONE_SNAPSHOT then use the native \p bdevtype if possible,
543 * else use an overlayfs.
9be53773
SH
544 */
545 struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
953e611c 546 const char *lxcpath, int flags, const char *bdevtype,
d659597e 547 const char *bdevdata, uint64_t newsize, char **hookargs);
9be53773 548
953e611c
JH
549 /*!
550 * \brief Allocate a console tty for the container.
0115f8fd 551 *
953e611c
JH
552 * \param c Container.
553 * \param[in,out] ttynum Terminal number to attempt to allocate,
554 * or \c -1 to allocate the first available tty.
555 * \param[out] masterfd File descriptor refering to the master side of the pty.
0115f8fd 556 *
953e611c
JH
557 * \return tty file descriptor number on success, or \c -1 on
558 * failure.
559 *
560 * \note On successful return, \p ttynum will contain the tty number
561 * that was allocated.
562 * \note The returned file descriptor is used to keep the tty
563 * allocated. The caller should call close(2) on the returned file
564 * descriptor when no longer required so that it may be allocated
565 * by another caller.
0115f8fd 566 */
b5159817
DE
567 int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
568
953e611c
JH
569 /*!
570 * \brief Allocate and run a console tty.
571 *
572 * \param c Container.
573 * \param ttynum Terminal number to attempt to allocate, \c -1 to
574 * allocate the first available tty or \c 0 to allocate the
575 * console.
576 * \param stdinfd File descriptor to read input from.
577 * \param stdoutfd File descriptor to write output to.
578 * \param stderrfd File descriptor to write error output to.
579 * \param escape The escape character (1 == 'a', 2 == 'b', ...).
b5159817 580 *
953e611c 581 * \return \c 0 on success, \c -1 on failure.
b5159817 582 *
953e611c
JH
583 * \note This function will not return until the console has been
584 * exited by the user.
b5159817
DE
585 */
586 int (*console)(struct lxc_container *c, int ttynum,
953e611c 587 int stdinfd, int stdoutfd, int stderrfd, int escape);
0115f8fd 588
953e611c
JH
589 /*!
590 * \brief Create a sub-process attached to a container and run
591 * a function inside it.
592 *
593 * \param c Container.
594 * \param exec_function Function to run.
595 * \param exec_payload Data to pass to \p exec_function.
596 * \param options \ref lxc_attach_options_t.
597 * \param[out] attached_process Process ID of process running inside
598 * container \p c that is running \p exec_function.
599 *
600 * \return \c 0 on success, \c -1 on error.
601 */
602 int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
603 void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
a0e93eeb 604
953e611c
JH
605 /*!
606 * \brief Run a program inside a container and wait for it to exit.
607 *
608 * \param c Container.
609 * \param options See \ref attach options.
610 * \param program Full path inside container of program to run.
611 * \param argv Array of arguments to pass to \p program.
612 *
613 * \return \c waitpid(2) status of exited process that ran \p
614 * program, or \c -1 on error.
615 */
a0e93eeb 616 int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
f5dd1d53 617
953e611c
JH
618 /*!
619 * \brief Run a program inside a container and wait for it to exit (list variant).
620 *
621 * \param c Container.
622 * \param options See \ref attach options.
623 * \param program Full path inside container of program to run.
624 * \param ... Command-line to pass to \p program (must end in \c NULL).
625 *
626 * \return \c waitpid(2) status of exited process that ran \p
627 * program, or \c -1 on error.
628 */
629 int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
f5dd1d53 630
953e611c
JH
631 /*!
632 * \brief Create a container snapshot.
633 *
634 * Assuming default paths, snapshots will be created as
635 * \c /var/lib/lxcsnaps/\<c\>/snap\<n\>
636 * where \c \<c\> represents the container name and \c \<n\>
637 * represents the zero-based snapshot number.
638 *
639 * \param c Container.
640 * \param commentfile Full path to file containing a description
641 * of the snapshot.
642 *
643 * \return -1 on error, or zero-based snapshot number.
f5dd1d53 644 *
953e611c 645 * \note \p commentfile may be \c NULL but this is discouraged.
f5dd1d53 646 */
f0ca2726 647 int (*snapshot)(struct lxc_container *c, const char *commentfile);
f5dd1d53 648
953e611c
JH
649 /*!
650 * \brief Obtain a list of container snapshots.
651 *
652 * \param c Container.
653 * \param[out] snapshots Dynamically-allocated Array of lxc_snapshot's.
f5dd1d53 654 *
953e611c 655 * \return Number of snapshots.
f5dd1d53 656 *
953e611c
JH
657 * \note The array returned in \p snapshots is allocated, so the caller must free it.
658 * \note To free an individual snapshot as returned in \p
659 * snapshots, call the snapshots \c free function (see \c src/tests/snapshot.c for an example).
660 */
661 int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
662
663 /*!
664 * \brief Create a new container based on a snapshot.
f5dd1d53 665 *
953e611c
JH
666 * The restored container will be a copy (not snapshot) of the snapshot,
667 * and restored in the lxcpath of the original container.
668 * \param c Container.
669 * \param snapname Name of snapshot.
670 * \param newname Name to be used for the restored snapshot.
671 * \return \c true on success, else \c false.
672 * \warning If \p newname is the same as the current container
673 * name, the container will be destroyed. However, this will
674 * fail if the snapshot is overlayfs-based, since the snapshots
675 * will pin the original container.
676 * \note As an example, if the container exists as \c /var/lib/lxc/c1, snapname might be \c 'snap0'
677 * (representing \c /var/lib/lxcsnaps/c1/snap0). If \p newname is \p c2,
678 * then \c snap0 will be copied to \c /var/lib/lxc/c2.
f5dd1d53 679 */
f0ca2726 680 bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
b494d2dd 681
953e611c
JH
682 /*!
683 * \brief Destroy the specified snapshot.
771d96b3 684 *
953e611c
JH
685 * \param c Container.
686 * \param snapname Name of snapshot.
687 *
688 * \return \c true on success, else \c false.
771d96b3 689 */
f0ca2726 690 bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
771d96b3 691
953e611c
JH
692 /*!
693 * \brief Determine if the caller may control the container.
694 *
695 * \param c Container.
696 *
697 * \return \c false if there is a control socket for the
698 * container monitor and the caller may not access it, otherwise
699 * returns \c true.
b494d2dd
SH
700 */
701 bool (*may_control)(struct lxc_container *c);
a9a0ed90 702
953e611c
JH
703 /*!
704 * \brief Add specified device to the container.
a9a0ed90 705 *
953e611c
JH
706 * \param c Container.
707 * \param src_path Full path of the device.
708 * \param dest_path Alternate path in the container (or \p NULL
709 * to use \p src_path).
710 *
711 * \return \c true on success, else \c false.
a9a0ed90 712 */
f0ca2726 713 bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
953e611c
JH
714
715 /*!
716 * \brief Remove specified device from the container.
a9a0ed90 717 *
953e611c
JH
718 * \param c Container.
719 * \param src_path Full path of the device.
720 * \param dest_path Alternate path in the container (or \p NULL
721 * to use \p src_path).
722 *
723 * \return \c true on success, else \c false.
a9a0ed90 724 */
f0ca2726 725 bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
f5dd1d53
SH
726};
727
953e611c
JH
728/*!
729 * \brief An LXC container snapshot.
730 */
f5dd1d53 731struct lxc_snapshot {
953e611c
JH
732 char *name; /*!< Name of snapshot */
733 char *comment_pathname; /*!< Full path to snapshots comment file (may be \c NULL) */
734 char *timestamp; /*!< Time snapshot was created */
735 char *lxcpath; /*!< Full path to LXCPATH for snapshot */
736
737 /*!
738 * \brief De-allocate the snapshot.
739 * \param s snapshot.
740 */
741 void (*free)(struct lxc_snapshot *s);
72d0e1cb
SG
742};
743
953e611c
JH
744/*!
745 * \brief Create a new container.
746 *
747 * \param name Name to use for container.
748 * \param configpath Full path to configuration file to use.
749 *
750 * \return Newly-allocated container, or \c NULL on error.
751 */
afeecbba 752struct lxc_container *lxc_container_new(const char *name, const char *configpath);
953e611c
JH
753
754/*!
755 * \brief Add a reference to the specified container.
756 *
757 * \param c Container.
758 *
759 * \return \c true on success, \c false on error.
760 */
72d0e1cb 761int lxc_container_get(struct lxc_container *c);
953e611c
JH
762
763/*!
764 * \brief Drop a reference to the specified container.
765 *
766 * \param c Container.
767 *
768 * \return \c 0 on success, \c 1 if reference was successfully dropped
769 * and container has been freed, and \c -1 on error.
770 *
771 * \warning If \c 1 is returned, \p c is no longer valid.
772 */
72d0e1cb 773int lxc_container_put(struct lxc_container *c);
953e611c
JH
774
775/*!
776 * \brief Obtain a list of all container states.
777 * \param[out] states Caller-allocated array to hold all states (may be \c NULL).
778 *
779 * \return Number of container states.
780 *
781 * \note Passing \c NULL for \p states allows the caller to first
782 * calculate how many states there are before calling the function again, the second time
783 * providing a suitably-sized array to store the static string pointers
784 * in.
785 * \note The \p states array should be freed by the caller, but not the strings the elements point to.
786 */
4a7c7daa 787int lxc_get_wait_states(const char **states);
953e611c 788
dbfa7128 789/*!
593e8478 790 * \brief Get the value for a global config key
953e611c 791 *
593e8478 792 * \param key The name of the config key
953e611c 793 *
593e8478 794 * \return String representing the current value for the key.
953e611c 795 */
593e8478 796const char *lxc_get_global_config_item(const char *key);
953e611c
JH
797
798/*!
799 * \brief Determine version of LXC.
800 * \return Static string representing version of LXC in use.
801 *
802 * \note Returned string must not be freed.
803 */
b6b918a1 804const char *lxc_get_version(void);
72d0e1cb 805
953e611c
JH
806/*!
807 * \brief Get a list of defined containers in a lxcpath.
a41f104b 808 *
953e611c
JH
809 * \param lxcpath lxcpath under which to look.
810 * \param names If not \c NULL, then a list of container names will be returned here.
811 * \param cret If not \c NULL, then a list of lxc_containers will be returned here.
812 *
813 * \return Number of containers found, or \c -1 on error.
814 *
815 * \note Values returned in \p cret are sorted by container name.
a41f104b
SH
816 */
817int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
818
953e611c
JH
819/*!
820 * \brief Get a list of active containers for a given lxcpath.
821 *
822 * \param lxcpath Full \c LXCPATH path to consider.
823 * \param[out] names Dynamically-allocated array of container names.
824 * \param[out] cret Dynamically-allocated list of containers.
825 *
826 * \return Number of containers found, or -1 on error.
a41f104b 827 *
953e611c
JH
828 * \note Some of the containers may not be "defined".
829 * \note Values returned in \p cret are sorted by container name.
830 * \note \p names and \p cret may both (or either) be specified as \c NULL.
831 * \note \p names and \p cret must be freed by the caller.
a41f104b
SH
832 */
833int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
834
953e611c
JH
835/*!
836 * \brief Get a complete list of all containers for a given lxcpath.
2871830a 837 *
953e611c
JH
838 * \param lxcpath Full \c LXCPATH path to consider.
839 * \param[out] names Dynamically-allocated array of container name.
840 * \param[out] cret Dynamically-allocated list of containers.
841 *
842 * \return Number of containers, or -1 on error.
843 *
844 * \note Some of the containers may not be "defined".
845 * \note Values returned in \p cret are sorted by container name.
846 * \note \p names and \p cret may both (or either) be specified as \c NULL.
847 * \note \p names and \p cret must be freed by the caller.
2871830a
DE
848 */
849int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
850
579e783e
AM
851#ifdef __cplusplus
852}
853#endif
854
9be53773 855#endif