busybox:启动阶段的静态 IP 配置过程

文章目录

  • [1. 前言](#1. 前言)
  • [2. 启动阶段 busybox 配置静态 IP 过程](#2. 启动阶段 busybox 配置静态 IP 过程)

1. 前言

限于作者能力水平,本文可能存在谬误,因此而给读者带来的损失,作者不做任何承诺。

2. 启动阶段 busybox 配置静态 IP 过程

编辑 /etc/network/interfaces 文件内容如下:

bash 复制代码
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.188
netmask 255.255.255.0
gateway 192.168.1.1

启动时用命令 /sbin/ifup eth0 配置网络,该命令调用 busybox/networking/ifupdown.c 中的函数 ifupdown_main() 解析 /etc/network/interfaces 对网络进行配置:

c 复制代码
/* busybox/networking/ifupdown.c */

ifupdown_main()
	...
	static_up()

static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
{
	int result;
# if ENABLE_FEATURE_IFUPDOWN_IP
	result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
			"dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
	result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
	result += execute("[[ip route add default via %gateway% dev %iface%[[ metric %metric%]]]]", ifd, exec);
	return ((result == 3) ? 3 : 0);
# else
	/* ifconfig said to set iface up before it processes hw %hwaddress%,
	 * which then of course fails. Thus we run two separate ifconfig */
	result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
				ifd, exec);
	result += execute("ifconfig %iface% %address% netmask %netmask%"
				"[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]]",
				ifd, exec);
	result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
	return ((result == 3) ? 3 : 0);
# endif
}
相关推荐
充电zcx23 分钟前
Linux:2:Linux下的基本指令
linux·运维·服务器
handler0134 分钟前
【Linux】虚拟地址空间解析
linux·运维·c++·线程·进程·虚拟地址空间·虚拟地址
新时代牛马2 小时前
Linux 驱动调试完整篇:从printk/dev_dbg、动态调试到 debugfs/ftrace 排障
java·linux·服务器
modelmd2 小时前
Linux SIGTERM信号-实验
linux
云泽8082 小时前
Git 版本控制系统(上):架构原理与操作入门
linux·git·架构
凌云若寒3 小时前
CODESOFT试用流程
linux·运维·网络·数据库·sentinel
吴声子夜歌3 小时前
Linux命令——学习Shell(三)
linux·chrome·学习
小猪佩奇TONY3 小时前
Linux 内核学习(18) --- linux dma_buf 机制
linux·运维·学习
Dream Cosmos4 小时前
自旋锁:从忙等待、原子操作到线程同步
linux
kaixin_啊啊4 小时前
PandaWiki 本地 AI 知识库实战:文档导入、智能问答与远程访问
linux·服务器·人工智能·windows·ai