在Linux下制作一個磁盤文件,在u-boot 階段對emmc 燒寫整個Linux系統方法


在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   
  • 擴容成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM