2 * Copyright (c) 1999 Michael Smith
4 * Copyright (c) 1999 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Locate and mount the root filesystem.
34 * The root filesystem is detailed in the kernel environment variable
35 * vfs.root.mountfrom, which is expected to be in the general format
38 * vfsname := the name of a VFS known to the kernel and capable
39 * of being mounted as root
40 * path := disk device name or other data used by the filesystem
41 * to locate its physical store
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/malloc.h>
52 #include <sys/reboot.h>
53 #include <sys/diskslice.h>
54 #include <sys/disklabel.h>
58 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
60 #define ROOTNAME "root_device"
62 struct vnode *rootvnode;
64 static void vfs_mountroot(void *junk);
65 static int vfs_mountroot_try(char *mountfrom);
66 static int vfs_mountroot_ask(void);
67 static void gets(char *cp);
69 /* legacy find-root code */
70 char *rootdevnames[2] = {NULL, NULL};
71 static int setrootbyname(char *name);
73 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, vfs_mountroot, NULL);
76 * Find and mount the root filesystem
79 vfs_mountroot(void *junk)
82 * The root filesystem information is compiled in, and we are
83 * booted with instructions to use it.
86 if ((boothowto & RB_DFLTROOT) &&
87 !vfs_mountroot_try(ROOTDEVNAME))
91 * We are booted with instructions to prompt for the root filesystem,
92 * or to use the compiled-in default when it doesn't exist.
94 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
95 if (!vfs_mountroot_ask())
100 * Try to use the value read by the loader from /etc/fstab, or
101 * supplied via some other means. This is the preferred
104 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
108 * Try values that may have been computed by the machine-dependant
111 if (!vfs_mountroot_try(rootdevnames[0]))
113 if (!vfs_mountroot_try(rootdevnames[1]))
117 * If we have a compiled-in default, and haven't already tried it, try
121 if (!(boothowto & RB_DFLTROOT))
122 !vfs_mountroot_try(ROOTDEVNAME))
127 * Everything so far has failed, prompt on the console if we haven't
128 * already tried that.
130 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
132 panic("Root mount failed, startup aborted.");
136 * Mount (mountfrom) as the root filesystem.
139 vfs_mountroot_try(char *mountfrom)
142 char *vfsname, *path;
146 vfsname = path = NULL;
149 if (mountfrom == NULL)
152 printf("Mounting root from %s\n", mountfrom);
154 /* parse vfs name and path */
155 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
156 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
157 sprintf(patt, "%%%d[a-z]:%%%ds", MFSNAMELEN, MNAMELEN);
158 if (sscanf(mountfrom, patt, vfsname, path) != 2)
161 /* allocate a root mount */
162 if ((error = vfs_rootmountalloc(vfsname, ROOTNAME, &mp))) {
163 printf("Can't allocate root mount for filesystem '%s': %d\n",
167 mp->mnt_flag |= MNT_ROOTFS;
169 /* do our best to set rootdev */
170 if (setrootbyname(path))
171 printf("setrootbyname failed\n");
173 strcpy(mp->mnt_stat.f_mntfromname, path);
174 error = VFS_MOUNT(mp, NULL, NULL, NULL, curproc);
177 free(vfsname, M_MOUNT);
182 printf("Root mount failed: %d\n", error);
185 /* register with list of mounted filesystems */
186 simple_lock(&mountlist_slock);
187 CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list);
188 simple_unlock(&mountlist_slock);
190 /* sanity check system clock against root filesystem timestamp */
191 inittodr(mp->mnt_time);
193 vfs_unbusy(mp, curproc);
198 * Spin prompting on the console for a suitable root filesystem
201 vfs_mountroot_ask(void)
208 printf("\nManual root filesystem specification:\n");
209 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
210 printf(" eg. ufs:/dev/da0s1a\n");
211 printf(" ? List valid disk boot devices\n");
212 printf(" <empty line> Abort manual input\n");
217 if (name[0] == '?') {
218 printf("Possibly valid devices for 'ufs' root:\n");
219 for (i = 0; i < NUMCDEVSW; i++) {
220 dev = makebdev(i, 0);
221 if (devsw(dev) != NULL)
222 printf(" \"%s\"", devsw(dev)->d_name);
227 if (!vfs_mountroot_try(name))
240 printf("%c", c = cngetc() & 0177);
271 * Set rootdev to match (name), given that we expect it to
272 * refer to a disk-like device.
275 setrootbyname(char *name)
278 int bd, unit, slice, part;
283 cp = rindex(name, '/');
288 while (cp != '\0' && (*cp < '0' || *cp > '9'))
291 printf("missing device name\n");
295 printf("missing unit number\n");
300 for (bd = 0; bd < NUMCDEVSW; bd++) {
301 dev = makebdev(bd, 0);
302 if (devsw(dev) != NULL &&
303 strcmp(devsw(dev)->d_name, name) == 0)
306 printf("no such device '%s'\n", name);
309 while (*cp >= '0' && *cp <= '9')
310 unit += 10 * unit + *cp++ - '0';
311 if (*cp == 's' && cp[1] >= '0' && cp[1] <= '9') {
312 slice = cp[1] - '0' + 1;
315 if (*cp >= 'a' && *cp <= 'h') {
320 printf("junk after name\n");
323 rootdev = makebdev(bd, dkmakeminor(unit, slice, part));