Difference between revisions of "Clean Core"

From LinuxMCE
Jump to: navigation, search
(that should do it)
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 +
{{versioninfo|710Status=Relevant to 0710 and older|710UpdatedDate=2nd June 2011|710UpdatedBy=purps|810Status=Not applicable|810UpdatedDate=2nd June 2011|810UpdatedBy=purps|1004Status=Not applicable|1004UpdatedDate=3rd August 2011|1004UpdatedBy=foxi352}}
 +
[[Category:LinuxMCE Configuration]]
 +
{| align="right"
 +
  | __TOC__
 +
  |}
 +
 +
 
== Introduction ==
 
== Introduction ==
  
This page is meant to document the efforts to turn the LMCE core into one or more "services" that are automaticly started at boot time.  In other words we would like to be able to boot a LinuxMCE machine without using the Launch Manager application, and the dependency uppon a graphic environment, at least for starting a (dedicated) core.
+
This page is meant to document the efforts to turn the LinuxMCE core into one or more Linux "services" that are automatically started at boot time.  In other words, we would like to be able to boot a dedicated LinuxMCE Core server without using the Launch Manager application and without the dependency upon a graphic environment.
  
Due to the lack of documentation we will have to figure a lot of things out on our own.  Please don't be shy to add any related knowledge you have, we don't really know what we're doing, so why should you?  At least by adding stuff here we'll be able to build uppon each others experience without having to reverse-engineer the wheel every time.  And who knows, one day we might even achieve our common goal.
+
Please don't be shy to add any related knowledge you have.
 +
 
 +
{{p}}
  
 
== Runlevels ==
 
== Runlevels ==
  
Runlevels are an easy way to change how the system boots.  LinuxMCE uses runlevel 2 for normal operation (depending on the selections you made during installation) and runlevel 1 is used for ''rescue mode''.  The runlevels 0 and 6 are used for doing a ''shutdown'' or a ''reboot''.  Which leaves the runlevels 3, 4 and 5 free for us to experiment with.
+
Runlevels are an easy way to change how the system boots.  LinuxMCE (as Kubuntu) uses runlevel 2 for normal operation (depending on the selections you made during installation) and runlevel 1 is used for ''rescue mode''.  The runlevels 0 and 6 are used for doing a ''shutdown'' or a ''reboot''.  Which leaves the runlevels 3, 4 and 5 free for us to experiment with.
  
In the end, when everything is confirmed working, I would like to propose the following scheme:
+
As an example we will be setting up the following scheme in this document:
 
# rescue  (as it is now)
 
# rescue  (as it is now)
# KDE  (as it was installed by Kubuntu, like '''mainly-PC''' is now)
+
# KDE  (as installed by Kubuntu, like '''mainly-PC''' is now)
# Core  (as in textmode, not fully working yet)
+
# Core  (as in textmode, ideal for headless systems)
# Core + KDE  (at the moment the LaunchManager still needs to be started for full operation)
+
# Core + KDE  (best of both worlds)
# Core + LaunchManager  (as the current '''dedicated''' core or hybrid is now)
+
# LaunchManager  (as the classic '''dedicated-MC''' setup is now)
  
For now, just adapt the examples to your wishes.
+
Of course you can just adapt the example to your specific wishes or needs.
 +
 
 +
{{p}}
  
 
=== Starting another runlevel ===
 
=== Starting another runlevel ===
Line 25: Line 36:
 
The "runlevel" command will show you the previous and current runlevel.   
 
The "runlevel" command will show you the previous and current runlevel.   
  
=== Selecting a runlevel at boottime ===
+
=== Setting up the runlevels ===
 +
 
 +
Traditionally the runlevels are controlled by manipulating a set of "symbolic links" in the ''/etc/rc'''X'''.d/'' directory, where '''X''' is the runlevel (0-6,S), however the Kubuntu system uses upstart, which has other ways as well.
 +
 
 +
When you are done with this you can switch to a different runlevel using one of the methods described elsewhere in this document, be sure to reboot in order to make a clean start.
 +
 
 +
Before starting it is always wise to make a backup of a few things, just in case:
 +
tar cjvf ~/backup-`date '+%Y%m%d:%H%M'`.tar.bz2 /etc/init.d /etc/rc*.d
 +
 
 +
{{p}}
 +
 
 +
==== Preparation ====
 +
 
 +
Here are additional scripts needed to start the core devices, normally these task are performed by the launch-manager and it's startup script.
 +
 
 +
First a script for init (upstart):
 +
cd /
 +
cat >etc/init.d/core <<"EOF"
 +
#!/bin/sh
 +
 +
case "$1" in
 +
        start)
 +
                /usr/pluto/bin/startCore.sh
 +
                ;;
 +
        stop)
 +
 +
                /usr/pluto/bin/StopCoreServices.sh
 +
                ;;
 +
 +
        *)
 +
                echo "Usage: /etc/init.d/core {start|stop}"
 +
                exit 1
 +
                ;;
 +
esac
 +
 +
exit 0
 +
EOF
 +
chmod +x etc/init.d/core
 +
 +
 
 +
Which calls a script that determines the device number of the core device:
 +
 
 +
cd /
 +
cat >usr/pluto/bin/startCore.sh <<"EOF"
 +
#!/bin/sh
 +
 +
BASEDIR=/usr/pluto/bin
 +
DeviceTemplate_Core=7
 +
 +
source $BASEDIR/SQL_Ops.sh
 +
 +
$BASEDIR/Config_Device_Changes.sh
 +
$BASEDIR/Start_OrbiterGen.sh
 +
$BASEDIR/UpdateMediaDaemonControl.sh -enable
 +
 +
$BASEDIR/Start_DCERouter.sh
 +
 +
$BASEDIR/checkforRaids.sh
 +
$BASEDIR/UpdateAvailableSerialPorts.sh
 +
 +
QUERY="SELECT PK_Device FROM Device JOIN DeviceTemplate ON Device.FK_DeviceTemplate=DeviceTemplate.PK_DeviceTemplate WHERE PK_DeviceTemplate=${DeviceTemplate_Core}"
 +
RESULT=$(RunSQL "$QUERY")
 +
for ROW in $RESULT
 +
do
 +
        CoreDev=$(Field 1 "$ROW")
 +
        $BASEDIR/startDevices.sh $CoreDev
 +
done
 +
 +
EOF
 +
chmod +x usr/pluto/bin/startCore.sh
 +
 +
 
 +
And in turn calls this script to start the whole device family, excluding any media directors:
 +
 
 +
cd /
 +
cat >usr/pluto/bin/startDevices.sh <<"EOF"
 +
#!/bin/sh
 +
 +
 +
BASEDIR=/usr/pluto/bin
 +
DeviceTemplate_MeDi=28
 +
 +
source $BASEDIR/SQL_Ops.sh
 +
 +
 +
DEVICES=$*
 +
 +
 +
for DEVICE in $DEVICES
 +
do
 +
 +
 +
        QUERY="SELECT ImplementsDCE, IsPlugin, Disabled, IF(PK_DeviceTemplate=${DeviceTemplate_MeDi},1,0) AS IsMeDi, REPLACE(DeviceTemplate.Description,' ','_') AS Description, CommandLine FROM Device JOIN DeviceTemplate ON Device.FK_DeviceTemplate=DeviceTemplate.PK_DeviceTemplate WHERE PK_Device=${DEVICE}"
 +
 +
        RESULT=$(RunSQL "$QUERY")
 +
        for ROW in $RESULT
 +
        do
 +
                IMPLEMENTSDCE=$(Field 1 "$ROW")
 +
                ISPLUGIN=$(Field 2 "$ROW")
 +
                DISABLED=$(Field 3 "$ROW")
 +
                ISMEDI=$(Field 4 "$ROW")
 +
                DESCRIPTION=$(Field 5 "$ROW")
 +
                COMMANDLINE=$(Field 6 "$ROW")
 +
 +
#              echo $RESULT, $IMPLEMENTSDCE $ISPLUGIN $DISABLED $ISMEDI $DESCRIPTION \"$COMMANDLINE\"
 +
 +
                if [ "$ISMEDI" != "0" ]
 +
                then
 +
                        echo "Device $DEVICE ($DESCRIPTION) is a Media Director"
 +
                        continue
 +
                fi
 +
 +
 +
 +
                if [ "$IMPLEMENTSDCE" = "0" ]
 +
                then
 +
 +
                        echo "Device $DEVICE ($DESCRIPTION) no DCE, going after the children"
 +
 +
                        CHILDREN=""
 +
                        QUERY="SELECT PK_Device FROM Device WHERE FK_Device_ControlledVia=$DEVICE"
 +
                        RESULT=$(RunSQL "$QUERY")
 +
                        for ROW in $RESULT
 +
                        do
 +
                                CHILD=$(Field 1 "$ROW")
 +
                                CHILDREN="$CHILDREN $CHILD"
 +
                        done
 +
 +
 +
                        if [ "$CHILDREN" ]
 +
                        then
 +
 +
                                $0 $CHILDREN
 +
                        else
 +
                                echo "Device $DEVICE ($DESCRIPTION) has no children"
 +
                        fi
 +
 +
                else
 +
 +
                        if [ "$ISPLUGIN" = "0" ]
 +
                        then
 +
 +
                                if [ "$DISABLED" = "0" ]
 +
                                then
 +
 +
                                        if [ -z "$COMMANDLINE" ]
 +
                                        then
 +
                                                COMMANDLINE=$DESCRIPTION
 +
                                        fi
 +
 +
                                        if [ -x "$BASEDIR/${COMMANDLINE}" ]
 +
                                        then
 +
 +
#                                              echo "/usr/bin/screen -d -m -S ${DESCRIPTION}-${DEVICE} $BASEDIR/Spawn_Device.sh ${DEVICE} ${DCERouter} ${COMMANDLINE}"
 +
                                                /usr/bin/screen -d -m -S ${DESCRIPTION}-${DEVICE} $BASEDIR/Spawn_Device.sh ${DEVICE} ${DCERouter} ${COMMANDLINE}
 +
                                                echo "Device $DEVICE ($DESCRIPTION) started  ${COMMANDLINE}"
 +
 +
                                        else
 +
                                                echo "Device $DEVICE ($DESCRIPTION) unable to start ${COMMANDLINE}"
 +
                                        fi
 +
 +
 +
 +
                                else
 +
                                        echo "Device $DEVICE ($DESCRIPTION) is disabled"
 +
                                fi
 +
 +
 +
                        else
 +
                                echo "Device $DEVICE ($DESCRIPTION) is a plugin"
 +
                        fi
 +
 +
                fi
 +
 +
        done
 +
 +
done
 +
 +
EOF
 +
chmod +x usr/pluto/bin/startDevices.sh
 +
{{p}}
 +
 
 +
===== On a Mainly Personal Computer installation =====
 +
 
 +
If you chose this install type you will need to create an extra startup script.  In other words: when your core machine starts up with a KDE desktop.  Here is a slightly simplified version of the one included in the "Dedicated Media Center" install type:
 +
 
 +
cd /
 +
cat >etc/init.d/launch-manager <<"EOF"
 +
#!/bin/sh
 +
if [ "$1" == "start" ]
 +
then
 +
        /usr/pluto/bin/Startup_Core-Hybrid.sh
 +
fi
 +
 +
EOF
 +
chmod +x etc/init.d/launch-manager
 +
{{p}}
 +
 
 +
===== On a Dedicated Media Center installation =====
 +
 
 +
 
 +
To make things a little bit easier for ourselves we're going to move a few things around a bit, this should give us about the same starting point as when using a "Mainly Personal Computer" setup.  Do this if your core machine normally starts up with the launch manager:
 +
 
 +
cd /
 +
mv -iv etc/init.d/kdm etc/init.d/launch-manager
 +
cp -iv etc/init.d/kdm.saved etc/init.d/kdm
 +
{{p}}
 +
 
 +
==== Changing the symlinks ====
 +
 
 +
In order to make the system boot only the core in runlevel 3, simply copy the symlinks of runlevel 2 and replace the ones that start KDE and the AVWizard with a symlink to the init script we created earlier during the preparations:
 +
cd /
 +
rm -rfv etc/rc3.d/*
 +
cp -av etc/rc2.d/* etc/rc3.d/
 +
ln -sfv ../init.d/core etc/rc3.d/S99core
 +
rm -fv etc/rc3.d/S99kdm etc/rc3.d/S990start_avwizard
 +
 +
 
 +
Making the system boot the core + KDE in runlevel 4 is even simpeler as we don't even have to remove the symlinks:
 +
cd /
 +
rm -rfv etc/rc4.d/*
 +
cp -av etc/rc2.d/* etc/rc4.d/
 +
ln -sfv ../init.d/core etc/rc4.d/S99core
 +
 +
 
 +
Starting the launchmanager in runlevel 5:
 +
cd /
 +
rm -rfv etc/rc5.d/*
 +
cp -av etc/rc2.d/* etc/rc5.d/
 +
ln -sfv ../init.d/launch-manager etc/rc5.d/S99launch-manager
 +
rm -fv etc/rc5.d/S99kdm
 +
 +
 
 +
==== Additional upstart files to modify ====
 +
 
 +
You can edit these with your favorite text editor.  In this example they are changed to start in runlevels 3-5 as well. 
 +
 
 +
''/etc/event.d/media-center-startup''
 +
 +
start on runlevel 2
 +
start on runlevel 3
 +
start on runlevel 4
 +
start on runlevel 5
 +
 +
stop on shutdown
 +
 +
script
 +
/usr/pluto/bin/startup-script.sh
 +
end script
 +
 +
''/etc/event.d/pluto-dhcpd-plugin''
 +
 +
start on runlevel 2
 +
start on runlevel 3
 +
start on runlevel 4
 +
start on runlevel 5
 +
 +
stop on shutdown
 +
 +
script
 +
screen -d -m -S DhcpdPlugin /usr/pluto/bin/Dhcpd-Plugin.sh
 +
end script
 +
 +
On a sidenote: ''/etc/event.d/pluto-dhcpd-plugin'' seems redundant as it appears to be started from ''/usr/pluto/bin/startup-script.sh'' as well.
 +
 
 +
{{p}}
 +
 
 +
=== Changing the default runlevel ===
 +
 
 +
The default runlevel is 2, unless specified otherwise in the ''/etc/inittab'' file, here is an example on how to create such a file (it will override an existing one):
 +
cd /
 +
cat >etc/inittab <<"EOF"
 +
# WARNING: Do NOT set the default runlevel to 0 (shutdown) or 6 (reboot).
 +
id:2:initdefault: # KDE
 +
#id:3:initdefault: # Core
 +
#id:4:initdefault: # Core + KDE
 +
#id:5:initdefault: # Launch Manager
 +
EOF
 +
 +
 
 +
This specifies a default runlevel 2 (which it already was without this file anyway), you can use your favorite text editor to edit it to your desire when you permanently want to switch.
 +
 
 +
Note that the ''runlevel='' options specified in the grub menu override this, at least if you have updated the ''/etc/event.d/rc-default'' file as described below.
 +
 
 +
{{p}}
 +
 
 +
=== Selecting a runlevel at boot time ===
  
This is a comfortable way to experiment by simply selecting an alternative target from the boot-menu.  If it doesn't work out you can simply reboot.  These instructions are for a dedicated LMCE hybrid, and will probably need to be expanded for the other installation typesThis is still work in progress.
+
This is a convenient way to experiment by simply selecting an alternative target from the boot-menu.  If it doesn't work out you can always simply reboot to another runlevel.   
  
First we need to replace one of ''upstart'''s files, ''/etc/event.d/rc-default''.  (It's not a big addition, but I didn't like the way it was written originally, so I just rewrote most of it.)
+
First we need to replace one of ''upstart'''s files, ''/etc/event.d/rc-default''.  (It wasn't a big addition, but I didn't like the inconsistent way it was written originally, so I just cleaned it up a bit.)
  cat >/etc/event.d/rc-default <<"EOF"
+
cd /
 +
  cat >etc/event.d/rc-default <<"EOF"
 
  # rc - runlevel compatibility
 
  # rc - runlevel compatibility
 
  #
 
  #
Line 46: Line 344:
 
                 RL="S"
 
                 RL="S"
 
         else
 
         else
                 RL="$(sed -n -e "s/.*runlevel=\([1-5]\).*/\1/p" /proc/cmdline || true)"
+
                 RL="$(sed -n -e 's/.*runlevel=\([1-5]\).*/\1/p' /proc/cmdline || true)"
 
         fi
 
         fi
 
   
 
   
Line 65: Line 363:
 
   
 
   
  
Now edit ''/boot/grub/menu.lst'' with your favorite editor as follows:
+
Now edit ''/boot/grub/menu.lst'' with your favorite text editor as follows:
  
 
Comment out the "hiddenmenu" option as follows (this will show you the boot menu without having to press [Esc]):
 
Comment out the "hiddenmenu" option as follows (this will show you the boot menu without having to press [Esc]):
Line 73: Line 371:
  
 
Copy the first boot target once for every runlevel you want to be able to start, and modify it to look something like this:
 
Copy the first boot target once for every runlevel you want to be able to start, and modify it to look something like this:
  title          LMCE 1.1 (0704) LaunchManager
+
  title          '''LMCE 1.1 (0704) KDE'''
 
  root            (hd''#'',''#'')
 
  root            (hd''#'',''#'')
 
  kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'' ro quiet splash '''runlevel=2'''
 
  kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'' ro quiet splash '''runlevel=2'''
Line 80: Line 378:
 
  savedefault
 
  savedefault
 
   
 
   
  title          LMCE 1.1 (0704) KDE
+
  title          '''LMCE 1.1 (0704) Core'''
 
  root            (hd''#'',''#'')
 
  root            (hd''#'',''#'')
 
  kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'' ro quiet splash '''runlevel=3'''
 
  kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=''xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'' ro quiet splash '''runlevel=3'''
Line 94: Line 392:
 
  savedefault
 
  savedefault
  
You can also remove the ''quiet splash'' part if you want to see what is going on during boot.  Another section of interest could be:
+
The last one is the original boot target, this will start whatever runlevel is set as the default (or runlevel 2 if none specified in ''/etc/inittab'').  It shouldn't take to much imagination to add targets for runlevels 4 and 5 as well.
 +
 
 +
You can also remove the ''quiet splash'' part if you want to see what is going on during boot.   
 +
 
 +
Another section of interest could be:
 
  ## default num
 
  ## default num
 
  # Set the default entry to the entry number NUM. Numbering starts from 0, and
 
  # Set the default entry to the entry number NUM. Numbering starts from 0, and
Line 104: Line 406:
 
  # array will desync and will not let you boot your system.
 
  # array will desync and will not let you boot your system.
 
  default        0
 
  default        0
 
=== Setting up the runlevels ===
 
 
Traditionally the runlevels are controlled by manipulating a set of "symbolic links" in the ''/etc/rc'''X'''.d/'' directory, where '''X''' is the runlevel (0-6,S), however the Kubuntu system uses upstart, which has other ways as well.
 
 
When you are done with this you can switch to a different runlevel using one of the methods described elsewhere in this document, be sure to reboot in order to make a clean start.
 
 
==== Changing the symlinks ====
 
 
In order to make the dedicated LinuxMCE system boot KDE in runlevel 3, only a single symbolic link has to be changed
 
cd /etc/rc3.d/
 
ln -sf ../init.d/kdm.saved S99kdm
 
 
 
In order to boot a core only in runlevel 4, we first need to add a script to the ''/etc/init.d/'' directory:
 
cat >/etc/init.d/core <<"EOF"
 
#!/bin/sh
 
 
case "$1" in
 
        start)
 
                /usr/pluto/bin/StartCoreServices.sh
 
                ;;
 
        stop)
 
 
                /usr/pluto/bin/StopCoreServices.sh
 
                ;;
 
 
        *)
 
                echo "Usage: /etc/init.d/core {start|stop}"
 
                exit 1
 
                ;;
 
esac
 
 
exit 0
 
EOF
 
chmod +x /etc/init.d/core
 
 
 
Then remove the link to start kdm, and create a few other links (as found in ''/etc/rc2.d'')
 
cd /etc/rc4.d/
 
rm -fv S99kdm
 
ln -s ../init.d/powernowd.early S10powernowd.early
 
ln -s ../init.d/instcheck.sh S23instcheck.sh
 
ln -s ../init.d/LMCEUpdate S98LMCEUpdate
 
ln -s ../init.d/0start_avwizard S990start_avwizard
 
ln -s ../init.d/stop-readahead S99stop-readahead
 
ln -s ../init.d/core S99core
 
 
See also: "Additional upstart files to modify" below.  You could combine the previous examples to create a runlevel 5 which starts Core + KDE (just use ''rc'''5'''.d'' instead).  And then you can start the launch manager from the desktop icon if you're missing certain devices too.
 
 
Booting a diskless media director works.  After starting a desktop manually, the launch manager will start the remaining devices and can fire up the local media director.  Now we probably just need figure out how to start those devices from an extra script.
 
 
to be continued...
 
 
==== Additional upstart files to modify ====
 
 
''/etc/event.d/media-center-startup''
 
 
start on runlevel 2
 
start on runlevel 4
 
 
stop on shutdown
 
stop on runlevel 3
 
stop on runlevel 5
 
 
script
 
/usr/pluto/bin/startup-script.sh
 
end script
 
 
''/etc/event.d/pluto-dhcpd-plugin''
 
 
start on runlevel 2
 
start on runlevel 4
 
 
stop on shutdown
 
stop on runlevel 3
 
stop on runlevel 5
 
 
script
 
screen -d -m -S DhcpdPlugin /usr/pluto/bin/Dhcpd-Plugin.sh
 
end script
 
 
In this example they are changed to start in runlevel 4 as well.
 
 
=== Changing the default runlevel ===
 
 
The default runlevel is 2, unless specified otherwise in the ''/etc/inittab'' file, here is an example on how to create such a file (it will override an existing one):
 
cat >/etc/inittab << EOF
 
# WARNING: Do NOT set the default runlevel to 0 (shutdown) or 6 (reboot).
 
id:2:initdefault: # LinuxMCE
 
#id:3:initdefault: # KDE
 
#id:4:initdefault: # Core
 
#id:5:initdefault: # Core + KDE
 
EOF
 
This specifies a default runlevel 2 (which it already was without this file anyway), you can use your favorite text editor to edit it to your desire when you permanently want to switch.
 
  
 
== Personal experience ==
 
== Personal experience ==
  
 
Moved to the [[Talk:Clean_Core|discussion/talk]] page.
 
Moved to the [[Talk:Clean_Core|discussion/talk]] page.

Latest revision as of 22:16, 19 October 2012

Version Status Date Updated Updated By
710 Relevant to 0710 and older 2nd June 2011 purps
810 Not applicable 2nd June 2011 purps
1004 Not applicable 3rd August 2011 foxi352
1204 Unknown N/A N/A
1404 Unknown N/A N/A
Usage Information


Introduction

This page is meant to document the efforts to turn the LinuxMCE core into one or more Linux "services" that are automatically started at boot time. In other words, we would like to be able to boot a dedicated LinuxMCE Core server without using the Launch Manager application and without the dependency upon a graphic environment.

Please don't be shy to add any related knowledge you have.



Runlevels

Runlevels are an easy way to change how the system boots. LinuxMCE (as Kubuntu) uses runlevel 2 for normal operation (depending on the selections you made during installation) and runlevel 1 is used for rescue mode. The runlevels 0 and 6 are used for doing a shutdown or a reboot. Which leaves the runlevels 3, 4 and 5 free for us to experiment with.

As an example we will be setting up the following scheme in this document:

  1. rescue (as it is now)
  2. KDE (as installed by Kubuntu, like mainly-PC is now)
  3. Core (as in textmode, ideal for headless systems)
  4. Core + KDE (best of both worlds)
  5. LaunchManager (as the classic dedicated-MC setup is now)

Of course you can just adapt the example to your specific wishes or needs.



Starting another runlevel

If you just want to start a different runlevel once without modifying your startup scripts to much (as described below) You can simply boot into rescue mode by pressing [Esc] if grub tells you to and selecting it from the boot menu. That should boot up to a text console in single user mode, from there you can easily switch to another runlevel, for example runlevel 3:

telinit 3

The "runlevel" command will show you the previous and current runlevel.

Setting up the runlevels

Traditionally the runlevels are controlled by manipulating a set of "symbolic links" in the /etc/rcX.d/ directory, where X is the runlevel (0-6,S), however the Kubuntu system uses upstart, which has other ways as well.

When you are done with this you can switch to a different runlevel using one of the methods described elsewhere in this document, be sure to reboot in order to make a clean start.

Before starting it is always wise to make a backup of a few things, just in case:

tar cjvf ~/backup-`date '+%Y%m%d:%H%M'`.tar.bz2 /etc/init.d /etc/rc*.d



Preparation

Here are additional scripts needed to start the core devices, normally these task are performed by the launch-manager and it's startup script.

First a script for init (upstart):

cd /
cat >etc/init.d/core <<"EOF"
#!/bin/sh

case "$1" in
        start)
                /usr/pluto/bin/startCore.sh
                ;;
        stop)

                /usr/pluto/bin/StopCoreServices.sh
                ;;

        *)
                echo "Usage: /etc/init.d/core {start|stop}"
                exit 1
                ;;
esac

exit 0
EOF
chmod +x etc/init.d/core

Which calls a script that determines the device number of the core device:

cd /
cat >usr/pluto/bin/startCore.sh <<"EOF"
#!/bin/sh

BASEDIR=/usr/pluto/bin
DeviceTemplate_Core=7

source $BASEDIR/SQL_Ops.sh

$BASEDIR/Config_Device_Changes.sh
$BASEDIR/Start_OrbiterGen.sh
$BASEDIR/UpdateMediaDaemonControl.sh -enable

$BASEDIR/Start_DCERouter.sh

$BASEDIR/checkforRaids.sh
$BASEDIR/UpdateAvailableSerialPorts.sh

QUERY="SELECT PK_Device FROM Device JOIN DeviceTemplate ON Device.FK_DeviceTemplate=DeviceTemplate.PK_DeviceTemplate WHERE PK_DeviceTemplate=${DeviceTemplate_Core}"
RESULT=$(RunSQL "$QUERY")
for ROW in $RESULT
do
        CoreDev=$(Field 1 "$ROW")
        $BASEDIR/startDevices.sh $CoreDev
done

EOF
chmod +x usr/pluto/bin/startCore.sh

And in turn calls this script to start the whole device family, excluding any media directors:

cd /
cat >usr/pluto/bin/startDevices.sh <<"EOF"
#!/bin/sh


BASEDIR=/usr/pluto/bin
DeviceTemplate_MeDi=28

source $BASEDIR/SQL_Ops.sh


DEVICES=$*


for DEVICE in $DEVICES
do


       QUERY="SELECT ImplementsDCE, IsPlugin, Disabled, IF(PK_DeviceTemplate=${DeviceTemplate_MeDi},1,0) AS IsMeDi, REPLACE(DeviceTemplate.Description,' ','_') AS Description, CommandLine FROM Device JOIN DeviceTemplate ON Device.FK_DeviceTemplate=DeviceTemplate.PK_DeviceTemplate WHERE PK_Device=${DEVICE}"

       RESULT=$(RunSQL "$QUERY")
       for ROW in $RESULT
       do
               IMPLEMENTSDCE=$(Field 1 "$ROW")
               ISPLUGIN=$(Field 2 "$ROW")
               DISABLED=$(Field 3 "$ROW")
               ISMEDI=$(Field 4 "$ROW")
               DESCRIPTION=$(Field 5 "$ROW")
               COMMANDLINE=$(Field 6 "$ROW")

#              echo $RESULT, $IMPLEMENTSDCE $ISPLUGIN $DISABLED $ISMEDI $DESCRIPTION \"$COMMANDLINE\"

               if [ "$ISMEDI" != "0" ]
               then
                       echo "Device $DEVICE ($DESCRIPTION) is a Media Director"
                       continue
               fi



               if [ "$IMPLEMENTSDCE" = "0" ]
               then

                       echo "Device $DEVICE ($DESCRIPTION) no DCE, going after the children"

                       CHILDREN=""
                       QUERY="SELECT PK_Device FROM Device WHERE FK_Device_ControlledVia=$DEVICE"
                       RESULT=$(RunSQL "$QUERY")
                       for ROW in $RESULT
                       do
                               CHILD=$(Field 1 "$ROW")
                               CHILDREN="$CHILDREN $CHILD"
                       done


                       if [ "$CHILDREN" ]
                       then

                               $0 $CHILDREN
                       else
                               echo "Device $DEVICE ($DESCRIPTION) has no children"
                       fi

               else

                       if [ "$ISPLUGIN" = "0" ]
                       then

                               if [ "$DISABLED" = "0" ]
                               then

                                       if [ -z "$COMMANDLINE" ]
                                       then
                                               COMMANDLINE=$DESCRIPTION
                                       fi

                                       if [ -x "$BASEDIR/${COMMANDLINE}" ]
                                       then

#                                               echo "/usr/bin/screen -d -m -S ${DESCRIPTION}-${DEVICE} $BASEDIR/Spawn_Device.sh ${DEVICE} ${DCERouter} ${COMMANDLINE}"
                                               /usr/bin/screen -d -m -S ${DESCRIPTION}-${DEVICE} $BASEDIR/Spawn_Device.sh ${DEVICE} ${DCERouter} ${COMMANDLINE}
                                               echo "Device $DEVICE ($DESCRIPTION) started  ${COMMANDLINE}"

                                       else
                                               echo "Device $DEVICE ($DESCRIPTION) unable to start ${COMMANDLINE}"
                                       fi



                               else
                                       echo "Device $DEVICE ($DESCRIPTION) is disabled"
                               fi


                       else
                               echo "Device $DEVICE ($DESCRIPTION) is a plugin"
                       fi

               fi

       done

done

EOF
chmod +x usr/pluto/bin/startDevices.sh



On a Mainly Personal Computer installation

If you chose this install type you will need to create an extra startup script. In other words: when your core machine starts up with a KDE desktop. Here is a slightly simplified version of the one included in the "Dedicated Media Center" install type:

cd /
cat >etc/init.d/launch-manager <<"EOF"
#!/bin/sh
if [ "$1" == "start" ]
then
        /usr/pluto/bin/Startup_Core-Hybrid.sh
fi

EOF
chmod +x etc/init.d/launch-manager



On a Dedicated Media Center installation

To make things a little bit easier for ourselves we're going to move a few things around a bit, this should give us about the same starting point as when using a "Mainly Personal Computer" setup. Do this if your core machine normally starts up with the launch manager:

cd /
mv -iv etc/init.d/kdm etc/init.d/launch-manager
cp -iv etc/init.d/kdm.saved etc/init.d/kdm



Changing the symlinks

In order to make the system boot only the core in runlevel 3, simply copy the symlinks of runlevel 2 and replace the ones that start KDE and the AVWizard with a symlink to the init script we created earlier during the preparations:

cd /
rm -rfv etc/rc3.d/*
cp -av etc/rc2.d/* etc/rc3.d/
ln -sfv ../init.d/core etc/rc3.d/S99core
rm -fv etc/rc3.d/S99kdm etc/rc3.d/S990start_avwizard

Making the system boot the core + KDE in runlevel 4 is even simpeler as we don't even have to remove the symlinks:

cd /
rm -rfv etc/rc4.d/*
cp -av etc/rc2.d/* etc/rc4.d/
ln -sfv ../init.d/core etc/rc4.d/S99core

Starting the launchmanager in runlevel 5:

cd /
rm -rfv etc/rc5.d/*
cp -av etc/rc2.d/* etc/rc5.d/
ln -sfv ../init.d/launch-manager etc/rc5.d/S99launch-manager
rm -fv etc/rc5.d/S99kdm

Additional upstart files to modify

You can edit these with your favorite text editor. In this example they are changed to start in runlevels 3-5 as well.

/etc/event.d/media-center-startup

start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5

stop on shutdown

script
/usr/pluto/bin/startup-script.sh
end script

/etc/event.d/pluto-dhcpd-plugin

start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5

stop on shutdown

script
screen -d -m -S DhcpdPlugin /usr/pluto/bin/Dhcpd-Plugin.sh
end script

On a sidenote: /etc/event.d/pluto-dhcpd-plugin seems redundant as it appears to be started from /usr/pluto/bin/startup-script.sh as well.



Changing the default runlevel

The default runlevel is 2, unless specified otherwise in the /etc/inittab file, here is an example on how to create such a file (it will override an existing one):

cd /
cat >etc/inittab <<"EOF"
# WARNING: Do NOT set the default runlevel to 0 (shutdown) or 6 (reboot).
id:2:initdefault: # KDE
#id:3:initdefault: # Core
#id:4:initdefault: # Core + KDE
#id:5:initdefault: # Launch Manager
EOF

This specifies a default runlevel 2 (which it already was without this file anyway), you can use your favorite text editor to edit it to your desire when you permanently want to switch.

Note that the runlevel= options specified in the grub menu override this, at least if you have updated the /etc/event.d/rc-default file as described below.



Selecting a runlevel at boot time

This is a convenient way to experiment by simply selecting an alternative target from the boot-menu. If it doesn't work out you can always simply reboot to another runlevel.

First we need to replace one of upstart's files, /etc/event.d/rc-default. (It wasn't a big addition, but I didn't like the inconsistent way it was written originally, so I just cleaned it up a bit.)

cd /
cat >etc/event.d/rc-default <<"EOF"
# rc - runlevel compatibility
#
# This task guesses what the "default runlevel" should be and starts the
# appropriate script.

start on stopped rcS

script
        runlevel --reboot || true


        if grep -q -w -- "-s\|single\|S" /proc/cmdline
        then
                RL="S"
        else
                RL="$(sed -n -e 's/.*runlevel=\([1-5]\).*/\1/p' /proc/cmdline || true)"
        fi

        if [ -z "$RL" -a -r /etc/inittab ]
        then
                RL="$(sed -n -e 's/^id:\([1-5]\):initdefault:.*/\1/p' /etc/inittab || true)"
        fi

        if [ -z "$RL" ]
        then
                RL="2"
        fi

        telinit $RL

end script
EOF

Now edit /boot/grub/menu.lst with your favorite text editor as follows:

Comment out the "hiddenmenu" option as follows (this will show you the boot menu without having to press [Esc]):

## hiddenmenu   
# Hides the menu by default (press ESC to see the menu)
#hiddenmenu

Copy the first boot target once for every runlevel you want to be able to start, and modify it to look something like this:

title           LMCE 1.1 (0704) KDE
root            (hd#,#)
kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ro quiet splash runlevel=2
initrd          /boot/initrd.img-2.6.20-15-generic
quiet
savedefault

title           LMCE 1.1 (0704) Core
root            (hd#,#)
kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ro quiet splash runlevel=3
initrd          /boot/initrd.img-2.6.20-15-generic
quiet
savedefault

title           Ubuntu, kernel 2.6.20-15-generic
root            (hd#,#)
kernel          /boot/vmlinuz-2.6.20-15-generic root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ro quiet splash 
initrd          /boot/initrd.img-2.6.20-15-generic
quiet
savedefault

The last one is the original boot target, this will start whatever runlevel is set as the default (or runlevel 2 if none specified in /etc/inittab). It shouldn't take to much imagination to add targets for runlevels 4 and 5 as well.

You can also remove the quiet splash part if you want to see what is going on during boot.

Another section of interest could be:

## default num
# Set the default entry to the entry number NUM. Numbering starts from 0, and
# the entry number 0 is the default if the command is not used.
#
# You can specify 'saved' instead of a number. In this case, the default entry
# is the entry saved with the command 'savedefault'.
# WARNING: If you are using dmraid do not change this entry to 'saved' or your
# array will desync and will not let you boot your system.
default         0

Personal experience

Moved to the discussion/talk page.