2016-12-26 17:53:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-11-06 11:18:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Name: qemu.py
|
|
|
|
# Purpose: Module to boot ISO and USB disks using QEMU.
|
|
|
|
# Depends: QEMU must be installed under Linux for availing this feature. For windows, QEMU package is shipped
|
|
|
|
# along with executable file
|
|
|
|
# Authors: Sundar
|
|
|
|
# Licence: This file is a part of multibootusb package. You can redistribute it or modify
|
|
|
|
# under the terms of GNU General Public License, v.2 or above
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import platform
|
|
|
|
from .admin import adminCmd
|
|
|
|
from PyQt5 import QtWidgets
|
2017-04-13 07:39:25 +00:00
|
|
|
from .gui.ui_multibootusb import Ui_MainWindow
|
2016-11-06 11:18:53 +00:00
|
|
|
from .gen import *
|
2017-04-13 07:39:25 +00:00
|
|
|
from . import config
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
|
2017-04-13 07:39:25 +00:00
|
|
|
class Qemu(QtWidgets.QMainWindow, Ui_MainWindow):
|
2016-11-06 11:18:53 +00:00
|
|
|
"""
|
|
|
|
ISO and USB booting using QEMU.
|
|
|
|
"""
|
|
|
|
|
2017-04-13 07:39:25 +00:00
|
|
|
def __init__(self):
|
|
|
|
QtWidgets.QMainWindow.__init__(self)
|
|
|
|
self.ui = Ui_MainWindow()
|
|
|
|
self.ui.setupUi(self)
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
def on_Qemu_Boot_iso_Click(self):
|
|
|
|
"""
|
|
|
|
Main function to boot a selected ISO.
|
|
|
|
:return:
|
|
|
|
"""
|
2017-04-13 07:39:25 +00:00
|
|
|
# if not self.ui.lineEdit_2.text():
|
|
|
|
if not config.image_path:
|
|
|
|
QtWidgets.QMessageBox.information(self, 'No ISO...', 'No ISO selected.\n\nPlease choose an ISO first.')
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
|
|
|
qemu = self.check_qemu_exist()
|
2017-04-13 07:39:25 +00:00
|
|
|
qemu_iso_link = config.image_path
|
|
|
|
if not qemu:
|
|
|
|
log("ERROR: ISO Boot: qemu not found!")
|
|
|
|
QtWidgets.QMessageBox.information(self, 'No QEMU...', 'Please install qemu to use this feature.')
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
|
|
|
ram = self.qemu_iso_ram()
|
2017-04-13 07:39:25 +00:00
|
|
|
if ram:
|
|
|
|
ram = " -m " + ram
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
2017-04-13 07:39:25 +00:00
|
|
|
ram = ""
|
2016-11-06 11:18:53 +00:00
|
|
|
|
2017-04-13 07:39:25 +00:00
|
|
|
cmd = qemu + ram + ' -boot d' + ' -cdrom ' + str(qemu_iso_link)
|
|
|
|
try:
|
|
|
|
log("Executing ==> " + cmd)
|
|
|
|
subprocess.Popen(cmd, shell=True)
|
|
|
|
except:
|
|
|
|
QtWidgets.QMessageBox.information(self, 'Error...', 'Error booting ISO\n'
|
|
|
|
'Unable to start QEMU.')
|
2016-11-06 11:18:53 +00:00
|
|
|
|
2017-04-13 07:39:25 +00:00
|
|
|
|
|
|
|
def on_Qemu_Boot_usb_Click(self):
|
2016-11-06 11:18:53 +00:00
|
|
|
"""
|
|
|
|
Main function to boot a selected USB disk.
|
|
|
|
:param usb_disk: Path to usb disk.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
qemu = self.check_qemu_exist()
|
|
|
|
|
2017-04-13 07:39:25 +00:00
|
|
|
if not config.usb_disk:
|
|
|
|
QtWidgets.QMessageBox.information(self, 'No disk...', 'No USB disk selected.\n\nPlease choose a disk first.')
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
2017-04-13 07:39:25 +00:00
|
|
|
qemu = self.check_qemu_exist()
|
2017-04-23 09:21:42 +00:00
|
|
|
if platform.system() == 'Linux' and config.usb_disk[-1].isdigit() is True:
|
|
|
|
qemu_usb_disk = config.usb_disk[:-1]
|
|
|
|
else:
|
|
|
|
qemu_usb_disk = config.usb_disk
|
2017-04-13 07:39:25 +00:00
|
|
|
|
|
|
|
if qemu is None:
|
|
|
|
log("ERROR: USB Boot: qemu not found!")
|
|
|
|
QtWidgets.QMessageBox.information(self, 'No QEMU...', 'Please install qemu to use this feature.')
|
2016-11-06 11:18:53 +00:00
|
|
|
else:
|
2017-04-13 07:39:25 +00:00
|
|
|
ram = self.qemu_usb_ram()
|
|
|
|
if ram:
|
|
|
|
ram = " -m " + ram
|
|
|
|
else:
|
|
|
|
ram = ""
|
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
if platform.system() == "Windows":
|
2017-04-13 07:39:25 +00:00
|
|
|
disk_number = self.get_physical_disk_number(qemu_usb_disk)
|
2016-11-06 11:18:53 +00:00
|
|
|
parent_dir = os.getcwd()
|
|
|
|
os.chdir(resource_path(os.path.join("data", "tools", "qemu")))
|
2017-04-23 10:39:57 +00:00
|
|
|
cmd = quote(qemu) + ' -L . -boot c' + ram + ' -hda //./PhysicalDrive' + disk_number
|
2017-04-13 07:39:25 +00:00
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
try:
|
2017-04-13 07:39:25 +00:00
|
|
|
log("Executing ==> " + cmd)
|
|
|
|
subprocess.Popen(cmd, shell=True)
|
2016-11-06 11:18:53 +00:00
|
|
|
except:
|
2017-04-13 07:39:25 +00:00
|
|
|
QtWidgets.QMessageBox.information(self, 'Error...', 'Error booting USB\nUnable to start QEMU.')
|
2016-11-06 11:18:53 +00:00
|
|
|
os.chdir(parent_dir)
|
2017-04-13 07:39:25 +00:00
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
elif platform.system() == "Linux":
|
2017-04-13 07:39:25 +00:00
|
|
|
cmd = qemu + ' -hda ' + qemu_usb_disk + ram + ' -vga std'
|
2016-11-06 11:18:53 +00:00
|
|
|
try:
|
2017-04-13 07:39:25 +00:00
|
|
|
log('Executing ==> ' + cmd)
|
2016-11-06 11:18:53 +00:00
|
|
|
# adminCmd([qemu, '-hda', usb_disk[:-1], '-m', ram, '-vga std'], gui=True)
|
2017-04-13 07:39:25 +00:00
|
|
|
subprocess.Popen(cmd, shell=True)
|
2016-11-06 11:18:53 +00:00
|
|
|
# adminCmd(qemu_cmd, gui=True)
|
|
|
|
except:
|
|
|
|
QtWidgets.QMessageBox.information(self, 'Error...', 'Error booting USB\n\nUnable to start QEMU.')
|
|
|
|
|
|
|
|
def qemu_iso_ram(self):
|
|
|
|
"""
|
|
|
|
Choose a ram size for ISO booting.
|
|
|
|
:return: Ram size as string.
|
|
|
|
"""
|
2017-04-13 07:39:25 +00:00
|
|
|
selected_ram = self.ui.combo_iso_boot_ram.currentText()
|
|
|
|
log("QEMU: ISO RAM = " + selected_ram)
|
|
|
|
|
|
|
|
if selected_ram == "Default":
|
2016-11-06 11:18:53 +00:00
|
|
|
return None
|
2017-04-13 07:39:25 +00:00
|
|
|
else:
|
|
|
|
return selected_ram
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
def qemu_usb_ram(self):
|
|
|
|
"""
|
|
|
|
Choose a ram size for USB booting.
|
|
|
|
:return: Ram size as string.
|
|
|
|
"""
|
2017-04-13 07:39:25 +00:00
|
|
|
selected_ram = self.ui.combo_usb_boot_ram.currentText()
|
|
|
|
log("QEMU: USB RAM = " + selected_ram)
|
|
|
|
|
|
|
|
if selected_ram == "Default":
|
2016-11-06 11:18:53 +00:00
|
|
|
return None
|
2017-04-13 07:39:25 +00:00
|
|
|
else:
|
|
|
|
return selected_ram
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
def check_qemu_exist(self):
|
|
|
|
"""
|
|
|
|
Check if QEMU is available on host system.
|
|
|
|
:return: path to QEMU program or None otherwise.
|
|
|
|
"""
|
|
|
|
if platform.system() == "Linux":
|
|
|
|
if subprocess.call('which qemu-system-x86_64', shell=True) == 0:
|
|
|
|
qemu = "qemu-system-x86_64"
|
|
|
|
elif subprocess.call('which qemu', shell=True) == 0:
|
|
|
|
qemu = "qemu"
|
|
|
|
else:
|
2017-04-13 07:39:25 +00:00
|
|
|
qemu = ""
|
2016-11-06 11:18:53 +00:00
|
|
|
|
|
|
|
elif platform.system() == "Windows":
|
2017-04-13 07:39:25 +00:00
|
|
|
qemu = resource_path(os.path.join("data", "tools", "qemu", "qemu-system-x86_64.exe"))
|
|
|
|
log(qemu)
|
|
|
|
|
|
|
|
if qemu:
|
|
|
|
log("QEMU: using " + qemu)
|
|
|
|
else:
|
|
|
|
log("QEMU: ERROR: not found!")
|
|
|
|
|
|
|
|
return qemu
|
2016-11-06 11:18:53 +00:00
|
|
|
|
2016-12-03 10:06:47 +00:00
|
|
|
|
2016-11-06 11:18:53 +00:00
|
|
|
def get_physical_disk_number(self, usb_disk):
|
|
|
|
"""
|
|
|
|
Get the physical disk number as detected ny Windows.
|
|
|
|
:param usb_disk: USB disk (Like F:)
|
|
|
|
:return: Disk number.
|
|
|
|
"""
|
|
|
|
import wmi
|
|
|
|
c = wmi.WMI ()
|
|
|
|
for physical_disk in c.Win32_DiskDrive ():
|
|
|
|
for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):
|
|
|
|
for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):
|
|
|
|
if logical_disk.Caption == usb_disk:
|
|
|
|
"""
|
2016-12-26 17:53:21 +00:00
|
|
|
log physical_disk.Caption
|
|
|
|
log partition.Caption
|
|
|
|
log logical_disk.Caption
|
2016-11-06 11:18:53 +00:00
|
|
|
"""
|
2016-12-26 17:53:21 +00:00
|
|
|
log("Physical Device Number is " + partition.Caption[6:-14])
|
2016-11-06 11:18:53 +00:00
|
|
|
return str(partition.Caption[6:-14])
|
2017-04-13 07:39:25 +00:00
|
|
|
|