La traduction en Français est en cours, si vous voulez aider, vous pouvez nous contacter.


This is the FSLUG Articles Page.



Table of content



Sort IP Addresses with GNU sort

Hardware profiles in Linux - Dual display and Laptop profiles.


Sort IP Addresses with GNU sort

This is a really handy command line script to sort IP Addresses

sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

Hardware profiles in Linux - Dual display and Laptop profiles.

Source

People who use laptops often need different configurations. They may want the option of removing their external display, docking station, or other peripheral.

In some cases, like with network cards, it's quite easy and normally the user doesn't need to modify anything. In some cases the user needs to execute some commands to modify the default behavior. Normally when you need to re-execute the same command twice it's time to write a script and put it in the right place to be executed in the right moment.

In this article I'll explain how to take advantage of the run level 4 (normally not used) to use two different hardware profiles.

In my case I'm using 2 displays in the office and only the internal one in the lab, at home, outdoor and so on.
This it means I need an /etc/X11/xorg.conf when the external display is connected and an another one in all the other situations.

As I have an ATI FireGL 5200, the xorg.conf file differ only for two lines:

diff /etc/X11/xorg.conf-DUAL /etc/X11/xorg.conf-SINGLE
116d115
<>

For other cards you can have a different setup.

But let's start from the ground up.

You should start to configure your X server for a single display, when you are done and satisfied with your setup, make a copy of your /etx/X11/xorg.conf file, as root, let's say to /etc/X11/xorg.conf-SINGLE. Now do the same for a dual display configuration and save your new brand /etc/X11/xorg.conf to /etc/X11/xorg.conf-DUAL.

Now that you have 2 different Xorg setup you can go on with the run levels configuration.

Just to remind you a bit of theory, in the /etc/inittab file you have the definition and configurations of the runlevels:

# The default runlevel is defined here
id:5:initdefault:
# /etc/init.d/rc takes care of runlevel handling
#
# runlevel 0 is System halt (Do not use this for initdefault!)
# runlevel 1 is Single user mode
# runlevel 2 is Local multiuser without remote network (e.g. NFS)
# runlevel 3 is Full multiuser with network
# runlevel 4 is Full multiuser with network and xdm in Single Display
# runlevel 5 is Full multiuser with network and xdm
# runlevel 6 is System reboot (Do not use this for initdefault!)
#
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6


What you see is my setup, normally the runlevel 4 is not used.

from the man 5 inittab
# Runlevel 0 is System halt (Never use this as initdefault!)
# Runlevel 1 is Single user mode
# Runlevel 2 is Local multiuser without remote network (e.g. NFS)
# Runlevel 3 is Full multiuser with network
# Runlevel 4 is Not used
# Runlevel 5 is Full multiuser with network and xdm
# Runlevel 6 is System reboot (Never use this as initdefault!)


And the l4:4:wait:/etc/init.d/rc 4 line is commented.

If you have OpenSuse proceed as follow.
  1. Login as root and edit the /etc/inittab file. Uncomment the l4:4:wait:/etc/init.d/rc 4 line as I did in my setup. Modify also the comment from "Not used" to what ever you like.
  2. execute yast2 runlevel and switch to "expert mode". You will have now the runlevel 4.
  3. Activate all the scripts you have on the runlevel 4 as are activated in the runlevel 5.
  4. Save and exit.
Now you have two runlevels with the same setup. They do exactly the same. Ah, obvoiusly you can adapt this for the runlevel 2 if you have other needs then a dual display profile. And if you don't have OpenSuse you can do the same with the command line, copying the scripts from /etc/rc5.d/ to /etc/rc4.d/. Take a look on the init, inittab, runlevels and insserv man pages of your distro.

Let's continue with our dual display profile.

The idea is to copy /etc/X11/xorg.conf-SINGLE to /etc/X11/xorg.conf when starting to the runlevel 4 and to copy back from /etc/X11/xorg.conf-DUAL to /etc/X11/xorg.conf when shutting down.

To achieve this I wrote a script, named display_mode that I placed under the /usr/sbin/ directory.

The main logic of the display_mode script is:

function single
{

[ -e /etc/X11/xorg.conf ] && cp -f /etc/X11/xorg.conf /etc/X11/xorg.conf-$DATE
[ -e /etc/X11/xorg.conf-SINGLE ] && cp -f /etc/X11/xorg.conf-SINGLE /etc/X11/xorg.conf
[ -e /etc/ati/amdpcsdb ] && rm -f /etc/ati/amdpcsdb
[ -e /usr/sbin/setSingleDesktop.sh ] && /usr/sbin/setSingleDesktop.sh

}

function dual
{

[ -h /etc/X11/xorg.conf ] && cp -f /etc/X11/xorg.conf /etc/X11/xorg.conf-$DATE
[ -e /etc/X11/xorg.conf-DUAL ] && cp -f /etc/X11/xorg.conf-DUAL /etc/X11/xorg.conf
[ -e /etc/ati/amdpcsdb ] && rm -f /etc/ati/amdpcsdb
[ -e /usr/sbin/setDualDesktop.sh ] && /usr/sbin/setDualDesktop.sh

}

while getopts ":hsd" opt; do
case $opt in
s ) echo "Set the X server config to Single display"
single
;;
d ) echo "Set the X server config to Dual display"
dual
;;
h ) helptext
graceful_exit ;;
* ) usage
clean_up
exit 1
esac
done
Of course, it can be improved, but it works... So when you call this script with display_mode -s , it will copy /etc/X11/xorg.conf-SINGLE to /etc/X11/xorg.conf , then it will execute another script needed by the ATI driver, but it's out of our scope, ask to them why it's so complicated!!!

display_mode -d will copy back /etc/X11/xorg.conf-DUAL to /etc/X11/xorg.conf

This is an handy script you can use at any time you needs, but our goal is to place it in the /etc/rc4.d/ directory

I used /etc/init.d/skeleton script to create a /etc/init.d/display_mode to call /usr/sbin/display_mode with the s option for the start case and with the d option for stop case.

case "$1" in
start)
echo -n "Starting display_mode "
## Start daemon with startproc(8). If this fails
## the return value is set appropriately by startproc.

$dispay_mode_bin -s

# Remember status and be verbose
rc_status -v
;;
stop)
echo -n "Shutting down display_mode "
## Stop daemon with killproc(8) and if this fails
## killproc sets the return value according to LSB.

$dispay_mode_bin -d

# Remember status and be verbose
rc_status -v
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
It's important to execute this script before the XDM start.
ls -la /etc/init.d/rc[45].d/S[0-9]*xdm*
lrwxrwxrwx 1 root root 11 Nov 6 15:57 /etc/init.d/rc4.d/S12earlyxdm -> ../earlyxdm
lrwxrwxrwx 1 root root 6 Nov 6 15:57 /etc/init.d/rc4.d/S12xdm -> ../xdm
lrwxrwxrwx 1 root root 11 Nov 10 09:10 /etc/init.d/rc5.d/S04earlyxdm -> ../earlyxdm
lrwxrwxrwx 1 root root 6 Nov 10 09:10 /etc/init.d/rc5.d/S11xdm -> ../xdm
So I created the links to the script /etc/init.d/display_mode to be executed before the xdm ones.
ls -la rc4.d/*display_mode
lrwxrwxrwx 1 root root 15 Nov 11 10:09 rc4.d/K15display_mode -> ../display_mode
lrwxrwxrwx 1 root root 15 Nov 11 10:10 rc4.d/S07display_mode -> ../display_mode
Start again yast2 runlevel and be sure the display_mode script will be executed just during the runlevel 4, apply your modification and exit.

Now you have two options to switch to runlevel 4.

  1. During the boot time you pass manually 4 to the kernel options.
  2. You create a second grub/lilo menu, a copy of your default one passing the option 4 to it.
Just as an example this is my grub menu file:

title openSUSE 11.0 - 2.6.25.18-0.2 (default)
root (hd0,0)
kernel /boot/vmlinuz-2.6.25.18-0.2-default root=/dev/disk/by-id/scsi-SATA_ST910021AS_3MH0KNGW-part1 resume=/dev/sda2 splash=silent showopts vga=0x317
initrd /boot/initrd-2.6.25.18-0.2-default

title openSUSE 11.0 - SINGLE Display
root (hd0,0)
kernel /boot/vmlinuz-2.6.25.18-0.2-default root=/dev/disk/by-id/scsi-SATA_ST910021AS_3MH0KNGW-part1 resume=/dev/sda2 splash=silent showopts 4 vga=0x317
initrd /boot/initrd-2.6.25.18-0.2-default