Testing deploying xbnet directly on OS without docker

This commit is contained in:
Emran Billah 2024-08-22 13:17:24 -03:00
parent 6e3d464ae8
commit c2e84c0d6d
12 changed files with 459 additions and 90 deletions

10
.env
View File

@ -1,14 +1,14 @@
# Base params
BASE_SUBNET=192.168.1
BASE_SUBNET=10.10.10
XBEE_INDEX=1
# Default params
DEFAULT_GATEWAY=192.168.1.1
DEFAULT_IPVLAN_IP=192.168.1.20 # Required only when running ipvlan net (look in docker compose)
DEFAULT_MACVLAN_IP=192.168.1.30 # Required only when running macvlan net (look in docker compose)
DEFAULT_GATEWAY=10.10.10.1
DEFAULT_IPVLAN_IP=10.10.10.20 # Required only when running ipvlan net (look in docker compose)
DEFAULT_MACVLAN_IP=10.10.10.30 # Required only when running macvlan net (look in docker compose)
# Configuration for xbnet0
XBEE_PORT=/dev/ttyUSB0
XBEE_BAUDRATE=230400
XBEE_NET_SRC_IP=192.168.1.201 # Ensure this IP matches the network range
XBEE_NET_SRC_IP=10.10.10.201 # Ensure this IP matches the network range
XBEE_NET_IFACE_NAME=xbnet0

View File

@ -1,7 +1,65 @@
# # Use an official Rust image as the base
# FROM rust:latest
# # Install necessary packages
# RUN apt-get update && apt-get install -y \
# libudev-dev \
# iproute2 \
# iputils-ping \
# net-tools \
# bridge-utils \
# iptables \
# supervisor \
# traceroute \
# nmap \
# tcpdump \
# vim \
# && rm -rf /var/lib/apt/lists/*
# # Clone the xbnet repository
# # The xbnet repository contains the source code needed to set up and manage the XBee network
# RUN git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet
# # Build xbnet
# # We build the xbnet project from source using Cargo, Rust's package manager and build system
# WORKDIR /usr/src/xbnet
# RUN cargo build --release
# # Copy the built binary to /usr/local/bin
# # The xbnet binary will be placed in /usr/local/bin to be accessible system-wide
# RUN cp target/release/xbnet /usr/local/bin/xbnet
# # Copy the entrypoint script
# # The entrypoint script handles the setup and monitoring of the XBee network
# COPY ./scripts/entrypoint.sh /entrypoint.sh
# RUN chmod +x /entrypoint.sh
# # Copy the supervisor config file
# # Supervisor configuration ensures that the XBee network service is managed and stays running
# COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# # Copy the health check script
# # The health check script will be used by Docker to monitor the health of the container
# COPY ./scripts/health_check.sh /health_check.sh
# RUN chmod +x /health_check.sh
# # Start supervisord as the main command
# # Supervisord will manage and monitor the services, including the XBee network
# CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# # Add healthcheck
# # The health check command periodically checks the health of the container using a custom script
# HEALTHCHECK CMD /health_check.sh || exit 1
###########################################################
# Use an official Rust image as the base
FROM rust:latest
# Install necessary packages
# Install necessary packages
RUN apt-get update && apt-get install -y \
libudev-dev \
iproute2 \
@ -11,39 +69,34 @@ RUN apt-get update && apt-get install -y \
iptables \
supervisor \
traceroute \
nmap \
tcpdump \
vim \
&& rm -rf /var/lib/apt/lists/*
# Clone the xbnet repository
# The xbnet repository contains the source code needed to set up and manage the XBee network
RUN git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet
# Build xbnet
# We build the xbnet project from source using Cargo, Rust's package manager and build system
WORKDIR /usr/src/xbnet
RUN cargo build --release
# Copy the built binary to /usr/local/bin
# The xbnet binary will be placed in /usr/local/bin to be accessible system-wide
RUN cp target/release/xbnet /usr/local/bin/xbnet
# Copy the entrypoint script
# The entrypoint script handles the setup and monitoring of the XBee network
COPY ./scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Copy the supervisor config file
# Supervisor configuration ensures that the XBee network service is managed and stays running
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy the health check script
# The health check script will be used by Docker to monitor the health of the container
COPY ./scripts/health_check.sh /health_check.sh
RUN chmod +x /health_check.sh
# Start supervisord as the main command
# Supervisord will manage and monitor the services, including the XBee network
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Add healthcheck
# The health check command periodically checks the health of the container using a custom script
HEALTHCHECK CMD /health_check.sh || exit 1

View File

@ -10,7 +10,7 @@ services:
container_name: xbnet0
privileged: true
env_file:
- .env-run-multiple
- .env
devices:
- "${XBEE0_PORT}:${XBEE0_PORT}"
command: >

View File

@ -1,3 +1,65 @@
# version: '3.8'
# services:
# xbnet-node:
# build: .
# container_name: xbnet_node
# privileged: true
# env_file: .env
# networks:
# # xbee_net:
# # ipv4_address: ${XBEE0_NET_SRC_IP} # Ensure this IP belongs to the xbee_net subnet
# # ipvlan_net:
# # ipv4_address: ${DEFAULT_IPVLAN_IP} # Assign an IP within the same range as your host's Wi-Fi
# macvlan_net:
# ipv4_address: ${DEFAULT_MACVLAN_IP} # Assign an IP within the same range as your host's Wi-Fi
# networks:
# # 1. Docker's internal way of communicating between host and containers - Support 2 way comms
# # xbee_net:
# # driver: bridge
# # ipam:
# # config:
# # - subnet: 192.168.2.0/24 # Ensure your xbnet IPs are in this range
# # 2. ipvlan working ok for comms from container to host, but not host to container
# # ipvlan_net:
# # driver: ipvlan
# # driver_opts:
# # mode: l3 # Use L3 mode, which is simpler for most setups
# # parent: wlp0s20f3 # This should be your host's Wi-Fi or Ethernet interface
# # ipam:
# # config:
# # - subnet: ${BASE_SUBNET}.0/24 # Match the Wi-Fi network range
# # gateway: ${DEFAULT_GATEWAY} # Your router's gateway
# # 3. macvlan not working for some reason
# macvlan_net:
# driver: macvlan
# driver_opts:
# parent: wlp0s20f3 # This should be your host's Wi-Fi interface
# ipam:
# config:
# - subnet: ${BASE_SUBNET}.0/24 # Match the Wi-Fi network range
# gateway: ${DEFAULT_GATEWAY} # Your router's gateway
##############################################################################
version: '3.8'
services:
@ -7,39 +69,14 @@ services:
privileged: true
env_file: .env
networks:
# xbee_net:
# ipv4_address: ${XBEE0_NET_SRC_IP} # Ensure this IP belongs to the xbee_net subnet
ipvlan_net:
ipv4_address: ${DEFAULT_IPVLAN_IP} # Assign an IP within the same range as your host's Wi-Fi
# macvlan_net:
# ipv4_address: ${DEFAULT_MACVLAN_IP} # Assign an IP within the same range as your host's Wi-Fi
xbee_net:
ipv4_address: 192.168.2.201 # IP for the xbnet in the Docker network
networks:
# 1. Docker's internal way of communicating between host and containers - Support 2 way comms
# xbee_net:
# driver: bridge
# ipam:
# config:
# - subnet: 192.168.2.0/24 # Ensure your xbnet IPs are in this range
# 2. ipvlan working ok for comms from container to host, but not host to container
ipvlan_net:
driver: ipvlan
driver_opts:
mode: l3 # Use L3 mode, which is simpler for most setups
parent: wlp0s20f3 # This should be your host's Wi-Fi or Ethernet interface
xbee_net:
driver: bridge
ipam:
config:
- subnet: ${BASE_SUBNET}.0/24 # Match the Wi-Fi network range
gateway: ${DEFAULT_GATEWAY} # Your router's gateway
# 3. macvlan not working for some reason
# macvlan_net:
# driver: macvlan
# driver_opts:
# parent: wlp0s20f3 # This should be your host's Wi-Fi interface
# ipam:
# config:
# - subnet: ${BASE_SUBNET}.0/24 # Match the Wi-Fi network range
# gateway: ${DEFAULT_GATEWAY} # Your router's gateway
- subnet: 192.168.2.0/24 # Docker network on 192.168.2.x
gateway: 192.168.2.1
ip_range: 192.168.2.200/29

View File

@ -1,12 +1,14 @@
# run-single.sh
#
# This script sets up the environment for running a Docker Compose configuration with XBee networks.
# It accepts an optional --index=<int> argument to specify the XBEE_INDEX value.
# If no index is provided, it defaults to 1.
# It accepts optional --index=<int> and --subnet=<subnet> arguments to specify the XBEE_INDEX and BASE_SUBNET values.
# If no index is provided, it defaults to 1. If no subnet is provided, it defaults to 192.168.1.
#
# Usage:
# ./run-single.sh # Runs with default index (1)
# ./run-single.sh --index=2 # Runs with index 2
# ./run-single.sh # Runs with default index (1) and default subnet (192.168.1)
# ./run-single.sh --index=2 # Runs with index 2 and default subnet (192.168.1)
# ./run-single.sh --subnet=192.168.50 # Runs with default index (1) and subnet 192.168.50
# ./run-single.sh --index=3 --subnet=192.168.50 # Runs with index 3 and subnet 192.168.50
#
# IPs assigned:
# BASE_SUBNET: eg.: 192.168.1
@ -17,8 +19,9 @@
#!/bin/bash
# Default index value
# Default values
index=1
BASE_SUBNET="192.168.1"
# Parse command-line arguments
for arg in "$@"
@ -28,14 +31,15 @@ do
index="${arg#*=}"
shift # Remove --index=<int> from processing
;;
--subnet=*)
BASE_SUBNET="${arg#*=}"
shift # Remove --subnet=<subnet> from processing
;;
*)
;;
esac
done
# Set BASE_SUBNET
BASE_SUBNET="192.168.1"
# Create a .env file with the specified parameters
cat <<EOF > .env
# Base params
@ -54,7 +58,6 @@ XBEE_NET_SRC_IP=${BASE_SUBNET}.20${index} # Ensure this IP matches the net
XBEE_NET_IFACE_NAME=xbnet0
EOF
# Restart Docker Composes services
# Restart Docker Compose services
docker compose -f docker-compose-run-single.yml down
docker compose -f docker-compose-run-single.yml up --build

71
scripts/create_tap_device.sh Executable file
View File

@ -0,0 +1,71 @@
#!/bin/bash
# create_tap_device.sh
#
# This script sets up an XBee network interface (xbnet) in tap mode. It creates a virtual Ethernet interface
# that runs over the XBee radio network, assigns it an IP address, and brings the interface up.
#
# Usage:
# ./create_tap_device.sh --index=<int> --subnet=<value> --port=<device> --serial-speed=<baudrate>
#
# Examples:
# ./create_tap_device.sh --index=1 --subnet=10.10.10 --port=/dev/ttyUSB0 --serial-speed=230400
# - Creates a tap interface with IP 10.10.10.1 using /dev/ttyUSB0 at 230400 baudrate.
#
# ./create_tap_device.sh --index=2 --subnet=192.168.2 --port=/dev/ttyUSB1 --serial-speed=115200
# - Creates a tap interface with IP 192.168.2.2 using /dev/ttyUSB1 at 115200 baudrate.
# Default values
INDEX=1
SUBNET="10.10.10"
PORT="/dev/ttyUSB0"
SERIAL_SPEED=230400
export RUST_BACKTRACE=full
# Parse command-line arguments
for arg in "$@"
do
case $arg in
--index=*)
INDEX="${arg#*=}"
shift # Remove --index=<int> from processing
;;
--subnet=*)
SUBNET="${arg#*=}"
shift # Remove --subnet=<value> from processing
;;
--port=*)
PORT="${arg#*=}"
shift # Remove --port=<value> from processing
;;
--serial-speed=*)
SERIAL_SPEED="${arg#*=}"
shift # Remove --serial-speed=<value> from processing
;;
*)
echo "Invalid argument: $arg"
exit 1
;;
esac
done
# Assign IP and interface name
IP="${SUBNET}.${INDEX}"
IFACE_NAME="xbnet${INDEX}"
# Run xbnet command
echo "Starting xbnet on ${PORT} with IP ${IP} on interface ${IFACE_NAME}"
xbnet -d --serial-speed ${SERIAL_SPEED} ${PORT} tap --iface-name ${IFACE_NAME} &
# Wait until the interface is created and up
echo "Waiting for interface ${IFACE_NAME} to be up..."
while ! ip link show ${IFACE_NAME} > /dev/null 2>&1; do
sleep 1
done
# Assign IP address to the interface
sudo ip addr add ${IP}/24 dev ${IFACE_NAME}
sudo ip link set ${IFACE_NAME} up
echo "Interface ${IFACE_NAME} created with IP ${IP}"

49
scripts/destroy_tap_device.sh Executable file
View File

@ -0,0 +1,49 @@
#!/bin/bash
# kill_tap_device.sh
#
# This script stops and cleans up resources for an XBee network interface (xbnet) in tap mode.
# It terminates the xbnet process, removes the virtual Ethernet interface, and releases associated resources.
#
# Usage:
# ./kill_tap_device.sh --index=<int>
#
# Example:
# ./kill_tap_device.sh --index=1 # Terminates and cleans up resources for xbnet1
# Default value
INDEX=1
# Parse command-line arguments
for arg in "$@"
do
case $arg in
--index=*)
INDEX="${arg#*=}"
shift # Remove --index=<int> from processing
;;
*)
echo "Invalid argument: $arg"
exit 1
;;
esac
done
# Define interface name
IFACE_NAME="xbnet${INDEX}"
# Terminate xbnet process associated with the interface
echo "Stopping xbnet on interface ${IFACE_NAME}..."
pkill -f "xbnet .* --iface-name ${IFACE_NAME}"
# Wait for the interface to go down
echo "Waiting for interface ${IFACE_NAME} to go down..."
while ip link show ${IFACE_NAME} > /dev/null 2>&1; do
sleep 1
done
# Clean up the interface
echo "Cleaning up interface ${IFACE_NAME}..."
sudo ip link delete ${IFACE_NAME}
echo "Interface ${IFACE_NAME} and associated resources cleaned up successfully."

214
scripts/entrypoint.sh Normal file → Executable file
View File

@ -1,19 +1,192 @@
# #!/bin/bash
# # entrypoint.sh
# #
# # This script is responsible for setting up and maintaining an XBee network interface within a Docker container.
# # It performs several key tasks, including checking for root privileges, setting up the XBee network interface,
# # configuring network bridges, and monitoring both the XBee device and Wi-Fi interface for connectivity.
# # The script runs in a loop to ensure continuous operation, retrying the setup process if any steps fail.
# #
# # Usage:
# # - This script is intended to be run as the entrypoint for a Docker container.
# # - Ensure that the script has executable permissions (`chmod +x entrypoint.sh`) before running it.
# # Source the get_connected_wifi_info.sh script
# source /scripts/get_connected_wifi_info.sh
# # Check if the script is running with root privileges
# check_root() {
# if [ "$(id -u)" -ne 0 ]; then
# echo "This script must be run as root. Exiting..."
# exit 1
# fi
# }
# # Clean up resources
# # - Kill the background process used to create the xbnet interface
# # - Clean up any resources created or copied over. This includes:
# # - Bridge interface
# # - xbnet interface
# cleanup() {
# echo "Cleaning up resources..."
# pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap"
# if ip link show br0 > /dev/null 2>&1; then
# ip link set br0 down
# brctl delbr br0
# fi
# if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
# ip link set $XBEE_NET_IFACE_NAME down
# ip link delete $XBEE_NET_IFACE_NAME
# fi
# }
# # Function to check if the XBee device is connected
# # - Ensures that the XBee device is present at the specified port before proceeding
# check_xbee_device() {
# if [ -e "$XBEE_PORT" ]; then
# echo "XBee device found at $XBEE_PORT. Proceeding with setup..."
# return 0
# else
# echo "Error: No XBee device found at $XBEE_PORT. Please connect the device."
# return 1
# fi
# }
# # Start the xbnet interface
# # - Attempts to start the xbnet interface in tap mode
# # - Verifies if the interface is successfully created and returns the appropriate status code
# start_xbnet_interface() {
# echo "Starting XBee network interface..."
# xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap &
# # Wait until the xbnet interface is created
# while [ ! -d "/sys/class/net/$XBEE_NET_IFACE_NAME" ]; do
# echo "Waiting for interface $XBEE_NET_IFACE_NAME to be created..."
# sleep 1
# done
# # Get the xbnet interface name (e.g., xbnet0) automatically
# XBEE_NET_IFACE_NAME=$(ls /sys/class/net | grep 'xbnet')
# # Check if the xbnet interface is found
# if [ -z "$XBEE_NET_IFACE_NAME" ]; then
# echo "Error: No XBee network interface found. Retrying setup..."
# cleanup
# return 1
# else
# echo "XBee network interface $XBEE_NET_IFACE_NAME created successfully."
# return 0
# fi
# }
# # Configure the network interface and bridge
# # - Creates the network interface with the name specified by XBEE_NET_IFACE_NAME in .env file
# # - Creates a bridge interface and attaches the xbnet interface to it
# # - Brings up the bridge interface
# configure_network_and_bridge() {
# ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME
# ip link set dev $XBEE_NET_IFACE_NAME up
# brctl addbr br0
# brctl addif br0 $XBEE_NET_IFACE_NAME
# # # Add the main network interface (e.g., eth0) to the bridge
# # if ip link show eth0 > /dev/null 2>&1; then
# # brctl addif br0 eth0
# # else
# # echo "Warning: eth0 not found. Skipping adding eth0 to bridge."
# # fi
# ip link set dev br0 up
# }
# # Monitor and bridge Wi-Fi
# # - Continuously checks for a connected Wi-Fi interface
# # - Bridges the Wi-Fi interface with the xbnet interface when found
# # - If no Wi-Fi interface is found, continues checking every 5 seconds
# monitor_and_bridge_wifi() {
# while true; do
# HOST_WIFI_IFACE=$(get_connected_wifi_device)
# if [ -n "$HOST_WIFI_IFACE" ]; then
# echo "Wi-Fi interface found: $HOST_WIFI_IFACE. Bridging with $XBEE_NET_IFACE_NAME..."
# brctl addif br0 $HOST_WIFI_IFACE
# # sudo iptables -t nat -A POSTROUTING -s ${DEFAULT_SUBNET}.0/24 -o $HOST_WIFI_IFACE -j MASQUERADE
# # sudo iptables -A FORWARD -i $HOST_WIFI_IFACE -o docker0 -j ACCEPT
# # sudo iptables -A FORWARD -i docker0 -o $HOST_WIFI_IFACE -j ACCEPT
# echo "Bridge configured. Monitoring for changes..."
# break
# else
# echo "No Wi-Fi interface found. Rechecking in 5 seconds..."
# sleep 5
# fi
# done
# }
# # Function to monitor the XBee device and restart setup if disconnected
# # - Periodically checks if the XBee device is still connected
# # - If the device is disconnected, triggers cleanup and restarts the setup process
# monitor_xbee_device() {
# while [ -e "$XBEE_PORT" ]; do
# sleep 5
# done
# echo "XBee device at $XBEE_PORT was removed. Cleaning up..."
# cleanup
# }
# # Main function to set up the XBee network
# # - Runs the setup process in a loop to ensure continuous operation
# # - If any step fails, it retries the entire process after a short delay
# setup_xbee_network() {
# while true; do
# check_xbee_device
# if [ $? -ne 0 ]; then
# sleep 5
# continue
# fi
# start_xbnet_interface
# if [ $? -ne 0 ]; then
# cleanup
# sleep 5
# continue
# fi
# configure_network_and_bridge
# monitor_and_bridge_wifi
# monitor_xbee_device
# done
# }
# # Run the setup only if the script is executed as root
# check_root
# setup_xbee_network
#############################################
#!/bin/bash
# entrypoint.sh
#
# This script is responsible for setting up and maintaining an XBee network interface within a Docker container.
# It performs several key tasks, including checking for root privileges, setting up the XBee network interface,
# configuring network bridges, and monitoring both the XBee device and Wi-Fi interface for connectivity.
# The script runs in a loop to ensure continuous operation, retrying the setup process if any steps fail.
# This script is responsible for setting up and maintaining an XBee network interface within a Docker container.
# It performs several key tasks, including checking for root privileges, setting up the XBee network interface,
# configuring network bridges, and monitoring the XBee device. The script runs in a loop to ensure continuous
# operation, retrying the setup process if any steps fail.
#
# Usage:
# - This script is intended to be run as the entrypoint for a Docker container.
# - Ensure that the script has executable permissions (`chmod +x entrypoint.sh`) before running it.
# Source the get_connected_wifi_info.sh script
source /scripts/get_connected_wifi_info.sh
# Check if the script is running with root privileges
check_root() {
if [ "$(id -u)" -ne 0 ]; then
@ -91,34 +264,10 @@ configure_network_and_bridge() {
brctl addbr br0
brctl addif br0 $XBEE_NET_IFACE_NAME
ip link set dev br0 up
}
# Monitor and bridge Wi-Fi
# - Continuously checks for a connected Wi-Fi interface
# - Bridges the Wi-Fi interface with the xbnet interface when found
# - If no Wi-Fi interface is found, continues checking every 5 seconds
monitor_and_bridge_wifi() {
while true; do
HOST_WIFI_IFACE=$(get_connected_wifi_device)
if [ -n "$HOST_WIFI_IFACE" ]; then
echo "Wi-Fi interface found: $HOST_WIFI_IFACE. Bridging with $XBEE_NET_IFACE_NAME..."
brctl addif br0 $HOST_WIFI_IFACE
iptables -t nat -A POSTROUTING -o $HOST_WIFI_IFACE -j MASQUERADE
iptables -A FORWARD -i br0 -o $HOST_WIFI_IFACE -j ACCEPT
iptables -A FORWARD -i $HOST_WIFI_IFACE -o br0 -j ACCEPT
echo "Bridge configured. Monitoring for changes..."
break
else
echo "No Wi-Fi interface found. Rechecking in 5 seconds..."
sleep 5
fi
done
}
# Function to monitor the XBee device and restart setup if disconnected
# - Periodically checks if the XBee device is still connected
# - If the device is disconnected, triggers cleanup and restarts the setup process
@ -150,7 +299,6 @@ setup_xbee_network() {
fi
configure_network_and_bridge
monitor_and_bridge_wifi
monitor_xbee_device
done
}

0
scripts/health_check.sh Normal file → Executable file
View File

8
scripts/host_setup.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh -e
# IP forwarding and NAT rules
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o wlp0s20f3 -j MASQUERADE
iptables -A FORWARD -i wlp0s20f3 -o docker0 -j ACCEPT
iptables -A FORWARD -i docker0 -o wlp0s20f3 -j ACCEPT
exit 0

0
scripts/setup_end_device.sh Normal file → Executable file
View File

0
scripts/setup_host_device.sh Normal file → Executable file
View File