Formatting, partitioning, and mounting disk management in Linux - detailed explanation
1. Disk knowledge
1.Disk meaning
2. Disk working mechanism
-
Magnetization : Magnetic material on a disk can be magnetized by applying a magnetic field to cause a change in the magnetic orientation of the material. This magnetization can be achieved by a magnetic field generated by an electromagnetic magnet or a magnetic head.
-
Magnetic field induction : The magnetic head is an inductor on the disk that can sense changes in the magnetic field and convert it into an electrical signal. When the head is close to the disk surface, it can detect the magnetization state of the magnetic material.
-
Data reading : When reading data, the magnetic head detects the data on the disk by sensing changes in the magnetic field. As the head passes over a magnetized area, the changes in the magnetic field induced are converted into electrical signals and interpreted by the disk controller as corresponding data.
-
Data writing : When writing data, the head changes the magnetization state on the disk by applying a magnetic field. The head stores data in the magnetic material by creating or changing magnetization patterns at corresponding locations on the disk, depending on the data to be written.
-
Seeking and rotation : In order to access specific tracks and sectors, the head needs to move to the target location on the disk surface. This process is called a seek operation. The movement of the heads is controlled by the disk drive, and they can move in the radius of the disk. In addition, disks achieve high-speed read and write operations by rotating the platters, and the rotation speed is usually expressed in revolutions per minute (RPM).
3. The professional name of the disk
3.1 Disk side and magnetic head
3.2 Sectors and tracks
3.3 Magnetic head and cylinder
4. Disk classification
-
SATA (Serial ATA) disk : SATA is currently the most common type of disk interface and is widely used in desktop and laptop computers. It provides higher data transfer speeds and lower costs, and supports hot plugging and hot swapping.
-
SAS (Serial Attached SCSI) disk : SAS is a high-performance disk interface mainly used in enterprise-level servers and storage systems. It provides higher data transfer speeds and more reliable connections, supports multi-path redundancy and hot-swappable capabilities.
-
SCSI (Small Computer System Interface) disk : SCSI is an early disk interface used to connect high-performance computer systems and external storage devices. It provides high-speed data transfer and flexible device connection options, supporting multi-device links and advanced features.
-
NVMe (Non-Volatile Memory Express) disk : NVMe is a high-performance disk interface based on the PCIe (Peripheral Component Interconnect Express) bus. It is designed specifically for solid-state drives (SSDs) with low latency and high-speed data transfer capabilities and is widely used in high-performance computing and data center environments.
2. Advantages of disk partitioning
3. Disk partition
1. Determine the disk device
Use the following command to view available disk devices:
/ # fdisk -l
Disk /dev/mmcblk1: 7456 MB, 7818182656 bytes, 15269888 sectors
238592 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/mmcblk1 doesn't contain a valid partition table
Disk /dev/mmcblk1boot0: 4 MB, 4194304 bytes, 8192 sectors
128 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/mmcblk1boot0 doesn't contain a valid partition table
Disk /dev/mmcblk1boot1: 4 MB, 4194304 bytes, 8192 sectors
128 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Disk /dev/mmcblk1boot1 doesn't contain a valid partition table
Disk /dev/mmcblk0: 15 GB, 15931539456 bytes, 31116288 sectors
486192 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes
Device Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
/dev/mmcblk0p1 * 0,32,33 8,73,1 2048 133119 131072 64.0M c Win95 FAT32 (LBA)
/dev/mmcblk0p2 8,73,2 912,229,21 133120 31116287 30983168 14.7G 83 Linux
From the above it can be concluded:
/dev/mmcblk1 : A disk with a capacity of 7456 MB. 238592 cylinders, 4 heads, 16 sectors per track. There is no valid partition table on the disk.
/dev/mmcblk1boot0 and /dev/mmcblk1boot1 are the boot sectors of the disk and are 4 MB in size, but they do not have a valid partition table.
/dev/mmcblk0 : A disk with a capacity of 15 GB. 486192 cylinders, 4 heads, 16 sectors per track. Contains two partitions:
-
/dev/mmcblk0p1 is the boot partition with a size of 64.0M and uses the Win95 FAT32 (LBA) file system.
-
/dev/mmcblk0p2 is the second partition, 14.7G in size, using the Linux file system (ID 83).
2. Use the fdisk command to partition the disk
/ # fdisk /dev/mmcblk1
The number of cylinders for this disk is set to 238592.
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):
At this time, the command bar prompts to enter 'm' , and you can see the following parameters:
Command (m for help): m
Command Action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):
The meaning of each command is as follows:
-
a: toggle a bootable flag
-
b: Edit BSD disk label (edit bsd disklabel)
-
c: toggle the dos compatibility flag
-
d: delete a partition
-
l: List known partition types (list known partition types)
-
n: add a new partition
-
o: Create a new empty DOS partition table (create a new empty DOS partition table)
-
p: print the partition table
-
q: quit without saving changes
-
s: create a new empty Sun disklabel (create a new empty Sun disklabel)
-
t: change a partition's system id
-
u: change display/entry units
-
v: verify the partition table
-
w: write table to disk and exit (write table to disk and exit)
-
x: Extra functionality (experts only) Continue below to create a partition:
Command (m for help): n
Partition type
p primary partition (1-4)
e extended
p
Partition number (1-4): 1
First sector (16-15269887, default 16):
Using default value 16
Last sector or +size{,K,M,G,T} (16-15269887, default 15269887): +32M
Command (m for help): n
Partition type
p primary partition (1-4)
e extended
p
Partition number (1-4): 2
First sector (65552-15269887, default 65552):
Using default value 65552
Last sector or +size{,K,M,G,T} (65552-15269887, default 15269887):
Using default value 15269887
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (Win95 FAT32 (LBA))
Command (m for help): a
Partition number (1-4): 1
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table
[ 1334.059435] mmcblk1: p1 p2
Here is a detailed explanation of the command entered above:
-
Create a new partition using command 'n' :
-
Select the primary partition type and enter 'p' .
-
Enter the partition number, select 1 here .
-
Enter the starting sector, the default is 16, you can directly press the Enter key to use the default value.
-
Enter the end sector or partition size. Enter '+32M' here to indicate that the partition size is 32MB.
-
Create a new partition again using command 'n' :
-
Select the primary partition type and enter 'p' .
-
Enter the partition number, select 2 here .
-
Enter the starting sector, the default is 65552, you can directly press the Enter key to use the default value.
-
Enter the end sector or partition size. The default value 15269887 is used here.
-
Use the command 't' to set the partition type:
-
Enter the partition number of the type to be set, select 1 here .
-
Enter the Hex code (Hex code). Entering 'c' here means setting the system type of partition 1 to Win95 FAT32 (LBA).
-
Use command 'a' to set the boot flag:
-
Enter the partition number to set the boot flag, select 1 here .
-
Use command 'w' to save changes and exit:
-
Confirm that the partition table has been modified, and call ioctl() to re-read the partition table.
-
Prompt that the partition table has been modified and the changes were saved successfully.
4. Disk formatting
-
Format '/dev/mmcblk1p1' to a Fat32 format file system
-
Format '/dev/mmcblk1p2' to an ext4 format file system
Fat32 format, use the command as follows:
mkfs.vfat -F 32 -n "boot" /dev/mmcblk1p1
The meaning of each option is explained below:
-
-F 32: Specifies the file system type to be created as FAT32.
-
-n MyVolume: Set the volume label to "MyVolume", which will be displayed as the name of the file system.
-
/dev/mmcblk1p1: is the device or partition to be formatted
ext4 format, use the following command:
mkfs.ext4 -F -L "rootfs" /dev/mmcblk1p2
-/bin/sh: mkfs.ext4: not found
The meaning of each option is explained below:
-
-F: Force formatting even if the device is mounted.
-
-L "rootfs": Set the file system label to "rootfs", which will be displayed as the name of the file system.
-
/dev/mmcblk1p2: The device or partition to be formatted.
mke2fs -F -L "rootfs" /dev/mmcblk1p2
Filesystem label=rootfs
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
475136 inodes, 1900542 blocks
95027 blocks (5%) reserved for the super user
First data block=0
Maximum filesystem blocks=4194304
58 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
/ #
5. Disk mounting
1. Create a mount point
mkdir /mnt/boot
2. Execute the mount command
Use the mount command to mount the file system to the specified mount point, for example:
mount /dev/mmcblk1p1 /mnt/boot
Use the " df " command to check whether the mounting is successful. The command is as follows:
/ # df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 15141732 161948 14188824 1% /
devtmpfs 216388 0 216388 0% /dev
proc 0 0 0 0% /proc
tmpfs 249668 0 249668 0% /tmp
sysfs 0 0 0 0% /sys
devpts 0 0 0 0% /dev/pts
/dev/mmcblk1p1 32260 1 32260 0% /mnt/boot
It is obvious that the '/dev/mmcblk1p1' partition has been successfully mounted to the '/mnt/boot' directory.
3. Access the file system
cd /mnt/boot
touch test.txt
/ # ls /mnt/boot/
/ #
/ # df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 15141732 161948 14188824 1% /
devtmpfs 216388 0 216388 0% /dev
proc 0 0 0 0% /proc
tmpfs 249668 0 249668 0% /tmp
sysfs 0 0 0 0% /sys
devpts 0 0 0 0% /dev/pts
/ # mount /dev/mmcblk1p1 /mnt/boot
[ 179.885442] FAT-fs (mmcblk1p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
/ # ls /mnt/boot/
test.txt
/ #
Another partition is mounted as follows:
mkdir /mnt/rootfs
mount /dev/mmcblk1p2 /mnt/rootfs
/ # df -h
Filesystem Size Used Available Use% Mounted on
/dev/root 14.4G 158.2M 13.5G 1% /
devtmpfs 211.3M 0 211.3M 0% /dev
tmpfs 243.8M 0 243.8M 0% /tmp
/dev/mmcblk1p1 31.5M 512 31.5M 0% /mnt/boot
/dev/mmcblk1p2 7.1G 20.0K 6.8G 0% /mnt/rootfs
6. Automatically mount when booting
1. Device path mounting
#<file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/mmcblk1p1 /mnt/boot vfat defaults 0 0
/dev/mmcblk1p2 /mnt/rootfs ext4 defaults 0 0
Restart the development board and use the 'df' command to check whether it is automatically mounted.
/ # df -h
Filesystem Size Used Available Use% Mounted on
/dev/root 14.4G 158.2M 13.5G 1% /
devtmpfs 211.3M 0 211.3M 0% /dev
tmpfs 243.8M 0 243.8M 0% /tmp
/dev/mmcblk1p1 31.5M 512 31.5M 0% /mnt/boot
/dev/mmcblk1p2 7.1G 20.0K 6.8G 0% /mnt/rootfs
ok, automatic mounting is successful.
2.UUID mounting
Get the UUID of a partition: Use the following command to get the UUID of each partition:
/ # blkid
/dev/mmcblk0p2: LABEL="rootfs" UUID="4bdc82c7-5e83-4992-9966-cd99a2317944" TYPE="ext4"
/dev/mmcblk0p1: LABEL="boot" UUID="DF8D-2A71" TYPE="vfat"
/dev/mmcblk1p2: LABEL="rootfs" UUID="54b75bd6-7cdf-4e80-aa48-1af163bf61f3" TYPE="ext2"
/dev/mmcblk1p1: TYPE="vfat"
Edit the /etc/fstab file and add the appropriate entries. For example:
#<file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
UUID=54b75bd6-7cdf-4e80-aa48-1af163bf61f3 /mnt/rootfs ext4 defaults 0 0
After saving, restart the development board and successfully mount it automatically, as follows:
/ # df -h
Filesystem Size Used Available Use% Mounted on
/dev/root 14.4G 158.2M 13.5G 1% /
devtmpfs 211.3M 0 211.3M 0% /dev
tmpfs 243.8M 0 243.8M 0% /tmp
/dev/mmcblk1p2 7.1G 20.0K 6.8G 0% /mnt/rootfs
1. Device path (/dev/mmcblk1p1):
-
Identify the path to the partition: Using the device path is a straightforward and simple way to identify the partition to be mounted. It is based on the physical path of the device file and can explicitly specify the partition to be mounted. For example, '/dev/mmcblk1p1' represents the first partition of the second MMC type disk.
-
Relatively easy to remember: Device paths are usually short and easy to remember because they are directly related to the name of the device file.
2. File system UUID:
-
Uniqueness of identifying partitions: Each file system has a unique UUID (Universally Unique Identifier) that is used to identify the partition. The UUID is generated when the file system is created and is globally unique. It is not affected by the device connection state, so even if the device path changes, the UUID will remain the same.
-
Stability and persistence: Using UUID to mount partitions can provide a more stable and durable mounting method, because even if the system is restarted or the device connection status is changed, the partition identified by the UUID can still be found accurately.
-
Better for automation and scripting: Using UUID makes it easier to automate and script because UUID provides a fixed identifier to uniquely identify a specific partition.
1. Detailed explanation of network transplantation and debugging of u-boot
2.u-boot startup process analysis - the most complete and detailed in history
3. Detailed explanation of EMMC and SD card operation commands of u-boot command
4.Transplant the Linux 6.3.5 system to the imx6ull development board
5. Rootfs root file system creation and mounting method - detailed explanation
For more knowledge about embedded C language, FreeRTOS, RT-Thread, Linux application programming, Linux driver and other related knowledge, follow the public account [Embedded Linux Knowledge Sharing], and watch the follow-up exciting content in time.