Skip to contentSkip to conversation

migration: adding migration to/from a file

PATCH In review Uri Lublin 5 participants 11 messages Jan 19, 2009 – Jan 20, 2009 Kbuild

lore
Share & tools
lore

Uri Lublin

author
#0 lore raw

PATCH 1/1 migration: adding migration to/from a file

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---
 Makefile         |    2 +-
 migration-file.c |  127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 migration.c      |    4 ++
 migration.h      |    5 ++
 4 files changed, 137 insertions(+), 1 deletions(-)
 create mode 100644 migration-file.c
Makefile +1 -1
index 8cbdcda..7e0e6f2 100644
@@ -81,7 +81,7 @@ OBJS+=usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o
 OBJS+=usb-serial.o usb-net.o
 OBJS+=sd.o ssi-sd.o
 OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
-OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
+OBJS+=buffered_file.o migration.o migration-tcp.o migration-file.o net.o qemu-sockets.o
 OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o
 
 ifdef CONFIG_BRLAPI
migration-file.c new file +127 -0

127 lines not on this page · raw message

migration.c +4 -0
index 0ef777a..0dde557 100644
@@ -44,6 +44,8 @@ void qemu_start_incoming_migration(const char *uri)
     else if (strstart(uri, "exec:", &p))
         exec_start_incoming_migration(p);
 #endif
+    else if (strstart(uri, "file:", &p))
+        file_start_incoming_migration(p);
     else
         fprintf(stderr, "unknown migration protocol: %s\n", uri);
 }
@@ -59,6 +61,8 @@ void do_migrate(int detach, const char *uri)
     else if (strstart(uri, "exec:", &p))
         s = exec_start_outgoing_migration(p, max_throttle, detach);
 #endif
+    else if (strstart(uri, "file:", &p))
+        s = file_start_outgoing_migration(p, (1<<31), detach);
     else
         term_printf("unknown migration protocol: %s\n", uri);
 
migration.h +5 -0
index d9771ad..30d4ab4 100644
@@ -67,6 +67,11 @@ MigrationState *tcp_start_outgoing_migration(const char *host_port,
 					     int64_t bandwidth_limit,
 					     int detach);
 
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int detach);
+int file_start_incoming_migration(const char *filename);
+
 void migrate_fd_error(FdMigrationState *s);
 
 void migrate_fd_cleanup(FdMigrationState *s);
Signature · 1 line
1.6.0.6
#1 lore raw
Quoted 7 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Quoted 6 lines

Also there is not point of doing a _live_ migration to file (except for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

Wouldn't it be better to just implement savevm to file?

Paul

#4 lore raw
In reply to Uri Lublin
Quoted 99 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except
for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---
 Makefile         |    2 +-
 migration-file.c |  127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 migration.c      |    4 ++
 migration.h      |    5 ++
 4 files changed, 137 insertions(+), 1 deletions(-)
 create mode 100644 migration-file.c
Makefile +1 -1
index 8cbdcda..7e0e6f2 100644
@@ -81,7 +81,7 @@ OBJS+=usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o
 OBJS+=usb-serial.o usb-net.o
 OBJS+=sd.o ssi-sd.o
 OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
-OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
+OBJS+=buffered_file.o migration.o migration-tcp.o migration-file.o net.o qemu-sockets.o
 OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o
 
 ifdef CONFIG_BRLAPI
migration-file.c new file +57 -0
new file mode 100644
index 0000000..5cde3e2
@@ -0,0 +1,127 @@
+/*
+ * QEMU live migration
+ *
+ * Copyright IBM, Corp. 2008
+ *           Red Hat, Inc. 2008
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Uri Lublin        <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <unistd.h>
+#include "qemu-common.h"
+#include "migration.h"
+#include "sysemu.h"
+#include "console.h"
+#include "block.h"
+
+//#define DEBUG_MIGRATION_FILE
+
+#ifdef DEBUG_MIGRATION_FILE
+#define dprintf(fmt, ...) \
+    do { printf("migration-file: " fmt, ## __VA_ARGS__); } while (0)
+#else
+#define dprintf(fmt, ...) \
+    do { } while (0)
+#endif
+
+static int file_close(FdMigrationState *s)
+{
+    close(s->fd);
+}
+
+static int file_errno(FdMigrationState *s)
+{
+    return errno;
+}
+
+static int file_write(FdMigrationState *s, const void * buf, size_t size)
+{
+    return write(s->fd, buf, size);
+}
+
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int async)
+{
+    FdMigrationState *s;
+    int fd;
+
+    s = qemu_mallocz(sizeof(*s));
+    if (s == NULL) {
+        perror("file_migration: qemu_mallocz failed");

migration-file.c line 67

Quoted 10 lines
migration-file.c new file +10 -0
+        term_printf("file_migration: qemu_mallocz failed");
+        goto err1;
+    }
+
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+    if (fd < 0) {
+        perror("file_migration: failed to open filename");
+        term_printf("file_migration: failed to open filename %s\n", filename);
+        goto err2;
+    }

The migration code assumes that the file descriptor used is non-blocking. In general, open() on a file system cannot produce a non-blocking file descriptor.

You could either use the posix-aio code to implement migration to a file or you could introduce a fork()'d process that wrote to a file from stdin. Although this is basically just exec dd of=.

It shouldn't be too hard to use posix-aio though. You just have to be clever about implementing the s->write op.

Regards,

Anthony Liguori

author
#2 lore raw
In reply to Paul Brook
Quoted 12 lines · Paul Brook
Quoted 11 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

Quoted 6 lines
Quoted 2 lines

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

Wouldn't it be better to just implement savevm to file?

Paul

It sure is simpler.

The possible advantage of migration-to-file (over savevm-to-file) is that in the future we may want to implement vm-snapshoting (live save to file + snapshot disk-image).

I've implemented a savevm_file, and will send it in a separate email.

Thanks,
Uri.

#5 lore raw
In reply to Anthony Liguori
Quoted 112 lines · Anthony Liguori
Quoted 111 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---
Makefile | 2 +-
migration-file.c | 127
++++++++++++++++++++++++++++++++++++++++++++++++++++++

migration.c      |    4 ++
migration.h      |    5 ++
4 files changed, 137 insertions(+), 1 deletions(-)

create mode 100644 migration-file.c

Makefile +0 -0
index 8cbdcda..7e0e6f2 100644
@@ -81,7 +81,7 @@ OBJS+=usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o 

usb-msd.o usb-wacom.o
OBJS+=usb-serial.o usb-net.o
OBJS+=sd.o ssi-sd.o
OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o

Makefile +1 -1
-OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
+OBJS+=buffered_file.o migration.o migration-tcp.o migration-file.o net.o 

qemu-sockets.o
OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o

ifdef CONFIG_BRLAPI

migration-file.c new file +65 -0
new file mode 100644
index 0000000..5cde3e2
@@ -0,0 +1,127 @@
+/*
+ * QEMU live migration
+ *
+ * Copyright IBM, Corp. 2008
+ *           Red Hat, Inc. 2008
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Uri Lublin        <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <unistd.h>
+#include "qemu-common.h"
+#include "migration.h"
+#include "sysemu.h"
+#include "console.h"
+#include "block.h"
+
+//#define DEBUG_MIGRATION_FILE
+
+#ifdef DEBUG_MIGRATION_FILE
+#define dprintf(fmt, ...) \
+    do { printf("migration-file: " fmt, ## __VA_ARGS__); } while (0)
+#else
+#define dprintf(fmt, ...) \
+    do { } while (0)
+#endif
+
+static int file_close(FdMigrationState *s)
+{
+    close(s->fd);
+}
+
+static int file_errno(FdMigrationState *s)
+{
+    return errno;
+}
+
+static int file_write(FdMigrationState *s, const void * buf, size_t size)
+{
+    return write(s->fd, buf, size);
+}
+
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int async)
+{
+    FdMigrationState *s;
+    int fd;
+
+    s = qemu_mallocz(sizeof(*s));
+    if (s == NULL) {
+        perror("file_migration: qemu_mallocz failed");
+        term_printf("file_migration: qemu_mallocz failed");
+        goto err1;
+    }
+
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+    if (fd < 0) {
+        perror("file_migration: failed to open filename");
+        term_printf("file_migration: failed to open filename %s\n", 
Quoted 8 lines

migration-file.c

Quoted 4 lines

filename);

migration-file.c new file +2 -0
+        goto err2;
+    }

The migration code assumes that the file descriptor used is non-blocking. In general, open() on a file system cannot produce a non-blocking file descriptor.

Does it matter if it blocks though ? Migrating to a file is not "live" migration anyway, so if we happen to block the rest of the QEMU event loop during course of writing to the file it doesn't seem too serious. Or is there some scenario which badly breaks with blocking writes even for non-live usage ?

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
author
#8 lore raw
In reply to Anthony Liguori
Quoted 34 lines · Anthony Liguori
Quoted 33 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except
for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---

migration-file.c new file +17 -0
+
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int async)
+{
+    FdMigrationState *s;
+    int fd;
+
+    s = qemu_mallocz(sizeof(*s));
+    if (s == NULL) {
+        perror("file_migration: qemu_mallocz failed");
+        term_printf("file_migration: qemu_mallocz failed");
+        goto err1;
+    }
+
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+    if (fd < 0) {
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +2 -0
+        perror("file_migration: failed to open filename");
+        term_printf("file_migration: failed to open filename %s\n", 

filename);

(quoted diff) +2 -0
+        goto err2;
+    }

The migration code assumes that the file descriptor used is non-blocking. In general, open() on a file system cannot produce a non-blocking file descriptor.

I can call fcntl with F_SETFL and O_NONBLOCK.

Quoted 2 lines

You could either use the posix-aio code to implement migration to a file or you could introduce a fork()'d process that wrote to a file from stdin. Although this is basically just exec dd of=.

We started with "exec dd..." for saving state in a file. It's obviously not the best solution, as it needs to fork/exec/pipe instead of just open a file.

Thanks,
Uri.

#9 lore raw
Quoted 47 lines · Uri Lublin
Quoted 37 lines · Anthony Liguori
Quoted 36 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---

migration-file.c new file +19 -0
+
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int async)
+{
+    FdMigrationState *s;
+    int fd;
+
+    s = qemu_mallocz(sizeof(*s));
+    if (s == NULL) {
+        perror("file_migration: qemu_mallocz failed");
+        term_printf("file_migration: qemu_mallocz failed");
+        goto err1;
+    }
+
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+    if (fd < 0) {
+        perror("file_migration: failed to open filename");
+        term_printf("file_migration: failed to open filename %s\n", 
Quoted 8 lines

Review

Quoted 4 lines

filename);

(quoted diff) +2 -0
+        goto err2;
+    }

The migration code assumes that the file descriptor used is non-blocking. In general, open() on a file system cannot produce a non-blocking file descriptor.

Quoted 1 line

I can call fcntl with F_SETFL and O_NONBLOCK.

IIRC that doesn't have any effect on plain files - they'll still potentially block on write.

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
author
#6 lore raw
In reply to Daniel P. Berrange
Quoted 19 lines · Daniel P. Berrange
Quoted 17 lines · Anthony Liguori
Quoted 16 lines · Uri Lublin

Migration to file, reuses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The saved state-file should be used only once and removed (or used with -snapshot, or a the disk-image should be copied), as the disk image is not saved, only the VM state.

Also there is not point of doing a _live_ migration to file (except
for debugging migration code), so I recommend to stop the VM before migrating its state to a file.

An advantage migration-to-file over savevm/loadvm is that for the latter a qcow2 is a requirement, while the former works for any image-format.

---

Quoted 7 lines
Quoted 1 line

The migration code assumes that the file descriptor used is non-blocking. In general, open() on a file system cannot produce a non-blocking file descriptor.

Does it matter if it blocks though ? Migrating to a file is not
"live" migration anyway, so if we happen to block the rest of the QEMU event loop during course of writing to the file it doesn't seem too serious. Or is there some scenario which badly breaks with blocking
writes even for non-live usage ?

I first thought it's not that important too.
But it may be less than optimal, especially if qemu does not respond to monitor commands, which may affect management applications. What does libvirt do when qemu does not respond to a monitor command ?

Uri.

#3 lore raw
In reply to Uri Lublin

Uri Lublin writes ("Re: [Qemu-devel] [PATCH] migration: adding migration to/from a file"):

Quoted 4 lines
Quoted 1 line · Paul Brook

Wouldn't it be better to just implement savevm to file?

It sure is simpler.

In the Xen tree we already have an alternatively implementation of savevm which does not depend on or interact with the cow snapshots. See below.

This is a bit of a mess and it would be nice to have some functionality in upstream that did the same thing. NB that since we want to retain compatibility with previous savefiles the exact syntax of the data saved to the file is important.

FYI in the Xen case we don't need the live migration support that qemu upstream and kvm have, because the RAM live migration is handled by the Xen toolstack, and the remaining qemu saved state is small enough to do all in one go. But that doesn't mean that it's a bad idea.

Ian.

void do_savevm(const char *name)

{
    QEMUFile *f;
    int saved_vm_running, ret;

    f = qemu_fopen(name, "wb");
    
    /* ??? Should this occur after vm_stop?  */
    qemu_aio_flush();

    saved_vm_running = vm_running;
    vm_stop(0);

    if (!f) {
        fprintf(logfile, "Failed to open savevm file '%s'\n", name);
        goto the_end;
    }
    
    ret = qemu_savevm_state(f);
    qemu_fclose(f);

    if (ret < 0)
        fprintf(logfile, "Error %d while writing VM to savevm file '%s'\n",
                ret, name);

the_end:

    if (saved_vm_running)
        vm_start();

    return;
}
#7 lore raw
In reply to Daniel P. Berrange
Quoted 2 lines · Daniel P. Berrange

Does it matter if it blocks though ? Migrating to a file is not "live" migration anyway,

Yes. The general reason is that blocking in QEMU is evil because it's single threaded. If you block on IO in QEMU, then everything comes grinding to a halt. The VM may not be running, but you still want the GUI to be responsive, the monitor, and VNC to be responsive. This is particularly import in the context of asynchronous monitor notifications. savevm may be a long operation and there may be pending notifications that would get significantly delayed.

If you issue a "stop" before doing live migration, the result is equivalent to "savevm" from a binary perspective but the monitor will remain responsive during the whole operation. I'd like to eliminate the blocking version of savevm entirely but we're currently prevent from doing this because of the savevm to a disk. The issue is that you cannot write to the qcow2 snapshot space while other IO operations can happen. stop'ing the guest would prevent this (obviously) but it would be nice to support this in a truly live form.

I think we need to adjust the qcow2 snapshot format to support chaining of snapshot segments. That would allow a truly live save to qcow2.

Regards,

Anthony Liguori

Quoted 7 lines

so if we happen to block the rest of the
QEMU event loop during course of writing to the file it doesn't seem too serious. Or is there some scenario which badly breaks with blocking writes even for non-live usage ?

Daniel

#10 lore raw
In reply to Daniel P. Berrange
Quoted 9 lines · Daniel P. Berrange
Quoted 3 lines · Uri Lublin

I can call fcntl with F_SETFL and O_NONBLOCK.

IIRC that doesn't have any effect on plain files - they'll still potentially block on write.

Yes, it doesn't. I learned this the hard way with the old migration code. When I implemented file: migration, I noticed it never worked properly (properly being live) unless I used a rate limit. Since we defaulted a rate limit, it seemed to work, but the true test is whether it works in the absence of rate limiting.

Regards,

Anthony Liguori

Quoted 2 lines

Daniel