docker_* helpers: pass usb host controllers to docker only if usb devices are connected, unify, bugfixes

Signed-off-by: Thierry Laurion <insurgo@riseup.net>
This commit is contained in:
Thierry Laurion 2024-11-21 16:54:08 -05:00
parent ee8d1d9ae8
commit dd540366b5
No known key found for this signature in database
GPG Key ID: 9A53E1BB3FF00461
3 changed files with 105 additions and 79 deletions

View File

@ -2,6 +2,7 @@
# Inform the user that the latest published Docker image is being used # Inform the user that the latest published Docker image is being used
echo "Using the latest Docker image: tlaurion/heads-dev-env:latest" echo "Using the latest Docker image: tlaurion/heads-dev-env:latest"
DOCKER_IMAGE="tlaurion/heads-dev-env:latest"
# Function to display usage information # Function to display usage information
usage() { usage() {
@ -15,12 +16,12 @@ usage() {
# Function to kill GPG toolstack related processes using USB devices # Function to kill GPG toolstack related processes using USB devices
kill_usb_processes() { kill_usb_processes() {
echo "Killing any GPG toolstack related processes on host currently using USB devices..." # check if scdaemon or pcscd processes are using USB devices
if [ -d /dev/bus/usb ]; then
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
echo "Killing GPG toolstack related processes using USB devices..."
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 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
if [ $? -ne 0 ]; then fi
echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:"
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"
exit 1
fi fi
} }
@ -42,8 +43,16 @@ kill_usb_processes
echo "----" echo "----"
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional." echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config." echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config."
echo
echo "Type exit within docker image to get back to host if launched interactively!"
echo "----" echo "----"
echo "Entering the Docker container. Type 'exit' to return to the host shell." echo
# Execute the docker run command with the provided parameters # Execute the docker run command with the provided parameters
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 -- "$@" if [ -d "/dev/bus/usb" ]; then
echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..."
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
else
echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..."
docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
fi

View File

@ -1,5 +1,8 @@
#!/bin/bash #!/bin/bash
#locally build docker name is linuxboot/heads:dev-env
DOCKER_IMAGE="linuxboot/heads:dev-env"
# Check if Nix is installed # Check if Nix is installed
if ! command -v nix &>/dev/null; then if ! command -v nix &>/dev/null; then
echo "Nix is not installed or not in the PATH. Please install Nix before running this script." echo "Nix is not installed or not in the PATH. Please install Nix before running this script."
@ -15,15 +18,14 @@ if ! command -v docker &> /dev/null; then
fi fi
# Inform the user about the Docker image being used # Inform the user about the Docker image being used
echo "** This ./docker_local_dev.sh script is for developers usage only. **" echo "!!! This ./docker_local_dev.sh script is for developers usage only. !!!"
echo "" echo ""
echo "Using the last locally produced Docker image: linuxboot/heads:dev-env" echo "Using the last locally built Docker image when flake.nix/flake.lock was modified and repo was dirty: linuxboot/heads:dev-env"
echo "Warning: Using anything other than the published Docker image might lead to non-reproducible builds." echo "!!! Warning: Using anything other than the published Docker image might lead to non-reproducible builds. !!!"
echo "" echo ""
echo "For using the latest published Docker image, refer to ./docker_latest.sh." echo "For using the latest published Docker image, refer to ./docker_latest.sh."
echo "For producing reproducible builds as CircleCI, refer to ./docker_repro.sh." echo "For producing reproducible builds as CircleCI, refer to ./docker_repro.sh."
echo "" echo ""
echo "---"
# Function to display usage information # Function to display usage information
usage() { usage() {
@ -37,12 +39,12 @@ usage() {
# Function to kill GPG toolstack related processes using USB devices # Function to kill GPG toolstack related processes using USB devices
kill_usb_processes() { kill_usb_processes() {
echo "Killing any GPG toolstack related processes on host currently using USB devices..." # check if scdaemon or pcscd processes are using USB devices
if [ -d /dev/bus/usb ]; then
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
echo "Killing GPG toolstack related processes using USB devices..."
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 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
if [ $? -ne 0 ]; then fi
echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:"
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"
exit 1
fi fi
} }
@ -59,14 +61,13 @@ done
# Check if the git repository is dirty and if flake.nix or flake.lock are part of the uncommitted changes # Check if the git repository is dirty and if flake.nix or flake.lock are part of the uncommitted changes
if [ -n "$(git status --porcelain | grep -E 'flake\.nix|flake\.lock')" ]; then if [ -n "$(git status --porcelain | grep -E 'flake\.nix|flake\.lock')" ]; then
echo "Warning: Uncommitted changes detected in flake.nix or flake.lock. The Docker image will be rebuilt." echo "**Warning: Uncommitted changes detected in flake.nix or flake.lock. The Docker image will be rebuilt!**"
echo "If this was not intended, please commit your changes and rerun the script." echo "If this was not intended, please CTRL-C now, commit your changes and rerun the script."
echo "Building the Docker image from flake.nix..." echo "Building the Docker image from flake.nix..."
nix --print-build-logs --verbose develop --ignore-environment --command true nix --print-build-logs --verbose develop --ignore-environment --command true
nix --print-build-logs --verbose build .#dockerImage && docker load <result nix --print-build-logs --verbose build .#dockerImage && docker load <result
else else
echo "Git repository is clean. Using the previously built Docker image." echo "Git repository is clean. Using the previously built Docker image when repository was unclean and flake.nix/flake.lock changes were uncommited."
echo "---"
sleep 1 sleep 1
fi fi
@ -77,8 +78,16 @@ kill_usb_processes
echo "----" echo "----"
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional." echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config." echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config."
echo
echo "Type exit within docker image to get back to host if launched interactively!"
echo "----" echo "----"
echo "Entering the Docker container. Type 'exit' to return to the host shell." echo
# Execute the docker run command with the provided parameters # Execute the docker run command with the provided parameters
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 -- "$@" if [ -d "/dev/bus/usb" ]; then
echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..."
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
else
echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..."
docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
fi

View File

@ -24,12 +24,12 @@ usage() {
# Function to kill GPG toolstack related processes using USB devices # Function to kill GPG toolstack related processes using USB devices
kill_usb_processes() { kill_usb_processes() {
echo "Killing any GPG toolstack related processes on host currently using USB devices..." # check if scdaemon or pcscd processes are using USB devices
if [ -d /dev/bus/usb ]; then
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
echo "Killing GPG toolstack related processes using USB devices..."
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 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
if [ $? -ne 0 ]; then fi
echo "Failed to kill GPG toolstack related processes using USB devices. Please run the following command manually:"
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"
exit 1
fi fi
} }
@ -51,8 +51,16 @@ kill_usb_processes
echo "----" echo "----"
echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional." echo "Usage reminder: The minimal command is 'make BOARD=XYZ', where additional options, including 'V=1' or 'CPUS=N' are optional."
echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config." echo "For more advanced QEMU testing options, refer to targets/qemu.md and boards/qemu-*/*.config."
echo
echo "Type exit within docker image to get back to host if launched interactively!"
echo "----" echo "----"
echo "Entering the Docker container. Type 'exit' to return to the host shell." echo
# Execute the docker run command with the provided parameters # Execute the docker run command with the provided parameters
if [ -d "/dev/bus/usb" ]; then
echo "--->Launching container with access to host's USB buses (some USB devices were connected to host)..."
docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@" docker run --device=/dev/bus/usb:/dev/bus/usb -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
else
echo "--->Launching container without access to host's USB buses (no USB devices was connected to host)..."
docker run -e DISPLAY=$DISPLAY --network host --rm -ti -v $(pwd):$(pwd) -w $(pwd) $DOCKER_IMAGE -- "$@"
fi