From 677595835ab2ade4540c5c0d32ad4292267c38b1 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 24 Jun 2025 10:32:48 +0200 Subject: [PATCH] update --- installation.sh | 315 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 246 insertions(+), 69 deletions(-) diff --git a/installation.sh b/installation.sh index f57ca10..7d478af 100644 --- a/installation.sh +++ b/installation.sh @@ -158,49 +158,6 @@ clone_repository() { print_success "Repository permissions set" } -# Setup directory structure -setup_directories() { - print_step "Setting up additional directories and files..." - - # Create additional directories - local dirs=( - "sqlite" - "logs" - "matrix/input" - ) - - for dir in "${dirs[@]}"; do - sudo mkdir -p "/var/www/moduleair_pro_4g/$dir" - print_success "Created directory: $dir" - done - - # Create required log files - local files=( - "logs/app.log" - "logs/loop.log" - "matrix/input_NPM.txt" - "matrix/input_MHZ16.txt" - "wifi_list.csv" - ) - - for file in "${files[@]}"; do - sudo touch "/var/www/moduleair_pro_4g/$file" - print_success "Created file: $file" - done - - # Copy config template if it exists - if [ -f "/var/www/moduleair_pro_4g/config.json.dist" ]; then - sudo cp /var/www/moduleair_pro_4g/config.json.dist /var/www/moduleair_pro_4g/config.json - print_success "Configuration file created from template" - else - print_warning "config.json.dist not found - skipping config file creation" - fi - - # Set proper permissions - sudo chown -R www-data:www-data /var/www/moduleair_pro_4g - sudo chmod -R 755 /var/www/moduleair_pro_4g - print_success "Directory permissions updated" -} # Configure Apache configure_apache() { @@ -223,6 +180,70 @@ configure_apache() { print_success "Apache configured and restarted" } +# System optimizations (disable audio, isolate CPU) +system_optimizations() { + print_step "Applying system optimizations..." + + # Find config.txt location + local config_file="/boot/firmware/config.txt" + if [ ! -f "$config_file" ]; then + config_file="/boot/config.txt" + fi + + if [ ! -f "$config_file" ]; then + print_error "Could not find config.txt file" + return 1 + fi + + # Disable onboard audio + print_status "Disabling onboard audio..." + if grep -q "^dtparam=audio=" "$config_file"; then + # Replace existing audio parameter + sudo sed -i 's/^dtparam=audio=.*/dtparam=audio=off/' "$config_file" + print_success "Audio parameter updated to off" + else + # Add audio parameter if not exists + echo "dtparam=audio=off" | sudo tee -a "$config_file" > /dev/null + print_success "Audio disabled in config.txt" + fi + + # Blacklist audio module + print_status "Blacklisting audio module..." + echo "blacklist snd_bcm2835" | sudo tee /etc/modprobe.d/blacklist.conf > /dev/null + print_success "Audio module blacklisted" + + # Find cmdline.txt location + local cmdline_file="/boot/firmware/cmdline.txt" + if [ ! -f "$cmdline_file" ]; then + cmdline_file="/boot/cmdline.txt" + fi + + if [ ! -f "$cmdline_file" ]; then + print_error "Could not find cmdline.txt file" + return 1 + fi + + # Add CPU isolation + print_status "Setting up CPU isolation..." + if ! grep -q "isolcpus=3" "$cmdline_file"; then + # Read current cmdline and append isolcpus + local current_cmdline=$(cat "$cmdline_file") + echo "${current_cmdline} isolcpus=3" | sudo tee "$cmdline_file" > /dev/null + print_success "CPU core 3 isolated" + else + print_warning "CPU isolation already configured" + fi + + # Check if audio module is currently loaded + if lsmod | grep -q snd_bcm2835; then + print_warning "Audio module is currently loaded. It will be disabled after reboot." + else + print_success "Audio module is not loaded" + fi + + print_success "System optimizations completed" +} + # Enable hardware interfaces enable_hardware() { print_step "Enabling hardware interfaces..." @@ -284,6 +305,58 @@ enable_hardware() { print_success "Hardware interfaces configuration completed" } +# Setup sudo authorization +setup_sudo_authorization() { + print_step "Setting up sudo authorization..." + + SUDOERS_FILE="/etc/sudoers" + + # First, fix any existing syntax errors + if sudo visudo -c 2>&1 | grep -q "syntax error"; then + print_warning "Syntax error detected in sudoers file. Attempting to fix..." + # Remove the problematic line if it exists + sudo sed -i '/www-data ALL=(ALL) NOPASSWD: \/usr\/bin\/python3 \* www-data/d' "$SUDOERS_FILE" + fi + + # Add proper sudo rules (each on a separate line) + if ! sudo grep -q "/usr/bin/nmcli" "$SUDOERS_FILE"; then + # Create a temporary file with the new rules + cat < /dev/null +# ModuleAir Pro 4G sudo rules +ALL ALL=(ALL) NOPASSWD: /usr/bin/nmcli, /usr/sbin/reboot +www-data ALL=(ALL) NOPASSWD: /usr/bin/git pull +www-data ALL=(ALL) NOPASSWD: /usr/bin/ssh +www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 * +www-data ALL=(ALL) NOPASSWD: /bin/systemctl * +www-data ALL=(ALL) NOPASSWD: /var/www/moduleair_pro_4g/* +EOF + + # Validate the temporary file + if sudo visudo -c -f /tmp/sudoers_additions; then + # Append to sudoers if valid + sudo cat /tmp/sudoers_additions >> "$SUDOERS_FILE" + print_success "Sudo authorization added" + else + print_error "Failed to add sudo rules - syntax validation failed" + sudo rm -f /tmp/sudoers_additions + return 1 + fi + + # Clean up + sudo rm -f /tmp/sudoers_additions + else + print_warning "Sudo authorization already set. Skipping." + fi + + # Validate sudoers file after changes + if ! sudo visudo -c; then + print_error "Sudoers file has syntax errors! Please fix manually with 'sudo visudo'" + return 1 + fi + + print_success "Sudo authorization configured successfully" +} + # Set device permissions set_permissions() { print_step "Setting device permissions..." @@ -302,36 +375,114 @@ set_permissions() { print_success "Device permissions set" } -# Check matrix binaries -check_matrix_binaries() { - print_step "Checking matrix display binaries..." +# Setup RTC save to DB service +setup_rtc_service() { + print_step "Setting up RTC save to DB service..." - local programs=( - "screenNetwork/network_status" - "screenSensors/displayAll4_v2" - "screenSensors/displayCO2_PM_Network" - "screenSensors/blank_screen" - "welcomeScreen/welcome_screen" - ) + # Create the service file + cat << 'EOF' | sudo tee /etc/systemd/system/rtc_save_to_db.service > /dev/null +[Unit] +Description=RTC Save to DB Script +After=network.target + +[Service] +ExecStart=/usr/bin/python3 /var/www/moduleair_pro_4g/RTC/save_to_db.py +Restart=always +RestartSec=1 +User=root +WorkingDirectory=/var/www/moduleair_pro_4g +StandardOutput=append:/var/www/moduleair_pro_4g/logs/rtc_save_to_db.log +StandardError=append:/var/www/moduleair_pro_4g/logs/rtc_save_to_db_errors.log + +[Install] +WantedBy=multi-user.target +EOF - for program in "${programs[@]}"; do - local binary_file="/var/www/moduleair_pro_4g/matrix/${program}" - - if [ -f "$binary_file" ] && [ -x "$binary_file" ]; then - print_success "Matrix binary found: $program" - else - print_warning "Matrix binary missing or not executable: $program" - fi - done + print_success "RTC service file created" - # Check matrix library - if [ -f "/var/www/moduleair_pro_4g/matrix/lib/librgbmatrix.a" ]; then - print_success "Matrix library found: librgbmatrix.a" + # Create logs directory if it doesn't exist + sudo mkdir -p /var/www/moduleair_pro_4g/logs + sudo chown -R www-data:www-data /var/www/moduleair_pro_4g/logs + print_status "Logs directory created/verified" + + # Reload systemd daemon + print_status "Reloading systemd daemon..." + sudo systemctl daemon-reload + + # Enable the service + print_status "Enabling RTC service..." + if sudo systemctl enable rtc_save_to_db.service; then + print_success "RTC service enabled" else - print_warning "Matrix library missing: librgbmatrix.a" + print_error "Failed to enable RTC service" + return 1 fi - print_success "Matrix binaries check completed" + # Start the service + print_status "Starting RTC service..." + if sudo systemctl start rtc_save_to_db.service; then + print_success "RTC service started" + else + print_warning "Failed to start RTC service - may need reboot" + fi + + # Check service status + print_status "Checking RTC service status..." + if systemctl is-active --quiet rtc_save_to_db.service; then + print_success "RTC service is running" + else + print_warning "RTC service is not running - will start after reboot" + fi + + return 0 +} + +# Setup cron jobs +setup_cron_jobs() { + print_step "Setting up cron jobs..." + + if [ -f "/var/www/moduleair_pro_4g/cron_jobs" ]; then + if sudo crontab "/var/www/moduleair_pro_4g/cron_jobs"; then + print_success "Cron jobs set up successfully" + else + print_error "Failed to set up cron jobs" + return 1 + fi + else + print_warning "Cron jobs file not found. Skipping." + fi +} + +# Create databases +create_databases() { + print_step "Creating databases..." + + if [ -f "/var/www/moduleair_pro_4g/sqlite/create_db.py" ]; then + if sudo /usr/bin/python3 "/var/www/moduleair_pro_4g/sqlite/create_db.py"; then + print_success "Databases created successfully" + else + print_error "Failed to create databases" + return 1 + fi + else + print_warning "Database creation script not found" + fi +} + +# Set configuration +set_configuration() { + print_step "Setting configuration..." + + if [ -f "/var/www/moduleair_pro_4g/sqlite/set_config.py" ]; then + if sudo /usr/bin/python3 "/var/www/moduleair_pro_4g/sqlite/set_config.py"; then + print_success "Configuration set successfully" + else + print_error "Failed to set configuration" + return 1 + fi + else + print_warning "Configuration script not found" + fi } # Setup systemd services @@ -404,6 +555,7 @@ final_status() { "moduleair-sara-data.timer" "moduleair-bme-data.timer" "moduleair-boot.service" + "rtc_save_to_db.service" ) for service in "${services[@]}"; do @@ -421,9 +573,15 @@ final_status() { echo -e "${GREEN}✓${NC} Directory structure created" echo -e "${GREEN}✓${NC} Apache web server configured" echo -e "${GREEN}✓${NC} Hardware interfaces enabled" + echo -e "${GREEN}✓${NC} System optimizations applied" echo -e "${GREEN}✓${NC} Device permissions set" + echo -e "${GREEN}✓${NC} Sudo authorization configured" + echo -e "${GREEN}✓${NC} Databases created" + echo -e "${GREEN}✓${NC} Configuration set" + echo -e "${GREEN}✓${NC} Cron jobs configured" echo -e "${GREEN}✓${NC} Matrix binaries verified" echo -e "${GREEN}✓${NC} Systemd services configured" + echo -e "${GREEN}✓${NC} RTC save to DB service installed" echo -e "${GREEN}✓${NC} Boot scripts created" print_header "NEXT STEPS" @@ -435,6 +593,7 @@ final_status() { echo -e "" echo -e "${YELLOW}3.${NC} Check service status:" echo -e " ${CYAN}systemctl list-timers | grep moduleair${NC}" + echo -e " ${CYAN}systemctl status rtc_save_to_db.service${NC}" echo -e "" echo -e "${YELLOW}4.${NC} Test RTC module (after reboot):" echo -e " ${CYAN}python3 /var/www/moduleair_pro_4g/RTC/read.py${NC}" @@ -444,8 +603,19 @@ final_status() { echo -e "" echo -e "${YELLOW}6.${NC} Check logs if needed:" echo -e " ${CYAN}tail -f /var/www/moduleair_pro_4g/logs/*.log${NC}" + echo -e " ${CYAN}tail -f /var/www/moduleair_pro_4g/logs/rtc_save_to_db.log${NC}" + echo -e "" + echo -e "${YELLOW}7.${NC} Check cron jobs:" + echo -e " ${CYAN}sudo crontab -l${NC}" + echo -e "" + echo -e "${YELLOW}8.${NC} Verify audio is disabled (after reboot):" + echo -e " ${CYAN}lsmod | grep snd_bcm2835${NC} (should return nothing)" + echo -e "" + echo -e "${YELLOW}9.${NC} Verify CPU isolation (after reboot):" + echo -e " ${CYAN}cat /proc/cmdline | grep isolcpus${NC}" echo -e "" print_warning "I2C and UART devices will NOT work until after reboot!" + print_warning "Audio and CPU isolation changes require reboot to take effect!" } # Main installation function @@ -459,12 +629,19 @@ main() { install_dependencies install_python_packages clone_repository - setup_directories configure_apache enable_hardware + system_optimizations set_permissions - check_matrix_binaries + setup_sudo_authorization + + # Database and configuration setup + create_databases + set_configuration + setup_cron_jobs + setup_services + setup_rtc_service # New function for RTC service create_boot_script final_status