annotate toys/pending/mount.c @ 1461:67c1402e9723 draft

Fix mount --move bug.
author Rob Landley <rob@landley.net>
date Sun, 07 Sep 2014 19:30:05 -0500
parents 94f7ec50ef50
children
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
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
9 USE_MOUNT(NEWTOY(mount, "?O:afnrvwt: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)
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
21 -O only mount -a entries that have this option
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 -f fake it (don't actually mount)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 -r read only (same as -o ro)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -w read/write (default, same as -o rw)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 -t specify filesystem type
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 -v verbose
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 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
29 as --longopts.
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 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
32 bind mounts (file on file, directory on directory), so you don't need
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
33 to say --bind or --loop. You can also "mount -a /path" to mount everything
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
34 in /etc/fstab under /path, even if it's noauto.
1323
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
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #define FOR_mount
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 #include "toys.h"
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 GLOBALS(
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 struct arg_list *optlist;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 char *type;
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
43 char *bigO;
1323
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 unsigned long flags;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 char *opts;
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
47 int okuser;
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 )
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
50 // TODO detect existing identical mount (procfs with different dev name?)
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
51 // TODO user, users, owner, group, nofail
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
52 // TODO -p (passfd)
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
53 // TODO -a -t notype,type2
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
54 // TODO --subtree
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
55 // TODO --rbind, -R
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
56 // TODO make "mount --bind,ro old new" work (implicit -o remount)
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
57 // TODO mount -a
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
58 // TODO mount -o remount
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
59 // TODO fstab: lookup default options for mount
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
60 // TODO implement -v
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
61 // TODO "mount -a -o remount,ro" should detect overmounts
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
62 // TODO work out how that differs from "mount -ar"
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
63 // TODO what if you --bind mount a block device somewhere (file, dir, dev)
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
64 // TODO "touch servername; mount -t cifs servername path"
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
65
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
66 // Strip flags out of comma separated list of options, return flags,.
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
67 static long flag_opts(char *new, long flags, char **more)
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
68 {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
69 struct {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
70 char *name;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
71 long flags;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
72 } opts[] = {
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
73 // 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
74 {"loop", 0}, {"bind", 0}, {"defaults", 0}, {"quiet", 0},
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
75 {"user", 0}, {"nouser", 0}, // checked in fstab, ignored in -o
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
76 // {"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
77 {"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
78 {"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
79 {"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
80 {"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
81 {"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
82 {"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
83 {"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
84 {"loud", ~MS_SILENT},
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
85 {"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
86 {"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
87 {"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
88 {"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
89 {"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
90 // 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
91 };
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
92
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
93 if (new) for (;;) {
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
94 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
95 int i;
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) *comma = 0;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
98
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
99 // 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
100 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
101 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
102
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
103 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
104 else flags |= ll;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
105
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
106 break;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
107 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
108
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
109 // 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
110 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
111 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
112 *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
113 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
114 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
115 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
116
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
117 if (!comma) break;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
118 *comma = ',';
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
119 new = comma + 1;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
120 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
121
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
122 return flags;
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
123 }
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
124
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
125 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
126 unsigned long flags, char *opts)
1323
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 FILE *fp = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 int rc = EINVAL;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
130
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 if (toys.optflags & FLAG_f) return;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
132
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
133 if (getuid()) {
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
134 if (TT.okuser) TT.okuser = 0;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
135 else {
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
136 error_msg("'%s' not user mountable in fstab");
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
137 return;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
138 }
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
139 }
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
140
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
141 // Autodetect bind mount or filesystem type
1461
67c1402e9723 Fix mount --move bug.
Rob Landley <rob@landley.net>
parents: 1460
diff changeset
142
67c1402e9723 Fix mount --move bug.
Rob Landley <rob@landley.net>
parents: 1460
diff changeset
143 if (type && !strcmp(type, "auto")) type = 0;
67c1402e9723 Fix mount --move bug.
Rob Landley <rob@landley.net>
parents: 1460
diff changeset
144 if (flags & MS_MOVE) {
67c1402e9723 Fix mount --move bug.
Rob Landley <rob@landley.net>
parents: 1460
diff changeset
145 if (type) error_exit("--move with -t");
67c1402e9723 Fix mount --move bug.
Rob Landley <rob@landley.net>
parents: 1460
diff changeset
146 } else if (!type) {
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
147 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
148
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
149 // file on file or dir on dir is a --bind mount.
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
150 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
151 && ((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
152 || (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
153 {
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 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
155 } else fp = xfopen("/proc/filesystems", "r");
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
156 } else if (!strcmp(type, "ignore")) return;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
157 else if (!strcmp(type, "swap"))
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
158 toys.exitval |= xpclose(xpopen((char *[]){"swapon", "--", dev, 0}, 0), 0);
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
159
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 for (;;) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 char *buf = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
162
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 // 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
164 if (fp) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 size_t i;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
166
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 if (getline(&buf, &i, fp)<0) break;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 type = buf;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 // skip nodev devices
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 if (!isspace(*type)) {
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 free(buf);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 continue;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 // trim whitespace
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 while (isspace(*type)) type++;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 i = strlen(type);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 if (i) type[i-1] = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 }
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
179 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
180 printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
181 for (;;) {
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
182 rc = mount(dev, dir, type, flags, opts);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
183 if ((rc != EACCES && rc != EROFS) || (flags & MS_RDONLY)) break;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
184 fprintf(stderr, "'%s' is read-only", dev);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
185 flags |= MS_RDONLY;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
186 }
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
187 free(buf);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
188
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
189 if (!rc) break;
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
190
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
191 // Trying to autodetect loop mounts like bind mounts above (file on dir)
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
192 // isn't good enough because "mount -t ext2 fs.img dir" is valid, but if
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
193 // you _do_ accept loop mounts with -t how do you tell "-t cifs" isn't
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
194 // looking for a block device if it's not in /proc/filesystems yet
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
195 // because the module that won't be loaded until you try the mount, and
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
196 // if you can't then DEVICE existing as a file would cause a false
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
197 // positive loopback mount (so "touch servername" becomes a potential
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
198 // denial of service attack...)
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
199 //
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
200 // Solution: try the mount, let the kernel tell us it wanted a block
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
201 // device, then do the loopback setup and retry the mount.
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
202
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
203 if (fp && errno == EINVAL) continue;
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
204
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
205 if (errno == ENOTBLK) {
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
206 char *losetup[] = {"losetup", "-fs", dev, 0};
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
207 int pipes[2], len;
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
208 pid_t pid;
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
209
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
210 if (flags & MS_RDONLY) losetup[1] = "-fsr";
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
211 pid = xpopen(losetup, pipes);
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
212 len = readall(pipes[1], toybuf, sizeof(toybuf)-1);
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
213 rc = xpclose(pid, pipes);
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
214 if (!rc && len > 1) {
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
215 if (toybuf[len-1] == '\n') --len;
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
216 toybuf[len] = 0;
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
217 dev = toybuf;
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
218
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
219 continue;
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
220 } else error_msg("losetup failed %d", rc);
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
221 } else perror_msg("'%s'->'%s'", dev, dir);
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
222
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
223 break;
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 if (fp) fclose(fp);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
227
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 void mount_main(void)
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 {
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
230 char *opts = 0, *dev = 0, *dir = 0, **ss;
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
231 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
232 struct arg_list *o;
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
233 struct mtab_list *mtl, *mm, *remount = 0;
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
234
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
235 // remount
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
236 // - overmounts
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
237 // shared subtree
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
238 // -o parsed after fstab options
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
239 // test if mountpoint already exists (-o noremount?)
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
240
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
241 // First pass; just accumulate string, don't parse flags yet. (This is so
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
242 // we can modify fstab entries with -a, or mtab with remount.)
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
243 for (o = TT.optlist; o; o = o->next) comma_collate(&opts, o->arg);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
244 if (toys.optflags & FLAG_r) comma_collate(&opts, "ro");
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
245 if (toys.optflags & FLAG_w) comma_collate(&opts, "rw");
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
246
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
247 // Treat each --option as -o option
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
248 for (ss = toys.optargs; *ss; ss++) {
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
249 char *sss = *ss;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
250
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
251 // If you realy, really want to mount a file named "--", we support it.
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
252 if (sss[0]=='-' && sss[1]=='-' && sss[2]) comma_collate(&opts, sss+2);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
253 else if (!dev) dev = sss;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
254 else if (!dir) dir = sss;
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
255 // same message as lib/args.c ">2" which we can't use because --opts count
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
256 else error_exit("Max 2 arguments\n");
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
257 }
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
258
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
259 if ((toys.optflags & FLAG_a) && dir) error_exit("-a with >1 arg");
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
260
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
261 // For remount we need _last_ match (in case of overmounts), so traverse
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
262 // in reverse order.
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
263 if (comma_scan(opts, "remount", 1))
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
264 remount = dlist_terminate(mtl = xgetmountlist("/proc/mounts"));
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
265
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
266 // Do we need to do an /etc/fstab trawl?
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
267 // This covers -a, -o remount, one argument, all user mounts
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
268 if ((toys.optflags & FLAG_a) || (dev && (!dir || getuid() || remount))) {
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
269 if (!remount) mtl = xgetmountlist("/etc/fstab");
1460
94f7ec50ef50 Debugging pass on mount. Not quite done yet, but the basics seem to work now.
Rob Landley <rob@landley.net>
parents: 1453
diff changeset
270
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
271 for (mm = remount ? remount : mtl; mm; mm = (remount ? mm->prev : mm->next))
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
272 {
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
273 int aflags, noauto, len;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
274 char *aopts = 0;
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
275
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
276 // Check for noauto and get it out of the option list. (Unknown options
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
277 // that make it to the kernel give filesystem drivers indigestion.)
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
278 noauto = comma_scan(mm->opts, "noauto", 1);
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
279 if (toys.optflags & FLAG_a) {
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
280 // "mount -a /path" to mount all entries under /path
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
281 if (dev) {
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
282 len = strlen(dev);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
283 if (strncmp(dev, mm->dir, len)
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
284 || (mm->dir[len] && mm->dir[len] != '/')) continue;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
285 } else if (noauto) continue; // never present in the remount case
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
286 if (!mountlist_istype(mm,TT.type) || !comma_scanall(mm->opts,TT.bigO))
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
287 continue;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
288 } else {
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
289 if (dir && strcmp(dir, mm->dir)) continue;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
290 if (dev && strcmp(dev, mm->device) && (dir || strcmp(dev, mm->dir)))
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
291 continue;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
292 }
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
293
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
294 // user only counts from fstab, not opts.
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
295 TT.okuser = comma_scan(mm->opts, "user", 1);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
296 aflags = flag_opts(mm->opts, flags, &aopts);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
297 aflags = flag_opts(opts, aflags, &aopts);
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
298
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
299 mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
300 free(aopts);
1334
9fd2bcedbeb5 mount: start on option parsing, implement loopback and bind mount autodetection.
Rob Landley <rob@landley.net>
parents: 1323
diff changeset
301
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
302 if (!(toys.optflags & FLAG_a)) break;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
303 }
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
304 if (CFG_TOYBOX_FREE) llist_traverse(mtl, free);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
305
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
306 // show mounts from /proc/mounts
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
307 } else if (!dev) {
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
308 for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) {
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 char *s = 0;
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
310
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
311 if (TT.type && strcmp(TT.type, mm->type)) continue;
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
312 if (*mm->device == '/') s = xabspath(mm->device, 0);
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 xprintf("%s on %s type %s (%s)\n",
1447
487716951287 Work in progress snapshot of mount, with fallout to umount. (Not done yet.)
Rob Landley <rob@landley.net>
parents: 1385
diff changeset
314 s ? s : mm->device, mm->dir, mm->type, mm->opts);
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 free(s);
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 }
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
317
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 // two arguments
1453
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
319 } else {
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
320 char *more = 0;
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
321
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
322 mount_filesystem(dev, dir, TT.type, flag_opts(opts, flags, &more), more);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
323 if (CFG_TOYBOX_FREE) free(more);
3c0ed9acbaa3 Next mount checkpoint.
Rob Landley <rob@landley.net>
parents: 1447
diff changeset
324 }
1323
c3061b237c4c First stab at mount, very incomplete.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 }