dd the data destroyer and make-key the saviour!

As a long-term Linux user who still likes to try different distributions and operating systems, I have what I called a “playputer”. That is a computer set aside for installing and trying out different operating systems on. It is a laptop and a bit of a Linux problem child with secure restricted boot, UEFI and Optimus graphics, all it needs to be the perfect storm is Broadcom wireless!

Trying out different Linux distributions means I often use the powerful but dangerous dd command to create my boot-able USB keys. If you are anything like me, then no matter how carefully you read what you have typed, any typo will only be seen almost immediately after pressing enter! With the dd command that is too late especially if you have pointed it as sda! The second problem with the dd command is the complete lack of feedback while it is doing anything.

make-key is my solution. It is a simple bash script that:

  • Performs basic validation on the parameters.
  • Asks for confirmation of the action.
  • Uses dd or pv if installed to give a progress bar.
#!/bin/bash
## =============================================================================
##  Copyright 2014 Richard Muir-Gladman
##      Distributed under the terms of the GNU General Public License
## =============================================================================
##  This file is part of make-key.
##  make-key is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  make-key is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  As it is a single file, I haven't included a copy of the GNU General Public
##  License along with make-key.  See .
## =============================================================================
 
##  Produce help message on help switch
if [ "$1" == "--help" ]
then
    echo "Must be run as root"
    echo "Usage: make-key file_name to_device"
    echo "Example: sudo make-key linux.iso sdh"
    exit
fi
 
##  Must be run as root
if [ "$EUID" -ne 0 ]
then
    echo "Error: Should be run as root."
    exit
fi
 
 
##  Get and validate command line parameters
##    If not valid then error and exit
if [ ! -f "$1" ]
then
    error="Error: Input file "
    error+="$1"
    error+=" not found."
    echo $error
    exit
fi
 
if [ ! -b "/dev/$2" ]
then
    error="Error: "
    error+="$2"
    error+=" is not a block device."
    echo $error
    exit
fi
 
##  Confirm input and destination drive with scary message
##    If confirmed then continue
device="/dev/"
device+="$2"
 
message="Copying "
message+="$1"
message+=" to "
message+="$device"
message+="."
echo "$message"
message="This will destroy the data on the device. Are you sure you want to continue? "
read -p "$message" yn
case $yn in
    [Yy]* )
        ##  Check for existence of pv and use if found
        if hash pv 2>/dev/null
        then
            pv "$1" | sudo dd bs=1M of="$device"
 
        ##  Otherwise inform and use dd only
        else
            echo "You should consider installing pv to get a nice progress bar!"
            echo "Working... Please wait..."
            sudo dd if="$1" of="$device" bs=1M
        fi
        echo "Done."
        ;;
    [Nn]* )
        exit
        ;;
esac
 
exit

If there are any real bash scripters reading this please feel free to let me know how to improve make-key. After you have stopped laughing of course.

Leave a Reply

Your email address will not be published. Required fields are marked *

*