Re: [PATCHv2 3/6] domain_conf: Add helpers to verify if device configuration is valid

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

 



On Thu, Jul 12, 2012 at 11:30:57AM +0200, Peter Krempa wrote:
> This patch adds helpers that validate domain's device configuration.
> This will be needed later on to verify devices being hot-plugged to
> guests. If the guest has no USB bus, then it's not valid to plug a USB
> device to that guest.
> ---
>  src/conf/domain_conf.c   |   43 +++++++++++++++++++++++++++++++++++++++++++
>  src/conf/domain_conf.h   |    5 +++++
>  src/libvirt_private.syms |    2 ++
>  3 files changed, 50 insertions(+), 0 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index e38cdfe..cb56f50 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -13263,6 +13263,49 @@ error:
>      return NULL;
>  }
> 
> +bool
> +virDomainDefHasUSB(virDomainDefPtr def)
> +{
> +    int i;
> +
> +    for (i = 0; i < def->ncontrollers; i++) {
> +        if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
> +            def->controllers[i]->model != VIR_DOMAIN_CONTROLLER_MODEL_USB_NONE)
> +            return true;
> +    }
> +
> +    return false;
> +}
> +
> +int
> +virDomainDefCompatibleDevice(virDomainDefPtr def,
> +                             virDomainDeviceDefPtr dev)
> +{
> +    int t = dev->type;
> +    if (!virDomainDefHasUSB(def) &&
> +        ((t == VIR_DOMAIN_DEVICE_DISK &&
> +          dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_USB) ||
> +         (t == VIR_DOMAIN_DEVICE_CONTROLLER &&
> +          dev->data.controller->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) ||
> +         (t == VIR_DOMAIN_DEVICE_INPUT &&
> +          dev->data.input->type == VIR_DOMAIN_INPUT_BUS_USB) ||
> +         (t == VIR_DOMAIN_DEVICE_HOSTDEV &&
> +          dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
> +          dev->data.hostdev->source.subsys.type ==
> +          VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) ||
> +         (t == VIR_DOMAIN_DEVICE_HUB &&
> +          dev->data.hub->type == VIR_DOMAIN_HUB_TYPE_USB) ||
> +         (t == VIR_DOMAIN_DEVICE_REDIRDEV &&
> +          dev->data.redirdev->bus == VIR_DOMAIN_REDIRDEV_BUS_USB))) {

This big check is a little hairy. I suggest you create a

  bool virDomainDeviceDefIsUSB(virDomainDeviceDefPtr dev)
  {
    ....
  }

then you can just do

  if (virDomainDeviceDefIsUSB(dev) &&
      !virDomainDefHasUSB(dev))

> +        virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> +                             _("Device configuration is not compatible: "
> +                               "Domain has no USB bus support"));
> +        return -1;
> +    }
> +
> +    return 0;
> +}
> +
>  int virDomainSaveXML(const char *configDir,
>                       virDomainDefPtr def,
>                       const char *xml)
> diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
> index d02695d..557bffa 100644
> --- a/src/conf/domain_conf.h
> +++ b/src/conf/domain_conf.h
> @@ -1971,6 +1971,11 @@ int virDomainDefFormatInternal(virDomainDefPtr def,
>                                 unsigned int flags,
>                                 virBufferPtr buf);
> 
> +bool virDomainDefHasUSB(virDomainDefPtr def);
> +
> +int virDomainDefCompatibleDevice(virDomainDefPtr def,
> +                                 virDomainDeviceDefPtr dev);
> +
>  int virDomainCpuSetParse(const char *str,
>                           char sep,
>                           char *cpuset,
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index b173590..6d748b4 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -278,9 +278,11 @@ virDomainDefAddImplicitControllers;
>  virDomainDefCheckABIStability;
>  virDomainDefClearDeviceAliases;
>  virDomainDefClearPCIAddresses;
> +virDomainDefCompatibleDevice;
>  virDomainDefFormat;
>  virDomainDefFormatInternal;
>  virDomainDefFree;
> +virDomainDefHasUSB;
>  virDomainDefParseFile;
>  virDomainDefParseNode;
>  virDomainDefParseString;


Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]