Post

USB Drive or SD Card Partitioning Using fdisk Utility

In this tutorial, we’ll explore the process of creating partitions for a hard disk drive (HDD), USB drive, or micro SD card using the Linux tool fdisk. Additionally, we’ll establish a file system within these partitions using another tool called mkfs.

Let’s generate two partitions: the first utilizing the fat32 filesystem and the second utilizing the ext4 filesystem. This partition combination is commonly employed for constructing a fully bootable operating system. The fat32 partition is designated for storing bootloader-related files, while the ext4 filesystem is employed for housing Linux files.

Insert the USB drive / SD card reader

When you insert a USB drive or SD card reader into the system’s USB port, it’s usually identified as sdX (e.g., sda, sdb), while an SD card appears as mmcblkX (e.g., mmcblk0). To identify the USB device from the kernel log, run:

1
journalctl  -ke

Correspondingly, partitions are created in the format sdXY (e.g., sdb1, sdc1) or mmcblkXpY (e.g., mmcblk0p1) respectively.

Check connected storage devices using the lsblk command. A typical output might look like this:

1
2
3
4
5
6
7
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdc           8:32   1  14.8G  0 disk
`-sdc1        8:33   1  14.8G  0 part
nvme0n1     259:0    0 476.9G  0 disk
|-nvme0n1p1 259:1    0   512M  0 part /boot/efi
|-nvme0n1p2 259:2    0 475.5G  0 part /
`-nvme0n1p3 259:3    0   976M  0 part [SWAP]

Here is another one:

1
2
3
4
5
6
7
8
sda           8:0    0 223.6G  0 disk
├─sda1        8:1    0   512M  0 part /boot/efi
├─sda2        8:2    0  93.1G  0 part /
├─sda3        8:3    0   7.5G  0 part [SWAP]
└─sda4        8:4    0 122.5G  0 part
sr0          11:0    1  1024M  0 rom
mmcblk0     179:0    0    29G  0 disk
└─mmcblk0p1 179:1    0    29G  0 part /media/nayab/8D39-59F7

Storage device names in Linux

Typical naming of storage devices are as follows:

Storage deviceLinux device (ex:)Partitions (ex:)
Hard disk drivesdX (sda, sdb etc.)sdXY (sda1, sdb2 etc)
USB flash drivesdX (sda, sdb etc.)sdXY (sda1, sdb2 etc)
NVMe SSDnvmeX (nvme0, nvme1 etc.)nvmeXnYpZ (nvme0n1p1, nvme1n2p2 etc.)
Micro SD card (SDHC)mmcblkX (mmcblk0, mmcblk1 etc.)mmcblkXpY (mmcblk0p1, mmcblk1p3 etc.)
Micro SD card (USB)sdX (sda, sdb etc.)sdXY (sda1, sdb2 etc)

Unmount the partitions

Before using the fdisk tool, ensure that the storage device partitions are unmounted.

1
sudo umount /dev/<device_partition>

or

1
sudo umount /dev/mmcblk0p1

Partitioning with fdisk

Now run the fdisk command with the device name.

1
sudo fdisk /dev/<dev_name>

Replace <dev_name> with USB device name (ex: /dev/sdb or /dev/mmcblk0).

Delete partitions

Press p to display the available partitions. Type d multiple times until all partitions are deleted, then type p again to confirm the changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk0: 29.81 GiB, 32010928128 bytes, 62521344 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
Disklabel type: dos
Disk identifier: 0x00000000

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *     2048   34815   32768   16M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      34816 1083391 1048576  512M 83 Linux

Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): d
No partition is defined yet!

Command (m for help): p

Disk /dev/mmcblk0: 29.81 GiB, 32010928128 bytes, 62521344 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
Disklabel type: dos
Disk identifier: 0x00000000

Command (m for help):

Create FAT32 partition

Now, let’s create a primary partition of 1GB to store bootloader files. Press n followed by p for primary partition. Press Enter for the default Fist sector followed by +1G when asked for Last sector.

1
2
3
4
5
6
7
8
9
10
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62521343, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62521343, default 62521343): +1G

Created a new partition 1 of type 'Linux' and of size 1 GiB

Change the partition type to FAT32 by typing t and then selecting b.

1
2
3
4
Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): b
Changed type of partition 'Linux' to 'W95 FAT32'.

Create Linux partition

Create another partition with the remaining size. Type n and keep pressing Enter until the prompt repeats. Then type t, followed by 2 for the partition number, and set the Hex code to 83 when prompted to specify the Linux type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p):

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

Created a new partition 2 of type 'Linux' and of size 28.8 GiB.

Command (m for help):


Command (m for help):


Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 83


Changed type of partition 'Linux' to 'Linux'.

Write changes to device

Type w to save the partition changes to the disk.

1
2
3
4
5
Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Verify the newly written partition information with the following command:

1
sudo fdisk /dev/<dev_name>

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
$ sudo fdisk -l /dev/mmcblk0
Disk /dev/mmcblk0: 29.81 GiB, 32010928128 bytes, 62521344 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
Disklabel type: dos
Disk identifier: 0x00000000

Device         Boot   Start      End  Sectors  Size Id Type
/dev/mmcblk0p1         2048  2099199  2097152    1G  b W95 FAT32
/dev/mmcblk0p2      2099200 62521343 60422144 28.8G 83 Linux

Build file system with mkfs tool

Build fat32 filesystem for partition 1:

1
sudo mkfs.vfat /dev/mmcblk0p1 -n boot

Build Linux filesystem for partition 2:

1
sudo mkfs.ext4 /dev/mmcblk0p2 -L rootfs

Check the partitions

Remove the USB/Card device and then reinsert it to verify the partitions using the lsblk command.

This post is licensed under CC BY 4.0 by the author.