lobo_tuerto's notes
for greatly improving DX
Blog
Notes
About

Format a USB drive as FAT32 in Linux

This can be useful if you need to flash some BIOS or interact with a Windows machine file system.

📅Date26 October 2019
🏷️
fat32linuxusb

Why FAT32

Although in theory no one should be using these file systems anymore, in practice, sometimes you just need one, for example for flashing your motherboard’s BIOS or some other arcane task. Or maybe just for having a drive that’s compatible with Windows.

Whatever the reason, here is a guide on how to achieve this using Linux.

First, correctly identify the drive location

Open a terminal and type:

sudo fdisk -l

You’ll see a list of storage devices connected to your computer and their partitions —if any.

You need to identify the one you just connected. It’s very easy if your devices are of different sizes, since that accurately pinpoint the drive you want to work with.

PLEASE MAKE SURE you identify the drive correctly, as the following procedure will wipe EVERYTHING on it with NO RECOVERY chance.
You’ve been warned!

I’ll be using a 1GB drive for this guide.
The data for this device using sudo fdisk -l looks like this:

Disk /dev/sdb: 983.51 MiB, 1031274496 bytes, 2014208 sectors
Disk model: DataTraveler II
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

So, my drive is /dev/sdb. Yours could be /dev/sdc or something else.

Pay careful attention.

Optional: Write zeros to the drive

This is optional.

But, for security reasons, and to verify that there are no outstanding problems with the drive, I recommend to do it every once in a while.

We’ll use the venerable dd command for that:

sudo dd if=/dev/zero of=/dev/sdb bs=1M status=progress conv=fdatasync

When finished, you’ll see something like this:

889000000 bytes (889 MB, 848 MiB) copied, 2 s, 444 MB/s
dd: error writing '/dev/sdb': No space left on device
1032+0 records in
1031+0 records out
1031274496 bytes (1.0 GB, 984 MiB) copied, 126.28 s, 8.2 MB/s

The write speed varies a lot depending on the type of drive you have.

Create the DOS partition table

Let’s open the disk with fdisk.

sudo fdisk /dev/sdb

You’ll see something along these lines:

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xad186630.

Command (m for help):

As you can see, it automatically created a new empty DOS partition table for us.

If you want to see the available options enter m for help:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Pressing o would give us the same result —a new empty DOS partition table.

Now, let’s write this brand new partition table to the /dev/sdb disk by pressing w:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

After the operation takes place, it’ll exit automatically.
Let’s re-open the drive, and enter the following sequence of commands:

sudo fdisk /dev/sdb
n
<accept all defaults>
t
b
p
w
  1. n => New partition —accept all defaults for partition type, partition number, first sector and last sector, so it takes all the space available on the device
  2. t => Changes a partition type
  3. b => Picks W95 FAT32
  4. p => Shows partition info
  5. w => Writes changes and exit

This is the output from the commands above:

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-2014207, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2014207, default 2014207): 

Created a new partition 1 of type 'Linux' and of size 982.5 MiB.

Command (m for help): t

Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): b
Changed type of partition 'W95 FAT32' to 'W95 FAT32'.

Command (m for help): p
Disk /dev/sdb: 983.51 MiB, 1031274496 bytes, 2014208 sectors
Disk model: DataTraveler II
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x02294794

Device     Boot Start     End Sectors   Size Id Type
/dev/sdb1        2048 2014207 2012160 982.5M  b W95 FAT32

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now, if you look at your drive info with sudo fdisk -l you’ll see something like this:

Disk /dev/sdb: 983.51 MiB, 1031274496 bytes, 2014208 sectors
Disk model: DataTraveler II
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x02294794

Device     Boot Start     End Sectors   Size Id Type
/dev/sdb1        2048 2014207 2012160 982.5M  b W95 FAT32

Create a new FAT32 file system

Now, let’s create a FAT32 file system on it so we can use the drive and copy files to it.

sudo mkfs.vfat -F 32 -n MYDRIVE /dev/sdb1

Output is like:

mkfs.fat 4.1 (2017-01-24)

That’s it, your drive should be already accessible from your file explorer and be ready to copy that pesky BIOS flash file to it! 🎉


Got comments or feedback?
Follow me on
rev-7175614