]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/nbd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/prctl.h>
15 #include "memory_utils.h"
18 #include "storage_utils.h"
19 #include "syscall_wrappers.h"
26 lxc_log_define(nbd
, lxc
);
28 struct nbd_attach_data
{
33 static int do_attach_nbd(void *d
);
34 static bool clone_attach_nbd(const char *nbd
, const char *path
);
35 static bool nbd_busy(int idx
);
36 static void nbd_detach(const char *path
);
37 static int nbd_get_partition(const char *src
);
38 static bool wait_for_partition(const char *path
);
40 bool attach_nbd(char *src
, struct lxc_conf
*conf
)
42 __do_free
char *orig
= NULL
;
46 orig
= must_copy_string(src
);
47 /* if path is followed by a partition, drop that for now */
48 p
= strchr(orig
, ':');
53 sprintf(path
, "/dev/nbd%d", i
);
55 if (!file_exists(path
))
63 if (!clone_attach_nbd(path
, orig
))
71 void detach_nbd_idx(int idx
)
76 ret
= snprintf(path
, 50, "/dev/nbd%d", idx
);
77 if (ret
< 0 || ret
>= 50)
83 int nbd_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
84 const char *oldname
, const char *cname
, const char *oldpath
,
85 const char *lxcpath
, int snap
, uint64_t newsize
,
86 struct lxc_conf
*conf
)
91 int nbd_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
92 struct bdev_specs
*specs
, const struct lxc_conf
*conf
)
97 int nbd_destroy(struct lxc_storage
*orig
)
102 bool nbd_detect(const char *path
)
104 if (!strncmp(path
, "nbd:", 4))
110 int nbd_mount(struct lxc_storage
*bdev
)
112 int ret
= -1, partition
;
116 if (strcmp(bdev
->type
, "nbd"))
119 if (!bdev
->src
|| !bdev
->dest
)
122 /* nbd_idx should have been copied by bdev_init from the lxc_conf */
123 if (bdev
->nbd_idx
< 0)
126 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
127 partition
= nbd_get_partition(src
);
129 ret
= snprintf(path
, 50, "/dev/nbd%dp%d", bdev
->nbd_idx
,
132 ret
= snprintf(path
, 50, "/dev/nbd%d", bdev
->nbd_idx
);
133 if (ret
< 0 || ret
>= 50) {
134 ERROR("Error setting up nbd device path");
138 /* It might take awhile for the partition files to show up */
140 if (!wait_for_partition(path
))
143 ret
= mount_unknown_fs(path
, bdev
->dest
, bdev
->mntopts
);
145 ERROR("Error mounting %s", bdev
->src
);
150 int nbd_umount(struct lxc_storage
*bdev
)
152 if (strcmp(bdev
->type
, "nbd"))
155 if (!bdev
->src
|| !bdev
->dest
)
158 return umount(bdev
->dest
);
161 bool requires_nbd(const char *path
)
163 if (strncmp(path
, "nbd:", 4) == 0)
169 static int do_attach_nbd(void *d
)
171 struct nbd_attach_data
*data
= d
;
172 const char *nbd
, *path
;
177 struct signalfd_siginfo fdsi
;
180 sigaddset(&mask
, SIGHUP
);
181 sigaddset(&mask
, SIGCHLD
);
186 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1) {
187 SYSERROR("Error blocking signals for nbd watcher");
191 sfd
= signalfd(-1, &mask
, 0);
193 SYSERROR("Error opening signalfd for nbd task");
197 if (prctl(PR_SET_PDEATHSIG
, prctl_arg(SIGHUP
), prctl_arg(0),
198 prctl_arg(0), prctl_arg(0)) < 0)
199 SYSERROR("Error setting parent death signal for nbd watcher");
204 s
= read(sfd
, &fdsi
, sizeof(struct signalfd_siginfo
));
205 if (s
!= sizeof(struct signalfd_siginfo
))
206 SYSERROR("Error reading from signalfd");
208 if (fdsi
.ssi_signo
== SIGHUP
) {
209 /* container has exited */
212 } else if (fdsi
.ssi_signo
== SIGCHLD
) {
215 /* If qemu-nbd fails, or is killed by a signal,
217 while (waitpid(-1, &status
, WNOHANG
) > 0) {
218 if ((WIFEXITED(status
) && WEXITSTATUS(status
) != 0) ||
219 WIFSIGNALED(status
)) {
230 if (sigprocmask(SIG_UNBLOCK
, &mask
, NULL
) == -1)
231 WARN("Warning: unblocking signals for nbd watcher");
233 execlp("qemu-nbd", "qemu-nbd", "-c", nbd
, path
, (char *)NULL
);
234 SYSERROR("Error executing qemu-nbd");
238 static bool clone_attach_nbd(const char *nbd
, const char *path
)
241 struct nbd_attach_data data
;
246 pid
= lxc_clone(do_attach_nbd
, &data
, CLONE_NEWPID
, NULL
);
253 static bool nbd_busy(int idx
)
258 ret
= snprintf(path
, 100, "/sys/block/nbd%d/pid", idx
);
259 if (ret
< 0 || ret
>= 100)
262 return file_exists(path
);
265 static void nbd_detach(const char *path
)
271 SYSERROR("Error forking to detach nbd");
276 ret
= wait_for_pid(pid
);
278 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
, ':');
298 p
= strchr(p
+1, ':');
304 if (*p
< '1' || *p
> '9')
310 static bool wait_for_partition(const char *path
)
315 if (file_exists(path
))
322 ERROR("Device %s did not show up after 5 seconds", path
);