# marcone/teslausb architecture

> Architecture diagram of the GitHub repository marcone/teslausb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/marcone/teslausb
- Repository: https://github.com/marcone/teslausb

## Overview

#### Purpose and execution model
teslausb turns a Raspberry Pi or other USB-OTG SBC into a Tesla-visible USB storage device. It exposes dashcam and music storage to the car, then uses the SBC’s network access to repair, archive, manage, and serve that data. The system is primarily Bash running on Linux, with small Python notification/API integrations and a C++ web utility.

Primary operational entry points are device setup in `setup/pi/setup-teslausb`, runtime orchestration in `run/archiveloop`, and USB gadget state changes in `run/enable_gadget.sh` and `run/disable_gadget.sh`.

#### Image build and first-boot provisioning
A customized Pi image is built through pi-gen sources, injecting packages, system tweaks, configuration samples, and first-run behavior. `pi-gen-sources/prepare.sh` prepares the build inputs; `pi-gen-sources/00-teslausb-tweaks/00-run.sh` installs the project stage; `pi-gen-sources/00-teslausb-tweaks/files/run_once` supports one-time initialization.

On-device setup configures storage, networking, automounting, Samba, SSH, and the web service. The central Pi setup flow is `setup/pi/configure.sh`, with backing-file creation in `setup/pi/create-backingfiles.sh` and `setup/pi/create-backingfiles-partition.sh`.

#### USB gadget and mutable storage boundary
The SBC emulates a USB mass-storage device for the Tesla. Gadget enable/disable scripts control when the car can access backing storage: `run/enable_gadget.sh`, `run/disable_gadget.sh`, and `run/auto.teslausb`.

The key safety boundary is Tesla-owned USB access versus Linux-owned filesystem access. Runtime scripts disable or detach the gadget before local maintenance/archiving, then restore it afterward. Read-only-root support and writable backing files reduce SD-card wear and isolate Tesla filesystem corruption; relevant setup is `setup/pi/make-root-fs-readonly.sh` and `run/remountfs_rw`.

#### Runtime lifecycle and recording maintenance
`run/archiveloop` is the long-running control loop coordinating archive availability, clip transfer, cleanup, and retry behavior. Vehicle state controls transitions: `run/awake_start`, `run/awake_stop`, and `run/waitforidle` gate work around Tesla activity.

Storage protection is handled by `run/manage_free_space.sh`; snapshot-style protection/recovery uses `run/make_snapshot.sh`, `run/release_snapshot.sh`, and `run/force_sync.sh`. Mounting backing images and deriving mount options are separated into `run/mountimage` and `run/mountoptsforimage`.

#### Pluggable archive backends
Archiving is an adapter-style subsystem selected by archive type. Each backend supplies a common lifecycle—reachability check, connect, archive clips, disconnect, and archive configuration—under parallel directories:
- CIFS/SMB: `run/cifs_archive/archive-clips.sh`, `run/cifs_archive/connect-archive.sh`
- NFS: `run/nfs_archive/archive-clips.sh`, `run/nfs_archive/connect-archive.sh`
- rsync: `run/rsync_archive/archive-clips.sh`, `run/rsync_archive/connect-archive.sh`
- rclone/cloud remotes: `run/rclone_archive/archive-clips.sh`, `run/rclone_archive/connect-archive.sh`
- no external archive: `run/none_archive/archive-clips.sh`

CIFS and NFS also include music synchronization, enabling network-hosted music to be copied onto the Tesla-visible drive.

#### Administration and web interface
The embedded web UI is static HTML/JavaScript plus CGI shell endpoints, deployed/configured by `setup/pi/configure-web.sh` and Nginx configuration `teslausb-www/teslausb.nginx`. The interface provides file browsing, clip viewing/download, diagnostics, configuration, reboot, and sync triggers.

Important privileged CGI boundaries are `teslausb-www/html/cgi-bin/trigger_sync.sh`, `teslausb-www/html/cgi-bin/status.sh`, and `teslausb-www/html/cgi-bin/config.sh`. These bridge browser requests into local system and archive operations.

#### External integrations and observability
The system depends on Linux USB gadget support, Wi-Fi/WPA2, filesystem/mount tooling, and optional SMB/CIFS, NFS, rsync, or rclone archive services. Samba and SSH are optional management-facing services configured in `setup/pi/configure-samba.sh` and `setup/pi/configure-ssh.sh`.

Notifications support push, Matrix, and AWS SNS through `run/send-push-message`, `run/send_matrix.py`, and `run/send_sns.py`. Tesla-aware behavior can use `run/tesla_api.py`; device health monitoring is implemented by `run/temperature_monitor`.

Main entry points: [`run/archiveloop`](https://github.com/marcone/teslausb/blob/main-dev/run/archiveloop) (archive loop), [`run/mountimage`](https://github.com/marcone/teslausb/blob/main-dev/run/mountimage) (mounted backing image), [`setup/pi/configure.sh`](https://github.com/marcone/teslausb/blob/main-dev/setup/pi/configure.sh) (Pi configuration), [`teslausb-www/teslausb.nginx`](https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/teslausb.nginx) (web UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_build["Image &amp; Provisioning"]
  node_pigen_prepare["pi-gen input prep<br/>build script<br/>[prepare.sh]"]
  node_pigen_stage["teslausb pi-gen stage<br/>image stage<br/>[00-run.sh]"]
  node_first_boot["first-run init<br/>boot script"]
  node_pi_configure["Pi configuration<br/>setup orchestrator<br/>[configure.sh]"]
  node_backing_files[("backing-file storage<br/>storage setup")]
  node_readonly_root["read-only root<br/>wear protection"]
  node_setup_entry["device setup<br/>setup entry point"]
end

subgraph group_runtime["Tesla Storage Runtime"]
  node_archive_loop["archive loop<br/>runtime orchestrator"]
  node_vehicle_gating["vehicle activity gate<br/>state control"]
  node_gadget_enable["enable USB gadget<br/>gadget control<br/>[enable_gadget.sh]"]
  node_gadget_disable["disable USB gadget<br/>gadget control<br/>[disable_gadget.sh]"]
  node_mounted_storage[("mounted backing image<br/>mutable storage boundary")]
  node_storage_safety["snapshot &amp; free-space control<br/>storage maintenance"]
end

subgraph group_archive["Archive Adapters"]
  node_archive_contract{{"archive lifecycle<br/>adapter contract"}}
  node_cifs_archive["CIFS/SMB archive<br/>archive backend<br/>[archive-clips.sh]"]
  node_nfs_archive["NFS archive<br/>archive backend<br/>[archive-clips.sh]"]
  node_rsync_archive["rsync archive<br/>archive backend<br/>[archive-clips.sh]"]
  node_rclone_archive["rclone archive<br/>archive backend<br/>[archive-clips.sh]"]
end

subgraph group_admin["Administration"]
  node_web_ui["web UI<br/>Nginx static and CGI<br/>[teslausb.nginx]"]
  node_sync_cgi["sync trigger CGI<br/>privileged endpoint<br/>[trigger_sync.sh]"]
  node_status_cgi["status CGI<br/>privileged endpoint<br/>[status.sh]"]
end

node_notifications["notifications<br/>integration"]

node_pigen_prepare -->|"supplies build inputs"| node_pigen_stage
node_pigen_stage -->|"installs"| node_first_boot
node_first_boot -->|"initializes"| node_pi_configure
node_setup_entry -->|"runs"| node_pi_configure
node_pi_configure -->|"creates"| node_backing_files
node_pi_configure -->|"enables"| node_readonly_root
node_backing_files -->|"provides image"| node_mounted_storage
node_archive_loop -->|"waits for idle"| node_vehicle_gating
node_archive_loop -->|"detaches before maintenance"| node_gadget_disable
node_gadget_disable -->|"permits Linux access"| node_mounted_storage
node_archive_loop -->|"maintains"| node_storage_safety
node_storage_safety -->|"protects"| node_mounted_storage
node_archive_loop -->|"selects backend"| node_archive_contract
node_archive_contract -.->|"implements"| node_cifs_archive
node_archive_contract -.->|"implements"| node_nfs_archive
node_archive_contract -.->|"implements"| node_rsync_archive
node_archive_contract -.->|"implements"| node_rclone_archive
node_archive_contract -->|"archives clips from"| node_mounted_storage
node_archive_loop -->|"restores after work"| node_gadget_enable
node_gadget_enable -->|"exports to Tesla"| node_mounted_storage
node_web_ui -->|"invokes"| node_sync_cgi
node_web_ui -->|"invokes"| node_status_cgi
node_sync_cgi -->|"triggers sync"| node_archive_loop
node_status_cgi -.->|"reads runtime status"| node_archive_loop
node_archive_loop -.->|"reports events"| node_notifications

click node_pigen_prepare "https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/prepare.sh"
click node_pigen_stage "https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/00-teslausb-tweaks/00-run.sh"
click node_first_boot "https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/00-teslausb-tweaks/files/run_once"
click node_pi_configure "https://github.com/marcone/teslausb/blob/main-dev/setup/pi/configure.sh"
click node_backing_files "https://github.com/marcone/teslausb/blob/main-dev/setup/pi/create-backingfiles.sh"
click node_readonly_root "https://github.com/marcone/teslausb/blob/main-dev/setup/pi/make-root-fs-readonly.sh"
click node_setup_entry "https://github.com/marcone/teslausb/blob/main-dev/setup/pi/setup-teslausb"
click node_archive_loop "https://github.com/marcone/teslausb/blob/main-dev/run/archiveloop"
click node_vehicle_gating "https://github.com/marcone/teslausb/blob/main-dev/run/waitforidle"
click node_gadget_enable "https://github.com/marcone/teslausb/blob/main-dev/run/enable_gadget.sh"
click node_gadget_disable "https://github.com/marcone/teslausb/blob/main-dev/run/disable_gadget.sh"
click node_mounted_storage "https://github.com/marcone/teslausb/blob/main-dev/run/mountimage"
click node_storage_safety "https://github.com/marcone/teslausb/blob/main-dev/run/manage_free_space.sh"
click node_cifs_archive "https://github.com/marcone/teslausb/blob/main-dev/run/cifs_archive/archive-clips.sh"
click node_nfs_archive "https://github.com/marcone/teslausb/blob/main-dev/run/nfs_archive/archive-clips.sh"
click node_rsync_archive "https://github.com/marcone/teslausb/blob/main-dev/run/rsync_archive/archive-clips.sh"
click node_rclone_archive "https://github.com/marcone/teslausb/blob/main-dev/run/rclone_archive/archive-clips.sh"
click node_web_ui "https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/teslausb.nginx"
click node_sync_cgi "https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/html/cgi-bin/trigger_sync.sh"
click node_status_cgi "https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/html/cgi-bin/status.sh"
click node_notifications "https://github.com/marcone/teslausb/blob/main-dev/run/send-push-message"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pigen_prepare,node_pigen_stage,node_first_boot,node_pi_configure,node_backing_files,node_readonly_root,node_setup_entry toneBlue
class node_archive_loop,node_vehicle_gating,node_gadget_enable,node_gadget_disable,node_mounted_storage,node_storage_safety toneAmber
class node_archive_contract,node_cifs_archive,node_nfs_archive,node_rsync_archive,node_rclone_archive toneMint
class node_web_ui,node_sync_cgi,node_status_cgi toneRose
class node_notifications toneNeutral
```

## Components

### Image & Provisioning

Builds and configures the Pi appliance.

- [pi-gen input prep](https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/prepare.sh): build script · `pi-gen-sources/prepare.sh`
- [teslausb pi-gen stage](https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/00-teslausb-tweaks/00-run.sh): image stage · `pi-gen-sources/00-teslausb-tweaks/00-run.sh`
- [first-run init](https://github.com/marcone/teslausb/blob/main-dev/pi-gen-sources/00-teslausb-tweaks/files/run_once): boot script · `pi-gen-sources/00-teslausb-tweaks/files/run_once`
- [Pi configuration](https://github.com/marcone/teslausb/blob/main-dev/setup/pi/configure.sh): setup orchestrator · Configures storage, network, services, and web UI. · `setup/pi/configure.sh`
- [backing-file storage](https://github.com/marcone/teslausb/blob/main-dev/setup/pi/create-backingfiles.sh): storage setup · `setup/pi/create-backingfiles.sh`
- [read-only root](https://github.com/marcone/teslausb/blob/main-dev/setup/pi/make-root-fs-readonly.sh): wear protection · `setup/pi/make-root-fs-readonly.sh`
- [device setup](https://github.com/marcone/teslausb/blob/main-dev/setup/pi/setup-teslausb): setup entry point · `setup/pi/setup-teslausb`

### Tesla Storage Runtime

Coordinates safe USB ownership and recording maintenance.

- [archive loop](https://github.com/marcone/teslausb/blob/main-dev/run/archiveloop): runtime orchestrator · Long-running control loop for transfer, cleanup, and retries. · `run/archiveloop`
- [vehicle activity gate](https://github.com/marcone/teslausb/blob/main-dev/run/waitforidle): state control · `run/waitforidle`
- [enable USB gadget](https://github.com/marcone/teslausb/blob/main-dev/run/enable_gadget.sh): gadget control · Exposes backing storage to the Tesla. · `run/enable_gadget.sh`
- [disable USB gadget](https://github.com/marcone/teslausb/blob/main-dev/run/disable_gadget.sh): gadget control · Returns filesystem ownership to Linux. · `run/disable_gadget.sh`
- [mounted backing image](https://github.com/marcone/teslausb/blob/main-dev/run/mountimage): mutable storage boundary · Tesla-visible recordings and music storage. · `run/mountimage`
- [snapshot & free-space control](https://github.com/marcone/teslausb/blob/main-dev/run/manage_free_space.sh): storage maintenance · `run/manage_free_space.sh`

### Archive Adapters

Selectable common archive lifecycle implementations.

- **archive lifecycle**: adapter contract · Reachability, connect, transfer, and disconnect flow.
- [CIFS/SMB archive](https://github.com/marcone/teslausb/blob/main-dev/run/cifs_archive/archive-clips.sh): archive backend · Also supports network music synchronization. · `run/cifs_archive/archive-clips.sh`
- [NFS archive](https://github.com/marcone/teslausb/blob/main-dev/run/nfs_archive/archive-clips.sh): archive backend · Also supports network music synchronization. · `run/nfs_archive/archive-clips.sh`
- [rsync archive](https://github.com/marcone/teslausb/blob/main-dev/run/rsync_archive/archive-clips.sh): archive backend · `run/rsync_archive/archive-clips.sh`
- [rclone archive](https://github.com/marcone/teslausb/blob/main-dev/run/rclone_archive/archive-clips.sh): archive backend · `run/rclone_archive/archive-clips.sh`

### Administration

Browser and remote management surfaces.

- [web UI](https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/teslausb.nginx): Nginx static and CGI · Browser administration and file access surface. · `teslausb-www/teslausb.nginx`
- [sync trigger CGI](https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/html/cgi-bin/trigger_sync.sh): privileged endpoint · Bridges browser sync requests into local operations. · `teslausb-www/html/cgi-bin/trigger_sync.sh`
- [status CGI](https://github.com/marcone/teslausb/blob/main-dev/teslausb-www/html/cgi-bin/status.sh): privileged endpoint · `teslausb-www/html/cgi-bin/status.sh`

### Other components

- [notifications](https://github.com/marcone/teslausb/blob/main-dev/run/send-push-message): integration · Push, Matrix, and AWS SNS delivery. · `run/send-push-message`

## Connections

- pi-gen input prep → teslausb pi-gen stage: supplies build inputs
- teslausb pi-gen stage → first-run init: installs
- first-run init → Pi configuration: initializes
- device setup → Pi configuration: runs
- Pi configuration → backing-file storage: creates
- Pi configuration → read-only root: enables
- backing-file storage → mounted backing image: provides image
- archive loop → vehicle activity gate: waits for idle
- archive loop → disable USB gadget: detaches before maintenance
- disable USB gadget → mounted backing image: permits Linux access
- archive loop → snapshot & free-space control: maintains
- snapshot & free-space control → mounted backing image: protects
- archive loop → archive lifecycle: selects backend
- archive lifecycle → CIFS/SMB archive: implements
- archive lifecycle → NFS archive: implements
- archive lifecycle → rsync archive: implements
- archive lifecycle → rclone archive: implements
- archive lifecycle → mounted backing image: archives clips from
- archive loop → enable USB gadget: restores after work
- enable USB gadget → mounted backing image: exports to Tesla
- web UI → sync trigger CGI: invokes
- web UI → status CGI: invokes
- sync trigger CGI → archive loop: triggers sync
- status CGI → archive loop: reads runtime status
- archive loop → notifications: reports events
