Commit caaaa583 authored by Daniel P. Berrange's avatar Daniel P. Berrange

Expose libvirt constants as perl constants. Change get_uuid to return raw...

Expose libvirt constants as perl constants. Change get_uuid to return raw string. Add get_uuid_string to return a printable version. Add get_node_info to Sys::Virt
parent b1cea72f
......@@ -120,6 +120,29 @@ get_type(con)
OUTPUT:
RETVAL
HV *
get_node_info(con)
virConnectPtr con;
PREINIT:
virNodeInfo info;
CODE:
if (virNodeGetInfo(con, &info) < 0) {
_croak_error(virGetLastError());
}
RETVAL = newHV();
hv_store (RETVAL, "model", 5, newSVpv(info.model, 0), 0);
hv_store (RETVAL, "memory", 6, newSViv(info.memory), 0);
hv_store (RETVAL, "cpus", 4, newSViv(info.cpus), 0);
hv_store (RETVAL, "mhz", 3, newSViv(info.mhz), 0);
hv_store (RETVAL, "nodes", 5, newSViv(info.nodes), 0);
hv_store (RETVAL, "sockets", 7, newSViv(info.sockets), 0);
hv_store (RETVAL, "cores", 5, newSViv(info.cores), 0);
hv_store (RETVAL, "threads", 7, newSViv(info.threads), 0);
OUTPUT:
RETVAL
AV *
_list_domain_ids(con)
virConnectPtr con;
......@@ -210,6 +233,19 @@ get_id(dom)
SV *
get_uuid(dom)
virDomainPtr dom;
PREINIT:
unsigned char rawuuid[16];
CODE:
if ((virDomainGetUUID(dom, rawuuid)) < 0) {
_croak_error(virGetLastError());
}
RETVAL = newSVpv((char*)rawuuid, 16);
OUTPUT:
RETVAL
SV *
get_uuid_string(dom)
virDomainPtr dom;
PREINIT:
unsigned char rawuuid[16];
char uuid[36];
......@@ -220,7 +256,6 @@ get_uuid(dom)
_croak_error(virGetLastError());
}
for (i = 0, j = 0 ; i < 16 ; i++) {
printf ("%x\n", rawuuid[i]);
uuid[j++] = hex[((rawuuid[i] >> 4) & 0xf)];
uuid[j++] = hex[(rawuuid[i] & 0xf)];
if (i == 3 || i == 5 || i == 7 || i == 9) {
......@@ -275,40 +310,16 @@ get_info(dom)
virDomainPtr dom;
PREINIT:
virDomainInfo info;
char *state;
CODE:
if (virDomainGetInfo(dom, &info) < 0) {
_croak_error(virGetLastError());
}
switch (info.state) {
case VIR_DOMAIN_RUNNING:
state = "running";
break;
case VIR_DOMAIN_BLOCKED:
state = "blocked";
break;
case VIR_DOMAIN_PAUSED:
state = "paused";
break;
case VIR_DOMAIN_SHUTDOWN:
state = "shutdown";
break;
case VIR_DOMAIN_SHUTOFF:
state = "shutoff";
break;
case VIR_DOMAIN_CRASHED:
state = "crashed";
break;
case VIR_DOMAIN_NOSTATE:
default:
state = "unknown";
break;
}
RETVAL = newHV();
hv_store (RETVAL, "state", 5, newSVpv(state, 0), 0);
hv_store (RETVAL, "state", 5, newSViv(info.state), 0);
hv_store (RETVAL, "maxMem", 6, newSViv(info.maxMem), 0);
hv_store (RETVAL, "memory", 6, newSViv(info.memory), 0);
hv_store (RETVAL, "nrVirtCpu", 9, newSViv(info.nrVirtCpu), 0);
hv_store (RETVAL, "cpuTime", 7, newSViv(info.cpuTime), 0);
OUTPUT:
RETVAL
......@@ -369,6 +380,15 @@ shutdown(dom)
_croak_error(virGetLastError());
}
void
reboot(dom, flags)
virDomainPtr dom;
unsigned int flags;
PPCODE:
if (!virDomainReboot(dom, flags)) {
_croak_error(virGetLastError());
}
void
destroy(dom_rv)
SV *dom_rv;
......@@ -396,9 +416,31 @@ DESTROY(dom_rv)
MODULE = Sys::Virt PACKAGE = Sys::Virt
PROTOTYPES: ENABLE
#define REGISTER_CONSTANT(name, key) _populate_constant(constants, #key, name)
BOOT:
{
HV *constants;
virSetErrorFunc(NULL, ignoreVirErrorFunc);
/* not the 'standard' way of doing perl constants, but a lot easier to maintain */
constants = perl_get_hv("Sys::Virt::Domain::_constants", TRUE);
REGISTER_CONSTANT(VIR_DOMAIN_NOSTATE, STATE_NOSTATE);
REGISTER_CONSTANT(VIR_DOMAIN_RUNNING, STATE_RUNNING);
REGISTER_CONSTANT(VIR_DOMAIN_BLOCKED, STATE_BLOCKED);
REGISTER_CONSTANT(VIR_DOMAIN_PAUSED, STATE_PAUSED);
REGISTER_CONSTANT(VIR_DOMAIN_SHUTDOWN, STATE_SHUTDOWN);
REGISTER_CONSTANT(VIR_DOMAIN_SHUTOFF, STATE_SHUTOFF);
REGISTER_CONSTANT(VIR_DOMAIN_CRASHED, STATE_CRASHED);
REGISTER_CONSTANT(VIR_DOMAIN_DESTROY, REBOOT_DESTROY);
REGISTER_CONSTANT(VIR_DOMAIN_RESTART, REBOOT_RESTART);
REGISTER_CONSTANT(VIR_DOMAIN_PRESERVE, REBOOT_PRESERVE);
REGISTER_CONSTANT(VIR_DOMAIN_RENAME_RESTART, REBOOT_RENAME_RESTART);
}
......@@ -43,14 +43,16 @@ use warnings;
use Sys::Virt::Error;
use Sys::Virt::Domain;
our $VERSION = '0.0.1';
our $VERSION = '0.1.0';
require XSLoader;
XSLoader::load('Sys::Virt', $VERSION);
=item my $vmm = Sys::Virt->new(address => $address, readonly => $ro);
Attach to the virtual machine monitor with the address of C<address>. This
parameter is currently unused, so it is recommended to pass an empty string.
Attach to the virtual machine monitor with the address of C<address>. The
address parameter may be omitted, in which case the default connection made
will be to the local Xen hypervisor. In the future it wil be possible to
specify explicit addresses for other types of hypervisor connection.
If the optional C<readonly> parameter is supplied, then an unprivileged
connection to the VMM will be attempted. If it is not supplied, then it
defaults to making a fully privileged connection to the VMM. THis in turn
......@@ -63,7 +65,7 @@ sub new {
my $class = ref($proto) || $proto;
my %params = @_;
my $address = exists $params{address} ? $params{address} : die "address parameter is required";
my $address = exists $params{address} ? $params{address} : "";
my $readonly = exists $params{readonly} ? $params{readonly} : 0;
my $self = Sys::Virt::_open($address, $readonly);
......@@ -216,6 +218,47 @@ sub get_micro_version {
=pod
=item my $info = $con->get_node_info()
Returns a hash reference summarising the capabilities of the host
node. The elements of the hash ar
=over 4
=item memory
The amount of physical memory in the host
=item model
The model of the CPU, eg x86_64
=item cpus
The total number of logical CPUs
=item mhz
The peak MHZ of the CPU
=item nodes
The number of NUMA cells
=item sockets
The number of CPU sockets
=item cores
The number of cores per socket
=item threads
The number of threads per core
=back
=back
=head1 BUGS
......
......@@ -28,8 +28,8 @@ sub _new {
my $con = exists $params{connection} ? $params{connection} : die "connection parameter is requried";
my $self;
if (exists $params{address}) {
$self = Sys::Virt::Domain::_lookup_by_name($con, $params{address});
if (exists $params{name}) {
$self = Sys::Virt::Domain::_lookup_by_name($con, $params{name});
} elsif (exists $params{id}) {
$self = Sys::Virt::Domain::_lookup_by_id($con, $params{id});
} elsif (exists $params{uuid}) {
......@@ -53,8 +53,13 @@ domain.
=item my $uuid = $dom->get_uuid()
Returns a string containing a globally unique identifier for
the domain.
Returns a 16 byte long string containing the raw globally unique identifier
(UUID) for the domain.
=item my $uuid = $dom->get_uuid_string()
Returns a printable string representation of the raw UUID, in the format
'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'.
=item my $name = $dom->get_name()
......@@ -70,12 +75,13 @@ the domain's configuration
Returns a string containing the name of the OS type running
within the domain.
=item $dom->suspend
=item $dom->suspend()
Temporarily stop execution of the domain, allowing later continuation
by calling the C<resume> method.
=item $dom->resume
=item $dom->resume()
Resume execution of a domain previously halted with the C<suspend>
method.
......@@ -114,9 +120,8 @@ The current number of virtual CPUs enabled in the domain
=item state
The execution state of the machine, one of the strings
C<running>, C<blocked>, C<paused>, C<shutdown>, C<shutoff>,
C<crashed> or C<unknown>.
The execution state of the machine, which will be one of the
constants &Sys::Virt::Domain::STATE_*.
=back
......@@ -135,12 +140,106 @@ kilobytes.
Request that the guest OS perform a gracefull shutdown and
poweroff.
=item $dom->reboot($flags)
Request that the guest OS perform a gracefull shutdown and
optionally restart. The C<$flags> parameter determines how
the domain restarts (if at all). It should be one of the
constants &Sys::Virt::Domain::REBOOT_* listed later in this
document.
=cut
sub DESTROY {
# Keep autoloader quiet
}
sub AUTOLOAD {
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function.
my $constname;
our $AUTOLOAD;
($constname = $AUTOLOAD) =~ s/.*:://;
die "&Sys::Virt::Domain::constant not defined" if $constname eq '_constant';
if (!exists $Sys::Virt::Domain::_constants{$constname}) {
die "no such constant \$" . __PACKAGE__ . "::$constname";
}
{
no strict 'refs';
*$AUTOLOAD = sub { $Sys::Virt::Domain::_constants{$constname} };
}
goto &$AUTOLOAD;
}
1;
=back
=head1 CONSTANTS
The first set of constants enumerate the possible machine
runtime states, returned by the C<get_info> method.
=over 4
=item &Sys::Virt::Domain::STATE_NOSTATE
The domain is active, but is not running / blocked (eg idle)
=item &Sys::Virt::Domain::STATE_RUNNING
The domain is active and running
=item &Sys::Virt::Domain::STATE_BLOCKED
The domain is active, but execution is blocked
=item &Sys::Virt::Domain::STATE_PAUSED
The domain is active, but execution has been paused
=item &Sys::Virt::Domain::STATE_SHUTDOWN
The domain is active, but in the shutdown phase
=item &Sys::Virt::Domain::STATE_SHUTOFF
The domain is inactive, and shut down.
=item &Sys::Virt::Domain::STATE_CRASHED
The domain is inactive, and crashed.
=back
The next set of constants enumerate the different flags
which can be passed when requesting a reboot.
=over 4
=item &Sys::Virt::Domain::REBOOT_DESTROY
Destroy the domain, rather than restarting the domain
=item &Sys::Virt::Domain::REBOOT_RESTART
Restart the domain after shutdown is complete
=item &Sys::Virt::Domain::REBOOT_PRESERVE
Leave the domain inactive after shutdown is complete
=item &Sys::Virt::Domain::REBOOT_RENAME_RESTART
Restart the domain under a different (automatically generated) name
after shutdown is complete
=back
=head1 AUTHORS
Daniel P. Berrange <berrange@redhat.com>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment