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

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