rbd: allow importing from stdin
PATCH 1/2 rbd: allow importing from stdin
This patch allows importing images from stdin with the following command:
rbd import --size=<size in MB> - [dest-image]
--- src/rbd.cc | 37 +++++++++++++++++++++++++------------ 1 files changed, 25 insertions(+), 12 deletions(-)
src/rbd.cc +25 -12
@@ -402,29 +402,37 @@ done_img: } static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, - const char *imgname, int *order, const char *path) + const char *imgname, int *order, const char *path, + int64_t size) { - int fd = open(path, O_RDONLY); - int r; - int64_t size = 0; + int fd, r; struct stat stat_buf; string md_oid; struct fiemap *fiemap; MyProgressContext pc("Importing image"); + if (! strcmp(path, "-")) { + fd = 0; + } else { + fd = open(path, O_RDONLY); + } + if (fd < 0) { r = -errno; cerr << "error opening " << path << std::endl; return r; } - r = fstat(fd, &stat_buf); - if (r < 0) { - r = -errno; - cerr << "stat error " << path << std::endl; - return r; + if (fd > 0) { + r = fstat(fd, &stat_buf); + if (r < 0) { + r = -errno; + cerr << "stat error " << path << std::endl; + return r; + } + size = (uint64_t)stat_buf.st_size; } - size = (uint64_t)stat_buf.st_size; + if (!size) { r = get_block_device_size(fd, &size); if (r < 0) {
@@ -505,7 +513,12 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, while (cur_seg) { bufferptr p(cur_seg); //cerr << "reading " << cur_seg << " bytes at offset " << file_pos << std::endl; - ssize_t rval = TEMP_FAILURE_RETRY(::pread(fd, p.c_str(), cur_seg, file_pos)); + ssize_t rval; + if(extent == 0 && fiemap->fm_extents[extent].fe_logical == 0) { + rval = TEMP_FAILURE_RETRY(::read(fd, p.c_str(), cur_seg)); + } else { + rval = TEMP_FAILURE_RETRY(::pread(fd, p.c_str(), cur_seg, file_pos)); + } if (rval < 0) { r = -errno; cerr << "error reading file: " << cpp_strerror(r) << std::endl;
@@ -1303,7 +1316,7 @@ int main(int argc, const char **argv) cerr << "pathname should be specified" << std::endl; exit(1); } - r = do_import(rbd, dest_io_ctx, destname, &order, path); + r = do_import(rbd, dest_io_ctx, destname, &order, path, size); if (r < 0) { cerr << "import failed: " << cpp_strerror(-r) << std::endl; exit(1);
Signature · 1 line
1.7.1
Quoted 9 lines · Christian Brunner
This patch allows importing images from stdin with the following command:
rbd import --size=<size in MB> - [dest-image]
Signed-off-by Christian Brunner <[email protected]>---
src/rbd.cc | 37 +++++++++++++++++++++++++------------1 files changed, 25 insertions(+), 12 deletions(-)
Quoted 16 lines
- r =
fstat(fd, &stat_buf);
- if (r < 0) {
- r = -errno;
- cerr << "stat error " << path <<std::endl;
- return r;
+ if (fd > 0) {
+ r =fstat(fd, &stat_buf);
+ if (r < 0) {
+ r = -errno;
+ cerr << "stat error " << path <<std::endl;
+ return r;
+ }
+ size = (uint64_t)stat_buf.st_size;
}
- size = (uint64_t)stat_buf.st_size;
+
There's no reason why you couldn't stat stdin too, it's just not guaranteed to give you anything helpful. Think "rbd import - myimg <file". Just do the stat and react to the results, don't avoid the stat.
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html