github.com/portworx/docker@v1.12.1/contrib/check-config.sh (about)

     1  #!/usr/bin/env bash
     2  set -e
     3  
     4  # bits of this were adapted from lxc-checkconfig
     5  # see also https://github.com/lxc/lxc/blob/lxc-1.0.2/src/lxc/lxc-checkconfig.in
     6  
     7  possibleConfigs=(
     8  	'/proc/config.gz'
     9  	"/boot/config-$(uname -r)"
    10  	"/usr/src/linux-$(uname -r)/.config"
    11  	'/usr/src/linux/.config'
    12  )
    13  
    14  if [ $# -gt 0 ]; then
    15  	CONFIG="$1"
    16  else
    17  	: ${CONFIG:="${possibleConfigs[0]}"}
    18  fi
    19  
    20  if ! command -v zgrep &> /dev/null; then
    21  	zgrep() {
    22  		zcat "$2" | grep "$1"
    23  	}
    24  fi
    25  
    26  kernelVersion="$(uname -r)"
    27  kernelMajor="${kernelVersion%%.*}"
    28  kernelMinor="${kernelVersion#$kernelMajor.}"
    29  kernelMinor="${kernelMinor%%.*}"
    30  
    31  is_set() {
    32  	zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null
    33  }
    34  is_set_in_kernel() {
    35  	zgrep "CONFIG_$1=y" "$CONFIG" > /dev/null
    36  }
    37  is_set_as_module() {
    38  	zgrep "CONFIG_$1=m" "$CONFIG" > /dev/null
    39  }
    40  
    41  color() {
    42  	local codes=()
    43  	if [ "$1" = 'bold' ]; then
    44  		codes=( "${codes[@]}" '1' )
    45  		shift
    46  	fi
    47  	if [ "$#" -gt 0 ]; then
    48  		local code=
    49  		case "$1" in
    50  			# see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
    51  			black) code=30 ;;
    52  			red) code=31 ;;
    53  			green) code=32 ;;
    54  			yellow) code=33 ;;
    55  			blue) code=34 ;;
    56  			magenta) code=35 ;;
    57  			cyan) code=36 ;;
    58  			white) code=37 ;;
    59  		esac
    60  		if [ "$code" ]; then
    61  			codes=( "${codes[@]}" "$code" )
    62  		fi
    63  	fi
    64  	local IFS=';'
    65  	echo -en '\033['"${codes[*]}"'m'
    66  }
    67  wrap_color() {
    68  	text="$1"
    69  	shift
    70  	color "$@"
    71  	echo -n "$text"
    72  	color reset
    73  	echo
    74  }
    75  
    76  wrap_good() {
    77  	echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
    78  }
    79  wrap_bad() {
    80  	echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
    81  }
    82  wrap_warning() {
    83  	wrap_color >&2 "$*" red
    84  }
    85  
    86  check_flag() {
    87  	if is_set_in_kernel "$1"; then
    88  		wrap_good "CONFIG_$1" 'enabled'
    89  	elif is_set_as_module "$1"; then
    90  		wrap_good "CONFIG_$1" 'enabled (as module)'
    91  	else
    92  		wrap_bad "CONFIG_$1" 'missing'
    93  	fi
    94  }
    95  
    96  check_flags() {
    97  	for flag in "$@"; do
    98  		echo "- $(check_flag "$flag")"
    99  	done
   100  }
   101  
   102  check_command() {
   103  	if command -v "$1" >/dev/null 2>&1; then
   104  		wrap_good "$1 command" 'available'
   105  	else
   106  		wrap_bad "$1 command" 'missing'
   107  	fi
   108  }
   109  
   110  check_device() {
   111  	if [ -c "$1" ]; then
   112  		wrap_good "$1" 'present'
   113  	else
   114  		wrap_bad "$1" 'missing'
   115  	fi
   116  }
   117  
   118  check_distro_userns() {
   119  	source /etc/os-release 2>/dev/null || /bin/true
   120  	if [[ "${ID}" =~ ^(centos|rhel)$ && "${VERSION_ID}" =~ ^7 ]]; then
   121  		# this is a CentOS7 or RHEL7 system
   122  		grep -q "user_namespace.enable=1" /proc/cmdline || {
   123  			# no user namespace support enabled
   124  			wrap_bad "  (RHEL7/CentOS7" "User namespaces disabled; add 'user_namespace.enable=1' to boot command line)"
   125  		}
   126  	fi
   127  }
   128  
   129  if [ ! -e "$CONFIG" ]; then
   130  	wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..."
   131  	for tryConfig in "${possibleConfigs[@]}"; do
   132  		if [ -e "$tryConfig" ]; then
   133  			CONFIG="$tryConfig"
   134  			break
   135  		fi
   136  	done
   137  	if [ ! -e "$CONFIG" ]; then
   138  		wrap_warning "error: cannot find kernel config"
   139  		wrap_warning "  try running this script again, specifying the kernel config:"
   140  		wrap_warning "    CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config"
   141  		exit 1
   142  	fi
   143  fi
   144  
   145  wrap_color "info: reading kernel config from $CONFIG ..." white
   146  echo
   147  
   148  echo 'Generally Necessary:'
   149  
   150  echo -n '- '
   151  cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)"
   152  cgroupDir="$(dirname "$cgroupSubsystemDir")"
   153  if [ -d "$cgroupDir/cpu" -o -d "$cgroupDir/cpuacct" -o -d "$cgroupDir/cpuset" -o -d "$cgroupDir/devices" -o -d "$cgroupDir/freezer" -o -d "$cgroupDir/memory" ]; then
   154  	echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
   155  else
   156  	if [ "$cgroupSubsystemDir" ]; then
   157  		echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
   158  	else
   159  		echo "$(wrap_bad 'cgroup hierarchy' 'nonexistent??')"
   160  	fi
   161  	echo "    $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
   162  fi
   163  
   164  if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
   165  	echo -n '- '
   166  	if command -v apparmor_parser &> /dev/null; then
   167  		echo "$(wrap_good 'apparmor' 'enabled and tools installed')"
   168  	else
   169  		echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')"
   170  		echo -n '    '
   171  		if command -v apt-get &> /dev/null; then
   172  			echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')"
   173  		elif command -v yum &> /dev/null; then
   174  			echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')"
   175  		else
   176  			echo "$(wrap_color '(look for an "apparmor" package for your distribution)')"
   177  		fi
   178  	fi
   179  fi
   180  
   181  flags=(
   182  	NAMESPACES {NET,PID,IPC,UTS}_NS
   183  	DEVPTS_MULTIPLE_INSTANCES
   184  	CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG
   185  	KEYS
   186  	VETH BRIDGE BRIDGE_NETFILTER
   187  	NF_NAT_IPV4 IP_NF_FILTER IP_NF_TARGET_MASQUERADE
   188  	NETFILTER_XT_MATCH_{ADDRTYPE,CONNTRACK}
   189  	NF_NAT NF_NAT_NEEDED
   190  
   191  	# required for bind-mounting /dev/mqueue into containers
   192  	POSIX_MQUEUE
   193  )
   194  check_flags "${flags[@]}"
   195  echo
   196  
   197  echo 'Optional Features:'
   198  {
   199  	check_flags USER_NS
   200  	check_distro_userns
   201  }
   202  {
   203  	check_flags SECCOMP
   204  }
   205  {
   206  	check_flags CGROUP_PIDS
   207  }
   208  {
   209  	check_flags MEMCG_SWAP MEMCG_SWAP_ENABLED
   210  	if  is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
   211  		echo "    $(wrap_color '(note that cgroup swap accounting is not enabled in your kernel config, you can enable it by setting boot option "swapaccount=1")' bold black)"
   212  	fi
   213  }
   214  
   215  if [ "$kernelMajor" -lt 4 ] || [ "$kernelMajor" -eq 4 -a "$kernelMinor" -le 5 ]; then
   216  	check_flags MEMCG_KMEM
   217  fi
   218  
   219  if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 18 ]; then
   220  	check_flags RESOURCE_COUNTERS
   221  fi
   222  
   223  if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 13 ]; then
   224  	netprio=NETPRIO_CGROUP
   225  else
   226  	netprio=CGROUP_NET_PRIO
   227  fi
   228  
   229  flags=(
   230  	BLK_CGROUP BLK_DEV_THROTTLING IOSCHED_CFQ CFQ_GROUP_IOSCHED
   231  	CGROUP_PERF
   232  	CGROUP_HUGETLB
   233  	NET_CLS_CGROUP $netprio
   234  	CFS_BANDWIDTH FAIR_GROUP_SCHED RT_GROUP_SCHED
   235  	IP_VS
   236  )
   237  check_flags "${flags[@]}"
   238  
   239  check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
   240  if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
   241  	echo "    $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
   242  fi
   243  
   244  check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
   245  if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
   246  	echo "    $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
   247  fi
   248  
   249  echo '- Network Drivers:'
   250  {
   251  	echo '- "'$(wrap_color 'overlay' blue)'":'
   252  	check_flags VXLAN | sed 's/^/  /'
   253  	echo '  Optional (for secure networks):'
   254  	check_flags XFRM_ALGO XFRM_USER | sed 's/^/  /'
   255  	echo '- "'$(wrap_color 'ipvlan' blue)'":'
   256  	check_flags IPVLAN | sed 's/^/  /'
   257  	echo '- "'$(wrap_color 'macvlan' blue)'":'
   258  	check_flags MACVLAN DUMMY | sed 's/^/  /'
   259  } | sed 's/^/  /'
   260  
   261  echo '- Storage Drivers:'
   262  {
   263  	echo '- "'$(wrap_color 'aufs' blue)'":'
   264  	check_flags AUFS_FS | sed 's/^/  /'
   265  	if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then
   266  		echo "    $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)"
   267  	fi
   268  
   269  	echo '- "'$(wrap_color 'btrfs' blue)'":'
   270  	check_flags BTRFS_FS | sed 's/^/  /'
   271  
   272  	echo '- "'$(wrap_color 'devicemapper' blue)'":'
   273  	check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/  /'
   274  
   275  	echo '- "'$(wrap_color 'overlay' blue)'":'
   276  	check_flags OVERLAY_FS | sed 's/^/  /'
   277  
   278  	echo '- "'$(wrap_color 'zfs' blue)'":'
   279  	echo "  - $(check_device /dev/zfs)"
   280  	echo "  - $(check_command zfs)"
   281  	echo "  - $(check_command zpool)"
   282  } | sed 's/^/  /'
   283  echo
   284  
   285  check_limit_over()
   286  {
   287  	if [ $(cat "$1") -le "$2" ]; then
   288  		wrap_bad "- $1" "$(cat $1)"
   289  		wrap_color "    This should be set to at least $2, for example set: sysctl -w kernel/keys/root_maxkeys=1000000" bold black
   290  	else
   291  		wrap_good "- $1" "$(cat $1)"
   292  	fi
   293  }
   294  
   295  echo 'Limits:'
   296  check_limit_over /proc/sys/kernel/keys/root_maxkeys 10000
   297  echo