Avatar of condor888
condor888
 asked on

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?
LinuxLinux DistributionsRaspberry Pi

Avatar of undefined
Last Comment
condor888

8/22/2022 - Mon
ivan rosa

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
condor888

ASKER
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 rosa

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Dan Craciun

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
condor888

ASKER
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 Craciun

OK.

From the command line, use fdisk or parted.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
condor888

ASKER
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 Craciun

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)
condor888

ASKER
does mkfs.vfat do the same thing? I tried mkfs.vfat but it doesn't seem to work.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Dan Craciun

gheist

Use official sd card formatter on windows.
Gerwin Jansen

>> 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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gheist

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

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.
condor888

ASKER
@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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
condor888

ASKER
@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!
condor888

ASKER
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

I can test for you later today.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
condor888

ASKER
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.
gheist

Unmount first (do not open SD card in PI.
Instrad of c you must type t (partition type)
ASKER CERTIFIED SOLUTION
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
condor888

ASKER
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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
condor888

ASKER
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?
gheist

No, you do not need to format extra time.
There is no difference between mkfs.vfat and mkfs.msdos
condor888

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gheist

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.
condor888

ASKER
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

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
condor888

ASKER
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

>> When you get a chance, would you please have a try on that?
Of course - later today. Will post back the results.
Gerwin Jansen

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
condor888

ASKER
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

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).
condor888

ASKER
Thanks, Gerwin. You are very helpful!
Your help has saved me hundreds of hours of internet surfing.
fblack61