OLD | NEW |
(Empty) | |
| 1 #!/bin/bash -e |
| 2 |
| 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # This script installs Debian-derived distributions in a chroot environment. |
| 8 # It can for example be used to have an accurate 32bit build and test |
| 9 # environment when otherwise working on a 64bit machine. |
| 10 # N. B. it is unlikely that this script will ever work on anything other than a |
| 11 # Debian-derived system. |
| 12 |
| 13 # Check that we are running as a regular user |
| 14 [ "$(id -nu)" = root ] && { |
| 15 echo "Run this script as a regular user and provide your \"sudo\"" \ |
| 16 "password if requested" >&2 |
| 17 exit 1 |
| 18 } |
| 19 mkdir -p "$HOME/chroot/" |
| 20 |
| 21 # Error handler |
| 22 trap 'exit 1' INT TERM QUIT |
| 23 trap 'sudo apt-get clean; tput bel; echo; echo Failed' EXIT |
| 24 |
| 25 # Install any missing applications that this script relies on. If these packages |
| 26 # are already installed, don't force another "apt-get install". That would |
| 27 # prevent them from being auto-removed, if they ever become eligible for that. |
| 28 # And as this script only needs the packages once, there is no good reason to |
| 29 # introduce a hard dependency on things such as dchroot and debootstrap. |
| 30 dep= |
| 31 for i in dchroot debootstrap; do |
| 32 [ -d /usr/share/doc/"$i" ] || dep="$dep $i" |
| 33 done |
| 34 [ -n "$dep" ] && sudo apt-get -y install $dep |
| 35 sudo apt-get -y install schroot |
| 36 |
| 37 # Create directory for chroot |
| 38 sudo mkdir -p /var/lib/chroot |
| 39 |
| 40 # Find chroot environments that can be installed with debootstrap |
| 41 targets="$(cd /usr/share/debootstrap/scripts |
| 42 ls | grep '^[a-z]*$')" |
| 43 |
| 44 # Ask user to pick one of the available targets |
| 45 echo "The following targets are available to be installed in a chroot:" |
| 46 j=1; for i in $targets; do |
| 47 printf '%4d: %s\n' "$j" "$i" |
| 48 j=$(($j+1)) |
| 49 done |
| 50 while :; do |
| 51 printf "Which target would you like to install: " |
| 52 read n |
| 53 [ "$n" -gt 0 -a "$n" -lt "$j" ] >&/dev/null && break |
| 54 done |
| 55 j=1; for i in $targets; do |
| 56 [ "$j" -eq "$n" ] && { distname="$i"; break; } |
| 57 j=$(($j+1)) |
| 58 done |
| 59 |
| 60 # On x86-64, ask whether the user wants to install x86-32 or x86-64 |
| 61 archflag= |
| 62 arch= |
| 63 if [ "$(uname -m)" = x86_64 ]; then |
| 64 while :; do |
| 65 echo "You are running a 64bit kernel. This allows you to install either a" |
| 66 printf "32bit or a 64bit chroot environment. %s" \ |
| 67 "Which one do you want (32, 64) " |
| 68 read arch |
| 69 [ "${arch}" == 32 -o "${arch}" == 64 ] && break |
| 70 done |
| 71 [ "${arch}" == 32 ] && archflag="--arch i386" || archflag="--arch amd64" |
| 72 arch="${arch}bit" |
| 73 fi |
| 74 target="${distname}${arch}" |
| 75 |
| 76 # Don't overwrite an existing installation |
| 77 [ -d /var/lib/chroot/"${target}" ] && { |
| 78 echo "This chroot already exists on your machine." >&2 |
| 79 echo "Delete /var/lib/chroot/${target} if you want to start over." >&2 |
| 80 exit 1 |
| 81 } |
| 82 sudo mkdir -p /var/lib/chroot/"${target}" |
| 83 |
| 84 # Remove stale entry from /etc/schroot/schroot.conf. Entries start |
| 85 # with the target name in square brackets, followed by an arbitrary |
| 86 # number of lines. The entry stops when either the end of file has |
| 87 # been reached, or when the beginning of a new target is encountered. |
| 88 # This means, we cannot easily match for a range of lines in |
| 89 # "sed". Instead, we actually have to iterate over each line and check |
| 90 # whether it is the beginning of a new entry. |
| 91 sudo sed -ni '/^[[]'"${target%bit}"']$/,${:1;n;/^[[]/b2;b1;:2;p;n;b2};p' \ |
| 92 /etc/schroot/schroot.conf |
| 93 |
| 94 # Download base system. This takes some time |
| 95 grep ubuntu.com /usr/share/debootstrap/scripts/"${distname}" >&/dev/null && |
| 96 mirror="http://archive.ubuntu.com/ubuntu" || |
| 97 mirror="http://ftp.us.debian.org/debian" |
| 98 sudo debootstrap ${archflag} "${distname}" /var/lib/chroot/"${target}" \ |
| 99 "$mirror" |
| 100 |
| 101 # Add new entry to /etc/schroot/schroot.conf |
| 102 grep ubuntu.com /usr/share/debootstrap/scripts/"${distname}" >&/dev/null && |
| 103 brand="Ubuntu" || brand="Debian" |
| 104 sudo sh -c 'cat >>/etc/schroot/schroot.conf' <<EOF |
| 105 [${target%bit}] |
| 106 description=${brand} ${distname} ${arch} |
| 107 type=directory |
| 108 directory=/var/lib/chroot/${target} |
| 109 priority=3 |
| 110 users=root |
| 111 groups=admin |
| 112 root-groups=admin |
| 113 personality=linux$([ "${arch}" != 64bit ] && echo 32) |
| 114 script-config=script-${target} |
| 115 |
| 116 EOF |
| 117 |
| 118 # Set up a special directory that changes contents depending on the target |
| 119 # that is executing. |
| 120 sed '/^FSTAB=/s,/mount-defaults",/mount-'"${target}"'",' \ |
| 121 /etc/schroot/script-defaults | |
| 122 sudo sh -c 'cat >/etc/schroot/script-'"${target}" |
| 123 sudo cp /etc/schroot/mount-defaults /etc/schroot/mount-"${target}" |
| 124 echo "$HOME/chroot/.${target} $HOME/chroot none rw,bind 0 0" | |
| 125 sudo sh -c 'cat >>/etc/schroot/mount-'"${target}" |
| 126 mkdir -p "$HOME/chroot/.${target}" |
| 127 |
| 128 # Install a helper script to launch commands in the chroot |
| 129 sudo sh -c 'cat >/usr/local/bin/'"${target%bit}" <<EOF |
| 130 #!/bin/bash |
| 131 if [ \$# -eq 0 ]; then |
| 132 exec schroot -c ${target%bit} -p |
| 133 else |
| 134 p="\$1"; shift |
| 135 exec schroot -c ${target%bit} -p "\$p" -- "\$@" |
| 136 fi |
| 137 exit 1 |
| 138 EOF |
| 139 sudo chown root:root /usr/local/bin/"${target%bit}" |
| 140 sudo chmod 755 /usr/local/bin/"${target%bit}" |
| 141 |
| 142 # Add a few more repositories to the chroot |
| 143 [ -r /var/lib/chroot/${target}/etc/apt/sources.list ] && |
| 144 sudo sed -i 's/ main$/ main restricted universe multiverse/ |
| 145 p |
| 146 t1 |
| 147 d |
| 148 :1;s/^deb/deb-src/ |
| 149 t |
| 150 d' /var/lib/chroot/${target}/etc/apt/sources.list |
| 151 |
| 152 # Update packages |
| 153 sudo schroot -c "${target%bit}" -p -- /bin/sh -c ' |
| 154 apt-get update; apt-get -y dist-upgrade' || : |
| 155 |
| 156 # Install a couple of missing packages |
| 157 for i in debian-keyring ubuntu-keyring locales sudo; do |
| 158 [ -d "/var/lib/chroot/${target}/usr/share/doc/$i" ] || |
| 159 sudo schroot -c "${target%bit}" -p -- apt-get -y install "$i" || : |
| 160 done |
| 161 |
| 162 # Configure locales |
| 163 sudo schroot -c "${target%bit}" -p -- /bin/sh -c ' |
| 164 l='"${LANG:-en_US}"'; l="${l%%.*}" |
| 165 [ -r /etc/locale.gen ] && |
| 166 sed -i "s/^# \($l\)/\1/" /etc/locale.gen |
| 167 locale-gen $LANG en_US en_US.UTF-8' || : |
| 168 |
| 169 # Configure "sudo" package |
| 170 sudo schroot -c "${target%bit}" -p -- /bin/sh -c ' |
| 171 egrep '"'^$(id -nu) '"' /etc/sudoers >/dev/null 2>&1 || |
| 172 echo '"'$(id -nu) ALL=(ALL) ALL'"' >>/etc/sudoers' |
| 173 |
| 174 # Install a few more commonly used packages |
| 175 sudo schroot -c "${target%bit}" -p -- apt-get -y install \ |
| 176 autoconf automake1.9 dpkg-dev g++-multilib gcc-multilib gdb less libtool \ |
| 177 strace |
| 178 |
| 179 # If running a 32bit environment on a 64bit machine, install a few binaries |
| 180 # as 64bit. |
| 181 if [ "${arch}" = 32bit ] && file /bin/bash 2>/dev/null | grep -q x86-64; then |
| 182 sudo schroot -c "${target%bit}" -p -- apt-get -y install \ |
| 183 lib64expat1 lib64ncurses5 lib64readline6 lib64z1 |
| 184 dep= |
| 185 for i in binutils gdb strace; do |
| 186 [ -d /usr/share/doc/"$i" ] || dep="$dep $i" |
| 187 done |
| 188 [ -n "$dep" ] && sudo apt-get -y install $dep |
| 189 sudo cp /usr/bin/gdb /var/lib/chroot/${target}/usr/local/bin/ |
| 190 sudo cp /usr/bin/ld /var/lib/chroot/${target}/usr/local/bin/ |
| 191 for i in libbfd libpython; do |
| 192 lib="$({ ldd /usr/bin/ld; ldd /usr/bin/gdb; } | |
| 193 grep "$i" | awk '{ print $3 }')" |
| 194 if [ -n "$lib" -a -r "$lib" ]; then |
| 195 sudo cp "$lib" /var/lib/chroot/${target}/usr/lib64/ |
| 196 fi |
| 197 done |
| 198 for lib in libssl libcrypt; do |
| 199 sudo cp /usr/lib/$lib* /var/lib/chroot/${target}/usr/lib64/ || : |
| 200 done |
| 201 fi |
| 202 |
| 203 # Clean up package files |
| 204 sudo schroot -c "${target%bit}" -p -- apt-get clean |
| 205 sudo apt-get clean |
| 206 |
| 207 # Let the user know what we did |
| 208 trap '' INT TERM QUIT |
| 209 trap '' EXIT |
| 210 cat <<EOF |
| 211 |
| 212 |
| 213 Successfully installed ${distname} ${arch} |
| 214 |
| 215 You can run programs inside of the chroot by invoking the "${target%bit}" |
| 216 command. |
| 217 |
| 218 Your home directory is shared between the host and the chroot. But I configured |
| 219 $HOME/chroot to be private to the chroot environment. You can use it |
| 220 for files that need to differ between environments. |
| 221 EOF |
OLD | NEW |