check-config.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
  35. declare -A colors=(
  36. [black]=30
  37. [red]=31
  38. [green]=32
  39. [yellow]=33
  40. [blue]=34
  41. [magenta]=35
  42. [cyan]=36
  43. [white]=37
  44. )
  45. color() {
  46. color=()
  47. if [ "$1" = 'bold' ]; then
  48. color+=( '1' )
  49. shift
  50. fi
  51. if [ $# -gt 0 ] && [ "${colors[$1]}" ]; then
  52. color+=( "${colors[$1]}" )
  53. fi
  54. local IFS=';'
  55. echo -en '\033['"${color[*]}"m
  56. }
  57. wrap_color() {
  58. text="$1"
  59. shift
  60. color "$@"
  61. echo -n "$text"
  62. color reset
  63. echo
  64. }
  65. wrap_good() {
  66. echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
  67. }
  68. wrap_bad() {
  69. echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
  70. }
  71. wrap_warning() {
  72. wrap_color >&2 "$*" red
  73. }
  74. check_flag() {
  75. if is_set_in_kernel "$1"; then
  76. wrap_good "CONFIG_$1" 'enabled'
  77. elif is_set_as_module "$1"; then
  78. wrap_good "CONFIG_$1" 'enabled (as module)'
  79. else
  80. wrap_bad "CONFIG_$1" 'missing'
  81. fi
  82. }
  83. check_flags() {
  84. for flag in "$@"; do
  85. echo "- $(check_flag "$flag")"
  86. done
  87. }
  88. check_command() {
  89. if command -v "$1" >/dev/null 2>&1; then
  90. wrap_good "$1 command" 'available'
  91. else
  92. wrap_bad "$1 command" 'missing'
  93. fi
  94. }
  95. check_device() {
  96. if [ -c "$1" ]; then
  97. wrap_good "$1" 'present'
  98. else
  99. wrap_bad "$1" 'missing'
  100. fi
  101. }
  102. if [ ! -e "$CONFIG" ]; then
  103. wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config ..."
  104. for tryConfig in "${possibleConfigs[@]}"; do
  105. if [ -e "$tryConfig" ]; then
  106. CONFIG="$tryConfig"
  107. break
  108. fi
  109. done
  110. if [ ! -e "$CONFIG" ]; then
  111. wrap_warning "error: cannot find kernel config"
  112. wrap_warning " try running this script again, specifying the kernel config:"
  113. wrap_warning " CONFIG=/path/to/kernel/.config $0 or $0 /path/to/kernel/.config"
  114. exit 1
  115. fi
  116. fi
  117. wrap_color "info: reading kernel config from $CONFIG ..." white
  118. echo
  119. echo 'Generally Necessary:'
  120. echo -n '- '
  121. cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)"
  122. cgroupDir="$(dirname "$cgroupSubsystemDir")"
  123. 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
  124. echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
  125. else
  126. if [ "$cgroupSubsystemDir" ]; then
  127. echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
  128. else
  129. echo "$(wrap_bad 'cgroup hierarchy' 'nonexistent??')"
  130. fi
  131. echo " $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
  132. fi
  133. if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
  134. echo -n '- '
  135. if command -v apparmor_parser &> /dev/null; then
  136. echo "$(wrap_good 'apparmor' 'enabled and tools installed')"
  137. else
  138. echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')"
  139. echo -n ' '
  140. if command -v apt-get &> /dev/null; then
  141. echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')"
  142. elif command -v yum &> /dev/null; then
  143. echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')"
  144. else
  145. echo "$(wrap_color '(look for an "apparmor" package for your distribution)')"
  146. fi
  147. fi
  148. fi
  149. flags=(
  150. NAMESPACES {NET,PID,IPC,UTS}_NS
  151. DEVPTS_MULTIPLE_INSTANCES
  152. CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED CPUSETS MEMCG
  153. MACVLAN VETH BRIDGE BRIDGE_NETFILTER
  154. NF_NAT_IPV4 IP_NF_FILTER IP_NF_TARGET_MASQUERADE
  155. NETFILTER_XT_MATCH_{ADDRTYPE,CONNTRACK}
  156. NF_NAT NF_NAT_NEEDED
  157. # required for bind-mounting /dev/mqueue into containers
  158. POSIX_MQUEUE
  159. )
  160. check_flags "${flags[@]}"
  161. echo
  162. echo 'Optional Features:'
  163. {
  164. check_flags USER_NS
  165. }
  166. {
  167. check_flags MEMCG_KMEM MEMCG_SWAP MEMCG_SWAP_ENABLED
  168. if is_set MEMCG_SWAP && ! is_set MEMCG_SWAP_ENABLED; then
  169. 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)"
  170. fi
  171. }
  172. if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 18 ]; then
  173. check_flags RESOURCE_COUNTERS
  174. fi
  175. if [ "$kernelMajor" -lt 3 ] || [ "$kernelMajor" -eq 3 -a "$kernelMinor" -le 13 ]; then
  176. netprio=NETPRIO_CGROUP
  177. else
  178. netprio=CGROUP_NET_PRIO
  179. fi
  180. flags=(
  181. BLK_CGROUP IOSCHED_CFQ BLK_DEV_THROTTLING
  182. CGROUP_PERF
  183. CGROUP_HUGETLB
  184. NET_CLS_CGROUP $netprio
  185. CFS_BANDWIDTH FAIR_GROUP_SCHED RT_GROUP_SCHED
  186. )
  187. check_flags "${flags[@]}"
  188. check_flags EXT3_FS EXT3_FS_XATTR EXT3_FS_POSIX_ACL EXT3_FS_SECURITY
  189. if ! is_set EXT3_FS || ! is_set EXT3_FS_XATTR || ! is_set EXT3_FS_POSIX_ACL || ! is_set EXT3_FS_SECURITY; then
  190. echo " $(wrap_color '(enable these ext3 configs if you are using ext3 as backing filesystem)' bold black)"
  191. fi
  192. check_flags EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY
  193. if ! is_set EXT4_FS || ! is_set EXT4_FS_POSIX_ACL || ! is_set EXT4_FS_SECURITY; then
  194. echo " $(wrap_color 'enable these ext4 configs if you are using ext4 as backing filesystem' bold black)"
  195. fi
  196. echo '- Storage Drivers:'
  197. {
  198. echo '- "'$(wrap_color 'aufs' blue)'":'
  199. check_flags AUFS_FS | sed 's/^/ /'
  200. if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then
  201. echo " $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)"
  202. fi
  203. echo '- "'$(wrap_color 'btrfs' blue)'":'
  204. check_flags BTRFS_FS | sed 's/^/ /'
  205. echo '- "'$(wrap_color 'devicemapper' blue)'":'
  206. check_flags BLK_DEV_DM DM_THIN_PROVISIONING | sed 's/^/ /'
  207. echo '- "'$(wrap_color 'overlay' blue)'":'
  208. check_flags OVERLAY_FS | sed 's/^/ /'
  209. echo '- "'$(wrap_color 'zfs' blue)'":'
  210. echo " - $(check_device /dev/zfs)"
  211. echo " - $(check_command zfs)"
  212. echo " - $(check_command zpool)"
  213. } | sed 's/^/ /'
  214. echo