Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Updated - Automatic system configuration based on profiles
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
optilude
Apprentice
Apprentice


Joined: 29 May 2002
Posts: 248
Location: England

PostPosted: Mon Apr 07, 2003 1:02 pm    Post subject: Updated - Automatic system configuration based on profiles Reply with quote

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
Code:

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:
Code:

#!/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:
Code:

#!/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:
Code:

#!/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
_________________
--
"Life is both a major and a minor key" -- Travis


Last edited by optilude on Mon Dec 22, 2003 8:14 pm; edited 2 times in total
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20053

PostPosted: Mon Apr 07, 2003 10:31 pm    Post subject: Reply with quote

I've moved the older version to duplicates. If there are future versions, please update this thread rather than creating another one. Thanks.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
optilude
Apprentice
Apprentice


Joined: 29 May 2002
Posts: 248
Location: England

PostPosted: Tue Apr 08, 2003 11:15 am    Post subject: Reply with quote

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).
_________________
--
"Life is both a major and a minor key" -- Travis
Back to top
View user's profile Send private message
Carlo
Developer
Developer


Joined: 12 Aug 2002
Posts: 3356

PostPosted: Tue Apr 08, 2003 1:43 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
optilude
Apprentice
Apprentice


Joined: 29 May 2002
Posts: 248
Location: England

PostPosted: Tue Apr 08, 2003 5:48 pm    Post subject: Reply with quote

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
_________________
--
"Life is both a major and a minor key" -- Travis
Back to top
View user's profile Send private message
fruers
n00b
n00b


Joined: 02 Feb 2003
Posts: 12
Location: Australia

PostPosted: Tue Aug 05, 2003 4:38 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
optilude
Apprentice
Apprentice


Joined: 29 May 2002
Posts: 248
Location: England

PostPosted: Tue Aug 05, 2003 8:42 am    Post subject: Reply with quote

*blush* :-)

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. :-)

/Martin
_________________
--
"Life is both a major and a minor key" -- Travis
Back to top
View user's profile Send private message
chimy
n00b
n00b


Joined: 24 Jul 2003
Posts: 19
Location: Zürich, Switzerland

PostPosted: Sun Nov 16, 2003 1:40 pm    Post subject: Reply with quote

I'm really trying to get to this. but I guess this is just too much for me :) since I only want to select diffrent boot levels it would be just somehow too much.
_________________
blend it all
http://www.blender3d.ch/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum