Skip to content

Instantly share code, notes, and snippets.

@mauricioprado00
Last active November 3, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioprado00/1e40021b7e5a8bfdc8049e51b851d4b9 to your computer and use it in GitHub Desktop.
Save mauricioprado00/1e40021b7e5a8bfdc8049e51b851d4b9 to your computer and use it in GitHub Desktop.
alpine-chroot
# for more info https://wiki.alpinelinux.org/wiki/Installing_Alpine_Linux_in_a_chroot
# one-liner:
# curl -L https://goo.gl/huUs7L | bash
# one liner with params:
# curl -L https://goo.gl/huUs7L | bash -s myalpinedir x86_64 v3.4
# one liner from yandex:
# curl -L https://goo.gl/huUs7L | MIRROR_FILTER=yandex bash -s myalpinedir x86_64 v3.4
# get a random mirror from the available list
this_gist='https://gist.githubusercontent.com/mauricioprado00/1e40021b7e5a8bfdc8049e51b851d4b9/raw/'
mirror=$(curl http://nl.alpinelinux.org/alpine/MIRRORS.txt | grep ${MIRROR_FILTER:-.} | sort -R | head -n1)
chroot_dir=${1:-alpine}
arch=${2:-x86} #x86_64
branch=${3:-latest-stable}
wget --output-document="APKINDEX.tar.gz" ${mirror}/${branch}/main/${arch}/APKINDEX.tar.gz
if [ $? -ne 0 ]; then
echo 'could not download apk index'
rm APKINDEX.tar.gz
exit 1
fi
apk_tools_version=$(tar -O -xzf APKINDEX.tar.gz APKINDEX | grep -A1 'P:apk-tools-static' | tail -n1 | sed 's#^V:##g')
rm APKINDEX.tar.gz
wget --output-document="alpine.apk" ${mirror}/${branch}/main/${arch}/apk-tools-static-${apk_tools_version}.apk
if [ $? -ne 0 ]; then
echo 'failed to download alpine apk tools'
rm alpine.apk
exit 2
fi
tempdir=$(mktemp -d)
tar -xzf alpine.apk -C ${tempdir} sbin/apk.static
rm alpine.apk
# install system
${tempdir}/sbin/apk.static -X ${mirror}/latest-stable/main -U --allow-untrusted --root ${chroot_dir} --initdb add alpine-base
# remove files for apk tools
rm -Rf ${tempdir}
# remove cache
rm -Rf ${chroot_dir}/var/cache/apk/*
# add name resolution
cp /etc/resolv.conf ${chroot_dir}/etc/
# or use opendns: echo -e 'nameserver 208.67.222.222\nnameserver 2620:0:ccc::2' > ${chroot_dir}/etc/resolv.conf
# add repository
mkdir -p ${chroot_dir}/etc/apk
echo "${mirror}/${branch}/main" > ${chroot_dir}/etc/apk/repositories
# add helpers
curl ${this_gist}/mount.sh > ${chroot_dir}/mount.sh
curl ${this_gist}/umount.sh > ${chroot_dir}/umount.sh
chmod +x ${chroot_dir}/*.sh
CYAN='\033[1;36m'
NC='\033[0m'
printf "${CYAN}to mount chroot${NC}: ${chroot_dir}/mount.sh \n"
printf "${CYAN}to umount chroot${NC}: ${chroot_dir}/umount.sh \n"
printf "${CYAN}to enter into chroot${NC}: chroot ${chroot_dir} /bin/sh -l \n"
chroot_dir=$(dirname $0)/
# mount proc and sys
mount -t proc none ${chroot_dir}/proc
mount -o bind /sys ${chroot_dir}/sys
# map all devices
mount -o bind /dev ${chroot_dir}/dev
chroot_dir=$(dirname $0)/
# unmount proc and sys
umount ${chroot_dir}/proc
umount ${chroot_dir}/sys
# unmap all devices
umount ${chroot_dir}/dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment