创建文件系统镜像文件

在这里,我们想建立一个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

原文地址:https://www.cnblogs.com/cuclaoliu/p/8794431.html

时间: 2024-08-29 23:49:44

创建文件系统镜像文件的相关文章

Linux mkisofs 创建光盘镜像文件(Linux指令学习笔记)

mkisofs命令 创建光盘文件的系统的命令是mkisofs.光盘系统有多种格式,利用Linux系统提供的光盘文件系统创建 命令mkisofs,可以创建多种iso9660文件系统. 我们一般不用mkisofs直接刻录光盘,而常用它创建一个光盘映像文件. mkisofs用法如下: mkisofs  [options] [-o filename] pathspec[pathspec...] -o filename :光盘映像文件名. pathspec:    要刻录的文件名,目录或者树目录. opt

Docker镜像文件(images)的存储结构

原文作者: Troy Howard(http://blog.thoward37.me/)原文地址:http://blog.thoward37.me/articles/where-are-docker-images-stored/ 翻译:刘斌(http://liubin.org/)本文地址:http://liubin.org/2014/03/10/about-docker-images-storage/ ? 关于本文的任何指正,请在新浪微博联系我或者在Twitter上. 如果你是刚开始接触 Doc

文件系统管理 之 Linux 创建文件系统及挂载文件系统流程详解

阅读此文,必须具备知识点:<Linux 查看磁盘分区.文件系统.使用情况的命令和相关工具介绍><实例解说 fdisk 使用方法><合理规划您的硬盘分区><Fedora / Redhat 软件包管理指南> 如果您想加载一个分区(文件系统),首先您得确认文件系统的类型,然后才能挂载使用,比如通过mount 加载,或者通过修改 /etc/fstab来开机自动加载:如果您想添加一个新的分区,或者增加一个新的硬盘,您要通过分区工具来添加分区,然后要创建分区的文件系统,

[9-1]磁盘分区、创建文件系统、挂载以及链接文件

本文旨在复习磁盘分区.创建文件系统.挂载以及链接文件的基本概念 知识储备 硬盘出厂其实不能写入数据的,是"盲盘",需要处理后使用,格式化分为2种 低格:将空白磁盘划分出柱面.磁道及若干个扇区,每个扇区又划分出标识部分ID.间隔区GAP和数据区DATA等.现在大多出厂已经完成低格,只需要用户磁盘分区即可. 高格:真正意义上的创建文件系统,明确存储设备或分区上的文件方法和数据结构,即在存储设备上组织文件的方法. 文件系统是用户存取磁盘文件操作的"代理人" 不同类型的操作

《Linux学习并不难》文件系统管理(3):在Linux系统中创建文件系统

18.3  <Linux学习并不难>文件系统管理(3):在Linux系统中创建文件系统 使用mkfs命令可以在分区上创建各种文件系统.mkfs命令本身并不执行建立文件系统的工作,而是去调用相关的程序来执行.这里的文件系统是要指定的,比如xfs.ext4.ext3.vfat或者是msdos等. 命令语法: mkfs [选项] [设备] 命令中各选项的含义如表所示. 选项 选项含义 -t <文件系统类型> 指定文件系统类型 -v 显示详细信息 例:为/dev/sda5磁盘分区创建xfs

挂载镜像文件,创建yum仓库

mount /dev/sr0 /mnt    #挂载镜像文件   cd /etc/yum.repos.d/ rm –rf *.repo   vi /abc.repo       #创建本地yum源文件 [abc]           #文件名 name=abc baseurl=file:///mnt    #指定安装源挂载路径 enabled=1       #开启yum仓库 gpgcheck=0         #不检查软件序列号 原文地址:http://blog.51cto.com/1368

给在XenCenter管理的XenServer服务器上创建的虚拟机共享(上传)宿主机的镜像文件

以win10为例,首先将要上传的镜像文件放在一个文件夹里,然后设置文件夹为共享文件夹.此处新建一个用户.步骤为"控制面板"-"用户账户"-"管理其他账户"-"在电脑设置中创建新用户" -"将其他人添加到这台电脑",然后输入用户名和密码.完成创建新的用户. 选择需要共享的文件夹,右键-授予访问权限-特定用户 3.选择要共享的用户,必须要有密码,不能是everyone.选择完了点击共享. 4.打开VMware,

文件系统管理之分区与创建文件系统

对于机械式磁盘,首先会经过生产厂商的低级格式化(划分磁道的过程),然后需要创建分区(根据柱面划分),最后经过高级格式化(格式化成使用的文件系统),再挂载(将设备关联至根文件系统),之后就可以正常的使用这个磁盘设备了 分区命令:fdisk 这个命令是一个交互式命令 fdisk -l [device] 查看当前系统所有磁盘设备(或某磁盘设备)上的分区,输出内容形似: 可以看出,分区是根据柱面划分的 fdisk device 实现对某设备进行分区相关操作 进入交互式模式之后,使用m子命令可以查看可以使

创建文件系统

在说完了磁盘的分区之后,该说的就是创建文件系统了,一块磁盘在使用之前只是一块有磁性的物品而已,需要我们人为的对其进行规划.所以使用磁盘的第一步是格式化.格式化分为低级格式化和高级格式化.低级格式化是在物理层面完成格式化,在磁盘设备出厂的时候,通过低级格式化来创建磁道.在低级格式化完成之后,磁盘就具有了能够使用的功能.但是我们要想往里头存数据,还需要更进一步的格式化,就叫做高级格式化,是在分区之后进行的,它的作用是用来创建文件系统,为分区构建逻辑编址单元.簇和块来完成数据的存储.在这里的一个块只能