[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] Allow passing of command line args to LXC container

From: "Daniel P. Berrange" <berrange redhat com>

When booting a virtual machine with a kernel/initrd it is possible
to pass command line arguments using the <cmdline>...args...</cmdline>
element in the guest XML. These appear to the kernel / init process
in /proc/cmdline.

When booting a container we do not have a custom /proc/cmdline,
but we can easily set an environment variable for it. Ideally
we could pass individual arguments to the init process as a
regular set of 'char *argv[]' parameters, but that would involve
libvirt parsing the <cmdline> XML text. This can easily be added
later, even if we add the env variable now

* docs/drvlxc.html.in: Document env variables passed to LXC
* src/conf/domain_conf.c: Add <cmdline> to be parsed for
  guests of type='exe'
* src/lxc/lxc_container.c: Set LIBVIRT_LXC_CMDLINE env var
 docs/drvlxc.html.in     |   17 +++++++++++++++++
 src/conf/domain_conf.c  |    1 +
 src/lxc/lxc_container.c |    2 ++
 3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/docs/drvlxc.html.in b/docs/drvlxc.html.in
index 5ce218d..47837d1 100644
--- a/docs/drvlxc.html.in
+++ b/docs/drvlxc.html.in
@@ -39,6 +39,23 @@ driver. On such kernels, it may be neccessary to unmount the blkio controller.
+<h2>Environment setup for the container init</h2>
+When the container "init" process is started, it will be given several useful
+environment variables.
+<dd>The name assigned to the container by libvirt</dd>
+<dd>The UUID assigned to the container by libvirt</dd>
+<dd>The unparsed command line arguments specified in the container configuration</dd>
 <h3>Example config version 1</h3>
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6fb1888..0f751ca 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6833,6 +6833,7 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
                 goto error;
+        def->os.cmdline = virXPathString("string(./os/cmdline[1])", ctxt);
     if (STREQ(def->os.type, "xen") ||
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 787df9a..69cea8e 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -116,6 +116,8 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
     virCommandAddEnvString(cmd, "TERM=linux");
     virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr);
     virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
+    if (vmDef->os.cmdline)
+        virCommandAddEnvPair(cmd, "LIBVIRT_LXC_CMDLINE", vmDef->os.cmdline);
     return cmd;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]