您当前的位置:首页 > 电子 > 单片机

MT7688学习笔记(7)——OpenWrt下编译添加自启动程序(创建MySQL为例)

时间:03-02来源:作者:点击数:

一、OpenWrt启动流程

大概流程:

1.CFE

2.linux

3./etc/preinit

4./sbin/init

5./etc/inittab

6./etc/init.d/rcS

7./etc/rc.d/S*

OpenWrt的启动脚本放在/etc/init.d目录下,所以应用程序需要在boot时自动运行,则需要在/etc/init.d中增加相应的脚本文件。而系统开机时自动运行/etc/rc.d目录下的脚本。所以在rc.d目录下、有init.d下脚本的链接文件。

二、创建文件

2.1 创建files目录

按OpenWrt的习惯,一般在package下创建一个自己程序的工程目录,里面分为src和files子目录和Makefile。src目录下一般放自己设计的全部源代码文件。Makefile见MT7688学习笔记(3)——定制OpenWrt系统及添加自开发软件。files目录下放安装文件。

2.2 创建init脚本

在files目录下创建自己程序的init脚本,如(cpp.init)。

三、编写Makefile脚本

MT7688学习笔记(3)——定制OpenWrt系统及添加自开发软件

如果用户空间的应用程序在boot时要自动运行,则需要在安装方法说明中增加自动运行的脚本文件安装和配置文件安装方法。

define Package/cpp/install
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cpp $(1)/bin/
    $(INSTALL_DATA) ./files/cpp.config $(1)/etc/config/cpp

	$(INSTALL_DIR) $(1)/etc/init.d  
	$(INSTALL_BIN) ./files/cpp.init $(1)/etc/init.d/cpp
endef

INSTALL_DIR: =install -d -m0755 意思是创建所属用户可读写和执行,其他用户可读可执行的目录。

INSTALL_BIN: =install -m0755 意思编译好的文件存放到镜像文件目录。

$(1)表示嵌入式系统的镜像目录。

四、编写init脚本

#!/bin/sh /etc/rc.common
# Copyright (C) 2010-2011 OpenWrt.org

START=99
STOP=10

第一行称为shebang line,它使用/etc/rc.common脚本作为包装器。

脚本文件需要START参数,说明在boot时的优先级,如果在boot过程启动后再关闭,则需要进一步设置STOP参数。如果STOP参数存在,其值必须大于START。

/etc/rc.d/S10boot知道,装载内核驱动模块的优先级为10,需要使用自己设计的内核驱动模块的程序其START的值必须大于10。同样由/etc/rc.d/S40network知道,使用网络通信的程序其START的值必须大于40。

start() {

  echo 'JWR1 start.' >/tmp/cpp.log

  if [ -b /dev/mmcblk0p1 ];then
    if  mountpoint -q /mnt
    then
      echo 'mount is mount' >>/tmp/cpp.log
      sd_state=1;
    else
      echo 'mount /dev/mmcblk0p1 /mnt/' >>/tmp/cpp.log
      mount /dev/mmcblk0p1 /mnt/      #......SD...
      if [ $? -eq 0 ];then
        echo 'mount /dev/mmcblk0p1 /mnt/ ok' >>/tmp/cpp.log
        sd_state=1;
      else
        echo 'mount /dev/mmcblk0p1 /mnt/ fail' >>/tmp/cpp.log
        sd_state=0;
      fi
    fi
  else	#没有SD卡
    echo '-b /dev/mmcblk0p1 null' >>/tmp/cpp.log
    sd_state=0;
  fi
......
......
stop() {
  echo 'cpp stop.'
	
  ps | grep mysqld | grep  -v grep

  if [ $? -eq 0 ];then
    killall mysqld
  fi 
}

脚本文件需要start()和stop()两个函数,start()是执行程序,stop()是关闭程序。关闭程序一般需要执行killall命令。

五、示例代码

以启动mysql和自定义程序(cpp)为例。

#!/bin/sh /etc/rc.common
# Copyright (C) 2010-2011 OpenWrt.org

START=99
STOP=10

#SERVICE_DAEMONIZE=1
#SERVICE_WRITE_PID=1
#SERVICE_STOP_TIME=9


HOSTNAME="127.0.0.1"
PORT="3306"
USERNAME="root"
PASSWORD="1234"
DB_NAME="project"
TABLE_NAME_SERVICE_OBJECT="serviceObject_t"
TABLE_NAME_EVENT="event_t"
TABLE_NAME_ACTION="action_t"
TABLE_NAME_IFTTT_LOG="iftttLog_t"


create_db_sql="CREATE DATABASE IF NOT EXISTS ${DB_NAME}"


create_table_serviceObject_sql="CREATE TABLE IF NOT EXISTS ${TABLE_NAME_SERVICE_OBJECT} (
  id int(11) unsigned NOT NULL,
  gid smallint unsigned DEFAULT NULL,
  type tinyint unsigned NOT NULL,
  matchCode smallint unsigned NOT NULL,
  tag tinyint unsigned NOT NULL,
  name varchar(255) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM;"


create_table_event_sql="CREATE TABLE IF NOT EXISTS ${TABLE_NAME_EVENT} (
  id smallint unsigned NOT NULL,
  type tinyint unsigned NOT NULL,
  pri tinyint unsigned DEFAULT NULL,
  mode tinyint unsigned DEFAULT NULL,
  upperLimit int(11) unsigned DEFAULT NULL,
  lowerLimit int(11) unsigned DEFAULT NULL,
  actionId int(11) DEFAULT NULL,
  actionType smallint unsigned DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM;"


create_table_action_sql="CREATE TABLE IF NOT EXISTS ${TABLE_NAME_ACTION} (
  id smallint unsigned NOT NULL,
  parm varchar(255) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM;"


create_table_IftttLog_sql="CREATE TABLE IF NOT EXISTS ${TABLE_NAME_IFTTT_LOG} (
  id  int(11) NOT NULL AUTO_INCREMENT ,
  d_id  varchar(255) NOT NULL ,
  e_type  int(11) NOT NULL ,
  e_value  int(11) NOT NULL ,
  a_type int(11) NOT NULL ,
  datetime  datetime NOT NULL ,
  uploadFlag int NOT NULL DEFAULT 0,
  PRIMARY KEY (id)
) ENGINE=MyISAM;"


sd_state=0;
mysql_state=0;

databaseCreate(){
	echo "mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"" >>/tmp/cpp.log
	mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"
	if [ $? -eq 0 ];then
		echo "mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_serviceObject_sql}"" >>/tmp/cpp.log
		mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_serviceObject_sql}"
		if [ $? -eq 0 ];then
			echo "mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_event_sql}"" >>/tmp/cpp.log
			mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_event_sql}"
				if [ $? -eq 0 ];then
					echo "mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_action_sql}"" >>/tmp/cpp.log
					mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_action_sql}"
						if [ $? -eq 0 ];then
							echo "mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_IftttLog_sql}"" >>/tmp/cpp.log
							mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DB_NAME} -e "${create_table_IftttLog_sql}"
							if [ $? -eq 0 ];then
								mysql_state=1
								echo 'mysql_state='yes'' >>/tmp/cpp.log
							else
								echo 'create_table_IftttLog_sql fail' >>/tmp/cpp.log
							fi
					else
						echo 'create_table_action_sql  fail' >>/tmp/cpp.log
					fi			
				else
					echo 'mysql create_table_event_sql  fail' >>/tmp/cpp.log
				fi
			else
				echo 'mysql create_table_serviceObject_sql  fail' >>/tmp/cpp.log
			fi

	else
		echo 'mysql  create_db_sql fail' >>/tmp/cpp.log
	fi
}

error() {
        echo "${initscript}:" "$@" 1>&2
}

#程序开始
start() {

        echo 'JWR1 start.' >/tmp/cpp.log

        if [ -b /dev/mmcblk0p1 ];then
                if  mountpoint -q /mnt
                then
                        echo 'mount is mount' >>/tmp/cpp.log
                        sd_state=1;
                else
                        echo 'mount /dev/mmcblk0p1 /mnt/' >>/tmp/cpp.log
                        mount /dev/mmcblk0p1 /mnt/      #......SD...
                        if [ $? -eq 0 ];then
                                echo 'mount /dev/mmcblk0p1 /mnt/ ok' >>/tmp/cpp.log
                                sd_state=1;
                        else
                                echo 'mount /dev/mmcblk0p1 /mnt/ fail' >>/tmp/cpp.log
                                sd_state=0;
                        fi
                fi
        else	#没有SD卡
                echo '-b /dev/mmcblk0p1 null' >>/tmp/cpp.log
                sd_state=0;
        fi

	
	if [ $sd_state -eq 0 ]; then
		mysqlPath="/usr/data";
		echo 'sed -i "s/mnt/usr/g" /etc/my.cnf' >>/tmp/cpp.log
		sed -i 's/mnt/usr/g' /etc/my.cnf  #将/etc/my.cnf里所有mnt替换为etc
	else
		mysqlPath="/mnt/data";
	fi

	if [ $? -eq 0 ];then
		if [ ! -d "$mysqlPath" ]; then
			echo 'mysql initialize' >>/tmp/cpp.log
			echo "mkdir ${mysqlPath}/mysql -p" >>/tmp/cpp.log
			echo "mkdir ${mysqlPath}/tmp -p" >>/tmp/cpp.log
			echo 'mysql_install_db --force' >>/tmp/cpp.log
	
			#echo 'ps | grep mysqld | grep -v grep' >>/tmp/cpp.log
			#ps | grep mysqld | grep  -v grep

			#if [ $? -eq 0 ];then
			#	echo 'killall mysqld' >>/tmp/cpp.log
			#	killall mysqld
			#	if [ $? -eq 0 ];then	
			#		echo 'killall mysqld ok' >>/tmp/cpp.log	
			#	fi
			#else
			#	echo 'mysqld not run, no need killall' >>/tmp/cpp.log
			#fi		
			chmod o+x /etc/init.d/mysqld
			mkdir ${mysqlPath}/mysql -p
			mkdir ${mysqlPath}/tmp -p
			mysql_install_db --force
		

			if [ $? -eq 0 ];then
				echo '/etc/init.d/mysqld start' >>/tmp/cpp.log
				/etc/init.d/mysqld start
				if [ $? -eq 0 ];then
					echo "mysqladmin -u root password ${PASSWORD}" >>/tmp/cpp.log
					sleep 1s
					mysqladmin -u root password ${PASSWORD}
					if [ $? -eq 0 ];then
						databaseCreate
					else
						echo 'mysqladmin -u root password '1234' fail' >>/tmp/cpp.log
					fi
				else
					echo '/etc/init.d/mysqld start fail' >>/tmp/cpp.log
				fi

			else
				echo 'mysql_install_db --force fail' >>/tmp/cpp.log
			fi
		else
			echo 'mysql No need to initialize' >>/tmp/cpp.log
			#echo 'ps | grep mysqld | grep -v grep' >>/tmp/cpp.log
			#ps | grep mysqld | grep  -v grep

			#if [ $? -eq 0 ];then
			#	echo 'killall mysqld' >>/tmp/cpp.log
			#	killall mysqld
			#else
			#	echo 'mysqld not run, no need killall' >>/tmp/cpp.log
			#fi

			#if [ $? -eq 0 ];then
			while [[find /tmp/run -name mysqld.sock] -ne 0]
			do
				echo '/etc/init.d/mysqld restart' >>/tmp/cpp.log
				/etc/init.d/mysqld restart
			done
			#	echo '/etc/init.d/mysqld restart' >>/tmp/cpp.log
			#	/etc/init.d/mysqld restart
			#	if [ $? -eq 0 ];then
			#		sleep 1s
			#		databaseCreate
			#	else
			#		echo '/etc/init.d/mysqld restart fail' >>/tmp/cpp.log
			#	fi
			#else
			#	echo 'killall mysqld fail' >>/tmp/cpp.log
			#fi
		fi
	else
		echo 'sed -i "s/mnt/tmp/g" /etc/my.cnf fail' >>/tmp/cpp.log
	fi
	
	echo "/bin/cpp ${mysql_state} ${sd_state}" >>/tmp/cpp.log
	/bin/cpp  ${mysql_state} ${sd_state};
}

stop() {
        echo 'cpp stop.'
	
        ps | grep mysqld | grep  -v grep

        if [ $? -eq 0 ];then
                killall mysqld
        fi 
}

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门