HOWTO USB Mass Storage Device

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc
Wikipedia has an article on:

Contents

[edit] Configure The Kernel

cd /usr/src/linux
make menuconfig 

Make at least the following choices:

Linux Kernel Configuration: 2.4 kernel
SCSI support  --->
     <*> SCSI support
     --- SCSI support type (disk, tape, CD-ROM)
     <*>   SCSI disk support
File systems --->
     <*> DOS FAT fs support
     <*>   MSDOS fs support
     < >     UMSDOS: Unix-like file system on top of standard MSDOS fs
     <*>   VFAT (Windows-95) fs support 
USB support --->
     <*> Support for USB 
     <*>   UHCI (Intel PIIX4, VIA, ...) support 
     <*>   OHCI (Compaq, iMacs, OPTi, SiS, ALi, ...) support 
     <*>   USB Mass Storage support 

Alternatively choose some parts as modules, and additionally enable generic SCSI support (Be aware that your partition will NOT be checked at boot time by fsck when using modules!):

Linux Kernel Configuration: 2.4 kernel using modules
SCSI support  --->
     <*> SCSI support
     --- SCSI support type (disk, tape, CD-ROM)
     <*>   SCSI disk support
     <M>   SCSI generic support
File systems --->
     <M> DOS FAT fs support
     <M>   MSDOS fs support
     < >     UMSDOS: Unix-like file system on top of standard MSDOS fs
     <M>   VFAT (Windows-95) fs support 
USB support --->
     <*> Support for USB 
     <*>   UHCI Alternate Driver (JE) support
     < >   OHCI (Compaq, iMacs, OPTi, SiS, ALi, ...) support 
     <M>   USB Mass Storage support 

If it's a CD drive, enable SCSI CDROM support too:

Linux Kernel Configuration: 2.6 kernel
SCSI device support --->
     <*> SCSI CDROM support

Compile and install this kernel, then reboot.

If you have a several-in-one card reader, you'll probably have to also set Device Drivers --> SCSI device support --> [*] Probe all LUNs on each SCSI device (kernel 2.6). These readers are usually set up with one slot as the primary slot, so if this option isn't set, you won't be able to see the other slots.

[edit] Check Config

Run dmesg and make sure the USB device was detected:

Code: dmesg | grep -i usb
hub.c: new USB device 00:09.0-2, assigned address 3
usb-storage: act_altsettting is 0
usb-storage: id_index calculated to be: 47
usb-storage: Array length appears to be: 68
usb-storage: Vendor: JMTek
usb-storage: Product: USBDrive
usb-storage: USB Mass Storage device detected
usb-storage: Endpoints: In: 0xe55f7d40 Out: 0xe55f7d54 Int: 0xe55f7d68 (Period 255)
usb-storage: Found existing GUID 0c7600050000000000000000
WARNING: USB Mass Storage data integrity not assured
USB Mass Storage device found at 3 

If you get a message like the following

usb.c: USB device 4 (vend/prod 0xdda/0x2005) is not claimed by any active driver.

then you need to compile and/or load the correct driver. If you use modules you need to load the correct module (e.g. usb-storage), either manually by using modprobe or automatically by adding the module to /etc/modules.autoload.d/kernel-2.{4|6}.

[edit] Mounting the Drive

How to find the correct device to mount? You can find the device name by doing:

ls -l /dev/disk/by-id/usb*

On my machine this yields:

 /dev/disk/by-id/usb-kingstons-part1 -> ../../sdb1

So now I know that the device that should be mounted is /dev/sdb1.

You can also look at /proc/partitions

cat /proc/partitions

It shows all recognized devices that can be partitioned and all partitions. This is also a useful way to find out if your device has been recognized.

(Note that the following assumes that your device is /dev/sda1!)

Mount the drive:

mkdir /mnt/usbstick
mount -t vfat /dev/sda1 /mnt/usbstick 

If you can't mount then try the alternative kernel config above that enables SCSI generic support. Then do emerge sg3_utils and run:

sg_scan -i
sg_map

This should give you the correct device name. You can now mount with the correct name (e.g. /dev/sdb1). You can also try with auto or msdos (for small cards) as the filesystem type (mount -t msdos /dev/x /mnt/x).

File: /etc/fstab
# MY USB STICK
/dev/sda1               /mnt/stick      vfat            noauto,user,exec,sync     0 0

Note that you have to use vfat in the fstab file, because if you choose auto you won't get the full name but only the first 8 chars.

Add the option "sync" to have synchronous changes between the actual usb mass storage device and the place that it's been mounted. Otherwise it would be done while you unmount it. This has the advantage of not losing data if you forgot to unmount a device and also you would see how long it will take when you are transfering data to it.

You don't need to use vfat in the fstab file as long as /etc/filesystems is set up properly. Specifically, if vfat exists in /etc/filesystems before other fat file systems or by itself with an asterisk on the last line, it will be tried first during a mount. Read the mount (8) man page for more details.

[edit] Troubleshooting

If you have a vfat device and mount says "wrong fs type, bad superblock, etc" and dmesg shows errors like "FAT: cpage437 not found", you need to make a few changes in your kernel. Go into menuconfig and then open up "File systems", "Natural Language Support" and turn on "Codepage 437 (United States, Canada)" and "NLS ISO 8859-1 (Latin 1: Western European Languages)". Now either load these modules or reboot (depending on how you configured them) and the device should mount.

You may need other Codepages, such as Codepage 850 (European). Check dmesg for errrors:

Unable to load NLS charset cp850 FAT: codepage cp850 not found

[edit] Beware the Low Performance USB Block Driver

If your USB disk is shown as /dev/ub/a then you're using the low Performance USB Block Driver (Device Drivers -> Block Devices -> Low Performance USB Block Driver). You will find that your transfer speed is crippled by this driver, because it is designed to meet the lowest common denominator of features for seriously lightweight (or cheap) devices. Disable it and use the usb-storage module (devices > usb) instead if you can.

[edit] Adding symlink via udev

This section requires you to have UDEV support

Using udev you can make sure that, no matter what order you plug in your USB devices, the storage device is always at /dev/usbdrive (or whatever mount point you like).

First, find the ID of your usb drive by using /sys: (assuming the device is currently in /dev/sda)

# udevinfo -a -p `udevinfo -q path -n /dev/sda`

One of the values that gets output, at least on my device, is 'ATTRS{serial}="0402170100000020EB5D00000000000"'. We can use serial numbers like this to make rules in udev, so make a /etc/udev/rules.d/10-local.rules if you don't already have one, and put a line similar to this:

File: /etc/udev/rules.d/10-local.rules
 SUBSYSTEMS=="usb", ATTRS{serial}=="0402170100000020EB5D00000000000", KERNEL=="sd?1", NAME="%k", SYMLINK="usbdrive"

When writing rules for Card Readers identification for each slot can be done similar to this:

File: /etc/udev/rules.d/10-local.rules
 SUBSYSTEMS=="scsi", ATTRS{model}=="USB MS Reader   ", KERNEL=="sd?1", NAME="%k", SYMLINK="ms-card"

Then make udevd re-read the updated ruleset:

# udevcontrol reload_rules

That was failing in my case, but the following command did it:

# udevstart

Now, according to this example, the usb drive should always show up in /dev/usbdrive.

You can also write rules for several drives or partitions:

File: /etc/udev/rules.d/10-local.rules
SUBSYSTEMS=="usb", ATTRS{serial}=="200711020016023C", KERNEL=="sd?", NAME="%k", SYMLINK="usbdriveb"
SUBSYSTEMS=="usb", ATTRS{serial}=="200711020016023C", KERNEL=="sd?1", NAME="%k", SYMLINK="usbdriveb1"
SUBSYSTEMS=="usb", ATTRS{serial}=="100", KERNEL=="sd?", NAME="%k", SYMLINK="usbdrivea"
SUBSYSTEMS=="usb", ATTRS{serial}=="100", KERNEL=="sd?1", NAME="%k", SYMLINK="usbdrivea1"
SUBSYSTEMS=="usb", ATTRS{serial}=="100", KERNEL=="sd?2", NAME="%k", SYMLINK="usbdrivea2"
SUBSYSTEMS=="usb", ATTRS{serial}=="100", KERNEL=="sd?3", NAME="%k", SYMLINK="usbdrivea3"

[edit] fstab

The following fstab entry will allow others (non-root accounts) to access your USB Storage Device:

File: /etc/fstab
 /dev/sda1 /mnt/usbstick vfat noauto,user,umask=000 0 0 

The following fstab entries will allow others (non-root accounts) to access your USB Storage Device, the partitions will also be checked by fsck and mounted at boot time:

File: /etc/fstab
 /dev/usbdrivea1		/mnt/usbdisk_2  reiserfs	auto,noatime,notail,exec 1 2
 /dev/usbdrivea2		/mnt/usbdisk_1	ext2		auto,noatime,exec        1 2
 /dev/usbdrivea3		/mnt/usbdisk	reiserfs	auto,noatime,notail,exec 1 2
 /dev/usbdriveb1		/mnt/usb	reiserfs	auto,noatime,notail,exec 1 2

As you can see, you can use any valid options for mount and your partition types. In that case, support for reiserfs and ext2 must be compiled directly into the kernel and not as modules, as well that the USB support.

Of course, the different mount points (/mnt/usbstick, /mnt/usb, whatever...) must exist.

[edit] Partitioning

While most USB mass storage devices work out-of-the-box with no repartitioning, some people may want to partition and/or reformat their devices in order to gain the benefits of a linux filesystem. Partitioning a USB mass storage device is very similar to partitioning a normal IDE or SCSI hard drive. Since the most commonly used setup treats a USB device as a SCSI device, your device will probably show up as "/dev/sda". Then, to partition, you can run:

cfdisk /dev/sda

or:

fdisk /dev/sda

and partition as you would any other drive. Then format the drives as you would normally; for example, the following command creates an ext3 filesystem on the 2nd partition:

mke2fs -j /dev/sda2

NOTE: If one of your partitions is going to be FAT32/vfat for windows compatibility, you probably want to make it /dev/sda1, since the default configuration for USB Mass Storage (on thumb drives, digital cameras, flash card readers, cell phones, etc.) is to have a FAT32/vfat on /dev/sda1. It makes the most sense to place your native linux partitions on /dev/sda2 and beyond. This way you can keep an fstab consistent with most mainstream devices.

[edit] File system and compatibility

[edit] Making your ext2/3 USB mass storage device compatible with Windows

If you want to format your USB mass storage device with a Linux filesystem (that are better than the FAT filesystem (symlinks, posix, no 4GB limitation, ...)), you might need Windows compatibility. So there is project of porting ext2/3 filesystem to Windows (for reading/writing a Linux partition):

http://sourceforge.net/projects/ext2fsd/

  • it has read/write support
  • it works very well and is well-implemented inside Windows

(An additional ext2-only driver for Windows is EXT2IFS: http://www.fs-driver.org It works fine, implemented as a windows kernel IFS plugin. It allows the selection between your default 8-bit windows codepage and UTF-8 for the filename encryption).

WARNING: You should consider whether giving Windows knowledge of ext2/3 is desirable if you have Linux and Windows on the same computer. Next time your Windows system goes unstable or gets a virus it would have the means to alter your Linux file systems. A better choice for interoperability would be to keep VFAT on the device.

But if you want to make your ext2/3 USB mass storage device interoperable, then download the last version of ext2fsd and run it on the Windows system: http://sourceforge.net/project/showfiles.php?group_id=43775

Then format the device for example as follows:

  • a fat partition for hosting the ext2fsd utility
  • the rest of the space formatted in ext2/3

For the Mac see: http://freshmeat.net/projects/ext2fs/ (not tested).

[edit] See Also

Personal tools
In other languages