summarylogtreecommitdiffstats
path: root/ethoscope-device.install
blob: 7bbf3daa2633fe35711aba216687d87e59ab9619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
## arg 1:  the new package version
#pre_install() {
	# do something here
#}

# arg 1:  the new package version
post_install() {
    # Detect Pi model for hardware-specific configuration
    detect_pi_model
    
    # Set up ethoscope user account
    setup_ethoscope_user
    
    # Configure git repository
    configure_git_repository
    
    # Install Python package
    install_python_package
    
    # Configure system identity
    configure_system_identity
    
    # Configure network
    configure_network
    
    # Configure Wi-Fi
    configure_wifi
    
    # Set up MariaDB
    setup_mariadb
    
    # Configure MariaDB
    configure_mariadb
    
    # Configure time sync
    configure_time_sync
    
    # Configure hardware
    configure_hardware
    
    # Configure system settings
    configure_system_settings
    
    # Enable services
    enable_services
    
    echo "Please reboot this PI now."
}

detect_pi_model() {
    # Get the hardware model from /proc/cpuinfo
    local revision=$(grep 'Revision' /proc/cpuinfo | awk '{print $3}')
    
    # Determine Pi model for filename
    if [[ "$revision" == "a01041" || "$revision" == "a21041" || "$revision" == "a22042" ]]; then
        PI_MODEL="pi2"
    elif [[ "$revision" == "a02082" || "$revision" == "a22082" || "$revision" == "a32082" || "$revision" == "a020d3" ]]; then
        PI_MODEL="pi3"
    elif [[ "$revision" =~ ^[bc][0-9a-f]{5}$ ]]; then
        PI_MODEL="pi4"
    elif [[ "$revision" =~ ^d[0-9a-f]{5}$ ]]; then
        PI_MODEL="pi5"
    else
        PI_MODEL="pi"
    fi
    
    echo "Detected Raspberry Pi model: $PI_MODEL (revision: $revision)"
}

setup_ethoscope_user() {
    echo "Setting up ethoscope user account..."
    if id "ethoscope" &>/dev/null; then
        echo "User 'ethoscope' already exists, skipping user creation"
    else
        echo "Creating ethoscope user account..."
        useradd -m ethoscope
    fi
    echo -e "ethoscope\nethoscope" | passwd ethoscope
    usermod -a -G root ethoscope
}

configure_git_repository() {
    echo "Configuring git repository..."
    cd /opt/ethoscope
    git remote set-url origin git://node/ethoscope.git
    git checkout dev
    git config --global --add safe.directory /opt/ethoscope
}

install_python_package() {
    echo "Installing Python package..."
    cd /opt/ethoscope/src/ethoscope
    pip install -e . --break-system-packages
}

configure_system_identity() {
    echo "Setting up default machine identity (ETHOSCOPE_000)..."
    echo "ETHOSCOPE_000" > /etc/machine-name
    echo "ETHOSCOPE_000" > /etc/hostname

    echo "Configuring login prompt with network information..."
    echo 'Ethoscope Linux \r  (\n) (\l)' > /etc/issue
    echo 'Ethernet IP: \4{eth0}' >> /etc/issue
    echo 'WIFI IP: \4{wlan0}' >> /etc/issue
    echo 'Time on Device: \d \t' >> /etc/issue

    # Create ethoclient command
    echo "Creating ethoclient command line tool..."
    echo $'#!/bin/env bash\npython /opt/ethoscope/src/ethoscope/scripts/ethoclient.py $@' > /usr/bin/ethoclient
    chmod +x /usr/bin/ethoclient
    
    # Create a timestamp for this installation
    echo $(date +%Y%m%d)_ethoscope_${PI_MODEL}.img > /etc/sdimagename
}

configure_network() {
    echo "Configuring network interfaces (ethernet + WiFi)..."
    
    # Create wired network config for eth0
    cat > /etc/systemd/network/20-wired.network << 'EOF'
[Match]
Name=eth0

[Network]
DHCP=yes
LinkLocalAddressing=yes

[DHCPv4]
RouteMetric=10
UseDNS=yes
EOF

    # WiFi configuration  
    cat > /etc/systemd/network/25-wireless.network << 'EOF'
[Match]
Name=wlan0

[Network]
DHCP=yes
LinkLocalAddressing=yes

[DHCPv4]
RouteMetric=20
UseDNS=yes
EOF

    # Enable systemd-networkd
    systemctl enable systemd-networkd
    systemctl disable systemd-networkd-wait-online  # Prevent boot hangs
    
    # Ensure interfaces are up
    echo "Bringing up network interfaces..."
    ip link set eth0 up || true
    ip link set wlan0 up || true
}

configure_wifi() {
    echo "Configuring Wi-Fi..."
    
    # Unblock Wi-Fi if rfkill is blocking it
    if command -v rfkill >/dev/null 2>&1; then
        echo "Unblocking Wi-Fi with rfkill..."
        rfkill unblock wifi
        rfkill unblock all
    fi

    wpa_passphrase ETHOSCOPE_WIFI ETHOSCOPE_1234 > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
    systemctl enable wpa_supplicant
    systemctl enable wpa_supplicant@wlan0.service
}

setup_mariadb() {
    echo "Setting up MariaDB database..."
    
    # Initialize MariaDB data directory if not already done
    if [ ! -d "/var/lib/mysql/mysql" ]; then
        echo "Initializing MariaDB data directory..."
        mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
    fi

    # Ensure proper ownership
    chown -R mysql:mysql /var/lib/mysql

    # Start MariaDB service
    systemctl start mysqld.service

    # Wait for MariaDB to be ready
    echo "Waiting for MariaDB to start..."
    for i in {1..30}; do
        if mysqladmin ping >/dev/null 2>&1; then
            echo "MariaDB is ready"
            break
        fi
        if [ $i -eq 30 ]; then
            echo "ERROR: MariaDB failed to start within 30 seconds"
            exit 1
        fi
        sleep 1
    done

    # Set up ethoscope database user
    echo "Creating ethoscope database user..."
    mysql -u root <<EOF
-- Create ethoscope user for local and network connections
CREATE USER IF NOT EXISTS 'ethoscope'@'localhost' IDENTIFIED BY 'ethoscope';
CREATE USER IF NOT EXISTS 'ethoscope'@'%' IDENTIFIED BY 'ethoscope';

-- Grant necessary permissions including RELOAD and GRANT OPTION
GRANT ALL PRIVILEGES ON *.* TO 'ethoscope'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'ethoscope'@'%' WITH GRANT OPTION;

-- Flush privileges to ensure changes take effect
FLUSH PRIVILEGES;
EOF

    if [ $? -eq 0 ]; then
        echo "Database user created successfully"
    else
        echo "ERROR: Failed to create database user"
        exit 1
    fi
}

configure_mariadb() {
    echo "Configuring MariaDB for ethoscope use on $PI_MODEL..."
    
    # Set memory limits based on Pi model
    local buffer_pool_size="64M"
    local log_file_size="16M"
    local key_buffer_size="16M"
    local max_connections="50"
    
    if [[ "$PI_MODEL" == "pi2" ]]; then
        buffer_pool_size="32M"
        log_file_size="8M"
        key_buffer_size="8M"
        max_connections="25"
    elif [[ "$PI_MODEL" == "pi3" ]]; then
        buffer_pool_size="64M"
        log_file_size="16M"
        key_buffer_size="16M"
        max_connections="40"
    elif [[ "$PI_MODEL" == "pi4" ]]; then
        buffer_pool_size="128M"
        log_file_size="32M"
        key_buffer_size="32M"
        max_connections="75"
    elif [[ "$PI_MODEL" == "pi5" ]]; then
        buffer_pool_size="256M"
        log_file_size="64M"
        key_buffer_size="64M"
        max_connections="100"
    fi
    
    # Create MariaDB configuration
    echo "Creating MariaDB configuration..."
    cat > /etc/my.cnf.d/ethoscope.cnf <<EOF
[server]
# Binary logging configuration for replication/backup
log-bin          = mysql-bin
binlog_format    = mixed
expire_logs_days = 10
max_binlog_size  = 100M

# Network configuration - allow connections from ethoscope network
bind-address     = 0.0.0.0

# Performance optimizations for $PI_MODEL
innodb_buffer_pool_size = $buffer_pool_size
innodb_log_file_size = $log_file_size
key_buffer_size = $key_buffer_size
max_connections = $max_connections

# Reduce disk I/O for SD card longevity
innodb_flush_log_at_trx_commit = 2
sync_binlog = 0
EOF
    
    echo "MariaDB configuration written successfully for $PI_MODEL"
}

configure_time_sync() {
    echo "Configuring NTP time synchronization with node..."
    echo 'server node' > /etc/ntp.conf
    echo 'server 127.127.1.0' >> /etc/ntp.conf
    echo 'fudge 127.127.1.0 stratum 10' >> /etc/ntp.conf
    echo 'restrict default kod limited nomodify nopeer noquery notrap' >> /etc/ntp.conf
    echo 'restrict 127.0.0.1' >> /etc/ntp.conf
    echo 'restrict ::1' >> /etc/ntp.conf
    echo 'driftfile /var/lib/ntp/ntp.drift' >> /etc/ntp.conf
}

configure_hardware() {
    echo "Configuring Raspberry Pi $PI_MODEL hardware..."
    
    # Common settings for all Pi versions
    echo "Disabling Bluetooth (not needed for ethoscope)..."
    echo 'dtoverlay=disable-bt' >> /boot/config.txt

    echo "Enable default HDMI output"
    echo 'hdmi_force_hotplug=1' >> /boot/config.txt

    echo "Enabling I2C support for hardware interfaces..."
    echo 'dtparam=i2c_arm=on' >> /boot/config.txt
    echo 'i2c-dev' >> /etc/modules-load.d/raspberrypi.conf

    # Camera configuration based on Pi model
    echo "Configuring camera for $PI_MODEL..."
    echo 'disable_camera_led=1' >> /boot/config.txt
    
    if [[ "$PI_MODEL" == "pi2" || "$PI_MODEL" == "pi3" ]]; then
        echo "Configuring legacy camera (Pi 2/3)..."
        echo 'start_file=start_x.elf' >> /boot/config.txt
        echo 'fixup_file=fixup_x.dat' >> /boot/config.txt
        echo 'gpu_mem=256' >> /boot/config.txt
        echo 'cma_lwm=' >> /boot/config.txt
        echo 'cma_hwm=' >> /boot/config.txt
        echo 'cma_offline_start=' >> /boot/config.txt

        # https://github.com/raspberrypi/firmware/issues/1167
        echo 'awb_auto_is_greyworld=1' >> /boot/config.txt

        echo 'Loading bcm2835 module for legacy camera'
        echo 'bcm2835-v4l2' > /etc/modules-load.d/picamera.conf
        
    elif [[ "$PI_MODEL" == "pi4" ]]; then
        echo "Configuring camera for Pi 4..."
        echo 'dtoverlay=vc4-kms-v3d' >> /boot/config.txt
        echo 'gpu_mem=256' >> /boot/config.txt
        echo 'dtoverlay=imx219' >> /boot/config.txt
        
    elif [[ "$PI_MODEL" == "pi5" ]]; then
        echo "Configuring camera for Pi 5..."
        echo 'dtoverlay=vc4-kms-v3d' >> /boot/config.txt
        echo 'gpu_mem=256' >> /boot/config.txt
        echo 'dtoverlay=imx219' >> /boot/config.txt
        
    else
        echo "Unknown Pi model, using basic camera configuration..."
        echo 'gpu_mem=128' >> /boot/config.txt
        echo 'dtoverlay=imx219' >> /boot/config.txt
    fi
    
    # Enable camera interface for all models
    echo 'camera_auto_detect=1' >> /boot/config.txt
    echo 'dtparam=camera=on' >> /boot/config.txt
}

configure_system_settings() {
    echo "Configuring system settings..."
    
    # Limit journal log space
    echo "Limiting systemd journal log space to 250MB..."
    echo 'SystemMaxUse=250MB' >> /etc/systemd/journald.conf

    # Generate locale
    echo "Generating en_GB.UTF-8 locale..."
    echo "en_GB.UTF-8 UTF-8" >> /etc/locale.gen
    locale-gen

    # Configure remote journal upload
    echo "Configuring remote journal upload..."
    echo $'[Upload]\nURL=http://node:19532\n' > /etc/systemd/journal-upload.conf
}

enable_services() {
    echo "Enabling ethoscope device services..."
    systemctl enable ethoscope_device.service ethoscope_listener.service ethoscope_update.service ethoscope_GPIO_listener.service
    
    echo "Enabling system services..."
    systemctl enable ntpd.service mysqld.service sshd.service avahi-daemon.service
}

## arg 1:  the new package version
## arg 2:  the old package version
#pre_upgrade() {
    # do something here
#}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    echo "Updating git repository configuration..."
    cd /opt/ethoscope
    git remote set-url origin git://node/ethoscope.git
    git checkout dev
    git config --global --add safe.directory /opt/ethoscope
    
    # Reinstall Python package
    cd /opt/ethoscope/src/ethoscope
    pip install -e . --break-system-packages
}

# arg 1:  the old package version
pre_remove() {
    echo "Disabling systemd service files..."
    systemctl disable ethoscope_device.service ethoscope_listener.service ethoscope_update.service ethoscope_GPIO_listener.service
}

## arg 1:  the old package version
#post_remove() {
    # do something here
#}