2008-08-08 01:37:16 +04:00
#
2013-10-28 00:59:46 +04:00
# Copyright (C) 2008, 2013 Red Hat, Inc.
2008-08-08 01:37:16 +04:00
# Copyright (C) 2008 Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
2014-05-20 01:32:22 +04:00
import logging
2012-05-14 17:24:56 +04:00
from gi . repository import GObject
2013-09-29 17:31:39 +04:00
from virtinst import pollhelpers
2013-09-20 04:18:12 +04:00
from virtinst import StoragePool , StorageVolume
2013-08-09 17:23:01 +04:00
from virtinst import util
2013-07-12 18:53:30 +04:00
2010-12-09 20:37:48 +03:00
from virtManager . libvirtobject import vmmLibvirtObject
2013-09-20 04:18:12 +04:00
class vmmStorageVolume ( vmmLibvirtObject ) :
def __init__ ( self , conn , backend , key ) :
2013-09-23 16:34:50 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , key , StorageVolume )
2013-09-20 04:18:12 +04:00
##########################
# Required class methods #
##########################
def get_name ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . name
2013-09-20 04:18:12 +04:00
def _XMLDesc ( self , flags ) :
2014-05-20 01:32:22 +04:00
try :
return self . _backend . XMLDesc ( flags )
except Exception , e :
logging . debug ( " XMLDesc for vol= %s failed: %s " ,
self . _backend . key ( ) , e )
raise
2013-09-20 04:18:12 +04:00
###########
# Actions #
###########
def get_parent_pool ( self ) :
pobj = self . _backend . storagePoolLookupByVolume ( )
return self . conn . get_pool_by_name ( pobj . name ( ) )
2013-09-30 23:23:14 +04:00
def delete ( self , force = True ) :
ignore = force
2013-09-20 04:18:12 +04:00
self . _backend . delete ( 0 )
self . _backend = None
#################
# XML accessors #
#################
2013-12-05 18:17:29 +04:00
def get_key ( self ) :
return self . get_xmlobj ( ) . key or " "
2013-09-20 04:18:12 +04:00
def get_target_path ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . target_path or " "
2013-09-20 04:18:12 +04:00
def get_format ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . format
2013-09-20 04:18:12 +04:00
def get_capacity ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . capacity
2013-09-20 04:18:12 +04:00
def get_allocation ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . allocation
2013-09-20 04:18:12 +04:00
def get_pretty_capacity ( self ) :
return util . pretty_bytes ( self . get_capacity ( ) )
def get_pretty_allocation ( self ) :
return util . pretty_bytes ( self . get_allocation ( ) )
2008-08-08 01:37:16 +04:00
2013-12-05 18:17:29 +04:00
def get_pretty_name ( self , pooltype ) :
name = self . get_name ( )
if pooltype != " iscsi " :
return name
key = self . get_key ( )
if not key :
return name
return " %s ( %s ) " % ( name , key )
2013-04-13 22:34:52 +04:00
2010-12-09 20:37:48 +03:00
class vmmStoragePool ( vmmLibvirtObject ) :
2012-05-14 17:24:56 +04:00
__gsignals__ = {
" refreshed " : ( GObject . SignalFlags . RUN_FIRST , None , [ ] )
}
2013-07-07 16:42:57 +04:00
def __init__ ( self , conn , backend , key ) :
2013-09-23 16:34:50 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , key , StoragePool )
2010-12-09 20:37:48 +03:00
2013-07-07 16:42:57 +04:00
self . _active = True
2013-09-20 04:18:12 +04:00
self . _support_isactive = None
2010-03-08 20:08:59 +03:00
2013-07-07 16:42:57 +04:00
self . _volumes = { }
2013-07-07 16:05:23 +04:00
self . tick ( )
2010-03-08 20:08:59 +03:00
self . refresh ( )
2008-08-08 01:37:16 +04:00
2013-09-20 04:18:12 +04:00
##########################
# Required class methods #
##########################
2010-12-09 20:37:48 +03:00
def get_name ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . name
2010-12-09 20:37:48 +03:00
def _XMLDesc ( self , flags ) :
2013-07-07 16:42:57 +04:00
return self . _backend . XMLDesc ( flags )
2010-12-09 20:37:48 +03:00
def _define ( self , xml ) :
2013-07-07 16:42:57 +04:00
return self . conn . define_pool ( xml )
2010-12-09 20:37:48 +03:00
2013-09-20 04:18:12 +04:00
###########
# Actions #
###########
def is_active ( self ) :
return self . _active
def _backend_get_active ( self ) :
if self . _support_isactive is None :
2013-10-06 18:08:04 +04:00
self . _support_isactive = self . conn . check_support (
self . conn . SUPPORT_POOL_ISACTIVE , self . _backend )
2013-09-20 04:18:12 +04:00
if not self . _support_isactive :
return True
return bool ( self . _backend . isActive ( ) )
2013-07-07 16:42:57 +04:00
def _set_active ( self , state ) :
if state == self . _active :
2013-07-07 16:05:23 +04:00
return
self . idle_emit ( state and " started " or " stopped " )
2013-07-07 16:42:57 +04:00
self . _active = state
2010-12-10 17:57:42 +03:00
self . refresh_xml ( )
2008-08-08 01:37:16 +04:00
2013-09-20 04:18:12 +04:00
def _kick_conn ( self ) :
self . conn . schedule_priority_tick ( pollpool = True )
def tick ( self ) :
self . _set_active ( self . _backend_get_active ( ) )
def set_autostart ( self , value ) :
self . _backend . setAutostart ( value )
def get_autostart ( self ) :
return self . _backend . autostart ( )
2008-08-08 01:37:16 +04:00
def can_change_alloc ( self ) :
2008-11-18 23:42:51 +03:00
typ = self . get_type ( )
2013-09-20 04:18:12 +04:00
return ( typ in [ StoragePool . TYPE_LOGICAL ] )
def supports_volume_creation ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . supports_volume_creation ( )
2013-07-07 19:06:15 +04:00
2008-08-08 01:37:16 +04:00
def start ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . create ( 0 )
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2012-02-10 23:07:51 +04:00
self . idle_add ( self . refresh_xml )
2008-08-08 01:37:16 +04:00
def stop ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . destroy ( )
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2012-02-10 23:07:51 +04:00
self . idle_add ( self . refresh_xml )
2008-08-08 01:37:16 +04:00
2013-10-01 02:26:43 +04:00
def delete ( self , force = True ) :
ignore = force
self . _backend . undefine ( )
2013-07-07 16:42:57 +04:00
self . _backend = None
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2008-08-08 01:37:16 +04:00
2008-09-06 05:53:26 +04:00
def refresh ( self ) :
2013-07-07 16:42:57 +04:00
if not self . is_active ( ) :
2010-04-21 18:56:06 +04:00
return
2012-02-10 23:07:51 +04:00
def cb ( ) :
self . refresh_xml ( )
self . update_volumes ( refresh = True )
self . emit ( " refreshed " )
2013-07-07 16:42:57 +04:00
self . _backend . refresh ( 0 )
2012-02-10 23:07:51 +04:00
self . idle_add ( cb )
2008-09-06 05:53:26 +04:00
2013-09-29 20:28:01 +04:00
def define_name ( self , newname ) :
return self . _define_name_helper ( " storagepool " ,
self . conn . rename_pool ,
newname )
2013-09-20 04:18:12 +04:00
###################
# Volume handling #
###################
2013-09-29 04:05:13 +04:00
def get_volumes ( self , refresh = True ) :
if refresh :
self . update_volumes ( )
2013-09-20 04:18:12 +04:00
return self . _volumes
def get_volume ( self , uuid ) :
return self . _volumes [ uuid ]
2012-01-30 07:15:01 +04:00
def update_volumes ( self , refresh = False ) :
2008-08-08 01:37:16 +04:00
if not self . is_active ( ) :
self . _volumes = { }
return
2013-09-29 17:31:39 +04:00
( ignore , new , allvols ) = pollhelpers . fetch_volumes (
self . conn . get_backend ( ) , self . get_backend ( ) , self . _volumes . copy ( ) ,
lambda obj , key : vmmStorageVolume ( self . conn , obj , key ) )
for volname in allvols :
if volname not in new and refresh :
allvols [ volname ] . refresh_xml ( )
self . _volumes = allvols
2013-09-20 04:18:12 +04:00
#################
# XML accessors #
#################
def get_type ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . type
2013-09-20 04:18:12 +04:00
def get_uuid ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . uuid
2013-09-20 04:18:12 +04:00
def get_target_path ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . target_path or " "
2013-09-20 04:18:12 +04:00
def get_allocation ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . allocation
2013-09-20 04:18:12 +04:00
def get_available ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . available
2013-09-20 04:18:12 +04:00
def get_capacity ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . capacity
2013-09-20 04:18:12 +04:00
def get_pretty_allocation ( self ) :
return util . pretty_bytes ( self . get_allocation ( ) )
def get_pretty_available ( self ) :
return util . pretty_bytes ( self . get_available ( ) )
def get_pretty_capacity ( self ) :
return util . pretty_bytes ( self . get_capacity ( ) )