Mercurial > hg > toybox
annotate toys/pending/mount.c @ 1323:c3061b237c4c draft
First stab at mount, very incomplete.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 29 May 2014 06:49:59 -0500 |
parents | |
children | 9fd2bcedbeb5 |
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 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 USE_MOUNT(NEWTOY(mount, ">2afnrvwt:o*[-rw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT)) |
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 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 config MOUNT_AUTODETECT |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 help |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 usage: mount |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 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
|
37 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
|
38 to say --bind or --loop. |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 */ |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 #define FOR_mount |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 #include "toys.h" |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 GLOBALS( |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 struct arg_list *optlist; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 char *type; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 unsigned long flags; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 char *opts; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 ) |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 static void do_mount(char *dev, char *dir, char *type, unsigned long flags, char *opts) |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 FILE *fp = 0; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 int rc = EINVAL; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 if (toys.optflags & FLAG_f) return; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 if (!TT.type) fp = xfopen("/proc/filesystems", "r"); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 for (;;) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 char *buf = 0; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 // 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
|
65 if (fp) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 size_t i; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 if (getline(&buf, &i, fp)<0) break; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 type = buf; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 // skip nodev devices |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 if (!isspace(*type)) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 free(buf); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 continue; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 // trim whitespace |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 while (isspace(*type)) type++; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
77 i = strlen(type); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
78 if (i) type[i-1] = 0; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 rc = mount(dev, dir, type, flags, opts); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 if (!fp || (rc && errno != EINVAL)) break; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 free(buf); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 if (fp) fclose(fp); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 if (rc) perror_msg("'%s' on '%s'", dev, dir); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 void mount_main(void) |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 if (toys.optflags & FLAG_a) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 fprintf(stderr, "not yet\n"); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 return; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 // show mounts |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
97 if (!toys.optc) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 struct mtab_list *mtl = xgetmountlist(0), *m; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
99 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 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
|
101 char *s = 0; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
103 if (TT.type && strcmp(TT.type, m->type)) continue; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 if (*m->device == '/') s = xabspath(m->device, 0); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 xprintf("%s on %s type %s (%s)\n", |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 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
|
107 free(s); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
108 } |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
109 |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
110 // one argument: from fstab, remount, subtree |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 } else if (toys.optc == 1) { |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 fprintf(stderr, "not yet\n"); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 return; |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 // two arguments |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 } else do_mount(toys.optargs[0], toys.optargs[1], TT.type, 0, ""); |
c3061b237c4c
First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 } |