def acquire(self, username, password, frontend='headless'):
        """Acquire a Machine resource."""
        with self._lock() as root_session:
            for clone in self._clones:
                # Search for a free clone
                session = Session()
                try:
                    clone.lock_machine(session, LockType.write)
                except Exception:
                    continue
                else:
                    try:
                        p = session.machine.restore_snapshot()
                        p.wait_for_completion(60 * 1000)
                    except Exception:
                        pass
                    session.unlock_machine()
                    break
            else:
                # Build a new clone
                machine = root_session.machine
                clone = machine.clone(name="%s Pool" % self.machine_name)
                p = clone.launch_vm_process(type_p=frontend)
                p.wait_for_completion(60 * 1000)
                session = clone.create_session()
                console = session.console
                guest = console.guest
                try:
                    guest_session = guest.create_session(username, password,
                                                         timeout_ms=300 * 1000)
                    idle_count = 0
                    timeout = 60
                    while idle_count < 5 and timeout > 0:
                        act = console.get_device_activity([DeviceType.hard_disk])
                        if act[0] == DeviceActivity.idle:
                            idle_count += 1
                        time.sleep(0.5)
                        timeout -= 0.5
                    guest_session.close()
                    console.pause()
                    p, id_p = console.machine.take_snapshot('initialised',
                                                            'machine pool',
                                                            True)
                    p.wait_for_completion(60 * 1000)
                    self._power_down(session)
                finally:
                    if session.state == SessionState.locked:
                        session.unlock_machine()

            # Launch our clone
            p = clone.launch_vm_process(type_p=frontend)
            p.wait_for_completion(60 * 1000)
            session = clone.create_session()
            return session
Acquire a Machine resource.