Find the answer to your Linux question:
Results 1 to 6 of 6
I've got a script that must be able to run without human intervention. And it needs to write to a USB-stick. Now I know the machines that this script will ...
  1. #1
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047

    Ways to determine a device name, the hands free approach

    I've got a script that must be able to run without human intervention. And it needs to write to a USB-stick.

    Now I know the machines that this script will run on, and it, put simply, calls USB-sticks /dev/sda1.

    So I have the device name /dev/sda1 stored as a variable. But I'm always afraid that one day someone is going to run the script when the device isn't called /dev/sda1, or that /dev/sda1 isn't the intended target.


    Right now I have this solved like this:
    The script checks whether a device called /dev/sd* is mounted. If it finds none or more than one occurrence, it exits. If it finds exactly one occurence it proceeds by checking whether the USB-stick has files on it... blablabla


    But what I want to happen:
    The script automagically detects a device with a filesystem between 1 and 10GB and marks this as target. (1GB is the minimum size needed, 10GB is set as maximum, to differentiate between USB-sticks and external hard drives who are a lot bigger).



    Now what would be the best approach?
    Looking for a device in the outputs of various commands I have two ways to go:

    I can set the device name as a variable, taking from the output of various commands like this:
    Code:
    USBDEVICE=`mount|tail -1|awk '{print $1}'` # Works 99% of the time
    Or like this: (doesn't work because newlines aren't preserved but you can see the basic structure)
    Code:
    for i in `/bin/df|grep /dev/sd`
    > do
    > TEST=`awk '{print $2}'`
    > if [ "$TEST" -gt "990000" ] ; then
    > if [ "$TEST" -le "9900000" ] ; then
    > CANDIDATE=`echo $i|awk '{print $1}'`
    > echo $CANDIDATE
    > fi
    > fi
    > done
    Or I can scan outputs of various commands with pre-defined device names:
    df|grep /dev/sda|blabla
    df|grep /dev/sdb|blabla
    df|grep /dev/sdc|blabla
    df|grep /dev/sdd|blabla
    Then once one if found, it compares size and then proceeds.



    Now I'm a bit puzzled. Both approaches can be gotten to work. And both approaches will yield the right result ~99% of the time. But seeing as how this script will be run by people who do not know what a device is, I cannot permit 'false positives' (ie it tests as correct, but isn't). Also, I take into account portability, namely if the script is run on a different machine than it's designed for (the machine I designed it for has no internal sata devices), it might kill kill kill the internal hard drive.

    I have to take the following into account:
    1)More than one /dev/sd*
    2)No /dev/sd*
    3)One /dev/sd*, but not the right one (size, user files, device=internal disk)
    4)One /dev/sd*, the right one

    Situations 1-3 should lead to "abort!", situation 4 should lead to "proceed"
    And a special case is present for when there are user files on the disk... it leads to "confirm erase"... which is a module of it's own.


    What do you think is the most reliable way of determining the device name for purposes of an automated script that needs to run without human input on this matter?
    Can't tell an OS by it's GUI

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Freston,
    Site greenfly.org - Tips - How to set up a removable USB Drive has some info that might interest you.

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Wouldn't it be better to take control of how the device is named by writing appropriate udev rules?

  4. #4
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by secondmouse
    Freston,
    Site greenfly.org - Tips - How to set up a removable USB Drive has some info that might interest you.
    Interesting. thanks!


    Quote Originally Posted by burschik
    Wouldn't it be better to take control of how the device is named by writing appropriate udev rules?
    Yeah, I considered that. The systems that this script is designed for handles 'unclean unmounts' (pulling the device out without unmounting) very cleanly. Pull one device out and put another one in and it will again be called /dev/sda1. That's not the problem.

    The problem is that this script will be released in the wild. I, then, have no further control over udev rules, mount/unmount options or what have you.
    There is also no way for me to know any of the specifics of the USB-stick. All I can guess is that it's got a vfat file system. Well, that is at least something. I can differentiate between a 8GB root partition called /dev/sda1 and a 8GB USB-stick called /dev/sda1 by filesystem.
    If, by some unlucky turn of events, something happens in the life of this script that gets it to run on another system that what it's designed for, or under other circumstances, I fear someone is going to loose his/her Master Boot Record in record time, so to speak.


    Right now I just have the variable defined statically and compare `mount` with `df` and prompt for user confirmation when files are found on the device. That may lead to situations where no device is found while there is one present (false negative). But that at least is safer than writing to the wrong device (false positive).
    I might add a little grep on `dmesg|tail` into the equation to get a hint of a dynamicaly set variable. But on the other hand, there is only so much you can expect from a bash script.
    I could also add a manual override flag. If the user knows either mountpoint or device name, then the script can adjust itself accordingly without much effort. This last option is probably the safest.



    --



    In case you are wondering what all the fuss is about . This script magically takes all the steps necesary to create a bootable USB-stick with a Linux installation CD image on it, including download. In case the intended machines ever need a reinstall, because they don't have a CD-ROM player on board. And the intended users don't have the knowledge needed to do this themselves.
    But in my minds eye I see them hooking up external drives, adding other peripherals, all distorting the tests that this script runs on the system before committing to 'write action'. Right now it errs on the side of safety, exiting with 'Fail status'. But somehow I think it shouldn't have to if I can set the device name as a dynamically defined variable. But nothing I think of will be right 100% of the time.
    Can't tell an OS by it's GUI

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Personally, I would consider your approach too brittle to work reliably. Let the user tell you which drive to use.

  6. #6
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by burschik
    Personally, I would consider your approach too brittle to work reliably. Let the user tell you which drive to use.
    Thanks! We agree. Hence my question

    It's just that I'm trying to steer away from asking the user for input as much as possible. They lack the knowledge. These machines are no-budget internet PC's with a couple of games and some office programs. It's the kind of PC you'd buy your mother so she can email. The users probably don't even know what a linux is

    Without a CD-drive the users have no means to ever rebuild the system if ever something went wrong with the install. But with this script, they have the means to build a rescue USB-stick. There should be no 'difficult' questions.
    Can't tell an OS by it's GUI

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...