home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Linux / Ubuntu_64-bit / ubuntu-11.04-desktop-amd64.iso / casper / filesystem.squashfs / lib / chroot-setup.sh next >
Text File  |  2010-10-15  |  6KB  |  201 lines

  1. # Setup for using apt to install packages in /target.
  2.  
  3. mountpoints () {
  4.     cut -d" " -f2 /proc/mounts | sort | uniq
  5. }
  6.  
  7. # Make sure mtab in the chroot reflects the currently mounted partitions.
  8. update_mtab() {
  9.     mtab=/target/etc/mtab
  10.  
  11.     if [ -h "$mtab" ]; then
  12.         logger -t $0 "warning: $mtab won't be updated since it is a symlink."
  13.         return 0
  14.     fi
  15.  
  16.     egrep '^[^ ]+ /target' /proc/mounts | (
  17.     while read devpath mountpoint fstype options n1 n2 ; do
  18.         devpath=`mapdevfs $devpath || echo $devpath`
  19.         mountpoint="${mountpoint#/target}"
  20.         # mountpoint for root will be empty
  21.         if [ -z "$mountpoint" ] ; then
  22.             mountpoint="/"
  23.         fi
  24.         echo $devpath $mountpoint $fstype $options $n1 $n2
  25.     done ) > $mtab
  26. }
  27.  
  28. chroot_setup () {
  29.     # Bail out if directories we need are not there
  30.     if [ ! -d /target/sbin ] || [ ! -d /target/usr/sbin ] || \
  31.        [ ! -d /target/proc ]; then
  32.         return 1
  33.     fi
  34.     if [ -d /sys/devices ] && [ ! -d /target/sys ]; then
  35.         return 1
  36.     fi
  37.  
  38.     if [ -e /var/run/chroot-setup.lock ]; then
  39.         cat >&2 <<EOF
  40. apt-install or in-target is already running, so you cannot run either of
  41. them again until the other instance finishes. You may be able to use
  42. 'chroot /target ...' instead.
  43. EOF
  44.         return 1
  45.     fi
  46.     touch /var/run/chroot-setup.lock
  47.  
  48.     # Create a policy-rc.d to stop maintainer scripts using invoke-rc.d 
  49.     # from running init scripts. In case of maintainer scripts that don't
  50.     # use invoke-rc.d, add a dummy start-stop-daemon.
  51.     cat > /target/usr/sbin/policy-rc.d <<EOF
  52. #!/bin/sh
  53. exit 101
  54. EOF
  55.     chmod a+rx /target/usr/sbin/policy-rc.d
  56.     
  57.     if [ -e /target/sbin/start-stop-daemon ]; then
  58.         mv /target/sbin/start-stop-daemon /target/sbin/start-stop-daemon.REAL
  59.     fi
  60.     cat > /target/sbin/start-stop-daemon <<EOF
  61. #!/bin/sh
  62. echo 1>&2
  63. echo 'Warning: Fake start-stop-daemon called, doing nothing.' 1>&2
  64. exit 0
  65. EOF
  66.     chmod a+rx /target/sbin/start-stop-daemon
  67.     
  68.     # If Upstart is in use, add a dummy initctl to stop it starting jobs.
  69.     if [ -x /target/sbin/initctl ]; then
  70.         mv /target/sbin/initctl /target/sbin/initctl.REAL
  71.         cat > /target/sbin/initctl <<EOF
  72. #!/bin/sh
  73. echo 1>&2
  74. echo 'Warning: Fake initctl called, doing nothing.' 1>&2
  75. exit 0
  76. EOF
  77.         chmod a+rx /target/sbin/initctl
  78.     fi
  79.  
  80.     # Record the current mounts
  81.     mountpoints > /tmp/mount.pre
  82.  
  83.     case `udpkg --print-os` in
  84.             "linux")
  85.             # Some packages (eg. the kernel-image package) require a mounted
  86.             # /proc/. Only mount it if not mounted already
  87.             if [ ! -f /target/proc/cmdline ]; then
  88.                 mount -t proc proc /target/proc
  89.             fi
  90.  
  91.             # For installing >=2.6.14 kernels we also need sysfs mounted
  92.             # Only mount it if not mounted already
  93.             if [ ! -d /target/sys/devices ]; then
  94.                 mount -t sysfs sysfs /target/sys
  95.             fi
  96.  
  97.             # In Lenny, /dev/ lacks the pty devices, so we need devpts mounted
  98.             if [ ! -e /target/dev/pts/0 ]; then
  99.                 mkdir -p /target/dev/pts
  100.                 mount -t devpts devpts -o noexec,nosuid,gid=5,mode=620 \
  101.                     /target/dev/pts
  102.             fi
  103.         ;;
  104.             "kfreebsd")
  105.             # Some packages (eg. the kernel-image package) require a mounted
  106.             # /proc/. Only mount it if not mounted already
  107.             if [ ! -f /target/proc/cmdline ]; then
  108.                 mount -t procfs proc /target/proc
  109.             fi
  110.             # Some package might need sysfs mounted
  111.             # Only mount it if not mounted already
  112.             if [ ! -d /target/sys/devices ]; then
  113.                 mount -t linsysfs sysfs /target/sys
  114.             fi
  115.         ;;
  116.     esac
  117.  
  118.     mountpoints > /tmp/mount.post
  119.  
  120.     update_mtab
  121.  
  122.     # Try to enable proxy when using HTTP.
  123.     # What about using ftp_proxy for FTP sources?
  124.     RET=$(debconf-get mirror/protocol || true)
  125.     if [ "$RET" = "http" ]; then
  126.         RET=$(debconf-get mirror/http/proxy || true)
  127.         if [ "$RET" ]; then
  128.             http_proxy="$RET"
  129.             export http_proxy
  130.         fi
  131.     fi
  132.  
  133.     # Pass debconf priority through.
  134.     DEBIAN_PRIORITY=$(debconf-get debconf/priority || true)
  135.     export DEBIAN_PRIORITY
  136.  
  137.     LANG=${IT_LANG_OVERRIDE:-$(debconf-get debian-installer/locale || true)}
  138.     export LANG
  139.     export PERL_BADLANG=0
  140.  
  141.     # Unset variables that would make scripts in the target think
  142.     # that debconf is already running there.
  143.     unset DEBIAN_HAS_FRONTEND
  144.     unset DEBIAN_FRONTEND
  145.     unset DEBCONF_FRONTEND
  146.     unset DEBCONF_REDIR
  147.     # Avoid debconf mailing notes.
  148.     DEBCONF_ADMIN_EMAIL=""
  149.     export DEBCONF_ADMIN_EMAIL
  150.     # Avoid apt-listchanges doing anything.
  151.     APT_LISTCHANGES_FRONTEND=none
  152.     export APT_LISTCHANGES_FRONTEND
  153.  
  154.     return 0
  155. }
  156.  
  157. chroot_cleanup () {
  158.     rm -f /target/usr/sbin/policy-rc.d
  159.     mv /target/sbin/start-stop-daemon.REAL /target/sbin/start-stop-daemon
  160.     if [ -x /target/sbin/initctl.REAL ]; then
  161.         mv /target/sbin/initctl.REAL /target/sbin/initctl
  162.     fi
  163.  
  164.     # Undo the mounts done by the packages during installation.
  165.     # Reverse sorting to umount the deepest mount points first.
  166.     # Items with count of 1 are new.
  167.     for dir in $( (cat /tmp/mount.pre /tmp/mount.pre; mountpoints ) | \
  168.              sort -r | uniq -c | grep "^[[:space:]]*1[[:space:]]" | \
  169.              sed "s/^[[:space:]]*[0-9][[:space:]]//"); do
  170.         if ! umount $dir; then
  171.             logger -t $0 "warning: Unable to umount '$dir'"
  172.         fi
  173.     done
  174.     rm -f /tmp/mount.pre /tmp/mount.post
  175.  
  176.     rm -f /var/run/chroot-setup.lock
  177. }
  178.  
  179. # Variant of chroot_cleanup that only cleans up chroot_setup's mounts.
  180. chroot_cleanup_localmounts () {
  181.     rm -f /target/usr/sbin/policy-rc.d
  182.     mv /target/sbin/start-stop-daemon.REAL /target/sbin/start-stop-daemon
  183.     if [ -x /target/sbin/initctl.REAL ]; then
  184.         mv /target/sbin/initctl.REAL /target/sbin/initctl
  185.     fi
  186.  
  187.     # Undo the mounts done by the packages during installation.
  188.     # Reverse sorting to umount the deepest mount points first.
  189.     # Items with count of 1 are new.
  190.     for dir in $( (cat /tmp/mount.pre /tmp/mount.pre /tmp/mount.post ) | \
  191.              sort -r | uniq -c | grep "^[[:space:]]*1[[:space:]]" | \
  192.              sed "s/^[[:space:]]*[0-9][[:space:]]//"); do
  193.         if ! umount $dir; then
  194.             logger -t $0 "warning: Unable to umount '$dir'"
  195.         fi
  196.     done
  197.     rm -f /tmp/mount.pre /tmp/mount.post
  198.  
  199.     rm -f /var/run/chroot-setup.lock
  200. }
  201.