2013-03-18 01:06:52 +04:00
#
# Common code for all guests
#
2015-02-24 15:32:51 +03:00
# Copyright 2006-2009, 2013, 2014, 2015 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
# Jeremy Katz <katzj@redhat.com>
#
2018-04-04 16:35:41 +03:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 22:00:02 +03:00
# See the COPYING file in the top-level directory.
2013-03-18 01:06:52 +04:00
import logging
2015-09-06 20:42:07 +03:00
import os
2013-03-18 01:06:52 +04:00
import libvirt
2015-04-06 22:42:40 +03:00
from virtcli import CLIConfig
2013-10-03 02:06:52 +04:00
2014-09-12 23:59:22 +04:00
from . import util
2018-03-21 00:23:34 +03:00
from . devices import * # pylint: disable=wildcard-import
2014-09-12 23:59:22 +04:00
from . distroinstaller import DistroInstaller
2018-03-20 22:10:04 +03:00
from . domain import * # pylint: disable=wildcard-import
2015-02-18 23:16:48 +03:00
from . domcapabilities import DomainCapabilities
2018-03-20 22:10:04 +03:00
from . osdict import OSDB
2014-09-12 23:59:22 +04:00
from . xmlbuilder import XMLBuilder , XMLProperty , XMLChildProperty
2013-03-18 01:06:52 +04:00
2018-03-21 01:59:14 +03:00
_ignore = Device
2013-03-18 01:06:52 +04:00
2018-03-21 00:23:34 +03:00
class _DomainDevices ( XMLBuilder ) :
2018-03-21 17:53:34 +03:00
XML_NAME = " devices "
2018-03-21 01:59:14 +03:00
_XML_PROP_ORDER = [ ' disk ' , ' controller ' , ' filesystem ' , ' interface ' ,
2018-03-21 16:08:30 +03:00
' smartcard ' , ' serial ' , ' parallel ' , ' console ' , ' channel ' ,
' input ' , ' tpm ' , ' graphics ' , ' sound ' , ' video ' , ' hostdev ' ,
' redirdev ' , ' watchdog ' , ' memballoon ' , ' rng ' , ' panic ' ,
' memory ' ]
2018-03-21 01:59:14 +03:00
2018-03-21 00:23:34 +03:00
disk = XMLChildProperty ( DeviceDisk )
controller = XMLChildProperty ( DeviceController )
filesystem = XMLChildProperty ( DeviceFilesystem )
interface = XMLChildProperty ( DeviceInterface )
smartcard = XMLChildProperty ( DeviceSmartcard )
serial = XMLChildProperty ( DeviceSerial )
parallel = XMLChildProperty ( DeviceParallel )
console = XMLChildProperty ( DeviceConsole )
channel = XMLChildProperty ( DeviceChannel )
input = XMLChildProperty ( DeviceInput )
tpm = XMLChildProperty ( DeviceTpm )
graphics = XMLChildProperty ( DeviceGraphics )
sound = XMLChildProperty ( DeviceSound )
video = XMLChildProperty ( DeviceVideo )
hostdev = XMLChildProperty ( DeviceHostdev )
redirdev = XMLChildProperty ( DeviceRedirdev )
watchdog = XMLChildProperty ( DeviceWatchdog )
memballoon = XMLChildProperty ( DeviceMemballoon )
rng = XMLChildProperty ( DeviceRng )
panic = XMLChildProperty ( DevicePanic )
memory = XMLChildProperty ( DeviceMemory )
2018-03-21 18:21:16 +03:00
def get_all ( self ) :
retlist = [ ]
# pylint: disable=protected-access
devtypes = _DomainDevices . _XML_PROP_ORDER
for devtype in devtypes :
retlist . extend ( getattr ( self , devtype ) )
return retlist
2018-03-21 00:23:34 +03:00
2013-07-14 02:56:09 +04:00
class Guest ( XMLBuilder ) :
2013-08-09 22:16:59 +04:00
@staticmethod
def check_vm_collision ( conn , name , do_remove ) :
"""
Remove the existing VM with the same name if requested , or error
if there is a collision .
"""
vm = None
try :
vm = conn . lookupByName ( name )
except libvirt . libvirtError :
pass
if vm is None :
return
if not do_remove :
raise RuntimeError ( _ ( " Domain named %s already exists! " ) % name )
try :
logging . debug ( " Explicitly replacing guest ' %s ' " , name )
if vm . ID ( ) != - 1 :
logging . info ( " Destroying guest ' %s ' " , name )
vm . destroy ( )
logging . info ( " Undefining guest ' %s ' " , name )
vm . undefine ( )
2017-05-05 19:47:21 +03:00
except libvirt . libvirtError as e :
2013-08-09 22:16:59 +04:00
raise RuntimeError ( _ ( " Could not remove old vm ' %s ' : %s " ) %
( str ( e ) ) )
@staticmethod
2014-01-18 03:44:26 +04:00
def validate_name ( conn , name , check_collision , validate = True ) :
if validate :
util . validate_name ( _ ( " Guest " ) , name )
2013-08-09 22:16:59 +04:00
if not check_collision :
return
try :
conn . lookupByName ( name )
2017-07-24 11:26:48 +03:00
except Exception :
2013-08-09 22:16:59 +04:00
return
raise ValueError ( _ ( " Guest name ' %s ' is already in use. " ) % name )
2013-07-18 01:58:24 +04:00
2018-03-21 17:53:34 +03:00
XML_NAME = " domain "
2013-09-24 14:56:16 +04:00
_XML_PROP_ORDER = [ " type " , " name " , " uuid " , " title " , " description " ,
2018-03-21 00:23:34 +03:00
" hotplugmemorymax " , " hotplugmemoryslots " , " maxmemory " , " memory " ,
" blkiotune " , " memtune " , " memoryBacking " ,
" vcpus " , " curvcpus " , " vcpu_placement " ,
" cpuset " , " numatune " , " resource " , " sysinfo " ,
" bootloader " , " os " , " idmap " , " features " , " cpu " , " clock " ,
" on_poweroff " , " on_reboot " , " on_crash " ,
" pm " , " emulator " , " devices " , " seclabels " ]
2013-07-18 01:58:24 +04:00
2013-09-11 19:47:09 +04:00
def __init__ ( self , * args , * * kwargs ) :
XMLBuilder . __init__ ( self , * args , * * kwargs )
2013-08-09 19:04:01 +04:00
2013-07-18 01:58:24 +04:00
self . autostart = False
self . replace = False
2013-03-18 01:06:52 +04:00
2013-10-03 02:06:52 +04:00
# Allow virt-manager to override the default graphics type
2015-04-06 22:42:40 +03:00
self . default_graphics_type = CLIConfig . default_graphics
2013-10-03 02:06:52 +04:00
2013-10-06 17:41:37 +04:00
self . skip_default_console = False
self . skip_default_channel = False
2014-02-05 21:49:16 +04:00
self . skip_default_sound = False
2014-02-05 21:58:53 +04:00
self . skip_default_usbredir = False
2014-02-08 01:07:32 +04:00
self . skip_default_graphics = False
2017-03-09 00:54:16 +03:00
self . skip_default_rng = False
2014-02-05 22:51:53 +04:00
self . x86_cpu_default = self . cpu . SPECIAL_MODE_HOST_MODEL_ONLY
2013-10-06 17:41:37 +04:00
2018-09-02 03:13:40 +03:00
self . __osinfo = None
2013-07-25 22:36:13 +04:00
self . _random_uuid = None
2015-06-06 23:56:03 +03:00
self . _install_cdrom_device = None
2015-04-07 23:38:52 +03:00
self . _defaults_are_set = False
2013-03-18 01:06:52 +04:00
# The libvirt virDomain object we 'Create'
self . domain = None
2015-07-10 13:06:48 +03:00
# This is set via Capabilities.build_virtinst_guest
self . capsinfo = None
2014-09-12 23:59:22 +04:00
self . installer = DistroInstaller ( self . conn )
2013-03-18 01:06:52 +04:00
######################
# Property accessors #
######################
2013-07-18 01:58:24 +04:00
def _validate_name ( self , val ) :
if val == self . name :
return
2013-08-09 22:16:59 +04:00
self . validate_name ( self . conn , val , check_collision = not self . replace )
2013-09-19 21:27:30 +04:00
name = XMLProperty ( " ./name " , validate_cb = _validate_name )
2013-03-18 01:06:52 +04:00
2013-07-18 01:58:24 +04:00
def _set_memory ( self , val ) :
if val is None :
return None
2013-03-18 01:06:52 +04:00
if self . maxmemory is None or self . maxmemory < val :
self . maxmemory = val
return val
2013-09-19 21:27:30 +04:00
memory = XMLProperty ( " ./currentMemory " , is_int = True ,
2013-07-18 01:58:24 +04:00
default_cb = lambda s : 1 ,
set_converter = _set_memory )
2013-09-19 21:27:30 +04:00
maxmemory = XMLProperty ( " ./memory " , is_int = True )
2017-05-04 14:08:14 +03:00
hotplugmemorymax = XMLProperty ( " ./maxMemory " , is_int = True )
hotplugmemoryslots = XMLProperty ( " ./maxMemory/@slots " , is_int = True )
2013-03-18 01:06:52 +04:00
2013-07-18 01:58:24 +04:00
def _set_vcpus ( self , val ) :
if val is None :
return None
2013-03-18 01:06:52 +04:00
2013-07-18 01:58:24 +04:00
# Don't force set curvcpus unless already specified
if self . curvcpus is not None and self . curvcpus > val :
self . curvcpus = val
return val
2013-09-19 21:27:30 +04:00
vcpus = XMLProperty ( " ./vcpu " , is_int = True ,
2013-07-18 01:58:24 +04:00
set_converter = _set_vcpus ,
default_cb = lambda s : 1 )
2013-09-19 21:27:30 +04:00
curvcpus = XMLProperty ( " ./vcpu/@current " , is_int = True )
2015-11-18 22:54:36 +03:00
vcpu_placement = XMLProperty ( " ./vcpu/@placement " )
2018-06-04 21:30:26 +03:00
cpuset = XMLProperty ( " ./vcpu/@cpuset " )
2013-07-18 01:58:24 +04:00
2013-07-25 22:36:13 +04:00
def _get_default_uuid ( self ) :
if self . _random_uuid is None :
self . _random_uuid = util . generate_uuid ( self . conn )
return self . _random_uuid
2017-12-14 20:02:36 +03:00
uuid = XMLProperty ( " ./uuid " , default_cb = _get_default_uuid )
2013-07-25 22:36:13 +04:00
2017-03-06 01:12:50 +03:00
2013-09-24 05:27:42 +04:00
id = XMLProperty ( " ./@id " , is_int = True )
2013-09-19 21:27:30 +04:00
type = XMLProperty ( " ./@type " , default_cb = lambda s : " xen " )
bootloader = XMLProperty ( " ./bootloader " )
description = XMLProperty ( " ./description " )
2013-09-24 14:56:16 +04:00
title = XMLProperty ( " ./title " )
2013-09-19 21:27:30 +04:00
emulator = XMLProperty ( " ./devices/emulator " )
2013-07-18 01:58:24 +04:00
2017-02-21 17:27:56 +03:00
on_poweroff = XMLProperty ( " ./on_poweroff " )
on_reboot = XMLProperty ( " ./on_reboot " )
on_crash = XMLProperty ( " ./on_crash " )
2015-10-07 13:50:13 +03:00
on_lockfailure = XMLProperty ( " ./on_lockfailure " )
2013-07-18 01:58:24 +04:00
2018-03-20 22:10:04 +03:00
seclabels = XMLChildProperty ( DomainSeclabel )
os = XMLChildProperty ( DomainOs , is_single = True )
2013-09-11 19:47:09 +04:00
features = XMLChildProperty ( DomainFeatures , is_single = True )
2018-03-20 22:10:04 +03:00
clock = XMLChildProperty ( DomainClock , is_single = True )
cpu = XMLChildProperty ( DomainCpu , is_single = True )
cputune = XMLChildProperty ( DomainCputune , is_single = True )
2013-09-11 19:47:09 +04:00
numatune = XMLChildProperty ( DomainNumatune , is_single = True )
2018-03-20 22:10:04 +03:00
pm = XMLChildProperty ( DomainPm , is_single = True )
2014-02-06 18:41:00 +04:00
blkiotune = XMLChildProperty ( DomainBlkiotune , is_single = True )
2018-03-20 22:10:04 +03:00
memtune = XMLChildProperty ( DomainMemtune , is_single = True )
memoryBacking = XMLChildProperty ( DomainMemoryBacking , is_single = True )
idmap = XMLChildProperty ( DomainIdmap , is_single = True )
2014-06-04 06:08:05 +04:00
resource = XMLChildProperty ( DomainResource , is_single = True )
2018-03-20 22:10:04 +03:00
sysinfo = XMLChildProperty ( DomainSysinfo , is_single = True )
2013-09-11 19:47:09 +04:00
2018-03-20 22:10:04 +03:00
xmlns_qemu = XMLChildProperty ( DomainXMLNSQemu , is_single = True )
2017-03-06 01:12:50 +03:00
2013-07-18 01:58:24 +04:00
2018-09-02 03:22:47 +03:00
##############################
# osinfo related definitions #
##############################
2013-07-18 01:58:24 +04:00
2018-09-02 03:13:40 +03:00
def _set_osinfo ( self , variant ) :
2015-04-04 19:04:11 +03:00
obj = OSDB . lookup_os ( variant )
2015-04-04 17:16:58 +03:00
if not obj :
2015-04-04 19:04:11 +03:00
obj = OSDB . lookup_os ( " generic " )
2018-09-02 03:13:40 +03:00
self . __osinfo = obj
def _get_osinfo ( self ) :
if not self . __osinfo :
self . _set_osinfo ( None )
return self . __osinfo
osinfo = property ( _get_osinfo )
2015-04-04 17:16:58 +03:00
2013-08-11 02:48:43 +04:00
def _get_os_variant ( self ) :
2018-09-02 03:13:40 +03:00
return self . osinfo . name
2013-08-11 02:48:43 +04:00
def _set_os_variant ( self , val ) :
2014-09-07 19:57:04 +04:00
if val :
val = val . lower ( )
2015-04-04 19:04:11 +03:00
if OSDB . lookup_os ( val ) is None :
2014-09-07 19:57:04 +04:00
raise ValueError (
_ ( " Distro ' %s ' does not exist in our dictionary " ) % val )
2013-11-10 03:33:49 +04:00
logging . debug ( " Setting Guest.os_variant to ' %s ' " , val )
2018-09-02 03:13:40 +03:00
self . _set_osinfo ( val )
2013-08-11 02:48:43 +04:00
os_variant = property ( _get_os_variant , _set_os_variant )
2013-03-18 01:06:52 +04:00
2018-09-02 03:22:47 +03:00
def _supports_virtio ( self , os_support ) :
if not self . conn . is_qemu ( ) :
return False
# These _only_ support virtio so don't check the OS
if ( self . os . is_arm_machvirt ( ) or
self . os . is_s390x ( ) or
self . os . is_pseries ( ) ) :
return True
if not os_support :
return False
if self . os . is_x86 ( ) :
return True
return False
def supports_virtionet ( self ) :
return self . _supports_virtio ( self . osinfo . supports_virtionet ( ) )
2013-03-18 01:06:52 +04:00
########################################
# Device Add/Remove Public API methods #
########################################
2013-07-25 22:11:09 +04:00
def add_device ( self , dev ) :
2018-03-21 00:23:34 +03:00
self . devices . add_child ( dev )
2013-07-24 16:46:55 +04:00
2013-09-11 02:32:10 +04:00
def remove_device ( self , dev ) :
2018-03-21 00:23:34 +03:00
self . devices . remove_child ( dev )
2013-03-18 01:06:52 +04:00
2018-03-21 00:23:34 +03:00
devices = XMLChildProperty ( _DomainDevices , is_single = True )
2013-03-18 01:06:52 +04:00
############################
# Install Helper functions #
############################
def _prepare_install ( self , meter , dry = False ) :
ignore = dry
# Fetch install media, prepare installer devices
2014-09-07 21:35:34 +04:00
self . installer . prepare ( self , meter )
2013-03-18 01:06:52 +04:00
# Initialize install device list
2015-06-06 23:56:03 +03:00
if self . _install_cdrom_device :
self . _install_cdrom_device . path = self . installer . cdrom_path ( )
self . _install_cdrom_device . validate ( )
2013-03-18 01:06:52 +04:00
2018-02-23 03:49:08 +03:00
def _prepare_get_install_xml ( self ) :
2015-04-07 23:38:52 +03:00
# We do a shallow copy of the OS block here, so that we can
# set the install time properties but not permanently overwrite
# any config the user explicitly requested.
2018-02-14 23:35:53 +03:00
data = ( self . os . bootorder , self . os . kernel , self . os . initrd ,
self . os . kernel_args , self . on_reboot )
2013-07-18 01:58:24 +04:00
return data
2018-02-23 03:49:08 +03:00
def _finish_get_install_xml ( self , data ) :
2018-02-14 23:35:53 +03:00
( self . os . bootorder , self . os . kernel , self . os . initrd ,
self . os . kernel_args , self . on_reboot ) = data
2013-07-17 21:06:01 +04:00
2015-04-07 23:38:52 +03:00
def _get_install_xml ( self , * args , * * kwargs ) :
2018-02-23 03:49:08 +03:00
data = self . _prepare_get_install_xml ( )
2013-07-24 19:32:30 +04:00
try :
return self . _do_get_install_xml ( * args , * * kwargs )
finally :
2018-02-23 03:49:08 +03:00
self . _finish_get_install_xml ( data )
2013-07-24 19:32:30 +04:00
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
def _do_get_install_xml ( self , install ) :
2013-03-18 01:06:52 +04:00
"""
Return the full Guest xml configuration .
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
@install : Whether we want the ' OS install ' configuration or
the ' post-install ' configuration . The difference is mostly
whether the install media is attached and set as the boot
device . Some installs , like an import or livecd , do not have
an ' install ' config .
2013-03-18 01:06:52 +04:00
"""
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
if install and not self . installer . has_install_phase ( ) :
2013-03-18 01:06:52 +04:00
return None
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
self . installer . alter_bootconfig ( self , install )
2015-06-06 21:52:21 +03:00
if not install :
self . _remove_cdrom_install_media ( )
2013-07-18 01:58:24 +04:00
2015-04-08 02:51:52 +03:00
if install :
self . on_reboot = " destroy "
2013-07-18 01:58:24 +04:00
2018-09-02 17:05:34 +03:00
self . os . set_defaults ( self )
2018-08-31 23:52:02 +03:00
return self . get_xml ( )
2013-03-18 01:06:52 +04:00
2015-04-07 23:38:52 +03:00
2015-09-06 20:42:07 +03:00
###########################
# Private install helpers #
###########################
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
def _build_xml ( self ) :
2016-06-17 15:17:03 +03:00
install_xml = self . _get_install_xml ( install = True )
2015-09-06 20:42:07 +03:00
final_xml = self . _get_install_xml ( install = False )
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
logging . debug ( " Generated install XML: %s " ,
2016-06-17 15:17:03 +03:00
( install_xml and ( " \n " + install_xml ) or " None required " ) )
2015-09-06 20:42:07 +03:00
logging . debug ( " Generated boot XML: \n %s " , final_xml )
2016-06-17 15:17:03 +03:00
return install_xml , final_xml
2015-09-06 20:42:07 +03:00
2017-04-27 20:11:13 +03:00
def _manual_transient_create ( self , install_xml , final_xml , needs_boot ) :
"""
For hypervisors ( like vz ) that don ' t implement createXML,
we need to define + start , and undefine on start failure
"""
domain = self . conn . defineXML ( install_xml or final_xml )
if not needs_boot :
return domain
# Handle undefining the VM if the initial startup fails
try :
domain . create ( )
2017-07-24 11:26:48 +03:00
except Exception :
2017-04-27 20:11:13 +03:00
try :
domain . undefine ( )
2017-07-24 11:26:48 +03:00
except Exception :
2017-04-27 20:11:13 +03:00
pass
2018-01-27 22:19:12 +03:00
raise
2017-10-11 14:35:52 +03:00
2017-04-27 20:11:13 +03:00
if install_xml and install_xml != final_xml :
domain = self . conn . defineXML ( final_xml )
return domain
2016-06-01 16:32:56 +03:00
def _create_guest ( self , meter , install_xml , final_xml , doboot , transient ) :
2015-09-06 20:42:07 +03:00
"""
Actually do the XML logging , guest defining / creating
2018-02-14 15:17:31 +03:00
: param doboot : Boot guest even if it has no install phase
2015-09-06 20:42:07 +03:00
"""
2016-06-17 15:27:26 +03:00
meter_label = _ ( " Creating domain... " )
meter = util . ensure_meter ( meter )
meter . start ( size = None , text = meter_label )
2017-04-27 20:11:13 +03:00
needs_boot = doboot or self . installer . has_install_phase ( )
if self . type == " vz " :
if transient :
raise RuntimeError ( _ ( " Domain type ' vz ' doesn ' t support "
" transient installs. " ) )
domain = self . _manual_transient_create (
install_xml , final_xml , needs_boot )
2015-09-06 20:42:07 +03:00
2017-02-21 17:27:59 +03:00
else :
2017-04-27 20:11:13 +03:00
if transient or needs_boot :
domain = self . conn . createXML ( install_xml or final_xml , 0 )
if not transient :
2017-04-13 21:56:03 +03:00
domain = self . conn . defineXML ( final_xml )
2015-09-06 20:42:07 +03:00
2017-04-13 21:56:03 +03:00
self . domain = domain
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
try :
logging . debug ( " XML fetched from libvirt object: \n %s " ,
2016-06-17 15:27:26 +03:00
self . domain . XMLDesc ( 0 ) )
2017-05-05 19:47:21 +03:00
except Exception as e :
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
logging . debug ( " Error fetching XML from libvirt object: %s " , e )
2015-09-06 20:42:07 +03:00
def _flag_autostart ( self ) :
"""
Set the autostart flag for self . domain if the user requested it
"""
if not self . autostart :
return
try :
self . domain . setAutostart ( True )
2017-05-05 19:47:21 +03:00
except libvirt . libvirtError as e :
2015-09-06 20:42:07 +03:00
if util . is_error_nosupport ( e ) :
2017-05-05 21:21:15 +03:00
logging . warning ( " Could not set autostart flag: libvirt "
2015-09-06 20:42:07 +03:00
" connection does not support autostart. " )
else :
raise e
2015-04-07 23:38:52 +03:00
##############
# Public API #
##############
2013-08-09 22:16:59 +04:00
def start_install ( self , meter = None ,
2016-06-01 16:32:56 +03:00
dry = False , return_xml = False ,
doboot = True , transient = False ) :
2013-03-18 01:06:52 +04:00
"""
Begin the guest install ( stage1 ) .
2018-02-14 15:17:31 +03:00
: param return_xml : Don ' t create the guest, just return generated XML
2013-03-18 01:06:52 +04:00
"""
2013-07-14 03:43:52 +04:00
if self . domain is not None :
raise RuntimeError ( _ ( " Domain has already been started! " ) )
2013-03-18 01:06:52 +04:00
2015-04-07 23:38:52 +03:00
self . set_install_defaults ( )
2013-03-18 01:06:52 +04:00
self . _prepare_install ( meter , dry )
try :
# Create devices if required (disk images, etc.)
if not dry :
2018-03-21 18:21:16 +03:00
for dev in self . devices . get_all ( ) :
2013-08-15 03:43:02 +04:00
dev . setup ( meter )
2013-03-18 01:06:52 +04:00
2016-06-17 15:17:03 +03:00
install_xml , final_xml = self . _build_xml ( )
2013-03-18 01:06:52 +04:00
if return_xml :
2016-06-17 15:17:03 +03:00
return ( install_xml , final_xml )
2013-03-18 01:06:52 +04:00
if dry :
return
# Remove existing VM if requested
2013-08-09 22:16:59 +04:00
self . check_vm_collision ( self . conn , self . name ,
do_remove = self . replace )
2013-03-18 01:06:52 +04:00
2016-06-01 16:32:56 +03:00
self . _create_guest ( meter , install_xml , final_xml ,
doboot , transient )
2013-03-18 01:06:52 +04:00
# Set domain autostart flag if requested
self . _flag_autostart ( )
finally :
2013-08-15 03:43:02 +04:00
self . installer . cleanup ( )
2013-03-18 01:06:52 +04:00
2015-09-06 21:42:55 +03:00
def get_created_disks ( self ) :
2018-03-21 00:23:34 +03:00
return [ d for d in self . devices . disk if d . storage_was_created ]
2015-09-06 21:42:55 +03:00
2015-09-06 20:42:07 +03:00
def cleanup_created_disks ( self , meter ) :
2013-03-18 01:06:52 +04:00
"""
2015-09-06 20:42:07 +03:00
Remove any disks we created as part of the install . Only ever
called by clients .
2013-03-18 01:06:52 +04:00
"""
2015-09-06 21:42:55 +03:00
clean_disks = self . get_created_disks ( )
2015-09-06 20:42:07 +03:00
if not clean_disks :
return
2013-03-18 01:06:52 +04:00
2015-09-06 20:42:07 +03:00
for disk in clean_disks :
logging . debug ( " Removing created disk path= %s vol_object= %s " ,
disk . path , disk . get_vol_object ( ) )
name = os . path . basename ( disk . path )
2013-03-18 01:06:52 +04:00
try :
2015-09-06 20:42:07 +03:00
meter . start ( size = None , text = _ ( " Removing disk ' %s ' " ) % name )
2013-03-18 01:06:52 +04:00
2015-09-06 20:42:07 +03:00
if disk . get_vol_object ( ) :
disk . get_vol_object ( ) . delete ( )
else :
os . unlink ( disk . path )
2013-03-18 01:06:52 +04:00
2015-09-06 20:42:07 +03:00
meter . end ( 0 )
2017-05-05 19:47:21 +03:00
except Exception as e :
2015-09-06 20:42:07 +03:00
logging . debug ( " Failed to remove disk ' %s ' " ,
name , exc_info = True )
logging . error ( " Failed to remove disk ' %s ' : %s " , name , e )
2013-03-18 01:06:52 +04:00
2015-02-18 23:16:48 +03:00
###########################
# XML convenience helpers #
###########################
def set_uefi_default ( self ) :
"""
Configure UEFI for the VM , but only if libvirt is advertising
a known UEFI binary path .
"""
domcaps = DomainCapabilities . build_from_guest ( self )
if not domcaps . supports_uefi_xml ( ) :
raise RuntimeError ( _ ( " Libvirt version does not support UEFI. " ) )
2015-02-22 19:02:55 +03:00
if not domcaps . arch_can_uefi ( ) :
2015-02-18 23:16:48 +03:00
raise RuntimeError (
_ ( " Don ' t know how to setup UEFI for arch ' %s ' " ) %
self . os . arch )
2015-02-22 19:02:55 +03:00
path = domcaps . find_uefi_path_for_arch ( )
2015-02-18 23:16:48 +03:00
if not path :
raise RuntimeError ( _ ( " Did not find any UEFI binary path for "
" arch ' %s ' " ) % self . os . arch )
self . os . loader_ro = True
self . os . loader_type = " pflash "
self . os . loader = path
2017-06-07 21:47:59 +03:00
self . check_uefi_secure ( )
2017-02-06 15:46:06 +03:00
2017-06-07 21:47:59 +03:00
def check_uefi_secure ( self ) :
2017-02-06 15:46:06 +03:00
"""
If the firmware name contains " secboot " it is probably build
with SMM feature required so we need to enable that feature ,
otherwise the firmware may fail to load . True secure boot is
currently supported only on x86 architecture and with q35 with
SMM feature enabled so change the machine to q35 as well .
2017-06-07 21:47:59 +03:00
To actually enforce the secure boot for the guest if Secure Boot
Mode is configured we need to enable loader secure feature .
2017-02-06 15:46:06 +03:00
"""
if not self . os . is_x86 ( ) :
return
if " secboot " not in self . os . loader :
return
self . features . smm = True
2017-06-07 21:47:59 +03:00
self . os . loader_secure = True
2017-02-06 15:46:06 +03:00
self . os . machine = " q35 "
2015-02-18 23:16:48 +03:00
2018-09-01 04:06:18 +03:00
2013-03-18 01:06:52 +04:00
###################
# Device defaults #
###################
2015-04-07 23:38:52 +03:00
def set_install_defaults ( self ) :
"""
Allow API users to set defaults ahead of time if they want it .
Used by vmmDomainVirtinst so the ' Customize before install ' dialog
shows accurate values .
If the user doesn ' t explicitly call this, it will be called by
start_install ( )
"""
if self . _defaults_are_set :
return
self . _set_defaults ( )
self . _defaults_are_set = True
2015-03-25 16:04:51 +03:00
def stable_defaults ( self , * args , * * kwargs ) :
return self . conn . stable_defaults ( self . emulator , * args , * * kwargs )
2015-03-23 23:43:39 +03:00
2017-06-28 23:12:04 +03:00
def _usb_disabled ( self ) :
2018-03-21 00:23:34 +03:00
controllers = [ c for c in self . devices . controller if
2017-06-28 23:12:04 +03:00
c . type == " usb " ]
if not controllers :
return False
return all ( [ c . model == " none " for c in controllers ] )
2013-07-25 22:36:13 +04:00
def add_default_input_device ( self ) :
if self . os . is_container ( ) :
return
2018-03-21 00:23:34 +03:00
if self . devices . input :
2013-10-06 16:30:33 +04:00
return
2018-03-21 00:23:34 +03:00
if not self . devices . graphics :
2017-06-28 19:05:14 +03:00
return
2017-06-28 23:22:23 +03:00
if self . _usb_disabled ( ) :
return
usb_tablet = False
usb_keyboard = False
2018-08-22 15:38:33 +03:00
if self . os . is_x86 ( ) and not self . os . is_xenpv ( ) :
2018-09-02 03:13:40 +03:00
usb_tablet = self . osinfo . supports_usbtablet ( )
2017-06-28 23:22:23 +03:00
if self . os . is_arm_machvirt ( ) :
usb_tablet = True
usb_keyboard = True
2017-06-28 23:12:04 +03:00
2017-06-28 23:22:23 +03:00
if usb_tablet :
2018-03-20 19:18:35 +03:00
dev = DeviceInput ( self . conn )
2017-06-28 23:12:04 +03:00
dev . type = " tablet "
dev . bus = " usb "
self . add_device ( dev )
2017-06-28 23:22:23 +03:00
if usb_keyboard :
2018-03-20 19:18:35 +03:00
dev = DeviceInput ( self . conn )
2017-06-28 23:22:23 +03:00
dev . type = " keyboard "
dev . bus = " usb "
self . add_device ( dev )
2013-07-25 22:36:13 +04:00
def add_default_console_device ( self ) :
2013-10-06 17:41:37 +04:00
if self . skip_default_console :
return
2018-03-21 00:23:34 +03:00
if self . devices . console or self . devices . serial :
2013-10-06 16:30:33 +04:00
return
2018-03-20 19:18:35 +03:00
dev = DeviceConsole ( self . conn )
2013-07-25 22:36:13 +04:00
dev . type = dev . TYPE_PTY
2015-11-13 04:48:13 +03:00
if self . os . is_s390x ( ) :
dev . target_type = " sclp "
2013-07-25 22:36:13 +04:00
self . add_device ( dev )
2013-08-18 16:59:19 +04:00
def add_default_video_device ( self ) :
if self . os . is_container ( ) :
return
2018-03-21 00:23:34 +03:00
if self . devices . video :
2013-10-06 16:30:33 +04:00
return
2018-03-21 00:23:34 +03:00
if not self . devices . graphics :
2013-10-06 16:30:33 +04:00
return
2018-03-20 19:18:35 +03:00
self . add_device ( DeviceVideo ( self . conn ) )
2013-08-18 16:59:19 +04:00
2013-10-02 23:51:01 +04:00
def add_default_usb_controller ( self ) :
if self . os . is_container ( ) :
return
2018-03-21 00:23:34 +03:00
if any ( [ d . type == " usb " for d in self . devices . controller ] ) :
2013-10-06 16:30:33 +04:00
return
2017-06-28 22:35:07 +03:00
usb2 = False
usb3 = False
if self . os . is_x86 ( ) :
usb2 = True
elif ( self . os . is_arm_machvirt ( ) and
self . conn . check_support (
self . conn . SUPPORT_CONN_MACHVIRT_PCI_DEFAULT ) ) :
usb3 = True
if not usb2 and not usb3 :
2013-10-02 23:51:01 +04:00
return
2017-06-28 22:35:07 +03:00
if usb2 :
if not self . conn . check_support (
2017-09-20 10:36:27 +03:00
self . conn . SUPPORT_CONN_DEFAULT_USB2 ) :
2017-06-28 22:35:07 +03:00
return
2018-03-20 19:18:35 +03:00
for dev in DeviceController . get_usb2_controllers ( self . conn ) :
2017-06-28 22:35:07 +03:00
self . add_device ( dev )
if usb3 :
2017-07-11 02:40:19 +03:00
self . add_device (
2018-03-20 19:18:35 +03:00
DeviceController . get_usb3_controller ( self . conn , self ) )
2013-10-02 23:51:01 +04:00
2013-10-06 17:19:59 +04:00
def add_default_channels ( self ) :
2013-10-06 17:41:37 +04:00
if self . skip_default_channel :
return
2018-03-21 00:23:34 +03:00
if self . devices . channel :
2013-10-06 17:19:59 +04:00
return
2015-11-04 09:30:24 +03:00
if self . os . is_s390x ( ) :
2015-11-11 01:55:13 +03:00
# Not wanted for s390 apparently
return
2013-10-06 17:19:59 +04:00
if ( self . conn . is_qemu ( ) and
2018-09-02 03:13:40 +03:00
self . _supports_virtio ( self . osinfo . supports_virtioserial ( ) ) and
2013-10-06 18:08:04 +04:00
self . conn . check_support ( self . conn . SUPPORT_CONN_AUTOSOCKET ) ) :
2018-03-20 19:18:35 +03:00
dev = DeviceChannel ( self . conn )
2013-10-06 17:19:59 +04:00
dev . type = " unix "
dev . target_type = " virtio "
dev . target_name = dev . CHANNEL_NAME_QEMUGA
self . add_device ( dev )
2014-02-08 01:07:32 +04:00
def add_default_graphics ( self ) :
if self . skip_default_graphics :
return
2018-03-21 00:23:34 +03:00
if self . devices . graphics :
2014-02-08 01:07:32 +04:00
return
2017-03-13 15:01:53 +03:00
if self . os . is_container ( ) and not self . conn . is_vz ( ) :
2014-02-08 01:07:32 +04:00
return
2017-06-28 22:17:20 +03:00
if self . os . arch not in [ " x86_64 " , " i686 " , " ppc64 " , " ppc64le " ] :
2014-02-08 01:07:32 +04:00
return
2018-03-20 19:18:35 +03:00
self . add_device ( DeviceGraphics ( self . conn ) )
2014-02-08 01:07:32 +04:00
2017-03-09 00:54:16 +03:00
def add_default_rng ( self ) :
if self . skip_default_rng :
return
2018-03-21 00:23:34 +03:00
if self . devices . rng :
2017-03-09 00:54:16 +03:00
return
2017-06-28 22:11:05 +03:00
if not ( self . os . is_x86 ( ) or
self . os . is_arm_machvirt ( ) or
self . os . is_pseries ( ) ) :
2017-03-09 00:54:16 +03:00
return
if ( self . conn . is_qemu ( ) and
2018-09-02 03:13:40 +03:00
self . osinfo . supports_virtiorng ( ) and
2017-03-09 00:54:16 +03:00
self . conn . check_support ( self . conn . SUPPORT_CONN_RNG_URANDOM ) ) :
2018-03-20 19:18:35 +03:00
dev = DeviceRng ( self . conn )
2017-03-09 00:54:16 +03:00
dev . type = " random "
dev . device = " /dev/urandom "
self . add_device ( dev )
2014-02-08 01:07:32 +04:00
def add_default_devices ( self ) :
self . add_default_graphics ( )
self . add_default_video_device ( )
self . add_default_input_device ( )
self . add_default_console_device ( )
self . add_default_usb_controller ( )
self . add_default_channels ( )
2017-03-09 00:54:16 +03:00
self . add_default_rng ( )
2014-02-08 01:07:32 +04:00
2015-06-06 23:56:03 +03:00
def _add_install_cdrom ( self ) :
if self . _install_cdrom_device :
return
if not self . installer . needs_cdrom ( ) :
return
2018-03-20 19:18:35 +03:00
dev = DeviceDisk ( self . conn )
2015-06-06 23:56:03 +03:00
dev . device = dev . DEVICE_CDROM
setattr ( dev , " installer_media " , not self . installer . livecd )
self . _install_cdrom_device = dev
self . add_device ( dev )
2015-06-06 21:52:21 +03:00
def _remove_cdrom_install_media ( self ) :
2018-03-21 00:23:34 +03:00
for dev in self . devices . disk :
2015-06-06 21:52:21 +03:00
# Keep the install cdrom device around, but with no media attached.
virtinst: guest: drop 'continue_install' concept
continue_install is intended to facilitate windows XP style 3 stage
installs:
stage 1: initial dos style disk setup, reboot
stage 2: actual full installer, reboot
stage 3: OS is functional, virt-install is done
The code assumed that we needed to keep the cdrom as the primary
boot device for the second stage, so virt-install/virt-manager needed
to hang around through the second stage run, wait until the VM shutdown,
then encode the final XML to boot of the disk.
Windows is and always has been smart enough to handle that case though...
after the initial boot, if we set the hd as the primary boot device
for stage 2, the disk bits that windows already installed will make
use of the cdrom as necessary. So the entire premise of continue_install
is irrelevant. Maybe back when it was added, when xen didn't even have
working ACPI support, this served a purpose, but I'm pretty sure we
can safely drop it nowadays.
2016-06-16 23:13:54 +03:00
# But only if we are installing windows which has a multi stage
# install.
2015-06-06 21:52:21 +03:00
if ( dev . is_cdrom ( ) and
getattr ( dev , " installer_media " , False ) and
2018-09-02 03:13:40 +03:00
not self . osinfo . is_windows ( ) ) :
2013-07-18 01:58:24 +04:00
dev . path = None
2013-07-24 19:32:30 +04:00
def _set_defaults ( self ) :
2018-09-02 17:01:41 +03:00
if self . os . is_xenpv ( ) or self . type == " vz " :
self . emulator = None
2015-06-06 23:56:03 +03:00
self . _add_install_cdrom ( )
2013-07-25 22:36:13 +04:00
self . _set_clock_defaults ( )
2013-07-18 01:58:24 +04:00
self . _set_cpu_defaults ( )
2018-09-02 16:02:32 +03:00
self . features . set_defaults ( self )
2018-09-02 16:52:00 +03:00
for seclabel in self . seclabels :
seclabel . set_defaults ( self )
2018-09-02 16:55:59 +03:00
self . pm . set_defaults ( self )
2013-07-18 01:58:24 +04:00
2018-03-21 18:21:16 +03:00
for dev in self . devices . get_all ( ) :
2013-10-06 21:17:35 +04:00
dev . set_defaults ( self )
2018-09-01 23:34:25 +03:00
2013-07-25 22:36:13 +04:00
self . _set_disk_defaults ( )
2014-11-20 19:27:09 +03:00
self . _add_implied_controllers ( )
2018-09-01 23:34:25 +03:00
self . _add_spice_devices ( )
2013-07-18 01:58:24 +04:00
2018-09-02 16:02:32 +03:00
def is_full_os_container ( self ) :
2014-03-10 18:25:14 +04:00
if not self . os . is_container ( ) :
return False
2018-03-21 00:23:34 +03:00
for fs in self . devices . filesystem :
2014-03-10 18:25:14 +04:00
if fs . target == " / " :
return True
return False
2013-07-25 22:36:13 +04:00
def _set_clock_defaults ( self ) :
if not self . os . is_hvm ( ) :
return
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
if self . clock . offset is None :
2018-09-02 03:13:40 +03:00
self . clock . offset = self . osinfo . get_clock ( )
2013-03-18 01:06:52 +04:00
2013-10-06 01:02:26 +04:00
if self . clock . timers :
return
if not self . os . is_x86 ( ) :
return
2018-09-01 04:06:18 +03:00
if not self . conn . is_qemu ( ) :
2013-10-06 01:02:26 +04:00
return
# Set clock policy that maps to qemu options:
# -no-hpet -no-kvm-pit-reinjection -rtc driftfix=slew
#
# hpet: Is unneeded and has a performance penalty
# pit: While it has no effect on windows, it doesn't hurt and
# is beneficial for linux
#
2014-07-07 02:46:16 +04:00
# If libvirt/qemu supports it and using a windows VM, also
# specify hypervclock.
#
2013-10-06 01:02:26 +04:00
# This is what has been recommended by the RH qemu guys :)
2018-02-08 01:27:56 +03:00
rtc = self . clock . timers . add_new ( )
2013-10-06 01:02:26 +04:00
rtc . name = " rtc "
rtc . tickpolicy = " catchup "
2018-02-08 01:27:56 +03:00
pit = self . clock . timers . add_new ( )
2013-10-06 01:02:26 +04:00
pit . name = " pit "
pit . tickpolicy = " delay "
2018-02-08 01:27:56 +03:00
hpet = self . clock . timers . add_new ( )
2013-10-06 01:02:26 +04:00
hpet . name = " hpet "
hpet . present = False
2015-07-20 20:06:34 +03:00
hv_clock = self . conn . check_support ( self . conn . SUPPORT_CONN_HYPERV_CLOCK )
hv_clock_rhel = self . conn . check_support ( self . conn . SUPPORT_CONN_HYPERV_CLOCK_RHEL )
2018-09-02 16:02:32 +03:00
if ( self . hyperv_supported ( ) and
2015-07-20 20:06:34 +03:00
( hv_clock or ( self . stable_defaults ( ) and hv_clock_rhel ) ) ) :
2018-02-08 01:27:56 +03:00
hyperv = self . clock . timers . add_new ( )
2014-07-07 02:46:16 +04:00
hyperv . name = " hypervclock "
hyperv . present = True
2018-03-28 22:45:30 +03:00
def _set_cpu_x86_kvm_default ( self ) :
if self . os . arch != self . conn . caps . host . cpu . arch :
return
self . cpu . set_special_mode ( self . x86_cpu_default )
if self . x86_cpu_default != self . cpu . SPECIAL_MODE_HOST_MODEL_ONLY :
return
if not self . cpu . model :
return
# It's possible that the value HOST_MODEL_ONLY gets from
# <capabilities> is not actually supported by qemu/kvm
# combo which will be reported in <domainCapabilities>
domcaps = DomainCapabilities . build_from_guest ( self )
domcaps_mode = domcaps . cpu . get_mode ( " custom " )
if not domcaps_mode :
return
cpu_model = domcaps_mode . get_model ( self . cpu . model )
if cpu_model and cpu_model . usable :
return
logging . debug ( " Host capabilities CPU ' %s ' is not supported "
2018-04-03 23:56:23 +03:00
" according to domain capabilities. Unsetting CPU model " ,
2018-03-28 22:45:30 +03:00
self . cpu . model )
self . cpu . model = None
2013-07-25 22:36:13 +04:00
def _set_cpu_defaults ( self ) :
self . cpu . set_topology_defaults ( self . vcpus )
2013-03-18 01:06:52 +04:00
2014-09-24 00:16:35 +04:00
if not self . conn . is_test ( ) and not self . conn . is_qemu ( ) :
2014-02-05 22:51:53 +04:00
return
2018-08-31 23:52:02 +03:00
if ( self . cpu . get_xml ( ) . strip ( ) or
2015-03-24 00:46:09 +03:00
self . cpu . special_mode_was_set ) :
2014-09-24 00:16:35 +04:00
# User already configured CPU
2014-02-05 22:51:53 +04:00
return
2014-09-24 00:16:35 +04:00
if self . os . is_arm_machvirt ( ) and self . type == " kvm " :
2015-04-23 17:14:58 +03:00
self . cpu . mode = self . cpu . SPECIAL_MODE_HOST_PASSTHROUGH
2014-09-24 00:16:35 +04:00
elif self . os . is_arm64 ( ) and self . os . is_arm_machvirt ( ) :
# -M virt defaults to a 32bit CPU, even if using aarch64
self . cpu . model = " cortex-a57 "
elif self . os . is_x86 ( ) and self . type == " kvm " :
2018-03-28 22:45:30 +03:00
self . _set_cpu_x86_kvm_default ( )
2015-11-03 19:15:26 +03:00
2018-09-02 03:13:40 +03:00
if self . osinfo . broken_x2apic ( ) :
2015-11-03 19:15:26 +03:00
self . cpu . add_feature ( " x2apic " , policy = " disable " )
2018-09-02 16:02:32 +03:00
def hyperv_supported ( self ) :
if not self . osinfo . is_windows ( ) :
return False
2015-01-26 18:52:03 +03:00
if ( self . os . loader_type == " pflash " and
self . os_variant in ( " win2k8r2 " , " win7 " ) ) :
return False
return True
2015-11-22 03:25:02 +03:00
def update_defaults ( self ) :
2015-05-20 19:27:13 +03:00
# This is used only by virt-manager to reset any defaults that may have
# changed through manual intervention via the customize wizard.
2015-11-22 03:25:02 +03:00
# UEFI doesn't work with hyperv bits
2018-09-02 16:02:32 +03:00
if not self . hyperv_supported ( ) :
2015-05-20 19:27:13 +03:00
self . features . hyperv_relaxed = None
self . features . hyperv_vapic = None
self . features . hyperv_spinlocks = None
self . features . hyperv_spinlocks_retries = None
for i in self . clock . timers :
if i . name == " hypervclock " :
self . clock . remove_timer ( i )
2013-07-25 22:36:13 +04:00
def _add_implied_controllers ( self ) :
2014-11-20 19:27:09 +03:00
has_spapr_scsi = False
has_virtio_scsi = False
has_any_scsi = False
2018-03-21 00:23:34 +03:00
for dev in self . devices . controller :
2014-11-20 19:27:09 +03:00
if dev . type == " scsi " :
has_any_scsi = True
if dev . address . type == " spapr-vio " :
has_spapr_scsi = True
if dev . model == " virtio " :
has_virtio_scsi = True
# Add spapr-vio controller if needed
if not has_spapr_scsi :
2018-03-21 00:23:34 +03:00
for dev in self . devices . disk :
2014-11-20 19:27:09 +03:00
if dev . address . type == " spapr-vio " :
2018-03-20 19:18:35 +03:00
ctrl = DeviceController ( self . conn )
2014-11-20 19:27:09 +03:00
ctrl . type = " scsi "
ctrl . address . set_addrstr ( " spapr-vio " )
2018-09-02 00:05:12 +03:00
ctrl . set_defaults ( self )
2014-11-20 19:27:09 +03:00
self . add_device ( ctrl )
break
# Add virtio-scsi controller if needed
2017-11-30 00:41:03 +03:00
if ( ( self . os . is_arm_machvirt ( ) or self . os . is_pseries ( ) ) and
2014-11-20 19:27:09 +03:00
not has_any_scsi and
not has_virtio_scsi ) :
2018-03-21 00:23:34 +03:00
for dev in self . devices . disk :
2014-11-20 19:27:09 +03:00
if dev . bus == " scsi " :
2018-03-20 19:18:35 +03:00
ctrl = DeviceController ( self . conn )
2014-11-20 19:27:09 +03:00
ctrl . type = " scsi "
ctrl . model = " virtio-scsi "
2018-09-02 00:05:12 +03:00
ctrl . set_defaults ( self )
2014-11-20 19:27:09 +03:00
self . add_device ( ctrl )
break
2015-06-06 23:56:03 +03:00
def _set_disk_defaults ( self ) :
2018-03-21 00:23:34 +03:00
disks = self . devices . disk
2015-04-07 23:38:52 +03:00
2013-07-25 22:36:13 +04:00
def set_disk_bus ( d ) :
2013-08-17 22:21:30 +04:00
if d . is_floppy ( ) :
2013-07-25 22:36:13 +04:00
d . bus = " fdc "
return
if self . os . is_xenpv ( ) :
d . bus = " xen "
return
2013-08-17 22:21:30 +04:00
if not self . os . is_hvm ( ) :
2015-04-06 20:43:18 +03:00
# This likely isn't correct, but it's kind of a catch all
# for virt types we don't know how to handle.
2013-08-17 22:21:30 +04:00
d . bus = " ide "
return
2013-03-18 01:06:52 +04:00
2014-11-20 23:39:53 +03:00
if self . os . is_arm_machvirt ( ) :
# We prefer virtio-scsi for machvirt, gets us hotplug
d . bus = " scsi "
2015-04-04 17:44:54 +03:00
elif ( d . is_disk ( ) and
2018-09-02 03:13:40 +03:00
self . _supports_virtio ( self . osinfo . supports_virtiodisk ( ) ) ) :
2013-08-18 01:53:17 +04:00
d . bus = " virtio "
2017-06-28 22:02:23 +03:00
elif self . os . is_pseries ( ) and d . is_cdrom ( ) :
d . bus = " scsi "
2013-08-18 16:59:19 +04:00
elif self . os . is_arm ( ) :
d . bus = " sd "
2015-04-06 20:43:18 +03:00
elif self . os . is_q35 ( ) :
d . bus = " sata "
2013-08-17 22:21:30 +04:00
else :
d . bus = " ide "
2013-03-18 01:06:52 +04:00
2015-04-07 23:38:52 +03:00
# Generate disk targets
2013-03-18 01:06:52 +04:00
used_targets = [ ]
2015-04-07 23:38:52 +03:00
for disk in disks :
2013-03-18 01:06:52 +04:00
if not disk . bus :
2013-07-25 22:36:13 +04:00
set_disk_bus ( disk )
2015-06-06 23:56:03 +03:00
for disk in disks :
if ( disk . target and
not getattr ( disk , " cli_generated_target " , False ) ) :
2013-03-18 01:06:52 +04:00
used_targets . append ( disk . target )
2015-06-06 23:56:03 +03:00
else :
2015-04-07 23:38:52 +03:00
disk . cli_generated_target = False
2013-03-18 01:06:52 +04:00
used_targets . append ( disk . generate_target ( used_targets ) )
2013-10-03 02:06:52 +04:00
def _add_spice_channels ( self ) :
2013-10-06 17:41:37 +04:00
if self . skip_default_channel :
return
2018-03-21 00:23:34 +03:00
for chn in self . devices . channel :
2014-02-05 21:49:16 +04:00
if chn . type == chn . TYPE_SPICEVMC :
return
2018-09-02 00:05:12 +03:00
dev = DeviceChannel ( self . conn )
dev . type = DeviceChannel . TYPE_SPICEVMC
dev . set_defaults ( self )
self . add_device ( dev )
2013-10-03 02:06:52 +04:00
2014-02-05 21:49:16 +04:00
def _add_spice_sound ( self ) :
if self . skip_default_sound :
return
2018-03-21 00:23:34 +03:00
if self . devices . sound :
2014-02-05 21:49:16 +04:00
return
2017-06-28 23:28:25 +03:00
if not self . os . is_hvm ( ) :
return
if not ( self . os . is_x86 ( ) or
self . os . is_arm_machvirt ) :
return
2018-09-02 00:05:12 +03:00
dev = DeviceSound ( self . conn )
dev . set_defaults ( self )
self . add_device ( dev )
2014-02-05 21:49:16 +04:00
2014-02-05 21:58:53 +04:00
def _add_spice_usbredir ( self ) :
if self . skip_default_usbredir :
return
2018-03-21 00:23:34 +03:00
if self . devices . redirdev :
2014-02-05 21:58:53 +04:00
return
2017-03-05 22:30:11 +03:00
if not self . os . is_x86 ( ) :
return
2014-02-05 21:58:53 +04:00
2014-09-21 04:16:31 +04:00
# If we use 4 devices here, we fill up all the emulated USB2 slots,
# and directly assigned devices are forced to fall back to USB1
# https://bugzilla.redhat.com/show_bug.cgi?id=1135488
for ignore in range ( 2 ) :
2018-03-20 19:18:35 +03:00
dev = DeviceRedirdev ( self . conn )
2014-02-05 21:58:53 +04:00
dev . bus = " usb "
dev . type = " spicevmc "
2018-09-02 00:05:12 +03:00
dev . set_defaults ( self )
2014-02-05 21:58:53 +04:00
self . add_device ( dev )
2014-09-06 20:58:59 +04:00
def has_spice ( self ) :
2018-03-21 00:23:34 +03:00
for gfx in self . devices . graphics :
2014-09-06 20:58:59 +04:00
if gfx . type == gfx . TYPE_SPICE :
return True
2013-10-03 02:06:52 +04:00
2016-03-04 14:31:53 +03:00
def has_gl ( self ) :
2018-03-21 00:23:34 +03:00
for gfx in self . devices . graphics :
2016-03-04 14:31:53 +03:00
if gfx . gl :
return True
2018-01-05 11:13:45 +03:00
def has_listen_none ( self ) :
2018-03-21 00:23:34 +03:00
for gfx in self . devices . graphics :
2018-01-05 11:13:45 +03:00
listen = gfx . get_first_listen_type ( )
if listen and listen == " none " :
return True
return False
2018-09-01 23:34:25 +03:00
def _add_spice_devices ( self ) :
if not self . has_spice ( ) :
return
2018-09-02 16:02:32 +03:00
if ( self . features . vmport is None and
2018-09-01 23:34:25 +03:00
self . os . is_x86 ( ) and
self . conn . check_support ( self . conn . SUPPORT_CONN_VMPORT ) ) :
self . features . vmport = False
self . _add_spice_channels ( )
self . _add_spice_sound ( )
self . _add_spice_usbredir ( )