How do I get a shortcut to a USB drive in Linux?

I am trying to get a USB stick shortcut in my c / C ++ application. I am using libudev to get USB info. But it does not provide disk shortcuts. Does anyone have any idea on how to get the disk shortcut. I'm working on an embedded platform, it doesn't have a / dev / disk folder. Please help. Kernel version: 3.3.8

+3


source to share


3 answers


Usually the usb filesystem has a vfat partition on it to make it compatible across msdos, windows, linux and mac architectures.

The label is a property of the vfat filesystem. It looks normal as the first directory entry in the root directory and is marked as a filesystem label. Recent implementations of msdos filesystems (just vfat exfat and fat32) write it to the fixed part of the boot record for that partition too, so you can read it from there.

You have a volume serial number with offset 0x43 (4 bytes) in the first sector of the partition. You also have a copy of the 0x47 volume label in that first sector as well (11 bytes long)

Trick: Generally the usb handle is split (with only one section), you must:

  • look in the first sector of the USB stick for the partition table and find the first definition.
  • then look in the first sector of this parry, find byte offset 0x43 and use four bytes as the volume serial number (it matches the UUID = "..." in the / etc / fstab linux file) and the eleven bytes that follow the volume label.

Note

Be careful that NTFS does not use this space for this purpose, and you could damage the NTFS partition there. Just read from this place.



Note 2

Also, do not try to write to this location even on vfat filesystems, as they also keep a copy of the volume label in the root directory of the filesystem.

Note 3

The easiest way to get a shortcut to a dos filesystem (and ext [234], ntfs, etc.) on Linux is with blkid (8) , which gives the following output:

/dev/sda1: UUID="0b2741c0-90f5-48d7-93ce-6a03d2e8e9aa" TYPE="ext4" 
/dev/sda5: UUID="62e2cbf2-d847-4048-856a-a90b91116285" TYPE="crypto_LUKS" 
/dev/mapper/sda5_crypt: UUID="vnBDh3-bcaR-Cu7E-ok5D-oeFp-5SyP-MmAEsb" TYPE="LVM2_member" 
/dev/mapper/my_vg-root: UUID="1b9f158b-35b5-490e-b914-bdc70e7f5c28" TYPE="ext4" 
/dev/mapper/my_vg-swap_1: UUID="36b8ac81-7043-42ae-9f2a-908d53e2a2b3" TYPE="swap" 
/dev/sdb1: LABEL="K003_1G" UUID="641B-80BF" TYPE="vfat" 

      

As you can see, the last entry is for vfat usb pendrive, but you need to parse this output (I guess it's not hard to do)

+2


source


I believe that the "label" on the disk is a property supported by the file system it uses, that is, not at the USB level.



You need the correct implementation of the filesystem, that is, "mount" the disk.

+1


source


You can use blkid to read the USB device label:

blkid USB_PATH | grep -o ""LABEL.*"" | cut -d'\"' -f2

      

0


source







All Articles