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 rss_bytes;
72 uint64_t page_faults;
73 uint64_t major_page_faults;
74 /* Kernel Memory usage */
75 uint64_t kmem_used;
76 uint64_t kmem_limit;
77 /* Cache usage */
78 uint64_t cache;
79 uint64_t cache_total;
80 /* total inactive file */
81 uint64_t inactive_file_total;
82};
83#endif
84
93 /* private fields */
98 char *name;
99
104 char *configfile;
105
110 char *pidfile;
111
116 struct lxc_lock *slock;
117
122 struct lxc_lock *privlock;
123
129 int numthreads;
130
137 struct lxc_conf *lxc_conf;
138
139 /* public fields */
142
145
148
151
152#ifdef HAVE_ISULAD
157 char *exit_fifo;
159 bool disable_pty;
160
162 bool open_stdin;
163
169 char *ocihookfile;
170
175 unsigned int start_timeout;
176
181 bool image_type_oci;
182#endif
183
191 bool (*is_defined)(struct lxc_container *c);
192
202 const char *(*state)(struct lxc_container *c);
203
211 bool (*is_running)(struct lxc_container *c);
212
220 bool (*freeze)(struct lxc_container *c);
221
229 bool (*unfreeze)(struct lxc_container *c);
230
239 pid_t (*init_pid)(struct lxc_container *c);
240
250 bool (*load_config)(struct lxc_container *c, const char *alt_file);
251
261 bool (*start)(struct lxc_container *c, int useinit, char * const argv[]);
262
276 bool (*startl)(struct lxc_container *c, int useinit, ...);
277
285 bool (*stop)(struct lxc_container *c);
286
296 bool (*want_daemonize)(struct lxc_container *c, bool state);
297
307 bool (*want_close_all_fds)(struct lxc_container *c, bool state);
308
318 char *(*config_file_name)(struct lxc_container *c);
319
332 bool (*wait)(struct lxc_container *c, const char *state, int timeout);
333
343 bool (*set_config_item)(struct lxc_container *c, const char *key, const char *value);
344
354 bool (*destroy)(struct lxc_container *c);
355
364 bool (*save_config)(struct lxc_container *c, const char *alt_file);
365
383 bool (*create)(struct lxc_container *c, const char *t, const char *bdevtype,
384 struct bdev_specs *specs, int flags, char *const argv[]);
385
406 bool (*createl)(struct lxc_container *c, const char *t, const char *bdevtype,
407 struct bdev_specs *specs, int flags, ...);
408
417 bool (*rename)(struct lxc_container *c, const char *newname);
418
426 bool (*reboot)(struct lxc_container *c);
427
438 bool (*shutdown)(struct lxc_container *c, int timeout);
439
445 void (*clear_config)(struct lxc_container *c);
446
457 bool (*clear_config_item)(struct lxc_container *c, const char *key);
458
477 int (*get_config_item)(struct lxc_container *c, const char *key, char *retv, int inlen);
478
479
490 char* (*get_running_config_item)(struct lxc_container *c, const char *key);
491
513 int (*get_keys)(struct lxc_container *c, const char *key, char *retv, int inlen);
514
525 char** (*get_interfaces)(struct lxc_container *c);
526
541 char** (*get_ips)(struct lxc_container *c, const char* interface, const char* family, int scope);
542
558 int (*get_cgroup_item)(struct lxc_container *c, const char *subsys, char *retv, int inlen);
559
569 bool (*set_cgroup_item)(struct lxc_container *c, const char *subsys, const char *value);
570
587 const char *(*get_config_path)(struct lxc_container *c);
588
598 bool (*set_config_path)(struct lxc_container *c, const char *path);
599
630 struct lxc_container *(*clone)(struct lxc_container *c, const char *newname,
631 const char *lxcpath, int flags, const char *bdevtype,
632 const char *bdevdata, uint64_t newsize, char **hookargs);
633
652 int (*console_getfd)(struct lxc_container *c, int *ttynum, int *ptmxfd);
653
671 int (*console)(struct lxc_container *c, int ttynum,
672 int stdinfd, int stdoutfd, int stderrfd, int escape);
673
687 int (*attach)(struct lxc_container *c, lxc_attach_exec_t exec_function,
688 void *exec_payload, lxc_attach_options_t *options, pid_t *attached_process);
689
701 int (*attach_run_wait)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char * const argv[]);
702
714 int (*attach_run_waitl)(struct lxc_container *c, lxc_attach_options_t *options, const char *program, const char *arg, ...);
715
732 int (*snapshot)(struct lxc_container *c, const char *commentfile);
733
746 int (*snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots);
747
765 bool (*snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname);
766
775 bool (*snapshot_destroy)(struct lxc_container *c, const char *snapname);
776
786 bool (*may_control)(struct lxc_container *c);
787
798 bool (*add_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
799
810 bool (*remove_device_node)(struct lxc_container *c, const char *src_path, const char *dest_path);
811
812 /* Post LXC-1.0 additions */
813
822 bool (*attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
823
832 bool (*detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev);
844 bool (*checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose);
845
856 bool (*restore)(struct lxc_container *c, char *directory, bool verbose);
857
868
877
878 /* Post LXC-1.1 additions */
888 int (*migrate)(struct lxc_container *c, unsigned int cmd, struct migrate_opts *opts, unsigned int size);
889
898 int (*console_log)(struct lxc_container *c, struct lxc_console_log *log);
899
909 bool (*reboot2)(struct lxc_container *c, int timeout);
910
914 int (*mount)(struct lxc_container *c, const char *source,
915 const char *target, const char *filesystemtype,
916 unsigned long mountflags, const void *data,
917 struct lxc_mount *mnt);
918
922 int (*umount)(struct lxc_container *c, const char *target,
923 unsigned long mountflags, struct lxc_mount *mnt);
924
933
941 int (*init_pidfd)(struct lxc_container *c);
942
943#ifdef HAVE_ISULAD
952 bool (*set_container_info_file) (struct lxc_container *c, const char *info_file);
953
962 bool (*set_terminal_init_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
963
972 bool (*add_terminal_fifos)(struct lxc_container *c, const char *in, const char *out, const char *err);
973
974 bool (*set_terminal_winch)(struct lxc_container *c, unsigned int height, unsigned int width);
975
976 bool (*set_exec_terminal_winch)(struct lxc_container *c, const char *suffix, unsigned int height, unsigned int width);
977
987 bool (*want_disable_pty)(struct lxc_container *c, bool state);
988
998 bool (*want_open_stdin)(struct lxc_container *c, bool state);
999
1008 bool (*clean_container_resource) (struct lxc_container *c, pid_t pid);
1009
1019 bool (*get_container_pids)(struct lxc_container *c,pid_t **pids,size_t *pids_len);
1020
1029 bool (*set_start_timeout)(struct lxc_container *c, unsigned int start_timeout);
1030
1039 bool (*set_oci_type)(struct lxc_container *c, bool image_type_oci);
1040
1049 bool (*get_container_metrics)(struct lxc_container *c, struct lxc_container_metrics *metrics);
1050#endif
1051};
1052
1057 char *name;
1060 char *lxcpath;
1066 void (*free)(struct lxc_snapshot *s);
1067};
1068
1069
1074 char *fstype;
1075 uint64_t fssize;
1076 struct {
1077 char *zfsroot;
1078 } zfs;
1079 struct {
1080 char *vg;
1081 char *lv;
1082 char *thinpool;
1083 } lvm;
1084 char *dir;
1085 struct {
1086 char *rbdname;
1087 char *rbdpool;
1088 } rbd;
1089};
1090
1094enum {
1095 MIGRATE_PRE_DUMP,
1096 MIGRATE_DUMP,
1097 MIGRATE_RESTORE,
1098 MIGRATE_FEATURE_CHECK,
1099};
1100
1104#define FEATURE_MEM_TRACK (1ULL << 0)
1105#define FEATURE_LAZY_PAGES (1ULL << 1)
1106
1111 /* new members should be added at the end */
1112 char *directory;
1113 bool verbose;
1114
1115 bool stop; /* stop the container after dump? */
1116 char *predump_dir; /* relative to directory above */
1117 char *pageserver_address; /* where should memory pages be send? */
1118 char *pageserver_port;
1119
1120 /* This flag indicates whether or not the container's rootfs will have
1121 * the same inodes on checkpoint and restore. In the case of e.g. zfs
1122 * send or btrfs send, or an LVM snapshot, this will be true, but it
1123 * won't if e.g. you rsync the filesystems between two machines.
1124 */
1125 bool preserves_inodes;
1126
1127 /* Path to an executable script that will be registered as a criu
1128 * "action script"
1129 */
1130 char *action_script;
1131
1132 /* If CRIU >= 2.4 is detected the option to skip in-flight connections
1133 * will be enabled by default. The flag 'disable_skip_in_flight' will
1134 * unconditionally disable this feature. In-flight connections are
1135 * not fully established TCP connections: SYN, SYN-ACK */
1136 bool disable_skip_in_flight;
1137
1138 /* This is the maximum file size for deleted files (which CRIU calls
1139 * "ghost" files) that will be handled. 0 indicates the CRIU default,
1140 * which at this time is 1MB.
1141 */
1142 uint64_t ghost_limit;
1143
1144 /* Some features cannot be checked by comparing the CRIU version.
1145 * Features like dirty page tracking or userfaultfd depend on
1146 * the architecture/kernel/criu combination. This is a bitmask
1147 * in which the desired feature checks can be encoded.
1148 */
1149 uint64_t features_to_check;
1150};
1151
1153 /* Clear the console log. */
1154 bool clear;
1155
1156 /* Retrieve the console log. */
1157 bool read;
1158
1159 /* This specifies the maximum size to read from the ringbuffer. Setting
1160 * it to 0 means that the a read can be as big as the whole ringbuffer.
1161 * On return callers can check how many bytes were actually read.
1162 * If "read" and "clear" are set to false and a non-zero value is
1163 * specified then up to "read_max" bytes of data will be discarded from
1164 * the ringbuffer.
1165 */
1166 uint64_t *read_max;
1167
1168 /* Data that was read from the ringbuffer. If "read_max" is 0 on return
1169 * "data" is invalid.
1170 */
1171 char *data;
1172};
1173
1182struct lxc_container *lxc_container_new(const char *name, const char *configpath);
1183
1184#ifdef HAVE_ISULAD
1195struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath);
1196#endif
1197
1205int lxc_container_get(struct lxc_container *c);
1206
1217int lxc_container_put(struct lxc_container *c);
1218
1231int lxc_get_wait_states(const char **states);
1232
1240const char *lxc_get_global_config_item(const char *key);
1241
1248const char *lxc_get_version(void);
1249
1261int list_defined_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1262
1277int list_active_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1278
1293int list_all_containers(const char *lxcpath, char ***names, struct lxc_container ***cret);
1294
1295struct lxc_log {
1296 const char *name;
1297 const char *lxcpath;
1298 const char *file;
1299 const char *level;
1300 const char *prefix;
1301 bool quiet;
1302};
1303
1309int lxc_log_init(struct lxc_log *log);
1310
1314void lxc_log_close(void);
1315
1321bool lxc_config_item_is_supported(const char *key);
1322
1328bool lxc_has_api_extension(const char *extension);
1329
1330#ifdef __cplusplus
1331}
1332#endif
1333
1334#endif
Specifications for how to create a new backing store.
Definition: lxccontainer.h:1073
char * zfsroot
Definition: lxccontainer.h:1077
char * vg
Definition: lxccontainer.h:1080
char * fstype
Definition: lxccontainer.h:1074
char * dir
Definition: lxccontainer.h:1084
char * lv
Definition: lxccontainer.h:1081
char * rbdname
Definition: lxccontainer.h:1086
uint64_t fssize
Definition: lxccontainer.h:1075
char * rbdpool
Definition: lxccontainer.h:1087
char * thinpool
Definition: lxccontainer.h:1082
Definition: attach_options.h:61
Definition: lxccontainer.h:1152
Definition: lxccontainer.h:92
bool(* checkpoint)(struct lxc_container *c, char *directory, bool stop, bool verbose)
Checkpoint a container.
Definition: lxccontainer.h:844
bool(* wait)(struct lxc_container *c, const char *state, int timeout)
Wait for container to reach a particular state.
Definition: lxccontainer.h:332
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:406
bool(* load_config)(struct lxc_container *c, const char *alt_file)
Load the specified configuration for the container.
Definition: lxccontainer.h:250
bool(* snapshot_destroy_all)(struct lxc_container *c)
Destroy all the container's snapshot.
Definition: lxccontainer.h:876
int(* console_log)(struct lxc_container *c, struct lxc_console_log *log)
Query the console log of a container.
Definition: lxccontainer.h:898
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:687
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:714
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:798
bool(* destroy)(struct lxc_container *c)
Delete the container.
Definition: lxccontainer.h:354
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:307
void(* clear_config)(struct lxc_container *c)
Completely clear the containers in-memory configuration.
Definition: lxccontainer.h:445
char * error_string
Definition: lxccontainer.h:141
bool(* attach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Add specified netdev to the container.
Definition: lxccontainer.h:822
bool(* set_config_item)(struct lxc_container *c, const char *key, const char *value)
Set a key/value configuration option.
Definition: lxccontainer.h:343
bool(* rename)(struct lxc_container *c, const char *newname)
Rename a container.
Definition: lxccontainer.h:417
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:810
const char *(* state)(struct lxc_container *c)
Determine state of container.
Definition: lxccontainer.h:202
bool(* clear_config_item)(struct lxc_container *c, const char *key)
Clear a configuration item.
Definition: lxccontainer.h:457
bool(* startl)(struct lxc_container *c, int useinit,...)
Start the container (list variant).
Definition: lxccontainer.h:276
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:922
bool(* start)(struct lxc_container *c, int useinit, char *const argv[])
Start the container.
Definition: lxccontainer.h:261
bool(* reboot2)(struct lxc_container *c, int timeout)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:909
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:671
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:558
bool(* stop)(struct lxc_container *c)
Stop the container.
Definition: lxccontainer.h:285
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:914
char * config_path
Definition: lxccontainer.h:150
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:477
bool(* may_control)(struct lxc_container *c)
Determine if the caller may control the container.
Definition: lxccontainer.h:786
bool(* snapshot_destroy)(struct lxc_container *c, const char *snapname)
Destroy the specified snapshot.
Definition: lxccontainer.h:775
bool(* is_defined)(struct lxc_container *c)
Determine if /var/lib/lxc/$name/config exists.
Definition: lxccontainer.h:191
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:513
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:701
int(* init_pidfd)(struct lxc_container *c)
Retrieve a pidfd for the container's init process.
Definition: lxccontainer.h:941
pid_t(* init_pid)(struct lxc_container *c)
Determine process ID of the containers init process.
Definition: lxccontainer.h:239
bool(* want_daemonize)(struct lxc_container *c, bool state)
Change whether the container wants to run disconnected from the terminal.
Definition: lxccontainer.h:296
bool(* shutdown)(struct lxc_container *c, int timeout)
Request the container shutdown by sending it SIGPWR.
Definition: lxccontainer.h:438
int(* console_getfd)(struct lxc_container *c, int *ttynum, int *ptmxfd)
Allocate a console tty for the container.
Definition: lxccontainer.h:652
int(* seccomp_notify_fd)(struct lxc_container *c)
Retrieve a file descriptor for the container's seccomp filter.
Definition: lxccontainer.h:932
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:888
int error_num
Definition: lxccontainer.h:144
bool daemonize
Definition: lxccontainer.h:147
bool(* is_running)(struct lxc_container *c)
Determine if container is running.
Definition: lxccontainer.h:211
int(* snapshot)(struct lxc_container *c, const char *commentfile)
Create a container snapshot.
Definition: lxccontainer.h:732
bool(* freeze)(struct lxc_container *c)
Freeze running container.
Definition: lxccontainer.h:220
int(* snapshot_list)(struct lxc_container *c, struct lxc_snapshot **snapshots)
Obtain a list of container snapshots.
Definition: lxccontainer.h:746
bool(* save_config)(struct lxc_container *c, const char *alt_file)
Save configuration to a file.
Definition: lxccontainer.h:364
bool(* detach_interface)(struct lxc_container *c, const char *dev, const char *dst_dev)
Remove specified netdev from the container.
Definition: lxccontainer.h:832
bool(* unfreeze)(struct lxc_container *c)
Thaw a frozen container.
Definition: lxccontainer.h:229
bool(* restore)(struct lxc_container *c, char *directory, bool verbose)
Restore a container from a checkpoint.
Definition: lxccontainer.h:856
bool(* reboot)(struct lxc_container *c)
Request the container reboot by sending it SIGINT.
Definition: lxccontainer.h:426
bool(* snapshot_restore)(struct lxc_container *c, const char *snapname, const char *newname)
Create a new container based on a snapshot.
Definition: lxccontainer.h:765
bool(* set_config_path)(struct lxc_container *c, const char *path)
Set the full path to the containers configuration file.
Definition: lxccontainer.h:598
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:383
bool(* destroy_with_snapshots)(struct lxc_container *c)
Delete the container and all its snapshots.
Definition: lxccontainer.h:867
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:569
Definition: lxclock.h:33
Definition: lxccontainer.h:1295
Definition: lxccontainer.h:43
An LXC container snapshot.
Definition: lxccontainer.h:1056
void(* free)(struct lxc_snapshot *s)
De-allocate the snapshot.
Definition: lxccontainer.h:1066
char * name
Definition: lxccontainer.h:1057
char * lxcpath
Definition: lxccontainer.h:1060
char * timestamp
Definition: lxccontainer.h:1059
char * comment_pathname
Definition: lxccontainer.h:1058
Options for the migrate API call.
Definition: lxccontainer.h:1110