Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Possible to get a variable from Grub2?

  1. #1
    Join Date
    Mar 2006
    Beans
    Hidden!

    Question Possible to get a variable from Grub2?

    Have setup a system (one linux OS) that can either be a command line or gui experience and would like to be able to select which version to run at the grub2 menu.

    I have carried out a command line installation of Xubuntu 10.04 then added openbox. I use SLiM (Simple Login Manager), which has an entry in rc.local to run it.

    Xubuntu offers no way of booting to the cli - that I can find, and SLiM seems to offer no way of turning it off.

    So is there any way a bash script could pick up which menu entry in grub2 was selected to be run, to then select whether SLiM should be run or not? I am thinking if I have custom / bespoke menu entries then these would be easier to find.

    Example script (in English!) to put in rc.local:

    Code:
    !#/bin/bash
    
    menu=Collect name of grub2 entry selected  << how to do this bit?
    
    if $menu = SLiM then
    
    /usr/bin/slim
    
    endif
    
    done
    Or is there a better way?

    ( no, the "text" kernel option doesn't work with SLiM)
    ( I can't find a way to start up with runlevel 1 without invoking recovery mode or dropping to a root shell)

    Happy to try just about anything (apart from working with animals or children) to solve this!

    Thanks
    Last edited by Jose Catre-Vandis; April 7th, 2011 at 09:06 PM. Reason: sppeling :)
    No longer participating......

  2. #2
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Possible to get a variable from Grub2?

    Easiest way is to make menu items that boot to different runlevels

    You can specify which runlevel to boot into by appending the number to the end of the kernel line in grub
    Eg.
    Code:
    linux /boot/vmlinuz-2.6.38-8-generic.....quiet splash 3
    Ubuntu defaults (I think) to runlevel 2
    so runlevel 3 can be your CLI only option

    You'll need to read up on how to create sysV init scripts, and how to specify which runlevels the script will execute under
    Then make an init script for your graphical login manager, and bind it to runlevel 2 only
    Code:
    while [ true ]; do CY=$(date +%y); CM=$(date +%m); if [ -n "$PY" ] && [ -n "$PM" ]; then echo "Ubuntu ${CY}.${CM} is the worst release ever"; echo "I'm going back to ${PY}.${PM}"; fi; PY="$CY"; PM="$CM"; sleep 182d; done

  3. #3
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Possible to get a variable from Grub2?

    Thanks amauk

    SLiM shows up on runlevel 3 as well.

    I'll go away and have a read up on sysV init scripts

    EDIT

    Hang on you've given me an idea.

    Code:
    who -r
    tells me which runlevel I am on so if I can check this in my script I can do this?
    Last edited by Jose Catre-Vandis; April 7th, 2011 at 09:52 PM.
    No longer participating......

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,281
    Distro
    Kubuntu

    Re: Possible to get a variable from Grub2?

    Quote Originally Posted by amauk View Post
    Ubuntu defaults (I think) to runlevel 2
    Are you sure? I always thought that it was runlevel 5.

  5. #5
    Join Date
    Jul 2005
    Location
    I think I'm here! Maybe?
    Beans
    Hidden!

    Re: Possible to get a variable from Grub2?

    Quote Originally Posted by dwhitney67 View Post
    Are you sure? I always thought that it was runlevel 5.
    No it is runlevel 2! Try the
    Code:
    who -r
    command.
    DISTRO: Xubuntu 16.04-64bit --- Code-tags --- Boot-Repair --- Grub2 wiki & Grub2 Basics --- RootSudo --- Wireless-Info --- SolvedThreads

  6. #6
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Possible to get a variable from Grub2?

    Quote Originally Posted by Jose Catre-Vandis View Post
    Hang on you've given me an idea.

    Code:
    who -r
    tells me which runlevel I am on so if I can check this in my script I can do this?
    Yes,
    but I'd advise against rolling your own custom solution

    I'd personally stick with init scripts,
    mainly because they're standardised and there's an entire framework behind their operation

    Say you're in CLI mode, and wish to switch to GUI
    How do you do this, with confidence that everything is loaded and working as it should?

    On switching runlevels
    - All the 'stop' init scripts for the current runlevel get executed and all loaded services stop
    - Runlevel changes
    - All the 'start' init scripts for the new runlevel get executed and all services that should be running at this runlevel are started


    *edit*
    Btw, if you do roll your own custom solution
    a better way to get the current runlevel is
    Code:
    runlevel | grep -o '[0-9]\+$'
    Last edited by amauk; April 7th, 2011 at 10:28 PM.
    Code:
    while [ true ]; do CY=$(date +%y); CM=$(date +%m); if [ -n "$PY" ] && [ -n "$PM" ]; then echo "Ubuntu ${CY}.${CM} is the worst release ever"; echo "I'm going back to ${PY}.${PM}"; fi; PY="$CY"; PM="$CM"; sleep 182d; done

  7. #7
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Possible to get a variable from Grub2?

    Thanks amauk, and thanks for the one liner

    I get what you are saying.

    I had a look at directories rc2.d and rc3.d and they have exactly the same symlinks in each. Does that mean they do the same thing? Is it that I just haven't installed a program yet that needs a specific run-level to operate?
    No longer participating......

  8. #8
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Possible to get a variable from Grub2?

    Doing a simple
    Code:
    CUR_RUNLVL=$(runlevel | grep -o '[0-9]\+$')
    if [ "$CUR_RUNLVL" -eq 2 ]; then
    	/usr/bin/slim
    fi
    is certainly easy,
    but I do still have reservations about bypassing init scripts (which were designed for this very purpose, specifying what gets run at specific runlevels)

    Certainly use rc.local as a first prototype,
    but I would have an init script for SLiM as an end-goal
    Code:
    while [ true ]; do CY=$(date +%y); CM=$(date +%m); if [ -n "$PY" ] && [ -n "$PM" ]; then echo "Ubuntu ${CY}.${CM} is the worst release ever"; echo "I'm going back to ${PY}.${PM}"; fi; PY="$CY"; PM="$CM"; sleep 182d; done

  9. #9
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Possible to get a variable from Grub2?

    Well, I couldn't resist so i went ahead and tried it out.

    Here is the script I put in /etc/rc.local:

    Code:
    !#/bin/bash
    
    slimtest=`who -r`
    runl=${slimtest:19:1}
    
    #alt method
    #slimtest=`runlevel | grep -o '[0-9]\+$'`
    
    if [ "$runl" == "2" ]; then 
    
    /usr/bin/slim
    
    fi
    I rebooted, selected the recovery mode menu entry, edited it to remove single and add a 3, pressed CTRL+X, and it booted up to a command prompt login

    I logged in, checked dmesg, nothing odd there, and then typed startx.

    Code:
    runlevel | grep -o '[0-9]\+$'
    returned "3"

    Worked fine booted up the normal way too

    Have more testing to do, and will have to see if I can break it!
    Last edited by Jose Catre-Vandis; April 7th, 2011 at 11:19 PM.
    No longer participating......

  10. #10
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: Possible to get a variable from Grub2?

    I've also just had a look at the rc2.d and rc3.d directories on my main system which i have had running for over a year, updated from lucid to meerkat, and packages installed to the hilt, and loads of configuration. The contents of each folder are exactly the same.

    So what have Ubuntu / Xubuntu been up to? Is there no difference at all between these two run-levels, aside from one supposedly being for cli mode?
    Last edited by Jose Catre-Vandis; April 7th, 2011 at 11:19 PM.
    No longer participating......

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •