diff --git a/setup.sh b/setup.sh index 8f70e14..e11d023 100755 --- a/setup.sh +++ b/setup.sh @@ -61,9 +61,34 @@ log 'Configuring Logger ...' draw_progress_bar 85 log 'Copying rules to udev ...' sudo cp -r src/rules/* /usr/lib/udev/rules.d +sudo cp src/rules/90-usb-storage.rules /etc/udev/rules.d +sudo cp src/rules/90-dvd-storage.rules /etc/udev/rules.d sudo udevadm control --reload-rules sudo udevadm trigger +draw_progress_bar 90 +sudo cp src/scripts/utils/usb/usb-storage-action /usr/local/bin/ +sudo chmod +x /usr/local/bin/usb-storage-action +sudo cp src/scripts/utils/usb/addUsbScript.sh /usr/local/bin/ +sudo chmod +x /usr/local/bin/addUsbScript.sh +sudo cp src/scripts/utils/usb/usb-socket-sono.socket /etc/systemd/system +sudo cp src/scripts/utils/usb/usb-socket-sono.service /etc/systemd/system +sudo systemctl enable usb-socket-sono.socket +sudo systemctl enable usb-socket-sono.service +sudo systemctl start usb-socket-sono.socket +sudo systemctl start usb-socket-sono.service + +sudo cp src/scripts/utils/dvd/dvd-storage-action /usr/local/bin/ +sudo chmod +x /usr/local/bin/dvd-storage-action +sudo cp src/scripts/utils/dvd/addDvdScript.sh /usr/local/bin/ +sudo chmod +x /usr/local/bin/addDvdScript.sh +sudo cp src/scripts/utils/dvd/dvd-socket-sono.socket /etc/systemd/system +sudo cp src/scripts/utils/dvd/dvd-socket-sono.service /etc/systemd/system +sudo systemctl enable dvd-socket-sono.socket +sudo systemctl enable dvd-socket-sono.service +sudo systemctl start dvd-socket-sono.socket +sudo systemctl start dvd-socket-sono.service + draw_progress_bar 95 log 'Install PCIE Driver' diff --git a/src/rules/90-dvd-storage.rules b/src/rules/90-dvd-storage.rules new file mode 100644 index 0000000..603bafc --- /dev/null +++ b/src/rules/90-dvd-storage.rules @@ -0,0 +1,5 @@ +# Rule for DVD disk insertion +#ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM_MEDIA}=="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action add %E{DEVNAME}" +# Rule for DVD disk removal +#ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM_MEDIA}!="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action remove %E{DEVNAME}" +ACTION=="change", SUBSYSTEM=="block", KERNEL=="sr[0-9]*", ENV{ID_CDROM}=="1", RUN+="/usr/bin/sudo /usr/local/bin/dvd-storage-action %E{ACTION} %E{DEVNAME}" diff --git a/src/rules/90-usb-storage.rules b/src/rules/90-usb-storage.rules new file mode 100644 index 0000000..9bbf420 --- /dev/null +++ b/src/rules/90-usb-storage.rules @@ -0,0 +1,4 @@ +# Rule for USB device connection +ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action add %E{DEVNAME}" +# Rule for USB device disconnection +ACTION=="remove", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action remove %E{DEVNAME}" diff --git a/src/scripts/utils/dvd/addDvdScript.sh b/src/scripts/utils/dvd/addDvdScript.sh new file mode 100644 index 0000000..9c9bfaa --- /dev/null +++ b/src/scripts/utils/dvd/addDvdScript.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Your script logic here +echo "Connection received on Unix socket" >> /var/log/exampleDvd.log diff --git a/src/scripts/utils/dvd/dvd-socket-sono.service b/src/scripts/utils/dvd/dvd-socket-sono.service new file mode 100644 index 0000000..b6fd490 --- /dev/null +++ b/src/scripts/utils/dvd/dvd-socket-sono.service @@ -0,0 +1,10 @@ +[Unit] +Description=Example Service for Unix Socket +After=network.target + +[Service] +ExecStart=/usr/local/bin/addDvdScript.sh +StandardInput=socket + +[Install] +WantedBy=multi-user.target diff --git a/src/scripts/utils/dvd/dvd-socket-sono.socket b/src/scripts/utils/dvd/dvd-socket-sono.socket new file mode 100644 index 0000000..36d96d8 --- /dev/null +++ b/src/scripts/utils/dvd/dvd-socket-sono.socket @@ -0,0 +1,9 @@ +[Unit] +Description=Example Unix Socket + +[Socket] +ListenStream=/tmp/dvd-Sono-Socket.socket +Accept=false + +[Install] +WantedBy=sockets.target diff --git a/src/scripts/utils/dvd/dvd-storage-action b/src/scripts/utils/dvd/dvd-storage-action new file mode 100644 index 0000000..b8f5f8c --- /dev/null +++ b/src/scripts/utils/dvd/dvd-storage-action @@ -0,0 +1,93 @@ +#!/bin/bash + +ACTION=$1 +DEVNAME=$2 + +SOCKET_PATH="/tmp/usb-Sono-Socket.socket" +LOG_FILE="/tmp/tmpDvd.log" +DEBOUNCE_FILE="/tmp/dvd_debounce_$(echo $DEVNAME | tr '/' '-')" +DEBOUNCE_TIME=10 # seconds + +current_time=$(date +%s) + +# Function to log messages +log_message() { + local message=$1 + echo $message | socat - UNIX-CONNECT:$SOCKET_PATH + echo $message >> $LOG_FILE +} + +# Function to get the size of the DVD +get_dvd_size() { + udisksctl info -b $DEVNAME | grep 'Size:' | awk '{print $2}' | head -n 1 +} + +# Function to check for debounce +check_debounce() { + SIZE=$(get_dvd_size) + if [SIZE -eq 0]; then + ATTEMPTS=3 + while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do + sleep 2 + SIZE=$(get_dvd_size) + log_message "Debounced: Retried size of $DEVNAME is $SIZE" + ATTEMPTS=$((ATTEMPTS - 1)) + done + elif [SIZE -eq 0]; then + log_message "exit on: Retried size of $DEVNAME is $SIZE" + exit 0 + fi + + if [ -f "$DEBOUNCE_FILE" ]; then + last_run_time=$(cat $DEBOUNCE_FILE) + elapsed_time=$((current_time - last_run_time)) + if [ $elapsed_time -lt $DEBOUNCE_TIME ]; then + log_message "Debounced: $ACTION $DEVNAME" + exit 0 + fi + fi + echo $current_time > $DEBOUNCE_FILE +} + +check_debounce + + + +if [ "$ACTION" == "change" ]; then + # Introduce a delay before checking the device + sleep 2 # Wait for 2 seconds before proceeding + + # Check if the device is already mounted + MOUNTED=$(lsblk -o MOUNTPOINT -nr $DEVNAME) + if [ -z "$MOUNTED" ]; then + # Device is not mounted, check if it is a raw DVD + SIZE=$(get_dvd_size) + + # Debug output for size + log_message "DEBUG: Initial size of $DEVNAME is $SIZE" + + # Retry logic for getting the size + ATTEMPTS=3 + while [ "$SIZE" -eq 0 ] && [ $ATTEMPTS -gt 0 ]; do + sleep 2 + SIZE=$(get_dvd_size) + log_message "DEBUG: Retried size of $DEVNAME is $SIZE" + ATTEMPTS=$((ATTEMPTS - 1)) + done + + if [ "$SIZE" -gt 0 ]; then + log_message "success insert raw DVD $DEVNAME" + else + # Proceed to mount the device + udisksctl mount -b $DEVNAME + if [ $? -eq 0 ]; then + log_message "success mount $DEVNAME" + else + log_message "failed to mount $DEVNAME" + fi + fi + else + log_message "$DEVNAME is already mounted at $MOUNTED" + fi + log_message "success insert $DEVNAME" +fi diff --git a/src/scripts/utils/usb/addUsbScript.sh b/src/scripts/utils/usb/addUsbScript.sh new file mode 100644 index 0000000..489d6be --- /dev/null +++ b/src/scripts/utils/usb/addUsbScript.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Your script logic here +echo "Connection received on Unix socket" >> /var/log/example.log diff --git a/src/scripts/utils/usb/usb-socket-sono.service b/src/scripts/utils/usb/usb-socket-sono.service new file mode 100644 index 0000000..7726825 --- /dev/null +++ b/src/scripts/utils/usb/usb-socket-sono.service @@ -0,0 +1,10 @@ +[Unit] +Description=Example Service for Unix Socket +After=network.target + +[Service] +ExecStart=/usr/local/bin/addUsbScript.sh +StandardInput=socket + +[Install] +WantedBy=multi-user.target diff --git a/src/scripts/utils/usb/usb-socket-sono.socket b/src/scripts/utils/usb/usb-socket-sono.socket new file mode 100644 index 0000000..9ae9907 --- /dev/null +++ b/src/scripts/utils/usb/usb-socket-sono.socket @@ -0,0 +1,9 @@ +[Unit] +Description=Example Unix Socket + +[Socket] +ListenStream=/tmp/usb-Sono-Socket.socket +Accept=false + +[Install] +WantedBy=sockets.target diff --git a/src/scripts/utils/usb/usb-storage-action b/src/scripts/utils/usb/usb-storage-action new file mode 100644 index 0000000..c7a03c1 --- /dev/null +++ b/src/scripts/utils/usb/usb-storage-action @@ -0,0 +1,37 @@ +#!/bin/bash + +# Get the action (add or remove) and the device name from the udev rule +ACTION=$1 + +# Get the device name from the udev rule +DEVICE_NAME=$2 + +SOCKET_PATH="/tmp/usb-Sono-Socket.socket" + +if [ "$ACTION" == "add" ]; then + # Mount the device + udisksctl mount -b "$DEVICE_NAME" + + # Check if the mount was successful + if [ $? -eq 0 ]; then + # Write a message to the socket + echo "success connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "success connect $DEVICE_NAME" >> /tmp/tmp.log + else + echo "failure connect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "failure connect $DEVICE_NAME" >> /tmp/tmp.log + fi +elif [ "$ACTION" == "remove" ]; then + # Unmount the device + udisksctl unmount -b "$DEVICE_NAME" + + # Check if the unmount was successful + if [ $? -eq 0 ]; then + # Write a message to the socket + echo "success disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "success disconnect $DEVICE_NAME" >> /tmp/tmp.log + else + echo "failure disconnect $DEVICE_NAME" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "failure disconnect $DEVICE_NAME" >> /tmp/tmp.log + fi +fi