github.com/rish1988/moby@v25.0.2+incompatible/contrib/check-config.sh (about)

     1  #!/usr/bin/env sh
     2  set -e
     3  
     4  EXITCODE=0
     5  
     6  # bits of this were adapted from lxc-checkconfig
     7  # see also https://github.com/lxc/lxc/blob/lxc-1.0.2/src/lxc/lxc-checkconfig.in
     8  
     9  possibleConfigs="
    10  	/proc/config.gz
    11  	/boot/config-$(uname -r)
    12  	/usr/src/linux-$(uname -r)/.config
    13  	/usr/src/linux/.config
    14  "
    15  
    16  if [ $# -gt 0 ]; then
    17  	CONFIG="$1"
    18  else
    19  	: "${CONFIG:=/proc/config.gz}"
    20  fi
    21  
    22  if ! command -v zgrep > /dev/null 2>&1; then
    23  	zgrep() {
    24  		zcat "$2" | grep "$1"
    25  	}
    26  fi
    27  
    28  useColor=true
    29  if [ "$NO_COLOR" = "1" ] || [ ! -t 1 ]; then
    30  	useColor=false
    31  fi
    32  kernelVersion="$(uname -r)"
    33  kernelMajor="${kernelVersion%%.*}"
    34  kernelMinor="${kernelVersion#$kernelMajor.}"
    35  kernelMinor="${kernelMinor%%.*}"
    36  
    37  is_set() {
    38  	zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null
    39  }
    40  is_set_in_kernel() {
    41  	zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null
    42  }
    43  is_set_as_module() {
    44  	zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null
    45  }
    46  
    47  color() {
    48  	# if stdout is not a terminal, then don't do color codes.
    49  	if [ "$useColor" = "false" ]; then
    50  		return 0
    51  	fi
    52  	codes=
    53  	if [ "$1" = 'bold' ]; then
    54  		codes='1'
    55  		shift
    56  	fi
    57  	if [ "$#" -gt 0 ]; then
    58  		code=
    59  		case "$1" in
    60  			# see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
    61  			black) code=30 ;;
    62  			red) code=31 ;;
    63  			green) code=32 ;;
    64  			yellow) code=33 ;;
    65  			blue) code=34 ;;
    66  			magenta) code=35 ;;
    67  			cyan) code=36 ;;
    68  			white) code=37 ;;
    69  		esac
    70  		if [ "$code" ]; then
    71  			codes="${codes:+$codes;}$code"
    72  		fi
    73  	fi
    74  	printf '\033[%sm' "$codes"
    75  }
    76  wrap_color() {
    77  	text="$1"
    78  	shift
    79  	color "$@"
    80  	printf '%s' "$text"
    81  	color reset
    82  	echo
    83  }
    84  
    85  wrap_good() {
    86  	echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
    87  }
    88  wrap_bad() {
    89  	echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
    90  }
    91  wrap_warning() {
    92  	wrap_color >&2 "$*" red
    93  }
    94  
    95  check_flag() {
    96  	if is_set_in_kernel "$1"; then
    97  		wrap_good "CONFIG_$1" 'enabled'
    98  	elif is_set_as_module "$1"; then
    99  		wrap_good "CONFIG_$1" 'enabled (as module)'
   100  	else
   101  		wrap_bad "CONFIG_$1" 'missing'
   102  		EXITCODE=1
   103  	fi
   104  }
   105  
   106  check_flags() {
   107  	for flag in "$@"; do
   108  		printf -- '- '
   109  		check_flag "$flag"
   110  	done
   111  }
   112  
   113  check_command() {
   114  	if command -v "$1" > /dev/null 2>&1; then
   115  		wrap_good "$1 command" 'available'
   116  	else
   117  		wrap_bad "$1 command" 'missing'
   118  		EXITCODE=1
   119  	fi
   120  }
   121  
   122  check_device() {
   123  	if [ -c "$1" ]; then
   124  		wrap_good "$1" 'present'
   125  	else
   126  		wrap_bad "$1" 'missing'
   127  		EXITCODE=1
   128  	fi
   129  }
   130  
   131  check_distro_userns() {
   132  	if [ ! -e /etc/os-release ]; then
   133  		return
   134  	fi
   135  	. /etc/os-release 2> /dev/null || /bin/true
   136  	case "$ID" in
   137  		centos | rhel)
   138  			case "$VERSION_ID" in
   139  				7*)
   140  					# this is a CentOS7 or RHEL7 system
   141  					grep -q 'user_namespace.enable=1' /proc/cmdline || {
   142  						# no user namespace support enabled
   143  						wrap_bad "  (RHEL7/CentOS7" "User namespaces disabled; add 'user_namespace.enable=1' to boot command line)"
   144  						EXITCODE=1
   145  					}
   146  					;;
   147  			esac
   148  			;;
   149  	esac
   150  }
   151  
   152  if [ ! -e "$CONFIG" ]; then
   153  	wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..."
   154  	for tryConfig in $possibleConfigs; do
   155  		if [ -e "$tryConfig" ]; then
   156  			CONFIG="$tryConfig"
   157  			break
   158  		fi
   159  	done
   160  	if [ ! -e "$CONFIG" ]; then
   161  		wrap_warning "error: cannot find kernel config"
   162  		wrap_warning "  try running this script again, specifying the kernel config:"
   163  		wrap_warning "    CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config"
   164  		exit 1
   165  	fi
   166  fi
   167  
   168  wrap_color "info: reading kernel config from $CONFIG ..." white
   169  echo
   170  
   171  echo 'Generally Necessary:'
   172  
   173  printf -- '- '
   174  if [ "$(stat -f -c %t /sys/fs/cgroup 2> /dev/null)" = '63677270' ]; then
   175  	wrap_good 'cgroup hierarchy' 'cgroupv2'
   176  	cgroupv2ControllerFile='/sys/fs/cgroup/cgroup.controllers'
   177  	if [ -f "$cgroupv2ControllerFile" ]; then
   178  		echo '  Controllers:'
   179  		for controller in cpu cpuset io memory pids; do
   180  			if grep -qE '(^| )'"$controller"'($| )' "$cgroupv2ControllerFile"; then
   181  				echo "  - $(wrap_good "$controller" 'available')"
   182  			else
   183  				echo "  - $(wrap_bad "$controller" 'missing')"
   184  			fi
   185  		done
   186  	else
   187  		wrap_bad "$cgroupv2ControllerFile" 'nonexistent??'
   188  	fi
   189  	# TODO find an efficient way to check if cgroup.freeze exists in subdir
   190  else
   191  	cgroupSubsystemDir="$(sed -rne '/^[^ ]+ ([^ ]+) cgroup ([^ ]*,)?(cpu|cpuacct|cpuset|devices|freezer|memory)[, ].*$/ { s//\1/p; q }' /proc/mounts)"
   192  	cgroupDir="$(dirname "$cgroupSubsystemDir")"
   193  	if [ -d "$cgroupDir/cpu" ] || [ -d "$cgroupDir/cpuacct" ] || [ -d "$cgroupDir/cpuset" ] || [ -d "$cgroupDir/devices" ] || [ -d "$cgroupDir/freezer" ] || [ -d "$cgroupDir/memory" ]; then
   194  		echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
   195  	else
   196  		if [ "$cgroupSubsystemDir" ]; then
   197  			echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
   198  		else
   199  			wrap_bad 'cgroup hierarchy' 'nonexistent??'
   200  		fi
   201  		EXITCODE=1
   202  		echo "    $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
   203  	fi
   204  fi
   205  
   206  if [ "$(cat /sys/module/apparmor/parameters/enabled 2> /dev/null)" = 'Y' ]; then
   207  	printf -- '- '
   208  	if command -v apparmor_parser > /dev/null 2>&1; then
   209  		wrap_good 'apparmor' 'enabled and tools installed'
   210  	else
   211  		wrap_bad 'apparmor' 'enabled, but apparmor_parser missing'
   212  		printf '    '
   213  		if command -v apt-get > /dev/null 2>&1; then
   214  			wrap_color '(use "apt-get install apparmor" to fix this)'
   215  		elif command -v yum > /dev/null 2>&1; then
   216  			wrap_color '(your best bet is "yum install apparmor-parser")'
   217  		else
   218  			wrap_color '(look for an "apparmor" package for your distribution)'
   219  		fi
   220  		EXITCODE=1
   221  	fi
   222  fi
   223  
   224  check_flags \
   225  	NAMESPACES NET_NS PID_NS IPC_NS UTS_NS \
   226  	CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG \
   227  	KEYS \
   228  	VETH BRIDGE BRIDGE_NETFILTER \
   229  	IP_NF_FILTER IP_NF_MANGLE IP_NF_TARGET_MASQUERADE \
   230  	NETFILTER_XT_MATCH_ADDRTYPE \
   231  	NETFILTER_XT_MATCH_CONNTRACK \
   232  	NETFILTER_XT_MATCH_IPVS \
   233  	NETFILTER_XT_MARK \
   234  	IP_NF_NAT NF_NAT \
   235  	POSIX_MQUEUE
   236  # (POSIX_MQUEUE is required for bind-mounting /dev/mqueue into containers)
   237  
   238  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -lt 8 ]); then
   239  	check_flags DEVPTS_MULTIPLE_INSTANCES
   240  fi
   241  
   242  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 1 ]; then
   243  	check_flags NF_NAT_IPV4
   244  fi
   245  
   246  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 2 ]; then
   247  	check_flags NF_NAT_NEEDED
   248  fi
   249  # check availability of BPF_CGROUP_DEVICE support
   250  if [ "$kernelMajor" -ge 5 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -ge 15 ]); then
   251  	check_flags CGROUP_BPF
   252  fi
   253  
   254  echo
   255  
   256  echo 'Optional Features:'
   257  {
   258  	check_flags USER_NS
   259  	check_distro_userns
   260  }
   261  {
   262  	check_flags SECCOMP
   263  	check_flags SECCOMP_FILTER
   264  }
   265  {
   266  	check_flags CGROUP_PIDS
   267  }
   268  {
   269  	check_flags MEMCG_SWAP
   270  	# Kernel v5.8+ removes MEMCG_SWAP_ENABLED.
   271  	if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 8 ]; then
   272  		CODE=${EXITCODE}
   273  		check_flags MEMCG_SWAP_ENABLED
   274  		# FIXME this check is cgroupv1-specific
   275  		if [ -e /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes ]; then
   276  			echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   277  			EXITCODE=${CODE}
   278  		elif is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
   279  			echo "    $(wrap_color '(cgroup swap accounting is currently not enabled, you can enable it by setting boot option "swapaccount=1")' bold black)"
   280  		fi
   281  	else
   282  		# Kernel v5.8+ enables swap accounting by default.
   283  		echo "    $(wrap_color '(cgroup swap accounting is currently enabled)' bold black)"
   284  	fi
   285  }
   286  {
   287  	if is_set LEGACY_VSYSCALL_NATIVE; then
   288  		printf -- '- '
   289  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NATIVE" 'enabled'
   290  		echo "    $(wrap_color '(dangerous, provides an ASLR-bypassing target with usable ROP gadgets.)' bold black)"
   291  	elif is_set LEGACY_VSYSCALL_EMULATE; then
   292  		printf -- '- '
   293  		wrap_good "CONFIG_LEGACY_VSYSCALL_EMULATE" 'enabled'
   294  	elif is_set LEGACY_VSYSCALL_NONE; then
   295  		printf -- '- '
   296  		wrap_bad "CONFIG_LEGACY_VSYSCALL_NONE" 'enabled'
   297  		echo "    $(wrap_color '(containers using eglibc <= 2.13 will not work. Switch to' bold black)"
   298  		echo "    $(wrap_color ' "CONFIG_VSYSCALL_[NATIVE|EMULATE]" or use "vsyscall=[native|emulate]"' bold black)"
   299  		echo "    $(wrap_color ' on kernel command line. Note that this will disable ASLR for the,' bold black)"
   300  		echo "    $(wrap_color ' VDSO which may assist in exploiting security vulnerabilities.)' bold black)"
   301  	# else Older kernels (prior to 3dc33bd30f3e, released in v4.40-rc1) do
   302  	#      not have these LEGACY_VSYSCALL options and are effectively
   303  	#      LEGACY_VSYSCALL_EMULATE. Even older kernels are presumably
   304  	#      effectively LEGACY_VSYSCALL_NATIVE.
   305  	fi
   306  }
   307  
   308  if [ "$kernelMajor" -lt 4 ] || ([ "$kernelMajor" -eq 4 ] && [ "$kernelMinor" -le 5 ]); then
   309  	check_flags MEMCG_KMEM
   310  fi
   311  
   312  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 18 ]); then
   313  	check_flags RESOURCE_COUNTERS
   314  fi
   315  
   316  if [ "$kernelMajor" -lt 3 ] || ([ "$kernelMajor" -eq 3 ] && [ "$kernelMinor" -le 13 ]); then
   317  	netprio=NETPRIO_CGROUP
   318  else
   319  	netprio=CGROUP_NET_PRIO
   320  fi
   321  
   322  if [ "$kernelMajor" -lt 5 ]; then
   323  	check_flags IOSCHED_CFQ CFQ_GROUP_IOSCHED
   324  fi
   325  
   326  check_flags \
   327  	BLK_CGROUP BLK_DEV_THROTTLING \
   328  	CGROUP_PERF \
   329  	CGROUP_HUGETLB \
   330  	NET_CLS_CGROUP $netprio \
   331  	CFS_BANDWIDTH FAIR_GROUP_SCHED \
   332  	IP_NF_TARGET_REDIRECT \
   333  	IP_VS \
   334  	IP_VS_NFCT \
   335  	IP_VS_PROTO_TCP \
   336  	IP_VS_PROTO_UDP \
   337  	IP_VS_RR \
   338  	SECURITY_SELINUX \
   339  	SECURITY_APPARMOR
   340  
   341  if ! is_set EXT4_USE_FOR_EXT2; then
   342  	check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
   343  	if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
   344  		echo "    $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
   345  	fi
   346  fi
   347  
   348  check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
   349  if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
   350  	if is_set EXT4_USE_FOR_EXT2; then
   351  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext3 or ext4 as backing filesystem' bold black)"
   352  	else
   353  		echo "    $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
   354  	fi
   355  fi
   356  
   357  echo '- Network Drivers:'
   358  echo "  - \"$(wrap_color 'overlay' blue)\":"
   359  check_flags VXLAN BRIDGE_VLAN_FILTERING | sed 's/^/    /'
   360  echo '      Optional (for encrypted networks):'
   361  check_flags CRYPTO CRYPTO_AEAD CRYPTO_GCM CRYPTO_SEQIV CRYPTO_GHASH \
   362  	XFRM XFRM_USER XFRM_ALGO INET_ESP NETFILTER_XT_MATCH_BPF | sed 's/^/      /'
   363  if [ "$kernelMajor" -lt 5 ] || [ "$kernelMajor" -eq 5 -a "$kernelMinor" -le 3 ]; then
   364  	check_flags INET_XFRM_MODE_TRANSPORT | sed 's/^/      /'
   365  fi
   366  echo "  - \"$(wrap_color 'ipvlan' blue)\":"
   367  check_flags IPVLAN | sed 's/^/    /'
   368  echo "  - \"$(wrap_color 'macvlan' blue)\":"
   369  check_flags MACVLAN DUMMY | sed 's/^/    /'
   370  echo "  - \"$(wrap_color 'ftp,tftp client in container' blue)\":"
   371  check_flags NF_NAT_FTP NF_CONNTRACK_FTP NF_NAT_TFTP NF_CONNTRACK_TFTP | sed 's/^/    /'
   372  
   373  # only fail if no storage drivers available
   374  CODE=${EXITCODE}
   375  EXITCODE=0
   376  STORAGE=1
   377  
   378  echo '- Storage Drivers:'
   379  echo "  - \"$(wrap_color 'btrfs' blue)\":"
   380  check_flags BTRFS_FS | sed 's/^/    /'
   381  check_flags BTRFS_FS_POSIX_ACL | sed 's/^/    /'
   382  [ "$EXITCODE" = 0 ] && STORAGE=0
   383  EXITCODE=0
   384  
   385  echo "  - \"$(wrap_color 'overlay' blue)\":"
   386  check_flags OVERLAY_FS | sed 's/^/    /'
   387  [ "$EXITCODE" = 0 ] && STORAGE=0
   388  EXITCODE=0
   389  
   390  echo "  - \"$(wrap_color 'zfs' blue)\":"
   391  printf '    - '
   392  check_device /dev/zfs
   393  printf '    - '
   394  check_command zfs
   395  printf '    - '
   396  check_command zpool
   397  [ "$EXITCODE" = 0 ] && STORAGE=0
   398  EXITCODE=0
   399  
   400  EXITCODE=$CODE
   401  [ "$STORAGE" = 1 ] && EXITCODE=1
   402  
   403  echo
   404  
   405  check_limit_over() {
   406  	if [ "$(cat "$1")" -le "$2" ]; then
   407  		wrap_bad "- $1" "$(cat "$1")"
   408  		wrap_color "    This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black
   409  		EXITCODE=1
   410  	else
   411  		wrap_good "- $1" "$(cat "$1")"
   412  	fi
   413  }
   414  
   415  echo 'Limits:'
   416  check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000
   417  echo
   418  
   419  exit $EXITCODE