#!/bin/bash # # alpine-chroot - Setup a Alpine Linux installation in a chroot. # Copyright (c) 2011-2016 Fabian Affolter # # 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 . # # Most parts of this script are written down at # http://wiki.alpinelinux.org/wiki/Setting_up_the_build_environment_in_chroot MIRROR=http://dl-5.alpinelinux.org/alpine APK_TOOL=apk-tools-2.6.7-r0.apk usage() { cat <<__EOF__ usage: alpine-chroot [-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 [ "$RELEASE" = "" ]; then LATEST=`curl --silent $MIRROR/.latest.txt` RELEASE=${LATEST:31:3} fi 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 # 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 \ --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