autoNAS / scripts / autonas-disk-handler.sh
Newer Older
d426b0e 3 months ago History
69 lines | 2.545kb
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
Bogdan Timofte authored 3 months ago
37
            log_message "Error: UUID, disk name, or mount point required for detach operation" "err"
Bogdan Timofte authored 3 months ago
38
            exit 1
39
        fi
40
        log_message "AutoNAS detach operation started for identifier: $2" "info"
Bogdan Timofte authored 3 months ago
41

            
42
        if ! handle_detach "$2"; then
43
            log_message "No configuration found for identifier: $2 - detach skipped" "info"
44
            log_message "This may be normal if the disk was not configured for AutoNAS" "info"
45
            exit 0
Bogdan Timofte authored 3 months ago
46
        fi
47
        ;;
48
    "import")
49
        if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
50
            log_message "Error: UUID, mount_point, and destination required for import operation" "err"
51
            exit 1
52
        fi
53
        log_message "AutoNAS background import operation started for UUID: $2" "info"
54
        run_background_import "$2" "$3" "$4" "$5"
55
        ;;
56
    "reload")
57
        handle_reload
58
        ;;
59
    *)
Bogdan Timofte authored 3 months ago
60
        echo "Usage: $0 {attach|attach-deferred|detach|import|reload} <UUID|name|mount_point> [args...]"
Bogdan Timofte authored 3 months ago
61
        echo "  attach <UUID>         - Attach disk by UUID (direct)"
62
        echo "  attach-deferred <UUID> - Attach disk by UUID (via systemd)"
Bogdan Timofte authored 3 months ago
63
        echo "  detach <UUID|name|mount_point> - Detach configured disk"
Bogdan Timofte authored 3 months ago
64
        echo "  import <UUID> <mount> <dest> [script] - Run background camera import"
65
        echo "This script is typically called by udev rules or systemd"
66
        log_message "Invalid arguments provided: $*" "err"
67
        exit 1
68
        ;;
Bogdan Timofte authored 3 months ago
69
esac