From 31b8e600c04320c38ac85374e1007403e0575ddd Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Tue, 23 Jun 2015 17:11:13 +0200 Subject: [PATCH] Add console_zone to cli [Feature] Launch a console for the specified zone. [Cause] N/A [Solution] Add console_zone to cli. [Verification] Build, run server, vasum-cli create/start/console/shutdown zone. Change-Id: I093837d6807931163321812a2bcba467a5a9f27e --- cli/CMakeLists.txt | 4 +++- cli/command-line-interface.cpp | 35 +++++++++++++++++++++++++++++++++++ cli/command-line-interface.hpp | 6 ++++++ cli/main.cpp | 9 +++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 36750f0..fe4b067 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -19,7 +19,9 @@ MESSAGE(STATUS "") MESSAGE(STATUS "Generating makefile for the command line interface...") -FILE(GLOB cli_SRCS *.cpp *.hpp) +FILE(GLOB cli_SRCS *.cpp *.hpp + ${COMMON_FOLDER}/utils/c-array.cpp + ${COMMON_FOLDER}/utils/c-array.hpp) ## Setup target ################################################################ SET(CLI_CODENAME "${PROJECT_NAME}-cli") diff --git a/cli/command-line-interface.cpp b/cli/command-line-interface.cpp index 3b100eb..cdd64b4 100644 --- a/cli/command-line-interface.cpp +++ b/cli/command-line-interface.cpp @@ -25,6 +25,7 @@ #include "config.hpp" #include "command-line-interface.hpp" #include "vasum-client.h" +#include "utils/c-array.hpp" #include #include @@ -39,6 +40,7 @@ #include #include #include +#include using namespace std; @@ -303,6 +305,39 @@ void start_zone(const Args& argv) CommandLineInterface::executeCallback(bind(vsm_start_zone, _1, argv[1].c_str())); } +void console_zone(const Args& argv) +{ + using namespace std::placeholders; + + if (argv.size() <= 1) { + throw runtime_error("Not enough parameters"); + } + + VsmZone zone; + CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, argv[1].c_str(), &zone)); + + if (stringAsInStream(zone->state) != "RUNNING") { + vsm_zone_free(zone); + throw runtime_error("Zone is not running"); + } + + std::string zonesPath = zone->rootfs_path; + std::string zoneRootFs = argv[1] + "/rootfs"; + zonesPath.erase(zonesPath.length()- zoneRootFs.length(), zoneRootFs.length()); + + vsm_zone_free(zone); + + utils::CStringArrayBuilder args; + args.add("lxc-console") + .add("-t 0") + .add("-n").add(argv[1].c_str()) + .add("-P").add(zonesPath.c_str()); + + if (!execv("/usr/bin/lxc-console", const_cast(args.c_array()))) { + throw runtime_error("Could not log into zone"); + } +} + void lock_zone(const Args& argv) { using namespace std::placeholders; diff --git a/cli/command-line-interface.hpp b/cli/command-line-interface.hpp index fe47929..54ea7b9 100644 --- a/cli/command-line-interface.hpp +++ b/cli/command-line-interface.hpp @@ -196,6 +196,12 @@ void shutdown_zone(const Args& argv); void start_zone(const Args& argv); /** + * Parses command line arguments and call lxc-console + * + */ +void console_zone(const Args& argv); + +/** * Parses command line arguments and call vsm_lock_zone * * @see vsm_lock_zone diff --git a/cli/main.cpp b/cli/main.cpp index 7c09df6..c5fe0c1 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -111,6 +111,15 @@ std::map commands = { } }, { + "console_zone", { + console_zone, + "console_zone", + "Log into zone", + MODE_COMMAND_LINE | MODE_INTERACTIVE, + {{"zone_id", "id zone name"}} + } + }, + { "lock_zone", { lock_zone, "lock_zone", -- 2.7.4