Use boot parameter to perform certain actions after boot

bootgrub2parameterxbmc

I'm asking myself if I can use Grub 2 to tell the Operating System which actions to perform after boot.

Background:
I have a HTPC with Debian Wheezy installed. Sometimes I want to use the System as a regular desktop but sometimes I'd just like to have XBMC started.

I tried out a dual boot with OpenElec which wasn't very convenient because I was not able to mount my RAID-1 in OpenElec.

My next attempt would be to use a second minimal Debian installation with XBMC. With this solution I could still choose between regular desktop and HTPC purpose in Grub.

But now I thought this could be a waste of time and disk space, as I have Debian already installed. It would be easier if there was some kind of boot parameter I could use to tell my regular Debian installation

  • "Boot to desktop" or
  • "Boot to XBMC"

Then I would just need a second entry in Grub to handle this boot parameter.

Is this somehow possible?

Best Answer

Any command line parameter to the Linux kernel will show up in /proc/cmdline. You can put anything you like in there (as long it's not a parameter recognised by the kernel or any of the drivers or the initrd system). For instance, you could build it up with fancy grub menus, and then use that in a startup script like /etc/rc.local to decide what to do with it.

For instance add a /etc/grub.d/09-xcmd with:

#! /usr/bin/tail -n+2
menuentry 'XMBC' {
  set xcmd='xcmd=xmbc'
  echo 'Now select a kernel'
  sleep 1
}

Then add to /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT \${xcmd}"

Run update-grub.

In /etc/rc.local, you could have:

case $(cat /proc/cmdline) in
  (*xcmd=*) exec sudo -Hu marcel startx;;
esac

And in your ~/.xinitrc:

cmdline=$(cat /proc/cmdline)
case $cmdline in
  (*xcmd=*)
    xcmd=${cmdline##*xcmd=}
    xcmd=${xcmd%% *}
    "$xcmd" &
esac
Related Question