Hieu,
Sorry if this is a bit pedantic (I'd rather give you the tools than write the code to give you your exact answer).
Start with the VolumeToBSDNode sample (
developer.apple.com). That doesn't quite do what you want but that's your first step.
Now, plug your USB storage device into your machine. When it mounts, open a Terminal window and execute the "df" command
e.g.
$df
Filesystem 512-blocks Used Avail Capacity Mounted on
/dev/disk0s2 487725344 322404632 164808712 66% /
/dev/disk4s2 2212616 702824 1509792 32% /Volumes/iDisk
/dev/disk5s1 2004912 1605848 399064 80% /Volumes/Key
disk0s2 (disk 0, slice 2) is my root device
disk4s2 is where my iDisk mounts. (disk4s2 is the bsd name)
disk5s1 is my USB Key
Find the bsd name that corresponds to you USB Key
now type in "ioreg -l" and you'll get a rather large dump in your Terminal window.
Find the bsd name that corresponds to your usb key (e.g. disk5s1)
Mine is:
| | | | | +-o Apple_HFS_Untitled_1@1 <class
IOMedia, registered, matched, active, busy 0, retain count 8>
| | | | | | {
| | | | | | "Leaf" = Yes
| | | | | | "Writable" = Yes
| | | | | | "BSD Minor" = 17
| | | | | | "IOBusyInterest" = "IOCommand is not serializable"
| | | | | | "Partition ID" = 1
| | | | | | "Preferred Block Size" = 512
| | | | | | "UUID" = "DD42F217-92C7-416C-84A6-E0296AE9BDA7"
| | | | | | "BSD Major" = 14
| | | | | | "BSD Name" = "disk5s1"
| | | | | | "Size" = 1026514944
| | | | | | "Content Hint" = "48465300-0000-11AA-AA11-00306543ECAC"
| | | | | | "Removable" = Yes
| | | | | | "BSD Unit" = 5
| | | | | | "Ejectable" = Yes
| | | | | | "Content" = "48465300-0000-11AA-AA11-00306543ECAC"
| | | | | | "Whole" = No
| | | | | | }
| | | | | |
Scroll up and you will see an IOMediaBSDClient, an IOMedia (where
"Whole" = Yes), ... and further up the food chain you will see an
IOUSBMassStorageClass and then then IOUSBInterface (etc., etc). Some
people prefer to use IORegistryExplorer (/Developer/Applications/...).
Now do the same for the bsd name that corresponds to your root device. Notice any differences that you could search for? :-)
Now, from user space each of these entries (like the one I pasted in,
above) are called "io_registry_entry_t", and going up the food chain
corresponds to calling IORegistryEntryGetParentIterator() (couple that
with IOIteratorNext(), IOObjectConformsTo() and IOObjectRelease() and
there's your more or less complete tool set).
VolumeToBSDNode is a nice little project to play around in.
hth,
Cheers,
-H.