From nobody Mon Sep 16 20:02:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1695043784; cv=none; d=zohomail.com; s=zohoarc; b=jnBny+p9V2SOUEMgzzkV45BQhBA5I1NlLYUDJFyP8z+iHsxvJtQO05k3t5nIUY7q2o5cAP+7CfOIdyQmlWmMZSxsvCNUOqjNSAer4esxgggF0x9Dw47KnPeRwLCmCk8U/YlxtRTVQQG3f3rJpUhGoCKH5KLdY0KjEtiJE/wOak4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695043784; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rF+zreg9l/KObQ93Dgii8/CJqUx2/gfVTVG1knBTOW8=; b=gSBZMcWIxfLUvTebQTQ5eqHYSiFr7iW9qVTGnweuiI6oYTTa4xTsyTwZHXbMBIfvHfUPKoLCeYTa2qMgqFIU7FcSms9+DuvNt67Y6Vw/Fc0cEiigMvxpVkV/2JgjelMNZI5vEmgv3J/dI7QdzNdVLPiefBt966vlq7KkQL6X1Qg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1695043784306376.2928389217725; Mon, 18 Sep 2023 06:29:44 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-642-T3pSwYzqOY6gBmfXSxcmpA-1; Mon, 18 Sep 2023 09:29:41 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CEB04380671D; Mon, 18 Sep 2023 13:29:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id D249220268CB; Mon, 18 Sep 2023 13:29:35 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 652EA194658D; Mon, 18 Sep 2023 13:29:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id BE364194658C for ; Mon, 18 Sep 2023 13:29:34 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id B2CC2170EC; Mon, 18 Sep 2023 13:29:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.226.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54D571C5BB for ; Mon, 18 Sep 2023 13:29:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695043783; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=rF+zreg9l/KObQ93Dgii8/CJqUx2/gfVTVG1knBTOW8=; b=K9BUq4VmFCIpPle8ehdDmSYAXwSmMHhpIgWvnzlUIom6PCYW8jF614HPMriWcB5w5irYVU 62yroZiYgkkE/3HKb1jb2QLkg/nXoICvk0fhHp6BQ42tJQXeC4/s89A1ap0B1SsH+DT4Wc BjYRNyrtAAcHOv2OFgJ2H7Ag50V8dzY= X-MC-Unique: T3pSwYzqOY6gBmfXSxcmpA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH v3 02/10] qemu_saveimage: introduce helpers to decompress memory state file Date: Mon, 18 Sep 2023 15:29:19 +0200 Message-ID: <66bab038f8e738012056b192f976339388d5f20e.1695043512.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695043785359100001 Content-Type: text/plain; charset="utf-8"; x-default="true" These new helpers separates the code from the logic used to start new QEMU process with memory state and will make it easier to move qemuSaveImageStartProcess() into qemu_process.c file. Signed-off-by: Pavel Hrdina --- src/qemu/qemu_saveimage.c | 155 +++++++++++++++++++++++++++----------- src/qemu/qemu_saveimage.h | 15 ++++ 2 files changed, 128 insertions(+), 42 deletions(-) diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c index 27b95b03ae..95fabee907 100644 --- a/src/qemu/qemu_saveimage.c +++ b/src/qemu/qemu_saveimage.c @@ -247,6 +247,116 @@ qemuSaveImageGetCompressionCommand(virQEMUSaveFormat = compression) } =20 =20 +/** + * qemuSaveImageDecompressionStart: + * @data: data from memory state file + * @fd: pointer to FD of memory state file + * @intermediatefd: pointer to FD to store original @fd + * @errbuf: error buffer for @retcmd + * @retcmd: new virCommand pointer + * + * Start process to decompress VM memory state from @fd. If decompression + * is needed the original FD is stored to @intermediatefd and new FD after + * decompression is stored to @fd so caller can use the same variable + * in both cases. + * + * Function qemuSaveImageDecompressionStop() needs to be used to correctly + * stop the process and swap FD to the original state. + * + * Caller is responsible for freeing @retcmd. + * + * Returns -1 on error, 0 on success. + */ +int +qemuSaveImageDecompressionStart(virQEMUSaveData *data, + int *fd, + int *intermediatefd, + char **errbuf, + virCommand **retcmd) +{ + virQEMUSaveHeader *header =3D &data->header; + g_autoptr(virCommand) cmd =3D NULL; + + if (header->version !=3D 2) + return 0; + + if (header->compressed =3D=3D QEMU_SAVE_FORMAT_RAW) + return 0; + + if (!(cmd =3D qemuSaveImageGetCompressionCommand(header->compressed))) + return -1; + + *intermediatefd =3D *fd; + *fd =3D -1; + + virCommandSetInputFD(cmd, *intermediatefd); + virCommandSetOutputFD(cmd, fd); + virCommandSetErrorBuffer(cmd, errbuf); + virCommandDoAsyncIO(cmd); + + if (virCommandRunAsync(cmd, NULL) < 0) { + *fd =3D *intermediatefd; + *intermediatefd =3D -1; + return -1; + } + + *retcmd =3D g_steal_pointer(&cmd); + return 0; +} + + +/** + * qemuSaveImageDecompressionStop: + * @cmd: virCommand pointer + * @fd: pointer to FD of memory state file + * @intermediatefd: pointer to FD to store original @fd + * @errbuf: error buffer for @cmd + * @started: boolean to indicate if QEMU process was started + * @path: path to the memory state file + * + * Stop decompression process and close both @fd and @intermediatefd if + * necessary. + * + * Returns -1 on errro, 0 on success. + */ +int +qemuSaveImageDecompressionStop(virCommand *cmd, + int *fd, + int *intermediatefd, + char *errbuf, + bool started, + const char *path) +{ + int rc =3D 0; + virErrorPtr orig_err =3D NULL; + + if (*intermediatefd =3D=3D -1) + return rc; + + if (!started) { + /* if there was an error setting up qemu, the intermediate + * process will wait forever to write to stdout, so we + * must manually kill it and ignore any error related to + * the process + */ + virErrorPreserveLast(&orig_err); + VIR_FORCE_CLOSE(*intermediatefd); + VIR_FORCE_CLOSE(*fd); + } + + rc =3D virCommandWait(cmd, NULL); + VIR_DEBUG("Decompression binary stderr: %s", NULLSTR(errbuf)); + virErrorRestore(&orig_err); + + if (VIR_CLOSE(*fd) < 0) { + virReportSystemError(errno, _("cannot close file: %1$s"), path); + rc =3D -1; + } + + return rc; +} + + /* Helper function to execute a migration to file with a correct save head= er * the caller needs to make sure that the processors are stopped and do al= l other * actions besides saving memory */ @@ -595,7 +705,6 @@ qemuSaveImageStartProcess(virConnectPtr conn, { qemuDomainObjPrivate *priv =3D vm->privateData; g_autoptr(qemuDomainSaveCookie) cookie =3D NULL; - virQEMUSaveHeader *header =3D &data->header; VIR_AUTOCLOSE intermediatefd =3D -1; g_autoptr(virCommand) cmd =3D NULL; g_autofree char *errbuf =3D NULL; @@ -605,25 +714,8 @@ qemuSaveImageStartProcess(virConnectPtr conn, virDomainXMLOptionGetSaveCookie(driver->x= mlopt)) < 0) return -1; =20 - if ((header->version =3D=3D 2) && - (header->compressed !=3D QEMU_SAVE_FORMAT_RAW)) { - if (!(cmd =3D qemuSaveImageGetCompressionCommand(header->compresse= d))) - return -1; - - intermediatefd =3D *fd; - *fd =3D -1; - - virCommandSetInputFD(cmd, intermediatefd); - virCommandSetOutputFD(cmd, fd); - virCommandSetErrorBuffer(cmd, &errbuf); - virCommandDoAsyncIO(cmd); - - if (virCommandRunAsync(cmd, NULL) < 0) { - *fd =3D intermediatefd; - intermediatefd =3D -1; - return -1; - } - } + if (qemuSaveImageDecompressionStart(data, fd, &intermediatefd, &errbuf= , &cmd) < 0) + return -1; =20 /* No cookie means libvirt which saved the domain was too old to mess = up * the CPU definitions. @@ -641,28 +733,7 @@ qemuSaveImageStartProcess(virConnectPtr conn, start_flags) =3D=3D 0) *started =3D true; =20 - if (intermediatefd !=3D -1) { - virErrorPtr orig_err =3D NULL; - - if (!*started) { - /* if there was an error setting up qemu, the intermediate - * process will wait forever to write to stdout, so we - * must manually kill it and ignore any error related to - * the process - */ - virErrorPreserveLast(&orig_err); - VIR_FORCE_CLOSE(intermediatefd); - VIR_FORCE_CLOSE(*fd); - } - - rc =3D virCommandWait(cmd, NULL); - VIR_DEBUG("Decompression binary stderr: %s", NULLSTR(errbuf)); - virErrorRestore(&orig_err); - } - if (VIR_CLOSE(*fd) < 0) { - virReportSystemError(errno, _("cannot close file: %1$s"), path); - rc =3D -1; - } + rc =3D qemuSaveImageDecompressionStop(cmd, fd, &intermediatefd, errbuf= , *started, path); =20 virDomainAuditStart(vm, "restored", *started); if (!*started || rc < 0) diff --git a/src/qemu/qemu_saveimage.h b/src/qemu/qemu_saveimage.h index 24249ddf4c..dcee482066 100644 --- a/src/qemu/qemu_saveimage.h +++ b/src/qemu/qemu_saveimage.h @@ -99,6 +99,21 @@ qemuSaveImageGetCompressionProgram(const char *imageForm= at, bool use_raw_on_fail) ATTRIBUTE_NONNULL(2); =20 +int +qemuSaveImageDecompressionStart(virQEMUSaveData *data, + int *fd, + int *intermediatefd, + char **errbuf, + virCommand **retcmd); + +int +qemuSaveImageDecompressionStop(virCommand *cmd, + int *fd, + int *intermediatefd, + char *errbuf, + bool started, + const char *path); + int qemuSaveImageCreate(virQEMUDriver *driver, virDomainObj *vm, --=20 2.41.0