From: Yu Yang <[email protected]> Clone a new VDI from a given snapshot VDI.
Signed-off-by: Yu Yang <[email protected]> --- lib/shared/sheepdog.h | 14 ++++++++++++++ lib/shared/vdi.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/shared/sheepdog.h b/lib/shared/sheepdog.h index f49ecdd..68b37b1 100644 --- a/lib/shared/sheepdog.h +++ b/lib/shared/sheepdog.h @@ -148,4 +148,18 @@ int sd_vdi_snapshot(struct sd_cluster *c, char *name, char *tag); */ int sd_vdi_create(struct sd_cluster *c, char *name, uint64_t size); +/* + * Clone a new VDI from a snapshot + * + * @c: pointer to the cluster descriptor + * @srcname: the source VDI name + * @srctag: the source VDI tag + * @dstname: the destination VDI name + * + * Return error code defined in sheepdog_proto.h. + * Only snapshot VDI can be cloned. + */ +int sd_vdi_clone(struct sd_cluster *c, char *srcname, + char *srctag, char *dstname); + #endif diff --git a/lib/shared/vdi.c b/lib/shared/vdi.c index 064d04d..27fb8d2 100644 --- a/lib/shared/vdi.c +++ b/lib/shared/vdi.c @@ -442,3 +442,41 @@ int sd_vdi_create(struct sd_cluster *c, char *name, uint64_t size) return ret; } + +int sd_vdi_clone(struct sd_cluster *c, char *srcname, + char *srctag, char *dstname) +{ + int ret; + struct sd_inode *inode = NULL; + + if (!srcname || *srcname == '\0') { + ret = SD_RES_INVALID_PARMS; + fprintf(stderr, "VDI name can NOT be null\n"); + goto out; + } + if (!dstname || *dstname == '\0') { + ret = SD_RES_INVALID_PARMS; + fprintf(stderr, "Destination VDI name can NOT be null\n"); + goto out; + } + if (!srctag || *srctag == '\0') { + ret = SD_RES_INVALID_PARMS; + fprintf(stderr, "Snapshot tag can NOT be null when clone\n"); + goto out; + } + + inode = xmalloc(sizeof(struct sd_inode)); + ret = vdi_read_inode(c, srcname, srctag, inode, false); + if (ret != SD_RES_SUCCESS) + goto out; + + ret = do_vdi_create(c, dstname, inode->vdi_size, inode->vdi_id, false, + inode->nr_copies, inode->copy_policy, + inode->store_policy, inode->block_size_shift); + if (ret != SD_RES_SUCCESS) + fprintf(stderr, "Clone VDI failed: %s\n", sd_strerror(ret)); + +out: + free(inode); + return ret; +} -- 1.7.9.5 -- sheepdog mailing list [email protected] https://lists.wpkg.org/mailman/listinfo/sheepdog
