tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

makefs: sparse files



The patch below is based on something one of our guys did.
I've changed the option to something vaguely mnemonic -Z (no zeros ;-)
and updated the man page.

This greatly reduces the time taken to generate files for use with
vm's.

This is for ffs only so far.

--sjg

Index: usr.sbin/makefs/ffs.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/ffs.c,v
retrieving revision 1.47
diff -u -p -r1.47 ffs.c
--- usr.sbin/makefs/ffs.c       19 Apr 2012 17:28:25 -0000      1.47
+++ usr.sbin/makefs/ffs.c       13 Jun 2012 19:34:40 -0000
@@ -498,11 +498,22 @@ ffs_create_image(const char *image, fsin
                bufsize = sfs.f_iosize;
 #endif
        bufrem = fsopts->size;
-       if (debug & DEBUG_FS_CREATE_IMAGE)
+
+       if (fsopts->sparse) {
+               if (ftruncate(fsopts->fd, bufrem) == -1) {
+                       printf ("ERROR in truncate. Sparse option disabled\n");
+                       fsopts->sparse = 0;
+               } else {
+                       bufrem = 0; /* File truncated at bufrem. Remaining is 0
 */
+                       buf = NULL;
+               }
+       }
+
+       if ((debug & DEBUG_FS_CREATE_IMAGE) && fsopts->sparse == 0)
                printf(
                    "zero-ing image `%s', %lld sectors, using %d byte chunks\n"
,
                    image, (long long)bufrem, bufsize);
-       if ((buf = calloc(1, bufsize)) == NULL) {
+       if ((bufrem > 0) && ((buf = calloc(1, bufsize)) == NULL)) {
                warn("Can't create buffer for sector");
                return (-1);
        }
@@ -516,7 +527,8 @@ ffs_create_image(const char *image, fsin
                }
                bufrem -= i;
        }
-       free(buf);
+       if (buf)
+               free(buf);
 
                /* make the file system */
        if (debug & DEBUG_FS_CREATE_IMAGE)
Index: usr.sbin/makefs/makefs.8
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.8,v
retrieving revision 1.40
diff -u -p -r1.40 makefs.8
--- usr.sbin/makefs/makefs.8    19 Apr 2012 16:00:25 -0000      1.40
+++ usr.sbin/makefs/makefs.8    13 Jun 2012 19:34:40 -0000
@@ -42,6 +42,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl x
+.Op Fl Z
 .Op Fl B Ar endian
 .Op Fl b Ar free-blocks
 .Op Fl d Ar debug-mask
@@ -206,6 +207,10 @@ Chip flash file system.
 .El
 .It Fl x
 Exclude file system nodes not explicitly listed in the specfile.
+.It Fl Z
+Create a sparse file for
+.Sy ffs .
+This is useful for virtual machine images.
 .El
 .Pp
 Where sizes are specified, a decimal number of bytes is expected.
Index: usr.sbin/makefs/makefs.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.c,v
retrieving revision 1.34
diff -u -p -r1.34 makefs.c
--- usr.sbin/makefs/makefs.c    29 Apr 2012 13:32:21 -0000      1.34
+++ usr.sbin/makefs/makefs.c    13 Jun 2012 19:34:40 -0000
@@ -117,7 +117,7 @@ main(int argc, char *argv[])
        start_time.tv_sec = start.tv_sec;
        start_time.tv_nsec = start.tv_usec * 1000;
 
-       while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) {
+       while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:xZ")) != -1) {
                switch (ch) {
 
                case 'B':
@@ -230,6 +230,10 @@ main(int argc, char *argv[])
                        fsoptions.onlyspec = 1;
                        break;
 
+               case 'Z':
+                       fsoptions.sparse = 1;
+                       break;
+
                case '?':
                default:
                        usage();
@@ -329,7 +333,7 @@ usage(void)
 
        prog = getprogname();
        fprintf(stderr,
-"usage: %s [-x] [-B endian] [-b free-blocks] [-d debug-mask]\n"
+"usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size]\n"
 "\t[-m maximum-size] [-N userdb-dir] [-o fs-options] [-S sector-size]\n"
 "\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n"
,
Index: usr.sbin/makefs/makefs.h
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.h,v
retrieving revision 1.26
diff -u -p -r1.26 makefs.h
--- usr.sbin/makefs/makefs.h    19 Apr 2012 17:09:53 -0000      1.26
+++ usr.sbin/makefs/makefs.h    13 Jun 2012 19:34:40 -0000
@@ -136,6 +136,7 @@ typedef struct {
        int     freeblockpc;    /* free block % */
        int     needswap;       /* non-zero if byte swapping needed */
        int     sectorsize;     /* sector size */
+       int     sparse;         /* sparse image, don't fill it with zeros */
 
        void    *fs_specific;   /* File system specific additions. */
 } fsinfo_t;


Home | Main Index | Thread Index | Old Index