Blob Blame History Raw
From 05d53de9cdc06a5c822b13aed9cae6ce9a278e09 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 22 Jan 2014 13:10:08 +0000
Subject: [PATCH] sparsify: Prevent overwriting block or char output devices
 (RHBZ#1056290).

virt-sparsify doesn't work if the output is a block device, and cannot
possibly work if the output is a char device.  Currently if you try
this it actually overwrites (deletes) the output device which is not
exactly desirable.  Therefore throw an error and exit.

Cherry picked from commit 13bca32e4ec9213e69ea55e837dea846719fb67e.
Added additional supporting functions is_block_device and
is_char_device from upstream.
---
 sparsify/sparsify.ml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/sparsify/sparsify.ml b/sparsify/sparsify.ml
index 659d46f..d2f5347 100644
--- a/sparsify/sparsify.ml
+++ b/sparsify/sparsify.ml
@@ -151,6 +151,23 @@ read the man page virt-sparsify(1).
   if contains_colon outdisk then
     error (f_"output filename '%s' contains a colon (':'); qemu-img command line syntax prevents us from using such an image") outdisk;
 
+  (* Check the output is not a block or char special (RHBZ#1056290). *)
+  let is_block_device file =
+    try (Unix.stat file).Unix.st_kind = Unix.S_BLK
+    with Unix.Unix_error _ -> false
+  and is_char_device file =
+    try (Unix.stat file).Unix.st_kind = Unix.S_CHR
+    with Unix.Unix_error _ -> false
+  in
+
+  if is_block_device outdisk then
+    error (f_"output '%s' cannot be a block device, it must be a regular file")
+      outdisk;
+
+  if is_char_device outdisk then
+    error (f_"output '%s' cannot be a character device, it must be a regular file")
+      outdisk;
+
   indisk, outdisk, compress, convert,
     debug_gc, format, ignores, machine_readable,
     option, quiet, verbose, trace, zeroes
-- 
1.8.3.1