Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Mar 2002 08:26:08 -0800
From:      Luigi Rizzo <rizzo@icir.org>
To:        arch@FreeBSD.ORG
Subject:   Re: For review: machdep.bootdev sysctl (for i386)
Message-ID:  <20020309082607.I21016@iguana.icir.org>
In-Reply-To: <20020309042728.A21016@iguana.icir.org>
References:  <20020309042728.A21016@iguana.icir.org>

next in thread | previous in thread | raw e-mail | index | archive | help
And as a followup, attached is a patch for sysctl(8) to parse
the information. Again it can be cleaned up a little bit
by making machdep.bootdev opaque, but that is purely
a stylistic issue -- in the end, finding the proper
parsing routine always involves a string comparison.

Note that this is only valid for the i386 so I will make
this code conditionally compiled if it ever gets committed.

	cheers
	luigi

On Sat, Mar 09, 2002 at 04:27:28AM -0800, Luigi Rizzo wrote:
> Looking at a way to determine the boot device from userland,
> I have come to the conclusion that the most reasonable way is to
> export to userland whatever information the kernel has (and this
> is machine dependent, sorry!), and let some userland program
...

Index: sysctl.c
===================================================================
RCS file: /home/ncvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.41
diff -u -r1.41 sysctl.c
--- sysctl.c	25 Feb 2002 03:36:06 -0000	1.41
+++ sysctl.c	9 Mar 2002 15:20:52 -0000
@@ -404,6 +404,55 @@
 }
 
 /*
+ * code to map a bootdev into a suitable string.
+ * 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 */
+};
+
+#include <sys/diskslice.h>
+#include <sys/reboot.h>
+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 @@
 		if (!nflag)
 			printf("%s%s", name, sep);
 		fmt++;
+		if (!strcmp(name, "machdep.bootdev"))
+			return machdep_bootdev(*(unsigned long *)p);
 		val = "";
 		while (len >= sizeof(long)) {
 			if(*fmt == 'U')

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020309082607.I21016>