25 #include <lxc/lxccontainer.h> 27 #include "filetoolkitwithundo.h" 29 #include "softwarecontainer-common.h" 30 #include "containerabstractinterface.h" 45 LOG_DECLARE_CLASS_CONTEXT(
"CONT",
"Container");
47 static constexpr
const char *GATEWAYS_PATH =
"/gateways";
49 enum class LXCContainerState
51 STOPPED, STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN, THAWED, ELEMENT_COUNT
54 static const char *toString(LXCContainerState state)
56 return s_LXCContainerStates[
static_cast<int>(state)];
59 static std::vector<const char *> s_LXCContainerStates;
60 static const char *s_LXCRoot;
67 static void init_lxc();
82 const std::string &configFile,
83 const std::string &containerRoot,
84 bool writeBufferEnabled =
false,
85 int shutdownTimeout = 1);
100 bool start(pid_t *pid);
102 bool setCgroupItem(std::string subsys, std::string value);
109 bool execute(
const std::string &commandLine,
111 const EnvironmentVariables &variables,
112 const std::string &workingDirectory =
"/",
122 bool execute(ExecFunction
function,
124 const EnvironmentVariables &variables = EnvironmentVariables(),
134 const EnvironmentVariables &variables = EnvironmentVariables(),
151 const std::string &pathInContainer,
152 bool readOnly =
true);
155 bool mountDevice(
const std::string &pathInHost);
161 bool destroy(
unsigned int timeout);
167 bool shutdown(
unsigned int timeout);
196 bool waitForState(LXCContainerState state,
int timeout = 20);
197 bool ensureContainerRunning();
208 std::string toString();
210 const char *id()
const;
211 std::string gatewaysDirInContainer()
const;
212 std::string gatewaysDir()
const;
214 bool setEnvironmentVariable(
const std::string &var,
const std::string &val);
222 static int unlimitCoreDump();
228 static int executeInContainerEntryFunction(
void *param);
233 bool bindMountCore(
const std::string &pathInHost,
234 const std::string &pathInContainer,
235 const std::string &tempDir,
238 bool remountReadOnlyInContainer(
const std::string &path);
243 bool rollbackCreate();
248 std::string m_configFile;
253 const std::string m_id;
255 std::string m_rootFSPath;
260 struct lxc_container *m_container =
nullptr;
262 std::string m_containerRoot;
264 bool m_writeBufferEnabled;
267 EnvironmentVariables m_gatewayEnvironmentVariables;
269 int m_shutdownTimeout = 1;
271 enum class ContainerState : unsigned int {
279 ContainerState m_state = ContainerState::DEFAULT;
bool create()
create Creates a new lxc_container and creates it with all the initialization.
Container(const std::string id, const std::string &configFile, const std::string &containerRoot, bool writeBufferEnabled=false, int shutdownTimeout=1)
Constructor.
bool execute(const std::string &commandLine, pid_t *pid, const EnvironmentVariables &variables, const std::string &workingDirectory="/", int stdin=-1, int stdout=1, int stderr=2)
Start a process from the given command line, with an environment consisting of the variables previous...
The Container class is an abstraction of the specific containment technology used.
bool stop()
Calls stop on the lxc container(force stop)
bool executeSync(ExecFunction function, pid_t *pid, const EnvironmentVariables &variables=EnvironmentVariables(), int stdin=-1, int stdout=1, int stderr=2)
synchronous version of execute
bool shutdown()
Calls shutdown on the lxc container.
bool bindMountInContainer(const std::string &pathInHost, const std::string &pathInContainer, bool readOnly=true)
Tries to bind mount a path from host to container.
bool start(pid_t *pid)
Start the container.
bool initialize()
Setup the container for startup.
bool destroy()
Calls shutdown, and then destroys the container.
Developers guide to adding a config item: