summaryrefslogtreecommitdiff
path: root/admin-schroot.sh
blob: 827de02274f821cc6edd94eda4b39b6d1eae022e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash

# Improve debug logs
PRGNAME=`basename $0`
PS4='+ $PRGNAME: ${FUNCNAME+"$FUNCNAME : "}$LINENO: '

set -e

arch="native"
dist="default"
schroot_master="$(dirname "$0")"
gen_schroot=false
target_ssh_opts=""
profile="tcwg-test"

while getopts "a:d:go:P:qv" OPTION; do
    case $OPTION in
	a) arch=$OPTARG ;;
	d) dist=$OPTARG ;;
	g) gen_schroot=true ;;
	o) target_ssh_opts="$OPTARG" ;;
	P) profile="$OPTARG" ;;
	q) exec > /dev/null ;;
	v) set -x ;;
    esac
done
shift $((OPTIND-1))

target="${1%:*}"
port="${1#*:}"

triplet_to_deb_arch()
{
    set -e
    case "$1" in
	aarch64-*linux-gnu) echo arm64 ;;
	arm-*linux-gnueabi*) echo armhf ;;
	i686-*linux-gnu) echo i386 ;;
	x86_64-*linux-gnu) echo amd64 ;;
	*) return 1 ;;
    esac
}

triplet_to_deb_dist()
{
    set -e
    case "$arch" in
	aarch64-*linux-gnu) echo trusty ;;
	arm-*linux-gnueabi*) echo trusty ;;
	i686-*linux-gnu) echo trusty ;;
	x86_64-*linux-gnu) echo trusty ;;
	*) return 1 ;;
    esac
}

print_apt_sources()
{
    set -e

    local deb_arch="$1"
    local deb_dist="$2"

    case "$deb_dist" in
	"precise"|"trusty")
	    local deb_mirror
	    case "$deb_arch" in
		amd64|i386) deb_mirror="http://archive.ubuntu.com/ubuntu/" ;;
		*) deb_mirror="http://ports.ubuntu.com/ubuntu-ports/" ;;
	    esac

	    cat <<EOF
deb $deb_mirror $deb_dist main restricted universe multiverse
deb-src $deb_mirror $deb_dist main restricted universe multiverse
deb $deb_mirror $deb_dist-updates main restricted universe multiverse
deb-src $deb_mirror $deb_dist-updates main restricted universe multiverse
deb $deb_mirror $deb_dist-security main restricted universe multiverse
deb-src $deb_mirror $deb_dist-security main restricted universe multiverse
deb $deb_mirror $deb_dist-backports main restricted universe multiverse
deb-src $deb_mirror $deb_dist-backports main restricted universe multiverse
EOF
	    ;;
	"jessie")
	    cat <<EOF
deb http://http.debian.net/debian/ $deb_dist main contrib non-free
deb-src http://http.debian.net/debian/ $deb_dist main contrib non-free
deb http://security.debian.org/ $deb_dist/updates main contrib non-free
deb-src http://security.debian.org/ $deb_dist/updates main contrib non-free
EOF
	    ;;
    esac
}

if [ -z "$target" ]; then
    echo ERROR: no target specified
    exit 1
fi

if [ -z "$port" ]; then
    port="22"
fi

cpu="$(ssh $target uname -m)"
case "$cpu" in
    aarch64) native_arch=aarch64-linux-gnu ;;
    armv7l) native_arch=arm-linux-gnueabihf ;;
    armv7*) native_arch=arm-linux-gnueabi ;;
    i686) native_arch=i686-linux-gnu ;;
    x86_64) native_arch=x86_64-linux-gnu ;;
    *)
	echo "ERROR: unrecognized native target $cpu"
	exit 1
	;;
esac

if [ x"$arch" = x"native" ]; then
    arch="$native_arch"
fi

deb_arch="$(triplet_to_deb_arch $arch)"

if [ x"deb_dist" = x"default" ]; then
    deb_dist="$(triplet_to_deb_dist $arch)"
else
    deb_dist="$dist"
fi

orig_target_ssh_opts="$target_ssh_opts"
target_ssh_opts="$target_ssh_opts -o ControlMaster=auto -o ControlPersist=1m -o ControlPath=$(mktemp --suffix -ssh)"

schroot_id=$profile-$deb_arch-$deb_dist

schroot="ssh $target_ssh_opts $target schroot -r -c session:$profile-$port -d / -u root --"
rsh_opts="$target_ssh_opts -o Port=$port -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
rsh="ssh $rsh_opts"
user="$(ssh $target_ssh_opts $target echo \$USER)"
home="$(ssh $target_ssh_opts $target pwd)"

if $gen_schroot; then
    chroot=/tmp/$schroot_id.$$

    # Make sure machine in the lab agree on what time it is.
    ssh $target_ssh_opts $target \
	sudo ntpdate pool.ntp.org >/dev/null 2>&1 || true

    ssh $target_ssh_opts $target \
	sudo rm -rf $chroot
    ssh $target_ssh_opts $target \
	sudo debootstrap \
	--arch=$deb_arch \
	--variant=minbase \
	--include=iptables,openssh-server,rsync,sshfs \
	$deb_dist $chroot

    # Configure APT sources.
    print_apt_sources "$deb_arch" "$deb_dist" \
	| ssh $target_ssh_opts $target sudo chroot $chroot bash -c "\"cat > /etc/apt/sources.list\""
    case "$deb_arch" in
	amd64|i386)
	    ssh $target_ssh_opts $target \
		sudo chroot $chroot bash -c "\"echo deb http://ftp.linuxfoundation.org/pub/lsb/repositories/debian lsb-4.1 main >> /etc/apt/sources.list\""
    esac

    extra_packages=""
    force_packages=""
    case "$deb_arch" in
	amd64) extra_packages="$extra_packages qemu-user" ;;
    esac

    case "$profile" in
	"tcwg-build")
	    extra_packages="$extra_packages autoconf autogen automake bash bison build-essential ccache ccrypt dejagnu flex gawk gdb gdbserver git g++ gcc libncurses5-dev libtool lsb-release make texinfo wget xz-utils"
	    case "$deb_arch" in
		amd64) extra_packages="$extra_packages gcc-multilib g++-multilib" ;;
	    esac
	    case "$deb_arch" in
		amd64|i386)
		    force_packages="$force_packages lsb-build-cc" ;;
	    esac
	    extra_packages="$extra_packages emacs vim"
	    # Install arp and nc for ssh's ProxyCommand
	    extra_packages="$extra_packages net-tools netcat"
	    # Install sudo to simplify root access
	    extra_packages="$extra_packages sudo"
	    ;;
    esac

    if [ x"$extra_packages" != x"" -o x"$force_packages" != x"" ]; then
	ssh $target_ssh_opts $target \
	    sudo chroot $chroot apt-get update
	if [ x"$extra_packages" != x"" ]; then
	    ssh $target_ssh_opts $target \
		sudo chroot $chroot apt-get install -y "$extra_packages"
	fi
	if [ x"$force_packages" != x"" ]; then
	    ssh $target_ssh_opts $target \
		sudo chroot $chroot apt-get install -y --force-yes "$force_packages"
	fi
    fi

    ssh $target_ssh_opts $target \
	sudo chroot $chroot rm -rf /var/cache/apt /var/lib/apt/lists

    if echo "$extra_packages" | grep -q "git"; then
	ssh $target_ssh_opts $target \
	    sudo ln -s /usr/share/doc/git/contrib/workdir/git-new-workdir $chroot/usr/local/bin/
	ssh $target_ssh_opts $target \
	    sudo chmod a+x $chroot/usr/share/doc/git/contrib/workdir/git-new-workdir
    fi

    # Install the Foundation Model on x86_64 build hosts
    if [ x"$profile" = xtcwg-build -a x"$deb_arch" = xamd64 ]; then
	if ! [ -e $schroot_master/FM000-KT-00035-r9p5-41rel0.tgz ]; then
	    echo "ERROR: cannot find Foundation Model tarball: $schroot_master/FM000-KT-00035-r9p5-41rel0.tgz"
	    exit 1
	fi
	ssh $target_ssh_opts $target \
	    sudo mkdir $chroot/linaro
	cat $schroot_master/FM000-KT-00035-r9p5-41rel0.tgz | ssh $target_ssh_opts $target \
	    sudo tar xzf - -C $chroot/linaro
    fi

    half_ram="$(ssh $target_ssh_opts $target free | grep "Mem:" | sed -e "s/Mem: *\([0-9]*\).*/\1/")"
    if [ x"$half_ram" = x"" -o "$half_ram" -lt "$((1024*1024))" ]; then
	# Don't limit RAM on starved boards (<1GB RAM).
	half_ram=""
    else
	half_ram="$(($half_ram / 2))"
    fi

    # Limit users to 5000 processes and half of RAM to avoid fork-bombs and
    # OOMs.
    for group in tcwg tcwg-infra; do
	ssh $target_ssh_opts $target \
	    sudo sh -c "\"echo @$group - nproc 5000 > $chroot/etc/security/limits.d/$group.conf\""
	if [ x"$half_ram" != x"" ]; then
	    ssh $target_ssh_opts $target \
		sudo sh -c "\"echo @$group - data $half_ram >> $chroot/etc/security/limits.d/$group.conf\""
	fi
    done

    # Tweak /etc/init.d/ssh to workaround upstart
    ssh $target_ssh_opts $target \
	sudo sed -i -e "'/check_for_upstart [0-9]/d'" $chroot/etc/init.d/ssh
    # Debian (but not Ubuntu) has wrong permissions on /bin/fusermount.
    ssh $target_ssh_opts $target \
	sudo chmod +x $chroot/bin/fusermount || true

    ssh $target_ssh_opts $target \
	sudo mkdir -p /var/chroots/
    ssh $target_ssh_opts $target \
	sudo bash -c "\"cd $chroot && tar --one-file-system -czf /var/chroots/$schroot_id.tgz .\""

    ssh $target_ssh_opts $target \
	sudo rm -rf $chroot

    case "$deb_arch" in
	armhf|i386) personality="
personality=linux32" ;;
	*) personality="" ;;
    esac

    case "$profile" in
	# Allow root access in test schroots (to install new system libs, etc)
	"tcwg-test") root_groups="
root-groups=users" ;;
	*) root_groups="" ;;
    esac

    ssh $target_ssh_opts $target \
	sudo bash -c "\"cat > /etc/schroot/chroot.d/$schroot_id\"" <<EOF
[$schroot_id]
type=file
file=/var/chroots/$schroot_id.tgz
groups=users
profile=$profile$personality$root_groups
EOF

    if ! [ -z "$schroot_master" ]; then
	scp $target_ssh_opts $target:/var/chroots/$schroot_id.tgz $schroot_master/
	mkdir -p $schroot_master/chroot.d/
	scp $target_ssh_opts $target:/etc/schroot/chroot.d/$schroot_id $schroot_master/chroot.d/
    fi
fi

if ! [ -z "$schroot_master" ]; then
    if ! (cd $schroot_master && git diff-files --quiet); then
	echo "Warning: Local tree has unstaged changes"
    fi

    # Make sure machine in the lab agree on what time it is.
    ssh $target_ssh_opts $target \
	sudo ntpdate pool.ntp.org >/dev/null 2>&1 || true

    ssh $target_ssh_opts $target \
	sudo mkdir -p /var/chroots/

    cat $schroot_master/$schroot_id.tgz | ssh $target_ssh_opts $target \
	sudo bash -c "\"cat > /var/chroots/$schroot_id.tgz\""
    cat $schroot_master/chroot.d/$schroot_id | ssh $target_ssh_opts $target \
	sudo bash -c "\"cat > /etc/schroot/chroot.d/$schroot_id\""

    (cd $schroot_master && tar -ch $profile/ setup.d/ | ssh $target_ssh_opts $target \
	sudo flock -x /etc/schroot -c "\"cd /etc/schroot && rm -rf $profile && tar -x && chown -R root:root $profile/ setup.d/\"")
fi