Skip to content

Instantly share code, notes, and snippets.

@Scarsz
Last active May 17, 2025 10:18
Show Gist options
  • Save Scarsz/233723568e80c189db08fdfe6507f630 to your computer and use it in GitHub Desktop.
Save Scarsz/233723568e80c189db08fdfe6507f630 to your computer and use it in GitHub Desktop.
Automatic TrueNAS media ingesting with UGREEN NAS blinky lights. Tested on DXP4800+
# /etc/udev/rules.d/99-ingest.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*[0-9]", ENV{ID_MODEL}=="MassStorageClass", RUN+="/bin/systemctl start ingest@%k.service"
# udevadm control --reload-rules
# udevadm trigger
#!/bin/bash
set -x
exec > /tmp/ingest.log 2>&1
echo "Script triggered on $(date)"
echo "id: $(id)"
ID="scarsz:scarsz"
DEVICE="$1"
SCRIPT_DIR=$(dirname "$0")
MOUNT_POINT="/mnt/sdcard"
DESTINATION="/mnt/Pool/Home/scarsz/Ingest-$(date +%s)"
UGREEN="$SCRIPT_DIR/ugreen_leds_cli"
mkdir -p "$MOUNT_POINT"
mkdir -p "$DESTINATION"
cleanup_and_exit() {
umount "$MOUNT_POINT" 2>/dev/null
kill $LED_PID 2>/dev/null
chmod +x $SCRIPT_DIR/leds.sh
$SCRIPT_DIR/leds.sh
exit
}
trap cleanup_and_exit INT
# prevent cron task from messing up our LED display
chmod -x $SCRIPT_DIR/leds.sh
umount "$MOUNT_POINT" 2>/dev/null
mount "${DEVICE}" "$MOUNT_POINT" || exit 1
TOTAL_BYTES=$(du -sb "$MOUNT_POINT" | awk '{print $1}')
TOTAL_MB=$(( TOTAL_BYTES / 1024 / 1024 ))
progress_and_blink_leds() {
$UGREEN all -off
$UGREEN power -on -color 255 0 0 -blink 50 50
START_BYTES=$(du -sb "$DESTINATION" | awk '{print $1}')
LAST_DIVISION=-1
while true; do
CURRENT_BYTES=$(du -sb "$DESTINATION" | awk '{print $1}')
COPIED_BYTES=$(( CURRENT_BYTES - START_BYTES ))
PERCENT=0
if [ "$TOTAL_BYTES" -gt 0 ]; then
PERCENT=$((COPIED_BYTES * 100 / (START_BYTES + TOTAL_BYTES)))
fi
if [ $(( $PERCENT / 20 )) -gt $LAST_DIVISION ]; then
LAST_DIVISION=$(( PERCENT / 20 ))
LEDS=()
if [ "$PERCENT" -ge 0 ]; then LEDS+=("netdev"); fi
if [ "$PERCENT" -ge 20 ]; then LEDS+=("disk1"); fi
if [ "$PERCENT" -ge 40 ]; then LEDS+=("disk2"); fi
if [ "$PERCENT" -ge 60 ]; then LEDS+=("disk3"); fi
if [ "$PERCENT" -ge 80 ]; then LEDS+=("disk4"); fi
if [ "${#LEDS[@]}" -gt 0 ]; then
LAST_INDEX=$((${#LEDS[@]} - 1))
BLINK_LED=${LEDS[$LAST_INDEX]}
SOLID_LEDS=("${LEDS[@]:0:$LAST_INDEX}")
if [ "${#SOLID_LEDS[@]}" -gt 0 ]; then
$UGREEN "${SOLID_LEDS[@]}" -on -color 0 255 0
fi
$UGREEN "$BLINK_LED" -on -color 0 255 0 -blink 150 150
fi
fi
COPIED_MB=$((COPIED_BYTES / 1024 / 1024))
echo "$COPIED_MB/$TOTAL_MB MB - $PERCENT%"
sleep 1
done
}
progress_and_blink_leds &
LED_PID=$!
rsync -a --chown $ID $MOUNT_POINT/ $DESTINATION/
kill $LED_PID
$UGREEN all -on -color 0 255 0
sleep 3
cleanup_and_exit
# /etc/systemd/system/[email protected]
[Unit]
Description=Ingest mass storage from /dev/%I
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/mnt/Pool/User/ingest.sh /dev/%I
# sudo systemctl daemon-reexec
# sudo systemctl daemon-reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment