annotate toys/pending/mount.c @ 1385:ffc015bddb26 draft 0.4.9

Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
author Rob Landley <rob@landley.net>
date Sun, 06 Jul 2014 23:43:29 -0500
parents 9fd2bcedbeb5
children 487716951287
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* mount.c - mount filesystems
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2014 Rob Landley <rob@landley.net>
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * Note: -hV is bad spec, haven't implemented -FsLU yet
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * no mtab (/proc/mounts does it) so -n is NOP.
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
9 USE_MOUNT(NEWTOY(mount, "?>2afnrvwt:o*[-rw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config MOUNT
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "mount"
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default n
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: mount [-afFrsvw] [-t TYPE] [-o OPTIONS...] [[DEVICE] DIR]
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Mount new filesystem(s) on directories. With no arguments, display existing
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 mounts.
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -a mount all entries in /etc/fstab (with -t, only entries of that TYPE)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -f fake it (don't actually mount)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 -r read only (same as -o ro)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 -w read/write (default, same as -o rw)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -t specify filesystem type
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 -v verbose
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 OPTIONS is a comma separated list of options, which can also be supplied
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 as --longopts.
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 This mount autodetects loopback mounts (a file on a directory) and
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 bind mounts (file on file, directory on directory), so you don't need
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 to say --bind or --loop.
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 */
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #define FOR_mount
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #include "toys.h"
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
37
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 GLOBALS(
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 struct arg_list *optlist;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 char *type;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 unsigned long flags;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 char *opts;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 )
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
45
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
46 // Strip flags out of comma separated list of options.
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
47 // Return flags,
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
48 static long parse_opts(char *new, long flags, char **more)
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
49 {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
50 struct {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
51 char *name;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
52 long flags;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
53 } opts[] = {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
54 // NOPs (we autodetect --loop and --bind)
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
55 {"loop", 0}, {"bind", 0}, {"defaults", 0}, {"quiet", 0},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
56 // {"noauto", 0}, {"swap", 0},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
57 {"ro", MS_RDONLY}, {"rw", ~MS_RDONLY},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
58 {"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
59 {"nodev", MS_NODEV}, {"dev", ~MS_NODEV},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
60 {"noexec", MS_NOEXEC}, {"exec", ~MS_NOEXEC},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
61 {"sync", MS_SYNCHRONOUS}, {"async", ~MS_SYNCHRONOUS},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
62 {"noatime", MS_NOATIME}, {"atime", ~MS_NOATIME},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
63 {"nodiratime", MS_NODIRATIME}, {"diratime", ~MS_NODIRATIME},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
64 {"loud", ~MS_SILENT},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
65 {"shared", MS_SHARED}, {"rshared", MS_SHARED|MS_REC},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
66 {"slave", MS_SLAVE}, {"rslave", MS_SLAVE|MS_REC},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
67 {"private", MS_PRIVATE}, {"rprivate", MS_SLAVE|MS_REC},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
68 {"unbindable", MS_UNBINDABLE}, {"runbindable", MS_UNBINDABLE|MS_REC},
1385
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
69 {"remount", MS_REMOUNT}, {"move", MS_MOVE},
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
70 // mand dirsync rec iversion strictatime
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
71 };
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
72
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
73 for (;;) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
74 char *comma = strchr(new, ',');
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
75 int i;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
76
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
77 if (comma) *comma = 0;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
78
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
79 // If we recognize an option, apply flags
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
80 for (i = 0; i < ARRAY_LEN(opts); i++) if (!strcasecmp(opts[i].name, new)) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
81 long ll = opts[i].flags;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
82
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
83 if (ll < 0) flags &= ll;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
84 else flags |= ll;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
85
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
86 break;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
87 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
88
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
89 // If we didn't recognize it, keep string version
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
90 if (more && i == ARRAY_LEN(opts)) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
91 i = *more ? strlen(*more) : 0;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
92 *more = xrealloc(*more, i + strlen(new) + 2);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
93 if (i) (*more)[i++] = ',';
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
94 strcpy(i+*more, new);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
95 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
96
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
97 if (!comma) break;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
98 *comma = ',';
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
99 new = comma + 1;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
100 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
101
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
102 return flags;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
103 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
104
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
105 static void mount_filesystem(char *dev, char *dir, char *type,
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
106 unsigned long flags, char *opts)
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 FILE *fp = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 int rc = EINVAL;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
110
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 if (toys.optflags & FLAG_f) return;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
112
1385
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
113 // Autodetect bind mount or filesystem type
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
114 if (!type) {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
115 struct stat stdev, stdir;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
116
1385
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
117 if (!stat(dev, &stdev) && !stat(dir, &stdir)
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
118 && ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode))
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
119 || (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode))))
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
120 {
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
121 flags |= MS_BIND;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
122 } else fp = xfopen("/proc/filesystems", "r");
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
123 }
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
124
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 for (;;) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 char *buf = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
127
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 // If type wasn't specified, try all of them in order.
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 if (fp) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 size_t i;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
131
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 if (getline(&buf, &i, fp)<0) break;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 type = buf;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 // skip nodev devices
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 if (!isspace(*type)) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 free(buf);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 continue;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 // trim whitespace
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 while (isspace(*type)) type++;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 i = strlen(type);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 if (i) type[i-1] = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
144 if (toys.optflags & FLAG_v)
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
145 printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 rc = mount(dev, dir, type, flags, opts);
1385
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
147
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
148 // Looking for bind mounts in autodetect above isn't good enough because
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
149 // "mount -t ext2 fs.img dir" is valid, but if you _do_ accept bind mounts
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
150 // with -t how do you tell "-t cifs" isn't looking for a block device if
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
151 // it's not in /proc/filesystems yet because the module that won't be
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
152 // loaded until you try the mount, and if you can't then DEVICE
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
153 // existing as a file would cause a false positive loopback mount.
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
154 //
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
155 // Solution: try mount, let the kernel tell us it wanted a block device,
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
156 // do the loopback setup and retry the mount.
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
157 if (rc && errno == ENOTBLK) {
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
158 char *losetup[] = {"losetup", "-fs", dev, 0};
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
159 int pipes[2], len;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
160 pid_t pid;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
161
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
162 if (flags & MS_RDONLY) losetup[1] = "-fsr";
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
163 pid = xpopen(losetup, pipes);
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
164 len = readall(pipes[1], toybuf, sizeof(toybuf)-1);
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
165 if (!xpclose(pid, pipes) && len > 1) {
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
166 if (toybuf[len-1] == '\n') --len;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
167 toybuf[len] = 0;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
168 dev = toybuf;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
169
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
170 continue;
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
171 } else error_msg("losetup failed %d", len);
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
172 }
ffc015bddb26 Autodetect --bind and --loop mounts in a way that doesn't interfere with network filesystems or -t newtype mounts that trigger a module load.
Rob Landley <rob@landley.net>
parents: 1334
diff changeset
173
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 if (!fp || (rc && errno != EINVAL)) break;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 free(buf);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 if (fp) fclose(fp);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
178
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 if (rc) perror_msg("'%s' on '%s'", dev, dir);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
181
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 void mount_main(void)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 {
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
184 long flags = MS_SILENT;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
185 struct arg_list *o;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
186 char *opts = 0;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
187
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 if (toys.optflags & FLAG_a) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 fprintf(stderr, "not yet\n");
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 return;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
192
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
193 if (toys.optflags & FLAG_r) flags |= MS_RDONLY;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
194 if (toys.optflags & FLAG_w) flags &= ~MS_RDONLY;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
195 for (o = TT.optlist; o; o = o->next)
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
196 flags = parse_opts(o->arg, flags, &opts);
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
197
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 // show mounts
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 if (!toys.optc) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 struct mtab_list *mtl = xgetmountlist(0), *m;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
201
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
202 for (mtl = xgetmountlist(0); mtl && (m = dlist_pop(&mtl)); free(m)) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 char *s = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
204
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 if (TT.type && strcmp(TT.type, m->type)) continue;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 if (*m->device == '/') s = xabspath(m->device, 0);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 xprintf("%s on %s type %s (%s)\n",
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 s ? s : m->device, m->dir, m->type, m->opts);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 free(s);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
211
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 // one argument: from fstab, remount, subtree
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 } else if (toys.optc == 1) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 fprintf(stderr, "not yet\n");
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 return;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 // two arguments
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
217 } else mount_filesystem(toys.optargs[0], toys.optargs[1], TT.type,
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
218 flags, opts ? opts : "");
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }