|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Check if Nix is installed |
| 4 | +if ! command -v nix &> /dev/null; then |
| 5 | + echo "Nix is not installed or not in the PATH. Please install Nix before running this script." |
| 6 | + echo "Refer to the README.md at the root of the repository for installation instructions." |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +# Check if Docker is installed |
| 11 | +if ! command -v docker &> /dev/null; then |
| 12 | + echo "Docker is not installed or not in the PATH. Please install Docker before running this script." |
| 13 | + echo "Refer to the README.md at the root of the repository for installation instructions." |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +# Inform the user about the Docker image being used |
| 18 | +echo "** This ./docker_local_dev.sh script is for developers usage only. **" |
| 19 | +echo "" |
| 20 | +echo "Using the last locally produced Docker image: linuxboot/heads:dev-env" |
| 21 | +echo "Warning: Using anything other than the published Docker image might lead to non-reproducible builds." |
| 22 | +echo "" |
| 23 | +echo "For using the latest published Docker image, refer to ./docker_latest.sh." |
| 24 | +echo "For producing reproducible builds as CircleCI, refer to ./docker_repro.sh." |
| 25 | +echo "" |
| 26 | +echo "---" |
| 27 | + |
| 28 | +# Function to display usage information |
| 29 | +usage() { |
| 30 | + echo "Usage: $0 [OPTIONS] -- [COMMAND]" |
| 31 | + echo "Options:" |
| 32 | + echo " CPUS=N Set the number of CPUs" |
| 33 | + echo " V=1 Enable verbose mode" |
| 34 | + echo "Command:" |
| 35 | + echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME" |
| 36 | +} |
| 37 | + |
| 38 | +# Function to kill GPG toolstack related processes using USB devices |
| 39 | +kill_usb_processes() { |
| 40 | + echo "Killing any GPG toolstack related processes on host currently using USB devices..." |
| 41 | + 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 |
| 42 | + if [ $? -ne 0 ]; then |
| 43 | + echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:" |
| 44 | + echo "sudo lsof /dev/bus/usb/00*/0* | awk 'NR>1 {print \$2}' | xargs -r ps -p | grep -E 'scdaemon|pcscd' | awk '{print \$1}' | xargs -r sudo kill -9" |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +# Handle Ctrl-C (SIGINT) to exit gracefully |
| 50 | +trap "echo 'Script interrupted. Exiting...'; exit 1" SIGINT |
| 51 | + |
| 52 | +# Check if --help or -h is provided |
| 53 | +for arg in "$@"; do |
| 54 | + if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then |
| 55 | + usage |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +# Check if the git repository is dirty and if flake.nix or flake.lock are part of the uncommitted changes |
| 61 | +if [ -n "$(git status --porcelain | grep -E 'flake\.nix|flake\.lock')" ]; then |
| 62 | + echo "Warning: Uncommitted changes detected in flake.nix or flake.lock. The Docker image will be rebuilt." |
| 63 | + echo "If this was not intended, please commit your changes and rerun the script." |
| 64 | + echo "Building the Docker image from flake.nix..." |
| 65 | + nix --print-build-logs --verbose develop --ignore-environment --command true |
| 66 | + nix --print-build-logs --verbose build .#dockerImage && docker load < result |
| 67 | +else |
| 68 | + echo "Git repository is clean. Using the previously built Docker image." |
| 69 | + echo "---" |
| 70 | + sleep 1 |
| 71 | +fi |
| 72 | + |
| 73 | +# Kill processes using USB devices |
| 74 | +kill_usb_processes |
| 75 | + |
| 76 | +# Inform the user about entering the Docker container |
| 77 | +echo "----" |
| 78 | +echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional." |
| 79 | +echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config." |
| 80 | +echo "----" |
| 81 | +echo "Entering the Docker container. Type 'exit' to return to the host shell." |
| 82 | + |
| 83 | +# Execute the docker run command with the provided parameters |
| 84 | +docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) linuxboot/heads:dev-env -- "$@" |
0 commit comments