]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/nbd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include <sys/prctl.h>
17 #include "memory_utils.h"
20 #include "storage_utils.h"
21 #include "syscall_wrappers.h"
28 lxc_log_define(nbd
, lxc
);
30 struct nbd_attach_data
{
35 static int do_attach_nbd(void *d
);
36 static bool clone_attach_nbd(const char *nbd
, const char *path
);
37 static bool nbd_busy(int idx
);
38 static void nbd_detach(const char *path
);
39 static int nbd_get_partition(const char *src
);
40 static bool wait_for_partition(const char *path
);
42 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
44 __do_free
char *orig
= NULL
;
48 orig
= must_copy_string(src
);
49 /* if path is followed by a partition, drop that for now */
50 p
= strchr(orig
, ':');
55 sprintf(path
, "/dev/nbd%d", i
);
57 if (!file_exists(path
))
65 if (!clone_attach_nbd(path
, orig
))
73 void detach_nbd_idx(int idx
)
78 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
79 if (ret
< 0 || ret
>= 50)
85 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
86 const char *oldname
, const char *cname
, const char *oldpath
,
87 const char *lxcpath
, int snap
, uint64_t newsize
,
88 struct lxc_conf
*conf
)
93 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
94 struct bdev_specs
*specs
, const struct lxc_conf
*conf
)
99 int nbd_destroy(struct lxc_storage
*orig
)
104 bool nbd_detect(const char *path
)
106 if (!strncmp(path
, "nbd:", 4))
112 int nbd_mount(struct lxc_storage
*bdev
)
114 int ret
= -1, partition
;
118 if (strcmp(bdev
->type
, "nbd"))
121 if (!bdev
->src
|| !bdev
->dest
)
124 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
125 if (bdev
->nbd_idx
< 0)
128 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
129 partition
= nbd_get_partition(src
);
131 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
134 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
135 if (ret
< 0 || ret
>= 50) {
136 ERROR("Error setting up nbd device path");
140 /* It might take awhile for the partition files to show up */
142 if (!wait_for_partition(path
))
145 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
147 ERROR("Error mounting %s", bdev
->src
);
152 int nbd_umount(struct lxc_storage
*bdev
)
154 if (strcmp(bdev
->type
, "nbd"))
157 if (!bdev
->src
|| !bdev
->dest
)
160 return umount(bdev
->dest
);
163 bool requires_nbd(const char *path
)
165 if (strncmp(path
, "nbd:", 4) == 0)
171 static int do_attach_nbd(void *d
)
173 struct nbd_attach_data
*data
= d
;
174 const char *nbd
, *path
;
179 struct signalfd_siginfo fdsi
;
182 sigaddset(&mask
, SIGHUP
);
183 sigaddset(&mask
, SIGCHLD
);
188 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
189 SYSERROR("Error blocking signals for nbd watcher");
193 sfd
= signalfd(-1, &mask
, 0);
195 SYSERROR("Error opening signalfd for nbd task");
199 if (prctl(PR_SET_PDEATHSIG
, prctl_arg(SIGHUP
), prctl_arg(0),
200 prctl_arg(0), prctl_arg(0)) < 0)
201 SYSERROR("Error setting parent death signal for nbd watcher");
206 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
207 if (s
!= sizeof(struct signalfd_siginfo
))
208 SYSERROR("Error reading from signalfd");
210 if (fdsi
.ssi_signo
== SIGHUP
) {
211 /* container has exited */
214 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
217 /* If qemu-nbd fails, or is killed by a signal,
219 while (waitpid(-1, &status
, WNOHANG
) > 0) {
220 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
221 WIFSIGNALED(status
)) {
232 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
233 WARN("Warning: unblocking signals for nbd watcher");
235 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
236 SYSERROR("Error executing qemu-nbd");
240 static bool clone_attach_nbd(const char *nbd
, const char *path
)
243 struct nbd_attach_data data
;
248 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
, NULL
);
255 static bool nbd_busy(int idx
)
260 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
261 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");
278 ret
= wait_for_pid(pid
);
280 ERROR("nbd disconnect returned an error");
284 execlp("qemu-nbd", "qemu-nbd", "-d", path
, (char *)NULL
);
285 SYSERROR("Error executing qemu-nbd");
290 * Pick the partition # off the end of a nbd:file:p
291 * description. Return 1-9 for the partition id, or 0
294 static int nbd_get_partition(const char *src
)
296 char *p
= strchr(src
, ':');
300 p
= strchr(p
+1, ':');
306 if (*p
< '1' || *p
> '9')
312 static bool wait_for_partition(const char *path
)
317 if (file_exists(path
))
324 ERROR("Device %s did not show up after 5 seconds", path
);