2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <../include/openpty.h>
41 #include <linux/loop.h>
43 #include <sys/types.h>
44 #include <sys/utsname.h>
45 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/mount.h>
50 #include <sys/prctl.h>
52 #include <arpa/inet.h>
54 #include <netinet/in.h>
65 #include "lxc.h" /* for lxc_cgroup_set() */
66 #include "caps.h" /* for lxc_caps_last_cap() */
72 #if HAVE_SYS_CAPABILITY_H
73 #include <sys/capability.h>
76 #if HAVE_SYS_PERSONALITY_H
77 #include <sys/personality.h>
80 #include "lxcseccomp.h"
82 lxc_log_define(lxc_conf
, lxc
);
85 #define MAXINDEXLEN 20
87 #define MAXLINELEN 128
90 #define MS_DIRSYNC 128
102 #define MS_SLAVE (1<<19)
106 #define MS_RELATIME (1 << 21)
109 #ifndef MS_STRICTATIME
110 #define MS_STRICTATIME (1 << 24)
113 #if HAVE_SYS_CAPABILITY_H
115 #define CAP_SETFCAP 31
118 #ifndef CAP_MAC_OVERRIDE
119 #define CAP_MAC_OVERRIDE 32
122 #ifndef CAP_MAC_ADMIN
123 #define CAP_MAC_ADMIN 33
127 #ifndef PR_CAPBSET_DROP
128 #define PR_CAPBSET_DROP 24
131 #ifndef LO_FLAGS_AUTOCLEAR
132 #define LO_FLAGS_AUTOCLEAR 4
135 char *lxchook_names
[NUM_LXC_HOOKS
] = {
136 "pre-start", "pre-mount", "mount", "start", "post-stop" };
138 extern int pivot_root(const char * new_root
, const char * put_old
);
140 typedef int (*instanciate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
153 static int instanciate_veth(struct lxc_handler
*, struct lxc_netdev
*);
154 static int instanciate_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
155 static int instanciate_vlan(struct lxc_handler
*, struct lxc_netdev
*);
156 static int instanciate_phys(struct lxc_handler
*, struct lxc_netdev
*);
157 static int instanciate_empty(struct lxc_handler
*, struct lxc_netdev
*);
159 static instanciate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
160 [LXC_NET_VETH
] = instanciate_veth
,
161 [LXC_NET_MACVLAN
] = instanciate_macvlan
,
162 [LXC_NET_VLAN
] = instanciate_vlan
,
163 [LXC_NET_PHYS
] = instanciate_phys
,
164 [LXC_NET_EMPTY
] = instanciate_empty
,
167 static int shutdown_veth(struct lxc_handler
*, struct lxc_netdev
*);
168 static int shutdown_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
169 static int shutdown_vlan(struct lxc_handler
*, struct lxc_netdev
*);
170 static int shutdown_phys(struct lxc_handler
*, struct lxc_netdev
*);
171 static int shutdown_empty(struct lxc_handler
*, struct lxc_netdev
*);
173 static instanciate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
174 [LXC_NET_VETH
] = shutdown_veth
,
175 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
176 [LXC_NET_VLAN
] = shutdown_vlan
,
177 [LXC_NET_PHYS
] = shutdown_phys
,
178 [LXC_NET_EMPTY
] = shutdown_empty
,
181 static struct mount_opt mount_opt
[] = {
182 { "defaults", 0, 0 },
183 { "ro", 0, MS_RDONLY
},
184 { "rw", 1, MS_RDONLY
},
185 { "suid", 1, MS_NOSUID
},
186 { "nosuid", 0, MS_NOSUID
},
187 { "dev", 1, MS_NODEV
},
188 { "nodev", 0, MS_NODEV
},
189 { "exec", 1, MS_NOEXEC
},
190 { "noexec", 0, MS_NOEXEC
},
191 { "sync", 0, MS_SYNCHRONOUS
},
192 { "async", 1, MS_SYNCHRONOUS
},
193 { "dirsync", 0, MS_DIRSYNC
},
194 { "remount", 0, MS_REMOUNT
},
195 { "mand", 0, MS_MANDLOCK
},
196 { "nomand", 1, MS_MANDLOCK
},
197 { "atime", 1, MS_NOATIME
},
198 { "noatime", 0, MS_NOATIME
},
199 { "diratime", 1, MS_NODIRATIME
},
200 { "nodiratime", 0, MS_NODIRATIME
},
201 { "bind", 0, MS_BIND
},
202 { "rbind", 0, MS_BIND
|MS_REC
},
203 { "relatime", 0, MS_RELATIME
},
204 { "norelatime", 1, MS_RELATIME
},
205 { "strictatime", 0, MS_STRICTATIME
},
206 { "nostrictatime", 1, MS_STRICTATIME
},
210 #if HAVE_SYS_CAPABILITY_H
211 static struct caps_opt caps_opt
[] = {
212 { "chown", CAP_CHOWN
},
213 { "dac_override", CAP_DAC_OVERRIDE
},
214 { "dac_read_search", CAP_DAC_READ_SEARCH
},
215 { "fowner", CAP_FOWNER
},
216 { "fsetid", CAP_FSETID
},
217 { "kill", CAP_KILL
},
218 { "setgid", CAP_SETGID
},
219 { "setuid", CAP_SETUID
},
220 { "setpcap", CAP_SETPCAP
},
221 { "linux_immutable", CAP_LINUX_IMMUTABLE
},
222 { "net_bind_service", CAP_NET_BIND_SERVICE
},
223 { "net_broadcast", CAP_NET_BROADCAST
},
224 { "net_admin", CAP_NET_ADMIN
},
225 { "net_raw", CAP_NET_RAW
},
226 { "ipc_lock", CAP_IPC_LOCK
},
227 { "ipc_owner", CAP_IPC_OWNER
},
228 { "sys_module", CAP_SYS_MODULE
},
229 { "sys_rawio", CAP_SYS_RAWIO
},
230 { "sys_chroot", CAP_SYS_CHROOT
},
231 { "sys_ptrace", CAP_SYS_PTRACE
},
232 { "sys_pacct", CAP_SYS_PACCT
},
233 { "sys_admin", CAP_SYS_ADMIN
},
234 { "sys_boot", CAP_SYS_BOOT
},
235 { "sys_nice", CAP_SYS_NICE
},
236 { "sys_resource", CAP_SYS_RESOURCE
},
237 { "sys_time", CAP_SYS_TIME
},
238 { "sys_tty_config", CAP_SYS_TTY_CONFIG
},
239 { "mknod", CAP_MKNOD
},
240 { "lease", CAP_LEASE
},
241 #ifdef CAP_AUDIT_WRITE
242 { "audit_write", CAP_AUDIT_WRITE
},
244 #ifdef CAP_AUDIT_CONTROL
245 { "audit_control", CAP_AUDIT_CONTROL
},
247 { "setfcap", CAP_SETFCAP
},
248 { "mac_override", CAP_MAC_OVERRIDE
},
249 { "mac_admin", CAP_MAC_ADMIN
},
251 { "syslog", CAP_SYSLOG
},
253 #ifdef CAP_WAKE_ALARM
254 { "wake_alarm", CAP_WAKE_ALARM
},
258 static struct caps_opt caps_opt
[] = {};
261 static int run_buffer(char *buffer
)
266 f
= popen(buffer
, "r");
268 SYSERROR("popen failed");
272 output
= malloc(LXC_LOG_BUFFER_SIZE
);
274 ERROR("failed to allocate memory for script output");
278 while(fgets(output
, LXC_LOG_BUFFER_SIZE
, f
))
279 DEBUG("script output: %s", output
);
283 if (pclose(f
) == -1) {
284 SYSERROR("Script exited on error");
291 static int run_script(const char *name
, const char *section
,
292 const char *script
, ...)
299 INFO("Executing script '%s' for container '%s', config section '%s'",
300 script
, name
, section
);
302 va_start(ap
, script
);
303 while ((p
= va_arg(ap
, char *)))
304 size
+= strlen(p
) + 1;
307 size
+= strlen(script
);
308 size
+= strlen(name
);
309 size
+= strlen(section
);
315 buffer
= alloca(size
);
317 ERROR("failed to allocate memory");
321 ret
= snprintf(buffer
, size
, "%s %s %s", script
, name
, section
);
322 if (ret
< 0 || ret
>= size
) {
323 ERROR("Script name too long");
328 va_start(ap
, script
);
329 while ((p
= va_arg(ap
, char *))) {
332 rc
= snprintf(buffer
+ ret
, len
, " %s", p
);
333 if (rc
< 0 || rc
>= len
) {
335 ERROR("Script args too long");
342 return run_buffer(buffer
);
345 static int find_fstype_cb(char* buffer
, void *data
)
355 /* we don't try 'nodev' entries */
356 if (strstr(buffer
, "nodev"))
360 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
361 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
363 DEBUG("trying to mount '%s'->'%s' with fstype '%s'",
364 cbarg
->rootfs
, cbarg
->target
, fstype
);
366 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, cbarg
->mntopt
, NULL
)) {
367 DEBUG("mount failed with error: %s", strerror(errno
));
371 INFO("mounted '%s' on '%s', with fstype '%s'",
372 cbarg
->rootfs
, cbarg
->target
, fstype
);
377 static int mount_unknow_fs(const char *rootfs
, const char *target
, int mntopt
)
392 * find the filesystem type with brute force:
393 * first we check with /etc/filesystems, in case the modules
394 * are auto-loaded and fall back to the supported kernel fs
401 for (i
= 0; i
< sizeof(fsfile
)/sizeof(fsfile
[0]); i
++) {
405 if (access(fsfile
[i
], F_OK
))
408 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
410 ERROR("failed to parse '%s'", fsfile
[i
]);
418 ERROR("failed to determine fs type for '%s'", rootfs
);
422 static int mount_rootfs_dir(const char *rootfs
, const char *target
)
424 return mount(rootfs
, target
, "none", MS_BIND
| MS_REC
, NULL
);
427 static int setup_lodev(const char *rootfs
, int fd
, struct loop_info64
*loinfo
)
432 rfd
= open(rootfs
, O_RDWR
);
434 SYSERROR("failed to open '%s'", rootfs
);
438 memset(loinfo
, 0, sizeof(*loinfo
));
440 loinfo
->lo_flags
= LO_FLAGS_AUTOCLEAR
;
442 if (ioctl(fd
, LOOP_SET_FD
, rfd
)) {
443 SYSERROR("failed to LOOP_SET_FD");
447 if (ioctl(fd
, LOOP_SET_STATUS64
, loinfo
)) {
448 SYSERROR("failed to LOOP_SET_STATUS64");
459 static int mount_rootfs_file(const char *rootfs
, const char *target
)
461 struct dirent dirent
, *direntp
;
462 struct loop_info64 loinfo
;
463 int ret
= -1, fd
= -1, rc
;
465 char path
[MAXPATHLEN
];
467 dir
= opendir("/dev");
469 SYSERROR("failed to open '/dev'");
473 while (!readdir_r(dir
, &dirent
, &direntp
)) {
478 if (!strcmp(direntp
->d_name
, "."))
481 if (!strcmp(direntp
->d_name
, ".."))
484 if (strncmp(direntp
->d_name
, "loop", 4))
487 rc
= snprintf(path
, MAXPATHLEN
, "/dev/%s", direntp
->d_name
);
488 if (rc
< 0 || rc
>= MAXPATHLEN
)
491 fd
= open(path
, O_RDWR
);
495 if (ioctl(fd
, LOOP_GET_STATUS64
, &loinfo
) == 0) {
500 if (errno
!= ENXIO
) {
501 WARN("unexpected error for ioctl on '%s': %m",
506 DEBUG("found '%s' free lodev", path
);
508 ret
= setup_lodev(rootfs
, fd
, &loinfo
);
510 ret
= mount_unknow_fs(path
, target
, 0);
517 WARN("failed to close directory");
522 static int mount_rootfs_block(const char *rootfs
, const char *target
)
524 return mount_unknow_fs(rootfs
, target
, 0);
529 * if rootfs is a directory, then open ${rootfs}.hold for writing for the
530 * duration of the container run, to prevent the container from marking the
531 * underlying fs readonly on shutdown.
532 * return -1 on error.
533 * return -2 if nothing needed to be pinned.
534 * return an open fd (>=0) if we pinned it.
536 int pin_rootfs(const char *rootfs
)
538 char absrootfs
[MAXPATHLEN
];
539 char absrootfspin
[MAXPATHLEN
];
543 if (rootfs
== NULL
|| strlen(rootfs
) == 0)
546 if (!realpath(rootfs
, absrootfs
)) {
547 SYSERROR("failed to get real path for '%s'", rootfs
);
551 if (access(absrootfs
, F_OK
)) {
552 SYSERROR("'%s' is not accessible", absrootfs
);
556 if (stat(absrootfs
, &s
)) {
557 SYSERROR("failed to stat '%s'", absrootfs
);
561 if (!__S_ISTYPE(s
.st_mode
, S_IFDIR
))
564 ret
= snprintf(absrootfspin
, MAXPATHLEN
, "%s%s", absrootfs
, ".hold");
565 if (ret
>= MAXPATHLEN
) {
566 SYSERROR("pathname too long for rootfs hold file");
570 fd
= open(absrootfspin
, O_CREAT
| O_RDWR
, S_IWUSR
|S_IRUSR
);
571 INFO("opened %s as fd %d\n", absrootfspin
, fd
);
575 static int mount_rootfs(const char *rootfs
, const char *target
)
577 char absrootfs
[MAXPATHLEN
];
581 typedef int (*rootfs_cb
)(const char *, const char *);
587 { S_IFDIR
, mount_rootfs_dir
},
588 { S_IFBLK
, mount_rootfs_block
},
589 { S_IFREG
, mount_rootfs_file
},
592 if (!realpath(rootfs
, absrootfs
)) {
593 SYSERROR("failed to get real path for '%s'", rootfs
);
597 if (access(absrootfs
, F_OK
)) {
598 SYSERROR("'%s' is not accessible", absrootfs
);
602 if (stat(absrootfs
, &s
)) {
603 SYSERROR("failed to stat '%s'", absrootfs
);
607 for (i
= 0; i
< sizeof(rtfs_type
)/sizeof(rtfs_type
[0]); i
++) {
609 if (!__S_ISTYPE(s
.st_mode
, rtfs_type
[i
].type
))
612 return rtfs_type
[i
].cb(absrootfs
, target
);
615 ERROR("unsupported rootfs type for '%s'", absrootfs
);
619 static int setup_utsname(struct utsname
*utsname
)
624 if (sethostname(utsname
->nodename
, strlen(utsname
->nodename
))) {
625 SYSERROR("failed to set the hostname to '%s'", utsname
->nodename
);
629 INFO("'%s' hostname has been setup", utsname
->nodename
);
634 static int setup_tty(const struct lxc_rootfs
*rootfs
,
635 const struct lxc_tty_info
*tty_info
, char *ttydir
)
637 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
643 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
645 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
647 ret
= snprintf(path
, sizeof(path
), "%s/dev/tty%d",
648 rootfs
->mount
, i
+ 1);
649 if (ret
>= sizeof(path
)) {
650 ERROR("pathname too long for ttys");
654 /* create dev/lxc/tty%d" */
655 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/tty%d",
656 rootfs
->mount
, ttydir
, i
+ 1);
657 if (ret
>= sizeof(lxcpath
)) {
658 ERROR("pathname too long for ttys");
661 ret
= creat(lxcpath
, 0660);
662 if (ret
==-1 && errno
!= EEXIST
) {
663 SYSERROR("error creating %s\n", lxcpath
);
668 if (ret
&& errno
!= ENOENT
) {
669 SYSERROR("error unlinking %s\n", path
);
673 if (mount(pty_info
->name
, lxcpath
, "none", MS_BIND
, 0)) {
674 WARN("failed to mount '%s'->'%s'",
675 pty_info
->name
, path
);
679 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/tty%d", ttydir
, i
+1);
680 if (ret
>= sizeof(lxcpath
)) {
681 ERROR("tty pathname too long");
684 ret
= symlink(lxcpath
, path
);
686 SYSERROR("failed to create symlink for tty %d\n", i
+1);
690 /* If we populated /dev, then we need to create /dev/ttyN */
691 if (access(path
, F_OK
)) {
692 ret
= creat(path
, 0660);
694 SYSERROR("error creating %s\n", path
);
695 /* this isn't fatal, continue */
699 if (mount(pty_info
->name
, path
, "none", MS_BIND
, 0)) {
700 WARN("failed to mount '%s'->'%s'",
701 pty_info
->name
, path
);
707 INFO("%d tty(s) has been setup", tty_info
->nbtty
);
712 static int setup_rootfs_pivot_root_cb(char *buffer
, void *data
)
714 struct lxc_list
*mountlist
, *listentry
, *iterator
;
715 char *pivotdir
, *mountpoint
, *mountentry
;
720 cbparm
= (void **)data
;
722 mountlist
= cbparm
[0];
723 pivotdir
= cbparm
[1];
725 /* parse entry, first field is mountname, ignore */
726 mountpoint
= strtok(mountentry
, " ");
730 /* second field is mountpoint */
731 mountpoint
= strtok(NULL
, " ");
735 /* only consider mountpoints below old root fs */
736 if (strncmp(mountpoint
, pivotdir
, strlen(pivotdir
)))
739 /* filter duplicate mountpoints */
741 lxc_list_for_each(iterator
, mountlist
) {
742 if (!strcmp(iterator
->elem
, mountpoint
)) {
750 /* add entry to list */
751 listentry
= malloc(sizeof(*listentry
));
753 SYSERROR("malloc for mountpoint listentry failed");
757 listentry
->elem
= strdup(mountpoint
);
758 if (!listentry
->elem
) {
759 SYSERROR("strdup failed");
762 lxc_list_add_tail(mountlist
, listentry
);
767 static int umount_oldrootfs(const char *oldrootfs
)
769 char path
[MAXPATHLEN
];
771 struct lxc_list mountlist
, *iterator
, *next
;
772 int ok
, still_mounted
, last_still_mounted
;
775 /* read and parse /proc/mounts in old root fs */
776 lxc_list_init(&mountlist
);
778 /* oldrootfs is on the top tree directory now */
779 rc
= snprintf(path
, sizeof(path
), "/%s", oldrootfs
);
780 if (rc
>= sizeof(path
)) {
781 ERROR("rootfs name too long");
784 cbparm
[0] = &mountlist
;
786 cbparm
[1] = strdup(path
);
788 SYSERROR("strdup failed");
792 rc
= snprintf(path
, sizeof(path
), "%s/proc/mounts", oldrootfs
);
793 if (rc
>= sizeof(path
)) {
794 ERROR("container proc/mounts name too long");
798 ok
= lxc_file_for_each_line(path
,
799 setup_rootfs_pivot_root_cb
, &cbparm
);
801 SYSERROR("failed to read or parse mount list '%s'", path
);
805 /* umount filesystems until none left or list no longer shrinks */
808 last_still_mounted
= still_mounted
;
811 lxc_list_for_each_safe(iterator
, &mountlist
, next
) {
813 /* umount normally */
814 if (!umount(iterator
->elem
)) {
815 DEBUG("umounted '%s'", (char *)iterator
->elem
);
816 lxc_list_del(iterator
);
823 } while (still_mounted
> 0 && still_mounted
!= last_still_mounted
);
826 lxc_list_for_each(iterator
, &mountlist
) {
828 /* let's try a lazy umount */
829 if (!umount2(iterator
->elem
, MNT_DETACH
)) {
830 INFO("lazy unmount of '%s'", (char *)iterator
->elem
);
834 /* be more brutal (nfs) */
835 if (!umount2(iterator
->elem
, MNT_FORCE
)) {
836 INFO("forced unmount of '%s'", (char *)iterator
->elem
);
840 WARN("failed to unmount '%s'", (char *)iterator
->elem
);
846 static int setup_rootfs_pivot_root(const char *rootfs
, const char *pivotdir
)
848 char path
[MAXPATHLEN
];
849 int remove_pivotdir
= 0;
852 /* change into new root fs */
854 SYSERROR("can't chdir to new rootfs '%s'", rootfs
);
859 pivotdir
= "lxc_putold";
861 /* compute the full path to pivotdir under rootfs */
862 rc
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, pivotdir
);
863 if (rc
>= sizeof(path
)) {
864 ERROR("pivot dir name too long");
868 if (access(path
, F_OK
)) {
870 if (mkdir_p(path
, 0755)) {
871 SYSERROR("failed to create pivotdir '%s'", path
);
876 DEBUG("created '%s' directory", path
);
879 DEBUG("mountpoint for old rootfs is '%s'", path
);
881 /* pivot_root into our new root fs */
882 if (pivot_root(".", path
)) {
883 SYSERROR("pivot_root syscall failed");
888 SYSERROR("can't chdir to / after pivot_root");
892 DEBUG("pivot_root syscall to '%s' successful", rootfs
);
894 /* we switch from absolute path to relative path */
895 if (umount_oldrootfs(pivotdir
))
898 /* remove temporary mount point, we don't consider the removing
900 if (remove_pivotdir
&& rmdir(pivotdir
))
901 WARN("can't remove mountpoint '%s': %m", pivotdir
);
907 * Do we want to add options for max size of /dev and a file to
908 * specify which devices to create?
910 static int mount_autodev(char *root
)
913 char path
[MAXPATHLEN
];
915 INFO("Mounting /dev under %s\n", root
);
916 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
917 if (ret
< 0 || ret
> MAXPATHLEN
)
919 ret
= mount("none", path
, "tmpfs", 0, "size=100000");
921 SYSERROR("Failed to mount /dev at %s\n", root
);
924 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/pts", root
);
925 if (ret
< 0 || ret
>= MAXPATHLEN
)
927 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
929 SYSERROR("Failed to create /dev/pts in container");
933 INFO("Mounted /dev under %s\n", root
);
938 * Try to run MAKEDEV console in the container. If something fails,
939 * continue anyway as it should not be detrimental to the container.
940 * This makes sure that things like /dev/vcs* exist.
941 * (Pass devpath in to reduce stack usage)
943 static void run_makedev(char *devpath
)
948 curd
= open(".", O_RDONLY
);
951 ret
= chdir(devpath
);
956 if (run_buffer("/sbin/MAKEDEV console"))
957 INFO("Error running MAKEDEV console in %s", devpath
);
960 INFO("Error returning to original directory: expect breakage");
971 struct lxc_devs lxc_devs
[] = {
972 { "null", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 3 },
973 { "zero", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 5 },
974 { "full", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 7 },
975 { "urandom", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 9 },
976 { "random", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 8 },
977 { "tty", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 5, 0 },
978 { "console", S_IFCHR
| S_IRUSR
| S_IWUSR
, 5, 1 },
981 static int setup_autodev(char *root
)
985 char path
[MAXPATHLEN
];
989 INFO("Creating initial consoles under %s/dev\n", root
);
991 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
992 if (ret
< 0 || ret
>= MAXPATHLEN
) {
993 ERROR("Error calculating container /dev location");
998 INFO("Populating /dev under %s\n", root
);
999 cmask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1000 for (i
= 0; i
< sizeof(lxc_devs
) / sizeof(lxc_devs
[0]); i
++) {
1002 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/%s", root
, d
->name
);
1003 if (ret
< 0 || ret
>= MAXPATHLEN
)
1005 ret
= mknod(path
, d
->mode
, makedev(d
->maj
, d
->min
));
1006 if (ret
&& errno
!= EEXIST
) {
1007 SYSERROR("Error creating %s\n", d
->name
);
1013 INFO("Populated /dev under %s\n", root
);
1018 * Detect whether / is mounted MS_SHARED. The only way I know of to
1019 * check that is through /proc/self/mountinfo.
1020 * I'm only checking for /. If the container rootfs or mount location
1021 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1022 * out would be too much work to be worth it.
1024 #define LINELEN 4096
1025 int detect_shared_rootfs(void)
1027 char buf
[LINELEN
], *p
;
1032 f
= fopen("/proc/self/mountinfo", "r");
1035 while ((p
= fgets(buf
, LINELEN
, f
))) {
1036 INFO("looking at .%s.", p
);
1037 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1038 p
= index(p
+1, ' ');
1041 p2
= index(p
+1, ' ');
1045 INFO("now p is .%s.", p
);
1046 if (strcmp(p
+1, "/") == 0) {
1047 // this is '/'. is it shared?
1048 p
= index(p2
+1, ' ');
1049 if (strstr(p
, "shared:"))
1058 * I'll forgive you for asking whether all of this is needed :) The
1060 * pivot_root will fail if the new root, the put_old dir, or the parent
1061 * of current->fs->root are MS_SHARED. (parent of current->fs_root may
1062 * or may not be current->fs_root - if we assumed it always was, we could
1063 * just mount --make-rslave /). So,
1064 * 1. mount a tiny tmpfs to be parent of current->fs->root.
1065 * 2. make that MS_SLAVE
1066 * 3. make a 'root' directory under that
1067 * 4. mount --rbind / under the $tinyroot/root.
1068 * 5. make that rslave
1069 * 6. chdir and chroot into $tinyroot/root
1070 * 7. $tinyroot will be unmounted by our parent in start.c
1072 static int chroot_into_slave(struct lxc_conf
*conf
)
1074 char path
[MAXPATHLEN
];
1075 const char *destpath
= conf
->rootfs
.mount
;
1078 if (mount(destpath
, destpath
, NULL
, MS_BIND
, 0)) {
1079 SYSERROR("failed to mount %s bind", destpath
);
1082 if (mount("", destpath
, NULL
, MS_SLAVE
, 0)) {
1083 SYSERROR("failed to make %s slave", destpath
);
1086 if (mount("none", destpath
, "tmpfs", 0, "size=10000")) {
1087 SYSERROR("Failed to mount tmpfs / at %s", destpath
);
1090 ret
= snprintf(path
, MAXPATHLEN
, "%s/root", destpath
);
1091 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1092 ERROR("out of memory making root path");
1095 if (mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)) {
1096 SYSERROR("Failed to create /dev/pts in container");
1099 if (mount("/", path
, NULL
, MS_BIND
|MS_REC
, 0)) {
1100 SYSERROR("Failed to rbind mount / to %s", path
);
1103 if (mount("", destpath
, NULL
, MS_SLAVE
|MS_REC
, 0)) {
1104 SYSERROR("Failed to make tmp-/ at %s rslave", path
);
1108 SYSERROR("Failed to chdir into tmp-/");
1112 SYSERROR("Failed to chroot into tmp-/");
1115 INFO("Chrooted into tmp-/ at %s\n", path
);
1119 static int setup_rootfs(struct lxc_conf
*conf
)
1121 const struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1126 if (access(rootfs
->mount
, F_OK
)) {
1127 SYSERROR("failed to access to '%s', check it is present",
1132 if (detect_shared_rootfs()) {
1133 if (chroot_into_slave(conf
)) {
1134 ERROR("Failed to chroot into slave /");
1139 if (mount_rootfs(rootfs
->path
, rootfs
->mount
)) {
1140 ERROR("failed to mount rootfs");
1144 DEBUG("mounted '%s' on '%s'", rootfs
->path
, rootfs
->mount
);
1149 int setup_pivot_root(const struct lxc_rootfs
*rootfs
)
1154 if (setup_rootfs_pivot_root(rootfs
->mount
, rootfs
->pivot
)) {
1155 ERROR("failed to setup pivot root");
1162 static int setup_pts(int pts
)
1164 char target
[PATH_MAX
];
1169 if (!access("/dev/pts/ptmx", F_OK
) && umount("/dev/pts")) {
1170 SYSERROR("failed to umount 'dev/pts'");
1174 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
,
1175 "newinstance,ptmxmode=0666")) {
1176 SYSERROR("failed to mount a new instance of '/dev/pts'");
1180 if (access("/dev/ptmx", F_OK
)) {
1181 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
1183 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
1187 if (realpath("/dev/ptmx", target
) && !strcmp(target
, "/dev/pts/ptmx"))
1190 /* fallback here, /dev/pts/ptmx exists just mount bind */
1191 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND
, 0)) {
1192 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
1196 INFO("created new pts instance");
1202 static int setup_personality(int persona
)
1204 #if HAVE_SYS_PERSONALITY_H
1208 if (personality(persona
) < 0) {
1209 SYSERROR("failed to set personality to '0x%x'", persona
);
1213 INFO("set personality to '0x%x'", persona
);
1219 static int setup_dev_console(const struct lxc_rootfs
*rootfs
,
1220 const struct lxc_console
*console
)
1222 char path
[MAXPATHLEN
];
1226 ret
= snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1227 if (ret
>= sizeof(path
)) {
1228 ERROR("console path too long\n");
1232 if (access(path
, F_OK
)) {
1233 WARN("rootfs specified but no console found at '%s'", path
);
1237 if (console
->peer
== -1) {
1238 INFO("no console output required");
1242 if (stat(path
, &s
)) {
1243 SYSERROR("failed to stat '%s'", path
);
1247 if (chmod(console
->name
, s
.st_mode
)) {
1248 SYSERROR("failed to set mode '0%o' to '%s'",
1249 s
.st_mode
, console
->name
);
1253 if (mount(console
->name
, path
, "none", MS_BIND
, 0)) {
1254 ERROR("failed to mount '%s' on '%s'", console
->name
, path
);
1258 INFO("console has been setup");
1262 static int setup_ttydir_console(const struct lxc_rootfs
*rootfs
,
1263 const struct lxc_console
*console
,
1266 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
1269 /* create rootfs/dev/<ttydir> directory */
1270 ret
= snprintf(path
, sizeof(path
), "%s/dev/%s", rootfs
->mount
,
1272 if (ret
>= sizeof(path
))
1274 ret
= mkdir(path
, 0755);
1275 if (ret
&& errno
!= EEXIST
) {
1276 SYSERROR("failed with errno %d to create %s\n", errno
, path
);
1279 INFO("created %s\n", path
);
1281 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/console",
1282 rootfs
->mount
, ttydir
);
1283 if (ret
>= sizeof(lxcpath
)) {
1284 ERROR("console path too long\n");
1288 snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1290 if (ret
&& errno
!= ENOENT
) {
1291 SYSERROR("error unlinking %s\n", path
);
1295 ret
= creat(lxcpath
, 0660);
1296 if (ret
==-1 && errno
!= EEXIST
) {
1297 SYSERROR("error %d creating %s\n", errno
, lxcpath
);
1302 if (console
->peer
== -1) {
1303 INFO("no console output required");
1307 if (mount(console
->name
, lxcpath
, "none", MS_BIND
, 0)) {
1308 ERROR("failed to mount '%s' on '%s'", console
->name
, lxcpath
);
1312 /* create symlink from rootfs/dev/console to 'lxc/console' */
1313 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/console", ttydir
);
1314 if (ret
>= sizeof(lxcpath
)) {
1315 ERROR("lxc/console path too long");
1318 ret
= symlink(lxcpath
, path
);
1320 SYSERROR("failed to create symlink for console");
1324 INFO("console has been setup on %s", lxcpath
);
1329 static int setup_console(const struct lxc_rootfs
*rootfs
,
1330 const struct lxc_console
*console
,
1333 /* We don't have a rootfs, /dev/console will be shared */
1337 return setup_dev_console(rootfs
, console
);
1339 return setup_ttydir_console(rootfs
, console
, ttydir
);
1342 static int setup_kmsg(const struct lxc_rootfs
*rootfs
,
1343 const struct lxc_console
*console
)
1345 char kpath
[MAXPATHLEN
];
1350 ret
= snprintf(kpath
, sizeof(kpath
), "%s/dev/kmsg", rootfs
->mount
);
1351 if (ret
< 0 || ret
>= sizeof(kpath
))
1354 ret
= unlink(kpath
);
1355 if (ret
&& errno
!= ENOENT
) {
1356 SYSERROR("error unlinking %s\n", kpath
);
1360 ret
= symlink("console", kpath
);
1362 SYSERROR("failed to create symlink for kmsg");
1369 int setup_cgroup(const char *name
, struct lxc_list
*cgroups
)
1371 struct lxc_list
*iterator
;
1372 struct lxc_cgroup
*cg
;
1375 if (lxc_list_empty(cgroups
))
1378 lxc_list_for_each(iterator
, cgroups
) {
1380 cg
= iterator
->elem
;
1382 if (lxc_cgroup_set(name
, cg
->subsystem
, cg
->value
))
1385 DEBUG("cgroup '%s' set to '%s'", cg
->subsystem
, cg
->value
);
1389 INFO("cgroup has been setup");
1394 static void parse_mntopt(char *opt
, unsigned long *flags
, char **data
)
1396 struct mount_opt
*mo
;
1398 /* If opt is found in mount_opt, set or clear flags.
1399 * Otherwise append it to data. */
1401 for (mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
1402 if (!strncmp(opt
, mo
->name
, strlen(mo
->name
))) {
1404 *flags
&= ~mo
->flag
;
1416 static int parse_mntopts(const char *mntopts
, unsigned long *mntflags
,
1420 char *p
, *saveptr
= NULL
;
1428 s
= strdup(mntopts
);
1430 SYSERROR("failed to allocate memory");
1434 data
= malloc(strlen(s
) + 1);
1436 SYSERROR("failed to allocate memory");
1442 for (p
= strtok_r(s
, ",", &saveptr
); p
!= NULL
;
1443 p
= strtok_r(NULL
, ",", &saveptr
))
1444 parse_mntopt(p
, mntflags
, &data
);
1455 static int mount_entry(const char *fsname
, const char *target
,
1456 const char *fstype
, unsigned long mountflags
,
1459 if (mount(fsname
, target
, fstype
, mountflags
& ~MS_REMOUNT
, data
)) {
1460 SYSERROR("failed to mount '%s' on '%s'", fsname
, target
);
1464 if ((mountflags
& MS_REMOUNT
) || (mountflags
& MS_BIND
)) {
1466 DEBUG("remounting %s on %s to respect bind or remount options",
1469 if (mount(fsname
, target
, fstype
,
1470 mountflags
| MS_REMOUNT
, data
)) {
1471 SYSERROR("failed to mount '%s' on '%s'",
1477 DEBUG("mounted '%s' on '%s', type '%s'", fsname
, target
, fstype
);
1482 static inline int mount_entry_on_systemfs(struct mntent
*mntent
)
1484 unsigned long mntflags
;
1488 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1489 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1493 ret
= mount_entry(mntent
->mnt_fsname
, mntent
->mnt_dir
,
1494 mntent
->mnt_type
, mntflags
, mntdata
);
1501 static int mount_entry_on_absolute_rootfs(struct mntent
*mntent
,
1502 const struct lxc_rootfs
*rootfs
,
1503 const char *lxc_name
)
1506 char path
[MAXPATHLEN
];
1507 unsigned long mntflags
;
1509 int r
, ret
= 0, offset
;
1511 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1512 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1516 /* if rootfs->path is a blockdev path, allow container fstab to
1517 * use $LXCPATH/CN/rootfs as the target prefix */
1518 r
= snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/rootfs", lxc_name
);
1519 if (r
< 0 || r
>= MAXPATHLEN
)
1522 aux
= strstr(mntent
->mnt_dir
, path
);
1524 offset
= strlen(path
);
1529 aux
= strstr(mntent
->mnt_dir
, rootfs
->path
);
1531 WARN("ignoring mount point '%s'", mntent
->mnt_dir
);
1534 offset
= strlen(rootfs
->path
);
1538 r
= snprintf(path
, MAXPATHLEN
, "%s/%s", rootfs
->mount
,
1540 if (r
< 0 || r
>= MAXPATHLEN
) {
1541 WARN("pathnme too long for '%s'", mntent
->mnt_dir
);
1547 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1555 static int mount_entry_on_relative_rootfs(struct mntent
*mntent
,
1558 char path
[MAXPATHLEN
];
1559 unsigned long mntflags
;
1563 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1564 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1568 /* relative to root mount point */
1569 ret
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, mntent
->mnt_dir
);
1570 if (ret
>= sizeof(path
)) {
1571 ERROR("path name too long");
1575 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1583 static int mount_file_entries(const struct lxc_rootfs
*rootfs
, FILE *file
,
1584 const char *lxc_name
)
1586 struct mntent
*mntent
;
1589 while ((mntent
= getmntent(file
))) {
1591 if (!rootfs
->path
) {
1592 if (mount_entry_on_systemfs(mntent
))
1597 /* We have a separate root, mounts are relative to it */
1598 if (mntent
->mnt_dir
[0] != '/') {
1599 if (mount_entry_on_relative_rootfs(mntent
,
1605 if (mount_entry_on_absolute_rootfs(mntent
, rootfs
, lxc_name
))
1611 INFO("mount points have been setup");
1616 static int setup_mount(const struct lxc_rootfs
*rootfs
, const char *fstab
,
1617 const char *lxc_name
)
1625 file
= setmntent(fstab
, "r");
1627 SYSERROR("failed to use '%s'", fstab
);
1631 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1637 static int setup_mount_entries(const struct lxc_rootfs
*rootfs
, struct lxc_list
*mount
,
1638 const char *lxc_name
)
1641 struct lxc_list
*iterator
;
1647 ERROR("tmpfile error: %m");
1651 lxc_list_for_each(iterator
, mount
) {
1652 mount_entry
= iterator
->elem
;
1653 fprintf(file
, "%s\n", mount_entry
);
1658 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1664 static int setup_caps(struct lxc_list
*caps
)
1666 struct lxc_list
*iterator
;
1671 lxc_list_for_each(iterator
, caps
) {
1673 drop_entry
= iterator
->elem
;
1677 for (i
= 0; i
< sizeof(caps_opt
)/sizeof(caps_opt
[0]); i
++) {
1679 if (strcmp(drop_entry
, caps_opt
[i
].name
))
1682 capid
= caps_opt
[i
].value
;
1687 /* try to see if it's numeric, so the user may specify
1688 * capabilities that the running kernel knows about but
1690 capid
= strtol(drop_entry
, &ptr
, 10);
1691 if (!ptr
|| *ptr
!= '\0' ||
1692 capid
== LONG_MIN
|| capid
== LONG_MAX
)
1693 /* not a valid number */
1695 else if (capid
> lxc_caps_last_cap())
1696 /* we have a number but it's not a valid
1702 ERROR("unknown capability %s", drop_entry
);
1706 DEBUG("drop capability '%s' (%d)", drop_entry
, capid
);
1708 if (prctl(PR_CAPBSET_DROP
, capid
, 0, 0, 0)) {
1709 SYSERROR("failed to remove %s capability", drop_entry
);
1715 DEBUG("capabilities has been setup");
1720 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
1722 struct sockaddr sockaddr
;
1726 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
1728 ERROR("mac address '%s' conversion failed : %s",
1729 hwaddr
, strerror(-ret
));
1733 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1734 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
1736 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1738 ERROR("socket failure : %s", strerror(errno
));
1742 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
1745 ERROR("ioctl failure : %s", strerror(errno
));
1747 DEBUG("mac address '%s' on '%s' has been setup", hwaddr
, ifname
);
1752 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
1754 struct lxc_list
*iterator
;
1755 struct lxc_inetdev
*inetdev
;
1758 lxc_list_for_each(iterator
, ip
) {
1760 inetdev
= iterator
->elem
;
1762 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
1763 &inetdev
->bcast
, inetdev
->prefix
);
1765 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
1766 ifindex
, strerror(-err
));
1774 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
1776 struct lxc_list
*iterator
;
1777 struct lxc_inet6dev
*inet6dev
;
1780 lxc_list_for_each(iterator
, ip
) {
1782 inet6dev
= iterator
->elem
;
1784 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
1785 &inet6dev
->mcast
, &inet6dev
->acast
,
1788 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
1789 ifindex
, strerror(-err
));
1797 static int setup_netdev(struct lxc_netdev
*netdev
)
1799 char ifname
[IFNAMSIZ
];
1800 char *current_ifname
= ifname
;
1803 /* empty network namespace */
1804 if (!netdev
->ifindex
) {
1805 if (netdev
->flags
& IFF_UP
) {
1806 err
= lxc_netdev_up("lo");
1808 ERROR("failed to set the loopback up : %s",
1816 /* retrieve the name of the interface */
1817 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1818 ERROR("no interface corresponding to index '%d'",
1823 /* default: let the system to choose one interface name */
1825 netdev
->name
= netdev
->type
== LXC_NET_PHYS
?
1826 netdev
->link
: "eth%d";
1828 /* rename the interface name */
1829 err
= lxc_netdev_rename_by_name(ifname
, netdev
->name
);
1831 ERROR("failed to rename %s->%s : %s", ifname
, netdev
->name
,
1836 /* Re-read the name of the interface because its name has changed
1837 * and would be automatically allocated by the system
1839 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1840 ERROR("no interface corresponding to index '%d'",
1845 /* set a mac address */
1846 if (netdev
->hwaddr
) {
1847 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
1848 ERROR("failed to setup hw address for '%s'",
1854 /* setup ipv4 addresses on the interface */
1855 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
1856 ERROR("failed to setup ip addresses for '%s'",
1861 /* setup ipv6 addresses on the interface */
1862 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
1863 ERROR("failed to setup ipv6 addresses for '%s'",
1868 /* set the network device up */
1869 if (netdev
->flags
& IFF_UP
) {
1872 err
= lxc_netdev_up(current_ifname
);
1874 ERROR("failed to set '%s' up : %s", current_ifname
,
1879 /* the network is up, make the loopback up too */
1880 err
= lxc_netdev_up("lo");
1882 ERROR("failed to set the loopback up : %s",
1888 /* We can only set up the default routes after bringing
1889 * up the interface, sine bringing up the interface adds
1890 * the link-local routes and we can't add a default
1891 * route if the gateway is not reachable. */
1893 /* setup ipv4 gateway on the interface */
1894 if (netdev
->ipv4_gateway
) {
1895 if (!(netdev
->flags
& IFF_UP
)) {
1896 ERROR("Cannot add ipv4 gateway for %s when not bringing up the interface", ifname
);
1900 if (lxc_list_empty(&netdev
->ipv4
)) {
1901 ERROR("Cannot add ipv4 gateway for %s when not assigning an address", ifname
);
1905 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
1907 ERROR("failed to setup ipv4 gateway for '%s': %s",
1908 ifname
, strerror(-err
));
1909 if (netdev
->ipv4_gateway_auto
) {
1910 char buf
[INET_ADDRSTRLEN
];
1911 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
1912 ERROR("tried to set autodetected ipv4 gateway '%s'", buf
);
1918 /* setup ipv6 gateway on the interface */
1919 if (netdev
->ipv6_gateway
) {
1920 if (!(netdev
->flags
& IFF_UP
)) {
1921 ERROR("Cannot add ipv6 gateway for %s when not bringing up the interface", ifname
);
1925 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
1926 ERROR("Cannot add ipv6 gateway for %s when not assigning an address", ifname
);
1930 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
1932 ERROR("failed to setup ipv6 gateway for '%s': %s",
1933 ifname
, strerror(-err
));
1934 if (netdev
->ipv6_gateway_auto
) {
1935 char buf
[INET6_ADDRSTRLEN
];
1936 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
1937 ERROR("tried to set autodetected ipv6 gateway '%s'", buf
);
1943 DEBUG("'%s' has been setup", current_ifname
);
1948 static int setup_network(struct lxc_list
*network
)
1950 struct lxc_list
*iterator
;
1951 struct lxc_netdev
*netdev
;
1953 lxc_list_for_each(iterator
, network
) {
1955 netdev
= iterator
->elem
;
1957 if (setup_netdev(netdev
)) {
1958 ERROR("failed to setup netdev");
1963 if (!lxc_list_empty(network
))
1964 INFO("network has been setup");
1969 void lxc_rename_phys_nics_on_shutdown(struct lxc_conf
*conf
)
1973 INFO("running to reset %d nic names", conf
->num_savednics
);
1974 for (i
=0; i
<conf
->num_savednics
; i
++) {
1975 struct saved_nic
*s
= &conf
->saved_nics
[i
];
1976 INFO("resetting nic %d to %s\n", s
->ifindex
, s
->orig_name
);
1977 lxc_netdev_rename_by_index(s
->ifindex
, s
->orig_name
);
1980 conf
->num_savednics
= 0;
1981 free(conf
->saved_nics
);
1984 static int setup_private_host_hw_addr(char *veth1
)
1990 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1994 snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
1995 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2001 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2002 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2007 DEBUG("mac address of host interface '%s' changed to private "
2008 "%02x:%02x:%02x:%02x:%02x:%02x", veth1
,
2009 ifr
.ifr_hwaddr
.sa_data
[0] & 0xff,
2010 ifr
.ifr_hwaddr
.sa_data
[1] & 0xff,
2011 ifr
.ifr_hwaddr
.sa_data
[2] & 0xff,
2012 ifr
.ifr_hwaddr
.sa_data
[3] & 0xff,
2013 ifr
.ifr_hwaddr
.sa_data
[4] & 0xff,
2014 ifr
.ifr_hwaddr
.sa_data
[5] & 0xff);
2019 static char *default_rootfs_mount
= LXCROOTFSMOUNT
;
2021 struct lxc_conf
*lxc_conf_init(void)
2023 struct lxc_conf
*new;
2026 new = malloc(sizeof(*new));
2028 ERROR("lxc_conf_init : %m");
2031 memset(new, 0, sizeof(*new));
2033 new->personality
= -1;
2034 new->console
.log_path
= NULL
;
2035 new->console
.log_fd
= -1;
2036 new->console
.path
= NULL
;
2037 new->console
.peer
= -1;
2038 new->console
.master
= -1;
2039 new->console
.slave
= -1;
2040 new->console
.name
[0] = '\0';
2041 new->maincmd_fd
= -1;
2042 new->rootfs
.mount
= default_rootfs_mount
;
2043 new->loglevel
= LXC_LOG_PRIORITY_NOTSET
;
2044 lxc_list_init(&new->cgroup
);
2045 lxc_list_init(&new->network
);
2046 lxc_list_init(&new->mount_list
);
2047 lxc_list_init(&new->caps
);
2048 for (i
=0; i
<NUM_LXC_HOOKS
; i
++)
2049 lxc_list_init(&new->hooks
[i
]);
2051 new->aa_profile
= NULL
;
2053 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2054 new->lsm_umount_proc
= 0;
2060 static int instanciate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2062 char veth1buf
[IFNAMSIZ
], *veth1
;
2063 char veth2buf
[IFNAMSIZ
], *veth2
;
2066 if (netdev
->priv
.veth_attr
.pair
)
2067 veth1
= netdev
->priv
.veth_attr
.pair
;
2069 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
2070 if (err
>= sizeof(veth1buf
)) { /* can't *really* happen, but... */
2071 ERROR("veth1 name too long");
2074 veth1
= mktemp(veth1buf
);
2075 /* store away for deconf */
2076 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
2079 snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
2080 veth2
= mktemp(veth2buf
);
2082 if (!strlen(veth1
) || !strlen(veth2
)) {
2083 ERROR("failed to allocate a temporary name");
2087 err
= lxc_veth_create(veth1
, veth2
);
2089 ERROR("failed to create %s-%s : %s", veth1
, veth2
,
2094 /* changing the high byte of the mac address to 0xfe, the bridge interface
2095 * will always keep the host's mac address and not take the mac address
2097 err
= setup_private_host_hw_addr(veth1
);
2099 ERROR("failed to change mac address of host interface '%s' : %s",
2100 veth1
, strerror(-err
));
2105 err
= lxc_netdev_set_mtu(veth1
, atoi(netdev
->mtu
));
2107 err
= lxc_netdev_set_mtu(veth2
, atoi(netdev
->mtu
));
2109 ERROR("failed to set mtu '%s' for %s-%s : %s",
2110 netdev
->mtu
, veth1
, veth2
, strerror(-err
));
2116 err
= lxc_bridge_attach(netdev
->link
, veth1
);
2118 ERROR("failed to attach '%s' to the bridge '%s' : %s",
2119 veth1
, netdev
->link
, strerror(-err
));
2124 netdev
->ifindex
= if_nametoindex(veth2
);
2125 if (!netdev
->ifindex
) {
2126 ERROR("failed to retrieve the index for %s", veth2
);
2130 err
= lxc_netdev_up(veth1
);
2132 ERROR("failed to set %s up : %s", veth1
, strerror(-err
));
2136 if (netdev
->upscript
) {
2137 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2138 "veth", veth1
, (char*) NULL
);
2143 DEBUG("instanciated veth '%s/%s', index is '%d'",
2144 veth1
, veth2
, netdev
->ifindex
);
2149 lxc_netdev_delete_by_name(veth1
);
2153 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2158 if (netdev
->priv
.veth_attr
.pair
)
2159 veth1
= netdev
->priv
.veth_attr
.pair
;
2161 veth1
= netdev
->priv
.veth_attr
.veth1
;
2163 if (netdev
->downscript
) {
2164 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2165 "down", "veth", veth1
, (char*) NULL
);
2172 static int instanciate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2174 char peerbuf
[IFNAMSIZ
], *peer
;
2177 if (!netdev
->link
) {
2178 ERROR("no link specified for macvlan netdev");
2182 err
= snprintf(peerbuf
, sizeof(peerbuf
), "mcXXXXXX");
2183 if (err
>= sizeof(peerbuf
))
2186 peer
= mktemp(peerbuf
);
2187 if (!strlen(peer
)) {
2188 ERROR("failed to make a temporary name");
2192 err
= lxc_macvlan_create(netdev
->link
, peer
,
2193 netdev
->priv
.macvlan_attr
.mode
);
2195 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
2196 peer
, netdev
->link
, strerror(-err
));
2200 netdev
->ifindex
= if_nametoindex(peer
);
2201 if (!netdev
->ifindex
) {
2202 ERROR("failed to retrieve the index for %s", peer
);
2203 lxc_netdev_delete_by_name(peer
);
2207 if (netdev
->upscript
) {
2208 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2209 "macvlan", netdev
->link
, (char*) NULL
);
2214 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
2215 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
2220 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2224 if (netdev
->downscript
) {
2225 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2226 "down", "macvlan", netdev
->link
,
2234 /* XXX: merge with instanciate_macvlan */
2235 static int instanciate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2237 char peer
[IFNAMSIZ
];
2240 if (!netdev
->link
) {
2241 ERROR("no link specified for vlan netdev");
2245 err
= snprintf(peer
, sizeof(peer
), "vlan%d", netdev
->priv
.vlan_attr
.vid
);
2246 if (err
>= sizeof(peer
)) {
2247 ERROR("peer name too long");
2251 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
2253 ERROR("failed to create vlan interface '%s' on '%s' : %s",
2254 peer
, netdev
->link
, strerror(-err
));
2258 netdev
->ifindex
= if_nametoindex(peer
);
2259 if (!netdev
->ifindex
) {
2260 ERROR("failed to retrieve the ifindex for %s", peer
);
2261 lxc_netdev_delete_by_name(peer
);
2265 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
2271 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2276 static int instanciate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2278 if (!netdev
->link
) {
2279 ERROR("no link specified for the physical interface");
2283 netdev
->ifindex
= if_nametoindex(netdev
->link
);
2284 if (!netdev
->ifindex
) {
2285 ERROR("failed to retrieve the index for %s", netdev
->link
);
2289 if (netdev
->upscript
) {
2291 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2292 "up", "phys", netdev
->link
, (char*) NULL
);
2300 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2304 if (netdev
->downscript
) {
2305 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2306 "down", "phys", netdev
->link
, (char*) NULL
);
2313 static int instanciate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2315 netdev
->ifindex
= 0;
2316 if (netdev
->upscript
) {
2318 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2319 "up", "empty", (char*) NULL
);
2326 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2330 if (netdev
->downscript
) {
2331 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2332 "down", "empty", (char*) NULL
);
2339 int lxc_create_network(struct lxc_handler
*handler
)
2341 struct lxc_list
*network
= &handler
->conf
->network
;
2342 struct lxc_list
*iterator
;
2343 struct lxc_netdev
*netdev
;
2345 lxc_list_for_each(iterator
, network
) {
2347 netdev
= iterator
->elem
;
2349 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
2350 ERROR("invalid network configuration type '%d'",
2355 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
2356 ERROR("failed to create netdev");
2365 void lxc_delete_network(struct lxc_handler
*handler
)
2367 struct lxc_list
*network
= &handler
->conf
->network
;
2368 struct lxc_list
*iterator
;
2369 struct lxc_netdev
*netdev
;
2371 lxc_list_for_each(iterator
, network
) {
2372 netdev
= iterator
->elem
;
2374 if (netdev
->ifindex
!= 0 && netdev
->type
== LXC_NET_PHYS
) {
2375 if (lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
))
2376 WARN("failed to rename to the initial name the " \
2377 "netdev '%s'", netdev
->link
);
2381 if (netdev_deconf
[netdev
->type
](handler
, netdev
)) {
2382 WARN("failed to destroy netdev");
2385 /* Recent kernel remove the virtual interfaces when the network
2386 * namespace is destroyed but in case we did not moved the
2387 * interface to the network namespace, we have to destroy it
2389 if (netdev
->ifindex
!= 0 &&
2390 lxc_netdev_delete_by_index(netdev
->ifindex
))
2391 WARN("failed to remove interface '%s'", netdev
->name
);
2395 int lxc_assign_network(struct lxc_list
*network
, pid_t pid
)
2397 struct lxc_list
*iterator
;
2398 struct lxc_netdev
*netdev
;
2401 lxc_list_for_each(iterator
, network
) {
2403 netdev
= iterator
->elem
;
2405 /* empty network namespace, nothing to move */
2406 if (!netdev
->ifindex
)
2409 err
= lxc_netdev_move_by_index(netdev
->ifindex
, pid
);
2411 ERROR("failed to move '%s' to the container : %s",
2412 netdev
->link
, strerror(-err
));
2416 DEBUG("move '%s' to '%d'", netdev
->name
, pid
);
2422 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2424 struct lxc_list
*network
= &handler
->conf
->network
;
2425 struct lxc_list
*iterator
;
2426 struct lxc_netdev
*netdev
;
2429 lxc_list_for_each(iterator
, network
) {
2430 netdev
= iterator
->elem
;
2432 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2435 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2436 ERROR("gateway = auto only supported for "
2437 "veth and macvlan");
2441 if (!netdev
->link
) {
2442 ERROR("gateway = auto needs a link interface");
2446 link_index
= if_nametoindex(netdev
->link
);
2450 if (netdev
->ipv4_gateway_auto
) {
2451 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2452 ERROR("failed to automatically find ipv4 gateway "
2453 "address from link interface '%s'", netdev
->link
);
2458 if (netdev
->ipv6_gateway_auto
) {
2459 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2460 ERROR("failed to automatically find ipv6 gateway "
2461 "address from link interface '%s'", netdev
->link
);
2470 int lxc_create_tty(const char *name
, struct lxc_conf
*conf
)
2472 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2475 /* no tty in the configuration */
2479 tty_info
->pty_info
=
2480 malloc(sizeof(*tty_info
->pty_info
)*conf
->tty
);
2481 if (!tty_info
->pty_info
) {
2482 SYSERROR("failed to allocate pty_info");
2486 for (i
= 0; i
< conf
->tty
; i
++) {
2488 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2490 if (openpty(&pty_info
->master
, &pty_info
->slave
,
2491 pty_info
->name
, NULL
, NULL
)) {
2492 SYSERROR("failed to create pty #%d", i
);
2493 tty_info
->nbtty
= i
;
2494 lxc_delete_tty(tty_info
);
2498 DEBUG("allocated pty '%s' (%d/%d)",
2499 pty_info
->name
, pty_info
->master
, pty_info
->slave
);
2501 /* Prevent leaking the file descriptors to the container */
2502 fcntl(pty_info
->master
, F_SETFD
, FD_CLOEXEC
);
2503 fcntl(pty_info
->slave
, F_SETFD
, FD_CLOEXEC
);
2508 tty_info
->nbtty
= conf
->tty
;
2510 INFO("tty's configured");
2515 void lxc_delete_tty(struct lxc_tty_info
*tty_info
)
2519 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
2520 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2522 close(pty_info
->master
);
2523 close(pty_info
->slave
);
2526 free(tty_info
->pty_info
);
2527 tty_info
->nbtty
= 0;
2530 int lxc_setup(const char *name
, struct lxc_conf
*lxc_conf
)
2532 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2536 if (setup_utsname(lxc_conf
->utsname
)) {
2537 ERROR("failed to setup the utsname for '%s'", name
);
2541 if (setup_network(&lxc_conf
->network
)) {
2542 ERROR("failed to setup the network for '%s'", name
);
2546 if (run_lxc_hooks(name
, "pre-mount", lxc_conf
)) {
2547 ERROR("failed to run pre-mount hooks for container '%s'.", name
);
2551 if (setup_rootfs(lxc_conf
)) {
2552 ERROR("failed to setup rootfs for '%s'", name
);
2556 if (lxc_conf
->autodev
) {
2557 if (mount_autodev(lxc_conf
->rootfs
.mount
)) {
2558 ERROR("failed to mount /dev in the container");
2563 if (setup_mount(&lxc_conf
->rootfs
, lxc_conf
->fstab
, name
)) {
2564 ERROR("failed to setup the mounts for '%s'", name
);
2568 if (setup_mount_entries(&lxc_conf
->rootfs
, &lxc_conf
->mount_list
, name
)) {
2569 ERROR("failed to setup the mount entries for '%s'", name
);
2573 if (run_lxc_hooks(name
, "mount", lxc_conf
)) {
2574 ERROR("failed to run mount hooks for container '%s'.", name
);
2578 if (lxc_conf
->autodev
) {
2579 if (setup_autodev(lxc_conf
->rootfs
.mount
)) {
2580 ERROR("failed to populate /dev in the container");
2585 if (setup_cgroup(name
, &lxc_conf
->cgroup
)) {
2586 ERROR("failed to setup the cgroups for '%s'", name
);
2590 if (setup_console(&lxc_conf
->rootfs
, &lxc_conf
->console
, lxc_conf
->ttydir
)) {
2591 ERROR("failed to setup the console for '%s'", name
);
2595 if (setup_kmsg(&lxc_conf
->rootfs
, &lxc_conf
->console
)) // don't fail
2596 ERROR("failed to setup kmsg for '%s'", name
);
2598 if (setup_tty(&lxc_conf
->rootfs
, &lxc_conf
->tty_info
, lxc_conf
->ttydir
)) {
2599 ERROR("failed to setup the ttys for '%s'", name
);
2603 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2604 INFO("rootfs path is .%s., mount is .%s.", lxc_conf
->rootfs
.path
,
2605 lxc_conf
->rootfs
.mount
);
2606 if (lxc_conf
->rootfs
.path
== NULL
|| strlen(lxc_conf
->rootfs
.path
) == 0)
2609 mounted
= lsm_mount_proc_if_needed(lxc_conf
->rootfs
.path
, lxc_conf
->rootfs
.mount
);
2610 if (mounted
== -1) {
2611 SYSERROR("failed to mount /proc in the container.");
2613 } else if (mounted
== 1) {
2614 lxc_conf
->lsm_umount_proc
= 1;
2618 if (setup_pivot_root(&lxc_conf
->rootfs
)) {
2619 ERROR("failed to set rootfs for '%s'", name
);
2623 if (setup_pts(lxc_conf
->pts
)) {
2624 ERROR("failed to setup the new pts instance");
2628 if (setup_personality(lxc_conf
->personality
)) {
2629 ERROR("failed to setup personality");
2633 if (setup_caps(&lxc_conf
->caps
)) {
2634 ERROR("failed to drop capabilities");
2638 NOTICE("'%s' is setup.", name
);
2643 int run_lxc_hooks(const char *name
, char *hook
, struct lxc_conf
*conf
)
2646 struct lxc_list
*it
;
2648 if (strcmp(hook
, "pre-start") == 0)
2649 which
= LXCHOOK_PRESTART
;
2650 else if (strcmp(hook
, "pre-mount") == 0)
2651 which
= LXCHOOK_PREMOUNT
;
2652 else if (strcmp(hook
, "mount") == 0)
2653 which
= LXCHOOK_MOUNT
;
2654 else if (strcmp(hook
, "start") == 0)
2655 which
= LXCHOOK_START
;
2656 else if (strcmp(hook
, "post-stop") == 0)
2657 which
= LXCHOOK_POSTSTOP
;
2660 lxc_list_for_each(it
, &conf
->hooks
[which
]) {
2662 char *hookname
= it
->elem
;
2663 ret
= run_script(name
, "lxc", hookname
, hook
, NULL
);
2670 static void lxc_remove_nic(struct lxc_list
*it
)
2672 struct lxc_netdev
*netdev
= it
->elem
;
2673 struct lxc_list
*it2
,*next
;
2681 if (netdev
->upscript
)
2682 free(netdev
->upscript
);
2684 free(netdev
->hwaddr
);
2687 if (netdev
->ipv4_gateway
)
2688 free(netdev
->ipv4_gateway
);
2689 if (netdev
->ipv6_gateway
)
2690 free(netdev
->ipv6_gateway
);
2691 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2696 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2705 /* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
2706 int lxc_clear_nic(struct lxc_conf
*c
, const char *key
)
2710 struct lxc_list
*it
;
2711 struct lxc_netdev
*netdev
;
2713 p1
= index(key
, '.');
2714 if (!p1
|| *(p1
+1) == '\0')
2717 ret
= sscanf(key
, "%d", &idx
);
2718 if (ret
!= 1) return -1;
2723 lxc_list_for_each(it
, &c
->network
) {
2728 if (i
< idx
) // we don't have that many nics defined
2731 if (!it
|| !it
->elem
)
2738 } else if (strcmp(p1
, "ipv4") == 0) {
2739 struct lxc_list
*it2
,*next
;
2740 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2745 } else if (strcmp(p1
, "ipv6") == 0) {
2746 struct lxc_list
*it2
,*next
;
2747 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2752 } else if (strcmp(p1
, "link") == 0) {
2755 netdev
->link
= NULL
;
2757 } else if (strcmp(p1
, "name") == 0) {
2760 netdev
->name
= NULL
;
2762 } else if (strcmp(p1
, "script.up") == 0) {
2763 if (netdev
->upscript
) {
2764 free(netdev
->upscript
);
2765 netdev
->upscript
= NULL
;
2767 } else if (strcmp(p1
, "hwaddr") == 0) {
2768 if (netdev
->hwaddr
) {
2769 free(netdev
->hwaddr
);
2770 netdev
->hwaddr
= NULL
;
2772 } else if (strcmp(p1
, "mtu") == 0) {
2777 } else if (strcmp(p1
, "ipv4_gateway") == 0) {
2778 if (netdev
->ipv4_gateway
) {
2779 free(netdev
->ipv4_gateway
);
2780 netdev
->ipv4_gateway
= NULL
;
2782 } else if (strcmp(p1
, "ipv6_gateway") == 0) {
2783 if (netdev
->ipv6_gateway
) {
2784 free(netdev
->ipv6_gateway
);
2785 netdev
->ipv6_gateway
= NULL
;
2793 int lxc_clear_config_network(struct lxc_conf
*c
)
2795 struct lxc_list
*it
,*next
;
2796 lxc_list_for_each_safe(it
, &c
->network
, next
) {
2802 int lxc_clear_config_caps(struct lxc_conf
*c
)
2804 struct lxc_list
*it
,*next
;
2806 lxc_list_for_each_safe(it
, &c
->caps
, next
) {
2814 int lxc_clear_cgroups(struct lxc_conf
*c
, const char *key
)
2816 struct lxc_list
*it
,*next
;
2818 const char *k
= key
+ 11;
2820 if (strcmp(key
, "lxc.cgroup") == 0)
2823 lxc_list_for_each_safe(it
, &c
->cgroup
, next
) {
2824 struct lxc_cgroup
*cg
= it
->elem
;
2825 if (!all
&& strcmp(cg
->subsystem
, k
) != 0)
2828 free(cg
->subsystem
);
2836 int lxc_clear_mount_entries(struct lxc_conf
*c
)
2838 struct lxc_list
*it
,*next
;
2840 lxc_list_for_each_safe(it
, &c
->mount_list
, next
) {
2848 int lxc_clear_hooks(struct lxc_conf
*c
, const char *key
)
2850 struct lxc_list
*it
,*next
;
2851 bool all
= false, done
= false;
2852 const char *k
= key
+ 9;
2855 if (strcmp(key
, "lxc.hook") == 0)
2858 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
2859 if (all
|| strcmp(k
, lxchook_names
[i
]) == 0) {
2860 lxc_list_for_each_safe(it
, &c
->hooks
[i
], next
) {
2870 ERROR("Invalid hook key: %s", key
);
2876 void lxc_clear_saved_nics(struct lxc_conf
*conf
)
2880 if (!conf
->num_savednics
)
2882 for (i
=0; i
< conf
->num_savednics
; i
++)
2883 free(conf
->saved_nics
[i
].orig_name
);
2884 conf
->saved_nics
= 0;
2885 free(conf
->saved_nics
);
2888 void lxc_conf_free(struct lxc_conf
*conf
)
2892 if (conf
->console
.path
)
2893 free(conf
->console
.path
);
2894 if (conf
->rootfs
.mount
!= default_rootfs_mount
)
2895 free(conf
->rootfs
.mount
);
2896 if (conf
->rootfs
.path
)
2897 free(conf
->rootfs
.path
);
2899 free(conf
->utsname
);
2905 free(conf
->logfile
);
2906 lxc_clear_config_network(conf
);
2908 if (conf
->aa_profile
)
2909 free(conf
->aa_profile
);
2911 lxc_seccomp_free(conf
);
2912 lxc_clear_config_caps(conf
);
2913 lxc_clear_cgroups(conf
, "lxc.cgroup");
2914 lxc_clear_hooks(conf
, "lxc.hook");
2915 lxc_clear_mount_entries(conf
);
2916 lxc_clear_saved_nics(conf
);