github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/contrib/mkimage.sh (about)

     1  #!/usr/bin/env bash
     2  set -e
     3  
     4  mkimg="$(basename "$0")"
     5  
     6  usage() {
     7  	echo >&2 "usage: $mkimg [-d dir] [-t tag] [--compression algo| --no-compression] script [script-args]"
     8  	echo >&2 "   ie: $mkimg -t someuser/debian debootstrap --variant=minbase jessie"
     9  	echo >&2 "       $mkimg -t someuser/ubuntu debootstrap --include=ubuntu-minimal --components=main,universe trusty"
    10  	echo >&2 "       $mkimg -t someuser/busybox busybox-static"
    11  	echo >&2 "       $mkimg -t someuser/centos:5 rinse --distribution centos-5"
    12  	echo >&2 "       $mkimg -t someuser/mageia:4 mageia-urpmi --version=4"
    13  	echo >&2 "       $mkimg -t someuser/mageia:4 mageia-urpmi --version=4 --mirror=http://somemirror/"
    14  	exit 1
    15  }
    16  
    17  scriptDir="$(dirname "$(readlink -f "$BASH_SOURCE")")/mkimage"
    18  
    19  os=
    20  os=$(uname -o)
    21  
    22  optTemp=$(getopt --options '+d:t:c:hC' --longoptions 'dir:,tag:,compression:,no-compression,help' --name "$mkimg" -- "$@")
    23  eval set -- "$optTemp"
    24  unset optTemp
    25  
    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  
    40  script="$1"
    41  [ "$script" ] || usage
    42  shift
    43  
    44  if [ "$compression" == 'auto' ] || [ -z "$compression" ]
    45  then
    46      compression='xz'
    47  fi
    48  
    49  [ "$compression" == 'none' ] && compression=''
    50  
    51  if [ ! -x "$scriptDir/$script" ]; then
    52  	echo >&2 "error: $script does not exist or is not executable"
    53  	echo >&2 "  see $scriptDir for possible scripts"
    54  	exit 1
    55  fi
    56  
    57  # don't mistake common scripts like .febootstrap-minimize as image-creators
    58  if [[ "$script" == .* ]]; then
    59  	echo >&2 "error: $script is a script helper, not a script"
    60  	echo >&2 "  see $scriptDir for possible scripts"
    61  	exit 1
    62  fi
    63  
    64  delDir=
    65  if [ -z "$dir" ]; then
    66  	dir="$(mktemp -d ${TMPDIR:-/var/tmp}/docker-mkimage.XXXXXXXXXX)"
    67  	delDir=1
    68  fi
    69  
    70  rootfsDir="$dir/rootfs"
    71  ( set -x; mkdir -p "$rootfsDir" )
    72  
    73  # pass all remaining arguments to $script
    74  "$scriptDir/$script" "$rootfsDir" "$@"
    75  
    76  # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them
    77  rm -rf "$rootfsDir/dev" "$rootfsDir/proc"
    78  mkdir -p "$rootfsDir/dev" "$rootfsDir/proc"
    79  
    80  # make sure /etc/resolv.conf has something useful in it
    81  mkdir -p "$rootfsDir/etc"
    82  cat > "$rootfsDir/etc/resolv.conf" <<'EOF'
    83  nameserver 8.8.8.8
    84  nameserver 8.8.4.4
    85  EOF
    86  
    87  tarFile="$dir/rootfs.tar${compression:+.$compression}"
    88  touch "$tarFile"
    89  
    90  (
    91  	set -x
    92  	tar --numeric-owner --create --auto-compress --file "$tarFile" --directory "$rootfsDir" --transform='s,^./,,' .
    93  )
    94  
    95  echo >&2 "+ cat > '$dir/Dockerfile'"
    96  cat > "$dir/Dockerfile" <<EOF
    97  FROM scratch
    98  ADD $(basename "$tarFile") /
    99  EOF
   100  
   101  # if our generated image has a decent shell, let's set a default command
   102  for shell in /bin/bash /usr/bin/fish /usr/bin/zsh /bin/sh; do
   103  	if [ -x "$rootfsDir/$shell" ]; then
   104  		( set -x; echo 'CMD ["'"$shell"'"]' >> "$dir/Dockerfile" )
   105  		break
   106  	fi
   107  done
   108  
   109  ( set -x; rm -rf "$rootfsDir" )
   110  
   111  if [ "$tag" ]; then
   112  	( set -x; docker build -t "$tag" "$dir" )
   113  elif [ "$delDir" ]; then
   114  	# 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_
   115  	( set -x; docker build "$dir" )
   116  fi
   117  
   118  if [ "$delDir" ]; then
   119  	( set -x; rm -rf "$dir" )
   120  fi