mount_unionfs.c revision 23680
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1992, 1993, 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software donated to Berkeley by
61558Srgrimes * Jan-Simon Pendry.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimeschar copyright[] =
391558Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#ifndef lint
441558Srgrimesstatic char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
451558Srgrimes#endif /* not lint */
461558Srgrimes
471558Srgrimes#include <sys/param.h>
481558Srgrimes#include <sys/mount.h>
491558Srgrimes
501558Srgrimes#include <miscfs/union/union.h>
511558Srgrimes
521558Srgrimes#include <err.h>
531558Srgrimes#include <stdio.h>
541558Srgrimes#include <stdlib.h>
551558Srgrimes#include <string.h>
5615770Swollman#include <sysexits.h>
571558Srgrimes#include <unistd.h>
581558Srgrimes
591558Srgrimes#include "mntopts.h"
601558Srgrimes
6115770Swollmanstatic struct mntopt mopts[] = {
621558Srgrimes	MOPT_STDOPTS,
631558Srgrimes	{ NULL }
641558Srgrimes};
651558Srgrimes
6615770Swollmanstatic int	subdir __P((const char *, const char *));
6718286Sbdestatic void	usage __P((void)) __dead2;
681558Srgrimes
691558Srgrimesint
701558Srgrimesmain(argc, argv)
711558Srgrimes	int argc;
721558Srgrimes	char *argv[];
731558Srgrimes{
741558Srgrimes	struct union_args args;
751558Srgrimes	int ch, mntflags;
761558Srgrimes	char target[MAXPATHLEN];
7723680Speter	struct vfsconf vfc;
7823680Speter	int error;
791558Srgrimes
801558Srgrimes	mntflags = 0;
811558Srgrimes	args.mntflags = UNMNT_ABOVE;
821558Srgrimes	while ((ch = getopt(argc, argv, "bo:r")) != EOF)
831558Srgrimes		switch (ch) {
841558Srgrimes		case 'b':
851558Srgrimes			args.mntflags &= ~UNMNT_OPMASK;
861558Srgrimes			args.mntflags |= UNMNT_BELOW;
871558Srgrimes			break;
881558Srgrimes		case 'o':
894065Swollman			getmntopts(optarg, mopts, &mntflags, 0);
901558Srgrimes			break;
911558Srgrimes		case 'r':
921558Srgrimes			args.mntflags &= ~UNMNT_OPMASK;
931558Srgrimes			args.mntflags |= UNMNT_REPLACE;
941558Srgrimes			break;
951558Srgrimes		case '?':
961558Srgrimes		default:
971558Srgrimes			usage();
981558Srgrimes			/* NOTREACHED */
991558Srgrimes		}
1001558Srgrimes	argc -= optind;
1011558Srgrimes	argv += optind;
1021558Srgrimes
1031558Srgrimes	if (argc != 2)
1041558Srgrimes		usage();
1051558Srgrimes
1061558Srgrimes	if (realpath(argv[0], target) == 0)
10715770Swollman		err(EX_OSERR, "%s", target);
1081558Srgrimes
1091558Srgrimes	if (subdir(target, argv[1]) || subdir(argv[1], target))
11015770Swollman		errx(EX_USAGE, "%s (%s) and %s are not distinct paths",
1111558Srgrimes		    argv[0], target, argv[1]);
1121558Srgrimes
1131558Srgrimes	args.target = target;
1141558Srgrimes
11523680Speter	error = getvfsbyname("union", &vfc);
11623680Speter	if (error && vfsisloadable("union")) {
11723680Speter		if (vfsload("union"))
11823680Speter			err(EX_OSERR, "vfsload(union)");
1192999Swollman		endvfsent();	/* flush cache */
12023680Speter		error = getvfsbyname("union", &vfc);
1212999Swollman	}
12223680Speter	if (error)
12315770Swollman		errx(EX_OSERR, "union filesystem is not available");
1242999Swollman
12523680Speter	if (mount(vfc.vfc_name, argv[1], mntflags, &args))
12615770Swollman		err(EX_OSERR, target);
1271558Srgrimes	exit(0);
1281558Srgrimes}
1291558Srgrimes
1301558Srgrimesint
1311558Srgrimessubdir(p, dir)
1321558Srgrimes	const char *p;
1331558Srgrimes	const char *dir;
1341558Srgrimes{
1351558Srgrimes	int l;
1361558Srgrimes
1371558Srgrimes	l = strlen(dir);
1381558Srgrimes	if (l <= 1)
1391558Srgrimes		return (1);
1401558Srgrimes
1411558Srgrimes	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
1421558Srgrimes		return (1);
1431558Srgrimes
1441558Srgrimes	return (0);
1451558Srgrimes}
1461558Srgrimes
1471558Srgrimesvoid
1481558Srgrimesusage()
1491558Srgrimes{
1501558Srgrimes	(void)fprintf(stderr,
1511558Srgrimes		"usage: mount_union [-br] [-o options] target_fs mount_point\n");
15215770Swollman	exit(EX_USAGE);
1531558Srgrimes}
154