| 1 |
# -*- perl -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2006-2009 Red Hat |
| 4 |
# Copyright (C) 2006-2009 Daniel P. Berrange |
| 5 |
# |
| 6 |
# This program is free software; You can redistribute it and/or modify |
| 7 |
# it under either: |
| 8 |
# |
| 9 |
# a) the GNU General Public License as published by the Free |
| 10 |
# Software Foundation; either version 2, or (at your option) any |
| 11 |
# later version, |
| 12 |
# |
| 13 |
# or |
| 14 |
# |
| 15 |
# b) the "Artistic License" |
| 16 |
# |
| 17 |
# The file "LICENSE" distributed along with this file provides full |
| 18 |
# details of the terms and conditions of the two licenses. |
| 19 |
|
| 20 |
=pod |
| 21 |
|
| 22 |
=head1 NAME |
| 23 |
|
| 24 |
Sys::Virt::StoragePool - Represent & manage a libvirt storage pool |
| 25 |
|
| 26 |
=head1 DESCRIPTION |
| 27 |
|
| 28 |
The C<Sys::Virt::StoragePool> module represents a storage pool managed |
| 29 |
by libvirt. There are a variety of storage pool implementations for |
| 30 |
LVM, Local directories/filesystems, network filesystems, disk |
| 31 |
partitioning, iSCSI, and SCSI. |
| 32 |
|
| 33 |
=head1 METHODS |
| 34 |
|
| 35 |
=over 4 |
| 36 |
|
| 37 |
=cut |
| 38 |
|
| 39 |
package Sys::Virt::StoragePool; |
| 40 |
|
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
|
| 44 |
|
| 45 |
sub _new { |
| 46 |
my $proto = shift; |
| 47 |
my $class = ref($proto) || $proto; |
| 48 |
my %params = @_; |
| 49 |
|
| 50 |
my $con = exists $params{connection} ? $params{connection} : die "connection parameter is required"; |
| 51 |
my $self; |
| 52 |
if (exists $params{name}) { |
| 53 |
$self = Sys::Virt::StoragePool::_lookup_by_name($con, $params{name}); |
| 54 |
} elsif (exists $params{uuid}) { |
| 55 |
if (length($params{uuid}) == 16) { |
| 56 |
$self = Sys::Virt::StoragePool::_lookup_by_uuid($con, $params{uuid}); |
| 57 |
} elsif (length($params{uuid}) == 32 || |
| 58 |
length($params{uuid}) == 36) { |
| 59 |
$self = Sys::Virt::StoragePool::_lookup_by_uuid_string($con, $params{uuid}); |
| 60 |
} else { |
| 61 |
die "UUID must be either 16 unsigned bytes, or 32/36 hex characters long"; |
| 62 |
} |
| 63 |
} elsif (exists $params{volume}) { |
| 64 |
$self = Sys::Virt::StoragePool::_lookup_by_volume($params{volume}); |
| 65 |
} elsif (exists $params{target_path}) { |
| 66 |
$self = Sys::Virt::StoragePool::_lookup_by_target_path($con, $params{target_path}); |
| 67 |
} elsif (exists $params{xml}) { |
| 68 |
if ($params{nocreate}) { |
| 69 |
$self = Sys::Virt::StoragePool::_define_xml($con, $params{xml}, $params{flags}); |
| 70 |
} else { |
| 71 |
$self = Sys::Virt::StoragePool::_create_xml($con, $params{xml}); |
| 72 |
} |
| 73 |
} else { |
| 74 |
die "address, id or uuid parameters are required"; |
| 75 |
} |
| 76 |
|
| 77 |
bless $self, $class; |
| 78 |
|
| 79 |
return $self; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
=item my $uuid = $pool->get_uuid() |
| 84 |
|
| 85 |
Returns a 16 byte long string containing the raw globally unique identifier |
| 86 |
(UUID) for the storage pool. |
| 87 |
|
| 88 |
=item my $uuid = $pool->get_uuid_string() |
| 89 |
|
| 90 |
Returns a printable string representation of the raw UUID, in the format |
| 91 |
'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'. |
| 92 |
|
| 93 |
=item my $name = $pool->get_name() |
| 94 |
|
| 95 |
Returns a string with a locally unique name of the storage pool |
| 96 |
|
| 97 |
=item $pool->is_active() |
| 98 |
|
| 99 |
Returns a true value if the storage pool is currently running |
| 100 |
|
| 101 |
=item $pool->is_persistent() |
| 102 |
|
| 103 |
Returns a true value if the storage pool has a persistent configuration |
| 104 |
file defined |
| 105 |
|
| 106 |
=item my $xml = $pool->get_xml_description() |
| 107 |
|
| 108 |
Returns an XML document containing a complete description of |
| 109 |
the storage pool's configuration |
| 110 |
|
| 111 |
=item $pool->create() |
| 112 |
|
| 113 |
Start a storage pool whose configuration was previously defined using the |
| 114 |
C<define_storage_pool> method in L<Sys::Virt>. |
| 115 |
|
| 116 |
=item $pool->undefine() |
| 117 |
|
| 118 |
Remove the configuration associated with a storage pool previously defined |
| 119 |
with the C<define_storage pool> method in L<Sys::Virt>. If the storage pool is |
| 120 |
running, you probably want to use the C<destroy> method instead. |
| 121 |
|
| 122 |
=item $pool->destroy() |
| 123 |
|
| 124 |
Immediately stop the storage pool. |
| 125 |
|
| 126 |
=item $flag = $pool->get_autostart(); |
| 127 |
|
| 128 |
Return a true value if the storage pool is configured to automatically |
| 129 |
start upon boot. Return false, otherwise |
| 130 |
|
| 131 |
=item $pool->set_autostart($flag) |
| 132 |
|
| 133 |
Set the state of the autostart flag, which determines whether the |
| 134 |
storage pool will automatically start upon boot of the host OS |
| 135 |
|
| 136 |
=item $pool->refresh([$flags]); |
| 137 |
|
| 138 |
Refresh the storage pool state. Typically this will rescan the list |
| 139 |
of storage volumes. The C<$flags> parameter is currently unused and |
| 140 |
if omitted defaults to zero. |
| 141 |
|
| 142 |
=item $pool->build([$flags]); |
| 143 |
|
| 144 |
Construct the storage pool if it does not exist. As an example, for |
| 145 |
a disk based storage pool this would ensure a partition table exists. |
| 146 |
The C<$flags> parameter allows control over the build operation |
| 147 |
and if omitted defaults to zero. |
| 148 |
|
| 149 |
=item $pool->delete([$flags]); |
| 150 |
|
| 151 |
Delete the storage pool. The C<$flags> parameter allows the data to |
| 152 |
be optionally wiped during delete and if omitted defaults to zero. |
| 153 |
|
| 154 |
=item $info = $pool->get_info() |
| 155 |
|
| 156 |
Retrieve information about the current storage pool state. The |
| 157 |
returned hash reference has the following keys |
| 158 |
|
| 159 |
=over 4 |
| 160 |
|
| 161 |
=item state |
| 162 |
|
| 163 |
The current status of the storage pool. See constants later. |
| 164 |
|
| 165 |
=item capacity |
| 166 |
|
| 167 |
The total logical size of the storage pool |
| 168 |
|
| 169 |
=item allocation |
| 170 |
|
| 171 |
The current physical allocation of the storage pool |
| 172 |
|
| 173 |
=item available |
| 174 |
|
| 175 |
The available space for creation of new volumes. This may |
| 176 |
be less than the difference between capacity & allocation |
| 177 |
if there are sizing / metadata constraints for volumes |
| 178 |
|
| 179 |
=back |
| 180 |
|
| 181 |
=item my $nnames = $pool->num_of_storage_volumes() |
| 182 |
|
| 183 |
Return the number of running volumes in this storage pool. The value |
| 184 |
can be used as the C<maxnames> parameter to C<list_storage_vol_names>. |
| 185 |
|
| 186 |
=item my @volNames = $pool->list_storage_vol_names($maxnames) |
| 187 |
|
| 188 |
Return a list of all volume names in this storage pool. The names can |
| 189 |
be used with the C<get_volume_by_name> method. |
| 190 |
|
| 191 |
=item my @vols = $pool->list_volumes() |
| 192 |
|
| 193 |
Return a list of all volumes in the storage pool. |
| 194 |
The elements in the returned list are instances of the |
| 195 |
L<Sys::Virt::StorageVol> class. This method requires O(n) |
| 196 |
RPC calls, so the C<list_all_volumes> method is |
| 197 |
recommended as a more efficient alternative. |
| 198 |
|
| 199 |
=cut |
| 200 |
|
| 201 |
sub list_volumes { |
| 202 |
my $self = shift; |
| 203 |
|
| 204 |
my $nnames = $self->num_of_storage_volumes(); |
| 205 |
my @names = $self->list_storage_vol_names($nnames); |
| 206 |
|
| 207 |
my @volumes; |
| 208 |
foreach my $name (@names) { |
| 209 |
eval { |
| 210 |
push @volumes, Sys::Virt::StorageVol->_new(pool => $self, name => $name); |
| 211 |
}; |
| 212 |
if ($@) { |
| 213 |
# nada - volume went away before we could look it up |
| 214 |
}; |
| 215 |
} |
| 216 |
return @volumes; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
=item my @volumes = $pool->list_all_volumes($flags) |
| 221 |
|
| 222 |
Return a list of all storage volumes associated with this pool. |
| 223 |
The elements in the returned list are instances of the |
| 224 |
L<Sys::Virt::StorageVol> class. The C<$flags> parameter can be |
| 225 |
used to filter the list of return storage volumes. |
| 226 |
|
| 227 |
=item my $vol = $pool->get_volume_by_name($name) |
| 228 |
|
| 229 |
Return the volume with a name of C<$name>. The returned object is |
| 230 |
an instance of the L<Sys::Virt::StorageVol> class. |
| 231 |
|
| 232 |
=cut |
| 233 |
|
| 234 |
sub get_volume_by_name { |
| 235 |
my $self = shift; |
| 236 |
my $name = shift; |
| 237 |
|
| 238 |
return Sys::Virt::StorageVol->_new(pool => $self, name => $name); |
| 239 |
} |
| 240 |
|
| 241 |
=item my $vol = $pool->create_volume($xml) |
| 242 |
|
| 243 |
Create a new volume based on the XML description passed into the C<$xml> |
| 244 |
parameter. The returned object is an instance of the L<Sys::Virt::StorageVol> |
| 245 |
class. If the optional C<clonevol> is provided, data will be copied from |
| 246 |
that source volume |
| 247 |
|
| 248 |
=cut |
| 249 |
|
| 250 |
sub create_volume { |
| 251 |
my $self = shift; |
| 252 |
my $xml = shift; |
| 253 |
|
| 254 |
return Sys::Virt::StorageVol->_new(pool => $self, xml => $xml); |
| 255 |
} |
| 256 |
|
| 257 |
=item my $vol = $pool->clone_volume($xml, $clonevol); |
| 258 |
|
| 259 |
Create a new volume based on the XML description passed into the C<$xml> |
| 260 |
parameter. The returned object is an instance of the L<Sys::Virt::StorageVol> |
| 261 |
class. The new volume will be populated with data from the specified clone |
| 262 |
source volume. |
| 263 |
|
| 264 |
=cut |
| 265 |
|
| 266 |
|
| 267 |
sub clone_volume { |
| 268 |
my $self = shift; |
| 269 |
my $xml = shift; |
| 270 |
my $clone = shift; |
| 271 |
|
| 272 |
return Sys::Virt::StorageVol->_new(pool => $self, xml => $xml, clone => $clone); |
| 273 |
} |
| 274 |
|
| 275 |
1; |
| 276 |
|
| 277 |
=back |
| 278 |
|
| 279 |
=head1 CONSTANTS |
| 280 |
|
| 281 |
The following sets of constants may be useful in dealing with some of |
| 282 |
the methods in this package |
| 283 |
|
| 284 |
=head2 STORAGE POOL DEFINE |
| 285 |
|
| 286 |
The following constants can be used to control the behaviour |
| 287 |
of storage pool define operations |
| 288 |
|
| 289 |
=over 4 |
| 290 |
|
| 291 |
=item Sys::Virt::StoragePool::DEFINE_VALIDATE |
| 292 |
|
| 293 |
Validate the XML document against the XML schema |
| 294 |
|
| 295 |
=back |
| 296 |
|
| 297 |
=head2 POOL STATES |
| 298 |
|
| 299 |
The following constants are useful for interpreting the C<state> |
| 300 |
key value in the hash returned by C<get_info> |
| 301 |
|
| 302 |
=over 4 |
| 303 |
|
| 304 |
=item Sys::Virt::StoragePool::STATE_INACTIVE |
| 305 |
|
| 306 |
The storage pool is not currently active |
| 307 |
|
| 308 |
=item Sys::Virt::StoragePool::STATE_BUILDING |
| 309 |
|
| 310 |
The storage pool is still being constructed and is not ready for use |
| 311 |
yet. |
| 312 |
|
| 313 |
=item Sys::Virt::StoragePool::STATE_RUNNING |
| 314 |
|
| 315 |
The storage pool is running and can be queried for volumes |
| 316 |
|
| 317 |
=item Sys::Virt::StoragePool::STATE_DEGRADED |
| 318 |
|
| 319 |
The storage pool is running, but its operation is degraded due |
| 320 |
to a failure. |
| 321 |
|
| 322 |
=item Sys::Virt::StoragePool::STATE_INACCESSIBLE |
| 323 |
|
| 324 |
The storage pool is not currently accessible |
| 325 |
|
| 326 |
=back |
| 327 |
|
| 328 |
=head2 DELETION MODES |
| 329 |
|
| 330 |
=over 4 |
| 331 |
|
| 332 |
=item Sys::Virt::StoragePool::DELETE_NORMAL |
| 333 |
|
| 334 |
Delete the pool without any attempt to scrub data |
| 335 |
|
| 336 |
=item Sys::Virt::StoragePool::DELETE_ZEROED |
| 337 |
|
| 338 |
Fill the allocated storage with zeros when deleting |
| 339 |
|
| 340 |
=back |
| 341 |
|
| 342 |
|
| 343 |
=head2 BUILD MODES |
| 344 |
|
| 345 |
=over 4 |
| 346 |
|
| 347 |
=item Sys::Virt::StoragePool::BUILD_NEW |
| 348 |
|
| 349 |
Construct a new storage pool from constituent bits |
| 350 |
|
| 351 |
=item Sys::Virt::StoragePool::BUILD_RESIZE |
| 352 |
|
| 353 |
Resize an existing built storage pool preserving data where |
| 354 |
appropriate |
| 355 |
|
| 356 |
=item Sys::Virt::StoragePool::BUILD_REPAIR |
| 357 |
|
| 358 |
Repair an existing storage pool operating in degraded mode |
| 359 |
|
| 360 |
=item Sys::Virt::StoragePool::BUILD_NO_OVERWRITE |
| 361 |
|
| 362 |
Do not overwrite existing storage pool data |
| 363 |
|
| 364 |
=item Sys::Virt::StoragePool::BUILD_OVERWRITE |
| 365 |
|
| 366 |
Overwrite existing storage pool data |
| 367 |
|
| 368 |
=back |
| 369 |
|
| 370 |
=head2 CREATE MODES |
| 371 |
|
| 372 |
When creating a storage pool it can be built at the same time. |
| 373 |
The following values are therefore close to their BUILD |
| 374 |
counterparts. |
| 375 |
|
| 376 |
=over 4 |
| 377 |
|
| 378 |
=item Sys::Virt::StoragePool::CREATE_NORMAL |
| 379 |
|
| 380 |
Just create the storage pool without building it. |
| 381 |
|
| 382 |
=item Sys::Virt::StoragePool::CREATE_WITH_BUILD |
| 383 |
|
| 384 |
When creating new storage pool also perform pool build without any flags. |
| 385 |
|
| 386 |
=item Sys::Virt::StoragePool::CREATE_WITH_BUILD_OVERWRITE |
| 387 |
|
| 388 |
Create the pool and perform pool build using the BUILD_OVERWRITE |
| 389 |
flag. This is mutually exclusive to CREATE_WITH_BUILD_NO_OVERWRITE. |
| 390 |
|
| 391 |
=item Sys::Virt::StoragePool::CREATE_WITH_BUILD_NO_OVERWRITE |
| 392 |
|
| 393 |
Create the pool and perform pool build using the BUILD_NO_OVERWRITE |
| 394 |
flag. This is mutually exclusive to CREATE_WITH_BUILD_OVERWRITE. |
| 395 |
|
| 396 |
=back |
| 397 |
|
| 398 |
=head2 XML DOCUMENTS |
| 399 |
|
| 400 |
The following constants are useful when requesting |
| 401 |
XML for storage pools |
| 402 |
|
| 403 |
=over 4 |
| 404 |
|
| 405 |
=item Sys::Virt::StoragePool::XML_INACTIVE |
| 406 |
|
| 407 |
Return XML describing the inactive state of the storage |
| 408 |
pool. |
| 409 |
|
| 410 |
=back |
| 411 |
|
| 412 |
=head1 LIST FILTERING |
| 413 |
|
| 414 |
The following constants are used to filter object lists |
| 415 |
|
| 416 |
=over 4 |
| 417 |
|
| 418 |
=item Sys::Virt::StoragePool::LIST_ACTIVE |
| 419 |
|
| 420 |
Include storage pools which are active |
| 421 |
|
| 422 |
=item Sys::Virt::StoragePool::LIST_INACTIVE |
| 423 |
|
| 424 |
Include storage pools which are inactive |
| 425 |
|
| 426 |
=item Sys::Virt::StoragePool::LIST_AUTOSTART |
| 427 |
|
| 428 |
Include storage pools which are marked for autostart |
| 429 |
|
| 430 |
=item Sys::Virt::StoragePool::LIST_NO_AUTOSTART |
| 431 |
|
| 432 |
Include storage pools which are not marked for autostart |
| 433 |
|
| 434 |
=item Sys::Virt::StoragePool::LIST_PERSISTENT |
| 435 |
|
| 436 |
Include storage pools which are persistent |
| 437 |
|
| 438 |
=item Sys::Virt::StoragePool::LIST_TRANSIENT |
| 439 |
|
| 440 |
Include storage pools which are transient |
| 441 |
|
| 442 |
=item Sys::Virt::StoragePool::LIST_DIR |
| 443 |
|
| 444 |
Include directory storage pools |
| 445 |
|
| 446 |
=item Sys::Virt::StoragePool::LIST_DISK |
| 447 |
|
| 448 |
Include disk storage pools |
| 449 |
|
| 450 |
=item Sys::Virt::StoragePool::LIST_FS |
| 451 |
|
| 452 |
Include filesystem storage pools |
| 453 |
|
| 454 |
=item Sys::Virt::StoragePool::LIST_ISCSI |
| 455 |
|
| 456 |
Include iSCSI storage pools |
| 457 |
|
| 458 |
=item Sys::Virt::StoragePool::LIST_LOGICAL |
| 459 |
|
| 460 |
Include LVM storage pools |
| 461 |
|
| 462 |
=item Sys::Virt::StoragePool::LIST_MPATH |
| 463 |
|
| 464 |
Include multipath storage pools |
| 465 |
|
| 466 |
=item Sys::Virt::StoragePool::LIST_NETFS |
| 467 |
|
| 468 |
Include network filesystem storage pools |
| 469 |
|
| 470 |
=item Sys::Virt::StoragePool::LIST_RBD |
| 471 |
|
| 472 |
Include RBD storage pools |
| 473 |
|
| 474 |
=item Sys::Virt::StoragePool::LIST_SCSI |
| 475 |
|
| 476 |
Include SCSI storage pools |
| 477 |
|
| 478 |
=item Sys::Virt::StoragePool::LIST_SHEEPDOG |
| 479 |
|
| 480 |
Include sheepdog storage pools |
| 481 |
|
| 482 |
=item Sys::Virt::StoragePool::LIST_GLUSTER |
| 483 |
|
| 484 |
Include gluster storage pools |
| 485 |
|
| 486 |
=item Sys::Virt::StoragePool::LIST_ZFS |
| 487 |
|
| 488 |
Include ZFS storage pools |
| 489 |
|
| 490 |
=item Sys::Virt::StoragePool::LIST_VSTORAGE |
| 491 |
|
| 492 |
Include VStorage storage pools |
| 493 |
|
| 494 |
=item Sys::Virt::StoragePool::LIST_ISCSI_DIRECT |
| 495 |
|
| 496 |
Include direct iSCSI pools |
| 497 |
|
| 498 |
=back |
| 499 |
|
| 500 |
=head2 EVENT ID CONSTANTS |
| 501 |
|
| 502 |
=over 4 |
| 503 |
|
| 504 |
=item Sys::Virt::StoragePool::EVENT_ID_LIFECYCLE |
| 505 |
|
| 506 |
Storage pool lifecycle events |
| 507 |
|
| 508 |
=item Sys::Virt::StoragePool::EVENT_ID_REFRESH |
| 509 |
|
| 510 |
Storage pool volume refresh events |
| 511 |
|
| 512 |
=back |
| 513 |
|
| 514 |
=head2 LIFECYCLE CHANGE EVENTS |
| 515 |
|
| 516 |
The following constants allow storage pool lifecycle change events |
| 517 |
to be interpreted. The events contain both a state change, and a |
| 518 |
reason though the reason is currently unused. |
| 519 |
|
| 520 |
=over 4 |
| 521 |
|
| 522 |
=item Sys::Virt::StoragePool::EVENT_DEFINED |
| 523 |
|
| 524 |
Indicates that a persistent configuration has been defined for |
| 525 |
the storage pool |
| 526 |
|
| 527 |
=item Sys::Virt::StoragePool::EVENT_STARTED |
| 528 |
|
| 529 |
The storage pool has started running |
| 530 |
|
| 531 |
=item Sys::Virt::StoragePool::EVENT_STOPPED |
| 532 |
|
| 533 |
The storage pool has stopped running |
| 534 |
|
| 535 |
=item Sys::Virt::StoragePool::EVENT_UNDEFINED |
| 536 |
|
| 537 |
The persistent configuration has gone away |
| 538 |
|
| 539 |
=item Sys::Virt::StoragePool::EVENT_CREATED |
| 540 |
|
| 541 |
The underlying storage has been built |
| 542 |
|
| 543 |
=item Sys::Virt::StoragePool::EVENT_DELETED |
| 544 |
|
| 545 |
The underlying storage has been released |
| 546 |
|
| 547 |
=back |
| 548 |
|
| 549 |
=head1 AUTHORS |
| 550 |
|
| 551 |
Daniel P. Berrange <[email protected]> |
| 552 |
|
| 553 |
=head1 COPYRIGHT |
| 554 |
|
| 555 |
Copyright (C) 2006-2009 Red Hat |
| 556 |
Copyright (C) 2006-2009 Daniel P. Berrange |
| 557 |
|
| 558 |
=head1 LICENSE |
| 559 |
|
| 560 |
This program is free software; you can redistribute it and/or modify |
| 561 |
it under the terms of either the GNU General Public License as published |
| 562 |
by the Free Software Foundation (either version 2 of the License, or at |
| 563 |
your option any later version), or, the Artistic License, as specified |
| 564 |
in the Perl README file. |
| 565 |
|
| 566 |
=head1 SEE ALSO |
| 567 |
|
| 568 |
L<Sys::Virt>, L<Sys::Virt::Error>, C<http://libvirt.org> |
| 569 |
|
| 570 |
=cut |