[Libguestfs] [PATCH] make-fs: Don't use du --apparent-size to estimate input size.

Richard W.M. Jones rjones at redhat.com
Tue Feb 18 08:48:21 UTC 2020


When calculating the initial size of the disk we must estimate
how much space is taken by the input.  This is quite difficult.

For directories we used ‘du --apparent-size -bs DIR’.  This is wrong
because ’-b’ implies ‘--apparent-size --block-size=1’.  But also
‘--apparent-size’ causes du to count the file size rather than number
of blocks used by files.

If you have a directory containing many small files this usually
underestimates resulting in disk sizes which are far too small to
actually contain the files.

There's no really good answer here because du can't exactly do what we
want, but we can at least remove this flag.  This causes much larger
estimates and therefore much larger virtual disks.

Thanks: Nikolay Ivanets
---
 make-fs/make-fs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/make-fs/make-fs.c b/make-fs/make-fs.c
index 5d8c3a385..386142280 100644
--- a/make-fs/make-fs.c
+++ b/make-fs/make-fs.c
@@ -393,7 +393,7 @@ static int
 estimate_input (const char *input, uint64_t *estimate_rtn, char **ifmt_rtn)
 {
   struct stat statbuf;
-  const char *argv[6];
+  const char *argv[5];
   CLEANUP_UNLINK_FREE char *tmpfile = NULL;
   CLEANUP_FCLOSE FILE *fp = NULL;
   char line[256];
@@ -424,11 +424,10 @@ estimate_input (const char *input, uint64_t *estimate_rtn, char **ifmt_rtn)
     }
 
     argv[0] = "du";
-    argv[1] = "--apparent-size";
-    argv[2] = "-b";
-    argv[3] = "-s";
-    argv[4] = input;
-    argv[5] = NULL;
+    argv[1] = "--block-size=1";
+    argv[2] = "-s";
+    argv[3] = input;
+    argv[4] = NULL;
 
     if (exec_command ((char **) argv, tmpfile) == -1)
       return -1;
-- 
2.24.1




More information about the Libguestfs mailing list