diff --git a/scripts/mbusb_gui.py b/scripts/mbusb_gui.py index 3fcbbeb..bbb1b22 100644 --- a/scripts/mbusb_gui.py +++ b/scripts/mbusb_gui.py @@ -160,16 +160,19 @@ class AppGui(qemu.Qemu, Imager, QtWidgets.QMainWindow, Ui_MainWindow): """ self.ui.installed_distros.clear() config.usb_disk = str(self.ui.combo_drives.currentText()) - config.imager_usb_disk = str(self.ui.combo_drives.currentText()) - if config.usb_disk: + # Get the GPT status of the disk and store it on a variable + try: + usb.gpt_device(config.usb_disk) + except Exception as e: + QtWidgets.QMessageBox.critical( + self, "The disk/partition is not usable.", str(e)) + self.ui.combo_drives.setCurrentIndex(0) + config.usb_disk = str(self.ui.combo_drives.currentText()) log("Selected device " + config.usb_disk) + config.imager_usb_disk = str(self.ui.combo_drives.currentText()) config.usb_details = usb.details(config.usb_disk) self.update_target_info() - - # Get the GPT status of the disk and store it on a variable - usb.gpt_device(config.usb_disk) - self.update_list_box(config.usb_disk) self.ui_update_persistence() else: @@ -199,7 +202,7 @@ class AppGui(qemu.Qemu, Imager, QtWidgets.QMainWindow, Ui_MainWindow): for device in detected_devices: if all(not device.startswith(d) for d in protected_drives): self.ui.combo_drives.addItem(str(device)) - self.ui.combo_drives.setCurrentIndex(0) + self.ui.combo_drives.setCurrentIndex(0) def update_list_box(self, usb_disk): """ diff --git a/scripts/osdriver.py b/scripts/osdriver.py index e1f9dc2..a19de75 100644 --- a/scripts/osdriver.py +++ b/scripts/osdriver.py @@ -245,6 +245,10 @@ class Windows(Base): def multibootusb_host_dir(self): return os.path.join(tempfile.gettempdir(), "multibootusb") + def gpt_device(self, dev_name): + partition, disk = wmi_get_drive_info(dev_name) + return partition.Type.startswith('GPT:') + class Linux(Base): def __init__(self): @@ -299,6 +303,30 @@ class Linux(Base): def multibootusb_host_dir(self): return os.path.join(os.path.expanduser('~'), ".multibootusb") + def gpt_device(self, dev_name): + disk_dev = dev_name.rstrip('0123456789') + try: + cmd = ['parted', disk_dev, '-s', 'print'] + with open(os.devnull) as devnull: + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, stdin=devnull) + _cmd_out, _err_out = p.communicate() + p.wait() + if p.returncode != 0: + lang = os.getenv('LANG') + encoding = lang.rsplit('.')[-1] if lang else 'utf-8' + raise RuntimeError(str(_err_out, encoding)) + except subprocess.CalledProcessError as e: + raise RuntimeError(str(e) + '\n\n' + stderr) + subprocess.check_call(['partprobe', disk_dev]) + if b'msdos' in _cmd_out: + return False + if b'gpt' in _cmd_out: + return True + raise RuntimeError("Disk '%s' is uninitialized and not usable." % + disk_dev) + + driverClass = { 'Windows' : Windows, 'Linux' : Linux, @@ -314,6 +342,7 @@ for func_name in [ 'dd_iso_image', 'find_mounted_partitions_on', 'multibootusb_host_dir', + 'gpt_device', ]: globals()[func_name] = getattr(osdriver, func_name) diff --git a/scripts/persistence.py b/scripts/persistence.py index dcbf392..8117c99 100644 --- a/scripts/persistence.py +++ b/scripts/persistence.py @@ -190,7 +190,8 @@ def detect_missing_tools(distro): try: with open(os.devnull) as devnull: for tool in [e2fsck_exe, resize2fs_exe]: - subprocess.Popen([tool], stdout=devnull, stderr=devnull) + p = subprocess.Popen([tool], stdout=devnull, stderr=devnull) + p.communicate() except FileNotFoundError: # Windows return "'%s.exe' is not installed or not available for use." % tool except OSError: # Linux diff --git a/scripts/usb.py b/scripts/usb.py index 0fa56ee..45efc27 100644 --- a/scripts/usb.py +++ b/scripts/usb.py @@ -397,26 +397,9 @@ def gpt_device(dev_name): :param dev_name: :return: True if GPT else False """ - if platform.system() == 'Windows': - partition, disk = gen.wmi_get_drive_info(dev_name) - is_gpt = partition.Type.startswith('GPT:') - gen.log('Device %s is a %s disk...' % - (dev_name, is_gpt and 'GPT' or 'MBR')) - config.usb_gpt = is_gpt - return is_gpt - if platform.system() == "Linux": - if gen.has_digit(dev_name): - _cmd_out = subprocess.check_output("parted " + dev_name[:-1] + " print", shell=True) - else: - _cmd_out = subprocess.check_output("parted " + dev_name + " print", shell=True) - if b'msdos' in _cmd_out: - config.usb_gpt = False - gen.log('Device ' + dev_name + ' is a MBR disk...') - return False - elif b'gpt' in _cmd_out: - config.usb_gpt = True - gen.log('Device ' + dev_name + ' is a GPT disk...') - return True + is_gpt = osdriver.gpt_device(dev_name) + config.usb_gpt = is_gpt + gen.log('Device %s is a %s disk.' % (dev_name, is_gpt and 'GPT' or 'MBR')) def unmount(usb_disk):