From nobody Thu Dec 18 13:58:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1743773426; cv=none; d=zohomail.com; s=zohoarc; b=b1P+oOp9fxVabIPlYkMLnxVlkI0FSu2xwH3LOXCvMc14izXRG7UYvN6GTDsTrfdY6E3/4C4L9P1t/MS3utFBNYVtjV6ibaETnAnei3oJJx2higD9bqCcNTO+EO/CDPZsEenGzk//idm4b3b4O78ptBJQ8bHeuprNkPDuDD+2Pqw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1743773426; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=aB9pRe95ZPm/ijY4byVfDE+VGG45tmX20PbF4UtmnGc=; b=m2PfTBV0jOZs1fRhiQd/w/jEBO9UAXlsKarlVIHad1pFgy86X80xIw/diAcpn8CwL9uQ+ZJadoaSM7nFyxdOSdmhsA2+smLSaKT406BfDGJ6gv20M/nhWCGRRnmI580iojh60RYKwvT2QAY+5jcABo+iRhTE5hGa84NhMjZFpxE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1743773426868631.2617709380285; Fri, 4 Apr 2025 06:30:26 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 31D1D165B; Fri, 4 Apr 2025 09:30:26 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 4A54F15ED; Fri, 4 Apr 2025 09:28:19 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 6E1F214E0; Fri, 4 Apr 2025 09:28:11 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 8F46911AA for ; Fri, 4 Apr 2025 09:28:10 -0400 (EDT) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-138-4_vGefk_OayOdiKJH6Mw8Q-1; Fri, 04 Apr 2025 09:28:09 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3DC3D18001DE for ; Fri, 4 Apr 2025 13:28:08 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.3.236]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 86FFE1955BC2 for ; Fri, 4 Apr 2025 13:28:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743773290; h=from:from: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; bh=8nny36b9rSjYJCjVV/TDWYLF7hHboJEoRHNnNmM5QHo=; b=VYXnmdLOP/9j5Kok9b4BRaJAnRqvhlP1KuhSYb44gXZDsR9dfJaNZ9ypgMcnYV43afr0JW IGGoxplMmZfQHU5cfMi+/dFZ9RvhUVy5E4HYZ+V0Eao21iV67qn/m7Hav+7EuRtiTN9clj dStot8jFkjx/eF0qlfaO4jxy75bXqac= X-MC-Unique: 4_vGefk_OayOdiKJH6Mw8Q-1 X-Mimecast-MFC-AGG-ID: 4_vGefk_OayOdiKJH6Mw8Q_1743773288 To: devel@lists.libvirt.org Subject: [PATCH 4/8] vbox: Move parts of vboxSnapshotRedefine() into a separate function Date: Fri, 4 Apr 2025 15:27:56 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: AMs7E2oiJu441l-lX9lVzyFWIWRMTyspqc7VwQUAz8g_1743773288 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: PRKDOEL7SB7LQPL5XT7U5GYK6PQLUQ7U X-Message-ID-Hash: PRKDOEL7SB7LQPL5XT7U5GYK6PQLUQ7U X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1743773428630019000 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik There's too much happening inside of vboxSnapshotRedefine(). Not only it makes the function hard to read, but it also increases stack size of the function. Move one part into a separate function: vboxSnapshotReplaceRWDisks() Signed-off-by: Michal Privoznik --- src/vbox/vbox_common.c | 216 ++++++++++++++++++++++------------------- 1 file changed, 118 insertions(+), 98 deletions(-) diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index 7d157e065e..b52e0c3e6a 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -4553,6 +4553,122 @@ static int vboxCloseDisksRecursively(virDomainPtr d= om, char *location) return ret; } =20 + +static int +vboxSnapshotReplaceRWDisks(struct _vboxDriver *data, + virVBoxSnapshotConfMachine *snapshotMachineDesc, + char *currentSnapshotXmlFilePath) +{ + g_auto(GStrv) realReadWriteDisksPath =3D NULL; + g_auto(GStrv) realReadOnlyDisksPath =3D NULL; + int realReadWriteDisksPathSize =3D 0; + int realReadOnlyDisksPathSize =3D 0; + int it =3D 0; + + /* + * We have created fake disks, so we have to remove them and replace t= hem with + * the read-write disks if there are any. The fake disks will be close= d during + * the machine unregistration. + */ + if (virVBoxSnapshotConfRemoveFakeDisks(snapshotMachineDesc) < 0) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to remove Fake Disks")); + return -1; + } + + realReadWriteDisksPathSize =3D virVBoxSnapshotConfGetRWDisksPathsFromL= ibvirtXML(currentSnapshotXmlFilePath, + = &realReadWriteDisksPath); + realReadOnlyDisksPathSize =3D virVBoxSnapshotConfGetRODisksPathsFromLi= bvirtXML(currentSnapshotXmlFilePath, + = &realReadOnlyDisksPath); + /* The read-only disk number is necessarily greater or equal to the + * read-write disk number */ + if (realReadOnlyDisksPathSize < realReadWriteDisksPathSize) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("The read only disk number must be greater or eq= ual to the read write disk number")); + return -1; + } + + for (it =3D 0; it < realReadWriteDisksPathSize; it++) { + virVBoxSnapshotConfHardDisk *readWriteDisk =3D NULL; + PRUnichar *locationUtf =3D NULL; + IMedium *readWriteMedium =3D NULL; + char *uuid =3D NULL; + PRUnichar *formatUtf =3D NULL; + char *format =3D NULL; + const char *parentUuid =3D NULL; + vboxIID iid; + nsresult rc; + + VBOX_IID_INITIALIZE(&iid); + VBOX_UTF8_TO_UTF16(realReadWriteDisksPath[it], &locationUtf); + rc =3D gVBoxAPI.UIVirtualBox.OpenMedium(data->vboxObj, + locationUtf, + DeviceType_HardDisk, + AccessMode_ReadWrite, + &readWriteMedium); + if (NS_FAILED(rc)) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to open HardDisk")); + VBOX_UTF16_FREE(locationUtf); + return -1; + } + VBOX_UTF16_FREE(locationUtf); + + rc =3D gVBoxAPI.UIMedium.GetId(readWriteMedium, &iid); + if (NS_FAILED(rc)) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the read write medium id")); + return -1; + } + gVBoxAPI.UIID.vboxIIDToUtf8(data, &iid, &uuid); + vboxIIDUnalloc(&iid); + + rc =3D gVBoxAPI.UIMedium.GetFormat(readWriteMedium, &formatUtf); + if (NS_FAILED(rc)) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get the read write medium format"= )); + return -1; + } + VBOX_UTF16_TO_UTF8(formatUtf, &format); + VBOX_UTF16_FREE(formatUtf); + + readWriteDisk =3D g_new0(virVBoxSnapshotConfHardDisk, 1); + + readWriteDisk->format =3D format; + readWriteDisk->uuid =3D uuid; + readWriteDisk->location =3D realReadWriteDisksPath[it]; + /* + * We get the current snapshot's read-only disk uuid in order to a= dd the + * read-write disk to the media registry as its child. The read-on= ly disk + * is already in the media registry because it is the fake disk's = parent. + */ + parentUuid =3D virVBoxSnapshotConfHardDiskUuidByLocation(snapshotM= achineDesc, + realReadOnl= yDisksPath[it]); + if (parentUuid =3D=3D NULL) { + VIR_FREE(readWriteDisk); + return -1; + } + + if (virVBoxSnapshotConfAddHardDiskToMediaRegistry(readWriteDisk, + snapshotMachineD= esc->mediaRegistry, + parentUuid) < 0)= { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to add hard disk to media Registry")= ); + VIR_FREE(readWriteDisk); + return -1; + } + rc =3D gVBoxAPI.UIMedium.Close(readWriteMedium); + if (NS_FAILED(rc)) { + vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to close HardDisk")); + return -1; + } + } + + return 0; +} + + static int vboxSnapshotRedefine(virDomainPtr dom, virDomainSnapshotDef *def, @@ -4596,10 +4712,6 @@ vboxSnapshotRedefine(virDomainPtr dom, char *currentSnapshotXmlFilePath =3D NULL; PRUnichar *machineNameUtf16 =3D NULL; char *machineName =3D NULL; - g_auto(GStrv) realReadWriteDisksPath =3D NULL; - int realReadWriteDisksPathSize =3D 0; - g_auto(GStrv) realReadOnlyDisksPath =3D NULL; - int realReadOnlyDisksPathSize =3D 0; virVBoxSnapshotConfSnapshot *newSnapshotPtr =3D NULL; unsigned char snapshotUuid[VIR_UUID_BUFLEN]; virVBoxSnapshotConfHardDisk **hardDiskToOpen =3D NULL; @@ -4668,102 +4780,10 @@ vboxSnapshotRedefine(virDomainPtr dom, } =20 if (snapshotFileExists) { - /* - * We have created fake disks, so we have to remove them and repla= ce them with - * the read-write disks if there are any. The fake disks will be c= losed during - * the machine unregistration. - */ - if (virVBoxSnapshotConfRemoveFakeDisks(snapshotMachineDesc) < 0) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to remove Fake Disks")); + if (vboxSnapshotReplaceRWDisks(data, snapshotMachineDesc, + currentSnapshotXmlFilePath) < 0) goto cleanup; - } - realReadWriteDisksPathSize =3D virVBoxSnapshotConfGetRWDisksPathsF= romLibvirtXML(currentSnapshotXmlFilePath, - &realReadWrit= eDisksPath); - realReadOnlyDisksPathSize =3D virVBoxSnapshotConfGetRODisksPathsFr= omLibvirtXML(currentSnapshotXmlFilePath, - &= realReadOnlyDisksPath); - /* The read-only disk number is necessarily greater or equal to the - * read-write disk number */ - if (realReadOnlyDisksPathSize < realReadWriteDisksPathSize) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("The read only disk number must be greater o= r equal to the read write disk number")); - goto cleanup; - } - for (it =3D 0; it < realReadWriteDisksPathSize; it++) { - virVBoxSnapshotConfHardDisk *readWriteDisk =3D NULL; - PRUnichar *locationUtf =3D NULL; - IMedium *readWriteMedium =3D NULL; - char *uuid =3D NULL; - PRUnichar *formatUtf =3D NULL; - char *format =3D NULL; - const char *parentUuid =3D NULL; - vboxIID iid; - - VBOX_IID_INITIALIZE(&iid); - VBOX_UTF8_TO_UTF16(realReadWriteDisksPath[it], &locationUtf); - rc =3D gVBoxAPI.UIVirtualBox.OpenMedium(data->vboxObj, - locationUtf, - DeviceType_HardDisk, - AccessMode_ReadWrite, - &readWriteMedium); - if (NS_FAILED(rc)) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to open HardDisk")); - VBOX_UTF16_FREE(locationUtf); - goto cleanup; - } - VBOX_UTF16_FREE(locationUtf); - - rc =3D gVBoxAPI.UIMedium.GetId(readWriteMedium, &iid); - if (NS_FAILED(rc)) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to get the read write medium id"= )); - goto cleanup; - } - gVBoxAPI.UIID.vboxIIDToUtf8(data, &iid, &uuid); - vboxIIDUnalloc(&iid); - - rc =3D gVBoxAPI.UIMedium.GetFormat(readWriteMedium, &formatUtf= ); - if (NS_FAILED(rc)) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to get the read write medium for= mat")); - goto cleanup; - } - VBOX_UTF16_TO_UTF8(formatUtf, &format); - VBOX_UTF16_FREE(formatUtf); - - readWriteDisk =3D g_new0(virVBoxSnapshotConfHardDisk, 1); - - readWriteDisk->format =3D format; - readWriteDisk->uuid =3D uuid; - readWriteDisk->location =3D realReadWriteDisksPath[it]; - /* - * We get the current snapshot's read-only disk uuid in order = to add the - * read-write disk to the media registry as its child. The rea= d-only disk - * is already in the media registry because it is the fake dis= k's parent. - */ - parentUuid =3D virVBoxSnapshotConfHardDiskUuidByLocation(snaps= hotMachineDesc, - realRea= dOnlyDisksPath[it]); - if (parentUuid =3D=3D NULL) { - VIR_FREE(readWriteDisk); - goto cleanup; - } =20 - if (virVBoxSnapshotConfAddHardDiskToMediaRegistry(readWriteDis= k, - snapshotMachineDesc->mediaRegis= try, - parentUuid) < 0) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to add hard disk to media Regist= ry")); - VIR_FREE(readWriteDisk); - goto cleanup; - } - rc =3D gVBoxAPI.UIMedium.Close(readWriteMedium); - if (NS_FAILED(rc)) { - vboxReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to close HardDisk")); - goto cleanup; - } - } /* * Now we have done this swap, we remove the snapshot xml file fro= m the * current machine location. --=20 2.49.0