autoNAS / scripts / autonas-disk-handler.sh
Newer Older
5b5a565 3 months ago History
82 lines | 3.233kb
Bogdan Timofte authored 3 months ago
1
#!/bin/bash
2

            
3
# AutoNAS Manager - System integration for udev/systemd events
4
# This script is called by udev rules when disks are attached/detached
5
# Uses autonas-core.sh for all business logic
6

            
7
# Load the AutoNAS core library
8
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
source "${SCRIPT_DIR}/autonas-core.sh" || {
10
    echo "Error: Cannot load AutoNAS core library" >&2
11
    exit 1
12
}
13

            
14
# Set LOG_TAG for this component
15
LOG_TAG="autonas-disk-handler"
16

            
17
# Main script logic
18
case "$1" in
19
    "attach")
20
        if [ -z "$2" ]; then
21
            log_message "Error: UUID required for attach operation" "err"
22
            exit 1
23
        fi
24
        log_message "AutoNAS attach operation started for UUID: $2" "info"
25
        handle_attach "$2"
26
        ;;
27
    "attach-deferred")
28
        if [ -z "$2" ]; then
29
            log_message "Error: UUID required for deferred attach operation" "err"
30
            exit 1
31
        fi
32
        log_message "AutoNAS deferred attach operation started for UUID: $2" "info"
33
        handle_attach "$2"
34
        ;;
35
    "detach")
36
        if [ -z "$2" ]; then
37
            log_message "Error: UUID or disk name required for detach operation" "err"
38
            exit 1
39
        fi
40
        log_message "AutoNAS detach operation started for identifier: $2" "info"
41

            
42
        # Check if identifier is UUID (contains hyphens and matches UUID pattern) or name
43
        if [[ "$2" =~ ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$ ]] || [[ "$2" =~ ^[0-9a-fA-F]{4}-[0-9a-fA-F]{4}$ ]]; then
44
            # It's a UUID - use directly
45
            handle_detach "$2"
46
        else
47
            # It's a name - get UUID from config
48
            local config_line
49
            config_line=$(get_config_by_name "$2" 2>/dev/null)
50
            if [ -n "$config_line" ]; then
51
                local uuid=$(echo "$config_line" | cut -d: -f1)
52
                handle_detach "$uuid"
53
            else
54
                log_message "No configuration found for name: $2 - detach skipped" "info"
55
                log_message "This may be normal if the disk was not configured for AutoNAS" "info"
56
                exit 0
57
            fi
58
        fi
59
        ;;
60
    "import")
61
        if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
62
            log_message "Error: UUID, mount_point, and destination required for import operation" "err"
63
            exit 1
64
        fi
65
        log_message "AutoNAS background import operation started for UUID: $2" "info"
66
        run_background_import "$2" "$3" "$4" "$5"
67
        ;;
68
    "reload")
69
        handle_reload
70
        ;;
71
    *)
72
        echo "Usage: $0 {attach|attach-deferred|detach|import|reload} <UUID|name> [args...]"
73
        echo "  attach <UUID>         - Attach disk by UUID (direct)"
74
        echo "  attach-deferred <UUID> - Attach disk by UUID (via systemd)"
75
        echo "  detach <UUID>         - Detach disk by UUID (via udev)"
76
        echo "  detach <name>         - Detach disk by name (for manual/clean unmount)"
77
        echo "  import <UUID> <mount> <dest> [script] - Run background camera import"
78
        echo "This script is typically called by udev rules or systemd"
79
        log_message "Invalid arguments provided: $*" "err"
80
        exit 1
81
        ;;
82
esac