Wednesday, March 11, 2009

How to mount/format a USB mass storage device (pen drive) in Linux

Using a USB mass storage device with Linux is not difficult at all. Linux is well equipped with command line utilities which can help accomplish this task in few easy steps.

Step1: Determine device file

When a USB device is connected to a machine running Linux, Linux kernel (brain of Operating System) will detect that a device has been connected to it and will log a message like this:

usb.c: registered new driver usb-storage
scsi3 : SCSI emulation for USB Mass Storage devices
Vendor: Generic Model: USB Disk Rev: 1.10
Type: Direct-Access ANSI SCSI revision: 02
Attached scsi removable disk sdb at scsi3, channel 0, id 0, lun 0
SCSI device sdb: 7897088 512-byte hdwr sectors (4043 MB)
sdb: Write Protect is off
sdb: sdb1 sdb2 sdb3 sdb4
WARNING: USB Mass Storage data integrity not assured
USB Mass Storage device found at 2
USB Mass Storage support registered.

The message above is obtained by running command
dmesg

dmesg command display message logged by Linux kernel. The message displayed above can be found at the end of log for a device connected a while ago. You can also run dmesg|tail to see last few lines.

The message above says that a USB device has been detected and the device file (don't bother what a device file is) is sdb for the whole device and 4 partitions are available at sdb1 sdb2 sdb3 sdb4. The device file are usually located under /dev directory. So
/dev/sdb is the device file for whole USB device and partitions are
/dev/sdb1
/dev/sdb2
/dev/sdb3
/dev/sdb4

Skip Step2 and Step3 if you don't want to change partitions and reformat partitions.

Step2: Alter partition table

fdisk utility helps in add/delete partitions. In our example, we have 4 partitions, lets delete them and create 1 partition of type FAT32.

Run fdisk as root user or sudo

1. Type sudo fdisk /dev/sdb to run fdisk program. It will display a command line interface.

#sudo fdisk /dev/sdb

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)


2. Type p to print partition table

Command (m for help): p

Disk /dev/sdb: 4043 MB, 4043309056 bytes
125 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 ? 100405 247697 570754815+ 72 Unknown
Partition 1 has different physical/logical beginnings (non-Linux?):
phys=(357, 116, 40) logical=(100404, 79, 11)
Partition 1 has different physical/logical endings:
phys=(357, 32, 45) logical=(247696, 24, 51)
Partition 1 does not end on cylinder boundary.
/dev/sdb2 ? 21767 271577 968014120 65 Novell Netware 386
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(288, 115, 43) logical=(21766, 48, 47)
Partition 2 has different physical/logical endings:
phys=(367, 114, 50) logical=(271576, 60, 42)
Partition 2 does not end on cylinder boundary.
/dev/sdb3 ? 241276 491086 968014096 79 Unknown
Partition 3 has different physical/logical beginnings (non-Linux?):
phys=(366, 32, 33) logical=(241275, 3, 30)
Partition 3 has different physical/logical endings:
phys=(357, 32, 43) logical=(491085, 14, 39)
Partition 3 does not end on cylinder boundary.
/dev/sdb4 ? 372346 372354 27749+ d Unknown
Partition 4 has different physical/logical beginnings (non-Linux?):
phys=(372, 97, 50) logical=(372345, 119, 25)
Partition 4 has different physical/logical endings:
phys=(0, 10, 0) logical=(372353, 14, 33)
Partition 4 does not end on cylinder boundary.

Partition table entries are not in disk order


3. Type d then 1 to delete first partition.

Command (m for help): d
Partition number (1-4): 1


4. Repeat command d to delete other partitions.

5. See current partition table with command p

Command (m for help): p

Disk /dev/sdb: 4043 MB, 4043309056 bytes
125 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes

Device Boot Start End Blocks Id System


Note: These changes are still not saved to the device. So if you want to quit, exit fdisk using command q

6. Create new partition with command n and then enter p for primary e for extended partition. If you want to have less than 5 partitions in your device, then enter p and create a primary partition.

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1018, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1018, default 1018):
Using default value 1018

Command (m for help): p

Disk /dev/sdb: 4043 MB, 4043309056 bytes
125 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 1018 3944719 83 Linux


7. The new partition is type Linux. If you want to create partition of type say "FAT32" then use t command to toggle partition's ID.

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (Win95 FAT32)


8. See you changes and if fine with it, then save changes and exit fdisk.

Command (m for help): p

Disk /dev/sdb: 4043 MB, 4043309056 bytes
125 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 1018 3944719 b Win95 FAT32

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

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.



Step3: Format partition

If you have create new partition(s), then fdisk command just create/alters partition table. The partition still need to be formatted to make it usable.

Command mkfs helps in formatting a partition. For example:

#sudo mkfs -t vfat /dev/sdb1
mkfs.vfat 2.8 (28 Feb 2001)


Step4: Mount a partition

When we connect a device to Windows, it mounts it automatically as some drive such as F:. In linux, we need to do it manually. Lets say we want to mount /dev/sdb1 (First partition) at /mnt/d. So create this directory
#sudo mkdir /mnt/d

and run command
#sudo mount -t vfat /dev/sdb1 /mnt/d to mount the partition.

Finally, We have mounted the USB device and its available for use under directory /mnt/d.


Step5: Unmount partition

To disconnect device, we have to unmount all the partitions previously mounted. This can be done as follows:
#sudo umount /dev/sdb1
or
#sudo eject /dev/sdb

With these easy steps, we can easily use USB mass storage devices with Linux.

4 comments:

Lokit said...

For more information visit USB Encryption, Encrypted Flash Drive and Linux Flash Drive

Unknown said...

I'm happy to see this helpful blog.
Files loss situation is generally an Inconvenience for the user.
As a way to resolve this challenge, the gurus have launched several personal information retrieval remedies.
End user have to be well aware of possible data files restoration tools to make sure that he is able to handle the info loss situation in a very proper way.
outlook pst file recovery

blasties said...

Thanks for this amazing article on "Mass Storage Devices" I was Just searching for NAS Storage products in 2020 and Landed on this amazing website of yours.

Edna said...

Thanks for this amazing article on USB Data I was Just searching forSan Francisco USB storage and found this amazing website of yours.