def unmount_volumes(disk_id): # all volumes on a disk have an index attached e.g. /dev/sdb1, /dev/sdb2 cmd = "fdisk -l | grep '%s[0-9][0-9]*' | awk '{print $1}'" % disk_id output, _, _ = run_cmd(cmd) # it may also happen that the disk does not have volumes # in which case the loop below won't do anything for volume in output.splitlines(): cmd = 'umount {}'.format(volume) _, error, return_code = run_cmd(cmd) if not return_code: debugger('volume {} successfully unmounted'.format(volume)) else: debugger('[ERROR] ' + error.strip('\n'))
def format_disk(disk_id): cmd = 'diskutil eraseDisk fat32 UNTITLED {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully erased and formatted'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def get_disk_ids(): cmd = "diskutil list | grep '/dev/'" output, error, return_code = run_cmd(cmd) if not return_code: return output.split() else: debugger('[ERROR] ' + error.strip('\n'))
def format_disk(disk_id): cmd = 'mkdosfs -I -F 32 -v {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully erased and formatted'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def unmount_disk(disk_id): cmd = 'diskutil unmountDisk {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully unmounted'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def unmount_disk(disk_id): cmd = 'diskutil unmountDisk {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully unmounted'.format(disk_id)) else: debugger('[ERROR: {}] {}'.format(cmd, error.strip('\n'))) raise disk_error(UNMOUNT_ERROR)
def is_installed(programs_list): cmd = 'which {}'.format(' '.join(programs_list)) output, error, return_code = run_cmd(cmd) if return_code: debugger('[ERROR] ' + error.strip('\n')) return True # if something goes wrong here, it shouldn't be catastrophic return len(output.split()) == len(programs_list)
def unmount_disk(disk_id): # to unmount an entire disk, we first need to unmount all it's volumes unmount_volumes(disk_id) # now we can safely unmount the disk cmd = 'umount {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('disk {} successfully unmounted'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def get_disk_ids(): cmd = "parted --list | grep 'Disk /dev/.*:' | awk '{print $2}'" output, error, return_code = run_cmd(cmd) disk_ids = [] for id in output.splitlines(): disk_ids.append(id[:-1]) if return_code: debugger('[ERROR] ' + error.strip('\n')) return disk_ids
def is_sufficient_space(required_mb): cmd = "df %s | grep -v 'Available' | awk '{print $4}'" % temp_path output, _, _ = run_cmd(cmd) try: free_space_mb = float(output.strip()) * 512 / BYTES_IN_MEGABYTE except: debugger('[ERROR] Failed parsing the line ' + output) return True debugger('Free space {0:.2f} MB in {1}'.format(free_space_mb, temp_path)) return free_space_mb > required_mb
def is_sufficient_space(path, required_mb): cmd = "df %s | grep -v 'Available' | awk '{print $4}'" % path output, _, _ = run_cmd(cmd) try: free_space_mb = float(output.strip()) * 512 / BYTES_IN_MEGABYTE except: debugger('[ERROR] Failed parsing the line ' + output) return True debugger('Free space {0:.2f} MB in {1}'.format(free_space_mb, path)) return free_space_mb > required_mb
def eject_disk(disk_id): ''' This method is used by the backendThread to ensure safe removal after burning finished successfully. ''' cmd = 'diskutil eject {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully ejected'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def eject_disk(disk_id): ''' This method is used by the backendThread to ensure safe removal after burning finished successfully. ''' cmd = 'eject {}'.format(disk_id) _, error, return_code = run_cmd(cmd) if not return_code: debugger('{} successfully ejected'.format(disk_id)) else: debugger('[ERROR] ' + error.strip('\n'))
def get_disk_sizes(): cmd = "fdisk -l | grep 'Disk /dev/'" output, error, return_code = run_cmd(cmd) disk_sizes = [] for line in sorted(output.splitlines()): size = line.split()[4] disk_sizes.append(float(size) / BYTES_IN_GIGABYTE) if return_code: debugger('[ERROR] ' + error.strip('\n')) return disk_sizes
def get_disk_names(): cmd = "parted --list | grep 'Model:'" output, error, return_code = run_cmd(cmd) disk_names = [] for name in output.splitlines(): disk_names.append(' '.join(name.split()[1:-1])) if return_code: debugger('[ERROR] ' + error.strip('\n')) # grab the first line of the output and the name is from the 4th word onwards return disk_names
def poll_burning_thread(thread): time.sleep(1) # wait for dd to start debugger('Polling burner for progress..') cmd = 'kill -INFO `pgrep ^dd`' # as long as the burning thread is running, send SIGINFO # to dd to trigger progress output while thread.is_alive(): _, error, return_code = run_cmd(cmd) if return_code: debugger('[ERROR] Sending signal to burning thread failed') return False time.sleep(0.3) return True
def get_disk_name_size(disk_id): cmd = "parted {} unit B print".format(disk_id) output, error, return_code = run_cmd(cmd) disk_name = '' disk_size = 0 if return_code: debugger('[ERROR] ' + error.strip('\n')) for line in output.splitlines(): if 'Model:' in line: disk_name = ' '.join(line.split()[1:]) if 'Disk {}:'.format(disk_id) in line: disk_size = float(line[:-1].split()[2]) / BYTES_IN_GIGABYTE if not disk_name or not disk_size: debugger('[ERROR] Parsing disk name and size failed') return disk_name, disk_size
def get_disk_name_size(disk_id): cmd = "diskutil info {}".format(disk_id) output, error, return_code = run_cmd(cmd) disk_name = '' disk_size = 0 if return_code: debugger('[ERROR] ' + error.strip('\n')) for line in output.splitlines(): if 'Device / Media Name:' in line: disk_name = ' '.join(line.split()[4:]) if 'Total Size:' in line: disk_size = float(line.split()[4][1:]) / BYTES_IN_GIGABYTE if not disk_name or not disk_size: debugger('[ERROR] Parsing disk name and size failed') return disk_name, disk_size