当前位置: 首页 > 工具软件 > SONiC OS > 使用案例 >

SONIC config_db.json文件的前生今世

岑俊弼
2023-12-01

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 类似资料: