TransWikia.com

How to generate small image of big ext4 partition?

Unix & Linux Asked by Malano on October 30, 2021

I’m working on low level memory writing of linux filesystem for embedded purpose. I created the partition image using following procedure:

  1. Create an empty image file ex for 4GiB.
    dd if=/dev/null of=example.img bs=1M seek=4096
  2. Add a filesystem to it (ext4)
    mkfs.ext4 -F example.img
  3. Mount it on local machine
    mkdir /mnt/example.img
    mount -t ext4 -o loop exmaple.img /mnt/exampleimg
  4. Copy the filesystem to mounted partition image
    cp -r rootfs/* /mnt/exampleimg/
  5. Unmount the image
    umount /mnt/exampleimg

After writing created image to first sector of the partition I need to replace, everything work, and I have access to data, the problem is, I need to copy whole 16GB partition, while the data on it, is only 2GB, and it took a lot of time.

I created smaller image with the same data, and it works, but I cannot exceed the fixed size of the partition image. System see the partition size is still big (fdisk -l /dev/mmcblok0, and sectors count in /sys/class/block/mmcblkop2/size), probably because the partition table has not been changed, but in case of copying file bigger than free space on the image(not real size of partition) I get cp: write error: No space left on device.

I cannot modify partition size afterwards using on target linux, because this is the partition of root filesystem on which Linux is working. Is there any way to create image with fake size/ with no unnecessary limits? I would like to get image of 16GB partition with data, while image size is smaller than 16GB.

2 Answers

I cannot modify partition size afterwards using on target linux, because this is the partition of root filesystem on which Linux is working.

This is not true when extending a file system. The manual for resize2fs says:

If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.).

So you can create a much smaller image, copy it onto the device then use resize2fs to extend the file system:

resize2fs /dev/mmcblok0

There is an alternative method that's dependent on the hardware you're using. It's complicated so I can give an outline for the technique, but I'll avoid writing out exact steps to use this technique.

The technique there is to create an image with a re-configured bootloader.
This is designed to run directly from an sd card. The bootloader starts up Linux, telling it to directly run a script (rather than the regular /sbin/init). The script then:

  • formats internal flash
  • mounts internal flash to /mnt
  • copies all files from the sd card to /mnt
  • re-configures the bootloader in /mnt/boot to run /sbin/init instead of the script.
  • Shuts down linux

Answered by Philip Couling on October 30, 2021

Do everything like you do (i.e. create a small image, copy it as is) but the last step would be

resize2fs -p /dev/mmcblok0

Answered by Artem S. Tashkinov on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP