How prepare a bootable SD card / eMMC using Linux fdisk tool ?

By | May 15, 2016

In the Previous post, we have discussed about the procedure to create a bootable SD card using disks GUI. This tool may not be available every time. Some times, we may need to format the SD card or eMMC from the embedded Linux target it self.

In such cases, we need to use Linux commands/tools to format the SD card. In this post, we will use “fdisk” command line tool and other filesystem tools to prepare a bootable SD card.

Prerequisite :

I would you recommend you to check the Previous post , section “SD card enumeration in Linux” to identify the proper SD card device node / name. Unless you know the proper device name, don’t proceed further. Because wrong usage of commands, can wipe your entire hard disk and lead to huge data loose.

How many partitions required ?

A primary/boot partition, formatted to FAT file format. This partition will have several components such as bootloaders, kernel, etc ..

A secondary partition for root filesystem.

Using ‘fdisk’ :

step 1 : List your partition table and identify SD card / eMMC card. use below command

$ sudo fdisk -l

You will also get a similar log as below.

minion@ubuntu:~$ sudo fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders, total 419430400 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: 0x000e3358

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   417333247   208665600   83  Linux
/dev/sda2       417335294   419428351     1046529    5  Extended
/dev/sda5       417335296   419428351     1046528   82  Linux swap / Solaris

Disk /dev/sdb: 15.9 GB, 15931539456 bytes
255 heads, 63 sectors/track, 1936 cylinders, total 31116288 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: 0x000d24fd

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63    31116287    15558112+   c  W95 FAT32 (LBA)

In my case, SD card device file name is /dev/sdb. Linux detected as a SCSI device (/dev/sdb1) as i’m using a external card reader.  If you are using SD/MMC card slot, device node will be “mmcblkX”

Step 2 : Erase Master Boot Record table as well as the partition table using ‘dd’ command.

sudo dd if=/dev/zero of=/dev/sdX bs=1M count=1

Log is as shown below

minion@ubuntu:~$ sudo dd if=/dev/zero of=/dev/sdb bs=1M count=1
[sudo] password for minion: 
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.103481 s, 10.1 MB/s

Step 3 : Create partitions

  • Open SD device node with fdisk command. You will get a fdisk command prompt “Command (m for help):” .
$ sudo fdisk /dev/sdX
  • In the fdisk command prompt, type ‘m’ and press enter for help.
  • To print partition table: type ‘p’ and press enter. You will see empty partition table as it is erased earlier.
Command (m for help): p

Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0xedddc4f4

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

Note : Treat “–>” symbol as enter in the upcoming steps

  • Create a 256 MB first partition: In the fdisk prompt follow this sequence,  “n –> p –> 1 –> enter –> +256M –>”. At last type ‘p’ to see the partition table.
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-31116287, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-31116287, default 31116287): +256M

Command (m for help): p

Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x9d29a9b7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      526335      262144   83  Linux

Command (m for help):
  • Create second partition for root filesystem. In the fdisk command prompt “n –> p –> 2 –> enter –> +2G –>”
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (526336-31116287, default 526336): 
Using default value 526336
Last sector, +sectors or +size{K,M,G} (526336-31116287, default 31116287): +2G

Command (m for help): p

Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x9d29a9b7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      526335      262144   83  Linux
/dev/sdb2          526336     4720639     2097152   83  Linux

Command (m for help):
  • Change the first partition to FAT. In the fdisk command prompt : “t –> 1 –> c –> p–>”. Code ‘c’ is for FAT 32 (LBA).
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): c
Command (m for help): p

Disk /dev/sdb: 15.9 GB, 15931539456 bytes
64 heads, 32 sectors/track, 15193 cylinders, total 31116288 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: 0x9d29a9b7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      526335      262144    c  W95 FAT32 (LBA)
/dev/sdb2          526336     4720639     2097152   83  Linux
  • Now write the partition table to SD card and exit using ‘w’ command.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Step 4 : Final step, format SD card partitions. first partition to FAT, second partition to EXT4.

sudo mkfs.vfat /dev/sdX1
sudo mkfs.ext4 /dev/sdX2

X can be a , b, c …

In case of SD/MMC card slot, commands changes as shown below.

sudo mkfs.vfat /dev/mmcblkXp1
sudo mkfs.ext4 /dev/mmcblkXp2

X can be 0, 1, 2, 3 …

 

Leave a Reply

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