autoNAS / scripts / autonas-disk-handler.sh
Newer Older
78 lines | 3.078kb
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"
Bogdan Timofte authored 2 weeks ago
33
        # Ignore unconfigured disks discovered by udev without marking the
34
        # systemd unit as failed. Boot disks and random removable media should
35
        # not create failed autonas-attach@... units just because AutoNAS does
36
        # not manage them.
37
        if ! resolve_config_identifier "$2" >/dev/null; then
38
            log_message "No configuration found for identifier: $2 - deferred attach skipped" "info"
39
            log_message "This is normal for disks not managed by AutoNAS" "info"
40
            exit 0
41
        fi
Bogdan Timofte authored 3 months ago
42
        handle_attach "$2"
43
        ;;
44
    "detach")
45
        if [ -z "$2" ]; then
Bogdan Timofte authored 3 months ago
46
            log_message "Error: UUID, disk name, or mount point required for detach operation" "err"
Bogdan Timofte authored 3 months ago
47
            exit 1
48
        fi
49
        log_message "AutoNAS detach operation started for identifier: $2" "info"
Bogdan Timofte authored 3 months ago
50

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