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)
46 #define LXC_MOUNT_API_V1 1
47 
48 struct bdev_specs;
49 
50 struct lxc_snapshot;
51 
52 struct lxc_lock;
53 
54 struct migrate_opts;
55 
56 struct lxc_console_log;
57 
58 struct lxc_mount {
59  int version;
60 };
61 
69 struct lxc_container {
70  /* private fields */
75  char *name;
76 
81  char *configfile;
82 
87  char *pidfile;
88 
93  struct lxc_lock *slock;
94 
99  struct lxc_lock *privlock;
100 
106  int numthreads;
107 
114  struct lxc_conf *lxc_conf;
115 
116  /* public fields */
119 
122 
124  bool daemonize;
125 
127  char *config_path;
128 
136  bool (*is_defined)(struct lxc_container *c);
137 
147  const char *(*state)(struct lxc_container *c);
148 
156  bool (*is_running)(struct lxc_container *c);
157 
165  bool (*freeze)(struct lxc_container *c);
166 
174  bool (*unfreeze)(struct lxc_container *c);
175 
184  pid_t (*init_pid)(struct lxc_container *c);
185 
195  bool (*load_config)(struct lxc_container *c, const char *alt_file);
196 
206  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
207 
221  bool (*startl)(struct lxc_container *c, int useinit, ...);
222 
230  bool (*stop)(struct lxc_container *c);
231 
241  bool (*want_daemonize)(struct lxc_container *c, bool state);
242 
252  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
253 
263  char *(*config_file_name)(struct lxc_container *c);
264 
277  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
278 
288  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
289 
299  bool (*destroy)(struct lxc_container *c);
300 
309  bool (*save_config)(struct lxc_container *c, const char *alt_file);
310 
328  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
329  struct bdev_specs *specs, int flags, char *const argv[]);
330 
351  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
352  struct bdev_specs *specs, int flags, ...);
353 
362  bool (*rename)(struct lxc_container *c, const char *newname);
363 
371  bool (*reboot)(struct lxc_container *c);
372 
383  bool (*shutdown)(struct lxc_container *c, int timeout);
384 
390  void (*clear_config)(struct lxc_container *c);
391 
402  bool (*clear_config_item)(struct lxc_container *c, const char *key);
403 
422  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
423 
424 
435  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
436 
458  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
459 
470  char** (*get_interfaces)(struct lxc_container *c);
471 
486  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
487 
503  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
504 
514  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
515 
532  const char *(*get_config_path)(struct lxc_container *c);
533 
543  bool (*set_config_path)(struct lxc_container *c, const char *path);
544 
575  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
576  const char *lxcpath, int flags, const char *bdevtype,
577  const char *bdevdata, uint64_t newsize, char **hookargs);
578 
597  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
598 
616  int (*console)(struct lxc_container *c, int ttynum,
617  int stdinfd, int stdoutfd, int stderrfd, int escape);
618 
632  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
633  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
634 
646  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
647 
659  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
660 
677  int (*snapshot)(struct lxc_container *c, const char *commentfile);
678 
691  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
692 
710  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
711 
720  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
721 
731  bool (*may_control)(struct lxc_container *c);
732 
743  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
744 
755  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
756 
757  /* Post LXC-1.0 additions */
758 
767  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
768 
777  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
789  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
790 
801  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
802 
813 
822 
823  /* Post LXC-1.1 additions */
833  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
834 
843  int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
844 
854  bool (*reboot2)(struct lxc_container *c, int timeout);
855 
859  int (*mount)(struct lxc_container *c, const char *source,
860  const char *target, const char *filesystemtype,
861  unsigned long mountflags, const void *data,
862  struct lxc_mount *mnt);
863 
867  int (*umount)(struct lxc_container *c, const char *target,
868  unsigned long mountflags, struct lxc_mount *mnt);
869 };
870 
874 struct lxc_snapshot {
875  char *name;
877  char *timestamp;
878  char *lxcpath;
884  void (*free)(struct lxc_snapshot *s);
885 };
886 
887 
891 struct bdev_specs {
892  char *fstype;
893  uint64_t fssize;
894  struct {
895  char *zfsroot;
896  } zfs;
897  struct {
898  char *vg;
899  char *lv;
900  char *thinpool;
901  } lvm;
902  char *dir;
903  struct {
904  char *rbdname;
905  char *rbdpool;
906  } rbd;
907 };
908 
912 enum {
913  MIGRATE_PRE_DUMP,
914  MIGRATE_DUMP,
915  MIGRATE_RESTORE,
916  MIGRATE_FEATURE_CHECK,
917 };
918 
922 #define FEATURE_MEM_TRACK (1ULL << 0)
923 #define FEATURE_LAZY_PAGES (1ULL << 1)
924 
928 struct migrate_opts {
929  /* new members should be added at the end */
930  char *directory;
931  bool verbose;
932 
933  bool stop; /* stop the container after dump? */
934  char *predump_dir; /* relative to directory above */
935  char *pageserver_address; /* where should memory pages be send? */
936  char *pageserver_port;
937 
938  /* This flag indicates whether or not the container's rootfs will have
939  * the same inodes on checkpoint and restore. In the case of e.g. zfs
940  * send or btrfs send, or an LVM snapshot, this will be true, but it
941  * won't if e.g. you rsync the filesystems between two machines.
942  */
943  bool preserves_inodes;
944 
945  /* Path to an executable script that will be registered as a criu
946  * "action script"
947  */
948  char *action_script;
949 
950  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
951  * will be enabled by default. The flag 'disable_skip_in_flight' will
952  * unconditionally disable this feature. In-flight connections are
953  * not fully established TCP connections: SYN, SYN-ACK */
954  bool disable_skip_in_flight;
955 
956  /* This is the maximum file size for deleted files (which CRIU calls
957  * "ghost" files) that will be handled. 0 indicates the CRIU default,
958  * which at this time is 1MB.
959  */
960  uint64_t ghost_limit;
961 
962  /* Some features cannot be checked by comparing the CRIU version.
963  * Features like dirty page tracking or userfaultfd depend on
964  * the architecture/kernel/criu combination. This is a bitmask
965  * in which the desired feature checks can be encoded.
966  */
967  uint64_t features_to_check;
968 };
969 
971  /* Clear the console log. */
972  bool clear;
973 
974  /* Retrieve the console log. */
975  bool read;
976 
977  /* This specifies the maximum size to read from the ringbuffer. Setting
978  * it to 0 means that the a read can be as big as the whole ringbuffer.
979  * On return callers can check how many bytes were actually read.
980  * If "read" and "clear" are set to false and a non-zero value is
981  * specified then up to "read_max" bytes of data will be discarded from
982  * the ringbuffer.
983  */
984  uint64_t *read_max;
985 
986  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
987  * "data" is invalid.
988  */
989  char *data;
990 };
991 
1000 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1001 
1009 int lxc_container_get(struct lxc_container *c);
1010 
1021 int lxc_container_put(struct lxc_container *c);
1022 
1035 int lxc_get_wait_states(const char **states);
1036 
1044 const char *lxc_get_global_config_item(const char *key);
1045 
1052 const char *lxc_get_version(void);
1053 
1065 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1066 
1081 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1082 
1097 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1098 
1099 struct lxc_log {
1100  const char *name;
1101  const char *lxcpath;
1102  const char *file;
1103  const char *level;
1104  const char *prefix;
1105  bool quiet;
1106 };
1107 
1113 int lxc_log_init(struct lxc_log *log);
1114 
1118 void lxc_log_close(void);
1119 
1125 bool lxc_config_item_is_supported(const char *key);
1126 
1132 bool lxc_has_api_extension(const char *extension);
1133 
1134 #ifdef __cplusplus
1135 }
1136 #endif
1137 
1138 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:206
Definition: lxccontainer.h:69
bool daemonize
Definition: lxccontainer.h:124
int lxc_log_init(struct lxc_log *log)
Initialize the log.
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:174
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:597
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:767
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:503
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:755
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:616
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:821
char * error_string
Definition: lxccontainer.h:118
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:184
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:812
char * vg
Definition: lxccontainer.h:898
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:777
Specifications for how to create a new backing store.
Definition: lxccontainer.h:891
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:383
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:422
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:328
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:402
uint64_t fssize
Definition: lxccontainer.h:893
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:743
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:833
char * zfsroot
Definition: lxccontainer.h:895
char * timestamp
Definition: lxccontainer.h:877
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:789
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:875
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:659
int error_num
Definition: lxccontainer.h:121
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:390
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:801
char * rbdpool
Definition: lxccontainer.h:905
char * thinpool
Definition: lxccontainer.h:900
int(* mount)(struct lxc_container *c, const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data, struct lxc_mount *mnt)
Mount the host&#39;s path source onto the container&#39;s path target.
Definition: lxccontainer.h:859
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:371
An LXC container snapshot.
Definition: lxccontainer.h:874
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:731
char * lxcpath
Definition: lxccontainer.h:878
int(* lxc_attach_exec_t)(void *payload)
Definition: attach_options.h:74
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:646
char * rbdname
Definition: lxccontainer.h:904
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:277
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:632
Definition: lxccontainer.h:58
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:195
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:720
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:252
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:514
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:543
Definition: lxccontainer.h:1099
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:230
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:156
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:288
char * lv
Definition: lxccontainer.h:899
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:165
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:147
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:691
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:892
char * comment_pathname
Definition: lxccontainer.h:876
const char * lxc_get_version(void)
Determine version of LXC.
Definition: lxccontainer.h:970
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:221
char * config_path
Definition: lxccontainer.h:127
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:309
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:299
Options for the migrate API call.
Definition: lxccontainer.h:928
bool lxc_has_api_extension(const char *extension)
Check if an API extension is supported by this LXC instance.
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:458
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:241
char * dir
Definition: lxccontainer.h:902
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:677