0
gufmar

research customize-image with (crontab) autostart script

Recommended Posts

I try to build a customized image as described in https://docs.armbian.com/Developer-Guide_User-Configurations/#user-provided-image-customization-script and by using the template with the OpenMediaVault example https://github.com/armbian/build/blob/master/config/templates/customize-image.sh.template

 

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/step1.sh <<- EOF
	#!/bin/bash
	
	# 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/sysinfo.py --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/step1.sh'  | crontab -u root -
	
	reboot
	EOF
	chmod +x /initial-setup/step1.sh

	echo "add crontab for step1"
	# execute the initial-setup shell script on first boot
	line="@reboot /initial-setup/step1.sh"
	(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/step1.sh`

 

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

 

Share this post


Link to post
Share on other sites

Hi,

 

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 customize-image.sh script:

 

cat <<EOT >|/usr/local/bin/usb3-memory-size.sh
#!/bin/bash
echo 1000 > /sys/module/usbcore/parameters/usbfs_memory_mb
exit 0
EOT
chmod +x /usr/local/bin/usb3-memory-size.sh
cat <<EOT >|/lib/systemd/system/usb3-memory-size.service 
[Unit]
Description=USB3 memory sizer
Before=basic.target
After=sysinit.target local-fs.target
DefaultDependencies=no
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "/usr/local/bin/usb3-memory-size.sh"
[Install]
WantedBy=basic.target
EOT
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.

 

 

Share this post


Link to post
Share on other sites
10 hours ago, Hijax said:

solution in favor of systemd services

loving this Armbian forum and community. It works great! TYVM

Share this post


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.

Guest
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.

Loading...
0