Professional Documents
Culture Documents
Install-Chroot SH
Install-Chroot SH
/bin/bash -e
# Older Debian based systems had both "admin" and "adm" groups, with "admin"
# apparently being used in more places. Newer distributions have standardized
# on just the "adm" group. Check /etc/group for the preferred name of the
# administrator group.
admin=$(grep '^admin:' /etc/group >&/dev/null && echo admin || echo adm)
usage() {
echo "usage: ${0##*/} [-m mirror] [-g group,...] [-s] [-c]"
echo "-b dir additional directories that should be bind mounted,"
echo ' or "NONE".'
echo " Default: if local filesystems present, ask user for
help"
echo "-g group,... groups that can use the chroot unauthenticated"
echo " Default: '${admin}' and current user's group ('$(id -
gn)')"
echo "-l List all installed chroot environments"
echo "-m mirror an alternate repository mirror for package downloads"
echo "-s configure default deb-srcs"
echo "-c always copy 64bit helper binaries to 32bit chroot"
echo "-h this help message"
}
process_opts() {
local OPTNAME OPTIND OPTERR OPTARG
while getopts ":b:g:lm:sch" OPTNAME; do
case "$OPTNAME" in
b)
if [ "${OPTARG}" = "NONE" -a -z "${bind_mounts}" ]; then
bind_mounts="${OPTARG}"
else
if [ "${bind_mounts}" = "NONE" -o "${OPTARG}" = "${OPTARG#/}" -o \
! -d "${OPTARG}" ]; then
echo "Invalid -b option(s)"
usage
exit 1
fi
bind_mounts="${bind_mounts}
${OPTARG} ${OPTARG} none rw,bind 0 0"
fi
;;
g)
[ -n "${OPTARG}" ] &&
chroot_groups="${chroot_groups}${chroot_groups:+,}${OPTARG}"
;;
l)
list_all_chroots
exit
;;
m)
if [ -n "${mirror}" ]; then
echo "You can only specify exactly one mirror location"
usage
exit 1
fi
mirror="$OPTARG"
;;
s)
add_srcs="y"
;;
c)
copy_64="y"
;;
h)
usage
exit 0
;;
\:)
echo "'-$OPTARG' needs an argument."
usage
exit 1
;;
*)
echo "invalid command-line option: $OPTARG"
usage
exit 1
;;
esac
done
list_all_chroots() {
for i in /var/lib/chroot/*; do
i="${i##*/}"
[ "${i}" = "*" ] && continue
[ -x "/usr/local/bin/${i%bit}" ] || continue
grep -qs "^\[${i%bit}\]\$" /etc/schroot/schroot.conf || continue
[ -r "/etc/schroot/script-${i}" -a \
-r "/etc/schroot/mount-${i}" ] || continue
echo "${i%bit}"
done
}
getkey() {
(
trap 'stty echo -iuclc icanon 2>/dev/null' EXIT INT TERM QUIT HUP
stty -echo iuclc -icanon 2>/dev/null
dd count=1 bs=1 2>/dev/null
)
}
chr() {
printf "\\$(printf '%03o' "$1")"
}
ord() {
printf '%d' $(printf '%c' "$1" | od -tu1 -An)
}
is_network_drive() {
stat -c %T -f "$1/" 2>/dev/null |
egrep -qs '^nfs|cifs|smbfs'
}
process_opts "$@"
echo "This script will help you through the process of installing a"
echo "Debian or Ubuntu distribution in a chroot environment. You will"
echo "have to provide your \"sudo\" password when requested."
echo
# Error handler
trap 'exit 1' INT TERM QUIT HUP
trap 'sudo apt-get clean; tput bel; echo; echo Failed' EXIT
# Install any missing applications that this script relies on. If these
packages
# are already installed, don't force another "apt-get install". That would
# prevent them from being auto-removed, if they ever become eligible for
that.
# And as this script only needs the packages once, there is no good reason to
# introduce a hard dependency on things such as dchroot and debootstrap.
dep=
for i in dchroot debootstrap libwww-perl; do
[ -d /usr/share/doc/"$i" ] || dep="$dep $i"
done
[ -n "$dep" ] && sudo apt-get -y install $dep
sudo apt-get -y install schroot
# Check for non-standard file system mount points and ask the user whether
# they should be imported into the chroot environment
# We limit to the first 26 mount points that much some basic heuristics,
# because a) that allows us to enumerate choices with a single character,
# and b) if we find more than 26 mount points, then these are probably
# false-positives and something is very unusual about the system's
# configuration. No need to spam the user with even more information that
# is likely completely irrelevant.
if [ -z "${bind_mounts}" ]; then
mounts="$(awk '$2 != "/" && $2 !~ "^/boot" && $2 !~ "^/home" &&
$2 !~ "^/media" && $2 !~ "^/run" &&
($3 ~ "ext[2-4]" || $3 == "reiserfs" || $3 == "btrfs" ||
$3 == "xfs" || $3 == "jfs" || $3 == "u?msdos" ||
$3 == "v?fat" || $3 == "hfs" || $3 == "ntfs" ||
$3 ~ "nfs[4-9]?" || $3 == "smbfs" || $3 == "cifs") {
print $2
}' /proc/mounts |
head -n26)"
if [ -n "${mounts}" ]; then
echo "You appear to have non-standard mount points that you"
echo "might want to import into the chroot environment:"
echo
sel=
while :; do
# Print a menu, listing all non-default mounts of local or network
# file systems.
j=1; for m in ${mounts}; do
c="$(printf $(printf '\\%03o' $((64+$j))))"
echo "$sel" | grep -qs $c &&
state="mounted in chroot" || state="$(tput el)"
printf " $c) %-40s${state}\n" "$m"
j=$(($j+1))
done
# Allow user to interactively (de-)select any of the entries
echo
printf "Select mount points that you want to be included or press %s" \
"SPACE to continue"
c="$(getkey | tr a-z A-Z)"
[ "$c" == " " ] && { echo; echo; break; }
if [ -z "$c" ] ||
[ "$c" '<' 'A' -o $(ord "$c") -gt $((64 + $(ord "$j"))) ]; then
# Invalid input, ring the console bell
tput bel
else
# Toggle the selection for the given entry
if echo "$sel" | grep -qs $c; then
sel="$(printf "$sel" | sed "s/$c//")"
else
sel="$sel$c"
fi
fi
# Reposition cursor to the top of the list of entries
tput cuu $(($j + 1))
echo
done
fi
j=1; for m in ${mounts}; do
c="$(chr $(($j + 64)))"
if echo "$sel" | grep -qs $c; then
bind_mounts="${bind_mounts}$m $m none rw,bind 0 0
"
fi
j=$(($j+1))
done
fi
if [ -d '/etc/schroot/default' ]; then
new_version=1
fstab="/etc/schroot/${target}/fstab"
else
new_version=0
fstab="/etc/schroot/mount-${target}"
fi
EOF
[ -n "${bind_mounts}" -a "${bind_mounts}" != "NONE" ] &&
printf "${bind_mounts}" |
sudo sh -c "cat >>${fstab}"
else
# Older versions of schroot wanted a "priority=" line, whereas recent
# versions deprecate "priority=" and warn if they see it. We don't have
# a good feature test, but scanning for the string "priority=" in the
# existing "schroot.conf" file is a good indication of what to do.
priority=$(grep -qs 'priority=' /etc/schroot/schroot.conf &&
echo 'priority=3' || :)
sudo sh -c 'cat >>/etc/schroot/schroot.conf' <<EOF
[${target%bit}]
description=${brand} ${distname} ${arch}
type=directory
directory=/var/lib/chroot/${target}
users=root
groups=${chroot_groups}
root-groups=${chroot_groups}
personality=linux$([ "${arch}" != 64bit ] && echo 32)
script-config=script-${target}
${priority}
EOF
# Add the extra mount points that the user told us about
[ -n "${bind_mounts}" -a "${bind_mounts}" != "NONE" ] &&
printf "${bind_mounts}" |
sudo sh -c 'cat >>'"${fstab}"
chroot="${0##*/}"
wrap() {
# Word-wrap the text passed-in on stdin. Optionally, on continuation lines
# insert the same number of spaces as the number of characters in the
# parameter(s) passed to this function.
# If the "fold" program cannot be found, or if the actual width of the
# terminal cannot be determined, this function doesn't attempt to do any
# wrapping.
local f="$(type -P fold)"
[ -z "${f}" ] && { cat; return; }
local c="$(stty -a </dev/tty 2>/dev/null |
sed 's/.*columns[[:space:]]*\([0-9]*\).*/\1/;t;d')"
[ -z "${c}" ] && { cat; return; }
local i="$(echo "$*"|sed 's/./ /g')"
local j="$(printf %s "${i}"|wc -c)"
if [ "${c}" -gt "${j}" ]; then
dd bs=1 count="${j}" 2>/dev/null
"${f}" -sw "$((${c}-${j}))" | sed '2,$s/^/'"${i}"'/'
else
"${f}" -sw "${c}"
fi
}
help() {
echo "Usage ${0##*/} [-h|--help] [-c|--clean] [-C|--clean-all] [-l|--list]
[--] args" | wrap "Usage ${0##*/} "
echo " help: print this message"
| wrap " "
echo " list: list all known chroot environments"
| wrap " "
echo " clean: remove all old chroot sessions for \"${chroot}\""
| wrap " "
echo " clean-all: remove all old chroot sessions for all environments"
| wrap " "
exit 0
}
clean() {
local s t rc
rc=0
for s in $(schroot -l --all-sessions); do
if [ -n "$1" ]; then
t="${s#session:}"
[ "${t#${chroot}-}" == "${t}" ] && continue
fi
if ls -l /proc/*/{cwd,fd} 2>/dev/null |
fgrep -qs "/var/lib/schroot/mount/${t}"; then
echo "Session \"${t}\" still has active users, not cleaning up" | wrap
rc=1
continue
fi
sudo schroot -c "${s}" -e || rc=1
done
exit ${rc}
}
list() {
for e in $(schroot -l); do
e="${e#chroot:}"
[ -x "/usr/local/bin/${e}" ] || continue
if schroot -l --all-sessions 2>/dev/null |
sed 's/^session://' |
grep -qs "^${e}-"; then
echo "${e} is currently active"
else
echo "${e}"
fi
done
exit 0
}
# Start a new chroot session and keep track of the session id. We inject this
# id into all processes that run inside the chroot. Unless they go out of
their
# way to clear their environment, we can then later identify our child and
# grand-child processes by scanning their environment.
session="$(schroot -c "${chroot}" -b)"
export CHROOT_SESSION_ID="${session}"
if [ $# -eq 0 ]; then
# Run an interactive shell session
schroot -c "${session}" -r -p
else
# Run a command inside of the chroot environment
p="$1"; shift
schroot -c "${session}" -r -p "$p" -- "$@"
fi
rc=$?
# Compute the inode of the root directory inside of the chroot environment.
i=$(schroot -c "${session}" -r -p ls -- -id /proc/self/root/. |
awk '{ print $1 }') 2>/dev/null
other_pids=
while [ -n "$i" ]; do
# Identify processes by the inode number of their root directory. Then
# remove all processes that we know belong to other sessions. We use
# "sort | uniq -u" to do what amounts to a "set subtraction operation".
pids=$({ ls -id1 /proc/*/root/. 2>/dev/null |
sed -e 's,^[^0-9]*'$i'.*/\([1-9][0-9]*\)/.*$,\1,
t
d';
echo "${other_pids}";
echo "${other_pids}"; } | sort | uniq -u) >/dev/null 2>&1
# Kill all processes that are still left running in the session. This is
# typically an assortment of daemon processes that were started
# automatically. They result in us being unable to tear down the session
# cleanly.
[ -z "${pids}" ] && break
for j in $pids; do
# Unfortunately, the way that schroot sets up sessions has the
# side-effect of being unable to tell one session apart from another.
# This can result in us attempting to kill processes in other sessions.
# We make a best-effort to avoid doing so.
k="$( ( xargs -0 -n1 </proc/$j/environ ) 2>/dev/null |
sed 's/^CHROOT_SESSION_ID=/x/;t1;d;:1;q')"
if [ -n "${k}" -a "${k#x}" != "${session}" ]; then
other_pids="${other_pids}
${j}"
continue
fi
kill -9 $pids
done
done
# End the chroot session. This should clean up all temporary files. But if we
# earlier failed to terminate all (daemon) processes inside of the session,
# deleting the session could fail. When that happens, the user has to
manually
# clean up the stale files by invoking us with "--clean" after having killed
# all running processes.
schroot -c "${session}" -e
# Since no goma processes are running, we can remove goma directory.
rm -rf "$GOMA_TMP_DIR"
exit $rc
EOF
sudo chown root:root /usr/local/bin/"${target%bit}"
sudo chmod 755 /usr/local/bin/"${target%bit}"
# Update packages
sudo "/usr/local/bin/${target%bit}" /bin/sh -c '
apt-get update; apt-get -y dist-upgrade' || :
# Configure locales
sudo "/usr/local/bin/${target%bit}" /bin/sh -c '
l='"${LANG:-en_US}"'; l="${l%%.*}"
[ -r /etc/locale.gen ] &&
sed -i "s/^# \($l\)/\1/" /etc/locale.gen
locale-gen $LANG en_US en_US.UTF-8' || :
This command can be used with arguments, in order to just run a single
program inside of the chroot environment (e.g. "${target%bit} make chrome")
or without arguments, in order to run an interactive shell session inside
of the chroot environment.
If you need to run things as "root", you can use "sudo" (e.g. try
"sudo ${target%bit} apt-get update").
Your home directory is shared between the host and the chroot. But I
configured "${HOME}/chroot" to be private to the chroot environment.
You can use it for files that need to differ between environments. This
would be a good place to store binaries that you have built from your
source files.
For Chrome, this probably means you want to make your "out" directory a
symbolic link that points somewhere inside of "${HOME}/chroot".
You still need to run "gclient runhooks" whenever you switch from building
outside of the chroot to inside of the chroot. But you will find that you
don't have to repeatedly erase and then completely rebuild all your object
and binary files.
EOF