Partition and Mount TF Card on Linux

Last updated on June 15, 2023 am

Partition and Mount TF Card on Linux

Simply plugging in a TF Card to the board may not work even with built-in drivers.

This blog takes an 16 GB micro SD card and a single board computer (SBC) Huashan-pi CV1812H as an example of how to do partitioning and mounting to let our Linux system recognize the card.

Basic Commands

df

Running the bash command df -h allows you to see all mounted disk devices.

1
2
3
4
5
6
7
8
9
10
11
12
Filesystem                Size      Used Available Use% Mounted on
/dev/root 13.6M 13.6M 0 100% /
devtmpfs 33.3M 0 33.3M 0% /dev
tmpfs 33.3M 0 33.3M 0% /dev/shm
tmpfs 2.9G 80.0K 2.7G 0% /tmp
tmpfs 33.3M 12.0K 33.3M 0% /run
tmpfs 33.3M 12.0K 33.3M 0% /run
tmpfs 2.9G 80.0K 2.7G 0% /tmp
tmpfs 33.3M 0 33.3M 0% /var/empty
/dev/mmcblk0p6 5.5M 5.5M 0 100% /mnt/cfg
/dev/mmcblk0p7 2.9G 80.0K 2.7G 0% /mnt/data
/dev/mmcblk0p7 2.9G 80.0K 2.7G 0% /tmp

It is clear that our 16 GB micro SD card is not listed.

fdisk

Running teh command fdisk -l shows all of hardware disk drives:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Disk /dev/mmcblk0: 7456 MB, 7818182656 bytes, 15269888 sectors
238592 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Disk /dev/mmcblk0 doesn't contain a valid partition table
Disk /dev/mmcblk0p1: 8 MB, 8388608 bytes, 16384 sectors
256 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Disk /dev/mmcblk0p1 doesn't contain a valid partition table
Disk /dev/mmcblk1: 15 GB, 15931539456 bytes, 31116288 sectors
1936 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/mmcblk1p1 0,130,3 1023,254,63 8192 31116287 31108096 14.8G c Win95 FAT32 (LBA)

Line 11 is exactly what we are looking for. This 15 GB is the size of our TF card. There is always a discrepancy between the labeled size and the actual available size on OS, which can be quite annoying.

Our card is located at the path /dev/mmcblk1

Partitioning

The next step is going to partition the disk. This can be done by using the fdisk command. For more details, you can refer to man fdisk or tldr fdisk.

1
fdisk /dev/mmcblk1

This command will allow us to perform the partitioning.

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
The number of cylinders for this disk is set to 1936.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p
Disk /dev/mmcblk1: 15 GB, 15931539456 bytes, 31116288 sectors
1936 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/mmcblk1p1 0,130,3 1023,254,63 8192 31116287 31108096 14.8G c Win95 FAT32 (LBA)

Command (m for help): n
Partition type
p primary partition (1-4)
e extended
e
Partition number (1-4): 4
First sector (63-31116287, default 63):
Using default value 63
Last sector or +size{,K,M,G,T} (63-8191, default 8191):
Using default value 8191

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table

press Enter to use default values for the first and last sector.

These values represent disk location references that map to specific locations on the disk.

Mounting

The last step is to mount the drive to a directory path. If the path /mnt/sd does not exist, create it using the mkdir command.

1
mount /dev/mmcblk1p1 /mnt/sd

Done

We are all set now. Running the command df -Th will verify that our TF card is mounted with vfat type, which corresponds to the FAT32 file system.

1
2
Filesystem           Type            Size      Used Available Use% Mounted on
/dev/mmcblk1p1 vfat 14.8G 672.0K 14.8G 0% /mnt/sd

On Removal

Simply unmount the device so that the card can be removed safely.

  1. Do not use the resource inside the mounted device, e.g., reading from or writing to it, and remember navigate outside the directory /mnt/sd.
  2. Run command umount /mnt/sd (unmount by mounted directory path) or umount /dev/mmcblk1p1 (unmount by device name), they are equivalent.

Next Time

Next time when you restart the board or plugged in the micro SD card again, running command mount /dev/mmcblk1p1 /mnt/sd is needed, otherwise the system still do not have a path to access the file in the card.

Alternatively, you can put the following lines into shell script to automate the procedure.

1
2
3
4
5
6
7
8
9
10
#!/bin/sh

# Check if the TF card device exists
if [ -e "/dev/mmcblk1" ]; then
echo "TF card is detected. Mounting..."
mount /dev/mmcblk1p1 /mnt/sd # Replace /mnt/sd with your desired mount point
echo "TF card mounted successfully."
else
echo "TF card is not detected."
fi

References

  1. Extend Partition on SD Card or eMMC
  2. Linux下分区详解之–Fdisk_ITPUB博客
  3. 4.1. 华山派-CV181系列开发板基本介绍 · sophgo/sophpi-huashan Wiki
  4. 4. Partitioning requirements
  5. 5. Partitioning with fdisk - 5.3. Mixed primary and logical partitions
  6. Why does my USB drive show less capacity than stated on my PC or MAC | Integral Memory

Partition and Mount TF Card on Linux
https://lingkang.dev/2023/06/13/access-tf/
Author
Lingkang
Posted on
June 13, 2023
Licensed under