Skip to content

Commit dd54036

Browse files
committed
docker_* helpers: pass usb host controllers to docker only if usb devices are connected, unify, bugfixes
Signed-off-by: Thierry Laurion <[email protected]>
1 parent ee8d1d9 commit dd54036

File tree

3 files changed

+105
-79
lines changed

3 files changed

+105
-79
lines changed

docker_latest.sh

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22

33
# Inform the user that the latest published Docker image is being used
44
echo "Using the latest Docker image: tlaurion/heads-dev-env:latest"
5+
DOCKER_IMAGE="tlaurion/heads-dev-env:latest"
56

67
# Function to display usage information
78
usage() {
8-
echo "Usage: $0 [OPTIONS] -- [COMMAND]"
9-
echo "Options:"
10-
echo " CPUS=N Set the number of CPUs"
11-
echo " V=1 Enable verbose mode"
12-
echo "Command:"
13-
echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME"
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"
1415
}
1516

1617
# Function to kill GPG toolstack related processes using USB devices
1718
kill_usb_processes() {
18-
echo "Killing any GPG toolstack related processes on host currently using USB devices..."
19-
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
20-
if [ $? -ne 0 ]; then
21-
echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:"
22-
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"
23-
exit 1
24-
fi
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
25+
fi
2526
}
2627

2728
# Handle Ctrl-C (SIGINT) to exit gracefully
2829
trap "echo 'Script interrupted. Exiting...'; exit 1" SIGINT
2930

3031
# Check if --help or -h is provided
3132
for arg in "$@"; do
32-
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
33-
usage
34-
exit 0
35-
fi
33+
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
34+
usage
35+
exit 0
36+
fi
3637
done
3738

3839
# Kill processes using USB devices
@@ -42,8 +43,16 @@ kill_usb_processes
4243
echo "----"
4344
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
4445
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!"
4548
echo "----"
46-
echo "Entering the Docker container. Type 'exit' to return to the host shell."
49+
echo
4750

4851
# Execute the docker run command with the provided parameters
49-
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) tlaurion/heads-dev-env:latest -- "$@"
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

docker_local_dev.sh

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,74 @@
11
#!/bin/bash
22

3+
#locally build docker name is linuxboot/heads:dev-env
4+
DOCKER_IMAGE="linuxboot/heads:dev-env"
5+
36
# 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
7+
if ! command -v nix &>/dev/null; then
8+
echo "Nix is not installed or not in the PATH. Please install Nix before running this script."
9+
echo "Refer to the README.md at the root of the repository for installation instructions."
10+
exit 1
811
fi
912

1013
# 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
14+
if ! command -v docker &>/dev/null; then
15+
echo "Docker is not installed or not in the PATH. Please install Docker before running this script."
16+
echo "Refer to the README.md at the root of the repository for installation instructions."
17+
exit 1
1518
fi
1619

1720
# Inform the user about the Docker image being used
18-
echo "** This ./docker_local_dev.sh script is for developers usage only. **"
21+
echo "!!! This ./docker_local_dev.sh script is for developers usage only. !!!"
1922
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."
23+
echo "Using the last locally built Docker image when flake.nix/flake.lock was modified and repo was dirty: linuxboot/heads:dev-env"
24+
echo "!!! Warning: Using anything other than the published Docker image might lead to non-reproducible builds. !!!"
2225
echo ""
2326
echo "For using the latest published Docker image, refer to ./docker_latest.sh."
2427
echo "For producing reproducible builds as CircleCI, refer to ./docker_repro.sh."
2528
echo ""
26-
echo "---"
2729

2830
# Function to display usage information
2931
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"
32+
echo "Usage: $0 [OPTIONS] -- [COMMAND]"
33+
echo "Options:"
34+
echo " CPUS=N Set the number of CPUs"
35+
echo " V=1 Enable verbose mode"
36+
echo "Command:"
37+
echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME"
3638
}
3739

3840
# Function to kill GPG toolstack related processes using USB devices
3941
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
42+
# check if scdaemon or pcscd processes are using USB devices
43+
if [ -d /dev/bus/usb ]; then
44+
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
45+
echo "Killing GPG toolstack related processes using USB devices..."
46+
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
47+
fi
48+
fi
4749
}
4850

4951
# Handle Ctrl-C (SIGINT) to exit gracefully
5052
trap "echo 'Script interrupted. Exiting...'; exit 1" SIGINT
5153

5254
# Check if --help or -h is provided
5355
for arg in "$@"; do
54-
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
55-
usage
56-
exit 0
57-
fi
56+
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
57+
usage
58+
exit 0
59+
fi
5860
done
5961

6062
# Check if the git repository is dirty and if flake.nix or flake.lock are part of the uncommitted changes
6163
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
64+
echo "**Warning: Uncommitted changes detected in flake.nix or flake.lock. The Docker image will be rebuilt!**"
65+
echo "If this was not intended, please CTRL-C now, commit your changes and rerun the script."
66+
echo "Building the Docker image from flake.nix..."
67+
nix --print-build-logs --verbose develop --ignore-environment --command true
68+
nix --print-build-logs --verbose build .#dockerImage && docker load <result
6769
else
68-
echo "Git repository is clean. Using the previously built Docker image."
69-
echo "---"
70-
sleep 1
70+
echo "Git repository is clean. Using the previously built Docker image when repository was unclean and flake.nix/flake.lock changes were uncommited."
71+
sleep 1
7172
fi
7273

7374
# Kill processes using USB devices
@@ -77,8 +78,16 @@ kill_usb_processes
7778
echo "----"
7879
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
7980
echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config."
81+
echo
82+
echo "Type exit within docker image to get back to host if launched interactively!"
8083
echo "----"
81-
echo "Entering the Docker container. Type 'exit' to return to the host shell."
84+
echo
8285

8386
# 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 -- "$@"
87+
if [ -d "/dev/bus/usb" ]; then
88+
echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..."
89+
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
90+
else
91+
echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..."
92+
docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
93+
fi

docker_repro.sh

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,43 @@ DOCKER_IMAGE=$(grep -oP '^\s*-?\s*image:\s*\K(tlaurion/heads-dev-env:[^\s]+)' .c
55

66
# Check if the Docker image was found
77
if [ -z "$DOCKER_IMAGE" ]; then
8-
echo "Error: Docker image not found in .circleci/config.yml"
9-
exit 1
8+
echo "Error: Docker image not found in .circleci/config.yml"
9+
exit 1
1010
fi
1111

1212
# Inform the user about the versioned CircleCI Docker image being used
1313
echo "Using CircleCI Docker image: $DOCKER_IMAGE"
1414

1515
# Function to display usage information
1616
usage() {
17-
echo "Usage: $0 [OPTIONS] -- [COMMAND]"
18-
echo "Options:"
19-
echo " CPUS=N Set the number of CPUs"
20-
echo " V=1 Enable verbose mode"
21-
echo "Command:"
22-
echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME"
17+
echo "Usage: $0 [OPTIONS] -- [COMMAND]"
18+
echo "Options:"
19+
echo " CPUS=N Set the number of CPUs"
20+
echo " V=1 Enable verbose mode"
21+
echo "Command:"
22+
echo " The command to run inside the Docker container, e.g., make BOARD=BOARD_NAME"
2323
}
2424

2525
# Function to kill GPG toolstack related processes using USB devices
2626
kill_usb_processes() {
27-
echo "Killing any GPG toolstack related processes on host currently using USB devices..."
28-
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
29-
if [ $? -ne 0 ]; then
30-
echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:"
31-
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"
32-
exit 1
33-
fi
27+
# check if scdaemon or pcscd processes are using USB devices
28+
if [ -d /dev/bus/usb ]; then
29+
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
30+
echo "Killing GPG toolstack related processes using USB devices..."
31+
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
32+
fi
33+
fi
3434
}
3535

3636
# Handle Ctrl-C (SIGINT) to exit gracefully
3737
trap "echo 'Script interrupted. Exiting...'; exit 1" SIGINT
3838

3939
# Check if --help or -h is provided
4040
for arg in "$@"; do
41-
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
42-
usage
43-
exit 0
44-
fi
41+
if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then
42+
usage
43+
exit 0
44+
fi
4545
done
4646

4747
# Kill processes using USB devices
@@ -51,8 +51,16 @@ kill_usb_processes
5151
echo "----"
5252
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
5353
echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config."
54+
echo
55+
echo "Type exit within docker image to get back to host if launched interactively!"
5456
echo "----"
55-
echo "Entering the Docker container. Type 'exit' to return to the host shell."
57+
echo
5658

5759
# Execute the docker run command with the provided parameters
58-
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
60+
if [ -d "/dev/bus/usb" ]; then
61+
echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..."
62+
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
63+
else
64+
echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..."
65+
docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
66+
fi

0 commit comments

Comments
 (0)