1#!/bin/sh2# Chroots into an existing alpine chroot.34set -e56##7# Variables8##910CHROOT=/usr/sbin/chroot11USER=root1213##14# Functions15##1617mount_me_maybe() {18 mountpoint="${1}"19 shift2021 busybox mountpoint -q "${mountpoint}" && return22 mount "$@" "${mountpoint}"23}2425##26# Go for it!27##2829if [ $(id -u) -ne 0 ]; then30 echo "${0##*/} has to be started as root." 1>&231 exit 132fi3334while getopts u: flag; do35 case "${flag}" in36 u) USER="${OPTARG}" ;;37 esac38done3940shift $((OPTIND - 1))41if [ $# -lt 1 ]; then42 echo "USAGE: ${0##*/} [-u USER] CHROOT [COMMAND]" 1>&243 exit 144fi4546chrootdir="${1}"47if [ $# -eq 1 ]; then48 set -- /bin/su -l "${USER}"49else50 shift51fi5253mount_me_maybe "${chrootdir}"/proc -t proc none54mount_me_maybe "${chrootdir}"/dev --rbind /dev55mount_me_maybe "${chrootdir}"/sys --rbind /sys56mount_me_maybe "${chrootdir}"/run --rbind /run5758cp -u -L /etc/resolv.conf "${chrootdir}/etc/resolv.conf"59exec env -i "${CHROOT}" "${chrootdir}" "$@"