Get FAT Drive Serial Numbers in Unix

Noah Petherbridge
kirsle
Posted by Noah Petherbridge on Thursday, December 31 2009 @ 01:15:43 PM
There's not a lot of information about this on The Google, so hopefully this blog will help anyone who wants to know how to get the serial number of a FAT partition from within a Unix-like operating system (including Linux and Mac OS X).

First, this is what I mean about serial numbers. Suppose you're using a Windows system, have a floppy disk at drive A:/ and a regular USB flash drive at E:/, and you run these commands in the command prompt:

C:\>vol E:
Volume in drive E is CRUZER
Volume Serial Number is 955C-59BF

C:\>vol A:
Volume in drive A has no label.
Volume Serial Number is EC2B-36AF
These serial numbers are assigned when the drive is formatted; reformatting a floppy disk or flash drive will give it a different serial number.

According to The Wikipedia, the serial number (ID) is kept in two different places on the partition depending on the version of FAT being used.

In FAT12 and FAT16 (used with floppy disks), the ID begins at byte offset 0x27 (39 in decimal); in FAT32 (used with flash drives and external hard drives), the ID begins at 0x43 (67 in decimal).

So, with the handy dd utility that comes standard on pretty much any Unix-like system, you can extract this information and display it. Here are a couple of one-liners you can run in a Unix terminal. I'll explain how they work afterward.

# For FAT32 filesystems (modern flash drives)
dd if=/dev/sdb1 skip=67 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'

# For FAT12/16 filesystems (old floppy drives)
dd if=testfloppy.img skip=39 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
I underlined the input file (if) and byte offset (skip) in both of these commands. In the first one, I ran the command on a real, physical, flash drive, that had a device node at /dev/sdb1 for its one and only partition. In the second one, I ran it on a floppy disk image file (who has a computer with a real floppy drive these days?)

If you're going to be using a physical device like in my first command, you need to run the command with root privileges (regular users can't read directly from the device node). My second example (using an image file) can be run as a regular user, however.

These commands printed in the terminal for me:

(for the flash drive)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.3445e-05 s, 120 kB/s
Serial Number: 955C-59BF

(for the floppy image)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.1551e-05 s, 127 kB/s
Serial Number: EC2B-36AF
And now, how the commands work. I'll use the flash drive command as the example. In this one-liner, three commands are being executed at once:
dd if=/dev/sdb1 skip=67 bs=1 count=4
hexdump -v -e '1/1 "%02X" " "'
xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
The dd command gets the operating system to read raw data from the flash drive at /dev/sdb1, skipping the first 67 bytes, reading only 1 byte at a time, and reading a total of 4 bytes. This gets the 4 byte serial number; now we need to display it in hexadecimal like Windows and DOS.

The hexdump command takes the 4 binary bytes and displays them in hexadecimal. On my flash drive, it looks like this: BF 59 5C 95. Note that the hex codes are out of order; Windows shows them as 955C-59BF - basically, the reverse of what hexdump shows. Hexdump is showing the correct order; Windows and DOS reverse them when they show you the serial number.

So, we run it through xargs (which turns the four hex numbers into four separate parameters) and sends them to a quick Perl script, which prints out "Serial Number:" and puts the hex codes in the correct order, to give the same result as Windows and DOS.

One could use this information to make a vol command for Unix. If the command checks other places in the filesystem headers to determine the version of FAT, it could automatically use the correct byte offset and get the serial number from both floppy disks and flash drives.

Categories:

[ Blog ]

Comments

There are 12 comments on this page.

guest
guest
Posted on Sunday, April 04 2010 @ 10:40:05 PM by mike.

have you ever heard about "/sbin/blkid"? It's in the core Linux package "util-linux", I am sure you have it :). The correct name for the "Serial Number:" is "UUID".

guest
guest
Posted on Wednesday, June 30 2010 @ 04:16:41 AM by Christophe .

Hello, Very nice . I was looking for more then a week on such solution. Since I need the UUID from 16 or 32 fats on usb sticks On a mac pc. For synchronizing php script this is a security build in to avoid persons to synchronize on another stick then their own. Thank's

guest
guest
Posted on Wednesday, August 11 2010 @ 08:07:08 AM by Kyle.

Tried it on Mac OS X, got the same serial number for 2 different flash drives.

guest
guest
Posted on Monday, November 29 2010 @ 11:47:37 PM by rarerade.

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this
.pl but can't see my serial number...

use Win32::OLE qw(in);
my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//.");
my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive");
my @DV = in($DriveCollection);
print $DV[0]->{SerialNumber};

guest
guest
Posted on Monday, November 29 2010 @ 11:48:27 PM by rarerade.

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this
.pl but can't see my serial number...

use Win32::OLE qw(in);
my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//.");
my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive");
my @DV = in($DriveCollection);
print $DV[0]->{SerialNumber};

Avatar
kirsle
Posted on Tuesday, November 30 2010 @ 12:33:35 AM by Noah Petherbridge.

my ($serial) = vol E:\\ =~ m/Volume Serial Number is ([A-Fa-f0-9-]{9})/;

;)

guest
guest
Posted on Tuesday, November 30 2010 @ 06:56:47 PM by rarerade.

Volume Serial Number and Harddisk Number is the same ?..

<New question>
Sir.kirsle I want to ask you some thing. How can i make .pl to use just 1 computer.
About .pl can check the computer key or serial number or Volume Serial Number.

EX. I give .pl program to my friend, I want .pl can use just his computer. If my friend save .pl to another computer he can't use it. ( How can I do like this Sir.)

&#3661;&#3661;You are very very important person,because no one help me like you.
Thank You For Helpping.

guest
guest
Posted on Sunday, March 13 2011 @ 02:46:30 PM by MANAS PRADHAN.

TELL ME HOW I CAN CHECK THE SERIAL NO GIVEN TO MY PEN DRIVE AT THE TIME OF PURCHASE , I NEED 2 KNOW AND MATCH THE ORIGINAL SERIAL NO WITH THE PEN DRIVE??
KINDLY REPLY ME SOON

Avatar
kirsle
Posted on Sunday, March 13 2011 @ 02:48:20 PM by Noah Petherbridge.

This only gets the DOS serial number given when the drive was formatted. I don't know how to get the hardware serial number given by the manufacturer though, sorry. :(

guest
guest
Posted on Wednesday, December 28 2011 @ 03:53:20 AM by Niko.

I follow your step, but on Macintosh HD my serial number is 0000-0000 and on DVDs\CDs it response: "Resource busy". If I search this SN with Windows on Mac HD or optical support I read my real SN why?

Thanks for your blog that is really important.

guest
guest
Posted on Sunday, April 22 2012 @ 12:25:17 AM by Adam.

Who still has floppy drives? I do lol!

Thanks for the one-liner for FAT12/16 serial numbers. It even adds that "-" in the right place!

I really appreciate people who put the time into these little things that add to a wealth of knowledge! Keep up the good work.

(If you need a floppy drive, I've got about 20 extras...)

guest
guest
Posted on Monday, January 21 2013 @ 10:58:16 PM by sanket.

I have a usb pen drive. I am developing an application which stores the serial number of the pen drive, so that it can check it the next time the pen drive is plugged in. Please provide me with all the steps required to find the serial number on a linux system.
Thanks !

Add a Comment

Your name:
Your Email:
Message:
Comments can be formatted with Markdown, and you can use
emoticons in your comment.

If you can see this, don't touch the following fields.



Kirsle
Channels
Creativity
Software
Web Tools
Subdomains
Miscellany
Links


Fan Club