2015-02-19 12:33:25 +02:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""
2015-04-08 20:17:34 +03:00
QEMU VM management ( creates command line , processes , files etc . ) in
order to run a QEMU VM .
2015-02-19 12:33:25 +02:00
"""
import sys
import os
2015-06-10 16:49:24 +03:00
import re
2015-02-19 12:33:25 +02:00
import shutil
import subprocess
import shlex
2015-02-19 17:46:57 +02:00
import asyncio
2015-03-24 06:52:02 +02:00
import socket
2015-06-03 07:33:38 +03:00
import gns3server
2015-02-19 12:33:25 +02:00
2015-10-04 15:41:39 +03:00
from pkg_resources import parse_version
2015-02-19 12:33:25 +02:00
from . qemu_error import QemuError
from . . adapters . ethernet_adapter import EthernetAdapter
2015-02-24 04:00:34 +02:00
from . . nios . nio_udp import NIOUDP
2015-04-27 23:38:15 +03:00
from . . nios . nio_tap import NIOTAP
2015-05-06 23:59:01 +03:00
from . . nios . nio_nat import NIONAT
2015-02-19 12:33:25 +02:00
from . . base_vm import BaseVM
2015-06-10 16:49:24 +03:00
from . . . schemas . qemu import QEMU_OBJECT_SCHEMA , QEMU_PLATFORMS
2015-03-04 17:01:56 +02:00
from . . . utils . asyncio import monitor_process
2015-06-17 18:11:25 +03:00
from . . . utils . images import md5sum
2015-02-19 12:33:25 +02:00
import logging
log = logging . getLogger ( __name__ )
class QemuVM ( BaseVM ) :
module_name = ' qemu '
"""
QEMU VM implementation .
2015-04-08 20:17:34 +03:00
: param name : Qemu VM name
: param vm_id : Qemu VM identifier
2015-02-19 12:33:25 +02:00
: param project : Project instance
2015-04-08 20:17:34 +03:00
: param manager : Manager instance
2015-02-19 12:33:25 +02:00
: param console : TCP console port
: param qemu_path : path to the QEMU binary
2015-06-10 16:49:24 +03:00
: param platform : Platform to emulate
2015-02-19 12:33:25 +02:00
: param console : TCP console port
"""
2015-10-19 04:19:27 +03:00
def __init__ ( self , name , vm_id , project , manager , linked_clone = True , qemu_path = None , console = None , console_type = " telnet " , platform = None ) :
2015-02-19 12:33:25 +02:00
2015-07-04 01:06:25 +03:00
super ( ) . __init__ ( name , vm_id , project , manager , console = console , console_type = console_type )
2015-03-16 20:45:21 +02:00
server_config = manager . config . get_section_config ( " Server " )
self . _host = server_config . get ( " host " , " 127.0.0.1 " )
self . _monitor_host = server_config . get ( " monitor_host " , " 127.0.0.1 " )
2015-10-19 04:19:27 +03:00
self . _linked_clone = linked_clone
2015-02-19 12:33:25 +02:00
self . _process = None
self . _cpulimit_process = None
2015-03-24 06:52:02 +02:00
self . _monitor = None
2015-02-19 12:33:25 +02:00
self . _stdout_file = " "
2015-08-27 17:06:11 +03:00
self . _execute_lock = asyncio . Lock ( )
2015-02-19 12:33:25 +02:00
2015-04-08 20:17:34 +03:00
# QEMU VM settings
2015-06-10 16:49:24 +03:00
if qemu_path :
try :
self . qemu_path = qemu_path
except QemuError as e :
2016-01-28 17:14:26 +02:00
# If the binary is not found for topologies 1.4 and later
# search via the platform otherwise use the binary name
2015-06-10 16:49:24 +03:00
if platform :
self . platform = platform
else :
2016-01-28 17:14:26 +02:00
self . qemu_path = os . path . basename ( qemu_path )
2015-06-10 16:49:24 +03:00
else :
self . platform = platform
2015-02-19 12:33:25 +02:00
self . _hda_disk_image = " "
self . _hdb_disk_image = " "
2015-03-10 19:50:30 +02:00
self . _hdc_disk_image = " "
self . _hdd_disk_image = " "
2015-08-03 08:02:02 +03:00
self . _hda_disk_interface = " ide "
self . _hdb_disk_interface = " ide "
self . _hdc_disk_interface = " ide "
self . _hdd_disk_interface = " ide "
self . _cdrom_image = " "
self . _boot_priority = " c "
2015-06-08 07:18:41 +03:00
self . _mac_address = " "
2015-02-19 12:33:25 +02:00
self . _options = " "
self . _ram = 256
2015-08-06 02:17:55 +03:00
self . _cpus = 1
2015-02-19 12:33:25 +02:00
self . _ethernet_adapters = [ ]
self . _adapter_type = " e1000 "
self . _initrd = " "
self . _kernel_image = " "
self . _kernel_command_line = " "
self . _legacy_networking = False
2015-06-03 07:33:38 +03:00
self . _acpi_shutdown = False
2015-02-19 12:33:25 +02:00
self . _cpu_throttling = 0 # means no CPU throttling
self . _process_priority = " low "
2015-06-08 07:18:41 +03:00
self . mac_address = " " # this will generate a MAC address
2015-02-19 12:33:25 +02:00
self . adapters = 1 # creates 1 adapter by default
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has been created ' . format ( name = self . _name , id = self . _id ) )
2015-02-19 12:33:25 +02:00
@property
def monitor ( self ) :
"""
Returns the TCP monitor port .
: returns : monitor port ( integer )
"""
return self . _monitor
@property
def qemu_path ( self ) :
"""
Returns the QEMU binary path for this QEMU VM .
: returns : QEMU path
"""
return self . _qemu_path
@qemu_path.setter
def qemu_path ( self , qemu_path ) :
"""
Sets the QEMU binary path this QEMU VM .
: param qemu_path : QEMU path
"""
2015-02-19 17:46:57 +02:00
if qemu_path and os . pathsep not in qemu_path :
2015-08-27 19:27:17 +03:00
new_qemu_path = shutil . which ( qemu_path , path = os . pathsep . join ( self . _manager . paths_list ( ) ) )
if new_qemu_path is None :
raise QemuError ( " QEMU binary path {} is not found in the path " . format ( qemu_path ) )
qemu_path = new_qemu_path
2015-02-19 17:46:57 +02:00
2015-06-10 16:49:24 +03:00
self . _check_qemu_path ( qemu_path )
self . _qemu_path = qemu_path
self . _platform = os . path . basename ( qemu_path )
if self . _platform == " qemu-kvm " :
self . _platform = " x86_64 "
else :
2015-08-26 14:47:12 +03:00
qemu_bin = os . path . basename ( qemu_path )
qemu_bin = re . sub ( r ' (w)? \ .(exe|EXE)$ ' , ' ' , qemu_bin )
2016-01-14 18:40:58 +02:00
# Old version of GNS3 provide a binary named qemu.exe
if qemu_bin == " qemu " :
self . _platform = " i386 "
else :
self . _platform = re . sub ( r ' ^qemu-system-(.*)$ ' , r ' \ 1 ' , qemu_bin , re . IGNORECASE )
2015-08-07 17:49:45 +03:00
if self . _platform . split ( " . " ) [ 0 ] not in QEMU_PLATFORMS :
2015-06-10 16:49:24 +03:00
raise QemuError ( " Platform {} is unknown " . format ( self . _platform ) )
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU path to {qemu_path} ' . format ( name = self . _name ,
id = self . _id ,
qemu_path = qemu_path ) )
def _check_qemu_path ( self , qemu_path ) :
2015-02-19 17:46:57 +02:00
if qemu_path is None :
2015-08-27 19:27:17 +03:00
raise QemuError ( " QEMU binary path is not set " )
2015-02-19 17:46:57 +02:00
if not os . path . exists ( qemu_path ) :
raise QemuError ( " QEMU binary ' {} ' is not accessible " . format ( qemu_path ) )
if not os . access ( qemu_path , os . X_OK ) :
raise QemuError ( " QEMU binary ' {} ' is not executable " . format ( qemu_path ) )
2015-06-10 16:49:24 +03:00
@property
def platform ( self ) :
"""
Return the current platform
"""
return self . _platform
@platform.setter
def platform ( self , platform ) :
self . _platform = platform
if sys . platform . startswith ( " win " ) :
self . qemu_path = " qemu-system- {} w.exe " . format ( platform )
else :
self . qemu_path = " qemu-system- {} " . format ( platform )
2015-02-19 12:33:25 +02:00
@property
def hda_disk_image ( self ) :
"""
Returns the hda disk image path for this QEMU VM .
: returns : QEMU hda disk image path
"""
return self . _hda_disk_image
@hda_disk_image.setter
def hda_disk_image ( self , hda_disk_image ) :
"""
Sets the hda disk image for this QEMU VM .
: param hda_disk_image : QEMU hda disk image path
"""
2015-04-14 19:46:55 +03:00
self . _hda_disk_image = self . manager . get_abs_image_path ( hda_disk_image )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hda disk image path to {disk_image} ' . format ( name = self . _name ,
id = self . _id ,
2015-04-14 19:46:55 +03:00
disk_image = self . _hda_disk_image ) )
2015-02-19 12:33:25 +02:00
@property
def hdb_disk_image ( self ) :
"""
Returns the hdb disk image path for this QEMU VM .
: returns : QEMU hdb disk image path
"""
return self . _hdb_disk_image
@hdb_disk_image.setter
def hdb_disk_image ( self , hdb_disk_image ) :
"""
Sets the hdb disk image for this QEMU VM .
: param hdb_disk_image : QEMU hdb disk image path
"""
2015-04-14 19:46:55 +03:00
self . _hdb_disk_image = self . manager . get_abs_image_path ( hdb_disk_image )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdb disk image path to {disk_image} ' . format ( name = self . _name ,
id = self . _id ,
2015-04-14 19:46:55 +03:00
disk_image = self . _hdb_disk_image ) )
2015-02-19 12:33:25 +02:00
2015-03-10 19:50:30 +02:00
@property
def hdc_disk_image ( self ) :
"""
Returns the hdc disk image path for this QEMU VM .
: returns : QEMU hdc disk image path
"""
return self . _hdc_disk_image
@hdc_disk_image.setter
def hdc_disk_image ( self , hdc_disk_image ) :
"""
Sets the hdc disk image for this QEMU VM .
: param hdc_disk_image : QEMU hdc disk image path
"""
2015-04-14 19:46:55 +03:00
self . _hdc_disk_image = self . manager . get_abs_image_path ( hdc_disk_image )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdc disk image path to {disk_image} ' . format ( name = self . _name ,
id = self . _id ,
2015-04-14 19:46:55 +03:00
disk_image = self . _hdc_disk_image ) )
2015-03-10 19:50:30 +02:00
@property
def hdd_disk_image ( self ) :
"""
Returns the hdd disk image path for this QEMU VM .
: returns : QEMU hdd disk image path
"""
return self . _hdd_disk_image
@hdd_disk_image.setter
def hdd_disk_image ( self , hdd_disk_image ) :
"""
Sets the hdd disk image for this QEMU VM .
: param hdd_disk_image : QEMU hdd disk image path
"""
2015-04-14 19:46:55 +03:00
self . _hdd_disk_image = hdd_disk_image
self . _hdd_disk_image = self . manager . get_abs_image_path ( hdd_disk_image )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdd disk image path to {disk_image} ' . format ( name = self . _name ,
id = self . _id ,
2015-04-14 19:46:55 +03:00
disk_image = self . _hdd_disk_image ) )
2015-03-10 19:50:30 +02:00
2015-08-03 08:02:02 +03:00
@property
def hda_disk_interface ( self ) :
"""
Returns the hda disk interface this QEMU VM .
: returns : QEMU hda disk interface
"""
return self . _hda_disk_interface
@hda_disk_interface.setter
def hda_disk_interface ( self , hda_disk_interface ) :
"""
Sets the hda disk interface for this QEMU VM .
: param hda_disk_interface : QEMU hda disk interface
"""
self . _hda_disk_interface = hda_disk_interface
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hda disk interface to {interface} ' . format ( name = self . _name ,
id = self . _id ,
interface = self . _hda_disk_interface ) )
@property
def hdb_disk_interface ( self ) :
"""
Returns the hdb disk interface this QEMU VM .
: returns : QEMU hdb disk interface
"""
2016-01-01 02:44:23 +02:00
return self . _hdb_disk_interface
2015-08-03 08:02:02 +03:00
@hdb_disk_interface.setter
def hdb_disk_interface ( self , hdb_disk_interface ) :
"""
Sets the hda disk interface for this QEMU VM .
: param hdb_disk_interface : QEMU hdb disk interface
"""
self . _hdb_disk_interface = hdb_disk_interface
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdb disk interface to {interface} ' . format ( name = self . _name ,
id = self . _id ,
interface = self . _hdb_disk_interface ) )
@property
def hdc_disk_interface ( self ) :
"""
Returns the hdc disk interface this QEMU VM .
: returns : QEMU hdc disk interface
"""
return self . _hdc_disk_interface
@hdc_disk_interface.setter
def hdc_disk_interface ( self , hdc_disk_interface ) :
"""
Sets the hdc disk interface for this QEMU VM .
: param hdc_disk_interface : QEMU hdc disk interface
"""
self . _hdc_disk_interface = hdc_disk_interface
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdc disk interface to {interface} ' . format ( name = self . _name ,
id = self . _id ,
interface = self . _hdc_disk_interface ) )
@property
def hdd_disk_interface ( self ) :
"""
Returns the hda disk interface this QEMU VM .
: returns : QEMU hda disk interface
"""
return self . _hdd_disk_interface
@hdd_disk_interface.setter
def hdd_disk_interface ( self , hdd_disk_interface ) :
"""
Sets the hdd disk interface for this QEMU VM .
: param hdd_disk_interface : QEMU hdd disk interface
"""
self . _hdd_disk_interface = hdd_disk_interface
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU hdd disk interface to {interface} ' . format ( name = self . _name ,
id = self . _id ,
interface = self . _hdd_disk_interface ) )
@property
def cdrom_image ( self ) :
"""
Returns the cdrom image path for this QEMU VM .
: returns : QEMU cdrom image path
"""
return self . _cdrom_image
@cdrom_image.setter
def cdrom_image ( self , cdrom_image ) :
"""
Sets the cdrom image for this QEMU VM .
: param cdrom_image : QEMU cdrom image path
"""
self . _cdrom_image = self . manager . get_abs_image_path ( cdrom_image )
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU cdrom image path to {cdrom_image} ' . format ( name = self . _name ,
id = self . _id ,
cdrom_image = self . _cdrom_image ) )
@property
def boot_priority ( self ) :
"""
Returns the boot priority for this QEMU VM .
: returns : QEMU boot priority
"""
return self . _boot_priority
@boot_priority.setter
def boot_priority ( self , boot_priority ) :
"""
Sets the boot priority for this QEMU VM .
: param boot_priority : QEMU boot priority
"""
self . _boot_priority = boot_priority
log . info ( ' QEMU VM " {name} " [ {id} ] has set the boot priority to {boot_priority} ' . format ( name = self . _name ,
id = self . _id ,
boot_priority = self . _boot_priority ) )
2015-02-19 12:33:25 +02:00
@property
def adapters ( self ) :
"""
2015-04-08 20:17:34 +03:00
Returns the number of Ethernet adapters for this QEMU VM .
2015-02-19 12:33:25 +02:00
: returns : number of adapters
"""
return len ( self . _ethernet_adapters )
@adapters.setter
def adapters ( self , adapters ) :
"""
2015-04-08 20:17:34 +03:00
Sets the number of Ethernet adapters for this QEMU VM .
2015-02-19 12:33:25 +02:00
: param adapters : number of adapters
"""
self . _ethernet_adapters . clear ( )
2015-04-08 20:17:34 +03:00
for adapter_number in range ( 0 , adapters ) :
2015-02-19 12:33:25 +02:00
self . _ethernet_adapters . append ( EthernetAdapter ( ) )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ]: number of Ethernet adapters changed to {adapters} ' . format ( name = self . _name ,
id = self . _id ,
adapters = adapters ) )
2015-02-19 12:33:25 +02:00
@property
def adapter_type ( self ) :
"""
2015-04-08 20:17:34 +03:00
Returns the adapter type for this QEMU VM .
2015-02-19 12:33:25 +02:00
: returns : adapter type ( string )
"""
return self . _adapter_type
@adapter_type.setter
def adapter_type ( self , adapter_type ) :
"""
2015-04-08 20:17:34 +03:00
Sets the adapter type for this QEMU VM .
2015-02-19 12:33:25 +02:00
: param adapter_type : adapter type ( string )
"""
self . _adapter_type = adapter_type
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ]: adapter type changed to {adapter_type} ' . format ( name = self . _name ,
id = self . _id ,
adapter_type = adapter_type ) )
2015-02-19 12:33:25 +02:00
2015-06-03 23:52:49 +03:00
@property
def mac_address ( self ) :
"""
Returns the MAC address for this QEMU VM .
: returns : adapter type ( string )
"""
return self . _mac_address
@mac_address.setter
def mac_address ( self , mac_address ) :
"""
Sets the MAC address for this QEMU VM .
: param mac_address : MAC address
"""
2015-06-08 07:18:41 +03:00
if not mac_address :
self . _mac_address = " 00:00:ab: %s : %s :00 " % ( self . id [ - 4 : - 2 ] , self . id [ - 2 : ] )
else :
self . _mac_address = mac_address
2015-06-03 23:52:49 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ]: MAC address changed to {mac_addr} ' . format ( name = self . _name ,
id = self . _id ,
mac_addr = mac_address ) )
2015-02-19 12:33:25 +02:00
@property
def legacy_networking ( self ) :
"""
Returns either QEMU legacy networking commands are used .
: returns : boolean
"""
return self . _legacy_networking
@legacy_networking.setter
def legacy_networking ( self , legacy_networking ) :
"""
Sets either QEMU legacy networking commands are used .
: param legacy_networking : boolean
"""
if legacy_networking :
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has enabled legacy networking ' . format ( name = self . _name , id = self . _id ) )
2015-02-19 12:33:25 +02:00
else :
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has disabled legacy networking ' . format ( name = self . _name , id = self . _id ) )
2015-02-19 12:33:25 +02:00
self . _legacy_networking = legacy_networking
2015-06-03 07:33:38 +03:00
@property
def acpi_shutdown ( self ) :
"""
Returns either this QEMU VM can be ACPI shutdown .
: returns : boolean
"""
return self . _acpi_shutdown
@acpi_shutdown.setter
def acpi_shutdown ( self , acpi_shutdown ) :
"""
Sets either this QEMU VM can be ACPI shutdown .
: param acpi_shutdown : boolean
"""
if acpi_shutdown :
log . info ( ' QEMU VM " {name} " [ {id} ] has enabled ACPI shutdown ' . format ( name = self . _name , id = self . _id ) )
else :
log . info ( ' QEMU VM " {name} " [ {id} ] has disabled ACPI shutdown ' . format ( name = self . _name , id = self . _id ) )
self . _acpi_shutdown = acpi_shutdown
2015-02-19 12:33:25 +02:00
@property
def cpu_throttling ( self ) :
"""
Returns the percentage of CPU allowed .
: returns : integer
"""
return self . _cpu_throttling
@cpu_throttling.setter
def cpu_throttling ( self , cpu_throttling ) :
"""
Sets the percentage of CPU allowed .
: param cpu_throttling : integer
"""
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the percentage of CPU allowed to {cpu} ' . format ( name = self . _name ,
id = self . _id ,
cpu = cpu_throttling ) )
2015-02-19 12:33:25 +02:00
self . _cpu_throttling = cpu_throttling
self . _stop_cpulimit ( )
if cpu_throttling :
self . _set_cpu_throttling ( )
@property
def process_priority ( self ) :
"""
Returns the process priority .
: returns : string
"""
return self . _process_priority
@process_priority.setter
def process_priority ( self , process_priority ) :
"""
Sets the process priority .
: param process_priority : string
"""
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the process priority to {priority} ' . format ( name = self . _name ,
id = self . _id ,
priority = process_priority ) )
2015-02-19 12:33:25 +02:00
self . _process_priority = process_priority
@property
def ram ( self ) :
"""
Returns the RAM amount for this QEMU VM .
: returns : RAM amount in MB
"""
return self . _ram
@ram.setter
def ram ( self , ram ) :
"""
Sets the amount of RAM for this QEMU VM .
: param ram : RAM amount in MB
"""
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the RAM to {ram} ' . format ( name = self . _name , id = self . _id , ram = ram ) )
2015-02-19 12:33:25 +02:00
self . _ram = ram
2015-08-06 02:17:55 +03:00
@property
def cpus ( self ) :
"""
Returns the number of vCPUs this QEMU VM .
: returns : number of vCPUs .
"""
return self . _cpus
@cpus.setter
def cpus ( self , cpus ) :
"""
Sets the number of vCPUs this QEMU VM .
: param cpus : number of vCPUs .
"""
log . info ( ' QEMU VM " {name} " [ {id} ] has set the number of vCPUs to {cpus} ' . format ( name = self . _name , id = self . _id , cpus = cpus ) )
self . _cpus = cpus
2015-02-19 12:33:25 +02:00
@property
def options ( self ) :
"""
Returns the options for this QEMU VM .
: returns : QEMU options
"""
return self . _options
@options.setter
def options ( self , options ) :
"""
Sets the options for this QEMU VM .
: param options : QEMU options
"""
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU options to {options} ' . format ( name = self . _name ,
id = self . _id ,
options = options ) )
2015-06-12 10:40:38 +03:00
2015-07-22 07:58:28 +03:00
if not sys . platform . startswith ( " linux " ) :
if " -no-kvm " in options :
2015-07-28 00:31:42 +03:00
options = options . replace ( " -no-kvm " , " " )
2015-07-22 07:58:28 +03:00
if " -enable-kvm " in options :
2015-07-28 00:31:42 +03:00
options = options . replace ( " -enable-kvm " , " " )
2015-11-10 17:25:02 +02:00
elif " -icount " in options and ( " -no-kvm " not in options ) :
2015-09-19 00:40:56 +03:00
# automatically add the -no-kvm option if -icount is detected
# to help with the migration of ASA VMs created before version 1.4
options = " -no-kvm " + options
2015-06-12 10:40:38 +03:00
self . _options = options . strip ( )
2015-02-19 12:33:25 +02:00
@property
def initrd ( self ) :
"""
Returns the initrd path for this QEMU VM .
: returns : QEMU initrd path
"""
return self . _initrd
@initrd.setter
def initrd ( self , initrd ) :
"""
Sets the initrd path for this QEMU VM .
: param initrd : QEMU initrd path
"""
2015-03-11 23:04:11 +02:00
if not os . path . isabs ( initrd ) :
server_config = self . manager . config . get_section_config ( " Server " )
2015-03-10 05:57:21 +02:00
initrd = os . path . join ( os . path . expanduser ( server_config . get ( " images_path " , " ~/GNS3/images " ) ) , " QEMU " , initrd )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU initrd path to {initrd} ' . format ( name = self . _name ,
id = self . _id ,
initrd = initrd ) )
2016-01-22 20:46:05 +02:00
if " asa " in initrd :
self . project . emit ( " log.warning " , { " message " : " Warning ASA 8 is not officialy supported by GNS3 and Cisco, we recommend to use ASAv. Depending of your hardware this could not work or you could be limited to one instance. " } )
2015-02-19 12:33:25 +02:00
self . _initrd = initrd
@property
def kernel_image ( self ) :
"""
Returns the kernel image path for this QEMU VM .
: returns : QEMU kernel image path
"""
return self . _kernel_image
@kernel_image.setter
def kernel_image ( self , kernel_image ) :
"""
Sets the kernel image path for this QEMU VM .
: param kernel_image : QEMU kernel image path
"""
2015-03-11 23:04:11 +02:00
if not os . path . isabs ( kernel_image ) :
server_config = self . manager . config . get_section_config ( " Server " )
2015-03-10 05:57:21 +02:00
kernel_image = os . path . join ( os . path . expanduser ( server_config . get ( " images_path " , " ~/GNS3/images " ) ) , " QEMU " , kernel_image )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU kernel image path to {kernel_image} ' . format ( name = self . _name ,
id = self . _id ,
kernel_image = kernel_image ) )
2015-02-19 12:33:25 +02:00
self . _kernel_image = kernel_image
@property
def kernel_command_line ( self ) :
"""
Returns the kernel command line for this QEMU VM .
: returns : QEMU kernel command line
"""
return self . _kernel_command_line
@kernel_command_line.setter
def kernel_command_line ( self , kernel_command_line ) :
"""
Sets the kernel command line for this QEMU VM .
: param kernel_command_line : QEMU kernel command line
"""
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ] has set the QEMU kernel command line to {kernel_command_line} ' . format ( name = self . _name ,
id = self . _id ,
kernel_command_line = kernel_command_line ) )
2015-02-19 12:33:25 +02:00
self . _kernel_command_line = kernel_command_line
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def _set_process_priority ( self ) :
"""
Changes the process priority
"""
2015-12-22 14:15:28 +02:00
if self . _process_priority == " normal " :
return
2015-02-19 12:33:25 +02:00
if sys . platform . startswith ( " win " ) :
try :
import win32api
import win32con
import win32process
except ImportError :
log . error ( " pywin32 must be installed to change the priority class for QEMU VM {} " . format ( self . _name ) )
else :
2015-12-22 14:15:28 +02:00
log . info ( " Setting QEMU VM {} priority class to {} " . format ( self . _name , self . _process_priority ) )
2015-02-19 12:33:25 +02:00
handle = win32api . OpenProcess ( win32con . PROCESS_ALL_ACCESS , 0 , self . _process . pid )
if self . _process_priority == " realtime " :
priority = win32process . REALTIME_PRIORITY_CLASS
elif self . _process_priority == " very high " :
priority = win32process . HIGH_PRIORITY_CLASS
elif self . _process_priority == " high " :
priority = win32process . ABOVE_NORMAL_PRIORITY_CLASS
elif self . _process_priority == " low " :
priority = win32process . BELOW_NORMAL_PRIORITY_CLASS
elif self . _process_priority == " very low " :
priority = win32process . IDLE_PRIORITY_CLASS
else :
priority = win32process . NORMAL_PRIORITY_CLASS
2016-01-03 22:23:35 +02:00
try :
win32process . SetPriorityClass ( handle , priority )
except win32process . error as e :
log . error ( ' Could not change process priority for QEMU VM " {} " : {} ' . format ( self . _name , e ) )
2015-02-19 12:33:25 +02:00
else :
if self . _process_priority == " realtime " :
priority = - 20
elif self . _process_priority == " very high " :
priority = - 15
elif self . _process_priority == " high " :
priority = - 5
elif self . _process_priority == " low " :
priority = 5
elif self . _process_priority == " very low " :
priority = 19
else :
priority = 0
try :
2015-02-19 17:46:57 +02:00
process = yield from asyncio . create_subprocess_exec ( ' renice ' , ' -n ' , str ( priority ) , ' -p ' , str ( self . _process . pid ) )
yield from process . wait ( )
2015-02-19 12:33:25 +02:00
except ( OSError , subprocess . SubprocessError ) as e :
2015-04-08 20:17:34 +03:00
log . error ( ' Could not change process priority for QEMU VM " {} " : {} ' . format ( self . _name , e ) )
2015-02-19 12:33:25 +02:00
def _stop_cpulimit ( self ) :
"""
Stops the cpulimit process .
"""
2015-02-26 11:18:01 +02:00
if self . _cpulimit_process and self . _cpulimit_process . returncode is None :
2015-02-19 12:33:25 +02:00
self . _cpulimit_process . kill ( )
try :
self . _process . wait ( 3 )
except subprocess . TimeoutExpired :
2015-04-08 20:17:34 +03:00
log . error ( " Could not kill cpulimit process {} " . format ( self . _cpulimit_process . pid ) )
2015-02-19 12:33:25 +02:00
def _set_cpu_throttling ( self ) :
"""
Limits the CPU usage for current QEMU process .
"""
if not self . is_running ( ) :
return
try :
if sys . platform . startswith ( " win " ) and hasattr ( sys , " frozen " ) :
cpulimit_exec = os . path . join ( os . path . dirname ( os . path . abspath ( sys . executable ) ) , " cpulimit " , " cpulimit.exe " )
else :
cpulimit_exec = " cpulimit "
2015-02-19 17:46:57 +02:00
subprocess . Popen ( [ cpulimit_exec , " --lazy " , " --pid= {} " . format ( self . _process . pid ) , " --limit= {} " . format ( self . _cpu_throttling ) ] , cwd = self . working_dir )
2015-02-19 12:33:25 +02:00
log . info ( " CPU throttled to {} % " . format ( self . _cpu_throttling ) )
except FileNotFoundError :
raise QemuError ( " cpulimit could not be found, please install it or deactivate CPU throttling " )
except ( OSError , subprocess . SubprocessError ) as e :
raise QemuError ( " Could not throttle CPU: {} " . format ( e ) )
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def start ( self ) :
"""
Starts this QEMU VM .
"""
2015-08-27 17:06:11 +03:00
with ( yield from self . _execute_lock ) :
if self . is_running ( ) :
# resume the VM if it is paused
yield from self . resume ( )
return
2015-02-19 12:33:25 +02:00
2015-08-27 17:06:11 +03:00
else :
2015-03-24 06:52:02 +02:00
2015-08-27 17:06:11 +03:00
if self . _manager . config . get_section_config ( " Qemu " ) . getboolean ( " monitor " , True ) :
try :
info = socket . getaddrinfo ( self . _monitor_host , 0 , socket . AF_UNSPEC , socket . SOCK_STREAM , 0 , socket . AI_PASSIVE )
if not info :
raise QemuError ( " getaddrinfo returns an empty list on {} " . format ( self . _monitor_host ) )
for res in info :
af , socktype , proto , _ , sa = res
# let the OS find an unused port for the Qemu monitor
with socket . socket ( af , socktype , proto ) as sock :
sock . bind ( sa )
self . _monitor = sock . getsockname ( ) [ 1 ]
except OSError as e :
raise QemuError ( " Could not find free port for the Qemu monitor: {} " . format ( e ) )
2015-10-13 00:57:37 +03:00
# check if there is enough RAM to run
self . check_available_ram ( self . ram )
2016-02-02 19:25:17 +02:00
command = yield from self . _build_command ( )
command_string = " " . join ( shlex . quote ( s ) for s in command )
2015-03-24 06:52:02 +02:00
try :
2015-08-27 17:06:11 +03:00
log . info ( " Starting QEMU with: {} " . format ( command_string ) )
self . _stdout_file = os . path . join ( self . working_dir , " qemu.log " )
log . info ( " logging to {} " . format ( self . _stdout_file ) )
with open ( self . _stdout_file , " w " , encoding = " utf-8 " ) as fd :
fd . write ( " Start QEMU with {} \n \n Execution log: \n " . format ( command_string ) )
2016-02-02 19:25:17 +02:00
self . command_line = ' ' . join ( command )
self . _process = yield from asyncio . create_subprocess_exec ( * command ,
2015-08-27 17:06:11 +03:00
stdout = fd ,
stderr = subprocess . STDOUT ,
cwd = self . working_dir )
log . info ( ' QEMU VM " {} " started PID= {} ' . format ( self . _name , self . _process . pid ) )
self . status = " started "
monitor_process ( self . _process , self . _termination_callback )
except ( OSError , subprocess . SubprocessError , UnicodeEncodeError ) as e :
stdout = self . read_stdout ( )
log . error ( " Could not start QEMU {} : {} \n {} " . format ( self . qemu_path , e , stdout ) )
raise QemuError ( " Could not start QEMU {} : {} \n {} " . format ( self . qemu_path , e , stdout ) )
2015-10-12 17:26:07 +03:00
yield from self . _set_process_priority ( )
2015-08-27 17:06:11 +03:00
if self . _cpu_throttling :
self . _set_cpu_throttling ( )
if " -enable-kvm " in command_string :
self . _hw_virtualization = True
2015-07-22 07:58:28 +03:00
2015-03-04 17:01:56 +02:00
def _termination_callback ( self , returncode ) :
"""
2015-05-13 22:53:42 +03:00
Called when the process has stopped .
2015-03-04 17:01:56 +02:00
: param returncode : Process returncode
"""
2015-05-13 22:53:42 +03:00
2015-03-04 17:01:56 +02:00
if self . started :
2015-05-13 22:53:42 +03:00
log . info ( " QEMU process has stopped, return code: %d " , returncode )
2015-03-04 17:01:56 +02:00
self . status = " stopped "
2015-07-22 07:58:28 +03:00
self . _hw_virtualization = False
2015-03-04 17:01:56 +02:00
self . _process = None
2015-06-26 15:41:58 +03:00
if returncode != 0 :
2015-06-26 16:10:41 +03:00
self . project . emit ( " log.error " , { " message " : " QEMU process has stopped, return code: {} \n {} " . format ( returncode , self . read_stdout ( ) ) } )
2015-03-04 17:01:56 +02:00
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def stop ( self ) :
"""
Stops this QEMU VM .
"""
2015-08-27 17:06:11 +03:00
with ( yield from self . _execute_lock ) :
# stop the QEMU process
self . _hw_virtualization = False
if self . is_running ( ) :
log . info ( ' Stopping QEMU VM " {} " PID= {} ' . format ( self . _name , self . _process . pid ) )
self . status = " stopped "
try :
if self . acpi_shutdown :
yield from self . _control_vm ( " system_powerdown " )
yield from gns3server . utils . asyncio . wait_for_process_termination ( self . _process , timeout = 30 )
else :
self . _process . terminate ( )
yield from gns3server . utils . asyncio . wait_for_process_termination ( self . _process , timeout = 3 )
2015-12-07 11:48:57 +02:00
except ProcessLookupError :
pass
2015-08-27 17:06:11 +03:00
except asyncio . TimeoutError :
2015-09-11 15:16:56 +03:00
try :
self . _process . kill ( )
except ProcessLookupError :
pass
2015-08-27 17:06:11 +03:00
if self . _process . returncode is None :
log . warn ( ' QEMU VM " {} " PID= {} is still running ' . format ( self . _name , self . _process . pid ) )
self . _process = None
self . _stop_cpulimit ( )
2015-02-19 12:33:25 +02:00
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-21 01:15:56 +02:00
def _control_vm ( self , command , expected = None ) :
2015-02-19 12:33:25 +02:00
"""
Executes a command with QEMU monitor when this VM is running .
: param command : QEMU monitor command ( e . g . info status , stop etc . )
2015-04-08 20:17:34 +03:00
: param expected : An array of expected strings
2015-02-19 12:33:25 +02:00
2015-04-08 20:17:34 +03:00
: returns : result of the command ( matched object or None )
2015-02-19 12:33:25 +02:00
"""
result = None
if self . is_running ( ) and self . _monitor :
log . debug ( " Execute QEMU monitor command: {} " . format ( command ) )
try :
2015-03-07 05:08:00 +02:00
log . info ( " Connecting to Qemu monitor on {} : {} " . format ( self . _monitor_host , self . _monitor ) )
2015-03-07 00:16:19 +02:00
reader , writer = yield from asyncio . open_connection ( self . _monitor_host , self . _monitor )
2015-02-19 12:33:25 +02:00
except OSError as e :
log . warn ( " Could not connect to QEMU monitor: {} " . format ( e ) )
return result
try :
2015-02-21 01:15:56 +02:00
writer . write ( command . encode ( ' ascii ' ) + b " \n " )
2015-02-19 12:33:25 +02:00
except OSError as e :
log . warn ( " Could not write to QEMU monitor: {} " . format ( e ) )
2015-02-21 01:15:56 +02:00
writer . close ( )
2015-02-19 12:33:25 +02:00
return result
if expected :
try :
2015-02-21 01:15:56 +02:00
while result is None :
line = yield from reader . readline ( )
if not line :
break
for expect in expected :
if expect in line :
2015-04-27 06:19:39 +03:00
result = line . decode ( " utf-8 " ) . strip ( )
2015-02-21 01:15:56 +02:00
break
2015-02-19 12:33:25 +02:00
except EOFError as e :
log . warn ( " Could not read from QEMU monitor: {} " . format ( e ) )
2015-02-21 01:15:56 +02:00
writer . close ( )
2015-02-19 12:33:25 +02:00
return result
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
def close ( self ) :
2015-04-08 20:17:34 +03:00
"""
Closes this QEMU VM .
"""
2015-02-19 17:46:57 +02:00
2016-02-29 11:38:30 +02:00
if not ( yield from super ( ) . close ( ) ) :
return False
2015-06-03 07:33:38 +03:00
self . acpi_shutdown = False
2015-02-19 17:46:57 +02:00
yield from self . stop ( )
2015-10-08 05:48:36 +03:00
for adapter in self . _ethernet_adapters :
if adapter is not None :
for nio in adapter . ports . values ( ) :
if nio and isinstance ( nio , NIOUDP ) :
self . manager . port_manager . release_udp_port ( nio . lport , self . _project )
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def _get_vm_status ( self ) :
"""
2015-04-08 20:17:34 +03:00
Returns this VM suspend status .
2015-02-19 12:33:25 +02:00
2015-03-17 12:02:14 +02:00
Status are extracted from :
https : / / github . com / qemu / qemu / blob / master / qapi - schema . json #L152
2015-02-19 12:33:25 +02:00
: returns : status ( string )
"""
2015-03-17 12:02:14 +02:00
result = yield from self . _control_vm ( " info status " , [
b " debug " , b " inmigrate " , b " internal-error " , b " io-error " ,
b " paused " , b " postmigrate " , b " prelaunch " , b " finish-migrate " ,
b " restore-vm " , b " running " , b " save-vm " , b " shutdown " , b " suspended " ,
b " watchdog " , b " guest-panicked "
] )
2015-03-23 04:40:19 +02:00
if result is None :
return result
2015-03-07 00:16:19 +02:00
return result . rsplit ( ' ' , 1 ) [ 1 ]
2015-02-19 12:33:25 +02:00
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def suspend ( self ) :
"""
Suspends this QEMU VM .
"""
2015-03-24 06:52:02 +02:00
if self . is_running ( ) :
vm_status = yield from self . _get_vm_status ( )
if vm_status is None :
raise QemuError ( " Suspending a QEMU VM is not supported " )
elif vm_status == " running " :
yield from self . _control_vm ( " stop " )
log . debug ( " QEMU VM has been suspended " )
else :
log . info ( " QEMU VM is not running to be suspended, current status is {} " . format ( vm_status ) )
2015-02-19 12:33:25 +02:00
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def reload ( self ) :
"""
Reloads this QEMU VM .
"""
2015-02-19 17:46:57 +02:00
yield from self . _control_vm ( " system_reset " )
2015-02-19 12:33:25 +02:00
log . debug ( " QEMU VM has been reset " )
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def resume ( self ) :
"""
Resumes this QEMU VM .
"""
2015-02-19 17:46:57 +02:00
vm_status = yield from self . _get_vm_status ( )
2015-03-23 04:40:19 +02:00
if vm_status is None :
raise QemuError ( " Resuming a QEMU VM is not supported " )
elif vm_status == " paused " :
2015-02-19 17:46:57 +02:00
yield from self . _control_vm ( " cont " )
2015-02-19 12:33:25 +02:00
log . debug ( " QEMU VM has been resumed " )
else :
log . info ( " QEMU VM is not paused to be resumed, current status is {} " . format ( vm_status ) )
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-04-08 20:17:34 +03:00
def adapter_add_nio_binding ( self , adapter_number , nio ) :
2015-02-19 12:33:25 +02:00
"""
Adds a port NIO binding .
2015-04-08 20:17:34 +03:00
: param adapter_number : adapter number
2015-03-07 05:08:00 +02:00
: param nio : NIO instance to add to the adapter
2015-02-19 12:33:25 +02:00
"""
try :
2015-04-08 20:17:34 +03:00
adapter = self . _ethernet_adapters [ adapter_number ]
2015-02-19 12:33:25 +02:00
except IndexError :
2015-04-08 20:17:34 +03:00
raise QemuError ( ' Adapter {adapter_number} does not exist on QEMU VM " {name} " ' . format ( name = self . _name ,
adapter_number = adapter_number ) )
2015-02-19 12:33:25 +02:00
if self . is_running ( ) :
2015-05-27 22:56:27 +03:00
raise QemuError ( " Sorry, adding a link to a started Qemu VM is not supported. " )
# FIXME: does the code below work? very undocumented feature...
2015-02-19 12:33:25 +02:00
# dynamically configure an UDP tunnel on the QEMU VM adapter
2016-02-05 11:06:34 +02:00
# if nio and isinstance(nio, NIOUDP):
# if self._legacy_networking:
# yield from self._control_vm("host_net_remove {} gns3-{}".format(adapter_number, adapter_number))
# yield from self._control_vm("host_net_add udp vlan={},name=gns3-{},sport={},dport={},daddr={}".format(adapter_number,
# adapter_number,
# nio.lport,
# nio.rport,
# nio.rhost))
# else:
# # Apparently there is a bug in Qemu...
# # netdev_add [user|tap|socket|hubport|netmap],id=str[,prop=value][,...] -- add host network device
# # netdev_del id -- remove host network device
# yield from self._control_vm("netdev_del gns3-{}".format(adapter_number))
# yield from self._control_vm("netdev_add socket,id=gns3-{},udp={}:{},localaddr={}:{}".format(adapter_number,
# nio.rhost,
# nio.rport,
# self._host,
# nio.lport))
2015-02-19 12:33:25 +02:00
adapter . add_nio ( 0 , nio )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ]: {nio} added to adapter {adapter_number} ' . format ( name = self . _name ,
2015-04-14 19:46:55 +03:00
id = self . _id ,
nio = nio ,
adapter_number = adapter_number ) )
2015-02-19 12:33:25 +02:00
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-04-08 20:17:34 +03:00
def adapter_remove_nio_binding ( self , adapter_number ) :
2015-02-19 12:33:25 +02:00
"""
Removes a port NIO binding .
2015-04-08 20:17:34 +03:00
: param adapter_number : adapter number
2015-02-19 12:33:25 +02:00
: returns : NIO instance
"""
try :
2015-04-08 20:17:34 +03:00
adapter = self . _ethernet_adapters [ adapter_number ]
2015-02-19 12:33:25 +02:00
except IndexError :
2015-04-08 20:17:34 +03:00
raise QemuError ( ' Adapter {adapter_number} does not exist on QEMU VM " {name} " ' . format ( name = self . _name ,
adapter_number = adapter_number ) )
2015-02-19 12:33:25 +02:00
if self . is_running ( ) :
2015-05-27 22:56:27 +03:00
# FIXME: does the code below work? very undocumented feature...
2015-02-19 12:33:25 +02:00
# dynamically disable the QEMU VM adapter
2015-04-08 20:17:34 +03:00
yield from self . _control_vm ( " host_net_remove {} gns3- {} " . format ( adapter_number , adapter_number ) )
yield from self . _control_vm ( " host_net_add user vlan= {} ,name=gns3- {} " . format ( adapter_number , adapter_number ) )
2015-02-19 12:33:25 +02:00
nio = adapter . get_nio ( 0 )
2015-03-07 05:08:00 +02:00
if isinstance ( nio , NIOUDP ) :
2015-03-22 01:19:12 +02:00
self . manager . port_manager . release_udp_port ( nio . lport , self . _project )
2015-02-19 12:33:25 +02:00
adapter . remove_nio ( 0 )
2015-04-08 20:17:34 +03:00
log . info ( ' QEMU VM " {name} " [ {id} ]: {nio} removed from adapter {adapter_number} ' . format ( name = self . _name ,
id = self . _id ,
nio = nio ,
adapter_number = adapter_number ) )
2015-02-19 12:33:25 +02:00
return nio
@property
def started ( self ) :
"""
Returns either this QEMU VM has been started or not .
: returns : boolean
"""
2015-03-04 17:01:56 +02:00
return self . status == " started "
2015-02-19 12:33:25 +02:00
def read_stdout ( self ) :
"""
Reads the standard output of the QEMU process .
Only use when the process has been stopped or has crashed .
"""
output = " "
if self . _stdout_file :
try :
2015-04-25 20:58:34 +03:00
with open ( self . _stdout_file , " rb " ) as file :
output = file . read ( ) . decode ( " utf-8 " , errors = " replace " )
2015-02-19 12:33:25 +02:00
except OSError as e :
2015-04-08 20:17:34 +03:00
log . warn ( " Could not read {} : {} " . format ( self . _stdout_file , e ) )
2015-02-19 12:33:25 +02:00
return output
def is_running ( self ) :
"""
Checks if the QEMU process is running
: returns : True or False
"""
2015-02-19 17:46:57 +02:00
if self . _process :
2015-02-25 16:42:01 +02:00
if self . _process . returncode is None :
return True
else :
self . _process = None
2015-02-19 12:33:25 +02:00
return False
def command ( self ) :
"""
Returns the QEMU command line .
: returns : QEMU command line ( string )
"""
return " " . join ( self . _build_command ( ) )
def _serial_options ( self ) :
if self . _console :
2015-03-03 03:19:11 +02:00
return [ " -serial " , " telnet: {} : {} ,server,nowait " . format ( self . _manager . port_manager . console_host , self . _console ) ]
2015-02-19 12:33:25 +02:00
else :
return [ ]
2015-06-25 04:09:17 +03:00
def _vnc_options ( self ) :
if self . _console :
2015-07-04 01:06:25 +03:00
vnc_port = self . _console - 5900 # subtract by 5900 to get the display number
2015-06-25 04:09:17 +03:00
return [ " -vnc " , " {} : {} " . format ( self . _manager . port_manager . console_host , vnc_port ) ]
else :
return [ ]
2015-02-19 12:33:25 +02:00
def _monitor_options ( self ) :
if self . _monitor :
2015-03-10 08:34:57 +02:00
return [ " -monitor " , " tcp: {} : {} ,server,nowait " . format ( self . _monitor_host , self . _monitor ) ]
2015-02-19 12:33:25 +02:00
else :
return [ ]
2015-07-27 17:19:15 +03:00
def _get_qemu_img ( self ) :
"""
Search the qemu - img binary in the same binary of the qemu binary
for avoiding version incompatibily .
: returns : qemu - img path or raise an error
"""
2015-02-19 12:33:25 +02:00
qemu_img_path = " "
2015-02-19 17:46:57 +02:00
qemu_path_dir = os . path . dirname ( self . qemu_path )
2015-02-19 12:33:25 +02:00
try :
for f in os . listdir ( qemu_path_dir ) :
if f . startswith ( " qemu-img " ) :
qemu_img_path = os . path . join ( qemu_path_dir , f )
except OSError as e :
raise QemuError ( " Error while looking for qemu-img in {} : {} " . format ( qemu_path_dir , e ) )
if not qemu_img_path :
raise QemuError ( " Could not find qemu-img in {} " . format ( qemu_path_dir ) )
2015-07-27 17:19:15 +03:00
return qemu_img_path
@asyncio.coroutine
def _disk_options ( self ) :
options = [ ]
qemu_img_path = self . _get_qemu_img ( )
2015-07-22 01:45:44 +03:00
if self . _hda_disk_image :
if not os . path . isfile ( self . _hda_disk_image ) or not os . path . exists ( self . _hda_disk_image ) :
if os . path . islink ( self . _hda_disk_image ) :
raise QemuError ( " hda disk image ' {} ' linked to ' {} ' is not accessible " . format ( self . _hda_disk_image , os . path . realpath ( self . _hda_disk_image ) ) )
else :
raise QemuError ( " hda disk image ' {} ' is not accessible " . format ( self . _hda_disk_image ) )
2015-10-19 04:19:27 +03:00
if self . _linked_clone :
hda_disk = os . path . join ( self . working_dir , " hda_disk.qcow2 " )
if not os . path . exists ( hda_disk ) :
# create the disk
try :
process = yield from asyncio . create_subprocess_exec ( qemu_img_path , " create " , " -o " ,
" backing_file= {} " . format ( self . _hda_disk_image ) ,
" -f " , " qcow2 " , hda_disk )
retcode = yield from process . wait ( )
log . info ( " {} returned with {} " . format ( qemu_img_path , retcode ) )
except ( OSError , subprocess . SubprocessError ) as e :
raise QemuError ( " Could not create hda disk image {} " . format ( e ) )
else :
hda_disk = self . _hda_disk_image
2015-08-03 08:02:02 +03:00
options . extend ( [ " -drive " , ' file= {} ,if= {} ,index=0,media=disk ' . format ( hda_disk , self . hda_disk_interface ) ] )
2015-03-10 19:50:30 +02:00
2015-02-19 12:33:25 +02:00
if self . _hdb_disk_image :
if not os . path . isfile ( self . _hdb_disk_image ) or not os . path . exists ( self . _hdb_disk_image ) :
if os . path . islink ( self . _hdb_disk_image ) :
raise QemuError ( " hdb disk image ' {} ' linked to ' {} ' is not accessible " . format ( self . _hdb_disk_image , os . path . realpath ( self . _hdb_disk_image ) ) )
else :
raise QemuError ( " hdb disk image ' {} ' is not accessible " . format ( self . _hdb_disk_image ) )
2015-10-19 04:19:27 +03:00
if self . _linked_clone :
hdb_disk = os . path . join ( self . working_dir , " hdb_disk.qcow2 " )
if not os . path . exists ( hdb_disk ) :
try :
process = yield from asyncio . create_subprocess_exec ( qemu_img_path , " create " , " -o " ,
" backing_file= {} " . format ( self . _hdb_disk_image ) ,
" -f " , " qcow2 " , hdb_disk )
retcode = yield from process . wait ( )
log . info ( " {} returned with {} " . format ( qemu_img_path , retcode ) )
except ( OSError , subprocess . SubprocessError ) as e :
raise QemuError ( " Could not create hdb disk image {} " . format ( e ) )
else :
hdb_disk = self . _hdb_disk_image
2015-08-03 08:02:02 +03:00
options . extend ( [ " -drive " , ' file= {} ,if= {} ,index=1,media=disk ' . format ( hdb_disk , self . hdb_disk_interface ) ] )
2015-02-19 12:33:25 +02:00
2015-03-10 19:50:30 +02:00
if self . _hdc_disk_image :
if not os . path . isfile ( self . _hdc_disk_image ) or not os . path . exists ( self . _hdc_disk_image ) :
if os . path . islink ( self . _hdc_disk_image ) :
raise QemuError ( " hdc disk image ' {} ' linked to ' {} ' is not accessible " . format ( self . _hdc_disk_image , os . path . realpath ( self . _hdc_disk_image ) ) )
else :
raise QemuError ( " hdc disk image ' {} ' is not accessible " . format ( self . _hdc_disk_image ) )
2015-10-19 04:19:27 +03:00
if self . _linked_clone :
hdc_disk = os . path . join ( self . working_dir , " hdc_disk.qcow2 " )
if not os . path . exists ( hdc_disk ) :
try :
process = yield from asyncio . create_subprocess_exec ( qemu_img_path , " create " , " -o " ,
" backing_file= {} " . format ( self . _hdc_disk_image ) ,
" -f " , " qcow2 " , hdc_disk )
retcode = yield from process . wait ( )
log . info ( " {} returned with {} " . format ( qemu_img_path , retcode ) )
except ( OSError , subprocess . SubprocessError ) as e :
raise QemuError ( " Could not create hdc disk image {} " . format ( e ) )
else :
hdc_disk = self . _hdc_disk_image
2015-08-03 08:02:02 +03:00
options . extend ( [ " -drive " , ' file= {} ,if= {} ,index=2,media=disk ' . format ( hdc_disk , self . hdc_disk_interface ) ] )
2015-03-10 19:50:30 +02:00
if self . _hdd_disk_image :
if not os . path . isfile ( self . _hdd_disk_image ) or not os . path . exists ( self . _hdd_disk_image ) :
if os . path . islink ( self . _hdd_disk_image ) :
raise QemuError ( " hdd disk image ' {} ' linked to ' {} ' is not accessible " . format ( self . _hdd_disk_image , os . path . realpath ( self . _hdd_disk_image ) ) )
else :
raise QemuError ( " hdd disk image ' {} ' is not accessible " . format ( self . _hdd_disk_image ) )
2015-10-19 04:19:27 +03:00
if self . _linked_clone :
hdd_disk = os . path . join ( self . working_dir , " hdd_disk.qcow2 " )
if not os . path . exists ( hdd_disk ) :
try :
process = yield from asyncio . create_subprocess_exec ( qemu_img_path , " create " , " -o " ,
" backing_file= {} " . format ( self . _hdd_disk_image ) ,
" -f " , " qcow2 " , hdd_disk )
retcode = yield from process . wait ( )
log . info ( " {} returned with {} " . format ( qemu_img_path , retcode ) )
except ( OSError , subprocess . SubprocessError ) as e :
raise QemuError ( " Could not create hdd disk image {} " . format ( e ) )
else :
hdd_disk = self . _hdd_disk_image
2015-08-03 08:02:02 +03:00
options . extend ( [ " -drive " , ' file= {} ,if= {} ,index=3,media=disk ' . format ( hdd_disk , self . hdd_disk_interface ) ] )
2015-03-10 19:50:30 +02:00
2015-02-19 12:33:25 +02:00
return options
2015-08-03 08:02:02 +03:00
def _cdrom_option ( self ) :
options = [ ]
if self . _cdrom_image :
if not os . path . isfile ( self . _cdrom_image ) or not os . path . exists ( self . _cdrom_image ) :
if os . path . islink ( self . _cdrom_image ) :
raise QemuError ( " cdrom image ' {} ' linked to ' {} ' is not accessible " . format ( self . _cdrom_image , os . path . realpath ( self . _cdrom_image ) ) )
else :
raise QemuError ( " cdrom image ' {} ' is not accessible " . format ( self . _cdrom_image ) )
if self . _hdc_disk_image :
raise QemuError ( " You cannot use a disk image on hdc disk and a CDROM image at the same time " )
options . extend ( [ " -cdrom " , self . _cdrom_image ] )
return options
2015-02-19 12:33:25 +02:00
def _linux_boot_options ( self ) :
options = [ ]
if self . _initrd :
if not os . path . isfile ( self . _initrd ) or not os . path . exists ( self . _initrd ) :
if os . path . islink ( self . _initrd ) :
raise QemuError ( " initrd file ' {} ' linked to ' {} ' is not accessible " . format ( self . _initrd , os . path . realpath ( self . _initrd ) ) )
else :
raise QemuError ( " initrd file ' {} ' is not accessible " . format ( self . _initrd ) )
options . extend ( [ " -initrd " , self . _initrd ] )
if self . _kernel_image :
if not os . path . isfile ( self . _kernel_image ) or not os . path . exists ( self . _kernel_image ) :
if os . path . islink ( self . _kernel_image ) :
raise QemuError ( " kernel image ' {} ' linked to ' {} ' is not accessible " . format ( self . _kernel_image , os . path . realpath ( self . _kernel_image ) ) )
else :
raise QemuError ( " kernel image ' {} ' is not accessible " . format ( self . _kernel_image ) )
options . extend ( [ " -kernel " , self . _kernel_image ] )
if self . _kernel_command_line :
options . extend ( [ " -append " , self . _kernel_command_line ] )
return options
2015-10-04 16:00:47 +03:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def _network_options ( self ) :
network_options = [ ]
2015-05-06 23:59:01 +03:00
network_options . extend ( [ " -net " , " none " ] ) # we do not want any user networking back-end if no adapter is connected.
2015-10-04 15:41:39 +03:00
patched_qemu = False
if self . _legacy_networking :
version = yield from self . manager . get_qemu_version ( self . qemu_path )
if version and parse_version ( version ) < parse_version ( " 1.1.0 " ) :
# this is a patched Qemu if version is below 1.1.0
patched_qemu = True
2015-05-06 23:59:01 +03:00
for adapter_number , adapter in enumerate ( self . _ethernet_adapters ) :
2015-06-03 23:52:49 +03:00
mac = " %s %02x " % ( self . _mac_address [ : - 2 ] , ( int ( self . _mac_address [ - 2 : ] ) + adapter_number ) % 255 )
2015-02-19 12:33:25 +02:00
nio = adapter . get_nio ( 0 )
2015-05-06 23:59:01 +03:00
if self . _legacy_networking :
# legacy QEMU networking syntax (-net)
if nio :
network_options . extend ( [ " -net " , " nic,vlan= {} ,macaddr= {} ,model= {} " . format ( adapter_number , mac , self . _adapter_type ) ] )
if isinstance ( nio , NIOUDP ) :
2015-10-04 15:41:39 +03:00
if patched_qemu :
# use patched Qemu syntax
network_options . extend ( [ " -net " , " udp,vlan= {} ,name=gns3- {} ,sport= {} ,dport= {} ,daddr= {} " . format ( adapter_number ,
adapter_number ,
nio . lport ,
nio . rport ,
nio . rhost ) ] )
else :
# use UDP tunnel support added in Qemu 1.1.0
network_options . extend ( [ " -net " , " socket,vlan= {} ,name=gns3- {} ,udp= {} : {} ,localaddr= {} : {} " . format ( adapter_number ,
adapter_number ,
nio . rhost ,
nio . rport ,
self . _host ,
nio . lport ) ] )
2015-05-06 23:59:01 +03:00
elif isinstance ( nio , NIOTAP ) :
network_options . extend ( [ " -net " , " tap,name=gns3- {} ,ifname= {} " . format ( adapter_number , nio . tap_device ) ] )
elif isinstance ( nio , NIONAT ) :
network_options . extend ( [ " -net " , " user,vlan= {} ,name=gns3- {} " . format ( adapter_number , adapter_number ) ] )
else :
network_options . extend ( [ " -net " , " nic,vlan= {} ,macaddr= {} ,model= {} " . format ( adapter_number , mac , self . _adapter_type ) ] )
else :
# newer QEMU networking syntax
if nio :
network_options . extend ( [ " -device " , " {} ,mac= {} ,netdev=gns3- {} " . format ( self . _adapter_type , mac , adapter_number ) ] )
if isinstance ( nio , NIOUDP ) :
2015-04-27 23:38:15 +03:00
network_options . extend ( [ " -netdev " , " socket,id=gns3- {} ,udp= {} : {} ,localaddr= {} : {} " . format ( adapter_number ,
nio . rhost ,
nio . rport ,
self . _host ,
nio . lport ) ] )
2015-05-06 23:59:01 +03:00
elif isinstance ( nio , NIOTAP ) :
2015-08-15 07:15:01 +03:00
network_options . extend ( [ " -netdev " , " tap,id=gns3- {} ,ifname= {} ,script=no,downscript=no " . format ( adapter_number , nio . tap_device ) ] )
2015-05-06 23:59:01 +03:00
elif isinstance ( nio , NIONAT ) :
network_options . extend ( [ " -netdev " , " user,id=gns3- {} " . format ( adapter_number ) ] )
2015-03-03 04:59:44 +02:00
else :
2015-05-06 23:59:01 +03:00
network_options . extend ( [ " -device " , " {} ,mac= {} " . format ( self . _adapter_type , mac ) ] )
2015-02-19 12:33:25 +02:00
return network_options
2015-02-23 21:21:00 +02:00
def _graphic ( self ) :
"""
2015-04-08 20:17:34 +03:00
Adds the correct graphic options depending of the OS
2015-02-23 21:21:00 +02:00
"""
2015-04-08 20:17:34 +03:00
2015-02-23 21:21:00 +02:00
if sys . platform . startswith ( " win " ) :
return [ ]
if len ( os . environ . get ( " DISPLAY " , " " ) ) > 0 :
return [ ]
return [ " -nographic " ]
2015-08-07 17:49:45 +03:00
def _run_with_kvm ( self , qemu_path , options ) :
"""
Check if we could run qemu with KVM
: param qemu_path : Path to qemu
: param options : String of qemu user options
: returns : Boolean True if we need to enable KVM
"""
if sys . platform . startswith ( " linux " ) and self . manager . config . get_section_config ( " Qemu " ) . getboolean ( " enable_kvm " , True ) \
and " -no-kvm " not in options :
# Turn OFF kvm for non x86 architectures
if os . path . basename ( qemu_path ) not in [ " qemu-system-x86_64 " , " qemu-system-i386 " , " qemu-kvm " ] :
return False
if not os . path . exists ( " /dev/kvm " ) :
2015-08-26 18:17:40 +03:00
raise QemuError ( " KVM acceleration cannot be used (/dev/kvm doesn ' t exist). You can turn off KVM support in the gns3_server.conf by adding enable_kvm = false to the [Qemu] section. " )
2015-08-07 17:49:45 +03:00
return True
return False
2015-02-19 17:46:57 +02:00
@asyncio.coroutine
2015-02-19 12:33:25 +02:00
def _build_command ( self ) :
"""
Command to start the QEMU process .
( to be passed to subprocess . Popen ( ) )
"""
2015-02-19 17:46:57 +02:00
command = [ self . qemu_path ]
2015-02-19 12:33:25 +02:00
command . extend ( [ " -name " , self . _name ] )
2015-08-07 18:08:10 +03:00
command . extend ( [ " -m " , " {} M " . format ( self . _ram ) ] )
2015-08-06 02:17:55 +03:00
command . extend ( [ " -smp " , " cpus= {} " . format ( self . _cpus ) ] )
2015-08-07 17:49:45 +03:00
if self . _run_with_kvm ( self . qemu_path , self . _options ) :
2015-06-08 23:51:06 +03:00
command . extend ( [ " -enable-kvm " ] )
2015-08-03 08:02:02 +03:00
command . extend ( [ " -boot " , " order= {} " . format ( self . _boot_priority ) ] )
cdrom_option = self . _cdrom_option ( )
command . extend ( cdrom_option )
2015-10-04 16:00:47 +03:00
command . extend ( ( yield from self . _disk_options ( ) ) )
2015-02-19 12:33:25 +02:00
command . extend ( self . _linux_boot_options ( ) )
2015-06-25 04:09:17 +03:00
if self . _console_type == " telnet " :
command . extend ( self . _serial_options ( ) )
elif self . _console_type == " vnc " :
command . extend ( self . _vnc_options ( ) )
else :
raise QemuError ( " Console type {} is unknown " . format ( self . _console_type ) )
2015-02-19 12:33:25 +02:00
command . extend ( self . _monitor_options ( ) )
2015-10-07 10:01:17 +03:00
command . extend ( ( yield from self . _network_options ( ) ) )
2015-08-27 19:46:02 +03:00
command . extend ( self . _graphic ( ) )
2015-02-19 12:33:25 +02:00
additional_options = self . _options . strip ( )
if additional_options :
2015-03-31 23:14:08 +03:00
try :
command . extend ( shlex . split ( additional_options ) )
except ValueError as e :
2015-06-25 04:09:17 +03:00
raise QemuError ( " Invalid additional options: {} error {} " . format ( additional_options , e ) )
2015-02-19 12:33:25 +02:00
return command
2015-02-19 20:43:45 +02:00
def __json__ ( self ) :
2015-02-19 21:22:30 +02:00
answer = {
2015-02-19 20:43:45 +02:00
" project_id " : self . project . id ,
2015-06-25 10:52:10 +03:00
" vm_id " : self . id ,
" vm_directory " : self . working_dir
2015-02-19 20:43:45 +02:00
}
2015-03-10 19:50:30 +02:00
# Qemu has a long list of options. The JSON schema is the single source of information
2015-02-19 21:22:30 +02:00
for field in QEMU_OBJECT_SCHEMA [ " required " ] :
if field not in answer :
2015-06-17 18:11:25 +03:00
try :
answer [ field ] = getattr ( self , field )
except AttributeError :
pass
2015-04-14 19:46:55 +03:00
answer [ " hda_disk_image " ] = self . manager . get_relative_image_path ( self . _hda_disk_image )
2015-06-17 18:11:25 +03:00
answer [ " hda_disk_image_md5sum " ] = md5sum ( self . _hda_disk_image )
2015-04-14 19:46:55 +03:00
answer [ " hdb_disk_image " ] = self . manager . get_relative_image_path ( self . _hdb_disk_image )
2015-06-17 18:11:25 +03:00
answer [ " hdb_disk_image_md5sum " ] = md5sum ( self . _hdb_disk_image )
2015-04-14 19:46:55 +03:00
answer [ " hdc_disk_image " ] = self . manager . get_relative_image_path ( self . _hdc_disk_image )
2015-06-17 18:11:25 +03:00
answer [ " hdc_disk_image_md5sum " ] = md5sum ( self . _hdc_disk_image )
2015-04-14 19:46:55 +03:00
answer [ " hdd_disk_image " ] = self . manager . get_relative_image_path ( self . _hdd_disk_image )
2015-06-17 18:11:25 +03:00
answer [ " hdd_disk_image_md5sum " ] = md5sum ( self . _hdd_disk_image )
2015-08-03 08:02:02 +03:00
answer [ " cdrom_image " ] = self . manager . get_relative_image_path ( self . _cdrom_image )
answer [ " cdrom_image_md5sum " ] = md5sum ( self . _cdrom_image )
2015-04-14 19:46:55 +03:00
answer [ " initrd " ] = self . manager . get_relative_image_path ( self . _initrd )
2015-06-17 18:11:25 +03:00
answer [ " initrd_md5sum " ] = md5sum ( self . _initrd )
2015-04-14 19:46:55 +03:00
answer [ " kernel_image " ] = self . manager . get_relative_image_path ( self . _kernel_image )
2015-06-17 18:11:25 +03:00
answer [ " kernel_image_md5sum " ] = md5sum ( self . _kernel_image )
2015-03-11 23:04:11 +02:00
2015-02-19 21:22:30 +02:00
return answer