mkimage.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. set -e
  3. mkimg="$(basename "$0")"
  4. usage() {
  5. echo >&2 "usage: $mkimg [-d dir] [-t tag] [--compression algo| --no-compression] script [script-args]"
  6. echo >&2 " ie: $mkimg -t someuser/debian debootstrap --variant=minbase jessie"
  7. echo >&2 " $mkimg -t someuser/ubuntu debootstrap --include=ubuntu-minimal --components=main,universe trusty"
  8. echo >&2 " $mkimg -t someuser/busybox busybox-static"
  9. echo >&2 " $mkimg -t someuser/centos:5 rinse --distribution centos-5"
  10. echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4"
  11. echo >&2 " $mkimg -t someuser/mageia:4 mageia-urpmi --version=4 --mirror=http://somemirror/"
  12. echo >&2 " $mkimg -t someuser/solaris solaris"
  13. exit 1
  14. }
  15. scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")/mkimage"
  16. os=
  17. os=$(uname -o)
  18. # set up path to gnu tools if solaris
  19. [[ $os == "Solaris" ]] && export PATH=/usr/gnu/bin:$PATH
  20. # TODO check for gnu-tar, gnu-getopt
  21. # TODO requires root/sudo due to some pkg operations. sigh.
  22. [[ $os == "Solaris" && $EUID != "0" ]] && echo >&2 "image create on Solaris requires superuser privilege"
  23. optTemp=$(getopt --options '+d:t:c:hC' --longoptions 'dir:,tag:,compression:,no-compression,help' --name "$mkimg" -- "$@")
  24. eval set -- "$optTemp"
  25. unset optTemp
  26. dir=
  27. tag=
  28. compression="auto"
  29. while true; do
  30. case "$1" in
  31. -d|--dir) dir="$2" ; shift 2 ;;
  32. -t|--tag) tag="$2" ; shift 2 ;;
  33. --compression) compression="$2" ; shift 2 ;;
  34. --no-compression) compression="none" ; shift 1 ;;
  35. -h|--help) usage ;;
  36. --) shift ; break ;;
  37. esac
  38. done
  39. script="$1"
  40. [ "$script" ] || usage
  41. shift
  42. if [ "$compression" == 'auto' ] || [ -z "$compression" ]
  43. then
  44. compression='xz'
  45. fi
  46. [ "$compression" == 'none' ] && compression=''
  47. if [ ! -x "$scriptDir/$script" ]; then
  48. echo >&2 "error: $script does not exist or is not executable"
  49. echo >&2 " see $scriptDir for possible scripts"
  50. exit 1
  51. fi
  52. # don't mistake common scripts like .febootstrap-minimize as image-creators
  53. if [[ "$script" == .* ]]; then
  54. echo >&2 "error: $script is a script helper, not a script"
  55. echo >&2 " see $scriptDir for possible scripts"
  56. exit 1
  57. fi
  58. delDir=
  59. if [ -z "$dir" ]; then
  60. dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)"
  61. delDir=1
  62. fi
  63. rootfsDir="$dir/rootfs"
  64. ( set -x; mkdir -p "$rootfsDir" )
  65. # pass all remaining arguments to $script
  66. "$scriptDir/$script" "$rootfsDir" "$@"
  67. # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them
  68. rm -rf "$rootfsDir/dev" "$rootfsDir/proc"
  69. mkdir -p "$rootfsDir/dev" "$rootfsDir/proc"
  70. # make sure /etc/resolv.conf has something useful in it
  71. mkdir -p "$rootfsDir/etc"
  72. cat > "$rootfsDir/etc/resolv.conf" <<'EOF'
  73. nameserver 8.8.8.8
  74. nameserver 8.8.4.4
  75. EOF
  76. tarFile="$dir/rootfs.tar${compression:+.$compression}"
  77. touch "$tarFile"
  78. (
  79. set -x
  80. tar --numeric-owner --create --auto-compress --file "$tarFile" --directory "$rootfsDir" --transform='s,^./,,' .
  81. )
  82. echo >&2 "+ cat > '$dir/Dockerfile'"
  83. cat > "$dir/Dockerfile" <<EOF
  84. FROM scratch
  85. ADD $(basename "$tarFile") /
  86. EOF
  87. # if our generated image has a decent shell, let's set a default command
  88. for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do
  89. if [ -x "$rootfsDir/$shell" ]; then
  90. ( set -x; echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile" )
  91. break
  92. fi
  93. done
  94. ( set -x; rm -rf "$rootfsDir" )
  95. if [ "$tag" ]; then
  96. ( set -x; docker build -t "$tag" "$dir" )
  97. elif [ "$delDir" ]; then
  98. # if we didn't specify a tag and we're going to delete our dir, let's just build an untagged image so that we did _something_
  99. ( set -x; docker build "$dir" )
  100. fi
  101. if [ "$delDir" ]; then
  102. ( set -x; rm -rf "$dir" )
  103. fi