I think it's a difficult problem and one which won't be solved using du. For comparison we have some nbdkit plugins for creating filesystems and they are far more sophisticated, especially this one: https://github.com/libguestfs/nbdkit/tree/master/plugins/floppy
I would note that the reason the test fails was nothing to do with free space. It was actually running out of inodes. With ext2/3/4 the number of inodes in the metadata is fixed at creation and based on the size of the disk (see /etc/mke2fs.conf inode_ratio and the mke2fs -i and -N options). Because of the small size of the disk, mke2fs was choosing < 100 inodes, but because the test had 100+ files it failed when creating the later files even though there was enough disk space. There's no easy way to fix this. I think it could even be considered as a bug in mke2fs.
Rich.
I've did extra testing and the issue really appears for small file systems composed from 100 x 10-bytes files or so because of lack of free inodes. Nevertheless we cannot swear that virt-make-fs will work for any possible file systems or set of files.--size=+N is for the rescue in such cases. Your patch made things worse because of sparse files. I think we cannot apply it. -- Mykola Ivanets