Last active
March 24, 2025 03:55
-
-
Save jaxsong/61abe002f0f4d0906086d7dbf2f66bf5 to your computer and use it in GitHub Desktop.
sd format on axis camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script Name: manage_sd_disk.sh | |
# Description: Automates unmounting, formatting, and mounting an SD disk. | |
# Usage: ./manage_sd_disk.sh [server_host] [username] [password] | |
# Dependencies: curl, xmllint | |
set -e # Exit script on error | |
set -o pipefail # Prevent errors in a pipeline from being masked | |
# Constants | |
DISK_ID="SD_DISK" | |
TIMEOUT=600 # Max wait time for each operation (10 minutes) | |
# Assign arguments or set defaults | |
SERVER_HOST=${1:-"127.0.0.1"} | |
USERNAME=$2 | |
PASSWORD=$3 | |
# Logging function | |
log() { | |
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | |
} | |
# Determine authentication method | |
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then | |
AUTH="--digest -u $USERNAME:$PASSWORD" | |
elif [ -n "$TURING_CAMERA_AUTH_DIGEST" ]; then | |
AUTH="$TURING_CAMERA_AUTH_DIGEST" | |
else | |
log "Error: Authentication credentials are required." | |
log "Provide username and password as arguments or set TURING_CAMERA_AUTH_DIGEST." | |
exit 1 | |
fi | |
# Function to wait until the job reaches 100% progress or times out | |
wait_for_progress() { | |
local job_id=$1 | |
local disk_id=$2 | |
local elapsed_time=0 | |
local progress=0 | |
log "Monitoring progress for job $job_id on disk $disk_id..." | |
while [ "$progress" -lt 100 ]; do | |
if [ "$elapsed_time" -ge "$TIMEOUT" ]; then | |
log "Error: Operation timed out after $TIMEOUT seconds." | |
exit 1 | |
fi | |
progress=$(curl -s $AUTH --request GET \ | |
"$SERVER_HOST/axis-cgi/disks/job.cgi?jobid=$job_id&diskid=$disk_id" | \ | |
xmllint --xpath 'string(/root/job/@progress)' - 2>/dev/null) | |
# Ensure progress is a valid number | |
if ! [[ "$progress" =~ ^[0-9]+$ ]]; then | |
log "Error: Invalid progress response. Aborting." | |
exit 1 | |
fi | |
log "Progress: ${progress}%" | |
sleep 2 | |
elapsed_time=$((elapsed_time + 2)) | |
done | |
} | |
# Function to perform a disk operation and wait for completion | |
perform_disk_operation() { | |
local operation=$1 | |
local url=$2 | |
local action_message=$3 | |
log "$action_message..." | |
local job_id=$(curl -s $AUTH --request GET "$url" | \ | |
xmllint --xpath 'string(/root/job/@jobid)' - 2>/dev/null) | |
if [ -z "$job_id" ]; then | |
log "Error: Failed to start $operation operation on $DISK_ID." | |
exit 1 | |
fi | |
log "Started $operation operation. Job ID: $job_id" | |
wait_for_progress "$job_id" "$DISK_ID" | |
log "$operation completed successfully." | |
} | |
# Step 1: Unmount the disk | |
perform_disk_operation "Unmount" \ | |
"$SERVER_HOST/axis-cgi/disks/mount.cgi?action=unmount&diskid=$DISK_ID" \ | |
"Unmounting $DISK_ID" | |
# Step 2: Format the disk | |
perform_disk_operation "Format" \ | |
"$SERVER_HOST/axis-cgi/disks/format.cgi?diskid=$DISK_ID&filesystem=ext4" \ | |
"Formatting $DISK_ID" | |
# Step 3: Mount the disk | |
perform_disk_operation "Mount" \ | |
"$SERVER_HOST/axis-cgi/disks/mount.cgi?action=mount&diskid=$DISK_ID" \ | |
"Mounting $DISK_ID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment