Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:guide-developer:uci-defaults [2019/09/30 09:53] – changed LEDE to OpenWrt bobafetthotmaildocs:guide-developer:uci-defaults [2023/10/14 09:08] (current) – [UCI defaults] vgaetera
Line 1: Line 1:
 ====== UCI defaults ====== ====== UCI defaults ======
 +See also: [[docs:guide-user:base-system:uci|The UCI system]]
  
-===== UCI Defaults =====+//OpenWrt// relies on UCI, the //Unified Configuration Interface//, to configure its core services. 
 +UCI defaults provides a way to preconfigure your images, using UCI.
  
-//OpenWrt// relies on UCI, the //Unified Configuration Interface//, to configure its core services. UCI Defaults provides a way to preconfigure your images, using UCI.+To set some system defaults the first time the device bootscreate a script in the directory ''/etc/uci-defaults''.
  
-To set some system defaults the first time the device boots, create a script in the folder /etc/uci-defaults/ +All scripts in that directory are automatically executed by the ''boot'' service: 
- +  * If they exit with code 0 they are deleted afterwards.
-All scripts in that folder are automatically executed by /etc/init.d/boot  +
-  * If they exit with code 0 they are deleted afterwards. +
   * Scripts that exit with non-zero exit code are not deleted and will be re-executed at the next boot until they also successfully exit.   * Scripts that exit with non-zero exit code are not deleted and will be re-executed at the next boot until they also successfully exit.
  
-In a live router you can see the existing uci-defaults scripts in /rom/etc/uci-defaults , as /etc/uci-defaults itself is typically empty (after all scripts have been run ok and have been deleted).+In a live router you can see the existing UCI defaults scripts in ''/rom/etc/uci-defaults'' , as ''/etc/uci-defaults'' itself is typically empty (after all scripts have been run successfully and have been deleted).
  
-uci-defaults scripts can be created by packages or they can be inserted into the build manually as custom files.+UCI defaults scripts can be created by packages or they can be inserted into the build manually as custom files.
  
-==== Integrating custom settings ====+===== Integrating custom settings ====
 +See also: 
 +[[docs:guide-developer:toolchain:use-buildsystem#custom_files|Build system - Custom files]], 
 +[[docs:guide-user:additional-software:imagebuilder#custom_files|Image builder - Custom files]]
  
-You can preload custom settings by adding batch scripts containing UCI commands into the ''/files/etc/uci-defaults'' directory. The path is identical for the buildroot and the image generator. The scripts will be run **after** the flashing process - in case of upgrading, that also includes appending the existing configuration to the JFFS2 partition (mounted as ''/overlay''). Scripts should not be executable. To ensure your scripts are not interfering with any other scripts, make sure they get executed last by giving them a high prefix (e.g. //zzzz_customisations//). A basic script could look like this:+Easiest way to include uci-defaults scripts in your firmware may be as custom files. 
 +You can preload custom settings by adding batch scripts containing UCI commands into the ''/files/etc/uci-defaults'' directory. 
 +The path is identical for the buildroot and the image generator. 
 +The scripts will be run **after** the flashing process - in case of upgrading, that also includes appending the existing configuration to the JFFS2 partition (mounted as ''/overlay''). 
 +Scripts should not be executable. 
 +To ensure your scripts are not interfering with any other scripts, make sure they get executed last by giving them a high prefix (e.g. //xx_custom//). 
 +A basic script could look like this:
  
-<code> +<code bash
-cat zzzz_customisations +cat << "EOF"/etc/uci-defaults/99-custom 
-#!/bin/sh +uci -q batch << EOI 
- +set network.lan.ipaddr='192.168.178.1' 
-uci -q batch <<-EOT +set wireless.@wifi-device[0].disabled='0' 
-  add dhcp host +set wireless.@wifi-iface[0].ssid='OpenWrt0815' 
-  set dhcp.@host[0].name='bellerophon' +add dhcp host 
-  set network.@host[0].ip='192.168.2.100' +set dhcp.@host[-1].name='bellerophon' 
-  set network.@host[0].mac='a1:b2:c3:d4:e5:f6' +set dhcp.@host[-1].ip='192.168.2.100' 
-    EOT+set dhcp.@host[-1].mac='a1:b2:c3:d4:e5:f6' 
 +rename firewall.@zone[0]='lan' 
 +rename firewall.@zone[1]='wan' 
 +rename firewall.@forwarding[0]='lan_wan' 
 +EOI 
 +EOF
 </code> </code>
  
-Once the script has run successfully and exited cleanly (exit status 0), it will be removed from ''etc/uci-defaults''. You can still consult the original in ''/rom/etc/uci-defaults'' if needed.+This is a simple example to set up the LAN IP address, SSID, enable Wi-Fi, configure a static DHCP lease, rename firewall zone and forwarding sections. 
 +Once the script has run successfully and exited cleanly (exit status 0), it will be removed from ''/etc/uci-defaults''. 
 +You can still consult the original in ''/rom/etc/uci-defaults'' if needed.
  
-==== Ensuring scripts don’t overwrite custom settings: implementing checks ====+===== Ensuring scripts don’t overwrite custom settings: implementing checks ====
 +Scripts in ''/etc/uci-defaults'' will get executed at every first boot (i.e. after a clean install or an upgrade), possibly overwriting already existing values. 
 +If this behaviour is undesired, we recommend you implement a test at the top of your script - e.g. probe for a custom setting your script would normally configure:
  
-Scripts in ''/etc/uci-defaults'' will get executed at every first boot (i.e. after a clean install or an upgrade), possibly overwriting already existing values. If this behaviour is undesired, we recommend you implement a test at the top of your script - e.g. probe for a custom setting your script would normally configure: +<code bash>
- +
-<code>+
 [ "$(uci -q get system.@system[0].zonename)" = "America/New York" ] && exit 0 [ "$(uci -q get system.@system[0].zonename)" = "America/New York" ] && exit 0
 </code> </code>
  
 This will make sure, when the key has the correct value set, that the script exits cleanly and gets removed from ''/etc/uci-defaults'' as explained above. This will make sure, when the key has the correct value set, that the script exits cleanly and gets removed from ''/etc/uci-defaults'' as explained above.
 +
 +===== Examples =====
 +  * [[docs:guide-user:additional-software:imagebuilder#restricting_root_access|Restricting root access]]
 +  * [[https://github.com/openwrt/openwrt/tree/master/package/base-files/files/etc/uci-defaults|uci-defaults @ base-files]]
 +  * [[https://github.com/freifunk-berlin/firmware-packages/tree/master/defaults|uci-defaults @ Freifunk Berlin]]
 +  * [[https://github.com/ffulm/firmware/blob/master/files/etc/uci-defaults/50_freifunk-setup|uci-defaults @ Freifunk Ulm]]
 +  * [[https://github.com/richb-hanover/OpenWrtScripts/blob/main/config-openwrt.sh|config-openwrt @ richb-hanover]]
  
  • Last modified: 2019/09/30 09:53
  • by bobafetthotmail