Match-id-3b1b67c3bf449ccb084588665428f4aef723d2b2

This commit is contained in:
BianTanggui
2020-07-27 17:44:05 +08:00
parent 2c262d37e9
commit 27561ec1ce
7 changed files with 102 additions and 68 deletions

10
cli/src/basic.c Normal file
View File

@@ -0,0 +1,10 @@
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli工具公共宏和结构体定义
*/
#include "basic.h"
void InitParsedConfig(struct ParsedConfig *parsedConfig)
{
parsedConfig->devicesNr = MAX_DEVICE_NR;
}

View File

@@ -16,6 +16,7 @@
#define ASCEND_ADDONS_PATH "/usr/local/Ascend/add-ons"
#define DEFAULT_DIR_MODE 0755
#define BUF_SIZE 1024
#define MAX_DEVICE_NR 1024
#define ALLOW_PATH "/devices.allow"
#define ROOT_GAP 4
@@ -23,13 +24,6 @@
#define MOUNT_SUBSTR_GAP 2
#define ROOT_SUBSTR_GAP 2
struct CmdArgs {
char devices[BUF_SIZE];
char rootfs[BUF_SIZE];
int pid;
char options[BUF_SIZE];
};
struct PathInfo {
char* src;
size_t srcLen;
@@ -37,4 +31,15 @@ struct PathInfo {
size_t dstLen;
};
struct ParsedConfig {
char rootfs[BUF_SIZE];
unsigned int devices[MAX_DEVICE_NR];
size_t devicesNr;
char containerNsPath[BUF_SIZE];
char cgroupPath[BUF_SIZE];
int originNsFd;
};
void InitParsedConfig(struct ParsedConfig *parsedConfig);
#endif

View File

@@ -199,7 +199,7 @@ int SetupDriverCgroup(FILE *cgroupAllow)
return 0;
}
int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSize)
int GetCgroupPath(int pid, char *effPath, size_t maxSize)
{
int ret;
char mountPath[BUF_SIZE] = {0x0};
@@ -219,9 +219,9 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
char cgroup[BUF_SIZE] = {0x0};
char cgroupPath[BUF_SIZE] = {0x0};
ret = snprintf_s(cgroupPath, BUF_SIZE, BUF_SIZE, "/proc/%d/cgroup", args->pid);
ret = sprintf_s(cgroupPath, BUF_SIZE, "/proc/%d/cgroup", pid);
if (ret < 0) {
LogError("error: assemble cgroup path failed: pid(%d)\n", args->pid);
LogError("error: assemble cgroup path failed: pid(%d).", pid);
return -1;
}
@@ -243,26 +243,18 @@ int GetCgroupPath(const struct CmdArgs *args, char *effPath, const size_t maxSiz
return 0;
}
int SetupCgroup(struct CmdArgs *args, const char *cgroupPath)
int SetupCgroup(const struct ParsedConfig *config)
{
int ret;
char deviceName[BUF_SIZE] = {0};
char resolvedCgroupPath[PATH_MAX] = {0};
FILE *cgroupAllow = NULL;
if (realpath(cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
LogError("error: cannot canonicalize cgroup path: %s\n", cgroupPath);
if (realpath(config->cgroupPath, resolvedCgroupPath) == NULL && errno != ENOENT) {
LogError("error: cannot canonicalize cgroup path: %s.", config->cgroupPath);
return -1;
}
static const char *sep = ",";
char list[BUF_SIZE] = {0};
errno_t err = strncpy_s(list, BUF_SIZE, args->devices, strlen(args->devices));
if (err != EOK) {
return -1;
}
char *token = NULL;
cgroupAllow = fopen((const char *)resolvedCgroupPath, "a");
if (cgroupAllow == NULL) {
LogError("error: failed to open cgroup file: %s.", resolvedCgroupPath);
@@ -275,24 +267,21 @@ int SetupCgroup(struct CmdArgs *args, const char *cgroupPath)
LogError("error: failed to setup driver cgroup.");
return -1;
}
token = strtok(list, sep);
while (token != NULL) {
ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token);
for (size_t idx = 0; idx < config->devicesNr; idx++) {
ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, config->devices[idx]);
if (ret < 0) {
fclose(cgroupAllow);
LogError("error: failed to assemble device path for no.%s\n", token);
LogError("error: failed to assemble device path for no.%u.", config->devices[idx]);
return -1;
}
ret = SetupDeviceCgroup(cgroupAllow, (const char *)deviceName);
if (ret < 0) {
fclose(cgroupAllow);
LogError("error: failed to setup cgroup %s\n", token);
LogError("error: failed to setup cgroup for %s.", deviceName);
return -1;
}
token = strtok(NULL, sep);
}
fclose(cgroupAllow);

View File

@@ -7,7 +7,7 @@
#include "basic.h"
int GetCgroupPath(const struct CmdArgs *args, char *effPath, size_t maxSize);
int SetupCgroup(struct CmdArgs *args, const char *cgroupPath);
int GetCgroupPath(int pid, char *effPath, size_t maxSize);
int SetupCgroup(const struct ParsedConfig *config);
#endif

View File

@@ -23,10 +23,11 @@
#define DECIMAL 10
struct ParsedConfig {
char containerNsPath[BUF_SIZE];
char cgroupPath[BUF_SIZE];
int originNsFd;
struct CmdArgs {
char devices[BUF_SIZE];
char rootfs[BUF_SIZE];
int pid;
char options[BUF_SIZE];
};
static struct option g_cmdOpts[] = {
@@ -129,9 +130,51 @@ static inline bool IsCmdArgsValid(struct CmdArgs *args)
return (strlen(args->devices) > 0) && (strlen(args->rootfs) > 0) && (args->pid > 0);
}
static int ParseDeviceIDs(unsigned int *idList, size_t *idListSize, char *devices)
{
static const char *sep = ",";
char *token = NULL;
char *context = NULL;
size_t idx = 0;
token = strtok_s(devices, sep, &context);
while (token != NULL) {
if (idx >= *idListSize) {
LogError("error: too many devices(%u), support %u devices maximally", idx, *idListSize);
return -1;
}
errno = 0;
idList[idx] = strtoul((const char *)token, NULL, DECIMAL);
if (errno != 0) {
LogError("error: failed to convert device id (%s) from cmd args, caused by: %s.", token, strerror(errno));
return -1;
}
idx++;
token = strtok_s(NULL, sep, &context);
}
*idListSize = idx;
return 0;
}
int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
{
int ret;
errno_t err;
err = strcpy_s(config->rootfs, BUF_SIZE, args->rootfs);
if (err != EOK) {
LogError("error: failed to copy rootfs path to parsed config.");
return -1;
}
ret = ParseDeviceIDs(config->devices, &config->devicesNr, (char *)args->devices);
if (ret < 0) {
LogError("error: failed to parse device ids from cmdline argument");
return -1;
}
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) {
@@ -139,7 +182,7 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
return -1;
}
ret = GetCgroupPath(args, config->cgroupPath, BUF_SIZE);
ret = GetCgroupPath(args->pid, config->cgroupPath, BUF_SIZE);
if (ret < 0) {
LogError("error: failed to get cgroup path.");
return -1;
@@ -172,6 +215,8 @@ int SetupContainer(struct CmdArgs *args)
int ret;
struct ParsedConfig config;
InitParsedConfig(&config);
ret = DoPrepare(args, &config);
if (ret < 0) {
LogError("error: failed to prepare nesessary config.");
@@ -186,14 +231,14 @@ int SetupContainer(struct CmdArgs *args)
return -1;
}
ret = DoMounting(args);
ret = DoMounting(&config);
if (ret < 0) {
LogError("error: failed to do mounting.");
close(config.originNsFd);
return -1;
}
ret = SetupCgroup(args, (const char *)config.cgroupPath);
ret = SetupCgroup(&config);
if (ret < 0) {
LogError("error: failed to set up cgroup.");
close(config.originNsFd);

View File

@@ -112,34 +112,22 @@ int MountDevice(const char *rootfs, const char *deviceName)
return 0;
}
int DoDeviceMounting(const char *rootfs, const char *devicesList)
int DoDeviceMounting(const char *rootfs, const unsigned int ids[], size_t idsNr)
{
static const char *sep = ",";
char list[BUF_SIZE] = {0};
char deviceName[BUF_SIZE] = {0};
errno_t err = strncpy_s(list, BUF_SIZE, devicesList, strlen(devicesList));
if (err != EOK) {
return -1;
}
char *token = NULL;
char *context = NULL;
token = strtok_s(list, sep, &context);
while (token != NULL) {
int ret = snprintf_s(deviceName, BUF_SIZE, BUF_SIZE, "%s%s", DEVICE_NAME, token);
for (size_t idx = 0; idx < idsNr; idx++) {
int ret = sprintf_s(deviceName, BUF_SIZE, "%s%u", DEVICE_NAME, ids[idx]);
if (ret < 0) {
LogError("error: assemble device name failed, id: %s\n", token);
LogError("error: assemble device name failed, id: %u.", ids[idx]);
return -1;
}
ret = MountDevice(rootfs, deviceName);
if (ret < 0) {
LogError("error: failed to mount device no. %s\n", token);
LogError("error: failed to mount device %s.", deviceName);
return -1;
}
token = strtok_s(NULL, sep, &context);
}
return 0;
@@ -233,31 +221,28 @@ int DoDirectoryMounting(const char *rootfs)
return 0;
}
int DoMounting(const struct CmdArgs *args)
int DoMounting(const struct ParsedConfig *config)
{
int ret;
ret = DoDeviceMounting(args->rootfs, args->devices);
ret = DoDeviceMounting(config->rootfs, config->devices, config->devicesNr);
if (ret < 0) {
LogError("error: failed to do mounts.");
return -1;
}
ret = DoCtrlDeviceMounting(args->rootfs);
ret = DoCtrlDeviceMounting(config->rootfs);
if (ret < 0) {
LogError("error: failed to do mount files.");
return -1;
}
if (IsOptionNoDrvSet()) {
// 不挂载DRIVER
return 0;
}
ret = DoDirectoryMounting(args->rootfs);
if (ret < 0) {
LogError("error: failed to do mount directory\n");
return -1;
if (!IsOptionNoDrvSet()) {
ret = DoDirectoryMounting(config->rootfs);
if (ret < 0) {
LogError("error: failed to do mount directory.");
return -1;
}
}
return 0;

View File

@@ -7,6 +7,6 @@
#include "basic.h"
int DoMounting(const struct CmdArgs *args);
int DoMounting(const struct ParsedConfig *config);
#endif