How can I reformat a Raspberry Pi SD card to FAT32 using another Raspberry Pi?

The Raspberry Pi SD card has two partitions. One is Linux and the other is W95 FAT32. Now I'd like to use the other Raspberry Pi (Running Raspbian) to format this SD card to FAT32, so that I can use it as a normal USB stick on Windows PCs?
condor888Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ivan rosaCommented:
Hello condor,

For Windows
http://www.partitionwizard.com/free-partition-manager.html

For Linux install
GPARTED

For MAC
Open "Disk utilities"

Ps all those are FREE
condor888Author Commented:
Hi Ivan, thanks for your comment! I'd prefer to do this via comment line under linux (on another Raspberry Pi running Rasbian preferably).
ivan rosaCommented:
Oh yeah, it is also possible but you would need to figure out first how to mount the USB in the console, then you would have to use the commands of

Identification the USB
Fdisk -l
The wiping
dd if=/dev/zero of=/dev/hdX

Be aware though Fatal damage to your current OS could happen if you are picking the incorrect USB, personally it would be way too much time and putting your raspbian in jeopardy vs using a regular PC
Dan CraciunIT ConsultantCommented:
Do you have another card attached via a card reader to the PI?

Cause it sound to me that you want to format the card from where you loaded the OS.
It can be done, but any mistake is irreversible.

HTH,
Dan
condor888Author Commented:
Hi Dan and Ivan, I have a Raspberry Pi and I want to use the Pi to format another SD card that has a Pi's image on it. I am formatting the card with an external card reader. Sorry about the confusion.
Dan CraciunIT ConsultantCommented:
OK.

From the command line, use fdisk or parted.
condor888Author Commented:
Hi Dan, I know that I could run fdisk -> d -> 1 -> n to delete the original partitions and create a new one. But I don't know how to format the new partition to FAT32 which is just like a normal thumb drive in order to use on Windows. Thanks!
Dan CraciunIT ConsultantCommented:
umount /dev/sdaX
mkdosfs -F 32 -I /dev/sdaX

where /dev/sdaX is the partition you want to format (you find that using fdisk -l)
condor888Author Commented:
does mkfs.vfat do the same thing? I tried mkfs.vfat but it doesn't seem to work.
Dan CraciunIT ConsultantCommented:
gheistCommented:
Use official sd card formatter on windows.
Gerwin Jansen, EE MVETopic Advisor Commented:
>> I tried mkfs.vfat but it doesn't seem to work.
Was creating the filesystem not successful? Or did the format finish but could you not read the formatted device? Or was there some other error, if so what was it?
gheistCommented:
Gerwin: official formater reprograms SD-card to avoid burned out flash sectors. It is highly desirable if other formatting ways dont work.
Linux way going as close to official formatter as we can should be (assuming recent distribution that aligns disk structures to :2^n sectors, not %63=0)
fdisk /dev/mmcblk0
n p 1 (1st partition must start at 1M aka 2048)
c 1 (type = 1 FAT12)
then
mkfs.vfat /dev/mmcblk0p1
Gerwin Jansen, EE MVETopic Advisor Commented:
Burned out? Any reference to that?
Besides the fact that asker may not have a Windows machine, a photo camera can also be used to format SD cards, they have a built-in formatter.
condor888Author Commented:
@Gerwin, when I used mkfs.vfat, it doesn't seem to be able to create FAT32 partition. So I'll probably try to use "mkdosfs -F 32 -I /dev/sdaX" as Dan pointed out. Do you think that'll work when use a rpi to format an external SD card to FAT32?
condor888Author Commented:
@gheist, Screen-Shot-2015-10-24-at-23.59.42.png, as the above picture shows, the DOS compatibility flag (pressing c) seems to be deprecated.

Also after using mkfs.vfat to format, the /dev/sda1 (external drive partition) is Linux, not FAT. Please see the following two pictures.
Screen-Shot-2015-10-25-at-00.04.35.pngScreen-Shot-2015-10-25-at-00.06.06.png
FYI, the whole sequence of operations is as follows:
sudo fdisk /dev/sda
(use d to delete the existing partitions)
n p 1 <enter> <enter>
c w

sudo mkfs.vfat /dev/sda1

Please advise. Thanks!
condor888Author Commented:
I ran "sudo mkdosfs -F 32 -I /dev/sda1" this as well as Dan suggested. But it does't seem to work as well. Please see the following screencast. The "System" is still "Linux".
Screen-Shot-2015-10-25-at-00.24.25.png
Gerwin Jansen, EE MVETopic Advisor Commented:
I can test for you later today.
condor888Author Commented:
Thanks Gerwin. Basically my requirement is with a rpi (here I use it just as a Linux machine) to change an external SD card (loaded with rpi image, but not the currently working one) back to FAT32 so that it is a normal SD card that can be used on Windows.
gheistCommented:
Unmount first (do not open SD card in PI.
Instrad of c you must type t (partition type)
Gerwin Jansen, EE MVETopic Advisor Commented:
Use fdisk to remove existing partitions (d), then add one new (p), change type (to c for FAT32) and write changes (w). Then use mkdosfs to create the FAT32 file system.

Start:
root@raspberrypi:/home/samba# fdisk -l /dev/sdb

Disk /dev/sdb: 8068 MB, 8068792320 bytes
4 heads, 16 sectors/track, 246240 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000baeb7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192     2511718     1251763+   e  W95 FAT16 (LBA)
/dev/sdb2         2514944    15693823     6589440   85  Linux extended
/dev/sdb3        15693824    15759359       32768   83  Linux
/dev/sdb5         2523136     2646015       61440    c  W95 FAT32 (LBA)
/dev/sdb6         2654208    15693823     6519808   83  Linux

Open in new window

Partitions removed:
Command (m for help): p

Disk /dev/sdb: 8068 MB, 8068792320 bytes
4 heads, 16 sectors/track, 246240 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000baeb7

   Device Boot      Start         End      Blocks   Id  System

Open in new window

New partition creation:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-15759359, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15759359, default 15759359): 
Using default value 15759359

Command (m for help): p

Disk /dev/sdb: 8068 MB, 8068792320 bytes
4 heads, 16 sectors/track, 246240 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000baeb7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    15759359     7878656   83  Linux

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

Open in new window

Write new partition table and create file system:
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.
root@raspberrypi:/home/samba# 

root@raspberrypi:/home/samba# mkdosfs -v -F 32 /dev/sdb1
mkdosfs 3.0.13 (30 Jun 2012)
/dev/sdb1 has 249 heads and 62 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 15757312 sectors;
file system has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 15360 sectors, and provides 1965820 clusters.
There are 32 reserved sectors.
Volume ID is 75fa3ea6, no volume label.
root@raspberrypi:/home/samba#

Open in new window

New partition:
root@raspberrypi:/home/samba# fdisk -l /dev/sdb

Disk /dev/sdb: 8068 MB, 8068792320 bytes
4 heads, 16 sectors/track, 246240 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000baeb7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    15759359     7878656    c  W95 FAT32 (LBA)
root@raspberrypi:/home/samba# 

Open in new window


The card is now FAT32 formatted and usable

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
condor888Author Commented:
Hi Gerwin, thanks, that works!

But one thing I don't understand is that you did "change type (c for FAT32) " and then "mkdosfs -v -F 32 /dev/sdb1", what are the difference between the two? Why do we need to do the FAT32 thing twice?
condor888Author Commented:
And Gerwin, I have another question for you. Suppose I have a SD card loaded with rpi image, and I want to refresh the image to a new version. Do I have to format the SD card to FAT32 before I use dd to write the rpi image to the card?
gheistCommented:
No, you do not need to format extra time.
There is no difference between mkfs.vfat and mkfs.msdos
condor888Author Commented:
Hi Gerwin, sorry about the delay, I still have a question for you.

But one thing I don't understand is that you did "change type (c for FAT32) " and then "mkdosfs -v -F 32 /dev/sdb1", what are the difference between the two? Why do we need to do the FAT32 thing twice?
gheistCommented:
You can do once, open a new question and ask why such injustice...
Your question states HOW ... ? Which means that you get over motivation part yourself.
condor888Author Commented:
Hi gheist, thanks for providing some great input comments for this question. But if you check the above thread, you can find that I ask this before to Gerwin but was not answered. You did contribute some input but wasn't directly targeted at this (or maybe I didn't get it right). I think that's the crucial part of the thread since it seems to be the only difference between my original approach and Gerwin's method. So it'll be great if he can provide just a little bit more explanation on that. Thanks for you all again.
Gerwin Jansen, EE MVETopic Advisor Commented:
Hello condor888, I always create a partition first before I create the file system (habit). So I change (c) the partition type to FAT32 before I do the 'format'. Note that the mkfs command has a partition parameter (/dev/sdb1) and not a device (/dev/sdb) to know where it needs to create the file system, the type (-F 32) would be redundant because the partition type has been set already. I never checked what mkfs command does when you try to run it without creating a partition first. We can try this of course.
condor888Author Commented:
Hi Gerwin, thanks for your quick reply. But I think if we only do mkfs without changing the partition type to FAT32 based on my test. That is why I asked this question. Maybe I did something wrong though. When you get a chance, would you please have a try on that?
Gerwin Jansen, EE MVETopic Advisor Commented:
>> When you get a chance, would you please have a try on that?
Of course - later today. Will post back the results.
Gerwin Jansen, EE MVETopic Advisor Commented:
It's quite a log that is posted below, the result is that mkdosfs will not make a partition for you. You need to do that upfront. That and set the partition type to FAT32 before formatting. I left partition type to Linux and tried to format, that didn't work.
Disk /dev/sda: 8068 MB, 8068792320 bytes
249 heads, 62 sectors/track, 1020 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): q

root@raspberrypi:~# mkdosfs -v -F 32 /dev/sda1
mkdosfs 3.0.13 (30 Jun 2012)
/dev/sda1: No such file or directory
root@raspberrypi:~# mkdosfs -v -F 32 /dev/sda 
mkdosfs 3.0.13 (30 Jun 2012)
mkdosfs: Device partition expected, not making filesystem on entire device '/dev/sda' (use -I to override)
root@raspberrypi:~# mkdosfs -v -F 32 -I /dev/sda
mkdosfs 3.0.13 (30 Jun 2012)
/dev/sda has 249 heads and 62 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 15759360 sectors;
file system has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 15360 sectors, and provides 1966076 clusters.
There are 32 reserved sectors.
Volume ID is 921f017f, no volume label.
root@raspberrypi:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
249 heads, 62 sectors/track, 1020 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

root@raspberrypi:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
249 heads, 62 sectors/track, 1020 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-15759359, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15759359, default 15759359): 
Using default value 15759359

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

Calling ioctl() to re-read partition table.
Syncing disks.
root@raspberrypi:~# mkdosfs -v -F 32 /dev/sda1
mkdosfs 3.0.13 (30 Jun 2012)
/dev/sda1 has 249 heads and 62 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 15757312 sectors;
file system has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 15360 sectors, and provides 1965820 clusters.
There are 32 reserved sectors.
Volume ID is 97535fe6, no volume label.
root@raspberrypi:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
204 heads, 14 sectors/track, 5517 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    15759359     7878656   83  Linux

Command (m for help): 

root@raspberrypi:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
204 heads, 14 sectors/track, 5517 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    15759359     7878656   83  Linux

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

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
204 heads, 14 sectors/track, 5517 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    15759359     7878656    c  W95 FAT32 (LBA)

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.
root@raspberrypi:~# mkdosfs -v -F 32 /dev/sda1
mkdosfs 3.0.13 (30 Jun 2012)
/dev/sda1 has 249 heads and 62 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 15757312 sectors;
file system has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 15360 sectors, and provides 1965820 clusters.
There are 32 reserved sectors.
Volume ID is 9d7734ee, no volume label.
root@raspberrypi:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 8068 MB, 8068792320 bytes
204 heads, 14 sectors/track, 5517 cylinders, total 15759360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    15759359     7878656    c  W95 FAT32 (LBA)

Command (m for help): q

root@raspberrypi:~# ls /mnt
root@raspberrypi:~# mkdir /mnt/sda1
root@raspberrypi:~# mount /dev/sda1 /mnt/sda1
root@raspberrypi:~# mount | grep sda
/dev/sda1 on /mnt/sda1 type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)
root@raspberrypi:~# df /mnt/sda1
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sda1        7863280     4   7863276   1% /mnt/sda1
root@raspberrypi:~# 

Open in new window

condor888Author Commented:
Hi Gerwin, thanks for your reply. So the "t     c"  is for changing the partition type to FAT32 (does the change happen in the MBR?), while the "mkdosfs -v -F 32 /dev/sda1" is doing the actual formatting. Both are needed. Did I get it right?

Thanks!
Gerwin Jansen, EE MVETopic Advisor Commented:
Yes, but the partition is in the partition table. MBR is the master boot record. t changes type, c is for FAT32, mkfs commands make the file system (or do the formatting).
condor888Author Commented:
Thanks, Gerwin. You are very helpful!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.