check-config.sh 7.7 KB

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