commit b1078d4f14a8ea66d81ab1a4ac16df4be2732daa Author: James Antill Date: Thu Dec 6 16:58:26 2012 -0500 Add an exported lvm_vg_create_lv_snapshot() API, to create snapshots LVs. diff --git a/liblvm/lvm2app.h b/liblvm/lvm2app.h index dd200e3..500f149 100644 --- a/liblvm/lvm2app.h +++ b/liblvm/lvm2app.h @@ -1002,6 +1002,32 @@ int lvm_vg_set_property(const vg_t vg, const char *name, lv_t lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size); /** + * Create a logical volume snapshot of another volume. + * This function commits the change to disk and does _not_ require calling + * lvm_vg_write(). + * NOTE: The commit behavior of this function is subject to change + * as the API is developed. + * + * \param vg + * VG handle obtained from lvm_vg_create() or lvm_vg_open(). + * + * \param origin + * Name of logical volume to create snapshot from. + * + * \param name + * Name of logical volume to create. + * + * \param size + * Size of logical volume in extents. + * + * \return + * non-NULL handle to an LV object created, or NULL if creation fails. + * + */ +lv_t lvm_vg_create_lv_snapshot(vg_t vg, const char *origin, + const char *name, uint64_t size); + +/** * Return a list of lvseg handles for a given LV handle. * * \memberof lv_t diff --git a/liblvm/lvm_lv.c b/liblvm/lvm_lv.c index 336b714..6a394fd 100644 --- a/liblvm/lvm_lv.c +++ b/liblvm/lvm_lv.c @@ -166,6 +166,33 @@ lv_t lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size) return (lv_t) lvl->lv; } +lv_t lvm_vg_create_lv_snapshot(vg_t vg, const char *origin, + const char *name, uint64_t size) +{ + struct lvcreate_params lp; + uint64_t extents; + struct lv_list *lvl; + + if (vg_read_error(vg)) + return NULL; + if (!vg_check_write_mode(vg)) + return NULL; + memset(&lp, 0, sizeof(lp)); + extents = extents_from_size(vg->cmd, size / SECTOR_SIZE, + vg->extent_size); + _lv_set_default_params(&lp, vg, name, extents); + if (!_lv_set_default_linear_params(vg->cmd, &lp)) + return_NULL; + lp.snapshot = 1; + lp.origin = origin; + if (!lv_create_single(vg, &lp)) + return NULL; + lvl = find_lv_in_vg(vg, name); + if (!lvl) + return NULL; + return (lv_t) lvl->lv; +} + /* * FIXME: This function should probably not commit to disk but require calling * lvm_vg_write.