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 
114  char *exit_fifo;
117 
120 
126  char *ocihookfile;
127 
132  unsigned int start_timeout;
133 
141  bool (*is_defined)(struct lxc_container *c);
142 
152  const char *(*state)(struct lxc_container *c);
153 
161  bool (*is_running)(struct lxc_container *c);
162 
170  bool (*freeze)(struct lxc_container *c);
171 
179  bool (*unfreeze)(struct lxc_container *c);
180 
189  pid_t (*init_pid)(struct lxc_container *c);
190 
200  bool (*load_config)(struct lxc_container *c, const char *alt_file);
201 
211  bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
212 
226  bool (*startl)(struct lxc_container *c, int useinit, ...);
227 
235  bool (*stop)(struct lxc_container *c);
236 
246  bool (*want_daemonize)(struct lxc_container *c, bool state);
247 
257  bool (*want_close_all_fds)(struct lxc_container *c, bool state);
258 
268  char *(*config_file_name)(struct lxc_container *c);
269 
282  bool (*wait)(struct lxc_container *c, const char *state, int timeout);
283 
293  bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
294 
304  bool (*destroy)(struct lxc_container *c);
305 
314  bool (*save_config)(struct lxc_container *c, const char *alt_file);
315 
333  bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
334  struct bdev_specs *specs, int flags, char *const argv[]);
335 
356  bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
357  struct bdev_specs *specs, int flags, ...);
358 
367  bool (*rename)(struct lxc_container *c, const char *newname);
368 
376  bool (*reboot)(struct lxc_container *c);
377 
388  bool (*shutdown)(struct lxc_container *c, int timeout);
389 
395  void (*clear_config)(struct lxc_container *c);
396 
407  bool (*clear_config_item)(struct lxc_container *c, const char *key);
408 
427  int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
428 
429 
440  char* (*get_running_config_item)(struct lxc_container *c, const char *key);
441 
463  int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
464 
475  char** (*get_interfaces)(struct lxc_container *c);
476 
491  char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
492 
508  int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
509 
519  bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
520 
537  const char *(*get_config_path)(struct lxc_container *c);
538 
548  bool (*set_config_path)(struct lxc_container *c, const char *path);
549 
580  struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
581  const char *lxcpath, int flags, const char *bdevtype,
582  const char *bdevdata, uint64_t newsize, char **hookargs);
583 
602  int (*console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd);
603 
621  int (*console)(struct lxc_container *c, int ttynum,
622  int stdinfd, int stdoutfd, int stderrfd, int escape);
623 
637  int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
638  void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
639 
651  int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
652 
664  int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
665 
682  int (*snapshot)(struct lxc_container *c, const char *commentfile);
683 
696  int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
697 
715  bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
716 
725  bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
726 
736  bool (*may_control)(struct lxc_container *c);
737 
748  bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
749 
760  bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
761 
762  /* Post LXC-1.0 additions */
763 
772  bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
773 
782  bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
794  bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
795 
806  bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
807 
818 
827 
828  /* Post LXC-1.1 additions */
838  int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
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 
864  int (*mount)(struct lxc_container *c, const char *source,
865  const char *target, const char *filesystemtype,
866  unsigned long mountflags, const void *data,
867  struct lxc_mount *mnt);
868 
872  int (*umount)(struct lxc_container *c, const char *target,
873  unsigned long mountflags, struct lxc_mount *mnt);
874 
882  int (*seccomp_notify_fd)(struct lxc_container *c);
883 
891  int (*init_pidfd)(struct lxc_container *c);
892 
901  bool (*set_container_info_file) (struct lxc_container *c, const char *info_file);
902 
911  bool (*set_terminal_init_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
912 
921  bool (*add_terminal_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
922 
923  bool (*set_terminal_winch)(struct lxc_container *c, unsigned int height, unsigned int width);
924 
925  bool (*set_exec_terminal_winch)(struct lxc_container *c, const char *suffix, unsigned int height, unsigned int width);
926 
936  bool (*want_disable_pty)(struct lxc_container *c, bool state);
937 
947  bool (*want_open_stdin)(struct lxc_container *c, bool state);
948 
957  bool (*clean_container_resource) (struct lxc_container *c, pid_t pid);
958 
968  bool (*get_container_pids)(struct lxc_container *c,pid_t **pids,size_t *pids_len);
969 
978  bool (*set_start_timeout)(struct lxc_container *c, unsigned int start_timeout);
979 };
980 
984 struct lxc_snapshot {
985  char *name;
987  char *timestamp;
988  char *lxcpath;
994  void (*free)(struct lxc_snapshot *s);
995 };
996 
997 
1001 struct bdev_specs {
1002  char *fstype;
1003  uint64_t fssize;
1004  struct {
1005  char *zfsroot;
1006  } zfs;
1007  struct {
1008  char *vg;
1009  char *lv;
1010  char *thinpool;
1011  } lvm;
1012  char *dir;
1013  struct {
1014  char *rbdname;
1015  char *rbdpool;
1016  } rbd;
1017 };
1018 
1022 enum {
1023  MIGRATE_PRE_DUMP,
1024  MIGRATE_DUMP,
1025  MIGRATE_RESTORE,
1026  MIGRATE_FEATURE_CHECK,
1027 };
1028 
1032 #define FEATURE_MEM_TRACK (1ULL << 0)
1033 #define FEATURE_LAZY_PAGES (1ULL << 1)
1034 
1039  /* new members should be added at the end */
1040  char *directory;
1041  bool verbose;
1042 
1043  bool stop; /* stop the container after dump? */
1044  char *predump_dir; /* relative to directory above */
1045  char *pageserver_address; /* where should memory pages be send? */
1046  char *pageserver_port;
1047 
1048  /* This flag indicates whether or not the container's rootfs will have
1049  * the same inodes on checkpoint and restore. In the case of e.g. zfs
1050  * send or btrfs send, or an LVM snapshot, this will be true, but it
1051  * won't if e.g. you rsync the filesystems between two machines.
1052  */
1053  bool preserves_inodes;
1054 
1055  /* Path to an executable script that will be registered as a criu
1056  * "action script"
1057  */
1058  char *action_script;
1059 
1060  /* If CRIU >= 2.4 is detected the option to skip in-flight connections
1061  * will be enabled by default. The flag 'disable_skip_in_flight' will
1062  * unconditionally disable this feature. In-flight connections are
1063  * not fully established TCP connections: SYN, SYN-ACK */
1064  bool disable_skip_in_flight;
1065 
1066  /* This is the maximum file size for deleted files (which CRIU calls
1067  * "ghost" files) that will be handled. 0 indicates the CRIU default,
1068  * which at this time is 1MB.
1069  */
1070  uint64_t ghost_limit;
1071 
1072  /* Some features cannot be checked by comparing the CRIU version.
1073  * Features like dirty page tracking or userfaultfd depend on
1074  * the architecture/kernel/criu combination. This is a bitmask
1075  * in which the desired feature checks can be encoded.
1076  */
1077  uint64_t features_to_check;
1078 };
1079 
1081  /* Clear the console log. */
1082  bool clear;
1083 
1084  /* Retrieve the console log. */
1085  bool read;
1086 
1087  /* This specifies the maximum size to read from the ringbuffer. Setting
1088  * it to 0 means that the a read can be as big as the whole ringbuffer.
1089  * On return callers can check how many bytes were actually read.
1090  * If "read" and "clear" are set to false and a non-zero value is
1091  * specified then up to "read_max" bytes of data will be discarded from
1092  * the ringbuffer.
1093  */
1094  uint64_t *read_max;
1095 
1096  /* Data that was read from the ringbuffer. If "read_max" is 0 on return
1097  * "data" is invalid.
1098  */
1099  char *data;
1100 };
1101 
1110 struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1111 
1122 struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath);
1123 
1131 int lxc_container_get(struct lxc_container *c);
1132 
1143 int lxc_container_put(struct lxc_container *c);
1144 
1157 int lxc_get_wait_states(const char **states);
1158 
1166 const char *lxc_get_global_config_item(const char *key);
1167 
1174 const char *lxc_get_version(void);
1175 
1187 int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1188 
1203 int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1204 
1219 int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1220 
1221 struct lxc_log {
1222  const char *name;
1223  const char *lxcpath;
1224  const char *file;
1225  const char *level;
1226  const char *prefix;
1227  bool quiet;
1228 };
1229 
1235 int lxc_log_init(struct lxc_log *log);
1236 
1240 void lxc_log_close(void);
1241 
1247 bool lxc_config_item_is_supported(const char *key);
1248 
1254 bool lxc_has_api_extension(const char *extension);
1255 
1256 #ifdef __cplusplus
1257 }
1258 #endif
1259 
1260 #endif
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:211
Definition: lxccontainer.h:50
bool daemonize
Definition: lxccontainer.h:105
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:179
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *masterfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:602
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:772
bool(* set_container_info_file)(struct lxc_container *c, const char *info_file)
An API call to set the path of info file.
Definition: lxccontainer.h:901
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:508
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:760
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:621
bool disable_pty
Definition: lxccontainer.h:116
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container&#39;s snapshot.
Definition: lxccontainer.h:826
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:189
bool(* destroy_with_snapshots)(struct lxc_container *c)
Delete the container and all its snapshots.
Definition: lxccontainer.h:817
char * vg
Definition: lxccontainer.h:1008
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:782
Specifications for how to create a new backing store.
Definition: lxccontainer.h:1001
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:388
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:427
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:333
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:407
uint64_t fssize
Definition: lxccontainer.h:1003
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:748
int(* init_pidfd)(struct lxc_container *c)
Retrieve a pidfd for the container&#39;s init process.
Definition: lxccontainer.h:891
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:838
bool(* get_container_pids)(struct lxc_container *c, pid_t **pids, size_t *pids_len)
An API call to get container pids.
Definition: lxccontainer.h:968
char * zfsroot
Definition: lxccontainer.h:1005
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:356
char * timestamp
Definition: lxccontainer.h:987
bool(* checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose)
Checkpoint a container.
Definition: lxccontainer.h:794
char * name
Definition: lxccontainer.h:985
int(* umount)(struct lxc_container *c, const char *target, unsigned long mountflags, struct lxc_mount *mnt)
Unmount the container&#39;s path target.
Definition: lxccontainer.h:872
bool(* want_open_stdin)(struct lxc_container *c, bool state)
Change whether the container wants to keep stdin active for parent process of container.
Definition: lxccontainer.h:947
void(* free)(struct lxc_snapshot *s)
De-allocate the snapshot.
Definition: lxccontainer.h:994
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:664
bool(* set_terminal_init_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err)
An API call to change the path of the console default fifos.
Definition: lxccontainer.h:911
int error_num
Definition: lxccontainer.h:102
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:395
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:806
bool(* set_start_timeout)(struct lxc_container *c, unsigned int start_timeout)
An API call to set start timeout.
Definition: lxccontainer.h:978
bool open_stdin
Definition: lxccontainer.h:119
char * rbdpool
Definition: lxccontainer.h:1015
char * thinpool
Definition: lxccontainer.h:1010
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:864
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:376
An LXC container snapshot.
Definition: lxccontainer.h:984
bool(* rename)(struct lxc_container *c, const char *newname)
Rename a container.
Definition: lxccontainer.h:367
int(* seccomp_notify_fd)(struct lxc_container *c)
Retrieve a file descriptor for the container&#39;s seccomp filter.
Definition: lxccontainer.h:882
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:736
char * lxcpath
Definition: lxccontainer.h:988
bool(* is_defined)(struct lxc_container *c)
Determine if /var/lib/lxc/$name/config exists.
Definition: lxccontainer.h:141
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:651
char * rbdname
Definition: lxccontainer.h:1014
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:282
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:637
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:200
bool(* snapshot_destroy)(struct lxc_container *c, const char *snapname)
Destroy the specified snapshot.
Definition: lxccontainer.h:725
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:257
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:519
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:548
Definition: lxccontainer.h:1221
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:235
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:161
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:293
char * lv
Definition: lxccontainer.h:1009
bool(* snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname)
Create a new container based on a snapshot.
Definition: lxccontainer.h:715
Definition: lxclock.h:33
Definition: attach_options.h:61
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:170
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:152
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:696
bool(* clean_container_resource)(struct lxc_container *c, pid_t pid)
An API call to clean resources of container.
Definition: lxccontainer.h:957
char * fstype
Definition: lxccontainer.h:1002
int(* console_log)(struct lxc_container *c, struct lxc_console_log *log)
Query the console log of a container.
Definition: lxccontainer.h:848
char * comment_pathname
Definition: lxccontainer.h:986
Definition: lxccontainer.h:1080
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:226
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:314
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:304
Options for the migrate API call.
Definition: lxccontainer.h:1038
bool(* add_terminal_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err)
An API call to add the path of terminal fifos.
Definition: lxccontainer.h:921
bool(* want_disable_pty)(struct lxc_container *c, bool state)
Change whether the container wants to create pty or pipes from the console log.
Definition: lxccontainer.h:936
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:463
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:246
char * dir
Definition: lxccontainer.h:1012
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:682