防止额外驱动器上的dd命令

我每天都使用dd来编写Raspberry Pi SD卡图像。

然而,偶尔我会滑倒并错误地划分错误的分区,从而杀死我的其他驱动器。

我想知道是否有办法能够使用dd来阻止写/dev/sdb/dev/sdc

这是Linux,所以很容易。 输入echo $PATH并找到1)您具有写访问权限的目录; 2)它在列表中的位置早于从type -p dd获得的目录。 将此文件放在名为dd目录中,并通过chmod +x使其可执行。

这是文件:

 ----------------------------- cut here --------------------------- #!/bin/bash # try to prevent foot-shooting via dd - complain if if= or of= is mounted debugmode=1 # # either abort with a message or pass everything to the real dd realdd="/usr/bin/dd" if [ $debugmode = 1 ] ; then realdd="/bin/echo" fi if [ ! -x "$realdd" ] ; then echo "$0 cannot find the real dd at $realdd" >&2 exit 255 fi errors=0 function diskok () { # passed a parameter, if=... or of=... # if the argument to if= or of= is "WRONG", pull the plug param=$1; device="${param##?f=}" # replace this with checks of your choice. I'm simply excluding any word in /etc/fstab if [[ $( grep -q "$device" /etc/fstab;echo $? ) = 0 ]]; then echo "Trying to dd a mounted device $param" >&2 (( errors++ )) fi } laundry="" # Re: Those tests for if and of. You can just do if [[ $1 = if=* ]] and if [[ $1 = of=* ]]. Or, better yet, use a case statement. – muru 7 mins ago while [[ $# -gt 0 ]] ; do case "$1" in if=*) diskok "$1" ;; of=*) diskok "$1" ;; esac laundry="$laundry $1" shift done if [ 0 -eq $errors ] ; then eval "$realdd $laundry" else echo "Saved" fi ----------------------------- cut here --------------------------- 

充分测试后,将debugmode=1更改为debugmode=0

这是我的测试:

 w3@aardvark:~(254)$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 170416096 29962928 131773488 19% / none 4 0 4 0% /sys/fs/cgroup udev 1938252 12 1938240 1% /dev tmpfs 390632 1104 389528 1% /run none 5120 0 5120 0% /run/lock none 1953148 444 1952704 1% /run/shm none 102400 24 102376 1% /run/user /dev/sda6 302248384 247234972 39637036 87% /home /home/w3/.Private 302248384 247234972 39637036 87% /home/w3 /dev/sdb 3840544 1229408 2611136 33% /home/w3/mnt/CLIPZIP w3@aardvark:~(0)$ dd bs=8293 if=/dev/sda of=/dev/sda5 of=/home Trying to dd a mounted device if=/dev/sda Trying to dd a mounted device of=/dev/sda5 Trying to dd a mounted device of=/home Saved w3@aardvark:~(254)$ dd bs=8293 if=/dev/sdaa of=/dev/sdaa5 of=/homae bs=8293 if=/dev/sdaa of=/dev/sdaa5 of=/homae 

请记住将测试更改为您想要的测试。

感谢@muru [[和案例

当然,你可以编写一个包装脚本来防止你自己做错事,但是如果建立了这种错误的安全感,你就会在其他系统上冒险,因为你已经习惯了只存在于你的系统。

因此我的答案是:

在root之前思考而不是,没有办法阻止其他设备。 当root设备可以访问时, dd也可以访问它。 dd只做你告诉它做的事。

dd也被称为D isk D estroyer 😉