# Updated - Automatic system configuration based on profiles

## optilude

UPDATE 2003-12-22: hprofile version 2.0 beta 1 has just been released. The most important changes are that you can now have multiple types of profiles (e.g. a boot profile like the one below, a net profile to connect to one of multiple networks, and a power profile for power management), and you can run arbitrary scripts when profiles are started or stopped. The new version is at http://hprofile.sourceforge.net and comes with lots of examples and gentoo init scripts.

This is the original posting: This is now somewhat obsolete, but I'm going to leave it here for people who may be using the old versions. New installs should follow the (much better) documentation on http://hprofile.sourceforge.net.

mod edit: Fixed typo (sourceforget) in url.

amne

Hello,

Note: Newer versions of the Gentoo RC system uses the notion of critical boot services that are always started first in the boot runlevel. The hprofile service *must* start before the modules service, because in most cases, you will want to switch around your kernel-specific file in /etc/modules.autoload.d to selectively load modules depending on your hardware setup. Therefore, hprofile must be included in the list of critical services.

From /sbin/rc, the default critical services are "checkroot hostname modules checkfs localmount"; To override these defaults, you have to create a file called .critical in /etc/runlevels/boot. On my machine this looks like:

/etc/runlevels/boot/.critical

```

checkroot hostname hprofile modules checkfs localmount

```

Now hprofile will be started before modules as expected, and all should work well.

Basically, I've written a system called hprofile, available from http://go.to/optilude (click the Software rectangle and then download it from the next page; sorry, direct URLs won't work reliably...).  I use it because I sometimes use VMware for Windows to boot my gentoo installation, and when I do this, I want things like /etc/X11/XF86Config, /etc/modules.d/alsa, and /etc/modules.autoload.d/kernel-2.6 to be different. I also want it to boot into a different runlevel, so that it doesn't start services that only make sense when I'm booting normally. hprofile lets me switch arbitrary files and runlevels based on arbitrary criteria - it would work just as well, say, for a laptop that may be in a docking station, or simply if you want to have a "light" boot option for quick boots, and is a lot more powerful and flexible than VMware's RedHat oriented "dualconf" script.

hprofile consists of three scripts: hpdet, to determine which profile to use, hprunlevel to find out which runlevel a profile corresponds to, and hprofile, which will swap arbitrary files anywhere in the file system based on the profile.

hpdet determines the current profile. It does so in one of three ways: The preferred method is by running /etc/hprofile/ptest, which should be a script you write that echoes the name of the profile. I have this test VMWare's "checkvm" program, and echo "dualboot" or "vmware" depending. Another technique would be to grep dmesg and look for signs of hardware that's only present in one profile. To override this, you can specify the profile on the kernel command line (lilo append line, or press "e" in grub), and give the option "hprofile=<profile>" to the kernel. As a fallback, you can specify a default runlevel in /etc/hprofile/default. 

hprunlevel is used to select the current runlevel. It does this through a very simple lookup table in /etc/hprofile/runlevels, mapping profile names to runlevel numbers. This can then be used an argument to "init". 

The way I choose runlevels in gentoo is as follows: In /etc/inittab, I have two special runlevels, with corresponding directories in /etc/runlevels: dualboot (runlevel 4) and vmware (runlevel 5). These contain the services I want to run in these hardware configurations. The default runlevel (3) contains only a dummy script that warns something went wrong in profile selection (I've yet to see that message, though). 

I then have following script called hprunlevel, which is placed in the "boot" runlevel, which will switch to the correct runlevel after all the bootup options have been set:

/etc/init.d/hprunlevel:

```

#!/sbin/runscript

# Copyright 1999-2002 Gentoo Technologies, Inc.

# Distributed under the terms of the GNU General Public License, v2 or later

depend() {

   after *

}

start() {

   # Change runlevel if necessary

   hp=`/usr/local/sbin/hpdet`

   lvl=`/usr/local/sbin/hprunlevel ${hp}`

   if [ lvl != "" ]

   then

     ebegin "Setting runlevel for profile ${hp}"

     /sbin/init ${lvl}

     eend $?

   fi

}

```

This will basically "shut down" the "boot" runlevel (which does nothing), and start the correct runlevel (4 or 5 in my case).

The dummy script in the "default" runlevel is hprunlevel-warning:

/etc/init.d/hprunlevel-warning:

```

#!/sbin/runscript

# Copyright 1999-2002 Gentoo Technologies, Inc.

# Distributed under the terms of the GNU General Public License, v2 or later

depend() {

   before *

}

start() {

   hp=`/usr/local/sbin/hpdet`

   lvl=`/usr/local/sbin/hprunlevel ${hp}`

   ewarn "Warning: The current profile is ${hp}, which implies runlevel ${lvl}"

}

```

Finally, hprofile does the file switching. It's run from the following script (/etc/init.d/hprofile), from the "boot" runlevel - it's important that this runs as early as possible, so that the config files it swaps will be in the right place at the right time:

/etc/init.d/hprofile:

```

#!/sbin/runscript

# Copyright 1999-2002 Gentoo Technologies, Inc.

# Distributed under the terms of the GNU General Public License, v2 or later

depend() {

        before modules

   need localmount

}

start() {

   hp=`/usr/local/sbin/hpdet`

   ebegin "Selecting hardware profile ${hp}"

   /usr/local/sbin/hprofile ${hp}

   eend $?

}

# hprofile uses ln -s for files, so we don't need to save...

#stop() {

#   hp=`/usr/local/sbin/hpdet`

#   ebegin "Saving modifications to hardware profile ${hp}"

#   /usr/local/sbin/hprofile ${hp} save

#   eend $?

#}

```

The hprofile script is of course what does the real work. The README file from the tarball is required (and easy) reading, but briefly, you can mirror any portion of your filesystem in /etc/hprofile/files and create alternates for different profiles. For example, if you have two profiles, "vmware" and "dualboot" and want different /etc/modules.autoload's for each, put the two alternatives in /etc/hprofile/files/etc/modules.autoload.vmware and /etc/hprofile/files/modules.autoload.dualboot. When hprofile is run, it will replace /etc/modules.autoload with a symlink to the correct file, doing a backup of the original if necessary. Individual users can do the same to files in the home directory by mirroring any portion of it in ~/.hprofile/files. 

As I mentioned, I've used this happily and extensively over the last couple of months for booting within and without VMWare with no problems at all. It really is quite powerful - for example, I have a different ~/.gnome2/gdm files - one to default to KDE for when I dualboot, the other defaulting to my (light) gnome setup when I'm in vmware (loading a full KDE 3.1 with lots of bells and whistles within VMware takes slightly longer than an eternity).

Hope this helps! If you have any questions or difficulties, don't hesitate to email me at optilude@gmx.net.

I'd be very happy if you could either post or email me your experiences if you do try this!

Best wishes,

Martin

----------

## pjp

I've moved the older version to duplicates.  If there are future versions, please update this thread rather than creating another one.  Thanks.

----------

## optilude

Yeah, thanks... Reason I created a new one was that the old one was in the Laptops forum, and I thought it fitted better in tips & tricks (esp as it may benefit people without a laptop).

----------

## Carlo

Cool - this profile processing should be a part of gentoo's init-system. Installing/repairing (from a gentoo boot cd) could be just different run-levels inluding (semi-)automatic hardware detection, for example. It would be a transparent way to implement such tasks.

Carlo

----------

## optilude

Carlo - have you tried hprofile? I'd be very interested in hearing of your experiences...

 *Carlo wrote:*   

> this profile processing should be a part of gentoo's init-system. Installing/repairing (from a gentoo boot cd) could be just different run-levels inluding (semi-)automatic hardware detection, for example. It would be a transparent way to implement such tasks.
> 
> 

 

Hmmm.... interesting - can you elaborate what you mean? If anyone is interested in integrating hprofile closer with the gentoo init system, I'd lend my full support. 

Best wishes,

Martin

----------

## fruers

I have three different configurations that i use for my laptop, docked at work, docked at home and normal laptop. For the past 6 months or so i've been updating the X config and network settings everytime i started the computer, always meant to get around to writing some scripts to do i automatically but was always too lazy (funny eh, i would've spent a tenth of the time if i had've written the script in the first place). Anyway, i used these scripts and some simple grepping to detect where i am and it all works great, got it all working in about 10 minutes. Thanks a lot, i highly recommend these scripts for anyone who works in multiple environments!

----------

## optilude

*blush*  :Smile: 

Hehe... I'm really glad to hear of someone else using them! They've served me well with dualboot vs. vmware, but I've never had a need for other configs, so I'm glad to hear they work as expected. Any comments or suggestions, please email me at optilude@gmx.net.  :Smile: 

/Martin

----------

## chimy

I'm really trying to get to this. but I guess this is just too much for me  :Smile:  since I only want to select diffrent boot levels it would be just somehow too much.

----------

