A journey on Linux from Console to Desktop

This post is aimed at giving an overview of the different elements, commands and configuration files that are involved when Linux switches from console to Desktop, with a focus in the context of Ubuntu 13.04. For an in-depth overview of X11 look at this great series of posts: Xplain.



The following is the structure of the software layers involved in the following discussion:
  • Kernel
  • Runlevel manager: Upstart, SysVinit or systemd
  • Display Manager: lightdm or gdm
  • X11 server: xorg
  • Session Manager/Shell: KDE or Gnome Shell
  • Window Manager/Compositor: compiz, metacity (Gnome), xfwm4 (XFCE) or xcompmgr
  • X11 Application


The execution structure can be summarized as follows:

  1. Upstarts manages the system events, and in particular the runlevel
  2. Display Manager Task (of upstarts) is invoked when entering runlevel 2
  3. The default Display Manager (DM) is activated when by the DM Task
  4. The DM launches the X11 server
  5. Then the Session Manager is started invoking also the Window Manager and other supporting applications

The first important concept is the one of Runlevel that is present since Unix System V. These are the runlevel values that correspond to different Linux states:
  • 0: halt
  • 1: single user
  • 2: graphical
  • 3-5: unused
  • 6: reboot


The runlevel is managed by the following commands:
  • “runlevel” to read it
  • “sudo init $runlevel” to switch to given runlevel
  • The default runlevel is in the file /etc/init/rc-sysinit.conf in the line “env DEFAULT_RUNLEVEL=2”

Upstart

Replaces the traditional /sbin/init daemon for providing services for starting and shutting down tasks and monitoring their execution. In Upstart everything is event based and based on D-Bus for exchanging events across the processes in the machine.


As the original init program the following paths are relevant:
  • /etc/init.d/
  • /etc/rc#.d/ where # is the runlevel. Except for special ones as “S”, that is invoked once per boot


The files in rc are links to the ones inside init.d. They are named , where K disables the task. Changes to these files have to be notified with “update-rc.d script defaults”.


With Upstarts the tasks are specified in the /etc/init folder. They have a special syntax that contains description of each service, properties, and events that trigger the execution of parts of the task. Commands can be sent to Upstart through DBus using the initctl command.

Display Manager Task of Upstart

The relevant task is present in /etc/init/lightdm.conf. This is the Upstart Task that is triggered by the raising of the runlevel. The specific start and stop actions are triggered by the following rules:


start on ((filesystem
       and runlevel [!06]
       and started dbus
       and plymouth-ready)
      or runlevel PREVLEVEL=S)


stop on runlevel [016]


The key action of this task is to run the current DM as specified by the file “/etx/X11/default-display-manager” that contains the full path. The default DM can be modified by means of “sudo dpkg-reconfigure ”.

Display Manager

The Display Manager is the service that manages the user graphical login at startup, when entering runlevel 2. The most common DM are gdm or lightdm. The latter is the default of Ubuntu. The current DM can be stopped as any service with “stop ” and started with “start”.


There are some options of the DM that can be customized. Autologin is enabled with “sudo /usr/lib/lightdm/lightdm-set-defaults --autologin username”. The script “/etc/lightdm/lightdm.conf” can be modified to specify the programs that are automatically launched at the different phases of session management: X start, Greeter start, User start. Finally the command line tool lightdm-set-defaults allows to specify several options.


The actions of lightdm are the same of gdm and provided by /etc/gdm. The actions are: Init PreSession PostLogin PostSession. They operate on various aspects of the X11 server.


The possibility to choose different session is provided throught the .desktop files present in /usr/share/xsessions. Each of these provides a STARTUP program that is different. For example Ubuntu session starts “gnome-session --session=ubuntu”.


X Server

The starting point is the execution of xinit, also invoked by startx. The xinit executes /etc/X11/xinit/xinitrc that invokes /etc/X11/Xsession. This script is important because it first runs all the scripts inside /etc/X11/Xsession.d that initialize the Window Manager and Desktop and finally it runs user scripts: .xsession, .xsessionrc and .Xsession.


The Xsession.d that contains several sub commands expressed as file name with the “priority” as the first two digits in the filename, similarly to init. Details about Xsession.d can be found here. The interesting scripts in Xsession.d are the following:


  • 55gnome-session_gnomerc: starts the Window Manager
  • 99upstart: starts the user session using “init --user”


Inside Ubuntu the script “50x11-common_determine-startup” determines the STARTUP program by looking at several option, although the default is the Session Manager pointed nby /usr/bin/x-session-manager.

Window Manager

The window manager provides services for moving the X11 windows around, comprising virtual desktops and compositing. Conceptually is over the X11 and it provides this service to also the Desktop shell, but it is activated from the Session Manager, and for this reason, although there is a link “/etc/alternatives/x-window-manager” it is controlled by the Session Manager configuration.


At high level the WM can be modified using the choosewm tool. Details about WMs can be found in the Debian wiki page.

Session Manager

The last element is the Desktop Shell that in X11 terms is called Session Manager. The default is provided by the link /etc/alternatives/x-session-manager and it selected by the Xsession.d script “55gnome-session_gnomerc” or provided by the Display Manager.


In particular for the Gnome session manager used by Ubuntu (gnome-session) there is an important “session” option that characterizes the behavior, and it is selected from the Display Manager. The sessions are stored in /usr/share/gnome-session/sessions. Each session describes a series of Desktop components as the Window Manager or the Panel and the associated executable. For example ubuntu.session is:


[GNOME Session]
Name=Ubuntu
RequiredComponents=gnome-settings-daemon;
RequiredProviders=windowmanager;panel;
DefaultProvider-windowmanager=compiz
DefaultProvider-panel=compiz
DesktopName=Unity


It has to be noted that in the case of the Gnome Display Manager (gdm), the user selection window is itself a session managed by gnome-session called “gdm-shell.session”.

Update: for the booting process check also Linux-Inside

Comments

Popular Posts