LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

基于Busybox的ifup/ifdown网络配置

 如何在基于Busybox的文件系统,自动进行网络配置?通过ifup/ifdown,从/etc/network/interfaces获取配置,是一种常见的方式。

下面从Buildroot配置ifupdown脚本、ifup/ifdown工作机制、/etc/network/interfaces分别简单记录。

1 Buildroot启动网络脚本

Buildroot配置ifupdown scripts,可以在系统启动时使能网络接口:

Target packages
    ->Networking applications
        ->ifupdown scripts

使能后会创建/etc/init.d/S40network:

#!/bin/sh
case "$1" in
  start)
    printf "Starting network: "
    /sbin/ifup -a
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;
  stop)
    printf "Stopping network: "
    /sbin/ifdown -a
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;
  restart|reload)
    "$0" stop
    "$0" start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit $?

 2 ifup/ifdown简析

ifup/ifdown是Busybox Applets,从/etc/network/interfaces解析参数进行网络配置。

 ifup/ifdown的入口函数为ifupdown_main():

ifupdown_main
    ->read_interfaces--根据/etc/network/interfaces参数,调用工具进行设置。
    ->run_mapping

其中主要iface设置如下,根据不同关键词调用不同函数:

            static const struct address_family_t *const addr_fams[] = {
                &addr_inet,
                &addr_inet6,
                &addr_link,
                NULL
            };
static const struct method_t methods[] = {
    { "manual"  , manual_up_down, manual_up_down, },
    { "wvdial"  , wvdial_up     , wvdial_down   , },
    { "ppp"     , ppp_up        , ppp_down      , },
    { "static"  , static_up     , static_down   , },
    { "bootp"   , bootp_up      , static_down   , },
    { "dhcp"    , dhcp_up       , dhcp_down     , },
    { "loopback", loopback_up   , loopback_down , },
};

static const struct address_family_t addr_inet = {
    "inet",
    ARRAY_SIZE(methods),
    methods
};

2.1 /etc/network/interfaces配置

查看当前系统中的网络接口:

ls /sys/class/net/
can0  eth0  lo

通过dhcp自动配置IP:

auto eth0                #自动启用并连接eth0网卡
iface eth0 inet dhcp     #使用DHCP服务器设置IP地址

关于dhcp配置,参考《Busybox udhcpc:自动识别ip配置以及流程解析》。

使用静态IP:

auto eth0
iface eth0 inet static
address 192.168.1.101     #设置IP地址为192.168.1.101
netmask 255.255.255.0     #设置掩码为255.255.255.0
gateway 192.168.1.1       #设置默认网关为192.168.1.1

更多参考:

 

posted on 2023-10-29 23:59  ArnoldLu  阅读(1426)  评论(1编辑  收藏  举报

导航