Dual Booting Ubuntu Linux & FreeBSD using the new Grub2

Ryan Wolf ryanjudaswolf at gmail.com
Sun Nov 1 08:31:14 UTC 2009


To the Ubuntu & FreeBSD Documentation Folks:

I spent all day trying to figure this out all because there isn't proper
documentation in books or on the internet on it, and no one's documentation
is up to date yet. After I figured it out, thought I would put together a
document so that others won't have to strain as hard as I did just
installing two operating systems that don't talk to each other to well. I
hope Ubuntu decides to have more support for access to BSD partitions, as it
doesn't seem to support it too well at the moment.

Enjoy the document guys. It's my first time writing one, so I hope it helps.

~ Ryan
-------------- next part --------------
31OCT09

* * *

Tutorial on dual booting Ubuntu 9.10 and FreeBSD 7.2-RELEASE
Created by Ryan Judas Wolf
ryanjudaswolf at gmail.com

* * *

Contents:

1. Proper Installation of the two operating systems
2. Discovering the Physical & Logical Drives in Ubuntu
3. Installing the Grub2 Bootloader
4. Configuration of Grub2; Files and Folders
	4.1 grub.cfg (/boot/grub/grub.cfg)
	4.2 grub (/etc/default/grub)
	4.3 /etc/grub.d/ (folder)
5. Adding Entries to Grub 2

* * *

1. Proper Installation of the two operating systems:

You can install the operating systems in any order, which is nice to know for the uninitiated. Just make sure that, when installing FreeBSD, to not install any bootloader and leave it untouched. If you make this mistake, you will have to reinstall FreeBSD at another time. In this case, run the live CD version of Ubuntu, and continue with the process below.

* * *

2. Discovering the Physical & Logical Drives in Ubuntu

A system typically boots from the hard disk at /dev/hda, /dev/sda, /dev/hdb, or /dev/sdb. Bring up a terminal session. The following command [parted] displays a list of the partitions that includes one with a boot flag:

	$ sudo parted /dev/hda print
	Error: Error opening /dev/hda: No medium found
	Retry/cancel? c

	wolf at Tsuhiro-Laptop:/$ sudo parted /dev/sda print
	[sudo] password for wolf: 
	Model: ATA Hitachi HTS54258 (scsi)
	Disk /dev/sda: 80.0GB
	Sector size (logical/physical): 512B/512B
	Partition Table: msdos

	Number  Start   End     Size    Type      File system     Flags
	 1      32.3kB  60.0GB  60.0GB  primary   ext4
 	 2      60.0GB  61.5GB  1497MB  extended
	 5      60.0GB  61.5GB  1497MB  logical   linux-swap(v1)
	 3      61.5GB  80.0GB  18.5GB  primary                   boot

	wolf at Tsuhiro-Laptop:/$ 

In this case, Ubuntu is installed on /dev/sda1, where /sda is the drive, and 1 is the primary partition that is is installed on. FreeBSD is installed on 
/dev/sda3.

* * *

3. Installing the Grub2 Bootloader

The following is to install/reinstall the Master Boot Record. You can take this approach if you installed Ubuntu first, then FreeBSD second on a separate partition on the same harddisk. 

	1. Boot the system using an Ubuntu Live (installation) CD/DVD
	2. Open a terminal emulator window (Menubar: Applications->
	   Accessories->Terminal).
	3. Give the following commands, substituting the name of the device
	   that holds the root partition that you found from the [parted]
	   command (e.g., /dev/sda1) for the example below '/dev/xxx/'. 
	   Substitute the name of the primary drive that you want to install
	   the MBR on (e.g., /dev/sda) for '/dev/yyy'. 
	4. If your linux primary drive is not currently mounted, you will have
	   to mount it with. Do not forget to unmount /mnt when done.
		
		a. If the system does not have a separate 'boot' partition, use
		   these commands:

		$ sudo mount /dev/xxx /mnt
		$ sudo grub-install --root-directory=/mnt /dev/yyy
		Installation finished. No error reported.
		This is the contents of the device map /mnt/boot/grub/device.map.
		Check if this is correct or not. If any of the lines is incorrect,
		fix it an re-run the script 'grub-install'.
		$ cat /boot/grub/device.map
		(hd0)  /dev/sda

	This last line is important to remember. You will be using it when you go to configure your system for use with FreeBSD. Copy it down at this time. Remembering to unmount your physical drive.

		$ sudo umount /mnt

		b. If the system *does* have a separate boot partition, use these commands, substituting the name of the device driver that holds the boot partition (e.g., /dev/sda1) for /dev/zzz:

		$ sudo mount /dev/xxx /mnt
		$ sudo mount /dev/zzz /mnt/boot
		$ sudo grub-install --root-directory=/mnt /dev/yyy
		Installation finished. No error reported.
		This is the contents of the device map /mnt/boot/grub/device.map.
		Check if this is correct or not. If any of the lines is incorrect,
		fix it and re-run the script 'grub-install'.
		$ cat /boot/grub/device.map
		(hd0)   /dev/sda
		$sudo umount /mnt
	
	5. Reboot the system. Remove the CD/DVD when the system asks you to.

	6. Continue to reboot the system of the hard drive you specified in the place of /dev/xxx.

If you ever have problems again, it is easy enough to run:

	$ sudo grub-install /dev/sda

Then make sure it is installed properly:

	$ cat /boot/grub/device.map
	(hd0)   /dev/sda
	
* * *	

4. Configuration of Grub2; Files and Folders:		

There are two locations that need to be known about for the new Grub2 bootloader, which is inconsistent with the original Grub 0.97 bootloader

/boot/grub/	- Main directory holding the Grub2 program
/etc/grub.d/    - Configuration directory for bootloader

While the main grub bootloader file continues to reside in the /boot/grub folder, it is no longer Grub's familiar menu.lst. The main Grub 2 instruction file is now grub.cfg. This file is produced by various scripts run when either the "update-grub" or "update-grub2" command is executed. The files primarily responsible for the content of grub.cfg are /etc/default/grub and individual script files located in /etc/grub.d/

The program name "update-grub2" is provided for compatibility with older versions of the grub2 packaging, but from now on this document will only use the name "update-grub".

Many of the files in /boot/grub will not be familiar to users of Grub Legacy. Especially noticeable are the multitude of *.mod files in the /boot/grub folder. Grub 2 is modular and the *.mod files are loaded as necessary by grub. Despite the addition of these files, the total size of the /boot/grub contents is fairly close to that of Grub Legacy and should not require a larger /boot partition. 

* * *

4.1 grub.cfg (/boot/grub/grub.cfg)

DO NOT EDIT THIS FILE This is the main Grub 2 file. It "replaces" Grub Legacy's /boot/grub/menu.lst. This file contains the Grub menu instructions. Unlike Grub Legacy's menu.lst file, grub.cfg is NOT MEANT TO BE EDITED!!!

    * grub.cfg is automatically generated or updated whenever "update-grub" is executed. The command must be run as root.

    * The scripts used to generate grub.cfg include the files contained in /etc/grub.d and information gathered from /etc/default/grub

    * The file is divided into sections. Each section of the file is clearly delineated and is recognizable by the section header ### BEGIN. The information on this line references the file in the /etc/grub.d folder which is used to incorporate the information into grub.cfg

    * By default, and whenever the "update-grub" command is executed, this file is made read-only. This is in keeping with the intent that the file should not be edited manually. If you must edit this file, instructions are provided later on this page.

    * Sample grub.cfg including two Ubuntu kernels, memtest86+, Windows and a custom entry (41_srcd) imported from an /etc/grub.d/ script: 

      #
      # DO NOT EDIT THIS FILE
      #
      # It is automatically generated by /usr/sbin/update-grub using templates
      # from /etc/grub.d and settings from /etc/default/grub
      #

      ### BEGIN /etc/grub.d/00_header ###
      set default=0
      set timeout=5
      set root=(hd0,5)
      search --fs-uuid --set b02e1934-12dd-418a
      if font /usr/share/grub/ascii.pff ; then
        set gfxmode=640x480
        insmod gfxterm
        insmod vbe
        terminal gfxterm
      fi
      ### END /etc/grub.d/00_header ###

      ### BEGIN /etc/grub.d/05_debian_theme ###
      set menu_color_normal=cyan/blue
      set menu_color_highlight=white/blue
      ### END /etc/grub.d/05_debian_theme ###

      ### BEGIN /etc/grub.d/10_hurd ###
      ### END /etc/grub.d/10_hurd ###

      ### BEGIN /etc/grub.d/10_linux ###
      set root=(hd0,5)
      search --fs-uuid --set b02e1934-12dd-418a-be3a-9ff7d3e7e7ea
      menuentry "Ubuntu, linux 2.6.28-13-generic" {
              linux   /boot/vmlinuz-2.6.28-13-generic root=UUID=b02e1934-12dd-418a ro  quiet splash 			vga800
              initrd  /boot/initrd.img-2.6.28-13-generic
      }
      menuentry "Ubuntu, linux 2.6.28-13-generic (single-user mode)" {
              linux   /boot/vmlinuz-2.6.28-13-generic root=UUID=b02e1934-12dd-418a ro single 
              initrd  /boot/initrd.img-2.6.28-13-generic
      }
      menuentry "Ubuntu, linux 2.6.28-11-generic" {
              linux   /boot/vmlinuz-2.6.28-11-generic root=UUID=b02e1934-12dd-418a ro  quiet splash 			vga800
              initrd  /boot/initrd.img-2.6.28-11-generic
      }
      menuentry "Ubuntu, linux 2.6.28-11-generic (single-user mode)" {
              linux   /boot/vmlinuz-2.6.28-11-generic root=UUID=b02e1934-12dd-418a ro single 
              initrd  /boot/initrd.img-2.6.28-11-generic
      }
      ### END /etc/grub.d/10_linux ###

      ### BEGIN /etc/grub.d/20_memtest86+ ###
      menuentry "Memory test (memtest86+)" {
              linux   /boot/memtest86+.bin
      }
      menuentry "Memory test (memtest86+, serial console 115200)" {
              linux   /boot/memtest86+.bin console=ttyS0,115200n8
      }
      ### END /etc/grub.d/20_memtest86+ ###

      ### BEGIN /etc/grub.d/30_os-prober ###
      menuentry "Microsoft Windows XP Home Edition (on /dev/sda1)" {
              set root=(hd0,1)
              chainloader +1
      }
      ### END /etc/grub.d/30_os-prober ###

      ### BEGIN /etc/grub.d/40_custom ###
      # This file is an example on how to add custom entries

      ### END /etc/grub.d/40_custom ###

      ### BEGIN /etc/grub.d/41_srcd ###
      menuentry "SystemRescue CD on hard drive" {
              set root=(hd0,10)
              linux   /sysrcd/rescuecd subdir=sysrcd setkmap=us
              initrd  /sysrcd/initram.igz
      } 
      ### END /etc/grub.d/41_srcd ###

* * *

4.2 grub (/etc/default/grub)

This file contains information previously found in the upper section of /boot/grub/menu.lst. It contains settings primarily affecting Grub's menu display. This file can be edited by root to make changes to these settings; they will be imported into grub.cfg when "update-grub" is executed.

    

      # If you change this file, run 'update-grub' afterwards to update
      # /boot/grub/grub.cfg.

      GRUB_DEFAULT=0
      GRUB_HIDDEN_TIMEOUT=0
      GRUB_HIDDEN_TIMEOUT_QUIET=true
      GRUB_TIMEOUT=10
      GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
      GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
      GRUB_CMDLINE_LINUX=""

      # Uncomment to disable graphical terminal (grub-pc only)
      #GRUB_TERMINAL=console

      # The resolution used on graphical terminal
      # note that you can use only modes which your graphic card supports via VBE
      # you can see them in real GRUB with the command `vbeinfo'
      #GRUB_GFXMODE=640x480

      # Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
      #GRUB_DISABLE_LINUX_UUID=true

      # Uncomment to disable generation of recovery mode menu entrys
      #GRUB_DISABLE_LINUX_RECOVERY="true"



      This file contains information formerly contained in the upper section of Grub Legacy's ''menu.lst'' and items contained on the end of the kernel line. The items in this file can be edited by a user with administrator (root) privileges. Grub developers have have placed explanatory comments within the file itself.

    * GRUB_DEFAULT=0

          o Sets the default and pre-selected menu entry. Entries may be numeric or saved

          o    GRUB_DEFAULT=0
                + Sets the default menu entry by menu position. As with Grub Legacy, the first "menuentry" in grub.cfg is 0, the second is 1, etc. 

          o    GRUB_DEFAULT=saved
                + Sets the default menu entry with whatever was selected on the last boot. If the menu is displayed during boot, the previously selected option will be highlighted. If no action is taken, this is selection which will be booted at the end of the timeout, or if the 	menu is hidden. 

    * GRUB_TIMEOUT=5

          o No change from Grub Legacy. This is the number of seconds before the default entry is automatically booted. To have your choice, you probably want to change this to a greater value. Setting a value of -1 will display the menu until the user makes a selection (no timeout). 

    * GRUB_HIDDEN_TIMEOUT=0
          o The menu will be hidden unless a # symbol is present at the beginning of this line. ( # GRUB_HIDDEN_TIMEOUT=0 ). You need to modify this in order to see the bootloader when you turn on your system. That said, put the # in front of it.

          o The default setting initially depends on the presence of other operating systems.
                + Another OS Detected: The menu will be displayed. ( The line will begin with a # symbol. )
                + No other OS Detected: The menu will be hidden. 

          o For integers greater than 0, the system will pause, but not display the menu, for the entered number of seconds.
          o 0 The menu will not be displayed. There will be no delay.
                + When this entry is set to 0:

                      # The user may force displaying the menu as the computer boots by holding down the SHIFT key.
                            * During boot, the system will check the SHIFT key status. If it cannot determine the key status, a short delay will enable the user to display the menu by pressing the ESC key. 

                      # If enabled, the splash screen designated in 05_debian_theme will be displayed even if the hidden menu feature is 				selected. 

    * GRUB_HIDDEN_MENU_QUIET=true
          o true - No countdown is displayed. The screen will be blank.
          o false - A counter will display on a blank screen for the duration of the GRUB_HIDDEN_TIMEOUT value. 

    * GRUB_DISTRIBUTOR=lsb_release -i -s 2> /dev/null || echo Debian
          o Determines the descriptive name in the menu entry. (Ubuntu, Xubuntu, Debian, etc.) 

    * GRUB_CMDLINE_LINUX
          o If it exists, this line imports any entries to the end of the linux command line (Grub Legacy's "kernel" line) for both normal and recovery modes. This is similar to the "altoptions" line in menu.lst 

    * GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
          o This line imports any entries to the end of the 'linux' line (Grub Legacy's "kernel" line). The entries are appended to the end of the normal mode only. This is similar to the "defoptions" line in menu.lst.  If you want a black screen with scrolling boot messages, remove "quiet splash". If you want a grub splash screen with limited messages underneath the Ubuntu logo, use only "splash". 
	  	  < This [only "splash"] will not work in Karmic during boot as the xsplash does not display the messages , but the messages at shutdown will be displayed. 

    * #GRUB_TERMINAL=console
          o Uncomment to disable graphical terminal (grub-pc only). 

    * #GRUB_DISABLE_LINUX_UUID=true
          o Uncomment this line if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux. 

    * GRUB_DISABLE_LINUX_RECOVERY=true
          o Add or uncomment this line to prevent "Recovery" mode kernel options from appearing in the menu. 

    * GRUB_DISABLE_OS_PROBER=true
          o Enables/disables the os-prober check of other partitions for operating systems, including Windows, Linux, OSX and Hurd. 

This is an example of what your file should look like after modified:

	# If you change this file, run 'update-grub' afterwards to update
	# /boot/grub/grub.cfg.

	GRUB_DEFAULT=0
	#GRUB_HIDDEN_TIMEOUT=10
	GRUB_HIDDEN_TIMEOUT_QUIET=false
	GRUB_TIMEOUT="10"
	GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
	GRUB_CMDLINE_LINUX_DEFAULT="splash"
	GRUB_CMDLINE_LINUX=""

	# Uncomment to disable graphical terminal (grub-pc only)
	#GRUB_TERMINAL=console

	# The resolution used on graphical terminal
	# note that you can use only modes which your graphic card supports via VBE
	# you can see them in real GRUB with the command `vbeinfo'
	GRUB_GFXMODE=1280x800

	# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
	#GRUB_DISABLE_LINUX_UUID=true

	# Uncomment to disable generation of recovery mode menu entrys
	#GRUB_DISABLE_LINUX_RECOVERY="true"

PLEASE NOTE: If you want to change the resolution of your menu, make sure that you know the proper resolutions that your computer can run at before modifying. If you are unsure, even in the slightest, leave the defaults at 640x480

* * *

4.3 /etc/grub.d/ (folder)

The files in this folder are read during execution of '"update-grub"'command. The scripts in these files export the information they gather into /boot/grub/grub.cfg. The files are run in sequential order - files with names starting with a numeral are run first, followed by files whose name begins with a letter. The order the file is run determines the location of items in the grub menu.

Custom entries can be added to the 40_custom file or placed in a new file.

Any file created must be executable in order to be included in the grub.cfg file during the "update-grub" command. This can be accomplished from the terminal with "sudo chmod u+x /etc/grub.d/filename". The following is a very brief look at what the files accomplish.

    * 00_header
          o Loads settings from /etc/default/grub, including visual presentations, 		    timeout, and terminal options. 
    * 05_debian_theme
          o Sets background, text colors, and themes. 
    * 10_hurd
          o Locates hurd kernels. 
    * 10_linux
          o Locates kernels based on the distributor determined by the "lsb_release 		    -i -s" results ("Ubuntu"). 
    * 20_memtest86+
          o If the file /boot/memtest86+.bin exists, it is included as a menu item. 
    * 30_os-prober
          o Searches for other OS's and includes them in the menu. 
    * 40_custom
          o A template for adding custom menu entries which will be inserted into grub.cfg upon execution of the "update-grub" command. This and any other custom files must be made executable to allow importation into grub.cfg. These files are placed in the Grub menu in the numerical order of the file name. 

* * *

5. Adding Entries to Grub 2

grub.cfg is updated when update-grub is run. Changes to grub.cfg should be made to the appropriate script files and not to the grub.cfg file itself.

NOTE: The first comment in grub.cfg is "DO NOT EDIT THIS FILE". The file is read-only and is returned to this status anytime update-grub is run. 

The file that you need to modify is in the /etc/grub.d folder. In your favorite editor, edit 40_custom and include the following information into it:

	#!/bin/sh
	exec tail -n +3 $0
	# This file provides an easy way to add custom menu entries.  Simply type the
	# menu entries you want to add after this comment.  Be careful not to change
	# the 'exec tail' line above.
	menuentry "FreeBSD 7.2-RELEASE i386" {
      	  set root=(hd0,3)
      	  chainloader +1
	}

Make sure to leave no spaces in the document, and copying the information thereafter 'menuentry' verbatim to the document. Make sure that your installation of FreeBSD is installed onto onto the proper drive (if that is hd0, make it so), and that the correct partition is designated (found from the [parter] command at the beginning of this document).

Once this is done, upgrade Grub2:

	wolf at Tsuhiro-Laptop:/$ sudo update-grub
	Generating grub.cfg ...
	Found linux image: /boot/vmlinuz-2.6.31-14-generic
	Found initrd image: /boot/initrd.img-2.6.31-14-generic
	Found memtest86+ image: /boot/memtest86+.bin
	done
	wolf at Tsuhiro-Laptop:/$ 

Your new amendment may or may not be recognized. So far, the os-probe that is with Grub2 does not recognize FreeBSD partitions. If you want to confirm that it is now in your grub.cfg file, just [cat] it to make sure, looking for the '40_custom' at the bottom for it. When you reboot, you should see the option for FreeBSD as you typed it. 

* * *

Enjoy your new dualboot system! If you have any questions, feel free to email me at ryanjudaswolf at gmail.com

Bibliography:
A Practical Guide to Ubuntu Linux, Mark G. Sobell p.430, 574
https://wiki.ubuntu.com/Grub2#User-defined%20Entries 

* * *





More information about the freebsd-doc mailing list