Hi tech@,

When I had to change a HD between machines I figured out that -P option
of mount_mfs(8) does not work with DUIDs: 

# mount_mfs -P ca7552589896b01e.d swap /mnt
mount_mfs: cannot stat ca7552589896b01e.d: No such file or directory

mount_mfs(8) already allows a DUID as the `special` argument, it is not
documented though. The patch bellow allow using -P with a DUID as block
device.

Important:
I used the same approach used in the code (pointer swap) to resolve DUID
to device using opendev(3). But after the patch there will be two
opendev(3) calls with non-NULL realpath, and as manpage states the
stored path is overwritten by subsequent calls.

It took some time of code reading and rereading until I convinced myself
that there is no problem in this *specific* situation.

However, I think that should be safier to use malloc(size = PATH_MAX) +
strlcpy() on `pop` and `special`. That way, code refactoring doesn't to
care about the relative position of the opendev(3) calls.

What do you think? Comments?


Patch: (available too at: https://www.diskless.io/openbsd/newfs.patch)


Index: sbin/newfs/newfs.8
===================================================================
RCS file: /cvs/src/sbin/newfs/newfs.8,v
retrieving revision 1.74
diff -u -p -r1.74 newfs.8
--- sbin/newfs/newfs.8  17 Mar 2016 07:18:34 -0000      1.74
+++ sbin/newfs/newfs.8  7 Apr 2019 05:49:13 -0000
@@ -293,7 +293,9 @@ is a directory, populate the created mfs
 contents of the directory.
 If
 .Ar file
-is a block device, populate the created mfs file system with the
+is a block device or a
+.Xr disklabel 8
+UID (DUID), populate the created mfs file system with the
 contents of the FFS file system contained on the device.
 .El
 .Pp
Index: sbin/newfs/newfs.c
===================================================================
RCS file: /cvs/src/sbin/newfs/newfs.c,v
retrieving revision 1.111
diff -u -p -r1.111 newfs.c
--- sbin/newfs/newfs.c  25 Nov 2018 17:12:10 -0000      1.111
+++ sbin/newfs/newfs.c  7 Apr 2019 05:49:13 -0000
@@ -174,7 +174,7 @@ main(int argc, char *argv[])
        struct stat st;
        struct statfs *mp;
        struct rlimit rl;
-       int fsi = -1, oflagset = 0, fso, len, n, maxpartitions;
+       int fsi = -1, oflagset = 0, fso, fp, fd, len, n, maxpartitions;
        char *cp = NULL, *s1, *s2, *special, *opstring, *realdev;
 #ifdef MFS
        char mountfromname[BUFSIZ];
@@ -526,6 +526,16 @@ havelabel:
                        args.export_info.ex_flags = MNT_EXRDONLY;
                if (mntflags & MNT_NOPERM)
                        mntflags |= MNT_NODEV | MNT_NOEXEC;
+
+               if (pop != NULL && isduid(pop, 0)) { 
+                       /* Resolve DUID to block device */
+                       fd = opendev(pop, O_RDONLY, OPENDEV_BLCK, &realdev);
+                       if (fd < 0)
+                               err(1, "could not open %s", pop);
+
+                       close(fd);
+                       pop = realdev;  
+               }
 
                switch (pid = fork()) {
                case -1:

Reply via email to