def import_disk(self): path = self.config.get("source") database = storage.Sqlite(self.opened_database, self.app, True) if self.config.get("thumbnails") == "True": if not os.path.exists(database.database + "_thumbs"): os.mkdir(database.database + "_thumbs") """Importamos un nuevo disco""" if not utils.is_mounted(path): self.__path = path self.__progress = self.app.progress_bar utils.mount(path, self._mount_callback) disk = self.reader.read(path, self.app.progress_bar) if disk: """ ahora insertamos los datos en la base de datos. """ disk = database.insert_disk(disk) if self.config.get("eject") == "True": utils.eject(path) self.app.set_guardar_state(True) # self.load_data() # self.treeDisk.select(disk) # self.show_files_from_selected() database.close() return disk
def ebsmount_add(devname, mountdir): """ebs device attached""" matching_devices = [] for device in udevdb.query(): if device.name.startswith(basename(devname)): matching_devices.append(device) for device in matching_devices: devpath = join('/dev', device.name) mountpath = join(mountdir, device.env.get('ID_FS_UUID', devpath[-1])[:6]) mountoptions = ",".join(config.mountoptions.split()) filesystem = device.env.get('ID_FS_TYPE', None) if not filesystem: log(devname, "could not identify filesystem: %s" % devpath) continue if not filesystem in config.filesystems.split(): log(devname, "filesystem (%s) not supported: %s" % (filesystem,devpath)) continue if is_mounted(devpath): log(devname, "already mounted: %s" % devpath) continue mount(devpath, mountpath, mountoptions) if exists(config.postmountscript): log(devname, "Executing: %s -m %s -d %s" % (config.postmountscript, mountpath, devname)) res = system("exec '%s' -m %s -d %s" % (config.postmountscript, mountpath, devname)) else: log(devname, "Script does not exist at %s" % config.postmountscript)
def ebsmount_add(devname, mountdir): """ebs device attached""" matching_devices = [] for device in udevdb.query(): if device.name.startswith(basename(devname)): matching_devices.append(device) for device in matching_devices: devpath = join('/dev', device.name) mountpath = join(mountdir, device.env.get('ID_FS_UUID', devpath[-1])[:4]) mountoptions = ",".join(config.mountoptions.split()) scriptpath = join(mountpath, ".ebsmount") filesystem = device.env.get('ID_FS_TYPE', None) if not filesystem: log(devname, "could not identify filesystem: %s" % devpath) continue if not filesystem in config.filesystems.split(): log(devname, "filesystem (%s) not supported: %s" % (filesystem,devpath)) continue if is_mounted(devpath): log(devname, "already mounted: %s" % devpath) continue mount(devpath, mountpath, mountoptions) log(devname, "mounted %s %s (%s)" % (devpath, mountpath, mountoptions)) if exists(scriptpath): cmd = "run-parts --verbose --exit-on-error %s" % scriptpath cmd += " 2>&1 | tee -a %s" % config.logfile system(cmd)
def encrypt_partition(device, passphrase): if is_mounted(device): unmount_partition(device) # Cannot use echo to pass the passphrase to cryptsetup because that adds a carriadge return shell_exec( "printf \"%s\" | cryptsetup luksFormat --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-random %s" % (passphrase, device)) return connect_block_device(device, passphrase)
def ebsmount_add(devname, mountdir): """ebs device attached""" matching_devices = [] for device in udevdb.query(): if device.name.startswith(basename(devname)): matching_devices.append(device) for device in matching_devices: devpath = join("/dev", device.name) mountpath = join(mountdir, device.env.get("ID_FS_UUID", devpath[-1])[:6]) mountoptions = ",".join(config.mountoptions.split()) hookspath = join(mountpath, ".ebsmount") filesystem = device.env.get("ID_FS_TYPE", None) if not filesystem: log(devname, "could not identify filesystem: %s" % devpath) continue if not filesystem in config.filesystems.split(): log(devname, "filesystem (%s) not supported: %s" % (filesystem, devpath)) continue if is_mounted(devpath): log(devname, "already mounted: %s" % devpath) continue mount(devpath, mountpath, mountoptions) log(devname, "mounted %s %s (%s)" % (devpath, mountpath, mountoptions)) if exists(hookspath): hooks = os.listdir(hookspath) hooks.sort() if hooks and not config.runhooks.lower() == "true": log(devname, "skipping hooks: RUNHOOKS not set to True") continue for file in hooks: fpath = join(hookspath, file) if not os.access(fpath, os.X_OK): log(devname, "skipping hook: '%s', not executable" % file) continue if not os.stat(fpath).st_uid == 0 or not os.stat(fpath).st_gid == 0: log(devname, "skipping hook: '%s', not owned root:root" % file) continue log(devname, "executing hook: %s" % file) os.environ["HOME"] = pwd.getpwuid(os.getuid()).pw_dir os.environ["MOUNTPOINT"] = mountpath system("/bin/bash --login -c '%s' 2>&1 | tee -a %s" % (fpath, config.logfile))
def ebsmount_add(devname, mountdir): """ebs device attached""" matching_devices = [] for device in udevdb.query(): if device.name.startswith(basename(devname)): matching_devices.append(device) for device in matching_devices: devpath = join('/dev', device.name) mountpath = join(mountdir, device.env.get('ID_FS_UUID', devpath[-1])[:6]) mountoptions = ",".join(config.mountoptions.split()) hookspath = join(mountpath, ".ebsmount") filesystem = device.env.get('ID_FS_TYPE', None) if not filesystem: log(devname, "could not identify filesystem: %s" % devpath) continue if not filesystem in config.filesystems.split(): log(devname, "filesystem (%s) not supported: %s" % (filesystem,devpath)) continue if is_mounted(devpath): log(devname, "already mounted: %s" % devpath) continue mount(devpath, mountpath, mountoptions) log(devname, "mounted %s %s (%s)" % (devpath, mountpath, mountoptions)) if exists(hookspath): hooks = os.listdir(hookspath) hooks.sort() if hooks and not config.runhooks.lower() == "true": log(devname, "skipping hooks: RUNHOOKS not set to True") continue for file in hooks: fpath = join(hookspath, file) if not os.access(fpath, os.X_OK): log(devname, "skipping hook: '%s', not executable" % file) continue if not os.stat(fpath).st_uid == 0 or not os.stat(fpath).st_gid == 0: log(devname, "skipping hook: '%s', not owned root:root" % file) continue log(devname, "executing hook: %s" % file) os.environ['HOME'] = pwd.getpwuid(os.getuid()).pw_dir os.environ['MOUNTPOINT'] = mountpath system("/bin/bash --login -c '%s' 2>&1 | tee -a %s" % (fpath, config.logfile))
def ebsmount_remove(devname, mountdir): """ebs device detached""" mounted = False for d in os.listdir(mountdir): path = join(mountdir, d) if is_mounted(path): mounted = True continue os.rmdir(path) if not mounted: os.rmdir(mountdir)
def is_mounted(self): return utils.is_mounted(self.real_path)
if opt == '-h': usage() if opt == '--format': filesystem = val if not len(args) == 1: usage() devname = args[0] if not os.path.exists(devname): fatal("%s does not exist" % devname) if not _expected_devpath(devname, config.devpaths.split()): fatal("devpath not of expected structure, or failed lookup") if filesystem: if is_mounted(devname): fatal("%s is mounted" % devname) if not filesystem in config.filesystems.split(): fatal("%s is not supported in %s" % (filesystem, config.CONF_FILE)) executil.system("mkfs." + filesystem, "-q", devname) ebsmount.ebsmount_add(devname, config.mountdir) if __name__ == "__main__": main()
def run(self): # Loop on index: need that when queueing a changed partition object steps = 10 nr_partitions = len(self.my_partitions) total_steps = nr_partitions * steps for i in range(nr_partitions): partition = self.my_partitions[i] is_swap = partition['fs_type'] == 'swap' # Create the backup directory backup_dir = join(self.backup_dir, "luks_bak/%s" % basename(partition['device'])) self.log.write("Backup directory: %s" % backup_dir, 'endecrypt', 'info') os.makedirs(backup_dir, exist_ok=True) # Rsync partition content to backup medium if (is_mounted(partition['mount_point']) and isdir(backup_dir)) or is_swap: rsync_code = 0 if not is_swap: rsync_code = self.backup_partition( partition['mount_point'], backup_dir) if rsync_code > 0: msg = _( "Could not create a backup on {backup_dir} (rsync code: {rsync_code}).\n" "Please, select another backup medium before you try again." .format(backup_dir=backup_dir, rsync_code=rsync_code)) self.queue.put([1, rsync_code, None, None, msg]) return False else: step = (i + 1) * 1 self.queue.put([ 1 / (total_steps / step), rsync_code, None, None, None ]) if self.encrypt: # Encrypt self.log.write( "Start encryption of %s" % partition['device'], 'endecrypt', 'info') partition['device'] = encrypt_partition( partition['device'], self.passphrase) partition['encrypted'] = True step = (i + 1) * 2 self.queue.put( [1 / (total_steps / step), 0, None, None, None]) else: # Decrypt fs_type = self.udisks2.get_filesystem( partition['device']) partition_path = partition['device'].replace( '/mapper', '') self.log.write("Unmount %s" % partition['device'], 'endecrypt', 'info') self.udisks2.unmount_device(partition['device']) partition['fs_type'] = fs_type partition['device'] = partition_path partition['encrypted'] = False self.log.write( "Save fs_type %s and partition_path %s of encrypted partition" % (partition['fs_type'], partition['device']), 'endecrypt', 'info') step = (i + 1) * 3 self.queue.put( [1 / (total_steps / step), 0, None, None, None]) #Format self.log.write("Start formatting %s" % partition['device'], 'endecrypt', 'info') if self.format_partition(partition): partition['uuid'] = get_uuid(partition['device']) step = (i + 1) * 4 self.queue.put( [1 / (total_steps / step), 0, None, None, None]) else: msg = _( "Could not format the device {device}.\n" "You need to manually format the device and restore your data from: {backup_dir}" .format(device=partition['device'], backup_dir=backup_dir)) self.queue.put([1, 105, None, None, msg]) return False # Mount the encrypted/decrypted partition to the old mount point mount = '' if not is_swap: self.log.write( "Mount (for restoring backup) %s to %s" % (partition['device'], partition['mount_point']), 'endecrypt', 'info') device, mount, filesystem = mount_partition( partition['device'], partition['mount_point'], self.passphrase, partition['fs_type']) step = (i + 1) * 5 self.queue.put( [1 / (total_steps / step), 0, None, None, None]) restore_failed = False if mount: # Make sure the user owns the pen drive if partition['removable']: user = get_logged_user() if user: shell_exec("chown -R {0}:{0} {1}".format( user, mount)) step = (i + 1) * 6 self.queue.put([ 1 / (total_steps / step), 0, None, None, None ]) # Rsync backup to the encrytped/decrypted partition self.log.write( "Restore backup %s to %s" % (backup_dir, partition['mount_point']), 'endecrypt', 'info') rsync_code = self.backup_partition( backup_dir, partition['mount_point']) if rsync_code == 0: step = (i + 1) * 7 self.queue.put([ 1 / (total_steps / step), rsync_code, None, None, None ]) else: # Return rsync error code for no such file or directory: 2 rsync_code = 2 restore_failed = True else: if not is_swap: restore_failed = True if restore_failed: msg = _( "Could not restore the backup (rsync code: {rsync_code}).\n" "You need to manually restore your data from: {backup_dir}" .format(rsync_code=rsync_code, backup_dir=backup_dir)) self.queue.put([1, rsync_code, None, None, msg]) return False if exists(backup_dir): # Remove backup data self.log.write("Remove backup data: %s" % backup_dir, 'endecrypt', 'info') rmtree(backup_dir) luks_bak = join(self.backup_dir, "luks_bak") if not os.listdir(luks_bak): os.rmdir(luks_bak) step = (i + 1) * 8 self.queue.put( [1 / (total_steps / step), 0, None, None, None]) # Partition finished: pass the partition object back to the caller step = (i + 1) * 9 self.queue.put([1 / (total_steps / step), 0, i, partition, None]) # Done self.queue.put([1, 0, None, None, None])
def clear_partition(device): if is_mounted(device): unmount_partition(device) shell_exec( "openssl enc -aes-256-ctr -pass pass:\"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)\" -nosalt < /dev/zero > %s 2>/dev/null" % device)
if opt == '-h': usage() if opt == '--format': filesystem = val if not len(args) == 1: usage() devname = args[0] if not os.path.exists(devname): fatal("%s does not exist" % devname) if not _expected_devpath(devname, config.devpaths.split()): fatal("devpath not of expected structure, or failed lookup") if filesystem: if is_mounted(devname): fatal("%s is mounted" % devname) if not filesystem in config.filesystems.split(): fatal("%s is not supported in %s" % (filesystem, config.CONF_FILE)) executil.system("mkfs." + filesystem, "-q", devname) ebsmount.ebsmount_add(devname, config.mountdir) if __name__=="__main__": main()