Add console_zone to cli 59/43859/6
authorDariusz Michaluk <d.michaluk@samsung.com>
Tue, 23 Jun 2015 15:11:13 +0000 (17:11 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Fri, 17 Jul 2015 09:42:44 +0000 (11:42 +0200)
[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
cli/command-line-interface.cpp
cli/command-line-interface.hpp
cli/main.cpp

index 36750f0..fe4b067 100644 (file)
@@ -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")
index 3b100eb..cdd64b4 100644 (file)
@@ -25,6 +25,7 @@
 #include "config.hpp"
 #include "command-line-interface.hpp"
 #include "vasum-client.h"
+#include "utils/c-array.hpp"
 
 #include <map>
 #include <stdexcept>
@@ -39,6 +40,7 @@
 #include <cassert>
 #include <linux/if_link.h>
 #include <arpa/inet.h>
+#include <unistd.h>
 
 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<char* const*>(args.c_array()))) {
+        throw runtime_error("Could not log into zone");
+    }
+}
+
 void lock_zone(const Args& argv)
 {
     using namespace std::placeholders;
index fe47929..54ea7b9 100644 (file)
@@ -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
index 7c09df6..c5fe0c1 100644 (file)
@@ -111,6 +111,15 @@ std::map<std::string, CommandLineInterface> 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",