[TriLUG] Error checking for dd?

Nathan Yinger via TriLUG trilug at trilug.org
Thu Dec 22 21:38:20 EST 2016


Thanks for the ideas. After a bit of tinkering, I came up with this shell
script to write an image and then test it:

-------------

if [ -z "$2" ] ; then
  echo 'Usage: dd-checked.sh IMAGE TARGET' ;
  exit 1 ;
fi

IMAGE="$1"
TARGET="$2"
IMAGE_SIZE=$(stat -c%s "$IMAGE")
BLOCKS=$(expr $IMAGE_SIZE / 4096)

# Exit immediately if these commands fail
set -e

echo "Writing $IMAGE to $TARGET with 4K block size..."
dd if="$IMAGE" of="$TARGET" bs=4K

echo "Waiting for write operations to sync (this may take a while with
slower media)..."
sync

set +e

dd if="$TARGET" bs=4K count=$(expr $BLOCKS + 1) status=none | head -c
$IMAGE_SIZE | diff $IMAGE -

if [ $? != 0 ] ; then
  echo 'The image written DIFFERS from the original file :(' ;
else
  echo 'The image written is IDENTICAL to the original file :)' ;
fi

-------------


Incidentally, the following can work in place of the second dd:

{
  dd if="$TARGET" bs=4K count=$BLOCKS status=none
  dd if="$TARGET" bs=1 skip=$(expr $BLOCKS \* 4096) count=$REMAINDER
status=none
} | diff $IMAGE -

Any thoughts on which is 'better'?


On Thu, Dec 22, 2016 at 9:09 PM, bick via TriLUG <trilug at trilug.org> wrote:

> To get rid of the blank space, you can use truncate (e.g., dd your sd card
> image back to your pc, then run "truncate --reference distro_file.img
> sd_card.img", then run diff).  Credit goes not to my own brain, but to the
> Pi Foundation (I've used this guide more times than I should have needed.):
> https://www.raspberrypi.org/documentation/installation/insta
> lling-images/linux.md
>
>
>
> On 12/20/2016 06:44 PM, Nathan Yinger via TriLUG wrote:
>
>> Does anyone know a good/easy way to check for errors when writing an image
>> to an SD card with dd? I keep having to flash RPi SD cards multiple times
>> before they work properly.
>>
>> After writing an SD card, I tried using dd to copy it back into a file to
>> diff with the original, but it cloned the entire SD card, including blank
>> space. (The original image was 45MB, and I stopped the copy back at
>> 100MB).
>>
>> ~Nathan Yinger
>>
>
> --
> This message was sent to: npyinger at ncsu.edu <npyinger at ncsu.edu>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  : http://www.trilug.org/mailman/
> options/trilug/npyinger%40ncsu.edu
> Welcome to TriLUG: http://trilug.org/welcome
>


More information about the TriLUG mailing list