Might be useful, particularly in scripting...

Behaves like losetup.

Index: sbin/mount_vnd/mount_vnd.c
===================================================================
RCS file: /cvs/src/sbin/mount_vnd/mount_vnd.c,v
retrieving revision 1.20
diff -u -p -r1.20 mount_vnd.c
--- sbin/mount_vnd/mount_vnd.c  24 Jan 2016 06:32:33 -0000      1.20
+++ sbin/mount_vnd/mount_vnd.c  28 Apr 2017 03:24:44 -0000
@@ -62,6 +62,7 @@
 #define VND_CONFIG     1
 #define VND_UNCONFIG   2
 #define VND_GET                3
+#define VND_FIND       4

 int verbose = 0;
 int run_mount_vnd = 0;
@@ -70,12 +71,13 @@ __dead void  usage(void);
 int             config(char *, char *, int, struct disklabel *, char *,
                     size_t);
 int             getinfo(const char *);
+int             findfirst(void);
 char           *get_pkcs_key(char *, char *);

 int
 main(int argc, char **argv)
 {
-       int      ch, rv, action, opt_c, opt_k, opt_K, opt_l, opt_u;
+       int      ch, rv, action, opt_c, opt_k, opt_K, opt_l, opt_u, opt_f = 0;
        char    *key, *mntopts, *rounds, *saltopt;
        size_t   keylen = 0;
        extern char *__progname;
@@ -88,7 +90,7 @@ main(int argc, char **argv)
        key = mntopts = rounds = saltopt = NULL;
        action = VND_CONFIG;

-       while ((ch = getopt(argc, argv, "ckK:lo:S:t:uv")) != -1) {
+       while ((ch = getopt(argc, argv, "ckK:lo:S:t:uv:f")) != -1) {
                switch (ch) {
                case 'c':
                        opt_c = 1;
@@ -103,6 +105,9 @@ main(int argc, char **argv)
                case 'l':
                        opt_l = 1;
                        break;
+               case 'f':
+                       opt_f = 1;
+                       break;
                case 'o':
                        mntopts = optarg;
                        break;
@@ -133,6 +138,8 @@ main(int argc, char **argv)

        if (opt_l)
                action = VND_GET;
+       else if (opt_f)
+               action = VND_FIND;
        else if (opt_u)
                action = VND_UNCONFIG;
        else
@@ -173,6 +180,8 @@ main(int argc, char **argv)
                rv = config(argv[0], NULL, action, NULL, NULL, 0);
        else if (action == VND_GET)
                rv = getinfo(argc ? argv[0] : NULL);
+       else if (action == VND_FIND)
+               rv = findfirst();
        else
                usage();

@@ -290,6 +299,35 @@ query:
        close(vd);

        return (0);
+}
+
+int
+findfirst(void)
+{
+       char *vname = DEFAULT_VND;
+       int vd;
+       struct vnd_user vnu;
+
+       vd = opendev((char *)vname, O_RDONLY, OPENDEV_PART, NULL);
+       if (vd < 0)
+               err(1, "open: %s", vname);
+
+       vnu.vnu_unit = -1;
+
+query:
+       if (ioctl(vd, VNDIOCGET, &vnu) == -1)
+               err(1, "ioctl: %s", vname);
+
+       if (!vnu.vnu_ino)
+               fprintf(stdout, "vnd%d\n", vnu.vnu_unit);
+       else {
+               vnu.vnu_unit++;
+               goto query;
+       }
+
+       close(vd);
+
+       return (0);
 }

 int

(cvs diff is dumb)

Reply via email to