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