diff options
author | luigi <luigi@FreeBSD.org> | 2002-03-10 20:08:44 +0000 |
---|---|---|
committer | luigi <luigi@FreeBSD.org> | 2002-03-10 20:08:44 +0000 |
commit | ad77debfc5dfa5b73a27c6a6b4f1f7657090fb38 (patch) | |
tree | 41722859cb6c9c6a67510a8addd4bbedc3dd3082 /sbin/sysctl | |
parent | c497ba78d2ef58a2c3bddbb3da9c12b9de6556c5 (diff) | |
download | FreeBSD-src-ad77debfc5dfa5b73a27c6a6b4f1f7657090fb38.zip FreeBSD-src-ad77debfc5dfa5b73a27c6a6b4f1f7657090fb38.tar.gz |
Export a (machine dependent) kernel variable bootdev as
machdep.guessed_bootdev, and add code to sysctl to parse its value
and give a (not necessarily correct) name to the device we booted
from (the main motivation for this code is to use the info in the
PicoBSD boot scripts, and the impact on the kernel is minimal).
NOTE: the information available in bootdev is not always reliable,
so you should not trust it too much. The parsing code is the same
as in boot2.c, and cannot cover all cases -- as it is, it seems to
work fine with floppies and IDE disks recognised by the BIOS. It
_should_ work as well with SCSI disks recognised by the BIOS.
Booting from a CDROM in floppy emulation will return /dev/fd0 (because
this is what the BIOS tells us).
Booting off the network (e.g. with etherboot) leaves bootdev unset so
the value will be printed as "invalid (0xffffffff)".
Finally, this feature might go away at some point, hopefully when we
have a more reliable way to get the same information.
MFC-after: 5 days
Diffstat (limited to 'sbin/sysctl')
-rw-r--r-- | sbin/sysctl/sysctl.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 4d5e3e1..7d66552 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -45,8 +45,10 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ +#include <sys/diskslice.h> /* used for bootdev parsing */ #include <sys/param.h> #include <sys/time.h> +#include <sys/reboot.h> /* used for bootdev parsing */ #include <sys/resource.h> #include <sys/stat.h> #include <sys/sysctl.h> @@ -404,6 +406,53 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind) } /* + * Code to map a bootdev major number into a suitable device name. + * Major numbers are mapped into names as in boot2.c + */ +struct _foo { + int majdev; + char *name; +} maj2name[] = { + 30, "ad", + 0, "wd", + 1, "wfd", + 2, "fd", + 4, "da", + -1, NULL /* terminator */ +}; + +static int +machdep_bootdev(u_long value) +{ + int majdev, unit, slice, part; + struct _foo *p; + + if (value & B_MAGICMASK != B_DEVMAGIC) { + printf("invalid (0x%08x)", value); + return 0; + } + majdev = B_TYPE(value); + unit = B_UNIT(value); + slice = B_SLICE(value); + part = B_PARTITION(value); + if (majdev == 2) { /* floppy, as known to the boot block... */ + printf("/dev/fd%d", unit); + return 0; + } + for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ; + if (p->name != NULL) { /* found */ + if (slice == WHOLE_DISK_SLICE) + printf("/dev/%s%d%c", p->name, unit, part); + else + printf("/dev/%s%ds%d%c", + p->name, unit, slice - BASE_SLICE + 1, part + 'a'); + } else + printf("unknown (major %d unit %d slice %d part %d)", + majdev, unit, slice, part); + return 0; +} + +/* * This formats and outputs the value of one variable * * Returns zero if anything was actually output. @@ -496,6 +545,8 @@ show_var(int *oid, int nlen) if (!nflag) printf("%s%s", name, sep); fmt++; + if (!strcmp(name, "machdep.guessed_bootdev")) + return machdep_bootdev(*(unsigned long *)p); val = ""; while (len >= sizeof(long)) { if(*fmt == 'U') |