Skip to content

Instantly share code, notes, and snippets.

@abeisgoat
Created December 23, 2022 03:56
Show Gist options
  • Save abeisgoat/b0567184214297cbfe2b2912d132f848 to your computer and use it in GitHub Desktop.
Save abeisgoat/b0567184214297cbfe2b2912d132f848 to your computer and use it in GitHub Desktop.
Script for detecting and mounting flash storage early in the lifecycle
#!/usr/bin/env bash
DEVICE=/dev/sda1
DEVICE_MOUNTED=""
get_device_mounted() {
MOUNTED_DIR=$(find /media/pi/ -maxdepth 1 -type d -printf '%f\n' | grep "-")
if [ -z "$MOUNTED_DIR" ]; then
DEVICE_MOUNTED=""
else
DEVICE_MOUNTED=true
fi
}
wait_for_device_mounted() {
echo "Waiting for device mount"
get_device_mounted
while [ -z "$DEVICE_MOUNTED" ]; do
sleep 0.1
get_device_mounted
done
echo "Device is mounted"
}
wait_for_device_insertion() {
echo "Waiting for block device..."
rm /tmp/device_block
touch /tmp/device_block
# This roundabout trick helps with buffering stdout, dunno why
dmesg -W | grep --line-buffered "Attached SCSI removable disk" -m 1 >> /tmp/device_block &
#dmesg -W | grep --line-buffered "sda: detected capacity change from 0" -m 1 >> /tmp/device_block &
tail -f -n1 /tmp/device_block | head -n 1
echo "Device found."
}
DEVICE_AUTOSTART=""
get_device_autostart() {
DEVICE_AUTOSTART=$(find /media/pi/*/autostart.sh -maxdepth 1 -type f)
}
DEVICE_AUTOKILL=""
get_device_autokill() {
DEVICE_AUTOKILL=$(find /media/pi/*/autokill.sh -maxdepth 1 -type f)
}
wait_for_device_removal() {
# If there is an autokill script on the disk, back it up
# so it can be ran if the disk is unsafely removed
get_device_autokill
echo $DEVICE_AUTOKILL
if test -f "$DEVICE_AUTOKILL"; then
echo "Backing up kill script..."
cp "$DEVICE_AUTOKILL" /tmp/autokill.sh
fi
# Then we wait for the drive to disappear
while test -f "$DEVICE_AUTOKILL"; do
echo "Waiting on catridge removal..."
sleep 1
done
}
AUTOSTART_ALIVE=""
get_autostart_alive() {
if ! [ -z "$AUTOSTART_PID" ]; then
if ps -p $AUTOSTART_PID > /dev/null; then
AUTOSTART_ALIVE=true
else
AUTOSTART_ALIVE=""
fi
fi
}
wait_for_exit() {
while true; do
sleep 0.2
get_autostart_alive
if [ -z "$AUTOSTART_ALIVE" ]; then
echo "Process exit (soft)"
break
fi
get_device_mounted
if [ -z "$DEVICE_MOUNTED" ]; then
echo "Process exit (force)"
break
fi
done
}
autokill() {
if [ -f "/tmp/autokill.sh" ]; then
echo "Invoking autokill.sh"
chmod +x /tmp/autokill.sh
sh /tmp/autokill.sh
rm /tmp/autokill.sh
fi
get_autostart_alive
if ! [ -z "$AUTOSTART_ALIVE" ]; then
echo "Killing $AUTOSTART_PID"
kill $AUTOSTART_PID
fi
}
DEVICE_VALID=""
get_device_valid() {
get_device_autostart
if [ -f "$DEVICE_AUTOSTART" ]; then
DEVICE_VALID=true
else
DEVICE_VALID=""
fi
}
autostart() {
get_device_autokill
if test -f "$DEVICE_AUTOKILL"; then
echo "Backing up autokill before autostart..."
cp "$DEVICE_AUTOKILL" /tmp/autokill.sh
fi
echo "Running" $DEVICE_AUTOSTART
sleep 0.5
sudo su pi -c "sh $DEVICE_AUTOSTART" &
AUTOSTART_PID=$!
echo "Watch PID $AUTOSTART_PID"
}
autokill
while :
do
wait_for_device_insertion
wait_for_device_mounted
get_device_valid
if ! [ -z "$DEVICE_VALID" ]; then
autostart
else
echo "Rejecting disk, no autostart"
fi
wait_for_exit
autokill
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment