SONIC config_db.json文件的前生今世

config_db.json的使用

  1. 系统启动时从config_db.json中读取数据并写入CONFIG_DB数据库,前提是config_db.json存在;
  2. 保存当前系统的一些配置信息,通过config save命令;
  3. 与CONFIG_DB存在联系,CONFIG_DB为系统实时配置数据库;

sonic_installer

sonic_buildimage/src/sonic-utilities/sonic_installer/main.py
安装新的镜像时会备份/etc/sonic下的配置文件: cp -ar /etc/sonic /host/old_config

if (binary_image_type != running_image_type) and not force:
    click.echo("Image file '{}' is of a different type than running image.\n" +
     "If you are sure you want to install this image, use -f|--force.\n" +
     "Aborting...".format(image_path))
        raise click.Abort()
        
    click.echo("Installing image {} and setting it as default...".format(binary_image_version))
    
    if running_image_type == IMAGE_TYPE_ABOOT:
        run_command("/usr/bin/unzip -od /tmp %s boot0" % image_path)
        run_command("swipath=%s target_path=/host sonic_upgrade=1 . /tmp/boot0" % image_path)
    else:
        os.chmod(image_path, stat.S_IXUSR)
        run_command(image_path)
        run_command('grub-set-default --boot-directory=' + HOST_PATH + ' 0')
        
    run_command("rm -rf /host/old_config")
   # copy directories and preserve original file structure, attributes and associated metadata
    run_command("cp -ar /etc/sonic /host/old_config")

rc.local

rc.local会检查firsttime(路径:/host/image-xxxx/platform/firsttime,image-xxxx为当前SONIC系统 version)标志文件判断是否第一次启动,如果是第一次启动,会在这期间从/host/sonic/old_config/恢复config_db.json,前提是必须存在old_config

# Try to take old configuration saved during installation
# and create a flag in /tmp/ to let updategraph service know
if [ -d /host/old_config ]; then
        mv -f /host/old_config /etc/sonic/
        rm -rf /etc/sonic/old_config/old_config
        touch /tmp/pending_config_migration
        
elif [ -f /host/minigraph.xml ]; then                                                     
        mkdir -p /etc/sonic/old_config                                          
        mv /host/minigraph.xml /etc/sonic/old_config/                                             touch /tmp/pending_config_migration      
        
elif [ -n "$migration" ] && [ -f /host/migration/minigraph.xml ];  then                           mkdir -p /etc/sonic/old_config
        mv /host/migration/minigraph.xml /etc/sonic/old_config/                                   [ -f /host/migration/acl.json ] && mv /host/migration/acl.json /etc/sonic/old_config/                                           
        [ -f /host/migration/snmp.yml ] && mv /host/migration/snmp.yml /etc/sonic/old_config/                                           
        touch /tmp/pending_config_migration                                                                                             
        [ -f /etc/sonic/updategraph.conf ] && sed -i -e "s/enabled=false/enabled=true/g" /etc/sonic/updategraph.conf    
else                                                                                            
touch /tmp/pending_config_initialization                                         
fi                          

firsttime标志文件

文件被创建于sonic-buildimage/build_debian.sh
关于build_debian.sh的一些说明可参考以下注释:

## This script is to automate the preparation for a debian file system, 
## which will be used for an ONIE installer image.
## USAGE:
##   USERNAME=username PASSWORD=password ./build_debian
...
## Prepare the file system directory
if [[ -d $FILESYSTEM_ROOT ]]; then
    sudo rm -rf $FILESYSTEM_ROOT || die "Failed to clean chroot directory"
fi
mkdir -p $FILESYSTEM_ROOT
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR
mkdir -p $FILESYSTEM_ROOT/$PLATFORM_DIR/x86_64-grub
touch $FILESYSTEM_ROOT/$PLATFORM_DIR/firsttime

build_debian.sh在编译SONIC镜像时会被执行,在slave.mk中

updategraph

sonic_buildimage/files/image_config/updategraph/updategraph
捕捉rc.local创建的标志文件pending_config_initialization或pending_config_migration,生成config_db.json;

if [ -f /tmp/pending_config_migration ]; then
    copy_config_files_and_directories $copy_list
    if [ x"${WARM_BOOT}" == x"true" ]; then
        echo "Warm reboot detected..."
    elif [ "$enabled" = "true" ]; then
        echo "Use minigraph.xml from old system..."
        reload_minigraph
        sonic-cfggen -d --print-data > /etc/sonic/config_db.json
    else
        echo "Use config_db.json from old system..."
        sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
    fi
    rm -f /tmp/pending_config_migration
    sed -i "/enabled=/d" /etc/sonic/updategraph.conf
    echo "enabled=false" >> /etc/sonic/updategraph.conf
    exit 0
fi

if [ -f /tmp/pending_config_initialization ]; then
    rm -f /tmp/pending_config_initialization
    if [ "$enabled" != "true" ]; then
        PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
        PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
        sonic-cfggen -H -k ${PRESET[0]} --preset ${PRESET[1]} > /etc/sonic/config_db.json
        redis-cli -n $CONFIG_DB_INDEX FLUSHDB
        sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
        redis-cli -n $CONFIG_DB_INDEX SET "CONFIG_DB_INITIALIZED" "1"
        exit 0
    fi
fi
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值