@@ -27,27 +27,31 @@ KERNEL_SUFFIX=-$(awk -F "= " '/kernel_flavor/ {print $2}' ../../../../data/defau
2727
2828echo " I: Generate Kernel config"
2929ARCH=$( dpkg --print-architecture)
30+ # array to hold config fragments for merging
31+ # allows safe handling of paths with spaces and avoids word-splitting issues
32+ # The first entry is the base defconfig; subsequent entries are merged on top.
33+ KCONFIG_MERGE_FRAGMENTS=()
3034if [ " ${ARCH} " = " arm64" ]; then
31- KERNEL_CONFIG=arch/arm64/configs/vyos_defconfig
32- cp ${CWD} /config/arm64/vyos_defconfig ${KERNEL_CONFIG}
35+ KCONFIG_MERGE_FRAGMENTS+=(" ${CWD} /config/arm64/vyos_defconfig" )
3336elif [ " ${ARCH} " = " amd64" ]; then
34- KERNEL_CONFIG=arch/x86/configs/vyos_defconfig
35- cp ${CWD} /config/x86/vyos_defconfig ${KERNEL_CONFIG}
37+ KCONFIG_MERGE_FRAGMENTS+=(" ${CWD} /config/x86/vyos_defconfig" )
3638else
3739 echo " E: unsupported architecture"
3840 exit 1
3941fi
40-
41- for KRN_CONF_SNIPPET in $( ls ${CWD} /config/ * .config)
42- do
43- echo " I: adding configuration snippet ${KRN_CONF_SNIPPET} "
44- cat ${KRN_CONF_SNIPPET} >> ${KERNEL_CONFIG}
45- done
42+ # NOTE: do NOT export KCONFIG_CONFIG here. KCONFIG_CONFIG is interpreted by
43+ # kbuild as the path to the OUTPUT .config file. Exporting it globally would
44+ # cause `make vyos_defconfig` (and the rest of the kernel build) to write the
45+ # resulting config to that path instead of producing a normal `.config` in the
46+ # kernel source root, which breaks downstream steps that expect `linux/.config`
47+ # to exist.
4648
4749# VyOS requires some small Kernel Patches - apply them here
4850# It's easier to have them here and make use of the upstream
4951# repository instead of maintaining a full Kernel Fork.
5052# Saving time/resources is essential :-)
53+ # Note that the patches should be applied BEFORE the kernel config is generated
54+ # because patches may add new options!
5155PATCH_DIR=${CWD} /patches/kernel
5256for patch in $( ls ${PATCH_DIR} )
5357do
@@ -62,19 +66,51 @@ TRUSTED_KEYS_FILE=trusted_keys.pem
6266# start with empty key file
6367echo -n " " > $TRUSTED_KEYS_FILE
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