08 package分析

1.1 软件包目录结构(以dnsmasq为例)

一般目录下会有两个文件夹和一个Makefile

Makefile
fille       :默认配置和初始化脚本
src         :存放代码
patch       :可选

1.1.1 Makefile

include $(TOPDIR)/rules.mk      # 包含顶层rules.mk

# 软件包的基本信息
PKG_NAME:=dnsmasq
PKG_VERSION:=2.78
PKG_RELEASE:=6

# 固件包
# 下载地址
# HASH校验
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz 
PKG_SOURCE_URL:=http://thekelleys.org.uk/dnsmasq/
PKG_HASH:=89949f438c74b0c7543f06689c319484bd126cc4b1f8c745c742ab397681252b

# 许可证书、协议
PKG_LICENSE:=GPL-2.0
PKG_LICENSE_FILES:=COPYING

# 编译目录
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_NAME)-$(PKG_VERSION)

PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
PKG_CONFIG_DEPENDS:=CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dhcpv6 \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dnssec \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_auth \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_ipset \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_conntrack \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_noid \
	CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_broken_rtc

include $(INCLUDE_DIR)/package.mk

define Package/dnsmasq/Default
  SECTION:=net
  CATEGORY:=Base system
  TITLE:=DNS and DHCP server
  URL:=http://www.thekelleys.org.uk/dnsmasq/
  USERID:=dnsmasq=453:dnsmasq=453
endef

define Package/dnsmasq
$(call Package/dnsmasq/Default)
  VARIANT:=nodhcpv6
endef

define Package/dnsmasq-dhcpv6
$(call Package/dnsmasq/Default)
  TITLE += (with DHCPv6 support)
  DEPENDS:=@IPV6
  VARIANT:=dhcpv6
endef

define Package/dnsmasq-full
$(call Package/dnsmasq/Default)
  TITLE += (with DNSSEC, DHCPv6, Auth DNS, IPset, Conntrack, NO_ID enabled by default)
  DEPENDS:=+PACKAGE_dnsmasq_full_dnssec:libnettle \
	+PACKAGE_dnsmasq_full_ipset:kmod-ipt-ipset \
	+PACKAGE_dnsmasq_full_conntrack:libnetfilter-conntrack
  VARIANT:=full
endef

define Package/dnsmasq/description
  It is intended to provide coupled DNS and DHCP service to a LAN.
endef

define Package/dnsmasq-dhcpv6/description
$(call Package/dnsmasq/description)

This is a variant with DHCPv6 support
endef

define Package/dnsmasq-full/description
$(call Package/dnsmasq/description)

This is a fully configurable variant with DHCPv6, DNSSEC, Authoritative DNS and
IPset, Conntrack support & NO_ID enabled by default.
endef

define Package/dnsmasq/conffiles
/etc/config/dhcp
/etc/dnsmasq.conf
endef

define Package/dnsmasq-full/config
	if PACKAGE_dnsmasq-full
	config PACKAGE_dnsmasq_full_dhcpv6
		bool "Build with DHCPv6 support."
		depends on IPV6
		default y
	config PACKAGE_dnsmasq_full_dnssec
		bool "Build with DNSSEC support."
		default y
	config PACKAGE_dnsmasq_full_auth
		bool "Build with the facility to act as an authoritative DNS server."
		default y
	config PACKAGE_dnsmasq_full_ipset
		bool "Build with IPset support."
		default y
	config PACKAGE_dnsmasq_full_conntrack
		bool "Build with Conntrack support."
		default y
	config PACKAGE_dnsmasq_full_noid
		bool "Build with NO_ID. (hide *.bind pseudo domain)"
		default y
	config PACKAGE_dnsmasq_full_broken_rtc
		bool "Build with HAVE_BROKEN_RTC."
		default n
	endif
endef

Package/dnsmasq-dhcpv6/conffiles = $(Package/dnsmasq/conffiles)
Package/dnsmasq-full/conffiles = $(Package/dnsmasq/conffiles)

TARGET_CFLAGS += -ffunction-sections -fdata-sections
TARGET_LDFLAGS += -Wl,--gc-sections

COPTS = $(if $(CONFIG_IPV6),,-DNO_IPV6)

ifeq ($(BUILD_VARIANT),nodhcpv6)
	COPTS += -DNO_DHCP6
endif

ifeq ($(BUILD_VARIANT),full)
	COPTS += $(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dhcpv6),,-DNO_DHCP6) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dnssec),-DHAVE_DNSSEC) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_auth),,-DNO_AUTH) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_ipset),,-DNO_IPSET) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_conntrack),-DHAVE_CONNTRACK,) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_noid),-DNO_ID,) \
		$(if $(CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_broken_rtc),-DHAVE_BROKEN_RTC)
	COPTS += $(if $(CONFIG_LIBNETTLE_MINI),-DNO_GMP,)
else
	COPTS += -DNO_AUTH -DNO_IPSET -DNO_ID
endif

MAKE_FLAGS := \
	$(TARGET_CONFIGURE_OPTS) \
	CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \
	LDFLAGS="$(TARGET_LDFLAGS)" \
	COPTS="$(COPTS)" \
	PREFIX="/usr"

define Package/dnsmasq/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(CP) $(PKG_INSTALL_DIR)/usr/sbin/dnsmasq $(1)/usr/sbin/
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DATA) ./files/dhcp.conf $(1)/etc/config/dhcp
	$(INSTALL_DATA) ./files/dnsmasq.conf $(1)/etc/dnsmasq.conf
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/dnsmasq.init $(1)/etc/init.d/dnsmasq
	$(INSTALL_DIR) $(1)/etc/hotplug.d/ntp
	$(INSTALL_DATA) ./files/dnsmasqsec.hotplug $(1)/etc/hotplug.d/ntp/25-dnsmasqsec
endef

Package/dnsmasq-dhcpv6/install = $(Package/dnsmasq/install)

define Package/dnsmasq-full/install
$(call Package/dnsmasq/install,$(1))
ifneq ($(CONFIG_PACKAGE_dnsmasq_full_dnssec),)
	$(INSTALL_DIR) $(1)/usr/share/dnsmasq
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/trust-anchors.conf $(1)/usr/share/dnsmasq
endif
endef

$(eval $(call BuildPackage,dnsmasq))
$(eval $(call BuildPackage,dnsmasq-dhcpv6))
$(eval $(call BuildPackage,dnsmasq-full))

1.1.1.1 rules.mk文件中经常使用的变量

  • INCLUDE_DIR : 源代码目录下的include目录
  • BUILD_DIR : 代码编译的根目录,通常为build_dir/target-
  • TARGET_CFLAGS : 指定目标平台的C语言编译选项
  • TARGET_LDFLAGS : 指定目标平台的编译连接选项
  • INSTALL_DIR : 创建目录,并设置目录权限
  • INSTALL_DATA : 安装数据文件,并设置权限为0644
  • INSTALL_CONF : 安装配置文件,并设置权限为0600
  • INSTALL_BIN : 安装可执行文件,并设置权限为0777

1.1.1.2 Makefile常用变量

变量 含义 示例
PKG_NAME 软件包名称
PKG_VERSION 版本号
PKG_RELEASE Makefile的版本
PKG_SOURCE 原始代码文件名
PKG_SOURCE_URL 下载源码的地址
PKG_MD5SUM 软件包的MD5值
PKG_LICENSE 这个软件的许可协议
PKG_LICENSE_FILES 许可协议文件
PKG_BUILD_DIR 软件包的编译目录
PKG_INSTALL 设置为1将调用软件包自己的“make install”,安装目录前缀为PKG_INSTALL_DIR
PKG_BUILD_PARALLEL 是否可以并行编译,“并行为1”
PKG_CONFIG_DEPENDS 编译依赖,指定那些选项依赖本软件包
PKG_INSTALL_DIR 调用原始软件包“make install”的安装目录
PKG_SOURCE_PROTO 用于下载的传输协议(git,svn)。如果为压缩包则不用指定
PKG_SOURCE_SUBDIR 下载目录,如果下载传输协议为:git svn时需要指定 PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION 下载协议为“git”时必须指定
PKG_MAINTAINER 维护者的姓名和邮箱
PKG_BUILD_DEPENDS 软件包编译依赖,即需要在这个包编译之前编译

1.1.1.3 软件包定义

软件包定义用于编译前的软件包选择和编译后的IPKG安装包生成。这些参数传递给buildroot进行交叉编译
Package/开头,Package开头的定义用于make menuconfig,选择编译生成的软件包
Build/开头,这些用于代码编译


  • Package选项
  1. SECTION:软件包的类型。eg: betwork、Sound
  2. CATEGORY:在menuconfig中显示到菜单分类中
  3. TITLE:标题,是软包的简短描述
  4. URL:软件包的原始网站地址
  5. MAINTAINER:维护者的姓名和邮件地址
  6. DEPENDS:依赖项,需要在本软件包之前安装的软件包
define Package/dnsmasq/Default
  SECTION:=net
  CATEGORY:=Base system
  TITLE:=DNS and DHCP server
  URL:=http://www.thekelleys.org.uk/dnsmasq/
  USERID:=dnsmasq=453:dnsmasq=453
endef

  • Package的选项
  1. Package/<>
    定义软件包的描述信息
  2. Package/<>/Default
    可选。软件包的默认选项
  3. Package/<>/description
    软包的详细描述
  4. Package/<>/install
    复制文件到ipkg目录中,使用$(1)代表ipkg目录
  5. Package/<>/config
    可选。对编译选项进行定义
  6. Package/<>/conffiles
    可选。定义本软件包的运行配置文件列表。一行一个文件
  7. Package/<>/preinst
    可选。安装之前实际执行的脚本
  8. Package/<>/postinst
    可选。安装完成之后执行的脚本
  9. Package/<>/prerm
    可选。在删除之前执行的脚本
  10. Package/<>/postrm
    可选。在删除之后执行的脚本

1.1.1.4 软件包的构建

软件包的编译步骤为:准备(Prepare)、配置(Configure)、编译(Compile)、安装(Install)
MAKE_FLAGS:指定编译选项
CONFIGURE_ARGS:指定配置选项
以上变量在package-default.mk中定义

  • Build/Prepare
    可选,用于解包和打补丁命令。
  • Build/Configure
    可选
  • Build/Compile
    可选,编译代码
  • Build/Install
    可选。安装编译后的命令。默认使用make install
  • Build/InstallDev
    可选。只对静态库和头文件等

1.1.2 软件包的编译

单独编译

make package/hello/build
make package/hello/install
make package/hello/{clean,compile,install}

加入平台编译(两种方法)

  • 使用make menuconfig选择hello软件包
  • 直接修改.config在其中添加CONFIG_PACKAGE_hello=y,(这个名字在软件包中定义)

1.1.3 软件的启动机制

启动机制

init进程是所有系统进程的父进程,它被内核调用并负责调用所有其他进程。如果其他进程的父进程退出,则init进程会成为它的父进程
内核启动后会调用/etc/inittab,然后执行inittab中的sysinit所指的脚本

cat /etc/inittab 
::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
::askconsole:/bin/login

内核调用/etc/inittab,然后调用脚本/etc/init.d/rcS。再由rcS逐个启动各个软件进程
OpenWrt会将需要自启动的软件在/etc/rc.d目录下创建对应的软链接。之后由rcS按照软链接的顺序执行就ok了(所有的启动脚本都放在/etc/init.d下面。然后软链接到/etc/rc.d
设备将以/etc/rc.d/S开头的脚本按顺序传递给rcS

posted @ 2022-01-16 16:07  人民广场的二道贩子  阅读(299)  评论(0编辑  收藏  举报