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
}
相关推荐
Coder个人博客2 小时前
Linux6.19-ARM64 mm mmu子模块深入分析
大数据·linux·车载系统·系统架构·系统安全·鸿蒙系统
Doro再努力5 小时前
Vim 快速上手实操手册:从入门到生产环境实战
linux·编辑器·vim
wypywyp5 小时前
8. ubuntu 虚拟机 linux 服务器 TCP/IP 概念辨析
linux·服务器·ubuntu
Doro再努力5 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
senijusene5 小时前
Linux软件编程:IO编程,标准IO(1)
linux·运维·服务器
忧郁的橙子.5 小时前
02-本地部署Ollama、Python
linux·运维·服务器
醇氧5 小时前
【linux】查看发行版信息
linux·运维·服务器
No8g攻城狮6 小时前
【Linux】Windows11 安装 WSL2 并运行 Ubuntu 22.04 详细操作步骤
linux·运维·ubuntu
XiaoFan0126 小时前
免密批量抓取日志并集中输出
java·linux·服务器
souyuanzhanvip7 小时前
ServerBox v1.0.1316 跨平台 Linux 服务器管理工具
linux·运维·服务器