]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/conf.h
config : add lxc.hook.destroy option
[mirror_lxc.git] / src / lxc / conf.h
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #ifndef __LXC_CONF_H
24 #define __LXC_CONF_H
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <netinet/in.h>
30 #include <net/if.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <stdbool.h>
34
35 #include "list.h"
36 #include "start.h" /* for lxc_handler */
37
38 #if HAVE_SCMP_FILTER_CTX
39 typedef void * scmp_filter_ctx;
40 #endif
41
42 /* worth moving to configure.ac? */
43 #define subuidfile "/etc/subuid"
44 #define subgidfile "/etc/subgid"
45
46 enum {
47 LXC_NET_EMPTY,
48 LXC_NET_VETH,
49 LXC_NET_MACVLAN,
50 LXC_NET_PHYS,
51 LXC_NET_VLAN,
52 LXC_NET_NONE,
53 LXC_NET_MAXCONFTYPE,
54 };
55
56 /*
57 * Defines the structure to configure an ipv4 address
58 * @address : ipv4 address
59 * @broadcast : ipv4 broadcast address
60 * @mask : network mask
61 */
62 struct lxc_inetdev {
63 struct in_addr addr;
64 struct in_addr bcast;
65 int prefix;
66 };
67
68 struct lxc_route {
69 struct in_addr addr;
70 };
71
72 /*
73 * Defines the structure to configure an ipv6 address
74 * @flags : set the address up
75 * @address : ipv6 address
76 * @broadcast : ipv6 broadcast address
77 * @mask : network mask
78 */
79 struct lxc_inet6dev {
80 struct in6_addr addr;
81 struct in6_addr mcast;
82 struct in6_addr acast;
83 int prefix;
84 };
85
86 struct lxc_route6 {
87 struct in6_addr addr;
88 };
89
90 struct ifla_veth {
91 char *pair; /* pair name */
92 char veth1[IFNAMSIZ]; /* needed for deconf */
93 };
94
95 struct ifla_vlan {
96 unsigned int flags;
97 unsigned int fmask;
98 unsigned short vid;
99 unsigned short pad;
100 };
101
102 struct ifla_macvlan {
103 int mode; /* private, vepa, bridge */
104 };
105
106 union netdev_p {
107 struct ifla_veth veth_attr;
108 struct ifla_vlan vlan_attr;
109 struct ifla_macvlan macvlan_attr;
110 };
111
112 /*
113 * Defines a structure to configure a network device
114 * @link : lxc.network.link, name of bridge or host iface to attach if any
115 * @name : lxc.network.name, name of iface on the container side
116 * @flags : flag of the network device (IFF_UP, ... )
117 * @ipv4 : a list of ipv4 addresses to be set on the network device
118 * @ipv6 : a list of ipv6 addresses to be set on the network device
119 * @upscript : a script filename to be executed during interface configuration
120 * @downscript : a script filename to be executed during interface destruction
121 */
122 struct lxc_netdev {
123 int type;
124 int flags;
125 int ifindex;
126 char *link;
127 char *name;
128 char *hwaddr;
129 char *mtu;
130 union netdev_p priv;
131 struct lxc_list ipv4;
132 struct lxc_list ipv6;
133 struct in_addr *ipv4_gateway;
134 bool ipv4_gateway_auto;
135 struct in6_addr *ipv6_gateway;
136 bool ipv6_gateway_auto;
137 char *upscript;
138 char *downscript;
139 };
140
141 /*
142 * Defines a generic struct to configure the control group.
143 * It is up to the programmer to specify the right subsystem.
144 * @subsystem : the targeted subsystem
145 * @value : the value to set
146 */
147 struct lxc_cgroup {
148 char *subsystem;
149 char *value;
150 };
151
152 enum idtype {
153 ID_TYPE_UID,
154 ID_TYPE_GID
155 };
156
157 /*
158 * id_map is an id map entry. Form in confile is:
159 * lxc.id_map = u 0 9800 100
160 * lxc.id_map = u 1000 9900 100
161 * lxc.id_map = g 0 9800 100
162 * lxc.id_map = g 1000 9900 100
163 * meaning the container can use uids and gids 0-99 and 1000-1099,
164 * with [ug]id 0 mapping to [ug]id 9800 on the host, and [ug]id 1000 to
165 * [ug]id 9900 on the host.
166 */
167 struct id_map {
168 enum idtype idtype;
169 unsigned long hostid, nsid, range;
170 };
171
172 /*
173 * Defines a structure containing a pty information for
174 * virtualizing a tty
175 * @name : the path name of the slave pty side
176 * @master : the file descriptor of the master
177 * @slave : the file descriptor of the slave
178 */
179 struct lxc_pty_info {
180 char name[MAXPATHLEN];
181 int master;
182 int slave;
183 int busy;
184 };
185
186 /*
187 * Defines the number of tty configured and contains the
188 * instantiated ptys
189 * @nbtty = number of configured ttys
190 */
191 struct lxc_tty_info {
192 int nbtty;
193 struct lxc_pty_info *pty_info;
194 };
195
196 struct lxc_tty_state;
197
198 /*
199 * Defines the structure to store the console information
200 * @peer : the file descriptor put/get console traffic
201 * @name : the file name of the slave pty
202 */
203 struct lxc_console {
204 int slave;
205 int master;
206 int peer;
207 struct lxc_pty_info peerpty;
208 struct lxc_epoll_descr *descr;
209 char *path;
210 char *log_path;
211 int log_fd;
212 char name[MAXPATHLEN];
213 struct termios *tios;
214 struct lxc_tty_state *tty_state;
215 };
216
217 /*
218 * Defines a structure to store the rootfs location, the
219 * optionals pivot_root, rootfs mount paths
220 * @rootfs : a path to the rootfs
221 * @pivot_root : a path to a pivot_root location to be used
222 */
223 struct lxc_rootfs {
224 char *path;
225 char *mount;
226 char *pivot;
227 char *options;
228 };
229
230 /*
231 * Automatic mounts for LXC to perform inside the container
232 */
233 enum {
234 LXC_AUTO_PROC_RW = 0x001, /* /proc read-write */
235 LXC_AUTO_PROC_MIXED = 0x002, /* /proc/sys and /proc/sysrq-trigger read-only */
236 LXC_AUTO_PROC_MASK = 0x003,
237
238 LXC_AUTO_SYS_RW = 0x004, /* /sys */
239 LXC_AUTO_SYS_RO = 0x008, /* /sys read-only */
240 LXC_AUTO_SYS_MIXED = 0x00C, /* /sys read-only and /sys/class/net read-write */
241 LXC_AUTO_SYS_MASK = 0x00C,
242
243 LXC_AUTO_CGROUP_RO = 0x010, /* /sys/fs/cgroup (partial mount, read-only) */
244 LXC_AUTO_CGROUP_RW = 0x020, /* /sys/fs/cgroup (partial mount, read-write) */
245 LXC_AUTO_CGROUP_MIXED = 0x030, /* /sys/fs/cgroup (partial mount, paths r/o, cgroup r/w) */
246 LXC_AUTO_CGROUP_FULL_RO = 0x040, /* /sys/fs/cgroup (full mount, read-only) */
247 LXC_AUTO_CGROUP_FULL_RW = 0x050, /* /sys/fs/cgroup (full mount, read-write) */
248 LXC_AUTO_CGROUP_FULL_MIXED = 0x060, /* /sys/fs/cgroup (full mount, parent r/o, own r/w) */
249 /* These are defined in such a way as to retain
250 * binary compatibility with earlier versions of
251 * this code. If the previous mask is applied,
252 * both of these will default back to the _MIXED
253 * variants, which is safe. */
254 LXC_AUTO_CGROUP_NOSPEC = 0x0B0, /* /sys/fs/cgroup (partial mount, r/w or mixed, depending on caps) */
255 LXC_AUTO_CGROUP_FULL_NOSPEC = 0x0E0, /* /sys/fs/cgroup (full mount, r/w or mixed, depending on caps) */
256 LXC_AUTO_CGROUP_MASK = 0x0F0,
257
258 LXC_AUTO_ALL_MASK = 0x0FF, /* all known settings */
259 };
260
261 /*
262 * Defines the global container configuration
263 * @rootfs : root directory to run the container
264 * @pivotdir : pivotdir path, if not set default will be used
265 * @mount : list of mount points
266 * @tty : numbers of tty
267 * @pts : new pts instance
268 * @mount_list : list of mount point (alternative to fstab file)
269 * @network : network configuration
270 * @utsname : container utsname
271 * @fstab : path to a fstab file format
272 * @caps : list of the capabilities to drop
273 * @keepcaps : list of the capabilities to keep
274 * @tty_info : tty data
275 * @console : console data
276 * @ttydir : directory (under /dev) in which to create console and ttys
277 * @lsm_aa_profile : apparmor profile to switch to or NULL
278 * @lsm_se_context : selinux type to switch to or NULL
279 */
280 enum lxchooks {
281 LXCHOOK_PRESTART, LXCHOOK_PREMOUNT, LXCHOOK_MOUNT, LXCHOOK_AUTODEV,
282 LXCHOOK_START, LXCHOOK_POSTSTOP, LXCHOOK_CLONE, LXCHOOK_DESTROY,
283 NUM_LXC_HOOKS};
284 extern char *lxchook_names[NUM_LXC_HOOKS];
285
286 struct saved_nic {
287 int ifindex;
288 char *orig_name;
289 };
290
291 struct lxc_conf {
292 int is_execute;
293 char *fstab;
294 int tty;
295 int pts;
296 int reboot;
297 int need_utmp_watch;
298 signed long personality;
299 struct utsname *utsname;
300 struct lxc_list cgroup;
301 struct lxc_list id_map;
302 struct lxc_list network;
303 struct saved_nic *saved_nics;
304 int num_savednics;
305 int auto_mounts;
306 struct lxc_list mount_list;
307 struct lxc_list caps;
308 struct lxc_list keepcaps;
309 struct lxc_tty_info tty_info;
310 char *pty_names; // comma-separated list of lxc.tty pty names
311 struct lxc_console console;
312 struct lxc_rootfs rootfs;
313 char *ttydir;
314 int close_all_fds;
315 struct lxc_list hooks[NUM_LXC_HOOKS];
316
317 char *lsm_aa_profile;
318 int lsm_aa_allow_incomplete;
319 char *lsm_se_context;
320 int tmp_umount_proc;
321 char *seccomp; // filename with the seccomp rules
322 #if HAVE_SCMP_FILTER_CTX
323 scmp_filter_ctx seccomp_ctx;
324 #endif
325 int maincmd_fd;
326 int autodev; // if 1, mount and fill a /dev at start
327 int haltsignal; // signal used to halt container
328 int rebootsignal; // signal used to reboot container
329 int stopsignal; // signal used to hard stop container
330 int kmsg; // if 1, create /dev/kmsg symlink
331 char *rcfile; // Copy of the top level rcfile we read
332
333 // Logfile and logleve can be set in a container config file.
334 // Those function as defaults. The defaults can be overriden
335 // by command line. However we don't want the command line
336 // specified values to be saved on c->save_config(). So we
337 // store the config file specified values here.
338 char *logfile; // the logfile as specifed in config
339 int loglevel; // loglevel as specifed in config (if any)
340 int logfd;
341
342 int inherit_ns_fd[LXC_NS_MAX];
343
344 int start_auto;
345 int start_delay;
346 int start_order;
347 struct lxc_list groups;
348 int nbd_idx;
349
350 /* set to true when rootfs has been setup */
351 bool rootfs_setup;
352
353 /* list of included files */
354 struct lxc_list includes;
355 /* config entries which are not "lxc.*" are aliens */
356 struct lxc_list aliens;
357
358 /* list of environment variables we'll add to the container when
359 * started */
360 struct lxc_list environment;
361
362 /* text representation of the config file */
363 char *unexpanded_config;
364 size_t unexpanded_len, unexpanded_alloced;
365
366 /* init command */
367 char *init_cmd;
368 };
369
370 #ifdef HAVE_TLS
371 extern __thread struct lxc_conf *current_config;
372 #else
373 extern struct lxc_conf *current_config;
374 #endif
375
376 int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf,
377 const char *lxcpath, char *argv[]);
378
379 extern int detect_shared_rootfs(void);
380
381 /*
382 * Initialize the lxc configuration structure
383 */
384 extern struct lxc_conf *lxc_conf_init(void);
385 extern void lxc_conf_free(struct lxc_conf *conf);
386
387 extern int pin_rootfs(const char *rootfs);
388
389 extern int lxc_requests_empty_network(struct lxc_handler *handler);
390 extern int lxc_create_network(struct lxc_handler *handler);
391 extern void lxc_delete_network(struct lxc_handler *handler);
392 extern int lxc_assign_network(struct lxc_list *networks, pid_t pid);
393 extern int lxc_map_ids(struct lxc_list *idmap, pid_t pid);
394 extern int lxc_find_gateway_addresses(struct lxc_handler *handler);
395
396 extern int lxc_create_tty(const char *name, struct lxc_conf *conf);
397 extern void lxc_delete_tty(struct lxc_tty_info *tty_info);
398
399 extern int lxc_clear_config_network(struct lxc_conf *c);
400 extern int lxc_clear_nic(struct lxc_conf *c, const char *key);
401 extern int lxc_clear_config_caps(struct lxc_conf *c);
402 extern int lxc_clear_config_keepcaps(struct lxc_conf *c);
403 extern int lxc_clear_cgroups(struct lxc_conf *c, const char *key);
404 extern int lxc_clear_mount_entries(struct lxc_conf *c);
405 extern int lxc_clear_automounts(struct lxc_conf *c);
406 extern int lxc_clear_hooks(struct lxc_conf *c, const char *key);
407 extern int lxc_clear_idmaps(struct lxc_conf *c);
408 extern int lxc_clear_groups(struct lxc_conf *c);
409 extern int lxc_clear_environment(struct lxc_conf *c);
410 extern int lxc_delete_autodev(struct lxc_handler *handler);
411
412 extern int do_rootfs_setup(struct lxc_conf *conf, const char *name,
413 const char *lxcpath);
414
415 /*
416 * Configure the container from inside
417 */
418
419 struct cgroup_process_info;
420 extern int lxc_setup(struct lxc_handler *handler);
421
422 extern void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf);
423
424 extern int find_unmapped_nsuid(struct lxc_conf *conf, enum idtype idtype);
425 extern int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype);
426 extern int chown_mapped_root(char *path, struct lxc_conf *conf);
427 extern int ttys_shift_ids(struct lxc_conf *c);
428 extern int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data);
429 extern int parse_mntopts(const char *mntopts, unsigned long *mntflags,
430 char **mntdata);
431 extern void tmp_proc_unmount(struct lxc_conf *lxc_conf);
432 void remount_all_slave(void);
433 extern void suggest_default_idmap(void);
434 FILE *write_mount_file(struct lxc_list *mount);
435 struct lxc_list *sort_cgroup_settings(struct lxc_list* cgroup_settings);
436 #endif