Developing without a Netbook -- Using the chroot


Est time: First-time setup: 20mins, 1min afterward

If you've done this once already, bookmark the cheat sheet.

Use this tutorial if:DevWithChroot_smallth.jpg
  • You don't need an IDE such as Anjuta or Eclipse. (Use emacs or vi)
  • You are familiar with gdb and Linux command-line use
  • You don't need your application to interact with the Moblin desktop. Your application will run stand-alone in a window on the desktop. See screenshot.
  • Your system uses an Intel graphics chipset $ lspci | grep "VGA compatible controller"
  • You have administrative privileges on your Linux desktop (sudo power)

Steps to Setup

  1. Download Moblin 2.1 development image from moblin.org
  2. Create chroot using the Moblin 2.1 development image

    We will create a "chroot" (change root) environment of Moblin 2.1. This extracts the Moblin 2.1 file system to a subdirectory on your desktop and opens a terminal window in that new environment. Working inside this terminal is the same as working inside the Moblin 2.1 environment and allows you to build and run your application as if it were on a Moblin 2.1 device.

    First time: $ sudo moblin-chroot -s <destination path> <image file>  Example: $ sudo moblin-chroot -s /opt/mobln2.1 /home/bob/Downloads/moblin-2.1-final-20091103-002.iso  After first time: $ sudo moblin-chroot <destination path> To exit chroot terminal, type "exit"

    (Optional) Change color of the chroot prompt
  3. Open a 2nd terminal window

    You now have 2 terminal windows open. Inside one is the chroot Moblin 2.1 environment and inside the other is the normal desktop environment. All instructions after this are either:
    "From chroot" -- meaning inside the chroot terminal
    "From desktop" -- meaning inside a normal desktop terminal
  4. (From desktop) Map desktop dbus to the chroot dbus

    $ sudo mount --bind <your desktop dbus dir> <chroot dbus dir> Example: $ sudo mount --bind /var/lib/dbus/ /opt/moblin2.1/var/lib/dbus/   
  5. (From chroot) Create a "src" directory for your applications source

    $ mkdir /root/src
  6. (From desktop) Mount your source directory to the chroot

    $ sudo mount --bind <source dir> <new chroot source dir from step 5> Example: $ sudo mount --bind /home/bob/src/ /opt/moblin2.1/root/src 
  7. (From desktop) Do all code editing and source control management

    From the desktop terminal, run emacs or your favorite editor. Also check-in and out your source to your version control repository. This is the same as normal development, the only difference is that you will be building and running your application from the chroot terminal. $ git clone ...   or  $ svn checkout ... 
  8. (From desktop) Allow applications started in the chroot terminal to display output on the desktop

    $ xhost +SI:localuser:root
  9. (From chroot) Build and run

    In the chroot you are now ready to build your application and run it. Getting it to build the first time might be tricky as the libraries and versions in Moblin 2.1 may be different from your desktop Linux OS. Some libraries may not even be present in Moblin 2.1. You can use "yum search ..." and "yum install ..." from the chroot terminal to search for and install packages from the Moblin 2.1 repository. If your application builds and runs in the Moblin chroot it will run on a Moblin 2.1 community distribution (See compliance note below)

    Build
    $ gcc helloworld.c -o helloworld Run
    $ ./helloworld
Congratulations That's it. You are ready to create and debug your awesome, world-changing application.

Additional / Optional Tips and Tricks

Setting app screensize for debug and production

Moblin applications are recommended to run full screen on the netbook. Standard netbook screensize is 1024x600. When debugging you will also want the application to run at 1024x600 which is probably not fullscreen on the desktop.

Use the following code to enable your application to run in a 1024x600 window if running on the desktop and fullscreen if running on a netbook-sized monitor.

Using GTK

#include <X11/Xutil.h>
#define SCN_WIDTH  1024
#define SCN_HEIGHT 600
...
void set_screen_size (GtkWindow *main_window)
   //get screen dimensions
   Screen *screen = XDefaultScreenOfDisplay(XOpenDisplay(NULL));
   int scn_width = XWidthOfScreen(screen);
   int scn_height = XHeightOfScreen(screen);

   //if screensize is larger than netbook size, show in window, else fullscreen
   if (scn_width > SCN_WIDTH || scn_height > SCN_HEIGHT) {
      scn_width = SCN_WIDTH;
      scn_height = SCN_HEIGHT;
      gtk_window_resize (main_window, scn_width, scn_height);
   } else {
      gtk_window_fullscreen (main_window);
      gtk_window_set_decorated (main_window, false);  //important for Moblin
   }

Using Clutter

#include <X11/Xutil.h>
#include <clutter/x11/clutter-x11.h>
#include <gdk/gdk.h>
#define SCN_WIDTH  1024
#define SCN_HEIGHT 600
...
void set_screen_size (ClutterActor *stage)
{
   //get screen dimensions
   Screen *screen = XDefaultScreenOfDisplay(XOpenDisplay(NULL));
   int scn_width = XWidthOfScreen(screen);
   int scn_height = XHeightOfScreen(screen);
   if (scn_width > SCN_WIDTH || scn_height > SCN_HEIGHT) {
      clutter_stage_set_fullscreen (CLUTTER_STAGE(stage), FALSE);
      scn_width = SCN_WIDTH;
      scn_height = SCN_HEIGHT;
   } else {
      Window win = clutter_x11_get_stage_window(CLUTTER_STAGE (stage));
      GdkWindow *gdk_win = gdk_window_foreign_new(win);
      gdk_window_set_decorations (gdk_win, 0);
   }
   clutter_actor_set_size(stage, scn_width, scn_height);
}

A note about Moblin Compliance

The Moblin OEM devices ship OSes from Moblin-compliant distributions. These distributions are guaranteed to include the core Moblin 2.1 libraries. However, there are many libraries in the Moblin 2.1 image (and therefore the chroot) which are not part of the compliant core. If your application depends on libraries outside this set, you don't have the same guarantee that they will be available on all OEM devices. For more on compliance and the core library set, see Moblin Compliance

(From chroot) Change color of prompt

It is nice to be able to identify the chroot terminal from the other terminals on a busy desktop. You can do this by changing the color or style of the prompt. Do this by changing the value of $PS1.

Append the following lines to /root/.bashrc
BLUE='\[\033[01;34m\]'
BLACK='\[\033[00m\]'
PS1=$BLACK'['$BLUE'\u@Project '$BLUE'\W'$BLACK']\$ '
export PS1
Reload .bashrc to set the new values: # . /root/.bashrc  This must do this step each time after entering chroot

Note: these color values work best for terminals with white text on black backgrounds. If you use another color scheme for your terminals you may want to play with the values.

Other useful colors that look good for white on black terminals:
RED='\[\033[01;31m\]'
GREEN='\[\033[01;32m\]'
YELLOW='\[\033[01;33m\]'