libvirt/tests/lxcxml2xmltest.c
Daniel P. Berrange aae0fc2a92 Add support for <hostdev mode="capabilities">
The <hostdev> device type has long had a redundant "mode"
attribute, which has always been "subsys". This finally
introduces a new mode "capabilities", which will be used
by the LXC driver for device assignment. Since container
based virtualization uses a single kernel, the idea of
assigning physical PCI devices doesn't make sense. It is
still reasonable to assign USB devices, but for assigning
arbitrary nodes in /dev, the new 'capabilities' mode is
to be used.

The first capability support is 'storage', which is for
assignment of block devices. Functionally this is really
pretty similar to the <disk> support. The only difference
is the device node name is identical in both host and
container namespaces.

    <hostdev mode='capabilities' type='storage'>
      <source>
        <block>/dev/sdf1</block>
      </source>
    </hostdev>

The second capability support is 'misc', which is for
assignment of character devices. There is no existing
parallel to this. Again the device node is the same
inside & outside the container.

    <hostdev mode='capabilities' type='misc'>
      <source>
        <char>/dev/input/event3</char>
      </source>
    </hostdev>

The reason for keeping the char & storage devices
separate in the domain XML, is to mirror the split
in the node device XML. NB the node device XML does
not yet report character devices, but that's another
new patch to come

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2012-12-17 17:50:50 +00:00

143 lines
3.3 KiB
C

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef WITH_LXC
# include "internal.h"
# include "testutils.h"
# include "lxc/lxc_conf.h"
# include "testutilslxc.h"
static virCapsPtr caps;
static int
testCompareXMLToXMLFiles(const char *inxml, const char *outxml, bool live)
{
char *inXmlData = NULL;
char *outXmlData = NULL;
char *actual = NULL;
int ret = -1;
virDomainDefPtr def = NULL;
if (virtTestLoadFile(inxml, &inXmlData) < 0)
goto fail;
if (virtTestLoadFile(outxml, &outXmlData) < 0)
goto fail;
if (!(def = virDomainDefParseString(caps, inXmlData,
1 << VIR_DOMAIN_VIRT_LXC,
live ? 0 : VIR_DOMAIN_XML_INACTIVE)))
goto fail;
if (!(actual = virDomainDefFormat(def, VIR_DOMAIN_XML_SECURE)))
goto fail;
if (STRNEQ(outXmlData, actual)) {
virtTestDifference(stderr, outXmlData, actual);
goto fail;
}
ret = 0;
fail:
VIR_FREE(inXmlData);
VIR_FREE(outXmlData);
VIR_FREE(actual);
virDomainDefFree(def);
return ret;
}
struct testInfo {
const char *name;
int different;
bool inactive_only;
};
static int
testCompareXMLToXMLHelper(const void *data)
{
const struct testInfo *info = data;
char *xml_in = NULL;
char *xml_out = NULL;
int ret = -1;
if (virAsprintf(&xml_in, "%s/lxcxml2xmldata/lxc-%s.xml",
abs_srcdir, info->name) < 0 ||
virAsprintf(&xml_out, "%s/lxcxml2xmloutdata/lxc-%s.xml",
abs_srcdir, info->name) < 0)
goto cleanup;
if (info->different) {
ret = testCompareXMLToXMLFiles(xml_in, xml_out, false);
} else {
ret = testCompareXMLToXMLFiles(xml_in, xml_in, false);
}
if (!info->inactive_only) {
if (info->different) {
ret = testCompareXMLToXMLFiles(xml_in, xml_out, true);
} else {
ret = testCompareXMLToXMLFiles(xml_in, xml_in, true);
}
}
cleanup:
VIR_FREE(xml_in);
VIR_FREE(xml_out);
return ret;
}
static int
mymain(void)
{
int ret = 0;
if ((caps = testLXCCapsInit()) == NULL)
return EXIT_FAILURE;
# define DO_TEST_FULL(name, is_different, inactive) \
do { \
const struct testInfo info = {name, is_different, inactive}; \
if (virtTestRun("LXC XML-2-XML " name, \
1, testCompareXMLToXMLHelper, &info) < 0) \
ret = -1; \
} while (0)
# define DO_TEST(name) \
DO_TEST_FULL(name, 0, false)
# define DO_TEST_DIFFERENT(name) \
DO_TEST_FULL(name, 1, false)
/* Unset or set all envvars here that are copied in lxcdBuildCommandLine
* using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
* values for these envvars */
setenv("PATH", "/bin", 1);
DO_TEST("systemd");
DO_TEST("hostdev");
virCapabilitiesFree(caps);
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIRT_TEST_MAIN(mymain)
#else
# include "testutils.h"
int
main(void)
{
return EXIT_AM_SKIP;
}
#endif /* WITH_LXC */