/**
 * gvir_storage_vol_upload:
 * @vol: the storage volume to upload
 * @stream: stream to use as input
 * @offset: position in @vol to start to write to
 * @length: limit on amount of data to upload, or 0 for uploading all data
 * @flags: the flags, not set yet, pass 0
 *
 * Returns: #TRUE of success, #FALSE otherwise
 */
gboolean gvir_storage_vol_upload(GVirStorageVol *vol,
                                 GVirStream *stream,
                                 guint64 offset,
                                 guint64 length,
                                 guint flags G_GNUC_UNUSED,
                                 GError **err)
{
    virStreamPtr stream_handle = NULL;
    gboolean ret = FALSE;

    g_object_get(stream, "handle", &stream_handle, NULL);

    g_return_val_if_fail(GVIR_IS_STORAGE_VOL(vol), FALSE);
    g_return_val_if_fail(GVIR_IS_STREAM(stream), FALSE);
    g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

    if (virStorageVolUpload(vol->priv->handle,
                            stream_handle,
                            offset,
                            length,
                            0) < 0) {
        gvir_set_error_literal(err,
                               GVIR_STORAGE_VOL_ERROR,
                               0,
                               "Unable to upload to stream");

        goto cleanup;
    }

    ret = TRUE;
cleanup:
    if (stream_handle != NULL)
        virStreamFree(stream_handle);
    return ret;
}
/**
 * gvir_storage_pool_create_volume:
 * @pool: the storage pool in which to create the volume
 * @conf: the configuration for the new volume
 * @err: Place-holder for possible errors
 *
 * Returns: (transfer full): the newly created volume. The returned object
 * should be unreffed with g_object_unref() when no longer needed.
 */
GVirStorageVol *gvir_storage_pool_create_volume
                                (GVirStoragePool *pool,
                                 GVirConfigStorageVol *conf,
                                 GError **err)
{
    const gchar *xml;
    virStorageVolPtr handle;
    GVirStoragePoolPrivate *priv;

    g_return_val_if_fail(GVIR_IS_STORAGE_POOL(pool), NULL);
    g_return_val_if_fail(GVIR_CONFIG_IS_STORAGE_VOL(conf), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(conf));

    g_return_val_if_fail(xml != NULL, NULL);

    priv = pool->priv;
    if (!(handle = virStorageVolCreateXML(priv->handle, xml, 0))) {
        gvir_set_error_literal(err, GVIR_STORAGE_POOL_ERROR,
                               0,
                               "Failed to create volume");
        return NULL;
    }

    GVirStorageVol *volume;
    const char *name;

    volume = GVIR_STORAGE_VOL(g_object_new(GVIR_TYPE_STORAGE_VOL,
                                           "handle", handle,
                                           "pool", pool,
                                           NULL));
    name = gvir_storage_vol_get_name(volume);
    if (name == NULL) {
        g_object_unref(G_OBJECT(volume));
        return NULL;
    }

    g_mutex_lock(priv->lock);
    if (priv->volumes != NULL) {
        g_hash_table_insert(priv->volumes, g_strdup(name), volume);
    } else {
        g_warn_if_reached();
        g_object_unref(G_OBJECT(volume));
        g_mutex_unlock(priv->lock);
        return NULL;
    }
    g_mutex_unlock(priv->lock);

    return g_object_ref(volume);
}
/**
 * gvir_storage_pool_build:
 * @pool: the storage pool to build
 * @flags:  the flags
 * @err: return location for any #GError
 *
 * Return value: #True on success, #False otherwise.
 */
gboolean gvir_storage_pool_build (GVirStoragePool *pool,
                                  guint flags,
                                  GError **err)
{
    g_return_val_if_fail(GVIR_IS_STORAGE_POOL(pool), FALSE);
    g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

    if (virStoragePoolBuild(pool->priv->handle, flags)) {
        gvir_set_error_literal(err, GVIR_STORAGE_POOL_ERROR,
                               0,
                               "Failed to build storage pool");
        return FALSE;
    }

    return TRUE;
}
/**
 * gvir_storage_vol_resize:
 * @vol: the storage volume to resize
 * @capacity: the new capacity of the volume
 * @flags: (type GVirStorageVolResizeFlags): the flags
 * @err: Return location for errors, or NULL
 *
 * Changes the capacity of the storage volume @vol to @capacity.
 *
 * Returns: #TRUE success, #FALSE otherwise
 */
gboolean gvir_storage_vol_resize(GVirStorageVol *vol,
                                 guint64 capacity,
                                 guint flags,
                                 GError **err)
{
    g_return_val_if_fail(GVIR_IS_STORAGE_VOL(vol), FALSE);
    g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

    if (virStorageVolResize(vol->priv->handle, capacity, flags) < 0) {
        gvir_set_error_literal(err,
                               GVIR_STORAGE_VOL_ERROR,
                               0,
                               "Unable to resize volume storage");
        return FALSE;
    }

    return TRUE;
}
/**
 * gvir_storage_vol_delete:
 * @vol: the storage volume to delete
 * @flags: the flags
 * @err: Return location for errors, or NULL
 *
 * Deletes the storage volume @vol.
 *
 * Returns: %TRUE on success, %FALSE otherwise
 */
gboolean gvir_storage_vol_delete(GVirStorageVol *vol,
                                 guint flags,
                                 GError **err)
{
    g_return_val_if_fail(GVIR_IS_STORAGE_VOL(vol), FALSE);
    g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

    gvir_storage_pool_delete_vol(vol->priv->pool, vol);

    if (virStorageVolDelete(vol->priv->handle, flags) < 0) {
        gvir_set_error_literal(err,
                               GVIR_STORAGE_VOL_ERROR,
                               0,
                               "Unable to delete storage volume");
        return FALSE;
    }

    return TRUE;
}
示例#6
0
/**
 * gvir_domain_interface_get_stats:
 * @self: the domain interface
 * @err: an error
 *
 * This function returns network interface stats. Individual fields
 * within the stats structure may be returned as -1, which indicates
 * that the hypervisor does not support that particular statistic.
 *
 * Returns: (transfer full): the stats or %NULL in case of error. The
 * returned object should be unreffed with g_object_unref() when no longer
 * needed.
 **/
GVirDomainInterfaceStats *gvir_domain_interface_get_stats(GVirDomainInterface *self, GError **err)
{
    GVirDomainInterfaceStats *ret = NULL;
    virDomainInterfaceStatsStruct stats;
    virDomainPtr handle;
    const gchar *path;

    g_return_val_if_fail(GVIR_IS_DOMAIN_INTERFACE(self), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    handle = gvir_domain_device_get_domain_handle(GVIR_DOMAIN_DEVICE(self));
    path = gvir_domain_interface_get_path (self);
    if (path == NULL) {
        ret = g_slice_new0(GVirDomainInterfaceStats);
        goto end;
    }

    if (virDomainInterfaceStats(handle, path, &stats, sizeof (stats)) < 0) {
        gvir_set_error_literal(err, GVIR_DOMAIN_INTERFACE_ERROR,
                               0,
                               "Unable to get domain interface stats");
        goto end;
    }

    ret = g_slice_new(GVirDomainInterfaceStats);
    ret->rx_bytes = stats.rx_bytes;
    ret->rx_packets = stats.rx_packets;
    ret->rx_errs = stats.rx_errs;
    ret->rx_drop = stats.rx_drop;
    ret->tx_bytes = stats.tx_bytes;
    ret->tx_packets = stats.tx_packets;
    ret->tx_errs = stats.tx_errs;
    ret->tx_drop = stats.tx_drop;

end:
    virDomainFree(handle);
    return ret;
}
/**
 * gvir_storage_pool_get_config:
 * @pool: the storage_pool
 * @flags: the flags
 * @err: Place-holder for possible errors
 *
 * Returns: (transfer full): the config. The returned object should be
 * unreffed with g_object_unref() when no longer needed.
 */
GVirConfigStoragePool *gvir_storage_pool_get_config(GVirStoragePool *pool,
                                                    guint flags,
                                                    GError **err)
{
    GVirStoragePoolPrivate *priv;
    gchar *xml;

    g_return_val_if_fail(GVIR_IS_STORAGE_POOL(pool), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    priv = pool->priv;
    if (!(xml = virStoragePoolGetXMLDesc(priv->handle, flags))) {
        gvir_set_error_literal(err, GVIR_STORAGE_POOL_ERROR,
                               0,
                               "Unable to get storage_pool XML config");
        return NULL;
    }

    GVirConfigStoragePool *conf = gvir_config_storage_pool_new_from_xml(xml, err);

    free(xml);
    return conf;
}
示例#8
0
/**
 * gvir_network_get_config:
 * @network: the network
 * @flags: the flags
 * @err: Place-holder for possible errors
 *
 * Returns: (transfer full): the config. The returned object should be
 * unreffed with g_object_unref() when no longer needed.
 */
GVirConfigNetwork *gvir_network_get_config(GVirNetwork *network,
                                           guint flags,
                                           GError **err)
{
    GVirNetworkPrivate *priv;
    gchar *xml;

    g_return_val_if_fail(GVIR_IS_NETWORK(network), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    priv = network->priv;
    if (!(xml = virNetworkGetXMLDesc(priv->handle, flags))) {
        gvir_set_error_literal(err, GVIR_NETWORK_ERROR,
                               0,
                               "Unable to get network XML config");
        return NULL;
    }

    GVirConfigNetwork *conf = gvir_config_network_new_from_xml(xml, err);

    free(xml);
    return conf;
}
/**
 * gvir_node_device_get_config:
 * @device: the node_device
 * @flags: the flags
 * @err: Place-holder for possible errors
 *
 * Returns: (transfer full): the config. The returned object should be
 * unreffed with g_object_unref() when no longer needed.

 */
GVirConfigNodeDevice *gvir_node_device_get_config(GVirNodeDevice *device,
                                                  guint flags,
                                                  GError **err)
{
    GVirNodeDevicePrivate *priv;
    gchar *xml;

    g_return_val_if_fail(GVIR_IS_NODE_DEVICE(device), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    priv = device->priv;
    if (!(xml = virNodeDeviceGetXMLDesc(priv->handle, flags))) {
        gvir_set_error_literal(err, GVIR_NODE_DEVICE_ERROR,
                               0,
                               "Unable to get node_device XML config");
        return NULL;
    }

    GVirConfigNodeDevice *conf = gvir_config_node_device_new_from_xml(xml, err);

    free(xml);
    return conf;
}
/**
 * gvir_domain_snapshot_get_config:
 * @snapshot: the domain_snapshot
 * @flags: the flags
 *
 * Returns: (transfer full): the config. The returned object should be
 * unreffed with g_object_unref() when no longer needed.

 */
GVirConfigDomainSnapshot *gvir_domain_snapshot_get_config
                                (GVirDomainSnapshot *snapshot,
                                 guint flags,
                                 GError **err)
{
    GVirDomainSnapshotPrivate *priv;
    gchar *xml;

    g_return_val_if_fail(GVIR_IS_DOMAIN_SNAPSHOT(snapshot), NULL);
    g_return_val_if_fail(err == NULL || *err == NULL, NULL);

    priv = snapshot->priv;
    if (!(xml = virDomainSnapshotGetXMLDesc(priv->handle, flags))) {
        gvir_set_error_literal(err, GVIR_DOMAIN_SNAPSHOT_ERROR,
                               0,
                               "Unable to get domain_snapshot XML config");
        return NULL;
    }

    GVirConfigDomainSnapshot *conf = gvir_config_domain_snapshot_new_from_xml(xml, err);

    free(xml);
    return conf;
}
/**
 * gvir_storage_pool_refresh:
 * @pool: the storage pool
 * @cancellable: (allow-none)(transfer none): cancellation object
 */
gboolean gvir_storage_pool_refresh(GVirStoragePool *pool,
                                   GCancellable *cancellable,
                                   GError **err)
{
    GVirStoragePoolPrivate *priv;
    GHashTable *vol_hash;
    gchar **volumes = NULL;
    gint nvolumes = 0;
    gboolean ret = FALSE;
    gint i;
    virStoragePoolPtr vpool = NULL;
    GError *lerr = NULL;

    g_return_val_if_fail(GVIR_IS_STORAGE_POOL(pool), FALSE);
    g_return_val_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable),
                         FALSE);
    g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

    priv = pool->priv;
    vpool = priv->handle;

    if (virStoragePoolRefresh(vpool, 0) < 0) {
        gvir_set_error_literal(err, GVIR_STORAGE_POOL_ERROR,
                               0,
                               "Unable to refresh storage pool");
        goto cleanup;
    }

    volumes = fetch_list(vpool,
                         "Storage Volumes",
                         virStoragePoolNumOfVolumes,
                         virStoragePoolListVolumes,
                         cancellable,
                         &nvolumes,
                         &lerr);
    if (lerr) {
        g_propagate_error(err, lerr);
        lerr = NULL;
        goto cleanup;
    }

    if (g_cancellable_set_error_if_cancelled(cancellable, err))
        goto cleanup;

    vol_hash = g_hash_table_new_full(g_str_hash,
                                     g_str_equal,
                                     g_free,
                                     g_object_unref);

    for (i = 0 ; i < nvolumes ; i++) {
        if (g_cancellable_set_error_if_cancelled(cancellable, err))
            goto cleanup;

        virStorageVolPtr vvolume;
        GVirStorageVol *volume;

        vvolume = virStorageVolLookupByName(vpool, volumes[i]);
        if (!vvolume)
            continue;

        volume = GVIR_STORAGE_VOL(g_object_new(GVIR_TYPE_STORAGE_VOL,
                                               "handle", vvolume,
                                               "pool", pool,
                                               NULL));
        virStorageVolFree(vvolume);
        g_hash_table_insert(vol_hash, g_strdup(volumes[i]), volume);
    }

    g_mutex_lock(priv->lock);
    if (priv->volumes)
        g_hash_table_unref(priv->volumes);
    priv->volumes = vol_hash;
    g_mutex_unlock(priv->lock);

    ret = TRUE;

cleanup:
    for (i = 0 ; i < nvolumes ; i++)
        g_free(volumes[i]);
    g_free(volumes);
    return ret;
}