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