Skip to content

Various minor enhancements for the 'console' command#1571

Open
sjg20 wants to merge 12 commits intolabgrid-project:masterfrom
sjg20:push4-cons
Open

Various minor enhancements for the 'console' command#1571
sjg20 wants to merge 12 commits intolabgrid-project:masterfrom
sjg20:push4-cons

Conversation

@sjg20
Copy link
Copy Markdown
Contributor

@sjg20 sjg20 commented Jan 20, 2025

Provides an internal console and a way to automatically select a final state

The internal console avoids the problem of losing output when labgrid closes the serial port and re-opens it in the switch from to microcom.

The final state can be used to ensure that the board is off after use, without needing to run labgrid-client again. It is similar to the initial state in that sense.

Together, this PR allows 'labgrid client' to function as a console device suitable for use with U-Boot's test.py, for example

See here for how this is used in a real lab:

https://ci.u-boot.org/u-boot/u-boot/-/jobs/19119

Checklist

  • [] Documentation for the feature
  • Tests for the feature
  • Add a section on how to use the feature to doc/usage.rst
  • PR has been tested
  • Man pages have been regenerated

@sjg20 sjg20 force-pushed the push4-cons branch 3 times, most recently from 6e98f86 to a8b218b Compare February 21, 2025 19:12
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 21, 2025

Codecov Report

❌ Patch coverage is 73.89163% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.8%. Comparing base (3912486) to head (93a0f16).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/remote/client.py 32.9% 53 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1571     +/-   ##
========================================
+ Coverage    45.8%   47.8%   +2.0%     
========================================
  Files         182     183      +1     
  Lines       14718   14852    +134     
========================================
+ Hits         6743    7113    +370     
+ Misses       7975    7739    -236     
Flag Coverage Δ
3.10 47.8% <73.8%> (+2.0%) ⬆️
3.11 47.8% <73.8%> (+2.0%) ⬆️
3.12 47.8% <73.8%> (+2.0%) ⬆️
3.13 47.8% <73.8%> (+2.0%) ⬆️
3.14 47.8% <73.8%> (+2.0%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread labgrid/remote/client.py Outdated
Comment thread labgrid/remote/client.py
@aparcar
Copy link
Copy Markdown
Contributor

aparcar commented Jul 23, 2025

Thanks for these patches, I applied most of them to my staging tree for testing in my lab. Some patches no longer apply against the master branch, could you kindly rebase this?

Regarding the internal console, does it make sense to make it more configurable so it's used for i.e. pytest runs, too?

@sjg20 sjg20 force-pushed the push4-cons branch 2 times, most recently from 5b43c2f to 63b4c2c Compare July 23, 2025 13:56
@sjg20
Copy link
Copy Markdown
Contributor Author

sjg20 commented Jul 23, 2025

Thanks for these patches, I applied most of them to my staging tree for testing in my lab. Some patches no longer apply against the master branch, could you kindly rebase this?

Yes, done. Thanks for looking at it.

Regarding the internal console, does it make sense to make it more configurable so it's used for i.e. pytest runs, too?

There is a LG_CONSOLE variable which I set in the pytest setup. Do you have any suggestions on how to make this work for you?

@aparcar
Copy link
Copy Markdown
Contributor

aparcar commented Jul 23, 2025

Regarding the internal console, does it make sense to make it more configurable so it's used for i.e. pytest runs, too?
There is a LG_CONSOLE variable which I set in the pytest setup. Do you have any suggestions on how to make this work for you?

Sorry I missed that, will apply it to my setup and test further. Thanks

sjg20 added a commit to sjg20/labgrid that referenced this pull request Apr 3, 2026
Cover the drivers, scripts, pytest and the Gitlab information.

Note: This PR exists just so others can replicate my lab. I have created
separate PRs for features and will dribble those in as they get reviewed:

labgrid-project#1482
labgrid-project#1483
labgrid-project#1484
labgrid-project#1571

Cover-letter:
U-Boot integration
END

Changes in v9:
- Rebase on upstream
- Add ~/.lgrc support for site-wide labgrid defaults
- Add automatic file logging via LG_LOG_DIR for both client and pytest
- Add timestamps and CI_JOB_ID to auto-log filenames
- Make log directories world-writable for multi-user sharing
- Enable automatic labgrid-client logging in lg-env
- Wait for USB resources instead of using a fixed sleep
- Extract _prepare_for_send() from _reset_for_send()
- Add get_send_driver_name() for USB resource discovery
- Reopen console when power-cycling for send
- Increase USB resource timeout and add power_off_delay
- Add support for the Badgerd SDWire mux
- Add QEMU disk-image and writer-arguments support for EFI
- Use snapshot mode for the QEMU EFI root disk
- Add check_lab.py for board reliability checking

Changes in v8:
- Fairly minor expansions to deal with new use cases

Changes in v6:
- Fix reference to crossbar in _ub-bisect-try and ub-smoke
- Add -x option to specify the extra board directory
- Add comments to the example lg-env
- Define USE_LABGRID to tell u-boot-hooks to use labgrid hooks
- Add a few more comments to ub-pyt
- Show the build path with ub-pyt -v
- Add --allow-unmatched to ub-smoke to avoid QEMU error
- Add a new section on compatibility
- Add a new section on script usage

Changes in v5:
- Rebase on latest grpc branch
- Fix pylint errors and warnings

Changes in v4:
- Support for Beagleplay, which needs files from two separate U-Boot
  builds
- Support for a 'recovery' button needed to boot the image
- Tidy up the internal-console support
- Fix pytest behaviour with an unpatched U-Boot (that doesn't have lab
  mode)

Changes in v3:
- Rebase on top of grpc branch
- Don't mess with terminal setting unless stdin is a terminal
- Don't show an error if there are no resources when auto-acquiring
- Support QEMU in UBootWriter

Some changes in v2:
- Rationalise the flags for the U-Boot scripts
- Support tracing with em100
- Support an internal terminal instead of microcom
- Add a -D flag for debugging
- Support send-only boards
- Add a way to build the U-Boot config
- Add a control for buildman's process-limit
- allow the build-dir to be specified in a variable
- add documentation about U-Boot-pytest integration
- add source_dir and config_file to UBootProviderDriver
- add an internal terminal
- expand the U-Boot scripts
- significantly improve the U-Boot-pytest integration

The approximate diffstat is:
 contrib/sync-places.py                |  23 +-
 contrib/u-boot/.gitignore             |   1 +
 contrib/u-boot/_ub-bisect-try         |  47 ++
 contrib/u-boot/conftest.py            |  21 +
 contrib/u-boot/get_args.sh            | 128 +++++
 contrib/u-boot/index.rst              | 232 +++++++++
 contrib/u-boot/lg-client              |  11 +
 contrib/u-boot/lg-env                 |  10 +
 contrib/u-boot/test_smoke.py          |   3 +
 contrib/u-boot/ub-bisect              |  44 ++
 contrib/u-boot/ub-cli                 |  39 ++
 contrib/u-boot/ub-int                 |  41 ++
 contrib/u-boot/ub-pyt                 |  69 +++
 contrib/u-boot/ub-smoke               |  48 ++
 doc/configuration.rst                 | 611 ++++++++++++++++++++++-
 doc/usage.rst                         | 288 +++++++++++
 labgrid/driver/__init__.py            |  10 +-
 labgrid/driver/common.py              |  11 +
 labgrid/driver/consoleexpectmixin.py  |   7 +
 labgrid/driver/powerdriver.py         |  29 ++
 labgrid/driver/qemudriver.py          |  84 ++--
 labgrid/driver/recoverydriver.py      |  25 +
 labgrid/driver/resetdriver.py         |   7 +
 labgrid/driver/servodriver.py         | 157 ++++++
 labgrid/driver/sfemulatordriver.py    | 102 ++++
 labgrid/driver/ubootdriver.py         |  27 +-
 labgrid/driver/ubootproviderdriver.py | 323 ++++++++++++
 labgrid/driver/ubootwriterdriver.py   | 160 ++++++
 labgrid/driver/usbhidrelay.py         |   7 +-
 labgrid/driver/usbloader.py           | 170 ++++++-
 labgrid/driver/usbstoragedriver.py    |  18 +-
 labgrid/factory.py                    |   4 +-
 labgrid/protocol/__init__.py          |   1 +
 labgrid/protocol/bootstrapprotocol.py |   8 +-
 labgrid/protocol/recoveryprotocol.py  |  14 +
 labgrid/protocol/resetprotocol.py     |  12 +
 labgrid/pytestplugin/fixtures.py      |  21 +-
 labgrid/pytestplugin/hooks.py         |   8 +
 labgrid/remote/client.py              | 351 ++++++++-----
 labgrid/remote/config.py              |   7 +-
 labgrid/remote/exporter.py            | 122 ++++-
 labgrid/resource/__init__.py          |   6 +
 labgrid/resource/remote.py            |  24 +
 labgrid/resource/servo.py             | 485 ++++++++++++++++++
 labgrid/resource/sfemulator.py        |  33 ++
 labgrid/resource/suggest.py           |   6 +
 labgrid/resource/udev.py              |  33 ++
 labgrid/strategy/ubootstrategy.py     | 151 +++++-
 labgrid/target.py                     | 120 ++++-
 labgrid/util/helper.py                | 220 ++++----
 labgrid/util/ssh.py                   |   3 +-
 labgrid/util/term.py                  | 184 +++++++
 labgrid/var_dict.py                   |   8 +
 man/labgrid-client.1                  |   6 +
 man/labgrid-client.rst                |   4 +
 man/labgrid-device-config.5           |   4 +
 56 files changed, 4267 insertions(+), 321 deletions(-)

[1] https://github.com/labgrid-project/labgrid/issues/created_by/sjg20

Signed-off-by: Simon Glass <sjg@chromium.org>
@sjg20 sjg20 force-pushed the push4-cons branch 3 times, most recently from 585f196 to 3a3d955 Compare April 3, 2026 16:52
sjg20 added 11 commits April 3, 2026 11:47
It is common to acquire a place and then use it to access the console.
Add a -a option to support this, to avoid running labgrid-client twice,
which takes longer and makes error handling harder.

If the place is acquired, it is released on exit.

Signed-off-by: Simon Glass <sjg@chromium.org>
Some messages are printed regardless of the logging level. Convert them
to logging statements so they can be suppressed.

Signed-off-by: Simon Glass <sjg@chromium.org>
When a command fails in labgrid-client it just shows the return code.
This makes it very hard to see what actually went wrong. Handle this
exception explicitly, showing the output of the tool which failed. This
allows easy debugging of the problem.

This is particularly important with things like USB loaders.

Signed-off-by: Simon Glass <sjg@chromium.org>
When a target may or may not be acquired, it is useful to be able to
release it without checking first. Add an option for this, so that it
is not necessary to check first.

Signed-off-by: Simon Glass <sjg@chromium.org>
Use logging rather than print so that this output can be controlled.

Signed-off-by: Simon Glass <sjg@chromium.org>
At present if a console device is used by a strategy driver it is then
removed by the client immediately afterwards. If the 'console' command
is used, this means that any console data is lost.

This is done so that the console device is release for microcom, so
move the logic in with the microcom logic. With the forthcoming internal
terminal, it will not be needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
Put this code into a function to reduce the size of the _get_target()
function.

Signed-off-by: Simon Glass <sjg@chromium.org>
It is possible to set the initial state of a strategy, but not the final
state. In many cases we want to power the board off and otherwise clean
things up at the end. Provide a -e option to accomplish this.

Signed-off-by: Simon Glass <sjg@chromium.org>
It is useful to be able to see if a place is allowed without raising an
error and needing a try...except block. Add a function to handle this
and update _check_allowed() to use it.

This will be used by the updated terminal suport.

Signed-off-by: Simon Glass <sjg@chromium.org>
There is quite a lot of code here, so move the terminal function into
its own file. This will make it easier to extend it later.

Signed-off-by: Simon Glass <sjg@chromium.org>
At present Labgrid uses microcom as its console. This has some
limitations:

- console output is lost between when the board is reset and microcom
  connects
- txdelay cannot be handled in microcom, meaning that boards may fail
  to receive expected output
- the console may echo a few characters back to the caller in the time
  between when 'labgrid-client console' is executed and when microcom
  starts (which causes failures with U-Boot test system)

For many use cases, microcom is more than is needed, so provide a simple
internal terminal which resolved the above problems.

It is enabled by a '-i' option to the 'console' command, as well as an
environment variable, so that it can be adjustly without updating a lot
of scripts.

To exit, press Ctrl-] twice, quickly.

Series-changes: 4
- Get internal console working with qemu
- Show a prompt when starting, to indicate it is waiting for the board

Signed-off-by: Simon Glass <sjg@chromium.org>
@sjg20 sjg20 force-pushed the push4-cons branch 2 times, most recently from ce507a9 to 93cfcac Compare April 3, 2026 18:22
Add unit tests for the terminal handling in term.py, covering the
external console launch (microcom and telnet fallback), the internal
console read/write loop, and terminal setup and teardown. Also add
tests for the new client methods including is_allowed(),
set_initial_state(), set_end_state() and the new parser arguments.

Use real pipes for stdin rather than mocking os.read(), giving more
realistic coverage of the keystroke and txdelay paths. The exit-char
deadline test uses a threading.Event to synchronise with the run
loop, avoiding brittle fixed sleeps.

Fix an UnboundLocalError in term.internal() where log_fd could be
referenced in the finally block before being assigned.

Also fix a duplicate --logfile append in term.external() when using
microcom.

Signed-off-by: Simon Glass <sjg@chromium.org>
Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants