LXC
lxccontainer.h
Go to the documentation of this file.
1 
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)
38 #define LXC_CLONE_KEEPMACADDR (1 << 1)
39 #define LXC_CLONE_SNAPSHOT (1 << 2)
40 #define LXC_CLONE_KEEPBDEVTYPE (1 << 3)
41 #define LXC_CLONE_MAYBE_SNAPSHOT (1 << 4)
42 #define LXC_CLONE_MAXFLAGS (1 << 5)
43 #define LXC_CREATE_QUIET (1 << 0)
44 #define LXC_CREATE_MAXFLAGS (1 << 1)
46 struct bdev_specs;
47 
48 struct lxc_snapshot;
49 
50 struct lxc_lock;
51 
52 struct migrate_opts;
53 
54 struct lxc_console_log;
55 
63 struct lxc_container {
64  /* private fields */
69  char *name;
70 
75  char *configfile;
76 
81  char *pidfile;
82 
87  struct lxc_lock *slock;
88 
93  struct lxc_lock *privlock;
94 
100  int numthreads;
101 
108  struct lxc_conf *lxc_conf;
109 
110  /* public fields */
113 
116 
118  bool daemonize;
119 
121  char *config_path;
122 
130  bool (*is_defined)(struct lxc_container *c);
131 
141  const char *(*state)(struct lxc_container *c);
142 
150  bool (*is_running)(struct lxc_container *c);
151 
159  bool (*freeze)(struct lxc_container *c);
160 
168  bool (*unfreeze)(struct lxc_container *c);
169 
178  pid_t (*init_pid)(struct lxc_container *c);
179 
189  bool (*load_config)(struct lxc_container *c, const char *alt_file);
190 
200  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
201 
215  bool (*startl)(struct lxc_container *c, int useinit, ...);
216 
224  bool (*stop)(struct lxc_container *c);
225 
235  bool (*want_daemonize)(struct lxc_container *c, bool state);
236 
246  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
247 
257  char *(*config_file_name)(struct lxc_container *c);
258 
271  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
272 
282  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
283 
293  bool (*destroy)(struct lxc_container *c);
294 
303  bool (*save_config)(struct lxc_container *c, const char *alt_file);
304 
322  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
323  struct bdev_specs *specs, int flags, char *const argv[]);
324 
345  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
346  struct bdev_specs *specs, int flags, ...);
347 
356  bool (*rename)(struct lxc_container *c, const char *newname);
357 
365  bool (*reboot)(struct lxc_container *c);
366 
377  bool (*shutdown)(struct lxc_container *c, int timeout);
378 
384  void (*clear_config)(struct lxc_container *c);
385 
396  bool (*clear_config_item)(struct lxc_container *c, const char *key);
397 
416  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
417 
418 
429  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
430 
452  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
453 
464  char** (*get_interfaces)(struct lxc_container *c);
465 
480  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
481 
497  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
498 
508  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
509 
526  const char *(*get_config_path)(struct lxc_container *c);
527 
537  bool (*set_config_path)(struct lxc_container *c, const char *path);
538 
569  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
570  const char *lxcpath, int flags, const char *bdevtype,
571  const char *bdevdata, uint64_t newsize, char **hookargs);
572 
591  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
592 
610  int (*console)(struct lxc_container *c, int ttynum,
611  int stdinfd, int stdoutfd, int stderrfd, int escape);
612 
626  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
627  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
628 
640  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
641 
653  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
654 
671  int (*snapshot)(struct lxc_container *c, const char *commentfile);
672 
685  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
686 
704  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
705 
714  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
715 
725  bool (*may_control)(struct lxc_container *c);
726 
737  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
738 
749  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
750 
751  /* Post LXC-1.0 additions */
752 
761  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
762 
771  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
783  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
784 
795  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
796 
807 
816 
817  /* Post LXC-1.1 additions */
827  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
828 
838  bool (*set_running_config_item)(struct lxc_container *c, const char *key, const char *value);
839 
848  int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
849 
859  bool (*reboot2)(struct lxc_container *c, int timeout);
860 };
861 
865 struct lxc_snapshot {
866  char *name;
868  char *timestamp;
869  char *lxcpath;
875  void (*free)(struct lxc_snapshot *s);
876 };
877 
878 
882 struct bdev_specs {
883  char *fstype;
884  uint64_t fssize;
885  struct {
886  char *zfsroot;
887  } zfs;
888  struct {
889  char *vg;
890  char *lv;
891  char *thinpool;
892  } lvm;
893  char *dir;
894  struct {
895  char *rbdname;
896  char *rbdpool;
897  } rbd;
898 };
899 
903 enum {
904  MIGRATE_PRE_DUMP,
905  MIGRATE_DUMP,
906  MIGRATE_RESTORE,
907 };
908 
912 struct migrate_opts {
913  /* new members should be added at the end */
914  char *directory;
915  bool verbose;
916 
917  bool stop; /* stop the container after dump? */
918  char *predump_dir; /* relative to directory above */
919  char *pageserver_address; /* where should memory pages be send? */
920  char *pageserver_port;
921 
922  /* This flag indicates whether or not the container's rootfs will have
923  * the same inodes on checkpoint and restore. In the case of e.g. zfs
924  * send or btrfs send, or an LVM snapshot, this will be true, but it
925  * won't if e.g. you rsync the filesystems between two machines.
926  */
927  bool preserves_inodes;
928 
929  /* Path to an executable script that will be registered as a criu
930  * "action script"
931  */
932  char *action_script;
933 
934  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
935  * will be enabled by default. The flag 'disable_skip_in_flight' will
936  * unconditionally disable this feature. In-flight connections are
937  * not fully established TCP connections: SYN, SYN-ACK */
938  bool disable_skip_in_flight;
939 
940  /* This is the maximum file size for deleted files (which CRIU calls
941  * "ghost" files) that will be handled. 0 indicates the CRIU default,
942  * which at this time is 1MB.
943  */
944  uint64_t ghost_limit;
945 };
946 
948  /* Clear the console log. */
949  bool clear;
950 
951  /* Retrieve the console log. */
952  bool read;
953 
954  /* This specifies the maximum size to read from the ringbuffer. Setting
955  * it to 0 means that the a read can be as big as the whole ringbuffer.
956  * On return callers can check how many bytes were actually read.
957  * If "read" and "clear" are set to false and a non-zero value is
958  * specified then up to "read_max" bytes of data will be discarded from
959  * the ringbuffer.
960  */
961  uint64_t *read_max;
962 
963  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
964  * "data" is invalid.
965  */
966  char *data;
967 
968  /* If a console log file was specified this flag indicates whether the
969  * contents of the ringbuffer should be written to the logfile when a
970  * request is sent to the ringbuffer.
971  */
972  bool write_logfile;
973 };
974 
983 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
984 
992 int lxc_container_get(struct lxc_container *c);
993 
1004 int lxc_container_put(struct lxc_container *c);
1005 
1018 int lxc_get_wait_states(const char **states);
1019 
1027 const char *lxc_get_global_config_item(const char *key);
1028 
1035 const char *lxc_get_version(void);
1036 
1048 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1049 
1064 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1065 
1080 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1081 
1082 struct lxc_log {
1083  const char *name;
1084  const char *lxcpath;
1085  const char *file;
1086  const char *level;
1087  const char *prefix;
1088  bool quiet;
1089 };
1090 
1096 int lxc_log_init(struct lxc_log *log);
1097 
1101 void lxc_log_close(void);
1102 
1108 bool lxc_config_item_is_supported(const char *key);
1109 
1110 #ifdef __cplusplus
1111 }
1112 #endif
1113 
1114 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:200
Definition: lxccontainer.h:63
bool daemonize
Definition: lxccontainer.h:118
int lxc_log_init(struct lxc_log *log)
Initialize the log.
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:168
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:591
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:761
int lxc_get_wait_states(const char **states)
Obtain a list of all container states.
int(* get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen)
Retrieve the specified cgroup subsystem value for the container.
Definition: lxccontainer.h:497
bool(* remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path)
Remove specified device from the container.
Definition: lxccontainer.h:749
int(* console)(struct lxc_container *c, int ttynum, int stdinfd, int stdoutfd, int stderrfd, int escape)
Allocate and run a console tty.
Definition: lxccontainer.h:610
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:815
char * error_string
Definition: lxccontainer.h:112
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:178
const char * lxc_get_global_config_item(const char *key)
Get the value for a global config key.
bool(* destroy_with_snapshots)(struct lxc_container *c)
Delete the container and all its snapshots.
Definition: lxccontainer.h:806
char * vg
Definition: lxccontainer.h:889
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:771
Specifications for how to create a new backing store.
Definition: lxccontainer.h:882
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:377
int(* get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen)
Retrieve the value of a config item.
Definition: lxccontainer.h:416
bool(* create)(struct lxc_container *c, const char *t, const char *bdevtype, struct bdev_specs *specs, int flags, char *const argv[])
Create a container.
Definition: lxccontainer.h:322
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:396
uint64_t fssize
Definition: lxccontainer.h:884
bool(* add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path)
Add specified device to the container.
Definition: lxccontainer.h:737
int(* migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size)
An API call to perform various migration operations.
Definition: lxccontainer.h:827
char * zfsroot
Definition: lxccontainer.h:886
bool(* createl)(struct lxc_container *c, const char *t, const char *bdevtype, struct bdev_specs *specs, int flags,...)
Create a container (list variant).
Definition: lxccontainer.h:345
char * timestamp
Definition: lxccontainer.h:868
bool lxc_config_item_is_supported(const char *key)
Check if the configuration item is supported by this LXC instance.
bool(* checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose)
Checkpoint a container.
Definition: lxccontainer.h:783
int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret)
Get a list of defined containers in a lxcpath.
char * name
Definition: lxccontainer.h:866
int lxc_container_put(struct lxc_container *c)
Drop a reference to the specified container.
void lxc_log_close(void)
Close log file.
int(* attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg,...)
Run a program inside a container and wait for it to exit (list variant).
Definition: lxccontainer.h:653
int error_num
Definition: lxccontainer.h:115
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:384
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:795
char * rbdpool
Definition: lxccontainer.h:896
char * thinpool
Definition: lxccontainer.h:891
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:365
An LXC container snapshot.
Definition: lxccontainer.h:865
bool(* rename)(struct lxc_container *c, const char *newname)
Rename a container.
Definition: lxccontainer.h:356
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:725
char * lxcpath
Definition: lxccontainer.h:869
int(* lxc_attach_exec_t)(void *payload)
Definition: attach_options.h:73
bool(* is_defined)(struct lxc_container *c)
Determine if /var/lib/lxc/$name/config exists.
Definition: lxccontainer.h:130
int(* attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *const argv[])
Run a program inside a container and wait for it to exit.
Definition: lxccontainer.h:640
char * rbdname
Definition: lxccontainer.h:895
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:271
int(* attach)(struct lxc_container *c, lxc_attach_exec_t exec_function, void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process)
Create a sub-process attached to a container and run a function inside it.
Definition: lxccontainer.h:626
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:189
int lxc_container_get(struct lxc_container *c)
Add a reference to the specified container.
bool(* snapshot_destroy)(struct lxc_container *c, const char *snapname)
Destroy the specified snapshot.
Definition: lxccontainer.h:714
int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret)
Get a complete list of all containers for a given lxcpath.
bool(* want_close_all_fds)(struct lxc_container *c, bool state)
Change whether the container wishes all file descriptors to be closed on startup. ...
Definition: lxccontainer.h:246
bool(* reboot2)(struct lxc_container *c, int timeout)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:859
bool(* set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value)
Set the specified cgroup subsystem value for the container.
Definition: lxccontainer.h:508
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:537
Definition: lxccontainer.h:1082
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:224
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:150
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:282
char * lv
Definition: lxccontainer.h:890
bool(* snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname)
Create a new container based on a snapshot.
Definition: lxccontainer.h:704
struct lxc_container * lxc_container_new(const char *name, const char *configpath)
Create a new container.
Definition: lxclock.h:40
Definition: attach_options.h:78
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:159
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:141
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:685
int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret)
Get a list of active containers for a given lxcpath.
char * fstype
Definition: lxccontainer.h:883
int(* console_log)(struct lxc_container *c, struct lxc_console_log *log)
Query the console log of a container.
Definition: lxccontainer.h:848
bool(* set_running_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option on a running container.
Definition: lxccontainer.h:838
char * comment_pathname
Definition: lxccontainer.h:867
const char * lxc_get_version(void)
Determine version of LXC.
Definition: lxccontainer.h:947
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:215
char * config_path
Definition: lxccontainer.h:121
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuaration to a file.
Definition: lxccontainer.h:303
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:293
Options for the migrate API call.
Definition: lxccontainer.h:912
int(* get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen)
Retrieve a list of config item keys given a key prefix.
Definition: lxccontainer.h:452
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:235
char * dir
Definition: lxccontainer.h:893
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:671