首页 > 技术文章 > 在Linux下制作一个磁盘文件,在u-boot 阶段对emmc 烧写整个Linux系统方法

chenfulin5 2017-03-31 09:53 原文

在Linux 下制作一个磁盘文件, 可以给他分区,以及存储文件,然后dd 到SD卡便可启动系统。

在u-boot 下启动后可以读取该文件,直接在u-boot 阶段就可以做烧写操作,省略了进入系统后才进行烧写的动作。


* 参考 http://www.orangepi.org/Docs/Makingabootable.html , 具体步骤如下:
  • 一、 dd 一段空白空间到一个普通文件上

    sudo dd if=/dev/zero of=my.img  bs=1M count=200
    # dd 一个200 兆的空间到
  • 二、 加载这个镜像通过 losetup 命令

    sudo losetup -f --show my.img
    # 它显示你挂载到那个设备节点下,一般为 /dev/loop0 ,1 ,2, 3...7
    /dev/loop0
  • 三、 通过 fdisk 命令对磁盘文件进行分区,就跟普通磁盘文件一样。

    sudo fdisk  /dev/loop0
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0x2e7df78e.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won't be recoverable.

    Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

    Command (m for help): n                        # 新建一个分区
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p                          # 主分区
    Partition number (1-4, default 1): 1           #  分区号
    First sector (2048-409599, default 2048):      #  默认2048
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-409599, default 409599): +30M 
                                                        # 给30M空间第一个分区 
    Command (m for help): n                        # 新建一个分区
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): p                            # 主分区
    Partition number (1-4, default 2):               # 使用默认的2
    Using default value 2                    
    First sector (63488-409599, default 63488):      # 使用默认值  
    Using default value 63488
    Last sector, +sectors or +size{K,M,G} (63488-409599, default 409599):     # 直接到结束
    Using default value 409599                

    Command (m for help): t                          #  改变分区
    Partition number (1-4): 1                        #  改变第几个分区
    Hex code (type L to list codes): e               #  改为FAT16分区
    Changed system type of partition 1 to e (W95 FAT16 (LBA))

    Command (m for help): a                          #  增加boot 属性
    Partition number (1-4): 1                        #  指定第一个分区增加boot 属性

    ommand (m for help): w                           #  保存相关信息
    The partition table has been altered!
        
    Calling ioctl() to re-read partition table.

    WARNING: Re-reading the partition table failed with error 22: 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)

    WARNING: If you have created or modified any DOS 6.x
    partitions, please see the fdisk manual page for additional
    information.
    Syncing disks.
  • 三、 同步这个img 并对他进行格式化

    sudo kpartx -av /dev/loop0
    [sudo] password for aplex: 
    add map loop0p1 (252:0): 0 61440 linear /dev/loop0 2048
    add map loop0p2 (252:1): 0 346112 linear /dev/loop0 63488
    # 格式化
    sudo mkfs.vfat    -n "boot" -F 16  /dev/mapper/loop0p1
    sudo mkfs.ext3   -L "rootfs"          /dev/mapper/loop0p2 
  • 四、 拷贝文件到两个分区

    sudo mount /dev/mapper/loop0p1 /mnt
    sudo cp  myfile    /mnt
    sudo umount /mnt
    # 第二个分区操作方法如上
  • 五、 将.img 解除映射

    sudo  kpartx -d /dev/loop0
    sudo  losetup -d /dev/loop0
    PART_START=$(fdisk -l /dev/mmcblk0 | grep "Linux" | awk '{print $2}')

    fdisk /dev/mmcblk0 << EOF
    d            # 删除一个分区
    2            # 指定删除第二个分区
    n
    p
    2            
    $PART_START     # 指定开始的地址
                    # 默认将整个 emmc 都扩到etx4 文件系统
    w
    EOF
  • 七、 重启,执行 resize2fs /dev/mmcblk0p2

    root@sbc-7109:~# resize2fs /dev/mmcblk0p2 
    root@sbc-7109:~# df -h
    Filesystem                Size      Used Available Use% Mounted on
    /dev/root                 3.6G    174.1M      3.2G   5% /
    devtmpfs                234.4M    160.0K    234.2M   0% /dev
    tmpfs                    40.0K         0     40.0K   0% /mnt/.psplash
    /dev/mmcblk0p1           10.0M      4.6M      5.4M  46% /media/mmcblk0p1
    tmpfs                    16.0M    148.0K     15.9M   1% /var/volatile
    tmpfs                   242.6M         0    242.6M   0% /dev/shm
    tmpfs                    16.0M         0     16.0M   0% /media/ram   
  • 扩容成功

推荐阅读