Ejemplo n.º 1
0
  def _GetQemuArgs(self, image_path, image_format):
    """Returns the args to qemu used to launch the VM.

    Args:
      image_path: Path to QEMU image.
      image_format: Format of the image.
    """
    # Append 'check' to warn if the requested CPU is not fully supported.
    if 'check' not in self.qemu_cpu.split(','):
      self.qemu_cpu += ',check'
    # Append 'vmx=on' if the host supports nested virtualization. It can be
    # enabled via 'vmx+' or 'vmx=on' (or similarly disabled) so just test for
    # the presence of 'vmx'. For more details, see:
    # https://www.kernel.org/doc/Documentation/virtual/kvm/nested-vmx.txt
    if 'vmx' not in self.qemu_cpu and self.enable_kvm:
      for f in glob.glob(self.NESTED_KVM_GLOB):
        if cros_build_lib.BooleanShellValue(osutils.ReadFile(f).strip(), False):
          self.qemu_cpu += ',vmx=on'
          break

    qemu_args = [self.qemu_path]
    if self.qemu_bios_path:
      if not os.path.isdir(self.qemu_bios_path):
        raise VMError('Invalid QEMU bios path: %s' % self.qemu_bios_path)
      qemu_args += ['-L', self.qemu_bios_path]

    qemu_args += [
        '-m', self.qemu_m, '-smp', str(self.qemu_smp), '-vga', 'virtio',
        '-daemonize',
        '-pidfile', self.pidfile,
        '-chardev', 'pipe,id=control_pipe,path=%s' % self.kvm_monitor,
        '-serial', 'file:%s' % self.kvm_serial,
        '-mon', 'chardev=control_pipe',
        '-cpu', self.qemu_cpu,
        '-usb', '-device', 'usb-tablet',
        '-device', 'virtio-net,netdev=eth0',
        '-device', 'virtio-scsi-pci,id=scsi',
        '-device', 'virtio-rng',
        '-device', 'scsi-hd,drive=hd',
        '-drive', 'if=none,id=hd,file=%s,cache=unsafe,format=%s'
        % (image_path, image_format),
    ]
    # netdev args, including hostfwds.
    netdev_args = ('user,id=eth0,net=10.0.2.0/27,hostfwd=tcp:%s:%d-:%d'
                   % (remote_access.LOCALHOST_IP, self.ssh_port,
                      remote_access.DEFAULT_SSH_PORT))
    if self.qemu_hostfwd:
      for hostfwd in self.qemu_hostfwd:
        netdev_args += ',hostfwd=%s' % hostfwd
    qemu_args += ['-netdev', netdev_args]

    if self.qemu_args:
      for arg in self.qemu_args:
        qemu_args += arg.split()
    if self.enable_kvm:
      qemu_args += ['-enable-kvm']
    if not self.display:
      qemu_args += ['-display', 'none']

    return qemu_args
Ejemplo n.º 2
0
 def UserEnabled():
     """See if the global colorization preference is enabled ($NOCOLOR env)"""
     is_disabled = cros_build_lib.BooleanShellValue(
         os.environ.get('NOCOLOR'),
         msg='$NOCOLOR env var is invalid',
         default=None)
     return not is_disabled if is_disabled is not None else None
Ejemplo n.º 3
0
def GetPrebuiltsFiles(prebuilts_root):
    """Find paths to prebuilts at the given root directory.

  Assumes the root contains a Portage package index named Packages.

  The package index paths are used to de-duplicate prebuilts uploaded. The
  immediate consequence of this is reduced storage usage. The non-obvious
  consequence is the shared packages generally end up with public permissions,
  while the board-specific packages end up with private permissions. This is
  what is meant to happen, but a further consequence of that is that when
  something happens that causes the toolchains to be uploaded as a private
  board's package, the board will not be able to build properly because
  it won't be able to fetch the toolchain packages, because they're expected
  to be public.

  Args:
    prebuilts_root: Absolute path to root directory containing a package index.

  Returns:
    List of paths to all prebuilt archives, relative to the root.
  """
    package_index = binpkg.GrabLocalPackageIndex(prebuilts_root)
    prebuilt_paths = []
    for package in package_index.packages:
        prebuilt_paths.append(package['CPV'] + '.tbz2')

        include_debug_symbols = package.get('DEBUG_SYMBOLS')
        if cros_build_lib.BooleanShellValue(include_debug_symbols,
                                            default=False):
            prebuilt_paths.append(package['CPV'] + '.debug.tbz2')

    _ValidatePrebuiltsFiles(prebuilts_root, prebuilt_paths)
    return prebuilt_paths
Ejemplo n.º 4
0
def UserActPrivate(opts, cl, private_str):
    """Set the private bit on a CL to private"""
    try:
        private = cros_build_lib.BooleanShellValue(private_str, False)
    except ValueError:
        raise RuntimeError('Unknown "boolean" value: %s' % private_str)

    helper, cl = GetGerrit(opts, cl)
    helper.SetPrivate(cl, private)
Ejemplo n.º 5
0
  def IsPinnableProject(self):
    """Return whether we should pin to a revision on the CrOS branch."""
    # Backwards compatibility is an issue here. Older manifests used a different
    # tag to spcify pinning behaviour. Support both for now. (crbug.com/470690)
    # Prefer explicit tagging.
    if self[constants.MANIFEST_ATTR_BRANCHING] != '':
      return (self[constants.MANIFEST_ATTR_BRANCHING] ==
              constants.MANIFEST_ATTR_BRANCHING_PIN)

    # Old heuristic.
    return cros_build_lib.BooleanShellValue(self.get('pin'), True)
Ejemplo n.º 6
0
def ParseBool(value):
    """Parse bool argument into a bool value.

  For the existing type=bool functionality, the parser uses the built-in bool(x)
  function to determine the value.  This function will only return false if x
  is False or omitted.  Even with this type specified, however, arguments that
  are generated from a command line initially get parsed as a string, and for
  any string value passed in to bool(x), it will always return True.

  Args:
    value: String representing a boolean value.

  Returns:
    True or False.
  """
    return cros_build_lib.BooleanShellValue(value, False)
Ejemplo n.º 7
0
    def testBooleanShellValue(self):
        """Verify BooleanShellValue() inputs work as expected"""
        for v in (None, ):
            self.assertTrue(cros_build_lib.BooleanShellValue(v, True))
            self.assertFalse(cros_build_lib.BooleanShellValue(v, False))

        for v in (1234, '', 'akldjsf', '"'):
            self.assertRaises(ValueError, cros_build_lib.BooleanShellValue, v,
                              True)
            self.assertTrue(cros_build_lib.BooleanShellValue(v, True, msg=''))
            self.assertFalse(cros_build_lib.BooleanShellValue(v, False,
                                                              msg=''))

        for v in (
                'yes',
                'YES',
                'YeS',
                'y',
                'Y',
                '1',
                'true',
                'True',
                'TRUE',
        ):
            self.assertTrue(cros_build_lib.BooleanShellValue(v, True))
            self.assertTrue(cros_build_lib.BooleanShellValue(v, False))

        for v in (
                'no',
                'NO',
                'nO',
                'n',
                'N',
                '0',
                'false',
                'False',
                'FALSE',
        ):
            self.assertFalse(cros_build_lib.BooleanShellValue(v, True))
            self.assertFalse(cros_build_lib.BooleanShellValue(v, False))
Ejemplo n.º 8
0
 def UserEnabled():
     """See if the global colorization preference is enabled ($NOCOLOR env)"""
     return not cros_build_lib.BooleanShellValue(
         os.environ.get('NOCOLOR'),
         msg='$NOCOLOR env var is invalid',
         default=False)
Ejemplo n.º 9
0
    def Start(self):
        """Start the VM."""

        self.Stop()

        logging.debug('Start VM')
        self._SetQemuPath()
        self._SetVMImagePath()

        self._CreateVMDir()
        if self.copy_on_write:
            self._CreateQcow2Image()
        # Make sure we can read these files later on by creating them as ourselves.
        osutils.Touch(self.kvm_serial)
        for pipe in [self.kvm_pipe_in, self.kvm_pipe_out]:
            os.mkfifo(pipe, 0o600)
        osutils.Touch(self.pidfile)

        # Append 'check' to warn if the requested CPU is not fully supported.
        if 'check' not in self.qemu_cpu.split(','):
            self.qemu_cpu += ',check'
        # Append 'vmx=on' if the host supports nested virtualization. It can be
        # enabled via 'vmx+' or 'vmx=on' (or similarly disabled) so just test for
        # the presence of 'vmx'. For more details, see:
        # https://www.kernel.org/doc/Documentation/virtual/kvm/nested-vmx.txt
        if 'vmx' not in self.qemu_cpu and self.enable_kvm:
            for f in glob.glob(self.NESTED_KVM_GLOB):
                if cros_build_lib.BooleanShellValue(
                        osutils.ReadFile(f).strip(), False):
                    self.qemu_cpu += ',vmx=on'
                    break

        qemu_args = [self.qemu_path]
        if self.qemu_bios_path:
            if not os.path.isdir(self.qemu_bios_path):
                raise VMError('Invalid QEMU bios path: %s' %
                              self.qemu_bios_path)
            qemu_args += ['-L', self.qemu_bios_path]

        qemu_args += [
            '-m',
            self.qemu_m,
            '-smp',
            str(self.qemu_smp),
            '-vga',
            'virtio',
            '-daemonize',
            '-usbdevice',
            'tablet',
            '-pidfile',
            self.pidfile,
            '-chardev',
            'pipe,id=control_pipe,path=%s' % self.kvm_monitor,
            '-serial',
            'file:%s' % self.kvm_serial,
            '-mon',
            'chardev=control_pipe',
            '-cpu',
            self.qemu_cpu,
            '-device',
            'virtio-net,netdev=eth0',
            '-device',
            'virtio-scsi-pci,id=scsi',
            '-device',
            'scsi-hd,drive=hd',
            '-drive',
            'if=none,id=hd,file=%s,cache=unsafe,format=%s' %
            (self.image_path, self.image_format),
        ]
        # netdev args, including hostfwds.
        netdev_args = ('user,id=eth0,net=10.0.2.0/27,hostfwd=tcp:%s:%d-:%d' %
                       (remote_access.LOCALHOST_IP, self.ssh_port,
                        remote_access.DEFAULT_SSH_PORT))
        if self.qemu_hostfwd:
            for hostfwd in self.qemu_hostfwd:
                netdev_args += ',hostfwd=%s' % hostfwd
        qemu_args += ['-netdev', netdev_args]

        if self.qemu_args:
            for arg in self.qemu_args:
                qemu_args += arg.split()
        if self.enable_kvm:
            qemu_args += ['-enable-kvm']
        if not self.display:
            qemu_args += ['-display', 'none']
        logging.info('Pid file: %s', self.pidfile)
        self.RunCommand(qemu_args)
        self.WaitForBoot()