Openwrt compilation -- user defined boot of add app

I. overview

1. In openwrt system, the init process is replaced by procd, which can monitor the status of the child process as the parent process. Once the subprocess exits, it can try to restart the process at a certain time.
2. If the application software needs to run automatically when booting, you need to add the corresponding script file in / etc/init.d. The script file requires the START parameter, which indicates the priority during boot. If you shut down the script file after the boot process is started, you need to set the STOP parameter further. If the STOP parameter exists, its value must be greater than START.
3. The script file needs two functions: start() and stop(). Start() is the executing program and stop() is the closing program.
4. It is known from / etc/rc.d/S10boot that the priority of loading kernel driver module is 10, and the START value of programs that need to use their own designed kernel driver module must be greater than 10.
5. Also known by / etc/rc.d/S40network, a program using network communication must have a START value greater than 40.

2, Self starting implementation mode 1

Realize self startup based on the original app application, as shown in: https://blog.csdn.net/zhemingbuhao/article/details/103941355

1. Create a directory root / under the package/myapp / directory to store startup scripts
# mkdir package/myapp/root

Then create the startup script file in the package/myapp/root / directory, and then create the startup script file mycode in the package/myapp/root / directory. The contents are as follows

#!/bin/sh /etc/rc.common

START=92 # In the order of execution, sorting by string is not numeric
STOP=92
SERVICE=app
PROG=/app/mycode
USE_PROCD=1 # Start with procd

# The start service function must be redefined
start_service()
{
	echo service mycode start
    procd_open_instance  # Create an instance. According to procd, an application can have multiple instances
    # ubus call service list to view instances
    procd_set_param command $PROG # The command that mycode executes is "/ app/mycode". If there are parameters after it, you can add them directly
    procd_set_param respawn # Define the repawn parameter to tell procd to try to restart when the mycode program exits
    procd_close_instance # Closing instance
}
# Service "triggers are redefined. If there is no operation, it can be deleted, but it cannot be empty
service_triggers()
{
    procd_add_reload_trigger mycode
}
#Reload service is redefined. If there is no operation, it can be deleted, but it cannot be empty
reload_service()
{
	echo service mycode reload
}
# Stop service redefines the operations to be performed after exiting the service. If there is no operation, it can be deleted, but it cannot be empty
stop_service()
{
	echo service mycode stop
}
#restart is redefined. If there is no operation, it can be deleted, but it cannot be empty
restart() 
{
	stop
	start
}
1. start_service()

To register services to procd, if your application does not have a configuration file, just implement * * start_service() *,
Procd? Set? Param sets many parameters. command is your application path. respawn can detect your application. If you hang up, you can restart it. You can also set the restart interval. Other parameters can be viewed by yourself.

2. stop_service()

This is called after procd kill's own application. If your application is turned off, you need some clean-up work to implement this.

3. service_triggers()

If your application needs to be associated with a configuration file test (which needs to be placed in the / etc/config / directory), you can track the modification of the file. If the file changes, call reload_service(). In service "triggers, you can also add tracking network modifications, or track multiple configuration files at the same time.

4. reload_service()

After the configuration file changes, you need to call this function, which can be implemented according to your own needs.

Note: the difference between start and reload is

start: generally refers to application startup
reload: generally, it means only reloading the part related to the configuration file change and not restarting the entire application.

2. Then modify the package/myapp/Makefile as follows
##############################################
# 
# OpenWrt Makefile for mycode program
#
##############################################
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME := mycode
PKG_VERSION := 0.1
PKG_RELEASE := 1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
  SECTION:=utils
  CATEGORY:=Songshuai Package
  SUBMENU:=Software Testing modules
  TITLE:=This is SongShuai's test project.
  MAINTAINER:=Songshuai
endef

define Package/$(PKG_NAME)/description
	If you can't figure out what this program does, you're probably
	brain-dead and need immediate medical attention.
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Configure
endef

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR) \
		ARCH="$(LINUX_KARCH)" \
		CC="$(TARGET_CC)" \
		CFLAGS="$(TARGET_CFLAGS) -Wall" \
		LDFLAGS="$(TARGET_LDFLAGS)"
endef

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/app $(1)/etc/init.d/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/app/
	$(INSTALL_BIN) ./root/mycode $(1)/etc/init.d/
endef

define Package/$(PKG_NAME)/preinst
	#!/bin/bash
	echo 'installing $(PKG_NAME)'
endef

define Package/$(PKG_NAME)/postinst
	#!/bin/sh
	# check if we are on real system
	if [ -z "$${IPKG_INSTROOT}" ]; then
		echo "Enabling rc.d symlink for $(PKG_NAME)"
		/etc/init.d/mycode enable
	fi

	echo '$(PKG_NAME) installed successed !'
	exit 0
endef

define Package/$(PKG_NAME)/prerm
	#!/bin/sh
	# check if we are on real system
	if [ -z "$${IPKG_INSTROOT}" ]; then
		echo "Removing rc.d symlink for $(PKG_NAME)"
		/etc/init.d/mycode disable
	fi
	echo 'removeing $(PKG_NAME)'
	exit 0
endef

define Package/$(PKG_NAME)/postrm
	#!/bin/bash
	echo '$(PKG_NAME) remove successed !'
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

If the variable $ipkg? Install is empty, it means it runs on the target device, otherwise it runs on the host.

3,make menuconfig

Or configure the mycode package as an ipk module

4. Compile separately
# make package/mycode/compile V=99
5 download the generated ipk package to the target device and install it through opkg, the program will automatically start and run
# opkg install mycode_0.1-1_mipsel_24kc.ipk 
Installing mycode (0.1-1) to root...
installing mycode
Configuring mycode.
Enabling rc.d symlink for mycode
mycode installed successed !
service mycode start
[ 00 ] This is /dev/console Test
[ 01 ] This is /dev/console Test
[ 02 ] This is /dev/console Test
...
6. Uninstall available instructions
# opkg remove mycode
Removing package mycode from root...
Removing rc.d symlink for mycode
removeing mycode
service mycode stop
mycode remove successed !

In the process of program running, you can use instructions to stop the program
service mycode stop
service mycode start
service mycode restart

If you want to disable self startup when the system starts, you can use it without removing
/etc/init.d/mycode disable,
/etc/init.d/mycode enable allow self startup

Explain:

If you choose to compile to the kernel during make menuconfig, i.e. y, you need to use instructions to compile the kernel

$ make V=99 -jn #n is the number of threads used
7. After the compilation is completed, the kernel file is directly rebooted. After startup, the application mycode has been loaded and run automatically.

The effect is the same as before.

Explain:

For rebooking kernel files, you can do it in general according to the options in uboot.
Immortal UBOOT: upload the direct file on the web page
Normal UBOOT: you can use tftpboot.

3, Self startup mode 2

If the compilation of the kernel or module is not involved, and you just want to start the program on the specified development board when you start it, you can do the related work directly on the development board. Here are the simple steps:

0. Transfer the compiled executable application to the development board

You can use FileZilla, scp

1. Generally, openwrt comes with vi editor, so you can open and edit it directly
# vi  /etc/init.d/mycode

Then enter the following:

#!/bin/sh /etc/rc.common

START=92
STOP=92
SERVICE=app
PROG=/app/mycode
USE_PROCD=1

start_service()
{
	echo service mycode start
    procd_open_instance
    procd_set_param command $PROG
    procd_set_param respawn
    procd_close_instance
}

service_triggers()
{
    procd_add_reload_trigger mycode
}

stop_service()
{
	echo service mycode stop
}

restart() 
{
	stop
	start
}
2. Increase the executable rights of mycode
#chmod +x mycode / / or use chomd 777 mycode
3. Enable self start
# /etc/init.d/mycode  enable
4. Restart
# reboot

After that, you will see that the restart is already running on its own.

Explain:

Yes, you are not mistaken. This method is actually the same as the first one, except that the compilation process in openwrt SDK is omitted. In contrast, this kind of board is actually more economical.
But the problem is obvious. In fact, the kernel file does not contain the executable application of mycode. The self startup script needs to upload the corresponding file to the development board file system through the tool.
If a large number of similar systems need to be rebooted, it is recommended to compile them. You can choose y when you make menuconfig. It is easier to compile them into the kernel. The rest is to burn and write the kernel files.

4, Self starting implementation mode 3

Of course, there is a similar startup mode. You can choose whether to start automatically in make uunuconfig. If there is relevant support for luci, you can use this mode to choose. Here is a brief description

1. New file

First, create a new folder, Luci app mycode, under. / feeds/luci/applications /. Then create a new related file or folder. If the program needs a configuration file, you can create a new config file and write the related configuration items and values.

luci-app-mycode
├── Makefile
└── root
    └── etc
        ├── config
        │   └── mycode
        └── init.d
            └── mycode
1) The contents of the Makefile are:
include $(TOPDIR)/rules.mk

LUCI_TITLE:=LuCI support for mycode
LUCI_PKGARCH:=all

include ../../luci.mk
2) config/mycode content

In this example, the content of this file is empty. If you want to configure it, you can edit it.

3) init.d/mycode content

Consistent with the content in the self starting script mycode above, it will not be repeated here.

2. Running the following command will update the luci options

If you write it for the first time, you need to update it before making menuconfig, otherwise it will not appear in luci option

./scripts/feeds update -a -i
./scripts/feeds install -a
3. Select in make menuconfig


Select y directly here. If M is selected, you can also use opkg and other installation means to test.

4. Compile
# make V=99
5. Rebooking kernel files

After burning, you will find that the mycode application has started automatically.

5, Self starting implementation mode 4

It seems that this method is a bit opportunistic and not very formal, but it can be used in general situations, that is, you can write your own startup script for middle two, and start your own application directly in it.

0. Transfer the compiled executable application to the development board

You can use FileZilla, scp

1. Write the startup script in / etc/rc.local
# vi /etc/rc.local

Add content to the document

app/mycode & # Start the application as a background service
2. Give / etc/rc.local executable permissions
# chmod +x rc.local
3. Restart the system
# reboot 

Once started, the application starts.

6, Self starting implementation mode 5

If you add a startup item directly to the development board, you can copy the compiled executable files to the folder, for example, / app/mycode file. This startup method is very direct and simple, that is, connect the program to be started to / etc/rc.d /.

1. Self starting settings

Specific instructions

# ln -s /app/mycode /etc/rc.d/S92mycode

Among them:
92 in S92mycode is the priority number of start. Please turn to the front or search by yourself for details.

After automatic restart, you will find that it's really automatic startup (it seems that it's better not to say, but not to say and feel less...)

2. Cancel self starting settings

Delete / etc/rc.d/S92mycode.

Is it easy to fry chicken....

34 original articles published, 32 praised, 80000 visitors+
Private letter follow

Tags: Makefile network SDK REST

Posted on Wed, 15 Jan 2020 02:52:47 -0500 by jonnym00