On September 18, 2014 9:40:44 AM CEST, [email protected] wrote:
>This patch adds an option "-t template" to mount_tmpfs, which
>populates the new tmpfs volume with a directory
>immediately after creation.
>
>Man page update included for explanation.
>
>Much of the code was grafted from newfs
>which implements this for mount_mfs ("-P" option).
>
>Suggestions, fixes, criticism, etc. welcome.
>
>Index: mount_tmpfs.8
>===================================================================
>RCS file: /cvs/src/sbin/mount_tmpfs/mount_tmpfs.8,v
>retrieving revision 1.3
>diff -u -p -u -r1.3 mount_tmpfs.8
>--- mount_tmpfs.8      5 Feb 2014 15:32:26 -0000       1.3
>+++ mount_tmpfs.8      17 Sep 2014 21:37:43 -0000
>@@ -42,6 +42,7 @@
> .Op Fl n Ar nodes
> .Op Fl o Ar options
> .Op Fl s Ar size
>+.Op Fl t Ar template
> .Op Fl u Ar user
> .Ar tmpfs
> .Ar mount_point
>@@ -86,10 +87,28 @@ If zero is given (the default), the avai
> main memory and swap space) will be used.
> Note that four megabytes are always reserved for the system and cannot
> be assigned to the file system.
>+.It Fl t Ar template
>+If
>+.Ar template
>+is a directory, populate the created mfs file system with the
>+contents of the directory.
>+If
>+.Ar template
>+is a block device, populate the created mfs file system with the
>+contents of the FFS file system contained on the device.
> .It Fl u Ar user
> Specifies the user name or UID of the root inode of the file system.
> Defaults to the mount point's UID.
> .El
>+.Pp
>+When the
>+.Fl t Ar template
>+option is used, permissions are always copied from the template.
>+The
>+.Fl u , Fl g , 
>+and
>+.Fl m
>+options only affect the root inode of the file system.
> .Pp
> Every option that accepts a numerical value as its argument can take a
> trailing
>Index: mount_tmpfs.c
>===================================================================
>RCS file: /cvs/src/sbin/mount_tmpfs/mount_tmpfs.c,v
>retrieving revision 1.4
>diff -u -p -u -r1.4 mount_tmpfs.c
>--- mount_tmpfs.c      21 Jan 2014 21:58:27 -0000      1.4
>+++ mount_tmpfs.c      17 Sep 2014 21:37:43 -0000
>@@ -39,6 +39,7 @@ __RCSID("$NetBSD: mount_tmpfs.c,v 1.24 2
> #include <sys/param.h>
> #include <sys/mount.h>
> #include <sys/stat.h>
>+#include <sys/wait.h>
> 
> #include <ctype.h>
> #include <err.h>
>@@ -51,8 +52,9 @@ __RCSID("$NetBSD: mount_tmpfs.c,v 1.24 2
> #include <string.h>
> #include <unistd.h>
> #include <util.h>
>+#include <paths.h>
> 
>-#include "mount_tmpfs.h"
>+#include "pathnames.h"
> 
>/*
>---------------------------------------------------------------------
>*/
> 
>@@ -74,11 +76,34 @@ static void        pathadj(const char *, char *
> 
>/*
>---------------------------------------------------------------------
>*/
> 
>-void
>-mount_tmpfs_parseargs(int argc, char *argv[],
>-      struct tmpfs_args *args, int *mntflags,
>-      char *canon_dev, char *canon_dir)
>+static int do_exec(const char *, const char *, char *const[]);
>+static int isdir(const char *);
>+static void tcopy(char *, char *, struct tmpfs_args *);
>+static int gettmpmnt(char *, size_t);
>+
>+/*
>---------------------------------------------------------------------
>*/
>+
>+static void
>+usage(void)
>+{
>+      extern char *__progname;
>+      (void)fprintf(stderr,
>+          "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s
>size]\n"
>+          "           [-u user] tmpfs mount_point\n", __progname);

Aren't you missing something relevant here...? :-)

/Alexander

>+      exit(1);
>+}
>+
>+/*
>---------------------------------------------------------------------
>*/
>+
>+int
>+mount_tmpfs(int argc, char *argv[])
> {
>+      struct tmpfs_args args = {0};
>+      char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
>+      int mntflags = 0;
>+      
>+      char *template = NULL;
>+
>       int gidset, modeset, uidset; /* Ought to be 'bool'. */
>       int ch;
>       gid_t gid;
>@@ -88,18 +113,16 @@ mount_tmpfs_parseargs(int argc, char *ar
>       struct stat sb;
> 
>       /* Set default values for mount point arguments. */
>-      memset(args, 0, sizeof(*args));
>-      args->ta_version = TMPFS_ARGS_VERSION;
>-      args->ta_size_max = 0;
>-      args->ta_nodes_max = 0;
>-      *mntflags = 0;
>+      args.ta_version = TMPFS_ARGS_VERSION;
>+      args.ta_size_max = 0;
>+      args.ta_nodes_max = 0;
> 
>       gidset = 0; gid = 0;
>       uidset = 0; uid = 0;
>       modeset = 0; mode = 0;
> 
>       optind = optreset = 1;
>-      while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
>+      while ((ch = getopt(argc, argv, "g:m:n:o:s:t:u:")) != -1 ) {
>               switch (ch) {
>               case 'g':
>                       gid = a_gid(optarg);
>@@ -116,18 +139,22 @@ mount_tmpfs_parseargs(int argc, char *ar
>                       if (scan_scaled(optarg, &tmpnumber) == -1)
>                               err(EXIT_FAILURE, "failed to parse nodes `%s'",
>                                   optarg);
>-                      args->ta_nodes_max = tmpnumber;
>+                      args.ta_nodes_max = tmpnumber;
>                       break;
> 
>               case 'o':
>-                      getmntopts(optarg, mopts, mntflags);
>+                      getmntopts(optarg, mopts, &mntflags);
>                       break;
> 
>               case 's':
>                       if (scan_scaled(optarg, &tmpnumber) == -1)
>                               err(EXIT_FAILURE, "failed to parse size `%s'",
>                                   optarg);
>-                      args->ta_size_max = tmpnumber;
>+                      args.ta_size_max = tmpnumber;
>+                      break;
>+              
>+              case 't':
>+                      template = optarg;
>                       break;
> 
>               case 'u':
>@@ -152,37 +179,29 @@ mount_tmpfs_parseargs(int argc, char *ar
>       if (stat(canon_dir, &sb) == -1)
>               err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
> 
>-      args->ta_root_uid = uidset ? uid : sb.st_uid;
>-      args->ta_root_gid = gidset ? gid : sb.st_gid;
>-      args->ta_root_mode = modeset ? mode : sb.st_mode;
>-}
>+      args.ta_root_uid = uidset ? uid : sb.st_uid;
>+      args.ta_root_gid = gidset ? gid : sb.st_gid;
>+      args.ta_root_mode = modeset ? mode : sb.st_mode;
>+      
>+      int newflags = mntflags;
>+      if(template) newflags &= ~MNT_RDONLY;
> 
>-/*
>---------------------------------------------------------------------
>*/
>-
>-static void
>-usage(void)
>-{
>-      extern char *__progname;
>-      (void)fprintf(stderr,
>-          "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s
>size]\n"
>-          "           [-u user] tmpfs mount_point\n", __progname);
>-      exit(1);
>-}
>-
>-/*
>---------------------------------------------------------------------
>*/
>-
>-int
>-mount_tmpfs(int argc, char *argv[])
>-{
>-      struct tmpfs_args args;
>-      char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
>-      int mntflags;
>-
>-      mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
>-          canon_dev, canon_dir);
>-
>-      if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) == -1)
>+      if (mount(MOUNT_TMPFS, canon_dir, newflags, &args) == -1)
>               err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
>+              
>+      if(template) {
>+              tcopy(template, canon_dir, &args);
>+
>+              if (mntflags & MNT_RDONLY) {
>+                      mntflags |= MNT_UPDATE;
>+                      if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) < 0) 
>{
>+                              warn("%s: mount (update, rdonly)", canon_dir);
>+                              if (unmount(canon_dir, 0) != 0)
>+                                      warn("unmount %s", canon_dir);
>+                              exit(1);
>+                      }
>+              }
>+      }
> 
>       return EXIT_SUCCESS;
> }
>@@ -249,4 +268,133 @@ pathadj(const char *input, char *adjuste
>               warnx("\"%s\" is a non-resolved or relative path.", input);
>               warnx("using \"%s\" instead.", adjusted);
>       }
>+}
>+
>+/* Code copied from sbin/newfs/newfs.c to copy a template. */
>+
>+static int
>+do_exec(const char *dir, const char *cmd, char *const argv[])
>+{
>+      pid_t pid;
>+      int ret, status;
>+      sig_t intsave, quitsave;
>+
>+      switch (pid = fork()) {
>+      case -1:
>+              err(1, "fork");
>+      case 0:
>+              if (dir != NULL && chdir(dir) != 0)
>+                      err(1, "chdir");
>+              if (execv(cmd, argv) != 0)
>+                      err(1, "%s", cmd);
>+              break;
>+      default:
>+              intsave = signal(SIGINT, SIG_IGN);
>+              quitsave = signal(SIGQUIT, SIG_IGN);
>+              for (;;) {
>+                      ret = waitpid(pid, &status, 0);
>+                      if (ret == -1)
>+                              err(11, "waitpid");
>+                      if (WIFEXITED(status)) {
>+                              status = WEXITSTATUS(status);
>+                              if (status != 0)
>+                                      warnx("%s: exited", cmd);
>+                              break;
>+                      } else if (WIFSIGNALED(status)) {
>+                              warnx("%s: %s", cmd,
>+                                  strsignal(WTERMSIG(status)));
>+                              status = 1;
>+                              break;
>+                      }
>+              }
>+              signal(SIGINT, intsave);
>+              signal(SIGQUIT, quitsave);
>+              return (status);
>+      }
>+      /* NOTREACHED */
>+      return (-1);
>+}
>+
>+static int
>+isdir(const char *path)
>+{
>+      struct stat st;
>+
>+      if (stat(path, &st) != 0)
>+              err(1, "cannot stat %s", path);
>+      if (!S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode))
>+              errx(1, "%s: not a dir or a block device", path);
>+      return (S_ISDIR(st.st_mode));
>+}
>+
>+static void
>+tcopy(char *src, char *dst, struct tmpfs_args *args)
>+{
>+      int ret, dir, created = 0;
>+      struct ufs_args mount_args;
>+      char mountpoint[MNAMELEN];
>+      char *const argv[] = { "pax", "-rw", "-pe", ".", dst, NULL } ;
>+
>+      dir = isdir(src);
>+      if (dir)
>+              strlcpy(mountpoint, src, sizeof(mountpoint));
>+      else {
>+              created = gettmpmnt(mountpoint, sizeof(mountpoint));
>+              memset(&mount_args, 0, sizeof(mount_args));
>+              mount_args.fspec = src;
>+              ret = mount(MOUNT_FFS, mountpoint, MNT_RDONLY, &mount_args);
>+              if (ret != 0) {
>+                      if (created && rmdir(mountpoint) != 0)
>+                              warn("rmdir %s", mountpoint);
>+                      if (unmount(dst, 0) != 0)
>+                              warn("unmount %s", dst);
>+                      err(1, "mount %s %s", src, mountpoint);
>+              }
>+      }
>+      ret = do_exec(mountpoint, "/bin/pax", argv);
>+      if (!dir && unmount(mountpoint, 0) != 0)
>+              warn("unmount %s", mountpoint);
>+      if (created && rmdir(mountpoint) != 0)
>+              warn("rmdir %s", mountpoint);
>+      if (ret != 0) {
>+              if (unmount(dst, 0) != 0)
>+                      warn("unmount %s", dst);
>+              errx(1, "copy %s to %s failed", mountpoint, dst);
>+      }
>+}
>+
>+static int
>+gettmpmnt(char *mountpoint, size_t len)
>+{
>+      const char *tmp;
>+      const char *mnt = _PATH_MNT;
>+      struct statfs fs;
>+      size_t n;
>+
>+      tmp = getenv("TMPDIR");
>+      if (tmp == NULL || *tmp == '\0')
>+              tmp = _PATH_TMP;
>+
>+      if (statfs(tmp, &fs) != 0)
>+              err(1, "statfs %s", tmp);
>+      if (fs.f_flags & MNT_RDONLY) {
>+              if (statfs(mnt, &fs) != 0)
>+                      err(1, "statfs %s", mnt);
>+              if (strcmp(fs.f_mntonname, "/") != 0)
>+                      errx(1, "tmp mountpoint %s busy", mnt);
>+              if (strlcpy(mountpoint, mnt, len) >= len)
>+                      errx(1, "tmp mountpoint %s too long", mnt);
>+              return (0);
>+      }
>+      n = strlcpy(mountpoint, tmp, len);
>+      if (n >= len)
>+              errx(1, "tmp mount point too long");
>+      if (mountpoint[n - 1] != '/')
>+              strlcat(mountpoint, "/", len);
>+      n = strlcat(mountpoint, "mntXXXXXXXXXX", len);
>+      if (n >= len)
>+              errx(1, "tmp mount point too long");
>+      if (mkdtemp(mountpoint) == NULL)
>+              err(1, "mkdtemp %s", mountpoint);
>+      return (1);
> }
>Index: pathnames.h
>===================================================================
>RCS file: pathnames.h
>diff -N pathnames.h
>--- /dev/null  1 Jan 1970 00:00:00 -0000
>+++ pathnames.h        17 Sep 2014 21:37:43 -0000
>@@ -0,0 +1,37 @@
>+/*    $OpenBSD: pathnames.h,v 1.3 2003/06/02 20:06:15 millert Exp $   */
>+/*    $NetBSD: pathnames.h,v 1.6 1995/03/18 14:57:06 cgd Exp $        */
>+
>+/*
>+ * Copyright (c) 1989, 1993, 1994
>+ *    The Regents of the University of California.  All rights reserved.
>+ *
>+ * Redistribution and use in source and binary forms, with or without
>+ * modification, are permitted provided that the following conditions
>+ * are met:
>+ * 1. Redistributions of source code must retain the above copyright
>+ *    notice, this list of conditions and the following disclaimer.
>+ * 2. Redistributions in binary form must reproduce the above
>copyright
>+ *    notice, this list of conditions and the following disclaimer in
>the
>+ *    documentation and/or other materials provided with the
>distribution.
>+ * 3. Neither the name of the University nor the names of its
>contributors
>+ *    may be used to endorse or promote products derived from this
>software
>+ *    without specific prior written permission.
>+ *
>+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
>AND
>+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
>THE
>+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
>PURPOSE
>+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
>LIABLE
>+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
>CONSEQUENTIAL
>+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
>GOODS
>+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
>INTERRUPTION)
>+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
>STRICT
>+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
>ANY WAY
>+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
>OF
>+ * SUCH DAMAGE.
>+ *
>+ *    @(#)pathnames.h 8.2 (Berkeley) 3/27/94
>+ */
>+
>+#define _PATH_SBIN    "/sbin"
>+#define _PATH_USRSBIN "/usr/sbin"
>+#define       _PATH_MOUNTDPID "/var/run/mountd.pid"

Reply via email to