source: trunk/debathena/scripts/build-server/make-chroot @ 25797

Revision 25797, 4.3 KB checked in by jdreed, 12 years ago (diff)
Add usage message and make error messages clearer
  • Property svn:executable set to *
Line 
1#!/bin/sh
2
3# Usage: make-chroot SUITE ARCH
4
5# Constructs a chroot environment for the Debian/Ubuntu version SUITE
6# and architecture ARCH, and sets up schroot configuration for it.
7# Assumes an approx cache is running at localhost:9999.
8
9DEBUG=0
10BZ2=0
11TYPE=tar
12while getopts 'dzot' opt; do
13    case "$opt" in
14        d)
15            DEBUG=1
16            set -x
17            ;;
18        z)
19            BZ2=1;;
20        o)
21            TYPE=overlayfs;;
22        t)
23            TYPE=tar;;
24    esac
25done
26shift $((OPTIND-1))
27
28if [ $BZ2 -eq 1 ] && [ "$TYPE" != "tar" ]; then
29    echo "-z is meaningless if not using a tar chroot and will be ignored" >&2
30fi
31
32if [ -z "$1" ] || [ -z "$2" ]; then
33    echo "Usage: $0 <suite> <arch>" >&2
34    exit 1
35fi
36
37set -e
38
39: ${VG=/dev/builder}
40: ${CHROOTDIR=/srv}
41: ${MIRRORHOST=localhost:9999}
42: ${DATA="$(dirname "$0")"}
43SUITE=$1
44ARCH=$2
45CHROOT="${SUITE}-${ARCH}-sbuild"
46DEVICE="$VG/$CHROOT"
47FILE="$CHROOTDIR"/"$CHROOT".tar
48[ $BZ2 -eq 1 ] && FILE="${FILE}.bz2"
49SIZE=4G
50
51exittrap() { :; }
52for sig in 1 2 13 15; do trap "exit $(($sig + 128))" $sig; done
53trap 'exittrap' EXIT
54
55if fgrep -qxRs "[$CHROOT]" /etc/schroot/schroot.conf /etc/schroot/chroot.d; then
56        echo "Chroot $CHROOT config already exists." >&2
57        exit 1
58fi
59if [ "$TYPE" = "tar" ] && [ -e "$FILE" ]; then
60        echo "Chroot tarball $FILE already exists." >&2
61        exit 1
62fi
63
64if [ "$TYPE" = "overlayfs" ] && lvdisplay $DEVICE > /dev/null 2>&1; then
65        echo "LV $DEVICE already exists." >&2
66        exit 1
67fi
68
69if [ -d /etc/schroot/chroot.d ]; then
70        CONF=/etc/schroot/chroot.d/$CHROOT
71else
72        CONF=/etc/schroot/schroot.conf
73fi
74
75if [ "$ARCH" = "i386" ]; then
76        PERSONALITY=linux32
77elif [ "$ARCH" = "amd64" ]; then
78        PERSONALITY=linux
79else
80        echo "Unrecognized architecture $ARCH." >&2
81        exit 1
82fi
83
84SOURCESD="$DATA/sources.list.d"
85if [ -e "$SOURCESD/debian/$SUITE.list" ]; then
86        SOURCES="$SOURCESD/debian/$SUITE.list"
87        MIRROR="http://$MIRRORHOST/debian"
88elif [ -e "$SOURCESD/ubuntu/$SUITE.list" ]; then
89        SOURCES="$SOURCESD/ubuntu/$SUITE.list"
90        MIRROR="http://$MIRRORHOST/ubuntu"
91else
92        echo "Unrecognized suite $SUITE. (Did you create the sources.list file?)" >&2
93        exit 1
94fi
95
96if [ "$TYPE" = "overlayfs" ]; then
97    lvcreate --size "$SIZE" --name "$CHROOT" "$VG"
98    mkfs.ext3 "$DEVICE"
99    tune2fs -c 0 -i 0 "$DEVICE"
100fi
101
102exittrap() {
103    if [ $DEBUG -ne 1 ]; then
104        rm -rf "$TMPDIR" || :
105    else
106        echo "DEBUG MODE: Clean up $TMPDIR by hand!"
107    fi
108}
109TMPDIR=$(mktemp -d /var/tmp/make-chroot.XXXXXX)
110if [ "$TYPE" = "overlayfs" ]; then
111    TARGET="$TMPDIR"
112    mount "$DEVICE" "$TARGET"
113else
114    TARGET="$TMPDIR/root"
115    SCRIPTCFG="default/config"
116fi
117debootstrap --variant=buildd --include=apt,apt-utils,gnupg,build-essential,fakeroot --arch "$ARCH" "$SUITE" "$TARGET" "$MIRROR"
118install -m a=rx,u+w "$DATA/policy-rc.d" "$TARGET/usr/sbin/"
119mkdir -p "$TARGET/dev/pts"
120# /dev/shm is a symlink to /run/shm on modern distros
121# So if it's not a symlink, then mkdir (and fail if it's something
122# that's not a symlink or a directory) and we have to use an schroot
123# config that bind-mounts /run/shm instead of /dev/shm
124if [ -L "$TARGET/dev/shm" ] &&
125   [ "$(readlink "$TARGET/dev/shm")" = "/run/shm" ]; then
126    SCRIPTCFG="default-slash-run/config"
127else
128    mkdir -p "$TARGET/dev/shm"
129fi
130mkdir -p "$TARGET/afs"
131mkdir -p "$TARGET/mit"
132if [ "$TYPE" = "overlayfs" ]; then
133    umount "$TARGET"
134    rmdir "$TARGET"
135else
136    TAROPTS="cf"
137    [ $BZ2 -eq 1 ] && TAROPTS="cjf"
138    (cd "$TMPDIR" && tar "$TAROPTS" "$FILE" --numeric-owner root)
139    rm -rf "$TMPDIR"
140fi
141exittrap() { :; }
142
143! [ -s "$CONF" ] || echo >> "$CONF"
144if [ "$TYPE" = "overlayfs" ]; then
145    cat >> "$CONF" <<EOF
146[$CHROOT]
147type=block-device
148union-type=overlayfs
149description=$CHROOT
150groups=root,sbuild
151root-groups=root,sbuild
152source-groups=root
153device=$DEVICE
154personality=$PERSONALITY
155EOF
156else
157    cat >> "$CONF" <<EOF
158[$CHROOT]
159type=file
160description=$CHROOT
161groups=root,sbuild
162root-groups=root,sbuild
163source-groups=root
164script-config=$SCRIPTCFG
165file=$FILE
166location=/root
167personality=$PERSONALITY
168EOF
169fi
170
171schroot --chroot="${CHROOT}-source" --directory=/ -- /bin/sh -xe - \
172    8< "$SOURCES" \
173    9< "$DATA/debathena-archive.asc" \
174    <<EOF
175#/debootstrap/debootstrap --second-stage
176dpkg-divert --local --rename --add /sbin/initctl
177ln -s /bin/true /sbin/initctl
178debconf-set-selections <<SELECTIONS
179debconf debconf/frontend        select  Noninteractive
180SELECTIONS
181sed "s|MIRRORHOST|$MIRRORHOST|" <&8 > /etc/apt/sources.list
182! type apt-key >/dev/null || apt-key add - <&9
183apt-get -q -y update
184apt-get -q -y dist-upgrade
185EOF
Note: See TracBrowser for help on using the repository browser.