1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*
* libvirt-gobject-domain-disk.c: libvirt gobject integration
*
* Copyright (C) 2011 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Marc-André Lureau <marcandre.lureau@redhat.com>
*/
#include <config.h>
#include <libvirt/virterror.h>
#include <string.h>
#include "libvirt-glib/libvirt-glib.h"
#include "libvirt-gobject/libvirt-gobject.h"
#include "libvirt-gobject-compat.h"
#include "libvirt-gobject/libvirt-gobject-domain-device-private.h"
#define GVIR_DOMAIN_DISK_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_DOMAIN_DISK, GVirDomainDiskPrivate))
struct _GVirDomainDiskPrivate
{
gboolean unused;
};
G_DEFINE_TYPE(GVirDomainDisk, gvir_domain_disk, GVIR_TYPE_DOMAIN_DEVICE);
#define GVIR_DOMAIN_DISK_ERROR gvir_domain_disk_error_quark()
static GQuark
gvir_domain_disk_error_quark(void)
{
return g_quark_from_static_string("gvir-domain-disk");
}
static void gvir_domain_disk_finalize(GObject *object)
{
GVirDomainDisk *self = GVIR_DOMAIN_DISK(object);
g_debug("Finalize GVirDomainDisk=%p", self);
G_OBJECT_CLASS(gvir_domain_disk_parent_class)->finalize(object);
}
static void gvir_domain_disk_class_init(GVirDomainDiskClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gvir_domain_disk_finalize;
g_type_class_add_private(klass, sizeof(GVirDomainDiskPrivate));
}
static void gvir_domain_disk_init(GVirDomainDisk *self)
{
g_debug("Init GVirDomainDisk=%p", self);
self->priv = GVIR_DOMAIN_DISK_GET_PRIVATE(self);
}
static GVirDomainDiskStats *
gvir_domain_disk_stats_copy(GVirDomainDiskStats *stats)
{
return g_slice_dup(GVirDomainDiskStats, stats);
}
static void
gvir_domain_disk_stats_free(GVirDomainDiskStats *stats)
{
g_slice_free(GVirDomainDiskStats, stats);
}
G_DEFINE_BOXED_TYPE(GVirDomainDiskStats, gvir_domain_disk_stats,
gvir_domain_disk_stats_copy, gvir_domain_disk_stats_free)
static const gchar *gvir_domain_disk_get_path(GVirDomainDisk *self)
{
GVirConfigDomainDevice *config;
const gchar *path;
config = gvir_domain_device_get_config(GVIR_DOMAIN_DEVICE(self));
path = gvir_config_domain_disk_get_target_dev(GVIR_CONFIG_DOMAIN_DISK(config));
g_object_unref(config);
return path;
}
/**
* gvir_domain_disk_get_stats:
* @self: the domain disk
* @err: an error
*
* This function returns network disk 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.
**/
GVirDomainDiskStats *gvir_domain_disk_get_stats(GVirDomainDisk *self, GError **err)
{
GVirDomainDiskStats *ret = NULL;
virDomainBlockStatsStruct stats;
virDomainPtr handle;
const gchar *path;
g_return_val_if_fail(GVIR_IS_DOMAIN_DISK(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_disk_get_path (self);
if (virDomainBlockStats(handle, path, &stats, sizeof (stats)) < 0) {
gvir_set_error_literal(err, GVIR_DOMAIN_DISK_ERROR,
0,
"Unable to get domain disk stats");
goto end;
}
ret = g_slice_new(GVirDomainDiskStats);
ret->rd_req = stats.rd_req;
ret->rd_bytes = stats.rd_bytes;
ret->wr_req = stats.wr_req;
ret->wr_bytes = stats.wr_bytes;
ret->errs = stats.errs;
end:
virDomainFree(handle);
return ret;
}
/**
* gvir_domain_disk_resize:
* @self: the domain disk
* @size: new size of the block image in kilobytes
* @flags: flags, currently unused. Pass '0'.
* @err: placeholder for an error, or NULL
*
* This function resize the disk of a running domain.
*
* Returns: TRUE if size was successfully changed, FALSE otherwise.
**/
gboolean gvir_domain_disk_resize(GVirDomainDisk *self,
guint64 size,
guint flags,
GError **err)
{
gboolean ret = FALSE;
virDomainPtr handle;
const gchar *path;
g_return_val_if_fail(GVIR_IS_DOMAIN_DISK(self), FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
handle = gvir_domain_device_get_domain_handle(GVIR_DOMAIN_DEVICE(self));
path = gvir_domain_disk_get_path (self);
if (virDomainBlockResize(handle, path, size, flags) < 0) {
gvir_set_error_literal(err, GVIR_DOMAIN_DISK_ERROR,
0,
"Failed to resize domain disk");
goto end;
}
ret = TRUE;
end:
virDomainFree(handle);
return ret;
}
|