|
1 | 1 | #!/bin/bash |
| 2 | +# |
| 3 | +# Run Heads build in Docker using the latest published image |
| 4 | +# This is suitable for development and testing with the most up-to-date environment |
| 5 | +# |
2 | 6 |
|
3 | | -# Inform the user that the latest published Docker image is being used |
4 | | -echo "Using the latest Docker image: tlaurion/heads-dev-env:latest" |
5 | | -DOCKER_IMAGE="tlaurion/heads-dev-env:latest" |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +# ============================================================================ |
| 10 | +# CONSTANTS |
| 11 | +# ============================================================================ |
| 12 | + |
| 13 | +readonly DOCKER_IMAGE="tlaurion/heads-dev-env:latest" |
| 14 | + |
| 15 | +# ============================================================================ |
| 16 | +# FUNCTIONS |
| 17 | +# ============================================================================ |
6 | 18 |
|
7 | | -# Function to display usage information |
8 | 19 | usage() { |
9 | | - echo "Usage: $0 [OPTIONS] -- [COMMAND]" |
10 | | - echo "Options:" |
11 | | - echo " CPUS=N Set the number of CPUs" |
12 | | - echo " V=1 Enable verbose mode" |
13 | | - echo "Command:" |
14 | | - echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME" |
| 20 | + cat << EOF |
| 21 | +Usage: $0 [OPTIONS] -- [COMMAND] |
| 22 | +
|
| 23 | +Options: |
| 24 | + CPUS=N Set the number of CPUs |
| 25 | + V=1 Enable verbose mode |
| 26 | + -h, --help Display this help message |
| 27 | +
|
| 28 | +Command: |
| 29 | + The command to run inside the Docker container (e.g., make BOARD=BOARD_NAME) |
| 30 | +
|
| 31 | +Examples: |
| 32 | + $0 make BOARD=qemu-coreboot-fbwhiptail-tpm2 |
| 33 | + $0 make BOARD=t440p V=1 |
| 34 | +
|
| 35 | +For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config |
| 36 | +EOF |
15 | 37 | } |
16 | 38 |
|
17 | | -# Function to kill GPG toolstack related processes using USB devices |
| 39 | +# Kill GPG toolstack related processes using USB devices |
18 | 40 | kill_usb_processes() { |
19 | | - # check if scdaemon or pcscd processes are using USB devices |
20 | | - if [ -d /dev/bus/usb ]; then |
21 | | - if sudo lsof /dev/bus/usb/00*/0* 2>/dev/null | awk 'NR>1 {print $2}' | xargs -r ps -p | grep -E 'scdaemon|pcscd' >/dev/null; then |
22 | | - echo "Killing GPG toolstack related processes using USB devices..." |
23 | | - sudo lsof /dev/bus/usb/00*/0* 2>/dev/null | awk 'NR>1 {print $2}' | xargs -r ps -p | grep -E 'scdaemon|pcscd' | awk '{print $1}' | xargs -r sudo kill -9 |
24 | | - fi |
| 41 | + if [ ! -d "/dev/bus/usb" ]; then |
| 42 | + return 0 |
| 43 | + fi |
| 44 | + |
| 45 | + if sudo lsof /dev/bus/usb/00*/0* 2>/dev/null | \ |
| 46 | + awk 'NR>1 {print $2}' | \ |
| 47 | + xargs -r ps -p | \ |
| 48 | + grep -E 'scdaemon|pcscd' >/dev/null 2>&1; then |
| 49 | + echo "Killing GPG toolstack related processes using USB devices..." |
| 50 | + sudo lsof /dev/bus/usb/00*/0* 2>/dev/null | \ |
| 51 | + awk 'NR>1 {print $2}' | \ |
| 52 | + xargs -r ps -p | \ |
| 53 | + grep -E 'scdaemon|pcscd' | \ |
| 54 | + awk '{print $1}' | \ |
| 55 | + xargs -r sudo kill -9 |
25 | 56 | fi |
26 | 57 | } |
27 | 58 |
|
28 | | -# Handle Ctrl-C (SIGINT) to exit gracefully |
29 | | -trap "echo 'Script interrupted. Exiting...'; exit 1" SIGINT |
| 59 | +# Build Docker run options based on available host capabilities |
| 60 | +build_docker_opts() { |
| 61 | + local opts="-e DISPLAY=${DISPLAY} --network host --rm -ti" |
| 62 | + |
| 63 | + # Add USB device if available |
| 64 | + if [ -d "/dev/bus/usb" ]; then |
| 65 | + opts="${opts} --device=/dev/bus/usb:/dev/bus/usb" |
| 66 | + echo "--->Launching container with access to host's USB buses..." >&2 |
| 67 | + else |
| 68 | + echo "--->Launching container without access to host's USB buses..." >&2 |
| 69 | + fi |
| 70 | + |
| 71 | + # Add KVM device if available |
| 72 | + if [ -e "/dev/kvm" ]; then |
| 73 | + opts="${opts} --device=/dev/kvm:/dev/kvm" |
| 74 | + fi |
| 75 | + |
| 76 | + # Add X11 display support |
| 77 | + opts="${opts} -v /tmp/.X11-unix:/tmp/.X11-unix" |
30 | 78 |
|
31 | | -# Check if --help or -h is provided |
| 79 | + # Add Xauthority if it exists |
| 80 | + if [ -f "${HOME}/.Xauthority" ]; then |
| 81 | + opts="${opts} -v ${HOME}/.Xauthority:/root/.Xauthority:ro" |
| 82 | + fi |
| 83 | + |
| 84 | + echo "${opts}" |
| 85 | +} |
| 86 | + |
| 87 | +# ============================================================================ |
| 88 | +# MAIN |
| 89 | +# ============================================================================ |
| 90 | + |
| 91 | +# Handle help request |
32 | 92 | for arg in "$@"; do |
33 | 93 | if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then |
34 | 94 | usage |
35 | 95 | exit 0 |
36 | 96 | fi |
37 | 97 | done |
38 | 98 |
|
| 99 | +echo "Using the latest Docker image: ${DOCKER_IMAGE}" |
| 100 | + |
| 101 | +# Handle Ctrl-C gracefully |
| 102 | +trap "echo 'Script interrupted. Exiting...'; exit 130" SIGINT |
| 103 | + |
39 | 104 | # Kill processes using USB devices |
40 | 105 | kill_usb_processes |
41 | 106 |
|
42 | | -# Inform the user about entering the Docker container |
43 | | -echo "----" |
44 | | -echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional." |
45 | | -echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config." |
46 | | -echo |
47 | | -echo "Type exit within docker image to get back to host if launched interactively!" |
48 | | -echo "----" |
49 | | -echo |
50 | | - |
51 | | -# Execute the docker run command with the provided parameters |
52 | | -if [ -d "/dev/bus/usb" ]; then |
53 | | - echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..." |
54 | | - docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@" |
55 | | -else |
56 | | - echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..." |
57 | | - docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@" |
58 | | -fi |
| 107 | +# Display usage information |
| 108 | +cat << EOF |
| 109 | +
|
| 110 | +---- |
| 111 | +Usage reminder: The minimal command is 'make BOARD=XYZ', where additional |
| 112 | +options, including 'V=1' or 'CPUS=N' are optional. |
| 113 | +
|
| 114 | +For more advanced QEMU testing options, refer to: |
| 115 | + - targets/qemu.md |
| 116 | + - boards/qemu-*/*.config |
| 117 | +
|
| 118 | +Type 'exit' within the Docker container to return to the host. |
| 119 | +---- |
| 120 | +
|
| 121 | +EOF |
| 122 | + |
| 123 | +# Build Docker options and execute |
| 124 | +DOCKER_RUN_OPTS=$(build_docker_opts) |
| 125 | + |
| 126 | +# shellcheck disable=SC2086 |
| 127 | +exec docker run ${DOCKER_RUN_OPTS} -v "$(pwd):$(pwd)" -w "$(pwd)" "${DOCKER_IMAGE}" -- "$@" |
0 commit comments