mount_fs.c revision 216399
1139743Simp/*
2123474Swpaul * Copyright (c) 1992, 1993, 1994
3123474Swpaul *	The Regents of the University of California.  All rights reserved.
4123474Swpaul *
5123474Swpaul * This code is derived from software donated to Berkeley by
6123474Swpaul * Jan-Simon Pendry.
7123474Swpaul *
8123474Swpaul * Redistribution and use in source and binary forms, with or without
9123474Swpaul * modification, are permitted provided that the following conditions
10123474Swpaul * are met:
11123474Swpaul * 1. Redistributions of source code must retain the above copyright
12123474Swpaul *    notice, this list of conditions and the following disclaimer.
13123474Swpaul * 2. Redistributions in binary form must reproduce the above copyright
14123474Swpaul *    notice, this list of conditions and the following disclaimer in the
15123474Swpaul *    documentation and/or other materials provided with the distribution.
16123474Swpaul * 4. Neither the name of the University nor the names of its contributors
17123474Swpaul *    may be used to endorse or promote products derived from this software
18123474Swpaul *    without specific prior written permission.
19123474Swpaul *
20123474Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21123474Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22123474Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23123474Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24123474Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25123474Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26123474Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27123474Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28123474Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29123474Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30123474Swpaul * SUCH DAMAGE.
31123474Swpaul */
32123474Swpaul
33123474Swpaul#ifndef lint
34123474Swpaulstatic const char copyright[] =
35123474Swpaul"@(#) Copyright (c) 1992, 1993, 1994\n\
36189488Sweongyo	The Regents of the University of California.  All rights reserved.\n";
37123474Swpaul#endif /* not lint */
38123474Swpaul
39123474Swpaul#ifndef lint
40123474Swpaul#if 0
41123474Swpaulstatic char sccsid[] = "@(#)mount_fs.c	8.6 (Berkeley) 4/26/95";
42123474Swpaul#endif
43123474Swpaulstatic const char rcsid[] =
44123474Swpaul	"$FreeBSD: head/sbin/mount/mount_fs.c 216399 2010-12-12 21:26:12Z joel $";
45123474Swpaul#endif /* not lint */
46123474Swpaul
47123474Swpaul#include <sys/param.h>
48123474Swpaul#include <sys/mount.h>
49124582Sobrien
50123474Swpaul#include <err.h>
51123474Swpaul#include <getopt.h>
52123474Swpaul#include <libgen.h>
53123474Swpaul#include <stdio.h>
54123474Swpaul#include <stdlib.h>
55123474Swpaul#include <string.h>
56123474Swpaul#include <unistd.h>
57123474Swpaul
58189488Sweongyo#include "extern.h"
59189488Sweongyo#include "mntopts.h"
60189488Sweongyo
61189488Sweongyostruct mntopt mopts[] = {
62189488Sweongyo	MOPT_STDOPTS,
63189488Sweongyo	MOPT_END
64189488Sweongyo};
65189488Sweongyo
66189488Sweongyostatic void
67189488Sweongyousage(void)
68189488Sweongyo{
69189488Sweongyo	(void)fprintf(stderr,
70189488Sweongyo		"usage: mount [-t fstype] [-o options] target_fs mount_point\n");
71189488Sweongyo	exit(1);
72189488Sweongyo}
73189488Sweongyo
74189488Sweongyoint
75189488Sweongyomount_fs(const char *vfstype, int argc, char *argv[])
76189488Sweongyo{
77189488Sweongyo	struct iovec *iov;
78189488Sweongyo	int iovlen;
79189488Sweongyo	int mntflags = 0;
80189488Sweongyo	int ch;
81189488Sweongyo	char *dev, *dir, mntpath[MAXPATHLEN];
82189488Sweongyo	char fstype[32];
83189488Sweongyo	char errmsg[255];
84189488Sweongyo	char *p, *val;
85189488Sweongyo	int ret;
86189488Sweongyo
87189488Sweongyo	strlcpy(fstype, vfstype, sizeof(fstype));
88189488Sweongyo	memset(errmsg, 0, sizeof(errmsg));
89189488Sweongyo
90189488Sweongyo	getmnt_silent = 1;
91189488Sweongyo	iov = NULL;
92189488Sweongyo	iovlen = 0;
93189488Sweongyo
94189488Sweongyo	optind = optreset = 1;		/* Reset for parse of new argv. */
95189488Sweongyo	while ((ch = getopt(argc, argv, "o:")) != -1) {
96189488Sweongyo		switch(ch) {
97189488Sweongyo		case 'o':
98189488Sweongyo			getmntopts(optarg, mopts, &mntflags, 0);
99189488Sweongyo			p = strchr(optarg, '=');
100189488Sweongyo			val = NULL;
101189488Sweongyo			if (p != NULL) {
102189488Sweongyo				*p = '\0';
103189488Sweongyo				val = p + 1;
104189488Sweongyo			}
105189488Sweongyo			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
106189488Sweongyo			break;
107189488Sweongyo		case '?':
108189488Sweongyo		default:
109189488Sweongyo			usage();
110189488Sweongyo		}
111189488Sweongyo	}
112189488Sweongyo
113189488Sweongyo	argc -= optind;
114189488Sweongyo	argv += optind;
115189488Sweongyo	if (argc != 2)
116189488Sweongyo		usage();
117189488Sweongyo
118189488Sweongyo	dev = argv[0];
119189488Sweongyo	dir = argv[1];
120189488Sweongyo
121189488Sweongyo	(void)checkpath(dir, mntpath);
122189488Sweongyo	(void)rmslashes(dev, dev);
123189488Sweongyo
124189488Sweongyo	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
125189488Sweongyo	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
126189488Sweongyo	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
127189488Sweongyo	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
128189488Sweongyo
129189488Sweongyo	ret = nmount(iov, iovlen, mntflags);
130189488Sweongyo	if (ret < 0)
131189488Sweongyo		err(1, "%s %s", dev, errmsg);
132189488Sweongyo
133189488Sweongyo	return (ret);
134123474Swpaul}
135123474Swpaul