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
15extern "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#ifdef HAVE_ISULAD
30#define LXC_IMAGE_OCI_KEY "lxc.imagetype.oci"
31#endif
32
33struct bdev_specs;
34
35struct lxc_snapshot;
36
37struct lxc_lock;
38
39struct migrate_opts;
40
41struct lxc_console_log;
42
43struct lxc_mount {
44 int version;
45};
46
47#ifdef HAVE_ISULAD
48struct lxc_blkio_metrics {
49 uint64_t read;
50 uint64_t write;
51 uint64_t total;
52};
53
54struct lxc_container_metrics {
55 /* State of container */
56 const char *state;
57 /* The process ID of the init container */
58 pid_t init;
59 /* Current pids */
60 uint64_t pids_current;
61 /* CPU usage */
62 uint64_t cpu_use_nanos;
63 uint64_t cpu_use_user;
64 uint64_t cpu_use_sys;
65 /* BlkIO usage */
66 struct lxc_blkio_metrics io_service_bytes;
67 struct lxc_blkio_metrics io_serviced;
68 /* Memory usage */
69 uint64_t mem_used;
70 uint64_t mem_limit;
71 uint64_t avaliable_bytes;
72 uint64_t usage_bytes;
73 uint64_t rss_bytes;
74 uint64_t page_faults;
75 uint64_t major_page_faults;
76 /* Kernel Memory usage */
77 uint64_t kmem_used;
78 uint64_t kmem_limit;
79 /* Cache usage */
80 uint64_t cache;
81 uint64_t cache_total;
82 /* total inactive file */
83 uint64_t inactive_file_total;
84};
85#endif
86
95 /* private fields */
100 char *name;
101
106 char *configfile;
107
112 char *pidfile;
113
118 struct lxc_lock *slock;
119
124 struct lxc_lock *privlock;
125
131 int numthreads;
132
139 struct lxc_conf *lxc_conf;
140
141 /* public fields */
144
147
150
153
154#ifdef HAVE_ISULAD
159 char *exit_fifo;
161 bool disable_pty;
162
164 bool open_stdin;
165
171 char *ocihookfile;
172
177 unsigned int start_timeout;
178
183 bool image_type_oci;
184#endif
185
193 bool (*is_defined)(struct lxc_container *c);
194
204 const char *(*state)(struct lxc_container *c);
205
213 bool (*is_running)(struct lxc_container *c);
214
222 bool (*freeze)(struct lxc_container *c);
223
231 bool (*unfreeze)(struct lxc_container *c);
232
241 pid_t (*init_pid)(struct lxc_container *c);
242
252 bool (*load_config)(struct lxc_container *c, const char *alt_file);
253
263 bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
264
278 bool (*startl)(struct lxc_container *c, int useinit, ...);
279
287 bool (*stop)(struct lxc_container *c);
288
298 bool (*want_daemonize)(struct lxc_container *c, bool state);
299
309 bool (*want_close_all_fds)(struct lxc_container *c, bool state);
310
320 char *(*config_file_name)(struct lxc_container *c);
321
334 bool (*wait)(struct lxc_container *c, const char *state, int timeout);
335
345 bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
346
356 bool (*destroy)(struct lxc_container *c);
357
366 bool (*save_config)(struct lxc_container *c, const char *alt_file);
367
385 bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
386 struct bdev_specs *specs, int flags, char *const argv[]);
387
408 bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
409 struct bdev_specs *specs, int flags, ...);
410
419 bool (*rename)(struct lxc_container *c, const char *newname);
420
428 bool (*reboot)(struct lxc_container *c);
429
440 bool (*shutdown)(struct lxc_container *c, int timeout);
441
447 void (*clear_config)(struct lxc_container *c);
448
459 bool (*clear_config_item)(struct lxc_container *c, const char *key);
460
479 int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
480
481
492 char* (*get_running_config_item)(struct lxc_container *c, const char *key);
493
515 int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
516
527 char** (*get_interfaces)(struct lxc_container *c);
528
543 char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
544
560 int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
561
571 bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
572
589 const char *(*get_config_path)(struct lxc_container *c);
590
600 bool (*set_config_path)(struct lxc_container *c, const char *path);
601
632 struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
633 const char *lxcpath, int flags, const char *bdevtype,
634 const char *bdevdata, uint64_t newsize, char **hookargs);
635
654 int (*console_getfd)(struct lxc_container *c, int *ttynum, int *ptmxfd);
655
673 int (*console)(struct lxc_container *c, int ttynum,
674 int stdinfd, int stdoutfd, int stderrfd, int escape);
675
689 int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
690 void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
691
703 int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
704
716 int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
717
734 int (*snapshot)(struct lxc_container *c, const char *commentfile);
735
748 int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
749
767 bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
768
777 bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
778
788 bool (*may_control)(struct lxc_container *c);
789
800 bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
801
812 bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
813
814 /* Post LXC-1.0 additions */
815
824 bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
825
834 bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
846 bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
847
858 bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
859
870
879
880 /* Post LXC-1.1 additions */
890 int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
891
900 int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
901
911 bool (*reboot2)(struct lxc_container *c, int timeout);
912
916 int (*mount)(struct lxc_container *c, const char *source,
917 const char *target, const char *filesystemtype,
918 unsigned long mountflags, const void *data,
919 struct lxc_mount *mnt);
920
924 int (*umount)(struct lxc_container *c, const char *target,
925 unsigned long mountflags, struct lxc_mount *mnt);
926
935
943 int (*init_pidfd)(struct lxc_container *c);
944
945#ifdef HAVE_ISULAD
954 bool (*set_container_info_file) (struct lxc_container *c, const char *info_file);
955
964 bool (*set_terminal_init_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
965
974 bool (*add_terminal_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
975
976 bool (*set_terminal_winch)(struct lxc_container *c, unsigned int height, unsigned int width);
977
978 bool (*set_exec_terminal_winch)(struct lxc_container *c, const char *suffix, unsigned int height, unsigned int width);
979
989 bool (*want_disable_pty)(struct lxc_container *c, bool state);
990
1000 bool (*want_open_stdin)(struct lxc_container *c, bool state);
1001
1010 bool (*clean_container_resource) (struct lxc_container *c, pid_t pid);
1011
1021 bool (*get_container_pids)(struct lxc_container *c,pid_t **pids,size_t *pids_len);
1022
1031 bool (*set_start_timeout)(struct lxc_container *c, unsigned int start_timeout);
1032
1041 bool (*set_oci_type)(struct lxc_container *c, bool image_type_oci);
1042
1051 bool (*get_container_metrics)(struct lxc_container *c, struct lxc_container_metrics *metrics);
1052#endif
1053};
1054
1059 char *name;
1062 char *lxcpath;
1068 void (*free)(struct lxc_snapshot *s);
1069};
1070
1071
1076 char *fstype;
1077 uint64_t fssize;
1078 struct {
1079 char *zfsroot;
1080 } zfs;
1081 struct {
1082 char *vg;
1083 char *lv;
1084 char *thinpool;
1085 } lvm;
1086 char *dir;
1087 struct {
1088 char *rbdname;
1089 char *rbdpool;
1090 } rbd;
1091};
1092
1096enum {
1097 MIGRATE_PRE_DUMP,
1098 MIGRATE_DUMP,
1099 MIGRATE_RESTORE,
1100 MIGRATE_FEATURE_CHECK,
1101};
1102
1106#define FEATURE_MEM_TRACK (1ULL << 0)
1107#define FEATURE_LAZY_PAGES (1ULL << 1)
1108
1113 /* new members should be added at the end */
1114 char *directory;
1115 bool verbose;
1116
1117 bool stop; /* stop the container after dump? */
1118 char *predump_dir; /* relative to directory above */
1119 char *pageserver_address; /* where should memory pages be send? */
1120 char *pageserver_port;
1121
1122 /* This flag indicates whether or not the container's rootfs will have
1123 * the same inodes on checkpoint and restore. In the case of e.g. zfs
1124 * send or btrfs send, or an LVM snapshot, this will be true, but it
1125 * won't if e.g. you rsync the filesystems between two machines.
1126 */
1127 bool preserves_inodes;
1128
1129 /* Path to an executable script that will be registered as a criu
1130 * "action script"
1131 */
1132 char *action_script;
1133
1134 /* If CRIU >= 2.4 is detected the option to skip in-flight connections
1135 * will be enabled by default. The flag 'disable_skip_in_flight' will
1136 * unconditionally disable this feature. In-flight connections are
1137 * not fully established TCP connections: SYN, SYN-ACK */
1138 bool disable_skip_in_flight;
1139
1140 /* This is the maximum file size for deleted files (which CRIU calls
1141 * "ghost" files) that will be handled. 0 indicates the CRIU default,
1142 * which at this time is 1MB.
1143 */
1144 uint64_t ghost_limit;
1145
1146 /* Some features cannot be checked by comparing the CRIU version.
1147 * Features like dirty page tracking or userfaultfd depend on
1148 * the architecture/kernel/criu combination. This is a bitmask
1149 * in which the desired feature checks can be encoded.
1150 */
1151 uint64_t features_to_check;
1152};
1153
1155 /* Clear the console log. */
1156 bool clear;
1157
1158 /* Retrieve the console log. */
1159 bool read;
1160
1161 /* This specifies the maximum size to read from the ringbuffer. Setting
1162 * it to 0 means that the a read can be as big as the whole ringbuffer.
1163 * On return callers can check how many bytes were actually read.
1164 * If "read" and "clear" are set to false and a non-zero value is
1165 * specified then up to "read_max" bytes of data will be discarded from
1166 * the ringbuffer.
1167 */
1168 uint64_t *read_max;
1169
1170 /* Data that was read from the ringbuffer. If "read_max" is 0 on return
1171 * "data" is invalid.
1172 */
1173 char *data;
1174};
1175
1184struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1185
1186#ifdef HAVE_ISULAD
1197struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath);
1198#endif
1199
1207int lxc_container_get(struct lxc_container *c);
1208
1219int lxc_container_put(struct lxc_container *c);
1220
1233int lxc_get_wait_states(const char **states);
1234
1242const char *lxc_get_global_config_item(const char *key);
1243
1250const char *lxc_get_version(void);
1251
1263int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1264
1279int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1280
1295int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1296
1297struct lxc_log {
1298 const char *name;
1299 const char *lxcpath;
1300 const char *file;
1301 const char *level;
1302 const char *prefix;
1303 bool quiet;
1304};
1305
1311int lxc_log_init(struct lxc_log *log);
1312
1316void lxc_log_close(void);
1317
1323bool lxc_config_item_is_supported(const char *key);
1324
1330bool lxc_has_api_extension(const char *extension);
1331
1332#ifdef __cplusplus
1333}
1334#endif
1335
1336#endif
Specifications for how to create a new backing store.
Definition: lxccontainer.h:1075
char * zfsroot
Definition: lxccontainer.h:1079
char * vg
Definition: lxccontainer.h:1082
char * fstype
Definition: lxccontainer.h:1076
char * dir
Definition: lxccontainer.h:1086
char * lv
Definition: lxccontainer.h:1083
char * rbdname
Definition: lxccontainer.h:1088
uint64_t fssize
Definition: lxccontainer.h:1077
char * rbdpool
Definition: lxccontainer.h:1089
char * thinpool
Definition: lxccontainer.h:1084
Definition: attach_options.h:61
Definition: lxccontainer.h:1154
Definition: lxccontainer.h:94
bool(* checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose)
Checkpoint a container.
Definition: lxccontainer.h:846
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:334
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:408
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:252
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container's snapshot.
Definition: lxccontainer.h:878
int(* console_log)(struct lxc_container *c, struct lxc_console_log *log)
Query the console log of a container.
Definition: lxccontainer.h:900
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:689
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:716
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:800
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:356
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:309
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:447
char * error_string
Definition: lxccontainer.h:143
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:824
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:345
bool(* rename)(struct lxc_container *c, const char *newname)
Rename a container.
Definition: lxccontainer.h:419
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:812
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:204
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:459
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:278
int(* umount)(struct lxc_container *c, const char *target, unsigned long mountflags, struct lxc_mount *mnt)
Unmount the container's path target.
Definition: lxccontainer.h:924
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:263
bool(* reboot2)(struct lxc_container *c, int timeout)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:911
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:673
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:560
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:287
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's path source onto the container's path target.
Definition: lxccontainer.h:916
char * config_path
Definition: lxccontainer.h:152
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:479
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:788
bool(* snapshot_destroy)(struct lxc_container *c, const char *snapname)
Destroy the specified snapshot.
Definition: lxccontainer.h:777
bool(* is_defined)(struct lxc_container *c)
Determine if /var/lib/lxc/$name/config exists.
Definition: lxccontainer.h:193
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:515
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:703
int(* init_pidfd)(struct lxc_container *c)
Retrieve a pidfd for the container's init process.
Definition: lxccontainer.h:943
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:241
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:298
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:440
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *ptmxfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:654
int(* seccomp_notify_fd)(struct lxc_container *c)
Retrieve a file descriptor for the container's seccomp filter.
Definition: lxccontainer.h:934
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:890
int error_num
Definition: lxccontainer.h:146
bool daemonize
Definition: lxccontainer.h:149
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:213
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:734
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:222
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:748
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:366
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:834
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:231
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:858
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:428
bool(* snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname)
Create a new container based on a snapshot.
Definition: lxccontainer.h:767
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:600
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:385
bool(* destroy_with_snapshots)(struct lxc_container *c)
Delete the container and all its snapshots.
Definition: lxccontainer.h:869
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:571
Definition: lxclock.h:33
Definition: lxccontainer.h:1297
Definition: lxccontainer.h:43
An LXC container snapshot.
Definition: lxccontainer.h:1058
void(* free)(struct lxc_snapshot *s)
De-allocate the snapshot.
Definition: lxccontainer.h:1068
char * name
Definition: lxccontainer.h:1059
char * lxcpath
Definition: lxccontainer.h:1062
char * timestamp
Definition: lxccontainer.h:1061
char * comment_pathname
Definition: lxccontainer.h:1060
Options for the migrate API call.
Definition: lxccontainer.h:1112