def with_open_session(mode=:write)
      # Set the session up
      session = Lib.lib.session

      close_session = false

      if session.state != :open
        # Open up a session for this virtual machine
        interface.lock_machine(session, mode)

        # Mark the session to be closed
        close_session = true
      end

      # Yield the block with the session
      yield session if block_given?

      # Close the session
      if close_session
        # Save these settings only if we're closing and only if the state
        # is not saved, since that doesn't allow the machine to be saved.
        session.machine.save_settings if mode == :write && session.machine.state != :saved

        # Close the session
        session.unlock_machine
      end
    rescue Exception
      # Close the session so we don't get locked out. We use a rescue block
      # here instead of an "ensure" since we ONLY want this to occur if an
      # exception is raised. Otherwise, we may or may not close the session,
      # depending how deeply nested this call to `with_open_session` is.
      # (see close_session boolean above)
      session.unlock_machine if session.state == :open

      # Reraise the exception, we're not actually catching it to handle it
      raise
    end
def with_open_session(mode=:write)
      # Set the session up
      session = Lib.lib.session

      close_session = false

      if session.state != :open
        # Open up a session for this virtual machine
        interface.lock_machine(session, mode)

        # Mark the session to be closed
        close_session = true
      end

      # Yield the block with the session
      yield session if block_given?

      # Close the session
      if close_session
        # Save these settings only if we're closing and only if the state
        # is not saved, since that doesn't allow the machine to be saved.
        session.machine.save_settings if mode == :write && session.machine.state != :saved

        # Close the session
        session.unlock_machine
      end
    rescue Exception
      # Close the session so we don't get locked out. We use a rescue block
      # here instead of an "ensure" since we ONLY want this to occur if an
      # exception is raised. Otherwise, we may or may not close the session,
      # depending how deeply nested this call to `with_open_session` is.
      # (see close_session boolean above)
      session.unlock_machine if session.state == :open

      # Reraise the exception, we're not actually catching it to handle it
      raise
    end
Opens a direct session with the machine this VM represents and yields
 the session object to a block. Many of the VirtualBox's settings can only
 be modified with an open session on a machine. An open session is similar
 to a write-lock. Once the session is completed, it must be closed, which
 this method does as well.