How to identify SD-cards and flash sticks as being such?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Since using Raspberry Pis, I've found myself frequently burning its
various distros to SD-cards, and I also burn "live" Linux distros to flash
sticks quite often.  So I've written a bash script to write image files to
/dev/sdb using dd.

However, since I use a number of machines, it's not entirely safe to
assume that the external flash device will be on /dev/sdb, so I want to
include a safety check to ensure that I don't trash an internal drive.
Since flash devices now have a storage size comparable to the hard drives
of not-so-old PCs, checking the size with fdisk doesn't help.

Anybody know a technique to check the type of device on a /dev/sdx node?

Re: How to identify SD-cards and flash sticks as being such?
["Followup-To:" header set to comp.sys.raspberry-pi.]
Quoted text here. Click to load it

By device id?
/dev/disk/by-id
/dev/disk/by-uuid

Re: How to identify SD-cards and flash sticks as being such?
Quoted text here. Click to load it

Thanks, but that doesn't seem to identify flash cards and sticks.

It seems that the by-id directory provides variously named devices linked
to the device id, but I've just checked a bunch of devices, and about the
only thing that I can distinguish consistently is the difference between
ATA and USB:

These are internal ATA drives:
ata-OCZ-VERTEX4_OCZ-1K1F4KH856G4W673
ata-Hitachi_HTS543216L9A300_090822FB2200VCGL464A

These are USB-attached hard drives:
usb-IBM-DBCA_-206480_222022840943-0:0
usb-FUJITSU_MHT2060AH_801344C26237-0:0
usb-StoreJet_Transcend_74AGW0BPT-0:0

These are SD-Card readers:
usb-ChipsBnk_SD_MMCReader_110074973765-0:0
usb-Multi_Flash_Reader_058F0O1111B-0:0
usb-USB2.0_CARD-READER_0201202010201000-0:0
usb-Mass_Storage_Device_121220130416-0:0
usb-Generic-_Multi-Card_20071114173400000-0:0

These are flash sticks:
usb-Generic_Flash_Disk_E41C63C0-0:0
usb-USBest_Technology_USB_Mass_Storage_Device_08102382493394-0:0
usb-USB_Flash_Disk_FB08102900097261-0:0

Re: How to identify SD-cards and flash sticks as being such?
["Followup-To:" header set to comp.sys.raspberry-pi.]
Quoted text here. Click to load it

Well... I thought that you KNOW what device you just plugged in...
and I still think this should be enough.
Ok... maybe it would be confusing if you plug in two identical devices.
Maybe use a label and then look at /dev/disk/by-label
Or... lsusb and then check what you think is a device you are going to
stamp on in a few moments after dd starts. :)

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

If establishing that it's USB is the only test that I can make, then so be
it; I can probably ensure that the only USB device plugged in is the one
that I'm going to burn.  I'm just thinking that some internal devices can
be connected via USB motherboard-headers.

It would be safer if I could figure out a way to distinguish between
multiple USB devices, pick the flash device, select the corresponding
device id, and only then supply the name of that device as a prompt to the
user to OK the burn.

Re: How to identify SD-cards and flash sticks as being such?
On Fri, 10 Oct 2014 15:40:31 +0100, Dave Farrance wrote:

Quoted text here. Click to load it

Individual device serial numbers? Only allow you script to operate on
known device serial numbers.

--  
Cheers
Dave.




Re: How to identify SD-cards and flash sticks as being such?
On 10.10.14 17:40, Dave Farrance wrote:
Quoted text here. Click to load it


The /sys pseudo-filesystem could help you. It is a jumble of symlinks,
and it is actually a peek into the internal data structures and
linkages in the kernel I/O system.

A good starting point could be /sys/class/block.

--  

Tauno Voipio


Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

I was able to generate files to compare between sda (hard drive) and sdb
(flash) using that pseudo-filesystem with this:

( cd /sys/class/block/sda/;
  find . -type f -exec sh -c 'echo -n {}=; cat {};' \; ) >sda.txt
( cd /sys/class/block/sdb/;
  find . -type f -exec sh -c 'echo -n  {}=; cat {};' \; ) >sdb.txt

There are numeric differences between the two that indicate things like
sector sizes but it'd be very difficult to parse with a script.

Re: How to identify SD-cards and flash sticks as being such?
Quoted text here. Click to load it

As far as USB is concerned, there's no difference between a USB stick (being
a USB to flash memory converter/controller and a flash chip) and a memory
card in a reader (being a USB to SD/whatever converter and a flash card).  
They're all simply mass storage devices.  So there's no universal way to do
it - the only way to tell is by the name ('reader', 'card', etc).

A hint might be that readers typically have multiple slots which appear as
multiple empty devices, but that's not guaranteed.

Theo

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

I see. I'll have to settle for just checking if it's USB. I googled for
ways to check and this will have to do:

[[ -n $(udevadm info /dev/sdb | grep ID_BUS=usb) ]] && echo yes it is usb

Re: How to identify SD-cards and flash sticks as being such?
Dave Farrance wrote:
Quoted text here. Click to load it

Plug the device in and then have a look at the last few lines in /var/log/messages.

That will look like this:

# tail /var/log/messages
Oct 10 21:01:00 pcict9 kernel: [6733354.032100] usb 5-8: SerialNumber: FBG1108231001392
Oct 10 21:01:00 pcict9 kernel: [6733354.032368] usb-storage 5-8:1.0: USB Mass Storage device detected
Oct 10 21:01:00 pcict9 kernel: [6733354.032442] scsi18 : usb-storage 5-8:1.0
Oct 10 21:01:00 pcict9 mtp-probe: checking bus 5, device 13: "/sys/devices/pci0000:00/0000:00:1d.7/usb5/5-8"
Oct 10 21:01:00 pcict9 mtp-probe: bus: 5, device: 13 was not an MTP device
Oct 10 21:01:01 pcict9 kernel: [6733355.249199] scsi 18:0:0:0: Direct-Access     USB      Flash Disk       1100 PQ: 0 ANSI: 4
Oct 10 21:01:01 pcict9 kernel: [6733355.250169] sd 18:0:0:0: [sdd] 3915776 512-byte logical blocks: (2.00 GB/1.86 GiB)
Oct 10 21:01:01 pcict9 kernel: [6733355.251316] sd 18:0:0:0: [sdd] Write Protect is off
Oct 10 21:01:01 pcict9 kernel: [6733355.256939]  sdd: sdd1
Oct 10 21:01:01 pcict9 kernel: [6733355.261073] sd 18:0:0:0: [sdd] Attached SCSI removable disk

This shows that the device I plugged in has got the device name /dev/sdd and that the device
has a single partition /dev/sdd1.


Regards,

Kees.

--  
Kees Theunissen.


Re: How to identify SD-cards and flash sticks as being such?
On 10/10/2014 1:13 PM, Dave Farrance wrote:
Quoted text here. Click to load it

Is there a way to see the time and date of the drive being mounted?  
Maybe you can add a check that the drive being written must have been  
mounted within the last x minutes?  Or prompt the user with the info on  
the drive including the mounting time and let the user have the final say.

--  

Rick

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

Perhaps one thing to consider is that a lot of built in SD card readers
on laptops aren't USB. My Dell Latitude at work for example shows a
"Standard Compliant SD Host Controller" connected to PCI Express.

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

OK. The one internal SD reader that I tried (on an Acer Revo mini-PC) did
show up as USB.

Re: How to identify SD-cards and flash sticks as being such?
Ok, thanks for the suggestions. I've settled on this method:

First check that the user is in the disk group (necessary for the
authority to write to usb devices, on Debian/Ubuntu distros, at least).

Check the file has suffix .img, or .iso, or is a .gz containing a single
file (like raspbmc) or a .zip containing a single file (like raspian).

Scan /dev/sdb, dev/sdc, etc until the first USB-connected device is found.

Get the "ID_Model" string of the SD adaptor, which is things like:
"SD_MMCReader" "CARD-READER" "Flash_Reader" "Storage_Device" "Multi-Card"
or for flash sticks, "Flash_Disk" or "USB_Mass_Storage_Device".

Prompt the user with the above info and ask if it's OK to continue.

pv needs to be installed to provide a nice progress bar and ETA etc, but
the other commands (udevadm, unzip, funzip) are default on Ubuntu/Debian.

I've tested it and it seems to be fully functional:


#!/bin/bash
# burn specified iso to usb flash (verifies that it IS on USB)
fail() { echo Error: $*; exit 1; }
[[ -z $(grep '\bdisk\b' < <(groups $USER)) ]] && fail $USER not in disk group
[[ "$1" != ?(*.iso|*.img|*.zip|*.gz) ]] && fail file suffix unrecognised
[[ "$1" == *.tar.gz ]] && fail tar files not handled
[[ "$1" == *.zip && "$(unzip -l "$1" | awk 'END{print $2$3}')" != "1file" ]] &&
  fail zip contains multiple files
for n in b c d e f q; do
  [[ $n == q ]] && fail USB device not found
  [[ -n $(udevadm info "/dev/sd$n" 2>&1 | grep ID_BUS=usb) ]] && break
done
sd=$(udevadm info /dev/sd$n | grep 'ID_MODEL=' | sed 's/.*ID_MODEL=//')
read -p "Burn $1 into \"$sd\" at /dev/sd$n [Yn]" yn
[[ "$yn" == [nN]* ]] && exit 1
[[ "$1" == ?(*.zip|*.gz) ]] && fz=funzip || fz='tee'
pv -tpreb "$1" | $fz | dd bs=1M oflag=dsync of="/dev/sd$n"

Re: How to identify SD-cards and flash sticks as being such?
Quoted text here. Click to load it

Just check the last few lines of dmesg output.

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

Yes, Kees suggested this too. After thinking about it, I realize that
scanning the whole message log for the last instance of the [sd?] string
probably is a quite robust method.

dmesg | grep -o '\[sd.\]' | tail -1 | sed -e 's:\[:/dev/:' -e 's:\]::'

Re: How to identify SD-cards and flash sticks as being such?
Quoted text here. Click to load it

Yes... but what if user plugs in USB device that is not recognized for some
reason? Like deffective device. I received one USB card reader that only
lights up its LEDs and does nothing more. No info in logs. It is not detected.
Well... as you've mentioned... at the end user has to decide. :)

Re: How to identify SD-cards and flash sticks as being such?
On 2014-10-12 11:59, Nikolaj Lazic wrote:

Quoted text here. Click to load it

One could make use of the kernel time in dmesg output. e.g. ignore block
devices attached within 2 minutes after system start...

Quoted text here. Click to load it

Still applies though

Re: How to identify SD-cards and flash sticks as being such?

Quoted text here. Click to load it

Yes. If you look at my second version of the script that I put in a reply
to Rob, you'll see that if the last inserted /dev/sd? device isn't a USB
device, you'll get a prompt to re-insert it. Then the user is prompted
with the device model name as a final check.

I think that's better that giving the user a deadline to complete an
action, given the holdups that are normally encountered when you're
developing things with computers, and in any case, uptime comparisons are
awkward to parse and would require several extra lines of code.  


Site Timeline