LXC
lxccontainer.h
Go to the documentation of this file.
1 
23 #ifndef __LXC_CONTAINER_H
24 #define __LXC_CONTAINER_H
25 
26 #include <malloc.h>
27 #include <semaphore.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 
32 #include <lxc/attach_options.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define LXC_CLONE_KEEPNAME (1 << 0)
39 #define LXC_CLONE_KEEPMACADDR (1 << 1)
40 #define LXC_CLONE_SNAPSHOT (1 << 2)
41 #define LXC_CLONE_KEEPBDEVTYPE (1 << 3)
42 #define LXC_CLONE_MAYBE_SNAPSHOT (1 << 4)
43 #define LXC_CLONE_MAXFLAGS (1 << 5)
44 #define LXC_CREATE_QUIET (1 << 0)
45 #define LXC_CREATE_MAXFLAGS (1 << 1)
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 
64 struct lxc_container {
65  /* private fields */
70  char *name;
71 
76  char *configfile;
77 
82  char *pidfile;
83 
88  struct lxc_lock *slock;
89 
94  struct lxc_lock *privlock;
95 
101  int numthreads;
102 
109  struct lxc_conf *lxc_conf;
110 
111  /* public fields */
114 
117 
119  bool daemonize;
120 
122  char *config_path;
123 
131  bool (*is_defined)(struct lxc_container *c);
132 
142  const char *(*state)(struct lxc_container *c);
143 
151  bool (*is_running)(struct lxc_container *c);
152 
160  bool (*freeze)(struct lxc_container *c);
161 
169  bool (*unfreeze)(struct lxc_container *c);
170 
179  pid_t (*init_pid)(struct lxc_container *c);
180 
190  bool (*load_config)(struct lxc_container *c, const char *alt_file);
191 
201  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
202 
216  bool (*startl)(struct lxc_container *c, int useinit, ...);
217 
225  bool (*stop)(struct lxc_container *c);
226 
236  bool (*want_daemonize)(struct lxc_container *c, bool state);
237 
247  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
248 
258  char *(*config_file_name)(struct lxc_container *c);
259 
272  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
273 
283  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
284 
294  bool (*destroy)(struct lxc_container *c);
295 
304  bool (*save_config)(struct lxc_container *c, const char *alt_file);
305 
323  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
324  struct bdev_specs *specs, int flags, char *const argv[]);
325 
346  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
347  struct bdev_specs *specs, int flags, ...);
348 
357  bool (*rename)(struct lxc_container *c, const char *newname);
358 
366  bool (*reboot)(struct lxc_container *c);
367 
378  bool (*shutdown)(struct lxc_container *c, int timeout);
379 
385  void (*clear_config)(struct lxc_container *c);
386 
397  bool (*clear_config_item)(struct lxc_container *c, const char *key);
398 
417  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
418 
419 
430  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
431 
453  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
454 
465  char** (*get_interfaces)(struct lxc_container *c);
466 
481  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
482 
498  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
499 
509  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
510 
527  const char *(*get_config_path)(struct lxc_container *c);
528 
538  bool (*set_config_path)(struct lxc_container *c, const char *path);
539 
570  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
571  const char *lxcpath, int flags, const char *bdevtype,
572  const char *bdevdata, uint64_t newsize, char **hookargs);
573 
592  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
593 
611  int (*console)(struct lxc_container *c, int ttynum,
612  int stdinfd, int stdoutfd, int stderrfd, int escape);
613 
627  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
628  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
629 
641  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
642 
654  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
655 
672  int (*snapshot)(struct lxc_container *c, const char *commentfile);
673 
686  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
687 
705  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
706 
715  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
716 
726  bool (*may_control)(struct lxc_container *c);
727 
738  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
739 
750  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
751 
752  /* Post LXC-1.0 additions */
753 
762  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
763 
772  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
784  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
785 
796  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
797 
808 
817 
818  /* Post LXC-1.1 additions */
828  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
829 
838  int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
839 
849  bool (*reboot2)(struct lxc_container *c, int timeout);
850 };
851 
855 struct lxc_snapshot {
856  char *name;
858  char *timestamp;
859  char *lxcpath;
865  void (*free)(struct lxc_snapshot *s);
866 };
867 
868 
872 struct bdev_specs {
873  char *fstype;
874  uint64_t fssize;
875  struct {
876  char *zfsroot;
877  } zfs;
878  struct {
879  char *vg;
880  char *lv;
881  char *thinpool;
882  } lvm;
883  char *dir;
884  struct {
885  char *rbdname;
886  char *rbdpool;
887  } rbd;
888 };
889 
893 enum {
894  MIGRATE_PRE_DUMP,
895  MIGRATE_DUMP,
896  MIGRATE_RESTORE,
897  MIGRATE_FEATURE_CHECK,
898 };
899 
903 #define FEATURE_MEM_TRACK (1ULL << 0)
904 #define FEATURE_LAZY_PAGES (1ULL << 1)
905 
909 struct migrate_opts {
910  /* new members should be added at the end */
911  char *directory;
912  bool verbose;
913 
914  bool stop; /* stop the container after dump? */
915  char *predump_dir; /* relative to directory above */
916  char *pageserver_address; /* where should memory pages be send? */
917  char *pageserver_port;
918 
919  /* This flag indicates whether or not the container's rootfs will have
920  * the same inodes on checkpoint and restore. In the case of e.g. zfs
921  * send or btrfs send, or an LVM snapshot, this will be true, but it
922  * won't if e.g. you rsync the filesystems between two machines.
923  */
924  bool preserves_inodes;
925 
926  /* Path to an executable script that will be registered as a criu
927  * "action script"
928  */
929  char *action_script;
930 
931  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
932  * will be enabled by default. The flag 'disable_skip_in_flight' will
933  * unconditionally disable this feature. In-flight connections are
934  * not fully established TCP connections: SYN, SYN-ACK */
935  bool disable_skip_in_flight;
936 
937  /* This is the maximum file size for deleted files (which CRIU calls
938  * "ghost" files) that will be handled. 0 indicates the CRIU default,
939  * which at this time is 1MB.
940  */
941  uint64_t ghost_limit;
942 
943  /* Some features cannot be checked by comparing the CRIU version.
944  * Features like dirty page tracking or userfaultfd depend on
945  * the architecture/kernel/criu combination. This is a bitmask
946  * in which the desired feature checks can be encoded.
947  */
948  uint64_t features_to_check;
949 };
950 
952  /* Clear the console log. */
953  bool clear;
954 
955  /* Retrieve the console log. */
956  bool read;
957 
958  /* This specifies the maximum size to read from the ringbuffer. Setting
959  * it to 0 means that the a read can be as big as the whole ringbuffer.
960  * On return callers can check how many bytes were actually read.
961  * If "read" and "clear" are set to false and a non-zero value is
962  * specified then up to "read_max" bytes of data will be discarded from
963  * the ringbuffer.
964  */
965  uint64_t *read_max;
966 
967  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
968  * "data" is invalid.
969  */
970  char *data;
971 };
972 
981 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
982 
990 int lxc_container_get(struct lxc_container *c);
991 
1002 int lxc_container_put(struct lxc_container *c);
1003 
1016 int lxc_get_wait_states(const char **states);
1017 
1025 const char *lxc_get_global_config_item(const char *key);
1026 
1033 const char *lxc_get_version(void);
1034 
1046 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1047 
1062 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1063 
1078 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1079 
1080 struct lxc_log {
1081  const char *name;
1082  const char *lxcpath;
1083  const char *file;
1084  const char *level;
1085  const char *prefix;
1086  bool quiet;
1087 };
1088 
1094 int lxc_log_init(struct lxc_log *log);
1095 
1099 void lxc_log_close(void);
1100 
1106 bool lxc_config_item_is_supported(const char *key);
1107 
1108 #ifdef __cplusplus
1109 }
1110 #endif
1111 
1112 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:201
Definition: lxccontainer.h:64
bool daemonize
Definition: lxccontainer.h:119
int lxc_log_init(struct lxc_log *log)
Initialize the log.
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:169
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:592
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:762
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:498
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:750
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:611
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:816
char * error_string
Definition: lxccontainer.h:113
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:179
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:807
char * vg
Definition: lxccontainer.h:879
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:772
Specifications for how to create a new backing store.
Definition: lxccontainer.h:872
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:378
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:417
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:323
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:397
uint64_t fssize
Definition: lxccontainer.h:874
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:738
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:828
char * zfsroot
Definition: lxccontainer.h:876
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:346
char * timestamp
Definition: lxccontainer.h:858
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:784
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:856
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:654
int error_num
Definition: lxccontainer.h:116
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:385
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:796
char * rbdpool
Definition: lxccontainer.h:886
char * thinpool
Definition: lxccontainer.h:881
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:366
An LXC container snapshot.
Definition: lxccontainer.h:855
bool(* rename)(struct lxc_container *c, const char *newname)
Rename a container.
Definition: lxccontainer.h:357
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:726
char * lxcpath
Definition: lxccontainer.h:859
int(* lxc_attach_exec_t)(void *payload)
Definition: attach_options.h:74
bool(* is_defined)(struct lxc_container *c)
Determine if /var/lib/lxc/$name/config exists.
Definition: lxccontainer.h:131
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:641
char * rbdname
Definition: lxccontainer.h:885
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:272
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:627
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:190
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:715
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:247
bool(* reboot2)(struct lxc_container *c, int timeout)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:849
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:509
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:538
Definition: lxccontainer.h:1080
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:225
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:151
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:283
char * lv
Definition: lxccontainer.h:880
bool(* snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname)
Create a new container based on a snapshot.
Definition: lxccontainer.h:705
struct lxc_container * lxc_container_new(const char *name, const char *configpath)
Create a new container.
Definition: lxclock.h:53
Definition: attach_options.h:79
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:160
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:142
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:686
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:873
int(* console_log)(struct lxc_container *c, struct lxc_console_log *log)
Query the console log of a container.
Definition: lxccontainer.h:838
char * comment_pathname
Definition: lxccontainer.h:857
const char * lxc_get_version(void)
Determine version of LXC.
Definition: lxccontainer.h:951
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:216
char * config_path
Definition: lxccontainer.h:122
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:304
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:294
Options for the migrate API call.
Definition: lxccontainer.h:909
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:453
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:236
char * dir
Definition: lxccontainer.h:883
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:672