Hi,

Am Dienstag, 24. Juli 2012, 15:28:54 schrieb Bastian Friedrich:
> 
> we are using Bacula's raw block device backup features for backing up
> virtual machines that use raw devices (logical volumes, or DRBD) as their
> storage. As advised by the bacula documentation (obviously, for the purpose
> of sparing storage space), we set "sparse = yes" for the respective file
> sets.
> 
> Recently, we discovered a problem with systems backed up in this way.
> 
> Bacula does not only back up sparse data; it also restores the data in such
> a way, which can result in problems. Imagine a virtual machine that has a
> (non- sparse!) file with large chunks of zero data in its file system. When
> these data are backed up, these null data are stored as a sparse stream.
> 
> During restore, however, this can result in a problem: The sparse data (all
> zeros) are not written to the disk; instead, the respective chunk is just
> skipped (via seek). As a result, the virtual machine now has random data in
> its (formerly all-zero) file.

the attached patch modifies Bacula's behavior during restore of data that 
originates from a block device with the "sparse" option set. Instead of simply 
seeking to the requested file position, zero bytes are written to the 
file/device in chunks of 64k.

This results in sparse blocks (i.e., blocks that only contain zeros) on the 
original disk being reproduced identically to the original data during restore 
to a new block device (of identical size). Restoring to a file ("image") 
results in it having the correct size, and of course identical data (e.g., 
identical md5sums).

On the downside, restored data of block devices always require the full amount 
of space on disk, even if they contain sparse blocks.

Comments welcome. The patch has NOT yet been tested to full extend; I'd 
welcome any feedback.

Thx
   Bastian

-- 
Collax GmbH . Basler Str. 115a . 79115 Freiburg . Germany
p: +49 (0) 89-990 157-28        www.collax.com

Geschäftsführer: Bernd Bönte, Boris Nalbach
AG München HRB 173695. Ust.-IdNr: DE270819312
This patch results in writing zeros instead of just seeking when restoring
raw devices. Raw devices will leave existent data in tact when seeking; this
may (will!) corrupt restored data.
--- bacula-5.2.6.ori/src/filed/restore.c	2012-02-21 16:59:33.000000000 +0100
+++ bacula-5.2.6/src/filed/restore.c	2012-07-26 13:54:29.000000000 +0200
@@ -1323,8 +1323,40 @@
       unser_begin(*data, OFFSET_FADDR_SIZE);
       unser_uint64(faddr);
       if (*addr != faddr) {
+         ssize_t offset = faddr - *addr;
          *addr = faddr;
-         if (blseek(bfd, (boffset_t)*addr, SEEK_SET) < 0) {
+
+	 if ((jcr->last_type == FT_RAW) && (offset > 0)) {
+            Dmsg0(150, "Sparse seek on a raw file -- dumping zeros instead\n");
+
+	    /* Pre-allocate a re-usable 64k zero-byte-buffer */
+	    static const int bufsize = 65536;
+	    char *buf = (char *)malloc(bufsize);
+	    if (!buf) {
+	       Jmsg1(jcr, M_ERROR, 0,
+		  "Out of memory while allocating zero buffer of length %d\n", bufsize);
+	       return false;
+	    }
+	    memset(buf, 0, bufsize);
+
+            /* Now, dump zeros in (max) 64k chunks */
+            while (offset > 0) {
+	       ssize_t chunksize = (offset > bufsize) ? bufsize : offset;
+               if (bwrite(bfd, buf, chunksize) != chunksize) {
+                  berrno be;
+                  Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), 
+                        jcr->last_fname, be.bstrerror(bfd->berrno));
+                  free(buf);
+                  return false;
+               }
+	       offset -= chunksize;
+	    }
+	    free(buf);
+
+#if 0
+	    Dmsg2(250, "Raw zeros result in current file position %d, expecting to be at address %d\n", blseek(bfd, 0, SEEK_CUR), *addr);
+#endif
+	 } else if (blseek(bfd, (boffset_t)*addr, SEEK_SET) < 0) {
             berrno be;
             Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
                   edit_uint64(*addr, ec1), jcr->last_fname, 
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Bacula-devel mailing list
Bacula-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-devel

Reply via email to