Welcome to little lamb

Code » anopa » commit 6cd7623

mount: Add option --mkdir

author Olivier Brunel
2015-02-23 19:22:26 UTC
committer Olivier Brunel
2015-04-04 12:47:35 UTC
parent 7d330a01619150df5b1467fe0c4b6e3c6bdc907d

mount: Add option --mkdir

doc/aa-mount.pod +7 -2
src/utils/aa-mount.c +13 -1

diff --git a/doc/aa-mount.pod b/doc/aa-mount.pod
index e0c1935..971670a 100644
--- a/doc/aa-mount.pod
+++ b/doc/aa-mount.pod
@@ -4,8 +4,8 @@ aa-mount - Mount a filesystem
 
 =head1 SYNOPSIS
 
-B<aa-mount> [B<-B> | B<-M>] [B<-r> | B<-w>] [B<-t> I<FSTYPE>] [B<-o> I<OPTIONS>]
-I<DEVICE> I<MOUNTPOINT>
+B<aa-mount> [B<-B> | B<-M>] [B<-r> | B<-w>] [B<-d>] [B<-t> I<FSTYPE>]
+[B<-o> I<OPTIONS>] I<DEVICE> I<MOUNTPOINT>
 
 =head1 OPTIONS
 
@@ -20,6 +20,11 @@ e.g:
     aa-mount -B olddir newdir
     aa-mount -o remount,ro,bind olddir newdir
 
+=item B<-d, --mkdir>
+
+Create directory I<MOUNTPOINT> before doing the mount. Note that this only tries
+to create the last element of the path, so all parents must already exists.
+
 =item B<-h, --help>
 
 Show help screen and exit.
diff --git a/src/utils/aa-mount.c b/src/utils/aa-mount.c
index 13b9324..51749ba 100644
--- a/src/utils/aa-mount.c
+++ b/src/utils/aa-mount.c
@@ -1,5 +1,8 @@
 
 #include <getopt.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/mount.h>
 #include <skalibs/strerr2.h>
 #include <skalibs/stralloc.h>
@@ -106,6 +109,7 @@ dieusage (int rc)
             " -o, --options OPTIONS         Use OPTIONS as mount options\n"
             " -r, --read-only               Mount read-only\n"
             " -w, --read-write              Mount read-write\n"
+            " -d, --mkdir                   Create MOUNTPOINT before mounting\n"
             " -h, --help                    Show this help screen and exit\n"
             " -V, --version                 Show version information and exit\n"
             );
@@ -118,11 +122,13 @@ main (int argc, char * const argv[])
     stralloc sa = STRALLOC_ZERO;
     unsigned long flags = MS_MGC_VAL;
     const char *fstype = NULL;
+    int mk = 0;
 
     for (;;)
     {
         struct option longopts[] = {
             { "bind",               no_argument,        NULL,   'B' },
+            { "mkdir",              no_argument,        NULL,   'd' },
             { "help",               no_argument,        NULL,   'h' },
             { "move",               no_argument,        NULL,   'M' },
             { "options",            required_argument,  NULL,   'o' },
@@ -134,7 +140,7 @@ main (int argc, char * const argv[])
         };
         int c;
 
-        c = getopt_long (argc, argv, "BhMo:rt:Vw", longopts, NULL);
+        c = getopt_long (argc, argv, "BdhMo:rt:Vw", longopts, NULL);
         if (c == -1)
             break;
         switch (c)
@@ -144,6 +150,10 @@ main (int argc, char * const argv[])
                     strerr_diefu1sys (2, "build user options");
                 break;
 
+            case 'd':
+                mk = 1;
+                break;
+
             case 'h':
                 dieusage (0);
 
@@ -186,6 +196,8 @@ main (int argc, char * const argv[])
 
     if (!stralloc_0 (&sa))
         strerr_diefu1sys (2, "build user options");
+    if (mk && mkdir (argv[1], 0755) < 0 && errno != EEXIST)
+        strerr_diefu4sys (2, "mkdir ", argv[1], " to mount ", argv[0]);
     if (mount (argv[0], argv[1], fstype, flags, sa.s) < 0)
         strerr_diefu4sys (3, "mount ", argv[0], " on ", argv[1]);