live backup vm
cover live backup vm
This patch is a scratch which enables a full live backup of VM,
actually it is equal to savevm lively. As a test it uses hmp command migrate to trigger the action.
The ultimate goal is providing another command to user as:
snapshotvm [with_state] [internal] [live]
which then create vm backups with a screen image(I am not sure if a screen snapshot is supported in qemu now). This function is enabled in VMWARE which can automatically create full vm snapshots lively with a screen image which seems cool, so I want to introduce it to qemu.
Limitations:
currently only implemented "snapshotvm with_state internal live" case. no screen captured in the time of creating snapshots.
no good integration with existing savevm, migration, snapshot_blkdev
code.
no test case yet.
hmp/qmp API not added.
Although there are so many gaps, but I want to send these patch
first to make sure I am in the right direction, esp the implemention of savevm lively with vmstate, to see if there is potentional problem, such as creating snapshots takes too long? Other cases would be simpler if this case have no major problem found.
Wenchao Xia (2):
live backup vm, export functions
live backup vm, snapshots all lively
block.c | 21 ++++++ block.h | 2 + buffered_file.c | 106 +++++++++++++++++++++++++++ buffered_file.h | 1 + migration.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ migration.h | 13 ++++ savevm.c | 37 +++------- sysemu.h | 2 + 8 files changed, 371 insertions(+), 26 deletions(-)
RFC PATCH 1/2 live backup vm, export functions
This patch moved bdrv_snapshot_find() from savevm.c to block.c,
also exports some function in savevm.c.
--- block.c | 21 +++++++++++++++++++++ block.h | 2 ++ savevm.c | 37 +++++++++++-------------------------- sysemu.h | 2 ++ 4 files changed, 36 insertions(+), 26 deletions(-)
block.c +21 -0
@@ -3322,6 +3322,27 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)return buf; } +int bdrv_snapshot_find (BlockDriverState *bs,QEMUSnapshotInfo *sn_info, +const char *name) +{ +QEMUSnapshotInfo *sn_tab, *sn; +int nb_sns, i, ret; + + ret = -ENOENT ; + nb_sns =bdrv_snapshot_list (bs, &sn_tab); +if (nb_sns <0 ) +return ret; +for (i =0 ; i < nb_sns; i++) { + sn = &sn_tab[i]; +if (!strcmp (sn->id_str, name) || !strcmp (sn->name, name)) { + *sn_info = *sn; + ret =0 ; +break ; + } + } +g_free (sn_tab); +return ret; +}/**************************************************************/ /* async I/Os */
block.h +2 -0
@@ -330,6 +330,8 @@ int bdrv_snapshot_list(BlockDriverState *bs,int bdrv_snapshot_load_tmp (BlockDriverState *bs,const char *snapshot_name);char *bdrv_snapshot_dump (char *buf,int buf_size,QEMUSnapshotInfo *sn); +int bdrv_snapshot_find (BlockDriverState *bs,QEMUSnapshotInfo *sn_info, +const char *name);char *get_human_readable_size (char *buf,int buf_size,int64_t size);int path_is_absolute (const char *path);
savevm.c +11 -26
@@ -698,7 +698,7 @@ int qemu_get_byte(QEMUFile *f)return result; } -static int64_t qemu_ftell (QEMUFile *f) +int64_t qemu_ftell (QEMUFile *f) {return f->buf_offset - f->buf_size + f->buf_index; }
@@ -2061,32 +2061,10 @@ out:return ret; } -static int bdrv_snapshot_find (BlockDriverState *bs,QEMUSnapshotInfo *sn_info, -const char *name) -{ -QEMUSnapshotInfo *sn_tab, *sn; -int nb_sns, i, ret; - - ret = -ENOENT ; - nb_sns =bdrv_snapshot_list (bs, &sn_tab); -if (nb_sns <0 ) -return ret; -for (i =0 ; i < nb_sns; i++) { - sn = &sn_tab[i]; -if (!strcmp (sn->id_str, name) || !strcmp (sn->name, name)) { - *sn_info = *sn; - ret =0 ; -break ; - } - } -g_free (sn_tab); -return ret; -} -/* * Deletes snapshots of a given name in all opened images. */ -static int del_existing_snapshots (Monitor *mon,const char *name) +int del_existing_snapshots (Monitor *mon,Error **errp,const char *name) {BlockDriverState *bs;QEMUSnapshotInfo sn1, *snapshot = &sn1;
@@ -2099,9 +2077,16 @@ static int del_existing_snapshots(Monitor *mon, const char *name) { ret =bdrv_snapshot_delete (bs, name);if (ret <0 ) { -monitor_printf (mon, +if (mon !=NULL ) { +monitor_printf (mon,"Error while deleting snapshot on '%s'\n" ,bdrv_get_device_name (bs)); + } +if (errp !=NULL ) { +error_setg (errp, +"Error while deleting snapshot on '%s'\n" , +bdrv_get_device_name (bs)); + }return -1 ; } }
@@ -2186,7 +2171,7 @@ void do_savevm(Monitor *mon, const QDict *qdict) }/* Delete old snapshots of the same name */ -if (name &&del_existing_snapshots (mon, name) <0 ) { +if (name &&del_existing_snapshots (mon,NULL , name) <0 ) { goto the_end; }
sysemu.h +2 -0
@@ -69,6 +69,8 @@ void do_savevm(Monitor *mon, const QDict *qdict);int load_vmstate (const char *name);void do_delvm (Monitor *mon,const QDict *qdict);void do_info_snapshots (Monitor *mon); +int del_existing_snapshots (Monitor *mon,Error **errp,const char *name); +int64_t qemu_ftell (QEMUFile *f);void qemu_announce_self (void );
Signature · 1 line
1.7.1
RFC PATCH 2/2 live backup vm, snapshots all lively
In reply to Wenchao Xia This patch uses a tricky to do live migration to local block
images, and then create internal snapshots. So basically this patch connect function in live migration and savevm to get a full live back effect.
In monitor, type migrate image: to trigger this action for
a test.
--- buffered_file.c | 106 +++++++++++++++++++++++++++ buffered_file.h | 1 + migration.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ migration.h | 13 ++++ 4 files changed, 335 insertions(+), 0 deletions(-)
buffered_file.c +106 -0
@@ -267,3 +267,109 @@ QEMUFile *qemu_fopen_ops_buffered(MigrationState *migration_state)return s->file; } + +/* image operations */ + +static int image_put_buffer (void *opaque,const uint8_t *buf,int64_t pos,int size) +{ +QEMUFileBuffered *s = opaque; +BlockDriverState *bds = s->migration_state->image.bds; +ssize_t error; + +DPRINTF ("putting %d bytes at %" PRId64"\n" , size, pos); + + error =qemu_file_get_error (s->file); +if (error) { +DPRINTF ("flush when error, bailing: %s\n" ,strerror (-error)); +return error; + } + +DPRINTF ("unfreezing output\n" ); + s->freeze_output =0 ; + +/* no buffer is used, so speed limit is not accurate but a memcpy is saved, +will buffer increase performance in this case? */ +if (size >0 ) { + error =bdrv_save_vmstate (bds, buf, pos, size); +if (error <0 ) { +DPRINTF ("bdrv_save_vmstate error, bailing: %s\n" , +strerror (-error)); +return error; + } + s->bytes_xfer += size; + } + +if (pos ==0 && size ==0 ) { +DPRINTF ("file is ready\n" ); +if (!s->freeze_output && s->bytes_xfer < s->xfer_limit) { +DPRINTF ("notifying client\n" ); +migrate_image_put_ready (s->migration_state); + } + } + +return size; +} + +static int image_close (void *opaque) +{ +QEMUFileBuffered *s = opaque; +BlockDriverState *bds = s->migration_state->image.bds; +int ret =0 ; + +DPRINTF ("closing\n" ); + s->xfer_limit =INT_MAX ; + ret =bdrv_flush (bds); + +qemu_del_timer (s->timer); +qemu_free_timer (s->timer); +g_free (s->buffer); +g_free (s); + +return ret; +} + +static const QEMUFileOps image_file_ops = { + .get_fd =NULL , + .put_buffer = image_put_buffer, + .close = image_close, + .rate_limit = buffered_rate_limit, + .get_rate_limit = buffered_get_rate_limit, + .set_rate_limit = buffered_set_rate_limit, +}; + +static void image_rate_tick (void *opaque) +{ +QEMUFileBuffered *s = opaque; + +if (qemu_file_get_error (s->file)) { +buffered_close (s); +return ; + } + +qemu_mod_timer (s->timer,qemu_get_clock_ms (rt_clock) +100 ); + +if (s->freeze_output) +return ; + + s->bytes_xfer =0 ; + +image_put_buffer (s,NULL ,0 ,0 ); +} + +QEMUFile *qemu_fopen_ops_image (MigrationState *migration_state) +{ +QEMUFileBuffered *s; + + s =g_malloc0 (sizeof (*s)); + + s->migration_state = migration_state; + s->xfer_limit = migration_state->bandwidth_limit /10 ; + + s->file =qemu_fopen_ops (s, &image_file_ops); + + s->timer =qemu_new_timer_ms (rt_clock, image_rate_tick, s); + +qemu_mod_timer (s->timer,qemu_get_clock_ms (rt_clock) +100 ); + +return s->file; +}
buffered_file.h +1 -0
@@ -18,5 +18,6 @@#include "migration.h" QEMUFile *qemu_fopen_ops_buffered (MigrationState *migration_state); +QEMUFile *qemu_fopen_ops_image (MigrationState *migration_state);#endif
migration.c +215 -0
230 lines not on this page · raw message
migration.h +13 -0
@@ -20,6 +20,7 @@#include "error.h" #include "vmstate.h" #include "qapi-types.h" +#include "block.h" struct MigrationParams {bool blk;
@@ -28,6 +29,13 @@ struct MigrationParams {typedef struct MigrationState MigrationState ; +struct MigrationImage { +BlockDriverState *bds; +QEMUSnapshotInfo sn; +}; + +typedef struct MigrationImage MigrationImage ; +struct MigrationState {int64_t bandwidth_limit;
@@ -45,6 +53,7 @@ struct MigrationStateint64_t dirty_pages_rate;bool enabled_capabilities[MIGRATION_CAPABILITY_MAX ];int64_t xbzrle_cache_size; +MigrationImage image; };void process_incoming_migration (QEMUFile *f);
@@ -128,4 +137,8 @@ int64_t migrate_xbzrle_cache_size(void);int64_t xbzrle_cache_resize (int64_t new_size); +void migrate_image_error (MigrationState *s); +void migrate_image_prepare (MigrationState *s,const char *name,Error **errp); +void migrate_image_start (MigrationState *s); +void migrate_image_put_ready (MigrationState *s);#endif
Signature · 1 line
1.7.1
We already have a full functional implementation to create live snapshots including VM state.
The interface allows you to create internal snapshots, or use external tools to create the blockdev snapshots directly on the underlying storage (we use that with nexenta). VM state is stored directly on a block device.
Unfortunately I had no time to cleanup those patches.
- Dietmar
Quoted 40 lines
This patch is a scratch which enables a full live backup of VM, actually it is
equal to savevm lively. As a test it uses hmp command migrate to trigger the action.The ultimate goal is providing another command to user as:
snapshotvm [with_state] [internal] [live] which then create vm backups with a screen image(I am not sure if a screen snapshot is supported in qemu now). This function is enabled in VMWARE which can automatically create full vm snapshots lively with a screen image which seems cool, so I want to introduce it to qemu.Limitations:
currently only implemented "snapshotvmwith_stateinternal live" case. no screen captured in the time of creating snapshots.
no good integration with existing savevm, migration,snapshot_blkdevcode. no test case yet.
hmp/qmp API not added.Although there are so many gaps, but I want to send these patch first to
make sure I am in the right direction, esp the implemention of savevm lively with vmstate, to see if there is potentional problem, such as creating snapshots takes too long? Other cases would be simpler if this case have no major problem found.Wenchao Xia (2):
live backup vm, export functions
live backup vm, snapshots all livelyblock.c | 21 ++++++ block.h | 2 + buffered_file.c | 106 +++++++++++++++++++++++++++ buffered_file.h | 1 + migration.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ migration.h | 13 ++++ savevm.c | 37 +++------- sysemu.h | 2 + 8 files changed, 371 insertions(+), 26 deletions(-)
Quoted 11 lines · Dietmar Maurer
We already have a full functional implementation to create live snapshots including VM state.
The interface allows you to create internal snapshots, or use external tools to create the blockdev snapshots directly on the underlying storage (we use that with nexenta). VM state is stored directly on a block device.
Unfortunately I had no time to cleanup those patches.
- Dietmar
have that patch been sent to mail-list? I'll rebase that patch
and take it as part of the API's implemention, if you agree.
Quoted 43 lines
Quoted 40 lines
This patch is a scratch which enables a full live backup of VM, actually it is
equal to savevm lively. As a test it uses hmp command migrate to trigger the action.The ultimate goal is providing another command to user as:
snapshotvm [with_state] [internal] [live] which then create vm backups with a screen image(I am not sure if a screen snapshot is supported in qemu now). This function is enabled in VMWARE which can automatically create full vm snapshots lively with a screen image which seems cool, so I want to introduce it to qemu.Limitations:
currently only implemented "snapshotvmwith_stateinternal live" case. no screen captured in the time of creating snapshots.
no good integration with existing savevm, migration,snapshot_blkdevcode. no test case yet.
hmp/qmp API not added.Although there are so many gaps, but I want to send these patch first to
make sure I am in the right direction, esp the implemention of savevm lively with vmstate, to see if there is potentional problem, such as creating snapshots takes too long? Other cases would be simpler if this case have no major problem found.Wenchao Xia (2):
live backup vm, export functions
live backup vm, snapshots all livelyblock.c | 21 ++++++ block.h | 2 + buffered_file.c | 106 +++++++++++++++++++++++++++ buffered_file.h | 1 + migration.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ migration.h | 13 ++++ savevm.c | 37 +++------- sysemu.h | 2 + 8 files changed, 371 insertions(+), 26 deletions(-)
Signature · 3 lines
Best Regards Wenchao Xia
Quoted 4 lines
Quoted 1 line
kvm.git;a=blob;f=debian/patches/in
Quoted 1 line
ternal-snapshot-
async.patch;h=6c86de3a6160c58d77baa41a7774c4a80e63639e
Quoted 8 lines
Quoted 6 lines
;hb=HEAD
Unfortunately I had no time to cleanup those patches.
- Dietmar
have that patch been sent to mail-list? I'll rebase that patch and take it as
part of the API's implemention, if you agree.
Sorry, as mentioned above I had no time to cleanup those patches so far. My plan is to send them early next year.
Quoted 8 lines · Dietmar Maurer
Quoted 8 lines
Quoted 1 line
kvm.git;a=blob;f=debian/patches/in
Quoted 1 line
ternal-snapshot-
async.patch;h=6c86de3a6160c58d77baa41a7774c4a80e63639e
Quoted 4 lines
;hb=HEAD
Unfortunately I had no time to cleanup those patches.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
- Dietmar
have that patch been sent to mail-list? I'll rebase that patch and take it as
part of the API's implemention, if you agree.Sorry, as mentioned above I had no time to cleanup those patches so far. My plan is to send them early next year.
I can do the clean up or rebase from the url you have given, and
include them as part of my serials. Just want to avoid conflict between your patch next year and mine by include yours as sub patches. :)
Signature · 3 lines
Best Regards Wenchao Xia
Quoted 3 lines
I can do the clean up or rebase from the url you have given, and include
them as part of my serials. Just want to avoid conflict between your patch next year and mine by include yours as sub patches. :)
That would be great.
Quoted 6 lines · Dietmar Maurer
Quoted 3 lines
I can do the clean up or rebase from the url you have given, and include
them as part of my serials. Just want to avoid conflict between your patch next year and mine by include yours as sub patches. :)That would be great.
I have read the code from the link, it seems it does two things, I
will break them into two parts:
1 create internal snapshots for a single device.
2 save/load vmstate in an specified image file that qemu support.
Have some questions:
in qmp_savevm_start(), vm_stop(RUN_STATE_SAVE_VM), I think this will
stop the VM running make it not lively, am I right?
Signature · 3 lines
Best Regards Wenchao Xia
OK, I will try to give a short overview on the functionality.
First, we decided to use 'internal' snapshots for our 'live snapshot' implementation. Internal snapshots almost always use some kind of reference counting, making snapshot action (like 'create/delete snapshot') quite efficient and fast.
Internal snapshot totally depends on the storage backend. For now we can create internal snapshots on the following storage backend:
* qcow2 files
* rbd
* sheepdog
* nexenta iscsi
As you can see, creating internal snapshots sometimes needs external tool. For example we use the nexenta API to create snapshots, while we use libiscsi to access them inside qemu.
So our snapshot implementation uses the following steps:
1.) qmp: savevm-start [statefile]
This save the VM state into [statefile] and then stops the VM.
2.) qga freezefs
3.) create snapshots: either using external tools or by qmp: snapshot-drive
4.) gqa unfreezefs
5.) qmp: savevm-end
This restarts/resumes the VM
Quoted 3 lines
Have some questions:
inqmp_savevm_start(),vm_stop(RUN_STATE_SAVE_VM), I think this will
stop the VM running make it not lively, am I right?
Yes, we need to halt the VM for a short period of time while we make snapshots. But that time is usually short.
- Dietmar
OK, I'll adjust the code into the upstream framework, thanks for your
declaration.
Quoted 42 lines
OK, I will try to give a short overview on the functionality.
First, we decided to use 'internal' snapshots for our 'live snapshot' implementation. Internal snapshots almost always use some kind of reference counting, making snapshot action (like 'create/delete snapshot') quite efficient and fast.
Internal snapshot totally depends on the storage backend. For now we can create internal snapshots on the following storage backend:
* qcow2 files
* rbd
* sheepdog
* nexenta iscsiAs you can see, creating internal snapshots sometimes needs external tool. For example we use the nexenta API to create snapshots, while we use libiscsi to access them inside qemu.
So our snapshot implementation uses the following steps:
1.) qmp: savevm-start [statefile]
This save the VM state into [statefile] and then stops the VM.
2.) qga freezefs
3.) create snapshots: either using external tools or by qmp: snapshot-drive
4.) gqa unfreezefs
5.) qmp: savevm-end
This restarts/resumes the VM
Quoted 3 lines
Have some questions:
inqmp_savevm_start(),vm_stop(RUN_STATE_SAVE_VM), I think this will
stop the VM running make it not lively, am I right?Yes, we need to halt the VM for a short period of time while we make snapshots. But that time is usually short.
- Dietmar
Signature · 3 lines
Best Regards Wenchao Xia
Quoted 5 lines
So our snapshot implementation uses the following steps:
1.) qmp: savevm-start [statefile]
This save the VM state into [statefile] and then stops the VM.
That's migrate exec:cat>statefile (or similar)
Quoted 1 line
2.) qga freezefs
How can freezefs run while the VM is stopped?
Quoted 4 lines
3.) create snapshots: either using external tools or by qmp:
snapshot-drive4.) gqa unfreezefs
Same as above, this should be done after step (5).
Quoted 3 lines
5.) qmp: savevm-end
This restarts/resumes the VM
That's simply "cont".
Paolo
Quoted 5 lines
Quoted 3 lines
1.) qmp: savevm-start [statefile]
This save the VM state into [statefile] and then stops the VM.
That's migrate exec:cat>statefile (or similar)
Basic functionality is the same, but we support saving state to block devices.
But there are many migration related patches in the last months, so I decide to not touch that code for now, and write my own.
But yes, in future, we can try to merge things.
Quoted 3 lines
Quoted 1 line
2.) qga freezefs
How can freezefs run while the VM is stopped?
Oh, good point ;-) So we need to call that inside savevm-start
Quoted 6 lines
Quoted 4 lines
3.) create snapshots: either using external tools or by qmp:
snapshot-drive4.) gqa unfreezefs
Same as above, this should be done after step (5).
yes
Quoted 5 lines
Quoted 3 lines
5.) qmp: savevm-end
This restarts/resumes the VM
That's simply "cont".
Yes - the plan was to do extra checks here. If it is not needed, we can remove it later.
I also want to avoid that a user can resume a VM which was suspended for taking snapshots, but I am not sure how to do that.
Quoted 7 lines
Quoted 5 lines
So our snapshot implementation uses the following steps:
1.) qmp: savevm-start [statefile]
This save the VM state into [statefile] and then stops the VM.
That's migrate exec:cat>statefile (or similar)
We save to block devices, so we need to allocate them in advance with a size of 2 x 'memory size'.
So we have an upper limit for the state file file, i.e. we stop the VM if ('statefile size' > 'assigned memory').
It would be great to have some other means to reduce state size (instead of stopping the VM), for example by slowing down the VM somehow? Any other ideas?
Il 12/12/2012 10:03, Dietmar Maurer ha scritto:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
So our snapshot implementation uses the following steps:
1.) qmp: savevm-start [statefile]
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
This save the VM state into [statefile] and then stops the VM.
That's migrate exec:cat>statefile (or similar)
We save to block devices, so we need to allocate them in advance with a size of 2 x 'memory size'.
So we have an upper limit for the state file file, i.e. we stop the VM if ('statefile size' > 'assigned memory').
This is a heuristic that we can add (though remember you'll need some extra space for the device state).
Quoted 2 lines
It would be great to have some other means to reduce state size (instead of stopping the VM), for example by slowing down the VM somehow?
Yes, you can use the cpu cgroup.
Paolo
Quoted 2 lines
Any other ideas?