@@ -26,27 +26,31 @@ KERNEL_SUFFIX=-$(awk -F "= " '/kernel_flavor/ {print $2}' ../../../../data/defau
2626
2727echo " I: Generate Kernel config"
2828ARCH=$( dpkg --print-architecture)
29+ # array to hold config fragments for merging
30+ # allows safe handling of paths with spaces and avoids word-splitting issues
31+ # The first entry is the base defconfig; subsequent entries are merged on top.
32+ KCONFIG_MERGE_FRAGMENTS=()
2933if [ " ${ARCH} " = " arm64" ]; then
30- KERNEL_CONFIG=arch/arm64/configs/vyos_defconfig
31- cp ${CWD} /config/arm64/vyos_defconfig ${KERNEL_CONFIG}
34+ KCONFIG_MERGE_FRAGMENTS+=(" ${CWD} /config/arm64/vyos_defconfig" )
3235elif [ " ${ARCH} " = " amd64" ]; then
33- KERNEL_CONFIG=arch/x86/configs/vyos_defconfig
34- cp ${CWD} /config/x86/vyos_defconfig ${KERNEL_CONFIG}
36+ KCONFIG_MERGE_FRAGMENTS+=(" ${CWD} /config/x86/vyos_defconfig" )
3537else
3638 echo " E: unsupported architecture"
3739 exit 1
3840fi
39-
40- for KRN_CONF_SNIPPET in $( ls ${CWD} /config/ * .config)
41- do
42- echo " I: adding configuration snippet ${KRN_CONF_SNIPPET} "
43- cat ${KRN_CONF_SNIPPET} >> ${KERNEL_CONFIG}
44- done
41+ # NOTE: do NOT export KCONFIG_CONFIG here. KCONFIG_CONFIG is interpreted by
42+ # kbuild as the path to the OUTPUT .config file. Exporting it globally would
43+ # cause `make vyos_defconfig` (and the rest of the kernel build) to write the
44+ # resulting config to that path instead of producing a normal `.config` in the
45+ # kernel source root, which breaks downstream steps that expect `linux/.config`
46+ # to exist.
4547
4648# VyOS requires some small Kernel Patches - apply them here
4749# It's easier to have them here and make use of the upstream
4850# repository instead of maintaining a full Kernel Fork.
4951# Saving time/resources is essential :-)
52+ # Note that the patches should be applied BEFORE the kernel config is generated
53+ # because patches may add new options!
5054PATCH_DIR=${CWD} /patches/kernel
5155for patch in $( ls ${PATCH_DIR} )
5256do
@@ -62,19 +66,51 @@ TRUSTED_KEYS_FILE=trusted_keys.pem
6266echo -n " " > $TRUSTED_KEYS_FILE
6367GIT_ROOT=$( git rev-parse --show-toplevel)
6468CERTS=$( find ${GIT_ROOT} /data/certificates -name " *.pem" -type f || true)
69+ TRUSTED_KEYS_FRAGMENT_TMP=" "
6570if [ ! -z " ${CERTS} " ]; then
6671 # add known public keys to Kernel certificate chain
6772 for file in $CERTS ; do
6873 cat $file >> $TRUSTED_KEYS_FILE
6974 done
7075 # Force Kernel module signing and embed public keys
71- echo " CONFIG_SYSTEM_TRUSTED_KEYRING" >> $KERNEL_CONFIG
72- echo " CONFIG_SYSTEM_TRUSTED_KEYS=\" $TRUSTED_KEYS_FILE \" " >> $KERNEL_CONFIG
76+ TRUSTED_KEYS_FRAGMENT_TMP=$( mktemp --suffix=.config)
77+ echo " CONFIG_SYSTEM_TRUSTED_KEYRING=y" > $TRUSTED_KEYS_FRAGMENT_TMP
78+ echo " CONFIG_SYSTEM_TRUSTED_KEYS=\" $TRUSTED_KEYS_FILE \" " >> $TRUSTED_KEYS_FRAGMENT_TMP
79+ fi
80+
81+ echo " I: Merge Kernel config snippets"
82+ # Collect config fragments into the fragment array
83+ for fragment in " ${CWD} " /config/* .config; do
84+ [ -f " ${fragment} " ] || continue
85+ echo " I: adding configuration snippet ${fragment} "
86+ KCONFIG_MERGE_FRAGMENTS+=(" ${fragment} " )
87+ done
88+ if [ -n " ${TRUSTED_KEYS_FRAGMENT_TMP} " ]; then
89+ echo " I: adding configuration snippet ${TRUSTED_KEYS_FRAGMENT_TMP} "
90+ KCONFIG_MERGE_FRAGMENTS+=(" ${TRUSTED_KEYS_FRAGMENT_TMP} " )
7391fi
7492
75- echo " I: make vyos_defconfig"
76- # Select Kernel configuration - currently there is only one
77- make vyos_defconfig
93+ # Use the kernel's own merge_config.sh to properly merge all config fragments.
94+ # This writes .config into the current (kernel source) dir.
95+ # The first fragment in the array is the base defconfig; the remaining ones
96+ # are merged on top of it. merge_config.sh will run `make alldefconfig` at
97+ # the end to expand defaults, this replaces the previous separate
98+ # `make vyos_defconfig` step.
99+ # This will contain some warnings about "<symbol> not in final .config"
100+ # which can mostly be ignored, but may provide relevant insight so we
101+ # don't suppress it.
102+ echo " I: Run scripts/kconfig/merge_config.sh to produce .config"
103+
104+ if [ ${# KCONFIG_MERGE_FRAGMENTS[@]} -eq 0 ]; then
105+ echo " E: No config fragments found for merging, cannot proceed"
106+ exit 1
107+ fi
108+ scripts/kconfig/merge_config.sh " ${KCONFIG_MERGE_FRAGMENTS[@]} "
109+
110+ if [ ! -f .config ]; then
111+ echo " E: merge_config.sh did not produce a .config in $( pwd) "
112+ exit 1
113+ fi
78114
79115echo " I: Generate environment file containing Kernel variable"
80116EPHEMERAL_KEY=" /tmp/ephemeral.key"
@@ -94,7 +130,7 @@ make bindeb-pkg BUILD_TOOLS=1 LOCALVERSION=${KERNEL_SUFFIX} KDEB_PKGVERSION=${KE
94130
95131# Back to the old Kernel build-scripts directory
96132cd $CWD
97- EPHEMERAL_KERNEL_KEY=$( grep -E " ^CONFIG_MODULE_SIG_KEY=" ${KERNEL_SRC} /$KERNEL_CONFIG | awk -F= ' {print $2}' | tr -d \" )
133+ EPHEMERAL_KERNEL_KEY=$( grep -E " ^CONFIG_MODULE_SIG_KEY=" " ${KERNEL_SRC} /.config " | awk -F= ' {print $2}' | tr -d \" )
98134if test -f " ${EPHEMERAL_KEY} " ; then
99135 rm -f ${EPHEMERAL_KEY}
100136fi
0 commit comments