def hum_readable_list_devices(full_info=False): if full_info: cmd="lsblk" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) print("\n") cmd="blkid" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) print("\n") cmd="sudo fdisk -l | grep -v grep | grep /dev/sd" exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: print("[CMD] " + str(cmd)) print(stdout) devices_list = BlockDeviceHandler.list_connected_devices() for device in devices_list: device_info = BlockDeviceHandler.get_device_info_data(device) for key, value in device_info.items(): sep_len = 12 - len(key) sep = " "*sep_len print("\t{}:{}{}".format(key, sep, value))
def prepare_block_device(): if BlockDeviceHandler.is_any_device_avaible(): module_print("Block device exists") devices = BlockDeviceHandler.list_connected_devices() for dev in devices: premount_path = BlockDeviceHandler.premount_device(dev) format_device_based_on_config_file(dev, premount_path) BlockDeviceHandler.unmount_all_premounted_devices()
def do_search_get_edit(): print("Search devices...") device_list = BlockDeviceHandler.list_connected_devices() print("Get label and uuid...") for device in device_list: device_info_dict = BlockDeviceHandler.get_device_info_data(device) print(device_info_dict) print("Edit fstab file and create mount points...") edit_fstab_create_mount_point(device_info_dict)
def restore_diskconf_file(path, confname="diskconf.json"): json_path = str(path) + "/" + str(confname) save_path = "/tmp/" + str(confname) cmd = "sudo cp {} {}".format(save_path, json_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr) cmd = "sudo rm -f {}".format(save_path) exitcode, stdout, stderr = LocalMachine.run_command(cmd) BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr)
def safe_format_disk_check_force_mode(json_data, dev): dev_data_modified = False # disk is not formatted dev_data = BlockDeviceHandler.get_device_info_data(dev) if json_data['label'] != dev_data['label']: dev_data_modified = True if json_data['format'] != dev_data['filesystem']: dev_data_modified = True if str(json_data['is_formatted']).lower() == "false": if str(json_data['force']).lower( ) == "true" and dev_data_modified is False: module_print( "[i] [format] Block device paramaters not changed but force mode is ON" ) return True elif dev_data_modified is True: module_print( "[i] [format] Requested block device parameter(s) changed - format" ) return True else: module_print( "[i] [Skip format] Blockdevice format not needed - label and system not changed" ) return False else: module_print("[i] [is_formatted:True] Blockdevice already formatted.") return False
def get_storage_structure_folders(set_extarnal_storage, external_storage_label): global default_storage_root storage_root_path, path_list = get_storage_root_and_base_path_list( set_extarnal_storage, external_storage_label) if set_extarnal_storage: if not BlockDeviceHandler.device_is_mounted(storage_root_path): return "External device is not available: " + str( storage_root_path) create_source_file_for_bash_scripts(path_list) text = "Storage structure folders:" text += "\n(default internal storage root: " + default_storage_root + ")" for path in path_list: text += "\n" + Colors.YELLOW + path + Colors.NC + "\tgroup: " + get_user_and_group( path)[1] cmd = "tree -L 2 " + str(path) exit_code, stdout, stderr = LocalMachine.run_command(cmd) if exit_code == 0: text += stdout console_out(text) return text
def format_device_based_on_config_file(dev, premount_path): module_print("Format device") diskconf_path = premount_path data = parse_config_file_from_disk(diskconf_path) if data is not None: if safe_format_disk_check_force_mode(data, dev): module_print("\tSave disk config file before formatting") save_diskconf_file(diskconf_path) module_print("\tUnmount device before formatting") BlockDeviceHandler.unmount_device(dev) module_print("\tFormat device") BlockDeviceHandler.format_ex4(dev, data['label']) module_print("\tMount formatted device") mount_point = BlockDeviceHandler.mount_device(dev) module_print("\tRestore config file to disk after formating") restore_diskconf_file(mount_point) module_print("\tSave back the the config file with the new state") write_state_config_file_from_disk(mount_point, data) else: module_print("\tDisk already formatted: {}:{}".format( dev, premount_path)) module_print("mount device: " + str(dev)) mount_point = BlockDeviceHandler.mount_device(dev)
def mount(): print("Mount all devices...") BlockDeviceHandler.mount_all_devices()
def pre_check(info="CKECK"): state, msg = BlockDeviceHandler.is_any_device_avaible() if not state: print("{} There are no connected devices: {}".format(info, msg)) sys.exit(444)
def main(): hum_readable_list_devices() print("\nFormat your disk to ext4 filesystem:") device = raw_input("Device (ex. /dev/sda1): ") label = raw_input("Wanted label (ex. what wou want - ex. drive): ") BlockDeviceHandler.format_ex4(device, label)