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