2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 char *lxchook_names
[NUM_LXC_HOOKS
] = {
170 "pre-start", "pre-mount", "mount", "start", "post-stop" };
172 typedef int (*instanciate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
185 static int instanciate_veth(struct lxc_handler
*, struct lxc_netdev
*);
186 static int instanciate_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
187 static int instanciate_vlan(struct lxc_handler
*, struct lxc_netdev
*);
188 static int instanciate_phys(struct lxc_handler
*, struct lxc_netdev
*);
189 static int instanciate_empty(struct lxc_handler
*, struct lxc_netdev
*);
191 static instanciate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
192 [LXC_NET_VETH
] = instanciate_veth
,
193 [LXC_NET_MACVLAN
] = instanciate_macvlan
,
194 [LXC_NET_VLAN
] = instanciate_vlan
,
195 [LXC_NET_PHYS
] = instanciate_phys
,
196 [LXC_NET_EMPTY
] = instanciate_empty
,
199 static int shutdown_veth(struct lxc_handler
*, struct lxc_netdev
*);
200 static int shutdown_macvlan(struct lxc_handler
*, struct lxc_netdev
*);
201 static int shutdown_vlan(struct lxc_handler
*, struct lxc_netdev
*);
202 static int shutdown_phys(struct lxc_handler
*, struct lxc_netdev
*);
203 static int shutdown_empty(struct lxc_handler
*, struct lxc_netdev
*);
205 static instanciate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
206 [LXC_NET_VETH
] = shutdown_veth
,
207 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
208 [LXC_NET_VLAN
] = shutdown_vlan
,
209 [LXC_NET_PHYS
] = shutdown_phys
,
210 [LXC_NET_EMPTY
] = shutdown_empty
,
213 static struct mount_opt mount_opt
[] = {
214 { "defaults", 0, 0 },
215 { "ro", 0, MS_RDONLY
},
216 { "rw", 1, MS_RDONLY
},
217 { "suid", 1, MS_NOSUID
},
218 { "nosuid", 0, MS_NOSUID
},
219 { "dev", 1, MS_NODEV
},
220 { "nodev", 0, MS_NODEV
},
221 { "exec", 1, MS_NOEXEC
},
222 { "noexec", 0, MS_NOEXEC
},
223 { "sync", 0, MS_SYNCHRONOUS
},
224 { "async", 1, MS_SYNCHRONOUS
},
225 { "dirsync", 0, MS_DIRSYNC
},
226 { "remount", 0, MS_REMOUNT
},
227 { "mand", 0, MS_MANDLOCK
},
228 { "nomand", 1, MS_MANDLOCK
},
229 { "atime", 1, MS_NOATIME
},
230 { "noatime", 0, MS_NOATIME
},
231 { "diratime", 1, MS_NODIRATIME
},
232 { "nodiratime", 0, MS_NODIRATIME
},
233 { "bind", 0, MS_BIND
},
234 { "rbind", 0, MS_BIND
|MS_REC
},
235 { "relatime", 0, MS_RELATIME
},
236 { "norelatime", 1, MS_RELATIME
},
237 { "strictatime", 0, MS_STRICTATIME
},
238 { "nostrictatime", 1, MS_STRICTATIME
},
242 #if HAVE_SYS_CAPABILITY_H
243 static struct caps_opt caps_opt
[] = {
244 { "chown", CAP_CHOWN
},
245 { "dac_override", CAP_DAC_OVERRIDE
},
246 { "dac_read_search", CAP_DAC_READ_SEARCH
},
247 { "fowner", CAP_FOWNER
},
248 { "fsetid", CAP_FSETID
},
249 { "kill", CAP_KILL
},
250 { "setgid", CAP_SETGID
},
251 { "setuid", CAP_SETUID
},
252 { "setpcap", CAP_SETPCAP
},
253 { "linux_immutable", CAP_LINUX_IMMUTABLE
},
254 { "net_bind_service", CAP_NET_BIND_SERVICE
},
255 { "net_broadcast", CAP_NET_BROADCAST
},
256 { "net_admin", CAP_NET_ADMIN
},
257 { "net_raw", CAP_NET_RAW
},
258 { "ipc_lock", CAP_IPC_LOCK
},
259 { "ipc_owner", CAP_IPC_OWNER
},
260 { "sys_module", CAP_SYS_MODULE
},
261 { "sys_rawio", CAP_SYS_RAWIO
},
262 { "sys_chroot", CAP_SYS_CHROOT
},
263 { "sys_ptrace", CAP_SYS_PTRACE
},
264 { "sys_pacct", CAP_SYS_PACCT
},
265 { "sys_admin", CAP_SYS_ADMIN
},
266 { "sys_boot", CAP_SYS_BOOT
},
267 { "sys_nice", CAP_SYS_NICE
},
268 { "sys_resource", CAP_SYS_RESOURCE
},
269 { "sys_time", CAP_SYS_TIME
},
270 { "sys_tty_config", CAP_SYS_TTY_CONFIG
},
271 { "mknod", CAP_MKNOD
},
272 { "lease", CAP_LEASE
},
273 #ifdef CAP_AUDIT_WRITE
274 { "audit_write", CAP_AUDIT_WRITE
},
276 #ifdef CAP_AUDIT_CONTROL
277 { "audit_control", CAP_AUDIT_CONTROL
},
279 { "setfcap", CAP_SETFCAP
},
280 { "mac_override", CAP_MAC_OVERRIDE
},
281 { "mac_admin", CAP_MAC_ADMIN
},
283 { "syslog", CAP_SYSLOG
},
285 #ifdef CAP_WAKE_ALARM
286 { "wake_alarm", CAP_WAKE_ALARM
},
290 static struct caps_opt caps_opt
[] = {};
293 static int run_buffer(char *buffer
)
298 f
= popen(buffer
, "r");
300 SYSERROR("popen failed");
304 output
= malloc(LXC_LOG_BUFFER_SIZE
);
306 ERROR("failed to allocate memory for script output");
310 while(fgets(output
, LXC_LOG_BUFFER_SIZE
, f
))
311 DEBUG("script output: %s", output
);
315 if (pclose(f
) == -1) {
316 SYSERROR("Script exited on error");
323 static int run_script(const char *name
, const char *section
,
324 const char *script
, ...)
331 INFO("Executing script '%s' for container '%s', config section '%s'",
332 script
, name
, section
);
334 va_start(ap
, script
);
335 while ((p
= va_arg(ap
, char *)))
336 size
+= strlen(p
) + 1;
339 size
+= strlen(script
);
340 size
+= strlen(name
);
341 size
+= strlen(section
);
347 buffer
= alloca(size
);
349 ERROR("failed to allocate memory");
353 ret
= snprintf(buffer
, size
, "%s %s %s", script
, name
, section
);
354 if (ret
< 0 || ret
>= size
) {
355 ERROR("Script name too long");
360 va_start(ap
, script
);
361 while ((p
= va_arg(ap
, char *))) {
364 rc
= snprintf(buffer
+ ret
, len
, " %s", p
);
365 if (rc
< 0 || rc
>= len
) {
367 ERROR("Script args too long");
374 return run_buffer(buffer
);
377 static int find_fstype_cb(char* buffer
, void *data
)
387 /* we don't try 'nodev' entries */
388 if (strstr(buffer
, "nodev"))
392 fstype
+= lxc_char_left_gc(fstype
, strlen(fstype
));
393 fstype
[lxc_char_right_gc(fstype
, strlen(fstype
))] = '\0';
395 DEBUG("trying to mount '%s'->'%s' with fstype '%s'",
396 cbarg
->rootfs
, cbarg
->target
, fstype
);
398 if (mount(cbarg
->rootfs
, cbarg
->target
, fstype
, cbarg
->mntopt
, NULL
)) {
399 DEBUG("mount failed with error: %s", strerror(errno
));
403 INFO("mounted '%s' on '%s', with fstype '%s'",
404 cbarg
->rootfs
, cbarg
->target
, fstype
);
409 static int mount_unknow_fs(const char *rootfs
, const char *target
, int mntopt
)
424 * find the filesystem type with brute force:
425 * first we check with /etc/filesystems, in case the modules
426 * are auto-loaded and fall back to the supported kernel fs
433 for (i
= 0; i
< sizeof(fsfile
)/sizeof(fsfile
[0]); i
++) {
437 if (access(fsfile
[i
], F_OK
))
440 ret
= lxc_file_for_each_line(fsfile
[i
], find_fstype_cb
, &cbarg
);
442 ERROR("failed to parse '%s'", fsfile
[i
]);
450 ERROR("failed to determine fs type for '%s'", rootfs
);
454 static int mount_rootfs_dir(const char *rootfs
, const char *target
)
456 return mount(rootfs
, target
, "none", MS_BIND
| MS_REC
, NULL
);
459 static int setup_lodev(const char *rootfs
, int fd
, struct loop_info64
*loinfo
)
464 rfd
= open(rootfs
, O_RDWR
);
466 SYSERROR("failed to open '%s'", rootfs
);
470 memset(loinfo
, 0, sizeof(*loinfo
));
472 loinfo
->lo_flags
= LO_FLAGS_AUTOCLEAR
;
474 if (ioctl(fd
, LOOP_SET_FD
, rfd
)) {
475 SYSERROR("failed to LOOP_SET_FD");
479 if (ioctl(fd
, LOOP_SET_STATUS64
, loinfo
)) {
480 SYSERROR("failed to LOOP_SET_STATUS64");
491 static int mount_rootfs_file(const char *rootfs
, const char *target
)
493 struct dirent dirent
, *direntp
;
494 struct loop_info64 loinfo
;
495 int ret
= -1, fd
= -1, rc
;
497 char path
[MAXPATHLEN
];
499 dir
= opendir("/dev");
501 SYSERROR("failed to open '/dev'");
505 while (!readdir_r(dir
, &dirent
, &direntp
)) {
510 if (!strcmp(direntp
->d_name
, "."))
513 if (!strcmp(direntp
->d_name
, ".."))
516 if (strncmp(direntp
->d_name
, "loop", 4))
519 rc
= snprintf(path
, MAXPATHLEN
, "/dev/%s", direntp
->d_name
);
520 if (rc
< 0 || rc
>= MAXPATHLEN
)
523 fd
= open(path
, O_RDWR
);
527 if (ioctl(fd
, LOOP_GET_STATUS64
, &loinfo
) == 0) {
532 if (errno
!= ENXIO
) {
533 WARN("unexpected error for ioctl on '%s': %m",
538 DEBUG("found '%s' free lodev", path
);
540 ret
= setup_lodev(rootfs
, fd
, &loinfo
);
542 ret
= mount_unknow_fs(path
, target
, 0);
549 WARN("failed to close directory");
554 static int mount_rootfs_block(const char *rootfs
, const char *target
)
556 return mount_unknow_fs(rootfs
, target
, 0);
561 * if rootfs is a directory, then open ${rootfs}.hold for writing for the
562 * duration of the container run, to prevent the container from marking the
563 * underlying fs readonly on shutdown.
564 * return -1 on error.
565 * return -2 if nothing needed to be pinned.
566 * return an open fd (>=0) if we pinned it.
568 int pin_rootfs(const char *rootfs
)
570 char absrootfs
[MAXPATHLEN
];
571 char absrootfspin
[MAXPATHLEN
];
575 if (rootfs
== NULL
|| strlen(rootfs
) == 0)
578 if (!realpath(rootfs
, absrootfs
)) {
579 SYSERROR("failed to get real path for '%s'", rootfs
);
583 if (access(absrootfs
, F_OK
)) {
584 SYSERROR("'%s' is not accessible", absrootfs
);
588 if (stat(absrootfs
, &s
)) {
589 SYSERROR("failed to stat '%s'", absrootfs
);
593 if (!__S_ISTYPE(s
.st_mode
, S_IFDIR
))
596 ret
= snprintf(absrootfspin
, MAXPATHLEN
, "%s%s", absrootfs
, ".hold");
597 if (ret
>= MAXPATHLEN
) {
598 SYSERROR("pathname too long for rootfs hold file");
602 fd
= open(absrootfspin
, O_CREAT
| O_RDWR
, S_IWUSR
|S_IRUSR
);
603 INFO("opened %s as fd %d\n", absrootfspin
, fd
);
607 static int mount_rootfs(const char *rootfs
, const char *target
)
609 char absrootfs
[MAXPATHLEN
];
613 typedef int (*rootfs_cb
)(const char *, const char *);
619 { S_IFDIR
, mount_rootfs_dir
},
620 { S_IFBLK
, mount_rootfs_block
},
621 { S_IFREG
, mount_rootfs_file
},
624 if (!realpath(rootfs
, absrootfs
)) {
625 SYSERROR("failed to get real path for '%s'", rootfs
);
629 if (access(absrootfs
, F_OK
)) {
630 SYSERROR("'%s' is not accessible", absrootfs
);
634 if (stat(absrootfs
, &s
)) {
635 SYSERROR("failed to stat '%s'", absrootfs
);
639 for (i
= 0; i
< sizeof(rtfs_type
)/sizeof(rtfs_type
[0]); i
++) {
641 if (!__S_ISTYPE(s
.st_mode
, rtfs_type
[i
].type
))
644 return rtfs_type
[i
].cb(absrootfs
, target
);
647 ERROR("unsupported rootfs type for '%s'", absrootfs
);
651 static int setup_utsname(struct utsname
*utsname
)
656 if (sethostname(utsname
->nodename
, strlen(utsname
->nodename
))) {
657 SYSERROR("failed to set the hostname to '%s'", utsname
->nodename
);
661 INFO("'%s' hostname has been setup", utsname
->nodename
);
666 static int setup_tty(const struct lxc_rootfs
*rootfs
,
667 const struct lxc_tty_info
*tty_info
, char *ttydir
)
669 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
675 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
677 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
679 ret
= snprintf(path
, sizeof(path
), "%s/dev/tty%d",
680 rootfs
->mount
, i
+ 1);
681 if (ret
>= sizeof(path
)) {
682 ERROR("pathname too long for ttys");
686 /* create dev/lxc/tty%d" */
687 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/tty%d",
688 rootfs
->mount
, ttydir
, i
+ 1);
689 if (ret
>= sizeof(lxcpath
)) {
690 ERROR("pathname too long for ttys");
693 ret
= creat(lxcpath
, 0660);
694 if (ret
==-1 && errno
!= EEXIST
) {
695 SYSERROR("error creating %s\n", lxcpath
);
700 if (ret
&& errno
!= ENOENT
) {
701 SYSERROR("error unlinking %s\n", path
);
705 if (mount(pty_info
->name
, lxcpath
, "none", MS_BIND
, 0)) {
706 WARN("failed to mount '%s'->'%s'",
707 pty_info
->name
, path
);
711 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/tty%d", ttydir
, i
+1);
712 if (ret
>= sizeof(lxcpath
)) {
713 ERROR("tty pathname too long");
716 ret
= symlink(lxcpath
, path
);
718 SYSERROR("failed to create symlink for tty %d\n", i
+1);
722 /* If we populated /dev, then we need to create /dev/ttyN */
723 if (access(path
, F_OK
)) {
724 ret
= creat(path
, 0660);
726 SYSERROR("error creating %s\n", path
);
727 /* this isn't fatal, continue */
731 if (mount(pty_info
->name
, path
, "none", MS_BIND
, 0)) {
732 WARN("failed to mount '%s'->'%s'",
733 pty_info
->name
, path
);
739 INFO("%d tty(s) has been setup", tty_info
->nbtty
);
744 static int setup_rootfs_pivot_root_cb(char *buffer
, void *data
)
746 struct lxc_list
*mountlist
, *listentry
, *iterator
;
747 char *pivotdir
, *mountpoint
, *mountentry
;
752 cbparm
= (void **)data
;
754 mountlist
= cbparm
[0];
755 pivotdir
= cbparm
[1];
757 /* parse entry, first field is mountname, ignore */
758 mountpoint
= strtok(mountentry
, " ");
762 /* second field is mountpoint */
763 mountpoint
= strtok(NULL
, " ");
767 /* only consider mountpoints below old root fs */
768 if (strncmp(mountpoint
, pivotdir
, strlen(pivotdir
)))
771 /* filter duplicate mountpoints */
773 lxc_list_for_each(iterator
, mountlist
) {
774 if (!strcmp(iterator
->elem
, mountpoint
)) {
782 /* add entry to list */
783 listentry
= malloc(sizeof(*listentry
));
785 SYSERROR("malloc for mountpoint listentry failed");
789 listentry
->elem
= strdup(mountpoint
);
790 if (!listentry
->elem
) {
791 SYSERROR("strdup failed");
794 lxc_list_add_tail(mountlist
, listentry
);
799 static int umount_oldrootfs(const char *oldrootfs
)
801 char path
[MAXPATHLEN
];
803 struct lxc_list mountlist
, *iterator
, *next
;
804 int ok
, still_mounted
, last_still_mounted
;
807 /* read and parse /proc/mounts in old root fs */
808 lxc_list_init(&mountlist
);
810 /* oldrootfs is on the top tree directory now */
811 rc
= snprintf(path
, sizeof(path
), "/%s", oldrootfs
);
812 if (rc
>= sizeof(path
)) {
813 ERROR("rootfs name too long");
816 cbparm
[0] = &mountlist
;
818 cbparm
[1] = strdup(path
);
820 SYSERROR("strdup failed");
824 rc
= snprintf(path
, sizeof(path
), "%s/proc/mounts", oldrootfs
);
825 if (rc
>= sizeof(path
)) {
826 ERROR("container proc/mounts name too long");
830 ok
= lxc_file_for_each_line(path
,
831 setup_rootfs_pivot_root_cb
, &cbparm
);
833 SYSERROR("failed to read or parse mount list '%s'", path
);
837 /* umount filesystems until none left or list no longer shrinks */
840 last_still_mounted
= still_mounted
;
843 lxc_list_for_each_safe(iterator
, &mountlist
, next
) {
845 /* umount normally */
846 if (!umount(iterator
->elem
)) {
847 DEBUG("umounted '%s'", (char *)iterator
->elem
);
848 lxc_list_del(iterator
);
855 } while (still_mounted
> 0 && still_mounted
!= last_still_mounted
);
858 lxc_list_for_each(iterator
, &mountlist
) {
860 /* let's try a lazy umount */
861 if (!umount2(iterator
->elem
, MNT_DETACH
)) {
862 INFO("lazy unmount of '%s'", (char *)iterator
->elem
);
866 /* be more brutal (nfs) */
867 if (!umount2(iterator
->elem
, MNT_FORCE
)) {
868 INFO("forced unmount of '%s'", (char *)iterator
->elem
);
872 WARN("failed to unmount '%s'", (char *)iterator
->elem
);
878 static int setup_rootfs_pivot_root(const char *rootfs
, const char *pivotdir
)
880 char path
[MAXPATHLEN
];
881 int remove_pivotdir
= 0;
884 /* change into new root fs */
886 SYSERROR("can't chdir to new rootfs '%s'", rootfs
);
891 pivotdir
= "lxc_putold";
893 /* compute the full path to pivotdir under rootfs */
894 rc
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, pivotdir
);
895 if (rc
>= sizeof(path
)) {
896 ERROR("pivot dir name too long");
900 if (access(path
, F_OK
)) {
902 if (mkdir_p(path
, 0755)) {
903 SYSERROR("failed to create pivotdir '%s'", path
);
908 DEBUG("created '%s' directory", path
);
911 DEBUG("mountpoint for old rootfs is '%s'", path
);
913 /* pivot_root into our new root fs */
914 if (pivot_root(".", path
)) {
915 SYSERROR("pivot_root syscall failed");
920 SYSERROR("can't chdir to / after pivot_root");
924 DEBUG("pivot_root syscall to '%s' successful", rootfs
);
926 /* we switch from absolute path to relative path */
927 if (umount_oldrootfs(pivotdir
))
930 /* remove temporary mount point, we don't consider the removing
932 if (remove_pivotdir
&& rmdir(pivotdir
))
933 WARN("can't remove mountpoint '%s': %m", pivotdir
);
939 * Do we want to add options for max size of /dev and a file to
940 * specify which devices to create?
942 static int mount_autodev(char *root
)
945 char path
[MAXPATHLEN
];
947 INFO("Mounting /dev under %s\n", root
);
948 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
949 if (ret
< 0 || ret
> MAXPATHLEN
)
951 ret
= mount("none", path
, "tmpfs", 0, "size=100000");
953 SYSERROR("Failed to mount /dev at %s\n", root
);
956 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/pts", root
);
957 if (ret
< 0 || ret
>= MAXPATHLEN
)
959 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
961 SYSERROR("Failed to create /dev/pts in container");
965 INFO("Mounted /dev under %s\n", root
);
970 * Try to run MAKEDEV console in the container. If something fails,
971 * continue anyway as it should not be detrimental to the container.
972 * This makes sure that things like /dev/vcs* exist.
973 * (Pass devpath in to reduce stack usage)
975 static void run_makedev(char *devpath
)
980 curd
= open(".", O_RDONLY
);
983 ret
= chdir(devpath
);
988 if (run_buffer("/sbin/MAKEDEV console"))
989 INFO("Error running MAKEDEV console in %s", devpath
);
992 INFO("Error returning to original directory: expect breakage");
1003 struct lxc_devs lxc_devs
[] = {
1004 { "null", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 3 },
1005 { "zero", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 5 },
1006 { "full", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 7 },
1007 { "urandom", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 9 },
1008 { "random", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 8 },
1009 { "tty", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 5, 0 },
1010 { "console", S_IFCHR
| S_IRUSR
| S_IWUSR
, 5, 1 },
1013 static int setup_autodev(char *root
)
1017 char path
[MAXPATHLEN
];
1021 INFO("Creating initial consoles under %s/dev\n", root
);
1023 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev", root
);
1024 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1025 ERROR("Error calculating container /dev location");
1030 INFO("Populating /dev under %s\n", root
);
1031 cmask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1032 for (i
= 0; i
< sizeof(lxc_devs
) / sizeof(lxc_devs
[0]); i
++) {
1034 ret
= snprintf(path
, MAXPATHLEN
, "%s/dev/%s", root
, d
->name
);
1035 if (ret
< 0 || ret
>= MAXPATHLEN
)
1037 ret
= mknod(path
, d
->mode
, makedev(d
->maj
, d
->min
));
1038 if (ret
&& errno
!= EEXIST
) {
1039 SYSERROR("Error creating %s\n", d
->name
);
1045 INFO("Populated /dev under %s\n", root
);
1050 * Detect whether / is mounted MS_SHARED. The only way I know of to
1051 * check that is through /proc/self/mountinfo.
1052 * I'm only checking for /. If the container rootfs or mount location
1053 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1054 * out would be too much work to be worth it.
1056 #define LINELEN 4096
1057 int detect_shared_rootfs(void)
1059 char buf
[LINELEN
], *p
;
1064 f
= fopen("/proc/self/mountinfo", "r");
1067 while ((p
= fgets(buf
, LINELEN
, f
))) {
1068 INFO("looking at .%s.", p
);
1069 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1070 p
= index(p
+1, ' ');
1073 p2
= index(p
+1, ' ');
1077 INFO("now p is .%s.", p
);
1078 if (strcmp(p
+1, "/") == 0) {
1079 // this is '/'. is it shared?
1080 p
= index(p2
+1, ' ');
1081 if (strstr(p
, "shared:"))
1090 * I'll forgive you for asking whether all of this is needed :) The
1092 * pivot_root will fail if the new root, the put_old dir, or the parent
1093 * of current->fs->root are MS_SHARED. (parent of current->fs_root may
1094 * or may not be current->fs_root - if we assumed it always was, we could
1095 * just mount --make-rslave /). So,
1096 * 1. mount a tiny tmpfs to be parent of current->fs->root.
1097 * 2. make that MS_SLAVE
1098 * 3. make a 'root' directory under that
1099 * 4. mount --rbind / under the $tinyroot/root.
1100 * 5. make that rslave
1101 * 6. chdir and chroot into $tinyroot/root
1102 * 7. $tinyroot will be unmounted by our parent in start.c
1104 static int chroot_into_slave(struct lxc_conf
*conf
)
1106 char path
[MAXPATHLEN
];
1107 const char *destpath
= conf
->rootfs
.mount
;
1110 if (mount(destpath
, destpath
, NULL
, MS_BIND
, 0)) {
1111 SYSERROR("failed to mount %s bind", destpath
);
1114 if (mount("", destpath
, NULL
, MS_SLAVE
, 0)) {
1115 SYSERROR("failed to make %s slave", destpath
);
1118 if (mount("none", destpath
, "tmpfs", 0, "size=10000")) {
1119 SYSERROR("Failed to mount tmpfs / at %s", destpath
);
1122 ret
= snprintf(path
, MAXPATHLEN
, "%s/root", destpath
);
1123 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1124 ERROR("out of memory making root path");
1127 if (mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)) {
1128 SYSERROR("Failed to create /dev/pts in container");
1131 if (mount("/", path
, NULL
, MS_BIND
|MS_REC
, 0)) {
1132 SYSERROR("Failed to rbind mount / to %s", path
);
1135 if (mount("", destpath
, NULL
, MS_SLAVE
|MS_REC
, 0)) {
1136 SYSERROR("Failed to make tmp-/ at %s rslave", path
);
1140 SYSERROR("Failed to chdir into tmp-/");
1144 SYSERROR("Failed to chroot into tmp-/");
1147 INFO("Chrooted into tmp-/ at %s\n", path
);
1151 static int setup_rootfs(struct lxc_conf
*conf
)
1153 const struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1158 if (access(rootfs
->mount
, F_OK
)) {
1159 SYSERROR("failed to access to '%s', check it is present",
1164 if (detect_shared_rootfs()) {
1165 if (chroot_into_slave(conf
)) {
1166 ERROR("Failed to chroot into slave /");
1171 if (mount_rootfs(rootfs
->path
, rootfs
->mount
)) {
1172 ERROR("failed to mount rootfs");
1176 DEBUG("mounted '%s' on '%s'", rootfs
->path
, rootfs
->mount
);
1181 int setup_pivot_root(const struct lxc_rootfs
*rootfs
)
1186 if (setup_rootfs_pivot_root(rootfs
->mount
, rootfs
->pivot
)) {
1187 ERROR("failed to setup pivot root");
1194 static int setup_pts(int pts
)
1196 char target
[PATH_MAX
];
1201 if (!access("/dev/pts/ptmx", F_OK
) && umount("/dev/pts")) {
1202 SYSERROR("failed to umount 'dev/pts'");
1206 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
,
1207 "newinstance,ptmxmode=0666")) {
1208 SYSERROR("failed to mount a new instance of '/dev/pts'");
1212 if (access("/dev/ptmx", F_OK
)) {
1213 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
1215 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
1219 if (realpath("/dev/ptmx", target
) && !strcmp(target
, "/dev/pts/ptmx"))
1222 /* fallback here, /dev/pts/ptmx exists just mount bind */
1223 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND
, 0)) {
1224 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
1228 INFO("created new pts instance");
1234 static int setup_personality(int persona
)
1236 #if HAVE_SYS_PERSONALITY_H
1240 if (personality(persona
) < 0) {
1241 SYSERROR("failed to set personality to '0x%x'", persona
);
1245 INFO("set personality to '0x%x'", persona
);
1251 static int setup_dev_console(const struct lxc_rootfs
*rootfs
,
1252 const struct lxc_console
*console
)
1254 char path
[MAXPATHLEN
];
1258 ret
= snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1259 if (ret
>= sizeof(path
)) {
1260 ERROR("console path too long\n");
1264 if (access(path
, F_OK
)) {
1265 WARN("rootfs specified but no console found at '%s'", path
);
1269 if (console
->peer
== -1) {
1270 INFO("no console output required");
1274 if (stat(path
, &s
)) {
1275 SYSERROR("failed to stat '%s'", path
);
1279 if (chmod(console
->name
, s
.st_mode
)) {
1280 SYSERROR("failed to set mode '0%o' to '%s'",
1281 s
.st_mode
, console
->name
);
1285 if (mount(console
->name
, path
, "none", MS_BIND
, 0)) {
1286 ERROR("failed to mount '%s' on '%s'", console
->name
, path
);
1290 INFO("console has been setup");
1294 static int setup_ttydir_console(const struct lxc_rootfs
*rootfs
,
1295 const struct lxc_console
*console
,
1298 char path
[MAXPATHLEN
], lxcpath
[MAXPATHLEN
];
1301 /* create rootfs/dev/<ttydir> directory */
1302 ret
= snprintf(path
, sizeof(path
), "%s/dev/%s", rootfs
->mount
,
1304 if (ret
>= sizeof(path
))
1306 ret
= mkdir(path
, 0755);
1307 if (ret
&& errno
!= EEXIST
) {
1308 SYSERROR("failed with errno %d to create %s\n", errno
, path
);
1311 INFO("created %s\n", path
);
1313 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/dev/%s/console",
1314 rootfs
->mount
, ttydir
);
1315 if (ret
>= sizeof(lxcpath
)) {
1316 ERROR("console path too long\n");
1320 snprintf(path
, sizeof(path
), "%s/dev/console", rootfs
->mount
);
1322 if (ret
&& errno
!= ENOENT
) {
1323 SYSERROR("error unlinking %s\n", path
);
1327 ret
= creat(lxcpath
, 0660);
1328 if (ret
==-1 && errno
!= EEXIST
) {
1329 SYSERROR("error %d creating %s\n", errno
, lxcpath
);
1334 if (console
->peer
== -1) {
1335 INFO("no console output required");
1339 if (mount(console
->name
, lxcpath
, "none", MS_BIND
, 0)) {
1340 ERROR("failed to mount '%s' on '%s'", console
->name
, lxcpath
);
1344 /* create symlink from rootfs/dev/console to 'lxc/console' */
1345 ret
= snprintf(lxcpath
, sizeof(lxcpath
), "%s/console", ttydir
);
1346 if (ret
>= sizeof(lxcpath
)) {
1347 ERROR("lxc/console path too long");
1350 ret
= symlink(lxcpath
, path
);
1352 SYSERROR("failed to create symlink for console");
1356 INFO("console has been setup on %s", lxcpath
);
1361 static int setup_console(const struct lxc_rootfs
*rootfs
,
1362 const struct lxc_console
*console
,
1365 /* We don't have a rootfs, /dev/console will be shared */
1369 return setup_dev_console(rootfs
, console
);
1371 return setup_ttydir_console(rootfs
, console
, ttydir
);
1374 static int setup_kmsg(const struct lxc_rootfs
*rootfs
,
1375 const struct lxc_console
*console
)
1377 char kpath
[MAXPATHLEN
];
1382 ret
= snprintf(kpath
, sizeof(kpath
), "%s/dev/kmsg", rootfs
->mount
);
1383 if (ret
< 0 || ret
>= sizeof(kpath
))
1386 ret
= unlink(kpath
);
1387 if (ret
&& errno
!= ENOENT
) {
1388 SYSERROR("error unlinking %s\n", kpath
);
1392 ret
= symlink("console", kpath
);
1394 SYSERROR("failed to create symlink for kmsg");
1401 int setup_cgroup(const char *name
, struct lxc_list
*cgroups
)
1403 struct lxc_list
*iterator
;
1404 struct lxc_cgroup
*cg
;
1407 if (lxc_list_empty(cgroups
))
1410 lxc_list_for_each(iterator
, cgroups
) {
1412 cg
= iterator
->elem
;
1414 if (lxc_cgroup_set(name
, cg
->subsystem
, cg
->value
))
1417 DEBUG("cgroup '%s' set to '%s'", cg
->subsystem
, cg
->value
);
1421 INFO("cgroup has been setup");
1426 static void parse_mntopt(char *opt
, unsigned long *flags
, char **data
)
1428 struct mount_opt
*mo
;
1430 /* If opt is found in mount_opt, set or clear flags.
1431 * Otherwise append it to data. */
1433 for (mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
1434 if (!strncmp(opt
, mo
->name
, strlen(mo
->name
))) {
1436 *flags
&= ~mo
->flag
;
1448 static int parse_mntopts(const char *mntopts
, unsigned long *mntflags
,
1452 char *p
, *saveptr
= NULL
;
1460 s
= strdup(mntopts
);
1462 SYSERROR("failed to allocate memory");
1466 data
= malloc(strlen(s
) + 1);
1468 SYSERROR("failed to allocate memory");
1474 for (p
= strtok_r(s
, ",", &saveptr
); p
!= NULL
;
1475 p
= strtok_r(NULL
, ",", &saveptr
))
1476 parse_mntopt(p
, mntflags
, &data
);
1487 static int mount_entry(const char *fsname
, const char *target
,
1488 const char *fstype
, unsigned long mountflags
,
1491 if (mount(fsname
, target
, fstype
, mountflags
& ~MS_REMOUNT
, data
)) {
1492 SYSERROR("failed to mount '%s' on '%s'", fsname
, target
);
1496 if ((mountflags
& MS_REMOUNT
) || (mountflags
& MS_BIND
)) {
1498 DEBUG("remounting %s on %s to respect bind or remount options",
1501 if (mount(fsname
, target
, fstype
,
1502 mountflags
| MS_REMOUNT
, data
)) {
1503 SYSERROR("failed to mount '%s' on '%s'",
1509 DEBUG("mounted '%s' on '%s', type '%s'", fsname
, target
, fstype
);
1514 static inline int mount_entry_on_systemfs(struct mntent
*mntent
)
1516 unsigned long mntflags
;
1520 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1521 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1525 ret
= mount_entry(mntent
->mnt_fsname
, mntent
->mnt_dir
,
1526 mntent
->mnt_type
, mntflags
, mntdata
);
1533 static int mount_entry_on_absolute_rootfs(struct mntent
*mntent
,
1534 const struct lxc_rootfs
*rootfs
,
1535 const char *lxc_name
)
1538 char path
[MAXPATHLEN
];
1539 unsigned long mntflags
;
1541 int r
, ret
= 0, offset
;
1543 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1544 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
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
, LXCPATH
"/%s/rootfs", 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
,
1587 static int mount_entry_on_relative_rootfs(struct mntent
*mntent
,
1590 char path
[MAXPATHLEN
];
1591 unsigned long mntflags
;
1595 if (parse_mntopts(mntent
->mnt_opts
, &mntflags
, &mntdata
) < 0) {
1596 ERROR("failed to parse mount option '%s'", mntent
->mnt_opts
);
1600 /* relative to root mount point */
1601 ret
= snprintf(path
, sizeof(path
), "%s/%s", rootfs
, mntent
->mnt_dir
);
1602 if (ret
>= sizeof(path
)) {
1603 ERROR("path name too long");
1607 ret
= mount_entry(mntent
->mnt_fsname
, path
, mntent
->mnt_type
,
1615 static int mount_file_entries(const struct lxc_rootfs
*rootfs
, FILE *file
,
1616 const char *lxc_name
)
1618 struct mntent
*mntent
;
1621 while ((mntent
= getmntent(file
))) {
1623 if (!rootfs
->path
) {
1624 if (mount_entry_on_systemfs(mntent
))
1629 /* We have a separate root, mounts are relative to it */
1630 if (mntent
->mnt_dir
[0] != '/') {
1631 if (mount_entry_on_relative_rootfs(mntent
,
1637 if (mount_entry_on_absolute_rootfs(mntent
, rootfs
, lxc_name
))
1643 INFO("mount points have been setup");
1648 static int setup_mount(const struct lxc_rootfs
*rootfs
, const char *fstab
,
1649 const char *lxc_name
)
1657 file
= setmntent(fstab
, "r");
1659 SYSERROR("failed to use '%s'", fstab
);
1663 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1669 static int setup_mount_entries(const struct lxc_rootfs
*rootfs
, struct lxc_list
*mount
,
1670 const char *lxc_name
)
1673 struct lxc_list
*iterator
;
1679 ERROR("tmpfile error: %m");
1683 lxc_list_for_each(iterator
, mount
) {
1684 mount_entry
= iterator
->elem
;
1685 fprintf(file
, "%s\n", mount_entry
);
1690 ret
= mount_file_entries(rootfs
, file
, lxc_name
);
1696 static int setup_caps(struct lxc_list
*caps
)
1698 struct lxc_list
*iterator
;
1703 lxc_list_for_each(iterator
, caps
) {
1705 drop_entry
= iterator
->elem
;
1709 for (i
= 0; i
< sizeof(caps_opt
)/sizeof(caps_opt
[0]); i
++) {
1711 if (strcmp(drop_entry
, caps_opt
[i
].name
))
1714 capid
= caps_opt
[i
].value
;
1719 /* try to see if it's numeric, so the user may specify
1720 * capabilities that the running kernel knows about but
1722 capid
= strtol(drop_entry
, &ptr
, 10);
1723 if (!ptr
|| *ptr
!= '\0' ||
1724 capid
== LONG_MIN
|| capid
== LONG_MAX
)
1725 /* not a valid number */
1727 else if (capid
> lxc_caps_last_cap())
1728 /* we have a number but it's not a valid
1734 ERROR("unknown capability %s", drop_entry
);
1738 DEBUG("drop capability '%s' (%d)", drop_entry
, capid
);
1740 if (prctl(PR_CAPBSET_DROP
, capid
, 0, 0, 0)) {
1741 SYSERROR("failed to remove %s capability", drop_entry
);
1747 DEBUG("capabilities has been setup");
1752 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
1754 struct sockaddr sockaddr
;
1758 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
1760 ERROR("mac address '%s' conversion failed : %s",
1761 hwaddr
, strerror(-ret
));
1765 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
1766 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
1768 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1770 ERROR("socket failure : %s", strerror(errno
));
1774 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
1777 ERROR("ioctl failure : %s", strerror(errno
));
1779 DEBUG("mac address '%s' on '%s' has been setup", hwaddr
, ifname
);
1784 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
1786 struct lxc_list
*iterator
;
1787 struct lxc_inetdev
*inetdev
;
1790 lxc_list_for_each(iterator
, ip
) {
1792 inetdev
= iterator
->elem
;
1794 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
1795 &inetdev
->bcast
, inetdev
->prefix
);
1797 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
1798 ifindex
, strerror(-err
));
1806 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
1808 struct lxc_list
*iterator
;
1809 struct lxc_inet6dev
*inet6dev
;
1812 lxc_list_for_each(iterator
, ip
) {
1814 inet6dev
= iterator
->elem
;
1816 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
1817 &inet6dev
->mcast
, &inet6dev
->acast
,
1820 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
1821 ifindex
, strerror(-err
));
1829 static int setup_netdev(struct lxc_netdev
*netdev
)
1831 char ifname
[IFNAMSIZ
];
1832 char *current_ifname
= ifname
;
1835 /* empty network namespace */
1836 if (!netdev
->ifindex
) {
1837 if (netdev
->flags
& IFF_UP
) {
1838 err
= lxc_netdev_up("lo");
1840 ERROR("failed to set the loopback up : %s",
1848 /* retrieve the name of the interface */
1849 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1850 ERROR("no interface corresponding to index '%d'",
1855 /* default: let the system to choose one interface name */
1857 netdev
->name
= netdev
->type
== LXC_NET_PHYS
?
1858 netdev
->link
: "eth%d";
1860 /* rename the interface name */
1861 err
= lxc_netdev_rename_by_name(ifname
, netdev
->name
);
1863 ERROR("failed to rename %s->%s : %s", ifname
, netdev
->name
,
1868 /* Re-read the name of the interface because its name has changed
1869 * and would be automatically allocated by the system
1871 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
1872 ERROR("no interface corresponding to index '%d'",
1877 /* set a mac address */
1878 if (netdev
->hwaddr
) {
1879 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
1880 ERROR("failed to setup hw address for '%s'",
1886 /* setup ipv4 addresses on the interface */
1887 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
1888 ERROR("failed to setup ip addresses for '%s'",
1893 /* setup ipv6 addresses on the interface */
1894 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
1895 ERROR("failed to setup ipv6 addresses for '%s'",
1900 /* set the network device up */
1901 if (netdev
->flags
& IFF_UP
) {
1904 err
= lxc_netdev_up(current_ifname
);
1906 ERROR("failed to set '%s' up : %s", current_ifname
,
1911 /* the network is up, make the loopback up too */
1912 err
= lxc_netdev_up("lo");
1914 ERROR("failed to set the loopback up : %s",
1920 /* We can only set up the default routes after bringing
1921 * up the interface, sine bringing up the interface adds
1922 * the link-local routes and we can't add a default
1923 * route if the gateway is not reachable. */
1925 /* setup ipv4 gateway on the interface */
1926 if (netdev
->ipv4_gateway
) {
1927 if (!(netdev
->flags
& IFF_UP
)) {
1928 ERROR("Cannot add ipv4 gateway for %s when not bringing up the interface", ifname
);
1932 if (lxc_list_empty(&netdev
->ipv4
)) {
1933 ERROR("Cannot add ipv4 gateway for %s when not assigning an address", ifname
);
1937 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
1939 ERROR("failed to setup ipv4 gateway for '%s': %s",
1940 ifname
, strerror(-err
));
1941 if (netdev
->ipv4_gateway_auto
) {
1942 char buf
[INET_ADDRSTRLEN
];
1943 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
1944 ERROR("tried to set autodetected ipv4 gateway '%s'", buf
);
1950 /* setup ipv6 gateway on the interface */
1951 if (netdev
->ipv6_gateway
) {
1952 if (!(netdev
->flags
& IFF_UP
)) {
1953 ERROR("Cannot add ipv6 gateway for %s when not bringing up the interface", ifname
);
1957 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
1958 ERROR("Cannot add ipv6 gateway for %s when not assigning an address", ifname
);
1962 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
1964 ERROR("failed to setup ipv6 gateway for '%s': %s",
1965 ifname
, strerror(-err
));
1966 if (netdev
->ipv6_gateway_auto
) {
1967 char buf
[INET6_ADDRSTRLEN
];
1968 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
1969 ERROR("tried to set autodetected ipv6 gateway '%s'", buf
);
1975 DEBUG("'%s' has been setup", current_ifname
);
1980 static int setup_network(struct lxc_list
*network
)
1982 struct lxc_list
*iterator
;
1983 struct lxc_netdev
*netdev
;
1985 lxc_list_for_each(iterator
, network
) {
1987 netdev
= iterator
->elem
;
1989 if (setup_netdev(netdev
)) {
1990 ERROR("failed to setup netdev");
1995 if (!lxc_list_empty(network
))
1996 INFO("network has been setup");
2001 void lxc_rename_phys_nics_on_shutdown(struct lxc_conf
*conf
)
2005 INFO("running to reset %d nic names", conf
->num_savednics
);
2006 for (i
=0; i
<conf
->num_savednics
; i
++) {
2007 struct saved_nic
*s
= &conf
->saved_nics
[i
];
2008 INFO("resetting nic %d to %s\n", s
->ifindex
, s
->orig_name
);
2009 lxc_netdev_rename_by_index(s
->ifindex
, s
->orig_name
);
2012 conf
->num_savednics
= 0;
2013 free(conf
->saved_nics
);
2016 static int setup_private_host_hw_addr(char *veth1
)
2022 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
2026 snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2027 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2033 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2034 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2039 DEBUG("mac address of host interface '%s' changed to private "
2040 "%02x:%02x:%02x:%02x:%02x:%02x", veth1
,
2041 ifr
.ifr_hwaddr
.sa_data
[0] & 0xff,
2042 ifr
.ifr_hwaddr
.sa_data
[1] & 0xff,
2043 ifr
.ifr_hwaddr
.sa_data
[2] & 0xff,
2044 ifr
.ifr_hwaddr
.sa_data
[3] & 0xff,
2045 ifr
.ifr_hwaddr
.sa_data
[4] & 0xff,
2046 ifr
.ifr_hwaddr
.sa_data
[5] & 0xff);
2051 static char *default_rootfs_mount
= LXCROOTFSMOUNT
;
2053 struct lxc_conf
*lxc_conf_init(void)
2055 struct lxc_conf
*new;
2058 new = malloc(sizeof(*new));
2060 ERROR("lxc_conf_init : %m");
2063 memset(new, 0, sizeof(*new));
2065 new->personality
= -1;
2066 new->console
.log_path
= NULL
;
2067 new->console
.log_fd
= -1;
2068 new->console
.path
= NULL
;
2069 new->console
.peer
= -1;
2070 new->console
.master
= -1;
2071 new->console
.slave
= -1;
2072 new->console
.name
[0] = '\0';
2073 new->maincmd_fd
= -1;
2074 new->rootfs
.mount
= default_rootfs_mount
;
2075 new->loglevel
= LXC_LOG_PRIORITY_NOTSET
;
2076 lxc_list_init(&new->cgroup
);
2077 lxc_list_init(&new->network
);
2078 lxc_list_init(&new->mount_list
);
2079 lxc_list_init(&new->caps
);
2080 for (i
=0; i
<NUM_LXC_HOOKS
; i
++)
2081 lxc_list_init(&new->hooks
[i
]);
2083 new->aa_profile
= NULL
;
2085 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2086 new->lsm_umount_proc
= 0;
2092 static int instanciate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2094 char veth1buf
[IFNAMSIZ
], *veth1
;
2095 char veth2buf
[IFNAMSIZ
], *veth2
;
2098 if (netdev
->priv
.veth_attr
.pair
)
2099 veth1
= netdev
->priv
.veth_attr
.pair
;
2101 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
2102 if (err
>= sizeof(veth1buf
)) { /* can't *really* happen, but... */
2103 ERROR("veth1 name too long");
2106 veth1
= mktemp(veth1buf
);
2107 /* store away for deconf */
2108 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
2111 snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
2112 veth2
= mktemp(veth2buf
);
2114 if (!strlen(veth1
) || !strlen(veth2
)) {
2115 ERROR("failed to allocate a temporary name");
2119 err
= lxc_veth_create(veth1
, veth2
);
2121 ERROR("failed to create %s-%s : %s", veth1
, veth2
,
2126 /* changing the high byte of the mac address to 0xfe, the bridge interface
2127 * will always keep the host's mac address and not take the mac address
2129 err
= setup_private_host_hw_addr(veth1
);
2131 ERROR("failed to change mac address of host interface '%s' : %s",
2132 veth1
, strerror(-err
));
2137 err
= lxc_netdev_set_mtu(veth1
, atoi(netdev
->mtu
));
2139 err
= lxc_netdev_set_mtu(veth2
, atoi(netdev
->mtu
));
2141 ERROR("failed to set mtu '%s' for %s-%s : %s",
2142 netdev
->mtu
, veth1
, veth2
, strerror(-err
));
2148 err
= lxc_bridge_attach(netdev
->link
, veth1
);
2150 ERROR("failed to attach '%s' to the bridge '%s' : %s",
2151 veth1
, netdev
->link
, strerror(-err
));
2156 netdev
->ifindex
= if_nametoindex(veth2
);
2157 if (!netdev
->ifindex
) {
2158 ERROR("failed to retrieve the index for %s", veth2
);
2162 err
= lxc_netdev_up(veth1
);
2164 ERROR("failed to set %s up : %s", veth1
, strerror(-err
));
2168 if (netdev
->upscript
) {
2169 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2170 "veth", veth1
, (char*) NULL
);
2175 DEBUG("instanciated veth '%s/%s', index is '%d'",
2176 veth1
, veth2
, netdev
->ifindex
);
2181 lxc_netdev_delete_by_name(veth1
);
2185 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2190 if (netdev
->priv
.veth_attr
.pair
)
2191 veth1
= netdev
->priv
.veth_attr
.pair
;
2193 veth1
= netdev
->priv
.veth_attr
.veth1
;
2195 if (netdev
->downscript
) {
2196 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2197 "down", "veth", veth1
, (char*) NULL
);
2204 static int instanciate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2206 char peerbuf
[IFNAMSIZ
], *peer
;
2209 if (!netdev
->link
) {
2210 ERROR("no link specified for macvlan netdev");
2214 err
= snprintf(peerbuf
, sizeof(peerbuf
), "mcXXXXXX");
2215 if (err
>= sizeof(peerbuf
))
2218 peer
= mktemp(peerbuf
);
2219 if (!strlen(peer
)) {
2220 ERROR("failed to make a temporary name");
2224 err
= lxc_macvlan_create(netdev
->link
, peer
,
2225 netdev
->priv
.macvlan_attr
.mode
);
2227 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
2228 peer
, netdev
->link
, strerror(-err
));
2232 netdev
->ifindex
= if_nametoindex(peer
);
2233 if (!netdev
->ifindex
) {
2234 ERROR("failed to retrieve the index for %s", peer
);
2235 lxc_netdev_delete_by_name(peer
);
2239 if (netdev
->upscript
) {
2240 err
= run_script(handler
->name
, "net", netdev
->upscript
, "up",
2241 "macvlan", netdev
->link
, (char*) NULL
);
2246 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
2247 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
2252 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2256 if (netdev
->downscript
) {
2257 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2258 "down", "macvlan", netdev
->link
,
2266 /* XXX: merge with instanciate_macvlan */
2267 static int instanciate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2269 char peer
[IFNAMSIZ
];
2272 if (!netdev
->link
) {
2273 ERROR("no link specified for vlan netdev");
2277 err
= snprintf(peer
, sizeof(peer
), "vlan%d", netdev
->priv
.vlan_attr
.vid
);
2278 if (err
>= sizeof(peer
)) {
2279 ERROR("peer name too long");
2283 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
2285 ERROR("failed to create vlan interface '%s' on '%s' : %s",
2286 peer
, netdev
->link
, strerror(-err
));
2290 netdev
->ifindex
= if_nametoindex(peer
);
2291 if (!netdev
->ifindex
) {
2292 ERROR("failed to retrieve the ifindex for %s", peer
);
2293 lxc_netdev_delete_by_name(peer
);
2297 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
2303 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2308 static int instanciate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2310 if (!netdev
->link
) {
2311 ERROR("no link specified for the physical interface");
2315 netdev
->ifindex
= if_nametoindex(netdev
->link
);
2316 if (!netdev
->ifindex
) {
2317 ERROR("failed to retrieve the index for %s", netdev
->link
);
2321 if (netdev
->upscript
) {
2323 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2324 "up", "phys", netdev
->link
, (char*) NULL
);
2332 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2336 if (netdev
->downscript
) {
2337 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2338 "down", "phys", netdev
->link
, (char*) NULL
);
2345 static int instanciate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2347 netdev
->ifindex
= 0;
2348 if (netdev
->upscript
) {
2350 err
= run_script(handler
->name
, "net", netdev
->upscript
,
2351 "up", "empty", (char*) NULL
);
2358 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
2362 if (netdev
->downscript
) {
2363 err
= run_script(handler
->name
, "net", netdev
->downscript
,
2364 "down", "empty", (char*) NULL
);
2371 int lxc_create_network(struct lxc_handler
*handler
)
2373 struct lxc_list
*network
= &handler
->conf
->network
;
2374 struct lxc_list
*iterator
;
2375 struct lxc_netdev
*netdev
;
2377 lxc_list_for_each(iterator
, network
) {
2379 netdev
= iterator
->elem
;
2381 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
2382 ERROR("invalid network configuration type '%d'",
2387 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
2388 ERROR("failed to create netdev");
2397 void lxc_delete_network(struct lxc_handler
*handler
)
2399 struct lxc_list
*network
= &handler
->conf
->network
;
2400 struct lxc_list
*iterator
;
2401 struct lxc_netdev
*netdev
;
2403 lxc_list_for_each(iterator
, network
) {
2404 netdev
= iterator
->elem
;
2406 if (netdev
->ifindex
!= 0 && netdev
->type
== LXC_NET_PHYS
) {
2407 if (lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
))
2408 WARN("failed to rename to the initial name the " \
2409 "netdev '%s'", netdev
->link
);
2413 if (netdev_deconf
[netdev
->type
](handler
, netdev
)) {
2414 WARN("failed to destroy netdev");
2417 /* Recent kernel remove the virtual interfaces when the network
2418 * namespace is destroyed but in case we did not moved the
2419 * interface to the network namespace, we have to destroy it
2421 if (netdev
->ifindex
!= 0 &&
2422 lxc_netdev_delete_by_index(netdev
->ifindex
))
2423 WARN("failed to remove interface '%s'", netdev
->name
);
2427 int lxc_assign_network(struct lxc_list
*network
, pid_t pid
)
2429 struct lxc_list
*iterator
;
2430 struct lxc_netdev
*netdev
;
2433 lxc_list_for_each(iterator
, network
) {
2435 netdev
= iterator
->elem
;
2437 /* empty network namespace, nothing to move */
2438 if (!netdev
->ifindex
)
2441 err
= lxc_netdev_move_by_index(netdev
->ifindex
, pid
);
2443 ERROR("failed to move '%s' to the container : %s",
2444 netdev
->link
, strerror(-err
));
2448 DEBUG("move '%s' to '%d'", netdev
->name
, pid
);
2454 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2456 struct lxc_list
*network
= &handler
->conf
->network
;
2457 struct lxc_list
*iterator
;
2458 struct lxc_netdev
*netdev
;
2461 lxc_list_for_each(iterator
, network
) {
2462 netdev
= iterator
->elem
;
2464 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2467 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2468 ERROR("gateway = auto only supported for "
2469 "veth and macvlan");
2473 if (!netdev
->link
) {
2474 ERROR("gateway = auto needs a link interface");
2478 link_index
= if_nametoindex(netdev
->link
);
2482 if (netdev
->ipv4_gateway_auto
) {
2483 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2484 ERROR("failed to automatically find ipv4 gateway "
2485 "address from link interface '%s'", netdev
->link
);
2490 if (netdev
->ipv6_gateway_auto
) {
2491 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2492 ERROR("failed to automatically find ipv6 gateway "
2493 "address from link interface '%s'", netdev
->link
);
2502 int lxc_create_tty(const char *name
, struct lxc_conf
*conf
)
2504 struct lxc_tty_info
*tty_info
= &conf
->tty_info
;
2507 /* no tty in the configuration */
2511 tty_info
->pty_info
=
2512 malloc(sizeof(*tty_info
->pty_info
)*conf
->tty
);
2513 if (!tty_info
->pty_info
) {
2514 SYSERROR("failed to allocate pty_info");
2518 for (i
= 0; i
< conf
->tty
; i
++) {
2520 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2522 if (openpty(&pty_info
->master
, &pty_info
->slave
,
2523 pty_info
->name
, NULL
, NULL
)) {
2524 SYSERROR("failed to create pty #%d", i
);
2525 tty_info
->nbtty
= i
;
2526 lxc_delete_tty(tty_info
);
2530 DEBUG("allocated pty '%s' (%d/%d)",
2531 pty_info
->name
, pty_info
->master
, pty_info
->slave
);
2533 /* Prevent leaking the file descriptors to the container */
2534 fcntl(pty_info
->master
, F_SETFD
, FD_CLOEXEC
);
2535 fcntl(pty_info
->slave
, F_SETFD
, FD_CLOEXEC
);
2540 tty_info
->nbtty
= conf
->tty
;
2542 INFO("tty's configured");
2547 void lxc_delete_tty(struct lxc_tty_info
*tty_info
)
2551 for (i
= 0; i
< tty_info
->nbtty
; i
++) {
2552 struct lxc_pty_info
*pty_info
= &tty_info
->pty_info
[i
];
2554 close(pty_info
->master
);
2555 close(pty_info
->slave
);
2558 free(tty_info
->pty_info
);
2559 tty_info
->nbtty
= 0;
2562 int lxc_setup(const char *name
, struct lxc_conf
*lxc_conf
)
2564 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2568 if (setup_utsname(lxc_conf
->utsname
)) {
2569 ERROR("failed to setup the utsname for '%s'", name
);
2573 if (setup_network(&lxc_conf
->network
)) {
2574 ERROR("failed to setup the network for '%s'", name
);
2578 if (run_lxc_hooks(name
, "pre-mount", lxc_conf
)) {
2579 ERROR("failed to run pre-mount hooks for container '%s'.", name
);
2583 if (setup_rootfs(lxc_conf
)) {
2584 ERROR("failed to setup rootfs for '%s'", name
);
2588 if (lxc_conf
->autodev
) {
2589 if (mount_autodev(lxc_conf
->rootfs
.mount
)) {
2590 ERROR("failed to mount /dev in the container");
2595 if (setup_mount(&lxc_conf
->rootfs
, lxc_conf
->fstab
, name
)) {
2596 ERROR("failed to setup the mounts for '%s'", name
);
2600 if (setup_mount_entries(&lxc_conf
->rootfs
, &lxc_conf
->mount_list
, name
)) {
2601 ERROR("failed to setup the mount entries for '%s'", name
);
2605 if (run_lxc_hooks(name
, "mount", lxc_conf
)) {
2606 ERROR("failed to run mount hooks for container '%s'.", name
);
2610 if (lxc_conf
->autodev
) {
2611 if (setup_autodev(lxc_conf
->rootfs
.mount
)) {
2612 ERROR("failed to populate /dev in the container");
2617 if (setup_cgroup(name
, &lxc_conf
->cgroup
)) {
2618 ERROR("failed to setup the cgroups for '%s'", name
);
2622 if (setup_console(&lxc_conf
->rootfs
, &lxc_conf
->console
, lxc_conf
->ttydir
)) {
2623 ERROR("failed to setup the console for '%s'", name
);
2627 if (setup_kmsg(&lxc_conf
->rootfs
, &lxc_conf
->console
)) // don't fail
2628 ERROR("failed to setup kmsg for '%s'", name
);
2630 if (setup_tty(&lxc_conf
->rootfs
, &lxc_conf
->tty_info
, lxc_conf
->ttydir
)) {
2631 ERROR("failed to setup the ttys for '%s'", name
);
2635 #if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2636 INFO("rootfs path is .%s., mount is .%s.", lxc_conf
->rootfs
.path
,
2637 lxc_conf
->rootfs
.mount
);
2638 if (lxc_conf
->rootfs
.path
== NULL
|| strlen(lxc_conf
->rootfs
.path
) == 0)
2641 mounted
= lsm_mount_proc_if_needed(lxc_conf
->rootfs
.path
, lxc_conf
->rootfs
.mount
);
2642 if (mounted
== -1) {
2643 SYSERROR("failed to mount /proc in the container.");
2645 } else if (mounted
== 1) {
2646 lxc_conf
->lsm_umount_proc
= 1;
2650 if (setup_pivot_root(&lxc_conf
->rootfs
)) {
2651 ERROR("failed to set rootfs for '%s'", name
);
2655 if (setup_pts(lxc_conf
->pts
)) {
2656 ERROR("failed to setup the new pts instance");
2660 if (setup_personality(lxc_conf
->personality
)) {
2661 ERROR("failed to setup personality");
2665 if (setup_caps(&lxc_conf
->caps
)) {
2666 ERROR("failed to drop capabilities");
2670 NOTICE("'%s' is setup.", name
);
2675 int run_lxc_hooks(const char *name
, char *hook
, struct lxc_conf
*conf
)
2678 struct lxc_list
*it
;
2680 if (strcmp(hook
, "pre-start") == 0)
2681 which
= LXCHOOK_PRESTART
;
2682 else if (strcmp(hook
, "pre-mount") == 0)
2683 which
= LXCHOOK_PREMOUNT
;
2684 else if (strcmp(hook
, "mount") == 0)
2685 which
= LXCHOOK_MOUNT
;
2686 else if (strcmp(hook
, "start") == 0)
2687 which
= LXCHOOK_START
;
2688 else if (strcmp(hook
, "post-stop") == 0)
2689 which
= LXCHOOK_POSTSTOP
;
2692 lxc_list_for_each(it
, &conf
->hooks
[which
]) {
2694 char *hookname
= it
->elem
;
2695 ret
= run_script(name
, "lxc", hookname
, hook
, NULL
);
2702 static void lxc_remove_nic(struct lxc_list
*it
)
2704 struct lxc_netdev
*netdev
= it
->elem
;
2705 struct lxc_list
*it2
,*next
;
2713 if (netdev
->upscript
)
2714 free(netdev
->upscript
);
2716 free(netdev
->hwaddr
);
2719 if (netdev
->ipv4_gateway
)
2720 free(netdev
->ipv4_gateway
);
2721 if (netdev
->ipv6_gateway
)
2722 free(netdev
->ipv6_gateway
);
2723 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2728 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2737 /* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
2738 int lxc_clear_nic(struct lxc_conf
*c
, const char *key
)
2742 struct lxc_list
*it
;
2743 struct lxc_netdev
*netdev
;
2745 p1
= index(key
, '.');
2746 if (!p1
|| *(p1
+1) == '\0')
2749 ret
= sscanf(key
, "%d", &idx
);
2750 if (ret
!= 1) return -1;
2755 lxc_list_for_each(it
, &c
->network
) {
2760 if (i
< idx
) // we don't have that many nics defined
2763 if (!it
|| !it
->elem
)
2770 } else if (strcmp(p1
, "ipv4") == 0) {
2771 struct lxc_list
*it2
,*next
;
2772 lxc_list_for_each_safe(it2
, &netdev
->ipv4
, next
) {
2777 } else if (strcmp(p1
, "ipv6") == 0) {
2778 struct lxc_list
*it2
,*next
;
2779 lxc_list_for_each_safe(it2
, &netdev
->ipv6
, next
) {
2784 } else if (strcmp(p1
, "link") == 0) {
2787 netdev
->link
= NULL
;
2789 } else if (strcmp(p1
, "name") == 0) {
2792 netdev
->name
= NULL
;
2794 } else if (strcmp(p1
, "script.up") == 0) {
2795 if (netdev
->upscript
) {
2796 free(netdev
->upscript
);
2797 netdev
->upscript
= NULL
;
2799 } else if (strcmp(p1
, "hwaddr") == 0) {
2800 if (netdev
->hwaddr
) {
2801 free(netdev
->hwaddr
);
2802 netdev
->hwaddr
= NULL
;
2804 } else if (strcmp(p1
, "mtu") == 0) {
2809 } else if (strcmp(p1
, "ipv4_gateway") == 0) {
2810 if (netdev
->ipv4_gateway
) {
2811 free(netdev
->ipv4_gateway
);
2812 netdev
->ipv4_gateway
= NULL
;
2814 } else if (strcmp(p1
, "ipv6_gateway") == 0) {
2815 if (netdev
->ipv6_gateway
) {
2816 free(netdev
->ipv6_gateway
);
2817 netdev
->ipv6_gateway
= NULL
;
2825 int lxc_clear_config_network(struct lxc_conf
*c
)
2827 struct lxc_list
*it
,*next
;
2828 lxc_list_for_each_safe(it
, &c
->network
, next
) {
2834 int lxc_clear_config_caps(struct lxc_conf
*c
)
2836 struct lxc_list
*it
,*next
;
2838 lxc_list_for_each_safe(it
, &c
->caps
, next
) {
2846 int lxc_clear_cgroups(struct lxc_conf
*c
, const char *key
)
2848 struct lxc_list
*it
,*next
;
2850 const char *k
= key
+ 11;
2852 if (strcmp(key
, "lxc.cgroup") == 0)
2855 lxc_list_for_each_safe(it
, &c
->cgroup
, next
) {
2856 struct lxc_cgroup
*cg
= it
->elem
;
2857 if (!all
&& strcmp(cg
->subsystem
, k
) != 0)
2860 free(cg
->subsystem
);
2868 int lxc_clear_mount_entries(struct lxc_conf
*c
)
2870 struct lxc_list
*it
,*next
;
2872 lxc_list_for_each_safe(it
, &c
->mount_list
, next
) {
2880 int lxc_clear_hooks(struct lxc_conf
*c
, const char *key
)
2882 struct lxc_list
*it
,*next
;
2883 bool all
= false, done
= false;
2884 const char *k
= key
+ 9;
2887 if (strcmp(key
, "lxc.hook") == 0)
2890 for (i
=0; i
<NUM_LXC_HOOKS
; i
++) {
2891 if (all
|| strcmp(k
, lxchook_names
[i
]) == 0) {
2892 lxc_list_for_each_safe(it
, &c
->hooks
[i
], next
) {
2902 ERROR("Invalid hook key: %s", key
);
2908 void lxc_clear_saved_nics(struct lxc_conf
*conf
)
2912 if (!conf
->num_savednics
)
2914 for (i
=0; i
< conf
->num_savednics
; i
++)
2915 free(conf
->saved_nics
[i
].orig_name
);
2916 conf
->saved_nics
= 0;
2917 free(conf
->saved_nics
);
2920 void lxc_conf_free(struct lxc_conf
*conf
)
2924 if (conf
->console
.path
)
2925 free(conf
->console
.path
);
2926 if (conf
->rootfs
.mount
!= default_rootfs_mount
)
2927 free(conf
->rootfs
.mount
);
2928 if (conf
->rootfs
.path
)
2929 free(conf
->rootfs
.path
);
2931 free(conf
->utsname
);
2937 free(conf
->logfile
);
2938 lxc_clear_config_network(conf
);
2940 if (conf
->aa_profile
)
2941 free(conf
->aa_profile
);
2943 lxc_seccomp_free(conf
);
2944 lxc_clear_config_caps(conf
);
2945 lxc_clear_cgroups(conf
, "lxc.cgroup");
2946 lxc_clear_hooks(conf
, "lxc.hook");
2947 lxc_clear_mount_entries(conf
);
2948 lxc_clear_saved_nics(conf
);