From patchwork Sat Sep 30 20:31:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Maslenkin X-Patchwork-Id: 1841640 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Tqm5tj6X; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Ryf6r06QPz1yp7 for ; Sun, 1 Oct 2023 07:33:08 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qmgdD-00078w-8V; Sat, 30 Sep 2023 16:32:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qmgdB-00073E-6n; Sat, 30 Sep 2023 16:32:05 -0400 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qmgd8-00036h-IU; Sat, 30 Sep 2023 16:32:04 -0400 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5031ccf004cso23687504e87.2; Sat, 30 Sep 2023 13:32:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696105920; x=1696710720; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5t/J/0u+/OvyV52+QAvVcBhTX97cKPXthEGVm4YgMTk=; b=Tqm5tj6X3/ScP6u265+VNwsliAQCUCU2IgKxyMddPT1qLGgbAxe+oX2PybTfUTh/Tl z8lRjqUWvLjWmYoJR+pXy+wVRivJv0EajNqHKEVLZrP/V4c6Zgvsdmq2eCy1OmLd+10U +0yOpMPQ2OFHWRK0wCTa6vc2c0MfaqLOUH9QvqsOuYWJLzXwaeqMieJqqE/M+3SqeMg5 VQletsPFlPwIMMVVt67yupXO1tgKaJGSX9M+w7MdrUbIzLNw+MgYJeH+8uayyV5QQU6f F4z4/sOlBADu8BaK+yiuBUT96F+06uE8uIuaqBhJoez3+8WElap/5ypHU08LT/6G/DAG 7mMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696105920; x=1696710720; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5t/J/0u+/OvyV52+QAvVcBhTX97cKPXthEGVm4YgMTk=; b=nI40Q357U+kaLU6popqNR/DBJnaadO5iK4nwYF68D9QUmdDNuBMsmkYf6ZCRdw+zhx lPx3nno/Mef8VysHW1YFAJ4FD7gnNTkuq/cWar2XXzQrWXFn0xx8gVt1BDKOM3U7adY4 MBPHGzvEfSLS1O3mZQXV1W181nOAt3uGMFCq9tWTJv7xhZqsSMeJeoQAa/XUpH6KjNnn VQ+hcXDMM+N/5sAqPD5dzrpA+DHJqmHQ0u9KNWBOYb17lTU92sNX7KyKgMGGLK9N+u+Z ThXtZGW0SsBgqkh2YBXUy77dG/Uzu+KQiOj9qN1KVVtCrke/PJA+wMQrUFH9PhJT0ZTY Lq8Q== X-Gm-Message-State: AOJu0YyRxdwQ/PGXLnW4LOSQ+32IoEPoKe6jfraRVXEhHWQchz6sXgmw bYMi4vp2uCq+y92251QNDz4v3NZ5YRw6P2yurM4= X-Google-Smtp-Source: AGHT+IGFq66JqiTzO//ReStg3b4S29MOA+c5G9Ne3fYHq7K1SUpkOr42f8dr+kAPmmpoOelFDzWTlg== X-Received: by 2002:ac2:5f6f:0:b0:4fe:49d:6ae2 with SMTP id c15-20020ac25f6f000000b004fe049d6ae2mr5664622lfc.0.1696105919812; Sat, 30 Sep 2023 13:31:59 -0700 (PDT) Received: from localhost.localdomain ([185.9.78.108]) by smtp.gmail.com with ESMTPSA id x3-20020a19f603000000b00501c12fe522sm3981185lfe.73.2023.09.30.13.31.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 30 Sep 2023 13:31:59 -0700 (PDT) From: Mike Maslenkin To: Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, kwolf@redhat.com, hreitz@redhat.com, den@openvz.org, Mike Maslenkin Subject: [PATCH v2 1/1] qemu-img: do not erase destination file in qemu-img dd command Date: Sat, 30 Sep 2023 23:31:57 +0300 Message-Id: <20230930203157.85766-1-mike.maslenkin@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::12b; envelope-from=mike.maslenkin@gmail.com; helo=mail-lf1-x12b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add a check that destination file exists and do not call bdrv_create for this case. Currently `qemu-img dd` command destroys content of destination file. Effectively this means that parameters (geometry) of destination image file are changing. This can be undesirable behavior for user especially if format of destination image does not support resizing. Steps to reproduce: 1. Create empty disk image with some non default size. `qemu-img create -f qcow2 $DEST_IMG 3T` Remember that `qemu-img info $DEST_IMG` returns: virtual size: 3 TiB (3298534883328 bytes) disk size: 240 KiB cluster_size: 65536 2. Run `qemu-img dd -O qcow2 of=$DEST_IMG if=$SRC_IMG bs=1M count=100` 3. Check `qemu-img info $DEST_IMG` output: virtual size: 100 MiB (104857600 bytes) disk size: 112 MiB cluster_size: 65536 Parameters of $DEST_IMG were changed. Actually `qemu-img dd` has created a new disk based on current default geometry for particular format. For example for "parallels" format default BAT for 256GB disk is written to empty file prior writing disk image data. With this patch virtual disk metadata and geometry of a destination image are preserved. As another visible change of `qemu-img dd` behavior is that if destination image is less than source it can finish with error (similar to "dd" utility): qemu-img: error while writing to output image file: Input/output error Signed-off-by: Mike Maslenkin --- diff from v1: removed additional fprintf call leaved in patch by accident --- qemu-img.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index a48edb71015c..1a83c14212fb 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -5150,13 +5150,15 @@ static int img_dd(int argc, char **argv) size - in.bsz * in.offset, &error_abort); } - ret = bdrv_create(drv, out.filename, opts, &local_err); - if (ret < 0) { - error_reportf_err(local_err, - "%s: error while creating output image: ", - out.filename); - ret = -1; - goto out; + if (!g_file_test(out.filename, G_FILE_TEST_EXISTS)) { + ret = bdrv_create(drv, out.filename, opts, &local_err); + if (ret < 0) { + error_reportf_err(local_err, + "%s: error while creating output image: ", + out.filename); + ret = -1; + goto out; + } } /* TODO, we can't honour --image-opts for the target,