summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Affolter <fabian@affolter-engineering.ch>2012-03-10 00:46:55 +0100
committerFabian Affolter <fabian@affolter-engineering.ch>2012-03-10 00:46:55 +0100
commit80ed964f96711bedc75c849c6df88c68161d5402 (patch)
tree40a0f813a68a477dc27f1f73916823171c73756e
parent82d43971fe0c61a2bd08a17781c6925d97a756b7 (diff)
downloadalpine-chroot-80ed964f96711bedc75c849c6df88c68161d5402.tar.bz2
alpine-chroot-80ed964f96711bedc75c849c6df88c68161d5402.tar.xz
Initial commit
-rw-r--r--alpine-chroot.sh122
1 files changed, 122 insertions, 0 deletions
diff --git a/alpine-chroot.sh b/alpine-chroot.sh
new file mode 100644
index 0000000..642365e
--- /dev/null
+++ b/alpine-chroot.sh
@@ -0,0 +1,122 @@
+# alpine-chroot - Setup a Alpine Linux installation in a chroot.
+# Copyright (c) 2011- 2012 Fabian Affolter <fabian at affolter-engineering.ch>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Most parts of this script are written down at
+# http://wiki.alpinelinux.org/wiki/Setting_up_the_build_environment_in_chroot
+#
+# coreutils, curl
+
+MIRROR=http://dl-3.alpinelinux.org/alpine
+APK_TOOL=apk-tools-static-2.2.5-r0.apk
+
+usage() {
+ cat <<__EOF__
+usage: mk-alpinechroot [-h] [-d Directory] [-r Release] [-a Architecture]
+
+Setup an Alpine Installation in a chroot
+
+options:
+ -h Show this help
+ -a Choice of architecture, default: Host architecture
+ -d Directory to install the chroot
+ -r Release of Alpine Linux, default: Latest stable release
+__EOF__
+ exit 1
+}
+
+case $1 in
+ ""|"-h"|"--help") usage;;
+esac
+
+while getopts ":h:a:d:r:" opt; do
+ case $opt in
+ h) usage;;
+ a) ARCH="$OPTARG";;
+ d) CHROOT="$OPTARG";;
+ r) RELEASE="$OPTARG";;
+ \?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
+ :) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
+ esac
+done
+
+if [ "$ARCH" = "" ]; then
+ ARCH=`uname -i`
+fi
+
+if [ "$ARCH" != "x86" ] && [ "$ARCH" != "x86_64" ]; then
+ echo "$ARCH not supported. Only x86 and x86_64 are supported."
+ exit 1
+fi
+
+if [ "$CHROOT" = "" ]; then
+ CHROOT="alpinechroot-$RELEASE"
+fi
+
+if [ "$RELEASE" = "" ]; then
+ LATEST=`curl --silent $MIRROR/.latest.txt`
+ RELEASE=${LATEST:31:3}
+fi
+
+# 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
+
+if [ -d $CHROOT ]; then
+echo "$CHROOT exists already."
+ exit 0
+else
+ mkdir -p $CHROOT
+fi
+
+build_chroot() {
+ curl --silent $MIRROR/v$RELEASE/main/$ARCH/$APK_TOOL -o $APK_TOOL
+ tar -xzf $APK_TOOL
+ ./sbin/apk.static \
+ -X $MIRROR/v$RELEASE/main \
+ -U \
+ --allow-untrusted \
+ --root ././$CHROOT-$RELEASE \
+ --initdb add alpine-base alpine-sdk
+
+ mkdir -p $CHROOT{/root,/etc/apk,/proc}
+ mount --bind /proc $CHROOT/proc
+ mknod -m 0666 $CHROOT/dev/full c 1 7
+ mknod -m 0666 $CHROOT/dev/ptmx c 5 2
+ mknod -m 0644 $CHROOT/dev/random c 1 8
+ mknod -m 0644 $CHROOT/dev/urandom c 1 9
+ mknod -m 0666 $CHROOT/dev/zero c 1 5
+ mknod -m 0666 $CHROOT/dev/tty c 5 0
+ rm -f $CHROOT/dev/null
+ mknod -m 0666 $CHROOT/dev/null c 1 3
+
+ cp /etc/resolv.conf $CHROOT/etc/
+ echo "$MIRROR/v$RELEASE/main" > $CHROOT/etc/apk/repositories
+
+ # Cleaning up
+ rm -rf sbin
+ rm -f APK_TOOL
+
+ echo " "
+ echo "Your Alpine Linux installation in '$CHROOT' is ready now."
+ echo "To start Alpine chroot, run as root:"
+ echo " chroot $CHROOT /bin/sh -l"
+ echo " "
+}
+
+build_chroot