]>
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"
41 #include "include/strlcpy.h"
44 lxc_log_define(nbd
, lxc
);
46 struct nbd_attach_data
{
51 static bool clone_attach_nbd(const char *nbd
, const char *path
);
52 static int do_attach_nbd(void *d
);
53 static bool nbd_busy(int idx
);
54 static void nbd_detach(const char *path
);
55 static int nbd_get_partition(const char *src
);
56 static bool wait_for_partition(const char *path
);
58 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
60 char *orig
, *p
, path
[50];
65 orig
= alloca(len
+ 1);
66 (void)strlcpy(orig
, src
, len
+ 1);
68 /* if path is followed by a partition, drop that for now */
69 p
= strchr(orig
, ':');
73 sprintf(path
, "/dev/nbd%d", i
);
74 if (!file_exists(path
))
80 if (!clone_attach_nbd(path
, orig
))
87 void detach_nbd_idx(int idx
)
92 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
93 if (ret
< 0 || ret
>= 50)
99 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
100 const char *oldname
, const char *cname
, const char *oldpath
,
101 const char *lxcpath
, int snap
, uint64_t newsize
,
102 struct lxc_conf
*conf
)
107 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
108 struct bdev_specs
*specs
)
113 int nbd_destroy(struct lxc_storage
*orig
)
118 bool nbd_detect(const char *path
)
120 if (!strncmp(path
, "nbd:", 4))
126 int nbd_mount(struct lxc_storage
*bdev
)
128 int ret
= -1, partition
;
132 if (strcmp(bdev
->type
, "nbd"))
135 if (!bdev
->src
|| !bdev
->dest
)
138 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
139 if (bdev
->nbd_idx
< 0)
142 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
143 partition
= nbd_get_partition(src
);
145 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
148 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
149 if (ret
< 0 || ret
>= 50) {
150 ERROR("Error setting up nbd device path");
154 /* It might take awhile for the partition files to show up */
156 if (!wait_for_partition(path
))
159 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
161 ERROR("Error mounting %s", bdev
->src
);
166 int nbd_umount(struct lxc_storage
*bdev
)
168 if (strcmp(bdev
->type
, "nbd"))
171 if (!bdev
->src
|| !bdev
->dest
)
174 return umount(bdev
->dest
);
177 bool requires_nbd(const char *path
)
179 if (strncmp(path
, "nbd:", 4) == 0)
184 static int do_attach_nbd(void *d
)
186 struct nbd_attach_data
*data
= d
;
187 const char *nbd
, *path
;
192 struct signalfd_siginfo fdsi
;
195 sigaddset(&mask
, SIGHUP
);
196 sigaddset(&mask
, SIGCHLD
);
201 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
202 SYSERROR("Error blocking signals for nbd watcher");
206 sfd
= signalfd(-1, &mask
, 0);
208 SYSERROR("Error opening signalfd for nbd task");
212 if (prctl(PR_SET_PDEATHSIG
, prctl_arg(SIGHUP
), prctl_arg(0),
213 prctl_arg(0), prctl_arg(0)) < 0)
214 SYSERROR("Error setting parent death signal for nbd watcher");
219 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
220 if (s
!= sizeof(struct signalfd_siginfo
))
221 SYSERROR("Error reading from signalfd");
223 if (fdsi
.ssi_signo
== SIGHUP
) {
224 /* container has exited */
227 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
229 /* If qemu-nbd fails, or is killed by a signal,
231 while (waitpid(-1, &status
, WNOHANG
) > 0) {
232 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
233 WIFSIGNALED(status
)) {
243 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
244 WARN("Warning: unblocking signals for nbd watcher");
246 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
247 SYSERROR("Error executing qemu-nbd");
251 static bool clone_attach_nbd(const char *nbd
, const char *path
)
254 struct nbd_attach_data data
;
259 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
);
265 static bool nbd_busy(int idx
)
270 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
271 if (ret
< 0 || ret
>= 100)
273 return file_exists(path
);
276 static void nbd_detach(const char *path
)
282 SYSERROR("Error forking to detach nbd");
286 ret
= wait_for_pid(pid
);
288 ERROR("nbd disconnect returned an error");
291 execlp("qemu-nbd", "qemu-nbd", "-d", path
, (char *)NULL
);
292 SYSERROR("Error executing qemu-nbd");
297 * Pick the partition # off the end of a nbd:file:p
298 * description. Return 1-9 for the partition id, or 0
301 static int nbd_get_partition(const char *src
)
303 char *p
= strchr(src
, ':');
306 p
= strchr(p
+1, ':');
310 if (*p
< '1' || *p
> '9')
315 static bool wait_for_partition(const char *path
)
319 if (file_exists(path
))
324 ERROR("Device %s did not show up after 5 seconds", path
);