2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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
33 #include <sys/syscall.h>
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() */
73 #if HAVE_SYS_CAPABILITY_H
74 #include <sys/capability.h>
77 #if HAVE_SYS_PERSONALITY_H
78 #include <sys/personality.h>
82 #include <../include/lxcmntent.h>
87 #include "lxcseccomp.h"
89 lxc_log_define(lxc_conf
, lxc
);
92 #define MAXINDEXLEN 20
94 #define MAXLINELEN 128
97 #define MS_DIRSYNC 128
109 #define MS_SLAVE (1<<19)
113 #define MS_RELATIME (1 << 21)
116 #ifndef MS_STRICTATIME
117 #define MS_STRICTATIME (1 << 24)
120 #if HAVE_SYS_CAPABILITY_H
122 #define CAP_SETFCAP 31
125 #ifndef CAP_MAC_OVERRIDE
126 #define CAP_MAC_OVERRIDE 32
129 #ifndef CAP_MAC_ADMIN
130 #define CAP_MAC_ADMIN 33
134 #ifndef PR_CAPBSET_DROP
135 #define PR_CAPBSET_DROP 24
138 #ifndef LO_FLAGS_AUTOCLEAR
139 #define LO_FLAGS_AUTOCLEAR 4
142 /* Define pivot_root() if missing from the C library */
143 #ifndef HAVE_PIVOT_ROOT
144 static int pivot_root(const char * new_root
, const char * put_old
)
146 #ifdef __NR_pivot_root
147 return syscall(__NR_pivot_root
, new_root
, put_old
);
154 extern int pivot_root(const char * new_root
, const char * put_old
);
157 /* Define sethostname() if missing from the C library */
158 #ifndef HAVE_SETHOSTNAME
159 static int sethostname(const char * name
, size_t len
)
161 #ifdef __NR_sethostname
162 return syscall(__NR_sethostname
, name
, len
);
170 /* Define __S_ISTYPE if missing from the C library */
172 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
175 char *lxchook_names
[NUM_LXC_HOOKS
] = {
176 "pre-start", "pre-mount", "mount", "autodev", "start", "post-stop" };
178 typedef int (*instanciate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
191 static int instanciate_veth(struct lxc_handler
*, struct lxc_netdev
*);
192 static int instanciate_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
193 static int instanciate_vlan(struct lxc_handler
*, struct lxc_netdev
*);
194 static int instanciate_phys(struct lxc_handler
*, struct lxc_netdev
*);
195 static int instanciate_empty(struct lxc_handler
*, struct lxc_netdev
*);
197 static instanciate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
198 [LXC_NET_VETH
] = instanciate_veth
,
199 [LXC_NET_MACVLAN
] = instanciate_macvlan
,
200 [LXC_NET_VLAN
] = instanciate_vlan
,
201 [LXC_NET_PHYS
] = instanciate_phys
,
202 [LXC_NET_EMPTY
] = instanciate_empty
,
205 static int shutdown_veth(struct lxc_handler
*, struct lxc_netdev
*);
206 static int shutdown_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
207 static int shutdown_vlan(struct lxc_handler
*, struct lxc_netdev
*);
208 static int shutdown_phys(struct lxc_handler
*, struct lxc_netdev
*);
209 static int shutdown_empty(struct lxc_handler
*, struct lxc_netdev
*);
211 static instanciate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
212 [LXC_NET_VETH
] = shutdown_veth
,
213 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
214 [LXC_NET_VLAN
] = shutdown_vlan
,
215 [LXC_NET_PHYS
] = shutdown_phys
,
216 [LXC_NET_EMPTY
] = shutdown_empty
,
219 static struct mount_opt mount_opt
[] = {
220 { "defaults", 0, 0 },
221 { "ro", 0, MS_RDONLY
},
222 { "rw", 1, MS_RDONLY
},
223 { "suid", 1, MS_NOSUID
},
224 { "nosuid", 0, MS_NOSUID
},
225 { "dev", 1, MS_NODEV
},
226 { "nodev", 0, MS_NODEV
},
227 { "exec", 1, MS_NOEXEC
},
228 { "noexec", 0, MS_NOEXEC
},
229 { "sync", 0, MS_SYNCHRONOUS
},
230 { "async", 1, MS_SYNCHRONOUS
},
231 { "dirsync", 0, MS_DIRSYNC
},
232 { "remount", 0, MS_REMOUNT
},
233 { "mand", 0, MS_MANDLOCK
},
234 { "nomand", 1, MS_MANDLOCK
},
235 { "atime", 1, MS_NOATIME
},
236 { "noatime", 0, MS_NOATIME
},
237 { "diratime", 1, MS_NODIRATIME
},
238 { "nodiratime", 0, MS_NODIRATIME
},
239 { "bind", 0, MS_BIND
},
240 { "rbind", 0, MS_BIND
|MS_REC
},
241 { "relatime", 0, MS_RELATIME
},
242 { "norelatime", 1, MS_RELATIME
},
243 { "strictatime", 0, MS_STRICTATIME
},
244 { "nostrictatime", 1, MS_STRICTATIME
},
248 #if HAVE_SYS_CAPABILITY_H
249 static struct caps_opt caps_opt
[] = {
250 { "chown", CAP_CHOWN
},
251 { "dac_override", CAP_DAC_OVERRIDE
},
252 { "dac_read_search", CAP_DAC_READ_SEARCH
},
253 { "fowner", CAP_FOWNER
},
254 { "fsetid", CAP_FSETID
},
255 { "kill", CAP_KILL
},
256 { "setgid", CAP_SETGID
},
257 { "setuid", CAP_SETUID
},
258 { "setpcap", CAP_SETPCAP
},
259 { "linux_immutable", CAP_LINUX_IMMUTABLE
},
260 { "net_bind_service", CAP_NET_BIND_SERVICE
},
261 { "net_broadcast", CAP_NET_BROADCAST
},
262 { "net_admin", CAP_NET_ADMIN
},
263 { "net_raw", CAP_NET_RAW
},
264 { "ipc_lock", CAP_IPC_LOCK
},
265 { "ipc_owner", CAP_IPC_OWNER
},
266 { "sys_module", CAP_SYS_MODULE
},
267 { "sys_rawio", CAP_SYS_RAWIO
},
268 { "sys_chroot", CAP_SYS_CHROOT
},
269 { "sys_ptrace", CAP_SYS_PTRACE
},
270 { "sys_pacct", CAP_SYS_PACCT
},
271 { "sys_admin", CAP_SYS_ADMIN
},
272 { "sys_boot", CAP_SYS_BOOT
},
273 { "sys_nice", CAP_SYS_NICE
},
274 { "sys_resource", CAP_SYS_RESOURCE
},
275 { "sys_time", CAP_SYS_TIME
},
276 { "sys_tty_config", CAP_SYS_TTY_CONFIG
},
277 { "mknod", CAP_MKNOD
},
278 { "lease", CAP_LEASE
},
279 #ifdef CAP_AUDIT_WRITE
280 { "audit_write", CAP_AUDIT_WRITE
},
282 #ifdef CAP_AUDIT_CONTROL
283 { "audit_control", CAP_AUDIT_CONTROL
},
285 { "setfcap", CAP_SETFCAP
},
286 { "mac_override", CAP_MAC_OVERRIDE
},
287 { "mac_admin", CAP_MAC_ADMIN
},
289 { "syslog", CAP_SYSLOG
},
291 #ifdef CAP_WAKE_ALARM
292 { "wake_alarm", CAP_WAKE_ALARM
},
296 static struct caps_opt caps_opt
[] = {};
299 static int run_buffer(char *buffer
)
305 f
= popen(buffer
, "r");
307 SYSERROR("popen failed");
311 output
= malloc(LXC_LOG_BUFFER_SIZE
);
313 ERROR("failed to allocate memory for script output");
318 while(fgets(output
, LXC_LOG_BUFFER_SIZE
, f
))
319 DEBUG("script output: %s", output
);
325 SYSERROR("Script exited on error");
327 } else if (WIFEXITED(ret
) && WEXITSTATUS(ret
) != 0) {
328 ERROR("Script exited with status %d", WEXITSTATUS(ret
));
330 } else if (WIFSIGNALED(ret
)) {
331 ERROR("Script terminated by signal %d (%s)", WTERMSIG(ret
),
332 strsignal(WTERMSIG(ret
)));
339 static int run_script(const char *name
, const char *section
,
340 const char *script
, ...)
347 INFO("Executing script '%s' for container '%s', config section '%s'",
348 script
, name
, section
);
350 va_start(ap
, script
);
351 while ((p
= va_arg(ap
, char *)))
352 size
+= strlen(p
) + 1;
355 size
+= strlen(script
);
356 size
+= strlen(name
);
357 size
+= strlen(section
);
363 buffer
= alloca(size
);
365 ERROR("failed to allocate memory");
369 ret
= snprintf(buffer
, size
, "%s %s %s", script
, name
, section
);
370 if (ret
< 0 || ret
>= size
) {
371 ERROR("Script name too long");
375 va_start(ap
, script
);
376 while ((p
= va_arg(ap
, char *))) {
379 rc
= snprintf(buffer
+ ret
, len
, " %s", p
);
380 if (rc
< 0 || rc
>= len
) {
381 ERROR("Script args too long");
388 return run_buffer(buffer
);
391 static int find_fstype_cb(char* buffer
, void *data
)
401 /* we don't try 'nodev' entries */
402 if (strstr(buffer
, "nodev"))
406 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
407 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
409 DEBUG("trying to mount '%s'->'%s' with fstype '%s'",
410 cbarg
->rootfs
, cbarg
->target
, fstype
);
412 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, cbarg
->mntopt
, NULL
)) {
413 DEBUG("mount failed with error: %s", strerror(errno
));
417 INFO("mounted '%s' on '%s', with fstype '%s'",
418 cbarg
->rootfs
, cbarg
->target
, fstype
);
423 static int mount_unknow_fs(const char *rootfs
, const char *target
, int mntopt
)
438 * find the filesystem type with brute force:
439 * first we check with /etc/filesystems, in case the modules
440 * are auto-loaded and fall back to the supported kernel fs
447 for (i
= 0; i
< sizeof(fsfile
)/sizeof(fsfile
[0]); i
++) {
451 if (access(fsfile
[i
], F_OK
))
454 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
456 ERROR("failed to parse '%s'", fsfile
[i
]);
464 ERROR("failed to determine fs type for '%s'", rootfs
);
468 static int mount_rootfs_dir(const char *rootfs
, const char *target
)
470 return mount(rootfs
, target
, "none", MS_BIND
| MS_REC
, NULL
);
473 static int setup_lodev(const char *rootfs
, int fd
, struct loop_info64
*loinfo
)
478 rfd
= open(rootfs
, O_RDWR
);
480 SYSERROR("failed to open '%s'", rootfs
);
484 memset(loinfo
, 0, sizeof(*loinfo
));
486 loinfo
->lo_flags
= LO_FLAGS_AUTOCLEAR
;
488 if (ioctl(fd
, LOOP_SET_FD
, rfd
)) {
489 SYSERROR("failed to LOOP_SET_FD");
493 if (ioctl(fd
, LOOP_SET_STATUS64
, loinfo
)) {
494 SYSERROR("failed to LOOP_SET_STATUS64");
505 static int mount_rootfs_file(const char *rootfs
, const char *target
)
507 struct dirent dirent
, *direntp
;
508 struct loop_info64 loinfo
;
509 int ret
= -1, fd
= -1, rc
;
511 char path
[MAXPATHLEN
];
513 dir
= opendir("/dev");
515 SYSERROR("failed to open '/dev'");
519 while (!readdir_r(dir
, &dirent
, &direntp
)) {
524 if (!strcmp(direntp
->d_name
, "."))
527 if (!strcmp(direntp
->d_name
, ".."))
530 if (strncmp(direntp
->d_name
, "loop", 4))
533 rc
= snprintf(path
, MAXPATHLEN
, "/dev/%s", direntp
->d_name
);
534 if (rc
< 0 || rc
>= MAXPATHLEN
)
537 fd
= open(path
, O_RDWR
);
541 if (ioctl(fd
, LOOP_GET_STATUS64
, &loinfo
) == 0) {
546 if (errno
!= ENXIO
) {
547 WARN("unexpected error for ioctl on '%s': %m",
553 DEBUG("found '%s' free lodev", path
);
555 ret
= setup_lodev(rootfs
, fd
, &loinfo
);
557 ret
= mount_unknow_fs(path
, target
, 0);
564 WARN("failed to close directory");
569 static int mount_rootfs_block(const char *rootfs
, const char *target
)
571 return mount_unknow_fs(rootfs
, target
, 0);
576 * if rootfs is a directory, then open ${rootfs}.hold for writing for the
577 * duration of the container run, to prevent the container from marking the
578 * underlying fs readonly on shutdown.
579 * return -1 on error.
580 * return -2 if nothing needed to be pinned.
581 * return an open fd (>=0) if we pinned it.
583 int pin_rootfs(const char *rootfs
)
585 char absrootfs
[MAXPATHLEN
];
586 char absrootfspin
[MAXPATHLEN
];
590 if (rootfs
== NULL
|| strlen(rootfs
) == 0)
593 if (!realpath(rootfs
, absrootfs
)) {
594 INFO("failed to get real path for '%s', not pinning", rootfs
);
598 if (access(absrootfs
, F_OK
)) {
599 SYSERROR("'%s' is not accessible", absrootfs
);
603 if (stat(absrootfs
, &s
)) {
604 SYSERROR("failed to stat '%s'", absrootfs
);
608 if (!S_ISDIR(s
.st_mode
))
611 ret
= snprintf(absrootfspin
, MAXPATHLEN
, "%s%s", absrootfs
, ".hold");
612 if (ret
>= MAXPATHLEN
) {
613 SYSERROR("pathname too long for rootfs hold file");
617 fd
= open(absrootfspin
, O_CREAT
| O_RDWR
, S_IWUSR
|S_IRUSR
);
618 INFO("opened %s as fd %d\n", absrootfspin
, fd
);
622 static int mount_rootfs(const char *rootfs
, const char *target
)
624 char absrootfs
[MAXPATHLEN
];
628 typedef int (*rootfs_cb
)(const char *, const char *);
634 { S_IFDIR
, mount_rootfs_dir
},
635 { S_IFBLK
, mount_rootfs_block
},
636 { S_IFREG
, mount_rootfs_file
},
639 if (!realpath(rootfs
, absrootfs
)) {
640 SYSERROR("failed to get real path for '%s'", rootfs
);
644 if (access(absrootfs
, F_OK
)) {
645 SYSERROR("'%s' is not accessible", absrootfs
);
649 if (stat(absrootfs
, &s
)) {
650 SYSERROR("failed to stat '%s'", absrootfs
);
654 for (i
= 0; i
< sizeof(rtfs_type
)/sizeof(rtfs_type
[0]); i
++) {
656 if (!__S_ISTYPE(s
.st_mode
, rtfs_type
[i
].type
))
659 return rtfs_type
[i
].cb(absrootfs
, target
);
662 ERROR("unsupported rootfs type for '%s'", absrootfs
);
666 static int setup_utsname(struct utsname
*utsname
)
671 if (sethostname(utsname
->nodename
, strlen(utsname
->nodename
))) {
672 SYSERROR("failed to set the hostname to '%s'", utsname
->nodename
);
676 INFO("'%s' hostname has been setup", utsname
->nodename
);
681 static int setup_tty(const struct lxc_rootfs
*rootfs
,
682 const struct lxc_tty_info
*tty_info
, char *ttydir
)
684 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
690 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
692 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
694 ret
= snprintf(path
, sizeof(path
), "%s/dev/tty%d",
695 rootfs
->mount
, i
+ 1);
696 if (ret
>= sizeof(path
)) {
697 ERROR("pathname too long for ttys");
701 /* create dev/lxc/tty%d" */
702 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/tty%d",
703 rootfs
->mount
, ttydir
, i
+ 1);
704 if (ret
>= sizeof(lxcpath
)) {
705 ERROR("pathname too long for ttys");
708 ret
= creat(lxcpath
, 0660);
709 if (ret
==-1 && errno
!= EEXIST
) {
710 SYSERROR("error creating %s\n", lxcpath
);
716 if (ret
&& errno
!= ENOENT
) {
717 SYSERROR("error unlinking %s\n", path
);
721 if (mount(pty_info
->name
, lxcpath
, "none", MS_BIND
, 0)) {
722 WARN("failed to mount '%s'->'%s'",
723 pty_info
->name
, path
);
727 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/tty%d", ttydir
, i
+1);
728 if (ret
>= sizeof(lxcpath
)) {
729 ERROR("tty pathname too long");
732 ret
= symlink(lxcpath
, path
);
734 SYSERROR("failed to create symlink for tty %d\n", i
+1);
738 /* If we populated /dev, then we need to create /dev/ttyN */
739 if (access(path
, F_OK
)) {
740 ret
= creat(path
, 0660);
742 SYSERROR("error creating %s\n", path
);
743 /* this isn't fatal, continue */
747 if (mount(pty_info
->name
, path
, "none", MS_BIND
, 0)) {
748 WARN("failed to mount '%s'->'%s'",
749 pty_info
->name
, path
);
755 INFO("%d tty(s) has been setup", tty_info
->nbtty
);
760 static int setup_rootfs_pivot_root_cb(char *buffer
, void *data
)
762 struct lxc_list
*mountlist
, *listentry
, *iterator
;
763 char *pivotdir
, *mountpoint
, *mountentry
, *saveptr
= NULL
;
768 cbparm
= (void **)data
;
770 mountlist
= cbparm
[0];
771 pivotdir
= cbparm
[1];
773 /* parse entry, first field is mountname, ignore */
774 mountpoint
= strtok_r(mountentry
, " ", &saveptr
);
778 /* second field is mountpoint */
779 mountpoint
= strtok_r(NULL
, " ", &saveptr
);
783 /* only consider mountpoints below old root fs */
784 if (strncmp(mountpoint
, pivotdir
, strlen(pivotdir
)))
787 /* filter duplicate mountpoints */
789 lxc_list_for_each(iterator
, mountlist
) {
790 if (!strcmp(iterator
->elem
, mountpoint
)) {
798 /* add entry to list */
799 listentry
= malloc(sizeof(*listentry
));
801 SYSERROR("malloc for mountpoint listentry failed");
805 listentry
->elem
= strdup(mountpoint
);
806 if (!listentry
->elem
) {
807 SYSERROR("strdup failed");
811 lxc_list_add_tail(mountlist
, listentry
);
816 static int umount_oldrootfs(const char *oldrootfs
)
818 char path
[MAXPATHLEN
];
820 struct lxc_list mountlist
, *iterator
, *next
;
821 int ok
, still_mounted
, last_still_mounted
;
824 /* read and parse /proc/mounts in old root fs */
825 lxc_list_init(&mountlist
);
827 /* oldrootfs is on the top tree directory now */
828 rc
= snprintf(path
, sizeof(path
), "/%s", oldrootfs
);
829 if (rc
>= sizeof(path
)) {
830 ERROR("rootfs name too long");
833 cbparm
[0] = &mountlist
;
835 cbparm
[1] = strdup(path
);
837 SYSERROR("strdup failed");
841 rc
= snprintf(path
, sizeof(path
), "%s/proc/mounts", oldrootfs
);
842 if (rc
>= sizeof(path
)) {
843 ERROR("container proc/mounts name too long");
847 ok
= lxc_file_for_each_line(path
,
848 setup_rootfs_pivot_root_cb
, &cbparm
);
850 SYSERROR("failed to read or parse mount list '%s'", path
);
854 /* umount filesystems until none left or list no longer shrinks */
857 last_still_mounted
= still_mounted
;
860 lxc_list_for_each_safe(iterator
, &mountlist
, next
) {
862 /* umount normally */
863 if (!umount(iterator
->elem
)) {
864 DEBUG("umounted '%s'", (char *)iterator
->elem
);
865 lxc_list_del(iterator
);
872 } while (still_mounted
> 0 && still_mounted
!= last_still_mounted
);
875 lxc_list_for_each(iterator
, &mountlist
) {
877 /* let's try a lazy umount */
878 if (!umount2(iterator
->elem
, MNT_DETACH
)) {
879 INFO("lazy unmount of '%s'", (char *)iterator
->elem
);
883 /* be more brutal (nfs) */
884 if (!umount2(iterator
->elem
, MNT_FORCE
)) {
885 INFO("forced unmount of '%s'", (char *)iterator
->elem
);
889 WARN("failed to unmount '%s'", (char *)iterator
->elem
);
895 static int setup_rootfs_pivot_root(const char *rootfs
, const char *pivotdir
)
897 char path
[MAXPATHLEN
];
898 int remove_pivotdir
= 0;
901 /* change into new root fs */
903 SYSERROR("can't chdir to new rootfs '%s'", rootfs
);
908 pivotdir
= "lxc_putold";
910 /* compute the full path to pivotdir under rootfs */
911 rc
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, pivotdir
);
912 if (rc
>= sizeof(path
)) {
913 ERROR("pivot dir name too long");
917 if (access(path
, F_OK
)) {
919 if (mkdir_p(path
, 0755)) {
920 SYSERROR("failed to create pivotdir '%s'", path
);
925 DEBUG("created '%s' directory", path
);
928 DEBUG("mountpoint for old rootfs is '%s'", path
);
930 /* pivot_root into our new root fs */
931 if (pivot_root(".", path
)) {
932 SYSERROR("pivot_root syscall failed");
937 SYSERROR("can't chdir to / after pivot_root");
941 DEBUG("pivot_root syscall to '%s' successful", rootfs
);
943 /* we switch from absolute path to relative path */
944 if (umount_oldrootfs(pivotdir
))
947 /* remove temporary mount point, we don't consider the removing
949 if (remove_pivotdir
&& rmdir(pivotdir
))
950 WARN("can't remove mountpoint '%s': %m", pivotdir
);
956 * Do we want to add options for max size of /dev and a file to
957 * specify which devices to create?
959 static int mount_autodev(char *root
)
962 char path
[MAXPATHLEN
];
964 INFO("Mounting /dev under %s\n", root
);
965 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
966 if (ret
< 0 || ret
> MAXPATHLEN
)
968 ret
= mount("none", path
, "tmpfs", 0, "size=100000");
970 SYSERROR("Failed to mount /dev at %s\n", root
);
973 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/pts", root
);
974 if (ret
< 0 || ret
>= MAXPATHLEN
)
976 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
978 SYSERROR("Failed to create /dev/pts in container");
982 INFO("Mounted /dev under %s\n", root
);
993 struct lxc_devs lxc_devs
[] = {
994 { "null", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 3 },
995 { "zero", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 5 },
996 { "full", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 7 },
997 { "urandom", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 9 },
998 { "random", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 8 },
999 { "tty", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 5, 0 },
1000 { "console", S_IFCHR
| S_IRUSR
| S_IWUSR
, 5, 1 },
1003 static int setup_autodev(char *root
)
1007 char path
[MAXPATHLEN
];
1011 INFO("Creating initial consoles under %s/dev\n", root
);
1013 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
1014 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1015 ERROR("Error calculating container /dev location");
1019 INFO("Populating /dev under %s\n", root
);
1020 cmask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1021 for (i
= 0; i
< sizeof(lxc_devs
) / sizeof(lxc_devs
[0]); i
++) {
1023 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/%s", root
, d
->name
);
1024 if (ret
< 0 || ret
>= MAXPATHLEN
)
1026 ret
= mknod(path
, d
->mode
, makedev(d
->maj
, d
->min
));
1027 if (ret
&& errno
!= EEXIST
) {
1028 SYSERROR("Error creating %s\n", d
->name
);
1034 INFO("Populated /dev under %s\n", root
);
1039 * Detect whether / is mounted MS_SHARED. The only way I know of to
1040 * check that is through /proc/self/mountinfo.
1041 * I'm only checking for /. If the container rootfs or mount location
1042 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1043 * out would be too much work to be worth it.
1045 #define LINELEN 4096
1046 int detect_shared_rootfs(void)
1048 char buf
[LINELEN
], *p
;
1053 f
= fopen("/proc/self/mountinfo", "r");
1056 while ((p
= fgets(buf
, LINELEN
, f
))) {
1057 INFO("looking at .%s.", p
);
1058 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1059 p
= index(p
+1, ' ');
1062 p2
= index(p
+1, ' ');
1066 INFO("now p is .%s.", p
);
1067 if (strcmp(p
+1, "/") == 0) {
1068 // this is '/'. is it shared?
1069 p
= index(p2
+1, ' ');
1070 if (p
&& strstr(p
, "shared:")) {
1081 * I'll forgive you for asking whether all of this is needed :) The
1083 * pivot_root will fail if the new root, the put_old dir, or the parent
1084 * of current->fs->root are MS_SHARED. (parent of current->fs_root may
1085 * or may not be current->fs_root - if we assumed it always was, we could
1086 * just mount --make-rslave /). So,
1087 * 1. mount a tiny tmpfs to be parent of current->fs->root.
1088 * 2. make that MS_SLAVE
1089 * 3. make a 'root' directory under that
1090 * 4. mount --rbind / under the $tinyroot/root.
1091 * 5. make that rslave
1092 * 6. chdir and chroot into $tinyroot/root
1093 * 7. $tinyroot will be unmounted by our parent in start.c
1095 static int chroot_into_slave(struct lxc_conf
*conf
)
1097 char path
[MAXPATHLEN
];
1098 const char *destpath
= conf
->rootfs
.mount
;
1101 if (mount(destpath
, destpath
, NULL
, MS_BIND
, 0)) {
1102 SYSERROR("failed to mount %s bind", destpath
);
1105 if (mount("", destpath
, NULL
, MS_SLAVE
, 0)) {
1106 SYSERROR("failed to make %s slave", destpath
);
1109 if (mount("none", destpath
, "tmpfs", 0, "size=10000")) {
1110 SYSERROR("Failed to mount tmpfs / at %s", destpath
);
1113 ret
= snprintf(path
, MAXPATHLEN
, "%s/root", destpath
);
1114 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1115 ERROR("out of memory making root path");
1118 if (mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)) {
1119 SYSERROR("Failed to create /dev/pts in container");
1122 if (mount("/", path
, NULL
, MS_BIND
|MS_REC
, 0)) {
1123 SYSERROR("Failed to rbind mount / to %s", path
);
1126 if (mount("", destpath
, NULL
, MS_SLAVE
|MS_REC
, 0)) {
1127 SYSERROR("Failed to make tmp-/ at %s rslave", path
);
1131 SYSERROR("Failed to chdir into tmp-/");
1135 SYSERROR("Failed to chroot into tmp-/");
1138 INFO("Chrooted into tmp-/ at %s\n", path
);
1142 static int setup_rootfs(struct lxc_conf
*conf
)
1144 const struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1146 if (!rootfs
->path
) {
1147 if (mount("", "/", NULL
, MS_SLAVE
|MS_REC
, 0)) {
1148 SYSERROR("Failed to make / rslave");
1154 if (access(rootfs
->mount
, F_OK
)) {
1155 SYSERROR("failed to access to '%s', check it is present",
1160 if (detect_shared_rootfs()) {
1161 if (chroot_into_slave(conf
)) {
1162 ERROR("Failed to chroot into slave /");
1167 // First try mounting rootfs using a bdev
1168 struct bdev
*bdev
= bdev_init(rootfs
->path
, rootfs
->mount
, NULL
);
1169 if (bdev
&& bdev
->ops
->mount(bdev
) == 0) {
1170 DEBUG("mounted '%s' on '%s'", rootfs
->path
, rootfs
->mount
);
1173 if (mount_rootfs(rootfs
->path
, rootfs
->mount
)) {
1174 ERROR("failed to mount rootfs");
1178 DEBUG("mounted '%s' on '%s'", rootfs
->path
, rootfs
->mount
);
1183 int setup_pivot_root(const struct lxc_rootfs
*rootfs
)
1188 if (setup_rootfs_pivot_root(rootfs
->mount
, rootfs
->pivot
)) {
1189 ERROR("failed to setup pivot root");
1196 static int setup_pts(int pts
)
1198 char target
[PATH_MAX
];
1203 if (!access("/dev/pts/ptmx", F_OK
) && umount("/dev/pts")) {
1204 SYSERROR("failed to umount 'dev/pts'");
1208 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
,
1209 "newinstance,ptmxmode=0666")) {
1210 SYSERROR("failed to mount a new instance of '/dev/pts'");
1214 if (access("/dev/ptmx", F_OK
)) {
1215 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
1217 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
1221 if (realpath("/dev/ptmx", target
) && !strcmp(target
, "/dev/pts/ptmx"))
1224 /* fallback here, /dev/pts/ptmx exists just mount bind */
1225 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND
, 0)) {
1226 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
1230 INFO("created new pts instance");
1236 static int setup_personality(int persona
)
1238 #if HAVE_SYS_PERSONALITY_H
1242 if (personality(persona
) < 0) {
1243 SYSERROR("failed to set personality to '0x%x'", persona
);
1247 INFO("set personality to '0x%x'", persona
);
1253 static int setup_dev_console(const struct lxc_rootfs
*rootfs
,
1254 const struct lxc_console
*console
)
1256 char path
[MAXPATHLEN
];
1260 ret
= snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1261 if (ret
>= sizeof(path
)) {
1262 ERROR("console path too long\n");
1266 if (access(path
, F_OK
)) {
1267 WARN("rootfs specified but no console found at '%s'", path
);
1271 if (console
->peer
== -1) {
1272 INFO("no console output required");
1276 if (stat(path
, &s
)) {
1277 SYSERROR("failed to stat '%s'", path
);
1281 if (chmod(console
->name
, s
.st_mode
)) {
1282 SYSERROR("failed to set mode '0%o' to '%s'",
1283 s
.st_mode
, console
->name
);
1287 if (mount(console
->name
, path
, "none", MS_BIND
, 0)) {
1288 ERROR("failed to mount '%s' on '%s'", console
->name
, path
);
1292 INFO("console has been setup");
1296 static int setup_ttydir_console(const struct lxc_rootfs
*rootfs
,
1297 const struct lxc_console
*console
,
1300 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
1303 /* create rootfs/dev/<ttydir> directory */
1304 ret
= snprintf(path
, sizeof(path
), "%s/dev/%s", rootfs
->mount
,
1306 if (ret
>= sizeof(path
))
1308 ret
= mkdir(path
, 0755);
1309 if (ret
&& errno
!= EEXIST
) {
1310 SYSERROR("failed with errno %d to create %s\n", errno
, path
);
1313 INFO("created %s\n", path
);
1315 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/console",
1316 rootfs
->mount
, ttydir
);
1317 if (ret
>= sizeof(lxcpath
)) {
1318 ERROR("console path too long\n");
1322 snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1324 if (ret
&& errno
!= ENOENT
) {
1325 SYSERROR("error unlinking %s\n", path
);
1329 ret
= creat(lxcpath
, 0660);
1330 if (ret
==-1 && errno
!= EEXIST
) {
1331 SYSERROR("error %d creating %s\n", errno
, lxcpath
);
1337 if (console
->peer
== -1) {
1338 INFO("no console output required");
1342 if (mount(console
->name
, lxcpath
, "none", MS_BIND
, 0)) {
1343 ERROR("failed to mount '%s' on '%s'", console
->name
, lxcpath
);
1347 /* create symlink from rootfs/dev/console to 'lxc/console' */
1348 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/console", ttydir
);
1349 if (ret
>= sizeof(lxcpath
)) {
1350 ERROR("lxc/console path too long");
1353 ret
= symlink(lxcpath
, path
);
1355 SYSERROR("failed to create symlink for console");
1359 INFO("console has been setup on %s", lxcpath
);
1364 static int setup_console(const struct lxc_rootfs
*rootfs
,
1365 const struct lxc_console
*console
,
1368 /* We don't have a rootfs, /dev/console will be shared */
1372 return setup_dev_console(rootfs
, console
);
1374 return setup_ttydir_console(rootfs
, console
, ttydir
);
1377 static int setup_kmsg(const struct lxc_rootfs
*rootfs
,
1378 const struct lxc_console
*console
)
1380 char kpath
[MAXPATHLEN
];
1385 ret
= snprintf(kpath
, sizeof(kpath
), "%s/dev/kmsg", rootfs
->mount
);
1386 if (ret
< 0 || ret
>= sizeof(kpath
))
1389 ret
= unlink(kpath
);
1390 if (ret
&& errno
!= ENOENT
) {
1391 SYSERROR("error unlinking %s\n", kpath
);
1395 ret
= symlink("console", kpath
);
1397 SYSERROR("failed to create symlink for kmsg");
1404 static int _setup_cgroup(const char *cgpath
, struct lxc_list
*cgroups
,
1407 struct lxc_list
*iterator
;
1408 struct lxc_cgroup
*cg
;
1411 if (lxc_list_empty(cgroups
))
1414 lxc_list_for_each(iterator
, cgroups
) {
1415 cg
= iterator
->elem
;
1417 if (devices
== !strncmp("devices", cg
->subsystem
, 7)) {
1418 if (lxc_cgroup_set_bypath(cgpath
, cg
->subsystem
,
1420 ERROR("Error setting %s to %s for %s\n",
1421 cg
->subsystem
, cg
->value
, cgpath
);
1426 DEBUG("cgroup '%s' set to '%s'", cg
->subsystem
, cg
->value
);
1430 INFO("cgroup has been setup");
1435 int setup_cgroup_devices(const char *cgpath
, struct lxc_list
*cgroups
)
1437 return _setup_cgroup(cgpath
, cgroups
, 1);
1440 int setup_cgroup(const char *cgpath
, struct lxc_list
*cgroups
)
1442 return _setup_cgroup(cgpath
, cgroups
, 0);
1445 static void parse_mntopt(char *opt
, unsigned long *flags
, char **data
)
1447 struct mount_opt
*mo
;
1449 /* If opt is found in mount_opt, set or clear flags.
1450 * Otherwise append it to data. */
1452 for (mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
1453 if (!strncmp(opt
, mo
->name
, strlen(mo
->name
))) {
1455 *flags
&= ~mo
->flag
;
1467 static int parse_mntopts(const char *mntopts
, unsigned long *mntflags
,
1471 char *p
, *saveptr
= NULL
;
1479 s
= strdup(mntopts
);
1481 SYSERROR("failed to allocate memory");
1485 data
= malloc(strlen(s
) + 1);
1487 SYSERROR("failed to allocate memory");
1493 for (p
= strtok_r(s
, ",", &saveptr
); p
!= NULL
;
1494 p
= strtok_r(NULL
, ",", &saveptr
))
1495 parse_mntopt(p
, mntflags
, &data
);
1506 static int mount_entry(const char *fsname
, const char *target
,
1507 const char *fstype
, unsigned long mountflags
,
1510 if (mount(fsname
, target
, fstype
, mountflags
& ~MS_REMOUNT
, data
)) {
1511 SYSERROR("failed to mount '%s' on '%s'", fsname
, target
);
1515 if ((mountflags
& MS_REMOUNT
) || (mountflags
& MS_BIND
)) {
1517 DEBUG("remounting %s on %s to respect bind or remount options",
1520 if (mount(fsname
, target
, fstype
,
1521 mountflags
| MS_REMOUNT
, data
)) {
1522 SYSERROR("failed to mount '%s' on '%s'",
1528 DEBUG("mounted '%s' on '%s', type '%s'", fsname
, target
, fstype
);
1533 static inline int mount_entry_on_systemfs(struct mntent
*mntent
)
1535 unsigned long mntflags
;
1539 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1540 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1544 ret
= mount_entry(mntent
->mnt_fsname
, mntent
->mnt_dir
,
1545 mntent
->mnt_type
, mntflags
, mntdata
);
1547 if (hasmntopt(mntent
, "optional") != NULL
)
1555 static int mount_entry_on_absolute_rootfs(struct mntent
*mntent
,
1556 const struct lxc_rootfs
*rootfs
,
1557 const char *lxc_name
)
1560 char path
[MAXPATHLEN
];
1561 unsigned long mntflags
;
1563 int r
, ret
= 0, offset
;
1564 const char *lxcpath
;
1566 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1567 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1571 lxcpath
= default_lxc_path();
1573 ERROR("Out of memory");
1577 /* if rootfs->path is a blockdev path, allow container fstab to
1578 * use $lxcpath/CN/rootfs as the target prefix */
1579 r
= snprintf(path
, MAXPATHLEN
, "%s/%s/rootfs", lxcpath
, lxc_name
);
1580 if (r
< 0 || r
>= MAXPATHLEN
)
1583 aux
= strstr(mntent
->mnt_dir
, path
);
1585 offset
= strlen(path
);
1590 aux
= strstr(mntent
->mnt_dir
, rootfs
->path
);
1592 WARN("ignoring mount point '%s'", mntent
->mnt_dir
);
1595 offset
= strlen(rootfs
->path
);
1599 r
= snprintf(path
, MAXPATHLEN
, "%s/%s", rootfs
->mount
,
1601 if (r
< 0 || r
>= MAXPATHLEN
) {
1602 WARN("pathnme too long for '%s'", mntent
->mnt_dir
);
1608 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1611 if (hasmntopt(mntent
, "optional") != NULL
)
1619 static int mount_entry_on_relative_rootfs(struct mntent
*mntent
,
1622 char path
[MAXPATHLEN
];
1623 unsigned long mntflags
;
1627 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1628 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1632 /* relative to root mount point */
1633 ret
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, mntent
->mnt_dir
);
1634 if (ret
>= sizeof(path
)) {
1635 ERROR("path name too long");
1639 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1642 if (hasmntopt(mntent
, "optional") != NULL
)
1650 static int mount_file_entries(const struct lxc_rootfs
*rootfs
, FILE *file
,
1651 const char *lxc_name
)
1653 struct mntent
*mntent
;
1656 while ((mntent
= getmntent(file
))) {
1658 if (!rootfs
->path
) {
1659 if (mount_entry_on_systemfs(mntent
))
1664 /* We have a separate root, mounts are relative to it */
1665 if (mntent
->mnt_dir
[0] != '/') {
1666 if (mount_entry_on_relative_rootfs(mntent
,
1672 if (mount_entry_on_absolute_rootfs(mntent
, rootfs
, lxc_name
))
1678 INFO("mount points have been setup");
1683 static int setup_mount(const struct lxc_rootfs
*rootfs
, const char *fstab
,
1684 const char *lxc_name
)
1692 file
= setmntent(fstab
, "r");
1694 SYSERROR("failed to use '%s'", fstab
);
1698 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1704 static int setup_mount_entries(const struct lxc_rootfs
*rootfs
, struct lxc_list
*mount
,
1705 const char *lxc_name
)
1708 struct lxc_list
*iterator
;
1714 ERROR("tmpfile error: %m");
1718 lxc_list_for_each(iterator
, mount
) {
1719 mount_entry
= iterator
->elem
;
1720 fprintf(file
, "%s\n", mount_entry
);
1725 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1731 static int setup_caps(struct lxc_list
*caps
)
1733 struct lxc_list
*iterator
;
1738 lxc_list_for_each(iterator
, caps
) {
1740 drop_entry
= iterator
->elem
;
1744 for (i
= 0; i
< sizeof(caps_opt
)/sizeof(caps_opt
[0]); i
++) {
1746 if (strcmp(drop_entry
, caps_opt
[i
].name
))
1749 capid
= caps_opt
[i
].value
;
1754 /* try to see if it's numeric, so the user may specify
1755 * capabilities that the running kernel knows about but
1757 capid
= strtol(drop_entry
, &ptr
, 10);
1758 if (!ptr
|| *ptr
!= '\0' ||
1759 capid
== LONG_MIN
|| capid
== LONG_MAX
)
1760 /* not a valid number */
1762 else if (capid
> lxc_caps_last_cap())
1763 /* we have a number but it's not a valid
1769 ERROR("unknown capability %s", drop_entry
);
1773 DEBUG("drop capability '%s' (%d)", drop_entry
, capid
);
1775 if (prctl(PR_CAPBSET_DROP
, capid
, 0, 0, 0)) {
1776 SYSERROR("failed to remove %s capability", drop_entry
);
1782 DEBUG("capabilities has been setup");
1787 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
1789 struct sockaddr sockaddr
;
1793 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
1795 ERROR("mac address '%s' conversion failed : %s",
1796 hwaddr
, strerror(-ret
));
1800 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1801 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
1803 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1805 ERROR("socket failure : %s", strerror(errno
));
1809 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
1812 ERROR("ioctl failure : %s", strerror(errno
));
1814 DEBUG("mac address '%s' on '%s' has been setup", hwaddr
, ifname
);
1819 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
1821 struct lxc_list
*iterator
;
1822 struct lxc_inetdev
*inetdev
;
1825 lxc_list_for_each(iterator
, ip
) {
1827 inetdev
= iterator
->elem
;
1829 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
1830 &inetdev
->bcast
, inetdev
->prefix
);
1832 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
1833 ifindex
, strerror(-err
));
1841 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
1843 struct lxc_list
*iterator
;
1844 struct lxc_inet6dev
*inet6dev
;
1847 lxc_list_for_each(iterator
, ip
) {
1849 inet6dev
= iterator
->elem
;
1851 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
1852 &inet6dev
->mcast
, &inet6dev
->acast
,
1855 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
1856 ifindex
, strerror(-err
));
1864 static int setup_netdev(struct lxc_netdev
*netdev
)
1866 char ifname
[IFNAMSIZ
];
1867 char *current_ifname
= ifname
;
1870 /* empty network namespace */
1871 if (!netdev
->ifindex
) {
1872 if (netdev
->flags
& IFF_UP
) {
1873 err
= lxc_netdev_up("lo");
1875 ERROR("failed to set the loopback up : %s",
1883 /* retrieve the name of the interface */
1884 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1885 ERROR("no interface corresponding to index '%d'",
1890 /* default: let the system to choose one interface name */
1892 netdev
->name
= netdev
->type
== LXC_NET_PHYS
?
1893 netdev
->link
: "eth%d";
1895 /* rename the interface name */
1896 err
= lxc_netdev_rename_by_name(ifname
, netdev
->name
);
1898 ERROR("failed to rename %s->%s : %s", ifname
, netdev
->name
,
1903 /* Re-read the name of the interface because its name has changed
1904 * and would be automatically allocated by the system
1906 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1907 ERROR("no interface corresponding to index '%d'",
1912 /* set a mac address */
1913 if (netdev
->hwaddr
) {
1914 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
1915 ERROR("failed to setup hw address for '%s'",
1921 /* setup ipv4 addresses on the interface */
1922 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
1923 ERROR("failed to setup ip addresses for '%s'",
1928 /* setup ipv6 addresses on the interface */
1929 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
1930 ERROR("failed to setup ipv6 addresses for '%s'",
1935 /* set the network device up */
1936 if (netdev
->flags
& IFF_UP
) {
1939 err
= lxc_netdev_up(current_ifname
);
1941 ERROR("failed to set '%s' up : %s", current_ifname
,
1946 /* the network is up, make the loopback up too */
1947 err
= lxc_netdev_up("lo");
1949 ERROR("failed to set the loopback up : %s",
1955 /* We can only set up the default routes after bringing
1956 * up the interface, sine bringing up the interface adds
1957 * the link-local routes and we can't add a default
1958 * route if the gateway is not reachable. */
1960 /* setup ipv4 gateway on the interface */
1961 if (netdev
->ipv4_gateway
) {
1962 if (!(netdev
->flags
& IFF_UP
)) {
1963 ERROR("Cannot add ipv4 gateway for %s when not bringing up the interface", ifname
);
1967 if (lxc_list_empty(&netdev
->ipv4
)) {
1968 ERROR("Cannot add ipv4 gateway for %s when not assigning an address", ifname
);
1972 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
1974 ERROR("failed to setup ipv4 gateway for '%s': %s",
1975 ifname
, strerror(-err
));
1976 if (netdev
->ipv4_gateway_auto
) {
1977 char buf
[INET_ADDRSTRLEN
];
1978 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
1979 ERROR("tried to set autodetected ipv4 gateway '%s'", buf
);
1985 /* setup ipv6 gateway on the interface */
1986 if (netdev
->ipv6_gateway
) {
1987 if (!(netdev
->flags
& IFF_UP
)) {
1988 ERROR("Cannot add ipv6 gateway for %s when not bringing up the interface", ifname
);
1992 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
1993 ERROR("Cannot add ipv6 gateway for %s when not assigning an address", ifname
);
1997 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
1999 ERROR("failed to setup ipv6 gateway for '%s': %s",
2000 ifname
, strerror(-err
));
2001 if (netdev
->ipv6_gateway_auto
) {
2002 char buf
[INET6_ADDRSTRLEN
];
2003 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
2004 ERROR("tried to set autodetected ipv6 gateway '%s'", buf
);
2010 DEBUG("'%s' has been setup", current_ifname
);
2015 static int setup_network(struct lxc_list
*network
)
2017 struct lxc_list
*iterator
;
2018 struct lxc_netdev
*netdev
;
2020 lxc_list_for_each(iterator
, network
) {
2022 netdev
= iterator
->elem
;
2024 if (setup_netdev(netdev
)) {
2025 ERROR("failed to setup netdev");
2030 if (!lxc_list_empty(network
))
2031 INFO("network has been setup");
2036 void lxc_rename_phys_nics_on_shutdown(struct lxc_conf
*conf
)
2040 INFO("running to reset %d nic names", conf
->num_savednics
);
2041 for (i
=0; i
<conf
->num_savednics
; i
++) {
2042 struct saved_nic
*s
= &conf
->saved_nics
[i
];
2043 INFO("resetting nic %d to %s\n", s
->ifindex
, s
->orig_name
);
2044 lxc_netdev_rename_by_index(s
->ifindex
, s
->orig_name
);
2047 conf
->num_savednics
= 0;
2048 free(conf
->saved_nics
);
2051 static int setup_private_host_hw_addr(char *veth1
)
2057 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
2061 snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2062 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2068 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2069 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2074 DEBUG("mac address of host interface '%s' changed to private "
2075 "%02x:%02x:%02x:%02x:%02x:%02x", veth1
,
2076 ifr
.ifr_hwaddr
.sa_data
[0] & 0xff,
2077 ifr
.ifr_hwaddr
.sa_data
[1] & 0xff,
2078 ifr
.ifr_hwaddr
.sa_data
[2] & 0xff,
2079 ifr
.ifr_hwaddr
.sa_data
[3] & 0xff,
2080 ifr
.ifr_hwaddr
.sa_data
[4] & 0xff,
2081 ifr
.ifr_hwaddr
.sa_data
[5] & 0xff);
2086 static char *default_rootfs_mount
= LXCROOTFSMOUNT
;
2088 struct lxc_conf
*lxc_conf_init(void)
2090 struct lxc_conf
*new;
2093 new = malloc(sizeof(*new));
2095 ERROR("lxc_conf_init : %m");
2098 memset(new, 0, sizeof(*new));
2100 new->personality
= -1;
2101 new->console
.log_path
= NULL
;
2102 new->console
.log_fd
= -1;
2103 new->console
.path
= NULL
;
2104 new->console
.peer
= -1;
2105 new->console
.master
= -1;
2106 new->console
.slave
= -1;
2107 new->console
.name
[0] = '\0';
2108 new->maincmd_fd
= -1;
2109 new->rootfs
.mount
= default_rootfs_mount
;
2111 lxc_list_init(&new->cgroup
);
2112 lxc_list_init(&new->network
);
2113 lxc_list_init(&new->mount_list
);
2114 lxc_list_init(&new->caps
);
2115 lxc_list_init(&new->id_map
);
2116 for (i
=0; i
<NUM_LXC_HOOKS
; i
++)
2117 lxc_list_init(&new->hooks
[i
]);
2119 new->aa_profile
= NULL
;
2121 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2122 new->lsm_umount_proc
= 0;
2128 static int instanciate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2130 char veth1buf
[IFNAMSIZ
], *veth1
;
2131 char veth2buf
[IFNAMSIZ
], *veth2
;
2134 if (netdev
->priv
.veth_attr
.pair
)
2135 veth1
= netdev
->priv
.veth_attr
.pair
;
2137 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
2138 if (err
>= sizeof(veth1buf
)) { /* can't *really* happen, but... */
2139 ERROR("veth1 name too long");
2142 veth1
= mktemp(veth1buf
);
2143 /* store away for deconf */
2144 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
2147 snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
2148 veth2
= mktemp(veth2buf
);
2150 if (!strlen(veth1
) || !strlen(veth2
)) {
2151 ERROR("failed to allocate a temporary name");
2155 err
= lxc_veth_create(veth1
, veth2
);
2157 ERROR("failed to create %s-%s : %s", veth1
, veth2
,
2162 /* changing the high byte of the mac address to 0xfe, the bridge interface
2163 * will always keep the host's mac address and not take the mac address
2165 err
= setup_private_host_hw_addr(veth1
);
2167 ERROR("failed to change mac address of host interface '%s' : %s",
2168 veth1
, strerror(-err
));
2173 err
= lxc_netdev_set_mtu(veth1
, atoi(netdev
->mtu
));
2175 err
= lxc_netdev_set_mtu(veth2
, atoi(netdev
->mtu
));
2177 ERROR("failed to set mtu '%s' for %s-%s : %s",
2178 netdev
->mtu
, veth1
, veth2
, strerror(-err
));
2184 err
= lxc_bridge_attach(netdev
->link
, veth1
);
2186 ERROR("failed to attach '%s' to the bridge '%s' : %s",
2187 veth1
, netdev
->link
, strerror(-err
));
2192 netdev
->ifindex
= if_nametoindex(veth2
);
2193 if (!netdev
->ifindex
) {
2194 ERROR("failed to retrieve the index for %s", veth2
);
2198 err
= lxc_netdev_up(veth1
);
2200 ERROR("failed to set %s up : %s", veth1
, strerror(-err
));
2204 if (netdev
->upscript
) {
2205 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2206 "veth", veth1
, (char*) NULL
);
2211 DEBUG("instanciated veth '%s/%s', index is '%d'",
2212 veth1
, veth2
, netdev
->ifindex
);
2217 lxc_netdev_delete_by_name(veth1
);
2221 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2226 if (netdev
->priv
.veth_attr
.pair
)
2227 veth1
= netdev
->priv
.veth_attr
.pair
;
2229 veth1
= netdev
->priv
.veth_attr
.veth1
;
2231 if (netdev
->downscript
) {
2232 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2233 "down", "veth", veth1
, (char*) NULL
);
2240 static int instanciate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2242 char peerbuf
[IFNAMSIZ
], *peer
;
2245 if (!netdev
->link
) {
2246 ERROR("no link specified for macvlan netdev");
2250 err
= snprintf(peerbuf
, sizeof(peerbuf
), "mcXXXXXX");
2251 if (err
>= sizeof(peerbuf
))
2254 peer
= mktemp(peerbuf
);
2255 if (!strlen(peer
)) {
2256 ERROR("failed to make a temporary name");
2260 err
= lxc_macvlan_create(netdev
->link
, peer
,
2261 netdev
->priv
.macvlan_attr
.mode
);
2263 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
2264 peer
, netdev
->link
, strerror(-err
));
2268 netdev
->ifindex
= if_nametoindex(peer
);
2269 if (!netdev
->ifindex
) {
2270 ERROR("failed to retrieve the index for %s", peer
);
2271 lxc_netdev_delete_by_name(peer
);
2275 if (netdev
->upscript
) {
2276 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2277 "macvlan", netdev
->link
, (char*) NULL
);
2282 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
2283 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
2288 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2292 if (netdev
->downscript
) {
2293 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2294 "down", "macvlan", netdev
->link
,
2302 /* XXX: merge with instanciate_macvlan */
2303 static int instanciate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2305 char peer
[IFNAMSIZ
];
2308 if (!netdev
->link
) {
2309 ERROR("no link specified for vlan netdev");
2313 err
= snprintf(peer
, sizeof(peer
), "vlan%d", netdev
->priv
.vlan_attr
.vid
);
2314 if (err
>= sizeof(peer
)) {
2315 ERROR("peer name too long");
2319 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
2321 ERROR("failed to create vlan interface '%s' on '%s' : %s",
2322 peer
, netdev
->link
, strerror(-err
));
2326 netdev
->ifindex
= if_nametoindex(peer
);
2327 if (!netdev
->ifindex
) {
2328 ERROR("failed to retrieve the ifindex for %s", peer
);
2329 lxc_netdev_delete_by_name(peer
);
2333 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
2339 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2344 static int instanciate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2346 if (!netdev
->link
) {
2347 ERROR("no link specified for the physical interface");
2351 netdev
->ifindex
= if_nametoindex(netdev
->link
);
2352 if (!netdev
->ifindex
) {
2353 ERROR("failed to retrieve the index for %s", netdev
->link
);
2357 if (netdev
->upscript
) {
2359 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2360 "up", "phys", netdev
->link
, (char*) NULL
);
2368 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2372 if (netdev
->downscript
) {
2373 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2374 "down", "phys", netdev
->link
, (char*) NULL
);
2381 static int instanciate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2383 netdev
->ifindex
= 0;
2384 if (netdev
->upscript
) {
2386 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2387 "up", "empty", (char*) NULL
);
2394 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2398 if (netdev
->downscript
) {
2399 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2400 "down", "empty", (char*) NULL
);
2407 int lxc_create_network(struct lxc_handler
*handler
)
2409 struct lxc_list
*network
= &handler
->conf
->network
;
2410 struct lxc_list
*iterator
;
2411 struct lxc_netdev
*netdev
;
2413 lxc_list_for_each(iterator
, network
) {
2415 netdev
= iterator
->elem
;
2417 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
2418 ERROR("invalid network configuration type '%d'",
2423 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
2424 ERROR("failed to create netdev");
2433 void lxc_delete_network(struct lxc_handler
*handler
)
2435 struct lxc_list
*network
= &handler
->conf
->network
;
2436 struct lxc_list
*iterator
;
2437 struct lxc_netdev
*netdev
;
2439 lxc_list_for_each(iterator
, network
) {
2440 netdev
= iterator
->elem
;
2442 if (netdev
->ifindex
!= 0 && netdev
->type
== LXC_NET_PHYS
) {
2443 if (lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
))
2444 WARN("failed to rename to the initial name the " \
2445 "netdev '%s'", netdev
->link
);
2449 if (netdev_deconf
[netdev
->type
](handler
, netdev
)) {
2450 WARN("failed to destroy netdev");
2453 /* Recent kernel remove the virtual interfaces when the network
2454 * namespace is destroyed but in case we did not moved the
2455 * interface to the network namespace, we have to destroy it
2457 if (netdev
->ifindex
!= 0 &&
2458 lxc_netdev_delete_by_index(netdev
->ifindex
))
2459 WARN("failed to remove interface '%s'", netdev
->name
);
2463 int lxc_assign_network(struct lxc_list
*network
, pid_t pid
)
2465 struct lxc_list
*iterator
;
2466 struct lxc_netdev
*netdev
;
2469 lxc_list_for_each(iterator
, network
) {
2471 netdev
= iterator
->elem
;
2473 /* empty network namespace, nothing to move */
2474 if (!netdev
->ifindex
)
2477 err
= lxc_netdev_move_by_index(netdev
->ifindex
, pid
);
2479 ERROR("failed to move '%s' to the container : %s",
2480 netdev
->link
, strerror(-err
));
2484 DEBUG("move '%s' to '%d'", netdev
->name
, pid
);
2490 static int write_id_mapping(enum idtype idtype
, pid_t pid
, const char *buf
,
2493 char path
[PATH_MAX
];
2497 ret
= snprintf(path
, PATH_MAX
, "/proc/%d/%cid_map", pid
, idtype
== ID_TYPE_UID
? 'u' : 'g');
2498 if (ret
< 0 || ret
>= PATH_MAX
) {
2499 fprintf(stderr
, "%s: path name too long", __func__
);
2502 f
= fopen(path
, "w");
2507 ret
= fwrite(buf
, buf_size
, 1, f
);
2509 SYSERROR("writing id mapping");
2510 closeret
= fclose(f
);
2512 SYSERROR("writing id mapping");
2513 return ret
< 0 ? ret
: closeret
;
2516 int lxc_map_ids(struct lxc_list
*idmap
, pid_t pid
)
2518 struct lxc_list
*iterator
;
2522 char *buf
= NULL
, *pos
;
2524 for(type
= ID_TYPE_UID
; type
<= ID_TYPE_GID
; type
++) {
2528 lxc_list_for_each(iterator
, idmap
) {
2529 /* The kernel only takes <= 4k for writes to /proc/<nr>/[ug]id_map */
2531 buf
= pos
= malloc(4096);
2535 map
= iterator
->elem
;
2536 if (map
->idtype
== type
) {
2537 left
= 4096 - (pos
- buf
);
2538 fill
= snprintf(pos
, left
, "%lu %lu %lu\n",
2539 map
->nsid
, map
->hostid
, map
->range
);
2540 if (fill
<= 0 || fill
>= left
)
2541 SYSERROR("snprintf failed, too many mappings");
2545 if (pos
== buf
) // no mappings were found
2547 ret
= write_id_mapping(type
, pid
, buf
, pos
-buf
);
2557 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2559 struct lxc_list
*network
= &handler
->conf
->network
;
2560 struct lxc_list
*iterator
;
2561 struct lxc_netdev
*netdev
;
2564 lxc_list_for_each(iterator
, network
) {
2565 netdev
= iterator
->elem
;
2567 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2570 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2571 ERROR("gateway = auto only supported for "
2572 "veth and macvlan");
2576 if (!netdev
->link
) {
2577 ERROR("gateway = auto needs a link interface");
2581 link_index
= if_nametoindex(netdev
->link
);
2585 if (netdev
->ipv4_gateway_auto
) {
2586 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2587 ERROR("failed to automatically find ipv4 gateway "
2588 "address from link interface '%s'", netdev
->link
);
2593 if (netdev
->ipv6_gateway_auto
) {
2594 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2595 ERROR("failed to automatically find ipv6 gateway "
2596 "address from link interface '%s'", netdev
->link
);
2605 int lxc_create_tty(const char *name
, struct lxc_conf
*conf
)
2607 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2610 /* no tty in the configuration */
2614 tty_info
->pty_info
=
2615 malloc(sizeof(*tty_info
->pty_info
)*conf
->tty
);
2616 if (!tty_info
->pty_info
) {
2617 SYSERROR("failed to allocate pty_info");
2621 for (i
= 0; i
< conf
->tty
; i
++) {
2623 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2625 if (openpty(&pty_info
->master
, &pty_info
->slave
,
2626 pty_info
->name
, NULL
, NULL
)) {
2627 SYSERROR("failed to create pty #%d", i
);
2628 tty_info
->nbtty
= i
;
2629 lxc_delete_tty(tty_info
);
2633 DEBUG("allocated pty '%s' (%d/%d)",
2634 pty_info
->name
, pty_info
->master
, pty_info
->slave
);
2636 /* Prevent leaking the file descriptors to the container */
2637 fcntl(pty_info
->master
, F_SETFD
, FD_CLOEXEC
);
2638 fcntl(pty_info
->slave
, F_SETFD
, FD_CLOEXEC
);
2643 tty_info
->nbtty
= conf
->tty
;
2645 INFO("tty's configured");
2650 void lxc_delete_tty(struct lxc_tty_info
*tty_info
)
2654 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
2655 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2657 close(pty_info
->master
);
2658 close(pty_info
->slave
);
2661 free(tty_info
->pty_info
);
2662 tty_info
->nbtty
= 0;
2666 * given a host uid, return the ns uid if it is mapped.
2667 * if it is not mapped, return the original host id.
2669 static int shiftid(struct lxc_conf
*c
, int uid
, enum idtype w
)
2671 struct lxc_list
*iterator
;
2675 lxc_list_for_each(iterator
, &c
->id_map
) {
2676 map
= iterator
->elem
;
2677 if (map
->idtype
!= w
)
2681 high
= map
->nsid
+ map
->range
;
2682 if (uid
< low
|| uid
>= high
)
2685 return uid
- low
+ map
->hostid
;
2692 * Take a pathname for a file created on the host, and map the uid and gid
2693 * into the container if needed. (Used for ttys)
2695 static int uid_shift_file(char *path
, struct lxc_conf
*c
)
2697 struct stat statbuf
;
2700 if (stat(path
, &statbuf
)) {
2701 SYSERROR("stat(%s)", path
);
2705 newuid
= shiftid(c
, statbuf
.st_uid
, ID_TYPE_UID
);
2706 newgid
= shiftid(c
, statbuf
.st_gid
, ID_TYPE_GID
);
2707 if (newuid
!= statbuf
.st_uid
|| newgid
!= statbuf
.st_gid
) {
2708 DEBUG("chowning %s from %d:%d to %d:%d\n", path
, (int)statbuf
.st_uid
, (int)statbuf
.st_gid
, newuid
, newgid
);
2709 if (chown(path
, newuid
, newgid
)) {
2710 SYSERROR("chown(%s)", path
);
2717 int uid_shift_ttys(int pid
, struct lxc_conf
*conf
)
2720 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2721 char path
[MAXPATHLEN
];
2722 char *ttydir
= conf
->ttydir
;
2724 if (!conf
->rootfs
.path
)
2726 /* first the console */
2727 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/%s/console", pid
, ttydir
? ttydir
: "");
2728 if (ret
< 0 || ret
>= sizeof(path
)) {
2729 ERROR("console path too long\n");
2732 if (uid_shift_file(path
, conf
)) {
2733 DEBUG("Failed to chown the console %s.\n", path
);
2736 for (i
=0; i
< tty_info
->nbtty
; i
++) {
2737 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/%s/tty%d",
2738 pid
, ttydir
? ttydir
: "", i
+ 1);
2739 if (ret
< 0 || ret
>= sizeof(path
)) {
2740 ERROR("pathname too long for ttys");
2743 if (uid_shift_file(path
, conf
)) {
2744 DEBUG("Failed to chown pty %s.\n", path
);
2752 int lxc_setup(const char *name
, struct lxc_conf
*lxc_conf
)
2754 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2758 if (setup_utsname(lxc_conf
->utsname
)) {
2759 ERROR("failed to setup the utsname for '%s'", name
);
2763 if (setup_network(&lxc_conf
->network
)) {
2764 ERROR("failed to setup the network for '%s'", name
);
2768 if (run_lxc_hooks(name
, "pre-mount", lxc_conf
)) {
2769 ERROR("failed to run pre-mount hooks for container '%s'.", name
);
2773 if (setup_rootfs(lxc_conf
)) {
2774 ERROR("failed to setup rootfs for '%s'", name
);
2778 if (lxc_conf
->autodev
) {
2779 if (mount_autodev(lxc_conf
->rootfs
.mount
)) {
2780 ERROR("failed to mount /dev in the container");
2785 if (setup_mount(&lxc_conf
->rootfs
, lxc_conf
->fstab
, name
)) {
2786 ERROR("failed to setup the mounts for '%s'", name
);
2790 if (!lxc_list_empty(&lxc_conf
->mount_list
) && setup_mount_entries(&lxc_conf
->rootfs
, &lxc_conf
->mount_list
, name
)) {
2791 ERROR("failed to setup the mount entries for '%s'", name
);
2795 if (run_lxc_hooks(name
, "mount", lxc_conf
)) {
2796 ERROR("failed to run mount hooks for container '%s'.", name
);
2800 if (lxc_conf
->autodev
) {
2801 if (run_lxc_hooks(name
, "autodev", lxc_conf
)) {
2802 ERROR("failed to run autodev hooks for container '%s'.", name
);
2805 if (setup_autodev(lxc_conf
->rootfs
.mount
)) {
2806 ERROR("failed to populate /dev in the container");
2811 if (setup_console(&lxc_conf
->rootfs
, &lxc_conf
->console
, lxc_conf
->ttydir
)) {
2812 ERROR("failed to setup the console for '%s'", name
);
2816 if (lxc_conf
->kmsg
) {
2817 if (setup_kmsg(&lxc_conf
->rootfs
, &lxc_conf
->console
)) // don't fail
2818 ERROR("failed to setup kmsg for '%s'", name
);
2821 if (setup_tty(&lxc_conf
->rootfs
, &lxc_conf
->tty_info
, lxc_conf
->ttydir
)) {
2822 ERROR("failed to setup the ttys for '%s'", name
);
2826 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2827 INFO("rootfs path is .%s., mount is .%s.", lxc_conf
->rootfs
.path
,
2828 lxc_conf
->rootfs
.mount
);
2829 if (lxc_conf
->rootfs
.path
== NULL
|| strlen(lxc_conf
->rootfs
.path
) == 0)
2832 mounted
= lsm_mount_proc_if_needed(lxc_conf
->rootfs
.path
, lxc_conf
->rootfs
.mount
);
2833 if (mounted
== -1) {
2834 SYSERROR("failed to mount /proc in the container.");
2836 } else if (mounted
== 1) {
2837 lxc_conf
->lsm_umount_proc
= 1;
2841 if (setup_pivot_root(&lxc_conf
->rootfs
)) {
2842 ERROR("failed to set rootfs for '%s'", name
);
2846 if (setup_pts(lxc_conf
->pts
)) {
2847 ERROR("failed to setup the new pts instance");
2851 if (setup_personality(lxc_conf
->personality
)) {
2852 ERROR("failed to setup personality");
2856 if (lxc_list_empty(&lxc_conf
->id_map
)) {
2857 if (setup_caps(&lxc_conf
->caps
)) {
2858 ERROR("failed to drop capabilities");
2863 NOTICE("'%s' is setup.", name
);
2868 int run_lxc_hooks(const char *name
, char *hook
, struct lxc_conf
*conf
)
2871 struct lxc_list
*it
;
2873 if (strcmp(hook
, "pre-start") == 0)
2874 which
= LXCHOOK_PRESTART
;
2875 else if (strcmp(hook
, "pre-mount") == 0)
2876 which
= LXCHOOK_PREMOUNT
;
2877 else if (strcmp(hook
, "mount") == 0)
2878 which
= LXCHOOK_MOUNT
;
2879 else if (strcmp(hook
, "autodev") == 0)
2880 which
= LXCHOOK_AUTODEV
;
2881 else if (strcmp(hook
, "start") == 0)
2882 which
= LXCHOOK_START
;
2883 else if (strcmp(hook
, "post-stop") == 0)
2884 which
= LXCHOOK_POSTSTOP
;
2887 lxc_list_for_each(it
, &conf
->hooks
[which
]) {
2889 char *hookname
= it
->elem
;
2890 ret
= run_script(name
, "lxc", hookname
, hook
, NULL
);
2897 static void lxc_remove_nic(struct lxc_list
*it
)
2899 struct lxc_netdev
*netdev
= it
->elem
;
2900 struct lxc_list
*it2
,*next
;
2908 if (netdev
->upscript
)
2909 free(netdev
->upscript
);
2911 free(netdev
->hwaddr
);
2914 if (netdev
->ipv4_gateway
)
2915 free(netdev
->ipv4_gateway
);
2916 if (netdev
->ipv6_gateway
)
2917 free(netdev
->ipv6_gateway
);
2918 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2923 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2932 /* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
2933 int lxc_clear_nic(struct lxc_conf
*c
, const char *key
)
2937 struct lxc_list
*it
;
2938 struct lxc_netdev
*netdev
;
2940 p1
= index(key
, '.');
2941 if (!p1
|| *(p1
+1) == '\0')
2944 ret
= sscanf(key
, "%d", &idx
);
2945 if (ret
!= 1) return -1;
2950 lxc_list_for_each(it
, &c
->network
) {
2955 if (i
< idx
) // we don't have that many nics defined
2958 if (!it
|| !it
->elem
)
2965 } else if (strcmp(p1
, "ipv4") == 0) {
2966 struct lxc_list
*it2
,*next
;
2967 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2972 } else if (strcmp(p1
, "ipv6") == 0) {
2973 struct lxc_list
*it2
,*next
;
2974 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2979 } else if (strcmp(p1
, "link") == 0) {
2982 netdev
->link
= NULL
;
2984 } else if (strcmp(p1
, "name") == 0) {
2987 netdev
->name
= NULL
;
2989 } else if (strcmp(p1
, "script.up") == 0) {
2990 if (netdev
->upscript
) {
2991 free(netdev
->upscript
);
2992 netdev
->upscript
= NULL
;
2994 } else if (strcmp(p1
, "hwaddr") == 0) {
2995 if (netdev
->hwaddr
) {
2996 free(netdev
->hwaddr
);
2997 netdev
->hwaddr
= NULL
;
2999 } else if (strcmp(p1
, "mtu") == 0) {
3004 } else if (strcmp(p1
, "ipv4_gateway") == 0) {
3005 if (netdev
->ipv4_gateway
) {
3006 free(netdev
->ipv4_gateway
);
3007 netdev
->ipv4_gateway
= NULL
;
3009 } else if (strcmp(p1
, "ipv6_gateway") == 0) {
3010 if (netdev
->ipv6_gateway
) {
3011 free(netdev
->ipv6_gateway
);
3012 netdev
->ipv6_gateway
= NULL
;
3020 int lxc_clear_config_network(struct lxc_conf
*c
)
3022 struct lxc_list
*it
,*next
;
3023 lxc_list_for_each_safe(it
, &c
->network
, next
) {
3029 int lxc_clear_config_caps(struct lxc_conf
*c
)
3031 struct lxc_list
*it
,*next
;
3033 lxc_list_for_each_safe(it
, &c
->caps
, next
) {
3041 int lxc_clear_cgroups(struct lxc_conf
*c
, const char *key
)
3043 struct lxc_list
*it
,*next
;
3045 const char *k
= key
+ 11;
3047 if (strcmp(key
, "lxc.cgroup") == 0)
3050 lxc_list_for_each_safe(it
, &c
->cgroup
, next
) {
3051 struct lxc_cgroup
*cg
= it
->elem
;
3052 if (!all
&& strcmp(cg
->subsystem
, k
) != 0)
3055 free(cg
->subsystem
);
3063 int lxc_clear_mount_entries(struct lxc_conf
*c
)
3065 struct lxc_list
*it
,*next
;
3067 lxc_list_for_each_safe(it
, &c
->mount_list
, next
) {
3075 int lxc_clear_hooks(struct lxc_conf
*c
, const char *key
)
3077 struct lxc_list
*it
,*next
;
3078 bool all
= false, done
= false;
3079 const char *k
= key
+ 9;
3082 if (strcmp(key
, "lxc.hook") == 0)
3085 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
3086 if (all
|| strcmp(k
, lxchook_names
[i
]) == 0) {
3087 lxc_list_for_each_safe(it
, &c
->hooks
[i
], next
) {
3097 ERROR("Invalid hook key: %s", key
);
3103 void lxc_clear_saved_nics(struct lxc_conf
*conf
)
3107 if (!conf
->num_savednics
)
3109 for (i
=0; i
< conf
->num_savednics
; i
++)
3110 free(conf
->saved_nics
[i
].orig_name
);
3111 conf
->saved_nics
= 0;
3112 free(conf
->saved_nics
);
3115 void lxc_conf_free(struct lxc_conf
*conf
)
3119 if (conf
->console
.path
)
3120 free(conf
->console
.path
);
3121 if (conf
->rootfs
.mount
!= default_rootfs_mount
)
3122 free(conf
->rootfs
.mount
);
3123 if (conf
->rootfs
.path
)
3124 free(conf
->rootfs
.path
);
3126 free(conf
->utsname
);
3133 lxc_clear_config_network(conf
);
3135 if (conf
->aa_profile
)
3136 free(conf
->aa_profile
);
3138 lxc_seccomp_free(conf
);
3139 lxc_clear_config_caps(conf
);
3140 lxc_clear_cgroups(conf
, "lxc.cgroup");
3141 lxc_clear_hooks(conf
, "lxc.hook");
3142 lxc_clear_mount_entries(conf
);
3143 lxc_clear_saved_nics(conf
);