Class for managing libvirt containers
authorJan Olszak <j.olszak@samsung.com>
Thu, 20 Feb 2014 10:24:33 +0000 (11:24 +0100)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:14 +0000 (13:47 +0200)
        [Issue#]      PSDAC-64
        [Bug]         N/A
        [Cause]       N/A
        [Solution]    N/A
        [Verfication] Build and install,
                      su; libvirtd
                      su; security-containers-server

Change-Id: I9ecd109cef8312fad0a006080c73b15032b5ff2e

CMakeLists.txt
packaging/security-containers.spec
src/server/include/scs-container.hpp [new file with mode: 0644]
src/server/include/scs-exception.hpp
src/server/include/scs-log.hpp [new file with mode: 0644]
src/server/src/CMakeLists.txt
src/server/src/main.cpp
src/server/src/scs-container.cpp [new file with mode: 0644]

index 2a82c73..429228b 100644 (file)
@@ -20,6 +20,9 @@
 CMAKE_MINIMUM_REQUIRED (VERSION 2.6.2)
 PROJECT(security-containers)
 
+## pkgconfig ###################################################################
+INCLUDE(FindPkgConfig)
+
 ## File names ##################################################################
 SET(SERVER_CODENAME "${PROJECT_NAME}-server")
 SET(CLIENT_CODENAME "${PROJECT_NAME}-client")
index 5ee08bc..611b2b0 100644 (file)
@@ -6,6 +6,8 @@ License:       Apache-2.0
 Group:         Security/Other
 Summary:       Daemon for managing containers
 BuildRequires: cmake
+BuildRequires: libvirt
+BuildRequires: libvirt-devel
 
 %description
 This package provides a daemon used to manage containers - start, stop and switch
diff --git a/src/server/include/scs-container.hpp b/src/server/include/scs-container.hpp
new file mode 100644 (file)
index 0000000..8e03044
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file    scs-container.hpp
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Declaration of the class for managing one container
+ */
+
+
+#ifndef SECURITY_CONTAINERS_SERVER_CONTAINER_HPP
+#define SECURITY_CONTAINERS_SERVER_CONTAINER_HPP
+
+#include <string>
+#include <libvirt/libvirt.h>
+
+namespace security_containers {
+
+class Container {
+
+public:
+    Container();
+    virtual ~Container();
+    void define(const char* configXML = NULL);
+    void undefine();
+    void start();
+    void stop();
+
+private:
+    virConnectPtr mVir = NULL; // pointer to the connection with libvirt
+    virDomainPtr  mDom = NULL; // pointer to the domain
+
+    bool mIsRunning = false; // is the domain now running
+
+    // TODO: This is a temporary sollution.
+    const std::string mDefaultConfigXML = "<domain type=\"lxc\">\
+                                         <name>cnsl</name>\
+                                         <memory>102400</memory>\
+                                         <os>\
+                                         <type>exe</type>\
+                                         <init>/bin/sh</init>\
+                                         </os>\
+                                         <devices>\
+                                         <console type=\"pty\"/>\
+                                         </devices>\
+                                         </domain>";
+    void connect();
+    void disconnect();
+
+};
+}
+#endif // SECURITY_CONTAINERS_SERVER_CONTAINER_HPP
index 169c918..606e3e1 100644 (file)
@@ -1,3 +1,28 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file    scs-exception.hpp
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Exceptions for the server
+ */
+
+
 #ifndef SECURITY_CONTAINERS_SERVER_EXCEPTION_HPP
 #define SECURITY_CONTAINERS_SERVER_EXCEPTION_HPP
 
@@ -13,6 +38,23 @@ struct ServerException: public std::runtime_error {
         std::runtime_error(mess) {};
 };
 
+/**
+ * @brief Error occured during an attempt to connect to libvirt's daemon.
+ */
+struct ConnectionException: public ServerException {
+    ConnectionException(const std::string& mess = "Security Containers Connection Exception"):
+        ServerException(mess) {};
+};
+
+/**
+ * @brief Error occured during an attempt to perform an operation on a domain,
+ * e.g. start, stop a container
+ */
+struct DomainOperationException: public ServerException {
+    DomainOperationException(const std::string& mess = "Security Containers Domain Operation Exception"):
+        ServerException(mess) {};
+};
+
 }
 
 #endif // SECURITY_CONTAINERS_SERVER_EXCEPTION_HPP
diff --git a/src/server/include/scs-log.hpp b/src/server/include/scs-log.hpp
new file mode 100644 (file)
index 0000000..c452ef1
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file    scs-log.hpp
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Logging macros
+ */
+
+
+#ifndef SECURITY_CONTAINERS_SERVER_LOG_HPP
+#define SECURITY_CONTAINERS_SERVER_LOG_HPP
+
+#include <iostream>
+
+#define LOGE(...) std::cerr << "[DS] " << __VA_ARGS__ << std::endl;
+
+#define LOGD(...) std::cout << "[DS] " << __VA_ARGS__ << std::endl;
+
+#define LOGW(...) std::cout << "[DS] " << __VA_ARGS__ << std::endl;
+
+#endif // SECURITY_CONTAINERS_SERVER_LOG_HPP
index 48d96f6..44382e9 100644 (file)
@@ -21,6 +21,14 @@ MESSAGE ("Generating makefile for the Server...")
 FILE(GLOB_RECURSE project_SRCS *.cpp *.cxx *.cc *.C *.c *.h *.hpp)
 MESSAGE("Files: " ${project_SRCS})
 
+## Setup target ################################################################
 SET(SERVER_CODENAME "${PROJECT_NAME}-server")
-ADD_EXECUTABLE(${SERVER_CODENAME} main.cpp)
+ADD_EXECUTABLE(${SERVER_CODENAME} ${project_SRCS} )
+
+## Link libraries ##############################################################
+PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt)
+INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS})
+TARGET_LINK_LIBRARIES(${SERVER_CODENAME} ${SERVER_DEPS_LIBRARIES})
+
+## Install #####################################################################
 INSTALL(TARGETS ${SERVER_CODENAME} DESTINATION bin)
\ No newline at end of file
index 243fa6e..2ac68b7 100644 (file)
 
 
 #include <iostream>
-#include <getopt.h>             // For getopt
+#include <getopt.h>  // For getopt
+#include <unistd.h>  // For sleep
 
-int main(int argc, char *argv[])
+#include <scs-container.hpp>  // TODO: Delete
+
+
+using namespace security_containers;
+
+// TODO: Delete when unit tests introduced.
+void test()
+{
+    Container c;
+    c.define();
+    c.start();
+    sleep(1);
+    c.start();
+    sleep(1);
+    c.stop();
+    sleep(1);
+    c.start();
+    sleep(1);
+    c.stop();
+    sleep(1);
+    c.undefine();
+}
+
+
+int main(int argc, char* argv[])
 {
     int optIndex = 0;
 
@@ -84,4 +109,6 @@ int main(int argc, char *argv[])
         std::cerr << std::endl;
         return 1;
     }
+
+    test();
 }
\ No newline at end of file
diff --git a/src/server/src/scs-container.cpp b/src/server/src/scs-container.cpp
new file mode 100644 (file)
index 0000000..d17e48f
--- /dev/null
@@ -0,0 +1,155 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file    scs-container.cpp
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Implementation of class for managing one container
+ */
+
+#include <assert.h>
+
+#include <scs-container.hpp>
+#include <scs-exception.hpp>
+#include <scs-log.hpp>
+
+using namespace security_containers;
+
+Container::Container()
+{
+    connect();
+}
+
+
+Container::~Container()
+{
+    disconnect();
+}
+
+
+void
+Container::connect()
+{
+    assert(mVir == NULL);
+
+    mVir = virConnectOpen("lxc://");
+    if (mVir == NULL) {
+        LOGE("Failed to open connection to lxc://");
+        throw ConnectionException();
+    }
+};
+
+
+void
+Container::disconnect()
+{
+    if (mVir == NULL) {
+        return;
+    }
+
+    if (virConnectClose(mVir) < 0) {
+        LOGE("Error during unconnecting from libvirt");
+    };
+    mVir = NULL;
+};
+
+
+void
+Container::start()
+{
+    assert(mVir != NULL);
+    assert(mDom != NULL);
+
+    if (mIsRunning) {
+        return;
+    }
+
+    // Autodestroyed when connection pointer released
+    // Any managed save file for this domain is discarded,
+    // and the domain boots from scratch
+    u_int flags = VIR_DOMAIN_START_AUTODESTROY;
+
+    if (virDomainCreateWithFlags(mDom, flags) < 0) {
+        LOGE("Failed to start the container");
+        throw DomainOperationException();
+    }
+
+    mIsRunning = true;
+};
+
+
+void
+Container::stop()
+{
+    assert(mVir != NULL);
+    assert(mDom != NULL);
+
+    if (!mIsRunning) {
+        return;
+    }
+    // Forceful termination of the guest
+    u_int flags = VIR_DOMAIN_DESTROY_DEFAULT;
+
+    if (virDomainDestroyFlags(mDom, flags) < 0) {
+        LOGE("Error during domain stopping");
+        throw DomainOperationException();
+    }
+
+    mIsRunning = false;
+};
+
+
+void
+Container::define(const char* configXML)
+{
+    assert(mVir != NULL);
+
+    if (configXML) {
+        mDom = virDomainDefineXML(mVir, configXML);
+    } else {
+        mDom = virDomainDefineXML(mVir, mDefaultConfigXML.c_str());
+    }
+
+    if (mDom == NULL) {
+        LOGE("Error during domain defining");
+        throw DomainOperationException();
+    }
+};
+
+
+void
+Container::undefine()
+{
+    assert(mVir != NULL);
+    assert(mDom != NULL);
+
+    stop();
+
+    // Remove domain configuration
+    if (virDomainUndefine(mDom) < 0) {
+        LOGE("Error during domain undefine");
+        throw DomainOperationException();
+    }
+
+    if (virDomainFree(mDom) < 0) {
+        LOGE("Error during domain destruction");
+        throw DomainOperationException();
+    }
+
+    mDom = NULL;
+};