LXC
lxccontainer.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 
3 #ifndef __LXC_CONTAINER_H
4 #define __LXC_CONTAINER_H
5 
6 #include <malloc.h>
7 #include <semaphore.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 
12 #include <lxc/attach_options.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define LXC_CLONE_KEEPNAME (1 << 0)
19 #define LXC_CLONE_KEEPMACADDR (1 << 1)
20 #define LXC_CLONE_SNAPSHOT (1 << 2)
21 #define LXC_CLONE_KEEPBDEVTYPE (1 << 3)
22 #define LXC_CLONE_MAYBE_SNAPSHOT (1 << 4)
23 #define LXC_CLONE_MAXFLAGS (1 << 5)
24 #define LXC_CLONE_ALLOW_RUNNING (1 << 6)
25 #define LXC_CREATE_QUIET (1 << 0)
26 #define LXC_CREATE_MAXFLAGS (1 << 1)
27 #define LXC_MOUNT_API_V1 1
28 
29 struct bdev_specs;
30 
31 struct lxc_snapshot;
32 
33 struct lxc_lock;
34 
35 struct migrate_opts;
36 
37 struct lxc_console_log;
38 
39 struct lxc_mount {
40  int version;
41 };
42 
50 struct lxc_container {
51  /* private fields */
56  char *name;
57 
62  char *configfile;
63 
68  char *pidfile;
69 
74  struct lxc_lock *slock;
75 
80  struct lxc_lock *privlock;
81 
87  int numthreads;
88 
95  struct lxc_conf *lxc_conf;
96 
97  /* public fields */
99  char *error_string;
100 
103 
105  bool daemonize;
106 
108  char *config_path;
109 
117  bool (*is_defined)(struct lxc_container *c);
118 
128  const char *(*state)(struct lxc_container *c);
129 
137  bool (*is_running)(struct lxc_container *c);
138 
146  bool (*freeze)(struct lxc_container *c);
147 
155  bool (*unfreeze)(struct lxc_container *c);
156 
165  pid_t (*init_pid)(struct lxc_container *c);
166 
176  bool (*load_config)(struct lxc_container *c, const char *alt_file);
177 
187  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
188 
202  bool (*startl)(struct lxc_container *c, int useinit, ...);
203 
211  bool (*stop)(struct lxc_container *c);
212 
222  bool (*want_daemonize)(struct lxc_container *c, bool state);
223 
233  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
234 
244  char *(*config_file_name)(struct lxc_container *c);
245 
258  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
259 
269  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
270 
280  bool (*destroy)(struct lxc_container *c);
281 
290  bool (*save_config)(struct lxc_container *c, const char *alt_file);
291 
309  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
310  struct bdev_specs *specs, int flags, char *const argv[]);
311 
332  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
333  struct bdev_specs *specs, int flags, ...);
334 
343  bool (*rename)(struct lxc_container *c, const char *newname);
344 
352  bool (*reboot)(struct lxc_container *c);
353 
364  bool (*shutdown)(struct lxc_container *c, int timeout);
365 
371  void (*clear_config)(struct lxc_container *c);
372 
383  bool (*clear_config_item)(struct lxc_container *c, const char *key);
384 
403  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
404 
405 
416  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
417 
439  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
440 
451  char** (*get_interfaces)(struct lxc_container *c);
452 
467  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
468 
484  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
485 
495  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
496 
513  const char *(*get_config_path)(struct lxc_container *c);
514 
524  bool (*set_config_path)(struct lxc_container *c, const char *path);
525 
556  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
557  const char *lxcpath, int flags, const char *bdevtype,
558  const char *bdevdata, uint64_t newsize, char **hookargs);
559 
578  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *ptxfd);
579 
597  int (*console)(struct lxc_container *c, int ttynum,
598  int stdinfd, int stdoutfd, int stderrfd, int escape);
599 
613  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
614  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
615 
627  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
628 
640  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
641 
658  int (*snapshot)(struct lxc_container *c, const char *commentfile);
659 
672  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
673 
691  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
692 
701  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
702 
712  bool (*may_control)(struct lxc_container *c);
713 
724  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
725 
736  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
737 
738  /* Post LXC-1.0 additions */
739 
748  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
749 
758  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
770  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
771 
782  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
783 
794 
803 
804  /* Post LXC-1.1 additions */
814  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
815 
824  int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
825 
835  bool (*reboot2)(struct lxc_container *c, int timeout);
836 
840  int (*mount)(struct lxc_container *c, const char *source,
841  const char *target, const char *filesystemtype,
842  unsigned long mountflags, const void *data,
843  struct lxc_mount *mnt);
844 
848  int (*umount)(struct lxc_container *c, const char *target,
849  unsigned long mountflags, struct lxc_mount *mnt);
850 
858  int (*seccomp_notify_fd)(struct lxc_container *c);
859 
868 
876  int (*init_pidfd)(struct lxc_container *c);
877 
885  int (*devpts_fd)(struct lxc_container *c);
886 };
887 
891 struct lxc_snapshot {
892  char *name;
894  char *timestamp;
895  char *lxcpath;
901  void (*free)(struct lxc_snapshot *s);
902 };
903 
904 
908 struct bdev_specs {
909  char *fstype;
910  uint64_t fssize;
911  struct {
912  char *zfsroot;
913  } zfs;
914  struct {
915  char *vg;
916  char *lv;
917  char *thinpool;
918  } lvm;
919  char *dir;
920  struct {
921  char *rbdname;
922  char *rbdpool;
923  } rbd;
924 };
925 
929 enum {
930  MIGRATE_PRE_DUMP,
931  MIGRATE_DUMP,
932  MIGRATE_RESTORE,
933  MIGRATE_FEATURE_CHECK,
934 };
935 
939 #define FEATURE_MEM_TRACK (1ULL << 0)
940 #define FEATURE_LAZY_PAGES (1ULL << 1)
941 
945 struct migrate_opts {
946  /* new members should be added at the end */
947  char *directory;
948  bool verbose;
949 
950  bool stop; /* stop the container after dump? */
951  char *predump_dir; /* relative to directory above */
952  char *pageserver_address; /* where should memory pages be send? */
953  char *pageserver_port;
954 
955  /* This flag indicates whether or not the container's rootfs will have
956  * the same inodes on checkpoint and restore. In the case of e.g. zfs
957  * send or btrfs send, or an LVM snapshot, this will be true, but it
958  * won't if e.g. you rsync the filesystems between two machines.
959  */
960  bool preserves_inodes;
961 
962  /* Path to an executable script that will be registered as a criu
963  * "action script"
964  */
965  char *action_script;
966 
967  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
968  * will be enabled by default. The flag 'disable_skip_in_flight' will
969  * unconditionally disable this feature. In-flight connections are
970  * not fully established TCP connections: SYN, SYN-ACK */
971  bool disable_skip_in_flight;
972 
973  /* This is the maximum file size for deleted files (which CRIU calls
974  * "ghost" files) that will be handled. 0 indicates the CRIU default,
975  * which at this time is 1MB.
976  */
977  uint64_t ghost_limit;
978 
979  /* Some features cannot be checked by comparing the CRIU version.
980  * Features like dirty page tracking or userfaultfd depend on
981  * the architecture/kernel/criu combination. This is a bitmask
982  * in which the desired feature checks can be encoded.
983  */
984  uint64_t features_to_check;
985 };
986 
988  /* Clear the console log. */
989  bool clear;
990 
991  /* Retrieve the console log. */
992  bool read;
993 
994  /* This specifies the maximum size to read from the ringbuffer. Setting
995  * it to 0 means that the a read can be as big as the whole ringbuffer.
996  * On return callers can check how many bytes were actually read.
997  * If "read" and "clear" are set to false and a non-zero value is
998  * specified then up to "read_max" bytes of data will be discarded from
999  * the ringbuffer.
1000  */
1001  uint64_t *read_max;
1002 
1003  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
1004  * "data" is invalid.
1005  */
1006  char *data;
1007 };
1008 
1017 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1018 
1026 int lxc_container_get(struct lxc_container *c);
1027 
1038 int lxc_container_put(struct lxc_container *c);
1039 
1052 int lxc_get_wait_states(const char **states);
1053 
1061 const char *lxc_get_global_config_item(const char *key);
1062 
1069 const char *lxc_get_version(void);
1070 
1082 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1083 
1098 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1099 
1114 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1115 
1116 struct lxc_log {
1117  const char *name;
1118  const char *lxcpath;
1119  const char *file;
1120  const char *level;
1121  const char *prefix;
1122  bool quiet;
1123 };
1124 
1130 int lxc_log_init(struct lxc_log *log);
1131 
1135 void lxc_log_close(void);
1136 
1142 bool lxc_config_item_is_supported(const char *key);
1143 
1149 bool lxc_has_api_extension(const char *extension);
1150 
1151 #ifdef __cplusplus
1152 }
1153 #endif
1154 
1155 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:187
Definition: lxccontainer.h:50
bool daemonize
Definition: lxccontainer.h:105
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:155
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:748
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:484
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:736
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:597
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:802
char * error_string
Definition: lxccontainer.h:99
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:165
int(* seccomp_notify_fd_active)(struct lxc_container *c)
Retrieve a file descriptor for the running container&#39;s seccomp filter.
Definition: lxccontainer.h:867
bool(* destroy_with_snapshots)(struct lxc_container *c)
Delete the container and all its snapshots.
Definition: lxccontainer.h:793
char * vg
Definition: lxccontainer.h:915
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:758
Specifications for how to create a new backing store.
Definition: lxccontainer.h:908
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:364
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:403
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:309
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:383
uint64_t fssize
Definition: lxccontainer.h:910
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:724
int(* init_pidfd)(struct lxc_container *c)
Retrieve a pidfd for the container&#39;s init process.
Definition: lxccontainer.h:876
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:814
char * zfsroot
Definition: lxccontainer.h:912
char * timestamp
Definition: lxccontainer.h:894
bool(* checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose)
Checkpoint a container.
Definition: lxccontainer.h:770
char * name
Definition: lxccontainer.h:892
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:640
int error_num
Definition: lxccontainer.h:102
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:371
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:782
char * rbdpool
Definition: lxccontainer.h:922
char * thinpool
Definition: lxccontainer.h:917
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:840
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:352
An LXC container snapshot.
Definition: lxccontainer.h:891
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:712
char * lxcpath
Definition: lxccontainer.h:895
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:627
char * rbdname
Definition: lxccontainer.h:921
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:258
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:613
Definition: lxccontainer.h:39
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:176
bool(* snapshot_destroy)(struct lxc_container *c, const char *snapname)
Destroy the specified snapshot.
Definition: lxccontainer.h:701
int(* devpts_fd)(struct lxc_container *c)
Retrieve a mount fd for the container&#39;s devpts instance.
Definition: lxccontainer.h:885
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:233
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *ptxfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:578
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:495
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:524
Definition: lxccontainer.h:1116
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:211
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:137
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:269
char * lv
Definition: lxccontainer.h:916
Definition: lxclock.h:35
Definition: attach_options.h:90
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:146
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:128
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:672
char * fstype
Definition: lxccontainer.h:909
char * comment_pathname
Definition: lxccontainer.h:893
Definition: lxccontainer.h:987
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:202
char * config_path
Definition: lxccontainer.h:108
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:290
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:280
Options for the migrate API call.
Definition: lxccontainer.h:945
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:439
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:222
char * dir
Definition: lxccontainer.h:919
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:658