customize-image with (crontab) autostart script

Recommended Posts

I try to build a customized image as described in and by using the template with the OpenMediaVault example


The goal is to build an image who auto-execute an initial setup script after being flashed and frist boot. The device has connected RGB-Leds and an OLED I2c Display on the GPIO pins. It should give color signals durring this initial setup, and then show system parameters (especially the DHCP-Address) on the oled screen *without* having a keyboard and HDMI-Display connected.


It generally works but I have some questions:
crontab @reboot execution only works when I first set a root password and remove the not_logged_in_yet file.

The customize-image function then 
- overlays a precompiled dtb file
- creates a shell script file with commands for execution after first boot
- this script is marked as executable 
- and finaly added to crontab

this last crontab command returns a "no crontab for root" in the build console, but is then added and also executed. (but only if I first chpasswd the root user)


PrepareDevice() {
	echo root:1234 | chpasswd
	rm /root/.not_logged_in_yet

	# I2c tuned to 400kHz
	cp /tmp/overlay/boot/dtb/rockchip/rk3399-rockpi4b.dtb /boot/dtb/rockchip/rk3399-rockpi4b.dtb

	mkdir /initial-setup
	cat > /initial-setup/ <<- EOF
	# turn blue RGBLed on as a first sign of life
	cd /sys/class/gpio
	echo 154 > export
	cd gpio154
	echo out > direction
	echo 1 > value

	# setup and configure the I2c OLED display
	apt-get --yes --force-yes install git git-core build-essential
	apt-get --yes --force-yes install python-dev python-pil python-smbus python-setuptools python-pip
	apt-get --yes --force-yes install i2c-tools libi2c-dev lm-sensors
	pip install psutil

	# blink as debug info
	echo 0 > value
	sleep 1s
	echo 1 > value

	# add the OLED script on every future reboot
	(crontab -u root -l; echo "@reboot python /root/OLEDisplay/ --display sh1106 --i2c-port 2 --rotate 2" ) | crontab -u root -
	# remove the initial setup on-boot execution
	crontab -u root -l | grep -v 'initial-setup/'  | crontab -u root -
	chmod +x /initial-setup/

	echo "add crontab for step1"
	# execute the initial-setup shell script on first boot
	line="@reboot /initial-setup/"
	(crontab -u root -l; echo "$line" ) | crontab -u root -

So far all ok.
The device boots up and the blue led turns on as expected.

But then for some (unknown) reason the apt install commands and crontab edits does not work as expected. 

It works when I connect a keyboard and HDMI display, log in as root and execute the `/initial-setup/`


The question is if there is a general better way or if someone can tell me how to set and remove the crontab scripts ?


Link to post
Share on other sites
Armbian is a community driven open source project. Do you like to contribute your code?



My experiences with crontab @reboot is that I dropped that solution in favor of systemd services.

To give you basic idea here comes a excerpt of code I put into the script:


cat <<EOT >|/usr/local/bin/
echo 1000 > /sys/module/usbcore/parameters/usbfs_memory_mb
exit 0
chmod +x /usr/local/bin/
cat <<EOT >|/lib/systemd/system/usb3-memory-size.service 
Description=USB3 memory sizer
ExecStart=/bin/bash -c "/usr/local/bin/"
systemctl enable usb3-memory-size.service 

It creates a new service that corrects the kernel setting for USB3 camera.

This works perfectly fine.


When such a service shall be triggered depends on your configuration. Look at the line of [Unit] section, explicitly before/after lines.

For example you can force such a service to start after login prompt pops up.



Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.