From 6683e16c035c63d10d1d286fa1f77d166fdddaf3 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Tue, 6 Sep 2016 15:44:39 +0800 Subject: [PATCH 1/2] [check-config] Search possible files in dir If we give a dir as argument for check-config, it will treat the dir as a config file, and grep config contents from the dir, and get wrong result, as: | # script/check-config.sh /linux | info: reading kernel config from /linux ... | | Generally Necessary: | - cgroup hierarchy: properly mounted [/sys/fs/cgroup] | - CONFIG_NAMESPACES: missing | - CONFIG_NET_NS: missing | - CONFIG_PID_NS: missing | - CONFIG_IPC_NS: missing | - CONFIG_UTS_NS: missing | - CONFIG_CGROUPS: missing | - CONFIG_CGROUP_CPUACCT: missing | - CONFIG_CGROUP_DEVICE: missing | - CONFIG_CGROUP_FREEZER: missing | - CONFIG_CGROUP_SCHED: missing | ... We can search possible config files in the dir, after patch: | # script/check-config.sh /linux | warning: /linux seems not a kernel config, searching other paths for kernel config ... | info: reading kernel config from /linux/.config ... | | Generally Necessary: | - cgroup hierarchy: properly mounted [/sys/fs/cgroup] | - CONFIG_NAMESPACES: enabled | - CONFIG_NET_NS: enabled | - CONFIG_PID_NS: enabled | - CONFIG_IPC_NS: enabled | - CONFIG_UTS_NS: enabled | - CONFIG_CGROUPS: enabled | - CONFIG_CGROUP_CPUACCT: enabled | - CONFIG_CGROUP_DEVICE: enabled | - CONFIG_CGROUP_FREEZER: enabled | - CONFIG_CGROUP_SCHED: enabled | ... Signed-off-by: Zhao Lei --- script/check-config.sh | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/script/check-config.sh b/script/check-config.sh index 5d06cb35..66c172ea 100755 --- a/script/check-config.sh +++ b/script/check-config.sh @@ -10,7 +10,12 @@ possibleConfigs=( "/usr/src/linux-$(uname -r)/.config" '/usr/src/linux/.config' ) - +possibleConfigFiles=( + 'config.gz' + "config-$(uname -r)" + '.config' +) + if [ $# -gt 0 ]; then CONFIG="$1" else @@ -110,15 +115,23 @@ check_distro_userns() { fi } -if [ ! -e "$CONFIG" ]; then - wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..." - for tryConfig in "${possibleConfigs[@]}"; do - if [ -e "$tryConfig" ]; then +if [ ! -f "$CONFIG" ]; then + wrap_warning "warning: $CONFIG seems not a kernel config, searching other paths for kernel config ..." + for tryConfig in "$CONFIG" "${possibleConfigs[@]}"; do + [[ -d "$tryConfig" ]] && { + for tryFile in "${possibleConfigFiles[@]}"; do + [[ -f "$tryConfig/$tryFile" ]] && { + tryConfig+="/$tryFile" + break + } + done + } + [[ -f "$tryConfig" ]] && { CONFIG="$tryConfig" break - fi + } done - if [ ! -e "$CONFIG" ]; then + if [ ! -f "$CONFIG" ]; then wrap_warning "error: cannot find kernel config" wrap_warning " try running this script again, specifying the kernel config:" wrap_warning " CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config" From a9ec7c631cc19ff5c83237281e6d1909b385c930 Mon Sep 17 00:00:00 2001 From: Zhao Lei Date: Tue, 6 Sep 2016 16:24:11 +0800 Subject: [PATCH 2/2] [check-config] No warning in blank argument If user run current script whthout argument, the script will search config in default dir list, but output following message: | # script/check-config.sh | warning: /proc/config.gz seems not a kernel config, searching other paths for kernel config ... ^^^^^^^^^^^^^^^ | info: reading kernel config from /boot/config-4.7.0_HEAD_523d939ef98fd712632d93a5a2b588e477a7565e_ ... | ... We can make output better by restruct the code struct: 1: Specify nothing Show info, and search default dir 2: Specify a config file Use it directly 3: Specify a wrong config file Show warning, and search default dir 4: Specify a dir Info, and search specified dir Test: | # script/check-config.sh | info: no config specified, searching for kernel config ... | info: reading kernel config from /boot/config-4.7.0_HEAD_523d939ef98fd712632d93a5a2b588e477a7565e_ ... | | # script/check-config.sh /linux/.config | info: reading kernel config from /linux/.config ... | | # script/check-config.sh /linux/.configgg | warning: /linux/.configgg seems not a kernel config, searching other paths for kernel config ... | info: reading kernel config from /boot/config-4.7.0_HEAD_523d939ef98fd712632d93a5a2b588e477a7565e_ ... | | # script/check-config.sh /linux | info: input is a directory, searching for kernel config in this directory... | info: reading kernel config from /linux/.config ... | Signed-off-by: Zhao Lei --- script/check-config.sh | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/script/check-config.sh b/script/check-config.sh index 66c172ea..d53991ed 100755 --- a/script/check-config.sh +++ b/script/check-config.sh @@ -16,12 +16,6 @@ possibleConfigFiles=( '.config' ) -if [ $# -gt 0 ]; then - CONFIG="$1" -else - : ${CONFIG:="${possibleConfigs[0]}"} -fi - if ! command -v zgrep &> /dev/null; then zgrep() { zcat "$2" | grep "$1" @@ -115,29 +109,56 @@ check_distro_userns() { fi } -if [ ! -f "$CONFIG" ]; then - wrap_warning "warning: $CONFIG seems not a kernel config, searching other paths for kernel config ..." - for tryConfig in "$CONFIG" "${possibleConfigs[@]}"; do +is_config() +{ + local config="$1" + + # Todo: more check + [[ -f "$config" ]] && return 0 + return 1 +} + +search_config() +{ + local target_dir="$1" + [[ "$target_dir" ]] || target_dir=("${possibleConfigs[@]}") + + local tryConfig + for tryConfig in "${target_dir[@]}"; do + is_config "$tryConfig" && { + CONFIG="$tryConfig" + return + } [[ -d "$tryConfig" ]] && { for tryFile in "${possibleConfigFiles[@]}"; do - [[ -f "$tryConfig/$tryFile" ]] && { - tryConfig+="/$tryFile" - break + is_config "$tryConfig/$tryFile" && { + CONFIG="$tryConfig/$tryFile" + return } done } - [[ -f "$tryConfig" ]] && { - CONFIG="$tryConfig" - break - } done - if [ ! -f "$CONFIG" ]; then - wrap_warning "error: cannot find kernel config" - wrap_warning " try running this script again, specifying the kernel config:" - wrap_warning " CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config" - exit 1 + + wrap_warning "error: cannot find kernel config" + wrap_warning " try running this script again, specifying the kernel config:" + wrap_warning " CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config" + exit 1 +} + +CONFIG="$1" + +is_config "$CONFIG" || { + if [[ ! "$CONFIG" ]]; then + wrap_color "info: no config specified, searching for kernel config ..." white + search_config + elif [[ -d "$CONFIG" ]]; then + wrap_color "info: input is a directory, searching for kernel config in this directory..." white + search_config "$CONFIG" + else + wrap_warning "warning: $CONFIG seems not a kernel config, searching other paths for kernel config ..." + search_config fi -fi +} wrap_color "info: reading kernel config from $CONFIG ..." white echo