Creating a filesystem image

There are many ways that we can create disk images. QEMU can support many disk image formats. If you want to find details about the image formats that can be supported by QEMU, you can check using the following Linux command:

$ man qemu-img  

The supported image formats are:

  • raw: This plain disk image format has the advantage of being simple and easily exportable to all other emulators.
  • qcow2: This is the QEMU image format, which is the most versatile format. It is a compressed image format, so it has a smaller image size and can support snapshots.
  • qcow: This is the old QEMU image format.
  • cow: This is the User Mode Linux Copy-On-Write image format.
  • vdi: This is the VirtualBox 1.1-compatible image format.
  • vmdk: This is the VMware 3- and 4-compatible image format.
  • vpc: This is the VirtualPC-compatible image format (VHD).
  • cloop: This is the Linux compressed loop image, useful only to reuse directly compressed CD-ROM images present, for example, in Knoppix CD-ROMs.

We will use the qcow2 file format to test our initrd.img for the Android emulator. In order to create a file image in qcow2 format, we need to add the following code in the Android.mk Makefile of bootable/newinstaller:

... 
initrd: $(BUILT_IMG)

X86EMU_EXTRA_SIZE := 100000000
X86EMU_DISK_SIZE := $(shell echo ${BOARD_SYSTEMIMAGE_PARTITION_SIZE}+${X86EMU_EXTRA_SIZE} | bc)
X86EMU_TMP := x86emu_tmp

qcow2_img: $(BUILT_IMG)
mkdir -p $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}
cd $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}; mkdir data
mv $(PRODUCT_OUT)/initrd.img $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}
mv $(PRODUCT_OUT)/install.img $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}
mv $(PRODUCT_OUT)/ramdisk.img $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}
mv $(PRODUCT_OUT)/system.img $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}
make_ext4fs -T -1 -l $(X86EMU_DISK_SIZE) $(PRODUCT_OUT)/${TARGET_PRODUCT}.img $(PRODUCT_OUT)/${X86EMU_TMP}
mv $(PRODUCT_OUT)/${X86EMU_TMP}/${TARGET_PRODUCT}/*.img $(PRODUCT_OUT)/
qemu-img convert -c -f raw -O qcow2 $(PRODUCT_OUT)/${TARGET_PRODUCT}.img $(PRODUCT_OUT)/${TARGET_PRODUCT}-qcow2.img
cd $(PRODUCT_OUT); qemu-img create -f qcow2 -b
./${TARGET_PRODUCT}-qcow2.img ./${TARGET_PRODUCT}.img
...

The first thing that we have to do in the preceding Makefile is to create a directory layout that can be used by initrd.img, as shown in the following snippet:

Directory layout of x86emu_x86.img

We create a data folder to be used as data storage. Then, we move existing image files in the AOSP output folder to the $OUT/x86emu_tmp/x86emu_x86 directory in order to create the preceding directory structure. These file images will be moved back after the file image is generated.

Once we have the right directory structure, we can use the make_ext4fs command to create a raw filesystem image with the following options:

make_ext4fs -T {timestamp} -l {size of file system} {image file name} {source directory} {target out directory}  

The size of the filesystem is BOARD_SYSTEMIMAGE_PARTITION_SIZE; additionally, X86EMU_EXTRA_SIZE. BOARD_SYSTEMIMAGE_PARTITION_SIZE is defined in the board configuration file for the system image size. X86EMU_EXTRA_SIZE is for the space of ramdisk and kernel images.

The next step is to generate the qcow2 format from the raw file image using the qemu-img command. Both raw and qcow2 format images can be used by the emulator, but the raw file image is much larger than the qcow2 image.

Since the qcow2 image can support the snapshot feature, we can also generate a snapshot image (x86emu_x86.img) based on the qcow2 image (x86emu_x86-qcow2.img). If we use the snapshot image, we can restore to the original qcow2 image at any time. The snapshot image can be created using the following commands:

$ cd $OUT
$ qemu-img create -f qcow2 -b ./x86emu_x86-qcow2.img ./x86emu_x86.img

After the image is generated, we can inspect it using the qemu-img command as follows:

$ qemu-img info x86emu_x86.img
image: x86emu_x86.img
file format: qcow2
virtual size: 1.3G (1442177024 bytes)
disk size: 196K
cluster_size: 65536
backing file: ./x86emu_x86-qcow2.img
Format specific information:
compat: 1.1
lazy refcounts: false

We see that the x86emu_x86.img image is the snapshot image of x86emu_x86-qcow2.img.

In the image that we just created, there are no partitions created. When we mount it in the Android emulator, it will appear as a /dev/sda or /dev/block/sda device. If we want to create partitions for the image file, we need to use the edit_mbr tool to do so. You can explore this option on your own. With multiple partitions, we can put the system, data, and cache into different partitions, which is closer to the disk layout in most mobile devices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.19.26