Making a bootable .img image file

From Orange Pi
Jump to: navigation, search

Note: these instructions are for a Linux computer, not the O-Pi!!!!


This page will describe how to make an image file which has the extension .img (pronounced 'dot image'). The method is not used to make a bootable SD card directly; instead, it is used to make a .img image file which can burned directly onto an SD card.



From the SD card

Assuming you have a bootable SD card, and the OS from the SD card has some custom-made/tailor-made software, then you can backup the OS from the SD card to an image file. The following will tell you how to create an .img file from your SD card. This method is easy and you only need to insert you SD card into your PC, and then run the following command:

sudo fdisk -l


It checks the SD card device node. When the USB port is being used, it will show "/dev/sdX". And when the mmc interface is being used, it will show "/dev/mmcblkY"

where X is a letter such as a, b, c,...etc.,

and Y is a number such as 0, 1, 2,...etc.


In the following, the phrase ${card} indicates either "/dev/sdX" or "/dev/mmcblkY".


Then use the command dd to backup the SD card into the .img file.

sudo dd if=${card} of=${output_dir}/Orange.img

where ${output_dir} indicates the directory in which the backed up Orangepi.img is stored.


Use a series of commands to specify an image

If you have no SD card currently available, but you just want to make a .img image file which can be saved on your PC and later burnt onto a card, then a series of commands can be used to implement this process. However, you must have already compiled uImage, scrpti.bin, uEnv.txt and the corresponding kernel lib directory (the kernel modules is included in it).


Create an empty .img file

First, create an empty .img file in the ${output_dir} directory:

sudo dd if=/dev/zero of=${output_dir}/Orangepi.img bs=1M count=3500

where the parameters bs=1M and count=3500 govern the size of your created .img file.


Of course, you can set the count value to whatever you want. Here, bs=1M and count=3500 indicate about 3.7GB, which can be burnt onto a normal 4GB SD card.


Load the created.img file

sudo losetup -f --show Orangepi.img

The command will mount the Orangepi.img (or whatever file name you have given it) on the first unused loop node, and show a line of messages about the number of mounted nodes, such as:

dev/loopX

where X indicates a number such as 0, 1, 2 , 3...etc


Partitioning the .img file

Use the fdisk command to partition the storage. Here, we'll create two partitions: one is 20MB, the other is the rest of the remaining space on the card.

sudo fdisk /dev/loopX


Add the partition:


Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x38d0a03d.
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 # Type n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): # Press Enter Key Using default response p Partition number (1-4, default 1): # Press Enter Key Using default value 1 First sector (2048-7167999, default 2048): # Press Enter Key Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-7167999, default 7167999): +20M # Type +20M
Command (m for help): n # Type n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): # Press Enter Key Using default response p Partition number (1-4, default 2): # Press Enter Key Using default value 2 First sector (43008-7167999, default 43008): # Press Enter Key Using default value 43008 Last sector, +sectors or +size{K,M,G} (43008-7167999, default 7167999): # Press Enter Key Using default value 7167999
Command (m for help): w # Type 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) Syncing disks.


Syncing the .img file partition

sudo kpartx -av /dev/loop0


Formatting the .img file partition

sudo mkfs.vfat /dev/mapper/loop0p1
sudo mkfs.ext4 /dev/mapper/loop0p2


Burn u-boot

sudo dd if=/dev/zero of=/dev/loopX bs=1k count=1023 seek=1
sudo dd if=${u-boot-dir}/u-boot-sunxi-with-spl.bin of=/dev/loopX bs=1024 seek=8


where the phrase ${u-boot-dir} represents the directory in which the u-boot-sunxi-with-spl.bin is.


Copy files

Copy uImage, script.bin, uEnv.txt(or boot.src) into the first partition:

sudo mount /dev/mapper/loopXp1 /mnt
sudo cp ${uImage_dir}/uImage /mnt
sudo cp ${script.bin_dir)/script.bin /mnt
sudo cp ${uEnv.txt_dir}/uEnv.txt /mnt
sudo umount /mnt


where the phrase ${uImage_dir} indicates the existing directory of uImage,

and ${script.bin_dir} indicates the existing directory of script.bin

and ${uEnv.txt_dir} indicates the existing directory of uEnv.txt.

For more info about uEnv.txt, please refer to:

Setting_up_the_bootable_SD_card#Boot_script


Copy the OS root file system into the second partition.

First,mount the second partition:

sudo mount /dev/mapper/loopXp2 /mnt

Then copy the OS root file system.


Please refer to: Setting_up_the_Linux_distribution_root_file_system


Replace the kernel module

sudo mount /dev/mapper/loopXp2 /mnt
sudo mkdir -p /mnt/lib/modules
sudo rm -rf /mnt/lib/modules/
sudo cp -r ${lib_dir}/lib /mnt/
sudo umount


Replace the phrase ${lib_dir} with the corresponding directory in which the kernel lib exists. This "lib" includes the kernel "modules" package and also some .so files.


Unmount the .img file

sudo kpartx -d /dev/loop0
sudo losetup -d /dev/loop0



After all that, you have finally created a bootable .img image file, and you can burn it onto your SD card either now or later.


(With thanks to native speaker “roses” for checking and upgrading this document)