Question: How to add a Script to run on first boot (uci-defaults) to the compile?

Just a tad of handholding needed here. I'm using the openwrt build system to compile OpenWrt from source code with NSS support for OnHub routers and all is going well and I've figured out the basics of how to do it and all. I'm still just a little uncertain about where exactly on the build machine I locate the script to run on first boot. Does it go in a particular, specific directory and need a specific name and does it have to be chmod +x and all that stuff before the compile?

Here's what I'm working on. I'm making a nice Mesh system with two OnHub routers and I'll be using the new debugged version of mesh11sd when it's released soon. I want to add this first boot script:

# UCI configuration
uci set mesh11sd.setup.auto_config='0'
uci set mesh11sd.setup.country='JP'
uci set mesh11sd.setup.ssid_suffix_enable='1'
uci set mesh11sd.setup.vtun_enable='1'
uci set mesh11sd.setup.auto_mesh_id='karla-mesh'
uci set mesh11sd.setup.auto_mesh_key='MyOptionalMeshKeySeed'
uci set mesh11sd.setup.mesh_gate_base_ssid='Karla'
uci set mesh11sd.setup.mesh_gate_encryption='3'
uci set mesh11sd.setup.mesh_gate_key='secret_wifi_password'
uci set mesh11sd.setup.vtun_gate_encryption='3'
uci set mesh11sd.setup.vtun_base_ssid 'GuestNetworkSSIDHere'
uci set mesh11sd.setup.vtun_gate_key='GuestWifiPasswordHere'
uci commit mesh11sd
uci commit network

# Set root password
rootpassword="secret_admin_password"
/bin/passwd root << EOF
$rootpassword
$rootpassword
EOF

# Critical exit code
exit 0

Where exactly does this go in the build system? The firmware selector makes it easy, but I don't quite know exactly how I add this to my compiled binary I'm making on my own computer.

Postscript: The answer from the wiki and @Hostle

~/openwrt/files/etc/uci-defaults/99_mesh11sd

No shebang, exit 0 at the end after EOF, and no executable flag.

easiest method is to just create the ~/openwrt/files/etc/uci-defaults/10_your_script.

other options ..

package/basefiles/files/etc/uci-defaults/10_your_script

target/linux///basefiles/etc/uci-defaults/10_your_script

some things to note ..

the numeric designation in the name -ie 10_xxxx is used to fire the script in order if you have multiple scripts within uci-defaults directory.

uci-default scripts must exit 0 upon success, this will ensure the script is only fired during firstboot.

1 Like

Do I have to chmod +x 10_your_script before compiling or is that automatic? I'm going to use 99_your_script because I want to insure it runs last in sequence. Does the script have to have any particular name to work? Can just name it 99_mesh11sd? Does it need a shebang on the first line? Any little tricky things newbies miss? Does the exit 0 go after or before the EOF that's at the end of the script now?

I make sure permissions are executable..yes

use 99_ to be certain

at your discretion .. does not matter

I
use best practices .. your choice

exit 0 after you've done what you need .. always good to check for failure, in this case you can either exit 1, the script will remain in uci-defaults and be fired on each boot until success ..exit 0

or you can just print a error msg to log and exit 0 anyway preventing retries.

logger "my script failed :( " && exit 0

or 

echo "my script failed :(" && exit 0 

EDIT
return 0 and return 1 work the same as exit as you can see in the example provided

For reference, you can see the boot service sources each file, deletes it if successful and does a commit after all are processed.

3 Likes

Just putting his link in the thread for my future reference. The tips and advice I got led me to this. Seems like I should have found this on my own.

UCI defaults (wiki)

Raises question about the executable bit being set in the script since it's run in the shell interpreter and not directly invoked as an executable. "The OpenWrt boot process does not rely on the executable bit." I'm going to delete the shebang too. "Because the shell is specified directly, the kernel never looks for a shebang line."

Thanks to all. I appreciate the quick answers and help. You really helped me help myself.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.