Browse Source

Merge pull request 'Add usb-storage rules' (#6) from usb-socket into master

Reviewed-on: http://194.5.205.38:3000/hatam/sono-os/pulls/6
master V0.1.0
Ali Hatami Tajik 5 months ago
parent
commit
2ed7f2bbf3
  1. 25
      setup.sh
  2. 5
      src/rules/90-dvd-storage.rules
  3. 4
      src/rules/90-usb-storage.rules
  4. 3
      src/scripts/utils/dvd/addDvdScript.sh
  5. 10
      src/scripts/utils/dvd/dvd-socket-sono.service
  6. 9
      src/scripts/utils/dvd/dvd-socket-sono.socket
  7. 93
      src/scripts/utils/dvd/dvd-storage-action
  8. 3
      src/scripts/utils/usb/addUsbScript.sh
  9. 10
      src/scripts/utils/usb/usb-socket-sono.service
  10. 9
      src/scripts/utils/usb/usb-socket-sono.socket
  11. 37
      src/scripts/utils/usb/usb-storage-action

25
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'

5
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}"

4
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}"

3
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

10
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

9
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

93
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

3
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

10
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

9
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

37
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
Loading…
Cancel
Save