Custom script to run on first boot using linux builder

Excuse me please.....I know this topic must have already be asked and answered but I couldn't find it.

I use the linux based imagebuilder sometimes but I have become spoiled by the fantastic "Custom script to run on first boot" add on with the web based GUI imagebuilder. There must be a way to incorporate "Custom script to run on first boot" into my CLI Linux builds. Could someone pls point me in the right direction for instructions on how to do this?

Thx for any help

You'll need to place a script into /etc/uci-defaults/ directory (use FILES parameter on image builder CLI to include that). Once successfully executed, the script will be deleted. You can check the /rom/etc/uci-defaults/ for examples/naming convention of the scripts which came with your firmware.

2 Likes

Thanks!!
I checked in /rom/etc/uci-defaults/ and a copy of the uci script I added thru the web GUI builder resides there. It has been named "99-asu-defaults". Should I assume I need to name a uci script "99-asu-defaults" so that the FILES parameter knows its uci commands. It looks like 99 is a sequence code or a type identifier. I don't mean to so picky but with rom flashes errors can be nightmares.

Thanks again

99 is the sequence number, so scripts are executed on the correct order.

Seeking more guidance.
I am having zero success attempting this.

"FILES=" points to a directory that resides within the image builder directory right??

So I create a directory named say...."files" in the image builder directory.
Now I just use the Parameter FILES="files" because the root path defaults to the image builder directory.. is that right?

I could name the directory "custom" and then the parameter would be FILES="custom"...is that right??


So now I create a a text file inside the "files" directory and I name it anything I want.
I'll name it "firstboot" Set permissions as NOT executable.


So now I copy an example script from the user guide and paste it into "firstboot" textfile.

cat << "EOF" > /etc/uci-defaults/xx_custom
uci -q batch << EOI
set network.lan.ipaddr='192.168.178.1'
commit network
set wireless.@wifi-device[0].disabled='0'
set wireless.@wifi-iface[0].ssid='OpenWrt0815'
commit wireless
add dhcp host
set dhcp.@host[-1].name='bellerophon'
set dhcp.@host[-1].ip='192.168.2.100'
set dhcp.@host[-1].mac='a1:b2:c3:d4:e5:f6'
commit dhcp
rename firewall.@zone[0]='lan'
rename firewall.@zone[1]='wan'
rename firewall.@forwarding[0]='lan_wan'
commit firewall
EOI
EOF

And to keep it simple for testing I edit it to read....

cat << "EOF" > /etc/uci-defaults/99-asu-defaults
uci -q batch << EOI
set network.lan.ipaddr='192.168.1.2'
set network.lan.gateway='192.168.1.1'
set network.lan.dns=192.168.1.1
set dhcp.lan.ignore=1
commit
EOI
EOF

This sets the ip address to 192.168.1.2 for a second AP router.
Save textfile "firstboot" to the "files" directory.


OK... so now from the root of image builder directory I execute....

make image PROFILE="linksys_wrt3200acm" PACKAGES="luci-app-samba4 luci-app-minidlna" FILES="files"

PACKAGES list abbreviated for posting...


Upon completion I have a functioning set of router flash files but
the "99-asu-defaults" file was not created nor executed.

What am I missing???
I am definitely an amateur and I would greatly appreciate any help with this.

Thx to all

Do not confuse absolute and relative paths, follow this example:
https://openwrt.org/docs/guide-user/additional-software/imagebuilder#restricting_root_access

It is worth mentioning that you can simply include the desired config files directly without having to execute a script with UCI parameters to change. In other words, you can take a known-good configuration from /etc/config (for example, network, firewall, DHCP, etc.) and include that int the files folder. Instead of executing a script to modify the core config files, the default configuration of your customized image will have those baked in.

You can literally copy files from a running system that is configured and known-good and then bake those into your image. It goes without saying, though, that you want to make sure that you have verified that everything does indeed work as expected so that you don't get surprised later.

The idea of executing a script is more useful if you have things that need to be done that can't be baked into the core files -- for example registering a device with an upstream administration/management system or something like that.

2 Likes

You place it in the correct subdirectory inside the "files" did. That "files" will be the root "/" in the router.

Read

And better example (for full toochain, but the same logic)

As another OpenWrt amateur, I think you have to stop using the firstboot filename on the build system and put the uci commands directly in files/etc/uci-defaults/99-asu-defaults without the cat wrapper:

uci -q batch << EOI
set network.lan.ipaddr='192.168.1.2'
set network.lan.gateway='192.168.1.1'
set network.lan.dns=192.168.1.1
set dhcp.lan.ignore=1
commit
EOI

You don’t want a script to create the 99-asu-defaults script to be in the image, you want the actual 99-asu-defaults script in the image.

OK...
Top left is my configured router..

Bottom left is the working directory for 21.02.1 Image Builder...

Right is readout of 192.168.1.2/etc/config/network from router which I have copied into the "files" directory residing in the working directory for the image builder.

From an image builder working directory command prompt I execute:

make image PROFILE="linksys_wrt1900ac-v2" FILES="files" PACKAGES="luci luci-app-samba4 luci-app-advanced-reboot luci-app-minidlna luci-app-hd-idle luci-app-wifischedule luci-app-commands luci-theme-openwrt luci-theme-openwrt-2020 luci-app-sqm luci-app-vnstat2 luci-app-statistics kmod-usb-storage kmod-usb-storage-extras kmod-usb-storage-uas irqbalance antfs-mount mount-utils block-mount blockd ntfs-3g ntfs-3g-low ntfs-3g-utils exfat-fsck exfat-mkfs kmod-fs-exfat openssh-sftp-client openssh-sftp-server netdata collectd collectd-mod-network collectd-mod-wireless collectd-mod-cpu collectd-mod-thermal collectd-mod-write-graphite collectd-mod-write-http luci-app-openvpn openvpn-openssl ip-full luci-proto-ppp"

And from that everything compiles successfully and I have working router OS flash files with all the "PACKAGES=" installed properly.

But.... the network config file that I copied into the "files" directory in the image builder working directory was not incorporated into the build. The compiler did not read or write the network config file from "files" into the build.

This isn't a bug in 21.02.1 is it??

It should be files/etc/config/network, not files/network.

1 Like

Screenshot 2021-10-31 174254

My understanding of the manual seems to say that only scripts go into the "files" directory and the are only executed after the OS image file has been successfully flashed to the router. The scripts are copied to files/etc/uci-defaults and excuted in the numerical order of the script file names. The 99 has the 99-asu-defaults executing last. If the script was successfully run and exits with a zero then it gets deleted by the system and a copy remains in /rom//etc/uci-defaults/.

The whole thing is real simple and smooth the the WEB GUI but I haven't found anybody posting an example of their successful Linux CLI command.

OK let me try that....
I'll be back

THAT WORKS !!!!!!!!
Thank you soooooo much!

What is all this stuff about writing scripts and and sending them to /etc/uci-defaults/ and naming the injected script with a starting number so it gets run last and on and on and on......

When all that is needed is create the config file and put it in the files directory with a complete and correct send address as in "/etc/config/******.

One question... I assume the sent file replaces the target file and not just updates it correct? In other words the sent config file needs to be a complete config file unlike sending a single uci command line.

If I wanted to execute a few uci commands post flash and after first boot such as ..

uci set network.lan.ipaddr=192.168.1.2
uci set network.lan.gateway=192.168.1.1
uci commit

How would that be done ?

Why do this post-boot? Why not bake it into the default config files?

Is this a trick question, uci-defaults, but seems that has already been covered in this thread.

1 Like

No no my apologies :slight_smile: This thing has just had me so twisted up that I have lost track of everywhere I have already been with it.

It started because I wanted to build a 21.02.1 image and the great people doing the WEB GUI 's had not got 21.02.1 going yet so I was just going to do one thru a linux image builder. And thus the rest of the story.....

And so any how baked in yes but config file update does come after the initial rom flash right? And your right that will serve all my purposes completely. I don't know of a reason that I would throw a single uci command at a post flash OS but just thought it would be good to know in case something weird that I haven't thought of comes up.

If you bake in a known good /etc/config/network file worth the settings you want, it will just work. No need to script it. There is no reason to include a script for something this simple.

1 Like

I think I was just having tunnel vision set off by the WEB GUI calling the addon box "custom script to run on first boot" so I told myself any changes that I wanted had to be in the form of a script.

I love learning something new every day :thinking:
Thx to everyone in this thread.