xref: /dragonfly/sbin/mount_dirfs/mount_dirfs.c (revision c69bf40f)
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/types.h>
37 #include <sys/diskslice.h>
38 #include <sys/diskmbr.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/syslimits.h>
42 #include <sys/mount.h>
43 #include <sys/sysctl.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <stddef.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <uuid.h>
54 #include <err.h>
55 #include <assert.h>
56 #include <ctype.h>
57 #include <mntopts.h>
58 
59 #define MOPT_UPDATE         { "update",     0, MNT_UPDATE, 0 }
60 #define PLATFORM_LEN	16
61 
62 static struct mntopt mopts[] = { MOPT_STDOPTS, MOPT_UPDATE, MOPT_NULL };
63 
64 static void usage(void);
65 
66 int
67 main(int ac, char **av)
68 {
69 	struct vfsconf vfc;
70 	int mount_flags = 0;
71 	int error;
72 	int ch;
73 	int init_flags = 0;
74 	char *mountpt, *hostdir;
75 	size_t vsize;
76 	char platform[PLATFORM_LEN] = {0};
77 
78 	mount_flags = 0;
79 
80 	while ((ch = getopt(ac, av, "o:u")) != -1) {
81 		switch(ch) {
82                 case 'u':
83                         init_flags |= MNT_UPDATE;
84                         break;
85 
86 		case 'o':
87 			getmntopts(optarg, mopts, &mount_flags, NULL);
88 			break;
89 		default:
90 			usage();
91 			/* not reached */
92 		}
93 	}
94 	ac -= optind;
95 	av += optind;
96 	mount_flags |= init_flags;
97 
98 	/*
99 	 * Check we're in a vkernel or abort.
100 	 */
101 	vsize = PLATFORM_LEN;
102 	error = sysctlbyname("hw.platform", &platform, &vsize, NULL,0);
103 	if (error)
104 		errx(1, "Failed to get hw.platform sysctl");
105 
106 	if (strnstr(platform, "vkernel", PLATFORM_LEN) == NULL)
107 		errx(1, "dirfs is only available for vkernels.");
108 
109         /*
110          * Only the mount point need be specified in update mode.
111          */
112         if (init_flags & MNT_UPDATE) {
113                 if (ac != 1) {
114                         usage();
115                         /* not reached */
116                 }
117                 mountpt = av[0];
118                 if (mount(vfc.vfc_name, mountpt, mount_flags, NULL))
119                         err(1, "mountpoint %s", mountpt);
120                 exit(0);
121         }
122 
123 	if (ac < 2) {
124 		usage();
125 		/* not reached */
126 	}
127 
128 	hostdir = av[0];
129 	mountpt = av[1];
130 
131 	/*
132 	 * Load the dirfs module if necessary (this bit stolen from
133 	 * mount_null).
134 	 */
135 	error = getvfsbyname("dirfs", &vfc);
136 	if (error && vfsisloadable("dirfs")) {
137 		if (vfsload("dirfs") != 0)
138 			err(1, "vfsload(dirfs)");
139 		endvfsent();
140 		error = getvfsbyname("dirfs", &vfc);
141 	}
142 	if (error)
143 		errx(1, "dirfs filesystem is not available");
144 
145 	error = mount(vfc.vfc_name, mountpt, mount_flags, hostdir);
146 	if (error)
147 		err(1, "failed to mount %s on %s", hostdir, mountpt);
148 
149 	exit (0);
150 }
151 
152 static
153 void
154 usage(void)
155 {
156 	fprintf(stderr, "usage: mount_dirfs [-u] [-o options] "
157 			"hostdir dir\n");
158 	exit(1);
159 }
160