xref: /dragonfly/sbin/mount_dirfs/mount_dirfs.c (revision 804580cb)
1509bc517SAntonio Huete Jimenez /*
2509bc517SAntonio Huete Jimenez  * Copyright (c) 2013 The DragonFly Project.  All rights reserved.
3509bc517SAntonio Huete Jimenez  *
4509bc517SAntonio Huete Jimenez  * This code is derived from software contributed to The DragonFly Project
5509bc517SAntonio Huete Jimenez  * by Antonio Huete Jimenez <tuxillo@quantumachine.net>
6509bc517SAntonio Huete Jimenez  * by Matthew Dillon <dillon@dragonflybsd.org>
7509bc517SAntonio Huete Jimenez  *
8509bc517SAntonio Huete Jimenez  * Redistribution and use in source and binary forms, with or without
9509bc517SAntonio Huete Jimenez  * modification, are permitted provided that the following conditions
10509bc517SAntonio Huete Jimenez  * are met:
11509bc517SAntonio Huete Jimenez  *
12509bc517SAntonio Huete Jimenez  * 1. Redistributions of source code must retain the above copyright
13509bc517SAntonio Huete Jimenez  *    notice, this list of conditions and the following disclaimer.
14509bc517SAntonio Huete Jimenez  * 2. Redistributions in binary form must reproduce the above copyright
15509bc517SAntonio Huete Jimenez  *    notice, this list of conditions and the following disclaimer in
16509bc517SAntonio Huete Jimenez  *    the documentation and/or other materials provided with the
17509bc517SAntonio Huete Jimenez  *    distribution.
18509bc517SAntonio Huete Jimenez  * 3. Neither the name of The DragonFly Project nor the names of its
19509bc517SAntonio Huete Jimenez  *    contributors may be used to endorse or promote products derived
20509bc517SAntonio Huete Jimenez  *    from this software without specific, prior written permission.
21509bc517SAntonio Huete Jimenez  *
22509bc517SAntonio Huete Jimenez  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23509bc517SAntonio Huete Jimenez  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24509bc517SAntonio Huete Jimenez  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25509bc517SAntonio Huete Jimenez  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26509bc517SAntonio Huete Jimenez  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27509bc517SAntonio Huete Jimenez  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28509bc517SAntonio Huete Jimenez  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29509bc517SAntonio Huete Jimenez  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30509bc517SAntonio Huete Jimenez  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31509bc517SAntonio Huete Jimenez  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32509bc517SAntonio Huete Jimenez  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33509bc517SAntonio Huete Jimenez  * SUCH DAMAGE.
34509bc517SAntonio Huete Jimenez  */
35509bc517SAntonio Huete Jimenez 
36*804580cbSSascha Wildner #include <sys/param.h>
37509bc517SAntonio Huete Jimenez #include <sys/diskslice.h>
38509bc517SAntonio Huete Jimenez #include <sys/diskmbr.h>
39509bc517SAntonio Huete Jimenez #include <sys/stat.h>
40509bc517SAntonio Huete Jimenez #include <sys/mount.h>
41509bc517SAntonio Huete Jimenez #include <sys/sysctl.h>
42509bc517SAntonio Huete Jimenez 
43509bc517SAntonio Huete Jimenez #include <stdio.h>
44509bc517SAntonio Huete Jimenez #include <stdlib.h>
45509bc517SAntonio Huete Jimenez #include <stdarg.h>
46509bc517SAntonio Huete Jimenez #include <stddef.h>
47509bc517SAntonio Huete Jimenez #include <unistd.h>
48509bc517SAntonio Huete Jimenez #include <string.h>
49509bc517SAntonio Huete Jimenez #include <errno.h>
50509bc517SAntonio Huete Jimenez #include <fcntl.h>
51509bc517SAntonio Huete Jimenez #include <uuid.h>
52509bc517SAntonio Huete Jimenez #include <err.h>
53509bc517SAntonio Huete Jimenez #include <assert.h>
54509bc517SAntonio Huete Jimenez #include <ctype.h>
55509bc517SAntonio Huete Jimenez #include <mntopts.h>
56509bc517SAntonio Huete Jimenez 
57509bc517SAntonio Huete Jimenez #define MOPT_UPDATE         { "update",     0, MNT_UPDATE, 0 }
58509bc517SAntonio Huete Jimenez #define PLATFORM_LEN	16
59509bc517SAntonio Huete Jimenez 
60509bc517SAntonio Huete Jimenez static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_UPDATE, MOPT_NULL };
61509bc517SAntonio Huete Jimenez 
62509bc517SAntonio Huete Jimenez static void usage(void);
63509bc517SAntonio Huete Jimenez 
64509bc517SAntonio Huete Jimenez int
main(int ac,char ** av)65509bc517SAntonio Huete Jimenez main(int ac, char **av)
66509bc517SAntonio Huete Jimenez {
67509bc517SAntonio Huete Jimenez 	struct vfsconf vfc;
68509bc517SAntonio Huete Jimenez 	int mount_flags = 0;
69509bc517SAntonio Huete Jimenez 	int error;
70509bc517SAntonio Huete Jimenez 	int ch;
71509bc517SAntonio Huete Jimenez 	int init_flags = 0;
72509bc517SAntonio Huete Jimenez 	char *mountpt, *hostdir;
73509bc517SAntonio Huete Jimenez 	size_t vsize;
74509bc517SAntonio Huete Jimenez 	char platform[PLATFORM_LEN] = {0};
75509bc517SAntonio Huete Jimenez 
76509bc517SAntonio Huete Jimenez 	mount_flags = 0;
77509bc517SAntonio Huete Jimenez 
78509bc517SAntonio Huete Jimenez 	while ((ch = getopt(ac, av, "o:u")) != -1) {
79509bc517SAntonio Huete Jimenez 		switch(ch) {
80509bc517SAntonio Huete Jimenez                 case 'u':
81509bc517SAntonio Huete Jimenez                         init_flags |= MNT_UPDATE;
82509bc517SAntonio Huete Jimenez                         break;
83509bc517SAntonio Huete Jimenez 
84509bc517SAntonio Huete Jimenez 		case 'o':
85509bc517SAntonio Huete Jimenez 			getmntopts(optarg, mopts, &mount_flags, NULL);
86509bc517SAntonio Huete Jimenez 			break;
87509bc517SAntonio Huete Jimenez 		default:
88509bc517SAntonio Huete Jimenez 			usage();
89509bc517SAntonio Huete Jimenez 			/* not reached */
90509bc517SAntonio Huete Jimenez 		}
91509bc517SAntonio Huete Jimenez 	}
92509bc517SAntonio Huete Jimenez 	ac -= optind;
93509bc517SAntonio Huete Jimenez 	av += optind;
94509bc517SAntonio Huete Jimenez 	mount_flags |= init_flags;
95509bc517SAntonio Huete Jimenez 
96509bc517SAntonio Huete Jimenez 	/*
97509bc517SAntonio Huete Jimenez 	 * Check we're in a vkernel or abort.
98509bc517SAntonio Huete Jimenez 	 */
99509bc517SAntonio Huete Jimenez 	vsize = PLATFORM_LEN;
100509bc517SAntonio Huete Jimenez 	error = sysctlbyname("hw.platform", &platform, &vsize, NULL,0);
101509bc517SAntonio Huete Jimenez 	if (error)
102509bc517SAntonio Huete Jimenez 		errx(1, "Failed to get hw.platform sysctl");
103509bc517SAntonio Huete Jimenez 
104509bc517SAntonio Huete Jimenez 	if (strnstr(platform, "vkernel", PLATFORM_LEN) == NULL)
105509bc517SAntonio Huete Jimenez 		errx(1, "dirfs is only available for vkernels.");
106509bc517SAntonio Huete Jimenez 
107509bc517SAntonio Huete Jimenez         /*
108509bc517SAntonio Huete Jimenez          * Only the mount point need be specified in update mode.
109509bc517SAntonio Huete Jimenez          */
110509bc517SAntonio Huete Jimenez         if (init_flags & MNT_UPDATE) {
111509bc517SAntonio Huete Jimenez                 if (ac != 1) {
112509bc517SAntonio Huete Jimenez                         usage();
113509bc517SAntonio Huete Jimenez                         /* not reached */
114509bc517SAntonio Huete Jimenez                 }
115509bc517SAntonio Huete Jimenez                 mountpt = av[0];
116509bc517SAntonio Huete Jimenez                 if (mount(vfc.vfc_name, mountpt, mount_flags, NULL))
117509bc517SAntonio Huete Jimenez                         err(1, "mountpoint %s", mountpt);
118509bc517SAntonio Huete Jimenez                 exit(0);
119509bc517SAntonio Huete Jimenez         }
120509bc517SAntonio Huete Jimenez 
121509bc517SAntonio Huete Jimenez 	if (ac < 2) {
122509bc517SAntonio Huete Jimenez 		usage();
123509bc517SAntonio Huete Jimenez 		/* not reached */
124509bc517SAntonio Huete Jimenez 	}
125509bc517SAntonio Huete Jimenez 
126509bc517SAntonio Huete Jimenez 	hostdir = av[0];
127509bc517SAntonio Huete Jimenez 	mountpt = av[1];
128509bc517SAntonio Huete Jimenez 
129509bc517SAntonio Huete Jimenez 	/*
130509bc517SAntonio Huete Jimenez 	 * Load the dirfs module if necessary (this bit stolen from
131509bc517SAntonio Huete Jimenez 	 * mount_null).
132509bc517SAntonio Huete Jimenez 	 */
133509bc517SAntonio Huete Jimenez 	error = getvfsbyname("dirfs", &vfc);
134509bc517SAntonio Huete Jimenez 	if (error && vfsisloadable("dirfs")) {
135509bc517SAntonio Huete Jimenez 		if (vfsload("dirfs") != 0)
136509bc517SAntonio Huete Jimenez 			err(1, "vfsload(dirfs)");
137509bc517SAntonio Huete Jimenez 		endvfsent();
138509bc517SAntonio Huete Jimenez 		error = getvfsbyname("dirfs", &vfc);
139509bc517SAntonio Huete Jimenez 	}
140509bc517SAntonio Huete Jimenez 	if (error)
141509bc517SAntonio Huete Jimenez 		errx(1, "dirfs filesystem is not available");
142509bc517SAntonio Huete Jimenez 
143509bc517SAntonio Huete Jimenez 	error = mount(vfc.vfc_name, mountpt, mount_flags, hostdir);
144509bc517SAntonio Huete Jimenez 	if (error)
145509bc517SAntonio Huete Jimenez 		err(1, "failed to mount %s on %s", hostdir, mountpt);
146509bc517SAntonio Huete Jimenez 
147509bc517SAntonio Huete Jimenez 	exit (0);
148509bc517SAntonio Huete Jimenez }
149509bc517SAntonio Huete Jimenez 
150509bc517SAntonio Huete Jimenez static
151509bc517SAntonio Huete Jimenez void
usage(void)152509bc517SAntonio Huete Jimenez usage(void)
153509bc517SAntonio Huete Jimenez {
154509bc517SAntonio Huete Jimenez 	fprintf(stderr, "usage: mount_dirfs [-u] [-o options] "
155509bc517SAntonio Huete Jimenez 			"hostdir dir\n");
156509bc517SAntonio Huete Jimenez 	exit(1);
157509bc517SAntonio Huete Jimenez }
158