github.com/opencontainers/runtime-tools@v0.9.0/contrib/rootfs-builder/get-stage3.sh (about)

     1  #!/bin/sh
     2  #
     3  # Download the current Gentoo stage3
     4  #
     5  # Copyright (C) 2014-2018 W. Trevor King <wking@tremily.us>
     6  #
     7  # Licensed under the Apache License, Version 2.0 (the "License");
     8  # you may not use this file except in compliance with the License.
     9  # You may obtain a copy of the License at
    10  #
    11  #     http://www.apache.org/licenses/LICENSE-2.0
    12  #
    13  # Unless required by applicable law or agreed to in writing, software
    14  # distributed under the License is distributed on an "AS IS" BASIS,
    15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  # See the License for the specific language governing permissions and
    17  # limitations under the License.
    18  
    19  die()
    20  {
    21  	echo "$1"
    22  	exit 1
    23  }
    24  
    25  MIRROR="${MIRROR:-http://distfiles.gentoo.org/}"
    26  if test -n "${STAGE3}"
    27  then
    28  	if test -n "${STAGE3_ARCH}"
    29  	then
    30  		die 'if you set STAGE3, you do not need to set STAGE3_ARCH'
    31  	fi
    32  	if test -n "${DATE}"
    33  	then
    34  		die 'if you set STAGE3, you do not need to set DATE'
    35  	fi
    36  	STAGE3_ARCH=$(echo "${STAGE3}" | sed -n 's/stage3-\([^-]*\)-.*/\1/p')
    37  	if test -z "${STAGE3_ARCH}"
    38  	then
    39  		die "could not calculate STAGE3_ARCH from ${STAGE3}"
    40  	fi
    41  	DATE=$(echo "${STAGE3}" | sed -n "s/stage3-${STAGE3_ARCH}-\([0-9TZ]*\)[.]tar[.].*/\1/p")
    42  	if test -z "${DATE}"
    43  	then
    44  		die "could not calculate DATE from ${STAGE3}"
    45  	fi
    46  else
    47  	STAGE3_ARCH="${STAGE3_ARCH:-amd64}"
    48  fi
    49  
    50  if test -z "${BASE_ARCH}"
    51  then
    52  	case "${STAGE3_ARCH}" in
    53  		arm*)
    54  			BASE_ARCH=arm
    55  			;;
    56  		i[46]86)
    57  			BASE_ARCH=x86
    58  			;;
    59  		ppc*)
    60  			BASE_ARCH=ppc
    61  			;;
    62  		*)
    63  			BASE_ARCH="${STAGE3_ARCH}"
    64  			;;
    65  	esac
    66  fi
    67  
    68  BASE_ARCH_URL="${BASE_ARCH_URL:-${MIRROR}releases/${BASE_ARCH}/autobuilds/}"
    69  
    70  if test -z "${STAGE3}"
    71  then
    72  	LATEST=$(wget -O - "${BASE_ARCH_URL}latest-stage3.txt")
    73  	if test -z "${DATE}"
    74  	then
    75  		DATE=$(echo "${LATEST}" | sed -n "s|/stage3-${STAGE3_ARCH}-[0-9TZ]*[.]tar.*||p")
    76  		if test -z "${DATE}"
    77  		then
    78  			die "could not calculate DATE from ${BASE_ARCH_URL}latest-stage3.txt"
    79  		fi
    80  	fi
    81  
    82  	STAGE3=$(echo "${LATEST}" | sed -n "s|${DATE}/\(stage3-${STAGE3_ARCH}-${DATE}[.]tar[.][^ ]*\) .*|\1|p")
    83  	if test -z "${STAGE3}"
    84  	then
    85  		die "could not calculate STAGE3 from ${BASE_ARCH_URL}latest-stage3.txt"
    86  	fi
    87  fi
    88  
    89  ARCH_URL="${ARCH_URL:-${BASE_ARCH_URL}${DATE}/}"
    90  STAGE3_CONTENTS="${STAGE3_CONTENTS:-${STAGE3}.CONTENTS}"
    91  STAGE3_DIGESTS="${STAGE3_DIGESTS:-${STAGE3}.DIGESTS.asc}"
    92  
    93  COMPRESSION=$(echo "${STAGE3}" | sed -n 's/^.*[.]\([^.]*\)$/\1/p')
    94  if test -z "${COMPRESSION}"
    95  then
    96  	die "could not calculate COMPRESSION from ${STAGE3}"
    97  fi
    98  for FILE in "${STAGE3}" "${STAGE3_CONTENTS}" "${STAGE3_DIGESTS}"; do
    99  	if [ ! -f "downloads/${FILE}" ]; then
   100  		wget -O "downloads/${FILE}" "${ARCH_URL}${FILE}"
   101  		if [ "$?" -ne 0 ]; then
   102  			rm -f "downloads/${FILE}" &&
   103  			die "failed to download ${ARCH_URL}${FILE}"
   104  		fi
   105  	fi
   106  
   107  	FILE_NOCOMPRESSION=$(echo "${FILE}" | sed "s/[.]${COMPRESSION}//")
   108  	if [ "${FILE_NOCOMPRESSION}" = "${FILE}" ]; then
   109  		die "unable to remove .${COMPRESSION} from ${FILE}"
   110  	fi
   111  	CURRENT=$(echo "${FILE_NOCOMPRESSION}" | sed "s/${DATE}/current/")
   112  	(
   113  		cd downloads &&
   114  		rm -f "${CURRENT}" &&
   115  		ln -s "${FILE}" "${CURRENT}" ||
   116  		die "failed to link ${CURRENT} -> ${FILE}"
   117  	)
   118  done