hprofile - dynamic profiles for linux: hardware configurations; network connections; power management; usage patterns...
SourceForge.net Logo





Frequently Asked Questions

Below, I've tried to answer some of the questions I anticipate you may have about hprofile. The best way to learn about it, of course, is to install it and have a play with it, but hopefully this page will help you decide whether hprofile is right for you.

I'm too lazy to read all this - give me the rough guide

The following steps describe how to set up a 'boot' profile. Other types of profiles are made in the same way, but note that the 'boot' profile is the only one that can be associated with runlevels. The example profiles that come with hprofile should get you started.

Install hprofile, and look in /etc/hprofiles/profiles/boot. Edit the file profiles and add the names of the different boot profiles, one per line. Do not include anything else. An example would be:
Then edit the file default so that it contains exactly one of the lines of the profiles file - this denotes the default, or "fall-back" profile.
Edit the script ptest so that it outputs the currently valid boot profile (if you skip this step, hprofile will not be able to automatically determine the profile, but you can still select them manually). An example could be:
if dmesg | grep "abc" >/dev/null ; then
echo "docked"
echo "portable"
Replace "abc" with some string describing hardware that is only present in the output of dmesg when the machine is in a docking station. Other tips for writing ptest scripts can be found below.
Say you need different versions of the file /etc/some-config-file. Put the two different versions in:
Note how the suffix describes the profile, and the position of the file relative to /etc/hprofile/profiles/boot/files mirrors its intended position relative to the root of the filesystem.
If you want some script to be run when the "docked" profile is started (i.e. right after the configuration files belonging to it are put in place), or stopped (i.e. right before it is replaced with a different profile, or if you run "hprofile -s boot"), put these in
and make sure they are executable.
Finally, to apply the "docked" profile, run:
# hprofile boot.docked
as root. To apply the current profile as determined by the ptest script you wrote, run:
# hprofile boot
To stop the current profile (hence ensuring it's no longer registered as being "active" and that its "stop" scripts, if any, are run), run
# hprofile -s boot
You probably want to run the last two of these as early in the boot process as possible (i.e. right after local filesystems have been mounted) and as late in the shutdown process as possible. Examples for Gentoo Linux comes with the tarball, but setups for other systems may differ. You may also want to look at the hprunlevel script, installed with hprofile, which will let you define different runlevels for the different boot profiles. Again, examples for Gentoo Linux comes with the tarball, so use these as a starting point.
So, what exactly do you mean by profiles?

Let's say you want to use your system in two different ways. Most of the time, you want it to run with all the bells and whistles, eye candy and services you can find, but occasionally, you just want it to boot as quickly as possible and drop you right into a minimalist desktop, so you can type and email and get on with life. Those are two profiles. You may for instance use LILO or GRUB to load Linux with a kernel option like "profile=full" or "profile=minimal" depending on which option you choose on the startup menu. You can then use hprofile to decide which runlevel your computer will boot into, based on the profile, and you can have alternate versions of any configuration file to suit your preferences.

You could also have different physical hardware to react to, for example if you have a laptop that may or may not be in a docking station. As long as it's possible to determine which profile the system is currently in from a script, hprofile can select the correct profile automatically.

Profiles don't have to be set at boot time, either. For example, you may want to define different power profiles, like "AC", "Battery" and "HighPerformance". You could start and stop power hungry services depending on your power profile, and execute commands to scale down the speed of your processor, adjust screen brightness and instigate other power-saving measures, as necessary. hprofile makes it very easy to execute scripts when a profile is "started" or "stopped".

Another type of profile may be a network profile. Say you connect to different networks at home and at work, or that you regularly use different wireless hotspots. You may have different network profiles called "home", "work", and "cafe". When you apply a profile, it will switch your /etc/hosts file to suit the network you're connected to, bring network interfaces up and down, set WEP encryption keys etc.

Each different type of profile has a name; the examples above could correlate to the the profiles "boot", "power" and "net". To select the "net" profile called "home", you would execute:

# hprofile net.home

Sounds complicated; how do I set it up?

Actually, it's pretty easy. Each profile type has its own directory in /etc/hprofile/profiles. This directory contains some configuration files (most notably the text file profiles which defines the names of the different profiles, such as "home", "work" and "cafe" from the "net" profile example above, and a script called ptest, which you can use to automatically determine the current profile, for example by examining the state of your hardware, or choices made by the user), and two directories, files and scripts.

The files directory works like a mirror of the file system, and contains alternate versions of files, named with suffixes of ".". For example, say you want different versions of your /etc/hosts file for the three "net" profiles, "home", "work" and "cafe". The three versions of the files will all lie in the directory /etc/hprofile/profiles/net/files/etc/, and will be called hosts.home, hosts.work and hosts.cafe respectively. Note how the different versions of the files are distinguished by their suffix, and how their position relative to the files directory mirrors their position relative to the root of the file system. If you then run

# hprofile net.home
a symbolic link will be created from /etc/hosts to /etc/hprofile/profiles/net/files/etc/hosts.home.

That's it! You can switch any configuration file in this way. It's also possible for users to do the same with files in their home directory. In the above example, they could create a directory ~/.hprofile/profiles/net/files/.signature.home, and .signature.work to have different ~/.signature files depending on whether the "home" or "work" network profiles are active.

It's just as easy to execute scripts when profiles are "started" or "stopped" (a profile is "stopped" if another profile of the same type is applied, or if you manually stop it by running hprofile with the -s option). For example, if you want to bring a net interface up for the "work" profile (and bring it down when you stop it), you can put the commands to do so in two scripts work.start and work.stop in the directory /etc/hprofile/profiles/net/scripts. These will be automatically called when the "work" profile is started or stopped (if they exist and are executable). Users can do the same in ~/.hprofile/profiles/net/scripts. For security reasons, user scripts are run as the user whose home directory they are in (note that this requires sudo to be installed).

What do I need to use it?

To use hprofile, all you really need is the Bash shell and the other standard GNU tools found on any GNU/Linux system. You'll also sudo if you want users to be able to execute scripts when profiles are started or stopped.

Help! I accidentally overwrote a file with one from a profile!

Before hprofile overwrites a file with a symlink to a profile-specific file, it will check if the file belongs to any profiles. If it does not, it will be backed up to the profile directory. For example, if you have a "net" profile that defines alternate /etc/hosts, but the current /etc/hosts file is different from all the /etc/hosts alternates found in /etc/hprofile/profiles/net/etc/hosts.*, hprofile will back up the original /etc/hosts to /etc/hosts/hprofile/profiles/net/hosts.bak.

How do I manage different hardware configurations?

Have a look at the example "boot" profile which comes in the hprofile tarball. You need to switch the files that load your hardware modules, and configuration files for software that interacts with your hardware such as XFree86, your network setup, etc.

You also need to ensure the profile is selected at boot. To automatically determine the profile, you should write a ptest script which examines the state of your hardware (say, by looking at the files in /proc or the output of dmesg) to determine which profile is appropriate. Then you should write boot- (also called rc- or init-) scripts to select the profile at startup. It's probably best to have these run as early as possible in the boot process (right after file systems have been mounted), to ensure subsequent services are started in the context of the correct profile. You may also want to switch runlevels based on the profile. The hprunlevel script can help you with this (see the README and the output of "hprunlevel -h" for more help.

Exactly which files should be switched, and how to select the profile at boot time will depend on your distribution and how it's set up. I use Gentoo Linux, so I can't give details of other distributions, but example boot profiles and Gentoo init scripts can be found in the tarball. If you use a different system, I would be grateful if you would email it to me, so I can include it in the tarball in the future.

How do I use hprofile to save battery life?

Example power profiles are also included with the distribution. These are quite simple - they do not switch any configuration files around, and rely solely on profile start scripts to change the CPU speed (using SpeedFreq) and LCD brightness (using ASUS ACPI extensions). You may want to investigate the xgamma program if you need a different way to change LCD brightness. There are several other ways to change CPU speed (have a look in the kernel configuration), and you may want to be more clever by stopping power hungry services, change the power usage of other components (such as a wireless card), etc.

If you're using ACPI, you can have power profiles changed automagically upon AC adapter and battery events. A simple example setup is included in the tarball, as is a startup script which will select the appropriate profile when the computer is booted.

How do I use hprofile to switch between different networks?

Example network profiles are also included in the tarball. These use the Gentoo Linux network scripts, /etc/init.d/net.eth0 and net.eth1, to bring network interfaces up and down, and selectively load the module with alias eth1 (set this in /etc/modules.conf, or preferably via a file in /etc/modules.d/ if your distribution supports it); I do this, because that module is responsible for my wireless card, and I don't want the wireless radio to be on when I'm not using it). There are many files that may be useful to switch based on network profile, but the only one I need to change is /etc/hosts.

It's a bit hard to determine which network profile the system is currently in unless you want to spend a lot of time probing networks; if you always use DHCP, you could look at which subnet you're on. The approach taken in the example is to always use the previous profile. When the network profile is stopped (from shutdown script when the machine is stopped), the "current" profile is stopped, and recorded as the "previous" profile. This means that if we want to use this profile again the next time the machine is started, we need to find the previous profile. This can be obtained by running

# hprofile -p net
which will echo the name of the previous profile.

What else could I use it for?

Any part of your system which you could define as being in one of several states is subject to be controlled via hprofile profiles. Most often, profiles will be controlled by some external condition (such as the attached hardware, the network you connect to, or the state of your battery), but you can be more creative by thinking about usage profiles. The initial example above is an example of this - are you starting the system in "full" or "minimalist" mode? Perhaps you want to define a "gaming" profile to configure your system for optimal gaming performance, or different profiles for different users of the system? If you've done something clever with hprofile you'd like to share, I'd be happy to include it as an example in the tarball and/or mention it here; just email me!

Tips & Tricks

Here are a few tips on what you can do with hprofile that are perhaps not obvious. Once again, additional tips are much appreciated via email.

When you're writing ptest scripts to automatically determine profiles, you can examine /proc/cmdline. This is the "kernel command line"; called the "append line" if you use LILO as your boot loader. Have a look at the ptest script in the example boot profile. Another file to examine to determine hardware configurations is /proc/pci, or the output of the dmesg command.
As mentioned in the network profile example above, hprofile keeps track of the "current" and "previous" profiles. Note that if you apply, say, the "work" profile (from the network example) once, and then apply the "home" profile twice, the "work" profile is still recorded as the "previous" profile (and "home" is the "current"). If you then run
# hprofile -s net
to stop the current (i.e. the "home") network profile, the "previous" profile becomes "home" and the "current" profile is unset.
You can revert to the previous profile by running
# hprofile -r <type>
This makes the "current" profile the "previous", and vice-versa. If you run the command again, you're back where you were. One use for this could be if you have an ACPI event for when the lid of your laptop is closed. This could start a "lid-closed" power profile to reduce power usage to the absolute minimum (or possibly put the computer in a sleep state). When the lid is opened again, a corresponding event could revert the system to the previous power profile, whatever it was.
The select-profile script, found in the extra directory of the tarball can be used to graphically select the current profile of a given type. This uses zenity, part of recent versions of GNOME, to display a GTK2 dialogue box and switch profiles based on your choices. You can run:
# select-profile power
for example, to display a user-friendly power profile selection menu. Note that like hprofile, this should be run as root, so you may want to create some rules in /etc/sudoers and allow users to run this command with root privileges.
If you use GDM (part of GNOME) as your login manager, you can have different versions of ~/.gnome2/gdm selected via user profiles (for example in the files ~/.hprofile/profiles/boot/files/.gnome2/gdm.dualboot and gdm.vmware for "dualboot" and "vmware" boot profiles), to start different desktops depending on your boot profile. This way, you could have an eye candy GNOME desktop, say, when you're running the machine in dualboot mode, and a minimalist WindowMaker setup for when you're booting Linux in VMWare for Windows.
Where can I find more help?

Have a look at the README file, which contains a comprehensive manual. It's also worth looking at the examples that comes with hprofile (which are actually the profiles I use on my machine). Run hprofile, and the supporting scripts hprunlevel and hpdet, with the -h command line option to see brief usage information. Finally, the extra directory which comes with the hprofile tarball contains sample startup scripts for Gentoo Linux, which show you how to apply boot, power and network profiles automatically when the system is started.

Copyright (c) 2003-2004 Martin Aspeli