Skip to content

Instantly share code, notes, and snippets.

@TakashiSasaki
Last active December 21, 2015 06:49
Show Gist options
  • Save TakashiSasaki/6266718 to your computer and use it in GitHub Desktop.
Save TakashiSasaki/6266718 to your computer and use it in GitHub Desktop.
safedd is a wrapper script for dd which cares forbidden 'of' targets listed in /etc/safeadd.conf .
#!/bin/sh
if [ ! -e /etc/safedd.conf ]; then
echo /etc/safedd.conf is not found.
echo Each line should be regex for forbidden '"'of'"' parameter.
echo Example of /etc/safedd.conf :
echo ' .*dev/sda.*'
echo ' .*dev/md[0-9].*'
exit -1
fi
ddbin=dd
if [ -e /opt/bin/coreutils-dd ]; then
ddbin=/opt/bin/coreutils-dd
fi
cat /etc/safedd.conf | while read l
do
if [ ${#l} = 0 ]; then
continue
fi
for x in $@; do
if expr $x : "of=${l}" > /dev/null; then
echo $x is not allowed.
exit 111
fi
done
done
if [ $? -eq 111 ]; then
exit -1
fi
if [ $? -ne 0 ]; then
exit -1
fi
echo ready to exec '"'"$ddbin $@"'"'
echo Type '"'yes'"' to continue.
read l
if [ "$l" == "yes" ]; then
$ddbin $@ & pid=$!
trap 'kill $pid; echo killed by signal 1,2,3 or 15; exit 1' 1 2 3 15
while true; do
sleep 10;
kill -USR1 $pid
if [ $? -eq 1 ]; then
echo $ddbin had been terminated.
break;
fi
done
else
echo Nothing has occurred.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment