How to build SONiC image with given configuration

919 views
Skip to first unread message

Nan Ni

unread,
Oct 8, 2020, 1:40:29 PM10/8/20
to sonicproject
Hello,
I'd like to build a SONiC image with a custom configuration file which switch runs with once booted up. Can someone point me how to do that during build process? Thanks for your help.

Nan

Masaru OKI

unread,
Oct 8, 2020, 2:27:14 PM10/8/20
to sonicproject
Hi,

No generic way, I think.
Factory default configuration are generated by do_config_initialization() in files/image_config/config-setup/config-setup.
You may customize this function.

2020年10月9日金曜日 2:40:29 UTC+9 Nan Ni:

Nan Ni

unread,
Oct 12, 2020, 9:53:13 AM10/12/20
to sonicproject
Thanks a lot for the information, Masaru.

Let me back up a little bit, my goal is to run the switch in desired configuration without using ZTP, DHCP, or console port, is there a better way besides building an image with given configuration? (Other NOSes allow user-provided startup file at boot time, but SONiC does not seem to have that option.) Thanks!

Nan

Rajendra Dendukuri

unread,
Oct 14, 2020, 12:35:20 AM10/14/20
to Nan Ni, sonicproject
Hi Nan,

You can define a factory default configuration template and a factory-default hook which renders the template. Please refer to the below link for an example.

You can modify sonic-buildimage to include these hooks and template files so that they are packaged into the image. When the switch boots without a "/etc/sonic/config_db.json", the factory default configuration is created and is used by the switch.

Add your hook script(s) in the following location
sonic-buildimage/files/image_config/config-setup/factory-default-hooks/my-hook-script

Add your config_db.json.j2 template to
sonic-buildimage/files/image_config/config-setup/templates/config_db.json.j2

Modify "files/build_templates/sonic_debian_extension.j2" to copy the my-hook-script to $FILESYSTEM_ROOT/etc/config-setup/factory-default-hooks.d/ and the template file to $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES

Regards,
Rajen

--
You received this message because you are subscribed to the Google Groups "sonicproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonicproject...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonicproject/fd27b2cf-b83e-4582-80dc-2b27b764aca1n%40googlegroups.com.

zhenghui cai

unread,
Oct 16, 2020, 9:55:58 AM10/16/20
to Rajendra Dendukuri, Nan Ni, sonicproject
Hi, Rajen, 

Is it common practice for each vendor to put their hooks/templates in common code? or we should put in platform/device directory then use those locations instead? 
Thanks

Zhenghui

Rajendra Dendukuri

unread,
Oct 16, 2020, 10:10:58 AM10/16/20
to zhenghui cai, Nan Ni, sonicproject
Hi,

If the templates and hooks are custom to your organization/vendor implementation, you may want to create a new directory hierarchy and use organization extension script to include them into your build. In this particular case, default configuration is an implementation choice so it has to be maintained outside of common SONiC source code. See "files/build_templates/organization_extensions.sh"

The platform/vendor directory have traditionally been used for platform specific settings and not for user space differences.

We can discuss.

Regards,
Rajen

Nan Ni

unread,
Oct 16, 2020, 4:31:25 PM10/16/20
to sonicproject
In a  cluster with many switches, it is not very efficient to build an image for each even if this can be automated, and sometimes the differences across images could be minor (e.g. IP address assignment). It would be convenient for multiple switches to use same image but allow a separate startup configuration file for each.   

Thanks,
Nan

shih...@gmail.com

unread,
Apr 10, 2021, 12:46:40 PM4/10/21
to sonicproject
Hi Rajen / All,

I'm looking into config-setup as well and wanted to revive this thread.  A few questions:

From earlier in the thread:
    Modify "files/build_templates/sonic_debian_extension.j2" to copy the my-hook-script to $FILESYSTEM_ROOT/etc/config-setup/factory-default-hooks.d/ and the template file to $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES
  * Why doesn't community code have this logic?  Why does one need to add it in their private repo to get config-setup infra to work?

 From what I understand, the two options for config-setup infra:
1) add scripts in sonic-buildimage/files/image_config/config-setup/
2) add scripts in other directory & modify files/build_templates/organization_extensions.sh

Neither is upstream-able to SONiC community GitHub since they are vendor or platform specific scripts in central file locations.  Is my understanding right?  If so, I would agree with Zhenghui that platform/vendor directory may be a better spot to put them.

Thanks - Jeff
Reply all
Reply to author
Forward
0 new messages