Raspberry Pi — How to reformat a bootable SD card.

Recently, I received a Raspberry Pi to play with, from a good friend of mine. One of the projects I am working on is to deploy artificial intelligence applications in low powered devices. Raspberry Pi seemed to be a great platform to experiment with. But, I ran into a lot of interesting issues. I will write on those issues and how to solve them in a later post. Today, I am going to write about a simple issue of reformatting a Raspberry Pi SD card after you are done tinkering around. Instead of just leaving the SD card unused, I decided to salvage the storage device used to run Raspberry Pi OS. Formatting the SD card is a fairly involved process.

Here are the detailed steps to re-format a bootable device, in my case: a SanDisk 128 GB microSD card. I am using a Windows 10 machine to reformat the storage device.

Step 1: In the first step we will learn how to launch diskpart, list all attached disks and select the appropriate disk.

Run command prompt as an administrative user.
Launch diskpart by typing diskpart in the command line:

diskpart

Here is an example output of how it looks like from the command line:

C:\WINDOWS\system32>diskpart

Microsoft DiskPart version 10.0.14393.0

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: RAHUL-PC

DISKPART>

To list all the attached disks to the operating system, type list disk:

list disk

The output will look something like this:

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           29 GB      0 B        *
  Disk 1    Online          183 GB      0 B
  Disk 2    Online          117 GB      0 B

To select the specific disk, type ‘select disk #’, where # represents the disk number. In the example above, the SD card is disk 2. To select disk 2 type the following command in the diskpart command prompt:

select disk 2

If you type ‘list disk’ again, it will show the list of disks, now with an asterisk preceding the selected disk.

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           29 GB      0 B        *
  Disk 1    Online          183 GB      0 B
* Disk 2    Online          117 GB   117 GB

 

Step2: In this step, we will remove all existing partitions and prepare the disk for reformatting by cleaning the device.

To remove existing partition, we need to know all the available partitions on the storage device. Let us list the existing partitions using list partition command:

list partition

In this example, the command line output looks like this:

DISKPART> list partition

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary             63 MB  1024 KB
  Partition 0    Primary            116 GB    64 MB

We have two available partitions: 0 and 1. Now, let us remove both of these partitions. To do that, first we have to select the partition using ‘select partition command’.

select partition #

Once we have selected the partitions, we can perform the partition removal operation using the ‘delete partition’ command.

delete partition

In this example, the storage device has two partitions. We have removed both of these partitions one by one. The command line output looks like this:

DISKPART> list partition

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary             63 MB  1024 KB
  Partition 0    Primary            116 GB    64 MB

DISKPART> select partition 0

Partition 0 is now the selected partition.

DISKPART> delete partition

DiskPart successfully deleted the selected partition.

DISKPART> list partition

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary            116 GB    64 MB

DISKPART> select partition 1

Partition 1 is now the selected partition.

DISKPART> delete partition

DiskPart successfully deleted the selected partition.

Once we have deleted all the available partitions, the next step is to prepare the disk for reformatting by cleaning the disk. Type ‘clean’ in diskpart command line and it will successfully clean the disk.

clean

The command line output for this example will look something similar to this:

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           29 GB      0 B        *
  Disk 1    Online          183 GB      0 B
* Disk 2    Online          117 GB   117 GB

DISKPART> select disk 2

Disk 2 is now the selected disk.

DISKPART> clean

DiskPart succeeded in cleaning the disk.

 

Step3: Create partitions and reformat the disk with an appropriate file system:

The diskpart has a command line option called create. It takes three arguments. Those arguments are:

  1. PARTITION – To create a partition.
  2. VOLUME – To create a volume.
  3. VDISK – To create a virtual disk file.

We will use the ‘create partition’ command. To run this, we need to know the command line arguments that ‘create partition’ accepts. There are five options for ‘create partition’ command. They are as follows:

  1. EFI – To create an EFI system partition.
  2. EXTENDED – To create an extended partition.
  3. LOGICAL – To create a logical drive.
  4. MSR – To create a Microsoft Reserved partition.
  5. PRIMARY – To create a primary partition.

I want to create a new primary partition. Therefore, the DISKPART command is going to be:

create partition  primary

Once, a primary partition is created, the next step is to reformat the new partition using NTFS file format. Microsoft recommends exFAT for removable media like SD card storage, but, I am selecting NTFS over exFAT for reasons of convenience. Before formatting, select the newly created partition. Then run the command:

format fs=NTFS quick

The command line output for my example is as follows:

DISKPART> create partition  primary

DiskPart succeeded in creating the specified partition.

DISKPART> list partition

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
* Partition 1    Primary            117 GB  1024 KB

DISKPART> select partition 1

Partition 1 is now the selected partition.

DISKPART> format fs=NTFS quick

  100 percent completed

DiskPart successfully formatted the volume.

Once, the formatting is successfully completed, there will be two messages:  ‘100 percent completed’ followed by ‘DiskPart successfully formatted the volume’. Now, the SD card should be mounted properly in Windows Explorer. Under ‘This PC’, there should be a new empty disk storage device that can now be attached as a removable device to any PC.

 

Leave a comment

Your email address will not be published. Required fields are marked *