]>
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
31 #include <sys/prctl.h>
37 #include "storage_utils.h"
40 lxc_log_define(nbd
, lxc
);
42 struct nbd_attach_data
{
47 static bool clone_attach_nbd(const char *nbd
, const char *path
);
48 static int do_attach_nbd(void *d
);
49 static bool nbd_busy(int idx
);
50 static void nbd_detach(const char *path
);
51 static int nbd_get_partition(const char *src
);
52 static bool wait_for_partition(const char *path
);
54 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
56 char *orig
= alloca(strlen(src
)+1), *p
, path
[50];
60 /* if path is followed by a partition, drop that for now */
61 p
= strchr(orig
, ':');
65 sprintf(path
, "/dev/nbd%d", i
);
66 if (!file_exists(path
))
72 if (!clone_attach_nbd(path
, orig
))
79 void detach_nbd_idx(int idx
)
84 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
85 if (ret
< 0 || ret
>= 50)
91 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
92 const char *oldname
, const char *cname
, const char *oldpath
,
93 const char *lxcpath
, int snap
, uint64_t newsize
,
94 struct lxc_conf
*conf
)
99 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
100 struct bdev_specs
*specs
)
105 int nbd_destroy(struct lxc_storage
*orig
)
110 bool nbd_detect(const char *path
)
112 if (!strncmp(path
, "nbd:", 4))
118 int nbd_mount(struct lxc_storage
*bdev
)
120 int ret
= -1, partition
;
124 if (strcmp(bdev
->type
, "nbd"))
127 if (!bdev
->src
|| !bdev
->dest
)
130 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
131 if (bdev
->nbd_idx
< 0)
134 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
135 partition
= nbd_get_partition(src
);
137 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
140 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
141 if (ret
< 0 || ret
>= 50) {
142 ERROR("Error setting up nbd device path");
146 /* It might take awhile for the partition files to show up */
148 if (!wait_for_partition(path
))
151 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
153 ERROR("Error mounting %s", bdev
->src
);
158 int nbd_umount(struct lxc_storage
*bdev
)
160 if (strcmp(bdev
->type
, "nbd"))
163 if (!bdev
->src
|| !bdev
->dest
)
166 return umount(bdev
->dest
);
169 bool requires_nbd(const char *path
)
171 if (strncmp(path
, "nbd:", 4) == 0)
176 static int do_attach_nbd(void *d
)
178 struct nbd_attach_data
*data
= d
;
179 const char *nbd
, *path
;
184 struct signalfd_siginfo fdsi
;
187 sigaddset(&mask
, SIGHUP
);
188 sigaddset(&mask
, SIGCHLD
);
193 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
194 SYSERROR("Error blocking signals for nbd watcher");
198 sfd
= signalfd(-1, &mask
, 0);
200 SYSERROR("Error opening signalfd for nbd task");
204 if (prctl(PR_SET_PDEATHSIG
, SIGHUP
, 0, 0, 0) < 0)
205 SYSERROR("Error setting parent death signal for nbd watcher");
210 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
211 if (s
!= sizeof(struct signalfd_siginfo
))
212 SYSERROR("Error reading from signalfd");
214 if (fdsi
.ssi_signo
== SIGHUP
) {
215 /* container has exited */
218 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
220 /* If qemu-nbd fails, or is killed by a signal,
222 while (waitpid(-1, &status
, WNOHANG
) > 0) {
223 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
224 WIFSIGNALED(status
)) {
234 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
235 WARN("Warning: unblocking signals for nbd watcher");
237 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
238 SYSERROR("Error executing qemu-nbd");
242 static bool clone_attach_nbd(const char *nbd
, const char *path
)
245 struct nbd_attach_data data
;
250 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
);
256 static bool nbd_busy(int idx
)
261 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
262 if (ret
< 0 || ret
>= 100)
264 return file_exists(path
);
267 static void nbd_detach(const char *path
)
273 SYSERROR("Error forking to detach nbd");
277 ret
= wait_for_pid(pid
);
279 ERROR("nbd disconnect returned an error");
282 execlp("qemu-nbd", "qemu-nbd", "-d", path
, (char *)NULL
);
283 SYSERROR("Error executing qemu-nbd");
288 * Pick the partition # off the end of a nbd:file:p
289 * description. Return 1-9 for the partition id, or 0
292 static int nbd_get_partition(const char *src
)
294 char *p
= strchr(src
, ':');
297 p
= strchr(p
+1, ':');
301 if (*p
< '1' || *p
> '9')
306 static bool wait_for_partition(const char *path
)
310 if (file_exists(path
))
315 ERROR("Device %s did not show up after 5 seconds", path
);