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_CLONE_ALLOW_RUNNING (1 << 6)
45 #define LXC_CREATE_QUIET (1 << 0)
46 #define LXC_CREATE_MAXFLAGS (1 << 1)
47 #define LXC_MOUNT_API_V1 1
48 
49 struct bdev_specs;
50 
51 struct lxc_snapshot;
52 
53 struct lxc_lock;
54 
55 struct migrate_opts;
56 
57 struct lxc_console_log;
58 
59 struct lxc_mount {
60  int version;
61 };
62 
70 struct lxc_container {
71  /* private fields */
76  char *name;
77 
82  char *configfile;
83 
88  char *pidfile;
89 
94  struct lxc_lock *slock;
95 
100  struct lxc_lock *privlock;
101 
107  int numthreads;
108 
115  struct lxc_conf *lxc_conf;
116 
117  /* public fields */
120 
123 
125  bool daemonize;
126 
128  char *config_path;
129 
137  bool (*is_defined)(struct lxc_container *c);
138 
148  const char *(*state)(struct lxc_container *c);
149 
157  bool (*is_running)(struct lxc_container *c);
158 
166  bool (*freeze)(struct lxc_container *c);
167 
175  bool (*unfreeze)(struct lxc_container *c);
176 
185  pid_t (*init_pid)(struct lxc_container *c);
186 
196  bool (*load_config)(struct lxc_container *c, const char *alt_file);
197 
207  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
208 
222  bool (*startl)(struct lxc_container *c, int useinit, ...);
223 
231  bool (*stop)(struct lxc_container *c);
232 
242  bool (*want_daemonize)(struct lxc_container *c, bool state);
243 
253  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
254 
264  char *(*config_file_name)(struct lxc_container *c);
265 
278  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
279 
289  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
290 
300  bool (*destroy)(struct lxc_container *c);
301 
310  bool (*save_config)(struct lxc_container *c, const char *alt_file);
311 
329  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
330  struct bdev_specs *specs, int flags, char *const argv[]);
331 
352  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
353  struct bdev_specs *specs, int flags, ...);
354 
363  bool (*rename)(struct lxc_container *c, const char *newname);
364 
372  bool (*reboot)(struct lxc_container *c);
373 
384  bool (*shutdown)(struct lxc_container *c, int timeout);
385 
391  void (*clear_config)(struct lxc_container *c);
392 
403  bool (*clear_config_item)(struct lxc_container *c, const char *key);
404 
423  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
424 
425 
436  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
437 
459  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
460 
471  char** (*get_interfaces)(struct lxc_container *c);
472 
487  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
488 
504  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
505 
515  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
516 
533  const char *(*get_config_path)(struct lxc_container *c);
534 
544  bool (*set_config_path)(struct lxc_container *c, const char *path);
545 
576  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
577  const char *lxcpath, int flags, const char *bdevtype,
578  const char *bdevdata, uint64_t newsize, char **hookargs);
579 
598  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
599 
617  int (*console)(struct lxc_container *c, int ttynum,
618  int stdinfd, int stdoutfd, int stderrfd, int escape);
619 
633  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
634  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
635 
647  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
648 
660  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
661 
678  int (*snapshot)(struct lxc_container *c, const char *commentfile);
679 
692  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
693 
711  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
712 
721  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
722 
732  bool (*may_control)(struct lxc_container *c);
733 
744  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
745 
756  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
757 
758  /* Post LXC-1.0 additions */
759 
768  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
769 
778  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
790  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
791 
802  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
803 
814 
823 
824  /* Post LXC-1.1 additions */
834  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
835 
844  int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
845 
855  bool (*reboot2)(struct lxc_container *c, int timeout);
856 
860  int (*mount)(struct lxc_container *c, const char *source,
861  const char *target, const char *filesystemtype,
862  unsigned long mountflags, const void *data,
863  struct lxc_mount *mnt);
864 
868  int (*umount)(struct lxc_container *c, const char *target,
869  unsigned long mountflags, struct lxc_mount *mnt);
870 };
871 
875 struct lxc_snapshot {
876  char *name;
878  char *timestamp;
879  char *lxcpath;
885  void (*free)(struct lxc_snapshot *s);
886 };
887 
888 
892 struct bdev_specs {
893  char *fstype;
894  uint64_t fssize;
895  struct {
896  char *zfsroot;
897  } zfs;
898  struct {
899  char *vg;
900  char *lv;
901  char *thinpool;
902  } lvm;
903  char *dir;
904  struct {
905  char *rbdname;
906  char *rbdpool;
907  } rbd;
908 };
909 
913 enum {
914  MIGRATE_PRE_DUMP,
915  MIGRATE_DUMP,
916  MIGRATE_RESTORE,
917  MIGRATE_FEATURE_CHECK,
918 };
919 
923 #define FEATURE_MEM_TRACK (1ULL << 0)
924 #define FEATURE_LAZY_PAGES (1ULL << 1)
925 
929 struct migrate_opts {
930  /* new members should be added at the end */
931  char *directory;
932  bool verbose;
933 
934  bool stop; /* stop the container after dump? */
935  char *predump_dir; /* relative to directory above */
936  char *pageserver_address; /* where should memory pages be send? */
937  char *pageserver_port;
938 
939  /* This flag indicates whether or not the container's rootfs will have
940  * the same inodes on checkpoint and restore. In the case of e.g. zfs
941  * send or btrfs send, or an LVM snapshot, this will be true, but it
942  * won't if e.g. you rsync the filesystems between two machines.
943  */
944  bool preserves_inodes;
945 
946  /* Path to an executable script that will be registered as a criu
947  * "action script"
948  */
949  char *action_script;
950 
951  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
952  * will be enabled by default. The flag 'disable_skip_in_flight' will
953  * unconditionally disable this feature. In-flight connections are
954  * not fully established TCP connections: SYN, SYN-ACK */
955  bool disable_skip_in_flight;
956 
957  /* This is the maximum file size for deleted files (which CRIU calls
958  * "ghost" files) that will be handled. 0 indicates the CRIU default,
959  * which at this time is 1MB.
960  */
961  uint64_t ghost_limit;
962 
963  /* Some features cannot be checked by comparing the CRIU version.
964  * Features like dirty page tracking or userfaultfd depend on
965  * the architecture/kernel/criu combination. This is a bitmask
966  * in which the desired feature checks can be encoded.
967  */
968  uint64_t features_to_check;
969 };
970 
972  /* Clear the console log. */
973  bool clear;
974 
975  /* Retrieve the console log. */
976  bool read;
977 
978  /* This specifies the maximum size to read from the ringbuffer. Setting
979  * it to 0 means that the a read can be as big as the whole ringbuffer.
980  * On return callers can check how many bytes were actually read.
981  * If "read" and "clear" are set to false and a non-zero value is
982  * specified then up to "read_max" bytes of data will be discarded from
983  * the ringbuffer.
984  */
985  uint64_t *read_max;
986 
987  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
988  * "data" is invalid.
989  */
990  char *data;
991 };
992 
1001 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1002 
1010 int lxc_container_get(struct lxc_container *c);
1011 
1022 int lxc_container_put(struct lxc_container *c);
1023 
1036 int lxc_get_wait_states(const char **states);
1037 
1045 const char *lxc_get_global_config_item(const char *key);
1046 
1053 const char *lxc_get_version(void);
1054 
1066 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1067 
1082 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1083 
1098 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1099 
1100 struct lxc_log {
1101  const char *name;
1102  const char *lxcpath;
1103  const char *file;
1104  const char *level;
1105  const char *prefix;
1106  bool quiet;
1107 };
1108 
1114 int lxc_log_init(struct lxc_log *log);
1115 
1119 void lxc_log_close(void);
1120 
1126 bool lxc_config_item_is_supported(const char *key);
1127 
1133 bool lxc_has_api_extension(const char *extension);
1134 
1135 #ifdef __cplusplus
1136 }
1137 #endif
1138 
1139 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:207
Definition: lxccontainer.h:70
bool daemonize
Definition: lxccontainer.h:125
int lxc_log_init(struct lxc_log *log)
Initialize the log.
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:175
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:598
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:768
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:504
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:756
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:617
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:822
char * error_string
Definition: lxccontainer.h:119
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:185
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:813
char * vg
Definition: lxccontainer.h:899
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:778
Specifications for how to create a new backing store.
Definition: lxccontainer.h:892
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:384
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:423
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:329
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:403
uint64_t fssize
Definition: lxccontainer.h:894
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:744
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:834
char * zfsroot
Definition: lxccontainer.h:896
char * timestamp
Definition: lxccontainer.h:878
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:790
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:876
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:660
int error_num
Definition: lxccontainer.h:122
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:391
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:802
char * rbdpool
Definition: lxccontainer.h:906
char * thinpool
Definition: lxccontainer.h:901
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:860
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:372
An LXC container snapshot.
Definition: lxccontainer.h:875
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:732
char * lxcpath
Definition: lxccontainer.h:879
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:647
char * rbdname
Definition: lxccontainer.h:905
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:278
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:633
Definition: lxccontainer.h:59
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:196
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:721
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:253
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:515
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:544
Definition: lxccontainer.h:1100
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:231
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:157
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:289
char * lv
Definition: lxccontainer.h:900
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:166
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:148
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:692
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:893
char * comment_pathname
Definition: lxccontainer.h:877
const char * lxc_get_version(void)
Determine version of LXC.
Definition: lxccontainer.h:971
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:222
char * config_path
Definition: lxccontainer.h:128
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:310
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:300
Options for the migrate API call.
Definition: lxccontainer.h:929
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:459
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:242
char * dir
Definition: lxccontainer.h:903
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:678