]>
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>
40 #include "storage_utils.h"
44 #include "include/strlcpy.h"
47 lxc_log_define(nbd
, lxc
);
49 struct nbd_attach_data
{
54 static int do_attach_nbd(void *d
);
55 static bool clone_attach_nbd(const char *nbd
, const char *path
);
56 static bool nbd_busy(int idx
);
57 static void nbd_detach(const char *path
);
58 static int nbd_get_partition(const char *src
);
59 static bool wait_for_partition(const char *path
);
61 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
63 char *orig
, *p
, path
[50];
68 orig
= alloca(len
+ 1);
69 (void)strlcpy(orig
, src
, len
+ 1);
71 /* if path is followed by a partition, drop that for now */
72 p
= strchr(orig
, ':');
77 sprintf(path
, "/dev/nbd%d", i
);
79 if (!file_exists(path
))
87 if (!clone_attach_nbd(path
, orig
))
95 void detach_nbd_idx(int idx
)
100 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
101 if (ret
< 0 || ret
>= 50)
107 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
108 const char *oldname
, const char *cname
, const char *oldpath
,
109 const char *lxcpath
, int snap
, uint64_t newsize
,
110 struct lxc_conf
*conf
)
115 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
116 struct bdev_specs
*specs
)
121 int nbd_destroy(struct lxc_storage
*orig
)
126 bool nbd_detect(const char *path
)
128 if (!strncmp(path
, "nbd:", 4))
134 int nbd_mount(struct lxc_storage
*bdev
)
136 int ret
= -1, partition
;
140 if (strcmp(bdev
->type
, "nbd"))
143 if (!bdev
->src
|| !bdev
->dest
)
146 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
147 if (bdev
->nbd_idx
< 0)
150 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
151 partition
= nbd_get_partition(src
);
153 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
156 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
157 if (ret
< 0 || ret
>= 50) {
158 ERROR("Error setting up nbd device path");
162 /* It might take awhile for the partition files to show up */
164 if (!wait_for_partition(path
))
167 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
169 ERROR("Error mounting %s", bdev
->src
);
174 int nbd_umount(struct lxc_storage
*bdev
)
176 if (strcmp(bdev
->type
, "nbd"))
179 if (!bdev
->src
|| !bdev
->dest
)
182 return umount(bdev
->dest
);
185 bool requires_nbd(const char *path
)
187 if (strncmp(path
, "nbd:", 4) == 0)
193 static int do_attach_nbd(void *d
)
195 struct nbd_attach_data
*data
= d
;
196 const char *nbd
, *path
;
201 struct signalfd_siginfo fdsi
;
204 sigaddset(&mask
, SIGHUP
);
205 sigaddset(&mask
, SIGCHLD
);
210 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
211 SYSERROR("Error blocking signals for nbd watcher");
215 sfd
= signalfd(-1, &mask
, 0);
217 SYSERROR("Error opening signalfd for nbd task");
221 if (prctl(PR_SET_PDEATHSIG
, prctl_arg(SIGHUP
), prctl_arg(0),
222 prctl_arg(0), prctl_arg(0)) < 0)
223 SYSERROR("Error setting parent death signal for nbd watcher");
228 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
229 if (s
!= sizeof(struct signalfd_siginfo
))
230 SYSERROR("Error reading from signalfd");
232 if (fdsi
.ssi_signo
== SIGHUP
) {
233 /* container has exited */
236 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
239 /* If qemu-nbd fails, or is killed by a signal,
241 while (waitpid(-1, &status
, WNOHANG
) > 0) {
242 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
243 WIFSIGNALED(status
)) {
254 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
255 WARN("Warning: unblocking signals for nbd watcher");
257 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
258 SYSERROR("Error executing qemu-nbd");
262 static bool clone_attach_nbd(const char *nbd
, const char *path
)
265 struct nbd_attach_data data
;
270 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
);
277 static bool nbd_busy(int idx
)
282 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
283 if (ret
< 0 || ret
>= 100)
286 return file_exists(path
);
289 static void nbd_detach(const char *path
)
295 SYSERROR("Error forking to detach nbd");
300 ret
= wait_for_pid(pid
);
302 ERROR("nbd disconnect returned an error");
306 execlp("qemu-nbd", "qemu-nbd", "-d", path
, (char *)NULL
);
307 SYSERROR("Error executing qemu-nbd");
312 * Pick the partition # off the end of a nbd:file:p
313 * description. Return 1-9 for the partition id, or 0
316 static int nbd_get_partition(const char *src
)
318 char *p
= strchr(src
, ':');
322 p
= strchr(p
+1, ':');
328 if (*p
< '1' || *p
> '9')
334 static bool wait_for_partition(const char *path
)
339 if (file_exists(path
))
346 ERROR("Device %s did not show up after 5 seconds", path
);