From 0c1ecbc4378d7c0365d4299cafc3d325b6a1b311 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 15:06:31 +0330 Subject: [PATCH 01/13] Add usb-storage rules In this rule check usb connection --- src/rules/90-usb-storage.rules | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/rules/90-usb-storage.rules diff --git a/src/rules/90-usb-storage.rules b/src/rules/90-usb-storage.rules new file mode 100644 index 0000000..a86bab5 --- /dev/null +++ b/src/rules/90-usb-storage.rules @@ -0,0 +1 @@ +ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action %E{DEVNAME}" \ No newline at end of file -- 2.26.2.windows.1 From e1f78484e964b0bec5c44092d2a405129f213290 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 15:57:26 +0330 Subject: [PATCH 02/13] Add usbDirectory in utils and addUsbScript on it --- src/scripts/utils/usb/addUsbScript.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/scripts/utils/usb/addUsbScript.sh 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 -- 2.26.2.windows.1 From fed494020f2fc15e89e937ae6cf9ed86ef70b06a Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 16:00:44 +0330 Subject: [PATCH 03/13] Add usb-storage-action on usb directory --- src/scripts/utils/usb/usb-storage-action | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/scripts/utils/usb/usb-storage-action diff --git a/src/scripts/utils/usb/usb-storage-action b/src/scripts/utils/usb/usb-storage-action new file mode 100644 index 0000000..732403e --- /dev/null +++ b/src/scripts/utils/usb/usb-storage-action @@ -0,0 +1,16 @@ +#!/bin/bash + +# Get the device name from the udev rule +DEVICE_NAME=$1 + +# Mount the device +udisksctl mount -b "$DEVICE_NAME" + +# Check if the mount was successful +if [ $? -eq 0 ]; then + # Define the socket path + SOCKET_PATH="/tmp/usb-Sono-Socket.socket" + + # Write a message to the socket + echo "USB storage device connected" | socat - UNIX-CONNECT:$SOCKET_PATH + echo "USB storage device connected" >> /tmp/tmp.log -- 2.26.2.windows.1 From c788fe92ee423c3fc787b6359d8f929a32cd9abe Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 16:28:23 +0330 Subject: [PATCH 04/13] Add usb-socket-sono.service --- src/scripts/utils/usb/usb-socket-sono.service | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/scripts/utils/usb/usb-socket-sono.service 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 -- 2.26.2.windows.1 From 39ab811cf64d4681bf892ca5bb95fa08b2c83968 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 16:30:46 +0330 Subject: [PATCH 05/13] Add usb-socket-sono.socket --- src/scripts/utils/usb/usb-socket-sono.socket | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/scripts/utils/usb/usb-socket-sono.socket 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 -- 2.26.2.windows.1 From 315598f5ad054f9b892bbc8aa95d4832370bce58 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 20:23:37 +0330 Subject: [PATCH 06/13] Refactor usb-storage-rules for checking add and remove --- src/rules/90-usb-storage.rules | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/rules/90-usb-storage.rules b/src/rules/90-usb-storage.rules index a86bab5..9bbf420 100644 --- a/src/rules/90-usb-storage.rules +++ b/src/rules/90-usb-storage.rules @@ -1 +1,4 @@ -ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_USAGE}=="filesystem", RUN+="/usr/bin/sudo /usr/local/bin/usb-storage-action %E{DEVNAME}" \ No newline at end of file +# 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}" -- 2.26.2.windows.1 From 7d50f208b7ed3dfa3166d6399acbf965dd262183 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Wed, 22 May 2024 20:34:26 +0330 Subject: [PATCH 07/13] Refactor usb-storage-action To have both add and remove modes --- src/scripts/utils/usb/usb-storage-action | 41 ++++++++++++++++++------ 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/scripts/utils/usb/usb-storage-action b/src/scripts/utils/usb/usb-storage-action index 732403e..c7a03c1 100644 --- a/src/scripts/utils/usb/usb-storage-action +++ b/src/scripts/utils/usb/usb-storage-action @@ -1,16 +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=$1 +DEVICE_NAME=$2 + +SOCKET_PATH="/tmp/usb-Sono-Socket.socket" -# Mount the device -udisksctl mount -b "$DEVICE_NAME" +if [ "$ACTION" == "add" ]; then + # Mount the device + udisksctl mount -b "$DEVICE_NAME" -# Check if the mount was successful -if [ $? -eq 0 ]; then - # Define the socket path - SOCKET_PATH="/tmp/usb-Sono-Socket.socket" + # 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" - # Write a message to the socket - echo "USB storage device connected" | socat - UNIX-CONNECT:$SOCKET_PATH - echo "USB storage device connected" >> /tmp/tmp.log + # 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 -- 2.26.2.windows.1 From 33dda07ef976563518b8d8c422d6895eaa03845b Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Tue, 28 May 2024 13:36:12 +0330 Subject: [PATCH 08/13] Add rules of dvd --- src/rules/90-dvd-storage.rules | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/rules/90-dvd-storage.rules 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}" -- 2.26.2.windows.1 From 1bf1b3678a74720931fafa0dc2b34097680ee96b Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Tue, 28 May 2024 13:39:09 +0330 Subject: [PATCH 09/13] Add socket file of dvd --- src/scripts/utils/dvd/dvd-socket-sono.socket | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/scripts/utils/dvd/dvd-socket-sono.socket 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 -- 2.26.2.windows.1 From 6427fbcba5b4e898769ca0ffde7ff218a51e8c9f Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Tue, 28 May 2024 13:40:25 +0330 Subject: [PATCH 10/13] Add service file of dvd --- src/scripts/utils/dvd/dvd-socket-sono.service | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/scripts/utils/dvd/dvd-socket-sono.service 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 -- 2.26.2.windows.1 From a1109e95dc29fa879acc27ec466d7e098f12df6d Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Tue, 28 May 2024 13:41:39 +0330 Subject: [PATCH 11/13] Add script of adding dvd --- src/scripts/utils/dvd/addDvdScript.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/scripts/utils/dvd/addDvdScript.sh 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 -- 2.26.2.windows.1 From d450f59065a1b0f59c70048392f753cd2b4f2fab Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Tue, 28 May 2024 13:42:44 +0330 Subject: [PATCH 12/13] Add dvd action file --- src/scripts/utils/dvd/dvd-storage-action | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/scripts/utils/dvd/dvd-storage-action 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 -- 2.26.2.windows.1 From c9f3f184678e3eef953d082a70293bfc55dcbaf2 Mon Sep 17 00:00:00 2001 From: Mohammad_Faraji Date: Sun, 7 Jul 2024 17:18:17 +0330 Subject: [PATCH 13/13] Update setup.sh Add some commands for usb and dvd script and rules --- setup.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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' -- 2.26.2.windows.1