How to decrease image size cloned by L4T?

Hi,
I found, after clone the system to image by using L4T, the size of the image file which ends with .img not .img.raw is 11GB, But actually thedf -h command shows it only used 4GB storage size of the emmc.
Is there any way to decrease image size cloned by L4T?

Thank you .

I think you can use simg2img binary to make it a regular ext4 and then use resize2fs.
img2simg will later be used to revert it before flashing

hello mozzie,

you’re able to make a sparse version of the image file,
for example, update backup.img.raw and re-name it as system.img as following.
$ sudo ./mksparse -v –fillpattern=0 /path/to/backup.img.raw system.img

thank you

Thank you.
I saw there is already one system.img file after I clone the system from the board by using

sudo ./flash -r -k APP -G .......... 

Does this command will automatic run mksparse tool?

During a normal flash where a new image is generated the file “Linux_for_Tegra/bootloader/system.img.raw” is first created (well, really it is created as “system.img”, and then renamed as “system.img.raw”). Once system.img.raw is created the flash software would use the command mentioned by @JerryChang ("mksparse" with a NULL fill pattern) to create the sparse file “system.img”.

Note that the flash software does not care if “bootloader/system.img” is raw or sparse, it works either way and flashes the same content. However, the raw “system.img.raw” is much larger than “system.img” (but the size of “system.img” approaches “system.img.raw” as the filesystem fills), so it flashes faster. On the other hand, you can’t loopback mount or examine a “sparse” image, but you can do so with a “raw” image.

If you place your own file as “bootloader/system.img”, regardless of how or where you got this (it could be a clone, it could be an edited clone, it could be from a previous flash), and if you use the “-r” option when manually flashing via “flash.sh”, then your image is used for the rootfs. All other partitions are flashed normally.

As an option to flash.sh there is “-S <size>” for specifying a size when generating a new image (or it seems useful as well when you are reusing a custom image not of default size…not sure if that is mandatory or not). Note that size is specified in integral multiples of either MiB or GiB, where an MiB is “1024x1024 = 1048576” bytes, and a GiB is “1024x1024x1024 = 1073741824” bytes. This would be the exact byte size ofsystem.img.raw”.

So if you used this command your raw image (and the partition size of rootfs, if there is enough room), then the partition would be 11GiB (or “11x1073741824” bytes):
sudo ./flash.sh -S 11GiB jetson-tx2 mmcblk0p1

If you were to reuse a clone and avoid creating a new image (meaning you’ve copied either the raw or sparse clone to file name “bootloader/system.img”), and if that image is 11811160064 bytes (11 GiB), then this command would work (note the addition of “-r”):
sudo ./flash.sh -S 11GiB -r jetson-tx2 mmcblk0p1

This doesn’t mean you can fit everything into 11GiB, it is just the command used. If you’ve customized an image and you know it fits in 10GiB, then you could use that.

Remember that what is actually flashed is always the size of the “raw” image, and that “mksparse” is just a tool to avoid flashing actual empty disk space content as a way to save time (and on the host PC to save space, although if you didn’t delete the raw image, then it actually consumes more space total).

Thank you very much.
Actually, I found after I deleted 2GB file, the df -h shows the system only used 4GB storage. Then I cloned the system, but the system.img after running mksparse command is still 11GB, and I did not use -S <size> in the flash command. so dose mksparse only deal with blocks which is filled by NULL?

mksparse” should take the larger raw version of the first file and create a smaller sparse image as the second file name. If the raw image is full, then the sparse image will be the same size as the raw image. As space is cleared in the raw image, that amount of space will be seen as a smaller file in the sparse image.

For example, if the raw image is 11GB (the size of the entire partition), and if the raw image has the filesystem full (such as seen when loopback mounted or flashed), then the sparse image will be 11GB. However, If the raw image is not full, and we’ll call it having 3GB of used space, with 8GB of free space, then the sparse file should approach “11GB - 8GB = 3GB” of total file size. Restoring the sparse file would in both cases produce an 11GB partition; restoring the full sparse image would result in the final partition being full, but restoring the 3GB sparse image would result in an 11GB partition which has only 3GB used (8GB free).

When you clone a system it does automatically run mksparse. What you will find is that if you named the clone “backup.img”, then it will have also created “backup.img.raw”. The “.img” file is sparse (smaller), the “.img.raw” file is the larger raw version. You get both versions, but on command line you only name the sparse version name.

Thank you very much