Match-id-0342455040ca22ad7bef331d4c7e37bae4fa44bf

This commit is contained in:
BianTanggui
2020-07-04 19:12:20 +08:00
parent 70b5d7ff3e
commit d29403afa5
8 changed files with 126 additions and 92 deletions

View File

@@ -3,7 +3,6 @@
* Description: ascend-docker-cli工具配置容器挂载Ascend NPU设备
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <getopt.h>
#include <string.h>
@@ -19,6 +18,7 @@
#include "ns.h"
#include "mount.h"
#include "cgrp.h"
#include "logging.h"
struct ParsedConfig {
char containerNsPath[BUF_SIZE];
@@ -44,26 +44,26 @@ int DoPrepare(const struct CmdArgs *args, struct ParsedConfig *config)
ret = GetNsPath(args->pid, "mnt", config->containerNsPath, BUF_SIZE);
if (ret < 0) {
fprintf(stderr, "error: failed to get container mnt ns path: pid(%d)\n", args->pid);
logError("error: failed to get container mnt ns path: pid(%d)\n", args->pid);
return -1;
}
ret = GetCgroupPath(args, config->cgroupPath, BUF_SIZE);
if (ret < 0) {
fprintf(stderr, "error: failed to get cgroup path\n");
logError("error: failed to get cgroup path\n");
return -1;
}
char originNsPath[BUF_SIZE] = {0};
ret = GetSelfNsPath("mnt", originNsPath, BUF_SIZE);
if (ret < 0) {
fprintf(stderr, "error: failed to get self ns path\n");
logError("error: failed to get self ns path\n");
return -1;
}
config->originNsFd = open((const char *)originNsPath, O_RDONLY); // proc接口非外部输入
if (config->originNsFd < 0) {
fprintf(stderr, "error: failed to get self ns fd: %s\n", originNsPath);
logError("error: failed to get self ns fd: %s\n", originNsPath);
return -1;
}
@@ -77,28 +77,28 @@ int SetupContainer(struct CmdArgs *args)
ret = DoPrepare(args, &config);
if (ret < 0) {
fprintf(stderr, "error: failed to prepare nesessary config\n");
logError("error: failed to prepare nesessary config\n");
return -1;
}
// enter container's mount namespace
ret = EnterNsByPath((const char *)config.containerNsPath, CLONE_NEWNS);
if (ret < 0) {
fprintf(stderr, "error: failed to set to container ns: %s\n", config.containerNsPath);
logError("error: failed to set to container ns: %s\n", config.containerNsPath);
close(config.originNsFd);
return -1;
}
ret = DoMounting(args);
if (ret < 0) {
fprintf(stderr, "error: failed to do mounting\n");
logError("error: failed to do mounting\n");
close(config.originNsFd);
return -1;
}
ret = SetupCgroup(args, (const char *)config.cgroupPath);
if (ret < 0) {
fprintf(stderr, "error: failed to set up cgroup\n");
logError("error: failed to set up cgroup\n");
close(config.originNsFd);
return -1;
}
@@ -106,7 +106,7 @@ int SetupContainer(struct CmdArgs *args)
// back to original namespace
ret = EnterNsByFd(config.originNsFd, CLONE_NEWNS);
if (ret < 0) {
fprintf(stderr, "error: failed to set ns back\n");
logError("error: failed to set ns back\n");
close(config.originNsFd);
return -1;
}
@@ -151,14 +151,14 @@ int Process(int argc, char **argv)
}
break;
default:
fprintf(stderr, "unrecongnized option\n");
logError("unrecongnized option\n");
isSucceed = false; // unrecognized option
break;
}
}
if (!isSucceed || !IsCmdArgsValid(&args)) {
fprintf(stderr, "error: information not completed or valid.\n");
logError("error: information not completed or valid.\n");
return -1;
}