media/webrtc/trunk/build/install-chroot.sh

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/bin/bash -e
michael@0 2
michael@0 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 4 # Use of this source code is governed by a BSD-style license that can be
michael@0 5 # found in the LICENSE file.
michael@0 6
michael@0 7 # This script installs Debian-derived distributions in a chroot environment.
michael@0 8 # It can for example be used to have an accurate 32bit build and test
michael@0 9 # environment when otherwise working on a 64bit machine.
michael@0 10 # N. B. it is unlikely that this script will ever work on anything other than a
michael@0 11 # Debian-derived system.
michael@0 12
michael@0 13 # Older Debian based systems had both "admin" and "adm" groups, with "admin"
michael@0 14 # apparently being used in more places. Newer distributions have standardized
michael@0 15 # on just the "adm" group. Check /etc/group for the prefered name of the
michael@0 16 # administrator group.
michael@0 17 admin=$(grep '^admin:' /etc/group >&/dev/null && echo admin || echo adm)
michael@0 18
michael@0 19 usage() {
michael@0 20 echo "usage: ${0##*/} [-m mirror] [-g group,...] [-s] [-c]"
michael@0 21 echo "-b dir additional directories that should be bind mounted,"
michael@0 22 echo ' or "NONE".'
michael@0 23 echo " Default: if local filesystems present, ask user for help"
michael@0 24 echo "-g group,... groups that can use the chroot unauthenticated"
michael@0 25 echo " Default: '${admin}' and current user's group ('$(id -gn)')"
michael@0 26 echo "-l List all installed chroot environments"
michael@0 27 echo "-m mirror an alternate repository mirror for package downloads"
michael@0 28 echo "-s configure default deb-srcs"
michael@0 29 echo "-c always copy 64bit helper binaries to 32bit chroot"
michael@0 30 echo "-h this help message"
michael@0 31 }
michael@0 32
michael@0 33 process_opts() {
michael@0 34 local OPTNAME OPTIND OPTERR OPTARG
michael@0 35 while getopts ":b:g:lm:sch" OPTNAME; do
michael@0 36 case "$OPTNAME" in
michael@0 37 b)
michael@0 38 if [ "${OPTARG}" = "NONE" -a -z "${bind_mounts}" ]; then
michael@0 39 bind_mounts="${OPTARG}"
michael@0 40 else
michael@0 41 if [ "${bind_mounts}" = "NONE" -o "${OPTARG}" = "${OPTARG#/}" -o \
michael@0 42 ! -d "${OPTARG}" ]; then
michael@0 43 echo "Invalid -b option(s)"
michael@0 44 usage
michael@0 45 exit 1
michael@0 46 fi
michael@0 47 bind_mounts="${bind_mounts}
michael@0 48 ${OPTARG} ${OPTARG} none rw,bind 0 0"
michael@0 49 fi
michael@0 50 ;;
michael@0 51 g)
michael@0 52 [ -n "${OPTARG}" ] &&
michael@0 53 chroot_groups="${chroot_groups}${chroot_groups:+,}${OPTARG}"
michael@0 54 ;;
michael@0 55 l)
michael@0 56 list_all_chroots
michael@0 57 exit
michael@0 58 ;;
michael@0 59 m)
michael@0 60 if [ -n "${mirror}" ]; then
michael@0 61 echo "You can only specify exactly one mirror location"
michael@0 62 usage
michael@0 63 exit 1
michael@0 64 fi
michael@0 65 mirror="$OPTARG"
michael@0 66 ;;
michael@0 67 s)
michael@0 68 add_srcs="y"
michael@0 69 ;;
michael@0 70 c)
michael@0 71 copy_64="y"
michael@0 72 ;;
michael@0 73 h)
michael@0 74 usage
michael@0 75 exit 0
michael@0 76 ;;
michael@0 77 \:)
michael@0 78 echo "'-$OPTARG' needs an argument."
michael@0 79 usage
michael@0 80 exit 1
michael@0 81 ;;
michael@0 82 *)
michael@0 83 echo "invalid command-line option: $OPTARG"
michael@0 84 usage
michael@0 85 exit 1
michael@0 86 ;;
michael@0 87 esac
michael@0 88 done
michael@0 89
michael@0 90 if [ $# -ge ${OPTIND} ]; then
michael@0 91 eval echo "Unexpected command line argument: \${${OPTIND}}"
michael@0 92 usage
michael@0 93 exit 1
michael@0 94 fi
michael@0 95 }
michael@0 96
michael@0 97 list_all_chroots() {
michael@0 98 for i in /var/lib/chroot/*; do
michael@0 99 i="${i##*/}"
michael@0 100 [ "${i}" = "*" ] && continue
michael@0 101 [ -x "/usr/local/bin/${i%bit}" ] || continue
michael@0 102 grep -qs "^\[${i%bit}\]\$" /etc/schroot/schroot.conf || continue
michael@0 103 [ -r "/etc/schroot/script-${i}" -a \
michael@0 104 -r "/etc/schroot/mount-${i}" ] || continue
michael@0 105 echo "${i%bit}"
michael@0 106 done
michael@0 107 }
michael@0 108
michael@0 109 getkey() {
michael@0 110 (
michael@0 111 trap 'stty echo -iuclc icanon 2>/dev/null' EXIT INT TERM QUIT HUP
michael@0 112 stty -echo iuclc -icanon 2>/dev/null
michael@0 113 dd count=1 bs=1 2>/dev/null
michael@0 114 )
michael@0 115 }
michael@0 116
michael@0 117 chr() {
michael@0 118 printf "\\$(printf '%03o' "$1")"
michael@0 119 }
michael@0 120
michael@0 121 ord() {
michael@0 122 printf '%d' $(printf '%c' "$1" | od -tu1 -An)
michael@0 123 }
michael@0 124
michael@0 125 is_network_drive() {
michael@0 126 stat -c %T -f "$1/" 2>/dev/null |
michael@0 127 egrep -qs '^nfs|cifs|smbfs'
michael@0 128 }
michael@0 129
michael@0 130 # Check that we are running as a regular user
michael@0 131 [ "$(id -nu)" = root ] && {
michael@0 132 echo "Run this script as a regular user and provide your \"sudo\"" \
michael@0 133 "password if requested" >&2
michael@0 134 exit 1
michael@0 135 }
michael@0 136
michael@0 137 process_opts "$@"
michael@0 138
michael@0 139 echo "This script will help you through the process of installing a"
michael@0 140 echo "Debian or Ubuntu distribution in a chroot environment. You will"
michael@0 141 echo "have to provide your \"sudo\" password when requested."
michael@0 142 echo
michael@0 143
michael@0 144 # Error handler
michael@0 145 trap 'exit 1' INT TERM QUIT HUP
michael@0 146 trap 'sudo apt-get clean; tput bel; echo; echo Failed' EXIT
michael@0 147
michael@0 148 # Install any missing applications that this script relies on. If these packages
michael@0 149 # are already installed, don't force another "apt-get install". That would
michael@0 150 # prevent them from being auto-removed, if they ever become eligible for that.
michael@0 151 # And as this script only needs the packages once, there is no good reason to
michael@0 152 # introduce a hard dependency on things such as dchroot and debootstrap.
michael@0 153 dep=
michael@0 154 for i in dchroot debootstrap libwww-perl; do
michael@0 155 [ -d /usr/share/doc/"$i" ] || dep="$dep $i"
michael@0 156 done
michael@0 157 [ -n "$dep" ] && sudo apt-get -y install $dep
michael@0 158 sudo apt-get -y install schroot
michael@0 159
michael@0 160 # Create directory for chroot
michael@0 161 sudo mkdir -p /var/lib/chroot
michael@0 162
michael@0 163 # Find chroot environments that can be installed with debootstrap
michael@0 164 targets="$(cd /usr/share/debootstrap/scripts
michael@0 165 ls | grep '^[a-z]*$')"
michael@0 166
michael@0 167 # Ask user to pick one of the available targets
michael@0 168 echo "The following targets are available to be installed in a chroot:"
michael@0 169 j=1; for i in $targets; do
michael@0 170 printf '%4d: %s\n' "$j" "$i"
michael@0 171 j=$(($j+1))
michael@0 172 done
michael@0 173 while :; do
michael@0 174 printf "Which target would you like to install: "
michael@0 175 read n
michael@0 176 [ "$n" -gt 0 -a "$n" -lt "$j" ] >&/dev/null && break
michael@0 177 done
michael@0 178 j=1; for i in $targets; do
michael@0 179 [ "$j" -eq "$n" ] && { distname="$i"; break; }
michael@0 180 j=$(($j+1))
michael@0 181 done
michael@0 182 echo
michael@0 183
michael@0 184 # On x86-64, ask whether the user wants to install x86-32 or x86-64
michael@0 185 archflag=
michael@0 186 arch=
michael@0 187 if [ "$(uname -m)" = x86_64 ]; then
michael@0 188 while :; do
michael@0 189 echo "You are running a 64bit kernel. This allows you to install either a"
michael@0 190 printf "32bit or a 64bit chroot environment. %s" \
michael@0 191 "Which one do you want (32, 64) "
michael@0 192 read arch
michael@0 193 [ "${arch}" == 32 -o "${arch}" == 64 ] && break
michael@0 194 done
michael@0 195 [ "${arch}" == 32 ] && archflag="--arch i386" || archflag="--arch amd64"
michael@0 196 arch="${arch}bit"
michael@0 197 echo
michael@0 198 fi
michael@0 199 target="${distname}${arch}"
michael@0 200
michael@0 201 # Don't accidentally overwrite an existing installation
michael@0 202 [ -d /var/lib/chroot/"${target}" ] && {
michael@0 203 while :; do
michael@0 204 echo "This chroot already exists on your machine."
michael@0 205 if schroot -l --all-sessions 2>&1 |
michael@0 206 sed 's/^session://' |
michael@0 207 grep -qs "^${target%bit}-"; then
michael@0 208 echo "And it appears to be in active use. Terminate all programs that"
michael@0 209 echo "are currently using the chroot environment and then re-run this"
michael@0 210 echo "script."
michael@0 211 echo "If you still get an error message, you might have stale mounts"
michael@0 212 echo "that you forgot to delete. You can always clean up mounts by"
michael@0 213 echo "executing \"${target%bit} -c\"."
michael@0 214 exit 1
michael@0 215 fi
michael@0 216 echo "I can abort installation, I can overwrite the existing chroot,"
michael@0 217 echo "or I can delete the old one and then exit. What would you like to"
michael@0 218 printf "do (a/o/d)? "
michael@0 219 read choice
michael@0 220 case "${choice}" in
michael@0 221 a|A) exit 1;;
michael@0 222 o|O) sudo rm -rf "/var/lib/chroot/${target}"; break;;
michael@0 223 d|D) sudo rm -rf "/var/lib/chroot/${target}" \
michael@0 224 "/usr/local/bin/${target%bit}" \
michael@0 225 "/etc/schroot/mount-${target}" \
michael@0 226 "/etc/schroot/script-${target}"
michael@0 227 sudo sed -ni '/^[[]'"${target%bit}"']$/,${
michael@0 228 :1;n;/^[[]/b2;b1;:2;p;n;b2};p' \
michael@0 229 "/etc/schroot/schroot.conf"
michael@0 230 trap '' INT TERM QUIT HUP
michael@0 231 trap '' EXIT
michael@0 232 echo "Deleted!"
michael@0 233 exit 0;;
michael@0 234 esac
michael@0 235 done
michael@0 236 echo
michael@0 237 }
michael@0 238 sudo mkdir -p /var/lib/chroot/"${target}"
michael@0 239
michael@0 240 # Offer to include additional standard repositories for Ubuntu-based chroots.
michael@0 241 alt_repos=
michael@0 242 grep -qs ubuntu.com /usr/share/debootstrap/scripts/"${distname}" && {
michael@0 243 while :; do
michael@0 244 echo "Would you like to add ${distname}-updates and ${distname}-security "
michael@0 245 printf "to the chroot's sources.list (y/n)? "
michael@0 246 read alt_repos
michael@0 247 case "${alt_repos}" in
michael@0 248 y|Y)
michael@0 249 alt_repos="y"
michael@0 250 break
michael@0 251 ;;
michael@0 252 n|N)
michael@0 253 break
michael@0 254 ;;
michael@0 255 esac
michael@0 256 done
michael@0 257 echo
michael@0 258 }
michael@0 259
michael@0 260 # Check for non-standard file system mount points and ask the user whether
michael@0 261 # they should be imported into the chroot environment
michael@0 262 # We limit to the first 26 mount points that much some basic heuristics,
michael@0 263 # because a) that allows us to enumerate choices with a single character,
michael@0 264 # and b) if we find more than 26 mount points, then these are probably
michael@0 265 # false-positives and something is very unusual about the system's
michael@0 266 # configuration. No need to spam the user with even more information that
michael@0 267 # is likely completely irrelevant.
michael@0 268 if [ -z "${bind_mounts}" ]; then
michael@0 269 mounts="$(awk '$2 != "/" && $2 !~ "^/boot" && $2 !~ "^/home" &&
michael@0 270 $2 !~ "^/media" && $2 !~ "^/run" &&
michael@0 271 ($3 ~ "ext[2-4]" || $3 == "reiserfs" || $3 == "btrfs" ||
michael@0 272 $3 == "xfs" || $3 == "jfs" || $3 == "u?msdos" ||
michael@0 273 $3 == "v?fat" || $3 == "hfs" || $3 == "ntfs" ||
michael@0 274 $3 ~ "nfs[4-9]?" || $3 == "smbfs" || $3 == "cifs") {
michael@0 275 print $2
michael@0 276 }' /proc/mounts |
michael@0 277 head -n26)"
michael@0 278 if [ -n "${mounts}" ]; then
michael@0 279 echo "You appear to have non-standard mount points that you"
michael@0 280 echo "might want to import into the chroot environment:"
michael@0 281 echo
michael@0 282 sel=
michael@0 283 while :; do
michael@0 284 # Print a menu, listing all non-default mounts of local or network
michael@0 285 # file systems.
michael@0 286 j=1; for m in ${mounts}; do
michael@0 287 c="$(printf $(printf '\\%03o' $((64+$j))))"
michael@0 288 echo "$sel" | grep -qs $c &&
michael@0 289 state="mounted in chroot" || state="$(tput el)"
michael@0 290 printf " $c) %-40s${state}\n" "$m"
michael@0 291 j=$(($j+1))
michael@0 292 done
michael@0 293 # Allow user to interactively (de-)select any of the entries
michael@0 294 echo
michael@0 295 printf "Select mount points that you want to be included or press %s" \
michael@0 296 "SPACE to continue"
michael@0 297 c="$(getkey | tr a-z A-Z)"
michael@0 298 [ "$c" == " " ] && { echo; echo; break; }
michael@0 299 if [ -z "$c" ] ||
michael@0 300 [ "$c" '<' 'A' -o $(ord "$c") -gt $((64 + $(ord "$j"))) ]; then
michael@0 301 # Invalid input, ring the console bell
michael@0 302 tput bel
michael@0 303 else
michael@0 304 # Toggle the selection for the given entry
michael@0 305 if echo "$sel" | grep -qs $c; then
michael@0 306 sel="$(printf "$sel" | sed "s/$c//")"
michael@0 307 else
michael@0 308 sel="$sel$c"
michael@0 309 fi
michael@0 310 fi
michael@0 311 # Reposition cursor to the top of the list of entries
michael@0 312 tput cuu $(($j + 1))
michael@0 313 echo
michael@0 314 done
michael@0 315 fi
michael@0 316 j=1; for m in ${mounts}; do
michael@0 317 c="$(chr $(($j + 64)))"
michael@0 318 if echo "$sel" | grep -qs $c; then
michael@0 319 bind_mounts="${bind_mounts}$m $m none rw,bind 0 0
michael@0 320 "
michael@0 321 fi
michael@0 322 j=$(($j+1))
michael@0 323 done
michael@0 324 fi
michael@0 325
michael@0 326 # Remove stale entry from /etc/schroot/schroot.conf. Entries start
michael@0 327 # with the target name in square brackets, followed by an arbitrary
michael@0 328 # number of lines. The entry stops when either the end of file has
michael@0 329 # been reached, or when the beginning of a new target is encountered.
michael@0 330 # This means, we cannot easily match for a range of lines in
michael@0 331 # "sed". Instead, we actually have to iterate over each line and check
michael@0 332 # whether it is the beginning of a new entry.
michael@0 333 sudo sed -ni '/^[[]'"${target%bit}"']$/,${:1;n;/^[[]/b2;b1;:2;p;n;b2};p' \
michael@0 334 /etc/schroot/schroot.conf
michael@0 335
michael@0 336 # Download base system. This takes some time
michael@0 337 if [ -z "${mirror}" ]; then
michael@0 338 grep -qs ubuntu.com /usr/share/debootstrap/scripts/"${distname}" &&
michael@0 339 mirror="http://archive.ubuntu.com/ubuntu" ||
michael@0 340 mirror="http://ftp.us.debian.org/debian"
michael@0 341 fi
michael@0 342
michael@0 343 sudo ${http_proxy:+http_proxy="${http_proxy}"} debootstrap ${archflag} \
michael@0 344 "${distname}" "/var/lib/chroot/${target}" "$mirror"
michael@0 345
michael@0 346 # Add new entry to /etc/schroot/schroot.conf
michael@0 347 grep -qs ubuntu.com /usr/share/debootstrap/scripts/"${distname}" &&
michael@0 348 brand="Ubuntu" || brand="Debian"
michael@0 349 if [ -z "${chroot_groups}" ]; then
michael@0 350 chroot_groups="${admin},$(id -gn)"
michael@0 351 fi
michael@0 352 # Older versions of schroot wanted a "priority=" line, whereas recent
michael@0 353 # versions deprecate "priority=" and warn if they see it. We don't have
michael@0 354 # a good feature test, but scanning for the string "priority=" in the
michael@0 355 # existing "schroot.conf" file is a good indication of what to do.
michael@0 356 priority=$(grep -qs 'priority=' /etc/schroot/schroot.conf &&
michael@0 357 echo 'priority=3' || :)
michael@0 358 sudo sh -c 'cat >>/etc/schroot/schroot.conf' <<EOF
michael@0 359 [${target%bit}]
michael@0 360 description=${brand} ${distname} ${arch}
michael@0 361 type=directory
michael@0 362 directory=/var/lib/chroot/${target}
michael@0 363 users=root
michael@0 364 groups=${chroot_groups}
michael@0 365 root-groups=${chroot_groups}
michael@0 366 personality=linux$([ "${arch}" != 64bit ] && echo 32)
michael@0 367 script-config=script-${target}
michael@0 368 ${priority}
michael@0 369
michael@0 370 EOF
michael@0 371
michael@0 372 # Set up a list of mount points that is specific to this
michael@0 373 # chroot environment.
michael@0 374 sed '/^FSTAB=/s,"[^"]*","/etc/schroot/mount-'"${target}"'",' \
michael@0 375 /etc/schroot/script-defaults |
michael@0 376 sudo sh -c 'cat >/etc/schroot/script-'"${target}"
michael@0 377 sed '\,^/home[/[:space:]],s/\([,[:space:]]\)bind[[:space:]]/\1rbind /' \
michael@0 378 /etc/schroot/mount-defaults |
michael@0 379 sudo sh -c 'cat > /etc/schroot/mount-'"${target}"
michael@0 380
michael@0 381 # Add the extra mount points that the user told us about
michael@0 382 [ -n "${bind_mounts}" -a "${bind_mounts}" != "NONE" ] &&
michael@0 383 printf "${bind_mounts}" |
michael@0 384 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}"
michael@0 385
michael@0 386 # If this system has a "/media" mountpoint, import it into the chroot
michael@0 387 # environment. Most modern distributions use this mount point to
michael@0 388 # automatically mount devices such as CDROMs, USB sticks, etc...
michael@0 389 if [ -d /media ] &&
michael@0 390 ! grep -qs '^/media' /etc/schroot/mount-"${target}"; then
michael@0 391 echo '/media /media none rw,rbind 0 0' |
michael@0 392 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}"
michael@0 393 fi
michael@0 394
michael@0 395 # Share /dev/shm and possibly /run/shm
michael@0 396 grep -qs '^/dev/shm' /etc/schroot/mount-"${target}" ||
michael@0 397 echo '/dev/shm /dev/shm none rw,bind 0 0' |
michael@0 398 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}"
michael@0 399 if [ -d "/var/lib/chroot/${target}/run" ] &&
michael@0 400 ! grep -qs '^/run/shm' /etc/schroot/mount-"${target}"; then
michael@0 401 { [ -d /run ] && echo '/run/shm /run/shm none rw,bind 0 0' ||
michael@0 402 echo '/dev/shm /run/shm none rw,bind 0 0'; } |
michael@0 403 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}"
michael@0 404 fi
michael@0 405
michael@0 406 # Set up a special directory that changes contents depending on the target
michael@0 407 # that is executing.
michael@0 408 d="$(readlink -f "${HOME}/chroot" 2>/dev/null || echo "${HOME}/chroot")"
michael@0 409 s="${d}/.${target}"
michael@0 410 echo "${s} ${d} none rw,bind 0 0" |
michael@0 411 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}"
michael@0 412 mkdir -p "${s}"
michael@0 413
michael@0 414 # Install a helper script to launch commands in the chroot
michael@0 415 sudo sh -c 'cat >/usr/local/bin/'"${target%bit}" <<'EOF'
michael@0 416 #!/bin/bash
michael@0 417
michael@0 418 chroot="${0##*/}"
michael@0 419
michael@0 420 wrap() {
michael@0 421 # Word-wrap the text passed-in on stdin. Optionally, on continuation lines
michael@0 422 # insert the same number of spaces as the number of characters in the
michael@0 423 # parameter(s) passed to this function.
michael@0 424 # If the "fold" program cannot be found, or if the actual width of the
michael@0 425 # terminal cannot be determined, this function doesn't attempt to do any
michael@0 426 # wrapping.
michael@0 427 local f="$(type -P fold)"
michael@0 428 [ -z "${f}" ] && { cat; return; }
michael@0 429 local c="$(stty -a </dev/tty 2>/dev/null |
michael@0 430 sed 's/.*columns[[:space:]]*\([0-9]*\).*/\1/;t;d')"
michael@0 431 [ -z "${c}" ] && { cat; return; }
michael@0 432 local i="$(echo "$*"|sed 's/./ /g')"
michael@0 433 local j="$(printf %s "${i}"|wc -c)"
michael@0 434 if [ "${c}" -gt "${j}" ]; then
michael@0 435 dd bs=1 count="${j}" 2>/dev/null
michael@0 436 "${f}" -sw "$((${c}-${j}))" | sed '2,$s/^/'"${i}"'/'
michael@0 437 else
michael@0 438 "${f}" -sw "${c}"
michael@0 439 fi
michael@0 440 }
michael@0 441
michael@0 442 help() {
michael@0 443 echo "Usage ${0##*/} [-h|--help] [-c|--clean] [-C|--clean-all] [-l|--list] [--] args" | wrap "Usage ${0##*/} "
michael@0 444 echo " help: print this message" | wrap " "
michael@0 445 echo " list: list all known chroot environments" | wrap " "
michael@0 446 echo " clean: remove all old chroot sessions for \"${chroot}\"" | wrap " "
michael@0 447 echo " clean-all: remove all old chroot sessions for all environments" | wrap " "
michael@0 448 exit 0
michael@0 449 }
michael@0 450
michael@0 451 clean() {
michael@0 452 local s t rc
michael@0 453 rc=0
michael@0 454 for s in $(schroot -l --all-sessions); do
michael@0 455 if [ -n "$1" ]; then
michael@0 456 t="${s#session:}"
michael@0 457 [ "${t#${chroot}-}" == "${t}" ] && continue
michael@0 458 fi
michael@0 459 if ls -l /proc/*/{cwd,fd} 2>/dev/null |
michael@0 460 fgrep -qs "/var/lib/schroot/mount/${t}"; then
michael@0 461 echo "Session \"${t}\" still has active users, not cleaning up" | wrap
michael@0 462 rc=1
michael@0 463 continue
michael@0 464 fi
michael@0 465 sudo schroot -c "${s}" -e || rc=1
michael@0 466 done
michael@0 467 exit ${rc}
michael@0 468 }
michael@0 469
michael@0 470 list() {
michael@0 471 for e in $(schroot -l); do
michael@0 472 e="${e#chroot:}"
michael@0 473 [ -x "/usr/local/bin/${e}" ] || continue
michael@0 474 if schroot -l --all-sessions 2>/dev/null |
michael@0 475 sed 's/^session://' |
michael@0 476 grep -qs "^${e}-"; then
michael@0 477 echo "${e} is currently active"
michael@0 478 else
michael@0 479 echo "${e}"
michael@0 480 fi
michael@0 481 done
michael@0 482 exit 0
michael@0 483 }
michael@0 484
michael@0 485 while [ "$#" -ne 0 ]; do
michael@0 486 case "$1" in
michael@0 487 --) shift; break;;
michael@0 488 -h|--help) shift; help;;
michael@0 489 -l|--list) shift; list;;
michael@0 490 -c|--clean) shift; clean "${chroot}";;
michael@0 491 -C|--clean-all) shift; clean;;
michael@0 492 *) break;;
michael@0 493 esac
michael@0 494 done
michael@0 495
michael@0 496 session="$(schroot -c "${chroot}" -b)"
michael@0 497
michael@0 498 if [ $# -eq 0 ]; then
michael@0 499 schroot -c "${session}" -r -p
michael@0 500 else
michael@0 501 p="$1"; shift
michael@0 502 schroot -c "${session}" -r -p "$p" -- "$@"
michael@0 503 fi
michael@0 504 rc=$?
michael@0 505
michael@0 506 i=$(schroot -c "${session}" -r -p ls -- -id /proc/self/root/. |
michael@0 507 awk '{ print $1 }') 2>/dev/null
michael@0 508 while [ -n "$i" ]; do
michael@0 509 pids=$(ls -id1 /proc/*/root/. 2>/dev/null |
michael@0 510 sed -e 's,^[^0-9]*'$i'.*/\([1-9][0-9]*\)/.*$,\1,
michael@0 511 t
michael@0 512 d') >/dev/null 2>&1
michael@0 513 [ -z "$pids" ] && break
michael@0 514 kill -9 $pids
michael@0 515 done
michael@0 516 schroot -c "${session}" -e
michael@0 517 exit $rc
michael@0 518 EOF
michael@0 519 sudo chown root:root /usr/local/bin/"${target%bit}"
michael@0 520 sudo chmod 755 /usr/local/bin/"${target%bit}"
michael@0 521
michael@0 522 # Add the standard Ubuntu update repositories if requested.
michael@0 523 [ "${alt_repos}" = "y" -a \
michael@0 524 -r "/var/lib/chroot/${target}/etc/apt/sources.list" ] &&
michael@0 525 sudo sed -i '/^deb .* [^ -]\+ main$/p
michael@0 526 s/^\(deb .* [^ -]\+\) main/\1-security main/
michael@0 527 p
michael@0 528 t1
michael@0 529 d
michael@0 530 :1;s/-security main/-updates main/
michael@0 531 t
michael@0 532 d' "/var/lib/chroot/${target}/etc/apt/sources.list"
michael@0 533
michael@0 534 # Add a few more repositories to the chroot
michael@0 535 [ -r "/var/lib/chroot/${target}/etc/apt/sources.list" ] &&
michael@0 536 sudo sed -i 's/ main$/ main restricted universe multiverse/' \
michael@0 537 "/var/lib/chroot/${target}/etc/apt/sources.list"
michael@0 538
michael@0 539 # Add the Ubuntu "partner" repository, if available
michael@0 540 if [ -r "/var/lib/chroot/${target}/etc/apt/sources.list" ] &&
michael@0 541 HEAD "http://archive.canonical.com/ubuntu/dists/${distname}/partner" \
michael@0 542 >&/dev/null; then
michael@0 543 sudo sh -c '
michael@0 544 echo "deb http://archive.canonical.com/ubuntu" \
michael@0 545 "'"${distname}"' partner" \
michael@0 546 >>"/var/lib/chroot/'"${target}"'/etc/apt/sources.list"'
michael@0 547 fi
michael@0 548
michael@0 549 # Add source repositories, if the user requested we do so
michael@0 550 [ "${add_srcs}" = "y" -a \
michael@0 551 -r "/var/lib/chroot/${target}/etc/apt/sources.list" ] &&
michael@0 552 sudo sed -i '/^deb[^-]/p
michael@0 553 s/^deb\([^-]\)/deb-src\1/' \
michael@0 554 "/var/lib/chroot/${target}/etc/apt/sources.list"
michael@0 555
michael@0 556 # Set apt proxy if host has set http_proxy
michael@0 557 if [ -n "${http_proxy}" ]; then
michael@0 558 sudo sh -c '
michael@0 559 echo "Acquire::http::proxy \"'"${http_proxy}"'\";" \
michael@0 560 >>"/var/lib/chroot/'"${target}"'/etc/apt/apt.conf"'
michael@0 561 fi
michael@0 562
michael@0 563 # Update packages
michael@0 564 sudo "/usr/local/bin/${target%bit}" /bin/sh -c '
michael@0 565 apt-get update; apt-get -y dist-upgrade' || :
michael@0 566
michael@0 567 # Install a couple of missing packages
michael@0 568 for i in debian-keyring ubuntu-keyring locales sudo; do
michael@0 569 [ -d "/var/lib/chroot/${target}/usr/share/doc/$i" ] ||
michael@0 570 sudo "/usr/local/bin/${target%bit}" apt-get -y install "$i" || :
michael@0 571 done
michael@0 572
michael@0 573 # Configure locales
michael@0 574 sudo "/usr/local/bin/${target%bit}" /bin/sh -c '
michael@0 575 l='"${LANG:-en_US}"'; l="${l%%.*}"
michael@0 576 [ -r /etc/locale.gen ] &&
michael@0 577 sed -i "s/^# \($l\)/\1/" /etc/locale.gen
michael@0 578 locale-gen $LANG en_US en_US.UTF-8' || :
michael@0 579
michael@0 580 # Enable multi-arch support, if available
michael@0 581 sudo "/usr/local/bin/${target%bit}" dpkg --assert-multi-arch >&/dev/null &&
michael@0 582 [ -r "/var/lib/chroot/${target}/etc/apt/sources.list" ] && {
michael@0 583 sudo sed -i 's/ / [arch=amd64,i386] /' \
michael@0 584 "/var/lib/chroot/${target}/etc/apt/sources.list"
michael@0 585 [ -d /var/lib/chroot/${target}/etc/dpkg/dpkg.cfg.d/ ] &&
michael@0 586 echo foreign-architecture \
michael@0 587 $([ "${arch}" = "32bit" ] && echo amd64 || echo i386) |
michael@0 588 sudo sh -c "cat >'/var/lib/chroot/${target}/etc/dpkg/dpkg.cfg.d/multiarch'"
michael@0 589 }
michael@0 590
michael@0 591 # Configure "sudo" package
michael@0 592 sudo "/usr/local/bin/${target%bit}" /bin/sh -c '
michael@0 593 egrep -qs '"'^$(id -nu) '"' /etc/sudoers ||
michael@0 594 echo '"'$(id -nu) ALL=(ALL) ALL'"' >>/etc/sudoers'
michael@0 595
michael@0 596 # Install a few more commonly used packages
michael@0 597 sudo "/usr/local/bin/${target%bit}" apt-get -y install \
michael@0 598 autoconf automake1.9 dpkg-dev g++-multilib gcc-multilib gdb less libtool \
michael@0 599 strace
michael@0 600
michael@0 601 # If running a 32bit environment on a 64bit machine, install a few binaries
michael@0 602 # as 64bit. This is only done automatically if the chroot distro is the same as
michael@0 603 # the host, otherwise there might be incompatibilities in build settings or
michael@0 604 # runtime dependencies. The user can force it with the '-c' flag.
michael@0 605 host_distro=$(grep -s DISTRIB_CODENAME /etc/lsb-release | \
michael@0 606 cut -d "=" -f 2)
michael@0 607 if [ "${copy_64}" = "y" -o \
michael@0 608 "${host_distro}" = "${distname}" -a "${arch}" = 32bit ] && \
michael@0 609 file /bin/bash 2>/dev/null | grep -q x86-64; then
michael@0 610 readlinepkg=$(sudo "/usr/local/bin/${target%bit}" sh -c \
michael@0 611 'apt-cache search "lib64readline.\$" | sort | tail -n 1 | cut -d " " -f 1')
michael@0 612 sudo "/usr/local/bin/${target%bit}" apt-get -y install \
michael@0 613 lib64expat1 lib64ncurses5 ${readlinepkg} lib64z1
michael@0 614 dep=
michael@0 615 for i in binutils gdb; do
michael@0 616 [ -d /usr/share/doc/"$i" ] || dep="$dep $i"
michael@0 617 done
michael@0 618 [ -n "$dep" ] && sudo apt-get -y install $dep
michael@0 619 sudo mkdir -p "/var/lib/chroot/${target}/usr/local/lib/amd64"
michael@0 620 for i in libbfd libpython; do
michael@0 621 lib="$({ ldd /usr/bin/ld; ldd /usr/bin/gdb; } |
michael@0 622 grep -s "$i" | awk '{ print $3 }')"
michael@0 623 if [ -n "$lib" -a -r "$lib" ]; then
michael@0 624 sudo cp "$lib" "/var/lib/chroot/${target}/usr/local/lib/amd64"
michael@0 625 fi
michael@0 626 done
michael@0 627 for lib in libssl libcrypt; do
michael@0 628 for path in /usr/lib /usr/lib/x86_64-linux-gnu; do
michael@0 629 sudo cp $path/$lib* \
michael@0 630 "/var/lib/chroot/${target}/usr/local/lib/amd64/" >&/dev/null || :
michael@0 631 done
michael@0 632 done
michael@0 633 for i in gdb ld; do
michael@0 634 sudo cp /usr/bin/$i "/var/lib/chroot/${target}/usr/local/lib/amd64/"
michael@0 635 sudo sh -c "cat >'/var/lib/chroot/${target}/usr/local/bin/$i'" <<EOF
michael@0 636 #!/bin/sh
michael@0 637 exec /lib64/ld-linux-x86-64.so.2 --library-path /usr/local/lib/amd64 \
michael@0 638 /usr/local/lib/amd64/$i "\$@"
michael@0 639 EOF
michael@0 640 sudo chmod 755 "/var/lib/chroot/${target}/usr/local/bin/$i"
michael@0 641 done
michael@0 642 fi
michael@0 643
michael@0 644
michael@0 645 # If the install-build-deps.sh script can be found, offer to run it now
michael@0 646 script="$(dirname $(readlink -f "$0"))/install-build-deps.sh"
michael@0 647 if [ -x "${script}" ]; then
michael@0 648 while :; do
michael@0 649 echo
michael@0 650 echo "If you plan on building Chrome inside of the new chroot environment,"
michael@0 651 echo "you now have to install the build dependencies. Do you want me to"
michael@0 652 printf "start the script that does this for you (y/n)? "
michael@0 653 read install_deps
michael@0 654 case "${install_deps}" in
michael@0 655 y|Y)
michael@0 656 echo
michael@0 657 # We prefer running the script in-place, but this might not be
michael@0 658 # possible, if it lives on a network filesystem that denies
michael@0 659 # access to root.
michael@0 660 tmp_script=
michael@0 661 if ! sudo /usr/local/bin/"${target%bit}" \
michael@0 662 sh -c "[ -x '${script}' ]" >&/dev/null; then
michael@0 663 tmp_script="/tmp/${script##*/}"
michael@0 664 cp "${script}" "${tmp_script}"
michael@0 665 fi
michael@0 666 # Some distributions automatically start an instance of the system-
michael@0 667 # wide dbus daemon, cron daemon or of the logging daemon, when
michael@0 668 # installing the Chrome build depencies. This prevents the chroot
michael@0 669 # session from being closed. So, we always try to shut down any running
michael@0 670 # instance of dbus and rsyslog.
michael@0 671 sudo /usr/local/bin/"${target%bit}" sh -c "${script} --no-lib32;
michael@0 672 rc=$?;
michael@0 673 /etc/init.d/cron stop >/dev/null 2>&1 || :;
michael@0 674 /etc/init.d/rsyslog stop >/dev/null 2>&1 || :;
michael@0 675 /etc/init.d/dbus stop >/dev/null 2>&1 || :;
michael@0 676 exit $rc"
michael@0 677 rc=$?
michael@0 678 [ -n "${tmp_script}" ] && rm -f "${tmp_script}"
michael@0 679 [ $rc -ne 0 ] && exit $rc
michael@0 680 break
michael@0 681 ;;
michael@0 682 n|N)
michael@0 683 break
michael@0 684 ;;
michael@0 685 esac
michael@0 686 done
michael@0 687 echo
michael@0 688 fi
michael@0 689
michael@0 690 # Check whether ~/chroot is on a (slow) network file system and offer to
michael@0 691 # relocate it. Also offer relocation, if the user appears to have multiple
michael@0 692 # spindles (as indicated by "${bind_mount}" being non-empty).
michael@0 693 # We only offer this option, if it doesn't look as if a chroot environment
michael@0 694 # is currently active. Otherwise, relocation is unlikely to work and it
michael@0 695 # can be difficult for the user to recover from the failed attempt to relocate
michael@0 696 # the ~/chroot directory.
michael@0 697 # We don't aim to solve this problem for every configuration,
michael@0 698 # but try to help with the common cases. For more advanced configuration
michael@0 699 # options, the user can always manually adjust things.
michael@0 700 mkdir -p "${HOME}/chroot/"
michael@0 701 if [ ! -h "${HOME}/chroot" ] &&
michael@0 702 ! egrep -qs '^[^[:space:]]*/chroot' /etc/fstab &&
michael@0 703 { [ -n "${bind_mounts}" -a "${bind_mounts}" != "NONE" ] ||
michael@0 704 is_network_drive "${HOME}/chroot"; } &&
michael@0 705 ! egrep -qs '/var/lib/[^/]*chroot/.*/chroot' /proc/mounts; then
michael@0 706 echo "${HOME}/chroot is currently located on the same device as your"
michael@0 707 echo "home directory."
michael@0 708 echo "This might not be what you want. Do you want me to move it somewhere"
michael@0 709 echo "else?"
michael@0 710 # If the computer has multiple spindles, many users configure all or part of
michael@0 711 # the secondary hard disk to be writable by the primary user of this machine.
michael@0 712 # Make some reasonable effort to detect this type of configuration and
michael@0 713 # then offer a good location for where to put the ~/chroot directory.
michael@0 714 suggest=
michael@0 715 for i in $(echo "${bind_mounts}"|cut -d ' ' -f 1); do
michael@0 716 if [ -d "$i" -a -w "$i" -a \( ! -a "$i/chroot" -o -w "$i/chroot/." \) ] &&
michael@0 717 ! is_network_drive "$i"; then
michael@0 718 suggest="$i"
michael@0 719 else
michael@0 720 for j in "$i/"*; do
michael@0 721 if [ -d "$j" -a -w "$j" -a \
michael@0 722 \( ! -a "$j/chroot" -o -w "$j/chroot/." \) ] &&
michael@0 723 ! is_network_drive "$j"; then
michael@0 724 suggest="$j"
michael@0 725 else
michael@0 726 for k in "$j/"*; do
michael@0 727 if [ -d "$k" -a -w "$k" -a \
michael@0 728 \( ! -a "$k/chroot" -o -w "$k/chroot/." \) ] &&
michael@0 729 ! is_network_drive "$k"; then
michael@0 730 suggest="$k"
michael@0 731 break
michael@0 732 fi
michael@0 733 done
michael@0 734 fi
michael@0 735 [ -n "${suggest}" ] && break
michael@0 736 done
michael@0 737 fi
michael@0 738 [ -n "${suggest}" ] && break
michael@0 739 done
michael@0 740 def_suggest="${HOME}"
michael@0 741 if [ -n "${suggest}" ]; then
michael@0 742 # For home directories that reside on network drives, make our suggestion
michael@0 743 # the default option. For home directories that reside on a local drive,
michael@0 744 # require that the user manually enters the new location.
michael@0 745 if is_network_drive "${HOME}"; then
michael@0 746 def_suggest="${suggest}"
michael@0 747 else
michael@0 748 echo "A good location would probably be in \"${suggest}\""
michael@0 749 fi
michael@0 750 fi
michael@0 751 while :; do
michael@0 752 printf "Physical location [${def_suggest}]: "
michael@0 753 read dir
michael@0 754 [ -z "${dir}" ] && dir="${def_suggest}"
michael@0 755 [ "${dir%%/}" == "${HOME%%/}" ] && break
michael@0 756 if ! [ -d "${dir}" -a -w "${dir}" ] ||
michael@0 757 [ -a "${dir}/chroot" -a ! -w "${dir}/chroot/." ]; then
michael@0 758 echo "Cannot write to ${dir}/chroot. Please try again"
michael@0 759 else
michael@0 760 mv "${HOME}/chroot" "${dir}/chroot"
michael@0 761 ln -s "${dir}/chroot" "${HOME}/chroot"
michael@0 762 for i in $(list_all_chroots); do
michael@0 763 sudo "$i" mkdir -p "${dir}/chroot"
michael@0 764 done
michael@0 765 sudo sed -i "s,${HOME}/chroot,${dir}/chroot,g" /etc/schroot/mount-*
michael@0 766 break
michael@0 767 fi
michael@0 768 done
michael@0 769 fi
michael@0 770
michael@0 771 # Clean up package files
michael@0 772 sudo schroot -c /usr/local/bin/"${target%bit}" -p -- apt-get clean
michael@0 773 sudo apt-get clean
michael@0 774
michael@0 775 trap '' INT TERM QUIT HUP
michael@0 776 trap '' EXIT
michael@0 777
michael@0 778 # Let the user know what we did
michael@0 779 cat <<EOF
michael@0 780
michael@0 781
michael@0 782 Successfully installed ${distname} ${arch}
michael@0 783
michael@0 784 You can run programs inside of the chroot by invoking the
michael@0 785 "/usr/local/bin/${target%bit}" command.
michael@0 786
michael@0 787 This command can be used with arguments, in order to just run a single
michael@0 788 program inside of the chroot environment (e.g. "${target%bit} make chrome")
michael@0 789 or without arguments, in order to run an interactive shell session inside
michael@0 790 of the chroot environment.
michael@0 791
michael@0 792 If you need to run things as "root", you can use "sudo" (e.g. try
michael@0 793 "sudo ${target%bit} apt-get update").
michael@0 794
michael@0 795 Your home directory is shared between the host and the chroot. But I
michael@0 796 configured "${HOME}/chroot" to be private to the chroot environment.
michael@0 797 You can use it for files that need to differ between environments. This
michael@0 798 would be a good place to store binaries that you have built from your
michael@0 799 source files.
michael@0 800
michael@0 801 For Chrome, this probably means you want to make your "out" directory a
michael@0 802 symbolic link that points somewhere inside of "${HOME}/chroot".
michael@0 803
michael@0 804 You still need to run "gclient runhooks" whenever you switch from building
michael@0 805 outside of the chroot to inside of the chroot. But you will find that you
michael@0 806 don't have to repeatedly erase and then completely rebuild all your object
michael@0 807 and binary files.
michael@0 808
michael@0 809 EOF

mercurial