]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/nbd.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
33 #include <sys/prctl.h>
38 #include "memory_utils.h"
41 #include "storage_utils.h"
42 #include "syscall_wrappers.h"
46 #include "include/strlcpy.h"
49 lxc_log_define(nbd
, lxc
);
51 struct nbd_attach_data
{
56 static int do_attach_nbd(void *d
);
57 static bool clone_attach_nbd(const char *nbd
, const char *path
);
58 static bool nbd_busy(int idx
);
59 static void nbd_detach(const char *path
);
60 static int nbd_get_partition(const char *src
);
61 static bool wait_for_partition(const char *path
);
63 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
65 __do_free
char *orig
= NULL
;
69 orig
= must_copy_string(src
);
70 /* if path is followed by a partition, drop that for now */
71 p
= strchr(orig
, ':');
76 sprintf(path
, "/dev/nbd%d", i
);
78 if (!file_exists(path
))
86 if (!clone_attach_nbd(path
, orig
))
94 void detach_nbd_idx(int idx
)
99 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
100 if (ret
< 0 || ret
>= 50)
106 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
107 const char *oldname
, const char *cname
, const char *oldpath
,
108 const char *lxcpath
, int snap
, uint64_t newsize
,
109 struct lxc_conf
*conf
)
114 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
115 struct bdev_specs
*specs
)
120 int nbd_destroy(struct lxc_storage
*orig
)
125 bool nbd_detect(const char *path
)
127 if (!strncmp(path
, "nbd:", 4))
133 int nbd_mount(struct lxc_storage
*bdev
)
135 int ret
= -1, partition
;
139 if (strcmp(bdev
->type
, "nbd"))
142 if (!bdev
->src
|| !bdev
->dest
)
145 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
146 if (bdev
->nbd_idx
< 0)
149 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
150 partition
= nbd_get_partition(src
);
152 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
155 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
156 if (ret
< 0 || ret
>= 50) {
157 ERROR("Error setting up nbd device path");
161 /* It might take awhile for the partition files to show up */
163 if (!wait_for_partition(path
))
166 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
168 ERROR("Error mounting %s", bdev
->src
);
173 int nbd_umount(struct lxc_storage
*bdev
)
175 if (strcmp(bdev
->type
, "nbd"))
178 if (!bdev
->src
|| !bdev
->dest
)
181 return umount(bdev
->dest
);
184 bool requires_nbd(const char *path
)
186 if (strncmp(path
, "nbd:", 4) == 0)
192 static int do_attach_nbd(void *d
)
194 struct nbd_attach_data
*data
= d
;
195 const char *nbd
, *path
;
200 struct signalfd_siginfo fdsi
;
203 sigaddset(&mask
, SIGHUP
);
204 sigaddset(&mask
, SIGCHLD
);
209 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
210 SYSERROR("Error blocking signals for nbd watcher");
214 sfd
= signalfd(-1, &mask
, 0);
216 SYSERROR("Error opening signalfd for nbd task");
220 if (prctl(PR_SET_PDEATHSIG
, prctl_arg(SIGHUP
), prctl_arg(0),
221 prctl_arg(0), prctl_arg(0)) < 0)
222 SYSERROR("Error setting parent death signal for nbd watcher");
227 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
228 if (s
!= sizeof(struct signalfd_siginfo
))
229 SYSERROR("Error reading from signalfd");
231 if (fdsi
.ssi_signo
== SIGHUP
) {
232 /* container has exited */
235 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
238 /* If qemu-nbd fails, or is killed by a signal,
240 while (waitpid(-1, &status
, WNOHANG
) > 0) {
241 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
242 WIFSIGNALED(status
)) {
253 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
254 WARN("Warning: unblocking signals for nbd watcher");
256 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
257 SYSERROR("Error executing qemu-nbd");
261 static bool clone_attach_nbd(const char *nbd
, const char *path
)
264 struct nbd_attach_data data
;
269 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
, NULL
);
276 static bool nbd_busy(int idx
)
281 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
282 if (ret
< 0 || ret
>= 100)
285 return file_exists(path
);
288 static void nbd_detach(const char *path
)
294 SYSERROR("Error forking to detach nbd");
299 ret
= wait_for_pid(pid
);
301 ERROR("nbd disconnect returned an error");
305 execlp("qemu-nbd", "qemu-nbd", "-d", path
, (char *)NULL
);
306 SYSERROR("Error executing qemu-nbd");
311 * Pick the partition # off the end of a nbd:file:p
312 * description. Return 1-9 for the partition id, or 0
315 static int nbd_get_partition(const char *src
)
317 char *p
= strchr(src
, ':');
321 p
= strchr(p
+1, ':');
327 if (*p
< '1' || *p
> '9')
333 static bool wait_for_partition(const char *path
)
338 if (file_exists(path
))
345 ERROR("Device %s did not show up after 5 seconds", path
);