skip navigation

www.Hilands.com


Content:: Linux - Mounting a USB Drive

Mounting a USB Drive Thumb Drive in Linux

Most user friendly distrubutions of linux will automatically detect and mount a USB Thumb Drive. If you are using a thin or light distrubtion you may have to mount the USB drive manually. I use the a light version of debian called the netinst version.
This article will explain how to use the Linux CLI or "Command Line Interface" to use the kernel message log to find your USB Thumb Drive. Determine which device file the kernel sets the drive to. Creating a location to mount the device. Mounting the USB Thumb Drive. Securily erasing and reformatting the drive and validating the erasing process.


Using dmesg to locate your USB Thumb Drive
Mounting a drive in linux is quite simple. The first part is to determine where the device is. We will find the device using the command "dmesg" or "Display Message". Display Message, "dmesg", will display the kernels message buffer. It is commonly recommended to "pipe" the dmesg command through tail. Chaining commands with pipe, "|", will take the standard output from the first program and have it processed by the second command. In this case the standard output from dmesg will be sent to tail. The tail command will then truncate the standard output to the last ten lines. # dmesg |tail The output of tail won't always show us all of the output we require. This can sometimes be the case when trying to find out where the USB drive is. For this case we will pipe the output of dmesg to the program less. # dmesg |less
Installing Less if the command is not found
The following message tells us that we have not installed the program less.
-bash: less: command not found
We can remedy this by using the apt-get command. # apt-get install less
Understanding the dmesg data
Inside less you can use the less than and greater than keys to go to the beginning and the end of the document. In this case we want to go to the bottom of the file using the greater than than key ">" (hold down the shift key and press the period key ".") The "q" key will allow us to exit less.

We should now see something similar to the following. You will notice the vendor of the device in this case SanDisk and the model Cruzer Mini.
usb 4-4: new high speed USB device using ehci_hcd and address 3
usb 4-4: configuration #1 chosen from 1 choice
scsi1 : SCSI emulation for USB Mass Storage devices
usb-storage: device found at 3
usb-storage: waiting for device to settle before scanning
  Vendor: SanDisk   Model: Cruzer Mini       Rev: 0.2
  Type:   Direct-Access                      ANSI SCSI revision: 02
SCSI device sda: 501759 512-byte hdwr sectors (257 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 00
sda: assuming drive cache: write through
SCSI device sda: 501759 512-byte hdwr sectors (257 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 00
sda: assuming drive cache: write through
 sda: sda1
sd 1:0:0:0: Attached scsi removable disk sda
usb-storage: device scan complete
For my Western Digital USB external harddrive I saw the following message. Note - It noticed the virtual CD on the drive.
[5179501.157781] usb 4-4: Product: My Book 1110
[5179501.157784] usb 4-4: Manufacturer: Western Digital
[5179501.157787] usb 4-4: SerialNumber: 574341565934333037303136
[5179501.368586] Initializing USB Mass Storage driver...
[5179501.369715] scsi0 : SCSI emulation for USB Mass Storage devices
[5179501.369867] usbcore: registered new interface driver usb-storage
[5179501.369873] USB Mass Storage support registered.
[5179501.371683] usb-storage: device found at 2
[5179501.371688] usb-storage: waiting for device to settle before scanning
[5179506.368184] usb-storage: device scan complete
[5179506.369052] scsi 0:0:0:0: Direct-Access     WD       My Book 1110     2003 PQ: 0 ANSI: 4
[5179506.369909] scsi 0:0:0:1: CD-ROM            WD       Virtual CD 1110  2003 PQ: 0 ANSI: 4
[5179506.370657] scsi 0:0:0:2: Enclosure         WD       SES Device       2003 PQ: 0 ANSI: 4
[5179506.513097] Driver 'sd' needs updating - please use bus_type methods
[5179506.515641] sd 0:0:0:0: [sda] 3905656832 512-byte hardware sectors (1999696
 MB)
[5179506.517366] sd 0:0:0:0: [sda] Write Protect is off
[5179506.517372] sd 0:0:0:0: [sda] Mode Sense: 23 00 10 00
[5179506.517376] sd 0:0:0:0: [sda] Assuming drive cache: write through
[5179506.518486] sd 0:0:0:0: [sda] 3905656832 512-byte hardware sectors (1999696 MB)
[5179506.520377] sd 0:0:0:0: [sda] Write Protect is off
[5179506.520384] sd 0:0:0:0: [sda] Mode Sense: 23 00 10 00
[5179506.520387] sd 0:0:0:0: [sda] Assuming drive cache: write through
[5179506.520412]  sda: sda1
[5179506.535872] sd 0:0:0:0: [sda] Attached SCSI disk
[5179506.571893] Driver 'sr' needs updating - please use bus_type methods
[5179506.575352] sr0: scsi3-mmc drive: 51x/51x caddy
[5179506.575489] sr 0:0:0:1: Attached scsi CD-ROM sr0
[5179506.604977] Driver 'ses' needs updating - please use bus_type methods
[5179506.604998] ses 0:0:0:2: Attached Enclosure device
[5179506.619771] sd 0:0:0:0: Attached scsi generic sg0 type 0
[5179506.619811] sr 0:0:0:1: Attached scsi generic sg1 type 5
[5179506.619842] ses 0:0:0:2: Attached scsi generic sg2 type 13
With the output from dmesg we can now see the USB drive is being read under SCSI emulation. Notice all the lines starting with "sda:", this is telling us the device is loaded into the sda device pool. Further down we see the line" sda: sda1" which is telling us the device sda1 is data on our USB drive.


Verify the /dev folder
We should now see a new file inside the "/dev" folder. ls /dev/sda* or ls /dev |grep sda The output should show us the devices the kernel found.
sda
sda1

Creating a directory to mount the USB Thumb Drive
Linux allows us to mount a device as a directory, or folder, in the file system. I tend to create a sub directory in "/mnt" to load devices like a usb drive. First we can view the folders that currently exist in the "/mnt" directory. # ls /mnt the output will most likely show a cdrom drive folder
cdrom
We will now create a usb folder inside of mount. # mkdir /mnt/usb
Mounting the Device
Now that we have somewhere to mount the drive we use the "mount" command. We will use the -t switch to specify the type of formatting the usb device is using. Most USB thumb drives are formated with 16 bit fat. The mount command knows this as vfat. Next we will specify the device to use "/dev/sda1" and then the directory we will be using "/mnt/usb" # mount -t vfat /dev/sda1 /mnt/usb/
# mount -t vfat <device discovered in dmesg> /mnt/usb

For the USB harddrive we can use the following. # mount /dev/sda1 /mnt/usb/ If you try to write to a file or create a directory on the harddrive and you do not have permissions it may be because the drive is running an NTFS file system. We can use df with the -T switch for file system and h for human readable output (for the sizes) # df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda1     ntfs    1.9T  258G  1.6T  14% /mnt/wdc
I read a few places that stated mounting with the -o rw option to enable read and write access to the drive. This DID NOT work for me. So a little bit of hunting and I found ntfs-3g. mount -t ntfs /dev/sda1 /mnt/wdc/ -o rw
Install ntfs-3g with apt-get. # apt-get install ntfs-3g
To mount the drive we will mount it with the type ntfs-3g # mount -t ntfs-3g /dev/sda1 /mnt/wdc/
Running a df again we will notice the type of drive mounted is fuseblk # df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda1  fuseblk    1.9T  258G  1.6T  14% /mnt/usb

Un-Mounting the Device
To unmount the device you can use the umount command. # umount /mnt/usb/ To safely remove the device you can use the eject command # eject /dev/sda1
# eject <device discovered in dmesg>
Secure erase and re-formatting your USB Thumb Drive
USB Thumb Drives are small and easy to loose and steal. There are many reasons to keep a drive clean and this is how we do it.
We will make use of the dd command and the zero and rand features of your operating system. Using the information we have above regarding the device file we can push random characters and zero's to the usb thumb drive.

First we will write random garbage to the drive using /dev/urandom we can run the command multiple times to overwrite the data to make recovery nearly impossible. If we wanted to leave the partition tables we could set write the random data to sda1 instead of sda, we would then skip the need to run fdisk on the device. # dd if=/dev/urandom of=/dev/sda We will follow the random data with a clean wipe of zeros # dd if=/dev/zero of=/dev/sda
dd: writing to `/dev/sda': No space left on device
501760+0 records in
501759+0 records out
256900608 bytes (257 MB) copied, 1.29539 seconds, 198 MB/s
Now that we have a nice clean USB Thumb Drive we need to format it for use. By default mkfs.vfat is not installed on the basic debian installation. We will use apt-get to install the mkdosfs tools allowing us to format the USB Thumb Drive. # apt-cache search mkdosfs
# apt-get install dosfstools First we will use the fdisk command to create a partition on the drive.

Before you format the thumb drive you need to be certain what device the drive is loaded as. If you did not check your kernel message log with dmesg do not run this command. # fdisk /dev/sda
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1011, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1011, default 1011):
Using default value 1011

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# mkfs -t vfat /dev/sda1 You should get the following response
mkfs.vfat 2.11 (12 Mar 2005)

Viewing the data on your drive with a hexeditor
To check and see if the data has been written we will use Ncurses hexedit by Adam Rogoyski.
http://www.rogoyski.com/adam/programs/hexedit/
With debian we can do a search of hexeditors # apt-cache search hexedit We can see the package as ncurses-hexedit # apt-get install ncurses-hexedit The binary executable in debian is called hexeditor. We will use hexeditor with the -d flag to call a device. # hexeditor -d /dev/sda1
Last Modified: 2011-02-02