xref: /dragonfly/sbin/mount_dirfs/mount_dirfs.c (revision e6d22e9b)
1 /*
2  * Copyright (c) 2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Antonio Huete Jimenez <tuxillo@quantumachine.net>
6  * by Matthew Dillon <dillon@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/diskslice.h>
38 #include <sys/diskmbr.h>
39 #include <sys/stat.h>
40 #include <sys/mount.h>
41 #include <sys/sysctl.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <stddef.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <uuid.h>
52 #include <err.h>
53 #include <assert.h>
54 #include <ctype.h>
55 #include <mntopts.h>
56 
57 #define MOPT_UPDATE         { "update",     0, MNT_UPDATE, 0 }
58 #define PLATFORM_LEN	16
59 
60 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_UPDATE, MOPT_NULL };
61 
62 static void usage(void);
63 
64 int
65 main(int ac, char **av)
66 {
67 	struct vfsconf vfc;
68 	int mount_flags = 0;
69 	int error;
70 	int ch;
71 	int init_flags = 0;
72 	char *mountpt, *hostdir;
73 	size_t vsize;
74 	char platform[PLATFORM_LEN] = {0};
75 
76 	mount_flags = 0;
77 
78 	while ((ch = getopt(ac, av, "o:u")) != -1) {
79 		switch(ch) {
80                 case 'u':
81                         init_flags |= MNT_UPDATE;
82                         break;
83 
84 		case 'o':
85 			getmntopts(optarg, mopts, &mount_flags, NULL);
86 			break;
87 		default:
88 			usage();
89 			/* not reached */
90 		}
91 	}
92 	ac -= optind;
93 	av += optind;
94 	mount_flags |= init_flags;
95 
96 	/*
97 	 * Check we're in a vkernel or abort.
98 	 */
99 	vsize = PLATFORM_LEN;
100 	error = sysctlbyname("hw.platform", &platform, &vsize, NULL,0);
101 	if (error)
102 		errx(1, "Failed to get hw.platform sysctl");
103 
104 	if (strnstr(platform, "vkernel", PLATFORM_LEN) == NULL)
105 		errx(1, "dirfs is only available for vkernels.");
106 
107         /*
108          * Only the mount point need be specified in update mode.
109          */
110         if (init_flags & MNT_UPDATE) {
111                 if (ac != 1) {
112                         usage();
113                         /* not reached */
114                 }
115                 mountpt = av[0];
116                 if (mount(vfc.vfc_name, mountpt, mount_flags, NULL))
117                         err(1, "mountpoint %s", mountpt);
118                 exit(0);
119         }
120 
121 	if (ac < 2) {
122 		usage();
123 		/* not reached */
124 	}
125 
126 	hostdir = av[0];
127 	mountpt = av[1];
128 
129 	/*
130 	 * Load the dirfs module if necessary (this bit stolen from
131 	 * mount_null).
132 	 */
133 	error = getvfsbyname("dirfs", &vfc);
134 	if (error && vfsisloadable("dirfs")) {
135 		if (vfsload("dirfs") != 0)
136 			err(1, "vfsload(dirfs)");
137 		endvfsent();
138 		error = getvfsbyname("dirfs", &vfc);
139 	}
140 	if (error)
141 		errx(1, "dirfs filesystem is not available");
142 
143 	error = mount(vfc.vfc_name, mountpt, mount_flags, hostdir);
144 	if (error)
145 		err(1, "failed to mount %s on %s", hostdir, mountpt);
146 
147 	exit (0);
148 }
149 
150 static
151 void
152 usage(void)
153 {
154 	fprintf(stderr, "usage: mount_dirfs [-u] [-o options] "
155 			"hostdir dir\n");
156 	exit(1);
157 }
158