2001-12-13 15:55:50

by DevilKin

[permalink] [raw]
Subject: User/kernelspace stuff to set/get kernel variables

Hello

I've been looking on the web, and couldn't really find what i would want...

Basically: is it possible to - one way or another - set variables at kernel boot and read those using userspace utilities?

for instance: i boot my kernel (using any old bootmanager that accepts kernel params)


LILO: linux network=dhcp


and later, in the init scripts, i check the value of this variable using some sort of userspace program, and if it happends to be 'dhcp' i'll invoke the dhcp client.
Otherwise i'd just give a static address.

I have other uses for this, for instance, you want your disks to be FSCK'ed, but don't wanna boot first, or, don't wanna go in single user mode


LILO: linux dofsck=true


Does something like this exist? Is it implementable in an easy way? (I know a few programming languages, but only little C(++)....)

Thanks


DK


2001-12-13 16:04:00

by Daniel Freedman

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

On Thu, Dec 13, 2001, DevilKin wrote:
> Hello
>
> I've been looking on the web, and couldn't really find what i would want...
>
> Basically: is it possible to - one way or another - set variables at kernel boot and read those using userspace utilities?

Sorry, I don't know, but see below for possible solution to your problem anyway.

> for instance: i boot my kernel (using any old bootmanager that accepts kernel params)
>
>
> LILO: linux network=dhcp

You might want to familiarize yourself with the following kernel parameter:

"ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>"

where most options are obvious, and you'll probably want device to be
"eth0" and autoconf to be "dhcp".

HTH,

Daniel


> and later, in the init scripts, i check the value of this variable using some sort of userspace program, and if it happends to be 'dhcp' i'll invoke the dhcp client.
> Otherwise i'd just give a static address.
>
> I have other uses for this, for instance, you want your disks to be FSCK'ed, but don't wanna boot first, or, don't wanna go in single user mode
>
>
> LILO: linux dofsck=true
>
>
> Does something like this exist? Is it implementable in an easy way? (I know a few programming languages, but only little C(++)....)
>
> Thanks
>
>
> DK
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

--
Daniel A. Freedman
Laboratory for Atomic and Solid State Physics
Department of Physics
Cornell University

2001-12-13 16:07:40

by Richard B. Johnson

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

On Thu, 13 Dec 2001, DevilKin wrote:

> Hello
>
> I've been looking on the web, and couldn't really find what i would want...
>
> Basically: is it possible to - one way or another - set variables at kernel boot and read those using userspace utilities?
>
> for instance: i boot my kernel (using any old bootmanager that accepts kernel params)
>
>
> LILO: linux network=dhcp
>

Is this what you want?

`cat /proc/cmdline`


Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).
Santa Claus is coming to town...
He knows if you've been sleeping,
He knows if you're awake;
He knows if you've been bad or good,
So he must be Attorney General Ashcroft.


2001-12-13 16:14:00

by Yann E. MORIN

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

Hi DK.

> Basically: is it possible to - one way or another - set variables at
> kernel boot and read those using userspace utilities?
> for instance: i boot my kernel (using any old bootmanager that
> accepts kernel params)
> LILO: linux network=dhcp
> and later, in the init scripts, i check the value of this variable
> using some sort of userspace program, and if it happends to be
> 'dhcp' i'll invoke the dhcp client.
[--8<--SNIP--8<--]

You'll at least find that in /proc/1/environ

It's a null-terminated list of strings that consist of what you want.
You'll see it as well while dumping the kernel log (dmesg) at the
begining. For my machine, I have:

--8<--
Kernel command line: auto BOOT_IMAGE=linux root=806 devfs=mount idebus=33
--8<--

Regards,
Yann.

--
.---------------------------.----------------------.------------------.
| Yann E. MORIN | Real-Time Embedded | ASCII RIBBON /"\ |
| phone (+33/0) 299 055 231 | Software Designer | CAMPAIGN \ / |
| fax (+33/0) 299 055 221 ?----------------------: AGAINST X |
| [email protected] http://www.atosorigin.com | HTML MAIL / \ |
?--------------------------------------------------?------------------?


2001-12-13 16:21:10

by Jan Janak

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

On Thu, Dec 13, 2001 at 04:54:05PM +0100, DevilKin wrote:
> Hello
>
> I've been looking on the web, and couldn't really find what i would want...
>
> Basically: is it possible to - one way or another - set variables at kernel boot and read those using userspace utilities?
>
> for instance: i boot my kernel (using any old bootmanager that accepts kernel params)
>
>
> LILO: linux network=dhcp
>
>
> and later, in the init scripts, i check the value of this variable using some sort of userspace program, and if it happends to be 'dhcp' i'll invoke the dhcp client.
> Otherwise i'd just give a static address.
>
> I have other uses for this, for instance, you want your disks to be FSCK'ed, but don't wanna boot first, or, don't wanna go in single user mode
>
>
> LILO: linux dofsck=true
>
>
> Does something like this exist? Is it implementable in an easy way? (I know a few programming languages, but only little C(++)....)
>

If you pass a parameter that is not recognized by the kernel, it will be passed to init as environment variable,
so all you need to do is check for the variable in your init scripts ($network in your example).

regards, Jan.

> Thanks
>
>
> DK
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

2001-12-13 16:58:44

by Russell King

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

On Thu, Dec 13, 2001 at 05:20:37PM +0100, Jan Janak wrote:
> If you pass a parameter that is not recognized by the kernel, it will be
> passed to init as environment variable, so all you need to do is check
> for the variable in your init scripts ($network in your example).

IIRC, Red Hat scripts grab them from /proc/cmdline

--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html

2001-12-13 19:49:01

by Jan Janak

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

On Thu, Dec 13, 2001 at 04:58:05PM +0000, Russell King wrote:
> On Thu, Dec 13, 2001 at 05:20:37PM +0100, Jan Janak wrote:
> > If you pass a parameter that is not recognized by the kernel, it will be
> > passed to init as environment variable, so all you need to do is check
> > for the variable in your init scripts ($network in your example).
>
> IIRC, Red Hat scripts grab them from /proc/cmdline

Only parameters of the form foo=bar will be set as environment variables (if not
recognized by the kernel).
AFAIK Red Hat scripts grab parameters of different form from /proc/cmdline.

But parameter of the form network=dhcp will be set as environment variable and
this is IMHO the easiest way how to get the parameter value in startup scripts.

regards, Jan.

>
> --
> Russell King ([email protected]) The developer of ARM Linux
> http://www.arm.linux.org.uk/personal/aboutme.html

2001-12-13 20:35:34

by H. Peter Anvin

[permalink] [raw]
Subject: Re: User/kernelspace stuff to set/get kernel variables

Followup to: <[email protected]>
By author: DevilKin <[email protected]>
In newsgroup: linux.dev.kernel
>
> Hello
>
> I've been looking on the web, and couldn't really find what i would want...
>
> Basically: is it possible to - one way or another - set variables at
> kernel boot and read those using userspace utilities?
>

The entire kernel command line is quoted verbatim in /proc/cmdline.

Additionally, items of the form foo=bar that are not recognized by the
kernel become environment variables to init, and items not of that
form *sometimes* become command-line options to init...

-hpa
--
<[email protected]> at work, <[email protected]> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <[email protected]>