block: fail live snapshot if disk has no medium

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: fix snapshot on QED

QED's opaque data includes a pointer back to the BlockDriverState.
This breaks when bdrv_append shuffles data between bs_new and bs_top.
To avoid this, add a "rebind" function that tells the driver about
the new relationship between the BlockDriverState and its opaque.

The patch also adds rebind to VVFAT for completeness, even though
it is not used with live snapshots.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: close unused image files at the end of streaming

Close the now unused images that were part of the previous backing file
chain and adjust ->backing_hd properly.

Note that this only works with relative paths.
Given the images:

 /tmp/a/base.raw
 /tmp/a/snap1.qcow2
 /tmp/b/snap2.qcow2

chained as:

 base(bak:"") <- snap1(bak:"base.raw") <- snap2(bak:"../a/snap1.qcow2")

merging snap1 and snap2 we will obtain:

 base(bak:"") <- snap2(bak:"base.raw")

However this should be maintained by the user/admin: one can also
screw up relative paths using qemu-img manually.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Federico Simoncelli <fsimonce@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: pass new base image format to bdrv_change_backing_file

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: push bdrv_change_backing_file error checking up from drivers

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: update in-memory backing file and format

These are needed to print "info block" output correctly.  QCOW2 does this
because it needs it to write the header, but QED does not, and common code
is the right place to do it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: clean up is_allocated_base

intermediate == NULL should never happen in is_allocated_base,
because the base is always on top's backing file chain.
Clean up the loop, and add comments about why we return 0 vs. 1.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: do not stream sectors that are unallocated in the base

Full streaming of a block device will always populate all clusters of the
destination, with logical zero clusters in the case of qed but with full
clusters in the case of qcow2.  However, this is not necessary; even
with qed it is bad because it will perform useless reads of the source,
which could be on a slow network medium).  A cluster that is unallocated
should never be read by the guests, except for silly things such as
"dd if=/dev/vda of=/dev/null", so it is not really necessary to stream
it.  If the guest does those silly things we will have to read it only
once from the slow source, since the behavior does not change for
guest-initiated copy-on-read.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add blkmirror block driver

Mirrored writes are used by live block copy.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
blkmirror: adjust for streaming

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add mirroring to transaction

With it comes a new image creation mode, "no-backing-file", that can
be used to stream an image so that the destination does not need the
original image's backing file(s).

Both bdrv_append and blkmirror will set the backing_hd on the target,
even if the image is created without one, so that both streaming and
copy-on-write work properly (at least with qcow2 or qed, not raw).

Streaming mode works with the following gotchas:

- streaming will rewrite every bit of the source image;

- zero writes are not supported by the blkmirror driver, hence both
  the source and the destination image will grow to full size.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add drive-mirror command and HMP equivalent

Since QMP transactions are supposed to take (pseudo) QMP commands,
add a standalone drive-mirror command.

The corresponding HMP command does not provide atomic snapshot+mirror,
but it can still be used together with image streaming.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: add the drive-reopen command

Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: accept nop change of backing file

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
build: sanitize the environment in the shell scripts

Otherwise, even simple things such as "tr a-z A-Z" can cause
problems with Turkish locales due to dotted/dotless I.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>