mkimage-yum.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env bash
  2. #
  3. # Create a base CentOS Docker image.
  4. #
  5. # This script is useful on systems with yum installed (e.g., building
  6. # a CentOS image on CentOS). See contrib/mkimage-rinse.sh for a way
  7. # to build CentOS images on other systems.
  8. usage() {
  9. cat <<EOOPTS
  10. $(basename $0) [OPTIONS] <name>
  11. OPTIONS:
  12. -p "<packages>" The list of packages to install in the container.
  13. The default is blank.
  14. -g "<groups>" The groups of packages to install in the container.
  15. The default is "Core".
  16. -y <yumconf> The path to the yum config to install packages from. The
  17. default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
  18. EOOPTS
  19. exit 1
  20. }
  21. # option defaults
  22. yum_config=/etc/yum.conf
  23. if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
  24. yum_config=/etc/dnf/dnf.conf
  25. alias yum=dnf
  26. fi
  27. install_groups="Core"
  28. while getopts ":y:p:g:h" opt; do
  29. case $opt in
  30. y)
  31. yum_config=$OPTARG
  32. ;;
  33. h)
  34. usage
  35. ;;
  36. p)
  37. install_packages="$OPTARG"
  38. ;;
  39. g)
  40. install_groups="$OPTARG"
  41. ;;
  42. \?)
  43. echo "Invalid option: -$OPTARG"
  44. usage
  45. ;;
  46. esac
  47. done
  48. shift $((OPTIND - 1))
  49. name=$1
  50. if [[ -z $name ]]; then
  51. usage
  52. fi
  53. target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
  54. set -x
  55. mkdir -m 755 "$target"/dev
  56. mknod -m 600 "$target"/dev/console c 5 1
  57. mknod -m 600 "$target"/dev/initctl p
  58. mknod -m 666 "$target"/dev/full c 1 7
  59. mknod -m 666 "$target"/dev/null c 1 3
  60. mknod -m 666 "$target"/dev/ptmx c 5 2
  61. mknod -m 666 "$target"/dev/random c 1 8
  62. mknod -m 666 "$target"/dev/tty c 5 0
  63. mknod -m 666 "$target"/dev/tty0 c 4 0
  64. mknod -m 666 "$target"/dev/urandom c 1 9
  65. mknod -m 666 "$target"/dev/zero c 1 5
  66. # amazon linux yum will fail without vars set
  67. if [ -d /etc/yum/vars ]; then
  68. mkdir -p -m 755 "$target"/etc/yum
  69. cp -a /etc/yum/vars "$target"/etc/yum/
  70. fi
  71. if [[ -n "$install_groups" ]];
  72. then
  73. yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
  74. --setopt=group_package_types=mandatory -y groupinstall $install_groups
  75. fi
  76. if [[ -n "$install_packages" ]];
  77. then
  78. yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
  79. --setopt=group_package_types=mandatory -y install $install_packages
  80. fi
  81. yum -c "$yum_config" --installroot="$target" -y clean all
  82. cat > "$target"/etc/sysconfig/network <<EOF
  83. NETWORKING=yes
  84. HOSTNAME=localhost.localdomain
  85. EOF
  86. # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
  87. # locales
  88. rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
  89. # docs and man pages
  90. rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
  91. # cracklib
  92. rm -rf "$target"/usr/share/cracklib
  93. # i18n
  94. rm -rf "$target"/usr/share/i18n
  95. # yum cache
  96. rm -rf "$target"/var/cache/yum
  97. mkdir -p --mode=0755 "$target"/var/cache/yum
  98. # sln
  99. rm -rf "$target"/sbin/sln
  100. # ldconfig
  101. rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
  102. mkdir -p --mode=0755 "$target"/var/cache/ldconfig
  103. version=
  104. for file in "$target"/etc/{redhat,system}-release
  105. do
  106. if [ -r "$file" ]; then
  107. version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
  108. break
  109. fi
  110. done
  111. if [ -z "$version" ]; then
  112. echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
  113. version=$name
  114. fi
  115. tar --numeric-owner -c -C "$target" . | docker import - $name:$version
  116. docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
  117. rm -rf "$target"