使用以下命令来查看可用磁盘设备:
/ # 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
从上面可以得出:
/dev/mmcblk1:容量为7456 MB的磁盘。238592柱面,4个磁头,每个磁道16个扇区。磁盘上没有有效的分区表。
/dev/mmcblk1boot0和/dev/mmcblk1boot1是磁盘的引导区,大小为4 MB,但它们没有有效的分区表。
/dev/mmcblk0:容量为15 GB的磁盘。486192柱面,4个磁头,每个磁道16个扇区。包含两个分区:
/ # 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):
此时命令栏提示输入'm',可以看到有如下参数:
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):
各指令含义如下:
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
以下是对上面输入命令的详细解释:
Fat32格式,使用命令如下:
mkfs.vfat -F 32 -n "boot" /dev/mmcblk1p1
每个选项的含义解释如下:
ext4格式,使用命令如下:
mkfs.ext4 -F -L "rootfs" /dev/mmcblk1p2
-/bin/sh: mkfs.ext4: not found
每个选项的含义解释如下:
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
/ #
mkdir /mnt/boot
使用mount命令将文件系统挂载到指定的挂载点上,例如:
mount /dev/mmcblk1p1 /mnt/boot
使用“df”指令查看是否挂载成功,命令如下:
/ # 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
很明显已经’/dev/mmcblk1p1‘分区已经成功挂载到’/mnt/boot‘目录下。
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
/ #
另一个分区挂载如下:
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
#
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
重新启动开发板,使用'df'命令查看是否自动挂载
/ # 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,自动挂载成功。
获取分区的UUID:使用以下命令各分区的UUID:
/ # 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"
编辑/etc/fstab文件并添加相应的条目。例如:
#
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
保存后,重启开发板,成功自动挂载,如下:
/ # 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
标识分区的路径:使用设备路径是一种直接而简单的方法来标识要挂载的分区。它基于设备文件的物理路径,可以明确地指定要挂载的分区。例如,'/dev/mmcblk1p1' 表示第二个MMC类型的磁盘的第一个分区。
相对容易记忆:设备路径通常较短且易于记忆,因为它们直接与设备文件的名称相关。
标识分区的唯一性:每个文件系统都有一个唯一的UUID(通用唯一标识符),用于识别分区。UUID是在创建文件系统时生成的,并且是全局唯一的。它不受设备连接状态的影响,因此即使设备路径发生变化,UUID仍将保持不变。
稳定性和持久性:使用UUID来挂载分区可以提供更稳定和持久的挂载方式,因为即使重新启动系统或更改设备连接状态,UUID标识的分区仍然可以准确地被找到。
更适合自动化和脚本:使用UUID可以更方便地进行自动化操作和脚本编写,因为UUID提供了一个固定的标识符来唯一标识特定的分区。
1.详细讲解u-boot之网络移植与调试
2.u-boot启动流程分析-史上最全最详细
3.u-boot命令之EMMC和SD卡操作命令详解
4.移植Linux 6.3.5系统到imx6ull开发板
5.rootfs根文件系统制作及挂载方法-详解
关于更多嵌入式C语言、FreeRTOS、RT-Thread、Linux应用编程、linux驱动等相关知识,关注公众号【嵌入式Linux知识共享】,后续精彩内容及时收看了解。