def test_no_root(self): parser = ImageParser() disk = parser.add_disk("...") v1 = Volume(disk) v1.mountpoint = '...' v1.info['lastmountpoint'] = '/etc/x' v2 = Volume(disk) v2.mountpoint = '....' v2.info['lastmountpoint'] = '/etc' disk.volumes.volumes = [v1, v2] with self.assertRaises(NoRootFoundError): parser.reconstruct()
def test_simple(self): parser = ImageParser() disk = parser.add_disk("...") v1 = Volume(disk) v1.mountpoint = '...' v1.info['lastmountpoint'] = '/' v2 = Volume(disk) v2.mountpoint = '....' v2.info['lastmountpoint'] = '/etc' disk.volumes.volumes = [v1, v2] with mock.patch.object(v2, "bindmount") as v2_bm: parser.reconstruct() v2_bm.assert_called_once_with(".../etc")
def test_multiple_roots(self): parser = ImageParser() disk = parser.add_disk("...") v1 = Volume(disk) v1.index = '1' v1.mountpoint = '...' v1.info['lastmountpoint'] = '/' v2 = Volume(disk) v2.index = '2' v2.mountpoint = '....' v2.info['lastmountpoint'] = '/' v3 = Volume(disk) v3.index = '3' v3.mountpoint = '.....' v3.info['lastmountpoint'] = '/etc' disk.volumes.volumes = [v1, v2, v3] with mock.patch.object(v1, "bindmount") as v1_bm, mock.patch.object(v2, "bindmount") as v2_bm, \ mock.patch.object(v3, "bindmount") as v3_bm: parser.reconstruct() v1_bm.assert_not_called() v2_bm.assert_not_called() v3_bm.assert_called_with('.../etc')
class ImageMounterShell(ArgumentParsedShell): prompt = '(imount) ' file = None parser = None args = None saved = False def save(self): with open(SAVE_PATH, 'wb') as f: pickle.dump(self.parser, f) self.saved = True def load(self): with open(SAVE_PATH, 'rb') as f: self.parser = pickle.load(f) def preloop(self): """if the parser is not already set, loads the parser.""" if not self.parser: self.stdout.write("Welcome to imagemounter {version}".format(version=__version__)) self.stdout.write("\n") self.parser = ImageParser() for p in self.args.paths: self.onecmd('disk "{}"'.format(p)) def onecmd(self, line): """Do not crash the entire program when a single command fails.""" try: return cmd.Cmd.onecmd(self, line) except Exception as e: print("Critical error.", e) def error(self, error): """Writes an error to the console""" self.stdout.write('*** %s\n' % error) def _get_all_indexes(self): """Returns all indexes available in the parser""" if self.parser: return [v.index for v in self.parser.get_volumes()] + [d.index for d in self.parser.disks] else: return None def _get_by_index(self, index): """Returns a volume,disk tuple for the specified index""" volume_or_disk = self.parser.get_by_index(index) volume, disk = (volume_or_disk, None) if not isinstance(volume_or_disk, Disk) else (None, volume_or_disk) return volume, disk ###################################################################### # disk command ###################################################################### def parser_disk(self, parser): parser.description = "Add a disk to the current parser" p = parser.add_argument('path', help='path to the disk image that you want to mount') try: from argcomplete.completers import FilesCompleter p.completer = FilesCompleter([".dd", ".e01", ".aff", ".DD", ".E01", ".AFF"]) except ImportError: pass parser.add_argument("--mounter", choices=DISK_MOUNTERS, help="the method to mount with") def arg_disk(self, args): args.path = os.path.expanduser(args.path) if not os.path.exists(args.path): return self.error("The path {path} does not exist".format(path=args.path)) disk = self.parser.add_disk(args.path, disk_mounter=args.mounter) disk.mount() for _ in disk.detect_volumes(): pass print("Added {path} to the image mounter as index {index}".format(path=args.path, index=disk.index)) ###################################################################### # mount command ###################################################################### def parser_mount(self, parser): parser.description = "Mount a volume or disk by its index" parser.add_argument('index', help='volume or disk index', choices=self._get_all_indexes()) parser.add_argument('-r', '--recursive', action='store_true', help='recursively mount all volumes under this index') parser.add_argument('-f', '--fstype', default=None, choices=FILE_SYSTEM_TYPES, help='specify the file system type for the volume') parser.add_argument('-k', '--key', default=None, help='specify the key for the volume') def arg_mount(self, args): col = get_coloring_func() volume, disk = self._get_by_index(args.index) if not args.recursive: if disk: if not disk.is_mounted: try: disk.mount() except Exception as e: pass else: print(col("Disk {} is already mounted.".format(disk.index), 'red')) else: if not volume.is_mounted: try: if args.key is not None: volume.key = args.key volume.init_volume(fstype=args.fstype) if volume.is_mounted: if volume.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(volume.mountpoint, "green", attrs=['bold']), index=volume.index)) else: print("Mounted volume {index} (no mountpoint available)".format(index=volume.index)) else: print("Refused to mount volume {index}.".format(index=volume.index)) except Exception as e: print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=volume.index), "red")) else: if volume.mountpoint: print(col("Volume {} is already mounted at {}.".format(volume.index, volume.mountpoint), 'red')) else: print(col("Volume {} is already mounted.".format(volume.index), 'red')) else: if disk: if not disk.is_mounted: try: disk.mount() except Exception as e: pass it = disk.init_volumes else: it = volume.init for v in it(): if v.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(v.mountpoint, "green", attrs=['bold']), index=v.index)) elif v.exception: e = v.exception print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=v.index), "red")) ###################################################################### # unmount command ###################################################################### def parser_unmount(self, parser): parser.description = "Unmount a disk or volume by its index. Is recursive by design." parser.add_argument('index', help='volume index', nargs='?', choices=self._get_all_indexes()) def arg_unmount(self, args): if args.index: volume = self.parser.get_by_index(args.index) volume.unmount() print("Unmounted {index}".format(index=volume.index)) else: self.parser.clean() print("Unmounted everything") ###################################################################### # show command ###################################################################### def parser_show(self, parser): parser.description = "Without arguments, displays a tree view of all known volumes and disks. With an " \ "argument, it provides details about the referenced volume or disk." parser.add_argument('index', help='volume index', nargs='?', choices=self._get_all_indexes()) def arg_show(self, args): if not args.index: self._show_tree() else: col = get_coloring_func() volume, disk = self._get_by_index(args.index) # displays a volume's details if volume: print(col("Details of volume {description}".format(description=volume.get_description()), attrs=['bold'])) if volume.is_mounted: print(col("Currently mounted", 'green')) print("\nAttributes:") for attr in ['size', 'offset', 'slot', 'flag', 'block_size', 'fstype', 'key']: print(" - {name:<15} {value}".format(name=attr, value=getattr(volume, attr))) if volume.mountpoint or volume.loopback or volume._paths: print("\nPaths:") if volume.mountpoint: print(" - mountpoint {value}".format(value=volume.mountpoint)) if volume.loopback: print(" - loopback {value}".format(value=volume.loopback)) for k, v in volume._paths.items(): print(" - {name:<15} {value}".format(name=k, value=v)) if volume.info: print("\nAdditional information:") for k, v in volume.info.items(): print(" - {name:<15} {value}".format(name=k, value=v)) else: # displays a disk's details print(col("Details of disk {description}".format(description=disk.paths[0]), attrs=['bold'])) if disk.is_mounted: print(col("Currently mounted", 'green')) print("\nAttributes:") for attr in ['offset', 'block_size', 'read_write', 'disk_mounter']: print(" - {name:<15} {value}".format(name=attr, value=getattr(disk, attr))) if disk.mountpoint or disk.rwpath or disk._paths: print("\nPaths:") if disk.mountpoint: print(" - mountpoint {value}".format(value=disk.mountpoint)) if disk.rwpath: print(" - rwpath {value}".format(value=disk.rwpath)) for k, v in disk._paths.items(): print(" - {name:<15} {value}".format(name=k, value=v)) def _show_tree(self): col = get_coloring_func() for disk in self.parser.disks: print("- {index:<5} {type} {filename}".format( index=col("{:<5}".format(disk.index), 'green' if disk.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(disk.volumes.vstype), attrs=['dark']), filename=disk.paths[0] )) def _show_volume_system(volumes, level=0): level += 1 for i, v in enumerate(volumes): level_str = " " * level + ("└ " if i == len(volumes) - 1 else "├ ") tp = v.volumes.vstype if v.fstype.type == 'volumesystem' else v.fstype if v.flag == 'alloc' else v.flag print("{level_str}{index} {type} {size:<10} {description}".format( level_str=level_str, index=col("{:<5}".format(v.index), 'green' if v.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(str(tp)), attrs=['dark']), description=v.get_description(with_index=False, with_size=False)[:30], size=v.get_formatted_size() )) _show_volume_system(v.volumes, level) _show_volume_system(disk.volumes) ###################################################################### # set command ###################################################################### def parser_set(self, parser): parser.description = "Modifies a property of a volume or disk. This is for advanced usage only." parser.add_argument('index', help='volume index', choices=self._get_all_indexes()) parser.add_argument('name', help='property name', choices=['size', 'offset', 'slot', 'flag', 'block_size', 'fstype', 'key', 'disk_mounter']) parser.add_argument('value', help='property value') def arg_set(self, args): col = get_coloring_func() volume, disk = self._get_by_index(args.index) if volume: if args.name in ['size', 'offset', 'block_size']: try: setattr(volume, args.name, int(args.value)) except ValueError: print(col("Invalid value provided for {}".format(args.name), 'red')) else: print(col("Updated value for {}".format(args.name), 'green')) elif args.name in ['slot', 'flag', 'fstype', 'key']: setattr(volume, args.name, args.value) print(col("Updated value for {}".format(args.name), 'green')) else: print(col("Property {} can't be set for a volume".format(args.name), 'red')) else: if args.name in ['offset', 'block_size']: try: setattr(disk, args.name, int(args.value)) except ValueError: print(col("Invalid value provided for {}".format(args.name), 'red')) else: print(col("Updated value for {}".format(args.name), 'green')) elif args.name in ['disk_mounter']: setattr(disk, args.name, args.value) print(col("Updated value for {}".format(args.name), 'green')) else: print(col("Property {} can't be set for a disk".format(args.name), 'red')) ###################################################################### # quit command ###################################################################### def do_save(self, arg): self.save() def do_EOF(self, arg): self.save() return True def do_quit(self, arg): """Quits the program.""" if self.saved: self.save() else: self.parser.clean() return True
class ImageMounterShell(ArgumentParsedShell): prompt = '(imount) ' file = None parser = None def preloop(self): self.stdout.write("Welcome to imagemounter {version}".format(version=__version__)) self.stdout.write("\n") self.parser = ImageParser() def error(self, error): self.stdout.write('*** %s\n' % error) def parser_disk(self, parser): parser.description = "Add a disk to the current parser" p = parser.add_argument('path', help='path to the disk image that you want to mount') try: from argcomplete.completers import FilesCompleter p.completer = FilesCompleter([".dd", ".e01", ".aff", ".DD", ".E01", ".AFF"]) except ImportError: pass parser.add_argument("--mounter", choices=DISK_MOUNTERS, help="the method to mount with") def arg_disk(self, args): if not os.path.exists(args.path): return self.error("The path {path} does not exist".format(path=args.path)) disk = self.parser.add_disk(args.path, disk_mounter=args.mounter) disk.mount() for _ in disk.detect_volumes(): pass print("Added {path} to the image mounter as index {index}".format(path=args.path, index=disk.index)) def _get_all_indexes(self): if self.parser: return [v.index for v in self.parser.get_volumes()] + [d.index for d in self.parser.disks] else: return None def parser_mount(self, parser): parser.description = "Mount a volume by its index" parser.add_argument('index', help='volume index', choices=self._get_all_indexes()) parser.add_argument('-r', '--recursive', action='store_true') def arg_mount(self, args): col = get_coloring_func() volume_or_disk = self.parser.get_by_index(args.index) volume, disk = (volume_or_disk, None) if not isinstance(volume_or_disk, Disk) else (None, volume_or_disk) if not args.recursive: if disk and not disk.is_mounted: try: disk.mount() except Exception as e: pass else: try: volume.init_volume() if volume.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(volume.mountpoint, "green", attrs=['bold']), index=volume.index)) else: print("Mounted volume {index} (no mountpoint available)".format(index=volume.index)) except Exception as e: import traceback ; traceback.print_exc() print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=volume.index), "red")) else: if disk: it = disk.init_volumes else: it = volume.init for v in it(): if v.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(v.mountpoint, "green", attrs=['bold']), index=v.index)) elif v.exception: e = v.exception print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=v.index), "red")) def parser_unmount(self, parser): parser.description = "Unmount a volume by its index" parser.add_argument('index', help='volume index', nargs='?', choices=self._get_all_indexes()) def arg_unmount(self, args): if args.index: volume = self.parser.get_by_index(args.index) volume.unmount() print("Unmounted {index}".format(index=volume.index)) else: self.parser.clean() print("Unmounted everything") def do_show(self, args): col = get_coloring_func() for disk in self.parser.disks: print("- {index:<5} {type} {filename}" .format(index=col("{:<5}".format(disk.index), 'green' if disk.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(disk.volumes.vstype), attrs=['dark']), filename=disk.paths[0])) def _show_volume_system(volumes, level=0): level += 1 for i, v in enumerate(volumes): level_str = " "*level + ("└ " if i == len(volumes)-1 else "├ ") tp = v.volumes.vstype if v.fstype == 'volumesystem' else v.fstype if v.flag == 'alloc' else v.flag print("{level_str}{index} {type} {size:<10} {description}" .format(level_str=level_str, index=col("{:<5}".format(v.index), 'green' if v.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(tp), attrs=['dark']), description=v.get_description(with_index=False, with_size=False)[:30], size=v.get_formatted_size())) _show_volume_system(v.volumes, level) _show_volume_system(disk.volumes) def do_quit(self, arg): """Quits the program.""" return True
class ImageMounterShell(ArgumentParsedShell): prompt = '(imount) ' file = None parser = None args = None saved = False def save(self): with open(SAVE_PATH, 'wb') as f: pickle.dump(self.parser, f) self.saved = True def load(self): with open(SAVE_PATH, 'rb') as f: self.parser = pickle.load(f) def preloop(self): """if the parser is not already set, loads the parser.""" if not self.parser: self.stdout.write("Welcome to imagemounter {version}".format(version=__version__)) self.stdout.write("\n") self.parser = ImageParser() for p in self.args.paths: self.onecmd('disk "{}"'.format(p)) def onecmd(self, line): """Do not crash the entire program when a single command fails.""" try: return cmd.Cmd.onecmd(self, line) except Exception as e: print("Critical error.", e) def error(self, error): """Writes an error to the console""" self.stdout.write('*** %s\n' % error) def _get_all_indexes(self): """Returns all indexes available in the parser""" if self.parser: return [v.index for v in self.parser.get_volumes()] + [d.index for d in self.parser.disks] else: return None def _get_by_index(self, index): """Returns a volume,disk tuple for the specified index""" volume_or_disk = self.parser.get_by_index(index) volume, disk = (volume_or_disk, None) if not isinstance(volume_or_disk, Disk) else (None, volume_or_disk) return volume, disk ###################################################################### # disk command ###################################################################### def parser_disk(self, parser): parser.description = "Add a disk to the current parser" p = parser.add_argument('path', help='path to the disk image that you want to mount') try: from argcomplete.completers import FilesCompleter p.completer = FilesCompleter([".dd", ".e01", ".aff", ".DD", ".E01", ".AFF"]) except ImportError: pass parser.add_argument("--mounter", choices=DISK_MOUNTERS, help="the method to mount with") def arg_disk(self, args): args.path = os.path.expanduser(args.path) if not os.path.exists(args.path): return self.error("The path {path} does not exist".format(path=args.path)) disk = self.parser.add_disk(args.path, disk_mounter=args.mounter) disk.mount() for _ in disk.detect_volumes(): pass print("Added {path} to the image mounter as index {index}".format(path=args.path, index=disk.index)) ###################################################################### # mount command ###################################################################### def parser_mount(self, parser): parser.description = "Mount a volume or disk by its index" parser.add_argument('index', help='volume or disk index', choices=self._get_all_indexes()) parser.add_argument('-r', '--recursive', action='store_true', help='recursively mount all volumes under this index') parser.add_argument('-f', '--fstype', default=None, choices=FILE_SYSTEM_TYPES, help='specify the file system type for the volume') parser.add_argument('-k', '--key', default=None, help='specify the key for the volume') def arg_mount(self, args): col = get_coloring_func() volume, disk = self._get_by_index(args.index) if not args.recursive: if disk: if not disk.is_mounted: try: disk.mount() except Exception as e: pass else: print(col("Disk {} is already mounted.".format(disk.index), 'red')) else: if not volume.is_mounted: try: if args.key is not None: volume.key = args.key volume.init_volume(fstype=args.fstype) if volume.is_mounted: if volume.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(volume.mountpoint, "green", attrs=['bold']), index=volume.index)) else: print("Mounted volume {index} (no mountpoint available)".format(index=volume.index)) else: print("Refused to mount volume {index}.".format(index=volume.index)) except Exception as e: print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=volume.index), "red")) else: if volume.mountpoint: print(col("Volume {} is already mounted at {}.".format(volume.index, volume.mountpoint), 'red')) else: print(col("Volume {} is already mounted.".format(volume.index), 'red')) else: if disk: if not disk.is_mounted: try: disk.mount() except Exception as e: pass it = disk.init_volumes else: it = volume.init for v in it(): if v.mountpoint: print("Mounted volume {index} at {path}" .format(path=col(v.mountpoint, "green", attrs=['bold']), index=v.index)) elif v.exception: e = v.exception print(col("An error occurred while mounting volume {index}: {type}: {args}" .format(type=type(e).__name__, args=" ".join(map(str, e.args)), index=v.index), "red")) ###################################################################### # unmount command ###################################################################### def parser_unmount(self, parser): parser.description = "Unmount a disk or volume by its index. Is recursive by design." parser.add_argument('index', help='volume index', nargs='?', choices=self._get_all_indexes()) def arg_unmount(self, args): if args.index: volume = self.parser.get_by_index(args.index) volume.unmount() print("Unmounted {index}".format(index=volume.index)) else: self.parser.clean() print("Unmounted everything") ###################################################################### # show command ###################################################################### def parser_show(self, parser): parser.description = "Without arguments, displays a tree view of all known volumes and disks. With an " \ "argument, it provides details about the referenced volume or disk." parser.add_argument('index', help='volume index', nargs='?', choices=self._get_all_indexes()) def arg_show(self, args): if not args.index: self._show_tree() else: col = get_coloring_func() volume, disk = self._get_by_index(args.index) # displays a volume's details if volume: print(col("Details of volume {description}".format(description=volume.get_description()), attrs=['bold'])) if volume.is_mounted: print(col("Currently mounted", 'green')) print("\nAttributes:") for attr in ['size', 'offset', 'slot', 'flag', 'block_size', 'fstype', 'key']: print(" - {name:<15} {value}".format(name=attr, value=getattr(volume, attr))) if volume.mountpoint or volume.loopback or volume._paths: print("\nPaths:") if volume.mountpoint: print(" - mountpoint {value}".format(value=volume.mountpoint)) if volume.loopback: print(" - loopback {value}".format(value=volume.loopback)) for k, v in volume._paths.items(): print(" - {name:<15} {value}".format(name=k, value=v)) if volume.info: print("\nAdditional information:") for k, v in volume.info.items(): print(" - {name:<15} {value}".format(name=k, value=v)) else: # displays a disk's details print(col("Details of disk {description}".format(description=disk.paths[0]), attrs=['bold'])) if disk.is_mounted: print(col("Currently mounted", 'green')) print("\nAttributes:") for attr in ['offset', 'block_size', 'read_write', 'disk_mounter']: print(" - {name:<15} {value}".format(name=attr, value=getattr(disk, attr))) if disk.mountpoint or disk.rwpath or disk._paths: print("\nPaths:") if disk.mountpoint: print(" - mountpoint {value}".format(value=disk.mountpoint)) if disk.rwpath: print(" - rwpath {value}".format(value=disk.rwpath)) for k, v in disk._paths.items(): print(" - {name:<15} {value}".format(name=k, value=v)) def _show_tree(self): col = get_coloring_func() for disk in self.parser.disks: print("- {index:<5} {type} {filename}".format( index=col("{:<5}".format(disk.index), 'green' if disk.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(disk.volumes.vstype), attrs=['dark']), filename=disk.paths[0] )) def _show_volume_system(volumes, level=0): level += 1 for i, v in enumerate(volumes): level_str = " "*level + ("└ " if i == len(volumes)-1 else "├ ") tp = v.volumes.vstype if v.fstype == 'volumesystem' else v.fstype if v.flag == 'alloc' else v.flag print("{level_str}{index} {type} {size:<10} {description}".format( level_str=level_str, index=col("{:<5}".format(v.index), 'green' if v.is_mounted else None, attrs=['bold']), type=col("{:<10}".format(tp), attrs=['dark']), description=v.get_description(with_index=False, with_size=False)[:30], size=v.get_formatted_size() )) _show_volume_system(v.volumes, level) _show_volume_system(disk.volumes) ###################################################################### # set command ###################################################################### def parser_set(self, parser): parser.description = "Modifies a property of a volume or disk. This is for advanced usage only." parser.add_argument('index', help='volume index', choices=self._get_all_indexes()) parser.add_argument('name', help='property name', choices=['size', 'offset', 'slot', 'flag', 'block_size', 'fstype', 'key', 'disk_mounter']) parser.add_argument('value', help='property value') def arg_set(self, args): col = get_coloring_func() volume, disk = self._get_by_index(args.index) if volume: if args.name in ['size', 'offset', 'block_size']: try: setattr(volume, args.name, int(args.value)) except ValueError: print(col("Invalid value provided for {}".format(args.name), 'red')) else: print(col("Updated value for {}".format(args.name), 'green')) elif args.name in ['slot', 'flag', 'fstype', 'key']: setattr(volume, args.name, args.value) print(col("Updated value for {}".format(args.name), 'green')) else: print(col("Property {} can't be set for a volume".format(args.name), 'red')) else: if args.name in ['offset', 'block_size']: try: setattr(disk, args.name, int(args.value)) except ValueError: print(col("Invalid value provided for {}".format(args.name), 'red')) else: print(col("Updated value for {}".format(args.name), 'green')) elif args.name in ['disk_mounter']: setattr(disk, args.name, args.value) print(col("Updated value for {}".format(args.name), 'green')) else: print(col("Property {} can't be set for a disk".format(args.name), 'red')) ###################################################################### # quit command ###################################################################### def do_save(self, arg): self.save() def do_EOF(self, arg): self.save() return True def do_quit(self, arg): """Quits the program.""" if self.saved: self.save() else: self.parser.clean() return True
def test_no_volumes(self): parser = ImageParser() parser.add_disk("...") with self.assertRaises(NoRootFoundError): parser.reconstruct()