mkimage-yum.sh 3.6 KB

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