# passing arguments to init inside initramfs

## flower1024

hi,

i created my own initramfs and want to pass arguments to the init process inside it.

e.g.

/init    -> normal start

/init backup --> come up with dialog system for backup

background: i use the initramfs to boot from an crypted usb device on multiple machines. and now i want to implement an dialog-driven backup system just inside initramfs (so i can dd-copy everything and its still crypted) before the system starts.

my initramfs is inside the kernel and i dont want an second kernel kust for the backup....

kind regards

flower

----------

## Dominique_71

You have to add the name of your environment variable in /etc/conf.d/env_whitelist, otherwise init will not accept the variable.

After, you can pass it in grub by editing the command line at boot time, or by doing 2 configurations in menu.lst, one without the param, the other with something as 

```
<name_of_your_env_variable>=backup
```

 in the kernel command line.

BTW, do you know how I can pass the outpuf of a command to a variable in an init script. I am doing an init script to switch at boot time between the opengl infrastructure and the nvidia infrastructure. It begin to work well, but the part with eselect take a lot of time, and it will be better if I can pass the output of "eselect opengl show" to a variable, and do a conditional test in order to switch or not the opengl.

----------

## flower1024

thanks for your reply

i'll test it tomorrow

one thing i don't understand: which process will not accept the env var? cause my init is just a script inside the initramfs... (shell = /bin/sh)

btw: you can always get the complete command line by cat /proc/cmdline - the problem is even if i mount proc its not there in initramfs - only when the system has startet.

here is your answer:

the output from anything you write between backticks will be inserted right there:

```

VAR=`ls -l`

echo $VAR

```

note: they are backticks. on german keyb-layout it is shift+left from backspace. there are three kinds of this ticks so make sure you get the right one!Last edited by flower1024 on Mon Sep 04, 2006 8:08 pm; edited 1 time in total

----------

## Dominique_71

 *flower1024 wrote:*   

> thanks for your reply
> 
> i'll test it tomorrow
> 
> one thing i don't understand: which process will not accept the env var? cause my init is just a script inside the initramfs... (shell = /bin/sh)

 

It is init that don't will accept it. Init don't accept environment variable by default. You have to authorise each variable you want to use in this file. It doesn't matter if init is in ram or not, it will work the same.

 *Quote:*   

> btw: you can always get the complete command line by cat /proc/cmdline - the problem is even if i mount proc its not there in initramfs - only when the system has startet.
> 
> here is your answer:
> 
> the output from anything you write between backticks will be inserted right there:
> ...

 

Thank you. I found a better solution with a $(command) construct. I can use the $(construct) inside the test. Just look here.

----------

## flower1024

but init is a script which i wrote!

it doesn't know anything about gentoo....or any /etc/env

the only thing why it could work is that my shell /bin/sh will look at this env-file and enable them; i'll test that tomorrow

/bin/sh (running my script) is pid1 not init. it will start the "normal" gentoo init when it has mounted my root fs.

the normal init process /bin/init has nothing to do with it, cause it does not exists at this time (root is not mounted yet). it is execute before /bin/init starts!

/usr/src/linux/Documentation/filesystems/ramfs-rootfs-initramfs.txt describes the initramfs (which is nearly the same as an initrd)

reply to your question:

the ${command} construct won't work. the only reason for it is that you can write sth. like

```

TIMER=1

echo "${TIMER}er"

echo "$TIMERer"

```

if you remove the brackets in this example then the var will not be found (shell is looking for TIMEer which is not existing).

----------

## Dominique_71

It was a confusion. In any linux, init is the first process the kernel will start. So maybe at next time, it will be better to choose another name.

It is $(command), not ${command}. You can take a look here to see how it work.

I think at it is possible to do MY_VAR=`$(command)` but I use directly $(command) in a test and it work.

----------

## flower1024

oh i missread the brackets.

in initramfs it is not possible to use another name, cause the kernel will start /bin/init in initramfs and this init decides which init really has to start. init= which worked with initrd does not work with the new initramfs format

----------

