Skip to content

Instantly share code, notes, and snippets.

@zenlor
Forked from emolitor/alpine-chroot.sh
Last active May 12, 2019 10:15
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 zenlor/52a70eaafa5563905546bc83deca7e2c to your computer and use it in GitHub Desktop.
Save zenlor/52a70eaafa5563905546bc83deca7e2c to your computer and use it in GitHub Desktop.
chroot script for testing aarch64
#!/bin/sh
#
# This simple script is setting up a Alpine Linux installation in a chroot.
# chroot will be placed in the current working directory.
#
# Most parts of this script are written down at
# http://wiki.alpinelinux.org/wiki/Setting_up_the_build_environment_in_chroot
#
# Licensed under GPLv2
#
# Copyright (c) 2011-2019 Fabian Affolter <fabian at affolter-engineering.ch>
# Copyright (c) 2019 Lorenzo Giuliani <lorenzo at frenzart.com>
# default variables
MIRROR=http://dl-5.alpinelinux.org/alpine
ARCH=${ARCH:-"armhf"}
VERSION=v3.9
APK_TOOL_VERSION=${APK_TOOL_VERSION:-"2.10.3-r1"}
# Check if root
checkroot() {
# Root has $UID 0
ROOT_UID=0
if [ "$UID" != "$ROOT_UID" ]
then
echo "You are not root. Please use su to become root."
exit 0
fi
}
checkdir() {
if [ -d $CHROOT ]
then
echo "$CHROOT already exists."
echo "To start your Alpine chroot:"
echo "sudo chroot $CHROOT /bin/sh -l"
exit 0
else
mkdir -p $CHROOT
fi
}
usage() {
cat <<EUSAGE
USAGE: ./alpine-chroot [flags] <command>
Flags:
-h --help this message
--arch=<value> CPU architecture [aarch64 armhf armv7 ppc64le s390x x86 x86_64] (default=${ARCH})
--apk-tool-version apk-tool version (default=${APK_TOOL_VERSION})
--mirror Alpine linux mirror (default=${MIRROR})
EUSAGE
}
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit 0
;;
--mirror)
MIRROR=$VALUE
;;
--arch)
ARCH=$VALUE
;;
--apk-tool-version)
APK_TOOL_VERSION=$VALUE
;;
enter | create)
ACTION=$PARAM
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
CHROOT=alpine-chroot-${ARCH}
APK_TOOL="apk-tools-static-${APK_TOOL_VERSION}.apk"
# create a new chroot
create() {
# download apk-tool
wget -O - $MIRROR/$VERSION/main/$ARCH/$APK_TOOL | tar -xzf -
./sbin/apk.static \
-X $MIRROR/$VERSION/main \
-U \
--allow-untrusted \
--root ././$CHROOT \
--initdb add alpine-base alpine-sdk
mkdir -p $CHROOT{/root,/etc/apk,/proc}
mount --bind /proc $CHROOT/proc
mknod -m 666 $CHROOT/dev/full c 1 7
mknod -m 666 $CHROOT/dev/ptmx c 5 2
mknod -m 644 $CHROOT/dev/random c 1 8
mknod -m 644 $CHROOT/dev/urandom c 1 9
mknod -m 666 $CHROOT/dev/zero c 1 5
mknod -m 666 $CHROOT/dev/tty c 5 0
rm -f $CHROOT/dev/null
mknod -m 666 $CHROOT/dev/null c 1 3
chmod 777 /dev/shm
cp /etc/resolv.conf $CHROOT/etc/
echo "$MIRROR/$VERSION/main" > $CHROOT/etc/apk/repositories
# Cleaning up
rm -rf sbin
rm -f APK_TOOL
cat <<EEND
Your Alpine Linux installation in '$CHROOT' is ready.
To start Alpine execute as root:
$0 --arch=$ARCH start
or manually:
mount -o bind ./aports $CHROOT/aports
chroot $CHROOT /bin/sh -l
EEND
}
case $ACTION in
enter)
checkroot
echo "mounting ./aports as /aports"
mount -o bind ./aports $CHROOT/aports
echo "entering chroot..."
chroot $CHROOT /bin/sh -l
;;
create)
checkroot
checkdir
create
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment