[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 |
---|
[25933] | 11 | TYPE=overlayfs |
---|
[25767] | 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} |
---|
[25969] | 42 | : ${DATA="/usr/share/debathena-build-server"} |
---|
[25972] | 43 | # This used to include its own copy of the key, we now just use the |
---|
| 44 | # one from archive-keyring |
---|
| 45 | : ${APTKEY="/usr/share/keyrings/debathena-archive-keyring.gpg"} |
---|
[25969] | 46 | if [ ! -d "$DATA" ]; then |
---|
| 47 | DATA="$(dirname "$0")" |
---|
| 48 | echo "WARNING: Falling back to DATA=$DATA" >&2 |
---|
| 49 | fi |
---|
[22687] | 50 | SUITE=$1 |
---|
| 51 | ARCH=$2 |
---|
| 52 | CHROOT="${SUITE}-${ARCH}-sbuild" |
---|
[25767] | 53 | DEVICE="$VG/$CHROOT" |
---|
[25490] | 54 | FILE="$CHROOTDIR"/"$CHROOT".tar |
---|
| 55 | [ $BZ2 -eq 1 ] && FILE="${FILE}.bz2" |
---|
[24021] | 56 | SIZE=4G |
---|
[22687] | 57 | |
---|
| 58 | exittrap() { :; } |
---|
| 59 | for sig in 1 2 13 15; do trap "exit $(($sig + 128))" $sig; done |
---|
| 60 | trap 'exittrap' EXIT |
---|
| 61 | |
---|
[23147] | 62 | if fgrep -qxRs "[$CHROOT]" /etc/schroot/schroot.conf /etc/schroot/chroot.d; then |
---|
[25797] | 63 | echo "Chroot $CHROOT config already exists." >&2 |
---|
[22687] | 64 | exit 1 |
---|
| 65 | fi |
---|
[25767] | 66 | if [ "$TYPE" = "tar" ] && [ -e "$FILE" ]; then |
---|
[25482] | 67 | echo "Chroot tarball $FILE already exists." >&2 |
---|
| 68 | exit 1 |
---|
| 69 | fi |
---|
[22687] | 70 | |
---|
[25767] | 71 | if [ "$TYPE" = "overlayfs" ] && lvdisplay $DEVICE > /dev/null 2>&1; then |
---|
| 72 | echo "LV $DEVICE already exists." >&2 |
---|
| 73 | exit 1 |
---|
| 74 | fi |
---|
| 75 | |
---|
[23147] | 76 | if [ -d /etc/schroot/chroot.d ]; then |
---|
| 77 | CONF=/etc/schroot/chroot.d/$CHROOT |
---|
| 78 | else |
---|
| 79 | CONF=/etc/schroot/schroot.conf |
---|
| 80 | fi |
---|
| 81 | |
---|
[22687] | 82 | if [ "$ARCH" = "i386" ]; then |
---|
| 83 | PERSONALITY=linux32 |
---|
| 84 | elif [ "$ARCH" = "amd64" ]; then |
---|
| 85 | PERSONALITY=linux |
---|
| 86 | else |
---|
| 87 | echo "Unrecognized architecture $ARCH." >&2 |
---|
| 88 | exit 1 |
---|
| 89 | fi |
---|
| 90 | |
---|
| 91 | SOURCESD="$DATA/sources.list.d" |
---|
| 92 | if [ -e "$SOURCESD/debian/$SUITE.list" ]; then |
---|
| 93 | SOURCES="$SOURCESD/debian/$SUITE.list" |
---|
[25484] | 94 | MIRROR="http://$MIRRORHOST/debian" |
---|
[22687] | 95 | elif [ -e "$SOURCESD/ubuntu/$SUITE.list" ]; then |
---|
| 96 | SOURCES="$SOURCESD/ubuntu/$SUITE.list" |
---|
[25484] | 97 | MIRROR="http://$MIRRORHOST/ubuntu" |
---|
[22687] | 98 | else |
---|
[25797] | 99 | echo "Unrecognized suite $SUITE. (Did you create the sources.list file?)" >&2 |
---|
[22687] | 100 | exit 1 |
---|
| 101 | fi |
---|
| 102 | |
---|
[25767] | 103 | if [ "$TYPE" = "overlayfs" ]; then |
---|
| 104 | lvcreate --size "$SIZE" --name "$CHROOT" "$VG" |
---|
| 105 | mkfs.ext3 "$DEVICE" |
---|
| 106 | tune2fs -c 0 -i 0 "$DEVICE" |
---|
| 107 | fi |
---|
| 108 | |
---|
[25661] | 109 | exittrap() { |
---|
| 110 | if [ $DEBUG -ne 1 ]; then |
---|
| 111 | rm -rf "$TMPDIR" || : |
---|
| 112 | else |
---|
| 113 | echo "DEBUG MODE: Clean up $TMPDIR by hand!" |
---|
| 114 | fi |
---|
| 115 | } |
---|
[25482] | 116 | TMPDIR=$(mktemp -d /var/tmp/make-chroot.XXXXXX) |
---|
[25990] | 117 | SCRIPTCFG="debathena/config" |
---|
[25767] | 118 | if [ "$TYPE" = "overlayfs" ]; then |
---|
| 119 | TARGET="$TMPDIR" |
---|
| 120 | mount "$DEVICE" "$TARGET" |
---|
| 121 | else |
---|
| 122 | TARGET="$TMPDIR/root" |
---|
| 123 | fi |
---|
[24264] | 124 | debootstrap --variant=buildd --include=apt,apt-utils,gnupg,build-essential,fakeroot --arch "$ARCH" "$SUITE" "$TARGET" "$MIRROR" |
---|
[22687] | 125 | install -m a=rx,u+w "$DATA/policy-rc.d" "$TARGET/usr/sbin/" |
---|
[22928] | 126 | mkdir -p "$TARGET/dev/pts" |
---|
[25660] | 127 | # /dev/shm is a symlink to /run/shm on modern distros |
---|
| 128 | # So if it's not a symlink, then mkdir (and fail if it's something |
---|
[25661] | 129 | # that's not a symlink or a directory) and we have to use an schroot |
---|
| 130 | # config that bind-mounts /run/shm instead of /dev/shm |
---|
| 131 | if [ -L "$TARGET/dev/shm" ] && |
---|
| 132 | [ "$(readlink "$TARGET/dev/shm")" = "/run/shm" ]; then |
---|
[25969] | 133 | SCRIPTCFG="debathena/config-slash-run" |
---|
[25661] | 134 | else |
---|
| 135 | mkdir -p "$TARGET/dev/shm" |
---|
| 136 | fi |
---|
[22928] | 137 | mkdir -p "$TARGET/afs" |
---|
| 138 | mkdir -p "$TARGET/mit" |
---|
[25767] | 139 | if [ "$TYPE" = "overlayfs" ]; then |
---|
| 140 | umount "$TARGET" |
---|
| 141 | rmdir "$TARGET" |
---|
| 142 | else |
---|
| 143 | TAROPTS="cf" |
---|
| 144 | [ $BZ2 -eq 1 ] && TAROPTS="cjf" |
---|
| 145 | (cd "$TMPDIR" && tar "$TAROPTS" "$FILE" --numeric-owner root) |
---|
| 146 | rm -rf "$TMPDIR" |
---|
| 147 | fi |
---|
[22687] | 148 | exittrap() { :; } |
---|
| 149 | |
---|
[23147] | 150 | ! [ -s "$CONF" ] || echo >> "$CONF" |
---|
[25767] | 151 | if [ "$TYPE" = "overlayfs" ]; then |
---|
| 152 | cat >> "$CONF" <<EOF |
---|
[22687] | 153 | [$CHROOT] |
---|
[25791] | 154 | type=block-device |
---|
| 155 | union-type=overlayfs |
---|
[22687] | 156 | description=$CHROOT |
---|
| 157 | groups=root,sbuild |
---|
| 158 | root-groups=root,sbuild |
---|
| 159 | source-groups=root |
---|
[25969] | 160 | script-config=$SCRIPTCFG |
---|
[25791] | 161 | device=$DEVICE |
---|
[22687] | 162 | personality=$PERSONALITY |
---|
| 163 | EOF |
---|
[25767] | 164 | else |
---|
| 165 | cat >> "$CONF" <<EOF |
---|
| 166 | [$CHROOT] |
---|
[25791] | 167 | type=file |
---|
[25767] | 168 | description=$CHROOT |
---|
| 169 | groups=root,sbuild |
---|
| 170 | root-groups=root,sbuild |
---|
| 171 | source-groups=root |
---|
[25791] | 172 | script-config=$SCRIPTCFG |
---|
| 173 | file=$FILE |
---|
| 174 | location=/root |
---|
[25767] | 175 | personality=$PERSONALITY |
---|
| 176 | EOF |
---|
| 177 | fi |
---|
[22687] | 178 | |
---|
[22928] | 179 | schroot --chroot="${CHROOT}-source" --directory=/ -- /bin/sh -xe - \ |
---|
[22687] | 180 | 8< "$SOURCES" \ |
---|
[25972] | 181 | 9< "$APTKEY" \ |
---|
[22687] | 182 | <<EOF |
---|
[22928] | 183 | #/debootstrap/debootstrap --second-stage |
---|
[24144] | 184 | dpkg-divert --local --rename --add /sbin/initctl |
---|
| 185 | ln -s /bin/true /sbin/initctl |
---|
[22696] | 186 | debconf-set-selections <<SELECTIONS |
---|
| 187 | debconf debconf/frontend select Noninteractive |
---|
| 188 | SELECTIONS |
---|
[25484] | 189 | sed "s|MIRRORHOST|$MIRRORHOST|" <&8 > /etc/apt/sources.list |
---|
[22687] | 190 | ! type apt-key >/dev/null || apt-key add - <&9 |
---|
| 191 | apt-get -q -y update |
---|
| 192 | apt-get -q -y dist-upgrade |
---|
| 193 | EOF |
---|
[25972] | 194 | echo "Done: $CHROOT created successfully." |
---|
| 195 | exit 0 |
---|