From patchwork Mon Nov 7 15:19:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roy Keene X-Patchwork-Id: 81107 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp1066829qge; Mon, 7 Nov 2016 07:23:05 -0800 (PST) X-Received: by 10.31.3.78 with SMTP id 75mr925944vkd.121.1478532185380; Mon, 07 Nov 2016 07:23:05 -0800 (PST) Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com. [209.132.183.25]) by mx.google.com with ESMTPS id w75si7582423vkh.78.2016.11.07.07.23.03 (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 07 Nov 2016 07:23:05 -0800 (PST) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id uA7FJTak023740; Mon, 7 Nov 2016 10:19:30 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id uA7FJSZB012147 for ; Mon, 7 Nov 2016 10:19:28 -0500 Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uA7FJS4W000526 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Mon, 7 Nov 2016 10:19:28 -0500 Received: from smtp86.iad3a.emailsrvr.com (smtp63.iad3a.emailsrvr.com [173.203.187.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CEE364E34C for ; Mon, 7 Nov 2016 15:19:26 +0000 (UTC) Received: from smtp19.relay.iad3a.emailsrvr.com (localhost [127.0.0.1]) by smtp19.relay.iad3a.emailsrvr.com (SMTP Server) with ESMTP id 2BE16C17E0; Mon, 7 Nov 2016 10:19:26 -0500 (EST) X-SMTPDoctor-Processed: csmtpprox beta Received: from smtp19.relay.iad3a.emailsrvr.com (localhost [127.0.0.1]) by smtp19.relay.iad3a.emailsrvr.com (SMTP Server) with ESMTP id 2826AC17B7; Mon, 7 Nov 2016 10:19:26 -0500 (EST) X-Auth-ID: rkeene@knightpoint.com Received: by smtp19.relay.iad3a.emailsrvr.com (Authenticated sender: rkeene-AT-knightpoint.com) with ESMTPSA id C2C70C17E0; Mon, 7 Nov 2016 10:19:25 -0500 (EST) X-Sender-Id: rkeene@knightpoint.com Received: from [10.193.13.51] (99-91-65-205.lightspeed.nworla.sbcglobal.net [99.91.65.205]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA) by 0.0.0.0:587 (trex/5.7.7); Mon, 07 Nov 2016 10:19:26 -0500 To: Peter Krempa References: <6f39beb8-cd59-deb3-bd0a-753bf813185e@knightpoint.com> <20161107092726.GZ2382981@andariel.pipo.sk> From: Roy Keene Message-ID: <33fc9eed-3b31-bb7d-aa35-595cd3ae04d6@knightpoint.com> Date: Mon, 7 Nov 2016 09:19:23 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20161107092726.GZ2382981@andariel.pipo.sk> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 191 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 07 Nov 2016 15:19:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 07 Nov 2016 15:19:27 +0000 (UTC) for IP:'173.203.187.63' DOMAIN:'smtp63.iad3a.emailsrvr.com' HELO:'smtp86.iad3a.emailsrvr.com' FROM:'rkeene@knightpoint.com' RCPT:'' X-RedHat-Spam-Score: -1.348 (BAYES_50, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SORBS_SPAM) 173.203.187.63 smtp63.iad3a.emailsrvr.com 173.203.187.63 smtp63.iad3a.emailsrvr.com X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-loop: libvir-list@redhat.com Cc: libvir-list@redhat.com Subject: Re: [libvirt] [PATCH] Allow saving QEMU libvirt state to a pipe X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com On 11/07/2016 03:27 AM, Peter Krempa wrote: > On Fri, Nov 04, 2016 at 15:20:44 -0500, Roy Keene wrote: >> All, >> >> Currently the "virsh save" command opens a file to save a domain's >> XML and memory state, does the save, and then re-opens the file to >> simulate seeking to the beginning to update the header to indicate that >> the file is complete. >> >> For pipes this is not possible, attempting to re-open the pipe will not >> connect you to the same consumer. Seeking is also not possible on a pipe. >> >> Attached is a patch that detects if the file being written to is not >> seekable, and if so writes the completed header initially, then writes >> the normal data, and avoids re-opening the file. >> >> This is useful to me for saving a VM state directly to Ceph RBD images >> without having an intermediate file. >> >> I've tested this patch successfully (against v1.3.5) for both saving and >> restoring a domain: >> >> # *( fifo="$(mktemp -u)"; mkfifo "${fifo}" && virsh save one-0 "${fifo}" >> & cat "${fifo}" | rbd import - rbd/test1234 & wait; rm -f "${fifo}" )* >> >> Domain one-0 saved to /tmp/tmp.HK4hChiQqB >> >> # >> >> # *( fifo="$(mktemp -u)"; mkfifo "${fifo}" && rbd export rbd/test1234 - >> > "${fifo}" & virsh restore "${fifo}" & wait; rm -f "${fifo}" ) * >> Exporting image: 100% complete...done. >> Domain restored from /tmp/tmp.0YaUZ5Y2yT >> >> # *virsh list* >> Id Name State >> ---------------------------------------------------- >> 11 one-0 running >> # >> >> -- >> Roy Keene >> Knight Point Systems, LLC >> Service-Disabled Veteran-Owned Business >> 1775 Wiehle Avenue Suite 101 | Reston, VA 20190 >> c: 813-956-3808 f: 571-266-3106 >> www.knightpoint.com >> DHS EAGLE II Prime Contractor: FC1 SDVOSB Track >> GSA Schedule 70 SDVOSB: GS-35F-0646S >> GSA MOBIS Schedule: GS-10F-0404Y >> ISO 20000 / ISO 27001 >> >> Notice: This e-mail message, including any attachments, is for the >> sole use of the intended recipient(s) and may contain confidential >> and privileged information. Any unauthorized review, copy, use, >> disclosure, or distribution is STRICTLY prohibited. If you are not >> the intended recipient, please contact the sender by reply e-mail >> and destroy all copies of the original message. > Trimming this on public postings might be a good idea. This is currently required by my company's policy. I'll work with them to try to come up with exceptions to this requirement. > >> diff --no-dereference -uNr libvirt-1.3.5.orig/src/qemu/qemu_driver.c libvirt-1.3.5-savepipe/src/qemu/qemu_driver.c >> --- libvirt-1.3.5.orig/src/qemu/qemu_driver.c 2016-05-28 11:47:33.000000000 -0500 >> +++ libvirt-1.3.5-savepipe/src/qemu/qemu_driver.c 2016-11-04 10:54:15.160805261 -0500 > This is a very old version of libvirt. It's 5 months old... > Please rebase the changes against > the git master since they can't be applied: > > Applying: Allow saving QEMU libvirt state to a pipe > error: patch failed: src/qemu/qemu_driver.c:3124 > error: src/qemu/qemu_driver.c: patch does not apply > Patch failed at 0001 Allow saving QEMU libvirt state to a pipe > > Additionally please generate the patches using git so that we have a > proper commit message. [rkeene@kps-rsk-laptop libvirt]$ patch -p1 < ~/devel/aurae/node/root/packages/libvirt/patches/libvirt-1.3.5-savepipe.diff patching file src/qemu/qemu_driver.c Hunk #1 succeeded at 3051 (offset -29 lines). Hunk #2 succeeded at 3059 (offset -29 lines). Hunk #3 succeeded at 3080 (offset -29 lines). Hunk #4 succeeded at 3114 with fuzz 1 (offset -30 lines). [rkeene@kps-rsk-laptop libvirt]$ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: src/qemu/qemu_driver.c no changes added to commit (use "git add" and/or "git commit -a") [rkeene@kps-rsk-laptop libvirt]$ Attach is the output of "git diff" (2savepipe) >> @@ -3080,6 +3080,7 @@ >> virQEMUSaveHeader header; >> bool bypassSecurityDriver = false; >> bool needUnlink = false; >> + bool canReopen = true; >> int ret = -1; >> int fd = -1; >> int directFlag = 0; >> @@ -3087,7 +3088,6 @@ >> unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING; >> >> memset(&header, 0, sizeof(header)); >> - memcpy(header.magic, QEMU_SAVE_PARTIAL, sizeof(header.magic)); >> header.version = QEMU_SAVE_VERSION; >> header.was_running = was_running ? 1 : 0; >> header.compressed = compressed; >> @@ -3109,12 +3109,32 @@ >> if (fd < 0) >> goto cleanup; >> >> + /* >> + * Determine if this file can be re-opened >> + * >> + * Right now we just try to seek into it, if that fails then that means we >> + * are probably not dealing with a regular file here and we probably >> + * cannot reopen it. >> + */ > Another option would be to pass a flag to the API, where the user would > declare by using such flag that the output is valid only on success of > the command and thus rewriting the header is not necessary. I thought about that, but it was more work to pass that flag around... initially I just wanted to see if this was the only problem with streaming the save. > >> + if (lseek(fd, 0, SEEK_SET) == ((off_t) -1)) { >> + canReopen = false; >> + } > This will fail make syntax-check on current master since single line if > statement bodies should not be put into braces. I'll add a comment inside the braces to make it pass the syntax-check. I'm against unbraced expressions. > >> + >> if (virSecurityManagerSetImageFDLabel(driver->securityManager, vm->def, fd) < 0) >> goto cleanup; >> >> if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags))) >> goto cleanup; >> >> + /* Set the header magic */ >> + if (canReopen) { >> + /* We will update the magic after the saving completes successfully */ >> + memcpy(header.magic, QEMU_SAVE_PARTIAL, sizeof(header.magic)); >> + } else { >> + /* If we cannot re-open the output, include the final magic here */ >> + memcpy(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic)); > This is okay though, comments are counted as a line too. > >> + } >> + >> /* Write header to file, followed by XML */ >> if (qemuDomainSaveHeader(fd, path, domXML, &header) < 0) >> goto cleanup; > The rest looks reasonable. I'd slightly more prefer the use of the flag > in comparison to auto detection. Attached is a competing patch for the flag, I agree it's better (3savepipe). It is untested. > Thanks for your patch thoug. > > Peter -- Roy Keene Knight Point Systems, LLC Service-Disabled Veteran-Owned Business 1775 Wiehle Avenue Suite 101 | Reston, VA 20190 c: 813-956-3808 f: 571-266-3106 www.knightpoint.com DHS EAGLE II Prime Contractor: FC1 SDVOSB Track GSA Schedule 70 SDVOSB: GS-35F-0646S GSA MOBIS Schedule: GS-10F-0404Y ISO 20000 / ISO 27001 Notice: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, copy, use, disclosure, or distribution is STRICTLY prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message. -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 5f50660..b17615b 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -1058,6 +1058,7 @@ typedef enum { VIR_DOMAIN_SAVE_BYPASS_CACHE = 1 << 0, /* Avoid file system cache pollution */ VIR_DOMAIN_SAVE_RUNNING = 1 << 1, /* Favor running over paused */ VIR_DOMAIN_SAVE_PAUSED = 1 << 2, /* Favor paused over running */ + VIR_DOMAIN_SAVE_PIPE = 1 << 3, /* Output is a pipe */ } virDomainSaveRestoreFlags; int virDomainSave (virDomainPtr domain, diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 38c8414..261fab1 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -3051,6 +3051,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver, virQEMUSaveHeader header; bool bypassSecurityDriver = false; bool needUnlink = false; + bool canReopen = true; int ret = -1; int fd = -1; int directFlag = 0; @@ -3058,7 +3059,6 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver, unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING; memset(&header, 0, sizeof(header)); - memcpy(header.magic, QEMU_SAVE_PARTIAL, sizeof(header.magic)); header.version = QEMU_SAVE_VERSION; header.was_running = was_running ? 1 : 0; header.compressed = compressed; @@ -3080,12 +3080,31 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver, if (fd < 0) goto cleanup; + /* + * Determine if the output is a pipe + */ + if ((flags & VIR_DOMAIN_SAVE_PIPE)) { + /* + * If the output is a pipe, we cannot re-open it + */ + canReopen = false; + } + if (virSecurityManagerSetImageFDLabel(driver->securityManager, vm->def, fd) < 0) goto cleanup; if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags))) goto cleanup; + /* Set the header magic */ + if (canReopen) { + /* We will update the magic after the saving completes successfully */ + memcpy(header.magic, QEMU_SAVE_PARTIAL, sizeof(header.magic)); + } else { + /* If we cannot re-open the output, include the final magic here */ + memcpy(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic)); + } + /* Write header to file, followed by XML */ if (qemuDomainSaveHeader(fd, path, domXML, &header) < 0) goto cleanup; @@ -3094,28 +3113,30 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver, if (qemuMigrationToFile(driver, vm, fd, compressedpath, asyncJob) < 0) goto cleanup; - /* Touch up file header to mark image complete. */ + if (canReopen) { + /* Touch up file header to mark image complete. */ - /* Reopen the file to touch up the header, since we aren't set - * up to seek backwards on wrapperFd. The reopened fd will - * trigger a single page of file system cache pollution, but - * that's acceptable. */ - if (VIR_CLOSE(fd) < 0) { - virReportSystemError(errno, _("unable to close %s"), path); - goto cleanup; - } + /* Reopen the file to touch up the header, since we aren't set + * up to seek backwards on wrapperFd. The reopened fd will + * trigger a single page of file system cache pollution, but + * that's acceptable. */ + if (VIR_CLOSE(fd) < 0) { + virReportSystemError(errno, _("unable to close %s"), path); + goto cleanup; + } - if (virFileWrapperFdClose(wrapperFd) < 0) - goto cleanup; + if (virFileWrapperFdClose(wrapperFd) < 0) + goto cleanup; - if ((fd = qemuOpenFile(driver, vm, path, O_WRONLY, NULL, NULL)) < 0) - goto cleanup; + if ((fd = qemuOpenFile(driver, vm, path, O_WRONLY, NULL, NULL)) < 0) + goto cleanup; - memcpy(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic)); + memcpy(header.magic, QEMU_SAVE_MAGIC, sizeof(header.magic)); - if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) { - virReportSystemError(errno, _("unable to write %s"), path); - goto cleanup; + if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) { + virReportSystemError(errno, _("unable to write %s"), path); + goto cleanup; + } } if (VIR_CLOSE(fd) < 0) { diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 184f64d..32f188a 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4169,6 +4169,10 @@ static const vshCmdOptDef opts_save[] = { .type = VSH_OT_BOOL, .help = N_("set domain to be paused on restore") }, + {.name = "pipe", + .type = VSH_OT_BOOL, + .help = N_("the file being saved to is a pipe") + }, {.name = "verbose", .type = VSH_OT_BOOL, .help = N_("display the progress of save") @@ -4205,6 +4209,8 @@ doSave(void *opaque) flags |= VIR_DOMAIN_SAVE_RUNNING; if (vshCommandOptBool(cmd, "paused")) flags |= VIR_DOMAIN_SAVE_PAUSED; + if (vshCommandOptBool(cmd, "pipe")) + flags |= VIR_DOMAIN_SAVE_PIPE; if (vshCommandOptStringReq(ctl, cmd, "xml", &xmlfile) < 0) goto out;