From 9735e11b3ecbaab9c3028d467c2b1706bcaff7bc Mon Sep 17 00:00:00 2001
From: wujing <wujing50@huawei.com>
Date: Tue, 10 Dec 2019 21:28:47 +0800
Subject: [PATCH 128/131] link /proc/mounts to /etc/mtab

Signed-off-by: wujing <wujing50@huawei.com>
---
 src/lxc/conf.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 21ec340f..b66e7bcf 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -4101,6 +4101,37 @@ static int setup_rootfs_mountopts(const struct lxc_rootfs *rootfs)
 	return 0;
 }
 
+static int create_mtab_link()
+{
+	ssize_t ret;
+	int mret;
+	struct stat sbuf;
+	const char *pathname = "/proc/mounts";
+	const char *slink = "/etc/mtab";
+
+	if (file_exists(slink)) {
+		return 0;
+	}
+
+	ret = stat(pathname, &sbuf);
+	if (ret < 0) {
+		SYSERROR("Failed to stat %s: %s", pathname, strerror(errno));
+		return -1;
+	}
+
+	mret = symlink(pathname, slink);
+	if (mret < 0 && errno != EEXIST) {
+		if (errno == EROFS) {
+			WARN("Failed to create link %s for target %s. Read-only filesystem", slink, pathname);
+		} else {
+			SYSERROR("Failed to create \"%s\"", slink);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 int lxc_setup(struct lxc_handler *handler)
 {
 	int ret;
@@ -4331,7 +4362,7 @@ int lxc_setup(struct lxc_handler *handler)
 		}
 	}
 
-	//isulad: system container, remount /proc/sys/xxx by mount_list
+	// isulad: system container, remount /proc/sys/xxx by mount_list
 	if (lxc_conf->systemd != NULL && strcmp(lxc_conf->systemd, "true") == 0) {
 		if (!lxc_list_empty(&lxc_conf->mount_list)) {
 			if (remount_proc_sys_mount_entries(&lxc_conf->mount_list)) {
@@ -4341,6 +4372,12 @@ int lxc_setup(struct lxc_handler *handler)
 		}
 	}
 
+	// isulad: create link /etc/mtab for /proc/mounts
+	if (create_mtab_link() != 0) {
+		ERROR("failed to create link /etc/mtab for target /proc/mounts");
+		goto on_error;
+	}
+
 	if (!lxc_list_empty(&lxc_conf->keepcaps)) {
 		if (!lxc_list_empty(&lxc_conf->caps)) {
 			ERROR("Container requests lxc.cap.drop and "
-- 
2.23.0

