check-config.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. : ${CONFIG:="${possibleConfigs[0]}"}
  12. if ! command -v zgrep &> /dev/null; then
  13. zgrep() {
  14. zcat "$2" | grep "$1"
  15. }
  16. fi
  17. is_set() {
  18. zgrep "CONFIG_$1=[y|m]" "$CONFIG" > /dev/null
  19. }
  20. # see http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
  21. declare -A colors=(
  22. [black]=30
  23. [red]=31
  24. [green]=32
  25. [yellow]=33
  26. [blue]=34
  27. [magenta]=35
  28. [cyan]=36
  29. [white]=37
  30. )
  31. color() {
  32. color=()
  33. if [ "$1" = 'bold' ]; then
  34. color+=( '1' )
  35. shift
  36. fi
  37. if [ $# -gt 0 ] && [ "${colors[$1]}" ]; then
  38. color+=( "${colors[$1]}" )
  39. fi
  40. local IFS=';'
  41. echo -en '\033['"${color[*]}"m
  42. }
  43. wrap_color() {
  44. text="$1"
  45. shift
  46. color "$@"
  47. echo -n "$text"
  48. color reset
  49. echo
  50. }
  51. wrap_good() {
  52. echo "$(wrap_color "$1" white): $(wrap_color "$2" green)"
  53. }
  54. wrap_bad() {
  55. echo "$(wrap_color "$1" bold): $(wrap_color "$2" bold red)"
  56. }
  57. wrap_warning() {
  58. wrap_color >&2 "$*" red
  59. }
  60. check_flag() {
  61. if is_set "$1"; then
  62. wrap_good "CONFIG_$1" 'enabled'
  63. else
  64. wrap_bad "CONFIG_$1" 'missing'
  65. fi
  66. }
  67. check_flags() {
  68. for flag in "$@"; do
  69. echo "- $(check_flag "$flag")"
  70. done
  71. }
  72. if [ ! -e "$CONFIG" ]; then
  73. wrap_warning "warning: $CONFIG does not exist, searching other paths for kernel config..."
  74. for tryConfig in "${possibleConfigs[@]}"; do
  75. if [ -e "$tryConfig" ]; then
  76. CONFIG="$tryConfig"
  77. break
  78. fi
  79. done
  80. if [ ! -e "$CONFIG" ]; then
  81. wrap_warning "error: cannot find kernel config"
  82. wrap_warning " try running this script again, specifying the kernel config:"
  83. wrap_warning " CONFIG=/path/to/kernel/.config $0"
  84. exit 1
  85. fi
  86. fi
  87. wrap_color "info: reading kernel config from $CONFIG ..." white
  88. echo
  89. echo 'Generally Necessary:'
  90. echo -n '- '
  91. cgroupSubsystemDir="$(awk '/[, ](cpu|cpuacct|cpuset|devices|freezer|memory)[, ]/ && $3 == "cgroup" { print $2 }' /proc/mounts | head -n1)"
  92. cgroupDir="$(dirname "$cgroupSubsystemDir")"
  93. 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
  94. echo "$(wrap_good 'cgroup hierarchy' 'properly mounted') [$cgroupDir]"
  95. else
  96. if [ "$cgroupSubsystemDir" ]; then
  97. echo "$(wrap_bad 'cgroup hierarchy' 'single mountpoint!') [$cgroupSubsystemDir]"
  98. else
  99. echo "$(wrap_bad 'cgroup hierarchy' 'nonexistent??')"
  100. fi
  101. echo " $(wrap_color '(see https://github.com/tianon/cgroupfs-mount)' yellow)"
  102. fi
  103. if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
  104. echo -n '- '
  105. if command -v apparmor_parser &> /dev/null; then
  106. echo "$(wrap_good 'apparmor' 'enabled and tools installed')"
  107. else
  108. echo "$(wrap_bad 'apparmor' 'enabled, but apparmor_parser missing')"
  109. echo -n ' '
  110. if command -v apt-get &> /dev/null; then
  111. echo "$(wrap_color '(use "apt-get install apparmor" to fix this)')"
  112. elif command -v yum &> /dev/null; then
  113. echo "$(wrap_color '(your best bet is "yum install apparmor-parser")')"
  114. else
  115. echo "$(wrap_color '(look for an "apparmor" package for your distribution)')"
  116. fi
  117. fi
  118. fi
  119. flags=(
  120. NAMESPACES {NET,PID,IPC,UTS}_NS
  121. DEVPTS_MULTIPLE_INSTANCES
  122. CGROUPS CGROUP_CPUACCT CGROUP_DEVICE CGROUP_FREEZER CGROUP_SCHED
  123. MACVLAN VETH BRIDGE
  124. NF_NAT_IPV4 IP_NF_FILTER IP_NF_TARGET_MASQUERADE
  125. NETFILTER_XT_MATCH_{ADDRTYPE,CONNTRACK}
  126. NF_NAT NF_NAT_NEEDED
  127. # required for bind-mounting /dev/mqueue into containers
  128. POSIX_MQUEUE
  129. )
  130. check_flags "${flags[@]}"
  131. echo
  132. echo 'Optional Features:'
  133. flags=(
  134. MEMCG_SWAP
  135. RESOURCE_COUNTERS
  136. CGROUP_PERF
  137. )
  138. check_flags "${flags[@]}"
  139. echo '- Storage Drivers:'
  140. {
  141. echo '- "'$(wrap_color 'aufs' blue)'":'
  142. check_flags AUFS_FS | sed 's/^/ /'
  143. if ! is_set AUFS_FS && grep -q aufs /proc/filesystems; then
  144. echo " $(wrap_color '(note that some kernels include AUFS patches but not the AUFS_FS flag)' bold black)"
  145. fi
  146. check_flags EXT4_FS_POSIX_ACL EXT4_FS_SECURITY | sed 's/^/ /'
  147. echo '- "'$(wrap_color 'btrfs' blue)'":'
  148. check_flags BTRFS_FS | sed 's/^/ /'
  149. echo '- "'$(wrap_color 'devicemapper' blue)'":'
  150. check_flags BLK_DEV_DM DM_THIN_PROVISIONING EXT4_FS EXT4_FS_POSIX_ACL EXT4_FS_SECURITY | sed 's/^/ /'
  151. echo '- "'$(wrap_color 'overlay' blue)'":'
  152. check_flags OVERLAY_FS | sed 's/^/ /'
  153. } | sed 's/^/ /'
  154. echo
  155. #echo 'Potential Future Features:'
  156. #check_flags USER_NS
  157. #echo