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.31 2007/05/27 18:37:23 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 nchandle rootnch
;
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 * XXX TGEN NATA and, presumably, 'old'ATA will also accept the device name
83 * without any fake partition, since the major & minor are identical for all
84 * three (acd*, acd*a and acd*c). However, due to an as-of-yet undiscovered
85 * bug, acd0c ends up with minor 2 when using NATA and booting cold. Since
86 * NATA's acd_open() is unable to fulfill mounts on such 'ghost' cdevs, acd0
87 * and acd1 have been added to the list of CD-ROM root device names.
89 static char *cdrom_rootdevnames
[] = {
99 static void vfs_mountroot(void *junk
);
100 static int vfs_mountroot_try(const char *mountfrom
);
101 static int vfs_mountroot_ask(void);
102 static int getline(char *cp
, int limit
);
104 /* legacy find-root code */
105 char *rootdevnames
[2] = {NULL
, NULL
};
106 static int setrootbyname(char *name
);
108 SYSINIT(mountroot
, SI_SUB_MOUNT_ROOT
, SI_ORDER_SECOND
, vfs_mountroot
, NULL
);
111 * Find and mount the root filesystem
114 vfs_mountroot(void *junk
)
117 cdev_t save_rootdev
= rootdev
;
120 * The root filesystem information is compiled in, and we are
121 * booted with instructions to use it.
124 if ((boothowto
& RB_DFLTROOT
) &&
125 !vfs_mountroot_try(ROOTDEVNAME
))
129 * We are booted with instructions to prompt for the root filesystem,
130 * or to use the compiled-in default when it doesn't exist.
132 if (boothowto
& (RB_DFLTROOT
| RB_ASKNAME
)) {
133 if (!vfs_mountroot_ask())
138 * We've been given the generic "use CDROM as root" flag. This is
139 * necessary because one media may be used in many different
140 * devices, so we need to search for them.
142 if (boothowto
& RB_CDROM
) {
143 for (i
= 0; cdrom_rootdevnames
[i
] != NULL
; i
++) {
144 if (!vfs_mountroot_try(cdrom_rootdevnames
[i
]))
150 * Try to use the value read by the loader from /etc/fstab, or
151 * supplied via some other means. This is the preferred
154 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
158 * If a vfs set rootdev, try it (XXX VINUM HACK!)
160 if (save_rootdev
!= NULL
) {
161 rootdev
= save_rootdev
;
162 if (!vfs_mountroot_try(""))
167 * Try values that may have been computed by the machine-dependant
170 if (rootdevnames
[0] && !vfs_mountroot_try(rootdevnames
[0]))
172 if (rootdevnames
[1] && !vfs_mountroot_try(rootdevnames
[1]))
176 * If we have a compiled-in default, and haven't already tried it, try
180 if (!(boothowto
& RB_DFLTROOT
))
181 if (!vfs_mountroot_try(ROOTDEVNAME
))
186 * Everything so far has failed, prompt on the console if we haven't
187 * already tried that.
189 if (!(boothowto
& (RB_DFLTROOT
| RB_ASKNAME
)) && !vfs_mountroot_ask())
191 panic("Root mount failed, startup aborted.");
195 * Mount (mountfrom) as the root filesystem.
198 vfs_mountroot_try(const char *mountfrom
)
201 char *vfsname
, *devname
;
210 if (mountfrom
== NULL
)
211 return(error
); /* don't complain */
214 kprintf("Mounting root from %s\n", mountfrom
);
217 /* parse vfs name and devname */
218 vfsname
= kmalloc(MFSNAMELEN
, M_MOUNT
, M_WAITOK
);
219 devname
= kmalloc(MNAMELEN
, M_MOUNT
, M_WAITOK
);
220 vfsname
[0] = devname
[0] = 0;
221 ksprintf(patt
, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN
, MNAMELEN
);
222 if (ksscanf(mountfrom
, patt
, vfsname
, devname
) < 1)
225 /* allocate a root mount */
226 error
= vfs_rootmountalloc(vfsname
,
227 devname
[0] != 0 ? devname
: ROOTNAME
, &mp
);
229 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
233 mp
->mnt_flag
|= MNT_ROOTFS
;
235 /* do our best to set rootdev */
236 if ((devname
[0] != 0) && setrootbyname(devname
))
237 kprintf("setrootbyname failed\n");
239 /* If the root device is a type "memory disk", mount RW */
240 if (rootdev
!= NULL
&& dev_is_good(rootdev
) &&
241 (dev_dflags(rootdev
) & D_MEMDISK
)) {
242 mp
->mnt_flag
&= ~MNT_RDONLY
;
245 error
= VFS_MOUNT(mp
, NULL
, NULL
, proc0
.p_ucred
);
249 kfree(vfsname
, M_MOUNT
);
251 kfree(devname
, M_MOUNT
);
257 kprintf("Root mount failed: %d\n", error
);
259 /* register with list of mounted filesystems */
260 mountlist_insert(mp
, MNTINS_FIRST
);
262 /* sanity check system clock against root fs timestamp */
263 inittodr(mp
->mnt_time
);
270 * Spin prompting on the console for a suitable root filesystem
272 static int vfs_mountroot_ask_callback(struct dev_ops
*ops
, void *arg
);
275 vfs_mountroot_ask(void)
280 kprintf("\nManual root filesystem specification:\n");
281 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
282 kprintf(" ? List valid disk boot devices\n");
283 kprintf(" panic Just panic\n");
284 kprintf(" abort Abort manual input\n");
286 kprintf("\nmountroot> ");
288 if (getline(name
, 128) < 0)
292 } else if (name
[0] == '?') {
293 kprintf("Possibly valid devices for 'ufs' root:\n");
294 dev_ops_scan(vfs_mountroot_ask_callback
, NULL
);
297 } else if (strcmp(name
, "panic") == 0) {
298 panic("panic from console");
299 } else if (strcmp(name
, "abort") == 0) {
301 } else if (vfs_mountroot_try(name
) == 0) {
310 vfs_mountroot_ask_callback(struct dev_ops
*ops
, void *arg __unused
)
314 dev
= get_dev(ops
->head
.maj
, 0);
315 if (dev_is_good(dev
))
316 kprintf(" \"%s\"", dev_dname(dev
));
321 getline(char *cp
, int limit
)
359 if (lp
- cp
>= limit
- 1) {
371 * Convert a given name to the cdev_t of the disk-like device
381 static int kgetdiskbyname_callback(struct dev_ops
*ops
, void *arg
);
384 kgetdiskbyname(const char *name
)
388 int unit
, slice
, part
;
390 struct kdbn_info info
;
393 * Get the base name of the device
395 if (strncmp(name
, __SYS_PATH_DEV
, sizeof(__SYS_PATH_DEV
) - 1) == 0)
396 name
+= sizeof(__SYS_PATH_DEV
) - 1;
397 cp
= __DECONST(char *, name
);
400 while (*cp
>= 'a' && *cp
<= 'z')
403 kprintf("missing device name\n");
411 unit
= strtol(cp
, &cp
, 10);
412 if (name
+ nlen
== (const char *)cp
|| unit
< 0 || unit
>= DKMAXUNITS
) {
413 kprintf("bad unit: %d\n", unit
);
418 * Get the slice. Note that if no partition or partition 'a' is
419 * specified, and no slice is specified, we will try both 'ad0a'
420 * (which is what you get when slice is 0), and also 'ad0' (the
421 * whole-disk partition, slice == 1).
435 if (*cp
>= 'a' && *cp
<= 'p') {
443 kprintf("junk after name\n");
450 bzero(&info
, sizeof(info
));
453 info
.minor
= dkmakeminor(unit
, slice
, part
);
454 dev_ops_scan(kgetdiskbyname_callback
, &info
);
455 if (info
.dev
== NULL
) {
456 kprintf("no such device '%*.*s'\n", nlen
, nlen
, name
);
463 rdev
= make_sub_dev(info
.dev
, info
.minor
);
469 kgetdiskbyname_callback(struct dev_ops
*ops
, void *arg
)
471 struct kdbn_info
*info
= arg
;
475 dev
= get_dev(ops
->head
.maj
, info
->minor
);
476 if (dev_is_good(dev
) && (dname
= dev_dname(dev
)) != NULL
) {
477 if (strlen(dname
) == info
->nlen
&&
478 strncmp(dname
, info
->name
, info
->nlen
) == 0) {
487 * Set rootdev to match (name), given that we expect it to
488 * refer to a disk-like device.
491 setrootbyname(char *name
)
495 diskdev
= kgetdiskbyname(name
);
496 if (diskdev
!= NULL
) {
505 DB_SHOW_COMMAND(disk
, db_getdiskbyname
)
509 if (modif
[0] == '\0') {
510 db_error("usage: show disk/devicename");
513 dev
= kgetdiskbyname(modif
);
515 db_printf("cdev_t = %p\n", dev
);
517 db_printf("No disk device matched.\n");