The config files are divided into sections and options/values.
Every section has a type, but does not necessarily have a name. Every option has a name and a value and is assigned to the section it was written under.
Syntax:
config [""] # Section
option "" # Option
Every parameter needs to be a single string and is formatted exactly like a parameter for a shell function. The same rules for Quoting and special characters also apply, as it is parsed by the shell.
Parsing configuration files in custom scripts
To be able to load configuration files, you need to include the common functions with:
. /etc/functions.sh
Then you can use config_load to load config files. The function first checks for as absolute filename and falls back to loading it from /etc/config (which is the most common way of using it).
If you want to use special callbacks for sections and/or options, you need to define the following shell functions before running config_load (after including /etc/functions.sh):
config_cb() {
local type="$1"
local name="$2"
# commands to be run for every section
}
option_cb() {
# commands to be run for every option
}
You can also alter option_cb from config_cb based on the section type. This allows you to process every single config section based on its type individually.
config_cb is run every time a new section starts (before options are being processed). You can access the last section through the CONFIG_SECTION variable. Also an extra call to config_cb (without a new section) is generated after config_load is done. That allows you to process sections both before and after all options were processed.
Another way of iterating on config sections is using the config_foreach command.
Syntax:
config_foreach name> [] []
This command will run the supplied function for every single config section in the currently loaded config. The section name will be passed to the function as argument 1. If the section type is added to the command line, the function will only be called for sections of the given type.
You can access already processed options with the config_get command Syntax:
# print the value of the option
config_get
# store the value inside the variable
config_get
In busybox ash the three-option config_get is faster, because it does not result in an extra fork, so it is the preferred way.
Additionally you can also modify or add options to sections by using the config_set command.
Syntax:
config_set
If a config section is unnamed, an automatically generated name will be assigned internally, e.g. cfg1, cfg2, ...
While it is possible, using unnamed sections through these autogenerated names is strongly discouraged. Use callbacks or config_foreach instead.
1.3.1 Hotplug
1.3.2 Init scripts
Because OpenWrt uses its own init script system, all init scripts must be installed as /etc/init.d/name use /etc/rc.common as a wrapper.
Example: /etc/init.d/httpd
#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=50
start() {
[ -d /www ] && httpd -p 80 -h /www -r OpenWrt
}
stop() {
killall httpd
}
as you can see, the script does not actually parse the command line arguments itself. This is done by the wrapper script /etc/rc.common.
start() and stop() are the basic functions, which almost any init script should provide. start() is called when the user runs /etc/init.d/httpd start or (if the script is enabled and does not override this behavior) at system boot time.
Enabling and disabling init scripts is done by running /etc/init.d/name enable or /etc/init.d/name disable. This creates or removes symbolic links to the init script in /etc/rc.d, which is processed by /etc/init.d/rcS at boot time.
The order in which these scripts are run is defined in the variable START in the init script. Changing it requires running /etc/init.d/name enable again.
You can also override these standard init script functions:
* boot()
Commands to be run at boot time. Defaults to start()
* restart()
Restart your service. Defaults to stop(); start()
* reload()
Reload the configuration files for your service. Defaults to restart()
You can also add custom commands by creating the appropriate functions and referencing them in the EXTRA_COMMANDS variable. Helptext is added in EXTRA_HELP.
Example:
status() {
# print the status info
}
EXTRA_COMMANDS="status"
EXTRA_HELP=" status Print the status of the service"