Format a USB drive under unix
Posted by davidnewcomb on 03 Jan 2012 in System Admin
In order to format a USB device under Unix you need to follow these simple steps under the
root
user account.
Find out the name of the device file representing the USB hardware device:
- If the system is busy I clean out the
dmesg
to make it easier to find the reports when the USB device is connected. I clear the existing log to a date stamped file:dmesg -c > dmesg.`date +%Y-%m-%d-%H-%M-%S`
- Plug in the USB device.
- Type:
dmesg
again and you should get something like:usb 1-5: new high speed USB device using ehci_hcd and address 4 usb 1-5: configuration #1 chosen from 1 choice Initializing USB Mass Storage driver... scsi4 : SCSI emulation for USB Mass Storage devices usbcore: registered new driver usb-storage USB Mass Storage support registered. usb-storage: device found at 4 usb-storage: waiting for device to settle before scanning Vendor: WDC WD15 Model: EARS-00S0XB0 Rev: 80.0 Type: Direct-Access ANSI SCSI revision: 05 SCSI device sdb: 2930277168 512-byte hdwr sectors (1500302 MB) sdb: Write Protect is off sdb: Mode Sense: 23 00 00 00 sdb: assuming drive cache: write through SCSI device sdb: 2930277168 512-byte hdwr sectors (1500302 MB) sdb: Write Protect is off sdb: Mode Sense: 23 00 00 00 sdb: assuming drive cache: write through sdb: unknown partition table sd 4:0:0:0: Attached scsi disk sdb sd 4:0:0:0: Attached scsi generic sg2 type 0
You can see that the kernel has assignedsdb
as the block device (i.e./dev/sdb
). You can also see that the kernel was not able to detect the partition type of the drive
fdisk
.
- Launch the program telling it which device file to look at:
fdisk /dev/sdb
- If you get the following message then ignore it because doing a write will just create an empty partition which isn’t what we want.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
- Add a partition by pressing n (for new partition).
Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-182401, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-182401, default 182401): 182401
I only want one partition and for that partition to contain the whole drive. If you wanted a different number of partitions then here would be the place to set that up. - Next up, we have to set the type of the filesystem. So hit t.
Selected partition 1 Hex code (type L to list codes): 7 Changed system type of partition 1 to 7 (HPFS/NTFS)
- Then look over what we have done.
Disk /dev/sdb: 1500.3 GB, 1500301910016 bytes 255 heads, 63 sectors/track, 182401 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 182401 1465136001 7 HPFS/NTFS
- Finally enter w to write the partition table and quit.
- Typing
dmesg
again gives:SCSI device sdb: 2930277168 512-byte hdwr sectors (1500302 MB) sdb: Write Protect is off sdb: Mode Sense: 23 00 00 00 sdb: assuming drive cache: write through sdb: sdb1
- The first thing we need to do is format the drive and create a filesystem. Issue the following command. There should be a longish wait (1.5TB drive took about 20 seconds.)
# mkfs -t vfat /dev/sdb1 mkfs.vfat 2.11 (12 Mar 2005)
- Now we are ready. We can mount the USB drive:
mount -t vfat /dev/sdb1 /mnt
- For confirmation we’ll check the mounted partitions with the command
df -h
to give:Filesystem Size Used Avail Use% Mounted on /dev/sda1 388G 30G 338G 9% / /dev/sda2 436G 366G 48G 89% /home tmpfs 1013M 0 1013M 0% /dev/shm /dev/sdb1 1.4T 16K 1.4T 1% /mnt
- Don’t forget to edit your backup scripts to reflect the new device name.
No feedback yet
Form is loading...