From 2bcecfbcffa062be3cece6152ea24d109cc91fda Mon Sep 17 00:00:00 2001 From: BianTanggui Date: Thu, 30 Jul 2020 21:07:02 +0800 Subject: [PATCH] Match-id-77d0dcf76c49b84511b8f24c2acab8d001fbf905 --- cli/src/basic.h | 7 ++- cli/src/mount.c | 119 ++++++++++++++++++++++++++++++++++++------------ cli/src/utils.c | 20 +++++--- cli/src/utils.h | 2 +- 4 files changed, 111 insertions(+), 37 deletions(-) diff --git a/cli/src/basic.h b/cli/src/basic.h index 8f188dc..b930316 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -12,8 +12,13 @@ #define DAVINCI_MANAGER "davinci_manager" #define DEVMM_SVM "devmm_svm" #define HISI_HDC "hisi_hdc" -#define ASCEND_DRIVER_PATH "/usr/local/Ascend/driver" +#define ASCEND_DRIVER_LIB64_PATH "/usr/local/Ascend/driver/lib64" +#define ASCEND_DRIVER_TOOLS_PATH "/usr/local/Ascend/driver/tools" +#define ASCEND_DRIVER_INC_PATH "/usr/local/Ascend/driver/include" #define ASCEND_ADDONS_PATH "/usr/local/Ascend/add-ons" +#define ASCEND_DCMI_PATH "/usr/local/dcmi" +#define ASCEND_NPU_SMI_PATH "/usr/local/sbin/npu-smi" +#define ASCEND_SLOG_CONF_PATH "/var/log/npu/conf/slog/slog.conf" #define DEFAULT_DIR_MODE 0755 #define BUF_SIZE 1024 #define MAX_DEVICE_NR 1024 diff --git a/cli/src/mount.c b/cli/src/mount.c index 8ea900a..30dd7ca 100644 --- a/cli/src/mount.c +++ b/cli/src/mount.c @@ -133,6 +133,39 @@ int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr) return 0; } +int MountFile(const char *rootfs, const char *filepath) +{ + int ret; + char dst[BUF_SIZE] = {0}; + + ret = sprintf_s(dst, BUF_SIZE, "%s%s", rootfs, filepath); + if (ret < 0) { + LogError("error: failed to assemble file mounting path, file: %s.", filepath); + return -1; + } + + struct stat srcStat; + ret = stat(filepath, &srcStat); + if (ret < 0) { + LogError("error: failed to stat src: %s.", filepath); + return -1; + } + + ret = CreateFile(dst, srcStat.st_mode); + if (ret < 0) { + LogError("error: failed to create mount dst file: %s.", dst); + return -1; + } + + ret = Mount(filepath, dst); + if (ret < 0) { + LogError("error: failed to mount dev."); + return -1; + } + + return 0; +} + int MountDir(const char *rootfs, const char *src) { int ret; @@ -149,25 +182,10 @@ int MountDir(const char *rootfs, const char *src) return -1; } - /* directory */ - char parentDir[BUF_SIZE] = {0}; - GetParentPathStr(dst, parentDir, BUF_SIZE); - if (CheckDirExists(parentDir) < 0) { - mode_t parentMode = DEFAULT_DIR_MODE; - ret = MakeParentDir(parentDir, parentMode); - if (ret < 0) { - LogError("error: failed to make dir: %s.", parentDir); - return -1; - } - } - - if (CheckDirExists(dst) < 0) { - const mode_t curMode = srcStat.st_mode; - ret = MkDir(dst, curMode); - if (ret < 0) { - LogError("error: failed to make dir: %s.", dst); - return -1; - } + ret = MakeDirWithParent(dst, DEFAULT_DIR_MODE); + if (ret < 0) { + LogError("error: failed to make dir: %s.", dst); + return -1; } ret = Mount(src, dst); @@ -206,9 +224,21 @@ int DoCtrlDeviceMounting(const char *rootfs) int DoDirectoryMounting(const char *rootfs) { /* directory */ - int ret = MountDir(rootfs, ASCEND_DRIVER_PATH); + int ret = MountDir(rootfs, ASCEND_DRIVER_LIB64_PATH); if (ret < 0) { - LogError("error: failed to do mount %s.", ASCEND_DRIVER_PATH); + LogError("error: failed to do mount %s.", ASCEND_DRIVER_LIB64_PATH); + return -1; + } + + ret = MountDir(rootfs, ASCEND_DRIVER_TOOLS_PATH); + if (ret < 0) { + LogError("error: failed to do mount %s.", ASCEND_DRIVER_TOOLS_PATH); + return -1; + } + + ret = MountDir(rootfs, ASCEND_DRIVER_INC_PATH); + if (ret < 0) { + LogError("error: failed to do mount %s.", ASCEND_DRIVER_INC_PATH); return -1; } @@ -218,6 +248,29 @@ int DoDirectoryMounting(const char *rootfs) return -1; } + ret = MountDir(rootfs, ASCEND_DCMI_PATH); + if (ret < 0) { + LogError("error: failed to do mount %s.", ASCEND_DCMI_PATH); + return -1; + } + + return 0; +} + +int DoFileMounting(const char *rootfs) +{ + int ret = MountFile(rootfs, ASCEND_NPU_SMI_PATH); + if (ret < 0) { + LogError("error: failed to do mount %s.", ASCEND_NPU_SMI_PATH); + return -1; + } + + MountFile(rootfs, ASCEND_SLOG_CONF_PATH); + if (ret < 0) { + LogError("error: failed to do mount %s.", ASCEND_SLOG_CONF_PATH); + return -1; + } + return 0; } @@ -227,22 +280,30 @@ int DoMounting(const struct ParsedConfig *config) ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr); if (ret < 0) { - LogError("error: failed to do mounts."); + LogError("error: failed to mount devices."); return -1; } ret = DoCtrlDeviceMounting(config->rootfs); if (ret < 0) { - LogError("error: failed to do mount files."); + LogError("error: failed to mount ctrl devices."); return -1; } - if (!IsOptionNoDrvSet()) { - ret = DoDirectoryMounting(config->rootfs); - if (ret < 0) { - LogError("error: failed to do mount directory."); - return -1; - } + if (IsOptionNoDrvSet()) { + return 0; + } + + ret = DoFileMounting(config->rootfs); + if (ret < 0) { + LogError("error: failed to mount files."); + return -1; + } + + ret = DoDirectoryMounting(config->rootfs); + if (ret < 0) { + LogError("error: failed to do mount directories."); + return -1; } return 0; diff --git a/cli/src/utils.c b/cli/src/utils.c index adf6d4b..85bfb36 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -71,7 +71,7 @@ int GetParentPathStr(const char *path, char *parent, size_t bufSize) return 0; } -int MakeParentDir(const char *path, mode_t mode) +int MakeDirWithParent(const char *path, mode_t mode) { if (*path == '\0' || *path == '.') { return 0; @@ -82,15 +82,13 @@ int MakeParentDir(const char *path, mode_t mode) char parentPath[BUF_SIZE] = {0}; GetParentPathStr(path, parentPath, BUF_SIZE); - if (strlen(parentPath) > 0 && MakeParentDir(parentPath, mode) < 0) { + if (strlen(parentPath) > 0 && MakeDirWithParent(parentPath, mode) < 0) { return -1; } - struct stat s; - int ret = stat(path, &s); + int ret = MkDir(path, mode); if (ret < 0) { - LogError("error: failed to stat path: %s.", path); - return (MkDir(path, mode)); + return -1; } return 0; @@ -98,6 +96,16 @@ int MakeParentDir(const char *path, mode_t mode) int CreateFile(const char *path, mode_t mode) { + /* directory */ + char parentDir[BUF_SIZE] = {0}; + GetParentPathStr(path, parentDir, BUF_SIZE); + + int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE); + if (ret < 0) { + LogError("error: failed to make parent dir for file: %s", path); + return -1; + } + char resolvedPath[PATH_MAX] = {0}; if (realpath(path, resolvedPath) == NULL && errno != ENOENT) { LogError("error: failed to resolve path %s.", path); diff --git a/cli/src/utils.h b/cli/src/utils.h index 39b59ae..29dc8d7 100644 --- a/cli/src/utils.h +++ b/cli/src/utils.h @@ -15,7 +15,7 @@ int MkDir(const char *dir, int mode); int VerifyPathInfo(const struct PathInfo* pathInfo); int CheckDirExists(const char *dir); int GetParentPathStr(const char *path, char *parent, size_t bufSize); -int MakeParentDir(const char *path, mode_t mode); +int MakeDirWithParent(const char *path, mode_t mode); int CreateFile(const char *path, mode_t mode); #endif \ No newline at end of file