]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxccontainer.h
api_extensions: introduce lxc_has_api_extension()
[mirror_lxc.git] / src / lxc / lxccontainer.h
1 /*! \file
2 *
3 * liblxcapi
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
23 #ifndef __LXC_CONTAINER_H
24 #define __LXC_CONTAINER_H
25 #include <malloc.h>
26 #include <semaphore.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30
31 #include <lxc/attach_options.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define LXC_CLONE_KEEPNAME (1 << 0) /*!< Do not edit the rootfs to change the hostname */
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) */
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 */
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 */
45 #define LXC_MOUNT_API_V1 1
46
47 struct bdev_specs;
48
49 struct lxc_snapshot;
50
51 struct lxc_lock;
52
53 struct migrate_opts;
54
55 struct lxc_console_log;
56
57 struct lxc_mount {
58 int version;
59 };
60
61 /*!
62 * An LXC container.
63 *
64 * Note that changing the order of struct members is an API change, as callers
65 * will end up having the wrong offset when calling a function. So when making
66 * changes, whenever possible stick to simply appending new members.
67 */
68 struct lxc_container {
69 /* private fields */
70 /*!
71 * \private
72 * Name of container.
73 */
74 char *name;
75
76 /*!
77 * \private
78 * Full path to configuration file.
79 */
80 char *configfile;
81
82 /*!
83 * \private
84 * File to store pid.
85 */
86 char *pidfile;
87
88 /*!
89 * \private
90 * Container semaphore lock.
91 */
92 struct lxc_lock *slock;
93
94 /*!
95 * \private
96 * Container private lock.
97 */
98 struct lxc_lock *privlock;
99
100 /*!
101 * \private
102 * Number of references to this container.
103 * \note protected by privlock.
104 */
105 int numthreads;
106
107 /*!
108 * \private
109 * Container configuration.
110 *
111 * \internal FIXME: do we want the whole lxc_handler?
112 */
113 struct lxc_conf *lxc_conf;
114
115 /* public fields */
116 /*! Human-readable string representing last error */
117 char *error_string;
118
119 /*! Last error number */
120 int error_num;
121
122 /*! Whether container wishes to be daemonized */
123 bool daemonize;
124
125 /*! Full path to configuration file */
126 char *config_path;
127
128 /*!
129 * \brief Determine if \c /var/lib/lxc/$name/config exists.
130 *
131 * \param c Container.
132 *
133 * \return \c true if container is defined, else \c false.
134 */
135 bool (*is_defined)(struct lxc_container *c);
136
137 /*!
138 * \brief Determine state of container.
139 *
140 * \param c Container.
141 *
142 * \return Static upper-case string representing state of container.
143 *
144 * \note Returned string must not be freed.
145 */
146 const char *(*state)(struct lxc_container *c);
147
148 /*!
149 * \brief Determine if container is running.
150 *
151 * \param c Container.
152 *
153 * \return \c true on success, else \c false.
154 */
155 bool (*is_running)(struct lxc_container *c);
156
157 /*!
158 * \brief Freeze running container.
159 *
160 * \param c Container.
161 *
162 * \return \c true on success, else \c false.
163 */
164 bool (*freeze)(struct lxc_container *c);
165
166 /*!
167 * \brief Thaw a frozen container.
168 *
169 * \param c Container.
170 *
171 * \return \c true on success, else \c false.
172 */
173 bool (*unfreeze)(struct lxc_container *c);
174
175 /*!
176 * \brief Determine process ID of the containers init process.
177 *
178 * \param c Container.
179 *
180 * \return pid of init process as seen from outside the
181 * container.
182 */
183 pid_t (*init_pid)(struct lxc_container *c);
184
185 /*!
186 * \brief Load the specified configuration for the container.
187 *
188 * \param c Container.
189 * \param alt_file Full path to alternate configuration file, or
190 * \c NULL to use the default configuration file.
191 *
192 * \return \c true on success, else \c false.
193 */
194 bool (*load_config)(struct lxc_container *c, const char *alt_file);
195
196 /*!
197 * \brief Start the container.
198 *
199 * \param c Container.
200 * \param useinit Use lxcinit rather than \c /sbin/init.
201 * \param argv Array of arguments to pass to init.
202 *
203 * \return \c true on success, else \c false.
204 */
205 bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
206
207 /*!
208 * \brief Start the container (list variant).
209 *
210 * \param c Container.
211 * \param useinit Use lxcinit rather than \c /sbin/init.
212 * \param ... Command-line to pass to init (must end in \c NULL).
213 *
214 * \return \c true on success, else \c false.
215 *
216 * \note Identical to \ref start except that that the init
217 * arguments are specified via a list rather than an array of
218 * pointers.
219 */
220 bool (*startl)(struct lxc_container *c, int useinit, ...);
221
222 /*!
223 * \brief Stop the container.
224 *
225 * \param c Container.
226 *
227 * \return \c true on success, else \c false.
228 */
229 bool (*stop)(struct lxc_container *c);
230
231 /*!
232 * \brief Change whether the container wants to run disconnected
233 * from the terminal.
234 *
235 * \param c Container.
236 * \param state Value for the daemonize bit (0 or 1).
237 *
238 * \return \c true on success, else \c false.
239 */
240 bool (*want_daemonize)(struct lxc_container *c, bool state);
241
242 /*!
243 * \brief Change whether the container wishes all file descriptors
244 * to be closed on startup.
245 *
246 * \param c Container.
247 * \param state Value for the close_all_fds bit (0 or 1).
248 *
249 * \return \c true on success, else \c false.
250 */
251 bool (*want_close_all_fds)(struct lxc_container *c, bool state);
252
253 /*!
254 * \brief Return current config file name.
255 *
256 * \param c Container.
257 *
258 * \return config file name, or \c NULL on error.
259 *
260 * \note The result is allocated, so the caller must free the result.
261 */
262 char *(*config_file_name)(struct lxc_container *c);
263
264 /*!
265 * \brief Wait for container to reach a particular state.
266 *
267 * \param c Container.
268 * \param state State to wait for.
269 * \param timeout Timeout in seconds.
270 *
271 * \return \c true if state reached within \p timeout, else \c false.
272 *
273 * \note A \p timeout of \c -1 means wait forever. A \p timeout
274 * of \c 0 means do not wait.
275 */
276 bool (*wait)(struct lxc_container *c, const char *state, int timeout);
277
278 /*!
279 * \brief Set a key/value configuration option.
280 *
281 * \param c Container.
282 * \param key Name of option to set.
283 * \param value Value of \p name to set.
284 *
285 * \return \c true on success, else \c false.
286 */
287 bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
288
289 /*!
290 * \brief Delete the container.
291 *
292 * \param c Container.
293 *
294 * \return \c true on success, else \c false.
295 *
296 * \note Container must be stopped and have no dependent snapshots.
297 */
298 bool (*destroy)(struct lxc_container *c);
299
300 /*!
301 * \brief Save configuaration to a file.
302 *
303 * \param c Container.
304 * \param alt_file Full path to file to save configuration in.
305 *
306 * \return \c true on success, else \c false.
307 */
308 bool (*save_config)(struct lxc_container *c, const char *alt_file);
309
310 /*!
311 * \brief Create a container.
312 *
313 * \param c Container (with lxcpath, name and a starting
314 * configuration set).
315 * \param t Template to execute to instantiate the root
316 * filesystem and adjust the configuration.
317 * \param bdevtype Backing store type to use (if \c NULL, \c dir will be used).
318 * \param specs Additional parameters for the backing store (for
319 * example LVM volume group to use).
320 * \param flags \c LXC_CREATE_* options (currently only \ref
321 * LXC_CREATE_QUIET is supported).
322 * \param argv Arguments to pass to the template, terminated by \c NULL (if no
323 * arguments are required, just pass \c NULL).
324 *
325 * \return \c true on success, else \c false.
326 */
327 bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
328 struct bdev_specs *specs, int flags, char *const argv[]);
329
330 /*!
331 * \brief Create a container (list variant).
332 *
333 * \param c Container (with lxcpath, name and a starting
334 * configuration set).
335 * \param t Template to execute to instantiate the root
336 * filesystem and adjust the configuration.
337 * \param bdevtype Backing store type to use (if \c NULL, \c dir will be used).
338 * \param specs Additional parameters for the backing store (for
339 * example LVM volume group to use).
340 * \param flags \c LXC_CREATE_* options (currently only \ref
341 * LXC_CREATE_QUIET is supported).
342 * \param ... Command-line to pass to init (must end in \c NULL).
343 *
344 * \return \c true on success, else \c false.
345 *
346 * \note Identical to \ref create except that the template
347 * arguments are specified as a list rather than an array of
348 * pointers.
349 */
350 bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
351 struct bdev_specs *specs, int flags, ...);
352
353 /*!
354 * \brief Rename a container
355 *
356 * \param c Container.
357 * \param newname New name to be used for the container.
358 *
359 * \return \c true on success, else \c false.
360 */
361 bool (*rename)(struct lxc_container *c, const char *newname);
362
363 /*!
364 * \brief Request the container reboot by sending it \c SIGINT.
365 *
366 * \param c Container.
367 *
368 * \return \c true if reboot request successful, else \c false.
369 */
370 bool (*reboot)(struct lxc_container *c);
371
372 /*!
373 * \brief Request the container shutdown by sending it \c
374 * SIGPWR.
375 *
376 * \param c Container.
377 * \param timeout Seconds to wait before returning false.
378 * (-1 to wait forever, 0 to avoid waiting).
379 *
380 * \return \c true if the container was shutdown successfully, else \c false.
381 */
382 bool (*shutdown)(struct lxc_container *c, int timeout);
383
384 /*!
385 * \brief Completely clear the containers in-memory configuration.
386 *
387 * \param c Container.
388 */
389 void (*clear_config)(struct lxc_container *c);
390
391 /*!
392 * \brief Clear a configuration item.
393 *
394 * \param c Container.
395 * \param key Name of option to clear.
396 *
397 * \return \c true on success, else \c false.
398 *
399 * \note Analog of \ref set_config_item.
400 */
401 bool (*clear_config_item)(struct lxc_container *c, const char *key);
402
403 /*!
404 * \brief Retrieve the value of a config item.
405 *
406 * \param c Container.
407 * \param key Name of option to get.
408 * \param[out] retv Caller-allocated buffer to write value of \p key
409 * into (or \c NULL to determine length of value).
410 * \param inlen Length of \p retv (may be zero).
411 *
412 * \return Length of config items value, or < 0 on error.
413 *
414 * \note The caller can (and should) determine how large a buffer to allocate for
415 * \p retv by initially passing its value as \c NULL and considering the return value.
416 * This function can then be called again passing a newly-allocated suitably-sized buffer.
417 * \note If \p retv is NULL, \p inlen is ignored.
418 * \note If \p inlen is smaller than required, nothing will be written to \p retv and still return
419 * the length of config item value.
420 */
421 int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
422
423
424 /*!
425 * \brief Retrieve the value of a config item from running container.
426 *
427 * \param c Container.
428 * \param key Name of option to get.
429 *
430 * \return the item or NULL on error.
431 *
432 * \note Returned string must be freed by the caller.
433 */
434 char* (*get_running_config_item)(struct lxc_container *c, const char *key);
435
436 /*!
437 * \brief Retrieve a list of config item keys given a key
438 * prefix.
439 *
440 * \param c Container.
441 * \param key Name of option to get.
442 * \param[out] retv Caller-allocated buffer to write list of keys to
443 * (or \c NULL to determine overall length of keys list).
444 * \param inlen Length of \p retv (may be zero).
445 *
446 * \return Length of keys list, or < 0 on error.
447 *
448 * \note The list values written to \p retv are separated by
449 * a newline character ('\\n').
450 * \note The caller can (and should) determine how large a buffer to allocate for
451 * \p retv by initially passing its value as \c NULL and considering the return value.
452 * This function can then be called again passing a newly-allocated suitably-sized buffer.
453 * \note If \p retv is NULL, \p inlen is ignored.
454 * \note If \p inlen is smaller than required, the value written
455 * to \p retv will be truncated.
456 */
457 int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
458
459 /*!
460 * \brief Obtain a list of network interfaces.
461 * \param c Container.
462 *
463 * \return Newly-allocated array of network interfaces, or \c
464 * NULL on error.
465 *
466 * \note The returned array is allocated, so the caller must free it.
467 * \note The returned array is terminated with a \c NULL entry.
468 */
469 char** (*get_interfaces)(struct lxc_container *c);
470
471 /*!
472 * \brief Determine the list of container IP addresses.
473 *
474 * \param c Container.
475 * \param interface Network interface name to consider.
476 * \param family Network family (for example "inet", "inet6").
477 * \param scope IPv6 scope id (ignored if \p family is not "inet6").
478 *
479 * \return Newly-allocated array of network interfaces, or \c
480 * NULL on error.
481 *
482 * \note The returned array is allocated, so the caller must free it.
483 * \note The returned array is terminated with a \c NULL entry.
484 */
485 char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
486
487 /*!
488 * \brief Retrieve the specified cgroup subsystem value for the container.
489 *
490 * \param c Container.
491 * \param subsys cgroup subsystem to retrieve.
492 * \param[out] retv Caller-allocated buffer to write value of \p
493 * subsys into (or \c NULL to determine length of value).
494 * \param inlen length of \p retv (may be zero).
495 *
496 * \return Length of \p subsys value, or < 0 on error.
497 *
498 * \note If \p retv is \c NULL, \p inlen is ignored.
499 * \note If \p inlen is smaller than required, the value written
500 * to \p retv will be truncated.
501 */
502 int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
503
504 /*!
505 * \brief Set the specified cgroup subsystem value for the container.
506 *
507 * \param c Container.
508 * \param subsys cgroup subsystem to consider.
509 * \param value Value to set for \p subsys.
510 *
511 * \return \c true on success, else \c false.
512 */
513 bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
514
515 /*!
516 * \brief Determine full path to the containers configuration file.
517 * Each container can have a custom configuration path. However
518 * by default it will be set to either the \c LXCPATH configure
519 * variable, or the lxcpath value in the \c LXC_GLOBAL_CONF configuration
520 * file (i.e. \c /etc/lxc/lxc.conf).
521 * The value for a specific container can be changed using
522 * \ref set_config_path. There is no other way to specify this in general at the moment.
523 *
524 * \param c Container.
525 *
526 * \return Static string representing full path to configuration
527 * file.
528 *
529 * \note Returned string must not be freed.
530 */
531 const char *(*get_config_path)(struct lxc_container *c);
532
533 /*!
534 * \brief Set the full path to the containers configuration
535 * file.
536 *
537 * \param c Container.
538 * \param path Full path to configuration file.
539 *
540 * \return \c true on success, else \c false.
541 */
542 bool (*set_config_path)(struct lxc_container *c, const char *path);
543
544 /*!
545 * \brief Copy a stopped container.
546 *
547 * \param c Original container.
548 * \param newname New name for the container. If \c NULL, the same
549 * name is used and a new lxcpath MUST be specified.
550 * \param lxcpath lxcpath in which to create the new container. If
551 * \c NULL, the original container's lxcpath will be used.
552 * (XXX: should we use the default instead?)
553 * \param flags Additional \c LXC_CLONE* flags to change the cloning behaviour:
554 * - \ref LXC_CLONE_KEEPNAME
555 * - \ref LXC_CLONE_KEEPMACADDR
556 * - \ref LXC_CLONE_SNAPSHOT
557 * \param bdevtype Optionally force the cloned bdevtype to a specified plugin.
558 * By default the original is used (subject to snapshot requirements).
559 * \param bdevdata Information about how to create the new storage
560 * (i.e. fstype and fsdata).
561 * \param newsize In case of a block device backing store, an
562 * optional size. If \c 0, the original backing store's size will
563 * be used if possible. Note this only applies to the rootfs. For
564 * any other filesystems, the original size will be duplicated.
565 * \param hookargs Additional arguments to pass to the clone hook script.
566 *
567 * \return Newly-allocated copy of container \p c, or \p NULL on
568 * error.
569 *
570 * \note If devtype was not specified, and \p flags contains \ref
571 * LXC_CLONE_SNAPSHOT then use the native \p bdevtype if possible,
572 * else use an overlayfs.
573 */
574 struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
575 const char *lxcpath, int flags, const char *bdevtype,
576 const char *bdevdata, uint64_t newsize, char **hookargs);
577
578 /*!
579 * \brief Allocate a console tty for the container.
580 *
581 * \param c Container.
582 * \param[in,out] ttynum Terminal number to attempt to allocate,
583 * or \c -1 to allocate the first available tty.
584 * \param[out] masterfd File descriptor referring to the master side of the pty.
585 *
586 * \return tty file descriptor number on success, or \c -1 on
587 * failure.
588 *
589 * \note On successful return, \p ttynum will contain the tty number
590 * that was allocated.
591 * \note The returned file descriptor is used to keep the tty
592 * allocated. The caller should call close(2) on the returned file
593 * descriptor when no longer required so that it may be allocated
594 * by another caller.
595 */
596 int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
597
598 /*!
599 * \brief Allocate and run a console tty.
600 *
601 * \param c Container.
602 * \param ttynum Terminal number to attempt to allocate, \c -1 to
603 * allocate the first available tty or \c 0 to allocate the
604 * console.
605 * \param stdinfd File descriptor to read input from.
606 * \param stdoutfd File descriptor to write output to.
607 * \param stderrfd File descriptor to write error output to.
608 * \param escape The escape character (1 == 'a', 2 == 'b', ...).
609 *
610 * \return \c 0 on success, \c -1 on failure.
611 *
612 * \note This function will not return until the console has been
613 * exited by the user.
614 */
615 int (*console)(struct lxc_container *c, int ttynum,
616 int stdinfd, int stdoutfd, int stderrfd, int escape);
617
618 /*!
619 * \brief Create a sub-process attached to a container and run
620 * a function inside it.
621 *
622 * \param c Container.
623 * \param exec_function Function to run.
624 * \param exec_payload Data to pass to \p exec_function.
625 * \param options \ref lxc_attach_options_t.
626 * \param[out] attached_process Process ID of process running inside
627 * container \p c that is running \p exec_function.
628 *
629 * \return \c 0 on success, \c -1 on error.
630 */
631 int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
632 void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
633
634 /*!
635 * \brief Run a program inside a container and wait for it to exit.
636 *
637 * \param c Container.
638 * \param options See \ref attach options.
639 * \param program Full path inside container of program to run.
640 * \param argv Array of arguments to pass to \p program.
641 *
642 * \return \c waitpid(2) status of exited process that ran \p
643 * program, or \c -1 on error.
644 */
645 int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
646
647 /*!
648 * \brief Run a program inside a container and wait for it to exit (list variant).
649 *
650 * \param c Container.
651 * \param options See \ref attach options.
652 * \param program Full path inside container of program to run.
653 * \param ... Command-line to pass to \p program (must end in \c NULL).
654 *
655 * \return \c waitpid(2) status of exited process that ran \p
656 * program, or \c -1 on error.
657 */
658 int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
659
660 /*!
661 * \brief Create a container snapshot.
662 *
663 * Assuming default paths, snapshots will be created as
664 * \c /var/lib/lxc/\<c\>/snaps/snap\<n\>
665 * where \c \<c\> represents the container name and \c \<n\>
666 * represents the zero-based snapshot number.
667 *
668 * \param c Container.
669 * \param commentfile Full path to file containing a description
670 * of the snapshot.
671 *
672 * \return -1 on error, or zero-based snapshot number.
673 *
674 * \note \p commentfile may be \c NULL but this is discouraged.
675 */
676 int (*snapshot)(struct lxc_container *c, const char *commentfile);
677
678 /*!
679 * \brief Obtain a list of container snapshots.
680 *
681 * \param c Container.
682 * \param[out] snapshots Dynamically-allocated Array of lxc_snapshot's.
683 *
684 * \return Number of snapshots.
685 *
686 * \note The array returned in \p snapshots is allocated, so the caller must free it.
687 * \note To free an individual snapshot as returned in \p
688 * snapshots, call the snapshots \c free function (see \c src/tests/snapshot.c for an example).
689 */
690 int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
691
692 /*!
693 * \brief Create a new container based on a snapshot.
694 *
695 * The restored container will be a copy (not snapshot) of the snapshot,
696 * and restored in the lxcpath of the original container.
697 * \param c Container.
698 * \param snapname Name of snapshot.
699 * \param newname Name to be used for the restored snapshot.
700 * \return \c true on success, else \c false.
701 * \warning If \p newname is the same as the current container
702 * name, the container will be destroyed. However, this will
703 * fail if the snapshot is overlay-based, since the snapshots
704 * will pin the original container.
705 * \note As an example, if the container exists as \c /var/lib/lxc/c1, snapname might be \c 'snap0'
706 * (representing \c /var/lib/lxc/c1/snaps/snap0). If \p newname is \p c2,
707 * then \c snap0 will be copied to \c /var/lib/lxc/c2.
708 */
709 bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
710
711 /*!
712 * \brief Destroy the specified snapshot.
713 *
714 * \param c Container.
715 * \param snapname Name of snapshot.
716 *
717 * \return \c true on success, else \c false.
718 */
719 bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
720
721 /*!
722 * \brief Determine if the caller may control the container.
723 *
724 * \param c Container.
725 *
726 * \return \c false if there is a control socket for the
727 * container monitor and the caller may not access it, otherwise
728 * returns \c true.
729 */
730 bool (*may_control)(struct lxc_container *c);
731
732 /*!
733 * \brief Add specified device to the container.
734 *
735 * \param c Container.
736 * \param src_path Full path of the device.
737 * \param dest_path Alternate path in the container (or \p NULL
738 * to use \p src_path).
739 *
740 * \return \c true on success, else \c false.
741 */
742 bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
743
744 /*!
745 * \brief Remove specified device from the container.
746 *
747 * \param c Container.
748 * \param src_path Full path of the device.
749 * \param dest_path Alternate path in the container (or \p NULL
750 * to use \p src_path).
751 *
752 * \return \c true on success, else \c false.
753 */
754 bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
755
756 /* Post LXC-1.0 additions */
757
758 /*!
759 * \brief Add specified netdev to the container.
760 *
761 * \param c Container.
762 * \param dev name of net device.
763 *
764 * \return \c true on success, else \c false.
765 */
766 bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
767
768 /*!
769 * \brief Remove specified netdev from the container.
770 *
771 * \param c Container.
772 * \param dev name of net device.
773 *
774 * \return \c true on success, else \c false.
775 */
776 bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
777 /*!
778 * \brief Checkpoint a container.
779 *
780 * \param c Container.
781 * \param directory The directory to dump the container to.
782 * \param stop Whether or not to stop the container after checkpointing.
783 * \param verbose Enable criu's verbose logs.
784 *
785 * \return \c true on success, else \c false.
786 * present at compile time).
787 */
788 bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
789
790 /*!
791 * \brief Restore a container from a checkpoint.
792 *
793 * \param c Container.
794 * \param directory The directory to restore the container from.
795 * \param verbose Enable criu's verbose logs.
796 *
797 * \return \c true on success, else \c false.
798 *
799 */
800 bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
801
802 /*!
803 * \brief Delete the container and all its snapshots.
804 *
805 * \param c Container.
806 *
807 * \return \c true on success, else \c false.
808 *
809 * \note Container must be stopped.
810 */
811 bool (*destroy_with_snapshots)(struct lxc_container *c);
812
813 /*!
814 * \brief Destroy all the container's snapshot.
815 *
816 * \param c Container.
817 *
818 * \return \c true on success, else \c false.
819 */
820 bool (*snapshot_destroy_all)(struct lxc_container *c);
821
822 /* Post LXC-1.1 additions */
823 /*!
824 * \brief An API call to perform various migration operations
825 *
826 * \param cmd One of the MIGRATE_ contstants.
827 * \param opts A migrate_opts struct filled with relevant options.
828 * \param size The size of the migrate_opts struct, i.e. sizeof(struct migrate_opts).
829 *
830 * \return \c 0 on success, nonzero on failure.
831 */
832 int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
833
834 /*!
835 * \brief Query the console log of a container.
836 *
837 * \param c Container.
838 * \param opts A lxc_console_log struct filled with relevant options.
839 *
840 * \return \c 0 on success, nonzero on failure.
841 */
842 int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
843
844 /*!
845 * \brief Request the container reboot by sending it \c SIGINT.
846 *
847 * \param c Container.
848 * \param timeout Seconds to wait before returning false.
849 * (-1 to wait forever, 0 to avoid waiting).
850 *
851 * \return \c true if the container was rebooted successfully, else \c false.
852 */
853 bool (*reboot2)(struct lxc_container *c, int timeout);
854
855 /*!
856 * \brief Mount the host's path `source` onto the container's path `target`.
857 */
858 int (*mount)(struct lxc_container *c, const char *source,
859 const char *target, const char *filesystemtype,
860 unsigned long mountflags, const void *data,
861 struct lxc_mount *mnt);
862
863 /*!
864 * \brief Unmount the container's path `target`.
865 */
866 int (*umount)(struct lxc_container *c, const char *target,
867 unsigned long mountflags, struct lxc_mount *mnt);
868 };
869
870 /*!
871 * \brief An LXC container snapshot.
872 */
873 struct lxc_snapshot {
874 char *name; /*!< Name of snapshot */
875 char *comment_pathname; /*!< Full path to snapshots comment file (may be \c NULL) */
876 char *timestamp; /*!< Time snapshot was created */
877 char *lxcpath; /*!< Full path to LXCPATH for snapshot */
878
879 /*!
880 * \brief De-allocate the snapshot.
881 * \param s snapshot.
882 */
883 void (*free)(struct lxc_snapshot *s);
884 };
885
886
887 /*!
888 * \brief Specifications for how to create a new backing store
889 */
890 struct bdev_specs {
891 char *fstype; /*!< Filesystem type */
892 uint64_t fssize; /*!< Filesystem size in bytes */
893 struct {
894 char *zfsroot; /*!< ZFS root path */
895 } zfs;
896 struct {
897 char *vg; /*!< LVM Volume Group name */
898 char *lv; /*!< LVM Logical Volume name */
899 char *thinpool; /*!< LVM thin pool to use, if any */
900 } lvm;
901 char *dir; /*!< Directory path */
902 struct {
903 char *rbdname; /*!< RBD image name */
904 char *rbdpool; /*!< Ceph pool name */
905 } rbd;
906 };
907
908 /*!
909 * \brief Commands for the migrate API call.
910 */
911 enum {
912 MIGRATE_PRE_DUMP,
913 MIGRATE_DUMP,
914 MIGRATE_RESTORE,
915 MIGRATE_FEATURE_CHECK,
916 };
917
918 /*!
919 * \brief Available feature checks.
920 */
921 #define FEATURE_MEM_TRACK (1ULL << 0)
922 #define FEATURE_LAZY_PAGES (1ULL << 1)
923
924 /*!
925 * \brief Options for the migrate API call.
926 */
927 struct migrate_opts {
928 /* new members should be added at the end */
929 char *directory;
930 bool verbose;
931
932 bool stop; /* stop the container after dump? */
933 char *predump_dir; /* relative to directory above */
934 char *pageserver_address; /* where should memory pages be send? */
935 char *pageserver_port;
936
937 /* This flag indicates whether or not the container's rootfs will have
938 * the same inodes on checkpoint and restore. In the case of e.g. zfs
939 * send or btrfs send, or an LVM snapshot, this will be true, but it
940 * won't if e.g. you rsync the filesystems between two machines.
941 */
942 bool preserves_inodes;
943
944 /* Path to an executable script that will be registered as a criu
945 * "action script"
946 */
947 char *action_script;
948
949 /* If CRIU >= 2.4 is detected the option to skip in-flight connections
950 * will be enabled by default. The flag 'disable_skip_in_flight' will
951 * unconditionally disable this feature. In-flight connections are
952 * not fully established TCP connections: SYN, SYN-ACK */
953 bool disable_skip_in_flight;
954
955 /* This is the maximum file size for deleted files (which CRIU calls
956 * "ghost" files) that will be handled. 0 indicates the CRIU default,
957 * which at this time is 1MB.
958 */
959 uint64_t ghost_limit;
960
961 /* Some features cannot be checked by comparing the CRIU version.
962 * Features like dirty page tracking or userfaultfd depend on
963 * the architecture/kernel/criu combination. This is a bitmask
964 * in which the desired feature checks can be encoded.
965 */
966 uint64_t features_to_check;
967 };
968
969 struct lxc_console_log {
970 /* Clear the console log. */
971 bool clear;
972
973 /* Retrieve the console log. */
974 bool read;
975
976 /* This specifies the maximum size to read from the ringbuffer. Setting
977 * it to 0 means that the a read can be as big as the whole ringbuffer.
978 * On return callers can check how many bytes were actually read.
979 * If "read" and "clear" are set to false and a non-zero value is
980 * specified then up to "read_max" bytes of data will be discarded from
981 * the ringbuffer.
982 */
983 uint64_t *read_max;
984
985 /* Data that was read from the ringbuffer. If "read_max" is 0 on return
986 * "data" is invalid.
987 */
988 char *data;
989 };
990
991 /*!
992 * \brief Create a new container.
993 *
994 * \param name Name to use for container.
995 * \param configpath Full path to configuration file to use.
996 *
997 * \return Newly-allocated container, or \c NULL on error.
998 */
999 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1000
1001 /*!
1002 * \brief Add a reference to the specified container.
1003 *
1004 * \param c Container.
1005 *
1006 * \return \c true on success, \c false on error.
1007 */
1008 int lxc_container_get(struct lxc_container *c);
1009
1010 /*!
1011 * \brief Drop a reference to the specified container.
1012 *
1013 * \param c Container.
1014 *
1015 * \return \c 0 on success, \c 1 if reference was successfully dropped
1016 * and container has been freed, and \c -1 on error.
1017 *
1018 * \warning If \c 1 is returned, \p c is no longer valid.
1019 */
1020 int lxc_container_put(struct lxc_container *c);
1021
1022 /*!
1023 * \brief Obtain a list of all container states.
1024 * \param[out] states Caller-allocated array to hold all states (may be \c NULL).
1025 *
1026 * \return Number of container states.
1027 *
1028 * \note Passing \c NULL for \p states allows the caller to first
1029 * calculate how many states there are before calling the function again, the second time
1030 * providing a suitably-sized array to store the static string pointers
1031 * in.
1032 * \note The \p states array should be freed by the caller, but not the strings the elements point to.
1033 */
1034 int lxc_get_wait_states(const char **states);
1035
1036 /*!
1037 * \brief Get the value for a global config key
1038 *
1039 * \param key The name of the config key
1040 *
1041 * \return String representing the current value for the key.
1042 */
1043 const char *lxc_get_global_config_item(const char *key);
1044
1045 /*!
1046 * \brief Determine version of LXC.
1047 * \return Static string representing version of LXC in use.
1048 *
1049 * \note Returned string must not be freed.
1050 */
1051 const char *lxc_get_version(void);
1052
1053 /*!
1054 * \brief Get a list of defined containers in a lxcpath.
1055 *
1056 * \param lxcpath lxcpath under which to look.
1057 * \param names If not \c NULL, then a list of container names will be returned here.
1058 * \param cret If not \c NULL, then a list of lxc_containers will be returned here.
1059 *
1060 * \return Number of containers found, or \c -1 on error.
1061 *
1062 * \note Values returned in \p cret are sorted by container name.
1063 */
1064 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1065
1066 /*!
1067 * \brief Get a list of active containers for a given lxcpath.
1068 *
1069 * \param lxcpath Full \c LXCPATH path to consider.
1070 * \param[out] names Dynamically-allocated array of container names.
1071 * \param[out] cret Dynamically-allocated list of containers.
1072 *
1073 * \return Number of containers found, or -1 on error.
1074 *
1075 * \note Some of the containers may not be "defined".
1076 * \note Values returned in \p cret are sorted by container name.
1077 * \note \p names and \p cret may both (or either) be specified as \c NULL.
1078 * \note \p names and \p cret must be freed by the caller.
1079 */
1080 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1081
1082 /*!
1083 * \brief Get a complete list of all containers for a given lxcpath.
1084 *
1085 * \param lxcpath Full \c LXCPATH path to consider.
1086 * \param[out] names Dynamically-allocated array of container name.
1087 * \param[out] cret Dynamically-allocated list of containers.
1088 *
1089 * \return Number of containers, or -1 on error.
1090 *
1091 * \note Some of the containers may not be "defined".
1092 * \note Values returned in \p cret are sorted by container name.
1093 * \note \p names and \p cret may both (or either) be specified as \c NULL.
1094 * \note \p names and \p cret must be freed by the caller.
1095 */
1096 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1097
1098 struct lxc_log {
1099 const char *name;
1100 const char *lxcpath;
1101 const char *file;
1102 const char *level;
1103 const char *prefix;
1104 bool quiet;
1105 };
1106
1107 /*!
1108 *\brief Initialize the log
1109 *
1110 *\param log lxc log configuration.
1111 */
1112 int lxc_log_init(struct lxc_log *log);
1113
1114 /*!
1115 * \brief Close log file.
1116 */
1117 void lxc_log_close(void);
1118
1119 /*!
1120 * \brief Check if the configuration item is supported by this LXC instance.
1121 *
1122 * \param key Configuration item to check for.
1123 */
1124 bool lxc_config_item_is_supported(const char *key);
1125
1126 /*!
1127 * \brief Check if an API extension is supported by this LXC instance.
1128 *
1129 * \param extension API extension to check for.
1130 */
1131 bool lxc_has_api_extension(const char *extension);
1132
1133 #ifdef __cplusplus
1134 }
1135 #endif
1136
1137 #endif