mkimage-yum.sh 3.8 KB

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