]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/rbd.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
26 #include <inttypes.h> /* Required for PRIu64 to work. */
34 #include "storage_utils.h"
38 #include "include/strlcpy.h"
41 lxc_log_define(rbd
, lxc
);
44 const char *osd_pool_name
;
49 int rbd_create_wrapper(void *data
)
51 struct rbd_args
*args
= data
;
53 execlp("rbd", "rbd", "create", "--pool", args
->osd_pool_name
,
54 args
->rbd_name
, "--size", args
->size
, (char *)NULL
);
59 int rbd_map_wrapper(void *data
)
61 struct rbd_args
*args
= data
;
63 execlp("rbd", "rbd", "map", "--pool", args
->osd_pool_name
,
64 args
->rbd_name
, (char *)NULL
);
69 int rbd_unmap_wrapper(void *data
)
71 struct rbd_args
*args
= data
;
73 execlp("rbd", "rbd", "unmap", args
->rbd_name
, (char *)NULL
);
78 int rbd_delete_wrapper(void *data
)
80 struct rbd_args
*args
= data
;
82 execlp("rbd", "rbd", "rm", args
->rbd_name
, (char *)NULL
);
87 int rbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
88 const char *oldname
, const char *cname
, const char *oldpath
,
89 const char *lxcpath
, int snap
, uint64_t newsize
,
90 struct lxc_conf
*conf
)
92 ERROR("rbd clonepaths not implemented");
96 int rbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
97 struct bdev_specs
*specs
)
99 const char *rbdpool
, *fstype
;
103 const char *cmd_args
[2];
104 char cmd_output
[MAXPATHLEN
];
105 const char *rbdname
= n
;
106 struct rbd_args args
= {0};
111 rbdpool
= specs
->rbd
.rbdpool
;
113 rbdpool
= lxc_global_config_value("lxc.bdev.rbd.rbdpool");
115 if (specs
->rbd
.rbdname
)
116 rbdname
= specs
->rbd
.rbdname
;
118 /* source device /dev/rbd/lxc/ctn */
119 len
= strlen(rbdpool
) + strlen(rbdname
) + 4 + 11;
120 bdev
->src
= malloc(len
);
122 ERROR("Failed to allocate memory");
126 ret
= snprintf(bdev
->src
, len
, "rbd:/dev/rbd/%s/%s", rbdpool
, rbdname
);
127 if (ret
< 0 || ret
>= len
) {
128 ERROR("Failed to create string");
132 /* fssize is in bytes */
133 size
= specs
->fssize
;
135 size
= DEFAULT_FS_SIZE
;
137 /* in megabytes for rbd tool */
138 ret
= snprintf(sz
, 24, "%" PRIu64
, size
/ 1024 / 1024);
139 if (ret
< 0 || ret
>= 24) {
140 ERROR("Failed to create string");
144 args
.osd_pool_name
= rbdpool
;
145 args
.rbd_name
= rbdname
;
147 ret
= run_command(cmd_output
, sizeof(cmd_output
), rbd_create_wrapper
,
150 ERROR("Failed to create rbd storage volume \"%s\": %s", rbdname
,
155 ret
= run_command(cmd_output
, sizeof(cmd_output
), rbd_map_wrapper
,
158 ERROR("Failed to map rbd storage volume \"%s\": %s", rbdname
,
163 fstype
= specs
->fstype
;
165 fstype
= DEFAULT_FSTYPE
;
167 cmd_args
[0] = fstype
;
168 cmd_args
[1] = lxc_storage_get_path(bdev
->src
, bdev
->type
);
169 ret
= run_command(cmd_output
, sizeof(cmd_output
), do_mkfs_exec_wrapper
,
172 ERROR("Failed to map rbd storage volume \"%s\": %s", rbdname
,
177 bdev
->dest
= strdup(dest
);
179 ERROR("Failed to duplicate string \"%s\"", dest
);
183 ret
= mkdir_p(bdev
->dest
, 0755);
184 if (ret
< 0 && errno
!= EEXIST
) {
185 ERROR("Failed to create directory \"%s\"", bdev
->dest
);
189 TRACE("Created rbd storage volume \"%s\"", bdev
->dest
);
193 int rbd_destroy(struct lxc_storage
*orig
)
198 char cmd_output
[MAXPATHLEN
];
199 struct rbd_args args
= {0};
202 src
= lxc_storage_get_path(orig
->src
, orig
->type
);
203 if (file_exists(src
)) {
205 ret
= run_command(cmd_output
, sizeof(cmd_output
),
206 rbd_unmap_wrapper
, (void *)&args
);
208 ERROR("Failed to map rbd storage volume \"%s\": %s",
215 rbdfullname
= alloca(len
- 8);
216 (void)strlcpy(rbdfullname
, &src
[9], len
- 8);
217 args
.rbd_name
= rbdfullname
;
219 ret
= run_command(cmd_output
, sizeof(cmd_output
),
220 rbd_delete_wrapper
, (void *)&args
);
222 ERROR("Failed to delete rbd storage volume \"%s\": %s",
223 rbdfullname
, cmd_output
);
230 bool rbd_detect(const char *path
)
232 if (!strncmp(path
, "rbd:", 4))
235 if (!strncmp(path
, "/dev/rbd/", 9))
241 int rbd_mount(struct lxc_storage
*bdev
)
245 if (strcmp(bdev
->type
, "rbd"))
248 if (!bdev
->src
|| !bdev
->dest
)
251 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
252 if (!file_exists(src
)) {
253 /* If blkdev does not exist it should be mapped, because it is
254 * not persistent on reboot.
256 ERROR("Block device %s is not mapped.", bdev
->src
);
260 return mount_unknown_fs(src
, bdev
->dest
, bdev
->mntopts
);
263 int rbd_umount(struct lxc_storage
*bdev
)
265 if (strcmp(bdev
->type
, "rbd"))
268 if (!bdev
->src
|| !bdev
->dest
)
271 return umount(bdev
->dest
);