Exemple #1
0
  def test_allow_all_fields_empty(self):
    inspection_results = model.InspectionResults(
        os=None, device=None, architecture=None)

    expected = {"architecture": None, "device": None, "os": None}
    actual = json.dumps(inspection_results, cls=model.ModelJSONEncoder)
    assert expected == json.loads(actual)
    def test_inspection_results_with_boot(self):
        boot_results = model.BootInspectionResults(bios_bootable=True,
                                                   uefi_bootable=True,
                                                   root_fs="btrfs")
        expected_boot_results = {
            "bios_bootable": True,
            "uefi_bootable": True,
            "root_fs": "btrfs"
        }
        actual_boot_results = json.dumps(boot_results,
                                         cls=model.ModelJSONEncoder)
        assert expected_boot_results == json.loads(actual_boot_results)

        inspection_results = model.InspectionResults(os=None,
                                                     device=None,
                                                     architecture=None)
        inspection_results.bios_bootable = boot_results.bios_bootable
        inspection_results.uefi_bootable = boot_results.uefi_bootable
        inspection_results.root_fs = boot_results.root_fs
        expected_inspection_results = {
            "architecture": None,
            "device": None,
            "os": None,
            "bios_bootable": True,
            "uefi_bootable": True,
            "root_fs": "btrfs"
        }
        actual_inspection_results = json.dumps(inspection_results,
                                               cls=model.ModelJSONEncoder)
        assert expected_inspection_results == json.loads(
            actual_inspection_results)
Exemple #3
0
def main():
    format_options_and_help = {
        'json': 'JSON without newlines. Suitable for consumption by '
        'another program.',
        'human': 'Readable format that includes newlines and indents.',
        'daisy':
        'Key-value format supported by Daisy\'s serial log collector.',
    }

    parser = argparse.ArgumentParser(
        description='Find boot-related properties of a disk.')
    parser.add_argument('--format',
                        choices=format_options_and_help.keys(),
                        default='human',
                        help=' '.join([
                            '`%s`: %s' % (key, value)
                            for key, value in format_options_and_help.items()
                        ]))
    parser.add_argument('device', help='a block device or disk file.')
    args = parser.parse_args()

    g = guestfs.GuestFS(python_return_dict=True)
    g.add_drive_opts(args.device, readonly=1)
    g.launch()

    req = urllib.request.Request(
        "http://metadata.google.internal/computeMetadata/v1"
        "/instance/attributes/is-inspect-os",
        headers={'Metadata-Flavor': 'Google'})
    is_inspect_os = urllib.request.urlopen(req).read()
    if is_inspect_os == b'true':
        results = inspection.inspect_device(g, args.device)
    else:
        results = model.InspectionResults(device=None,
                                          os=None,
                                          architecture=None)

    boot_results = inspection.inspect_boot_loader(g, args.device)
    results.bios_bootable = boot_results.bios_bootable
    results.uefi_bootable = boot_results.uefi_bootable
    results.root_fs = boot_results.root_fs

    globals()['_output_' + args.format](results)
def inspect_device(g, device: str) -> model.InspectionResults:
    """Finds boot-related properties for a device using offline inspection.

  Args:
    g (guestfs.GuestFS): A launched, but unmounted, GuestFS instance.
    device: a reference to a mounted block device (eg: /dev/sdb), or
    to a virtual disk file (eg: /opt/images/disk.vmdk).

  Example:

    g = guestfs.GuestFS(python_return_dict=True)
    g.add_drive_opts("/dev/sdb", format="raw")
    g.launch()
    results = inspect_device(g, "/dev/sdb")
  """

    roots = g.inspect_os()
    if len(roots) == 0:
        print('inspect_vm: no operating systems found', file=sys.stderr)
        sys.exit(1)
    root = roots[0]
    mount_points = g.inspect_get_mountpoints(root)
    for dev, mp in sorted(mount_points.items(), key=lambda k: len(k[0])):
        try:
            g.mount_ro(mp, dev)
        except RuntimeError as msg:
            print('%s (ignored)' % msg, file=sys.stderr)
    fs = boot_inspect.system.filesystems.GuestFSFilesystem(g)
    operating_system = linux.Inspector(fs, _LINUX).inspect()
    if not operating_system:
        operating_system = windows.Inspector(g, root).inspect()
    arch = architecture.Inspector(g, root).inspect()
    g.umount_all()

    return model.InspectionResults(
        device=device,
        os=operating_system,
        architecture=arch,
    )
Exemple #5
0
  def test_allow_empty_minor_version(self):
    inspection_results = model.InspectionResults(
      device="/dev/sdb",
      os=model.OperatingSystem(
        distro=model.Distro.UBUNTU,
        version=model.Version(major="14", ),
      ),
      architecture=model.Architecture.x64,
    )

    expected = {
      "device": "/dev/sdb",
      "os": {
        "distro": "ubuntu",
        "version": {
          "major": "14",
          "minor": "",
        }
      },
      "architecture": "x64",
    }
    actual = json.dumps(inspection_results, cls=model.ModelJSONEncoder)
    assert expected == json.loads(actual)
Exemple #6
0
  def test_happy_case(self):
    inspection_results = model.InspectionResults(
      device="/dev/sdb",
      os=model.OperatingSystem(
        distro=model.Distro.WINDOWS,
        version=model.Version(major="8", minor="1"),
      ),
      architecture=model.Architecture.x86,
    )

    expected = {
      "device": "/dev/sdb",
      "os": {
        "distro": "windows",
        "version": {
          "major": "8",
          "minor": "1",
        }
      },
      "architecture": "x86",
    }
    actual = json.dumps(inspection_results, cls=model.ModelJSONEncoder)
    assert expected == json.loads(actual)