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.16 2006/05/06 18:48:52 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/namecache.h>
62 #include <sys/paths.h>
63 #include <sys/thread2.h>
70 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
72 #define ROOTNAME "root_device"
74 struct vnode *rootvnode;
75 struct namecache *rootncp;
78 * The root specifiers we will try if RB_CDROM is specified. Note that
79 * the ATA driver will accept acd*a and acd*c, but the SCSI driver
80 * will only accept cd*c, so use 'c'.
82 static char *cdrom_rootdevnames[] = {
90 static void vfs_mountroot(void *junk);
91 static int vfs_mountroot_try(const char *mountfrom);
92 static int vfs_mountroot_ask(void);
93 static void gets(char *cp);
95 /* legacy find-root code */
96 char *rootdevnames[2] = {NULL, NULL};
97 static int setrootbyname(char *name);
99 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
102 * Find and mount the root filesystem
105 vfs_mountroot(void *junk)
108 dev_t save_rootdev = rootdev;
111 * The root filesystem information is compiled in, and we are
112 * booted with instructions to use it.
115 if ((boothowto & RB_DFLTROOT) &&
116 !vfs_mountroot_try(ROOTDEVNAME))
120 * We are booted with instructions to prompt for the root filesystem,
121 * or to use the compiled-in default when it doesn't exist.
123 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
124 if (!vfs_mountroot_ask())
129 * We've been given the generic "use CDROM as root" flag. This is
130 * necessary because one media may be used in many different
131 * devices, so we need to search for them.
133 if (boothowto & RB_CDROM) {
134 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
135 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
141 * Try to use the value read by the loader from /etc/fstab, or
142 * supplied via some other means. This is the preferred
145 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
149 * If a vfs set rootdev, try it (XXX VINUM HACK!)
151 if (save_rootdev != NODEV) {
152 rootdev = save_rootdev;
153 if (!vfs_mountroot_try(""))
158 * Try values that may have been computed by the machine-dependant
161 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
163 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
167 * If we have a compiled-in default, and haven't already tried it, try
171 if (!(boothowto & RB_DFLTROOT))
172 if (!vfs_mountroot_try(ROOTDEVNAME))
177 * Everything so far has failed, prompt on the console if we haven't
178 * already tried that.
180 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
182 panic("Root mount failed, startup aborted.");
186 * Mount (mountfrom) as the root filesystem.
189 vfs_mountroot_try(const char *mountfrom)
192 char *vfsname, *devname;
201 if (mountfrom == NULL)
202 return(error); /* don't complain */
205 printf("Mounting root from %s\n", mountfrom);
208 /* parse vfs name and devname */
209 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
210 devname = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
211 vfsname[0] = devname[0] = 0;
212 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
213 if (sscanf(mountfrom, patt, vfsname, devname) < 1)
216 /* allocate a root mount */
217 error = vfs_rootmountalloc(vfsname,
218 devname[0] != 0 ? devname : ROOTNAME, &mp);
220 printf("Can't allocate root mount for filesystem '%s': %d\n",
224 mp->mnt_flag |= MNT_ROOTFS;
226 /* do our best to set rootdev */
227 if ((devname[0] != 0) && setrootbyname(devname))
228 printf("setrootbyname failed\n");
230 /* If the root device is a type "memory disk", mount RW */
231 if (rootdev != NODEV && dev_is_good(rootdev) &&
232 (dev_dflags(rootdev) & D_MEMDISK)) {
233 mp->mnt_flag &= ~MNT_RDONLY;
236 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
240 free(vfsname, M_MOUNT);
242 free(devname, M_MOUNT);
248 printf("Root mount failed: %d\n", error);
250 /* register with list of mounted filesystems */
251 mountlist_insert(mp, MNTINS_FIRST);
253 /* sanity check system clock against root fs timestamp */
254 inittodr(mp->mnt_time);
261 * Spin prompting on the console for a suitable root filesystem
264 vfs_mountroot_ask(void)
271 printf("\nManual root filesystem specification:\n");
272 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
273 printf(" eg. ufs:da0s1a\n");
274 printf(" ? List valid disk boot devices\n");
275 printf(" <empty line> Abort manual input\n");
276 printf("\nmountroot> ");
280 if (name[0] == '?') {
281 printf("Possibly valid devices for 'ufs' root:\n");
282 for (i = 0; i < NUMCDEVSW; i++) {
283 dev = udev2dev(makeudev(i, 0), 0);
284 if (dev_is_good(dev))
285 printf(" \"%s\"", dev_dname(dev));
290 if (!vfs_mountroot_try(name))
303 printf("%c", c = cngetc() & 0177);
334 * Convert a given name to the dev_t of the disk-like device
338 getdiskbyname(const char *name)
342 int cd, unit, slice, part;
347 * Get the base name of the device
349 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
350 name += sizeof(__SYS_PATH_DEV) - 1;
351 cp = __DECONST(char *, name);
354 while (*cp >= 'a' && *cp <= 'z')
357 printf("missing device name\n");
365 unit = strtol(cp, &cp, 10);
366 if (name + nlen == (const char *)cp || unit < 0 || unit > DKMAXUNIT) {
367 printf("bad unit: %d\n", unit);
372 * Get the slice. Note that if no partition or partition 'a' is
373 * specified, and no slice is specified, we will try both 'ad0a'
374 * (which is what you get when slice is 0), and also 'ad0' (the
375 * whole-disk partition, slice == 1).
379 if (slice < 1 || slice > 9) {
380 printf("bad slice number\n");
383 ++slice; /* slice #1 starts at 2 */
392 if (*cp >= 'a' && *cp <= 'p') {
400 printf("junk after name\n");
407 for (cd = 0; cd < NUMCDEVSW; cd++) {
410 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0);
411 if (dev_is_good(dev) && (dname = dev_dname(dev)) != NULL) {
412 if (strlen(dname) == nlen &&
413 strncmp(dname, name, nlen) == 0) {
418 if (cd == NUMCDEVSW) {
419 printf("no such device '%*.*s'\n", nlen, nlen, name);
426 rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part));
431 * Set rootdev to match (name), given that we expect it to
432 * refer to a disk-like device.
435 setrootbyname(char *name)
439 diskdev = getdiskbyname(name);
440 if (diskdev != NODEV) {
449 DB_SHOW_COMMAND(disk, db_getdiskbyname)
453 if (modif[0] == '\0') {
454 db_error("usage: show disk/devicename");
457 dev = getdiskbyname(modif);
459 db_printf("dev_t = %p\n", dev);
461 db_printf("No disk device matched.\n");