diff --git a/cli/src/basic.h b/cli/src/basic.h index 3335cb1..ab5d288 100644 --- a/cli/src/basic.h +++ b/cli/src/basic.h @@ -10,6 +10,7 @@ #include #define DEVICE_NAME "davinci" +#define VDEVICE_NAME "vdavinci" #define DAVINCI_MANAGER "davinci_manager" #define DEVMM_SVM "devmm_svm" #define HISI_HDC "hisi_hdc" diff --git a/cli/src/cgrp.c b/cli/src/cgrp.c index dc60394..4783c02 100644 --- a/cli/src/cgrp.c +++ b/cli/src/cgrp.c @@ -8,12 +8,16 @@ #include #include #include -#include #include + +#include #include #include + #include "securec.h" + #include "utils.h" +#include "options.h" bool TakeNthWord(char **pLine, unsigned int n, char **word) { @@ -268,7 +272,9 @@ int SetupCgroup(const struct ParsedConfig *config) } for (size_t idx = 0; idx < config->devicesNr; idx++) { - ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, config->devices[idx]); + int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", + (IsVirtual() ? VDEVICE_NAME : DEVICE_NAME), + config->devices[idx]); if (ret < 0) { fclose(cgroupAllow); LOG_ERROR("error: failed to assemble device path for no.%u.", config->devices[idx]); diff --git a/cli/src/main.c b/cli/src/main.c index 50dc311..662d578 100644 --- a/cli/src/main.c +++ b/cli/src/main.c @@ -16,7 +16,7 @@ #include "basic.h" #include "ns.h" -#include "mount.h" +#include "u_mount.h" #include "cgrp.h" #include "options.h" diff --git a/cli/src/options.c b/cli/src/options.c index 7ab75d9..144e3d3 100644 --- a/cli/src/options.c +++ b/cli/src/options.c @@ -10,6 +10,7 @@ static struct { bool noDrv; + bool isVirtual; } g_runtimeOptions; static struct { @@ -17,6 +18,7 @@ static struct { bool *flag; } g_optionNameFlagTable[] = { {"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver + {"VIRTUAL", &g_runtimeOptions.isVirtual}, {NULL, NULL} }; @@ -24,23 +26,21 @@ void ParseRuntimeOptions(const char *options) { // set defaults value g_runtimeOptions.noDrv = false; + g_runtimeOptions.isVirtual = false; static const char *seperator = ","; char *runtimeOptions = strdup(options); char *context = NULL; char *token = NULL; - token = strtok_s(runtimeOptions, seperator, &context); - while (token != NULL) { + for (token = strtok_s(runtimeOptions, seperator, &context); + token != NULL; + token = strtok_s(NULL, seperator, &context)) { for (int i = 0; g_optionNameFlagTable[i].name != NULL; i++) { - if (strcmp((const char *)token, g_optionNameFlagTable[i].name)) { - continue; + if (!strcmp((const char *)token, g_optionNameFlagTable[i].name)) { + *g_optionNameFlagTable[i].flag = true; } - - *g_optionNameFlagTable[i].flag = true; } - - token = strtok_s(NULL, seperator, &context); } free(runtimeOptions); @@ -50,3 +50,8 @@ bool IsOptionNoDrvSet() { return g_runtimeOptions.noDrv; } + +bool IsVirtual() +{ + return g_runtimeOptions.isVirtual; +} diff --git a/cli/src/options.h b/cli/src/options.h index ab7ddda..e30eda1 100644 --- a/cli/src/options.h +++ b/cli/src/options.h @@ -9,5 +9,6 @@ void ParseRuntimeOptions(const char *options); bool IsOptionNoDrvSet(); +bool IsVirtual(); #endif diff --git a/cli/src/mount.c b/cli/src/u_mount.c similarity index 80% rename from cli/src/mount.c rename to cli/src/u_mount.c index a05dc56..7670794 100644 --- a/cli/src/mount.c +++ b/cli/src/u_mount.c @@ -2,7 +2,7 @@ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * Description: ascend-docker-cli工具容器设备与驱动挂载模块 */ -#include "mount.h" +#include "u_mount.h" #include #include @@ -10,6 +10,8 @@ #include #include #include "securec.h" + +#include "basic.h" #include "utils.h" #include "options.h" @@ -34,8 +36,8 @@ int Mount(const char *src, const char *dst) return 0; } -static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, - struct PathInfo* pathInfo) +static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName, + const char *dstDeviceName, struct PathInfo* pathInfo) { int ret; errno_t err; @@ -52,7 +54,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, char *src = pathInfo->src; char *dst = pathInfo->dst; - ret = sprintf_s(src, srcBufSize, "/dev/%s", deviceName); + ret = sprintf_s(src, srcBufSize, "/dev/%s", srcDeviceName); if (ret < 0) { return -1; } @@ -67,7 +69,15 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, return -1; } - err = strcpy_s(dst, dstBufSize, (const char *)resolvedDst); + if (dstDeviceName != NULL) { + ret = sprintf_s(dst, dstBufSize, "%s/dev/%s", rootfs, dstDeviceName); + if (ret < 0) { + return -1; + } + } else { + err = strcpy_s(dst, dstBufSize, resolvedDst); + } + if (err != EOK) { LOG_ERROR("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst); return -1; @@ -76,16 +86,16 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, return 0; } -int MountDevice(const char *rootfs, const char *deviceName) +int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDeviceName) { int ret; char src[BUF_SIZE] = {0}; char dst[BUF_SIZE] = {0}; struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE}; - ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo); + ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo); if (ret < 0) { - LOG_ERROR("error: failed to get device mount src and(or) dst path, device name: %s.", deviceName); + LOG_ERROR("error: failed to get device mount src and(or) dst path, device name: %s.", srcDeviceName); return -1; } @@ -124,20 +134,21 @@ int MountDevice(const char *rootfs, const char *deviceName) return 0; } -int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr) +int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) { - char deviceName[BUF_SIZE] = {0}; + char srcDeviceName[BUF_SIZE] = {0}; + char dstDeviceName[BUF_SIZE] = {0}; for (size_t idx = 0; idx < idsNr; idx++) { - int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]); - if (ret < 0) { + int srcRet = sprintf_s(srcDeviceName, BUF_SIZE, "%s%u", device_name, ids[idx]); + int dstRet = sprintf_s(dstDeviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]); + if (srcRet < 0 || dstRet < 0) { LOG_ERROR("error: assemble device name failed, id: %u.", ids[idx]); return -1; } - - ret = MountDevice(rootfs, deviceName); + int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName); if (ret < 0) { - LOG_ERROR("error: failed to mount device %s.", deviceName); + LOG_ERROR("error: failed to mount device %s.", srcDeviceName); return -1; } } @@ -211,19 +222,19 @@ int MountDir(const char *rootfs, const char *src) int DoCtrlDeviceMounting(const char *rootfs) { /* device */ - int ret = MountDevice(rootfs, DAVINCI_MANAGER); + int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL); if (ret < 0) { LOG_ERROR("error: failed to mount device %s.", DAVINCI_MANAGER); return -1; } - ret = MountDevice(rootfs, DEVMM_SVM); + ret = MountDevice(rootfs, DEVMM_SVM, NULL); if (ret < 0) { LOG_ERROR("error: failed to mount device %s.", DEVMM_SVM); return -1; } - ret = MountDevice(rootfs, HISI_HDC); + ret = MountDevice(rootfs, HISI_HDC, NULL); if (ret < 0) { LOG_ERROR("error: failed to mount device %s.", HISI_HDC); return -1; @@ -265,8 +276,9 @@ int DoFileMounting(const char *rootfs, const struct MountList *list) int DoMounting(const struct ParsedConfig *config) { int ret; - - ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr); + ret = DoDeviceMounting(config->rootfs, + (IsVirtual() ? VDEVICE_NAME : DEVICE_NAME), + config->devices, config->devicesNr); if (ret < 0) { LOG_ERROR("error: failed to mount devices."); return -1; diff --git a/cli/src/mount.h b/cli/src/u_mount.h similarity index 100% rename from cli/src/mount.h rename to cli/src/u_mount.h diff --git a/cli/test/dt/testcase/gtest_mytest.cpp b/cli/test/dt/testcase/gtest_mytest.cpp index 22b3558..3f24791 100644 --- a/cli/test/dt/testcase/gtest_mytest.cpp +++ b/cli/test/dt/testcase/gtest_mytest.cpp @@ -15,6 +15,7 @@ using namespace std; using namespace testing; #define DAVINCI_MANAGER_PATH "/dev/davinci_manager" +#define DEVICE_NAME "davinci" #define BUF_SIZE 1024 #define MAX_DEVICE_NR 1024 #define MAX_MOUNT_NR 512 @@ -36,7 +37,7 @@ extern "C" int GetNsPath(const int pid, const char *nsType, char *buf, size_t bu extern "C" int GetSelfNsPath(const char *nsType, char *buf, size_t bufSize); extern "C" int EnterNsByPath(const char *path, int nsType); extern "C" int MountDevice(const char *rootfs, const char *deviceName); -extern "C" int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr); +extern "C" int DoDeviceMounting(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr); extern "C" int CheckDirExists(char *dir, int len); extern "C" int GetParentPathStr(const char *path, char *parent, size_t bufSize); extern "C" int MakeDirWithParent(const char *path, mode_t mode); @@ -57,6 +58,7 @@ extern "C" int DoDirectoryMounting(const char *rootfs, const struct MountList *l extern "C" int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config); extern "C" int ParseRuntimeOptions(const char *options); extern "C" bool IsOptionNoDrvSet(); +extern "C" bool IsVirtual(); struct MountList { unsigned int count; @@ -215,12 +217,12 @@ int Stub_EnterNsByPath_Failed(const char *path, int nsType) return 0; } -int Stub_DoDeviceMounting_Success(const char *rootfs, const unsigned int ids[], size_t idsNr) +int Stub_DoDeviceMounting_Success(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) { return 0; } -int Stub_DoDeviceMounting_Failed(const char *rootfs, const unsigned int ids[], size_t idsNr) +int Stub_DoDeviceMounting_Failed(const char *rootfs, const char *device_name, const unsigned int ids[], size_t idsNr) { return -1; } @@ -457,7 +459,8 @@ TEST(DoDeviceMounting, StatusOne) char *rootfs = "/home"; unsigned int devicesList[2] = {1, 2}; size_t idNr = 2; - int ret = DoDeviceMounting(rootfs, devicesList, idNr); + char *device_name = "davinci"; + int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr); GlobalMockObject::verify(); EXPECT_EQ(0, ret); } @@ -468,7 +471,8 @@ TEST(DoDeviceMounting, StatusTwo) char *rootfs = "/home"; unsigned int devicesList[2] = {1, 2}; size_t idNr = 2; - int ret = DoDeviceMounting(rootfs, devicesList, idNr); + char *device_name = "davinci"; + int ret = DoDeviceMounting(rootfs, device_name, devicesList, idNr); GlobalMockObject::verify(); EXPECT_EQ(-1, ret); } diff --git a/hook/main.go b/hook/main.go index 1fcaf6e..c3c9453 100644 --- a/hook/main.go +++ b/hook/main.go @@ -45,6 +45,7 @@ var ( var validRuntimeOptions = [...]string{ "NODRV", + "VIRTUAL", } type containerConfig struct {