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
28 * $FreeBSD: src/sys/kern/vfs_conf.c,v 1.49.2.5 2003/01/07 11:56:53 joerg Exp $
29 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.7 2004/03/01 06:33:17 dillon Exp $
33 * Locate and mount the root filesystem.
35 * The root filesystem is detailed in the kernel environment variable
36 * vfs.root.mountfrom, which is expected to be in the general format
39 * vfsname := the name of a VFS known to the kernel and capable
40 * of being mounted as root
41 * path := disk device name or other data used by the filesystem
42 * to locate its physical store
46 #include "opt_rootdevname.h"
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/malloc.h>
55 #include <sys/reboot.h>
56 #include <sys/diskslice.h>
57 #include <sys/disklabel.h>
60 #include <sys/device.h>
61 #include <sys/paths.h>
68 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
70 #define ROOTNAME "root_device"
72 struct vnode *rootvnode;
75 * The root specifiers we will try if RB_CDROM is specified.
77 static char *cdrom_rootdevnames[] = {
84 static void vfs_mountroot(void *junk);
85 static int vfs_mountroot_try(char *mountfrom);
86 static int vfs_mountroot_ask(void);
87 static void gets(char *cp);
89 /* legacy find-root code */
90 char *rootdevnames[2] = {NULL, NULL};
91 static int setrootbyname(char *name);
93 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
96 * Find and mount the root filesystem
99 vfs_mountroot(void *junk)
104 * The root filesystem information is compiled in, and we are
105 * booted with instructions to use it.
108 if ((boothowto & RB_DFLTROOT) &&
109 !vfs_mountroot_try(ROOTDEVNAME))
113 * We are booted with instructions to prompt for the root filesystem,
114 * or to use the compiled-in default when it doesn't exist.
116 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
117 if (!vfs_mountroot_ask())
122 * We've been given the generic "use CDROM as root" flag. This is
123 * necessary because one media may be used in many different
124 * devices, so we need to search for them.
126 if (boothowto & RB_CDROM) {
127 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
128 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
134 * Try to use the value read by the loader from /etc/fstab, or
135 * supplied via some other means. This is the preferred
138 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
142 * Try values that may have been computed by the machine-dependant
145 if (!vfs_mountroot_try(rootdevnames[0]))
147 if (!vfs_mountroot_try(rootdevnames[1]))
151 * If we have a compiled-in default, and haven't already tried it, try
155 if (!(boothowto & RB_DFLTROOT))
156 if (!vfs_mountroot_try(ROOTDEVNAME))
161 * Everything so far has failed, prompt on the console if we haven't
162 * already tried that.
164 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
166 panic("Root mount failed, startup aborted.");
170 * Mount (mountfrom) as the root filesystem.
173 vfs_mountroot_try(char *mountfrom)
175 struct thread *td = curthread;
177 char *vfsname, *path;
188 if (mountfrom == NULL)
189 return(error); /* don't complain */
191 s = splcam(); /* Overkill, but annoying without it */
192 printf("Mounting root from %s\n", mountfrom);
195 /* parse vfs name and path */
196 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
197 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
198 vfsname[0] = path[0] = 0;
199 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
200 if (sscanf(mountfrom, patt, vfsname, path) < 1)
203 /* allocate a root mount */
204 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
207 printf("Can't allocate root mount for filesystem '%s': %d\n",
211 mp->mnt_flag |= MNT_ROOTFS;
213 /* do our best to set rootdev */
214 if ((path[0] != 0) && setrootbyname(path))
215 printf("setrootbyname failed\n");
217 /* If the root device is a type "memory disk", mount RW */
218 if (rootdev != NODEV && dev_dport(rootdev) &&
219 (dev_dflags(rootdev) & D_MEMDISK)) {
220 mp->mnt_flag &= ~MNT_RDONLY;
223 error = VFS_MOUNT(mp, NULL, NULL, NULL, td);
227 free(vfsname, M_MOUNT);
235 printf("Root mount failed: %d\n", error);
238 /* register with list of mounted filesystems */
239 lwkt_gettoken(&ilock, &mountlist_token);
240 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
241 lwkt_reltoken(&ilock);
243 /* sanity check system clock against root fs timestamp */
244 inittodr(mp->mnt_time);
251 * Spin prompting on the console for a suitable root filesystem
254 vfs_mountroot_ask(void)
261 printf("\nManual root filesystem specification:\n");
262 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
263 printf(" eg. ufs:%sda0s1a\n", __SYS_PATH_DEV);
264 printf(" ? List valid disk boot devices\n");
265 printf(" <empty line> Abort manual input\n");
266 printf("\nmountroot> ");
270 if (name[0] == '?') {
271 printf("Possibly valid devices for 'ufs' root:\n");
272 for (i = 0; i < NUMCDEVSW; i++) {
274 if (dev_dport(dev) != NULL)
275 printf(" \"%s\"", dev_dname(dev));
280 if (!vfs_mountroot_try(name))
293 printf("%c", c = cngetc() & 0177);
324 * Convert a given name to the dev_t of the disk-like device
328 getdiskbyname(char *name) {
330 int cd, unit, slice, part;
335 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
336 name += sizeof(__SYS_PATH_DEV) - 1;
338 while (*cp != '\0' && (*cp < '0' || *cp > '9') && *cp != '/')
341 printf("missing device name\n");
344 if (*cp == '\0' || *cp == '/')
349 for (cd = 0; cd < NUMCDEVSW; cd++) {
350 dev = makedev(cd, 0);
351 if (dev_dport(dev) != NULL &&
352 strcmp(dev_dname(dev), name) == 0)
355 printf("no such device '%s'\n", name);
358 if (dev_dmaj(dev) == major(rootdev))
359 /* driver has already configured rootdev, e. g. vinum */
362 printf("missing unit number\n");
365 while (*cp >= '0' && *cp <= '9')
366 unit = 10 * unit + *cp++ - '0';
367 if (*cp == 's' && cp[1] >= '0' && cp[1] <= '9') {
368 slice = cp[1] - '0' + 1;
371 if (*cp >= 'a' && *cp <= 'h') {
376 printf("junk after name\n");
379 return (makedev(cd, dkmakeminor(unit, slice, part)));
383 * Set rootdev to match (name), given that we expect it to
384 * refer to a disk-like device.
387 setrootbyname(char *name)
391 diskdev = getdiskbyname(name);
392 if (diskdev != NODEV) {
401 DB_SHOW_COMMAND(disk, db_getdiskbyname)
405 if (modif[0] == '\0') {
406 db_error("usage: show disk/devicename");
409 dev = getdiskbyname(modif);
411 db_printf("dev_t = %p\n", dev);
413 db_printf("No disk device matched.\n");