mount: support "-O option"; stop trying to mount swap partitions

function                                             old     new   delta
mount_main                                           975    1152    +177
umount_main                                          640     636      -4
packed_usage                                       25666   25662      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 177/-8)            Total: 169 bytes
This commit is contained in:
Denis Vlasenko 2009-03-14 22:57:20 +00:00
parent 447ab18cf6
commit 7aaedcf21e
4 changed files with 137 additions and 61 deletions

View File

@ -2796,45 +2796,50 @@
"$ dmesg | more\n" "$ dmesg | more\n"
#define mount_trivial_usage \ #define mount_trivial_usage \
"[flags] DEVICE NODE [-o options,more-options]" "[flags] DEVICE NODE [-o OPT,OPT]"
#define mount_full_usage "\n\n" \ #define mount_full_usage "\n\n" \
"Mount a filesystem. Filesystem autodetection requires /proc be mounted.\n" \ "Mount a filesystem. Filesystem autodetection requires /proc be mounted.\n" \
"\nOptions:" \ "\nOptions:" \
"\n -a Mount all filesystems in fstab" \ "\n -a Mount all filesystems in fstab" \
USE_FEATURE_MOUNT_FAKE( \ USE_FEATURE_MOUNT_FAKE( \
"\n -f "USE_FEATURE_MTAB_SUPPORT("Update /etc/mtab, but ")"don't mount" \ USE_FEATURE_MTAB_SUPPORT( \
"\n -f Update /etc/mtab, but don't mount" \
) \
SKIP_FEATURE_MTAB_SUPPORT( \
"\n -f Dry run" \
) \
) \ ) \
USE_FEATURE_MTAB_SUPPORT( \ USE_FEATURE_MTAB_SUPPORT( \
"\n -n Don't update /etc/mtab" \ "\n -n Don't update /etc/mtab" \
) \ ) \
"\n -r Read-only mount" \ "\n -r Read-only mount" \
"\n -t fs-type Filesystem type" \
"\n -w Read-write mount (default)" \ "\n -w Read-write mount (default)" \
"\n" \ "\n -t FSTYPE Filesystem type" \
"-o option:\n" \ "\n -O OPT Mount only filesystems with option OPT (-a only)" \
"\n-o OPT:" \
USE_FEATURE_MOUNT_LOOP( \ USE_FEATURE_MOUNT_LOOP( \
" loop Ignored (loop devices are autodetected)\n" \ "\n loop Ignored (loop devices are autodetected)" \
) \ ) \
USE_FEATURE_MOUNT_FLAGS( \ USE_FEATURE_MOUNT_FLAGS( \
" [a]sync Writes are asynchronous / synchronous\n" \ "\n [a]sync Writes are [a]synchronous" \
" [no]atime Disable / enable updates to inode access times\n" \ "\n [no]atime Disable/enable updates to inode access times" \
" [no]diratime Disable / enable atime updates to directories\n" \ "\n [no]diratime Disable/enable atime updates to directories" \
" [no]relatime Disable / enable atime updates relative to modification time\n" \ "\n [no]relatime Disable/enable atime updates relative to modification time" \
" [no]dev Allow use of special device files / disallow them\n" \ "\n [no]dev (Dis)allow use of special device files" \
" [no]exec Allow use of executable files / disallow them\n" \ "\n [no]exec (Dis)allow use of executable files" \
" [no]suid Allow set-user-id-root programs / disallow them\n" \ "\n [no]suid (Dis)allow set-user-id-root programs" \
" [r]shared Convert [recursively] to a shared subtree\n" \ "\n [r]shared Convert [recursively] to a shared subtree" \
" [r]slave Convert [recursively] to a slave subtree\n" \ "\n [r]slave Convert [recursively] to a slave subtree" \
" [r]private Convert [recursively] to a private subtree\n" \ "\n [r]private Convert [recursively] to a private subtree" \
" [un]bindable Make mount point [un]able to be bind mounted\n" \ "\n [un]bindable Make mount point [un]able to be bind mounted" \
" bind Bind a directory to an additional location\n" \ "\n bind Bind a directory to an additional location" \
" move Relocate an existing mount point\n" \ "\n move Relocate an existing mount point" \
) \ ) \
" remount Remount a mounted filesystem, changing its flags\n" \ "\n remount Remount a mounted filesystem, changing its flags" \
" ro/rw Mount for read-only / read-write\n" \ "\n ro/rw Read-only/read-write mount" \
"\n" \ "\n" \
"There are EVEN MORE flags that are specific to each filesystem\n" \ "\nThere are EVEN MORE flags that are specific to each filesystem" \
"You'll have to see the written documentation for those filesystems" \ "\nYou'll have to see the written documentation for those filesystems" \
#define mount_example_usage \ #define mount_example_usage \
"$ mount\n" \ "$ mount\n" \

View File

@ -5,40 +5,38 @@
* This allows us to match fstypes that start with no like so * This allows us to match fstypes that start with no like so
* mount -at ,noddy * mount -at ,noddy
* *
* Returns 0 for a match, otherwise -1 * Returns 1 for a match, otherwise 0
* *
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include "libbb.h" #include "libbb.h"
int FAST_FUNC match_fstype(const struct mntent *mt, const char *fstype) int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
{ {
int no = 0; int match = 1;
int len; int len;
if (!mt) if (!t_fstype)
return -1; return match;
if (!fstype) if (t_fstype[0] == 'n' && t_fstype[1] == 'o') {
return 0; match--;
t_fstype += 2;
if (fstype[0] == 'n' && fstype[1] == 'o') {
no = -1;
fstype += 2;
} }
len = strlen(mt->mnt_type); len = strlen(mt->mnt_type);
while (fstype) { while (1) {
if (!strncmp(mt->mnt_type, fstype, len) if (strncmp(mt->mnt_type, t_fstype, len) == 0
&& (!fstype[len] || fstype[len] == ',') && (t_fstype[len] == '\0' || t_fstype[len] == ',')
) { ) {
return no; return match;
} }
fstype = strchr(fstype, ','); t_fstype = strchr(t_fstype, ',');
if (fstype) if (!t_fstype)
fstype++; break;
t_fstype++;
} }
return -(no + 1); return !match;
} }

View File

@ -63,7 +63,7 @@ enum {
}; };
#define OPTION_STR "o:t:rwanfvsi" #define OPTION_STR "o:t:rwanfvsiO:"
enum { enum {
OPT_o = (1 << 0), OPT_o = (1 << 0),
OPT_t = (1 << 1), OPT_t = (1 << 1),
@ -75,6 +75,7 @@ enum {
OPT_v = (1 << 7), OPT_v = (1 << 7),
OPT_s = (1 << 8), OPT_s = (1 << 8),
OPT_i = (1 << 9), OPT_i = (1 << 9),
OPT_O = (1 << 10),
}; };
#if ENABLE_FEATURE_MTAB_SUPPORT #if ENABLE_FEATURE_MTAB_SUPPORT
@ -1721,6 +1722,45 @@ static int singlemount(struct mntent *mp, int ignore_busy)
return rc; return rc;
} }
/* -O support
* Unlike -t, -O should interpret "no" prefix differently:
* -t noa,b,c = -t no(a,b,c) = mount all except fs'es with types a,b, and c
* -O noa,b,c = -O noa,b,c = mount all with without option a,
* or with option b or c.
* But for now we do not support -O a,b,c at all (only -O a).
*
* Another difference from -t support (match_fstype) is that
* we need to examine the _list_ of options in fsopt, not just a string.
*/
static int match_opt(const char *fs_opt, const char *O_opt)
{
int match = 1;
int len;
if (!O_opt)
return match;
if (O_opt[0] == 'n' && O_opt[1] == 'o') {
match--;
O_opt += 2;
}
len = strlen(O_opt);
while (1) {
if (strncmp(fs_opt, O_opt, len) == 0
&& (fs_opt[len] == '\0' || fs_opt[len] == ',')
) {
return match;
}
fs_opt = strchr(fs_opt, ',');
if (!fs_opt)
break;
fs_opt++;
}
return !match;
}
// Parse options, if necessary parse fstab/mtab, and call singlemount for // Parse options, if necessary parse fstab/mtab, and call singlemount for
// each directory to be mounted. // each directory to be mounted.
static const char must_be_root[] ALIGN1 = "you must be root"; static const char must_be_root[] ALIGN1 = "you must be root";
@ -1728,8 +1768,9 @@ static const char must_be_root[] ALIGN1 = "you must be root";
int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mount_main(int argc UNUSED_PARAM, char **argv) int mount_main(int argc UNUSED_PARAM, char **argv)
{ {
char *cmdopts = xstrdup(""); char *cmdopts = xzalloc(1);
char *fstype = NULL; char *fstype = NULL;
char *O_optmatch = NULL;
char *storage_path; char *storage_path;
llist_t *lst_o = NULL; llist_t *lst_o = NULL;
const char *fstabname; const char *fstabname;
@ -1752,9 +1793,9 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
argv[j] = NULL; argv[j] = NULL;
// Parse remaining options // Parse remaining options
// Max 2 params; -v is a counter // Max 2 params; -o is a list, -v is a counter
opt_complementary = "?2o::" USE_FEATURE_MOUNT_VERBOSE(":vv"); opt_complementary = "?2o::" USE_FEATURE_MOUNT_VERBOSE("vv");
opt = getopt32(argv, OPTION_STR, &lst_o, &fstype opt = getopt32(argv, OPTION_STR, &lst_o, &fstype, &O_optmatch
USE_FEATURE_MOUNT_VERBOSE(, &verbose)); USE_FEATURE_MOUNT_VERBOSE(, &verbose));
while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
@ -1807,7 +1848,7 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
// Past this point, we are handling either "mount -a [opts]" // Past this point, we are handling either "mount -a [opts]"
// or "mount [opts] single_param" // or "mount [opts] single_param"
i = parse_mount_options(cmdopts, 0); // FIXME: should be "long", not "int" i = parse_mount_options(cmdopts, NULL); // FIXME: should be "long", not "int"
if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
bb_error_msg_and_die(must_be_root); bb_error_msg_and_die(must_be_root);
@ -1865,20 +1906,29 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
// If we're mounting all // If we're mounting all
} else { } else {
// Do we need to match a filesystem type?
if (fstype && match_fstype(mtcur, fstype))
continue;
// Skip noauto and swap anyway.
if (parse_mount_options(mtcur->mnt_opts, 0) & (MOUNT_NOAUTO | MOUNT_SWAP))
continue;
// No, mount -a won't mount anything, // No, mount -a won't mount anything,
// even user mounts, for mere humans // even user mounts, for mere humans
if (nonroot) if (nonroot)
bb_error_msg_and_die(must_be_root); bb_error_msg_and_die(must_be_root);
resolve_mount_spec(&mtpair->mnt_fsname); // Does type match? (NULL matches always)
if (!match_fstype(mtcur, fstype))
continue;
// Skip noauto and swap anyway.
if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
// swap is bogus "fstype", parse_mount_options can't check fstypes
|| strcasecmp(mtcur->mnt_type, "swap") == 0
) {
continue;
}
// Does (at least one) option match?
// (NULL matches always)
if (!match_opt(mtcur->mnt_opts, O_optmatch))
continue;
resolve_mount_spec(&mtcur->mnt_fsname);
// NFS mounts want this to be xrealloc-able // NFS mounts want this to be xrealloc-able
mtcur->mnt_opts = xstrdup(mtcur->mnt_opts); mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
@ -1895,13 +1945,35 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
// End of fstab/mtab is reached. // End of fstab/mtab is reached.
// Were we looking for something specific? // Were we looking for something specific?
if (argv[0]) { if (argv[0]) {
long l;
// If we didn't find anything, complain // If we didn't find anything, complain
if (!mtcur->mnt_fsname) if (!mtcur->mnt_fsname)
bb_error_msg_and_die("can't find %s in %s", bb_error_msg_and_die("can't find %s in %s",
argv[0], fstabname); argv[0], fstabname);
// What happens when we try to "mount swap_partition"?
// (fstab containts "swap_partition swap swap defaults 0 0")
// util-linux-ng 2.13.1 does this:
// stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
// mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
// lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
// write(2, "mount: mount point swap does not exist\n", 39) = 39
// exit_group(32) = ?
#if 0
// In case we want to simply skip swap partitions:
l = parse_mount_options(mtcur->mnt_opts, NULL);
if ((l & MOUNT_SWAP)
// swap is bogus "fstype", parse_mount_options can't check fstypes
|| strcasecmp(mtcur->mnt_type, "swap") == 0
) {
goto ret;
}
#endif
if (nonroot) { if (nonroot) {
// fstab must have "users" or "user" // fstab must have "users" or "user"
if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS)) l = parse_mount_options(mtcur->mnt_opts, NULL);
if (!(l & MOUNT_USERS))
bb_error_msg_and_die(must_be_root); bb_error_msg_and_die(must_be_root);
} }
@ -1914,6 +1986,7 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
free(mtcur->mnt_opts); free(mtcur->mnt_opts);
} }
//ret:
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
endmntent(fstab); endmntent(fstab);
if (ENABLE_FEATURE_CLEAN_UP) { if (ENABLE_FEATURE_CLEAN_UP) {

View File

@ -73,9 +73,9 @@ int umount_main(int argc UNUSED_PARAM, char **argv)
} else { } else {
while (getmntent_r(fp, &me, path, PATH_MAX)) { while (getmntent_r(fp, &me, path, PATH_MAX)) {
/* Match fstype if passed */ /* Match fstype if passed */
if (fstype && match_fstype(&me, fstype)) if (!match_fstype(&me, fstype))
continue; continue;
m = xmalloc(sizeof(struct mtab_list)); m = xzalloc(sizeof(*m));
m->next = mtl; m->next = mtl;
m->device = xstrdup(me.mnt_fsname); m->device = xstrdup(me.mnt_fsname);
m->dir = xstrdup(me.mnt_dir); m->dir = xstrdup(me.mnt_dir);