3

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.

3
  • 1
    should that dd be reading from /dev/zero rather than /dev/null? Is that a typo? Jul 21, 2020 at 9:39
  • Have you tried using partclone? I think that if you do a partclone backup of the filesystem image to stdout and then pipe that in to another partclone instance to do a "restore" of the block device, then you'd only copy used blocks rather than the entire filesystem. The catch is you'd have to determine the partition size and create the partition yourself prior to restoring from the "backup". Jul 21, 2020 at 10:15
  • Can't you do it simpler? Create the file system in the target device and mount it. Then copy the files like in step 4, but instead of the loop file system you would do it directly to the target file system. If you would rather have a single "image" file, you could create a tar archive you can extract to the target file system. Jul 21, 2020 at 12:05

2 Answers 2

1

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

1

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
2
  • 2
    Additional notes: The partition size is independent of the filesystem size. Also, when preparing the image, you can use resize2fs -M /dev/image/partitionx to shrink to the minimum size. Then use e2image -rap to transfer the data.
    – Hermann
    Jul 21, 2020 at 10:27
  • 1
    Note that when creating the image, it is possible to copy the files into the image as it is being formatted, like mke2fs -d /source example.img to copy all files from the /source directory into the root directory of the filesystem. This can be more efficient if your source directory is "exact" and does not need to be modified/customized after copying into the files into the image.
    – LustreOne
    Jul 23, 2020 at 7:36

You must log in to answer this question.