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() */
72 #if HAVE_SYS_CAPABILITY_H
73 #include <sys/capability.h>
76 #if HAVE_SYS_PERSONALITY_H
77 #include <sys/personality.h>
81 #include <../include/lxcmntent.h>
86 #include "lxcseccomp.h"
88 lxc_log_define(lxc_conf
, lxc
);
91 #define MAXINDEXLEN 20
93 #define MAXLINELEN 128
96 #define MS_DIRSYNC 128
108 #define MS_SLAVE (1<<19)
112 #define MS_RELATIME (1 << 21)
115 #ifndef MS_STRICTATIME
116 #define MS_STRICTATIME (1 << 24)
119 #if HAVE_SYS_CAPABILITY_H
121 #define CAP_SETFCAP 31
124 #ifndef CAP_MAC_OVERRIDE
125 #define CAP_MAC_OVERRIDE 32
128 #ifndef CAP_MAC_ADMIN
129 #define CAP_MAC_ADMIN 33
133 #ifndef PR_CAPBSET_DROP
134 #define PR_CAPBSET_DROP 24
137 #ifndef LO_FLAGS_AUTOCLEAR
138 #define LO_FLAGS_AUTOCLEAR 4
141 /* Define pivot_root() if missing from the C library */
142 #ifndef HAVE_PIVOT_ROOT
143 static int pivot_root(const char * new_root
, const char * put_old
)
145 #ifdef __NR_pivot_root
146 return syscall(__NR_pivot_root
, new_root
, put_old
);
153 extern int pivot_root(const char * new_root
, const char * put_old
);
156 /* Define sethostname() if missing from the C library */
157 #ifndef HAVE_SETHOSTNAME
158 static int sethostname(const char * name
, size_t len
)
160 #ifdef __NR_sethostname
161 return syscall(__NR_sethostname
, name
, len
);
169 /* Define __S_ISTYPE if missing from the C library */
171 #define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
174 char *lxchook_names
[NUM_LXC_HOOKS
] = {
175 "pre-start", "pre-mount", "mount", "autodev", "start", "post-stop" };
177 typedef int (*instanciate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
190 static int instanciate_veth(struct lxc_handler
*, struct lxc_netdev
*);
191 static int instanciate_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
192 static int instanciate_vlan(struct lxc_handler
*, struct lxc_netdev
*);
193 static int instanciate_phys(struct lxc_handler
*, struct lxc_netdev
*);
194 static int instanciate_empty(struct lxc_handler
*, struct lxc_netdev
*);
196 static instanciate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
197 [LXC_NET_VETH
] = instanciate_veth
,
198 [LXC_NET_MACVLAN
] = instanciate_macvlan
,
199 [LXC_NET_VLAN
] = instanciate_vlan
,
200 [LXC_NET_PHYS
] = instanciate_phys
,
201 [LXC_NET_EMPTY
] = instanciate_empty
,
204 static int shutdown_veth(struct lxc_handler
*, struct lxc_netdev
*);
205 static int shutdown_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
206 static int shutdown_vlan(struct lxc_handler
*, struct lxc_netdev
*);
207 static int shutdown_phys(struct lxc_handler
*, struct lxc_netdev
*);
208 static int shutdown_empty(struct lxc_handler
*, struct lxc_netdev
*);
210 static instanciate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
211 [LXC_NET_VETH
] = shutdown_veth
,
212 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
213 [LXC_NET_VLAN
] = shutdown_vlan
,
214 [LXC_NET_PHYS
] = shutdown_phys
,
215 [LXC_NET_EMPTY
] = shutdown_empty
,
218 static struct mount_opt mount_opt
[] = {
219 { "defaults", 0, 0 },
220 { "ro", 0, MS_RDONLY
},
221 { "rw", 1, MS_RDONLY
},
222 { "suid", 1, MS_NOSUID
},
223 { "nosuid", 0, MS_NOSUID
},
224 { "dev", 1, MS_NODEV
},
225 { "nodev", 0, MS_NODEV
},
226 { "exec", 1, MS_NOEXEC
},
227 { "noexec", 0, MS_NOEXEC
},
228 { "sync", 0, MS_SYNCHRONOUS
},
229 { "async", 1, MS_SYNCHRONOUS
},
230 { "dirsync", 0, MS_DIRSYNC
},
231 { "remount", 0, MS_REMOUNT
},
232 { "mand", 0, MS_MANDLOCK
},
233 { "nomand", 1, MS_MANDLOCK
},
234 { "atime", 1, MS_NOATIME
},
235 { "noatime", 0, MS_NOATIME
},
236 { "diratime", 1, MS_NODIRATIME
},
237 { "nodiratime", 0, MS_NODIRATIME
},
238 { "bind", 0, MS_BIND
},
239 { "rbind", 0, MS_BIND
|MS_REC
},
240 { "relatime", 0, MS_RELATIME
},
241 { "norelatime", 1, MS_RELATIME
},
242 { "strictatime", 0, MS_STRICTATIME
},
243 { "nostrictatime", 1, MS_STRICTATIME
},
247 #if HAVE_SYS_CAPABILITY_H
248 static struct caps_opt caps_opt
[] = {
249 { "chown", CAP_CHOWN
},
250 { "dac_override", CAP_DAC_OVERRIDE
},
251 { "dac_read_search", CAP_DAC_READ_SEARCH
},
252 { "fowner", CAP_FOWNER
},
253 { "fsetid", CAP_FSETID
},
254 { "kill", CAP_KILL
},
255 { "setgid", CAP_SETGID
},
256 { "setuid", CAP_SETUID
},
257 { "setpcap", CAP_SETPCAP
},
258 { "linux_immutable", CAP_LINUX_IMMUTABLE
},
259 { "net_bind_service", CAP_NET_BIND_SERVICE
},
260 { "net_broadcast", CAP_NET_BROADCAST
},
261 { "net_admin", CAP_NET_ADMIN
},
262 { "net_raw", CAP_NET_RAW
},
263 { "ipc_lock", CAP_IPC_LOCK
},
264 { "ipc_owner", CAP_IPC_OWNER
},
265 { "sys_module", CAP_SYS_MODULE
},
266 { "sys_rawio", CAP_SYS_RAWIO
},
267 { "sys_chroot", CAP_SYS_CHROOT
},
268 { "sys_ptrace", CAP_SYS_PTRACE
},
269 { "sys_pacct", CAP_SYS_PACCT
},
270 { "sys_admin", CAP_SYS_ADMIN
},
271 { "sys_boot", CAP_SYS_BOOT
},
272 { "sys_nice", CAP_SYS_NICE
},
273 { "sys_resource", CAP_SYS_RESOURCE
},
274 { "sys_time", CAP_SYS_TIME
},
275 { "sys_tty_config", CAP_SYS_TTY_CONFIG
},
276 { "mknod", CAP_MKNOD
},
277 { "lease", CAP_LEASE
},
278 #ifdef CAP_AUDIT_WRITE
279 { "audit_write", CAP_AUDIT_WRITE
},
281 #ifdef CAP_AUDIT_CONTROL
282 { "audit_control", CAP_AUDIT_CONTROL
},
284 { "setfcap", CAP_SETFCAP
},
285 { "mac_override", CAP_MAC_OVERRIDE
},
286 { "mac_admin", CAP_MAC_ADMIN
},
288 { "syslog", CAP_SYSLOG
},
290 #ifdef CAP_WAKE_ALARM
291 { "wake_alarm", CAP_WAKE_ALARM
},
295 static struct caps_opt caps_opt
[] = {};
298 static int run_buffer(char *buffer
)
303 f
= popen(buffer
, "r");
305 SYSERROR("popen failed");
309 output
= malloc(LXC_LOG_BUFFER_SIZE
);
311 ERROR("failed to allocate memory for script output");
316 while(fgets(output
, LXC_LOG_BUFFER_SIZE
, f
))
317 DEBUG("script output: %s", output
);
321 if (pclose(f
) == -1) {
322 SYSERROR("Script exited on error");
329 static int run_script(const char *name
, const char *section
,
330 const char *script
, ...)
337 INFO("Executing script '%s' for container '%s', config section '%s'",
338 script
, name
, section
);
340 va_start(ap
, script
);
341 while ((p
= va_arg(ap
, char *)))
342 size
+= strlen(p
) + 1;
345 size
+= strlen(script
);
346 size
+= strlen(name
);
347 size
+= strlen(section
);
353 buffer
= alloca(size
);
355 ERROR("failed to allocate memory");
359 ret
= snprintf(buffer
, size
, "%s %s %s", script
, name
, section
);
360 if (ret
< 0 || ret
>= size
) {
361 ERROR("Script name too long");
365 va_start(ap
, script
);
366 while ((p
= va_arg(ap
, char *))) {
369 rc
= snprintf(buffer
+ ret
, len
, " %s", p
);
370 if (rc
< 0 || rc
>= len
) {
371 ERROR("Script args too long");
378 return run_buffer(buffer
);
381 static int find_fstype_cb(char* buffer
, void *data
)
391 /* we don't try 'nodev' entries */
392 if (strstr(buffer
, "nodev"))
396 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
397 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
399 DEBUG("trying to mount '%s'->'%s' with fstype '%s'",
400 cbarg
->rootfs
, cbarg
->target
, fstype
);
402 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, cbarg
->mntopt
, NULL
)) {
403 DEBUG("mount failed with error: %s", strerror(errno
));
407 INFO("mounted '%s' on '%s', with fstype '%s'",
408 cbarg
->rootfs
, cbarg
->target
, fstype
);
413 static int mount_unknow_fs(const char *rootfs
, const char *target
, int mntopt
)
428 * find the filesystem type with brute force:
429 * first we check with /etc/filesystems, in case the modules
430 * are auto-loaded and fall back to the supported kernel fs
437 for (i
= 0; i
< sizeof(fsfile
)/sizeof(fsfile
[0]); i
++) {
441 if (access(fsfile
[i
], F_OK
))
444 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
446 ERROR("failed to parse '%s'", fsfile
[i
]);
454 ERROR("failed to determine fs type for '%s'", rootfs
);
458 static int mount_rootfs_dir(const char *rootfs
, const char *target
)
460 return mount(rootfs
, target
, "none", MS_BIND
| MS_REC
, NULL
);
463 static int setup_lodev(const char *rootfs
, int fd
, struct loop_info64
*loinfo
)
468 rfd
= open(rootfs
, O_RDWR
);
470 SYSERROR("failed to open '%s'", rootfs
);
474 memset(loinfo
, 0, sizeof(*loinfo
));
476 loinfo
->lo_flags
= LO_FLAGS_AUTOCLEAR
;
478 if (ioctl(fd
, LOOP_SET_FD
, rfd
)) {
479 SYSERROR("failed to LOOP_SET_FD");
483 if (ioctl(fd
, LOOP_SET_STATUS64
, loinfo
)) {
484 SYSERROR("failed to LOOP_SET_STATUS64");
495 static int mount_rootfs_file(const char *rootfs
, const char *target
)
497 struct dirent dirent
, *direntp
;
498 struct loop_info64 loinfo
;
499 int ret
= -1, fd
= -1, rc
;
501 char path
[MAXPATHLEN
];
503 dir
= opendir("/dev");
505 SYSERROR("failed to open '/dev'");
509 while (!readdir_r(dir
, &dirent
, &direntp
)) {
514 if (!strcmp(direntp
->d_name
, "."))
517 if (!strcmp(direntp
->d_name
, ".."))
520 if (strncmp(direntp
->d_name
, "loop", 4))
523 rc
= snprintf(path
, MAXPATHLEN
, "/dev/%s", direntp
->d_name
);
524 if (rc
< 0 || rc
>= MAXPATHLEN
)
527 fd
= open(path
, O_RDWR
);
531 if (ioctl(fd
, LOOP_GET_STATUS64
, &loinfo
) == 0) {
536 if (errno
!= ENXIO
) {
537 WARN("unexpected error for ioctl on '%s': %m",
543 DEBUG("found '%s' free lodev", path
);
545 ret
= setup_lodev(rootfs
, fd
, &loinfo
);
547 ret
= mount_unknow_fs(path
, target
, 0);
554 WARN("failed to close directory");
559 static int mount_rootfs_block(const char *rootfs
, const char *target
)
561 return mount_unknow_fs(rootfs
, target
, 0);
566 * if rootfs is a directory, then open ${rootfs}.hold for writing for the
567 * duration of the container run, to prevent the container from marking the
568 * underlying fs readonly on shutdown.
569 * return -1 on error.
570 * return -2 if nothing needed to be pinned.
571 * return an open fd (>=0) if we pinned it.
573 int pin_rootfs(const char *rootfs
)
575 char absrootfs
[MAXPATHLEN
];
576 char absrootfspin
[MAXPATHLEN
];
580 if (rootfs
== NULL
|| strlen(rootfs
) == 0)
583 if (!realpath(rootfs
, absrootfs
)) {
584 SYSERROR("failed to get real path for '%s'", rootfs
);
588 if (access(absrootfs
, F_OK
)) {
589 SYSERROR("'%s' is not accessible", absrootfs
);
593 if (stat(absrootfs
, &s
)) {
594 SYSERROR("failed to stat '%s'", absrootfs
);
598 if (!S_ISDIR(s
.st_mode
))
601 ret
= snprintf(absrootfspin
, MAXPATHLEN
, "%s%s", absrootfs
, ".hold");
602 if (ret
>= MAXPATHLEN
) {
603 SYSERROR("pathname too long for rootfs hold file");
607 fd
= open(absrootfspin
, O_CREAT
| O_RDWR
, S_IWUSR
|S_IRUSR
);
608 INFO("opened %s as fd %d\n", absrootfspin
, fd
);
612 static int mount_rootfs(const char *rootfs
, const char *target
)
614 char absrootfs
[MAXPATHLEN
];
618 typedef int (*rootfs_cb
)(const char *, const char *);
624 { S_IFDIR
, mount_rootfs_dir
},
625 { S_IFBLK
, mount_rootfs_block
},
626 { S_IFREG
, mount_rootfs_file
},
629 if (!realpath(rootfs
, absrootfs
)) {
630 SYSERROR("failed to get real path for '%s'", rootfs
);
634 if (access(absrootfs
, F_OK
)) {
635 SYSERROR("'%s' is not accessible", absrootfs
);
639 if (stat(absrootfs
, &s
)) {
640 SYSERROR("failed to stat '%s'", absrootfs
);
644 for (i
= 0; i
< sizeof(rtfs_type
)/sizeof(rtfs_type
[0]); i
++) {
646 if (!__S_ISTYPE(s
.st_mode
, rtfs_type
[i
].type
))
649 return rtfs_type
[i
].cb(absrootfs
, target
);
652 ERROR("unsupported rootfs type for '%s'", absrootfs
);
656 static int setup_utsname(struct utsname
*utsname
)
661 if (sethostname(utsname
->nodename
, strlen(utsname
->nodename
))) {
662 SYSERROR("failed to set the hostname to '%s'", utsname
->nodename
);
666 INFO("'%s' hostname has been setup", utsname
->nodename
);
671 static int setup_tty(const struct lxc_rootfs
*rootfs
,
672 const struct lxc_tty_info
*tty_info
, char *ttydir
)
674 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
680 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
682 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
684 ret
= snprintf(path
, sizeof(path
), "%s/dev/tty%d",
685 rootfs
->mount
, i
+ 1);
686 if (ret
>= sizeof(path
)) {
687 ERROR("pathname too long for ttys");
691 /* create dev/lxc/tty%d" */
692 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/tty%d",
693 rootfs
->mount
, ttydir
, i
+ 1);
694 if (ret
>= sizeof(lxcpath
)) {
695 ERROR("pathname too long for ttys");
698 ret
= creat(lxcpath
, 0660);
699 if (ret
==-1 && errno
!= EEXIST
) {
700 SYSERROR("error creating %s\n", lxcpath
);
706 if (ret
&& errno
!= ENOENT
) {
707 SYSERROR("error unlinking %s\n", path
);
711 if (mount(pty_info
->name
, lxcpath
, "none", MS_BIND
, 0)) {
712 WARN("failed to mount '%s'->'%s'",
713 pty_info
->name
, path
);
717 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/tty%d", ttydir
, i
+1);
718 if (ret
>= sizeof(lxcpath
)) {
719 ERROR("tty pathname too long");
722 ret
= symlink(lxcpath
, path
);
724 SYSERROR("failed to create symlink for tty %d\n", i
+1);
728 /* If we populated /dev, then we need to create /dev/ttyN */
729 if (access(path
, F_OK
)) {
730 ret
= creat(path
, 0660);
732 SYSERROR("error creating %s\n", path
);
733 /* this isn't fatal, continue */
737 if (mount(pty_info
->name
, path
, "none", MS_BIND
, 0)) {
738 WARN("failed to mount '%s'->'%s'",
739 pty_info
->name
, path
);
745 INFO("%d tty(s) has been setup", tty_info
->nbtty
);
750 static int setup_rootfs_pivot_root_cb(char *buffer
, void *data
)
752 struct lxc_list
*mountlist
, *listentry
, *iterator
;
753 char *pivotdir
, *mountpoint
, *mountentry
, *saveptr
;
758 cbparm
= (void **)data
;
760 mountlist
= cbparm
[0];
761 pivotdir
= cbparm
[1];
763 /* parse entry, first field is mountname, ignore */
764 mountpoint
= strtok_r(mountentry
, " ", &saveptr
);
768 /* second field is mountpoint */
769 mountpoint
= strtok_r(NULL
, " ", &saveptr
);
773 /* only consider mountpoints below old root fs */
774 if (strncmp(mountpoint
, pivotdir
, strlen(pivotdir
)))
777 /* filter duplicate mountpoints */
779 lxc_list_for_each(iterator
, mountlist
) {
780 if (!strcmp(iterator
->elem
, mountpoint
)) {
788 /* add entry to list */
789 listentry
= malloc(sizeof(*listentry
));
791 SYSERROR("malloc for mountpoint listentry failed");
795 listentry
->elem
= strdup(mountpoint
);
796 if (!listentry
->elem
) {
797 SYSERROR("strdup failed");
801 lxc_list_add_tail(mountlist
, listentry
);
806 static int umount_oldrootfs(const char *oldrootfs
)
808 char path
[MAXPATHLEN
];
810 struct lxc_list mountlist
, *iterator
, *next
;
811 int ok
, still_mounted
, last_still_mounted
;
814 /* read and parse /proc/mounts in old root fs */
815 lxc_list_init(&mountlist
);
817 /* oldrootfs is on the top tree directory now */
818 rc
= snprintf(path
, sizeof(path
), "/%s", oldrootfs
);
819 if (rc
>= sizeof(path
)) {
820 ERROR("rootfs name too long");
823 cbparm
[0] = &mountlist
;
825 cbparm
[1] = strdup(path
);
827 SYSERROR("strdup failed");
831 rc
= snprintf(path
, sizeof(path
), "%s/proc/mounts", oldrootfs
);
832 if (rc
>= sizeof(path
)) {
833 ERROR("container proc/mounts name too long");
837 ok
= lxc_file_for_each_line(path
,
838 setup_rootfs_pivot_root_cb
, &cbparm
);
840 SYSERROR("failed to read or parse mount list '%s'", path
);
844 /* umount filesystems until none left or list no longer shrinks */
847 last_still_mounted
= still_mounted
;
850 lxc_list_for_each_safe(iterator
, &mountlist
, next
) {
852 /* umount normally */
853 if (!umount(iterator
->elem
)) {
854 DEBUG("umounted '%s'", (char *)iterator
->elem
);
855 lxc_list_del(iterator
);
862 } while (still_mounted
> 0 && still_mounted
!= last_still_mounted
);
865 lxc_list_for_each(iterator
, &mountlist
) {
867 /* let's try a lazy umount */
868 if (!umount2(iterator
->elem
, MNT_DETACH
)) {
869 INFO("lazy unmount of '%s'", (char *)iterator
->elem
);
873 /* be more brutal (nfs) */
874 if (!umount2(iterator
->elem
, MNT_FORCE
)) {
875 INFO("forced unmount of '%s'", (char *)iterator
->elem
);
879 WARN("failed to unmount '%s'", (char *)iterator
->elem
);
885 static int setup_rootfs_pivot_root(const char *rootfs
, const char *pivotdir
)
887 char path
[MAXPATHLEN
];
888 int remove_pivotdir
= 0;
891 /* change into new root fs */
893 SYSERROR("can't chdir to new rootfs '%s'", rootfs
);
898 pivotdir
= "lxc_putold";
900 /* compute the full path to pivotdir under rootfs */
901 rc
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, pivotdir
);
902 if (rc
>= sizeof(path
)) {
903 ERROR("pivot dir name too long");
907 if (access(path
, F_OK
)) {
909 if (mkdir_p(path
, 0755)) {
910 SYSERROR("failed to create pivotdir '%s'", path
);
915 DEBUG("created '%s' directory", path
);
918 DEBUG("mountpoint for old rootfs is '%s'", path
);
920 /* pivot_root into our new root fs */
921 if (pivot_root(".", path
)) {
922 SYSERROR("pivot_root syscall failed");
927 SYSERROR("can't chdir to / after pivot_root");
931 DEBUG("pivot_root syscall to '%s' successful", rootfs
);
933 /* we switch from absolute path to relative path */
934 if (umount_oldrootfs(pivotdir
))
937 /* remove temporary mount point, we don't consider the removing
939 if (remove_pivotdir
&& rmdir(pivotdir
))
940 WARN("can't remove mountpoint '%s': %m", pivotdir
);
946 * Do we want to add options for max size of /dev and a file to
947 * specify which devices to create?
949 static int mount_autodev(char *root
)
952 char path
[MAXPATHLEN
];
954 INFO("Mounting /dev under %s\n", root
);
955 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
956 if (ret
< 0 || ret
> MAXPATHLEN
)
958 ret
= mount("none", path
, "tmpfs", 0, "size=100000");
960 SYSERROR("Failed to mount /dev at %s\n", root
);
963 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/pts", root
);
964 if (ret
< 0 || ret
>= MAXPATHLEN
)
966 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
968 SYSERROR("Failed to create /dev/pts in container");
972 INFO("Mounted /dev under %s\n", root
);
983 struct lxc_devs lxc_devs
[] = {
984 { "null", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 3 },
985 { "zero", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 5 },
986 { "full", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 7 },
987 { "urandom", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 9 },
988 { "random", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 8 },
989 { "tty", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 5, 0 },
990 { "console", S_IFCHR
| S_IRUSR
| S_IWUSR
, 5, 1 },
993 static int setup_autodev(char *root
)
997 char path
[MAXPATHLEN
];
1001 INFO("Creating initial consoles under %s/dev\n", root
);
1003 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
1004 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1005 ERROR("Error calculating container /dev location");
1009 INFO("Populating /dev under %s\n", root
);
1010 cmask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1011 for (i
= 0; i
< sizeof(lxc_devs
) / sizeof(lxc_devs
[0]); i
++) {
1013 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/%s", root
, d
->name
);
1014 if (ret
< 0 || ret
>= MAXPATHLEN
)
1016 ret
= mknod(path
, d
->mode
, makedev(d
->maj
, d
->min
));
1017 if (ret
&& errno
!= EEXIST
) {
1018 SYSERROR("Error creating %s\n", d
->name
);
1024 INFO("Populated /dev under %s\n", root
);
1029 * Detect whether / is mounted MS_SHARED. The only way I know of to
1030 * check that is through /proc/self/mountinfo.
1031 * I'm only checking for /. If the container rootfs or mount location
1032 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1033 * out would be too much work to be worth it.
1035 #define LINELEN 4096
1036 int detect_shared_rootfs(void)
1038 char buf
[LINELEN
], *p
;
1043 f
= fopen("/proc/self/mountinfo", "r");
1046 while ((p
= fgets(buf
, LINELEN
, f
))) {
1047 INFO("looking at .%s.", p
);
1048 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1049 p
= index(p
+1, ' ');
1052 p2
= index(p
+1, ' ');
1056 INFO("now p is .%s.", p
);
1057 if (strcmp(p
+1, "/") == 0) {
1058 // this is '/'. is it shared?
1059 p
= index(p2
+1, ' ');
1060 if (strstr(p
, "shared:")) {
1071 * I'll forgive you for asking whether all of this is needed :) The
1073 * pivot_root will fail if the new root, the put_old dir, or the parent
1074 * of current->fs->root are MS_SHARED. (parent of current->fs_root may
1075 * or may not be current->fs_root - if we assumed it always was, we could
1076 * just mount --make-rslave /). So,
1077 * 1. mount a tiny tmpfs to be parent of current->fs->root.
1078 * 2. make that MS_SLAVE
1079 * 3. make a 'root' directory under that
1080 * 4. mount --rbind / under the $tinyroot/root.
1081 * 5. make that rslave
1082 * 6. chdir and chroot into $tinyroot/root
1083 * 7. $tinyroot will be unmounted by our parent in start.c
1085 static int chroot_into_slave(struct lxc_conf
*conf
)
1087 char path
[MAXPATHLEN
];
1088 const char *destpath
= conf
->rootfs
.mount
;
1091 if (mount(destpath
, destpath
, NULL
, MS_BIND
, 0)) {
1092 SYSERROR("failed to mount %s bind", destpath
);
1095 if (mount("", destpath
, NULL
, MS_SLAVE
, 0)) {
1096 SYSERROR("failed to make %s slave", destpath
);
1099 if (mount("none", destpath
, "tmpfs", 0, "size=10000")) {
1100 SYSERROR("Failed to mount tmpfs / at %s", destpath
);
1103 ret
= snprintf(path
, MAXPATHLEN
, "%s/root", destpath
);
1104 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1105 ERROR("out of memory making root path");
1108 if (mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)) {
1109 SYSERROR("Failed to create /dev/pts in container");
1112 if (mount("/", path
, NULL
, MS_BIND
|MS_REC
, 0)) {
1113 SYSERROR("Failed to rbind mount / to %s", path
);
1116 if (mount("", destpath
, NULL
, MS_SLAVE
|MS_REC
, 0)) {
1117 SYSERROR("Failed to make tmp-/ at %s rslave", path
);
1121 SYSERROR("Failed to chdir into tmp-/");
1125 SYSERROR("Failed to chroot into tmp-/");
1128 INFO("Chrooted into tmp-/ at %s\n", path
);
1132 static int setup_rootfs(struct lxc_conf
*conf
)
1134 const struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1136 if (!rootfs
->path
) {
1137 if (mount("", "/", NULL
, MS_SLAVE
|MS_REC
, 0)) {
1138 SYSERROR("Failed to make / rslave");
1144 if (access(rootfs
->mount
, F_OK
)) {
1145 SYSERROR("failed to access to '%s', check it is present",
1150 if (detect_shared_rootfs()) {
1151 if (chroot_into_slave(conf
)) {
1152 ERROR("Failed to chroot into slave /");
1157 if (mount_rootfs(rootfs
->path
, rootfs
->mount
)) {
1158 ERROR("failed to mount rootfs");
1162 DEBUG("mounted '%s' on '%s'", rootfs
->path
, rootfs
->mount
);
1167 int setup_pivot_root(const struct lxc_rootfs
*rootfs
)
1172 if (setup_rootfs_pivot_root(rootfs
->mount
, rootfs
->pivot
)) {
1173 ERROR("failed to setup pivot root");
1180 static int setup_pts(int pts
)
1182 char target
[PATH_MAX
];
1187 if (!access("/dev/pts/ptmx", F_OK
) && umount("/dev/pts")) {
1188 SYSERROR("failed to umount 'dev/pts'");
1192 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
,
1193 "newinstance,ptmxmode=0666")) {
1194 SYSERROR("failed to mount a new instance of '/dev/pts'");
1198 if (access("/dev/ptmx", F_OK
)) {
1199 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
1201 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
1205 if (realpath("/dev/ptmx", target
) && !strcmp(target
, "/dev/pts/ptmx"))
1208 /* fallback here, /dev/pts/ptmx exists just mount bind */
1209 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND
, 0)) {
1210 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
1214 INFO("created new pts instance");
1220 static int setup_personality(int persona
)
1222 #if HAVE_SYS_PERSONALITY_H
1226 if (personality(persona
) < 0) {
1227 SYSERROR("failed to set personality to '0x%x'", persona
);
1231 INFO("set personality to '0x%x'", persona
);
1237 static int setup_dev_console(const struct lxc_rootfs
*rootfs
,
1238 const struct lxc_console
*console
)
1240 char path
[MAXPATHLEN
];
1244 ret
= snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1245 if (ret
>= sizeof(path
)) {
1246 ERROR("console path too long\n");
1250 if (access(path
, F_OK
)) {
1251 WARN("rootfs specified but no console found at '%s'", path
);
1255 if (console
->peer
== -1) {
1256 INFO("no console output required");
1260 if (stat(path
, &s
)) {
1261 SYSERROR("failed to stat '%s'", path
);
1265 if (chmod(console
->name
, s
.st_mode
)) {
1266 SYSERROR("failed to set mode '0%o' to '%s'",
1267 s
.st_mode
, console
->name
);
1271 if (mount(console
->name
, path
, "none", MS_BIND
, 0)) {
1272 ERROR("failed to mount '%s' on '%s'", console
->name
, path
);
1276 INFO("console has been setup");
1280 static int setup_ttydir_console(const struct lxc_rootfs
*rootfs
,
1281 const struct lxc_console
*console
,
1284 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
1287 /* create rootfs/dev/<ttydir> directory */
1288 ret
= snprintf(path
, sizeof(path
), "%s/dev/%s", rootfs
->mount
,
1290 if (ret
>= sizeof(path
))
1292 ret
= mkdir(path
, 0755);
1293 if (ret
&& errno
!= EEXIST
) {
1294 SYSERROR("failed with errno %d to create %s\n", errno
, path
);
1297 INFO("created %s\n", path
);
1299 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/console",
1300 rootfs
->mount
, ttydir
);
1301 if (ret
>= sizeof(lxcpath
)) {
1302 ERROR("console path too long\n");
1306 snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1308 if (ret
&& errno
!= ENOENT
) {
1309 SYSERROR("error unlinking %s\n", path
);
1313 ret
= creat(lxcpath
, 0660);
1314 if (ret
==-1 && errno
!= EEXIST
) {
1315 SYSERROR("error %d creating %s\n", errno
, lxcpath
);
1321 if (console
->peer
== -1) {
1322 INFO("no console output required");
1326 if (mount(console
->name
, lxcpath
, "none", MS_BIND
, 0)) {
1327 ERROR("failed to mount '%s' on '%s'", console
->name
, lxcpath
);
1331 /* create symlink from rootfs/dev/console to 'lxc/console' */
1332 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/console", ttydir
);
1333 if (ret
>= sizeof(lxcpath
)) {
1334 ERROR("lxc/console path too long");
1337 ret
= symlink(lxcpath
, path
);
1339 SYSERROR("failed to create symlink for console");
1343 INFO("console has been setup on %s", lxcpath
);
1348 static int setup_console(const struct lxc_rootfs
*rootfs
,
1349 const struct lxc_console
*console
,
1352 /* We don't have a rootfs, /dev/console will be shared */
1356 return setup_dev_console(rootfs
, console
);
1358 return setup_ttydir_console(rootfs
, console
, ttydir
);
1361 static int setup_kmsg(const struct lxc_rootfs
*rootfs
,
1362 const struct lxc_console
*console
)
1364 char kpath
[MAXPATHLEN
];
1369 ret
= snprintf(kpath
, sizeof(kpath
), "%s/dev/kmsg", rootfs
->mount
);
1370 if (ret
< 0 || ret
>= sizeof(kpath
))
1373 ret
= unlink(kpath
);
1374 if (ret
&& errno
!= ENOENT
) {
1375 SYSERROR("error unlinking %s\n", kpath
);
1379 ret
= symlink("console", kpath
);
1381 SYSERROR("failed to create symlink for kmsg");
1388 int setup_cgroup(const char *cgpath
, struct lxc_list
*cgroups
)
1390 struct lxc_list
*iterator
;
1391 struct lxc_cgroup
*cg
;
1394 if (lxc_list_empty(cgroups
))
1397 lxc_list_for_each(iterator
, cgroups
) {
1399 cg
= iterator
->elem
;
1401 if (lxc_cgroup_set_bypath(cgpath
, cg
->subsystem
, cg
->value
)) {
1402 ERROR("Error setting %s to %s for %s\n", cg
->subsystem
,
1407 DEBUG("cgroup '%s' set to '%s'", cg
->subsystem
, cg
->value
);
1411 INFO("cgroup has been setup");
1416 static void parse_mntopt(char *opt
, unsigned long *flags
, char **data
)
1418 struct mount_opt
*mo
;
1420 /* If opt is found in mount_opt, set or clear flags.
1421 * Otherwise append it to data. */
1423 for (mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
1424 if (!strncmp(opt
, mo
->name
, strlen(mo
->name
))) {
1426 *flags
&= ~mo
->flag
;
1438 static int parse_mntopts(const char *mntopts
, unsigned long *mntflags
,
1442 char *p
, *saveptr
= NULL
;
1450 s
= strdup(mntopts
);
1452 SYSERROR("failed to allocate memory");
1456 data
= malloc(strlen(s
) + 1);
1458 SYSERROR("failed to allocate memory");
1464 for (p
= strtok_r(s
, ",", &saveptr
); p
!= NULL
;
1465 p
= strtok_r(NULL
, ",", &saveptr
))
1466 parse_mntopt(p
, mntflags
, &data
);
1477 static int mount_entry(const char *fsname
, const char *target
,
1478 const char *fstype
, unsigned long mountflags
,
1481 if (mount(fsname
, target
, fstype
, mountflags
& ~MS_REMOUNT
, data
)) {
1482 SYSERROR("failed to mount '%s' on '%s'", fsname
, target
);
1486 if ((mountflags
& MS_REMOUNT
) || (mountflags
& MS_BIND
)) {
1488 DEBUG("remounting %s on %s to respect bind or remount options",
1491 if (mount(fsname
, target
, fstype
,
1492 mountflags
| MS_REMOUNT
, data
)) {
1493 SYSERROR("failed to mount '%s' on '%s'",
1499 DEBUG("mounted '%s' on '%s', type '%s'", fsname
, target
, fstype
);
1504 static inline int mount_entry_on_systemfs(struct mntent
*mntent
)
1506 unsigned long mntflags
;
1510 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1511 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1515 ret
= mount_entry(mntent
->mnt_fsname
, mntent
->mnt_dir
,
1516 mntent
->mnt_type
, mntflags
, mntdata
);
1518 if (hasmntopt(mntent
, "optional") != NULL
)
1526 static int mount_entry_on_absolute_rootfs(struct mntent
*mntent
,
1527 const struct lxc_rootfs
*rootfs
,
1528 const char *lxc_name
)
1531 char path
[MAXPATHLEN
];
1532 unsigned long mntflags
;
1534 int r
, ret
= 0, offset
;
1535 const char *lxcpath
;
1537 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1538 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1542 lxcpath
= default_lxc_path();
1544 ERROR("Out of memory");
1548 /* if rootfs->path is a blockdev path, allow container fstab to
1549 * use $lxcpath/CN/rootfs as the target prefix */
1550 r
= snprintf(path
, MAXPATHLEN
, "%s/%s/rootfs", lxcpath
, lxc_name
);
1551 if (r
< 0 || r
>= MAXPATHLEN
)
1554 aux
= strstr(mntent
->mnt_dir
, path
);
1556 offset
= strlen(path
);
1561 aux
= strstr(mntent
->mnt_dir
, rootfs
->path
);
1563 WARN("ignoring mount point '%s'", mntent
->mnt_dir
);
1566 offset
= strlen(rootfs
->path
);
1570 r
= snprintf(path
, MAXPATHLEN
, "%s/%s", rootfs
->mount
,
1572 if (r
< 0 || r
>= MAXPATHLEN
) {
1573 WARN("pathnme too long for '%s'", mntent
->mnt_dir
);
1579 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1582 if (hasmntopt(mntent
, "optional") != NULL
)
1590 static int mount_entry_on_relative_rootfs(struct mntent
*mntent
,
1593 char path
[MAXPATHLEN
];
1594 unsigned long mntflags
;
1598 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1599 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1603 /* relative to root mount point */
1604 ret
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, mntent
->mnt_dir
);
1605 if (ret
>= sizeof(path
)) {
1606 ERROR("path name too long");
1610 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1613 if (hasmntopt(mntent
, "optional") != NULL
)
1621 static int mount_file_entries(const struct lxc_rootfs
*rootfs
, FILE *file
,
1622 const char *lxc_name
)
1624 struct mntent
*mntent
;
1627 while ((mntent
= getmntent(file
))) {
1629 if (!rootfs
->path
) {
1630 if (mount_entry_on_systemfs(mntent
))
1635 /* We have a separate root, mounts are relative to it */
1636 if (mntent
->mnt_dir
[0] != '/') {
1637 if (mount_entry_on_relative_rootfs(mntent
,
1643 if (mount_entry_on_absolute_rootfs(mntent
, rootfs
, lxc_name
))
1649 INFO("mount points have been setup");
1654 static int setup_mount(const struct lxc_rootfs
*rootfs
, const char *fstab
,
1655 const char *lxc_name
)
1663 file
= setmntent(fstab
, "r");
1665 SYSERROR("failed to use '%s'", fstab
);
1669 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1675 static int setup_mount_entries(const struct lxc_rootfs
*rootfs
, struct lxc_list
*mount
,
1676 const char *lxc_name
)
1679 struct lxc_list
*iterator
;
1685 ERROR("tmpfile error: %m");
1689 lxc_list_for_each(iterator
, mount
) {
1690 mount_entry
= iterator
->elem
;
1691 fprintf(file
, "%s\n", mount_entry
);
1696 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1702 static int setup_caps(struct lxc_list
*caps
)
1704 struct lxc_list
*iterator
;
1709 lxc_list_for_each(iterator
, caps
) {
1711 drop_entry
= iterator
->elem
;
1715 for (i
= 0; i
< sizeof(caps_opt
)/sizeof(caps_opt
[0]); i
++) {
1717 if (strcmp(drop_entry
, caps_opt
[i
].name
))
1720 capid
= caps_opt
[i
].value
;
1725 /* try to see if it's numeric, so the user may specify
1726 * capabilities that the running kernel knows about but
1728 capid
= strtol(drop_entry
, &ptr
, 10);
1729 if (!ptr
|| *ptr
!= '\0' ||
1730 capid
== LONG_MIN
|| capid
== LONG_MAX
)
1731 /* not a valid number */
1733 else if (capid
> lxc_caps_last_cap())
1734 /* we have a number but it's not a valid
1740 ERROR("unknown capability %s", drop_entry
);
1744 DEBUG("drop capability '%s' (%d)", drop_entry
, capid
);
1746 if (prctl(PR_CAPBSET_DROP
, capid
, 0, 0, 0)) {
1747 SYSERROR("failed to remove %s capability", drop_entry
);
1753 DEBUG("capabilities has been setup");
1758 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
1760 struct sockaddr sockaddr
;
1764 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
1766 ERROR("mac address '%s' conversion failed : %s",
1767 hwaddr
, strerror(-ret
));
1771 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1772 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
1774 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1776 ERROR("socket failure : %s", strerror(errno
));
1780 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
1783 ERROR("ioctl failure : %s", strerror(errno
));
1785 DEBUG("mac address '%s' on '%s' has been setup", hwaddr
, ifname
);
1790 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
1792 struct lxc_list
*iterator
;
1793 struct lxc_inetdev
*inetdev
;
1796 lxc_list_for_each(iterator
, ip
) {
1798 inetdev
= iterator
->elem
;
1800 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
1801 &inetdev
->bcast
, inetdev
->prefix
);
1803 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
1804 ifindex
, strerror(-err
));
1812 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
1814 struct lxc_list
*iterator
;
1815 struct lxc_inet6dev
*inet6dev
;
1818 lxc_list_for_each(iterator
, ip
) {
1820 inet6dev
= iterator
->elem
;
1822 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
1823 &inet6dev
->mcast
, &inet6dev
->acast
,
1826 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
1827 ifindex
, strerror(-err
));
1835 static int setup_netdev(struct lxc_netdev
*netdev
)
1837 char ifname
[IFNAMSIZ
];
1838 char *current_ifname
= ifname
;
1841 /* empty network namespace */
1842 if (!netdev
->ifindex
) {
1843 if (netdev
->flags
& IFF_UP
) {
1844 err
= lxc_netdev_up("lo");
1846 ERROR("failed to set the loopback up : %s",
1854 /* retrieve the name of the interface */
1855 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1856 ERROR("no interface corresponding to index '%d'",
1861 /* default: let the system to choose one interface name */
1863 netdev
->name
= netdev
->type
== LXC_NET_PHYS
?
1864 netdev
->link
: "eth%d";
1866 /* rename the interface name */
1867 err
= lxc_netdev_rename_by_name(ifname
, netdev
->name
);
1869 ERROR("failed to rename %s->%s : %s", ifname
, netdev
->name
,
1874 /* Re-read the name of the interface because its name has changed
1875 * and would be automatically allocated by the system
1877 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1878 ERROR("no interface corresponding to index '%d'",
1883 /* set a mac address */
1884 if (netdev
->hwaddr
) {
1885 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
1886 ERROR("failed to setup hw address for '%s'",
1892 /* setup ipv4 addresses on the interface */
1893 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
1894 ERROR("failed to setup ip addresses for '%s'",
1899 /* setup ipv6 addresses on the interface */
1900 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
1901 ERROR("failed to setup ipv6 addresses for '%s'",
1906 /* set the network device up */
1907 if (netdev
->flags
& IFF_UP
) {
1910 err
= lxc_netdev_up(current_ifname
);
1912 ERROR("failed to set '%s' up : %s", current_ifname
,
1917 /* the network is up, make the loopback up too */
1918 err
= lxc_netdev_up("lo");
1920 ERROR("failed to set the loopback up : %s",
1926 /* We can only set up the default routes after bringing
1927 * up the interface, sine bringing up the interface adds
1928 * the link-local routes and we can't add a default
1929 * route if the gateway is not reachable. */
1931 /* setup ipv4 gateway on the interface */
1932 if (netdev
->ipv4_gateway
) {
1933 if (!(netdev
->flags
& IFF_UP
)) {
1934 ERROR("Cannot add ipv4 gateway for %s when not bringing up the interface", ifname
);
1938 if (lxc_list_empty(&netdev
->ipv4
)) {
1939 ERROR("Cannot add ipv4 gateway for %s when not assigning an address", ifname
);
1943 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
1945 ERROR("failed to setup ipv4 gateway for '%s': %s",
1946 ifname
, strerror(-err
));
1947 if (netdev
->ipv4_gateway_auto
) {
1948 char buf
[INET_ADDRSTRLEN
];
1949 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
1950 ERROR("tried to set autodetected ipv4 gateway '%s'", buf
);
1956 /* setup ipv6 gateway on the interface */
1957 if (netdev
->ipv6_gateway
) {
1958 if (!(netdev
->flags
& IFF_UP
)) {
1959 ERROR("Cannot add ipv6 gateway for %s when not bringing up the interface", ifname
);
1963 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
1964 ERROR("Cannot add ipv6 gateway for %s when not assigning an address", ifname
);
1968 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
1970 ERROR("failed to setup ipv6 gateway for '%s': %s",
1971 ifname
, strerror(-err
));
1972 if (netdev
->ipv6_gateway_auto
) {
1973 char buf
[INET6_ADDRSTRLEN
];
1974 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
1975 ERROR("tried to set autodetected ipv6 gateway '%s'", buf
);
1981 DEBUG("'%s' has been setup", current_ifname
);
1986 static int setup_network(struct lxc_list
*network
)
1988 struct lxc_list
*iterator
;
1989 struct lxc_netdev
*netdev
;
1991 lxc_list_for_each(iterator
, network
) {
1993 netdev
= iterator
->elem
;
1995 if (setup_netdev(netdev
)) {
1996 ERROR("failed to setup netdev");
2001 if (!lxc_list_empty(network
))
2002 INFO("network has been setup");
2007 void lxc_rename_phys_nics_on_shutdown(struct lxc_conf
*conf
)
2011 INFO("running to reset %d nic names", conf
->num_savednics
);
2012 for (i
=0; i
<conf
->num_savednics
; i
++) {
2013 struct saved_nic
*s
= &conf
->saved_nics
[i
];
2014 INFO("resetting nic %d to %s\n", s
->ifindex
, s
->orig_name
);
2015 lxc_netdev_rename_by_index(s
->ifindex
, s
->orig_name
);
2018 conf
->num_savednics
= 0;
2019 free(conf
->saved_nics
);
2022 static int setup_private_host_hw_addr(char *veth1
)
2028 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
2032 snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2033 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2039 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2040 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2045 DEBUG("mac address of host interface '%s' changed to private "
2046 "%02x:%02x:%02x:%02x:%02x:%02x", veth1
,
2047 ifr
.ifr_hwaddr
.sa_data
[0] & 0xff,
2048 ifr
.ifr_hwaddr
.sa_data
[1] & 0xff,
2049 ifr
.ifr_hwaddr
.sa_data
[2] & 0xff,
2050 ifr
.ifr_hwaddr
.sa_data
[3] & 0xff,
2051 ifr
.ifr_hwaddr
.sa_data
[4] & 0xff,
2052 ifr
.ifr_hwaddr
.sa_data
[5] & 0xff);
2057 static char *default_rootfs_mount
= LXCROOTFSMOUNT
;
2059 struct lxc_conf
*lxc_conf_init(void)
2061 struct lxc_conf
*new;
2064 new = malloc(sizeof(*new));
2066 ERROR("lxc_conf_init : %m");
2069 memset(new, 0, sizeof(*new));
2071 new->personality
= -1;
2072 new->console
.log_path
= NULL
;
2073 new->console
.log_fd
= -1;
2074 new->console
.path
= NULL
;
2075 new->console
.peer
= -1;
2076 new->console
.master
= -1;
2077 new->console
.slave
= -1;
2078 new->console
.name
[0] = '\0';
2079 new->maincmd_fd
= -1;
2080 new->rootfs
.mount
= default_rootfs_mount
;
2082 lxc_list_init(&new->cgroup
);
2083 lxc_list_init(&new->network
);
2084 lxc_list_init(&new->mount_list
);
2085 lxc_list_init(&new->caps
);
2086 lxc_list_init(&new->id_map
);
2087 for (i
=0; i
<NUM_LXC_HOOKS
; i
++)
2088 lxc_list_init(&new->hooks
[i
]);
2090 new->aa_profile
= NULL
;
2092 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2093 new->lsm_umount_proc
= 0;
2099 static int instanciate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2101 char veth1buf
[IFNAMSIZ
], *veth1
;
2102 char veth2buf
[IFNAMSIZ
], *veth2
;
2105 if (netdev
->priv
.veth_attr
.pair
)
2106 veth1
= netdev
->priv
.veth_attr
.pair
;
2108 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
2109 if (err
>= sizeof(veth1buf
)) { /* can't *really* happen, but... */
2110 ERROR("veth1 name too long");
2113 veth1
= mktemp(veth1buf
);
2114 /* store away for deconf */
2115 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
2118 snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
2119 veth2
= mktemp(veth2buf
);
2121 if (!strlen(veth1
) || !strlen(veth2
)) {
2122 ERROR("failed to allocate a temporary name");
2126 err
= lxc_veth_create(veth1
, veth2
);
2128 ERROR("failed to create %s-%s : %s", veth1
, veth2
,
2133 /* changing the high byte of the mac address to 0xfe, the bridge interface
2134 * will always keep the host's mac address and not take the mac address
2136 err
= setup_private_host_hw_addr(veth1
);
2138 ERROR("failed to change mac address of host interface '%s' : %s",
2139 veth1
, strerror(-err
));
2144 err
= lxc_netdev_set_mtu(veth1
, atoi(netdev
->mtu
));
2146 err
= lxc_netdev_set_mtu(veth2
, atoi(netdev
->mtu
));
2148 ERROR("failed to set mtu '%s' for %s-%s : %s",
2149 netdev
->mtu
, veth1
, veth2
, strerror(-err
));
2155 err
= lxc_bridge_attach(netdev
->link
, veth1
);
2157 ERROR("failed to attach '%s' to the bridge '%s' : %s",
2158 veth1
, netdev
->link
, strerror(-err
));
2163 netdev
->ifindex
= if_nametoindex(veth2
);
2164 if (!netdev
->ifindex
) {
2165 ERROR("failed to retrieve the index for %s", veth2
);
2169 err
= lxc_netdev_up(veth1
);
2171 ERROR("failed to set %s up : %s", veth1
, strerror(-err
));
2175 if (netdev
->upscript
) {
2176 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2177 "veth", veth1
, (char*) NULL
);
2182 DEBUG("instanciated veth '%s/%s', index is '%d'",
2183 veth1
, veth2
, netdev
->ifindex
);
2188 lxc_netdev_delete_by_name(veth1
);
2192 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2197 if (netdev
->priv
.veth_attr
.pair
)
2198 veth1
= netdev
->priv
.veth_attr
.pair
;
2200 veth1
= netdev
->priv
.veth_attr
.veth1
;
2202 if (netdev
->downscript
) {
2203 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2204 "down", "veth", veth1
, (char*) NULL
);
2211 static int instanciate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2213 char peerbuf
[IFNAMSIZ
], *peer
;
2216 if (!netdev
->link
) {
2217 ERROR("no link specified for macvlan netdev");
2221 err
= snprintf(peerbuf
, sizeof(peerbuf
), "mcXXXXXX");
2222 if (err
>= sizeof(peerbuf
))
2225 peer
= mktemp(peerbuf
);
2226 if (!strlen(peer
)) {
2227 ERROR("failed to make a temporary name");
2231 err
= lxc_macvlan_create(netdev
->link
, peer
,
2232 netdev
->priv
.macvlan_attr
.mode
);
2234 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
2235 peer
, netdev
->link
, strerror(-err
));
2239 netdev
->ifindex
= if_nametoindex(peer
);
2240 if (!netdev
->ifindex
) {
2241 ERROR("failed to retrieve the index for %s", peer
);
2242 lxc_netdev_delete_by_name(peer
);
2246 if (netdev
->upscript
) {
2247 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2248 "macvlan", netdev
->link
, (char*) NULL
);
2253 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
2254 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
2259 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2263 if (netdev
->downscript
) {
2264 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2265 "down", "macvlan", netdev
->link
,
2273 /* XXX: merge with instanciate_macvlan */
2274 static int instanciate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2276 char peer
[IFNAMSIZ
];
2279 if (!netdev
->link
) {
2280 ERROR("no link specified for vlan netdev");
2284 err
= snprintf(peer
, sizeof(peer
), "vlan%d", netdev
->priv
.vlan_attr
.vid
);
2285 if (err
>= sizeof(peer
)) {
2286 ERROR("peer name too long");
2290 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
2292 ERROR("failed to create vlan interface '%s' on '%s' : %s",
2293 peer
, netdev
->link
, strerror(-err
));
2297 netdev
->ifindex
= if_nametoindex(peer
);
2298 if (!netdev
->ifindex
) {
2299 ERROR("failed to retrieve the ifindex for %s", peer
);
2300 lxc_netdev_delete_by_name(peer
);
2304 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
2310 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2315 static int instanciate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2317 if (!netdev
->link
) {
2318 ERROR("no link specified for the physical interface");
2322 netdev
->ifindex
= if_nametoindex(netdev
->link
);
2323 if (!netdev
->ifindex
) {
2324 ERROR("failed to retrieve the index for %s", netdev
->link
);
2328 if (netdev
->upscript
) {
2330 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2331 "up", "phys", netdev
->link
, (char*) NULL
);
2339 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2343 if (netdev
->downscript
) {
2344 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2345 "down", "phys", netdev
->link
, (char*) NULL
);
2352 static int instanciate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2354 netdev
->ifindex
= 0;
2355 if (netdev
->upscript
) {
2357 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2358 "up", "empty", (char*) NULL
);
2365 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2369 if (netdev
->downscript
) {
2370 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2371 "down", "empty", (char*) NULL
);
2378 int lxc_create_network(struct lxc_handler
*handler
)
2380 struct lxc_list
*network
= &handler
->conf
->network
;
2381 struct lxc_list
*iterator
;
2382 struct lxc_netdev
*netdev
;
2384 lxc_list_for_each(iterator
, network
) {
2386 netdev
= iterator
->elem
;
2388 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
2389 ERROR("invalid network configuration type '%d'",
2394 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
2395 ERROR("failed to create netdev");
2404 void lxc_delete_network(struct lxc_handler
*handler
)
2406 struct lxc_list
*network
= &handler
->conf
->network
;
2407 struct lxc_list
*iterator
;
2408 struct lxc_netdev
*netdev
;
2410 lxc_list_for_each(iterator
, network
) {
2411 netdev
= iterator
->elem
;
2413 if (netdev
->ifindex
!= 0 && netdev
->type
== LXC_NET_PHYS
) {
2414 if (lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
))
2415 WARN("failed to rename to the initial name the " \
2416 "netdev '%s'", netdev
->link
);
2420 if (netdev_deconf
[netdev
->type
](handler
, netdev
)) {
2421 WARN("failed to destroy netdev");
2424 /* Recent kernel remove the virtual interfaces when the network
2425 * namespace is destroyed but in case we did not moved the
2426 * interface to the network namespace, we have to destroy it
2428 if (netdev
->ifindex
!= 0 &&
2429 lxc_netdev_delete_by_index(netdev
->ifindex
))
2430 WARN("failed to remove interface '%s'", netdev
->name
);
2434 int lxc_assign_network(struct lxc_list
*network
, pid_t pid
)
2436 struct lxc_list
*iterator
;
2437 struct lxc_netdev
*netdev
;
2440 lxc_list_for_each(iterator
, network
) {
2442 netdev
= iterator
->elem
;
2444 /* empty network namespace, nothing to move */
2445 if (!netdev
->ifindex
)
2448 err
= lxc_netdev_move_by_index(netdev
->ifindex
, pid
);
2450 ERROR("failed to move '%s' to the container : %s",
2451 netdev
->link
, strerror(-err
));
2455 DEBUG("move '%s' to '%d'", netdev
->name
, pid
);
2461 static int write_id_mapping(enum idtype idtype
, pid_t pid
, const char *buf
,
2464 char path
[PATH_MAX
];
2468 ret
= snprintf(path
, PATH_MAX
, "/proc/%d/%cid_map", pid
, idtype
== ID_TYPE_UID
? 'u' : 'g');
2469 if (ret
< 0 || ret
>= PATH_MAX
) {
2470 fprintf(stderr
, "%s: path name too long", __func__
);
2473 f
= fopen(path
, "w");
2478 ret
= fwrite(buf
, buf_size
, 1, f
);
2480 SYSERROR("writing id mapping");
2481 closeret
= fclose(f
);
2483 SYSERROR("writing id mapping");
2484 return ret
< 0 ? ret
: closeret
;
2487 int lxc_map_ids(struct lxc_list
*idmap
, pid_t pid
)
2489 struct lxc_list
*iterator
;
2493 char *buf
= NULL
, *pos
;
2495 for(type
= ID_TYPE_UID
; type
<= ID_TYPE_GID
; type
++) {
2499 lxc_list_for_each(iterator
, idmap
) {
2500 /* The kernel only takes <= 4k for writes to /proc/<nr>/[ug]id_map */
2502 buf
= pos
= malloc(4096);
2506 map
= iterator
->elem
;
2507 if (map
->idtype
== type
) {
2508 left
= 4096 - (pos
- buf
);
2509 fill
= snprintf(pos
, left
, "%lu %lu %lu\n",
2510 map
->nsid
, map
->hostid
, map
->range
);
2511 if (fill
<= 0 || fill
>= left
)
2512 SYSERROR("snprintf failed, too many mappings");
2516 if (pos
== buf
) // no mappings were found
2518 ret
= write_id_mapping(type
, pid
, buf
, pos
-buf
);
2528 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2530 struct lxc_list
*network
= &handler
->conf
->network
;
2531 struct lxc_list
*iterator
;
2532 struct lxc_netdev
*netdev
;
2535 lxc_list_for_each(iterator
, network
) {
2536 netdev
= iterator
->elem
;
2538 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2541 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2542 ERROR("gateway = auto only supported for "
2543 "veth and macvlan");
2547 if (!netdev
->link
) {
2548 ERROR("gateway = auto needs a link interface");
2552 link_index
= if_nametoindex(netdev
->link
);
2556 if (netdev
->ipv4_gateway_auto
) {
2557 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2558 ERROR("failed to automatically find ipv4 gateway "
2559 "address from link interface '%s'", netdev
->link
);
2564 if (netdev
->ipv6_gateway_auto
) {
2565 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2566 ERROR("failed to automatically find ipv6 gateway "
2567 "address from link interface '%s'", netdev
->link
);
2576 int lxc_create_tty(const char *name
, struct lxc_conf
*conf
)
2578 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2581 /* no tty in the configuration */
2585 tty_info
->pty_info
=
2586 malloc(sizeof(*tty_info
->pty_info
)*conf
->tty
);
2587 if (!tty_info
->pty_info
) {
2588 SYSERROR("failed to allocate pty_info");
2592 for (i
= 0; i
< conf
->tty
; i
++) {
2594 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2596 if (openpty(&pty_info
->master
, &pty_info
->slave
,
2597 pty_info
->name
, NULL
, NULL
)) {
2598 SYSERROR("failed to create pty #%d", i
);
2599 tty_info
->nbtty
= i
;
2600 lxc_delete_tty(tty_info
);
2604 DEBUG("allocated pty '%s' (%d/%d)",
2605 pty_info
->name
, pty_info
->master
, pty_info
->slave
);
2607 /* Prevent leaking the file descriptors to the container */
2608 fcntl(pty_info
->master
, F_SETFD
, FD_CLOEXEC
);
2609 fcntl(pty_info
->slave
, F_SETFD
, FD_CLOEXEC
);
2614 tty_info
->nbtty
= conf
->tty
;
2616 INFO("tty's configured");
2621 void lxc_delete_tty(struct lxc_tty_info
*tty_info
)
2625 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
2626 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2628 close(pty_info
->master
);
2629 close(pty_info
->slave
);
2632 free(tty_info
->pty_info
);
2633 tty_info
->nbtty
= 0;
2637 * given a host uid, return the ns uid if it is mapped.
2638 * if it is not mapped, return the original host id.
2640 static int shiftid(struct lxc_conf
*c
, int uid
, enum idtype w
)
2642 struct lxc_list
*iterator
;
2646 lxc_list_for_each(iterator
, &c
->id_map
) {
2647 map
= iterator
->elem
;
2648 if (map
->idtype
!= w
)
2652 high
= map
->nsid
+ map
->range
;
2653 if (uid
< low
|| uid
>= high
)
2656 return uid
- low
+ map
->hostid
;
2663 * Take a pathname for a file created on the host, and map the uid and gid
2664 * into the container if needed. (Used for ttys)
2666 static int uid_shift_file(char *path
, struct lxc_conf
*c
)
2668 struct stat statbuf
;
2671 if (stat(path
, &statbuf
)) {
2672 SYSERROR("stat(%s)", path
);
2676 newuid
= shiftid(c
, statbuf
.st_uid
, ID_TYPE_UID
);
2677 newgid
= shiftid(c
, statbuf
.st_gid
, ID_TYPE_GID
);
2678 if (newuid
!= statbuf
.st_uid
|| newgid
!= statbuf
.st_gid
) {
2679 DEBUG("chowning %s from %d:%d to %d:%d\n", path
, (int)statbuf
.st_uid
, (int)statbuf
.st_gid
, newuid
, newgid
);
2680 if (chown(path
, newuid
, newgid
)) {
2681 SYSERROR("chown(%s)", path
);
2688 int uid_shift_ttys(int pid
, struct lxc_conf
*conf
)
2691 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2692 char path
[MAXPATHLEN
];
2693 char *ttydir
= conf
->ttydir
;
2695 if (!conf
->rootfs
.path
)
2697 /* first the console */
2698 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/%s/console", pid
, ttydir
? ttydir
: "");
2699 if (ret
< 0 || ret
>= sizeof(path
)) {
2700 ERROR("console path too long\n");
2703 if (uid_shift_file(path
, conf
)) {
2704 DEBUG("Failed to chown the console %s.\n", path
);
2707 for (i
=0; i
< tty_info
->nbtty
; i
++) {
2708 ret
= snprintf(path
, sizeof(path
), "/proc/%d/root/dev/%s/tty%d",
2709 pid
, ttydir
? ttydir
: "", i
+ 1);
2710 if (ret
< 0 || ret
>= sizeof(path
)) {
2711 ERROR("pathname too long for ttys");
2714 if (uid_shift_file(path
, conf
)) {
2715 DEBUG("Failed to chown pty %s.\n", path
);
2723 int lxc_setup(const char *name
, struct lxc_conf
*lxc_conf
)
2725 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2729 if (setup_utsname(lxc_conf
->utsname
)) {
2730 ERROR("failed to setup the utsname for '%s'", name
);
2734 if (setup_network(&lxc_conf
->network
)) {
2735 ERROR("failed to setup the network for '%s'", name
);
2739 if (run_lxc_hooks(name
, "pre-mount", lxc_conf
)) {
2740 ERROR("failed to run pre-mount hooks for container '%s'.", name
);
2744 if (setup_rootfs(lxc_conf
)) {
2745 ERROR("failed to setup rootfs for '%s'", name
);
2749 if (lxc_conf
->autodev
) {
2750 if (mount_autodev(lxc_conf
->rootfs
.mount
)) {
2751 ERROR("failed to mount /dev in the container");
2756 if (setup_mount(&lxc_conf
->rootfs
, lxc_conf
->fstab
, name
)) {
2757 ERROR("failed to setup the mounts for '%s'", name
);
2761 if (!lxc_list_empty(&lxc_conf
->mount_list
) && setup_mount_entries(&lxc_conf
->rootfs
, &lxc_conf
->mount_list
, name
)) {
2762 ERROR("failed to setup the mount entries for '%s'", name
);
2766 if (run_lxc_hooks(name
, "mount", lxc_conf
)) {
2767 ERROR("failed to run mount hooks for container '%s'.", name
);
2771 if (lxc_conf
->autodev
) {
2772 if (run_lxc_hooks(name
, "autodev", lxc_conf
)) {
2773 ERROR("failed to run autodev hooks for container '%s'.", name
);
2776 if (setup_autodev(lxc_conf
->rootfs
.mount
)) {
2777 ERROR("failed to populate /dev in the container");
2782 if (setup_console(&lxc_conf
->rootfs
, &lxc_conf
->console
, lxc_conf
->ttydir
)) {
2783 ERROR("failed to setup the console for '%s'", name
);
2787 if (lxc_conf
->kmsg
) {
2788 if (setup_kmsg(&lxc_conf
->rootfs
, &lxc_conf
->console
)) // don't fail
2789 ERROR("failed to setup kmsg for '%s'", name
);
2792 if (setup_tty(&lxc_conf
->rootfs
, &lxc_conf
->tty_info
, lxc_conf
->ttydir
)) {
2793 ERROR("failed to setup the ttys for '%s'", name
);
2797 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2798 INFO("rootfs path is .%s., mount is .%s.", lxc_conf
->rootfs
.path
,
2799 lxc_conf
->rootfs
.mount
);
2800 if (lxc_conf
->rootfs
.path
== NULL
|| strlen(lxc_conf
->rootfs
.path
) == 0)
2803 mounted
= lsm_mount_proc_if_needed(lxc_conf
->rootfs
.path
, lxc_conf
->rootfs
.mount
);
2804 if (mounted
== -1) {
2805 SYSERROR("failed to mount /proc in the container.");
2807 } else if (mounted
== 1) {
2808 lxc_conf
->lsm_umount_proc
= 1;
2812 if (setup_pivot_root(&lxc_conf
->rootfs
)) {
2813 ERROR("failed to set rootfs for '%s'", name
);
2817 if (setup_pts(lxc_conf
->pts
)) {
2818 ERROR("failed to setup the new pts instance");
2822 if (setup_personality(lxc_conf
->personality
)) {
2823 ERROR("failed to setup personality");
2827 if (lxc_list_empty(&lxc_conf
->id_map
)) {
2828 if (setup_caps(&lxc_conf
->caps
)) {
2829 ERROR("failed to drop capabilities");
2834 NOTICE("'%s' is setup.", name
);
2839 int run_lxc_hooks(const char *name
, char *hook
, struct lxc_conf
*conf
)
2842 struct lxc_list
*it
;
2844 if (strcmp(hook
, "pre-start") == 0)
2845 which
= LXCHOOK_PRESTART
;
2846 else if (strcmp(hook
, "pre-mount") == 0)
2847 which
= LXCHOOK_PREMOUNT
;
2848 else if (strcmp(hook
, "mount") == 0)
2849 which
= LXCHOOK_MOUNT
;
2850 else if (strcmp(hook
, "autodev") == 0)
2851 which
= LXCHOOK_AUTODEV
;
2852 else if (strcmp(hook
, "start") == 0)
2853 which
= LXCHOOK_START
;
2854 else if (strcmp(hook
, "post-stop") == 0)
2855 which
= LXCHOOK_POSTSTOP
;
2858 lxc_list_for_each(it
, &conf
->hooks
[which
]) {
2860 char *hookname
= it
->elem
;
2861 ret
= run_script(name
, "lxc", hookname
, hook
, NULL
);
2868 static void lxc_remove_nic(struct lxc_list
*it
)
2870 struct lxc_netdev
*netdev
= it
->elem
;
2871 struct lxc_list
*it2
,*next
;
2879 if (netdev
->upscript
)
2880 free(netdev
->upscript
);
2882 free(netdev
->hwaddr
);
2885 if (netdev
->ipv4_gateway
)
2886 free(netdev
->ipv4_gateway
);
2887 if (netdev
->ipv6_gateway
)
2888 free(netdev
->ipv6_gateway
);
2889 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2894 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2903 /* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
2904 int lxc_clear_nic(struct lxc_conf
*c
, const char *key
)
2908 struct lxc_list
*it
;
2909 struct lxc_netdev
*netdev
;
2911 p1
= index(key
, '.');
2912 if (!p1
|| *(p1
+1) == '\0')
2915 ret
= sscanf(key
, "%d", &idx
);
2916 if (ret
!= 1) return -1;
2921 lxc_list_for_each(it
, &c
->network
) {
2926 if (i
< idx
) // we don't have that many nics defined
2929 if (!it
|| !it
->elem
)
2936 } else if (strcmp(p1
, "ipv4") == 0) {
2937 struct lxc_list
*it2
,*next
;
2938 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2943 } else if (strcmp(p1
, "ipv6") == 0) {
2944 struct lxc_list
*it2
,*next
;
2945 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2950 } else if (strcmp(p1
, "link") == 0) {
2953 netdev
->link
= NULL
;
2955 } else if (strcmp(p1
, "name") == 0) {
2958 netdev
->name
= NULL
;
2960 } else if (strcmp(p1
, "script.up") == 0) {
2961 if (netdev
->upscript
) {
2962 free(netdev
->upscript
);
2963 netdev
->upscript
= NULL
;
2965 } else if (strcmp(p1
, "hwaddr") == 0) {
2966 if (netdev
->hwaddr
) {
2967 free(netdev
->hwaddr
);
2968 netdev
->hwaddr
= NULL
;
2970 } else if (strcmp(p1
, "mtu") == 0) {
2975 } else if (strcmp(p1
, "ipv4_gateway") == 0) {
2976 if (netdev
->ipv4_gateway
) {
2977 free(netdev
->ipv4_gateway
);
2978 netdev
->ipv4_gateway
= NULL
;
2980 } else if (strcmp(p1
, "ipv6_gateway") == 0) {
2981 if (netdev
->ipv6_gateway
) {
2982 free(netdev
->ipv6_gateway
);
2983 netdev
->ipv6_gateway
= NULL
;
2991 int lxc_clear_config_network(struct lxc_conf
*c
)
2993 struct lxc_list
*it
,*next
;
2994 lxc_list_for_each_safe(it
, &c
->network
, next
) {
3000 int lxc_clear_config_caps(struct lxc_conf
*c
)
3002 struct lxc_list
*it
,*next
;
3004 lxc_list_for_each_safe(it
, &c
->caps
, next
) {
3012 int lxc_clear_cgroups(struct lxc_conf
*c
, const char *key
)
3014 struct lxc_list
*it
,*next
;
3016 const char *k
= key
+ 11;
3018 if (strcmp(key
, "lxc.cgroup") == 0)
3021 lxc_list_for_each_safe(it
, &c
->cgroup
, next
) {
3022 struct lxc_cgroup
*cg
= it
->elem
;
3023 if (!all
&& strcmp(cg
->subsystem
, k
) != 0)
3026 free(cg
->subsystem
);
3034 int lxc_clear_mount_entries(struct lxc_conf
*c
)
3036 struct lxc_list
*it
,*next
;
3038 lxc_list_for_each_safe(it
, &c
->mount_list
, next
) {
3046 int lxc_clear_hooks(struct lxc_conf
*c
, const char *key
)
3048 struct lxc_list
*it
,*next
;
3049 bool all
= false, done
= false;
3050 const char *k
= key
+ 9;
3053 if (strcmp(key
, "lxc.hook") == 0)
3056 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
3057 if (all
|| strcmp(k
, lxchook_names
[i
]) == 0) {
3058 lxc_list_for_each_safe(it
, &c
->hooks
[i
], next
) {
3068 ERROR("Invalid hook key: %s", key
);
3074 void lxc_clear_saved_nics(struct lxc_conf
*conf
)
3078 if (!conf
->num_savednics
)
3080 for (i
=0; i
< conf
->num_savednics
; i
++)
3081 free(conf
->saved_nics
[i
].orig_name
);
3082 conf
->saved_nics
= 0;
3083 free(conf
->saved_nics
);
3086 void lxc_conf_free(struct lxc_conf
*conf
)
3090 if (conf
->console
.path
)
3091 free(conf
->console
.path
);
3092 if (conf
->rootfs
.mount
!= default_rootfs_mount
)
3093 free(conf
->rootfs
.mount
);
3094 if (conf
->rootfs
.path
)
3095 free(conf
->rootfs
.path
);
3097 free(conf
->utsname
);
3102 lxc_clear_config_network(conf
);
3104 if (conf
->aa_profile
)
3105 free(conf
->aa_profile
);
3107 lxc_seccomp_free(conf
);
3108 lxc_clear_config_caps(conf
);
3109 lxc_clear_cgroups(conf
, "lxc.cgroup");
3110 lxc_clear_hooks(conf
, "lxc.hook");
3111 lxc_clear_mount_entries(conf
);
3112 lxc_clear_saved_nics(conf
);