summaryrefslogtreecommitdiffstats
path: root/alpine-chroot
blob: 49555d6b0bb1d0132f9afebf9203dea474835df7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
#
# alpine-chroot - Setup a Alpine Linux installation in a chroot.
# Copyright (c) 2011-2014 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

MIRROR=http://dl-5.alpinelinux.org/alpine
APK_TOOL=apk-tools-static-2.4.3-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