HOWTO: a script for relatively safe "dd"

Forum rules
Share your brain ;)
pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

HOWTO: a script for relatively safe "dd"

Unread post by pidsley » Sun Aug 04, 2013 1:11 am

"bbqusb" works well for copying iso images to a usb stick if you are using a bbq system, or if you have yad installed on your machine. My main machine is a sid netinstall, and I don't have yad, so I wrote this simple script for dd copying. It assumes the stick is at /dev/sdb, but you can change this. You can also specify the device on the command line, but it will not let you dd to /dev/sda. Of course, if you have more than one hard drive, or your main drive is not /dev/sda, you should change this. I call this script "ddiso" and normal usage is simply

Code: Select all

ddiso isofile

where "isofile" is the name of the iso to copy to /dev/sdb.

copy the iso to /dev/sdc:

Code: Select all

ddiso isofile sdc

Code: Select all

#!/bin/bash
# script to copy an iso to usb stick at /dev/sdb

DEVICE='/dev/sdb' # assume /dev/sdb

function usage() {
    echo 'ddiso: copies an iso file to usb drive at '$DEVICE
    echo 'usage: ddiso isofile <sdX>'
    echo 'assumes /dev/sdb if device is not specified'
    exit 1
} 

if [ -z $1 ]; then # no iso file specified
    usage
fi

if [ ! -e $1 ]; then # iso file not found
    echo 'file "'$1'" does not exist'
    usage
fi

if [ ! -z $2 ]; then # alternate device specified
    if [ $2 = 'sda' ]; then
        echo "I can't let you do that"
        exit 1
    fi
	DEVICE='/dev/'$2
fi

echo 'copy '$1' to usb stick at '$DEVICE
read -n1 -p 'are you sure? (y/n)'
echo 
if [ $REPLY = 'y' ]; then
    cmd='dd bs=4M if='$1' of='$DEVICE
    echo $cmd
    $cmd
    echo 'done'
    sync
    echo 'now safe to remove '$DEVICE
fi

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: HOWTO: a script for relatively safe "dd"

Unread post by dkeg » Sun Aug 04, 2013 2:31 am

but it will not let you dd to /dev/sda
Gee, thanks ... what's the saying, a day late and a dollar short. Eh, like I said, I got my /home partition!

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: HOWTO: a script for relatively safe "dd"

Unread post by machinebacon » Sun Aug 04, 2013 3:08 am

Thanks pidsley, I'll do a rewrite of bbqusb some day - while I really really recommend playing with yad :) It's the greatest GUI stuff after dialog, and doesn't depend on anything else than a gtk2-library
..gnutella..

Post Reply