Match-id-2cc585b8e7d972e49b1798f0745ac2acaaedd2e2

This commit is contained in:
BianTanggui
2021-02-07 10:42:00 +08:00
parent ac1f3b9e31
commit 2f8e7977e8
9 changed files with 66 additions and 36 deletions

View File

@@ -10,6 +10,7 @@
#include <limits.h> #include <limits.h>
#define DEVICE_NAME "davinci" #define DEVICE_NAME "davinci"
#define VDEVICE_NAME "vdavinci"
#define DAVINCI_MANAGER "davinci_manager" #define DAVINCI_MANAGER "davinci_manager"
#define DEVMM_SVM "devmm_svm" #define DEVMM_SVM "devmm_svm"
#define HISI_HDC "hisi_hdc" #define HISI_HDC "hisi_hdc"

View File

@@ -8,12 +8,16 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "securec.h" #include "securec.h"
#include "utils.h" #include "utils.h"
#include "options.h"
bool TakeNthWord(char **pLine, unsigned int n, char **word) 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++) { 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) { if (ret < 0) {
fclose(cgroupAllow); fclose(cgroupAllow);
LOG_ERROR("error: failed to assemble device path for no.%u.", config->devices[idx]); LOG_ERROR("error: failed to assemble device path for no.%u.", config->devices[idx]);

View File

@@ -16,7 +16,7 @@
#include "basic.h" #include "basic.h"
#include "ns.h" #include "ns.h"
#include "mount.h" #include "u_mount.h"
#include "cgrp.h" #include "cgrp.h"
#include "options.h" #include "options.h"

View File

@@ -10,6 +10,7 @@
static struct { static struct {
bool noDrv; bool noDrv;
bool isVirtual;
} g_runtimeOptions; } g_runtimeOptions;
static struct { static struct {
@@ -17,6 +18,7 @@ static struct {
bool *flag; bool *flag;
} g_optionNameFlagTable[] = { } g_optionNameFlagTable[] = {
{"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver {"NODRV", &g_runtimeOptions.noDrv}, // 不挂载Driver
{"VIRTUAL", &g_runtimeOptions.isVirtual},
{NULL, NULL} {NULL, NULL}
}; };
@@ -24,23 +26,21 @@ void ParseRuntimeOptions(const char *options)
{ {
// set defaults value // set defaults value
g_runtimeOptions.noDrv = false; g_runtimeOptions.noDrv = false;
g_runtimeOptions.isVirtual = false;
static const char *seperator = ","; static const char *seperator = ",";
char *runtimeOptions = strdup(options); char *runtimeOptions = strdup(options);
char *context = NULL; char *context = NULL;
char *token = NULL; char *token = NULL;
token = strtok_s(runtimeOptions, seperator, &context); for (token = strtok_s(runtimeOptions, seperator, &context);
while (token != NULL) { token != NULL;
token = strtok_s(NULL, seperator, &context)) {
for (int i = 0; g_optionNameFlagTable[i].name != NULL; i++) { for (int i = 0; g_optionNameFlagTable[i].name != NULL; i++) {
if (strcmp((const char *)token, g_optionNameFlagTable[i].name)) { if (!strcmp((const char *)token, g_optionNameFlagTable[i].name)) {
continue; *g_optionNameFlagTable[i].flag = true;
} }
*g_optionNameFlagTable[i].flag = true;
} }
token = strtok_s(NULL, seperator, &context);
} }
free(runtimeOptions); free(runtimeOptions);
@@ -50,3 +50,8 @@ bool IsOptionNoDrvSet()
{ {
return g_runtimeOptions.noDrv; return g_runtimeOptions.noDrv;
} }
bool IsVirtual()
{
return g_runtimeOptions.isVirtual;
}

View File

@@ -9,5 +9,6 @@
void ParseRuntimeOptions(const char *options); void ParseRuntimeOptions(const char *options);
bool IsOptionNoDrvSet(); bool IsOptionNoDrvSet();
bool IsVirtual();
#endif #endif

View File

@@ -2,7 +2,7 @@
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli工具容器设备与驱动挂载模块 * Description: ascend-docker-cli工具容器设备与驱动挂载模块
*/ */
#include "mount.h" #include "u_mount.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
@@ -10,6 +10,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mount.h> #include <sys/mount.h>
#include "securec.h" #include "securec.h"
#include "basic.h"
#include "utils.h" #include "utils.h"
#include "options.h" #include "options.h"
@@ -34,8 +36,8 @@ int Mount(const char *src, const char *dst)
return 0; return 0;
} }
static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName, static int GetDeviceMntSrcDst(const char *rootfs, const char *srcDeviceName,
struct PathInfo* pathInfo) const char *dstDeviceName, struct PathInfo* pathInfo)
{ {
int ret; int ret;
errno_t err; errno_t err;
@@ -52,7 +54,7 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
char *src = pathInfo->src; char *src = pathInfo->src;
char *dst = pathInfo->dst; char *dst = pathInfo->dst;
ret = sprintf_s(src, srcBufSize, "/dev/%s", deviceName); ret = sprintf_s(src, srcBufSize, "/dev/%s", srcDeviceName);
if (ret < 0) { if (ret < 0) {
return -1; return -1;
} }
@@ -67,7 +69,15 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
return -1; 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) { if (err != EOK) {
LOG_ERROR("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst); LOG_ERROR("error: failed to copy resolved device mnt path to dst: %s.", resolvedDst);
return -1; return -1;
@@ -76,16 +86,16 @@ static int GetDeviceMntSrcDst(const char *rootfs, const char *deviceName,
return 0; return 0;
} }
int MountDevice(const char *rootfs, const char *deviceName) int MountDevice(const char *rootfs, const char *srcDeviceName, const char *dstDeviceName)
{ {
int ret; int ret;
char src[BUF_SIZE] = {0}; char src[BUF_SIZE] = {0};
char dst[BUF_SIZE] = {0}; char dst[BUF_SIZE] = {0};
struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE}; struct PathInfo pathInfo = {src, BUF_SIZE, dst, BUF_SIZE};
ret = GetDeviceMntSrcDst(rootfs, deviceName, &pathInfo); ret = GetDeviceMntSrcDst(rootfs, srcDeviceName, dstDeviceName, &pathInfo);
if (ret < 0) { 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; return -1;
} }
@@ -124,20 +134,21 @@ int MountDevice(const char *rootfs, const char *deviceName)
return 0; 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++) { for (size_t idx = 0; idx < idsNr; idx++) {
int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]); int srcRet = sprintf_s(srcDeviceName, BUF_SIZE, "%s%u", device_name, ids[idx]);
if (ret < 0) { 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]); LOG_ERROR("error: assemble device name failed, id: %u.", ids[idx]);
return -1; return -1;
} }
int ret = MountDevice(rootfs, srcDeviceName, dstDeviceName);
ret = MountDevice(rootfs, deviceName);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", deviceName); LOG_ERROR("error: failed to mount device %s.", srcDeviceName);
return -1; return -1;
} }
} }
@@ -211,19 +222,19 @@ int MountDir(const char *rootfs, const char *src)
int DoCtrlDeviceMounting(const char *rootfs) int DoCtrlDeviceMounting(const char *rootfs)
{ {
/* device */ /* device */
int ret = MountDevice(rootfs, DAVINCI_MANAGER); int ret = MountDevice(rootfs, DAVINCI_MANAGER, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", DAVINCI_MANAGER); LOG_ERROR("error: failed to mount device %s.", DAVINCI_MANAGER);
return -1; return -1;
} }
ret = MountDevice(rootfs, DEVMM_SVM); ret = MountDevice(rootfs, DEVMM_SVM, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", DEVMM_SVM); LOG_ERROR("error: failed to mount device %s.", DEVMM_SVM);
return -1; return -1;
} }
ret = MountDevice(rootfs, HISI_HDC); ret = MountDevice(rootfs, HISI_HDC, NULL);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount device %s.", HISI_HDC); LOG_ERROR("error: failed to mount device %s.", HISI_HDC);
return -1; return -1;
@@ -265,8 +276,9 @@ int DoFileMounting(const char *rootfs, const struct MountList *list)
int DoMounting(const struct ParsedConfig *config) int DoMounting(const struct ParsedConfig *config)
{ {
int ret; int ret;
ret = DoDeviceMounting(config->rootfs,
ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr); (IsVirtual() ? VDEVICE_NAME : DEVICE_NAME),
config->devices, config->devicesNr);
if (ret < 0) { if (ret < 0) {
LOG_ERROR("error: failed to mount devices."); LOG_ERROR("error: failed to mount devices.");
return -1; return -1;

View File

@@ -15,6 +15,7 @@ using namespace std;
using namespace testing; using namespace testing;
#define DAVINCI_MANAGER_PATH "/dev/davinci_manager" #define DAVINCI_MANAGER_PATH "/dev/davinci_manager"
#define DEVICE_NAME "davinci"
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define MAX_DEVICE_NR 1024 #define MAX_DEVICE_NR 1024
#define MAX_MOUNT_NR 512 #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 GetSelfNsPath(const char *nsType, char *buf, size_t bufSize);
extern "C" int EnterNsByPath(const char *path, int nsType); extern "C" int EnterNsByPath(const char *path, int nsType);
extern "C" int MountDevice(const char *rootfs, const char *deviceName); 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 CheckDirExists(char *dir, int len);
extern "C" int GetParentPathStr(const char *path, char *parent, size_t bufSize); extern "C" int GetParentPathStr(const char *path, char *parent, size_t bufSize);
extern "C" int MakeDirWithParent(const char *path, mode_t mode); 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 DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config);
extern "C" int ParseRuntimeOptions(const char *options); extern "C" int ParseRuntimeOptions(const char *options);
extern "C" bool IsOptionNoDrvSet(); extern "C" bool IsOptionNoDrvSet();
extern "C" bool IsVirtual();
struct MountList { struct MountList {
unsigned int count; unsigned int count;
@@ -215,12 +217,12 @@ int Stub_EnterNsByPath_Failed(const char *path, int nsType)
return 0; 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; 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; return -1;
} }
@@ -457,7 +459,8 @@ TEST(DoDeviceMounting, StatusOne)
char *rootfs = "/home"; char *rootfs = "/home";
unsigned int devicesList[2] = {1, 2}; unsigned int devicesList[2] = {1, 2};
size_t idNr = 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(); GlobalMockObject::verify();
EXPECT_EQ(0, ret); EXPECT_EQ(0, ret);
} }
@@ -468,7 +471,8 @@ TEST(DoDeviceMounting, StatusTwo)
char *rootfs = "/home"; char *rootfs = "/home";
unsigned int devicesList[2] = {1, 2}; unsigned int devicesList[2] = {1, 2};
size_t idNr = 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(); GlobalMockObject::verify();
EXPECT_EQ(-1, ret); EXPECT_EQ(-1, ret);
} }

View File

@@ -45,6 +45,7 @@ var (
var validRuntimeOptions = [...]string{ var validRuntimeOptions = [...]string{
"NODRV", "NODRV",
"VIRTUAL",
} }
type containerConfig struct { type containerConfig struct {