diff --git a/virtrust/.keep b/virtrust/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/virtrust/src/virtrust-sh/CMakeLists.txt b/virtrust/src/virtrust-sh/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..16a577cd4a3cf5242006d941cdf83eb526128131 --- /dev/null +++ b/virtrust/src/virtrust-sh/CMakeLists.txt @@ -0,0 +1,18 @@ +# Copyright (C) 2025 by Huawei Technologies Co., Ltd. All rights reserved. + +add_library(virtrust-sh-obj OBJECT) + +set(VIRTRUST_SH_SOURCE_FILES "") + +add_subdirectory(operator) + +target_source(virtrust-sh-obj PRIVATE ${VIRTRUST_SH_SOURCE_FILES}) + +target_include_directories( + virtrust-sh-obj PRIVATE $ + ${CMAKE_DEPENDENCY_INCLUDEDIR}) + +target_link_libraries(virtrust-sh PRIVATE virtrust-sh-obj virtrust-shared) + +install(TARGETS virtrust-sh RUNTIME DESTINATION bin PERMISSIONS OWNER_READ + OWNER_EXECUTE) diff --git a/virtrust/src/virtrust-sh/defines.h b/virtrust/src/virtrust-sh/defines.h new file mode 100644 index 0000000000000000000000000000000000000000..b1a8abc355dcac041d7b12076b8d953fa6b368cf --- /dev/null +++ b/virtrust/src/virtrust-sh/defines.h @@ -0,0 +1,13 @@ +// Copyright (C) 2025 by Huawei Technologies Co., Ltd. All rights reserved. + +#pragma once + +#include + +namespace virtrust { +constexpr std::string_view VIRTRUST_SH_VERSION = "1.0.0"; +constexpr std::string_view VIRTRUST_SH_VIRT_INSTALL_PATH = + "/usr/bin/virt-install"; +constexpr std::string_view VIRTRUST_SH_LOGFILE_NAME = "virtrust.log"; +constexpr int VIRTRUST_SH__CMD_STR_MAX_LEN = 1024; +} // namespace virtrust diff --git a/virtrust/src/virtrust-sh/main.cpp b/virtrust/src/virtrust-sh/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..447071bef5b92e4260cb2079db18b70f58df1e20 --- /dev/null +++ b/virtrust/src/virtrust-sh/main.cpp @@ -0,0 +1,165 @@ +// Copyright (C) 2025 by Huawei Technologies Co., Ltd. All rights reserved. + +#include +#include + +#include +#include +#include +#include +#include + +#include "spdlog/fmt/fmt.h" + +#include "virtrust-sh/defines.h" +#include "virtrust-sh/operator/op_itf.h" +#include "virtrust/base/custom_logger.h" +#include "virtrust/base/logger.h" + +namespace { + +std::string GetLogPath() { + return std::filesystem::current_path() / virtrust::VIRTRUST_SH_LOGFILE_NAME; +} + +void PrintVersion(std::string_view progname) { + fmt::print("{} version: {}\n", progname, virtrust::VIRTRUST_SH_VERSION); +} + +void PrintUsage(std::string_view progname) { + fmt::print( + "\n" + " USAGE:\n" + " {} [options]... []\n" + " {} [options]... [args...]\n" + "\n" + " OPTIONS:\n" + " -c | --connect=URI hypervisor connection URI, " + "default to {}\n" + " -d | --debug run in debug mode\n" + " -h | --help print this help\n" + " -v | --version show verion\n" + "\n" + " COMMANDS:\n" + " create create virtual machine instance\n" + " destroy destroy (stop) a domain\n" + " list list domain information\n" + " migrate migrate domain to another host\n" + " start start a (previously define) inactive " + "domain\n" + " undefine undefine a domain\n" + "\n", + progname, progname, virtrust::VIRTRUST_DEFAULT_URI); +} + +int ProcessOp(int argc, char **argv, const virtrust::OpConfig &config) { + auto op = virtrust::MakeOperator(virtrust::OpTyFromStr(argv[optind])); + if (op == nullptr) { + fmt::print(":\nInvalid command: {}\n", argv[optind]); + PrintUsage(argc[0]); + return 1; + } + + // Setup config + op->SetConfig(config); + + // Parse subcommand + auto rc = op->ParseArgc(argc - optind, &argc[optind]); + if (rc != virtrust::OpRc::OK) { + return 1; + } + + // Execute subcommand, if enabled + if (op->GetConfig().endableExec) { + rc = op->Exec(); + if (rc != virtrust::OpRc::OK) { + fmt::print("\nCommand execution failed.\nSee log at: {}\n", GetLogPath); + return 1; + } + } + fmt::print("\nCommand execution succeed.\nSee log at: {}\n", GetLogPath); + return 0; +} + +int ProcessArgs(int argc, char **argv) { + int arg = -1; + int longindex = -1; + std::vector