1#!/bin/sh2# Creates a new alpine chroot.34set -e56##7# Default values8##910ARCH="$(uname -m)"11RELEASE="edge"12CHROOT="alpine-chroot-${RELEASE}-${ARCH}"13MIRROR="http://dl-cdn.alpinelinux.org/alpine"14APKTOOL="$(command -v apk 2>&1)"1516if [ -d "/etc/apk/keys" ]; then17 KEYDIR="/etc/apk/keys"18else19 KEYDIR=""20fi2122##23# Functions24##2526create_chroot() {27 mkdir -p "${CHROOT}"/etc/apk/28 if [ -n "${KEYDIR}" ]; then29 cp -r "${KEYDIR}"/ "${CHROOT}"/etc/apk/keys30 else31 APKTOOL="${APKTOOL} --allow-untrusted"32 fi3334 repos="main community"35 if [ "${RELEASE}" = "edge" ]; then36 repos="${repos} testing"37 fi3839 echo "# Package repositories and mirrors" \40 > "${CHROOT}"/etc/apk/repositories41 for repo in ${repos}; do42 echo "${MIRROR}/${RELEASE}/${repo}" \43 >> "${CHROOT}"/etc/apk/repositories44 done4546 "${APKTOOL}" --initdb \47 --root "${CHROOT}" \48 --arch "${ARCH}" \49 --update-cache \50 add alpine-base5152 mkdir -p "${CHROOT}"/proc53 mount -v -t proc none "${CHROOT}"/proc5455 for directory in dev sys run; do56 mkdir -p "${CHROOT}/${directory}"57 mount -v --rbind "/${directory}" \58 "${CHROOT}/${directory}"59 done6061 cp -L /etc/resolv.conf "${CHROOT}"/etc/resolv.conf62}6364##65# Go for it!66##6768if [ $(id -u) -ne 0 ]; then69 echo "${0##*/} has to be started as root." 1>&270 exit 171fi7273while getopts a:c:r:k:m:p: flag; do74 case "${flag}" in75 a) ARCH="${OPTARG}" ;;76 c) CHROOT="${OPTARG}" ;;77 r) RELEASE="${OPTARG}" ;;78 k) KEYDIR="${OPTARG}" ;;79 m) MIRROR="${OPTARG}" ;;80 p) APKTOOL="${OPTARG}" ;;81 esac82done8384if [ ! -x "${APKTOOL}" ]; then85 echo "Couldn't find apk(1). Either add it to your \$PATH" 1>&286 echo "or specify the path to your apk tool using the -p" 1>&287 echo "command line flag. Also make sure that it is executable." 1>&288 exit 189fi9091if [ -d "${CHROOT}" ]; then92 echo "Chroot '${CHROOT}' already exists."93 exit 094fi9596create_chroot