From 1b864c64a8463b7c903e91789b21dc3d11547b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 09:05:48 +0800 Subject: [PATCH 01/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E5=A2=9E=E5=8A=A0containerd?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 61 +- build/scripts/uninstall.sh | 4 +- go.mod | 4 + go.sum | 6 + install/main.go | 232 +----- install/process/common.go | 14 + install/process/constant.go | 43 ++ install/process/containerd_process.go | 118 +++ install/process/docker_process.go | 193 +++++ .../docker_process_test.go} | 2 +- runtime/main.go | 676 +----------------- runtime/process/process.go | 676 ++++++++++++++++++ .../{main_test.go => process/process_test.go} | 14 +- 13 files changed, 1146 insertions(+), 897 deletions(-) create mode 100644 install/process/common.go create mode 100644 install/process/constant.go create mode 100644 install/process/containerd_process.go create mode 100644 install/process/docker_process.go rename install/{main_test.go => process/docker_process_test.go} (99%) create mode 100644 runtime/process/process.go rename runtime/{main_test.go => process/process_test.go} (98%) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index cc9f80b..26e6121 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -22,6 +22,8 @@ start_script=${start_arg#*--} ASCEND_RUNTIME_CONFIG_DIR=/etc/ascend-docker-runtime.d DOCKER_CONFIG_DIR=/etc/docker +CONTAINERD_CONFIG_DIR=/etc/containerd/ +INSTALL_SCENE=docker INSTALL_PATH=/usr/local/Ascend/Ascend-Docker-Runtime readonly INSTALL_LOG_DIR=/var/log/ascend-docker-runtime readonly INSTALL_LOG_PATH=${INSTALL_LOG_DIR}/installer.log @@ -145,6 +147,7 @@ Options: --ce= Only iSula need to specify the container engine(eg: --ce=isula) MUST use with --install or --uninstall --version Query Ascend-docker-runtime version + --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) " } @@ -171,6 +174,7 @@ function save_install_args() { echo -e "a200=${a200}" echo -e "a200isoc=${a200isoc}" echo -e "a200ia2=${a200ia2}" + echo -e "install-scene=${INSTALL_SCENE}" } > "${INSTALL_PATH}"/ascend_docker_runtime_install.info chmod 640 ${INSTALL_PATH}/ascend_docker_runtime_install.info } @@ -273,22 +277,42 @@ function install() echo "[INFO]: install executable files success" - check_path ${DOCKER_CONFIG_DIR}/daemon.json - if [[ $? != 0 ]]; then - log "[ERROR]" "install failed, ${DOCKER_CONFIG_DIR}/daemon.json is invalid" + if [[ "${INSTALL_SCENE}" == "docker" ]]; then + echo "[INFO]: install is 'docker'." + check_path ${DOCKER_CONFIG_DIR}/daemon.json + if [[ $? != 0 ]]; then + log "[ERROR]" "install failed, ${DOCKER_CONFIG_DIR}/daemon.json is invalid" + exit 1 + fi + [[ ! -d ${DOCKER_CONFIG_DIR} ]] && mkdir -p -m 750 ${DOCKER_CONFIG_DIR} + + SRC="${DOCKER_CONFIG_DIR}/daemon.json.${PPID}" + DST="${DOCKER_CONFIG_DIR}/daemon.json" + elif [[ "${INSTALL_SCENE}" == "containerd" ]]; then + echo "[INFO]: install is 'containerd'." + check_path ${CONTAINERD_CONFIG_DIR}/config.toml + if [[ $? != 0 ]]; then + log "[ERROR]" "install failed, ${CONTAINERD_CONFIG_DIR}/config.toml is invalid" + exit 1 + fi + [[ ! -d ${CONTAINERD_CONFIG_DIR} ]] && mkdir -p -m 750 ${CONTAINERD_CONFIG_DIR} + + SRC="${CONTAINERD_CONFIG_DIR}/config.toml.${PPID}" + DST="${CONTAINERD_CONFIG_DIR}/config.toml" + if [ ! -e ${DST} ]; then + containerd config default > ${DST} + fi + else + log "[ERROR]" "install failed, invalid value '${INSTALL_SCENE}' of 'install-scene' " exit 1 fi - [[ ! -d ${DOCKER_CONFIG_DIR} ]] && mkdir -p -m 750 ${DOCKER_CONFIG_DIR} - SRC="${DOCKER_CONFIG_DIR}/daemon.json.${PPID}" - DST="${DOCKER_CONFIG_DIR}/daemon.json" # exit when return code is not 0, if use 'set -e' - ./ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} > /dev/null + ./ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE} > /dev/null if [[ $? != 0 ]]; then - log "[ERROR]" "install failed, './ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT}' return non-zero" + log "[ERROR]" "install failed, './ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE}' return non-zero" exit 1 fi - mv -f ${SRC} ${DST} log "[INFO]" "${DST} modify success" chmod 600 ${DST} @@ -406,7 +430,7 @@ function upgrade() echo "[INFO]: The version of Ascend Docker Runtime is: v${PACKAGE_VERSION}" log "[INFO]" "Ascend Docker Runtime upgrade success" } - +INSTALL_SCENE_FLAG=n INSTALL_FLAG=n INSTALL_PATH_FLAG=n UNINSTALL_FLAG=n @@ -431,6 +455,23 @@ fi while true do case "$3" in + --install-scene=*) + if [ "${INSTALL_SCENE_FLAG}" == "y" ]; then + log "[ERROR]" "install failed, '--install-scene' Repeat parameter!" + exit 1 + fi + need_help=n + INSTALL_SCENE_FLAG=y + if [ "$3" == "install-scene=docker" ]; then + INSTALL_SCENE=docker + elif [ "$3" == "install-scene=containerd" ]; then + INSTALL_SCENE=containerd + else + log "[ERROR]" "failed, Please check the parameter of --install-scene=" + exit 1 + fi + shift + ;; --install) if [ "${INSTALL_FLAG}" == "y" ]; then log "[ERROR]" "install failed, '--install' Repeat parameter!" diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index e6b113c..433a9cf 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -146,9 +146,9 @@ if [[ $? != 0 ]]; then fi # exit when return code is not 0, if use 'set -e' -${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} > /dev/null +${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE} > /dev/null if [[ $? != 0 ]]; then - log "[ERROR]" "uninstall failed, '${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT}' return non-zero" + log "[ERROR]" "uninstall failed, '${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE}' return non-zero" exit 1 fi diff --git a/go.mod b/go.mod index ffaf2ab..b4d150e 100644 --- a/go.mod +++ b/go.mod @@ -17,16 +17,20 @@ require ( github.com/containerd/cgroups v1.0.4 // indirect github.com/containerd/continuity v0.3.0 // indirect github.com/containerd/ttrpc v1.1.2 // indirect + github.com/containerd/typeurl v1.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect + github.com/imdario/mergo v0.3.12 // indirect github.com/klauspost/compress v1.11.13 // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect github.com/opencontainers/runc v1.1.12 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sirupsen/logrus v1.9.0 // indirect diff --git a/go.sum b/go.sum index 3d095d5..f4f96ae 100644 --- a/go.sum +++ b/go.sum @@ -191,6 +191,7 @@ github.com/containerd/ttrpc v1.1.2/go.mod h1:XX4ZTnoOId4HklF4edwc4DcqskFZuvXB1Ev github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc= github.com/containerd/typeurl v0.0.0-20190911142611-5eb25027c9fd/go.mod h1:GeKYzf2pQcqv7tJ0AoCuuhtnqhva5LNU3U+OyKxxJpk= github.com/containerd/typeurl v1.0.1/go.mod h1:TB1hUtrpaiO88KEK56ijojHS1+NeF0izUACaJW2mdXg= +github.com/containerd/typeurl v1.0.2 h1:Chlt8zIieDbzQFzXzAeBEF92KhExuE4p9p92/QmY7aY= github.com/containerd/typeurl v1.0.2/go.mod h1:9trJWW2sRlGub4wZJRTW83VtbOLS6hwcDZXTn6oPz9s= github.com/containerd/zfs v0.0.0-20200918131355-0a33824f23a2/go.mod h1:8IgZOBdv8fAgXddBT4dBXJPtxyRsejFIpXoklgxgEjw= github.com/containerd/zfs v0.0.0-20210301145711-11e8f1707f62/go.mod h1:A9zfAbMlQwE+/is6hi0Xw8ktpL+6glmqZYtevJgaB8Y= @@ -245,6 +246,7 @@ github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7/go.mod h1:eEKB0N0r github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-events v0.0.0-20170721190031-9461782956ad/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= +github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8= github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= github.com/docker/go-metrics v0.0.0-20180209012529-399ea8c73916/go.mod h1:/u0gXw0Gay3ceNrsHubL3BtdOL2fHf93USgMTe0W5dI= github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw= @@ -403,6 +405,7 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= @@ -528,6 +531,8 @@ github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3 github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -1018,6 +1023,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/install/main.go b/install/main.go index b14f1f2..e4c7557 100644 --- a/install/main.go +++ b/install/main.go @@ -16,14 +16,12 @@ package main import ( + "ascend-docker-runtime/install/process" "context" - "encoding/json" "flag" "fmt" - "io/ioutil" "log" "os" - "path/filepath" "strings" "huawei.com/npu-exporter/v5/common-utils/hwlog" @@ -51,17 +49,8 @@ const noDefaultTemplate = `{ }` const ( - actionPosition = 0 - srcFilePosition = 1 - destFilePosition = 2 - runtimeFilePosition = 3 - rmCommandLength = 4 - addCommandLength = 5 - addCommand = "add" - maxCommandLength = 65535 - logPath = "/var/log/ascend-docker-runtime/install-helper-run.log" - rmCommand = "rm" - maxFileSize = 1024 * 1024 * 10 + maxCommandLength = 65535 + logPath = "/var/log/ascend-docker-runtime/install-helper-run.log" ) var reserveDefaultRuntime = false @@ -83,7 +72,28 @@ func main() { log.Fatalf("command error, please check %s for detail", logPath) } - err, behavior := process() + const helpMessage = "\tadd \n" + + "\t rm \n" + + "\t -h help command" + helpFlag := flag.Bool("h", false, helpMessage) + flag.Parse() + if *helpFlag { + _, err := fmt.Println(helpMessage) + log.Fatalf("need help, error: %v", err) + } + command := flag.Args() + if len(command) == 0 { + log.Fatalf("error param") + } + var behavior string + if command[len(command)-1] == process.InstallSceneDocker { + err, behavior = process.DockerProcess(command) + } else if command[len(command)-1] == process.InstallSceneContainerd { + err, behavior = process.ContainerdProcess(command) + } else { + hwlog.RunLog.Errorf("error param: %v", command[len(command)-1]) + log.Fatalf("error param: %v", command[len(command)-1]) + } if err != nil { hwlog.RunLog.Errorf("%v run script failed: %v", logPrefixWords, err) log.Fatal(fmt.Errorf("error in installation")) @@ -109,195 +119,3 @@ func initLogModule(ctx context.Context) error { } return nil } - -func checkParamAndGetBehavior(action string, command []string) (bool, string) { - correctParam, behavior := false, "" - if action == addCommand && len(command) == addCommandLength { - correctParam = true - behavior = "install" - } - if action == rmCommand && len(command) == rmCommandLength { - correctParam = true - behavior = "uninstall" - } - return correctParam, behavior -} - -func process() (error, string) { - const helpMessage = "\tadd \n" + - "\t rm \n" + - "\t -h help command" - helpFlag := flag.Bool("h", false, helpMessage) - flag.Parse() - if *helpFlag { - _, err := fmt.Println(helpMessage) - return err, "" - } - command := flag.Args() - if len(command) == 0 { - return fmt.Errorf("error param"), "" - } - - action := command[actionPosition] - correctParam, behavior := checkParamAndGetBehavior(action, command) - if !correctParam { - return fmt.Errorf("error param"), "" - } - - srcFilePath := command[srcFilePosition] - if _, err := os.Stat(srcFilePath); os.IsNotExist(err) { - if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(srcFilePath), true, false); err != nil { - return err, behavior - } - } else { - if _, err := mindxcheckutils.RealFileChecker(srcFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err, behavior - } - } - - destFilePath := command[destFilePosition] - if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(destFilePath), true, false); err != nil { - return err, behavior - } - runtimeFilePath := "" - if len(command) == addCommandLength { - runtimeFilePath = command[runtimeFilePosition] - if _, err := mindxcheckutils.RealFileChecker(runtimeFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err, behavior - } - } - - setReserveDefaultRuntime(command) - - // check file permission - writeContent, err := createJsonString(srcFilePath, runtimeFilePath, action) - if err != nil { - return err, behavior - } - return writeJson(destFilePath, writeContent), behavior -} - -func createJsonString(srcFilePath, runtimeFilePath, action string) ([]byte, error) { - var writeContent []byte - if _, err := os.Stat(srcFilePath); err == nil { - daemon, err := modifyDaemon(srcFilePath, runtimeFilePath, action) - if err != nil { - return nil, err - } - writeContent, err = json.MarshalIndent(daemon, "", " ") - if err != nil { - return nil, err - } - } else if os.IsNotExist(err) { - // not existed - if !reserveDefaultRuntime { - writeContent = []byte(fmt.Sprintf(commonTemplate, runtimeFilePath)) - } else { - writeContent = []byte(fmt.Sprintf(noDefaultTemplate, runtimeFilePath)) - } - } else { - return nil, err - } - return writeContent, nil -} - -func writeJson(destFilePath string, writeContent []byte) error { - if _, err := os.Stat(destFilePath); os.IsNotExist(err) { - const perm = 0600 - file, err := os.OpenFile(destFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm) - if err != nil { - return fmt.Errorf("create target file failed") - } - _, err = file.Write(writeContent) - if err != nil { - closeErr := file.Close() - return fmt.Errorf("write target file failed with close err %v", closeErr) - } - err = file.Close() - if err != nil { - return fmt.Errorf("close target file failed") - } - return nil - } else { - return fmt.Errorf("target file already existed") - } -} - -func modifyDaemon(srcFilePath, runtimeFilePath, action string) (map[string]interface{}, error) { - // existed... - daemon, err := loadOriginJson(srcFilePath) - if err != nil { - return nil, err - } - - if _, ok := daemon["runtimes"]; !ok && action == addCommand { - daemon["runtimes"] = map[string]interface{}{} - } - runtimeValue := daemon["runtimes"] - runtimeConfig, runtimeConfigOk := runtimeValue.(map[string]interface{}) - if !runtimeConfigOk && action == addCommand { - return nil, fmt.Errorf("extract runtime failed") - } - if action == addCommand { - if _, ok := runtimeConfig["ascend"]; !ok { - runtimeConfig["ascend"] = map[string]interface{}{} - } - ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) - if !ok { - return nil, fmt.Errorf("extract ascend failed") - } - ascendConfig["path"] = runtimeFilePath - if _, ok := ascendConfig["runtimeArgs"]; !ok { - ascendConfig["runtimeArgs"] = []string{} - } - if !reserveDefaultRuntime { - daemon["default-runtime"] = "ascend" - } - } else if action == rmCommand { - if runtimeConfigOk { - delete(runtimeConfig, "ascend") - } - if value, ok := daemon["default-runtime"]; ok && value == "ascend" { - delete(daemon, "default-runtime") - } - } else { - return nil, fmt.Errorf("param error") - } - return daemon, nil -} - -func loadOriginJson(srcFilePath string) (map[string]interface{}, error) { - if fileInfo, err := os.Stat(srcFilePath); err != nil { - return nil, err - } else if fileInfo.Size() > maxFileSize { - return nil, fmt.Errorf("file size too large") - } - - file, err := os.Open(srcFilePath) - if err != nil { - return nil, fmt.Errorf("open daemon.json failed") - } - content, err := ioutil.ReadAll(file) - if err != nil { - closeErr := file.Close() - return nil, fmt.Errorf("read daemon.json failed, close file err is %v", closeErr) - } - err = file.Close() - if err != nil { - return nil, fmt.Errorf("close daemon.json failed") - } - - var daemon map[string]interface{} - err = json.Unmarshal(content, &daemon) - if err != nil { - return nil, fmt.Errorf("load daemon.json failed") - } - return daemon, nil -} - -func setReserveDefaultRuntime(command []string) { - reserveCmdPostion := len(command) - 1 - if command[reserveCmdPostion] == "yes" { - reserveDefaultRuntime = true - } -} diff --git a/install/process/common.go b/install/process/common.go new file mode 100644 index 0000000..5ae3c7e --- /dev/null +++ b/install/process/common.go @@ -0,0 +1,14 @@ +package process + +func checkParamAndGetBehavior(action string, command []string) (bool, string) { + correctParam, behavior := false, "" + if action == addCommand && len(command) == addCommandLength { + correctParam = true + behavior = "install" + } + if action == rmCommand && len(command) == rmCommandLength { + correctParam = true + behavior = "uninstall" + } + return correctParam, behavior +} diff --git a/install/process/constant.go b/install/process/constant.go new file mode 100644 index 0000000..64fdb9b --- /dev/null +++ b/install/process/constant.go @@ -0,0 +1,43 @@ +package process + +const commonTemplate = `{ + "runtimes": { + "ascend": { + "path": "%s", + "runtimeArgs": [] + } + }, + "default-runtime": "ascend" +}` + +const noDefaultTemplate = `{ + "runtimes": { + "ascend": { + "path": "%s", + "runtimeArgs": [] + } + } +}` + +const ( + actionPosition = 0 + srcFilePosition = 1 + destFilePosition = 2 + runtimeFilePosition = 3 + rmCommandLength = 5 + addCommandLength = 6 + addCommand = "add" + rmCommand = "rm" + maxFileSize = 1024 * 1024 * 10 +) + +const ( + // InstallSceneDocker is a 'docker' string of scene + InstallSceneDocker = "docker" + // InstallSceneContainerd is a 'containerd' string of scene + InstallSceneContainerd = "containerd" + cgroupsFilePath = "/proc/cgroups" + hierarchyStr = "hierarchy" + initHierarchyCol = -1 + cgroupV2HierarchyValue = "2" +) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go new file mode 100644 index 0000000..e9398f7 --- /dev/null +++ b/install/process/containerd_process.go @@ -0,0 +1,118 @@ +package process + +import ( + "bufio" + "fmt" + "log" + "os" + "path/filepath" + "strings" + + "github.com/containerd/containerd/services/server/config" + "huawei.com/npu-exporter/v5/common-utils/hwlog" + + "ascend-docker-runtime/mindxcheckutils" +) + +func ContainerdProcess(command []string) (error, string) { + action := command[actionPosition] + correctParam, behavior := checkParamAndGetBehavior(action, command) + if !correctParam { + return fmt.Errorf("error param"), "" + } + srcFilePath := command[srcFilePosition] + if _, err := os.Stat(srcFilePath); os.IsNotExist(err) { + if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(srcFilePath), true, false); err != nil { + hwlog.RunLog.Errorf("check failed, error: %v", err) + return err, behavior + } + } else { + if _, err := mindxcheckutils.RealFileChecker(srcFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { + hwlog.RunLog.Errorf("check failed, error: %v", err) + return err, behavior + } + } + destFilePath := command[destFilePosition] + if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(destFilePath), true, false); err != nil { + return err, behavior + } + runtimeFilePath := "" + if len(command) == addCommandLength { + runtimeFilePath = command[runtimeFilePosition] + if _, err := mindxcheckutils.RealFileChecker(runtimeFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { + hwlog.RunLog.Errorf("failed to check, error: %v", err) + return err, behavior + } + } + err := editContainerdConfig(srcFilePath, runtimeFilePath, action) + if err != nil { + hwlog.RunLog.Errorf("failed to edit containerd config, err: %v", err) + return err, behavior + } + return nil, behavior +} + +func isCgroupV2() (bool, error) { + if _, err := os.Stat(cgroupsFilePath); err != nil { + hwlog.RunLog.Errorf("file %v stat error: %v", err) + return false, err + } + realPath, err := mindxcheckutils.RealFileChecker(cgroupsFilePath, false, true, mindxcheckutils.DefaultSize) + if err != nil { + hwlog.RunLog.Errorf("failed to check file real path, err: %v", err) + return false, err + } + file, err := os.Open(realPath) + if err != nil { + hwlog.RunLog.Errorf("failed to open %v: %v", realPath, err) + return false, err + } + defer func() { + closeErr := file.Close() + if closeErr != nil { + hwlog.RunLog.Errorf("failed to close file, err: %v", err) + } + }() + scanner := bufio.NewScanner(file) + hierarchyCol := initHierarchyCol + if scanner.Scan() { + line := scanner.Text() + parts := strings.Fields(line) + for index, part := range parts { + if strings.Contains(part, hierarchyStr) { + hierarchyCol = index + break + } + } + } + if hierarchyCol == initHierarchyCol { + return false, nil + } + for scanner.Scan() { + line := scanner.Text() + parts := strings.Fields(line) + if parts[hierarchyCol] != cgroupV2HierarchyValue { + return false, nil + } + } + return true, nil +} + +func editContainerdConfig(srcFilePath, runtimeFilePath, action string) error { + cfg := config.Config{} + if err := config.LoadConfig(srcFilePath, &cfg); err != nil { + log.Fatalf("Failed to load configuration file: %v", err) + } + if isCgroupV2() { + // TODO + hwlog.RunLog.Infof("it is cgroup v2") + } else { + hwlog.RunLog.Infof("it is cgroup v2") + if value, ok := cfg.Plugins["io.containerd.runtime.v1.linux"]; ok { + hwlog.RunLog.Infof("value: %+v", value) + } else { + hwlog.RunLog.Infof(" not ok, plugins: %+v", cfg.Plugins) + } + } + return nil +} diff --git a/install/process/docker_process.go b/install/process/docker_process.go new file mode 100644 index 0000000..70be119 --- /dev/null +++ b/install/process/docker_process.go @@ -0,0 +1,193 @@ +/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +// Package process deal the docker or containerd scene installation +package process + +import ( + "ascend-docker-runtime/mindxcheckutils" + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path/filepath" +) + +var reserveDefaultRuntime = false + +func DockerProcess(command []string) (error, string) { + + action := command[actionPosition] + correctParam, behavior := checkParamAndGetBehavior(action, command) + if !correctParam { + return fmt.Errorf("error param"), "" + } + + srcFilePath := command[srcFilePosition] + if _, err := os.Stat(srcFilePath); os.IsNotExist(err) { + if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(srcFilePath), true, false); err != nil { + return err, behavior + } + } else { + if _, err := mindxcheckutils.RealFileChecker(srcFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err, behavior + } + } + + destFilePath := command[destFilePosition] + if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(destFilePath), true, false); err != nil { + return err, behavior + } + runtimeFilePath := "" + if len(command) == addCommandLength { + runtimeFilePath = command[runtimeFilePosition] + if _, err := mindxcheckutils.RealFileChecker(runtimeFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err, behavior + } + } + + setReserveDefaultRuntime(command) + + // check file permission + writeContent, err := createJsonString(srcFilePath, runtimeFilePath, action) + if err != nil { + return err, behavior + } + return writeJson(destFilePath, writeContent), behavior +} + +func createJsonString(srcFilePath, runtimeFilePath, action string) ([]byte, error) { + var writeContent []byte + if _, err := os.Stat(srcFilePath); err == nil { + daemon, err := modifyDaemon(srcFilePath, runtimeFilePath, action) + if err != nil { + return nil, err + } + writeContent, err = json.MarshalIndent(daemon, "", " ") + if err != nil { + return nil, err + } + } else if os.IsNotExist(err) { + // not existed + if !reserveDefaultRuntime { + writeContent = []byte(fmt.Sprintf(commonTemplate, runtimeFilePath)) + } else { + writeContent = []byte(fmt.Sprintf(noDefaultTemplate, runtimeFilePath)) + } + } else { + return nil, err + } + return writeContent, nil +} + +func writeJson(destFilePath string, writeContent []byte) error { + if _, err := os.Stat(destFilePath); os.IsNotExist(err) { + const perm = 0600 + file, err := os.OpenFile(destFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm) + if err != nil { + return fmt.Errorf("create target file failed") + } + _, err = file.Write(writeContent) + if err != nil { + closeErr := file.Close() + return fmt.Errorf("write target file failed with close err %v", closeErr) + } + err = file.Close() + if err != nil { + return fmt.Errorf("close target file failed") + } + return nil + } else { + return fmt.Errorf("target file already existed") + } +} + +func modifyDaemon(srcFilePath, runtimeFilePath, action string) (map[string]interface{}, error) { + // existed... + daemon, err := loadOriginJson(srcFilePath) + if err != nil { + return nil, err + } + + if _, ok := daemon["runtimes"]; !ok && action == addCommand { + daemon["runtimes"] = map[string]interface{}{} + } + runtimeValue := daemon["runtimes"] + runtimeConfig, runtimeConfigOk := runtimeValue.(map[string]interface{}) + if !runtimeConfigOk && action == addCommand { + return nil, fmt.Errorf("extract runtime failed") + } + if action == addCommand { + if _, ok := runtimeConfig["ascend"]; !ok { + runtimeConfig["ascend"] = map[string]interface{}{} + } + ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("extract ascend failed") + } + ascendConfig["path"] = runtimeFilePath + if _, ok := ascendConfig["runtimeArgs"]; !ok { + ascendConfig["runtimeArgs"] = []string{} + } + if !reserveDefaultRuntime { + daemon["default-runtime"] = "ascend" + } + } else if action == rmCommand { + if runtimeConfigOk { + delete(runtimeConfig, "ascend") + } + if value, ok := daemon["default-runtime"]; ok && value == "ascend" { + delete(daemon, "default-runtime") + } + } else { + return nil, fmt.Errorf("param error") + } + return daemon, nil +} + +func loadOriginJson(srcFilePath string) (map[string]interface{}, error) { + if fileInfo, err := os.Stat(srcFilePath); err != nil { + return nil, err + } else if fileInfo.Size() > maxFileSize { + return nil, fmt.Errorf("file size too large") + } + + file, err := os.Open(srcFilePath) + if err != nil { + return nil, fmt.Errorf("open daemon.json failed") + } + content, err := ioutil.ReadAll(file) + if err != nil { + closeErr := file.Close() + return nil, fmt.Errorf("read daemon.json failed, close file err is %v", closeErr) + } + err = file.Close() + if err != nil { + return nil, fmt.Errorf("close daemon.json failed") + } + + var daemon map[string]interface{} + err = json.Unmarshal(content, &daemon) + if err != nil { + return nil, fmt.Errorf("load daemon.json failed") + } + return daemon, nil +} + +func setReserveDefaultRuntime(command []string) { + reserveCmdPostion := len(command) - 1 + if command[reserveCmdPostion] == "yes" { + reserveDefaultRuntime = true + } +} diff --git a/install/main_test.go b/install/process/docker_process_test.go similarity index 99% rename from install/main_test.go rename to install/process/docker_process_test.go index ebecd29..51e7fbb 100644 --- a/install/main_test.go +++ b/install/process/docker_process_test.go @@ -13,7 +13,7 @@ */ // Package main -package main +package process import ( "encoding/json" diff --git a/runtime/main.go b/runtime/main.go index f3dc0e7..a495651 100644 --- a/runtime/main.go +++ b/runtime/main.go @@ -1,680 +1,16 @@ -/* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -// Package main package main import ( + "ascend-docker-runtime/mindxcheckutils" + "ascend-docker-runtime/runtime/process" "context" - "encoding/json" "fmt" - "io/ioutil" + "huawei.com/npu-exporter/v5/common-utils/hwlog" "log" "os" - "os/exec" - "path" - "path/filepath" - "regexp" - "sort" - "strconv" "strings" - "syscall" - - "github.com/containerd/containerd/oci" - "github.com/opencontainers/runtime-spec/specs-go" - "huawei.com/npu-exporter/v5/common-utils/hwlog" - - "ascend-docker-runtime/mindxcheckutils" - "ascend-docker-runtime/runtime/dcmi" ) -const ( - runLogPath = "/var/log/ascend-docker-runtime/runtime-run.log" - hookDefaultFilePath = "/usr/local/bin/ascend-docker-hook" - - maxCommandLength = 65535 - hookCli = "ascend-docker-hook" - destroyHookCli = "ascend-docker-destroy" - dockerRuncFile = "docker-runc" - runcFile = "runc" - envLength = 2 - kvPairSize = 2 - borderNum = 2 - - // ENV for device-plugin to identify ascend-docker-runtime - useAscendDocker = "ASCEND_DOCKER_RUNTIME=True" - devicePlugin = "ascend-device-plugin" - ascendVisibleDevices = "ASCEND_VISIBLE_DEVICES" - ascendRuntimeOptions = "ASCEND_RUNTIME_OPTIONS" - - // void indicates that the NPU card does not need to be mounted - void = "void" -) - -var ( - hookCliPath = hookCli - hookDefaultFile = hookDefaultFilePath - dockerRuncName = dockerRuncFile - runcName = runcFile -) - -const ( - // Atlas200ISoc Product name - Atlas200ISoc = "Atlas 200I SoC A1" - // Atlas200 Product name - Atlas200 = "Atlas 200 Model 3000" - // Ascend310 ascend 310 chip - Ascend310 = "Ascend310" - // Ascend310P ascend 310P chip - Ascend310P = "Ascend310P" - // Ascend310B ascend 310B chip - Ascend310B = "Ascend310B" - // Ascend910 ascend 910 chip - Ascend910 = "Ascend910" - ascend = "Ascend" - - devicePath = "/dev/" - davinciName = "davinci" - virtualDavinciName = "vdavinci" - davinciManager = "davinci_manager" - davinciManagerDocker = "davinci_manager_docker" - notRenameDeviceType = "" - devmmSvm = "devmm_svm" - hisiHdc = "hisi_hdc" - svm0 = "svm0" - tsAisle = "ts_aisle" - upgrade = "upgrade" - sys = "sys" - vdec = "vdec" - vpc = "vpc" - pngd = "pngd" - venc = "venc" - dvppCmdList = "dvpp_cmdlist" - logDrv = "log_drv" - acodec = "acodec" - ai = "ai" - ao = "ao" - vo = "vo" - hdmi = "hdmi" -) - -type args struct { - bundleDirPath string - cmd string -} - -// GetDeviceTypeByChipName get device type by chipName -func GetDeviceTypeByChipName(chipName string) string { - if strings.Contains(chipName, "310B") { - return Ascend310B - } - if strings.Contains(chipName, "310P") { - return Ascend310P - } - if strings.Contains(chipName, "310") { - return Ascend310 - } - if strings.Contains(chipName, "910") { - return Ascend910 - } - return "" -} - -func getArgs() (*args, error) { - args := &args{} - - for i, param := range os.Args { - if param == "--bundle" || param == "-b" { - if len(os.Args)-i <= 1 { - return nil, fmt.Errorf("bundle option needs an argument") - } - args.bundleDirPath = os.Args[i+1] - } else if param == "create" { - args.cmd = param - } - } - - return args, nil -} - -func initLogModule(ctx context.Context) error { - const backups = 2 - const logMaxAge = 365 - const fileMaxSize = 2 - runLogConfig := hwlog.LogConfig{ - LogFileName: runLogPath, - LogLevel: 0, - MaxBackups: backups, - MaxAge: logMaxAge, - OnlyToFile: true, - FileMaxSize: fileMaxSize, - } - if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { - fmt.Printf("hwlog init failed, error is %v", err) - return err - } - return nil -} - -var execRunc = func() error { - tempRuncPath, err := exec.LookPath(dockerRuncName) - if err != nil { - tempRuncPath, err = exec.LookPath(runcName) - if err != nil { - return fmt.Errorf("failed to find the path of runc: %v", err) - } - } - runcPath, err := filepath.EvalSymlinks(tempRuncPath) - if err != nil { - return fmt.Errorf("failed to find realpath of runc %v", err) - } - if _, err := mindxcheckutils.RealFileChecker(runcPath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err - } - - if err := mindxcheckutils.ChangeRuntimeLogMode("runtime-run-"); err != nil { - return err - } - if err = syscall.Exec(runcPath, append([]string{runcPath}, os.Args[1:]...), os.Environ()); err != nil { - return fmt.Errorf("failed to exec runc: %v", err) - } - - return nil -} - -func addAscendDockerEnv(spec *specs.Spec) { - if spec == nil || spec.Process == nil || spec.Process.Env == nil { - return - } - spec.Process.Env = append(spec.Process.Env, useAscendDocker) -} - -func addHook(spec *specs.Spec, deviceIdList *[]int) error { - if deviceIdList == nil { - return nil - } - currentExecPath, err := os.Executable() - if err != nil { - return fmt.Errorf("cannot get the path of ascend-docker-runtime: %v", err) - } - - hookCliPath = path.Join(path.Dir(currentExecPath), hookCli) - if _, err := mindxcheckutils.RealFileChecker(hookCliPath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err - } - if _, err = os.Stat(hookCliPath); err != nil { - return fmt.Errorf("cannot find ascend-docker-hook executable file at %s: %v", hookCliPath, err) - } - - if spec.Hooks == nil { - spec.Hooks = &specs.Hooks{} - } - - needUpdate := true - if len(spec.Hooks.Prestart) > maxCommandLength { - return fmt.Errorf("too many items in Prestart ") - } - for _, hook := range spec.Hooks.Prestart { - if strings.Contains(hook.Path, hookCli) { - needUpdate = false - break - } - } - if needUpdate { - spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{ - Path: hookCliPath, - Args: []string{hookCliPath}, - }) - } - - if len(spec.Process.Env) > maxCommandLength { - return fmt.Errorf("too many items in Env ") - } - - if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { - return nil - } - - vdevice, err := dcmi.CreateVDevice(&dcmi.NpuWorker{}, spec, *deviceIdList) - if err != nil { - return err - } - hwlog.RunLog.Infof("vnpu split done: vdevice: %v", vdevice.VdeviceID) - - if vdevice.VdeviceID != -1 { - updateEnvAndPostHook(spec, vdevice, deviceIdList) - } - - return nil -} - -func removeDuplication(devices []int) []int { - list := make([]int, 0, len(devices)) - prev := -1 - - for _, device := range devices { - if device == prev { - continue - } - - list = append(list, device) - prev = device - } - - return list -} - -func parseDevices(visibleDevices string) ([]int, error) { - devices := make([]int, 0) - const maxDevice = 128 - - for _, d := range strings.Split(visibleDevices, ",") { - d = strings.TrimSpace(d) - if strings.Contains(d, "-") { - borders := strings.Split(d, "-") - if len(borders) != borderNum { - return nil, fmt.Errorf("invalid device range: %s", d) - } - - borders[0] = strings.TrimSpace(borders[0]) - borders[1] = strings.TrimSpace(borders[1]) - - left, err := strconv.Atoi(borders[0]) - if err != nil || left < 0 { - return nil, fmt.Errorf("invalid left boarder range parameter: %s", borders[0]) - } - - right, err := strconv.Atoi(borders[1]) - if err != nil || right > maxDevice { - return nil, fmt.Errorf("invalid right boarder range parameter: %s", borders[1]) - } - - if left > right { - return nil, fmt.Errorf("left boarder (%d) should not be larger than the right one(%d)", left, right) - } - - for n := left; n <= right; n++ { - devices = append(devices, n) - } - } else { - n, err := strconv.Atoi(d) - if err != nil { - return nil, fmt.Errorf("invalid single device parameter: %s", d) - } - - devices = append(devices, n) - } - } - - sort.Slice(devices, func(i, j int) bool { return i < j }) - return removeDuplication(devices), nil -} - -func parseAscendDevices(visibleDevices string) ([]int, error) { - devicesList := strings.Split(visibleDevices, ",") - devices := make([]int, 0, len(devicesList)) - chipType := "" - - for _, d := range devicesList { - matchGroups := regexp.MustCompile(`^Ascend(910|310|310B|310P)-(\d+)$`).FindStringSubmatch(strings.TrimSpace(d)) - if matchGroups == nil { - return nil, fmt.Errorf("invalid device format: %s", d) - } - n, err := strconv.Atoi(matchGroups[2]) - if err != nil { - return nil, fmt.Errorf("invalid device id: %s", d) - } - - if chipType == "" { - chipType = matchGroups[1] - } - if chipType != "" && chipType != matchGroups[1] { - return nil, fmt.Errorf("invalid device chip type: %s", d) - } - - devices = append(devices, n) - - } - chipName, err := dcmi.GetChipName() - if err != nil { - return nil, fmt.Errorf("get chip name error: %v", err) - } - if ascend+chipType != GetDeviceTypeByChipName(chipName) { - return nil, fmt.Errorf("chip type not match really: %s", chipType) - } - - sort.Slice(devices, func(i, j int) bool { return i < j }) - return removeDuplication(devices), nil -} - -func getValueByKey(data []string, name string) string { - for _, envLine := range data { - words := strings.SplitN(envLine, "=", kvPairSize) - if len(words) != kvPairSize { - hwlog.RunLog.Error("environment error") - return "" - } - - if words[0] == name { - return words[1] - } - } - - return "" -} - -func getValueByDeviceKey(data []string) string { - res := "" - for i := len(data) - 1; i >= 0; i-- { - words := strings.SplitN(data[i], "=", kvPairSize) - if len(words) != kvPairSize { - hwlog.RunLog.Error("environment error") - return "" - } - - if words[0] == ascendVisibleDevices { - res = words[1] - break - } - } - if res == "" { - hwlog.RunLog.Error("ASCEND_VISIBLE_DEVICES env variable is empty, will not mount any ascend device") - } - return res -} - -func addDeviceToSpec(spec *specs.Spec, dPath string, deviceType string) error { - device, err := oci.DeviceFromPath(dPath) - if err != nil { - return fmt.Errorf("failed to get %s info : %#v", dPath, err) - } - - switch deviceType { - case virtualDavinciName: - vDeviceNumber := regexp.MustCompile("[0-9]+").FindAllString(dPath, -1) - if len(vDeviceNumber) != 1 { - return fmt.Errorf("invalid vdavinci path: %s", dPath) - } - device.Path = devicePath + davinciName + vDeviceNumber[0] - case davinciManagerDocker: - device.Path = devicePath + davinciManager - default: // do nothing - - } - - spec.Linux.Devices = append(spec.Linux.Devices, *device) - newDeviceCgroup := specs.LinuxDeviceCgroup{ - Allow: true, - Type: device.Type, - Major: &device.Major, - Minor: &device.Minor, - Access: "rwm", - } - spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, newDeviceCgroup) - return nil -} - -func addAscend310BManagerDevice(spec *specs.Spec) error { - var Ascend310BManageDevices = []string{ - svm0, - tsAisle, - upgrade, - sys, - vdec, - vpc, - pngd, - venc, - dvppCmdList, - logDrv, - acodec, - ai, - ao, - vo, - hdmi, - } - - for _, device := range Ascend310BManageDevices { - dPath := devicePath + device - if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { - hwlog.RunLog.Warnf("failed to add %s to spec : %#v", dPath, err) - } - } - - davinciManagerPath := devicePath + davinciManagerDocker - if _, err := os.Stat(davinciManagerPath); err != nil { - hwlog.RunLog.Warnf("failed to get davinci manager docker, err: %#v", err) - davinciManagerPath = devicePath + davinciManager - if _, err := os.Stat(davinciManagerPath); err != nil { - return fmt.Errorf("failed to get davinci manager, err: %#v", err) - } - } - return addDeviceToSpec(spec, davinciManagerPath, davinciManagerDocker) -} - -func addCommonManagerDevice(spec *specs.Spec) error { - var commonManagerDevices = []string{ - devmmSvm, - hisiHdc, - } - - for _, device := range commonManagerDevices { - dPath := devicePath + device - if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { - return fmt.Errorf("failed to add common manage device to spec : %#v", err) - } - } - - return nil -} - -func addManagerDevice(spec *specs.Spec) error { - chipName, err := dcmi.GetChipName() - if err != nil { - return fmt.Errorf("get chip name error: %#v", err) - } - devType := GetDeviceTypeByChipName(chipName) - hwlog.RunLog.Infof("device type is: %s", devType) - if devType == Ascend310B { - return addAscend310BManagerDevice(spec) - } - - if err := addDeviceToSpec(spec, devicePath+davinciManager, notRenameDeviceType); err != nil { - return fmt.Errorf("add davinci_manager to spec error: %#v", err) - } - - productType, err := dcmi.GetProductType(&dcmi.NpuWorker{}) - if err != nil { - return fmt.Errorf("parse product type error: %#v", err) - } - hwlog.RunLog.Infof("product type is %s", productType) - - switch productType { - // do nothing - case Atlas200ISoc, Atlas200: - default: - if err = addCommonManagerDevice(spec); err != nil { - return fmt.Errorf("add common manage device error: %#v", err) - } - } - - return nil -} - -func checkVisibleDevice(spec *specs.Spec) ([]int, error) { - visibleDevices := getValueByDeviceKey(spec.Process.Env) - if visibleDevices == "" || visibleDevices == void { - return nil, nil - } - - if strings.Contains(visibleDevices, ascend) { - devices, err := parseAscendDevices(visibleDevices) - if err != nil { - return nil, fmt.Errorf("failed to parse ascend device : %v", err) - } - hwlog.RunLog.Infof("ascend devices is: %v", devices) - return devices, err - } - devices, err := parseDevices(visibleDevices) - if err != nil { - return nil, fmt.Errorf("failed to parse device : %v", err) - } - hwlog.RunLog.Infof("devices is: %v", devices) - return devices, err -} - -func addDevice(spec *specs.Spec, deviceIdList []int) error { - deviceName := davinciName - if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { - deviceName = virtualDavinciName - } - for _, deviceId := range deviceIdList { - dPath := devicePath + deviceName + strconv.Itoa(deviceId) - if err := addDeviceToSpec(spec, dPath, deviceName); err != nil { - return fmt.Errorf("failed to add davinci device to spec: %v", err) - } - } - - if err := addManagerDevice(spec); err != nil { - return fmt.Errorf("failed to add Manager device to spec: %v", err) - } - - return nil -} - -func updateEnvAndPostHook(spec *specs.Spec, vdevice dcmi.VDeviceInfo, deviceIdList *[]int) { - if deviceIdList == nil { - return - } - newEnv := make([]string, 0, len(spec.Process.Env)+1) - needAddVirtualFlag := true - *deviceIdList = []int{int(vdevice.VdeviceID)} - for _, line := range spec.Process.Env { - words := strings.Split(line, "=") - if len(words) == envLength && strings.TrimSpace(words[0]) == ascendRuntimeOptions { - needAddVirtualFlag = false - if strings.Contains(words[1], "VIRTUAL") { - newEnv = append(newEnv, line) - continue - } else { - newEnv = append(newEnv, strings.TrimSpace(line)+",VIRTUAL") - continue - } - } - newEnv = append(newEnv, line) - } - if needAddVirtualFlag { - newEnv = append(newEnv, fmt.Sprintf("ASCEND_RUNTIME_OPTIONS=VIRTUAL")) - } - spec.Process.Env = newEnv - if currentExecPath, err := os.Executable(); err == nil { - postHookCliPath := path.Join(path.Dir(currentExecPath), destroyHookCli) - spec.Hooks.Poststop = append(spec.Hooks.Poststop, specs.Hook{ - Path: postHookCliPath, - Args: []string{postHookCliPath, fmt.Sprintf("%d", vdevice.CardID), fmt.Sprintf("%d", vdevice.DeviceID), - fmt.Sprintf("%d", vdevice.VdeviceID)}, - }) - } -} - -func modifySpecFile(path string) error { - stat, err := os.Stat(path) - if err != nil { - return fmt.Errorf("spec file doesnt exist %s: %v", path, err) - } - if _, err = mindxcheckutils.RealFileChecker(path, true, true, mindxcheckutils.DefaultSize); err != nil { - return err - } - - jsonFile, err := os.OpenFile(path, os.O_RDWR, stat.Mode()) - if err != nil { - return fmt.Errorf("cannot open oci spec file %s: %v", path, err) - } - - defer jsonFile.Close() - - jsonContent, err := ioutil.ReadAll(jsonFile) - if err != nil { - return fmt.Errorf("failed to read oci spec file %s: %v", path, err) - } - - if err = jsonFile.Truncate(0); err != nil { - return fmt.Errorf("failed to truncate: %v", err) - } - if _, err = jsonFile.Seek(0, 0); err != nil { - return fmt.Errorf("failed to seek: %v", err) - } - - var spec specs.Spec - if err = json.Unmarshal(jsonContent, &spec); err != nil { - return fmt.Errorf("failed to unmarshal oci spec file %s: %v", path, err) - } - - devices, err := checkVisibleDevice(&spec) - if err != nil { - hwlog.RunLog.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) - return fmt.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) - } - if len(devices) != 0 { - if err = addHook(&spec, &devices); err != nil { - hwlog.RunLog.Errorf("failed to inject hook, err: %v", err) - return fmt.Errorf("failed to inject hook, err: %v", err) - } - if err = addDevice(&spec, devices); err != nil { - return fmt.Errorf("failed to add device to env: %v", err) - } - } - - addAscendDockerEnv(&spec) - - jsonOutput, err := json.Marshal(spec) - if err != nil { - return fmt.Errorf("failed to marshal OCI spec file: %v", err) - } - - if _, err = jsonFile.WriteAt(jsonOutput, 0); err != nil { - return fmt.Errorf("failed to write OCI spec file: %v", err) - } - - return nil -} - -func doProcess() error { - args, err := getArgs() - if err != nil { - return fmt.Errorf("failed to get args: %v", err) - } - - if args.cmd != "create" { - return execRunc() - } - - if args.bundleDirPath == "" { - args.bundleDirPath, err = os.Getwd() - if err != nil { - return fmt.Errorf("failed to get current working dir: %v", err) - } - } - - specFilePath := args.bundleDirPath + "/config.json" - - if err = modifySpecFile(specFilePath); err != nil { - return fmt.Errorf("failed to modify spec file %s: %v", specFilePath, err) - } - - return execRunc() -} - func main() { defer func() { if err := recover(); err != nil { @@ -682,7 +18,7 @@ func main() { } }() ctx, _ := context.WithCancel(context.Background()) - if err := initLogModule(ctx); err != nil { + if err := process.InitLogModule(ctx); err != nil { log.Fatal(err) } logPrefixWords, err := mindxcheckutils.GetLogPrefix() @@ -695,11 +31,11 @@ func main() { } }() if !mindxcheckutils.StringChecker(strings.Join(os.Args, " "), 0, - maxCommandLength, mindxcheckutils.DefaultWhiteList+" ") { + process.MaxCommandLength, mindxcheckutils.DefaultWhiteList+" ") { hwlog.RunLog.Errorf("%v ascend docker runtime args check failed", logPrefixWords) log.Fatal("command error") } - if err = doProcess(); err != nil { + if err = process.DoProcess(); err != nil { hwlog.RunLog.Errorf("%v docker runtime failed: %v", logPrefixWords, err) log.Fatal(err) } diff --git a/runtime/process/process.go b/runtime/process/process.go new file mode 100644 index 0000000..9755d45 --- /dev/null +++ b/runtime/process/process.go @@ -0,0 +1,676 @@ +/* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +// Package process +package process + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" + "syscall" + + "github.com/containerd/containerd/oci" + "github.com/opencontainers/runtime-spec/specs-go" + "huawei.com/npu-exporter/v5/common-utils/hwlog" + + "ascend-docker-runtime/mindxcheckutils" + "ascend-docker-runtime/runtime/dcmi" +) + +const ( + runLogPath = "/var/log/ascend-docker-runtime/runtime-run.log" + hookDefaultFilePath = "/usr/local/bin/ascend-docker-hook" + // MaxCommandLength is the max length of command + MaxCommandLength = 65535 + hookCli = "ascend-docker-hook" + destroyHookCli = "ascend-docker-destroy" + dockerRuncFile = "docker-runc" + runcFile = "runc" + envLength = 2 + kvPairSize = 2 + borderNum = 2 + + // ENV for device-plugin to identify ascend-docker-runtime + useAscendDocker = "ASCEND_DOCKER_RUNTIME=True" + devicePlugin = "ascend-device-plugin" + ascendVisibleDevices = "ASCEND_VISIBLE_DEVICES" + ascendRuntimeOptions = "ASCEND_RUNTIME_OPTIONS" + + // void indicates that the NPU card does not need to be mounted + void = "void" +) + +var ( + hookCliPath = hookCli + hookDefaultFile = hookDefaultFilePath + dockerRuncName = dockerRuncFile + runcName = runcFile +) + +const ( + // Atlas200ISoc Product name + Atlas200ISoc = "Atlas 200I SoC A1" + // Atlas200 Product name + Atlas200 = "Atlas 200 Model 3000" + // Ascend310 ascend 310 chip + Ascend310 = "Ascend310" + // Ascend310P ascend 310P chip + Ascend310P = "Ascend310P" + // Ascend310B ascend 310B chip + Ascend310B = "Ascend310B" + // Ascend910 ascend 910 chip + Ascend910 = "Ascend910" + ascend = "Ascend" + + devicePath = "/dev/" + davinciName = "davinci" + virtualDavinciName = "vdavinci" + davinciManager = "davinci_manager" + davinciManagerDocker = "davinci_manager_docker" + notRenameDeviceType = "" + devmmSvm = "devmm_svm" + hisiHdc = "hisi_hdc" + svm0 = "svm0" + tsAisle = "ts_aisle" + upgrade = "upgrade" + sys = "sys" + vdec = "vdec" + vpc = "vpc" + pngd = "pngd" + venc = "venc" + dvppCmdList = "dvpp_cmdlist" + logDrv = "log_drv" + acodec = "acodec" + ai = "ai" + ao = "ao" + vo = "vo" + hdmi = "hdmi" +) + +type args struct { + bundleDirPath string + cmd string +} + +// GetDeviceTypeByChipName get device type by chipName +func GetDeviceTypeByChipName(chipName string) string { + if strings.Contains(chipName, "310B") { + return Ascend310B + } + if strings.Contains(chipName, "310P") { + return Ascend310P + } + if strings.Contains(chipName, "310") { + return Ascend310 + } + if strings.Contains(chipName, "910") { + return Ascend910 + } + return "" +} + +// InitLogModule inits the log info +func InitLogModule(ctx context.Context) error { + const backups = 2 + const logMaxAge = 365 + const fileMaxSize = 2 + runLogConfig := hwlog.LogConfig{ + LogFileName: runLogPath, + LogLevel: 0, + MaxBackups: backups, + MaxAge: logMaxAge, + OnlyToFile: true, + FileMaxSize: fileMaxSize, + } + if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { + fmt.Printf("hwlog init failed, error is %v", err) + return err + } + return nil +} + +func getArgs() (*args, error) { + args := &args{} + + for i, param := range os.Args { + if param == "--bundle" || param == "-b" { + if len(os.Args)-i <= 1 { + return nil, fmt.Errorf("bundle option needs an argument") + } + args.bundleDirPath = os.Args[i+1] + } else if param == "create" { + args.cmd = param + } + } + + return args, nil +} + +var execRunc = func() error { + tempRuncPath, err := exec.LookPath(dockerRuncName) + if err != nil { + tempRuncPath, err = exec.LookPath(runcName) + if err != nil { + return fmt.Errorf("failed to find the path of runc: %v", err) + } + } + runcPath, err := filepath.EvalSymlinks(tempRuncPath) + if err != nil { + return fmt.Errorf("failed to find realpath of runc %v", err) + } + if _, err := mindxcheckutils.RealFileChecker(runcPath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err + } + + if err := mindxcheckutils.ChangeRuntimeLogMode("runtime-run-"); err != nil { + return err + } + if err = syscall.Exec(runcPath, append([]string{runcPath}, os.Args[1:]...), os.Environ()); err != nil { + return fmt.Errorf("failed to exec runc: %v", err) + } + + return nil +} + +func addAscendDockerEnv(spec *specs.Spec) { + if spec == nil || spec.Process == nil || spec.Process.Env == nil { + return + } + spec.Process.Env = append(spec.Process.Env, useAscendDocker) +} + +func addHook(spec *specs.Spec, deviceIdList *[]int) error { + if deviceIdList == nil { + return nil + } + currentExecPath, err := os.Executable() + if err != nil { + return fmt.Errorf("cannot get the path of ascend-docker-runtime: %v", err) + } + + hookCliPath = path.Join(path.Dir(currentExecPath), hookCli) + if _, err := mindxcheckutils.RealFileChecker(hookCliPath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err + } + if _, err = os.Stat(hookCliPath); err != nil { + return fmt.Errorf("cannot find ascend-docker-hook executable file at %s: %v", hookCliPath, err) + } + + if spec.Hooks == nil { + spec.Hooks = &specs.Hooks{} + } + + needUpdate := true + if len(spec.Hooks.Prestart) > MaxCommandLength { + return fmt.Errorf("too many items in Prestart ") + } + for _, hook := range spec.Hooks.Prestart { + if strings.Contains(hook.Path, hookCli) { + needUpdate = false + break + } + } + if needUpdate { + spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{ + Path: hookCliPath, + Args: []string{hookCliPath}, + }) + } + + if len(spec.Process.Env) > MaxCommandLength { + return fmt.Errorf("too many items in Env ") + } + + if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { + return nil + } + + vdevice, err := dcmi.CreateVDevice(&dcmi.NpuWorker{}, spec, *deviceIdList) + if err != nil { + return err + } + hwlog.RunLog.Infof("vnpu split done: vdevice: %v", vdevice.VdeviceID) + + if vdevice.VdeviceID != -1 { + updateEnvAndPostHook(spec, vdevice, deviceIdList) + } + + return nil +} + +func removeDuplication(devices []int) []int { + list := make([]int, 0, len(devices)) + prev := -1 + + for _, device := range devices { + if device == prev { + continue + } + + list = append(list, device) + prev = device + } + + return list +} + +func parseDevices(visibleDevices string) ([]int, error) { + devices := make([]int, 0) + const maxDevice = 128 + + for _, d := range strings.Split(visibleDevices, ",") { + d = strings.TrimSpace(d) + if strings.Contains(d, "-") { + borders := strings.Split(d, "-") + if len(borders) != borderNum { + return nil, fmt.Errorf("invalid device range: %s", d) + } + + borders[0] = strings.TrimSpace(borders[0]) + borders[1] = strings.TrimSpace(borders[1]) + + left, err := strconv.Atoi(borders[0]) + if err != nil || left < 0 { + return nil, fmt.Errorf("invalid left boarder range parameter: %s", borders[0]) + } + + right, err := strconv.Atoi(borders[1]) + if err != nil || right > maxDevice { + return nil, fmt.Errorf("invalid right boarder range parameter: %s", borders[1]) + } + + if left > right { + return nil, fmt.Errorf("left boarder (%d) should not be larger than the right one(%d)", left, right) + } + + for n := left; n <= right; n++ { + devices = append(devices, n) + } + } else { + n, err := strconv.Atoi(d) + if err != nil { + return nil, fmt.Errorf("invalid single device parameter: %s", d) + } + + devices = append(devices, n) + } + } + + sort.Slice(devices, func(i, j int) bool { return i < j }) + return removeDuplication(devices), nil +} + +func parseAscendDevices(visibleDevices string) ([]int, error) { + devicesList := strings.Split(visibleDevices, ",") + devices := make([]int, 0, len(devicesList)) + chipType := "" + + for _, d := range devicesList { + matchGroups := regexp.MustCompile(`^Ascend(910|310|310B|310P)-(\d+)$`).FindStringSubmatch(strings.TrimSpace(d)) + if matchGroups == nil { + return nil, fmt.Errorf("invalid device format: %s", d) + } + n, err := strconv.Atoi(matchGroups[2]) + if err != nil { + return nil, fmt.Errorf("invalid device id: %s", d) + } + + if chipType == "" { + chipType = matchGroups[1] + } + if chipType != "" && chipType != matchGroups[1] { + return nil, fmt.Errorf("invalid device chip type: %s", d) + } + + devices = append(devices, n) + + } + chipName, err := dcmi.GetChipName() + if err != nil { + return nil, fmt.Errorf("get chip name error: %v", err) + } + if ascend+chipType != GetDeviceTypeByChipName(chipName) { + return nil, fmt.Errorf("chip type not match really: %s", chipType) + } + + sort.Slice(devices, func(i, j int) bool { return i < j }) + return removeDuplication(devices), nil +} + +func getValueByKey(data []string, name string) string { + for _, envLine := range data { + words := strings.SplitN(envLine, "=", kvPairSize) + if len(words) != kvPairSize { + hwlog.RunLog.Error("environment error") + return "" + } + + if words[0] == name { + return words[1] + } + } + + return "" +} + +func getValueByDeviceKey(data []string) string { + res := "" + for i := len(data) - 1; i >= 0; i-- { + words := strings.SplitN(data[i], "=", kvPairSize) + if len(words) != kvPairSize { + hwlog.RunLog.Error("environment error") + return "" + } + + if words[0] == ascendVisibleDevices { + res = words[1] + break + } + } + if res == "" { + hwlog.RunLog.Error("ASCEND_VISIBLE_DEVICES env variable is empty, will not mount any ascend device") + } + return res +} + +func addDeviceToSpec(spec *specs.Spec, dPath string, deviceType string) error { + device, err := oci.DeviceFromPath(dPath) + if err != nil { + return fmt.Errorf("failed to get %s info : %#v", dPath, err) + } + + switch deviceType { + case virtualDavinciName: + vDeviceNumber := regexp.MustCompile("[0-9]+").FindAllString(dPath, -1) + if len(vDeviceNumber) != 1 { + return fmt.Errorf("invalid vdavinci path: %s", dPath) + } + device.Path = devicePath + davinciName + vDeviceNumber[0] + case davinciManagerDocker: + device.Path = devicePath + davinciManager + default: // do nothing + + } + + spec.Linux.Devices = append(spec.Linux.Devices, *device) + newDeviceCgroup := specs.LinuxDeviceCgroup{ + Allow: true, + Type: device.Type, + Major: &device.Major, + Minor: &device.Minor, + Access: "rwm", + } + spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, newDeviceCgroup) + return nil +} + +func addAscend310BManagerDevice(spec *specs.Spec) error { + var Ascend310BManageDevices = []string{ + svm0, + tsAisle, + upgrade, + sys, + vdec, + vpc, + pngd, + venc, + dvppCmdList, + logDrv, + acodec, + ai, + ao, + vo, + hdmi, + } + + for _, device := range Ascend310BManageDevices { + dPath := devicePath + device + if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { + hwlog.RunLog.Warnf("failed to add %s to spec : %#v", dPath, err) + } + } + + davinciManagerPath := devicePath + davinciManagerDocker + if _, err := os.Stat(davinciManagerPath); err != nil { + hwlog.RunLog.Warnf("failed to get davinci manager docker, err: %#v", err) + davinciManagerPath = devicePath + davinciManager + if _, err := os.Stat(davinciManagerPath); err != nil { + return fmt.Errorf("failed to get davinci manager, err: %#v", err) + } + } + return addDeviceToSpec(spec, davinciManagerPath, davinciManagerDocker) +} + +func addCommonManagerDevice(spec *specs.Spec) error { + var commonManagerDevices = []string{ + devmmSvm, + hisiHdc, + } + + for _, device := range commonManagerDevices { + dPath := devicePath + device + if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { + return fmt.Errorf("failed to add common manage device to spec : %#v", err) + } + } + + return nil +} + +func addManagerDevice(spec *specs.Spec) error { + chipName, err := dcmi.GetChipName() + if err != nil { + return fmt.Errorf("get chip name error: %#v", err) + } + devType := GetDeviceTypeByChipName(chipName) + hwlog.RunLog.Infof("device type is: %s", devType) + if devType == Ascend310B { + return addAscend310BManagerDevice(spec) + } + + if err := addDeviceToSpec(spec, devicePath+davinciManager, notRenameDeviceType); err != nil { + return fmt.Errorf("add davinci_manager to spec error: %#v", err) + } + + productType, err := dcmi.GetProductType(&dcmi.NpuWorker{}) + if err != nil { + return fmt.Errorf("parse product type error: %#v", err) + } + hwlog.RunLog.Infof("product type is %s", productType) + + switch productType { + // do nothing + case Atlas200ISoc, Atlas200: + default: + if err = addCommonManagerDevice(spec); err != nil { + return fmt.Errorf("add common manage device error: %#v", err) + } + } + + return nil +} + +func checkVisibleDevice(spec *specs.Spec) ([]int, error) { + visibleDevices := getValueByDeviceKey(spec.Process.Env) + if visibleDevices == "" || visibleDevices == void { + return nil, nil + } + + if strings.Contains(visibleDevices, ascend) { + devices, err := parseAscendDevices(visibleDevices) + if err != nil { + return nil, fmt.Errorf("failed to parse ascend device : %v", err) + } + hwlog.RunLog.Infof("ascend devices is: %v", devices) + return devices, err + } + devices, err := parseDevices(visibleDevices) + if err != nil { + return nil, fmt.Errorf("failed to parse device : %v", err) + } + hwlog.RunLog.Infof("devices is: %v", devices) + return devices, err +} + +func addDevice(spec *specs.Spec, deviceIdList []int) error { + deviceName := davinciName + if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { + deviceName = virtualDavinciName + } + for _, deviceId := range deviceIdList { + dPath := devicePath + deviceName + strconv.Itoa(deviceId) + if err := addDeviceToSpec(spec, dPath, deviceName); err != nil { + return fmt.Errorf("failed to add davinci device to spec: %v", err) + } + } + + if err := addManagerDevice(spec); err != nil { + return fmt.Errorf("failed to add Manager device to spec: %v", err) + } + + return nil +} + +func updateEnvAndPostHook(spec *specs.Spec, vdevice dcmi.VDeviceInfo, deviceIdList *[]int) { + if deviceIdList == nil { + return + } + newEnv := make([]string, 0, len(spec.Process.Env)+1) + needAddVirtualFlag := true + *deviceIdList = []int{int(vdevice.VdeviceID)} + for _, line := range spec.Process.Env { + words := strings.Split(line, "=") + if len(words) == envLength && strings.TrimSpace(words[0]) == ascendRuntimeOptions { + needAddVirtualFlag = false + if strings.Contains(words[1], "VIRTUAL") { + newEnv = append(newEnv, line) + continue + } else { + newEnv = append(newEnv, strings.TrimSpace(line)+",VIRTUAL") + continue + } + } + newEnv = append(newEnv, line) + } + if needAddVirtualFlag { + newEnv = append(newEnv, fmt.Sprintf("ASCEND_RUNTIME_OPTIONS=VIRTUAL")) + } + spec.Process.Env = newEnv + if currentExecPath, err := os.Executable(); err == nil { + postHookCliPath := path.Join(path.Dir(currentExecPath), destroyHookCli) + spec.Hooks.Poststop = append(spec.Hooks.Poststop, specs.Hook{ + Path: postHookCliPath, + Args: []string{postHookCliPath, fmt.Sprintf("%d", vdevice.CardID), fmt.Sprintf("%d", vdevice.DeviceID), + fmt.Sprintf("%d", vdevice.VdeviceID)}, + }) + } +} + +func modifySpecFile(path string) error { + stat, err := os.Stat(path) + if err != nil { + return fmt.Errorf("spec file doesnt exist %s: %v", path, err) + } + if _, err = mindxcheckutils.RealFileChecker(path, true, true, mindxcheckutils.DefaultSize); err != nil { + return err + } + + jsonFile, err := os.OpenFile(path, os.O_RDWR, stat.Mode()) + if err != nil { + return fmt.Errorf("cannot open oci spec file %s: %v", path, err) + } + + defer jsonFile.Close() + + jsonContent, err := ioutil.ReadAll(jsonFile) + if err != nil { + return fmt.Errorf("failed to read oci spec file %s: %v", path, err) + } + + if err = jsonFile.Truncate(0); err != nil { + return fmt.Errorf("failed to truncate: %v", err) + } + if _, err = jsonFile.Seek(0, 0); err != nil { + return fmt.Errorf("failed to seek: %v", err) + } + + var spec specs.Spec + if err = json.Unmarshal(jsonContent, &spec); err != nil { + return fmt.Errorf("failed to unmarshal oci spec file %s: %v", path, err) + } + + devices, err := checkVisibleDevice(&spec) + if err != nil { + hwlog.RunLog.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) + return fmt.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) + } + if len(devices) != 0 { + if err = addHook(&spec, &devices); err != nil { + hwlog.RunLog.Errorf("failed to inject hook, err: %v", err) + return fmt.Errorf("failed to inject hook, err: %v", err) + } + if err = addDevice(&spec, devices); err != nil { + return fmt.Errorf("failed to add device to env: %v", err) + } + } + + addAscendDockerEnv(&spec) + + jsonOutput, err := json.Marshal(spec) + if err != nil { + return fmt.Errorf("failed to marshal OCI spec file: %v", err) + } + + if _, err = jsonFile.WriteAt(jsonOutput, 0); err != nil { + return fmt.Errorf("failed to write OCI spec file: %v", err) + } + + return nil +} + +func DoProcess() error { + args, err := getArgs() + if err != nil { + return fmt.Errorf("failed to get args: %v", err) + } + + if args.cmd != "create" { + return execRunc() + } + + if args.bundleDirPath == "" { + args.bundleDirPath, err = os.Getwd() + if err != nil { + return fmt.Errorf("failed to get current working dir: %v", err) + } + } + + specFilePath := args.bundleDirPath + "/config.json" + + if err = modifySpecFile(specFilePath); err != nil { + return fmt.Errorf("failed to modify spec file %s: %v", specFilePath, err) + } + + return execRunc() +} diff --git a/runtime/main_test.go b/runtime/process/process_test.go similarity index 98% rename from runtime/main_test.go rename to runtime/process/process_test.go index a3920a4..d3aa876 100644 --- a/runtime/main_test.go +++ b/runtime/process/process_test.go @@ -13,7 +13,7 @@ */ // Package main -package main +package process import ( "context" @@ -27,7 +27,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/assert" - "main/dcmi" + "ascend-docker-runtime/runtime/dcmi" ) const ( @@ -57,7 +57,7 @@ func TestArgsIsCreate(t *testing.T) { return nil }) - err := doProcess() + err := DoProcess() assert.NotNil(t, err) } @@ -73,7 +73,7 @@ func TestArgsIsCreateCase1(t *testing.T) { return nil }) - err := doProcess() + err := DoProcess() assert.NotNil(t, err) } @@ -89,7 +89,7 @@ func TestArgsIsCreateCase2(t *testing.T) { return nil }) - err := doProcess() + err := DoProcess() assert.NotNil(t, err) } @@ -117,7 +117,7 @@ func TestArgsIsCreateCase3(t *testing.T) { return nil }) - err = doProcess() + err = DoProcess() assert.NotNil(t, err) } @@ -145,7 +145,7 @@ func TestArgsIsCreateCase4(t *testing.T) { return nil }) - err = doProcess() + err = DoProcess() assert.Nil(t, err) } -- Gitee From 42ef1ebb2dca6858f3ac8bd2f5d3817d824e3169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 11:49:15 +0800 Subject: [PATCH 02/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E5=A2=9E=E5=8A=A0containerd?= =?UTF-8?q?=20cgroup=20v1=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/help.info | 3 +- build/scripts/run_main.sh | 4 +- install/process/constant.go | 18 +++- install/process/containerd_process.go | 127 ++++++++++++++++++++++++-- 4 files changed, 134 insertions(+), 18 deletions(-) diff --git a/build/scripts/help.info b/build/scripts/help.info index b99ec05..7e273ee 100644 --- a/build/scripts/help.info +++ b/build/scripts/help.info @@ -7,4 +7,5 @@ (eg: --install-type=A200IA2, when your product is A200I A2 or A200I DK A2) --ce= Only iSula need to specify the container engine(eg: --ce=isula) MUST use with --install or --uninstall - --version Query Ascend-docker-runtime version \ No newline at end of file + --version Query Ascend-docker-runtime version + --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) \ No newline at end of file diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index 26e6121..dbc491b 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -462,9 +462,9 @@ do fi need_help=n INSTALL_SCENE_FLAG=y - if [ "$3" == "install-scene=docker" ]; then + if [ "$3" == "--install-scene=docker" ]; then INSTALL_SCENE=docker - elif [ "$3" == "install-scene=containerd" ]; then + elif [ "$3" == "--install-scene=containerd" ]; then INSTALL_SCENE=containerd else log "[ERROR]" "failed, Please check the parameter of --install-scene=" diff --git a/install/process/constant.go b/install/process/constant.go index 64fdb9b..80904b6 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -35,9 +35,17 @@ const ( // InstallSceneDocker is a 'docker' string of scene InstallSceneDocker = "docker" // InstallSceneContainerd is a 'containerd' string of scene - InstallSceneContainerd = "containerd" - cgroupsFilePath = "/proc/cgroups" - hierarchyStr = "hierarchy" - initHierarchyCol = -1 - cgroupV2HierarchyValue = "2" + InstallSceneContainerd = "containerd" + cgroupsFilePath = "/proc/cgroups" + hierarchyStr = "hierarchy" + initHierarchyCol = -1 + cgroupV2HierarchyValue = "2" + v1NeedChangeKeyRuntime = "runtime" + v1NeedChangeKeyRuntimeType = "runtime_type" + v1RuntimeType = "io.containerd.runtime.v1.linux" + defaultRuntimeValue = "runc" + v1RuntimeTypeFisrtLevelPlugin = "io.containerd.grpc.v1.cri" + containerdKey = "containerd" + runtimesKey = "runtimes" + runcKey = "runc" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index e9398f7..b952b80 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -3,7 +3,7 @@ package process import ( "bufio" "fmt" - "log" + "github.com/pelletier/go-toml" "os" "path/filepath" "strings" @@ -44,7 +44,7 @@ func ContainerdProcess(command []string) (error, string) { return err, behavior } } - err := editContainerdConfig(srcFilePath, runtimeFilePath, action) + err := editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action) if err != nil { hwlog.RunLog.Errorf("failed to edit containerd config, err: %v", err) return err, behavior @@ -98,21 +98,128 @@ func isCgroupV2() (bool, error) { return true, nil } -func editContainerdConfig(srcFilePath, runtimeFilePath, action string) error { +func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action string) error { cfg := config.Config{} if err := config.LoadConfig(srcFilePath, &cfg); err != nil { - log.Fatalf("Failed to load configuration file: %v", err) + hwlog.RunLog.Errorf("failed to load configuration file: %v", err) + return err } - if isCgroupV2() { + isV2, err := isCgroupV2() + if err != nil { + hwlog.RunLog.Errorf("failed to check whether cgroup v2: %v", err) + return err + } + runtimeValue := defaultRuntimeValue + if action == addCommand { + runtimeValue = runtimeFilePath + } + if isV2 { // TODO hwlog.RunLog.Infof("it is cgroup v2") } else { - hwlog.RunLog.Infof("it is cgroup v2") - if value, ok := cfg.Plugins["io.containerd.runtime.v1.linux"]; ok { - hwlog.RunLog.Infof("value: %+v", value) - } else { - hwlog.RunLog.Infof(" not ok, plugins: %+v", cfg.Plugins) + hwlog.RunLog.Infof("it is cgroup v1") + err = changeCgroupV1Config(&cfg, runtimeValue) + if err != nil { + hwlog.RunLog.Errorf("failed to change cgroup v1 config, error: %v", err) + return err + } + } + err = writeContainerdConfigToFile(cfg, destFilePath) + if err != nil { + hwlog.RunLog.Errorf("failed to write configuration file: %v", err) + return err + } + return nil +} + +func changeCgroupV1Config(cfg *config.Config, runtimeValue string) error { + err := changeCgroupV1RuntimeConfig(cfg, runtimeValue) + if err != nil { + hwlog.RunLog.Errorf("failed to change cgroup V1 runtime config, error: %v", err) + return err + } + return changeCgroupV1RuntimeTypeConfig(cfg) +} + +func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error { + if value, ok := cfg.Plugins[v1RuntimeType]; ok { + valueMap := value.ToMap() + valueMap[v1NeedChangeKeyRuntime] = runtimeValue + newTree, err := toml.TreeFromMap(valueMap) + if err != nil { + hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + return err } + cfg.Plugins[v1RuntimeType] = *newTree + } else { + hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeType, cfg.Plugins) + return fmt.Errorf("can not find plugin: %v", v1RuntimeType) + } + return nil +} + +func changeCgroupV1RuntimeTypeConfig(cfg *config.Config) error { + value, ok := cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] + if !ok { + hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) + return fmt.Errorf("can not find plugin: %v", v1RuntimeTypeFisrtLevelPlugin) + } + valueMap := value.ToMap() + containerdConfig := valueMap[containerdKey] + runtimesConfig, err := getMap(containerdConfig, runtimesKey) + if err != nil { + return err + } + runcConfig, err := getMap(runtimesConfig, runcKey) + if err != nil { + return err + } + runcConfigMap, ok := runcConfig.(map[string]interface{}) + if !ok { + hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", runcKey, runcConfig) + return fmt.Errorf("can not convert config: %v", runcKey) + } + runcConfigMap[v1NeedChangeKeyRuntimeType] = v1RuntimeType + //valueMap[containerdKey].(map[string]interface{})[runtimesKey].(map[string]interface{})[runcKey] = runcConfigMap + newTree, err := toml.TreeFromMap(valueMap) + if err != nil { + hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + return err + } + cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] = *newTree + return nil +} + +func getMap(input interface{}, key string) (interface{}, error) { + inputMap, ok := input.(map[string]interface{}) + if !ok { + hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", key, input) + return nil, fmt.Errorf("can not convert config: %v", key) + } + output, ok := inputMap[key] + if !ok { + hwlog.RunLog.Errorf("can not find config %v, config is: %+v", key, inputMap) + return nil, fmt.Errorf("can not find config: %v", key) + } + return output, nil +} + +func writeContainerdConfigToFile(cfg config.Config, destFilePath string) error { + tomlString, err := toml.Marshal(cfg) + if err != nil { + hwlog.RunLog.Errorf("failed to marshall to toml, error: %v", err) + return err + } + file, err := os.Create(destFilePath) + if err != nil { + hwlog.RunLog.Errorf("failed to create file, error: %v", err) + return err + } + defer file.Close() + _, err = file.Write(tomlString) + if err != nil { + hwlog.RunLog.Errorf("failed to write, error: %v", err) + return err } return nil } -- Gitee From 0b6e59fb209f76697009ffef3244c509c448fd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 11:54:42 +0800 Subject: [PATCH 03/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E5=A2=9E=E5=8A=A0containerd?= =?UTF-8?q?=20cgroup=20v1=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runtime/main.go | 676 +++++++++++++++++- .../{process/process_test.go => main_test.go} | 12 +- runtime/process/process.go | 676 ------------------ 3 files changed, 676 insertions(+), 688 deletions(-) rename runtime/{process/process_test.go => main_test.go} (99%) delete mode 100644 runtime/process/process.go diff --git a/runtime/main.go b/runtime/main.go index a495651..f3dc0e7 100644 --- a/runtime/main.go +++ b/runtime/main.go @@ -1,16 +1,680 @@ +/* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +// Package main package main import ( - "ascend-docker-runtime/mindxcheckutils" - "ascend-docker-runtime/runtime/process" "context" + "encoding/json" "fmt" - "huawei.com/npu-exporter/v5/common-utils/hwlog" + "io/ioutil" "log" "os" + "os/exec" + "path" + "path/filepath" + "regexp" + "sort" + "strconv" "strings" + "syscall" + + "github.com/containerd/containerd/oci" + "github.com/opencontainers/runtime-spec/specs-go" + "huawei.com/npu-exporter/v5/common-utils/hwlog" + + "ascend-docker-runtime/mindxcheckutils" + "ascend-docker-runtime/runtime/dcmi" ) +const ( + runLogPath = "/var/log/ascend-docker-runtime/runtime-run.log" + hookDefaultFilePath = "/usr/local/bin/ascend-docker-hook" + + maxCommandLength = 65535 + hookCli = "ascend-docker-hook" + destroyHookCli = "ascend-docker-destroy" + dockerRuncFile = "docker-runc" + runcFile = "runc" + envLength = 2 + kvPairSize = 2 + borderNum = 2 + + // ENV for device-plugin to identify ascend-docker-runtime + useAscendDocker = "ASCEND_DOCKER_RUNTIME=True" + devicePlugin = "ascend-device-plugin" + ascendVisibleDevices = "ASCEND_VISIBLE_DEVICES" + ascendRuntimeOptions = "ASCEND_RUNTIME_OPTIONS" + + // void indicates that the NPU card does not need to be mounted + void = "void" +) + +var ( + hookCliPath = hookCli + hookDefaultFile = hookDefaultFilePath + dockerRuncName = dockerRuncFile + runcName = runcFile +) + +const ( + // Atlas200ISoc Product name + Atlas200ISoc = "Atlas 200I SoC A1" + // Atlas200 Product name + Atlas200 = "Atlas 200 Model 3000" + // Ascend310 ascend 310 chip + Ascend310 = "Ascend310" + // Ascend310P ascend 310P chip + Ascend310P = "Ascend310P" + // Ascend310B ascend 310B chip + Ascend310B = "Ascend310B" + // Ascend910 ascend 910 chip + Ascend910 = "Ascend910" + ascend = "Ascend" + + devicePath = "/dev/" + davinciName = "davinci" + virtualDavinciName = "vdavinci" + davinciManager = "davinci_manager" + davinciManagerDocker = "davinci_manager_docker" + notRenameDeviceType = "" + devmmSvm = "devmm_svm" + hisiHdc = "hisi_hdc" + svm0 = "svm0" + tsAisle = "ts_aisle" + upgrade = "upgrade" + sys = "sys" + vdec = "vdec" + vpc = "vpc" + pngd = "pngd" + venc = "venc" + dvppCmdList = "dvpp_cmdlist" + logDrv = "log_drv" + acodec = "acodec" + ai = "ai" + ao = "ao" + vo = "vo" + hdmi = "hdmi" +) + +type args struct { + bundleDirPath string + cmd string +} + +// GetDeviceTypeByChipName get device type by chipName +func GetDeviceTypeByChipName(chipName string) string { + if strings.Contains(chipName, "310B") { + return Ascend310B + } + if strings.Contains(chipName, "310P") { + return Ascend310P + } + if strings.Contains(chipName, "310") { + return Ascend310 + } + if strings.Contains(chipName, "910") { + return Ascend910 + } + return "" +} + +func getArgs() (*args, error) { + args := &args{} + + for i, param := range os.Args { + if param == "--bundle" || param == "-b" { + if len(os.Args)-i <= 1 { + return nil, fmt.Errorf("bundle option needs an argument") + } + args.bundleDirPath = os.Args[i+1] + } else if param == "create" { + args.cmd = param + } + } + + return args, nil +} + +func initLogModule(ctx context.Context) error { + const backups = 2 + const logMaxAge = 365 + const fileMaxSize = 2 + runLogConfig := hwlog.LogConfig{ + LogFileName: runLogPath, + LogLevel: 0, + MaxBackups: backups, + MaxAge: logMaxAge, + OnlyToFile: true, + FileMaxSize: fileMaxSize, + } + if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { + fmt.Printf("hwlog init failed, error is %v", err) + return err + } + return nil +} + +var execRunc = func() error { + tempRuncPath, err := exec.LookPath(dockerRuncName) + if err != nil { + tempRuncPath, err = exec.LookPath(runcName) + if err != nil { + return fmt.Errorf("failed to find the path of runc: %v", err) + } + } + runcPath, err := filepath.EvalSymlinks(tempRuncPath) + if err != nil { + return fmt.Errorf("failed to find realpath of runc %v", err) + } + if _, err := mindxcheckutils.RealFileChecker(runcPath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err + } + + if err := mindxcheckutils.ChangeRuntimeLogMode("runtime-run-"); err != nil { + return err + } + if err = syscall.Exec(runcPath, append([]string{runcPath}, os.Args[1:]...), os.Environ()); err != nil { + return fmt.Errorf("failed to exec runc: %v", err) + } + + return nil +} + +func addAscendDockerEnv(spec *specs.Spec) { + if spec == nil || spec.Process == nil || spec.Process.Env == nil { + return + } + spec.Process.Env = append(spec.Process.Env, useAscendDocker) +} + +func addHook(spec *specs.Spec, deviceIdList *[]int) error { + if deviceIdList == nil { + return nil + } + currentExecPath, err := os.Executable() + if err != nil { + return fmt.Errorf("cannot get the path of ascend-docker-runtime: %v", err) + } + + hookCliPath = path.Join(path.Dir(currentExecPath), hookCli) + if _, err := mindxcheckutils.RealFileChecker(hookCliPath, true, false, mindxcheckutils.DefaultSize); err != nil { + return err + } + if _, err = os.Stat(hookCliPath); err != nil { + return fmt.Errorf("cannot find ascend-docker-hook executable file at %s: %v", hookCliPath, err) + } + + if spec.Hooks == nil { + spec.Hooks = &specs.Hooks{} + } + + needUpdate := true + if len(spec.Hooks.Prestart) > maxCommandLength { + return fmt.Errorf("too many items in Prestart ") + } + for _, hook := range spec.Hooks.Prestart { + if strings.Contains(hook.Path, hookCli) { + needUpdate = false + break + } + } + if needUpdate { + spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{ + Path: hookCliPath, + Args: []string{hookCliPath}, + }) + } + + if len(spec.Process.Env) > maxCommandLength { + return fmt.Errorf("too many items in Env ") + } + + if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { + return nil + } + + vdevice, err := dcmi.CreateVDevice(&dcmi.NpuWorker{}, spec, *deviceIdList) + if err != nil { + return err + } + hwlog.RunLog.Infof("vnpu split done: vdevice: %v", vdevice.VdeviceID) + + if vdevice.VdeviceID != -1 { + updateEnvAndPostHook(spec, vdevice, deviceIdList) + } + + return nil +} + +func removeDuplication(devices []int) []int { + list := make([]int, 0, len(devices)) + prev := -1 + + for _, device := range devices { + if device == prev { + continue + } + + list = append(list, device) + prev = device + } + + return list +} + +func parseDevices(visibleDevices string) ([]int, error) { + devices := make([]int, 0) + const maxDevice = 128 + + for _, d := range strings.Split(visibleDevices, ",") { + d = strings.TrimSpace(d) + if strings.Contains(d, "-") { + borders := strings.Split(d, "-") + if len(borders) != borderNum { + return nil, fmt.Errorf("invalid device range: %s", d) + } + + borders[0] = strings.TrimSpace(borders[0]) + borders[1] = strings.TrimSpace(borders[1]) + + left, err := strconv.Atoi(borders[0]) + if err != nil || left < 0 { + return nil, fmt.Errorf("invalid left boarder range parameter: %s", borders[0]) + } + + right, err := strconv.Atoi(borders[1]) + if err != nil || right > maxDevice { + return nil, fmt.Errorf("invalid right boarder range parameter: %s", borders[1]) + } + + if left > right { + return nil, fmt.Errorf("left boarder (%d) should not be larger than the right one(%d)", left, right) + } + + for n := left; n <= right; n++ { + devices = append(devices, n) + } + } else { + n, err := strconv.Atoi(d) + if err != nil { + return nil, fmt.Errorf("invalid single device parameter: %s", d) + } + + devices = append(devices, n) + } + } + + sort.Slice(devices, func(i, j int) bool { return i < j }) + return removeDuplication(devices), nil +} + +func parseAscendDevices(visibleDevices string) ([]int, error) { + devicesList := strings.Split(visibleDevices, ",") + devices := make([]int, 0, len(devicesList)) + chipType := "" + + for _, d := range devicesList { + matchGroups := regexp.MustCompile(`^Ascend(910|310|310B|310P)-(\d+)$`).FindStringSubmatch(strings.TrimSpace(d)) + if matchGroups == nil { + return nil, fmt.Errorf("invalid device format: %s", d) + } + n, err := strconv.Atoi(matchGroups[2]) + if err != nil { + return nil, fmt.Errorf("invalid device id: %s", d) + } + + if chipType == "" { + chipType = matchGroups[1] + } + if chipType != "" && chipType != matchGroups[1] { + return nil, fmt.Errorf("invalid device chip type: %s", d) + } + + devices = append(devices, n) + + } + chipName, err := dcmi.GetChipName() + if err != nil { + return nil, fmt.Errorf("get chip name error: %v", err) + } + if ascend+chipType != GetDeviceTypeByChipName(chipName) { + return nil, fmt.Errorf("chip type not match really: %s", chipType) + } + + sort.Slice(devices, func(i, j int) bool { return i < j }) + return removeDuplication(devices), nil +} + +func getValueByKey(data []string, name string) string { + for _, envLine := range data { + words := strings.SplitN(envLine, "=", kvPairSize) + if len(words) != kvPairSize { + hwlog.RunLog.Error("environment error") + return "" + } + + if words[0] == name { + return words[1] + } + } + + return "" +} + +func getValueByDeviceKey(data []string) string { + res := "" + for i := len(data) - 1; i >= 0; i-- { + words := strings.SplitN(data[i], "=", kvPairSize) + if len(words) != kvPairSize { + hwlog.RunLog.Error("environment error") + return "" + } + + if words[0] == ascendVisibleDevices { + res = words[1] + break + } + } + if res == "" { + hwlog.RunLog.Error("ASCEND_VISIBLE_DEVICES env variable is empty, will not mount any ascend device") + } + return res +} + +func addDeviceToSpec(spec *specs.Spec, dPath string, deviceType string) error { + device, err := oci.DeviceFromPath(dPath) + if err != nil { + return fmt.Errorf("failed to get %s info : %#v", dPath, err) + } + + switch deviceType { + case virtualDavinciName: + vDeviceNumber := regexp.MustCompile("[0-9]+").FindAllString(dPath, -1) + if len(vDeviceNumber) != 1 { + return fmt.Errorf("invalid vdavinci path: %s", dPath) + } + device.Path = devicePath + davinciName + vDeviceNumber[0] + case davinciManagerDocker: + device.Path = devicePath + davinciManager + default: // do nothing + + } + + spec.Linux.Devices = append(spec.Linux.Devices, *device) + newDeviceCgroup := specs.LinuxDeviceCgroup{ + Allow: true, + Type: device.Type, + Major: &device.Major, + Minor: &device.Minor, + Access: "rwm", + } + spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, newDeviceCgroup) + return nil +} + +func addAscend310BManagerDevice(spec *specs.Spec) error { + var Ascend310BManageDevices = []string{ + svm0, + tsAisle, + upgrade, + sys, + vdec, + vpc, + pngd, + venc, + dvppCmdList, + logDrv, + acodec, + ai, + ao, + vo, + hdmi, + } + + for _, device := range Ascend310BManageDevices { + dPath := devicePath + device + if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { + hwlog.RunLog.Warnf("failed to add %s to spec : %#v", dPath, err) + } + } + + davinciManagerPath := devicePath + davinciManagerDocker + if _, err := os.Stat(davinciManagerPath); err != nil { + hwlog.RunLog.Warnf("failed to get davinci manager docker, err: %#v", err) + davinciManagerPath = devicePath + davinciManager + if _, err := os.Stat(davinciManagerPath); err != nil { + return fmt.Errorf("failed to get davinci manager, err: %#v", err) + } + } + return addDeviceToSpec(spec, davinciManagerPath, davinciManagerDocker) +} + +func addCommonManagerDevice(spec *specs.Spec) error { + var commonManagerDevices = []string{ + devmmSvm, + hisiHdc, + } + + for _, device := range commonManagerDevices { + dPath := devicePath + device + if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { + return fmt.Errorf("failed to add common manage device to spec : %#v", err) + } + } + + return nil +} + +func addManagerDevice(spec *specs.Spec) error { + chipName, err := dcmi.GetChipName() + if err != nil { + return fmt.Errorf("get chip name error: %#v", err) + } + devType := GetDeviceTypeByChipName(chipName) + hwlog.RunLog.Infof("device type is: %s", devType) + if devType == Ascend310B { + return addAscend310BManagerDevice(spec) + } + + if err := addDeviceToSpec(spec, devicePath+davinciManager, notRenameDeviceType); err != nil { + return fmt.Errorf("add davinci_manager to spec error: %#v", err) + } + + productType, err := dcmi.GetProductType(&dcmi.NpuWorker{}) + if err != nil { + return fmt.Errorf("parse product type error: %#v", err) + } + hwlog.RunLog.Infof("product type is %s", productType) + + switch productType { + // do nothing + case Atlas200ISoc, Atlas200: + default: + if err = addCommonManagerDevice(spec); err != nil { + return fmt.Errorf("add common manage device error: %#v", err) + } + } + + return nil +} + +func checkVisibleDevice(spec *specs.Spec) ([]int, error) { + visibleDevices := getValueByDeviceKey(spec.Process.Env) + if visibleDevices == "" || visibleDevices == void { + return nil, nil + } + + if strings.Contains(visibleDevices, ascend) { + devices, err := parseAscendDevices(visibleDevices) + if err != nil { + return nil, fmt.Errorf("failed to parse ascend device : %v", err) + } + hwlog.RunLog.Infof("ascend devices is: %v", devices) + return devices, err + } + devices, err := parseDevices(visibleDevices) + if err != nil { + return nil, fmt.Errorf("failed to parse device : %v", err) + } + hwlog.RunLog.Infof("devices is: %v", devices) + return devices, err +} + +func addDevice(spec *specs.Spec, deviceIdList []int) error { + deviceName := davinciName + if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { + deviceName = virtualDavinciName + } + for _, deviceId := range deviceIdList { + dPath := devicePath + deviceName + strconv.Itoa(deviceId) + if err := addDeviceToSpec(spec, dPath, deviceName); err != nil { + return fmt.Errorf("failed to add davinci device to spec: %v", err) + } + } + + if err := addManagerDevice(spec); err != nil { + return fmt.Errorf("failed to add Manager device to spec: %v", err) + } + + return nil +} + +func updateEnvAndPostHook(spec *specs.Spec, vdevice dcmi.VDeviceInfo, deviceIdList *[]int) { + if deviceIdList == nil { + return + } + newEnv := make([]string, 0, len(spec.Process.Env)+1) + needAddVirtualFlag := true + *deviceIdList = []int{int(vdevice.VdeviceID)} + for _, line := range spec.Process.Env { + words := strings.Split(line, "=") + if len(words) == envLength && strings.TrimSpace(words[0]) == ascendRuntimeOptions { + needAddVirtualFlag = false + if strings.Contains(words[1], "VIRTUAL") { + newEnv = append(newEnv, line) + continue + } else { + newEnv = append(newEnv, strings.TrimSpace(line)+",VIRTUAL") + continue + } + } + newEnv = append(newEnv, line) + } + if needAddVirtualFlag { + newEnv = append(newEnv, fmt.Sprintf("ASCEND_RUNTIME_OPTIONS=VIRTUAL")) + } + spec.Process.Env = newEnv + if currentExecPath, err := os.Executable(); err == nil { + postHookCliPath := path.Join(path.Dir(currentExecPath), destroyHookCli) + spec.Hooks.Poststop = append(spec.Hooks.Poststop, specs.Hook{ + Path: postHookCliPath, + Args: []string{postHookCliPath, fmt.Sprintf("%d", vdevice.CardID), fmt.Sprintf("%d", vdevice.DeviceID), + fmt.Sprintf("%d", vdevice.VdeviceID)}, + }) + } +} + +func modifySpecFile(path string) error { + stat, err := os.Stat(path) + if err != nil { + return fmt.Errorf("spec file doesnt exist %s: %v", path, err) + } + if _, err = mindxcheckutils.RealFileChecker(path, true, true, mindxcheckutils.DefaultSize); err != nil { + return err + } + + jsonFile, err := os.OpenFile(path, os.O_RDWR, stat.Mode()) + if err != nil { + return fmt.Errorf("cannot open oci spec file %s: %v", path, err) + } + + defer jsonFile.Close() + + jsonContent, err := ioutil.ReadAll(jsonFile) + if err != nil { + return fmt.Errorf("failed to read oci spec file %s: %v", path, err) + } + + if err = jsonFile.Truncate(0); err != nil { + return fmt.Errorf("failed to truncate: %v", err) + } + if _, err = jsonFile.Seek(0, 0); err != nil { + return fmt.Errorf("failed to seek: %v", err) + } + + var spec specs.Spec + if err = json.Unmarshal(jsonContent, &spec); err != nil { + return fmt.Errorf("failed to unmarshal oci spec file %s: %v", path, err) + } + + devices, err := checkVisibleDevice(&spec) + if err != nil { + hwlog.RunLog.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) + return fmt.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) + } + if len(devices) != 0 { + if err = addHook(&spec, &devices); err != nil { + hwlog.RunLog.Errorf("failed to inject hook, err: %v", err) + return fmt.Errorf("failed to inject hook, err: %v", err) + } + if err = addDevice(&spec, devices); err != nil { + return fmt.Errorf("failed to add device to env: %v", err) + } + } + + addAscendDockerEnv(&spec) + + jsonOutput, err := json.Marshal(spec) + if err != nil { + return fmt.Errorf("failed to marshal OCI spec file: %v", err) + } + + if _, err = jsonFile.WriteAt(jsonOutput, 0); err != nil { + return fmt.Errorf("failed to write OCI spec file: %v", err) + } + + return nil +} + +func doProcess() error { + args, err := getArgs() + if err != nil { + return fmt.Errorf("failed to get args: %v", err) + } + + if args.cmd != "create" { + return execRunc() + } + + if args.bundleDirPath == "" { + args.bundleDirPath, err = os.Getwd() + if err != nil { + return fmt.Errorf("failed to get current working dir: %v", err) + } + } + + specFilePath := args.bundleDirPath + "/config.json" + + if err = modifySpecFile(specFilePath); err != nil { + return fmt.Errorf("failed to modify spec file %s: %v", specFilePath, err) + } + + return execRunc() +} + func main() { defer func() { if err := recover(); err != nil { @@ -18,7 +682,7 @@ func main() { } }() ctx, _ := context.WithCancel(context.Background()) - if err := process.InitLogModule(ctx); err != nil { + if err := initLogModule(ctx); err != nil { log.Fatal(err) } logPrefixWords, err := mindxcheckutils.GetLogPrefix() @@ -31,11 +695,11 @@ func main() { } }() if !mindxcheckutils.StringChecker(strings.Join(os.Args, " "), 0, - process.MaxCommandLength, mindxcheckutils.DefaultWhiteList+" ") { + maxCommandLength, mindxcheckutils.DefaultWhiteList+" ") { hwlog.RunLog.Errorf("%v ascend docker runtime args check failed", logPrefixWords) log.Fatal("command error") } - if err = process.DoProcess(); err != nil { + if err = doProcess(); err != nil { hwlog.RunLog.Errorf("%v docker runtime failed: %v", logPrefixWords, err) log.Fatal(err) } diff --git a/runtime/process/process_test.go b/runtime/main_test.go similarity index 99% rename from runtime/process/process_test.go rename to runtime/main_test.go index d3aa876..6721a4b 100644 --- a/runtime/process/process_test.go +++ b/runtime/main_test.go @@ -13,7 +13,7 @@ */ // Package main -package process +package main import ( "context" @@ -57,7 +57,7 @@ func TestArgsIsCreate(t *testing.T) { return nil }) - err := DoProcess() + err := doProcess() assert.NotNil(t, err) } @@ -73,7 +73,7 @@ func TestArgsIsCreateCase1(t *testing.T) { return nil }) - err := DoProcess() + err := doProcess() assert.NotNil(t, err) } @@ -89,7 +89,7 @@ func TestArgsIsCreateCase2(t *testing.T) { return nil }) - err := DoProcess() + err := doProcess() assert.NotNil(t, err) } @@ -117,7 +117,7 @@ func TestArgsIsCreateCase3(t *testing.T) { return nil }) - err = DoProcess() + err = doProcess() assert.NotNil(t, err) } @@ -145,7 +145,7 @@ func TestArgsIsCreateCase4(t *testing.T) { return nil }) - err = DoProcess() + err = doProcess() assert.Nil(t, err) } diff --git a/runtime/process/process.go b/runtime/process/process.go deleted file mode 100644 index 9755d45..0000000 --- a/runtime/process/process.go +++ /dev/null @@ -1,676 +0,0 @@ -/* Copyright(C) 2022. Huawei Technologies Co.,Ltd. All rights reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -// Package process -package process - -import ( - "context" - "encoding/json" - "fmt" - "io/ioutil" - "os" - "os/exec" - "path" - "path/filepath" - "regexp" - "sort" - "strconv" - "strings" - "syscall" - - "github.com/containerd/containerd/oci" - "github.com/opencontainers/runtime-spec/specs-go" - "huawei.com/npu-exporter/v5/common-utils/hwlog" - - "ascend-docker-runtime/mindxcheckutils" - "ascend-docker-runtime/runtime/dcmi" -) - -const ( - runLogPath = "/var/log/ascend-docker-runtime/runtime-run.log" - hookDefaultFilePath = "/usr/local/bin/ascend-docker-hook" - // MaxCommandLength is the max length of command - MaxCommandLength = 65535 - hookCli = "ascend-docker-hook" - destroyHookCli = "ascend-docker-destroy" - dockerRuncFile = "docker-runc" - runcFile = "runc" - envLength = 2 - kvPairSize = 2 - borderNum = 2 - - // ENV for device-plugin to identify ascend-docker-runtime - useAscendDocker = "ASCEND_DOCKER_RUNTIME=True" - devicePlugin = "ascend-device-plugin" - ascendVisibleDevices = "ASCEND_VISIBLE_DEVICES" - ascendRuntimeOptions = "ASCEND_RUNTIME_OPTIONS" - - // void indicates that the NPU card does not need to be mounted - void = "void" -) - -var ( - hookCliPath = hookCli - hookDefaultFile = hookDefaultFilePath - dockerRuncName = dockerRuncFile - runcName = runcFile -) - -const ( - // Atlas200ISoc Product name - Atlas200ISoc = "Atlas 200I SoC A1" - // Atlas200 Product name - Atlas200 = "Atlas 200 Model 3000" - // Ascend310 ascend 310 chip - Ascend310 = "Ascend310" - // Ascend310P ascend 310P chip - Ascend310P = "Ascend310P" - // Ascend310B ascend 310B chip - Ascend310B = "Ascend310B" - // Ascend910 ascend 910 chip - Ascend910 = "Ascend910" - ascend = "Ascend" - - devicePath = "/dev/" - davinciName = "davinci" - virtualDavinciName = "vdavinci" - davinciManager = "davinci_manager" - davinciManagerDocker = "davinci_manager_docker" - notRenameDeviceType = "" - devmmSvm = "devmm_svm" - hisiHdc = "hisi_hdc" - svm0 = "svm0" - tsAisle = "ts_aisle" - upgrade = "upgrade" - sys = "sys" - vdec = "vdec" - vpc = "vpc" - pngd = "pngd" - venc = "venc" - dvppCmdList = "dvpp_cmdlist" - logDrv = "log_drv" - acodec = "acodec" - ai = "ai" - ao = "ao" - vo = "vo" - hdmi = "hdmi" -) - -type args struct { - bundleDirPath string - cmd string -} - -// GetDeviceTypeByChipName get device type by chipName -func GetDeviceTypeByChipName(chipName string) string { - if strings.Contains(chipName, "310B") { - return Ascend310B - } - if strings.Contains(chipName, "310P") { - return Ascend310P - } - if strings.Contains(chipName, "310") { - return Ascend310 - } - if strings.Contains(chipName, "910") { - return Ascend910 - } - return "" -} - -// InitLogModule inits the log info -func InitLogModule(ctx context.Context) error { - const backups = 2 - const logMaxAge = 365 - const fileMaxSize = 2 - runLogConfig := hwlog.LogConfig{ - LogFileName: runLogPath, - LogLevel: 0, - MaxBackups: backups, - MaxAge: logMaxAge, - OnlyToFile: true, - FileMaxSize: fileMaxSize, - } - if err := hwlog.InitRunLogger(&runLogConfig, ctx); err != nil { - fmt.Printf("hwlog init failed, error is %v", err) - return err - } - return nil -} - -func getArgs() (*args, error) { - args := &args{} - - for i, param := range os.Args { - if param == "--bundle" || param == "-b" { - if len(os.Args)-i <= 1 { - return nil, fmt.Errorf("bundle option needs an argument") - } - args.bundleDirPath = os.Args[i+1] - } else if param == "create" { - args.cmd = param - } - } - - return args, nil -} - -var execRunc = func() error { - tempRuncPath, err := exec.LookPath(dockerRuncName) - if err != nil { - tempRuncPath, err = exec.LookPath(runcName) - if err != nil { - return fmt.Errorf("failed to find the path of runc: %v", err) - } - } - runcPath, err := filepath.EvalSymlinks(tempRuncPath) - if err != nil { - return fmt.Errorf("failed to find realpath of runc %v", err) - } - if _, err := mindxcheckutils.RealFileChecker(runcPath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err - } - - if err := mindxcheckutils.ChangeRuntimeLogMode("runtime-run-"); err != nil { - return err - } - if err = syscall.Exec(runcPath, append([]string{runcPath}, os.Args[1:]...), os.Environ()); err != nil { - return fmt.Errorf("failed to exec runc: %v", err) - } - - return nil -} - -func addAscendDockerEnv(spec *specs.Spec) { - if spec == nil || spec.Process == nil || spec.Process.Env == nil { - return - } - spec.Process.Env = append(spec.Process.Env, useAscendDocker) -} - -func addHook(spec *specs.Spec, deviceIdList *[]int) error { - if deviceIdList == nil { - return nil - } - currentExecPath, err := os.Executable() - if err != nil { - return fmt.Errorf("cannot get the path of ascend-docker-runtime: %v", err) - } - - hookCliPath = path.Join(path.Dir(currentExecPath), hookCli) - if _, err := mindxcheckutils.RealFileChecker(hookCliPath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err - } - if _, err = os.Stat(hookCliPath); err != nil { - return fmt.Errorf("cannot find ascend-docker-hook executable file at %s: %v", hookCliPath, err) - } - - if spec.Hooks == nil { - spec.Hooks = &specs.Hooks{} - } - - needUpdate := true - if len(spec.Hooks.Prestart) > MaxCommandLength { - return fmt.Errorf("too many items in Prestart ") - } - for _, hook := range spec.Hooks.Prestart { - if strings.Contains(hook.Path, hookCli) { - needUpdate = false - break - } - } - if needUpdate { - spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{ - Path: hookCliPath, - Args: []string{hookCliPath}, - }) - } - - if len(spec.Process.Env) > MaxCommandLength { - return fmt.Errorf("too many items in Env ") - } - - if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { - return nil - } - - vdevice, err := dcmi.CreateVDevice(&dcmi.NpuWorker{}, spec, *deviceIdList) - if err != nil { - return err - } - hwlog.RunLog.Infof("vnpu split done: vdevice: %v", vdevice.VdeviceID) - - if vdevice.VdeviceID != -1 { - updateEnvAndPostHook(spec, vdevice, deviceIdList) - } - - return nil -} - -func removeDuplication(devices []int) []int { - list := make([]int, 0, len(devices)) - prev := -1 - - for _, device := range devices { - if device == prev { - continue - } - - list = append(list, device) - prev = device - } - - return list -} - -func parseDevices(visibleDevices string) ([]int, error) { - devices := make([]int, 0) - const maxDevice = 128 - - for _, d := range strings.Split(visibleDevices, ",") { - d = strings.TrimSpace(d) - if strings.Contains(d, "-") { - borders := strings.Split(d, "-") - if len(borders) != borderNum { - return nil, fmt.Errorf("invalid device range: %s", d) - } - - borders[0] = strings.TrimSpace(borders[0]) - borders[1] = strings.TrimSpace(borders[1]) - - left, err := strconv.Atoi(borders[0]) - if err != nil || left < 0 { - return nil, fmt.Errorf("invalid left boarder range parameter: %s", borders[0]) - } - - right, err := strconv.Atoi(borders[1]) - if err != nil || right > maxDevice { - return nil, fmt.Errorf("invalid right boarder range parameter: %s", borders[1]) - } - - if left > right { - return nil, fmt.Errorf("left boarder (%d) should not be larger than the right one(%d)", left, right) - } - - for n := left; n <= right; n++ { - devices = append(devices, n) - } - } else { - n, err := strconv.Atoi(d) - if err != nil { - return nil, fmt.Errorf("invalid single device parameter: %s", d) - } - - devices = append(devices, n) - } - } - - sort.Slice(devices, func(i, j int) bool { return i < j }) - return removeDuplication(devices), nil -} - -func parseAscendDevices(visibleDevices string) ([]int, error) { - devicesList := strings.Split(visibleDevices, ",") - devices := make([]int, 0, len(devicesList)) - chipType := "" - - for _, d := range devicesList { - matchGroups := regexp.MustCompile(`^Ascend(910|310|310B|310P)-(\d+)$`).FindStringSubmatch(strings.TrimSpace(d)) - if matchGroups == nil { - return nil, fmt.Errorf("invalid device format: %s", d) - } - n, err := strconv.Atoi(matchGroups[2]) - if err != nil { - return nil, fmt.Errorf("invalid device id: %s", d) - } - - if chipType == "" { - chipType = matchGroups[1] - } - if chipType != "" && chipType != matchGroups[1] { - return nil, fmt.Errorf("invalid device chip type: %s", d) - } - - devices = append(devices, n) - - } - chipName, err := dcmi.GetChipName() - if err != nil { - return nil, fmt.Errorf("get chip name error: %v", err) - } - if ascend+chipType != GetDeviceTypeByChipName(chipName) { - return nil, fmt.Errorf("chip type not match really: %s", chipType) - } - - sort.Slice(devices, func(i, j int) bool { return i < j }) - return removeDuplication(devices), nil -} - -func getValueByKey(data []string, name string) string { - for _, envLine := range data { - words := strings.SplitN(envLine, "=", kvPairSize) - if len(words) != kvPairSize { - hwlog.RunLog.Error("environment error") - return "" - } - - if words[0] == name { - return words[1] - } - } - - return "" -} - -func getValueByDeviceKey(data []string) string { - res := "" - for i := len(data) - 1; i >= 0; i-- { - words := strings.SplitN(data[i], "=", kvPairSize) - if len(words) != kvPairSize { - hwlog.RunLog.Error("environment error") - return "" - } - - if words[0] == ascendVisibleDevices { - res = words[1] - break - } - } - if res == "" { - hwlog.RunLog.Error("ASCEND_VISIBLE_DEVICES env variable is empty, will not mount any ascend device") - } - return res -} - -func addDeviceToSpec(spec *specs.Spec, dPath string, deviceType string) error { - device, err := oci.DeviceFromPath(dPath) - if err != nil { - return fmt.Errorf("failed to get %s info : %#v", dPath, err) - } - - switch deviceType { - case virtualDavinciName: - vDeviceNumber := regexp.MustCompile("[0-9]+").FindAllString(dPath, -1) - if len(vDeviceNumber) != 1 { - return fmt.Errorf("invalid vdavinci path: %s", dPath) - } - device.Path = devicePath + davinciName + vDeviceNumber[0] - case davinciManagerDocker: - device.Path = devicePath + davinciManager - default: // do nothing - - } - - spec.Linux.Devices = append(spec.Linux.Devices, *device) - newDeviceCgroup := specs.LinuxDeviceCgroup{ - Allow: true, - Type: device.Type, - Major: &device.Major, - Minor: &device.Minor, - Access: "rwm", - } - spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, newDeviceCgroup) - return nil -} - -func addAscend310BManagerDevice(spec *specs.Spec) error { - var Ascend310BManageDevices = []string{ - svm0, - tsAisle, - upgrade, - sys, - vdec, - vpc, - pngd, - venc, - dvppCmdList, - logDrv, - acodec, - ai, - ao, - vo, - hdmi, - } - - for _, device := range Ascend310BManageDevices { - dPath := devicePath + device - if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { - hwlog.RunLog.Warnf("failed to add %s to spec : %#v", dPath, err) - } - } - - davinciManagerPath := devicePath + davinciManagerDocker - if _, err := os.Stat(davinciManagerPath); err != nil { - hwlog.RunLog.Warnf("failed to get davinci manager docker, err: %#v", err) - davinciManagerPath = devicePath + davinciManager - if _, err := os.Stat(davinciManagerPath); err != nil { - return fmt.Errorf("failed to get davinci manager, err: %#v", err) - } - } - return addDeviceToSpec(spec, davinciManagerPath, davinciManagerDocker) -} - -func addCommonManagerDevice(spec *specs.Spec) error { - var commonManagerDevices = []string{ - devmmSvm, - hisiHdc, - } - - for _, device := range commonManagerDevices { - dPath := devicePath + device - if err := addDeviceToSpec(spec, dPath, notRenameDeviceType); err != nil { - return fmt.Errorf("failed to add common manage device to spec : %#v", err) - } - } - - return nil -} - -func addManagerDevice(spec *specs.Spec) error { - chipName, err := dcmi.GetChipName() - if err != nil { - return fmt.Errorf("get chip name error: %#v", err) - } - devType := GetDeviceTypeByChipName(chipName) - hwlog.RunLog.Infof("device type is: %s", devType) - if devType == Ascend310B { - return addAscend310BManagerDevice(spec) - } - - if err := addDeviceToSpec(spec, devicePath+davinciManager, notRenameDeviceType); err != nil { - return fmt.Errorf("add davinci_manager to spec error: %#v", err) - } - - productType, err := dcmi.GetProductType(&dcmi.NpuWorker{}) - if err != nil { - return fmt.Errorf("parse product type error: %#v", err) - } - hwlog.RunLog.Infof("product type is %s", productType) - - switch productType { - // do nothing - case Atlas200ISoc, Atlas200: - default: - if err = addCommonManagerDevice(spec); err != nil { - return fmt.Errorf("add common manage device error: %#v", err) - } - } - - return nil -} - -func checkVisibleDevice(spec *specs.Spec) ([]int, error) { - visibleDevices := getValueByDeviceKey(spec.Process.Env) - if visibleDevices == "" || visibleDevices == void { - return nil, nil - } - - if strings.Contains(visibleDevices, ascend) { - devices, err := parseAscendDevices(visibleDevices) - if err != nil { - return nil, fmt.Errorf("failed to parse ascend device : %v", err) - } - hwlog.RunLog.Infof("ascend devices is: %v", devices) - return devices, err - } - devices, err := parseDevices(visibleDevices) - if err != nil { - return nil, fmt.Errorf("failed to parse device : %v", err) - } - hwlog.RunLog.Infof("devices is: %v", devices) - return devices, err -} - -func addDevice(spec *specs.Spec, deviceIdList []int) error { - deviceName := davinciName - if strings.Contains(getValueByKey(spec.Process.Env, ascendRuntimeOptions), "VIRTUAL") { - deviceName = virtualDavinciName - } - for _, deviceId := range deviceIdList { - dPath := devicePath + deviceName + strconv.Itoa(deviceId) - if err := addDeviceToSpec(spec, dPath, deviceName); err != nil { - return fmt.Errorf("failed to add davinci device to spec: %v", err) - } - } - - if err := addManagerDevice(spec); err != nil { - return fmt.Errorf("failed to add Manager device to spec: %v", err) - } - - return nil -} - -func updateEnvAndPostHook(spec *specs.Spec, vdevice dcmi.VDeviceInfo, deviceIdList *[]int) { - if deviceIdList == nil { - return - } - newEnv := make([]string, 0, len(spec.Process.Env)+1) - needAddVirtualFlag := true - *deviceIdList = []int{int(vdevice.VdeviceID)} - for _, line := range spec.Process.Env { - words := strings.Split(line, "=") - if len(words) == envLength && strings.TrimSpace(words[0]) == ascendRuntimeOptions { - needAddVirtualFlag = false - if strings.Contains(words[1], "VIRTUAL") { - newEnv = append(newEnv, line) - continue - } else { - newEnv = append(newEnv, strings.TrimSpace(line)+",VIRTUAL") - continue - } - } - newEnv = append(newEnv, line) - } - if needAddVirtualFlag { - newEnv = append(newEnv, fmt.Sprintf("ASCEND_RUNTIME_OPTIONS=VIRTUAL")) - } - spec.Process.Env = newEnv - if currentExecPath, err := os.Executable(); err == nil { - postHookCliPath := path.Join(path.Dir(currentExecPath), destroyHookCli) - spec.Hooks.Poststop = append(spec.Hooks.Poststop, specs.Hook{ - Path: postHookCliPath, - Args: []string{postHookCliPath, fmt.Sprintf("%d", vdevice.CardID), fmt.Sprintf("%d", vdevice.DeviceID), - fmt.Sprintf("%d", vdevice.VdeviceID)}, - }) - } -} - -func modifySpecFile(path string) error { - stat, err := os.Stat(path) - if err != nil { - return fmt.Errorf("spec file doesnt exist %s: %v", path, err) - } - if _, err = mindxcheckutils.RealFileChecker(path, true, true, mindxcheckutils.DefaultSize); err != nil { - return err - } - - jsonFile, err := os.OpenFile(path, os.O_RDWR, stat.Mode()) - if err != nil { - return fmt.Errorf("cannot open oci spec file %s: %v", path, err) - } - - defer jsonFile.Close() - - jsonContent, err := ioutil.ReadAll(jsonFile) - if err != nil { - return fmt.Errorf("failed to read oci spec file %s: %v", path, err) - } - - if err = jsonFile.Truncate(0); err != nil { - return fmt.Errorf("failed to truncate: %v", err) - } - if _, err = jsonFile.Seek(0, 0); err != nil { - return fmt.Errorf("failed to seek: %v", err) - } - - var spec specs.Spec - if err = json.Unmarshal(jsonContent, &spec); err != nil { - return fmt.Errorf("failed to unmarshal oci spec file %s: %v", path, err) - } - - devices, err := checkVisibleDevice(&spec) - if err != nil { - hwlog.RunLog.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) - return fmt.Errorf("failed to check ASCEND_VISIBLE_DEVICES parameter, err: %v", err) - } - if len(devices) != 0 { - if err = addHook(&spec, &devices); err != nil { - hwlog.RunLog.Errorf("failed to inject hook, err: %v", err) - return fmt.Errorf("failed to inject hook, err: %v", err) - } - if err = addDevice(&spec, devices); err != nil { - return fmt.Errorf("failed to add device to env: %v", err) - } - } - - addAscendDockerEnv(&spec) - - jsonOutput, err := json.Marshal(spec) - if err != nil { - return fmt.Errorf("failed to marshal OCI spec file: %v", err) - } - - if _, err = jsonFile.WriteAt(jsonOutput, 0); err != nil { - return fmt.Errorf("failed to write OCI spec file: %v", err) - } - - return nil -} - -func DoProcess() error { - args, err := getArgs() - if err != nil { - return fmt.Errorf("failed to get args: %v", err) - } - - if args.cmd != "create" { - return execRunc() - } - - if args.bundleDirPath == "" { - args.bundleDirPath, err = os.Getwd() - if err != nil { - return fmt.Errorf("failed to get current working dir: %v", err) - } - } - - specFilePath := args.bundleDirPath + "/config.json" - - if err = modifySpecFile(specFilePath); err != nil { - return fmt.Errorf("failed to modify spec file %s: %v", specFilePath, err) - } - - return execRunc() -} -- Gitee From e4097ae283c4ae7a8f70b089246e4035971de98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 14:26:20 +0800 Subject: [PATCH 04/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/common.go | 14 +++++++ install/process/constant.go | 14 +++++++ install/process/containerd_process.go | 14 +++++++ install/process/docker_process.go | 54 ++++++++++++++++---------- install/process/docker_process_test.go | 49 +++++++++++++---------- 5 files changed, 103 insertions(+), 42 deletions(-) diff --git a/install/process/common.go b/install/process/common.go index 5ae3c7e..7e73dc7 100644 --- a/install/process/common.go +++ b/install/process/common.go @@ -1,3 +1,17 @@ +/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package process func checkParamAndGetBehavior(action string, command []string) (bool, string) { diff --git a/install/process/constant.go b/install/process/constant.go index 80904b6..ef7fda3 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -1,3 +1,17 @@ +/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package process const commonTemplate = `{ diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index b952b80..af27eb6 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -1,3 +1,17 @@ +/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package process import ( diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 70be119..5885b88 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -129,31 +129,43 @@ func modifyDaemon(srcFilePath, runtimeFilePath, action string) (map[string]inter return nil, fmt.Errorf("extract runtime failed") } if action == addCommand { - if _, ok := runtimeConfig["ascend"]; !ok { - runtimeConfig["ascend"] = map[string]interface{}{} - } - ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) - if !ok { - return nil, fmt.Errorf("extract ascend failed") - } - ascendConfig["path"] = runtimeFilePath - if _, ok := ascendConfig["runtimeArgs"]; !ok { - ascendConfig["runtimeArgs"] = []string{} - } - if !reserveDefaultRuntime { - daemon["default-runtime"] = "ascend" - } + runtimeConfig, daemon, err = addDockerDaemon(runtimeConfig, daemon, runtimeFilePath) } else if action == rmCommand { - if runtimeConfigOk { - delete(runtimeConfig, "ascend") - } - if value, ok := daemon["default-runtime"]; ok && value == "ascend" { - delete(daemon, "default-runtime") - } + runtimeConfig, daemon, err = rmDockerDaemon(runtimeConfig, daemon, runtimeConfigOk) } else { return nil, fmt.Errorf("param error") } - return daemon, nil + return daemon, err +} + +func addDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeFilePath string, +) (map[string]interface{}, map[string]interface{}, error) { + if _, ok := runtimeConfig["ascend"]; !ok { + runtimeConfig["ascend"] = map[string]interface{}{} + } + ascendConfig, ok := runtimeConfig["ascend"].(map[string]interface{}) + if !ok { + return nil, nil, fmt.Errorf("extract ascend failed") + } + ascendConfig["path"] = runtimeFilePath + if _, ok := ascendConfig["runtimeArgs"]; !ok { + ascendConfig["runtimeArgs"] = []string{} + } + if !reserveDefaultRuntime { + daemon["default-runtime"] = "ascend" + } + return runtimeConfig, daemon, nil +} + +func rmDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeConfigOk bool, +) (map[string]interface{}, map[string]interface{}, error) { + if runtimeConfigOk { + delete(runtimeConfig, "ascend") + } + if value, ok := daemon["default-runtime"]; ok && value == "ascend" { + delete(daemon, "default-runtime") + } + return runtimeConfig, daemon, nil } func loadOriginJson(srcFilePath string) (map[string]interface{}, error) { diff --git a/install/process/docker_process_test.go b/install/process/docker_process_test.go index 51e7fbb..2742b87 100644 --- a/install/process/docker_process_test.go +++ b/install/process/docker_process_test.go @@ -22,7 +22,8 @@ import ( "testing" ) -const oldString = `{ +const ( + oldString = `{ "runtimes": { "ascend": { "path": "/test/runtime", @@ -31,6 +32,12 @@ const oldString = `{ }, "default-runtime": "ascend" }` + defaultRuntime = `"default-runtime"` + oldJson = "old.json" + createOldFail = "create old failed %s" + updateFail = "update failed %s" + updateFailAndData = "update failed %s, %v" +) func jSONBytesEqual(a, b []byte) (bool, error) { var contentA, contentB interface{} @@ -56,16 +63,16 @@ func TestCreateJsonStringWholeNew(t *testing.T) { func TestCreateJsonStringUpdate(t *testing.T) { const perm = 0600 - if fid, err := os.OpenFile("old.json", os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm); err == nil { + if fid, err := os.OpenFile(oldJson, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm); err == nil { _, err = fid.Write([]byte(oldString)) closeErr := fid.Close() if err != nil || closeErr != nil { - t.Fatalf("create old failed %s", err) + t.Fatalf(createOldFail, err) } } - data, err := createJsonString("old.json", "/test/runtime1", "add") + data, err := createJsonString(oldJson, "/test/runtime1", "add") if err != nil { - t.Fatalf("update failed %s", err) + t.Fatalf(updateFail, err) } expectString := `{ "runtimes": { @@ -74,10 +81,10 @@ func TestCreateJsonStringUpdate(t *testing.T) { "runtimeArgs": [] } }, - "default-runtime": "ascend" - }` + ` + defaultRuntime + `: "ascend" +}` if eq, err := jSONBytesEqual([]byte(expectString), data); err != nil || !eq { - t.Fatalf("update failed %s, %v", err, string(data)) + t.Fatalf(updateFailAndData, err, string(data)) } } @@ -94,18 +101,18 @@ func TestCreateJsonStringUpdateWithOtherParam(t *testing.T) { "runtimeArgs": [1,2,3] } }, - "default-runtime": "runc" - }` + ` + defaultRuntime + `: "runc" +}` if fid, err := os.OpenFile("old.json", os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm); err == nil { _, err = fid.Write([]byte(oldStringWithParam)) closeErr := fid.Close() if err != nil || closeErr != nil { - t.Fatalf("create old failed %s", err) + t.Fatalf(createOldFail, err) } } - data, err := createJsonString("old.json", "/test/runtime1", "add") + data, err := createJsonString(oldJson, "/test/runtime1", "add") if err != nil { - t.Fatalf("update failed %s", err) + t.Fatalf(updateFail, err) } expectString := `{ "runtimes": { @@ -118,30 +125,30 @@ func TestCreateJsonStringUpdateWithOtherParam(t *testing.T) { "runtimeArgs": [1,2,3] } }, - "default-runtime": "ascend" - }` + ` + defaultRuntime + `: "ascend" +}` if eq, err := jSONBytesEqual([]byte(expectString), data); err != nil || !eq { - t.Fatalf("update failed %s, %v", err, string(data)) + t.Fatalf(updateFailAndData, err, string(data)) } } func TestCreateJsonStrinRm(t *testing.T) { const perm = 0600 - if fid, err := os.OpenFile("old.json", os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm); err == nil { + if fid, err := os.OpenFile(oldJson, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm); err == nil { _, err = fid.Write([]byte(oldString)) closeErr := fid.Close() if err != nil || closeErr != nil { - t.Fatalf("create old failed %s", err) + t.Fatalf(createOldFail, err) } } - data, err := createJsonString("old.json", "", "rm") + data, err := createJsonString(oldJson, "", "rm") if err != nil { - t.Fatalf("update failed %s", err) + t.Fatalf(updateFail, err) } expectString := `{ "runtimes": {} }` if eq, err := jSONBytesEqual([]byte(expectString), data); err != nil || !eq { - t.Fatalf("update failed %s, %v", err, string(data)) + t.Fatalf(updateFailAndData, err, string(data)) } } -- Gitee From c52ae38f6d5622a1b29ec79a231a8dfcbf83e8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 14:32:52 +0800 Subject: [PATCH 05/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 1 + install/process/docker_process.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/install/process/constant.go b/install/process/constant.go index ef7fda3..7a5902f 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -46,6 +46,7 @@ const ( ) const ( + defaultRuntimeKey = "default-runtime" // InstallSceneDocker is a 'docker' string of scene InstallSceneDocker = "docker" // InstallSceneContainerd is a 'containerd' string of scene diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 5885b88..fc85cde 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -152,7 +152,7 @@ func addDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeFilePa ascendConfig["runtimeArgs"] = []string{} } if !reserveDefaultRuntime { - daemon["default-runtime"] = "ascend" + daemon[defaultRuntimeKey] = "ascend" } return runtimeConfig, daemon, nil } @@ -162,8 +162,8 @@ func rmDockerDaemon(runtimeConfig, daemon map[string]interface{}, runtimeConfigO if runtimeConfigOk { delete(runtimeConfig, "ascend") } - if value, ok := daemon["default-runtime"]; ok && value == "ascend" { - delete(daemon, "default-runtime") + if value, ok := daemon[defaultRuntimeKey]; ok && value == "ascend" { + delete(daemon, defaultRuntimeKey) } return runtimeConfig, daemon, nil } -- Gitee From d31e1d11e5350883e989b685c970d7bcbfc504e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 4 Jul 2024 15:34:19 +0800 Subject: [PATCH 06/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91containerd=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 2 +- build/scripts/uninstall.sh | 8 +++++--- install/process/constant.go | 1 + install/process/containerd_process.go | 13 +++++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index dbc491b..cb40401 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -339,7 +339,7 @@ function uninstall() exit 1 fi - "${INSTALL_PATH}"/script/uninstall.sh ${ISULA} + "${INSTALL_PATH}"/script/uninstall.sh ${ISULA} ${INSTALL_SCENE} if [[ $? != 0 ]]; then log "[ERROR]" "uninstall failed, '${INSTALL_PATH}/script/uninstall.sh ${ISULA}' return non-zero" exit 1 diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index 433a9cf..249f623 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -123,7 +123,7 @@ fi ROOT=$(cd $(dirname $0); pwd)/.. RESERVEDEFAULT=no -if [ "$*" == "isula" ] ; then +if [ "$1" == "isula" ] ; then DST='/etc/isulad/daemon.json' echo "[INFO]: You will recover iSula's daemon" RESERVEDEFAULT=yes @@ -131,7 +131,10 @@ else DST='/etc/docker/daemon.json' echo "[INFO]: You will recover Docker's daemon" fi - +INSTALL_SCENE=$2 +if [ ${INSTALL_SCENE} == containerd ] ; then + DST='/etc/containerd/config.toml' +fi SRC="${DST}.${PPID}" if [ ! -f "${DST}" ]; then @@ -144,7 +147,6 @@ if [[ $? != 0 ]]; then log "[ERROR]" "uninstall failed, ${DST} is invalid" exit 1 fi - # exit when return code is not 0, if use 'set -e' ${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE} > /dev/null if [[ $? != 0 ]]; then diff --git a/install/process/constant.go b/install/process/constant.go index 7a5902f..cfb32f2 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -58,6 +58,7 @@ const ( v1NeedChangeKeyRuntime = "runtime" v1NeedChangeKeyRuntimeType = "runtime_type" v1RuntimeType = "io.containerd.runtime.v1.linux" + v1DefaultRuncRuntimeType = "io.containerd.runc.v2" defaultRuntimeValue = "runc" v1RuntimeTypeFisrtLevelPlugin = "io.containerd.grpc.v1.cri" containerdKey = "containerd" diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index af27eb6..1270bd2 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -124,15 +124,17 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action str return err } runtimeValue := defaultRuntimeValue + runtimeType := v1DefaultRuncRuntimeType if action == addCommand { runtimeValue = runtimeFilePath + runtimeType = v1RuntimeType } if isV2 { // TODO hwlog.RunLog.Infof("it is cgroup v2") } else { hwlog.RunLog.Infof("it is cgroup v1") - err = changeCgroupV1Config(&cfg, runtimeValue) + err = changeCgroupV1Config(&cfg, runtimeValue, runtimeType) if err != nil { hwlog.RunLog.Errorf("failed to change cgroup v1 config, error: %v", err) return err @@ -146,13 +148,13 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action str return nil } -func changeCgroupV1Config(cfg *config.Config, runtimeValue string) error { +func changeCgroupV1Config(cfg *config.Config, runtimeValue, runtimeType string) error { err := changeCgroupV1RuntimeConfig(cfg, runtimeValue) if err != nil { hwlog.RunLog.Errorf("failed to change cgroup V1 runtime config, error: %v", err) return err } - return changeCgroupV1RuntimeTypeConfig(cfg) + return changeCgroupV1RuntimeTypeConfig(cfg, runtimeType) } func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error { @@ -172,7 +174,7 @@ func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error return nil } -func changeCgroupV1RuntimeTypeConfig(cfg *config.Config) error { +func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) error { value, ok := cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] if !ok { hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) @@ -193,8 +195,7 @@ func changeCgroupV1RuntimeTypeConfig(cfg *config.Config) error { hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", runcKey, runcConfig) return fmt.Errorf("can not convert config: %v", runcKey) } - runcConfigMap[v1NeedChangeKeyRuntimeType] = v1RuntimeType - //valueMap[containerdKey].(map[string]interface{})[runtimesKey].(map[string]interface{})[runcKey] = runcConfigMap + runcConfigMap[v1NeedChangeKeyRuntimeType] = runtimeType newTree, err := toml.TreeFromMap(valueMap) if err != nil { hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) -- Gitee From e46b63884e46a067dd67ae5f6d93a0c14fec4727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 11:06:54 +0800 Subject: [PATCH 07/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91containerd=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=87=AA=E5=8A=A8=E8=AF=86=E5=88=ABcgroup?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=96=B9=E5=BC=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 5 ++- build/scripts/uninstall.sh | 5 ++- install/main.go | 9 +++-- install/process/constant.go | 10 ++--- install/process/containerd_process.go | 56 +++++---------------------- 5 files changed, 24 insertions(+), 61 deletions(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index cb40401..b73090a 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -307,10 +307,11 @@ function install() exit 1 fi + CGROUP_INFO=$(stat -fc %T /sys/fs/cgroup/) # exit when return code is not 0, if use 'set -e' - ./ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE} > /dev/null + ./ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE} ${CGROUP_INFO} > /dev/null if [[ $? != 0 ]]; then - log "[ERROR]" "install failed, './ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE}' return non-zero" + log "[ERROR]" "install failed, './ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE} ${CGROUP_INFO}' return non-zero" exit 1 fi mv -f ${SRC} ${DST} diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index 249f623..e0d9d76 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -147,10 +147,11 @@ if [[ $? != 0 ]]; then log "[ERROR]" "uninstall failed, ${DST} is invalid" exit 1 fi +CGROUP_INFO=$(stat -fc %T /sys/fs/cgroup/) # exit when return code is not 0, if use 'set -e' -${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE} > /dev/null +${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE} ${CGROUP_INFO} > /dev/null if [[ $? != 0 ]]; then - log "[ERROR]" "uninstall failed, '${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE}' return non-zero" + log "[ERROR]" "uninstall failed, '${ROOT}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} ${RESERVEDEFAULT} ${INSTALL_SCENE} ${CGROUP_INFO}' return non-zero" exit 1 fi diff --git a/install/main.go b/install/main.go index e4c7557..8b4f012 100644 --- a/install/main.go +++ b/install/main.go @@ -49,8 +49,9 @@ const noDefaultTemplate = `{ }` const ( - maxCommandLength = 65535 - logPath = "/var/log/ascend-docker-runtime/install-helper-run.log" + maxCommandLength = 65535 + logPath = "/var/log/ascend-docker-runtime/install-helper-run.log" + installSceneIndexFromEnd = 2 ) var reserveDefaultRuntime = false @@ -86,9 +87,9 @@ func main() { log.Fatalf("error param") } var behavior string - if command[len(command)-1] == process.InstallSceneDocker { + if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneDocker { err, behavior = process.DockerProcess(command) - } else if command[len(command)-1] == process.InstallSceneContainerd { + } else if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneContainerd { err, behavior = process.ContainerdProcess(command) } else { hwlog.RunLog.Errorf("error param: %v", command[len(command)-1]) diff --git a/install/process/constant.go b/install/process/constant.go index cfb32f2..882ae42 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -38,8 +38,8 @@ const ( srcFilePosition = 1 destFilePosition = 2 runtimeFilePosition = 3 - rmCommandLength = 5 - addCommandLength = 6 + rmCommandLength = 6 + addCommandLength = 7 addCommand = "add" rmCommand = "rm" maxFileSize = 1024 * 1024 * 10 @@ -51,10 +51,6 @@ const ( InstallSceneDocker = "docker" // InstallSceneContainerd is a 'containerd' string of scene InstallSceneContainerd = "containerd" - cgroupsFilePath = "/proc/cgroups" - hierarchyStr = "hierarchy" - initHierarchyCol = -1 - cgroupV2HierarchyValue = "2" v1NeedChangeKeyRuntime = "runtime" v1NeedChangeKeyRuntimeType = "runtime_type" v1RuntimeType = "io.containerd.runtime.v1.linux" @@ -64,4 +60,6 @@ const ( containerdKey = "containerd" runtimesKey = "runtimes" runcKey = "runc" + cgroupInfoIndexFromEnd = 1 + cgroupV2InfoStr = "cgroup2fs" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 1270bd2..cbddc15 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -15,7 +15,6 @@ package process import ( - "bufio" "fmt" "github.com/pelletier/go-toml" "os" @@ -58,7 +57,8 @@ func ContainerdProcess(command []string) (error, string) { return err, behavior } } - err := editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action) + cgroupInfo := command[len(command)-cgroupInfoIndexFromEnd] + err := editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cgroupInfo) if err != nil { hwlog.RunLog.Errorf("failed to edit containerd config, err: %v", err) return err, behavior @@ -66,59 +66,21 @@ func ContainerdProcess(command []string) (error, string) { return nil, behavior } -func isCgroupV2() (bool, error) { - if _, err := os.Stat(cgroupsFilePath); err != nil { - hwlog.RunLog.Errorf("file %v stat error: %v", err) - return false, err +func isCgroupV2(cgroupInfo string) (bool, error) { + hwlog.RunLog.Infof("cgroup info: %v", cgroupInfo) + if strings.Contains(cgroupInfo, cgroupV2InfoStr) { + return true, nil } - realPath, err := mindxcheckutils.RealFileChecker(cgroupsFilePath, false, true, mindxcheckutils.DefaultSize) - if err != nil { - hwlog.RunLog.Errorf("failed to check file real path, err: %v", err) - return false, err - } - file, err := os.Open(realPath) - if err != nil { - hwlog.RunLog.Errorf("failed to open %v: %v", realPath, err) - return false, err - } - defer func() { - closeErr := file.Close() - if closeErr != nil { - hwlog.RunLog.Errorf("failed to close file, err: %v", err) - } - }() - scanner := bufio.NewScanner(file) - hierarchyCol := initHierarchyCol - if scanner.Scan() { - line := scanner.Text() - parts := strings.Fields(line) - for index, part := range parts { - if strings.Contains(part, hierarchyStr) { - hierarchyCol = index - break - } - } - } - if hierarchyCol == initHierarchyCol { - return false, nil - } - for scanner.Scan() { - line := scanner.Text() - parts := strings.Fields(line) - if parts[hierarchyCol] != cgroupV2HierarchyValue { - return false, nil - } - } - return true, nil + return false, nil } -func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action string) error { +func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cgroupInfo string) error { cfg := config.Config{} if err := config.LoadConfig(srcFilePath, &cfg); err != nil { hwlog.RunLog.Errorf("failed to load configuration file: %v", err) return err } - isV2, err := isCgroupV2() + isV2, err := isCgroupV2(cgroupInfo) if err != nil { hwlog.RunLog.Errorf("failed to check whether cgroup v2: %v", err) return err -- Gitee From 6dd56398634dd6a4ec0af819c9cbbc82f79bbbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 11:33:29 +0800 Subject: [PATCH 08/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91containerd=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81cgroup=20v2=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 2 + install/process/containerd_process.go | 57 +++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/install/process/constant.go b/install/process/constant.go index 882ae42..3c8b89e 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -60,6 +60,8 @@ const ( containerdKey = "containerd" runtimesKey = "runtimes" runcKey = "runc" + runcOptionsKey = "options" + binaryNameKey = "BinaryName" cgroupInfoIndexFromEnd = 1 cgroupV2InfoStr = "cgroup2fs" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index cbddc15..e7faaee 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -85,17 +85,25 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg hwlog.RunLog.Errorf("failed to check whether cgroup v2: %v", err) return err } - runtimeValue := defaultRuntimeValue - runtimeType := v1DefaultRuncRuntimeType - if action == addCommand { - runtimeValue = runtimeFilePath - runtimeType = v1RuntimeType - } if isV2 { - // TODO hwlog.RunLog.Infof("it is cgroup v2") + binaryName := "" + if action == addCommand { + binaryName = runtimeFilePath + } + err = changeCgroupV2BinaryNameConfig(&cfg, binaryName) + if err != nil { + hwlog.RunLog.Errorf("failed to change cgroup v2 config, error: %v", err) + return err + } } else { hwlog.RunLog.Infof("it is cgroup v1") + runtimeValue := defaultRuntimeValue + runtimeType := v1DefaultRuncRuntimeType + if action == addCommand { + runtimeValue = runtimeFilePath + runtimeType = v1RuntimeType + } err = changeCgroupV1Config(&cfg, runtimeValue, runtimeType) if err != nil { hwlog.RunLog.Errorf("failed to change cgroup v1 config, error: %v", err) @@ -110,6 +118,41 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg return nil } +func changeCgroupV2BinaryNameConfig(cfg *config.Config, binaryName string) error { + value, ok := cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] + if !ok { + hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) + return fmt.Errorf("can not find plugin: %v", v1RuntimeTypeFisrtLevelPlugin) + } + valueMap := value.ToMap() + containerdConfig := valueMap[containerdKey] + runtimesConfig, err := getMap(containerdConfig, runtimesKey) + if err != nil { + return err + } + runcConfig, err := getMap(runtimesConfig, runcKey) + if err != nil { + return err + } + runcOptionsConfig, err := getMap(runcConfig, runcOptionsKey) + if err != nil { + return err + } + runcOptionsConfigMap, ok := runcOptionsConfig.(map[string]interface{}) + if !ok { + hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", runcOptionsKey, runcOptionsConfig) + return fmt.Errorf("can not convert config: %v", runcOptionsKey) + } + runcOptionsConfigMap[binaryNameKey] = binaryName + newTree, err := toml.TreeFromMap(valueMap) + if err != nil { + hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + return err + } + cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] = *newTree + return nil +} + func changeCgroupV1Config(cfg *config.Config, runtimeValue, runtimeType string) error { err := changeCgroupV1RuntimeConfig(cfg, runtimeValue) if err != nil { -- Gitee From 40cfd14a6a401af478d654eedb8b5012f7245ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 14:12:19 +0800 Subject: [PATCH 09/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 28 ++++++++++++++++--------- install/process/containerd_process.go | 30 +++++++++++++-------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/install/process/constant.go b/install/process/constant.go index 3c8b89e..435eab6 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -34,18 +34,19 @@ const noDefaultTemplate = `{ }` const ( - actionPosition = 0 - srcFilePosition = 1 - destFilePosition = 2 - runtimeFilePosition = 3 - rmCommandLength = 6 - addCommandLength = 7 - addCommand = "add" - rmCommand = "rm" - maxFileSize = 1024 * 1024 * 10 + actionPosition = 0 + srcFilePosition = 1 + destFilePosition = 2 + runtimeFilePosition = 3 + rmCommandLength = 6 + addCommandLength = 7 + maxFileSize = 1024 * 1024 * 10 + cgroupInfoIndexFromEnd = 1 ) const ( + addCommand = "add" + rmCommand = "rm" defaultRuntimeKey = "default-runtime" // InstallSceneDocker is a 'docker' string of scene InstallSceneDocker = "docker" @@ -62,6 +63,13 @@ const ( runcKey = "runc" runcOptionsKey = "options" binaryNameKey = "BinaryName" - cgroupInfoIndexFromEnd = 1 cgroupV2InfoStr = "cgroup2fs" ) + +const ( + notFindPluginLogStr = "can not find plugin %v, plugins is: %+v" + notFindOluginErrorStr = "can not find plugin: %v" + convertConfigFailLogStr = "can not convert config %v, config is: %+v" + convertConfigFailErrorStr + convertTreeFailLogStr = "failed to convert map to tree, error: %v" +) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index e7faaee..7e769c7 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -121,8 +121,8 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg func changeCgroupV2BinaryNameConfig(cfg *config.Config, binaryName string) error { value, ok := cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] if !ok { - hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) - return fmt.Errorf("can not find plugin: %v", v1RuntimeTypeFisrtLevelPlugin) + hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) + return fmt.Errorf(notFindOluginErrorStr, v1RuntimeTypeFisrtLevelPlugin) } valueMap := value.ToMap() containerdConfig := valueMap[containerdKey] @@ -140,13 +140,13 @@ func changeCgroupV2BinaryNameConfig(cfg *config.Config, binaryName string) error } runcOptionsConfigMap, ok := runcOptionsConfig.(map[string]interface{}) if !ok { - hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", runcOptionsKey, runcOptionsConfig) - return fmt.Errorf("can not convert config: %v", runcOptionsKey) + hwlog.RunLog.Errorf(convertConfigFailLogStr, runcOptionsKey, runcOptionsConfig) + return fmt.Errorf(convertConfigFailErrorStr, runcOptionsKey) } runcOptionsConfigMap[binaryNameKey] = binaryName newTree, err := toml.TreeFromMap(valueMap) if err != nil { - hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + hwlog.RunLog.Errorf(convertTreeFailLogStr, err) return err } cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] = *newTree @@ -168,13 +168,13 @@ func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error valueMap[v1NeedChangeKeyRuntime] = runtimeValue newTree, err := toml.TreeFromMap(valueMap) if err != nil { - hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + hwlog.RunLog.Errorf(convertTreeFailLogStr, err) return err } cfg.Plugins[v1RuntimeType] = *newTree } else { - hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeType, cfg.Plugins) - return fmt.Errorf("can not find plugin: %v", v1RuntimeType) + hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeType, cfg.Plugins) + return fmt.Errorf(notFindOluginErrorStr, v1RuntimeType) } return nil } @@ -182,8 +182,8 @@ func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) error { value, ok := cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] if !ok { - hwlog.RunLog.Errorf("can not find plugin %v, plugins is: %+v", v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) - return fmt.Errorf("can not find plugin: %v", v1RuntimeTypeFisrtLevelPlugin) + hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeTypeFisrtLevelPlugin, cfg.Plugins) + return fmt.Errorf(notFindOluginErrorStr, v1RuntimeTypeFisrtLevelPlugin) } valueMap := value.ToMap() containerdConfig := valueMap[containerdKey] @@ -197,13 +197,13 @@ func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) err } runcConfigMap, ok := runcConfig.(map[string]interface{}) if !ok { - hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", runcKey, runcConfig) - return fmt.Errorf("can not convert config: %v", runcKey) + hwlog.RunLog.Errorf(convertConfigFailLogStr, runcKey, runcConfig) + return fmt.Errorf(convertConfigFailErrorStr, runcKey) } runcConfigMap[v1NeedChangeKeyRuntimeType] = runtimeType newTree, err := toml.TreeFromMap(valueMap) if err != nil { - hwlog.RunLog.Errorf("failed to convert map to tree, error: %v", err) + hwlog.RunLog.Errorf(convertTreeFailLogStr, err) return err } cfg.Plugins[v1RuntimeTypeFisrtLevelPlugin] = *newTree @@ -213,8 +213,8 @@ func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) err func getMap(input interface{}, key string) (interface{}, error) { inputMap, ok := input.(map[string]interface{}) if !ok { - hwlog.RunLog.Errorf("can not convert config %v, config is: %+v", key, input) - return nil, fmt.Errorf("can not convert config: %v", key) + hwlog.RunLog.Errorf(convertConfigFailLogStr, key, input) + return nil, fmt.Errorf(convertConfigFailErrorStr, key) } output, ok := inputMap[key] if !ok { -- Gitee From c324ada4046887abd4bd05f62cb08eb4fd15ac48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 14:36:44 +0800 Subject: [PATCH 10/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=89=93=E5=8D=B0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=A0=BC=E5=BC=8F=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 70 +++++++++++++++++++------------------- build/scripts/uninstall.sh | 12 +++---- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index b73090a..361b974 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -40,7 +40,7 @@ function check_log { check_sub_path ${INSTALL_LOG_DIR} if [[ $? != 0 ]]; then - echo "[ERROR]: ${INSTALL_LOG_DIR} is invalid" + echo "[ERROR] ${INSTALL_LOG_DIR} is invalid" exit 1 fi @@ -71,11 +71,11 @@ function log { function check_path { local path="$1" if [[ ${#path} -gt 1024 ]] || [[ ${#path} -le 0 ]]; then - echo "[ERROR]: parameter is invalid, length not in 1~1024" + echo "[ERROR] parameter is invalid, length not in 1~1024" return 1 fi if [[ -n $(echo "${path}" | grep -Ev '^[a-zA-Z0-9./_-]*$') ]]; then - echo "[ERROR]: parameter is invalid, char not all in 'a-zA-Z0-9./_-'" + echo "[ERROR] parameter is invalid, char not all in 'a-zA-Z0-9./_-'" return 1 fi path=$(realpath -m -s "${path}") @@ -110,16 +110,16 @@ function check_sub_path { function check_path_permission { local path="$1" if [[ -L "${path}" ]]; then - echo "[ERROR]: ${path} is soft link" + echo "[ERROR] ${path} is soft link" return 1 fi if [[ $(stat -c %u "${path}") != 0 ]] || [[ "$(stat -c %g ${path})" != 0 ]]; then - echo "[ERROR]: user or group of ${path} is not root" + echo "[ERROR] user or group of ${path} is not root" return 1 fi local permission=$(stat -c %A "${path}") if [[ $(echo "${permission}" | cut -c6) == w ]] || [[ $(echo "${permission}" | cut -c9) == w ]]; then - echo "[ERROR]: group or other of ${path} has write permisson" + echo "[ERROR] group or other of ${path} has write permisson" return 1 fi } @@ -154,10 +154,10 @@ Options: function check_platform { plat="$(uname -m)" if [[ $start_script =~ $plat ]]; then - echo "[INFO]: platform($plat) matched!" + echo "[INFO] platform($plat) matched!" return 0 else - echo "[ERROR]: platform($plat) mismatch for $start_script, please check it" + echo "[ERROR] platform($plat) mismatch for $start_script, please check it" return 1 fi } @@ -182,26 +182,26 @@ function save_install_args() { function add_so() { check_path "/etc/os-release" if [[ $? != 0 ]]; then - echo "[ERROR]: /etc/os-release is invalid" + echo "[ERROR] /etc/os-release is invalid" return 1 fi if grep -qi "ubuntu" "/etc/os-release"; then - echo "[info]: os is Ubuntu" + echo "[info] os is Ubuntu" echo -e "\n/usr/lib/aarch64-linux-gnu/libcrypto.so.1.1" >> ${ASCEND_RUNTIME_CONFIG_DIR}/base.list echo "/usr/lib/aarch64-linux-gnu/libyaml-0.so.2" >> ${ASCEND_RUNTIME_CONFIG_DIR}/base.list elif grep -qi "euler" "/etc/os-release"; then - echo "[info]: os is Euler/OpenEuler" + echo "[info] os is Euler/OpenEuler" echo -e "\n/usr/lib64/libcrypto.so.1.1" >> ${ASCEND_RUNTIME_CONFIG_DIR}/base.list echo "/usr/lib64/libyaml-0.so.2" >> ${ASCEND_RUNTIME_CONFIG_DIR}/base.list else - echo "[ERROR]: not support this os" + echo "[ERROR] not support this os" return 1 fi } function install() { - echo "[INFO]: installing ascend docker runtime" + echo "[INFO] installing ascend docker runtime" check_platform if [[ $? != 0 ]]; then log "[ERROR]" "install failed, run package and os not matched in arch" @@ -275,10 +275,10 @@ function install() fi chmod 440 ${ASCEND_RUNTIME_CONFIG_DIR}/base.list - echo "[INFO]: install executable files success" + echo "[INFO] install executable files success" if [[ "${INSTALL_SCENE}" == "docker" ]]; then - echo "[INFO]: install is 'docker'." + echo "[INFO] install scene is 'docker'." check_path ${DOCKER_CONFIG_DIR}/daemon.json if [[ $? != 0 ]]; then log "[ERROR]" "install failed, ${DOCKER_CONFIG_DIR}/daemon.json is invalid" @@ -289,7 +289,7 @@ function install() SRC="${DOCKER_CONFIG_DIR}/daemon.json.${PPID}" DST="${DOCKER_CONFIG_DIR}/daemon.json" elif [[ "${INSTALL_SCENE}" == "containerd" ]]; then - echo "[INFO]: install is 'containerd'." + echo "[INFO] install scene is 'containerd'." check_path ${CONTAINERD_CONFIG_DIR}/config.toml if [[ $? != 0 ]]; then log "[ERROR]" "install failed, ${CONTAINERD_CONFIG_DIR}/config.toml is invalid" @@ -319,15 +319,15 @@ function install() chmod 600 ${DST} save_install_args - echo "[INFO]: Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" - echo "[INFO]: The version of Ascend Docker Runtime is: ${PACKAGE_VERSION}" - echo '[INFO]: please reboot daemon and container engine to take effect' + echo "[INFO] Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" + echo "[INFO] the version of Ascend Docker Runtime is: ${PACKAGE_VERSION}" + echo '[INFO] please reboot daemon and container engine to take effect' log "[INFO]" "Ascend Docker Runtime install success" } function uninstall() { - echo "[INFO]: Uninstalling ascend docker runtime ${PACKAGE_VERSION}" + echo "[INFO] uninstalling ascend docker runtime ${PACKAGE_VERSION}" if [ ! -d "${INSTALL_PATH}" ]; then log "[WARNING]" "uninstall skipping, the specified install path does not exist" @@ -351,7 +351,7 @@ function uninstall() function upgrade() { - echo "[INFO]: upgrading ascend docker runtime" + echo "[INFO] upgrading ascend docker runtime" check_platform if [[ $? != 0 ]]; then log "[ERROR]" "upgrade failed, run package and os not matched in arch" @@ -426,9 +426,9 @@ function upgrade() fi chmod 440 ${ASCEND_RUNTIME_CONFIG_DIR}/base.list - echo "[INFO]: Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" - echo '[INFO]: upgrade ascend docker runtime success' - echo "[INFO]: The version of Ascend Docker Runtime is: v${PACKAGE_VERSION}" + echo "[INFO] Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" + echo '[INFO] upgrade ascend docker runtime success' + echo "[INFO] the version of Ascend Docker Runtime is: v${PACKAGE_VERSION}" log "[INFO]" "Ascend Docker Runtime upgrade success" } INSTALL_SCENE_FLAG=n @@ -458,7 +458,7 @@ do case "$3" in --install-scene=*) if [ "${INSTALL_SCENE_FLAG}" == "y" ]; then - log "[ERROR]" "install failed, '--install-scene' Repeat parameter!" + log "[ERROR]" "install failed, repeat parameter '--install-scene' !" exit 1 fi need_help=n @@ -468,14 +468,14 @@ do elif [ "$3" == "--install-scene=containerd" ]; then INSTALL_SCENE=containerd else - log "[ERROR]" "failed, Please check the parameter of --install-scene=" + log "[ERROR]" "failed, please check the parameter of --install-scene=" exit 1 fi shift ;; --install) if [ "${INSTALL_FLAG}" == "y" ]; then - log "[ERROR]" "install failed, '--install' Repeat parameter!" + log "[ERROR]" "install failed, repeat parameter '--install' !" exit 1 fi need_help=n @@ -484,7 +484,7 @@ do ;; --uninstall) if [ "${UNINSTALL_FLAG}" == "y" ]; then - log "[ERROR]" "uninstall failed, '--uninstall' Repeat parameter!" + log "[ERROR]" "uninstall failed, repeat parameter '--uninstall' !" exit 1 fi need_help=n @@ -493,7 +493,7 @@ do ;; --install-path=*) if [ "${INSTALL_PATH_FLAG}" == "y" ]; then - log "[ERROR]" "failed, '--install-path' Repeat parameter!" + log "[ERROR]" "failed, repeat parameter '--install-path' !" exit 1 fi need_help=n @@ -504,7 +504,7 @@ do ;; --upgrade) if [ "${UPGRADE_FLAG}" == "y" ]; then - log "[ERROR]" "upgrade failed, '--upgrade' Repeat parameter!" + log "[ERROR]" "upgrade failed, repeat parameter '--upgrade' !" exit 1 fi need_help=n @@ -513,7 +513,7 @@ do ;; --ce=*) if [ "${ISULA}" == "isula" ]; then - log "[ERROR]" "failed, '--ce' Repeat parameter!" + log "[ERROR]" "failed, repeat parameter '--ce' !" exit 1 fi need_help=n @@ -522,7 +522,7 @@ do ISULA=isula RESERVEDEFAULT=yes else - log "[ERROR]" "failed, Please check the parameter of --ce=" + log "[ERROR]" "failed, please check the parameter of --ce=" exit 1 fi shift @@ -530,7 +530,7 @@ do --install-type=*) if [ "${a500}" == "y" ] || [ "${a200}" == "y" ] || [ "${a200isoc}" == "y" ] || [ "${a200ia2}" == "y" ] || [ "${a500a2}" == "y" ]; then - log "[ERROR]" "failed, '--install-type' Repeat parameter!" + log "[ERROR]" "failed, repeat parameter '--install-type' !" exit 1 fi need_help=n @@ -546,7 +546,7 @@ do elif [ "$3" == "--install-type=A200IA2" ]; then a200ia2=y else - log "[ERROR]" "failed, Please check the parameter of --install-type=" + log "[ERROR]" "failed, please check the parameter of --install-type=" exit 1 fi shift @@ -559,7 +559,7 @@ do ;; *) if [ "x$3" != "x" ]; then - log "[ERROR]" "failed, Unsupported parameters: $3" + log "[ERROR]" "failed, unsupported parameters: $3" print_help exit 1 fi diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index e0d9d76..f147c46 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -29,7 +29,7 @@ function check_log { check_sub_path ${INSTALL_LOG_DIR} if [[ $? != 0 ]]; then - echo "[ERROR]: ${INSTALL_LOG_DIR} is invalid" + echo "[ERROR] ${INSTALL_LOG_DIR} is invalid" exit 1 fi @@ -60,11 +60,11 @@ function log { function check_path { local path="$1" if [[ ${#path} -gt 1024 ]] || [[ ${#path} -le 0 ]]; then - echo "[ERROR]: parameter is invalid, length not in 1~1024" + echo "[ERROR] parameter is invalid, length not in 1~1024" return 1 fi if [[ -n $(echo "${path}" | grep -Ev '^[a-zA-Z0-9./_-]*$') ]]; then - echo "[ERROR]: parameter is invalid, char not all in 'a-zA-Z0-9./_-'" + echo "[ERROR] parameter is invalid, char not all in 'a-zA-Z0-9./_-'" return 1 fi path=$(realpath -m -s "${path}") @@ -99,16 +99,16 @@ function check_sub_path { function check_path_permission { local path="$1" if [[ -L "${path}" ]]; then - echo "[ERROR]: ${path} is soft link" + echo "[ERROR] ${path} is soft link" return 1 fi if [[ $(stat -c %u "${path}") != 0 ]] || [[ "$(stat -c %g ${path})" != 0 ]]; then - echo "[ERROR]: user or group of ${path} is not root" + echo "[ERROR] user or group of ${path} is not root" return 1 fi local permission=$(stat -c %A "${path}") if [[ $(echo "${permission}" | cut -c6) == w ]] || [[ $(echo "${permission}" | cut -c9) == w ]]; then - echo "[ERROR]: group or other of ${path} has write permisson" + echo "[ERROR] group or other of ${path} has write permisson" return 1 fi } -- Gitee From 4111cedc90414d336559fa0288c18f9ed014630f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 15:02:35 +0800 Subject: [PATCH 11/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=89=93=E5=8D=B0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=A0=BC=E5=BC=8F=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/uninstall.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index f147c46..613f99d 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -125,11 +125,11 @@ ROOT=$(cd $(dirname $0); pwd)/.. RESERVEDEFAULT=no if [ "$1" == "isula" ] ; then DST='/etc/isulad/daemon.json' - echo "[INFO]: You will recover iSula's daemon" + echo "[INFO] You will recover iSula's daemon" RESERVEDEFAULT=yes else DST='/etc/docker/daemon.json' - echo "[INFO]: You will recover Docker's daemon" + echo "[INFO] You will recover Docker's daemon" fi INSTALL_SCENE=$2 if [ ${INSTALL_SCENE} == containerd ] ; then @@ -175,6 +175,6 @@ fi if test -d ${INSTALL_ROOT_PATH} then rm -rf ${INSTALL_ROOT_PATH} - echo "[INFO]: delete ${INSTALL_ROOT_PATH} successful" + echo "[INFO] delete ${INSTALL_ROOT_PATH} successful" fi log "[INFO]" "uninstall.sh exec success" -- Gitee From 186019e15cb1e3dadbd90489e78ef2c8d1a312c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 5 Jul 2024 15:11:45 +0800 Subject: [PATCH 12/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=89=93=E5=8D=B0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=A0=BC=E5=BC=8F=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index 361b974..ef852ba 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -22,7 +22,7 @@ start_script=${start_arg#*--} ASCEND_RUNTIME_CONFIG_DIR=/etc/ascend-docker-runtime.d DOCKER_CONFIG_DIR=/etc/docker -CONTAINERD_CONFIG_DIR=/etc/containerd/ +CONTAINERD_CONFIG_DIR=/etc/containerd INSTALL_SCENE=docker INSTALL_PATH=/usr/local/Ascend/Ascend-Docker-Runtime readonly INSTALL_LOG_DIR=/var/log/ascend-docker-runtime @@ -300,6 +300,7 @@ function install() SRC="${CONTAINERD_CONFIG_DIR}/config.toml.${PPID}" DST="${CONTAINERD_CONFIG_DIR}/config.toml" if [ ! -e ${DST} ]; then + echo "[INFO] containerd config file does not exist, default ${DST} will be created" containerd config default > ${DST} fi else -- Gitee From e9b4ef6656d96b42011f2ec4d02e836d50d7ea5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Mon, 8 Jul 2024 11:28:12 +0800 Subject: [PATCH 13/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91add=20log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 1 + install/process/containerd_process.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/install/process/constant.go b/install/process/constant.go index 435eab6..40c588e 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -72,4 +72,5 @@ const ( convertConfigFailLogStr = "can not convert config %v, config is: %+v" convertConfigFailErrorStr convertTreeFailLogStr = "failed to convert map to tree, error: %v" + getMapFaileLogStr = "failed to get map, key: %v, error: %v" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 7e769c7..0fd6a9c 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -128,14 +128,17 @@ func changeCgroupV2BinaryNameConfig(cfg *config.Config, binaryName string) error containerdConfig := valueMap[containerdKey] runtimesConfig, err := getMap(containerdConfig, runtimesKey) if err != nil { + hwlog.RunLog.Errorf(getMapFaileLogStr, runtimesKey, err) return err } runcConfig, err := getMap(runtimesConfig, runcKey) if err != nil { + hwlog.RunLog.Errorf(getMapFaileLogStr, runcKey, err) return err } runcOptionsConfig, err := getMap(runcConfig, runcOptionsKey) if err != nil { + hwlog.RunLog.Errorf(getMapFaileLogStr, runcOptionsKey, err) return err } runcOptionsConfigMap, ok := runcOptionsConfig.(map[string]interface{}) @@ -189,10 +192,12 @@ func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) err containerdConfig := valueMap[containerdKey] runtimesConfig, err := getMap(containerdConfig, runtimesKey) if err != nil { + hwlog.RunLog.Errorf(getMapFaileLogStr, runtimesKey, err) return err } runcConfig, err := getMap(runtimesConfig, runcKey) if err != nil { + hwlog.RunLog.Errorf(getMapFaileLogStr, runcKey, err) return err } runcConfigMap, ok := runcConfig.(map[string]interface{}) @@ -235,7 +240,12 @@ func writeContainerdConfigToFile(cfg config.Config, destFilePath string) error { hwlog.RunLog.Errorf("failed to create file, error: %v", err) return err } - defer file.Close() + defer func() { + err := file.Close() + if err != nil { + hwlog.RunLog.Errorf("failed to close file, error: %v", err) + } + }() _, err = file.Write(tomlString) if err != nil { hwlog.RunLog.Errorf("failed to write, error: %v", err) -- Gitee From bbe3f7e4f3a48d62349d3843d1e36e7b291f1487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 11:07:37 +0800 Subject: [PATCH 14/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=96=B0=E5=A2=9E=E5=8F=82?= =?UTF-8?q?=E6=95=B0config-file-path=EF=BC=8C=E6=94=AF=E6=8C=81=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E8=BE=93=E5=85=A5=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/help.info | 5 ++- build/scripts/run_main.sh | 88 ++++++++++++++++++++++++-------------- build/scripts/uninstall.sh | 5 ++- install/main.go | 1 + 4 files changed, 66 insertions(+), 33 deletions(-) diff --git a/build/scripts/help.info b/build/scripts/help.info index 7e273ee..629519f 100644 --- a/build/scripts/help.info +++ b/build/scripts/help.info @@ -8,4 +8,7 @@ --ce= Only iSula need to specify the container engine(eg: --ce=isula) MUST use with --install or --uninstall --version Query Ascend-docker-runtime version - --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) \ No newline at end of file + --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) + --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). + If this parameter is not specified, the default configuration file path of docker or containerd is used. + For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. \ No newline at end of file diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index ef852ba..8862469 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -23,6 +23,7 @@ start_script=${start_arg#*--} ASCEND_RUNTIME_CONFIG_DIR=/etc/ascend-docker-runtime.d DOCKER_CONFIG_DIR=/etc/docker CONTAINERD_CONFIG_DIR=/etc/containerd +CONFIG_FILE_PATH="" INSTALL_SCENE=docker INSTALL_PATH=/usr/local/Ascend/Ascend-Docker-Runtime readonly INSTALL_LOG_DIR=/var/log/ascend-docker-runtime @@ -148,6 +149,9 @@ Options: MUST use with --install or --uninstall --version Query Ascend-docker-runtime version --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) + --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). + If this parameter is not specified, the default configuration file path of docker or containerd is used. + For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. " } @@ -175,6 +179,7 @@ function save_install_args() { echo -e "a200isoc=${a200isoc}" echo -e "a200ia2=${a200ia2}" echo -e "install-scene=${INSTALL_SCENE}" + echo -e "config-file-path=${CONFIG_FILE_PATH}" } > "${INSTALL_PATH}"/ascend_docker_runtime_install.info chmod 640 ${INSTALL_PATH}/ascend_docker_runtime_install.info } @@ -277,37 +282,43 @@ function install() echo "[INFO] install executable files success" - if [[ "${INSTALL_SCENE}" == "docker" ]]; then - echo "[INFO] install scene is 'docker'." - check_path ${DOCKER_CONFIG_DIR}/daemon.json - if [[ $? != 0 ]]; then - log "[ERROR]" "install failed, ${DOCKER_CONFIG_DIR}/daemon.json is invalid" - exit 1 - fi - [[ ! -d ${DOCKER_CONFIG_DIR} ]] && mkdir -p -m 750 ${DOCKER_CONFIG_DIR} - - SRC="${DOCKER_CONFIG_DIR}/daemon.json.${PPID}" - DST="${DOCKER_CONFIG_DIR}/daemon.json" - elif [[ "${INSTALL_SCENE}" == "containerd" ]]; then - echo "[INFO] install scene is 'containerd'." - check_path ${CONTAINERD_CONFIG_DIR}/config.toml - if [[ $? != 0 ]]; then - log "[ERROR]" "install failed, ${CONTAINERD_CONFIG_DIR}/config.toml is invalid" - exit 1 - fi - [[ ! -d ${CONTAINERD_CONFIG_DIR} ]] && mkdir -p -m 750 ${CONTAINERD_CONFIG_DIR} - - SRC="${CONTAINERD_CONFIG_DIR}/config.toml.${PPID}" - DST="${CONTAINERD_CONFIG_DIR}/config.toml" - if [ ! -e ${DST} ]; then - echo "[INFO] containerd config file does not exist, default ${DST} will be created" - containerd config default > ${DST} - fi + if [[ ${CONFIG_FILE_PATH} == "" ]]; then + if [[ "${INSTALL_SCENE}" == "docker" ]]; then + echo "[INFO] install scene is 'docker'." + check_path ${DOCKER_CONFIG_DIR}/daemon.json + if [[ $? != 0 ]]; then + log "[ERROR]" "install failed, ${DOCKER_CONFIG_DIR}/daemon.json is invalid" + exit 1 + fi + [[ ! -d ${DOCKER_CONFIG_DIR} ]] && mkdir -p -m 750 ${DOCKER_CONFIG_DIR} + + SRC="${DOCKER_CONFIG_DIR}/daemon.json.${PPID}" + DST="${DOCKER_CONFIG_DIR}/daemon.json" + elif [[ "${INSTALL_SCENE}" == "containerd" ]]; then + echo "[INFO] install scene is 'containerd'." + check_path ${CONTAINERD_CONFIG_DIR}/config.toml + if [[ $? != 0 ]]; then + log "[ERROR]" "install failed, ${CONTAINERD_CONFIG_DIR}/config.toml is invalid" + exit 1 + fi + [[ ! -d ${CONTAINERD_CONFIG_DIR} ]] && mkdir -p -m 750 ${CONTAINERD_CONFIG_DIR} + + SRC="${CONTAINERD_CONFIG_DIR}/config.toml.${PPID}" + DST="${CONTAINERD_CONFIG_DIR}/config.toml" + if [ ! -e ${DST} ]; then + echo "[INFO] containerd config file does not exist, default ${DST} will be created" + containerd config default > ${DST} + fi + else + log "[ERROR]" "install failed, invalid value '${INSTALL_SCENE}' of 'install-scene' " + exit 1 + fi else - log "[ERROR]" "install failed, invalid value '${INSTALL_SCENE}' of 'install-scene' " - exit 1 + SRC="${CONFIG_FILE_PATH}.${PPID}" + DST="${CONFIG_FILE_PATH}" fi + CGROUP_INFO=$(stat -fc %T /sys/fs/cgroup/) # exit when return code is not 0, if use 'set -e' ./ascend-docker-plugin-install-helper add ${DST} ${SRC} ${INSTALL_PATH}/ascend-docker-runtime ${RESERVEDEFAULT} ${INSTALL_SCENE} ${CGROUP_INFO} > /dev/null @@ -341,9 +352,9 @@ function uninstall() exit 1 fi - "${INSTALL_PATH}"/script/uninstall.sh ${ISULA} ${INSTALL_SCENE} + "${INSTALL_PATH}"/script/uninstall.sh ${ISULA} ${INSTALL_SCENE} ${CONFIG_FILE_PATH} if [[ $? != 0 ]]; then - log "[ERROR]" "uninstall failed, '${INSTALL_PATH}/script/uninstall.sh ${ISULA}' return non-zero" + log "[ERROR]" "uninstall failed, '${INSTALL_PATH}/script/uninstall.sh ${ISULA} ${INSTALL_SCENE} ${CONFIG_FILE_PATH}' return non-zero" exit 1 fi @@ -433,6 +444,7 @@ function upgrade() log "[INFO]" "Ascend Docker Runtime upgrade success" } INSTALL_SCENE_FLAG=n +CONFIG_FILE_PATH_FLAG=n INSTALL_FLAG=n INSTALL_PATH_FLAG=n UNINSTALL_FLAG=n @@ -459,7 +471,7 @@ do case "$3" in --install-scene=*) if [ "${INSTALL_SCENE_FLAG}" == "y" ]; then - log "[ERROR]" "install failed, repeat parameter '--install-scene' !" + log "[ERROR]" "failed, repeat parameter '--install-scene' !" exit 1 fi need_help=n @@ -474,6 +486,20 @@ do fi shift ;; + --config-file-path=*) + if [ "${CONFIG_FILE_PATH_FLAG}" == "y" ]; then + log "[ERROR]" "failed, repeat parameter '--config-file-path' !" + exit 1 + fi + need_help=n + CONFIG_FILE_PATH_FLAG=y + CONFIG_FILE_PATH=$(echo $3 | cut -d"=" -f2) + if [[ ! -e "$CONFIG_FILE_PATH" ]]; then + log "[ERROR]" "failed, file '$CONFIG_FILE_PATH' does not exist." + exit 1 + fi + shift + ;; --install) if [ "${INSTALL_FLAG}" == "y" ]; then log "[ERROR]" "install failed, repeat parameter '--install' !" diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index 613f99d..798eaf5 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -135,8 +135,11 @@ INSTALL_SCENE=$2 if [ ${INSTALL_SCENE} == containerd ] ; then DST='/etc/containerd/config.toml' fi +CONFIG_FILE_PATH=$3 +if [[ ${CONFIG_FILE_PATH} != "" ]]; then + DST=${CONFIG_FILE_PATH} +fi SRC="${DST}.${PPID}" - if [ ! -f "${DST}" ]; then log "[WARNING]" "uninstall skipping, ${DST} does not exist" exit 0 diff --git a/install/main.go b/install/main.go index 8b4f012..bd43897 100644 --- a/install/main.go +++ b/install/main.go @@ -87,6 +87,7 @@ func main() { log.Fatalf("error param") } var behavior string + hwlog.RunLog.Infof("command: %v", command) if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneDocker { err, behavior = process.DockerProcess(command) } else if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneContainerd { -- Gitee From 18e0a1cd1d654cbf1aed36b893b0cfee419dd8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 14:29:56 +0800 Subject: [PATCH 15/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E8=B0=83=E6=95=B4help?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/help.info | 6 ++++-- build/scripts/run_main.sh | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/build/scripts/help.info b/build/scripts/help.info index 629519f..795c031 100644 --- a/build/scripts/help.info +++ b/build/scripts/help.info @@ -8,7 +8,9 @@ --ce= Only iSula need to specify the container engine(eg: --ce=isula) MUST use with --install or --uninstall --version Query Ascend-docker-runtime version - --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) - --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). + --install-scene= Installation scenario, only docker or containerd + (eg: --install-scene=docker, default: docker) + --config-file-path Specifies the path of the Docker or containerd configuration file + (eg: --config-file-path=/etc/containerd/config.toml). If this parameter is not specified, the default configuration file path of docker or containerd is used. For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. \ No newline at end of file diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index 8862469..eddce95 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -149,7 +149,8 @@ Options: MUST use with --install or --uninstall --version Query Ascend-docker-runtime version --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) - --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). + --config-file-path Specifies the path of the Docker or containerd configuration file + (eg: --config-file-path=/etc/containerd/config.toml). If this parameter is not specified, the default configuration file path of docker or containerd is used. For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. " -- Gitee From 8c14dc24210dfcca032b0c45a8cd00f49e5c7112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 14:34:30 +0800 Subject: [PATCH 16/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E8=B0=83=E6=95=B4help?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/help.info | 5 +++-- build/scripts/run_main.sh | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/build/scripts/help.info b/build/scripts/help.info index 795c031..213989f 100644 --- a/build/scripts/help.info +++ b/build/scripts/help.info @@ -12,5 +12,6 @@ (eg: --install-scene=docker, default: docker) --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). - If this parameter is not specified, the default configuration file path of docker or containerd is used. - For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. \ No newline at end of file + If this parameter is not specified, the default configuration file path + of docker or containerd is used. For docker, the path is /etc/docker/daemon.json. + For containerd, the path is /etc/containerd/config.toml. \ No newline at end of file diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index eddce95..216129d 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -151,8 +151,9 @@ Options: --install-scene= Installation scenario, only docker or containerd(eg: --install-scene=docker, default: docker) --config-file-path Specifies the path of the Docker or containerd configuration file (eg: --config-file-path=/etc/containerd/config.toml). - If this parameter is not specified, the default configuration file path of docker or containerd is used. - For docker, the path is /etc/docker/daemon.json. For containerd, the path is /etc/containerd/config.toml. + If this parameter is not specified, the default configuration file path + of docker or containerd is used. For docker, the path is /etc/docker/daemon.json. + For containerd, the path is /etc/containerd/config.toml. " } -- Gitee From 4cb23fcf5e491b78f4fd27e8f461d234cf803aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 14:45:18 +0800 Subject: [PATCH 17/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E8=B0=83=E6=95=B4help?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b4d150e..354d854 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/agiledragon/gomonkey/v2 v2.8.0 github.com/containerd/containerd v1.6.24 github.com/opencontainers/runtime-spec v1.0.3-0.20220718201635-a8106e99982b + github.com/pelletier/go-toml v1.9.5 github.com/prashantv/gostub v0.0.0-00010101000000-000000000000 github.com/stretchr/testify v1.8.2 huawei.com/npu-exporter/v5 v5.0.0 @@ -30,7 +31,6 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect github.com/opencontainers/runc v1.1.12 // indirect - github.com/pelletier/go-toml v1.9.5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sirupsen/logrus v1.9.0 // indirect -- Gitee From d294b4a91b3a0cea5e6e151e42950eec8e11ddcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 14:56:00 +0800 Subject: [PATCH 18/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E8=B0=83=E6=95=B4help?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/install/main.go b/install/main.go index bd43897..4fc1433 100644 --- a/install/main.go +++ b/install/main.go @@ -73,9 +73,10 @@ func main() { log.Fatalf("command error, please check %s for detail", logPath) } - const helpMessage = "\tadd \n" + - "\t rm \n" + - "\t -h help command" + const helpMessage = "\tadd " + + " \n" + + "\t rm " + + " \n" + "\t -h help command" helpFlag := flag.Bool("h", false, helpMessage) flag.Parse() if *helpFlag { -- Gitee From 8557dc1b00d8aee1a63150eb0f15253cc1d253ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 15:02:50 +0800 Subject: [PATCH 19/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/main.go | 2 +- install/process/containerd_process.go | 2 +- install/process/docker_process.go | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/install/main.go b/install/main.go index 4fc1433..773610d 100644 --- a/install/main.go +++ b/install/main.go @@ -16,7 +16,6 @@ package main import ( - "ascend-docker-runtime/install/process" "context" "flag" "fmt" @@ -26,6 +25,7 @@ import ( "huawei.com/npu-exporter/v5/common-utils/hwlog" + "ascend-docker-runtime/install/process" "ascend-docker-runtime/mindxcheckutils" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 0fd6a9c..3776e3c 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -16,12 +16,12 @@ package process import ( "fmt" - "github.com/pelletier/go-toml" "os" "path/filepath" "strings" "github.com/containerd/containerd/services/server/config" + "github.com/pelletier/go-toml" "huawei.com/npu-exporter/v5/common-utils/hwlog" "ascend-docker-runtime/mindxcheckutils" diff --git a/install/process/docker_process.go b/install/process/docker_process.go index fc85cde..6467859 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -16,12 +16,13 @@ package process import ( - "ascend-docker-runtime/mindxcheckutils" "encoding/json" "fmt" "io/ioutil" "os" "path/filepath" + + "ascend-docker-runtime/mindxcheckutils" ) var reserveDefaultRuntime = false -- Gitee From a67ac97305129e971dbb90670f53479fadc4e93b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 17:10:23 +0800 Subject: [PATCH 20/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/main.go | 5 +++-- install/process/containerd_process.go | 1 + install/process/docker_process.go | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/install/main.go b/install/main.go index 773610d..dbd468c 100644 --- a/install/main.go +++ b/install/main.go @@ -89,9 +89,10 @@ func main() { } var behavior string hwlog.RunLog.Infof("command: %v", command) - if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneDocker { + installScene := command[len(command)-installSceneIndexFromEnd] + if installScene == process.InstallSceneDocker { err, behavior = process.DockerProcess(command) - } else if command[len(command)-installSceneIndexFromEnd] == process.InstallSceneContainerd { + } else if installScene == process.InstallSceneContainerd { err, behavior = process.ContainerdProcess(command) } else { hwlog.RunLog.Errorf("error param: %v", command[len(command)-1]) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 3776e3c..217579a 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -27,6 +27,7 @@ import ( "ascend-docker-runtime/mindxcheckutils" ) +// ContainerdProcess modifies the containerd configuration file when installing or uninstalling the containerd scenario. func ContainerdProcess(command []string) (error, string) { action := command[actionPosition] correctParam, behavior := checkParamAndGetBehavior(action, command) diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 6467859..8c9e10a 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -27,6 +27,7 @@ import ( var reserveDefaultRuntime = false +// DockerProcess modifies the docker configuration file when installing or uninstalling the docker scenario. func DockerProcess(command []string) (error, string) { action := command[actionPosition] -- Gitee From 497fdd80126b44f7f1c7750a5da94c492c21afa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 9 Jul 2024 20:00:18 +0800 Subject: [PATCH 21/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E6=9B=B4=E6=96=B0Copyright?= =?UTF-8?q?=E5=B9=B4=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/common.go | 2 +- install/process/constant.go | 2 +- install/process/containerd_process.go | 2 +- install/process/docker_process.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/install/process/common.go b/install/process/common.go index 7e73dc7..0cff12a 100644 --- a/install/process/common.go +++ b/install/process/common.go @@ -1,4 +1,4 @@ -/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +/* Copyright(C) 2024. Huawei Technologies Co.,Ltd. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/install/process/constant.go b/install/process/constant.go index 40c588e..5f8991d 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -1,4 +1,4 @@ -/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +/* Copyright(C) 2024. Huawei Technologies Co.,Ltd. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 217579a..ec68803 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -1,4 +1,4 @@ -/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +/* Copyright(C) 2024. Huawei Technologies Co.,Ltd. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 8c9e10a..77b5871 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -1,4 +1,4 @@ -/* Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved. +/* Copyright(C) 2024. Huawei Technologies Co.,Ltd. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at -- Gitee From 7394bdfde08b925c64807c0ee110f070e58e878e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Tue, 16 Jul 2024 12:02:24 +0800 Subject: [PATCH 22/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E5=A4=8D=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/containerd_process.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index ec68803..fb0fee4 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -145,7 +145,7 @@ func changeCgroupV2BinaryNameConfig(cfg *config.Config, binaryName string) error runcOptionsConfigMap, ok := runcOptionsConfig.(map[string]interface{}) if !ok { hwlog.RunLog.Errorf(convertConfigFailLogStr, runcOptionsKey, runcOptionsConfig) - return fmt.Errorf(convertConfigFailErrorStr, runcOptionsKey) + return fmt.Errorf(convertConfigFailErrorStr, runcOptionsKey, runcOptionsConfig) } runcOptionsConfigMap[binaryNameKey] = binaryName newTree, err := toml.TreeFromMap(valueMap) @@ -204,7 +204,7 @@ func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) err runcConfigMap, ok := runcConfig.(map[string]interface{}) if !ok { hwlog.RunLog.Errorf(convertConfigFailLogStr, runcKey, runcConfig) - return fmt.Errorf(convertConfigFailErrorStr, runcKey) + return fmt.Errorf(convertConfigFailErrorStr, runcKey, runcConfig) } runcConfigMap[v1NeedChangeKeyRuntimeType] = runtimeType newTree, err := toml.TreeFromMap(valueMap) @@ -220,7 +220,7 @@ func getMap(input interface{}, key string) (interface{}, error) { inputMap, ok := input.(map[string]interface{}) if !ok { hwlog.RunLog.Errorf(convertConfigFailLogStr, key, input) - return nil, fmt.Errorf(convertConfigFailErrorStr, key) + return nil, fmt.Errorf(convertConfigFailErrorStr, key, input) } output, ok := inputMap[key] if !ok { -- Gitee From 0385e08e93a29109192a50f53d674cb7a039aa22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 25 Jul 2024 11:27:25 +0800 Subject: [PATCH 23/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/main.go | 4 ++-- install/process/containerd_process.go | 23 +++++++++++------------ install/process/docker_process.go | 16 ++++++++-------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/install/main.go b/install/main.go index dbd468c..571c692 100644 --- a/install/main.go +++ b/install/main.go @@ -91,9 +91,9 @@ func main() { hwlog.RunLog.Infof("command: %v", command) installScene := command[len(command)-installSceneIndexFromEnd] if installScene == process.InstallSceneDocker { - err, behavior = process.DockerProcess(command) + behavior, err = process.DockerProcess(command) } else if installScene == process.InstallSceneContainerd { - err, behavior = process.ContainerdProcess(command) + behavior, err = process.ContainerdProcess(command) } else { hwlog.RunLog.Errorf("error param: %v", command[len(command)-1]) log.Fatalf("error param: %v", command[len(command)-1]) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index fb0fee4..6bcfe35 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -28,43 +28,43 @@ import ( ) // ContainerdProcess modifies the containerd configuration file when installing or uninstalling the containerd scenario. -func ContainerdProcess(command []string) (error, string) { +func ContainerdProcess(command []string) (string, error) { action := command[actionPosition] correctParam, behavior := checkParamAndGetBehavior(action, command) if !correctParam { - return fmt.Errorf("error param"), "" + return "", fmt.Errorf("error param") } srcFilePath := command[srcFilePosition] if _, err := os.Stat(srcFilePath); os.IsNotExist(err) { if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(srcFilePath), true, false); err != nil { hwlog.RunLog.Errorf("check failed, error: %v", err) - return err, behavior + return behavior, err } } else { if _, err := mindxcheckutils.RealFileChecker(srcFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { hwlog.RunLog.Errorf("check failed, error: %v", err) - return err, behavior + return behavior, err } } destFilePath := command[destFilePosition] if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(destFilePath), true, false); err != nil { - return err, behavior + return behavior, err } runtimeFilePath := "" if len(command) == addCommandLength { runtimeFilePath = command[runtimeFilePosition] if _, err := mindxcheckutils.RealFileChecker(runtimeFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { hwlog.RunLog.Errorf("failed to check, error: %v", err) - return err, behavior + return behavior, err } } cgroupInfo := command[len(command)-cgroupInfoIndexFromEnd] err := editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cgroupInfo) if err != nil { hwlog.RunLog.Errorf("failed to edit containerd config, err: %v", err) - return err, behavior + return behavior, err } - return nil, behavior + return behavior, nil } func isCgroupV2(cgroupInfo string) (bool, error) { @@ -176,11 +176,10 @@ func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error return err } cfg.Plugins[v1RuntimeType] = *newTree - } else { - hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeType, cfg.Plugins) - return fmt.Errorf(notFindOluginErrorStr, v1RuntimeType) + return nil } - return nil + hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeType, cfg.Plugins) + return fmt.Errorf(notFindOluginErrorStr, v1RuntimeType) } func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) error { diff --git a/install/process/docker_process.go b/install/process/docker_process.go index 77b5871..084061c 100644 --- a/install/process/docker_process.go +++ b/install/process/docker_process.go @@ -28,34 +28,34 @@ import ( var reserveDefaultRuntime = false // DockerProcess modifies the docker configuration file when installing or uninstalling the docker scenario. -func DockerProcess(command []string) (error, string) { +func DockerProcess(command []string) (string, error) { action := command[actionPosition] correctParam, behavior := checkParamAndGetBehavior(action, command) if !correctParam { - return fmt.Errorf("error param"), "" + return "", fmt.Errorf("error param") } srcFilePath := command[srcFilePosition] if _, err := os.Stat(srcFilePath); os.IsNotExist(err) { if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(srcFilePath), true, false); err != nil { - return err, behavior + return behavior, err } } else { if _, err := mindxcheckutils.RealFileChecker(srcFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err, behavior + return behavior, err } } destFilePath := command[destFilePosition] if _, err := mindxcheckutils.RealDirChecker(filepath.Dir(destFilePath), true, false); err != nil { - return err, behavior + return behavior, err } runtimeFilePath := "" if len(command) == addCommandLength { runtimeFilePath = command[runtimeFilePosition] if _, err := mindxcheckutils.RealFileChecker(runtimeFilePath, true, false, mindxcheckutils.DefaultSize); err != nil { - return err, behavior + return behavior, err } } @@ -64,9 +64,9 @@ func DockerProcess(command []string) (error, string) { // check file permission writeContent, err := createJsonString(srcFilePath, runtimeFilePath, action) if err != nil { - return err, behavior + return behavior, err } - return writeJson(destFilePath, writeContent), behavior + return behavior, writeJson(destFilePath, writeContent) } func createJsonString(srcFilePath, runtimeFilePath, action string) ([]byte, error) { -- Gitee From 4f3eb218fbc0aaaf6850463bf45a2c135d89e3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 25 Jul 2024 11:30:41 +0800 Subject: [PATCH 24/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/containerd_process.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 6bcfe35..34c6869 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -167,19 +167,20 @@ func changeCgroupV1Config(cfg *config.Config, runtimeValue, runtimeType string) } func changeCgroupV1RuntimeConfig(cfg *config.Config, runtimeValue string) error { - if value, ok := cfg.Plugins[v1RuntimeType]; ok { - valueMap := value.ToMap() - valueMap[v1NeedChangeKeyRuntime] = runtimeValue - newTree, err := toml.TreeFromMap(valueMap) - if err != nil { - hwlog.RunLog.Errorf(convertTreeFailLogStr, err) - return err - } - cfg.Plugins[v1RuntimeType] = *newTree - return nil + value, ok := cfg.Plugins[v1RuntimeType] + if !ok { + hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeType, cfg.Plugins) + return fmt.Errorf(notFindOluginErrorStr, v1RuntimeType) } - hwlog.RunLog.Errorf(notFindPluginLogStr, v1RuntimeType, cfg.Plugins) - return fmt.Errorf(notFindOluginErrorStr, v1RuntimeType) + valueMap := value.ToMap() + valueMap[v1NeedChangeKeyRuntime] = runtimeValue + newTree, err := toml.TreeFromMap(valueMap) + if err != nil { + hwlog.RunLog.Errorf(convertTreeFailLogStr, err) + return err + } + cfg.Plugins[v1RuntimeType] = *newTree + return nil } func changeCgroupV1RuntimeTypeConfig(cfg *config.Config, runtimeType string) error { -- Gitee From 817fa405140d5fec5289f84eb8875b409edf69b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 25 Jul 2024 16:36:06 +0800 Subject: [PATCH 25/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index 216129d..b7cb08c 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -334,7 +334,7 @@ function install() save_install_args echo "[INFO] Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" - echo "[INFO] the version of Ascend Docker Runtime is: ${PACKAGE_VERSION}" + echo "[INFO] The version of Ascend Docker Runtime is: ${PACKAGE_VERSION}" echo '[INFO] please reboot daemon and container engine to take effect' log "[INFO]" "Ascend Docker Runtime install success" } -- Gitee From efa57f0ecc14009f9eac8aa4703bae13d94c07cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Thu, 25 Jul 2024 17:28:04 +0800 Subject: [PATCH 26/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index b7cb08c..6caaf3e 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -442,7 +442,7 @@ function upgrade() echo "[INFO] Ascend Docker Runtime has been installed in: ${INSTALL_PATH}" echo '[INFO] upgrade ascend docker runtime success' - echo "[INFO] the version of Ascend Docker Runtime is: v${PACKAGE_VERSION}" + echo "[INFO] The version of Ascend Docker Runtime is: v${PACKAGE_VERSION}" log "[INFO]" "Ascend Docker Runtime upgrade success" } INSTALL_SCENE_FLAG=n -- Gitee From 34527694455659b91ebb46727feb31482ec7b683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 26 Jul 2024 09:02:16 +0800 Subject: [PATCH 27/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/run_main.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/build/scripts/run_main.sh b/build/scripts/run_main.sh index 6caaf3e..c5b0d43 100644 --- a/build/scripts/run_main.sh +++ b/build/scripts/run_main.sh @@ -473,7 +473,7 @@ do case "$3" in --install-scene=*) if [ "${INSTALL_SCENE_FLAG}" == "y" ]; then - log "[ERROR]" "failed, repeat parameter '--install-scene' !" + log "[ERROR]" "failed, '--install-scene' Repeat parameter!" exit 1 fi need_help=n @@ -490,7 +490,7 @@ do ;; --config-file-path=*) if [ "${CONFIG_FILE_PATH_FLAG}" == "y" ]; then - log "[ERROR]" "failed, repeat parameter '--config-file-path' !" + log "[ERROR]" "failed, '--config-file-path' Repeat parameter!" exit 1 fi need_help=n @@ -504,7 +504,7 @@ do ;; --install) if [ "${INSTALL_FLAG}" == "y" ]; then - log "[ERROR]" "install failed, repeat parameter '--install' !" + log "[ERROR]" "install failed, '--install' Repeat parameter!" exit 1 fi need_help=n @@ -513,7 +513,7 @@ do ;; --uninstall) if [ "${UNINSTALL_FLAG}" == "y" ]; then - log "[ERROR]" "uninstall failed, repeat parameter '--uninstall' !" + log "[ERROR]" "uninstall failed, '--uninstall' Repeat parameter!" exit 1 fi need_help=n @@ -522,7 +522,7 @@ do ;; --install-path=*) if [ "${INSTALL_PATH_FLAG}" == "y" ]; then - log "[ERROR]" "failed, repeat parameter '--install-path' !" + log "[ERROR]" "failed, '--install-path' Repeat parameter!" exit 1 fi need_help=n @@ -533,7 +533,7 @@ do ;; --upgrade) if [ "${UPGRADE_FLAG}" == "y" ]; then - log "[ERROR]" "upgrade failed, repeat parameter '--upgrade' !" + log "[ERROR]" "upgrade failed, '--upgrade' Repeat parameter!" exit 1 fi need_help=n @@ -542,7 +542,7 @@ do ;; --ce=*) if [ "${ISULA}" == "isula" ]; then - log "[ERROR]" "failed, repeat parameter '--ce' !" + log "[ERROR]" "failed, '--ce' Repeat parameter!" exit 1 fi need_help=n @@ -559,7 +559,7 @@ do --install-type=*) if [ "${a500}" == "y" ] || [ "${a200}" == "y" ] || [ "${a200isoc}" == "y" ] || [ "${a200ia2}" == "y" ] || [ "${a500a2}" == "y" ]; then - log "[ERROR]" "failed, repeat parameter '--install-type' !" + log "[ERROR]" "failed, '--install-type' Repeat parameter!" exit 1 fi need_help=n -- Gitee From deb30f29aa95af8ab7f7fe2d1e52b24809f90ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 26 Jul 2024 11:59:20 +0800 Subject: [PATCH 28/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BC=98=E5=8C=96=E5=8D=B8?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/scripts/uninstall.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build/scripts/uninstall.sh b/build/scripts/uninstall.sh index 798eaf5..2b55cb3 100644 --- a/build/scripts/uninstall.sh +++ b/build/scripts/uninstall.sh @@ -132,8 +132,10 @@ else echo "[INFO] You will recover Docker's daemon" fi INSTALL_SCENE=$2 -if [ ${INSTALL_SCENE} == containerd ] ; then +if [ "${INSTALL_SCENE}" == "containerd" ] ; then DST='/etc/containerd/config.toml' +elif [ "${INSTALL_SCENE}" == "" ] ; then + INSTALL_SCENE=docker fi CONFIG_FILE_PATH=$3 if [[ ${CONFIG_FILE_PATH} != "" ]]; then -- Gitee From ca0bf1819e8eb691283ed6631f2f0b3afcc35c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 26 Jul 2024 17:05:18 +0800 Subject: [PATCH 29/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/constant.go | 12 ++++++------ install/process/containerd_process.go | 18 +++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/install/process/constant.go b/install/process/constant.go index 5f8991d..a5a461b 100644 --- a/install/process/constant.go +++ b/install/process/constant.go @@ -67,10 +67,10 @@ const ( ) const ( - notFindPluginLogStr = "can not find plugin %v, plugins is: %+v" - notFindOluginErrorStr = "can not find plugin: %v" - convertConfigFailLogStr = "can not convert config %v, config is: %+v" - convertConfigFailErrorStr - convertTreeFailLogStr = "failed to convert map to tree, error: %v" - getMapFaileLogStr = "failed to get map, key: %v, error: %v" + notFindPluginLogStr = "can not find plugin %v, plugins is: %+v" + notFindOluginErrorStr = "can not find plugin: %v" + convertConfigFailLogStr = "can not convert config %v, config is: %+v" + convertConfigFailErrorStr = "can not convert config %v, config is: %+v" + convertTreeFailLogStr = "failed to convert map to tree, error: %v" + getMapFaileLogStr = "failed to get map, key: %v, error: %v" ) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 34c6869..6a16bab 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -67,12 +67,12 @@ func ContainerdProcess(command []string) (string, error) { return behavior, nil } -func isCgroupV2(cgroupInfo string) (bool, error) { +func isCgroupV2(cgroupInfo string) bool { hwlog.RunLog.Infof("cgroup info: %v", cgroupInfo) if strings.Contains(cgroupInfo, cgroupV2InfoStr) { - return true, nil + return true } - return false, nil + return false } func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cgroupInfo string) error { @@ -81,18 +81,14 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg hwlog.RunLog.Errorf("failed to load configuration file: %v", err) return err } - isV2, err := isCgroupV2(cgroupInfo) - if err != nil { - hwlog.RunLog.Errorf("failed to check whether cgroup v2: %v", err) - return err - } + isV2 := isCgroupV2(cgroupInfo) if isV2 { hwlog.RunLog.Infof("it is cgroup v2") binaryName := "" if action == addCommand { binaryName = runtimeFilePath } - err = changeCgroupV2BinaryNameConfig(&cfg, binaryName) + err := changeCgroupV2BinaryNameConfig(&cfg, binaryName) if err != nil { hwlog.RunLog.Errorf("failed to change cgroup v2 config, error: %v", err) return err @@ -105,13 +101,13 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg runtimeValue = runtimeFilePath runtimeType = v1RuntimeType } - err = changeCgroupV1Config(&cfg, runtimeValue, runtimeType) + err := changeCgroupV1Config(&cfg, runtimeValue, runtimeType) if err != nil { hwlog.RunLog.Errorf("failed to change cgroup v1 config, error: %v", err) return err } } - err = writeContainerdConfigToFile(cfg, destFilePath) + err := writeContainerdConfigToFile(cfg, destFilePath) if err != nil { hwlog.RunLog.Errorf("failed to write configuration file: %v", err) return err -- Gitee From 217da05f55fed93e6f66f913e4f05a5c4f5b1603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 26 Jul 2024 17:14:16 +0800 Subject: [PATCH 30/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/containerd_process.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 6a16bab..9c3bdd6 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -67,21 +67,13 @@ func ContainerdProcess(command []string) (string, error) { return behavior, nil } -func isCgroupV2(cgroupInfo string) bool { - hwlog.RunLog.Infof("cgroup info: %v", cgroupInfo) - if strings.Contains(cgroupInfo, cgroupV2InfoStr) { - return true - } - return false -} - func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cgroupInfo string) error { cfg := config.Config{} if err := config.LoadConfig(srcFilePath, &cfg); err != nil { hwlog.RunLog.Errorf("failed to load configuration file: %v", err) return err } - isV2 := isCgroupV2(cgroupInfo) + isV2 := strings.Contains(cgroupInfo, cgroupV2InfoStr) if isV2 { hwlog.RunLog.Infof("it is cgroup v2") binaryName := "" -- Gitee From a72436f8ef2197fcef89d30f2a82f2380aa87201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B8=A3=E6=B2=BC?= Date: Fri, 26 Jul 2024 17:22:15 +0800 Subject: [PATCH 31/31] =?UTF-8?q?=20=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=20Modification=E3=80=91=E4=BF=AE=E6=94=B9=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install/process/containerd_process.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/install/process/containerd_process.go b/install/process/containerd_process.go index 9c3bdd6..c471eb4 100644 --- a/install/process/containerd_process.go +++ b/install/process/containerd_process.go @@ -73,9 +73,8 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg hwlog.RunLog.Errorf("failed to load configuration file: %v", err) return err } - isV2 := strings.Contains(cgroupInfo, cgroupV2InfoStr) - if isV2 { - hwlog.RunLog.Infof("it is cgroup v2") + if strings.Contains(cgroupInfo, cgroupV2InfoStr) { + hwlog.RunLog.Info("it is cgroup v2") binaryName := "" if action == addCommand { binaryName = runtimeFilePath @@ -86,7 +85,7 @@ func editContainerdConfig(srcFilePath, runtimeFilePath, destFilePath, action, cg return err } } else { - hwlog.RunLog.Infof("it is cgroup v1") + hwlog.RunLog.Info("it is cgroup v1") runtimeValue := defaultRuntimeValue runtimeType := v1DefaultRuncRuntimeType if action == addCommand { -- Gitee