创建文件系统镜像文件
Posted cuclaoliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建文件系统镜像文件相关的知识,希望对你有一定的参考价值。
在这里,我们想建立一个2G左右大小(可以按需设定)的镜像文件,包含三个分区:第一个分区是 b, fat32分区,大小256M,用来存储内核文件,设备树文件,uboot使用的启动脚本文件,配置FPGA的rbf文件等;第三个分区是 a2, raw分区,放在镜像文件的末端,大小 2MiB,用来存放 preloader 和 uboot 的代码,主引导记录引导系统,根据需要 uboot 也可以在 fat32 分区中保存为镜像文件 u-boot.img;第二个分区占用了所有剩下的所有空间,用作 Linux 的根文件系统,83, ext4文件系统。
1. 创建文件镜像
$ dd if=/dev/zero of=./sd.img bs=1M count=2048 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 2.57052 s, 835 MB/s
2. 将其作为环设备进行挂载
$ sudo losetup --show -f sd.img [sudo] password for stephen: /dev/loop0
3. 分区
当前分区情况
$ sudo fdisk /dev/loop0 Welcome to fdisk (util-linux 2.32). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x4478d16c. Command (m for help): p Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 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: 0x4478d16c Command (m for help):
创建第一个分区,大小 256MiB,文件系统 FAT32
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): 1 First sector (2048-4194303, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): +256M Created a new partition 1 of type ‘Linux‘ and of size 256 MiB. Command (m for help): t Selected partition 1 Hex code (type L to list all codes): b Changed type of partition ‘Linux‘ to ‘W95 FAT32‘.
创建第三个分区,放在镜像最后,大小 2MiB,即 4096 扇区(每个扇区 512B),因最后的扇区是 4194303(在命令 p 的结果中查看,或者在新建分区时可用的 First sector 最大值),因此,该分区使用的扇区范围为 4190208~4194303。
Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): 3 First sector (526336-4194303, default 526336): 4190208 Last sector, +sectors or +size{K,M,G,T,P} (4190208-4194303, default 4194303): Created a new partition 3 of type ‘Linux‘ and of size 2 MiB. Command (m for help): t Partition number (1,3, default 3): 3 Hex code (type L to list all codes): a2 Changed type of partition ‘Linux‘ to ‘unknown‘.
最后创建 Linux 分区,使用剩下的所有空间。
Command (m for help): n Partition type p primary (2 primary, 0 extended, 2 free) e extended (container for logical partitions) Select (default p): p Partition number (2,4, default 2): 2 First sector (526336-4194303, default 526336): Last sector, +sectors or +size{K,M,G,T,P} (526336-4190207, default 4190207): Created a new partition 2 of type ‘Linux‘ and of size 1.8 GiB.
分好区的情况,仔细检查,是否正确。
Command (m for help): p Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 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: 0x4478d16c Device Boot Start End Sectors Size Id Type /dev/loop0p1 2048 526335 524288 256M b W95 FAT32 /dev/loop0p2 526336 4190207 3663872 1.8G 83 Linux /dev/loop0p3 4190208 4194303 4096 2M a2 unknown
写操作应用修改退出
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: Invalid argument The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
使用命令 partprobe 通知内核我们修改了分区。
$ sudo partprobe /dev/loop0 $ sudo fdisk -l /dev/loop0 Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 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: 0x4478d16c Device Boot Start End Sectors Size Id Type /dev/loop0p1 2048 526335 524288 256M b W95 FAT32 /dev/loop0p2 526336 4190207 3663872 1.8G 83 Linux /dev/loop0p3 4190208 4194303 4096 2M a2 unknown
格式化 FAT32 磁盘分区,建立文件系统
$ sudo mkfs /dev/loop0p1 mke2fs 1.44.1 (24-Mar-2018) Discarding device blocks: done Creating filesystem with 262144 1k blocks and 65536 inodes Filesystem UUID: ac344fe7-038c-4544-9473-7b994dd2705f Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
挂载 FAT32 文件系统,拷贝所需文件
$ mkdir mount_temp $ sudo mount /dev/loop0p1 ./mount_temp $ sudo cp output_files/soc_system.rbf u-boot.scr soc_system.dtb linux_kernle/arch/arm/boot/zImage ./mount_temp $ sudo sync $ sudo umount ./mount_temp
同理格式化 Linux 分区,拷贝根文件系统。
$ sudo mkfs /dev/loop0p2 mke2fs 1.44.1 (24-Mar-2018) Discarding device blocks: done Creating filesystem with 457984 4k blocks and 114688 inodes Filesystem UUID: 650a1fde-4197-4dce-b05b-7bf0b502ba07 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done $ sudo mount /dev/loop0p2 ./mount_temp $ sudo cp -avx rootfs/* ./mount_temp $ sudo sync $ sudo umount ./mount_temp
写 RAW 分区,先写入 preloader-mkpimage.bin,大小为 262144B,即 256 KiB;接着写入 u-boot.img,需要跳过 preloader-mkpimage.bin 占用的 256 KiB,所以,加上 seek 参数,保留了之前的 bs*seek 大小的空间内容。
$ sudo dd if=./software/spl_bsp/preloader-mkpimage.bin of=/dev/loop0p3 bs=64k 4+0 records in 4+0 records out 262144 bytes (262 kB, 256 KiB) copied, 0.0844437 s, 3.1 MB/s $ sudo dd if=./software/spl_bsp/uboot-socfpga/u-boot.img of=/dev/loop0p3 bs=64k seek=4 3+1 records in 3+1 records out 238312 bytes (238 kB, 233 KiB) copied, 0.0675932 s, 3.5 MB/s
松开
$ sudo losetup -d /dev/loop0
最后就可以插上sd卡,烧写了。
$ sudo dd if=./sd.img of=/dev/sdb bs=1M $ sudo sync
以上是关于创建文件系统镜像文件的主要内容,如果未能解决你的问题,请参考以下文章