autoNAS / scripts / autonas-udev-wrapper.sh
Newer Older
5b5a565 3 months ago History
65 lines | 1.891kb
Bogdan Timofte authored 3 months ago
1
#!/bin/bash
2

            
3
# AutoNAS udev wrapper - runs the manager script in background to avoid udev timeout
4
# Usage: autonas-udev-wrapper.sh attach <uuid>
5

            
6
# Global configuration
7
LOG_TAG="autonas-wrapper"
8

            
9
# Set comprehensive environment for udev context
10
export PATH="/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin"
11
export HOME="/root"
12
export USER="root"
13
export LOGNAME="root"
14
export SHELL="/bin/bash"
15

            
16
# Set proper umask for mount operations
17
umask 022
18

            
19
UUID="$2"
20
ACTION="$1"
21

            
22
# Validate arguments
23
if [ -z "$ACTION" ] || [ -z "$UUID" ]; then
24
    logger -p local0.err -t "$LOG_TAG" "Invalid arguments: ACTION='$ACTION', UUID='$UUID'"
25
    exit 1
26
fi
27

            
28
# Log the call with device info
29
logger -p local0.info -t "$LOG_TAG" "Called with action: $ACTION, UUID: $UUID"
30

            
31
# Get device info for logging
32
if [ -n "$UUID" ]; then
33
    DEVICE_PATH="/dev/disk/by-uuid/$UUID"
34
    if [ -e "$DEVICE_PATH" ]; then
35
        ACTUAL_DEVICE=$(readlink -f "$DEVICE_PATH" 2>/dev/null)
36
        logger -p local0.info -t "$LOG_TAG" "Device path: $ACTUAL_DEVICE"
37
    fi
38
fi
39

            
40
# Run the actual script through systemd for reliable execution
41
(
42
    # Additional delay for device stabilization
43
    sleep 3
44

            
45
    # Ensure we're running as root with full privileges
46
    if [ "$(id -u)" != "0" ]; then
47
        logger -p local0.err -t "$LOG_TAG" "Not running as root, UID: $(id -u)"
48
        exit 1
49
    fi
50

            
51
    # Use systemd service directly for reliable execution
52
    if [ "$ACTION" = "attach" ]; then
53
        systemctl start "autonas-attach@${UUID}.service"
54
        logger -p local0.info -t "$LOG_TAG" "Started systemd service for UUID: $UUID"
55
    else
56
        logger -p local0.warning -t "$LOG_TAG" "Detach operations not supported through systemd wrapper"
57
    fi
58

            
59
) &
60

            
61
# Log background job started
62
logger -p local0.info -t "$LOG_TAG" "Started background job for $ACTION $UUID (PID: $!)"
63

            
64
# Exit immediately so udev doesn't wait
65
exit 0