Set up GRUB to boot a Linux SD card (on a Linux computer)

From LSDevLinux
Jump to: navigation, search

GRUB is the bootloader used by most Linux systems. If your computer has Ubuntu installed it is using grub to boot, though if you don't have a "dual-boot" system you may not see it.

If you have a Linux-only system you need to follow the instructions on this page to set up Balsa to boot from an SD card on your computer.

If your computer is set up to dual-boot with Windows and another OS, you may follow either these instructions or Booting a Linux SD card from Windows.

Recently a new version of Grub, Grub 2, was introduced, which uses different control files than Grub 1, which is commonly called Legacy Grub. The instructions below will help you determine which version your computer is using, and help you configure your computer to boot from a BalsaRoot SD card

Determine which version of Grub you have

If your Ubuntu version is 9.04 (Jaunty) or older, you most likely have Grub 1 (legacy). If you have a clean install of 9.10 (Lucid) or newer you probably have Grub 2. But if you have upgraded from an older version your computer may still be running Grub 1; and Grub 2 may be installed on some older systems.

The procedure for editing your Grub configuration is very different for each version, so you need to determine which one your computer is using.

Open a Terminal and type at the prompt:

grub-install -v

This will display something like this in the Terminal:

grub-install (GNU GRUB 1.98-1ubuntu7)

If the number in this line (the Grub version) is less than 1.0, you have Grub 1.

If the version number is greater than 1.00 you have Grub 2. (go figure...)

Automatic Balsa setup: run a script

Boot into Linux on the computer on which you want to run Balsa. Insert the BalsaRoot SD card into this computer.

  • Download this script and save it in Downloads, or somewhere in your home folder.
  • Open the File Manager and browse to the folder where this file is. Right-click on the file and select Properties.
  • In the Properties window, click on the Permissions tab. Find the line that says
Execute: [ ] Allow executing file as program
Click to mark/tick the checkbox. (This is based on Ubuntu 10.04. Some Linux dists may use a different gui or command to mark this file as "executable.")
  • Open a Terminal, and type sudo followed by a space.
  • Drag the icon of the downloaded file to the terminal window.
  • This will paste the script's location into the command line. The result should look similar to this:
sudo '/home/user/Downloads/builder_replacements-2_usr_share_balsa_grub_balsa-install.sh'
  • Press Enter to run the script.
  • Enter your password when prompted.
  • After the script has finished and the prompt reappears in Terminal, restart the computer.
  • The computer should boot into the Balsa login screen, or else into the Grub menu, from which you may choose which OS you want to boot into. Balsa will probably be the default if no choice is made during the time the Grub menu appears.
  • If you don't want Balsa to be the default boot choice, you can rename a file as follows:
cd /etc/grub.d
sudo mv -i 06_balsa 35_balsa
sudo update-grub 
However, note that any time update-grub is run with the Balsa card inserted, eg when an updated kernel is installed on your host system, it will generate additional entries for the kernels it finds on the card, but these entries WILL NOT WORK and you should not use them. Use only the explicit Balsa menu entry created by the downloaded script.

Continue with First time you boot into Balsa

Manual Balsa setup: Edit Grub files on your computer

If you use one of the automatic setup scripts above, you do NOT need to read further on this page. These instructions are provided in case the scripts don't work or are not available.

Copy files from SD card (Grub 1 and Grub 2)

Insert your BalsaRoot SD card into the computer on which it will run and make sure it is mounted (you will see "BalsaRoot" under "Places").

Open a Terminal, and enter the following commands, one line at a time. Press ENTER after each line to run that command.

[Hint: '"`UNIQ--toggledisplay-00000000-QINU`"'

These commands will clean up any previous versions:

sudo rm -rf /boot/balsa.old
sudo mv /boot/balsa /boot/balsa.old
sudo mkdir -p /boot/balsa

Now we'll copy the Balsa boot files from the SD card to your main /boot directory.

sudo cp /media/BalsaRoot/boot/vmlinuz.*generic /boot/balsa
ls /boot/balsa/vml*
cd /boot/balsa

Now type this next command into the Terminal. Type "vmlin" and then hit the <TAB> key, and the full name of this file will be entered automatically. Then continue typing the command. Be sure to include a <SPACE> after the filename, before /boot/balsa/vmlinuz. [Hint: '"`UNIQ--toggledisplay-00000001-QINU`"'

sudo ln -s vmlin<TAB> /boot/balsa/vmlinuz

Now follow the same procedure to copy and link a second file.

sudo cp /media/BalsaRoot/boot/initrd.img.*generic /boot/balsa
ls /boot/balsa/initrd.img.*generic
cd /boot/balsa
sudo ln -s initr<TAB> /boot/balsa/initrd.img

Running this command should list four files in this directory (they might appear in pretty colors; this is OK):

ls /boot/balsa

Edit Grub 2 configuration files

Grub2 does not use menu.lst. Instead, Certain boot options are stored in the file, /etc/default/grub, which may be edited by an administrator. See below, and https://help.ubuntu.com/community/Grub2 for details.

Step 1: Determine partitions

In a Terminal type

mount -l

This command lists all your mounted drives. Examine the output. Find the line that says something like

/dev/sda1 on / ...

Jot this down for use later in #Create Balsa boot entry file.

If you see a /home partition listed, jot it down as well. For example:

/dev/sda3 on /home ...

The green and blue elements may contain different characters. /dev/sda1 specifies which partition your main linux OS is installed on. sda designates the first storage device in your system (usually the internal hard drive); 1 and 3 specify which partition of that device functions as the root filesystem (on /), and home (on /home). If no /home line is in the list, don't worry; this means that your /home is inside of the / (root) partition, it doesn't live on a separate partition.

The computer's second storage device is designated sdb, etc. Each device may have more than one partition.

If the BalsaRoot SD card is mounted you'll see something like

/dev/sdb1 on /media/BalsaRoot type ext3 ...

Note: '"`UNIQ--toggledisplay-00000002-QINU`"'

Step 2: Create Balsa boot entry file

Boot menu items are entered in individual configuration files in the /etc/grub.d/ directory. The filenames in this directory are numbered in the order in which they should be read; so if you want the Balsa boot entry to appear at the top of the menu it should begin with a number between 06 and 09, because the automatically created Linux entries are specified in a file named 10_Linux.

For Balsa, create a new file with the required header info, by copying the supplied custom boot file. Then open this file, /etc/grub.d/06_balsa, for editing:

sudo cp /etc/grub.d/40_custom /etc/grub.d/06_balsa
sudo gedit /etc/grub.d/06_balsa

Leave the original lines in this file as they are. Copy the following lines, and paste them into the end of the file. Be careful to include the trailing "}" on the final line:

menuentry "Balsa on SD"{
recordfail
insmod ext2
search --no-floppy
set root=(hd0,1)
linux /boot/balsa/vmlinuz root=LABEL=BalsaRoot BALSAFILES=sda1 ro quiet splash
initrd /boot/balsa/initrd.img
}

Now we have to change the green and blue bits in the lines you just pasted in (they won't show up as colored in the text editor).

The blue is simpler. If you noted a separate /home partition above in Determine partitions, replace the blue sda1 with the partition number of your /home (sda3 in the example). So if /home is at sda3, the linux line in your 06_balsa file will contain

linux /boot/balsa/vmlinuz root=LABEL=BalsaRoot BALSAFILES=sda3 ro quiet splash

If you do not have a separate /home partition, enter the / partition you noted earlier (sda1 in the example).

The green text in parentheses, (hd0,1), is a bit trickier. We have to translate the / root partition designation, sda1, to a different format.

In (hd0,1) the 0 means 'first hard drive' and the 1 means 'first partition.'

If your / is on /sda2, you would specify

set root=(hd0,2)

If your / is on /sdb3, you would specify

set root=(hd1,3)

**Important note about a difference between Grub 2 and Grub 1: '"`UNIQ--toggledisplay-00000003-QINU`"'

Save the file 06_balsa and close the text editor. Make the file executable. In Terminal run:

sudo chmod +x /etc/grub.d/06_balsa_on_sd


For more details about specifications for the menuentry file, see [1]

Step 3: Set menu options for Grub 2's boot menu

Creating the 06_balsa menu file above will cause Balsa to boot by default. It will not change the default boot menu options; it's best to edit the grub2 configuration file--this is equivalent to the top portion of legacy grub's menu.lst.

sudo gedit /etc/default/grub
Make the Grub menu appear/be hidden by default

Place a hash mark(#) in front of this line to comment it out:

GRUB_HIDDEN_TIMEOUT=0
becomes
#GRUB_HIDDEN_TIMEOUT=0

If the hash mark is present the menu will not be hidden. If you are setting up this computer for a Balsa user, remove the hash mark to hide the grub menu; the user will not be offered a choice of boot options but will boot into Balsa by default.

If you want to change your default OS, change the value on this line:

GRUB_DEFAULT=0

Here, "0" refers to the first item in the grub2 menu, which is Balsa if you followed the above instructions. "1" refers to the second item, which is probably your standard Linux OS unless you've made previous changes in Grub.

So these settings:

GRUB_DEFAULT=1
#GRUB_HIDDEN_TIMEOUT=0
...
GRUB_TIMEOUT="10"

will cause the grub2 menu to appear for 10 seconds each time you boot the computer. The "Balsa on SD" menu item will appear first in the list, your latest Linux kernel menu item will be next, and will be highlighted, indicating that if you do nothing, this is the option that will boot after 10 seconds. You have those 10 seconds in which to choose a different item to boot.

These settings:

GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
...
GRUB_TIMEOUT="0"

will cause the computer to start Balsa automatically each time it is booted.

You can override this setting by holding down the SHIFT key while booting the computer, thus forcing the grub2 menu to appear. It's a bit tricky to know when to press the SHIFT key, however, and it may take a few tries to "catch" the right moment.

After you finish editing, save and close /etc/default/grub, then update the grub2 menu:

sudo update-grub

Restart the computer; boot into Balsa by selecting the item BALSA in the grub menu. See First time you boot into Balsa for more help.


For more information on the options available in /etc/default/grub, see Configuring Grub 2

Notes and sources

Alternate command-line method for changing grub2 default boot entry:

sudo grub-set-default 1 # sets GRUB_DEFAULT=1 in /etc/default/grub

I think you would have to run sudo update-grub again after this change


Supposedly, in Karmic and newer versions, a preferred update command to update-grub is

sudo grub -mkconfig

This command has various other options such as -o (produce an output file), -v (show version). When I tried it on my karmic machine (fresh karmic install which included grub2) it said, "the program 'grub' is not currently installed. You can install it with: sudo apt-get install grub". I'm not sure how grub2 is working without grub being installed... but it is.


Edit Grub 1 configuration file

More resources on Grub

Grub2 Worms Into Ubuntu, part 1
Cleaning up Your GRUB 2 Menu
Linux Commands for GRUB2
Community Documentation: GRUB2
Grub 1 (Grub Legacy) Manual
Grub 2 Manual