From owner-freebsd-arch Sun Mar 10 2: 3:52 2002 Delivered-To: freebsd-arch@freebsd.org Received: from iguana.icir.org (iguana.icir.org [192.150.187.36]) by hub.freebsd.org (Postfix) with ESMTP id F398C37B405; Sun, 10 Mar 2002 02:03:43 -0800 (PST) Received: (from rizzo@localhost) by iguana.icir.org (8.11.6/8.11.3) id g2AA3hB30509; Sun, 10 Mar 2002 02:03:43 -0800 (PST) (envelope-from rizzo) Date: Sun, 10 Mar 2002 02:03:43 -0800 From: Luigi Rizzo To: Michael Smith Cc: arch@FreeBSD.ORG Subject: Re: For review: machdep.bootdev sysctl (for i386) Message-ID: <20020310020343.J28779@iguana.icir.org> References: <20020309224310.A28779@iguana.icir.org> <200203100909.g2A99lj02920@mass.dis.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200203100909.g2A99lj02920@mass.dis.org> User-Agent: Mutt/1.3.23i Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Mar 10, 2002 at 01:09:47AM -0800, Michael Smith wrote: > > I am not claiming that bootdev gives the correct answer in 100% of > > the cases. As you correctly notice, there is probably not a "perfect" > > solution. > > This isn't really the issue. The issue is that one cannot tell whether > it's giving you an accurate answer unless you already know what the > answer is, in which case it's useless anyway. "you" as a human being know the answer when you have the box in front of you and know where you have installed your code. But the system (which is booting the exact same image from either a floppy or a CF or from the net) does not. _This_ is the issue. Sure, you can embed the info in the configuration -- so i need to build one image for a floppy, four for IDE disks (slice 1, 2, 3, 4), another set of four for the SCSI disks... I'd rather keep the number of variants down, thank you. Basically, having bootdev (or equivalent) lets me say: Use this image for fd, ad*, da* when booted from BIOS. Do not use it in other cases. whereas now I have to say Use this image for fd only. when booted from BIOS. Do not use it in other cases. Sure there is room for mistakes, they occur if you run some code on a system where it is not meant to work. So what ? That is what instructions are for (and yes, engineers never read instructions, but then the failure modes of determining a device from bootdev or randomly guessing that it has to be /dev/fd0 look the same to me...) > Your examples don't actually demonstrate how you plan to decode bootdev > into anything meaningful in the BSD space; without this, your entire > exercise is a waste of time, since you can't *do* anything with the value > otherwise. It was in the followup message that I mentioned. I am attaching it at the end. The userland command (sysctl in this case, but it can be some other binary) produces a string that a script can parse. Something like this: dev=`sysctl machdep.bootdev` if [ -c $dev ] ; then # exists and is a char.special file, do something with it else echo "sorry, could not map bootdev to something sensible" fi > You're trying to hit something that isn't a nail with the sort of hammer > best left to a toddler chasing the family cat. oh yeah, i was worried that we were going to keep the discussion on purely technical terms :) Mom, I want my teddy bear, Mike says I am wrong!!! cheers luigi Date: Sat, 9 Mar 2002 08:26:08 -0800 From: Luigi Rizzo To: arch@FreeBSD.ORG Subject: Re: For review: machdep.bootdev sysctl (for i386) Message-ID: <20020309082607.I21016@iguana.icir.org> References: <20020309042728.A21016@iguana.icir.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020309042728.A21016@iguana.icir.org> User-Agent: Mutt/1.3.23i Status: RO Content-Length: 2506 Lines: 91 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 +#include +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