Using a USB flash drive with Linux
USB Flash drives are one of the coolest new tools available these days. And they're getting so cheap, it's easy to carry one around with you anywhere. Finally it looks like the USB flash drive will kill off the floppy once and for all – except for the lack of native Linux support.
I carry my files back and forth from the office to home with a 32MB USB flash drive. But transporting files from my Linux test machine at work typically involves two steps:
1. Copy the bits onto a Windows machine via Samba
2. Write those bits to my USB flash drive using the Wintel machine.
Now this is not really an arduous process, but wouldn't it be nice to use that flash drive directly in the Linux system? Well I recently figured out how to do that, and I thought I'd share my experience with you.
This tip was put together on a box running Red Hat 9, but the same procedure should yield the same results on any Linux distro.
To start off, you'll need to be logged in as root to set this up and to set permissions.
Verify that you have the needed kernel modules loaded. To find out what modules you have loaded, open a terminal window and type the following:
lsmod | more
The output of lsmod will look like this:
Module Size Used by Not tainted
nls_cp437 5116 0 (autoclean)
vfat 13004 0 (autoclean)
fat 38808 0 (autoclean) [vfat]
nls_iso8859-1 3516 0 (autoclean)
udf 98400 0 (autoclean)
ide-scsi 12208 0
soundcore 6404 6 (autoclean) [snd]
sd_mod 13516 0 (autoclean)
lp 8996 0 (autoclean)
parport 37056 0 (autoclean) [lp]
autofs 13268 0 (autoclean) (unused)
e100 60644 1
ipt_REJECT 3928 6 (autoclean)
iptable_filter 2412 1 (autoclean)
ip_tables 15096 2 [ipt_REJECT iptable_filter]
sg 36524 0 (autoclean)
sr_mod 18136 0 (autoclean)
scsi_mod 107160 4 [ide-scsi sd_mod sg sr_mod]
ide-cd 35708 0
cdrom 33728 0 [sr_mod ide-cd]
keybdev 2944 0 (unused)
mousedev 5492 1
hid 22148 0 (unused)
input 5856 0 [keybdev mousedev hid]
usb-uhci 26348 0 (unused)
usbcore 78784 1 [hid usb-uhci]
ext3 70784 2
jbd 51892 2 [ext3]
By default, Red Hat loads usb-uhci and usbcore on startup. But you'll need to load an additional module called usb-storage in order to get a flash drive working. To do this, simply type:
Next, we'll need to define a mount point for the USB flash drive, which includes a directory for the mount point. So go to the /mnt sub-directory and create this sub-directory.
Now we need to edit a file called fstab, which lives in the /etc directory. This file defines storage devices and the location of their mount-points.
Open the file using gedit, emacs or your text editor of choice. Its contents will look like this:
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
/dev/hda3 swap swap defaults 0 0
/dev/cdrom /mnt/cdrom udf,iso9660 noauto,owner,kudzu,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0
We need to add a line to this file that reads:
/dev/sda1 /mnt/usbstick vfat user,noauto,umask=0 0 0
You can copy/paste the above line directly into your fstab file.
The "sda1" represents the device name that the kernel gives the USB flash drive when it gets plugged in.
Once you've added this line to the fstab file, save it and close your text editor.
Now we're almost ready to plug in your USB flash drive. Open a second terminal window and type:
tail -s 3 -f /var/log/messages
This command will poll the kernel's message log every three seconds, and displays the latest messages the kernel has spat out. This is a useful debug tool to make sure the USB flash drive has been enumerated, and assigned a device name. Generally, the device name will be:
Now, go ahead and plug your flash drive into the USB port.
Up and Running
Once you've plugged the drive in, look at the terminal window where you're monitoring the kernel's event messages and verify that it has enumerated the USB device. You should see something like this:
Aug 26 17:06:09 localhost kernel: hub.c: new USB device 00:1f.2-1, assigned address 4
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Setup usb-storage for USB product d7d/100/100
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Setup nomadjukebox for USB product d7d/100/100
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Module setup nomadjukebox for USB product d7d/100/100
Aug 26 17:06:13 localhost kernel: SCSI device sda: 121856 512-byte hdwr sectors (62 MB) Aug 26 17:06:13 localhost kernel: sda: Write Protect is off
Aug 26 17:06:13 localhost kernel: sda: sda1 Aug 26 17:06:13 localhost devlabel: devlabel service started/restarted
The key event here is that the device was assigned as /dev/sda1. You can now mount the volume by typing:
If all has gone well, a disk icon will appear on your KDE/Gnome desktop and double-clicking on it will open a window that reveals the contents of your USB flash drive.
There's also a way to automate this process, where you can mount your USB flash drive without having to type anything at a command line. In Gnome, when you right-click anywhere on the desktop, one of the menu choices you have is Scripts, which is a quick and easy way to execute Bash scripts without having to open a terminal window. By default, there are no scripts in the folder that this menu points to, but there is an option to open that folder. Once in the folder, create a new text file and open it in your favorite text editor (we use gedit) to write the following script.
You can simply copy/paste what we have here into your Bash script:
We run the modprobe command just to make sure that the usb-storage module is loaded. If it's already loaded, there's no harm done, and if it wasn't already loaded, now it is.
Now save the script as something like mount usbstick, and copy it into the /root/.gnome2/nautilus-scripts sub-directory.
From Gnome/KDE, right-click on this script and go to the Permissions tab dialog. Set the script as executable by the appropriate groups/users, and click OK.
You'll want this script to be available to non-root users, so be sure to copy it to their respective sub-directories:
Now when you right-click on the desktop and go down to the Scripts menu choice, in the Scripts sub-menu you should see your mount usbstick script.
If you have your USB flash drive mounted as a volume, right-click on it, and the bottom menu choice should be Unmount Volume. Go ahead and unmount the volume and physically remove the USB flash drive.
Now go ahead and re-insert the flash drive into an available USB port. Next, right-click on the desktop, go into the Scripts sub-menu and execute your mount usbstick script. The drive icon for your flash drive should appear on your desktop, and you're ready to pull bits off of it or write bits to it to carry home.
Unfortunately this info is not from me.
I wish I could provide so much info in a row, but I'm still far from being a guru.
My source is here :
nice explanation...its nice when people figure stuff out and they post a howto for what they have done just in case someone else needs the same info some day...there are a few threads like this, but this one is nice and detailed...nice job! :D
It's probably not exactly the same thing, but with Mandrake 9.1, I was able to plug in a smartcard using the usb smartcard reader for my digital camera, and was able to read/write to it immediately without any fiddling. Pretty nifty stuff, but from the looks of it, the usb flash drives are different.
Is there a way to automate it even more, so all I want is that as I plug the USB mass-storage device an icon pops on my desktop and clicking that I can access the device.
I have configured USB to work and I can mount the device by hand and use it, but it would be neat if the icon appeared/disappeared as i plug/unplug my USB device(s).
one thing you can do is use autofs instead of adding that line to /etc/fstab
autofs is a means to automatically mount (or try to) devices when you first access them. It also handles unmounting when your all done (through a configurable idle timer)
I have this in my /etc/auto.misc:
Maybe I didn't make myself clear enough..
The following scene is my goal:
No USB attached -> no /dev/sda1 (or similar) icon on the desktop
Attach USB -> /dev/sda1 icon appears on the desktop
Remove USB -> /dev/sda1 icon disappears from the desktop
I've seen this happen in SuSE, but I'm running Debian now and don't have the SuSE live-eval cd anymore. (Though I could download it, but I'm low on disk space right now..)
hmm.. I now realize that the previous post was not a reply to my post, but to the first post instead. Sorry. But since I've gotten this far in writing, I'll go all the way to the end.
A far more intellectual approach would also recognize the device I've attached.. Instead of just giving me a mass-storage icon for my USB Flash, it would give a video camera icon for my camcorder. They both are accessed as usb-mass-storage devices. Since all the information can be found (ie. with usbview or cdrecord -scanbus or directly from /proc/bus/usb/devices or from kernel log), some intelligence could be applied.
I know I could hack some script for this, but it would be ugly and something I wouldn't bare to share with others. But I think the SuSE guys already have a decent solution, I'll try to find that out. (Or wait that someone posts it here :D )
OK, here's a question: what if my system doesn't have a device "/dev/sda1" (or similar) on it? how do i put it in?
I just bought a brand spankin' new Canon 300D camera and would like to direct-connect to my box with Mandrake 9.1. I have never used any other USB devices, so this is all new.
Everything else is no problem, but i don't have a device to mount from and have no idea how to get one. help!
I'd add a little bit more explanation about the /dev/sda1, such as, /dev/sdX1, in that X can be any letter depending on what scsi devices (N+1) you may already have connected to your box.
thanks for all the info membrax.... I'm having a little bit of a problem though...
I did all you said in your instructions, but somehow i keep getting this output after typing the tail command and plugging in the drive:
Oct 2 12:35:35 Solilofia kernel: SCSI device sda: 256000 512-byte hdwr sectors
Oct 2 12:35:35 Solilofia kernel: sda: Write Protect is off
Oct 2 12:35:35 Solilofia kernel: sda: sda1 sda2 sda3 sda4
Oct 2 12:35:35 Solilofia kernel: SCSI error: host 1 id 0 lun 0 return code = 8000002
Oct 2 12:35:35 Solilofia kernel: ^ISense class 7, sense error 0, extended sense 0
I suppose the information before the error is what I'm supposed to get, but what does this error mean? After trying to mount, I get this:
mount: wrong fs type, bad option, bad superblock on /dev/sda1,
or too many mounted file systems
After that, I ran an fdisk -l command and I got this:
Device Boot Start End Blocks Id System
/dev/sda1 ? 7679804 9857553 272218546+ 20 Unknown
Partition 1 has different physical/logical beginnings (non-Linux?):
phys=(356, 97, 46) logical=(7679803, 4, 9)
Partition 1 has different physical/logical endings:
phys=(357, 116, 40) logical=(9857552, 1, 1)
Partition 1 does not end on cylinder boundary.
In this case, what do I need to do? Thanks in advance for your help.
So is there no ultra-sapient linux guru that knows what these errors mean?
.... nevermind I already made it work... it turned out to be that my system didn't recognize it as ¨sda1¨ but just as ¨sda¨.... trial and error is a real bitch, but it works.
I have now a few hours experience with linux, mandrake 9, that's all. but I would like to use my usb memory stick as well as my firewire external harddrive without any command line stuff. can it be done the "drag and drop" and "doubleclick" way??
probably not. but if you are going to be using linux, the sooner you get your feet wet on the command line, the better off you'll be. Otherwise, you'll be a helpless newbie forever, quite frankly
saposmak, I'm having the same trouble now that you had apparently. I tried altering the fstab file to just reading /dev/sda (as well as sda1, sda2...), but I still can't get it to work. What did you do to fix it?
Windows doesn't seem to require usb devices to have valid partition tables because it treates them kind of like floppies. Linux does require a valid partition table because it treats them like hard-drives. The manufacturers seem to only support the windows world. After partitioning my usb drive properly, both windows and linux were happy. Being able to mount a device as /dev/sda probably indicates it has a bogus partition table - and you may find even though its mounted - it size or other parameters may be incorrect.
See my web-page for details - and for more info on how to deal with buggy usb chip implementations - which can also cause you grief...
|All times are GMT -5. The time now is 03:17 AM.|