2006-06-28 23:50:17 +04:00
#
2014-01-20 20:09:13 +04:00
# Copyright (C) 2006, 2013, 2014 Red Hat, Inc.
2006-06-28 23:50:17 +04:00
# Copyright (C) 2006 Daniel P. Berrange <berrange@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
2007-11-20 19:12:20 +03:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2006-06-28 23:50:17 +04:00
#
2006-06-15 01:52:49 +04:00
2006-09-26 02:41:47 +04:00
import logging
2013-10-01 05:41:10 +04:00
import os
2010-12-11 00:04:42 +03:00
import time
import threading
2011-04-18 20:39:53 +04:00
import libvirt
2013-10-01 01:11:22 +04:00
2016-06-07 18:33:21 +03:00
from gi . repository import GObject
2014-09-18 01:25:03 +04:00
from virtinst import DomainCapabilities
2013-10-01 01:11:22 +04:00
from virtinst import DomainSnapshot
from virtinst import Guest
2013-08-09 17:23:01 +04:00
from virtinst import util
2013-10-01 01:11:22 +04:00
from virtinst import VirtualController
2006-08-08 19:51:58 +04:00
2014-09-13 00:10:45 +04:00
from . libvirtobject import vmmLibvirtObject
2010-02-26 03:35:01 +03:00
2014-02-12 01:40:37 +04:00
if not hasattr ( libvirt , " VIR_DOMAIN_PMSUSPENDED " ) :
setattr ( libvirt , " VIR_DOMAIN_PMSUSPENDED " , 7 )
2013-04-13 22:34:52 +04:00
2014-01-27 02:09:07 +04:00
vm_status_icons = {
libvirt . VIR_DOMAIN_BLOCKED : " state_running " ,
libvirt . VIR_DOMAIN_CRASHED : " state_shutoff " ,
libvirt . VIR_DOMAIN_PAUSED : " state_paused " ,
libvirt . VIR_DOMAIN_RUNNING : " state_running " ,
libvirt . VIR_DOMAIN_SHUTDOWN : " state_shutoff " ,
libvirt . VIR_DOMAIN_SHUTOFF : " state_shutoff " ,
libvirt . VIR_DOMAIN_NOSTATE : " state_running " ,
2014-02-12 01:40:37 +04:00
libvirt . VIR_DOMAIN_PMSUSPENDED : " state_paused " ,
2014-01-27 02:09:07 +04:00
}
2014-03-22 22:34:23 +04:00
class _SENTINEL ( object ) :
pass
2010-09-09 01:53:51 +04:00
def compare_device ( origdev , newdev , idx ) :
devprops = {
" disk " : [ " target " , " bus " ] ,
" interface " : [ " macaddr " , " vmmindex " ] ,
" input " : [ " bus " , " type " , " vmmindex " ] ,
" sound " : [ " model " , " vmmindex " ] ,
2013-07-15 19:07:40 +04:00
" video " : [ " model " , " vmmindex " ] ,
2010-09-09 01:53:51 +04:00
" watchdog " : [ " vmmindex " ] ,
2011-07-19 20:37:18 +04:00
" hostdev " : [ " type " , " managed " , " vmmindex " ,
" product " , " vendor " ,
" function " , " domain " , " slot " ] ,
2013-07-16 17:14:37 +04:00
" serial " : [ " type " , " target_port " ] ,
" parallel " : [ " type " , " target_port " ] ,
" console " : [ " type " , " target_type " , " target_port " ] ,
2010-09-09 01:53:51 +04:00
" graphics " : [ " type " , " vmmindex " ] ,
2010-12-16 20:41:47 +03:00
" controller " : [ " type " , " index " ] ,
2013-07-16 17:14:37 +04:00
" channel " : [ " type " , " target_name " ] ,
2011-05-19 23:18:33 +04:00
" filesystem " : [ " target " , " vmmindex " ] ,
2011-06-23 19:42:03 +04:00
" smartcard " : [ " mode " , " vmmindex " ] ,
2011-09-02 05:23:27 +04:00
" redirdev " : [ " bus " , " type " , " vmmindex " ] ,
2013-06-26 05:45:08 +04:00
" tpm " : [ " type " , " vmmindex " ] ,
2013-09-23 17:39:56 +04:00
" rng " : [ " type " , " vmmindex " ] ,
2014-01-10 13:37:55 +04:00
" panic " : [ " type " , " vmmindex " ] ,
2010-09-09 01:53:51 +04:00
}
if id ( origdev ) == id ( newdev ) :
return True
if type ( origdev ) is not type ( newdev ) :
return False
for devprop in devprops [ origdev . virtual_device_type ] :
origval = getattr ( origdev , devprop )
if devprop == " vmmindex " :
newval = idx
else :
newval = getattr ( newdev , devprop )
if origval != newval :
return False
return True
2009-12-02 16:39:10 +03:00
2013-04-13 22:34:52 +04:00
2014-02-10 01:21:26 +04:00
def _find_device ( guest , origdev ) :
2010-09-09 01:53:51 +04:00
devlist = guest . get_devices ( origdev . virtual_device_type )
2016-04-18 23:42:12 +03:00
for idx , dev in enumerate ( devlist ) :
2010-09-09 01:53:51 +04:00
if compare_device ( origdev , dev , idx ) :
return dev
2009-12-02 16:39:10 +03:00
2010-09-09 01:53:51 +04:00
return None
2009-12-02 16:39:10 +03:00
2013-04-13 22:34:52 +04:00
2010-12-11 00:04:42 +03:00
def start_job_progress_thread ( vm , meter , progtext ) :
current_thread = threading . currentThread ( )
def jobinfo_cb ( ) :
while True :
time . sleep ( .5 )
if not current_thread . isAlive ( ) :
return False
try :
jobinfo = vm . job_info ( )
data_total = float ( jobinfo [ 3 ] )
2013-04-13 22:34:52 +04:00
# data_processed = float(jobinfo[4])
2010-12-11 00:04:42 +03:00
data_remaining = float ( jobinfo [ 5 ] )
# data_total is 0 if the job hasn't started yet
if not data_total :
continue
if not meter . started :
meter . start ( size = data_total ,
text = progtext )
progress = data_total - data_remaining
meter . update ( progress )
except :
logging . exception ( " Error calling jobinfo " )
return False
return True
if vm . getjobinfo_supported :
t = threading . Thread ( target = jobinfo_cb ,
name = " job progress reporting " ,
args = ( ) )
t . daemon = True
t . start ( )
2013-04-13 22:34:52 +04:00
2012-01-25 20:52:45 +04:00
class vmmInspectionData ( object ) :
def __init__ ( self ) :
self . type = None
self . distro = None
self . major_version = None
self . minor_version = None
self . hostname = None
self . product_name = None
self . product_variant = None
self . icon = None
self . applications = None
2013-11-27 00:57:06 +04:00
self . error = False
2012-01-25 20:52:45 +04:00
2013-04-13 22:34:52 +04:00
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
class vmmDomainSnapshot ( vmmLibvirtObject ) :
"""
Class wrapping a virDomainSnapshot object
"""
def __init__ ( self , conn , backend ) :
2013-09-10 01:14:16 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , backend . getName ( ) ,
2013-10-01 01:11:22 +04:00
DomainSnapshot )
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
2015-04-10 16:15:44 +03:00
##########################
# Required class methods #
##########################
2014-06-03 01:17:47 +04:00
def _backend_get_name ( self ) :
return self . _backend . getName ( )
2015-04-10 16:15:44 +03:00
def _conn_tick_poll_param ( self ) :
return None
def class_name ( self ) :
return " snapshot "
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
def _XMLDesc ( self , flags ) :
2013-09-10 01:14:16 +04:00
return self . _backend . getXMLDesc ( flags = flags )
2015-04-10 01:02:42 +03:00
def _get_backend_status ( self ) :
return self . _STATUS_ACTIVE
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
2015-04-10 16:15:44 +03:00
def tick ( self , stats_update = True ) :
ignore = stats_update
2015-04-10 21:08:25 +03:00
def _init_libvirt_state ( self ) :
2015-04-11 00:50:06 +03:00
self . ensure_latest_xml ( )
2015-04-10 16:15:44 +03:00
###########
# Actions #
###########
2013-09-30 23:23:14 +04:00
def delete ( self , force = True ) :
ignore = force
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
self . _backend . delete ( )
2013-10-01 01:11:22 +04:00
def run_status ( self ) :
status = DomainSnapshot . state_str_to_int ( self . get_xmlobj ( ) . state )
return vmmDomain . pretty_run_status ( status )
def run_status_icon_name ( self ) :
status = DomainSnapshot . state_str_to_int ( self . get_xmlobj ( ) . state )
2014-01-27 02:09:07 +04:00
if status not in vm_status_icons :
2013-10-01 01:11:22 +04:00
logging . debug ( " Unknown status %d , using NOSTATE " , status )
status = libvirt . VIR_DOMAIN_NOSTATE
2014-01-27 02:09:07 +04:00
return vm_status_icons [ status ]
2013-10-01 01:11:22 +04:00
2014-02-01 20:25:35 +04:00
def is_current ( self ) :
return self . _backend . isCurrent ( )
2013-10-01 01:53:55 +04:00
def is_external ( self ) :
if self . get_xmlobj ( ) . memory_type == " external " :
return True
for disk in self . get_xmlobj ( ) . disks :
if disk . snapshot == " external " :
return True
return False
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
2011-05-04 00:17:52 +04:00
class vmmDomain ( vmmLibvirtObject ) :
2012-11-08 17:15:02 +04:00
"""
Class wrapping virDomain libvirt objects . Is also extended to be
backed by a virtinst . Guest object for new VM ' customize before install '
"""
2012-05-14 17:24:56 +04:00
__gsignals__ = {
" resources-sampled " : ( GObject . SignalFlags . RUN_FIRST , None , [ ] ) ,
" inspection-changed " : ( GObject . SignalFlags . RUN_FIRST , None , [ ] ) ,
2013-05-13 14:14:11 +04:00
" pre-startup " : ( GObject . SignalFlags . RUN_FIRST , None , [ object ] ) ,
2012-05-14 17:24:56 +04:00
}
2013-10-01 01:11:22 +04:00
@staticmethod
def pretty_run_status ( status , has_saved = False ) :
if status == libvirt . VIR_DOMAIN_RUNNING :
return _ ( " Running " )
elif status == libvirt . VIR_DOMAIN_PAUSED :
return _ ( " Paused " )
elif status == libvirt . VIR_DOMAIN_SHUTDOWN :
return _ ( " Shutting Down " )
elif status == libvirt . VIR_DOMAIN_SHUTOFF :
if has_saved :
return _ ( " Saved " )
else :
return _ ( " Shutoff " )
elif status == libvirt . VIR_DOMAIN_CRASHED :
return _ ( " Crashed " )
2014-02-12 01:40:37 +04:00
elif status == libvirt . VIR_DOMAIN_PMSUSPENDED :
2013-10-01 01:11:22 +04:00
return _ ( " Suspended " )
2015-04-14 00:25:40 +03:00
logging . debug ( " Unknown status %s , returning ' Unknown ' " , status )
2013-10-01 01:11:22 +04:00
return _ ( " Unknown " )
2014-03-18 18:04:59 +04:00
@staticmethod
def pretty_status_reason ( status , reason ) :
2015-07-10 15:07:03 +03:00
def key ( x , y ) :
return getattr ( libvirt , " VIR_DOMAIN_ " + x , y )
2014-03-18 18:04:59 +04:00
reasons = {
libvirt . VIR_DOMAIN_RUNNING : {
key ( " RUNNING_BOOTED " , 1 ) : _ ( " Booted " ) ,
key ( " RUNNING_MIGRATED " , 2 ) : _ ( " Migrated " ) ,
key ( " RUNNING_RESTORED " , 3 ) : _ ( " Restored " ) ,
key ( " RUNNING_FROM_SNAPSHOT " , 4 ) : _ ( " From snapshot " ) ,
key ( " RUNNING_UNPAUSED " , 5 ) : _ ( " Unpaused " ) ,
key ( " RUNNING_MIGRATION_CANCELED " , 6 ) : _ ( " Migration canceled " ) ,
key ( " RUNNING_SAVE_CANCELED " , 7 ) : _ ( " Save canceled " ) ,
key ( " RUNNING_WAKEUP " , 8 ) : _ ( " Event wakeup " ) ,
key ( " RUNNING_CRASHED " , 9 ) : _ ( " Crashed " ) ,
} ,
libvirt . VIR_DOMAIN_PAUSED : {
key ( " PAUSED_USER " , 1 ) : _ ( " User " ) ,
key ( " PAUSED_MIGRATION " , 2 ) : _ ( " Migrating " ) ,
key ( " PAUSED_SAVE " , 3 ) : _ ( " Saving " ) ,
key ( " PAUSED_DUMP " , 4 ) : _ ( " Dumping " ) ,
key ( " PAUSED_IOERROR " , 5 ) : _ ( " I/O error " ) ,
key ( " PAUSED_WATCHDOG " , 6 ) : _ ( " Watchdog " ) ,
key ( " PAUSED_FROM_SNAPSHOT " , 7 ) : _ ( " From snapshot " ) ,
key ( " PAUSED_SHUTTING_DOWN " , 8 ) : _ ( " Shutting down " ) ,
key ( " PAUSED_SNAPSHOT " , 9 ) : _ ( " Creating snapshot " ) ,
key ( " PAUSED_CRASHED " , 10 ) : _ ( " Crashed " ) ,
} ,
libvirt . VIR_DOMAIN_SHUTDOWN : {
key ( " SHUTDOWN_USER " , 1 ) : _ ( " User " ) ,
} ,
libvirt . VIR_DOMAIN_SHUTOFF : {
key ( " SHUTOFF_SHUTDOWN " , 1 ) : _ ( " Shutdown " ) ,
key ( " SHUTOFF_DESTROYED " , 2 ) : _ ( " Destroyed " ) ,
key ( " SHUTOFF_CRASHED " , 3 ) : _ ( " Crashed " ) ,
key ( " SHUTOFF_MIGRATED " , 4 ) : _ ( " Migrated " ) ,
key ( " SHUTOFF_SAVED " , 5 ) : _ ( " Saved " ) ,
key ( " SHUTOFF_FAILED " , 6 ) : _ ( " Failed " ) ,
key ( " SHUTOFF_FROM_SNAPSHOT " , 7 ) : _ ( " From snapshot " ) ,
} ,
libvirt . VIR_DOMAIN_CRASHED : {
key ( " CRASHED_PANICKED " , 1 ) : _ ( " Panicked " ) ,
}
}
return reasons . get ( status ) and reasons [ status ] . get ( reason )
2013-07-07 16:42:57 +04:00
def __init__ ( self , conn , backend , key ) :
2013-10-01 01:11:22 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , key , Guest )
2010-02-26 03:35:01 +03:00
2010-05-13 18:37:31 +04:00
self . cloning = False
2007-04-12 23:36:04 +04:00
2015-05-05 01:16:09 +03:00
self . _stats = [ ]
self . _stats_rates = {
2010-11-30 01:28:52 +03:00
" diskRdRate " : 10.0 ,
" diskWrRate " : 10.0 ,
" netTxRate " : 10.0 ,
" netRxRate " : 10.0 ,
}
2010-05-13 19:21:01 +04:00
self . _install_abort = False
2011-04-10 06:40:22 +04:00
self . _id = None
2014-06-03 01:17:47 +04:00
self . _uuid = None
2014-09-12 02:01:41 +04:00
self . _has_managed_save = None
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
self . _snapshot_list = None
2014-09-12 18:15:09 +04:00
self . _autostart = None
2014-09-18 01:25:03 +04:00
self . _domain_caps = None
2015-04-10 01:02:42 +03:00
self . _status_reason = None
2011-05-04 00:17:52 +04:00
2010-05-12 20:57:32 +04:00
self . managedsave_supported = False
2011-06-21 19:04:48 +04:00
self . remote_console_supported = False
2013-09-24 14:57:32 +04:00
self . title_supported = False
2014-01-13 02:52:13 +04:00
self . mem_stats_supported = False
2014-03-18 18:04:59 +04:00
self . domain_state_supported = False
2013-12-18 17:42:43 +04:00
2014-01-13 02:39:21 +04:00
self . _enable_mem_stats = False
2014-02-12 00:29:30 +04:00
self . _enable_cpu_stats = False
2015-11-19 05:17:22 +03:00
self . _mem_stats_period_is_set = False
2014-01-13 02:39:21 +04:00
2011-04-11 18:21:19 +04:00
self . _enable_net_poll = False
2010-02-09 01:13:36 +03:00
self . _stats_net_supported = True
2010-02-11 17:30:32 +03:00
self . _stats_net_skip = [ ]
2011-04-11 18:21:19 +04:00
self . _enable_disk_poll = False
2010-02-09 01:13:36 +03:00
self . _stats_disk_supported = True
2010-02-11 17:30:32 +03:00
self . _stats_disk_skip = [ ]
2014-02-21 14:38:41 +04:00
self . _summary_disk_stats_skip = False
2010-02-09 01:13:36 +03:00
2011-07-18 22:53:55 +04:00
self . inspection = vmmInspectionData ( )
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
def _cleanup ( self ) :
for snap in self . _snapshot_list or [ ] :
snap . cleanup ( )
self . _snapshot_list = None
2015-04-10 21:08:25 +03:00
def _init_libvirt_state ( self ) :
2013-10-06 18:08:04 +04:00
self . managedsave_supported = self . conn . check_support (
self . conn . SUPPORT_DOMAIN_MANAGED_SAVE , self . _backend )
self . remote_console_supported = self . conn . check_support (
self . conn . SUPPORT_DOMAIN_CONSOLE_STREAM , self . _backend )
self . title_supported = self . conn . check_support (
self . conn . SUPPORT_DOMAIN_GET_METADATA , self . _backend )
2014-01-13 02:52:13 +04:00
self . mem_stats_supported = self . conn . check_support (
self . conn . SUPPORT_DOMAIN_MEMORY_STATS , self . _backend )
2014-03-18 18:04:59 +04:00
self . domain_state_supported = self . conn . check_support (
self . conn . SUPPORT_DOMAIN_STATE , self . _backend )
2011-06-21 19:04:48 +04:00
2011-05-04 00:17:52 +04:00
# Determine available XML flags (older libvirt versions will error
# out if passed SECURE_XML, INACTIVE_XML, etc)
( self . _inactive_xml_flags ,
2011-07-23 00:43:26 +04:00
self . _active_xml_flags ) = self . conn . get_dom_flags ( self . _backend )
2011-05-04 00:17:52 +04:00
2015-05-05 01:41:26 +03:00
# This needs to come before initial stats tick
self . _on_config_sample_network_traffic_changed ( )
self . _on_config_sample_disk_io_changed ( )
self . _on_config_sample_mem_stats_changed ( )
self . _on_config_sample_cpu_stats_changed ( )
2015-04-10 21:08:25 +03:00
# Prime caches
2015-04-19 16:26:16 +03:00
info = self . _backend . info ( )
self . _refresh_status ( newstatus = info [ 0 ] )
2015-05-05 01:41:26 +03:00
self . _tick_stats ( info )
2015-04-10 21:08:25 +03:00
self . has_managed_save ( )
self . snapshots_supported ( )
2011-05-04 00:17:52 +04:00
# Hook up listeners that need to be cleaned up
2014-09-28 15:37:16 +04:00
self . add_gsettings_handle (
2014-02-12 00:29:30 +04:00
self . config . on_stats_enable_cpu_poll_changed (
2015-05-04 23:33:56 +03:00
self . _on_config_sample_cpu_stats_changed ) )
2014-09-28 15:37:16 +04:00
self . add_gsettings_handle (
2011-05-04 00:17:52 +04:00
self . config . on_stats_enable_net_poll_changed (
2015-05-04 23:33:56 +03:00
self . _on_config_sample_network_traffic_changed ) )
2014-09-28 15:37:16 +04:00
self . add_gsettings_handle (
2011-05-04 00:17:52 +04:00
self . config . on_stats_enable_disk_poll_changed (
2015-05-04 23:33:56 +03:00
self . _on_config_sample_disk_io_changed ) )
2014-09-28 15:37:16 +04:00
self . add_gsettings_handle (
2014-01-13 02:39:21 +04:00
self . config . on_stats_enable_memory_poll_changed (
2015-05-04 23:33:56 +03:00
self . _on_config_sample_mem_stats_changed ) )
2011-05-04 00:17:52 +04:00
2015-09-17 23:06:14 +03:00
if ( self . get_name ( ) == " Domain-0 " and
self . get_uuid ( ) == " 00000000-0000-0000-0000-000000000000 " ) :
# We don't want virt-manager to track Domain-0 since it
# doesn't work with our UI. Raising an error will ensures it
# is blacklisted.
raise RuntimeError ( " Can ' t track Domain-0 as a vmmDomain " )
2013-05-13 14:14:11 +04:00
self . connect ( " pre-startup " , self . _prestartup_nodedev_check )
def _prestartup_nodedev_check ( self , src , ret ) :
ignore = src
2013-05-28 04:04:55 +04:00
error = _ ( " There is more than one ' %s ' device attached to "
2013-05-13 14:14:11 +04:00
" your host, and we can ' t determine which one to "
" use for your guest. \n "
" To fix this, remove and reattach the USB device "
" to your guest using the ' Add Hardware ' wizard. " )
for hostdev in self . get_hostdev_devices ( ) :
devtype = hostdev . type
if devtype != " usb " :
continue
vendor = hostdev . vendor
product = hostdev . product
bus = hostdev . bus
device = hostdev . device
if vendor and product :
2015-04-10 16:37:03 +03:00
count = self . conn . get_nodedev_count ( " usb_device " ,
2013-05-13 14:14:11 +04:00
vendor ,
product )
if count > 1 and not ( bus and device ) :
prettyname = " %s %s " % ( vendor , product )
ret . append ( error % prettyname )
2011-05-04 00:17:52 +04:00
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
2011-05-04 00:17:52 +04:00
###########################
# Misc API getter methods #
###########################
2011-03-23 23:56:12 +03:00
2015-04-10 19:52:42 +03:00
def reports_stats ( self ) :
return True
2014-02-11 21:07:13 +04:00
def _using_events ( self ) :
return self . conn . using_domain_events
2010-02-07 20:10:41 +03:00
def get_id ( self ) :
2012-11-08 17:15:02 +04:00
if self . _id is None :
2011-05-04 00:17:52 +04:00
self . _id = self . _backend . ID ( )
return self . _id
2008-12-17 23:27:06 +03:00
2011-05-04 00:17:52 +04:00
def status ( self ) :
2015-04-10 01:02:42 +03:00
return self . _normalize_status ( self . _get_status ( ) )
2008-12-17 23:27:06 +03:00
2014-03-18 18:04:59 +04:00
def status_reason ( self ) :
2015-04-10 01:02:42 +03:00
if self . _status_reason is None :
self . _status_reason = 1
if self . domain_state_supported :
self . _status_reason = self . _backend . state ( ) [ 1 ]
return self . _status_reason
2014-03-18 18:04:59 +04:00
2010-05-13 18:37:31 +04:00
def get_cloning ( self ) :
return self . cloning
def set_cloning ( self , val ) :
self . cloning = bool ( val )
2010-05-13 19:21:01 +04:00
# If manual shutdown or destroy specified, make sure we don't continue
# install process
def get_install_abort ( self ) :
return bool ( self . _install_abort )
2014-01-20 20:09:13 +04:00
def stable_defaults ( self ) :
2015-03-23 23:43:39 +03:00
return self . get_xmlobj ( ) . stable_defaults ( )
2009-09-25 17:52:03 +04:00
2013-07-01 22:33:59 +04:00
def has_spicevmc_type_redirdev ( self ) :
devs = self . get_redirdev_devices ( )
for dev in devs :
if dev . type == " spicevmc " :
return True
return False
2011-05-04 00:17:52 +04:00
def get_id_pretty ( self ) :
i = self . get_id ( )
if i < 0 :
return " - "
return str ( i )
2013-10-01 04:33:42 +04:00
##################
# Support checks #
##################
def _get_getvcpus_supported ( self ) :
2013-10-06 18:08:04 +04:00
return self . conn . check_support (
self . conn . SUPPORT_DOMAIN_GETVCPUS , self . _backend )
2013-10-01 04:33:42 +04:00
getvcpus_supported = property ( _get_getvcpus_supported )
def _get_getjobinfo_supported ( self ) :
2013-10-06 18:08:04 +04:00
return self . conn . check_support (
self . conn . SUPPORT_DOMAIN_JOB_INFO , self . _backend )
2013-10-01 04:33:42 +04:00
getjobinfo_supported = property ( _get_getjobinfo_supported )
def snapshots_supported ( self ) :
2013-10-06 18:08:04 +04:00
if not self . conn . check_support (
self . conn . SUPPORT_DOMAIN_LIST_SNAPSHOTS , self . _backend ) :
2013-10-01 04:33:42 +04:00
return _ ( " Libvirt connection does not support snapshots. " )
if self . list_snapshots ( ) :
return
# Check if our disks are all qcow2
seen_qcow2 = False
for disk in self . get_disk_devices ( refresh_if_nec = False ) :
if disk . read_only :
continue
if not disk . path :
continue
if disk . driver_type == " qcow2 " :
seen_qcow2 = True
continue
return _ ( " Snapshots are only supported if all writeable disks "
" images allocated to the guest are qcow2 format. " )
if not seen_qcow2 :
return _ ( " Snapshots require at least one writeable qcow2 disk "
" image allocated to the guest. " )
2014-09-18 01:25:03 +04:00
def get_domain_capabilities ( self ) :
if not self . _domain_caps :
2015-02-18 23:16:48 +03:00
self . _domain_caps = DomainCapabilities . build_from_guest (
self . get_xmlobj ( ) )
2014-09-18 01:25:03 +04:00
return self . _domain_caps
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
2011-05-04 00:17:52 +04:00
#############################
# Internal XML handling API #
#############################
2010-11-30 01:28:52 +03:00
2010-09-09 01:53:51 +04:00
def _invalidate_xml ( self ) :
vmmLibvirtObject . _invalidate_xml ( self )
2011-04-11 19:55:20 +04:00
self . _id = None
2015-04-10 01:02:42 +03:00
self . _status_reason = None
self . _has_managed_save = None
2010-09-09 01:53:51 +04:00
2015-04-11 00:14:08 +03:00
def _lookup_device_to_define ( self , xmlobj , origdev , for_hotplug ) :
if for_hotplug :
return origdev
2014-02-10 01:21:26 +04:00
2015-04-11 00:14:08 +03:00
dev = _find_device ( xmlobj , origdev )
2010-09-09 01:53:51 +04:00
if dev :
2014-02-10 01:21:26 +04:00
return dev
2010-09-09 01:53:51 +04:00
# If we are removing multiple dev from an active VM, a double
# attempt may result in a lookup failure. If device is present
# in the active XML, assume all is good.
2014-02-10 01:21:26 +04:00
if _find_device ( self . get_xmlobj ( ) , origdev ) :
2010-12-10 17:57:42 +03:00
logging . debug ( " Device in active config but not inactive config. " )
2010-09-09 01:53:51 +04:00
return
raise RuntimeError ( _ ( " Could not find specified device in the "
" inactive VM configuration: %s " ) % repr ( origdev ) )
2011-05-04 00:17:52 +04:00
##############################
# Persistent XML change APIs #
##############################
# Device Add/Remove
2010-09-09 01:53:51 +04:00
def add_device ( self , devobj ) :
"""
Redefine guest with appended device XML ' devxml '
"""
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
xmlobj . add_device ( devobj )
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2010-09-09 01:53:51 +04:00
def remove_device ( self , devobj ) :
"""
Remove passed device from the inactive guest XML
"""
# HACK: If serial and console are both present, they both need
# to be removed at the same time
con = None
if hasattr ( devobj , " virtmanager_console_dup " ) :
con = getattr ( devobj , " virtmanager_console_dup " )
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , False )
if not editdev :
return
2010-09-09 01:53:51 +04:00
2015-04-11 00:14:08 +03:00
if con :
rmcon = _find_device ( xmlobj , con )
if rmcon :
xmlobj . remove_device ( rmcon )
xmlobj . remove_device ( editdev )
2010-09-09 01:53:51 +04:00
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2010-09-09 01:53:51 +04:00
2014-03-22 22:34:23 +04:00
def define_cpu ( self , vcpus = _SENTINEL , maxvcpus = _SENTINEL ,
2015-11-18 23:05:21 +03:00
model = _SENTINEL , sockets = _SENTINEL ,
2014-03-22 22:34:23 +04:00
cores = _SENTINEL , threads = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
guest = self . _make_xmlobj_to_define ( )
2014-03-22 22:34:23 +04:00
2015-04-07 21:53:28 +03:00
if vcpus != _SENTINEL :
guest . curvcpus = int ( vcpus )
if maxvcpus != _SENTINEL :
guest . vcpus = int ( maxvcpus )
if sockets != _SENTINEL :
guest . cpu . sockets = sockets
guest . cpu . cores = cores
guest . cpu . threads = threads
if model != _SENTINEL :
if model in guest . cpu . SPECIAL_MODES :
guest . cpu . set_special_mode ( model )
else :
guest . cpu . model = model
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( guest )
2010-12-17 00:05:55 +03:00
2014-03-22 22:34:23 +04:00
def define_memory ( self , memory = _SENTINEL , maxmem = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
guest = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
if memory != _SENTINEL :
guest . memory = int ( memory )
if maxmem != _SENTINEL :
guest . maxmemory = int ( maxmem )
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( guest )
2009-09-25 17:52:03 +04:00
2014-03-22 22:34:23 +04:00
def define_overview ( self , machine = _SENTINEL , description = _SENTINEL ,
2014-09-18 01:25:03 +04:00
title = _SENTINEL , idmap_list = _SENTINEL , loader = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
guest = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
if machine != _SENTINEL :
guest . os . machine = machine
if description != _SENTINEL :
guest . description = description or None
if title != _SENTINEL :
guest . title = title or None
if loader != _SENTINEL :
if loader is None :
# Implies seabios, aka the default, so clear everything
guest . os . loader = None
guest . os . loader_ro = None
guest . os . loader_type = None
guest . os . nvram = None
guest . os . nvram_template = None
else :
# Implies UEFI
guest . os . loader = loader
guest . os . loader_type = " pflash "
guest . os . loader_ro = True
if idmap_list != _SENTINEL :
if idmap_list is not None :
# pylint: disable=unpacking-non-sequence
( uid_target , uid_count , gid_target , gid_count ) = idmap_list
guest . idmap . uid_start = 0
guest . idmap . uid_target = uid_target
guest . idmap . uid_count = uid_count
guest . idmap . gid_start = 0
guest . idmap . gid_target = gid_target
guest . idmap . gid_count = gid_count
else :
guest . idmap . clear ( )
2009-09-25 17:52:03 +04:00
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( guest )
2013-09-24 14:57:32 +04:00
2014-03-22 22:34:23 +04:00
def define_boot ( self , boot_order = _SENTINEL , boot_menu = _SENTINEL ,
kernel = _SENTINEL , initrd = _SENTINEL , dtb = _SENTINEL ,
2014-06-01 01:34:23 +04:00
kernel_args = _SENTINEL , init = _SENTINEL , initargs = _SENTINEL ) :
2014-02-10 01:21:26 +04:00
2015-04-11 00:23:11 +03:00
guest = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
def _change_boot_order ( ) :
2014-03-22 22:34:23 +04:00
boot_dev_order = [ ]
devmap = dict ( ( dev . vmmidstr , dev ) for dev in
self . get_bootable_devices ( ) )
for b in boot_order :
if b in devmap :
boot_dev_order . append ( devmap [ b ] )
2014-02-10 01:21:26 +04:00
# Unset the traditional boot order
guest . os . bootorder = [ ]
2014-03-22 22:34:23 +04:00
# Unset device boot order
2014-02-10 01:21:26 +04:00
for dev in guest . get_all_devices ( ) :
dev . boot . order = None
count = 1
for origdev in boot_dev_order :
2015-04-11 00:14:08 +03:00
dev = self . _lookup_device_to_define ( guest , origdev , False )
2014-02-10 01:21:26 +04:00
if not dev :
continue
dev . boot . order = count
count + = 1
2015-04-07 21:53:28 +03:00
if boot_order != _SENTINEL :
if self . can_use_device_boot_order ( ) :
_change_boot_order ( )
else :
guest . os . bootorder = boot_order
if boot_menu != _SENTINEL :
guest . os . enable_bootmenu = bool ( boot_menu )
if init != _SENTINEL :
guest . os . init = init
guest . os . set_initargs_string ( initargs )
if kernel != _SENTINEL :
guest . os . kernel = kernel or None
if initrd != _SENTINEL :
guest . os . initrd = initrd or None
if dtb != _SENTINEL :
guest . os . dtb = dtb or None
if kernel_args != _SENTINEL :
guest . os . kernel_args = kernel_args or None
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( guest )
2010-09-09 01:53:51 +04:00
2015-04-11 00:14:08 +03:00
######################
# Device XML editing #
######################
def define_disk ( self , devobj , do_hotplug ,
2014-03-22 22:34:23 +04:00
path = _SENTINEL , readonly = _SENTINEL , serial = _SENTINEL ,
shareable = _SENTINEL , removable = _SENTINEL , cache = _SENTINEL ,
io = _SENTINEL , driver_type = _SENTINEL , bus = _SENTINEL , addrstr = _SENTINEL ,
2015-03-13 16:39:47 +03:00
sgio = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
def _change_bus ( ) :
2012-01-30 00:38:33 +04:00
oldprefix = editdev . get_target_prefix ( ) [ 0 ]
oldbus = editdev . bus
2014-03-22 22:34:23 +04:00
editdev . bus = bus
2012-01-30 00:32:25 +04:00
2014-03-22 22:34:23 +04:00
if oldbus == bus :
2012-01-30 00:32:25 +04:00
return
editdev . address . clear ( )
2014-03-22 22:34:23 +04:00
editdev . address . set_addrstr ( addrstr )
2012-01-30 00:32:25 +04:00
2012-01-30 00:38:33 +04:00
if oldprefix == editdev . get_target_prefix ( ) [ 0 ] :
return
2012-01-30 00:32:25 +04:00
used = [ ]
disks = ( self . get_disk_devices ( ) +
self . get_disk_devices ( inactive = True ) )
for d in disks :
used . append ( d . target )
if editdev . target :
used . remove ( editdev . target )
editdev . target = None
editdev . generate_target ( used )
2009-09-25 17:52:03 +04:00
2015-04-11 00:14:08 +03:00
if path != _SENTINEL :
editdev . path = path
if not do_hotplug :
editdev . sync_path_props ( )
if readonly != _SENTINEL :
editdev . read_only = readonly
if shareable != _SENTINEL :
editdev . shareable = shareable
if removable != _SENTINEL :
editdev . removable = removable
if cache != _SENTINEL :
editdev . driver_cache = cache or None
if io != _SENTINEL :
editdev . driver_io = io or None
if driver_type != _SENTINEL :
editdev . driver_type = driver_type or None
if serial != _SENTINEL :
editdev . serial = serial or None
if sgio != _SENTINEL :
editdev . sgio = sgio or None
if bus != _SENTINEL :
_change_bus ( )
if do_hotplug :
2015-08-11 01:30:12 +03:00
self . hotplug ( device = editdev )
2015-04-11 00:14:08 +03:00
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_network ( self , devobj , do_hotplug ,
2014-03-22 22:34:23 +04:00
ntype = _SENTINEL , source = _SENTINEL ,
mode = _SENTINEL , model = _SENTINEL , addrstr = _SENTINEL ,
vtype = _SENTINEL , managerid = _SENTINEL , typeid = _SENTINEL ,
2014-05-31 23:51:24 +04:00
typeidversion = _SENTINEL , instanceid = _SENTINEL ,
2015-04-08 01:52:35 +03:00
portgroup = _SENTINEL , macaddr = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if ntype != _SENTINEL :
editdev . source = None
editdev . type = ntype
editdev . source = source
editdev . source_mode = mode or None
editdev . portgroup = portgroup or None
if model != _SENTINEL :
if editdev . model != model :
editdev . address . clear ( )
editdev . address . set_addrstr ( addrstr )
editdev . model = model
2013-02-17 17:40:37 +04:00
2015-04-11 00:14:08 +03:00
if vtype != _SENTINEL :
editdev . virtualport . type = vtype or None
editdev . virtualport . managerid = managerid or None
editdev . virtualport . typeid = typeid or None
editdev . virtualport . typeidversion = typeidversion or None
editdev . virtualport . instanceid = instanceid or None
if macaddr != _SENTINEL :
editdev . macaddr = macaddr
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_graphics ( self , devobj , do_hotplug ,
2014-03-22 22:34:23 +04:00
listen = _SENTINEL , port = _SENTINEL , tlsport = _SENTINEL ,
passwd = _SENTINEL , keymap = _SENTINEL , gtype = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if listen != _SENTINEL :
editdev . listen = listen
if port != _SENTINEL :
editdev . port = port
if tlsport != _SENTINEL :
editdev . tlsPort = tlsport
if passwd != _SENTINEL :
editdev . passwd = passwd
if keymap != _SENTINEL :
editdev . keymap = keymap
if gtype != _SENTINEL :
editdev . type = gtype
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2011-05-19 20:13:20 +04:00
2015-04-11 00:14:08 +03:00
def define_sound ( self , devobj , do_hotplug , model = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if model != _SENTINEL :
if editdev . model != model :
editdev . address . clear ( )
editdev . model = model
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_video ( self , devobj , do_hotplug , model = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if model != _SENTINEL and model != editdev . model :
2014-03-22 22:34:23 +04:00
editdev . model = model
2012-01-30 00:40:53 +04:00
editdev . address . clear ( )
2011-05-04 20:45:27 +04:00
# Clear out heads/ram values so they reset to default. If
# we ever allow editing these values in the UI we should
# drop this
editdev . vram = None
editdev . heads = None
2013-05-28 02:50:25 +04:00
editdev . ram = None
2014-12-01 17:56:29 +03:00
editdev . vgamem = None
2011-05-04 20:45:27 +04:00
2015-04-11 00:14:08 +03:00
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2009-09-25 17:52:03 +04:00
2015-04-11 00:14:08 +03:00
def define_watchdog ( self , devobj , do_hotplug ,
2014-03-22 22:34:23 +04:00
model = _SENTINEL , action = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if model != _SENTINEL :
if editdev . model != model :
editdev . address . clear ( )
editdev . model = model
if action != _SENTINEL :
editdev . action = action
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_smartcard ( self , devobj , do_hotplug , model = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
if model != _SENTINEL :
editdev . mode = model
editdev . type = editdev . TYPE_DEFAULT
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_controller ( self , devobj , do_hotplug , model = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
def _change_model ( ) :
2014-01-22 07:46:06 +04:00
if editdev . type == " usb " :
2015-05-19 20:21:36 +03:00
ctrls = xmlobj . get_devices ( " controller " )
2014-01-22 07:46:06 +04:00
ctrls = [ x for x in ctrls if ( x . type ==
VirtualController . TYPE_USB ) ]
for dev in ctrls :
2015-05-19 20:21:36 +03:00
xmlobj . remove_device ( dev )
2014-01-22 07:46:06 +04:00
2014-03-22 22:34:23 +04:00
if model == " ich9-ehci1 " :
2014-01-22 07:46:06 +04:00
for dev in VirtualController . get_usb2_controllers (
2015-05-19 20:21:36 +03:00
xmlobj . conn ) :
xmlobj . add_device ( dev )
2014-01-22 07:46:06 +04:00
else :
2015-05-19 20:21:36 +03:00
dev = VirtualController ( xmlobj . conn )
2014-01-22 07:46:06 +04:00
dev . type = " usb "
2015-05-19 19:50:39 +03:00
dev . model = model
2015-05-19 20:21:36 +03:00
xmlobj . add_device ( dev )
2014-03-22 22:34:23 +04:00
2015-05-19 19:50:39 +03:00
else :
editdev . model = model
2014-03-22 22:34:23 +04:00
self . hotplug ( device = editdev )
2011-09-23 19:14:15 +04:00
2015-04-11 00:14:08 +03:00
if model != _SENTINEL :
_change_model ( )
2014-03-22 22:34:23 +04:00
2015-04-11 00:14:08 +03:00
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2011-09-23 19:14:15 +04:00
2015-04-11 00:14:08 +03:00
def define_filesystem ( self , devobj , do_hotplug , newdev = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
2014-03-22 22:34:23 +04:00
2015-04-11 00:14:08 +03:00
if newdev != _SENTINEL :
2014-03-22 22:34:23 +04:00
# pylint: disable=maybe-no-member
2014-01-21 13:05:31 +04:00
editdev . type = newdev . type
2015-11-19 05:12:35 +03:00
editdev . accessmode = newdev . accessmode
2014-01-21 13:05:31 +04:00
editdev . wrpolicy = newdev . wrpolicy
editdev . driver = newdev . driver
editdev . format = newdev . format
editdev . readonly = newdev . readonly
editdev . units = newdev . units
editdev . source = newdev . source
editdev . target = newdev . target
2015-04-11 00:14:08 +03:00
if do_hotplug :
self . hotplug ( device = editdev )
2015-09-20 23:48:30 +03:00
else :
self . _redefine_xmlobj ( xmlobj )
2015-04-11 00:14:08 +03:00
def define_hostdev ( self , devobj , do_hotplug , rom_bar = _SENTINEL ) :
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-11 00:14:08 +03:00
editdev = self . _lookup_device_to_define ( xmlobj , devobj , do_hotplug )
if not editdev :
return
2011-09-23 19:14:15 +04:00
2015-04-11 00:14:08 +03:00
if rom_bar != _SENTINEL :
editdev . rom_bar = rom_bar
2014-03-22 22:34:23 +04:00
2015-04-11 00:14:08 +03:00
if do_hotplug :
self . hotplug ( device = editdev )
else :
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2014-02-10 23:24:22 +04:00
2009-09-25 17:52:03 +04:00
2011-05-04 00:17:52 +04:00
####################
# Hotplug routines #
####################
2009-09-25 17:52:03 +04:00
2011-05-04 00:17:52 +04:00
def attach_device ( self , devobj ) :
"""
Hotplug device to running guest
"""
if not self . is_active ( ) :
return
2010-12-13 22:31:27 +03:00
2011-05-04 00:17:52 +04:00
devxml = devobj . get_xml_config ( )
2015-04-11 00:14:08 +03:00
logging . debug ( " attach_device with xml= \n %s " , devxml )
2011-05-04 00:17:52 +04:00
self . _backend . attachDevice ( devxml )
2010-12-13 22:31:27 +03:00
2011-05-04 00:17:52 +04:00
def detach_device ( self , devobj ) :
"""
Hotunplug device from running guest
"""
if not self . is_active ( ) :
return
2009-09-25 17:52:03 +04:00
2015-04-11 00:14:08 +03:00
devxml = devobj . get_xml_config ( )
logging . debug ( " detach_device with xml= \n %s " , devxml )
self . _backend . detachDevice ( devxml )
2009-09-25 17:52:03 +04:00
2014-03-22 22:34:23 +04:00
def _update_device ( self , devobj , flags = None ) :
if flags is None :
flags = getattr ( libvirt , " VIR_DOMAIN_DEVICE_MODIFY_LIVE " , 1 )
2009-09-25 17:52:03 +04:00
2011-05-04 00:17:52 +04:00
xml = devobj . get_xml_config ( )
2015-04-11 00:14:08 +03:00
logging . debug ( " update_device with xml= \n %s " , xml )
2011-05-04 00:17:52 +04:00
self . _backend . updateDeviceFlags ( xml , flags )
2009-09-25 17:52:03 +04:00
2014-03-22 22:34:23 +04:00
def hotplug ( self , vcpus = _SENTINEL , memory = _SENTINEL , maxmem = _SENTINEL ,
2015-08-11 01:30:12 +03:00
description = _SENTINEL , title = _SENTINEL , device = _SENTINEL ) :
2014-03-22 22:34:23 +04:00
if not self . is_active ( ) :
return
def _hotplug_memory ( val ) :
if val != self . get_memory ( ) :
self . _backend . setMemory ( val )
def _hotplug_maxmem ( val ) :
if val != self . maximum_memory ( ) :
self . _backend . setMaxMemory ( val )
2010-05-12 20:57:32 +04:00
2014-03-22 22:34:23 +04:00
def _hotplug_metadata ( val , mtype ) :
flags = ( libvirt . VIR_DOMAIN_AFFECT_LIVE |
libvirt . VIR_DOMAIN_AFFECT_CONFIG )
self . _backend . setMetadata ( mtype , val , None , None , flags )
2011-05-04 00:17:52 +04:00
2014-03-22 22:34:23 +04:00
if vcpus != _SENTINEL :
vcpus = int ( vcpus )
if vcpus != self . vcpu_count ( ) :
self . _backend . setVcpus ( vcpus )
2011-05-04 00:17:52 +04:00
2014-03-22 22:34:23 +04:00
if memory != _SENTINEL :
logging . info ( " Hotplugging curmem= %s maxmem= %s for VM ' %s ' " ,
memory , maxmem , self . get_name ( ) )
2011-05-04 00:17:52 +04:00
actual_cur = self . get_memory ( )
if memory :
if maxmem < actual_cur :
# Set current first to avoid error
2014-03-22 22:34:23 +04:00
_hotplug_memory ( memory )
_hotplug_maxmem ( maxmem )
2011-05-04 00:17:52 +04:00
else :
2014-03-22 22:34:23 +04:00
_hotplug_maxmem ( maxmem )
_hotplug_memory ( memory )
2011-05-04 00:17:52 +04:00
else :
2014-03-22 22:34:23 +04:00
_hotplug_maxmem ( maxmem )
2012-10-17 01:26:06 +04:00
2014-03-22 22:34:23 +04:00
if description != _SENTINEL :
_hotplug_metadata ( description ,
libvirt . VIR_DOMAIN_METADATA_DESCRIPTION )
if title != _SENTINEL :
_hotplug_metadata ( title , libvirt . VIR_DOMAIN_METADATA_TITLE )
2012-10-17 01:26:06 +04:00
2015-08-11 01:30:12 +03:00
if device != _SENTINEL :
2014-03-22 22:34:23 +04:00
self . _update_device ( device )
2013-09-24 14:57:32 +04:00
2011-05-04 00:17:52 +04:00
########################
# Libvirt API wrappers #
########################
2015-04-10 16:15:44 +03:00
def _conn_tick_poll_param ( self ) :
return " pollvm "
def class_name ( self ) :
return " domain "
2011-05-04 00:17:52 +04:00
def _define ( self , newxml ) :
2011-07-23 00:43:26 +04:00
self . conn . define_domain ( newxml )
2011-05-04 00:17:52 +04:00
def _XMLDesc ( self , flags ) :
return self . _backend . XMLDesc ( flags )
2015-04-10 01:02:42 +03:00
def _get_backend_status ( self ) :
return self . _backend . info ( ) [ 0 ]
2011-05-04 00:17:52 +04:00
def get_autostart ( self ) :
2014-09-12 18:15:09 +04:00
if self . _autostart is None :
self . _autostart = self . _backend . autostart ( )
return self . _autostart
2011-05-04 00:17:52 +04:00
def set_autostart ( self , val ) :
self . _backend . setAutostart ( val )
2014-09-12 18:15:09 +04:00
# Recache value
self . _autostart = None
self . get_autostart ( )
2011-05-04 00:17:52 +04:00
def job_info ( self ) :
return self . _backend . jobInfo ( )
def abort_job ( self ) :
self . _backend . abortJob ( )
2009-09-25 17:52:03 +04:00
2011-06-21 19:04:48 +04:00
def open_console ( self , devname , stream , flags = 0 ) :
return self . _backend . openConsole ( devname , stream , flags )
2009-09-25 17:52:03 +04:00
2016-03-04 14:31:54 +03:00
def open_graphics_fd ( self ) :
2016-05-08 01:14:58 +03:00
return self . _backend . openGraphicsFD ( 0 )
2016-03-04 14:31:54 +03:00
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
def refresh_snapshots ( self ) :
self . _snapshot_list = None
def list_snapshots ( self ) :
if self . _snapshot_list is None :
newlist = [ ]
for rawsnap in self . _backend . listAllSnapshots ( ) :
2015-04-10 21:08:25 +03:00
obj = vmmDomainSnapshot ( self . conn , rawsnap )
obj . init_libvirt_state ( )
newlist . append ( obj )
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
self . _snapshot_list = newlist
return self . _snapshot_list [ : ]
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
def revert_to_snapshot ( self , snap ) :
self . _backend . revertToSnapshot ( snap . get_backend ( ) )
def create_snapshot ( self , xml , redefine = False ) :
flags = 0
if redefine :
flags = ( flags | libvirt . VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE )
2013-09-30 22:28:43 +04:00
if not redefine :
logging . debug ( " Creating snapshot flags= %s xml= \n %s " , flags , xml )
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 18:18:47 +04:00
self . _backend . snapshotCreateXML ( xml , flags )
2011-05-04 00:17:52 +04:00
########################
# XML Parsing routines #
########################
2011-05-26 20:38:48 +04:00
def is_container ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . is_container ( )
2011-05-26 20:38:48 +04:00
def is_xenpv ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . is_xenpv ( )
2011-05-26 20:38:48 +04:00
def is_hvm ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . is_hvm ( )
2011-05-26 20:38:48 +04:00
2011-05-04 00:17:52 +04:00
def get_uuid ( self ) :
2014-06-03 01:17:47 +04:00
if self . _uuid is None :
self . _uuid = self . _backend . UUIDString ( )
return self . _uuid
2011-05-04 00:17:52 +04:00
def get_abi_type ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . os_type
2011-05-04 00:17:52 +04:00
def get_hv_type ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . type
2010-02-07 20:10:41 +03:00
def get_pretty_hv_type ( self ) :
2014-01-27 02:42:24 +04:00
return self . conn . pretty_hv ( self . get_abi_type ( ) , self . get_hv_type ( ) )
2010-02-07 20:10:41 +03:00
def get_arch ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . arch
2011-05-26 20:38:48 +04:00
def get_init ( self ) :
2014-06-01 01:34:23 +04:00
import pipes
init = self . get_xmlobj ( ) . os . init
initargs = " " . join (
[ pipes . quote ( i . val ) for i in self . get_xmlobj ( ) . os . initargs ] )
return init , initargs
2010-02-07 20:10:41 +03:00
def get_emulator ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . emulator
2012-01-05 13:35:40 +04:00
def get_machtype ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . machine
2014-02-21 14:13:10 +04:00
def get_idmap ( self ) :
return self . get_xmlobj ( ) . idmap
2009-09-25 17:52:03 +04:00
2014-02-12 00:03:42 +04:00
def get_name_or_title ( self ) :
title = self . get_title ( )
if title :
return title
return self . get_name ( )
def get_title ( self ) :
2014-09-12 16:57:13 +04:00
return self . get_xmlobj ( ) . title
2010-02-10 20:16:59 +03:00
def get_description ( self ) :
2014-09-12 16:57:13 +04:00
return self . get_xmlobj ( ) . description
2010-02-10 20:16:59 +03:00
2010-11-29 20:55:22 +03:00
def get_memory ( self ) :
2013-09-23 16:34:50 +04:00
return int ( self . get_xmlobj ( ) . memory )
2010-11-29 20:55:22 +03:00
def maximum_memory ( self ) :
2013-09-23 16:34:50 +04:00
return int ( self . get_xmlobj ( ) . maxmemory )
2010-11-29 20:55:22 +03:00
def vcpu_count ( self ) :
2014-02-12 01:23:51 +04:00
return int ( self . get_xmlobj ( ) . curvcpus or self . get_xmlobj ( ) . vcpus )
2010-02-07 20:10:41 +03:00
def vcpu_max_count ( self ) :
2014-02-12 01:23:51 +04:00
return int ( self . get_xmlobj ( ) . vcpus )
2009-11-15 23:17:03 +03:00
2010-12-17 00:05:55 +03:00
def get_cpu_config ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . cpu
2010-12-17 00:05:55 +03:00
2014-02-10 01:21:26 +04:00
def _convert_old_boot_order ( self ) :
boot_order = self . _get_old_boot_order ( )
ret = [ ]
disks = self . get_disk_devices ( )
nets = self . get_network_devices ( )
for b in boot_order :
if b == " network " :
ret + = [ n . vmmidstr for n in nets ]
if b == " hd " :
ret + = [ d . vmmidstr for d in disks if
d . device not in [ " cdrom " , " floppy " ] ]
if b == " cdrom " :
ret + = [ d . vmmidstr for d in disks if d . device == " cdrom " ]
if b == " floppy " :
ret + = [ d . vmmidstr for d in disks if d . device == " floppy " ]
return ret
def _get_device_boot_order ( self ) :
devs = self . get_bootable_devices ( )
order = [ ]
for dev in devs :
if not dev . boot . order :
continue
order . append ( ( dev . vmmidstr , dev . boot . order ) )
if not order :
# No devices individually marked bootable, convert traditional
# boot XML to fine grained, for the UI.
return self . _convert_old_boot_order ( )
order . sort ( key = lambda p : p [ 1 ] )
return [ p [ 0 ] for p in order ]
def _get_old_boot_order ( self ) :
2013-09-23 16:34:50 +04:00
return self . get_xmlobj ( ) . os . bootorder
2014-02-10 01:21:26 +04:00
def get_boot_order ( self ) :
if self . can_use_device_boot_order ( ) :
return self . _get_device_boot_order ( )
return self . _get_old_boot_order ( )
2010-12-12 06:00:52 +03:00
def get_boot_menu ( self ) :
2013-09-23 16:34:50 +04:00
guest = self . get_xmlobj ( )
2013-07-17 15:53:47 +04:00
return bool ( guest . os . enable_bootmenu )
2010-12-12 07:00:19 +03:00
def get_boot_kernel_info ( self ) :
2013-09-23 16:34:50 +04:00
guest = self . get_xmlobj ( )
2013-08-18 20:25:20 +04:00
return ( guest . os . kernel , guest . os . initrd ,
guest . os . dtb , guest . os . kernel_args )
2010-12-12 07:00:19 +03:00
2011-05-04 00:17:52 +04:00
# XML Device listing
2009-09-25 17:52:03 +04:00
2010-02-07 20:10:41 +03:00
def get_serial_devs ( self ) :
2010-09-04 01:56:40 +04:00
devs = self . get_char_devices ( )
devlist = [ ]
2009-11-04 19:07:03 +03:00
2013-04-12 00:32:00 +04:00
devlist + = [ x for x in devs if x . virtual_device_type == " serial " ]
devlist + = [ x for x in devs if x . virtual_device_type == " console " ]
2010-09-04 01:56:40 +04:00
return devlist
2010-02-13 00:33:43 +03:00
2010-09-08 02:38:01 +04:00
def _build_device_list ( self , device_type ,
2013-06-30 22:33:01 +04:00
refresh_if_nec = True , inactive = False ) :
2013-09-23 16:34:50 +04:00
guest = self . get_xmlobj ( refresh_if_nec = refresh_if_nec ,
2010-09-03 23:19:54 +04:00
inactive = inactive )
devs = guest . get_devices ( device_type )
2010-09-03 22:10:04 +04:00
2016-04-18 23:42:12 +03:00
for idx , dev in enumerate ( devs ) :
dev . vmmindex = idx
dev . vmmidstr = dev . virtual_device_type + ( " %.3d " % idx )
2010-09-08 02:38:01 +04:00
return devs
2013-06-30 22:33:01 +04:00
def get_network_devices ( self , refresh_if_nec = True ) :
return self . _build_device_list ( " interface " , refresh_if_nec )
2010-09-08 02:38:01 +04:00
def get_video_devices ( self ) :
return self . _build_device_list ( " video " )
def get_hostdev_devices ( self ) :
return self . _build_device_list ( " hostdev " )
def get_watchdog_devices ( self ) :
return self . _build_device_list ( " watchdog " )
def get_input_devices ( self ) :
return self . _build_device_list ( " input " )
def get_graphics_devices ( self ) :
return self . _build_device_list ( " graphics " )
def get_sound_devices ( self ) :
return self . _build_device_list ( " sound " )
2010-12-16 20:41:47 +03:00
def get_controller_devices ( self ) :
return self . _build_device_list ( " controller " )
2011-05-19 23:18:33 +04:00
def get_filesystem_devices ( self ) :
return self . _build_device_list ( " filesystem " )
2011-06-23 19:42:03 +04:00
def get_smartcard_devices ( self ) :
return self . _build_device_list ( " smartcard " )
2011-09-02 05:23:27 +04:00
def get_redirdev_devices ( self ) :
return self . _build_device_list ( " redirdev " )
2013-06-26 05:45:08 +04:00
def get_tpm_devices ( self ) :
return self . _build_device_list ( " tpm " )
2013-09-23 17:39:56 +04:00
def get_rng_devices ( self ) :
return self . _build_device_list ( " rng " )
2014-01-10 13:37:55 +04:00
def get_panic_devices ( self ) :
return self . _build_device_list ( " panic " )
2010-09-08 02:38:01 +04:00
2013-06-30 22:33:01 +04:00
def get_disk_devices ( self , refresh_if_nec = True , inactive = False ) :
devs = self . _build_device_list ( " disk " , refresh_if_nec , inactive )
2010-09-08 02:38:01 +04:00
2010-09-03 22:10:04 +04:00
# Iterate through all disks and calculate what number they are
# HACK: We are making a variable in VirtualDisk to store the index
idx_mapping = { }
2010-09-03 23:19:54 +04:00
for dev in devs :
devtype = dev . device
bus = dev . bus
2010-09-03 22:10:04 +04:00
key = devtype + ( bus or " " )
2010-12-10 19:47:07 +03:00
if key not in idx_mapping :
2010-09-03 22:10:04 +04:00
idx_mapping [ key ] = 1
2010-09-03 23:19:54 +04:00
dev . disk_bus_index = idx_mapping [ key ]
2010-09-03 22:10:04 +04:00
idx_mapping [ key ] + = 1
2010-09-03 23:19:54 +04:00
return devs
2009-09-25 17:52:03 +04:00
2010-02-07 20:10:41 +03:00
def get_char_devices ( self ) :
2010-09-04 01:56:40 +04:00
devs = [ ]
2010-09-08 02:38:01 +04:00
serials = self . _build_device_list ( " serial " )
parallels = self . _build_device_list ( " parallel " )
consoles = self . _build_device_list ( " console " )
2011-04-05 03:35:32 +04:00
channels = self . _build_device_list ( " channel " )
2010-09-04 01:56:40 +04:00
2011-04-05 03:35:32 +04:00
for devicelist in [ serials , parallels , consoles , channels ] :
2010-09-04 01:56:40 +04:00
devs . extend ( devicelist )
# Don't display <console> if it's just a duplicate of <serial>
if ( len ( consoles ) > 0 and len ( serials ) > 0 ) :
con = consoles [ 0 ]
ser = serials [ 0 ]
2013-07-16 17:14:37 +04:00
if ( con . type == ser . type and
2010-09-04 01:56:40 +04:00
con . target_type is None or con . target_type == " serial " ) :
2010-09-08 02:38:01 +04:00
ser . virtmanager_console_dup = con
2010-09-04 01:56:40 +04:00
devs . remove ( con )
return devs
2009-10-06 02:00:13 +04:00
2014-03-22 22:34:23 +04:00
def can_use_device_boot_order ( self ) :
# Return 'True' if guest can use new style boot device ordering
return self . conn . check_support (
self . conn . SUPPORT_CONN_DEVICE_BOOTORDER )
def get_bootable_devices ( self ) :
devs = self . get_disk_devices ( )
devs + = self . get_network_devices ( )
devs + = self . get_hostdev_devices ( )
# redirdev can also be marked bootable, but it should be rarely
# used and clutters the UI
return devs
2011-05-04 00:17:52 +04:00
############################
# Domain lifecycle methods #
############################
2012-02-10 23:07:51 +04:00
# All these methods are usually run asynchronously from threads, so
# let's be extra careful and have anything which might touch UI
2012-05-14 17:24:56 +04:00
# or GObject.props invoked in an idle callback
2012-02-10 23:07:51 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2011-05-04 00:17:52 +04:00
def shutdown ( self ) :
2012-02-10 23:07:51 +04:00
self . _install_abort = True
2011-05-04 00:17:52 +04:00
self . _backend . shutdown ( )
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2011-05-04 00:17:52 +04:00
def reboot ( self ) :
2012-02-10 23:07:51 +04:00
self . _install_abort = True
2011-05-04 00:17:52 +04:00
self . _backend . reboot ( 0 )
2012-02-10 23:07:51 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2012-02-10 23:07:51 +04:00
def destroy ( self ) :
self . _install_abort = True
self . _backend . destroy ( )
2011-05-04 00:17:52 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2013-03-17 01:59:32 +04:00
def reset ( self ) :
self . _install_abort = True
self . _backend . reset ( 0 )
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2011-05-04 00:17:52 +04:00
def startup ( self ) :
if self . get_cloning ( ) :
raise RuntimeError ( _ ( " Cannot start guest while cloning "
" operation in progress " ) )
2013-05-13 14:14:11 +04:00
pre_startup_ret = [ ]
self . emit ( " pre-startup " , pre_startup_ret )
for error in pre_startup_ret :
raise RuntimeError ( error )
2011-05-04 00:17:52 +04:00
self . _backend . create ( )
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2011-05-04 00:17:52 +04:00
def suspend ( self ) :
self . _backend . suspend ( )
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2013-09-30 23:23:14 +04:00
def delete ( self , force = True ) :
2013-09-30 23:36:09 +04:00
flags = 0
2013-09-30 23:23:14 +04:00
if force :
2013-09-30 23:36:09 +04:00
flags | = getattr ( libvirt ,
" VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA " , 0 )
flags | = getattr ( libvirt , " VIR_DOMAIN_UNDEFINE_MANAGED_SAVE " , 0 )
2014-09-20 04:06:27 +04:00
if ( self . get_xmlobj ( ) . os . loader_ro is True and
2014-09-21 15:12:48 +04:00
self . get_xmlobj ( ) . os . loader_type == " pflash " ) :
2014-09-18 02:33:30 +04:00
flags | = getattr ( libvirt , " VIR_DOMAIN_UNDEFINE_NVRAM " , 0 )
2013-09-30 23:36:09 +04:00
try :
self . _backend . undefineFlags ( flags )
2013-10-03 03:11:59 +04:00
except libvirt . libvirtError :
logging . exception ( " libvirt undefineFlags failed, "
2013-09-30 23:36:09 +04:00
" falling back to old style " )
self . _backend . undefine ( )
2011-05-04 00:17:52 +04:00
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2011-05-04 00:17:52 +04:00
def resume ( self ) :
if self . get_cloning ( ) :
raise RuntimeError ( _ ( " Cannot resume guest while cloning "
" operation in progress " ) )
self . _backend . resume ( )
2015-04-10 01:27:45 +03:00
@vmmLibvirtObject.lifecycle_action
2015-04-11 20:52:48 +03:00
def save ( self , meter = None ) :
2015-04-10 01:27:45 +03:00
self . _install_abort = True
if meter :
start_job_progress_thread ( self , meter , _ ( " Saving domain to disk " ) )
2015-04-11 20:52:48 +03:00
self . _backend . managedSave ( 0 )
2011-05-04 00:17:52 +04:00
2014-09-12 02:01:41 +04:00
def has_managed_save ( self ) :
2011-05-04 00:17:52 +04:00
if not self . managedsave_supported :
return False
2014-09-12 02:01:41 +04:00
if self . _has_managed_save is None :
try :
self . _has_managed_save = self . _backend . hasManagedSaveImage ( 0 )
except libvirt . libvirtError , e :
if not util . exception_is_libvirt_error ( e , " VIR_ERR_NO_DOMAIN " ) :
raise
return False
return self . _has_managed_save
def remove_saved_image ( self ) :
if not self . has_managed_save ( ) :
2012-07-09 00:06:16 +04:00
return
self . _backend . managedSaveRemove ( 0 )
2014-09-12 02:01:41 +04:00
self . _has_managed_save = None
2012-07-09 00:06:16 +04:00
2011-05-04 00:17:52 +04:00
2015-04-16 01:21:24 +03:00
def migrate ( self , destconn , interface = None ,
2015-04-22 00:32:43 +03:00
secure = False , unsafe = False , temporary = False , meter = None ) :
2012-02-10 23:07:51 +04:00
self . _install_abort = True
2011-05-04 00:17:52 +04:00
flags = 0
2015-04-16 01:40:28 +03:00
flags | = libvirt . VIR_MIGRATE_LIVE
2015-04-22 00:32:43 +03:00
if not temporary :
flags | = libvirt . VIR_MIGRATE_PERSIST_DEST
flags | = libvirt . VIR_MIGRATE_UNDEFINE_SOURCE
2011-05-04 00:17:52 +04:00
if secure :
flags | = libvirt . VIR_MIGRATE_PEER2PEER
flags | = libvirt . VIR_MIGRATE_TUNNELLED
2013-12-11 01:33:12 +04:00
if unsafe :
flags | = libvirt . VIR_MIGRATE_UNSAFE
2014-09-25 20:19:01 +04:00
libvirt_destconn = destconn . get_backend ( ) . get_conn_for_api_arg ( )
2015-04-22 00:32:43 +03:00
logging . debug ( " Migrating: conn= %s flags= %s uri= %s secure= %s "
" unsafe= %s temporary= %s " ,
destconn , flags , interface , secure , unsafe , temporary )
2011-05-04 00:17:52 +04:00
if meter :
start_job_progress_thread ( self , meter , _ ( " Migrating domain " ) )
2016-08-31 20:26:24 +03:00
params = { }
if interface :
params [ libvirt . VIR_MIGRATE_PARAM_URI ] = interface
self . _backend . migrate3 ( libvirt_destconn , params , flags )
2012-02-10 23:07:51 +04:00
2013-07-07 19:06:15 +04:00
# Don't schedule any conn update, migrate dialog handles it for us
2011-05-04 00:17:52 +04:00
2014-01-13 02:39:21 +04:00
#################
# Stats helpers #
#################
2009-11-04 16:46:09 +03:00
2010-02-07 20:10:41 +03:00
def _sample_cpu_stats ( self , info , now ) :
2014-02-12 00:29:30 +04:00
if not self . _enable_cpu_stats :
return 0 , 0 , 0 , 0
2015-04-19 16:26:16 +03:00
if not info :
info = self . _backend . info ( )
2014-02-12 00:29:30 +04:00
2010-02-07 20:10:41 +03:00
prevCpuTime = 0
prevTimestamp = 0
2011-07-12 05:22:50 +04:00
cpuTime = 0
cpuTimeAbs = 0
pcentHostCpu = 0
2011-07-12 16:49:47 +04:00
pcentGuestCpu = 0
2011-07-12 05:22:50 +04:00
2015-05-05 01:16:09 +03:00
if len ( self . _stats ) > 0 :
prevTimestamp = self . _stats [ 0 ] [ " timestamp " ]
prevCpuTime = self . _stats [ 0 ] [ " cpuTimeAbs " ]
2009-11-04 16:46:09 +03:00
2010-02-07 20:10:41 +03:00
if not ( info [ 0 ] in [ libvirt . VIR_DOMAIN_SHUTOFF ,
libvirt . VIR_DOMAIN_CRASHED ] ) :
2011-08-23 23:47:31 +04:00
guestcpus = info [ 3 ]
2010-02-07 20:10:41 +03:00
cpuTime = info [ 4 ] - prevCpuTime
cpuTimeAbs = info [ 4 ]
2011-07-23 00:43:26 +04:00
hostcpus = self . conn . host_active_processor_count ( )
2009-11-05 00:30:51 +03:00
2011-07-12 16:49:47 +04:00
pcentbase = ( ( ( cpuTime ) * 100.0 ) /
( ( now - prevTimestamp ) * 1000.0 * 1000.0 * 1000.0 ) )
pcentHostCpu = pcentbase / hostcpus
2015-02-25 18:27:16 +03:00
# Under RHEL-5.9 using a XEN HV guestcpus can be 0 during shutdown
# so play safe and check it.
pcentGuestCpu = guestcpus > 0 and pcentbase / guestcpus or 0
2009-11-05 00:30:51 +03:00
2011-07-12 05:22:50 +04:00
pcentHostCpu = max ( 0.0 , min ( 100.0 , pcentHostCpu ) )
2011-07-12 16:49:47 +04:00
pcentGuestCpu = max ( 0.0 , min ( 100.0 , pcentGuestCpu ) )
2011-07-12 05:22:50 +04:00
2011-07-12 16:49:47 +04:00
return cpuTime , cpuTimeAbs , pcentHostCpu , pcentGuestCpu
2009-11-05 00:30:51 +03:00
2010-02-07 20:10:41 +03:00
def _get_cur_rate ( self , what ) :
2015-05-05 01:16:09 +03:00
if len ( self . _stats ) > 1 :
ret = ( float ( self . _stats [ 0 ] [ what ] -
self . _stats [ 1 ] [ what ] ) /
float ( self . _stats [ 0 ] [ " timestamp " ] -
self . _stats [ 1 ] [ " timestamp " ] ) )
2010-02-07 20:10:41 +03:00
else :
ret = 0.0
2013-04-13 22:34:52 +04:00
return max ( ret , 0 , 0 ) # avoid negative values at poweroff
2009-11-05 22:59:11 +03:00
2010-02-07 20:10:41 +03:00
def _set_max_rate ( self , record , what ) :
2015-05-05 01:16:09 +03:00
if record [ what ] > self . _stats_rates [ what ] :
self . _stats_rates [ what ] = record [ what ]
2011-07-25 23:09:42 +04:00
def _get_max_rate ( self , name1 , name2 ) :
2015-05-05 01:16:09 +03:00
return float ( max ( self . _stats_rates [ name1 ] , self . _stats_rates [ name2 ] ) )
2009-11-05 00:30:51 +03:00
2011-05-04 00:17:52 +04:00
def _get_record_helper ( self , record_name ) :
2015-05-05 01:16:09 +03:00
if len ( self . _stats ) == 0 :
2011-05-04 00:17:52 +04:00
return 0
2015-05-05 01:16:09 +03:00
return self . _stats [ 0 ] [ record_name ]
2006-06-27 22:15:28 +04:00
2015-05-04 23:33:56 +03:00
def _vector_helper ( self , record_name , limit , ceil = 100.0 ) :
2010-02-07 20:10:41 +03:00
vector = [ ]
2015-05-04 23:33:56 +03:00
statslen = self . config . get_stats_history_length ( ) + 1
if limit is not None :
statslen = min ( statslen , limit )
for i in range ( statslen ) :
2015-05-05 01:16:09 +03:00
if i < len ( self . _stats ) :
vector . append ( self . _stats [ i ] [ record_name ] / ceil )
2010-02-07 20:10:41 +03:00
else :
vector . append ( 0 )
2015-05-04 23:33:56 +03:00
2010-02-07 20:10:41 +03:00
return vector
2007-04-11 22:48:36 +04:00
2015-05-04 23:33:56 +03:00
def _in_out_vector_helper ( self , name1 , name2 , limit , ceil ) :
2011-07-25 23:09:42 +04:00
if ceil is None :
ceil = self . _get_max_rate ( name1 , name2 )
2009-07-09 22:35:55 +04:00
2015-05-04 23:33:56 +03:00
return ( self . _vector_helper ( name1 , limit , ceil = ceil ) ,
self . _vector_helper ( name2 , limit , ceil = ceil ) )
2014-02-12 00:29:30 +04:00
2010-03-24 07:22:17 +03:00
2011-05-04 00:17:52 +04:00
###################
# Stats accessors #
###################
2010-03-24 07:22:17 +03:00
2011-05-04 00:17:52 +04:00
def stats_memory ( self ) :
return self . _get_record_helper ( " curmem " )
def cpu_time ( self ) :
return self . _get_record_helper ( " cpuTime " )
2011-07-12 05:22:50 +04:00
def host_cpu_time_percentage ( self ) :
return self . _get_record_helper ( " cpuHostPercent " )
2011-07-12 16:49:47 +04:00
def guest_cpu_time_percentage ( self ) :
return self . _get_record_helper ( " cpuGuestPercent " )
2011-05-04 00:17:52 +04:00
def network_rx_rate ( self ) :
return self . _get_record_helper ( " netRxRate " )
def network_tx_rate ( self ) :
return self . _get_record_helper ( " netTxRate " )
def disk_read_rate ( self ) :
return self . _get_record_helper ( " diskRdRate " )
def disk_write_rate ( self ) :
return self . _get_record_helper ( " diskWrRate " )
2010-03-24 07:22:17 +03:00
2011-05-04 00:17:52 +04:00
def network_traffic_rate ( self ) :
return self . network_tx_rate ( ) + self . network_rx_rate ( )
2011-07-25 23:09:42 +04:00
def network_traffic_max_rate ( self ) :
return self . _get_max_rate ( " netRxRate " , " netTxRate " )
2011-05-04 00:17:52 +04:00
def disk_io_rate ( self ) :
return self . disk_read_rate ( ) + self . disk_write_rate ( )
2011-07-25 23:09:42 +04:00
def disk_io_max_rate ( self ) :
return self . _get_max_rate ( " diskRdRate " , " diskWrRate " )
2010-03-24 07:22:17 +03:00
2015-05-04 23:33:56 +03:00
def host_cpu_time_vector ( self , limit = None ) :
return self . _vector_helper ( " cpuHostPercent " , limit )
def guest_cpu_time_vector ( self , limit = None ) :
return self . _vector_helper ( " cpuGuestPercent " , limit )
def stats_memory_vector ( self , limit = None ) :
return self . _vector_helper ( " currMemPercent " , limit )
def network_traffic_vectors ( self , limit = None , ceil = None ) :
return self . _in_out_vector_helper (
" netRxRate " , " netTxRate " , limit , ceil )
def disk_io_vectors ( self , limit = None , ceil = None ) :
return self . _in_out_vector_helper (
" diskRdRate " , " diskWrRate " , limit , ceil )
2010-03-24 07:22:17 +03:00
2011-05-04 00:17:52 +04:00
###################
# Status helpers ##
###################
2009-07-09 22:35:55 +04:00
2011-05-04 00:17:52 +04:00
def _normalize_status ( self , status ) :
if status == libvirt . VIR_DOMAIN_NOSTATE :
return libvirt . VIR_DOMAIN_RUNNING
elif status == libvirt . VIR_DOMAIN_BLOCKED :
return libvirt . VIR_DOMAIN_RUNNING
return status
def is_active ( self ) :
return not self . is_shutoff ( )
def is_shutoff ( self ) :
return self . status ( ) == libvirt . VIR_DOMAIN_SHUTOFF
def is_crashed ( self ) :
return self . status ( ) == libvirt . VIR_DOMAIN_CRASHED
def is_stoppable ( self ) :
return self . status ( ) in [ libvirt . VIR_DOMAIN_RUNNING ,
2014-02-12 01:40:37 +04:00
libvirt . VIR_DOMAIN_PAUSED ,
libvirt . VIR_DOMAIN_PMSUSPENDED ]
2011-05-04 00:17:52 +04:00
def is_destroyable ( self ) :
return ( self . is_stoppable ( ) or
self . status ( ) in [ libvirt . VIR_DOMAIN_CRASHED ] )
def is_runable ( self ) :
2015-04-12 17:24:35 +03:00
return self . is_shutoff ( ) or self . is_crashed ( )
2011-05-04 00:17:52 +04:00
def is_pauseable ( self ) :
return self . status ( ) in [ libvirt . VIR_DOMAIN_RUNNING ]
def is_unpauseable ( self ) :
return self . status ( ) in [ libvirt . VIR_DOMAIN_PAUSED ]
def is_paused ( self ) :
return self . status ( ) in [ libvirt . VIR_DOMAIN_PAUSED ]
2016-10-06 18:12:59 +03:00
def is_clonable ( self ) :
return self . status ( ) in [ libvirt . VIR_DOMAIN_SHUTOFF ,
libvirt . VIR_DOMAIN_PAUSED ,
libvirt . VIR_DOMAIN_PMSUSPENDED ]
2011-05-04 00:17:52 +04:00
2013-10-01 01:11:22 +04:00
def run_status ( self ) :
2014-09-12 02:01:41 +04:00
return self . pretty_run_status ( self . status ( ) , self . has_managed_save ( ) )
2014-03-18 18:04:59 +04:00
def run_status_reason ( self ) :
return self . pretty_status_reason ( self . status ( ) , self . status_reason ( ) )
2011-07-12 19:50:11 +04:00
def run_status_icon_name ( self ) :
2012-10-30 02:55:51 +04:00
status = self . status ( )
2014-01-27 02:09:07 +04:00
if status not in vm_status_icons :
2015-04-14 00:25:40 +03:00
logging . debug ( " Unknown status %s , using NOSTATE " , status )
2012-10-30 02:55:51 +04:00
status = libvirt . VIR_DOMAIN_NOSTATE
2014-01-27 02:09:07 +04:00
return vm_status_icons [ status ]
2011-05-04 00:17:52 +04:00
2013-04-18 01:39:25 +04:00
def inspection_data_updated ( self ) :
self . idle_emit ( " inspection-changed " )
2010-11-30 01:40:28 +03:00
2013-04-18 01:39:25 +04:00
##################
# config helpers #
##################
def on_console_scaling_changed ( self , * args , * * kwargs ) :
2014-06-03 01:17:47 +04:00
return self . config . listen_pervm ( self . get_uuid ( ) , " /scaling " ,
2013-04-18 01:39:25 +04:00
* args , * * kwargs )
2011-05-04 00:17:52 +04:00
def set_console_scaling ( self , value ) :
2014-06-03 01:17:47 +04:00
self . config . set_pervm ( self . get_uuid ( ) , " /scaling " , value )
2011-05-04 00:17:52 +04:00
def get_console_scaling ( self ) :
2014-06-03 01:17:47 +04:00
ret = self . config . get_pervm ( self . get_uuid ( ) , " /scaling " )
2013-04-18 01:39:25 +04:00
if ret == - 1 :
return self . config . get_console_scaling ( )
return ret
2010-11-30 01:40:28 +03:00
2014-01-31 18:13:53 +04:00
def on_console_resizeguest_changed ( self , * args , * * kwargs ) :
2014-06-03 01:17:47 +04:00
return self . config . listen_pervm ( self . get_uuid ( ) , " /resize-guest " ,
2014-01-31 18:13:53 +04:00
* args , * * kwargs )
def set_console_resizeguest ( self , value ) :
2014-06-03 01:17:47 +04:00
self . config . set_pervm ( self . get_uuid ( ) , " /resize-guest " , value )
2014-01-31 18:13:53 +04:00
def get_console_resizeguest ( self ) :
2014-06-03 01:17:47 +04:00
ret = self . config . get_pervm ( self . get_uuid ( ) , " /resize-guest " )
2014-01-31 18:13:53 +04:00
if ret == - 1 :
return self . config . get_console_resizeguest ( )
return ret
2011-05-04 00:17:52 +04:00
def set_details_window_size ( self , w , h ) :
2014-06-03 01:17:47 +04:00
self . config . set_pervm ( self . get_uuid ( ) , " /vm-window-size " , ( w , h ) )
2011-05-04 00:17:52 +04:00
def get_details_window_size ( self ) :
2014-06-03 01:17:47 +04:00
ret = self . config . get_pervm ( self . get_uuid ( ) , " /vm-window-size " )
2013-04-18 01:39:25 +04:00
return ret
2010-11-30 01:40:28 +03:00
2013-04-18 01:39:25 +04:00
def get_console_password ( self ) :
2014-06-03 01:17:47 +04:00
return self . config . get_pervm ( self . get_uuid ( ) , " /console-password " )
2013-04-18 01:39:25 +04:00
def set_console_password ( self , username , keyid ) :
2014-06-03 01:17:47 +04:00
return self . config . set_pervm ( self . get_uuid ( ) , " /console-password " ,
2013-04-18 01:39:25 +04:00
( username , keyid ) )
2016-06-07 16:25:55 +03:00
def del_console_password ( self ) :
return self . config . set_pervm ( self . get_uuid ( ) , " /console-password " ,
( " " , - 1 ) )
2011-07-18 22:53:55 +04:00
2015-05-04 23:33:56 +03:00
def _on_config_sample_network_traffic_changed ( self , ignore = None ) :
self . _enable_net_poll = self . config . get_stats_enable_net_poll ( )
def _on_config_sample_disk_io_changed ( self , ignore = None ) :
self . _enable_disk_poll = self . config . get_stats_enable_disk_poll ( )
def _on_config_sample_mem_stats_changed ( self , ignore = None ) :
self . _enable_mem_stats = self . config . get_stats_enable_memory_poll ( )
def _on_config_sample_cpu_stats_changed ( self , ignore = None ) :
self . _enable_cpu_stats = self . config . get_stats_enable_cpu_poll ( )
2013-10-01 05:41:10 +04:00
def get_cache_dir ( self ) :
ret = os . path . join ( self . conn . get_cache_dir ( ) , self . get_uuid ( ) )
if not os . path . exists ( ret ) :
2013-11-01 16:26:24 +04:00
os . makedirs ( ret , 0755 )
2013-10-01 05:41:10 +04:00
return ret
2011-05-04 00:17:52 +04:00
###################
# Polling helpers #
###################
2010-11-30 01:40:28 +03:00
def _sample_network_traffic ( self ) :
rx = 0
tx = 0
2011-07-27 20:02:29 +04:00
if ( not self . _stats_net_supported or
2011-04-11 18:21:19 +04:00
not self . _enable_net_poll or
not self . is_active ( ) ) :
2014-02-01 17:15:24 +04:00
self . _stats_net_skip = [ ]
2010-11-30 01:40:28 +03:00
return rx , tx
2013-06-30 22:33:01 +04:00
for netdev in self . get_network_devices ( refresh_if_nec = False ) :
2010-11-30 01:40:28 +03:00
dev = netdev . target_dev
if not dev :
continue
if dev in self . _stats_net_skip :
continue
try :
2011-05-04 00:17:52 +04:00
io = self . _backend . interfaceStats ( dev )
2010-11-30 01:40:28 +03:00
if io :
rx + = io [ 0 ]
tx + = io [ 4 ]
except libvirt . libvirtError , err :
2013-08-09 17:23:01 +04:00
if util . is_error_nosupport ( err ) :
2012-01-17 07:04:40 +04:00
logging . debug ( " Net stats not supported: %s " , err )
2010-11-30 01:40:28 +03:00
self . _stats_net_supported = False
else :
logging . error ( " Error reading net stats for "
2012-01-17 07:04:40 +04:00
" ' %s ' dev ' %s ' : %s " ,
self . get_name ( ) , dev , err )
2012-03-02 04:07:56 +04:00
if self . is_active ( ) :
logging . debug ( " Adding %s to skip list " , dev )
self . _stats_net_skip . append ( dev )
else :
logging . debug ( " Aren ' t running, don ' t add to skiplist " )
2010-11-30 01:40:28 +03:00
return rx , tx
def _sample_disk_io ( self ) :
rd = 0
wr = 0
2011-04-11 18:21:19 +04:00
if ( not self . _stats_disk_supported or
not self . _enable_disk_poll or
not self . is_active ( ) ) :
2014-02-01 17:15:24 +04:00
self . _stats_disk_skip = [ ]
2010-11-30 01:40:28 +03:00
return rd , wr
2014-02-21 14:38:41 +04:00
# Some drivers support this method for getting all usage at once
if not self . _summary_disk_stats_skip :
try :
io = self . _backend . blockStats ( ' ' )
if io :
rd = io [ 1 ]
wr = io [ 3 ]
return rd , wr
except libvirt . libvirtError :
self . _summary_disk_stats_skip = True
# did not work, iterate over all disks
2013-06-30 22:33:01 +04:00
for disk in self . get_disk_devices ( refresh_if_nec = False ) :
2010-11-30 01:40:28 +03:00
dev = disk . target
if not dev :
continue
if dev in self . _stats_disk_skip :
continue
try :
2011-05-04 00:17:52 +04:00
io = self . _backend . blockStats ( dev )
2010-11-30 01:40:28 +03:00
if io :
rd + = io [ 1 ]
wr + = io [ 3 ]
except libvirt . libvirtError , err :
2013-08-09 17:23:01 +04:00
if util . is_error_nosupport ( err ) :
2012-01-17 07:04:40 +04:00
logging . debug ( " Disk stats not supported: %s " , err )
2010-11-30 01:40:28 +03:00
self . _stats_disk_supported = False
else :
logging . error ( " Error reading disk stats for "
2012-01-17 07:04:40 +04:00
" ' %s ' dev ' %s ' : %s " ,
self . get_name ( ) , dev , err )
2012-03-02 04:07:56 +04:00
if self . is_active ( ) :
logging . debug ( " Adding %s to skip list " , dev )
self . _stats_disk_skip . append ( dev )
else :
logging . debug ( " Aren ' t running, don ' t add to skiplist " )
2010-11-30 01:40:28 +03:00
return rd , wr
2009-10-06 02:57:29 +04:00
2015-11-19 05:17:22 +03:00
def _set_mem_stats_period ( self ) :
# QEMU requires we explicitly enable memory stats polling per VM
# if we wan't fine grained memory stats
if not self . conn . check_support (
self . conn . SUPPORT_CONN_MEM_STATS_PERIOD ) :
return
# Only works for virtio balloon
if not any ( [ b for b in self . get_xmlobj ( ) . get_devices ( " memballoon " ) if
b . model == " virtio " ] ) :
return
try :
secs = 5
self . _backend . setMemoryStatsPeriod ( secs ,
libvirt . VIR_DOMAIN_AFFECT_LIVE )
except Exception , e :
logging . debug ( " Error setting memstats period: %s " , e )
2014-01-13 02:39:21 +04:00
def _sample_mem_stats ( self ) :
2014-01-13 02:52:13 +04:00
if ( not self . mem_stats_supported or
2014-01-13 02:39:21 +04:00
not self . _enable_mem_stats or
not self . is_active ( ) ) :
2015-11-19 05:17:22 +03:00
self . _mem_stats_period_is_set = False
2014-01-13 02:39:21 +04:00
return 0 , 0
2015-11-19 05:17:22 +03:00
if self . _mem_stats_period_is_set is False :
self . _set_mem_stats_period ( )
self . _mem_stats_period_is_set = True
2014-01-13 02:39:21 +04:00
curmem = 0
totalmem = 1
try :
stats = self . _backend . memoryStats ( )
2015-11-19 05:17:22 +03:00
totalmem = stats . get ( " actual " , 1 )
curmem = stats . get ( " rss " , 0 )
if " unused " in stats :
curmem = max ( 0 , totalmem - stats . get ( " unused " , totalmem ) )
2014-01-13 02:39:21 +04:00
except libvirt . libvirtError , err :
2014-01-13 02:52:13 +04:00
logging . error ( " Error reading mem stats: %s " , err )
2014-01-13 02:39:21 +04:00
2015-11-19 05:17:22 +03:00
pcentCurrMem = ( curmem / float ( totalmem ) ) * 100
2014-01-13 02:39:21 +04:00
pcentCurrMem = max ( 0.0 , min ( pcentCurrMem , 100.0 ) )
return pcentCurrMem , curmem
2013-07-07 17:42:21 +04:00
def tick ( self , stats_update = True ) :
2015-04-19 16:26:16 +03:00
if ( not self . _using_events ( ) and
not stats_update ) :
return
2014-02-11 21:07:13 +04:00
2014-02-12 00:29:30 +04:00
info = [ ]
2015-04-19 16:26:16 +03:00
dosignal = False
if not self . _using_events ( ) :
# For domains it's pretty important that we are always using
# the latest XML, but other objects probably don't want to do
# this since it could be a performance hit.
self . _invalidate_xml ( )
2014-02-12 00:29:30 +04:00
info = self . _backend . info ( )
2015-04-19 16:26:16 +03:00
dosignal = self . _refresh_status ( newstatus = info [ 0 ] , cansignal = False )
2013-07-07 17:42:21 +04:00
if stats_update :
self . _tick_stats ( info )
2015-04-19 16:26:16 +03:00
if dosignal :
self . idle_emit ( " state-changed " )
2013-07-07 17:42:21 +04:00
if stats_update :
self . idle_emit ( " resources-sampled " )
def _tick_stats ( self , info ) :
2010-02-07 20:10:41 +03:00
expected = self . config . get_stats_history_length ( )
2015-05-05 01:16:09 +03:00
current = len ( self . _stats )
2010-02-07 20:10:41 +03:00
if current > expected :
2015-05-05 01:16:09 +03:00
del self . _stats [ expected : current ]
2009-01-15 19:15:04 +03:00
2013-07-07 16:12:15 +04:00
now = time . time ( )
2011-07-12 16:49:47 +04:00
( cpuTime , cpuTimeAbs ,
pcentHostCpu , pcentGuestCpu ) = self . _sample_cpu_stats ( info , now )
2013-12-18 17:42:43 +04:00
pcentCurrMem , curmem = self . _sample_mem_stats ( )
2011-04-11 18:21:19 +04:00
rdBytes , wrBytes = self . _sample_disk_io ( )
rxBytes , txBytes = self . _sample_network_traffic ( )
2009-06-18 18:38:15 +04:00
2010-12-10 19:47:07 +03:00
newStats = {
" timestamp " : now ,
" cpuTime " : cpuTime ,
" cpuTimeAbs " : cpuTimeAbs ,
2011-07-12 05:22:50 +04:00
" cpuHostPercent " : pcentHostCpu ,
2011-07-12 16:49:47 +04:00
" cpuGuestPercent " : pcentGuestCpu ,
2011-04-10 08:02:39 +04:00
" curmem " : curmem ,
2010-12-10 19:47:07 +03:00
" currMemPercent " : pcentCurrMem ,
2014-06-16 07:56:02 +04:00
" diskRdKiB " : rdBytes / 1024 ,
" diskWrKiB " : wrBytes / 1024 ,
" netRxKiB " : rxBytes / 1024 ,
" netTxKiB " : txBytes / 1024 ,
2010-12-10 19:47:07 +03:00
}
2009-06-18 18:38:15 +04:00
2010-12-10 19:47:07 +03:00
for r in [ " diskRd " , " diskWr " , " netRx " , " netTx " ] :
2014-06-16 07:56:02 +04:00
newStats [ r + " Rate " ] = self . _get_cur_rate ( r + " KiB " )
2010-02-07 20:10:41 +03:00
self . _set_max_rate ( newStats , r + " Rate " )
2015-05-05 01:16:09 +03:00
self . _stats . insert ( 0 , newStats )
2008-10-18 23:29:45 +04:00
2010-02-07 20:16:13 +03:00
2011-05-04 00:17:52 +04:00
########################
# Libvirt domain class #
########################
class vmmDomainVirtinst ( vmmDomain ) :
2010-02-07 20:16:13 +03:00
"""
Domain object backed by a virtinst Guest object .
Used for launching a details window for customizing a VM before install .
"""
2013-07-07 16:42:57 +04:00
def __init__ ( self , conn , backend , key ) :
vmmDomain . __init__ ( self , conn , backend , key )
2015-04-07 21:53:28 +03:00
self . _orig_xml = None
2010-09-09 01:53:51 +04:00
2015-04-08 00:44:58 +03:00
# This encodes all the virtinst defaults up front, so the customize
# dialog actually shows disk buses, cache values, default devices, etc.
backend . set_install_defaults ( )
2015-04-14 00:25:40 +03:00
self . title_supported = True
self . _refresh_status ( )
2015-05-04 00:01:16 +03:00
logging . debug ( " %s initialized with XML= \n %s " , self , self . _XMLDesc ( 0 ) )
2015-04-14 00:25:40 +03:00
2010-02-07 20:16:13 +03:00
def get_name ( self ) :
return self . _backend . name
2014-06-03 01:17:47 +04:00
def get_uuid ( self ) :
return self . _backend . uuid
2010-02-07 20:16:13 +03:00
def get_id ( self ) :
return - 1
2014-09-12 02:01:41 +04:00
def has_managed_save ( self ) :
2011-05-04 00:17:52 +04:00
return False
2013-10-02 15:42:24 +04:00
def snapshots_supported ( self ) :
return False
2010-02-07 20:16:13 +03:00
def get_autostart ( self ) :
return self . _backend . autostart
def set_autostart ( self , val ) :
self . _backend . autostart = bool ( val )
2015-04-10 01:02:42 +03:00
self . emit ( " state-changed " )
2010-02-07 20:16:13 +03:00
2015-04-07 21:53:28 +03:00
def _using_events ( self ) :
return False
2015-04-14 00:25:40 +03:00
def _get_backend_status ( self ) :
return libvirt . VIR_DOMAIN_SHUTOFF
def _init_libvirt_state ( self ) :
pass
2015-04-07 21:53:28 +03:00
2015-04-10 16:15:44 +03:00
def tick ( self , stats_update = True ) :
ignore = stats_update
2015-04-07 21:53:28 +03:00
################
# XML handling #
################
2011-03-24 20:24:01 +03:00
def define_name ( self , newname ) :
2015-04-07 21:53:28 +03:00
# We need to overwrite this, since the implementation for libvirt
# needs to do some crazy stuff.
2015-04-11 00:23:11 +03:00
xmlobj = self . _make_xmlobj_to_define ( )
2015-04-07 21:53:28 +03:00
xmlobj . name = str ( newname )
2015-04-11 00:23:11 +03:00
self . _redefine_xmlobj ( xmlobj )
2015-04-07 21:53:28 +03:00
def _XMLDesc ( self , flags ) :
ignore = flags
return self . _backend . get_xml_config ( )
def _define ( self , newxml ) :
ignore = newxml
2015-04-10 01:02:42 +03:00
self . emit ( " state-changed " )
2015-04-07 21:53:28 +03:00
def _invalidate_xml ( self ) :
vmmDomain . _invalidate_xml ( self )
self . _orig_xml = None
def _make_xmlobj_to_define ( self ) :
if not self . _orig_xml :
self . _orig_xml = self . _backend . get_xml_config ( )
return self . _backend
2015-04-11 00:23:11 +03:00
def _redefine_xmlobj ( self , xmlobj , origxml = None ) :
vmmDomain . _redefine_xmlobj ( self , xmlobj , origxml = self . _orig_xml )