From 28b71ec040235309298cb61ea642dcfd339a0893 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 27 Sep 2010 16:49:31 +0200 Subject: [PATCH] sde-prepare-chroot: split mount/umount code out of Build-Pkg --- bin/sde-prepare-chroot | 165 +++++++++++++++++++++++++++++++++++++++++ scripts/Build-Pkg | 70 +---------------- scripts/Build-Target | 17 ----- 3 files changed, 169 insertions(+), 83 deletions(-) create mode 100755 bin/sde-prepare-chroot diff --git a/bin/sde-prepare-chroot b/bin/sde-prepare-chroot new file mode 100755 index 0000000..9fb553e --- /dev/null +++ b/bin/sde-prepare-chroot @@ -0,0 +1,165 @@ +#!/bin/sh +# --- SDE-COPYRIGHT-NOTE-BEGIN --- +# This copyright note is auto-generated by ./scripts/Create-CopyPatch. +# +# Filename: bin/sde-prepare-chroot +# Copyright (C) 2006 - 2010 The OpenSDE Project +# Copyright (C) 2004 - 2006 The T2 SDE Project +# Copyright (C) 1998 - 2003 Clifford Wolf +# +# More information can be found in the files COPYING and README. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. A copy of the +# GNU General Public License can be found in the file COPYING. +# --- SDE-COPYRIGHT-NOTE-END --- + +#Description: Prepare sandbox for chrooted building + +[ -n "$SDEROOT" ] || + export SDEROOT=$( cd "${0%/*}/.."; pwd -P ) + +. "$SDEROOT/lib/libsde.in" + +prepare_usage() { + local progname=$(echo ${0##*/} | tr '-' ' ') + + [ $# -eq 0 ] || echo_error "$@" + cat <<-EOT + Usage: $progname [-u] [-b ] ... + EOT + + exit 1 +} + +# parse arguments +shortopts='?ub:' +longopts='help' +options=$(getopt -o "$shortopts" -l "$longopts" -- "$@") +[ $? -eq 0 ] || prepare_usage + +eval set -- "$options" + +chroot_mode=mount +chroot_base="$SDEROOT" + +while [ $# -gt 0 ]; do + case "$1" in + --help) + check_usage + ;; + -u) + chroot_mode=umount + ;; + -b) + if [ -x "$2/bin/sde" ]; then + chroot_base="$(cd "$2/" && pwd -P)" + echo_info "Using $chroot_base instead of $SDEROOT" + else + prepare_usage "$2: Invalid tree." + fi + ;; + + --) + shift; break + ;; + *) + break + esac + shift +done + +x_mknod() { + [ -c "$1" -o -b "$1" ] || mknod "$@" +} + +mountpoint() { + grep -q " $1 " /proc/mounts +} + +chroot_mount() { + local realconf= realdown= realbase= realxroot= + local x= s= d= + + [ -d dev/loop ] || mkdir -p dev/loop + x_mknod dev/null c 1 3 + x_mknod dev/zero c 1 5 + x_mknod dev/random c 1 8 + x_mknod dev/urandom c 1 9 + x_mknod dev/loop/0 b 7 0 + x_mknod dev/loop/1 b 7 1 + x_mknod dev/loop/2 b 7 2 + x_mknod dev/loop/3 b 7 3 + #_mknod dev/tty c 5 0 + + [ -L dev/fd ] || ln -s /proc/self/fd dev/fd + + [ -r proc/mounts ] || mount -nt proc none proc + + realxroot=$(pwd -P) + realconf=$(cd "$chroot_base"/config; pwd -P) + realdown=$(cd "$chroot_base"/download; pwd -P) + realbase=$(dirname $(cd "$chroot_base"/scripts; pwd -P)) + + for x in "loop:$realbase" "config:$realconf" "download:$realdown"; do + s="${x#*:}" d="TOOLCHAIN/${x%%:*}" + [ -d "$d" ] || mkdir -p "$d" + + mountpoint "$realxroot/$d" || mount --bind "$s" "$d" + done + + for x in bin etc lib doc src package architecture target; do + [ -e TOOLCHAIN/$x ] || ln -s "loop/$x" "TOOLCHAIN/$x" + done + + x=${realxroot##*/} + if [ ! -e "TOOLCHAIN/build/$x" ]; then + mkdir -p "TOOLCHAIN/build" + ln -sn ../.. "TOOLCHAIN/build/$x" + fi +} + +chroot_umount() { + local busy= x= y= + local realxroot=$(pwd -P) + + if [ -d var/adm/logs ]; then + if ( cd var/adm/logs; fuser *.log > /dev/null 2>&1 ); then + busy=yes + fi + fi + + if [ -z "$busy" ]; then + echo_info "Unmounting loop mounts ..." + for x in loop config mount; do + y=$realxroot/TOOLCHAIN/$x + if mountpoint $y; then + umount -d -f $y || umount -d -f -l $y + fi + done + + y=$realxroot/proc + if mountpoint $y; then + umount -d -f $y || umount -d -f -l $y + fi + fi +} + +errno=0 cmd="chroot_$chroot_mode" +for d; do + if [ -d "$d/TOOLCHAIN" ]; then + dir="$(cd "$d/" && pwd -P)" + else + dir= + fi + + if [ -z "$dir" ]; then + echo_error "$d: Invalid sandbox" + errno=1 + else + (set -e; cd "$dir"; "$cmd") + [ $? -eq 0 ] || errno=1 + fi +done +exit $errno diff --git a/scripts/Build-Pkg b/scripts/Build-Pkg index 74a46ce..b98d018 100755 --- a/scripts/Build-Pkg +++ b/scripts/Build-Pkg @@ -152,53 +152,9 @@ builddir="$base/src.$pkg.$config.$id" if [ "$chroot" = 1 ] ; then + "$base/bin/sde-prepare-chroot" -b "$base" "$xroot" || exit 1 cd "$xroot" || exit 1 - x_mknod() { - if [ ! -c "$1" -a ! -b "$1" ]; then - mknod "$@" - fi - } - - mkdir -p dev/loop - x_mknod dev/null c 1 3 - x_mknod dev/zero c 1 5 - x_mknod dev/random c 1 8 - x_mknod dev/urandom c 1 9 - x_mknod dev/loop/0 b 7 0 - x_mknod dev/loop/1 b 7 1 - x_mknod dev/loop/2 b 7 2 - x_mknod dev/loop/3 b 7 3 - #_mknod dev/tty c 5 0 - - if [ ! -L dev/fd ]; then - ln -s /proc/self/fd dev/fd - fi - - realconf=$(cd $base/config; pwd -P) - realdown=$(cd $base/download; pwd -P) - realbase=$(dirname $(cd $base/scripts; pwd -P)) - - if [ ! -e TOOLCHAIN/loop/scripts ]; then - mkdir -p TOOLCHAIN/{loop,config,download} - mount --bind $realbase TOOLCHAIN/loop - mount --bind $realconf TOOLCHAIN/config - mount --bind $realdown TOOLCHAIN/download - fi - - if [ ! -f proc/mounts ]; then - mount -nt proc none proc - fi - - for x in $sderootdirs; do - if [ ! -e TOOLCHAIN/$x ]; then ln -s "loop/$x" "TOOLCHAIN/$x"; fi - done - - if [ ! -e "TOOLCHAIN/build/$SDECFG_ID" ]; then - mkdir -p "TOOLCHAIN/build" - ln -snf ../.. "TOOLCHAIN/build/$SDECFG_ID" - fi - mkdir -p "TOOLCHAIN/src.$pkg.$config.$id" ln -s "$PWD/TOOLCHAIN/src.$pkg.$config.$id" "$builddir" @@ -209,24 +165,10 @@ if [ "$chroot" = 1 ] ; then cat > $builddir/debug.sh <<- EOT #!/bin/bash - if [ ! -f "$xroot/proc/mounts" ]; then - mount -nt proc none "$xroot/proc" - fi - if [ ! -e "$xroot/TOOLCHAIN/loop/scripts" ]; then - mkdir -p "$xroot/"TOOLCHAIN/{loop,config,download} - mount --bind $realbase "$xroot/TOOLCHAIN/loop" - mount --bind $realconf "$xroot/TOOLCHAIN/config" - mount --bind $realdown "$xroot/TOOLCHAIN/download" - fi + "$base/bin/sde-prepare-chroot" -b "$base" "$xroot" || exit $? export ROCK_THIS_IS_CHROOT=1 /usr/sbin/chroot "$xroot" /bin/bash TOOLCHAIN/src.$pkg.$config.$id/debug_x.sh - - if ! fuser -s "$xroot/TOOLCHAIN/" ; then - echo "Loop mounts appear unused - un-mounting ..." - umount -l "$xroot/TOOLCHAIN/"{download,config,loop} - umount -l "$xroot/proc" - fi EOT chmod +x $builddir/debug.sh @@ -968,9 +910,6 @@ fi cd $base -umount -r -d -f $builddir/* 2> /dev/null -umount -r -d -f -l $builddir/* 2> /dev/null - if [ "$SDECFG_SRC_TMPFS_LOG" = 1 -a -n "$( type -p df )" ]; then mkdir -p $root/var/adm/sde-debug if [ ! -f $root/var/adm/sde-debug/tmpfslog.txt ] ; then @@ -984,11 +923,9 @@ if [ "$SDECFG_SRC_TMPFS_LOG" = 1 -a -n "$( type -p df )" ]; then expand -t20 >> $root/var/adm/sde-debug/tmpfslog.txt fi -umount -r -d -f $builddir 2> /dev/null -umount -r -d -f -l $builddir 2> /dev/null - if [ -f $root/var/adm/logs/$stagelevel-$xpkg.log ] ; then if [ $clear_src = 1 ] ; then + # TODO: umount if TMPFS rm -rf $builddir/* $builddir else cp $root/var/adm/logs/$stagelevel-$xpkg.out $builddir/BUILD-LOG @@ -1001,6 +938,7 @@ if [ -f $root/var/adm/logs/$stagelevel-$xpkg.log ] ; then exit 0 else if [ $clear_src = 1 -a "$SDECFG_ALWAYS_CLEAN" = 1 ] ; then + # TODO: umount if TMPFS rm -rf $builddir/* $builddir else cp $root/var/adm/logs/$stagelevel-$xpkg.out $builddir/ERROR-LOG diff --git a/scripts/Build-Target b/scripts/Build-Target index 8eba069..8cd47a0 100755 --- a/scripts/Build-Target +++ b/scripts/Build-Target @@ -260,23 +260,6 @@ pkgloop_action() { return $rc } -# Try to umount any directories mounted by Build-Pkg -chroot -# if we are the last process using them. -# -umount_chroot() { - exec 201> /dev/null - if ! ( cd ${build_logs}; fuser *.log > /dev/null 2>&1 ); then - echo_status "Unmounting loop mounts ..." - umount -d -f $build_toolchain/{download,config,loop} 2> /dev/null - umount -d -f -l $build_toolchain/{download,config,loop} 2> /dev/null - umount -d -f $build_root/proc 2> /dev/null - umount -d -f -l $build_root/proc 2> /dev/null - fi -} - -# must trap outside the group command -trap 'umount_chroot' EXIT - { ln -sf build_target_$$.log ${build_logs}/build_target.log ./bin/sde-build-tools -1 -c $config