enter-chroot 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /bin/sh -
  2. # Copyright (c) 2016-2018 Matias Fonzo, <selk@dragora.org>.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. PROGRAM="${0##*/}"
  16. # Exit immediately on any error
  17. set -e
  18. # Override locale settings
  19. LC_ALL=C
  20. export LC_ALL
  21. # Get physical working directory, absolute path name
  22. CWD=$(CDPATH= cd -P -- $(dirname -- "$0") && printf "$PWD")
  23. # Functions
  24. unmountSeries()
  25. {
  26. for node in "${rootdir}/usr/src/qi/archive" \
  27. "${rootdir}/usr/src/qi/patches" \
  28. "${rootdir}/usr/src/qi/recipes" \
  29. "${rootdir}/usr/src/qi/sources" \
  30. "${rootdir}/sys" \
  31. "${rootdir}/proc" \
  32. "${rootdir}/dev/pts" \
  33. "${rootdir}/dev" ; \
  34. do
  35. if mount | grep -q "$node"
  36. then
  37. echo "Unmounting ${node} ..."
  38. if ! umount "$node"
  39. then
  40. echo "Doing a lazy unmount for $node ..."
  41. umount -l "$node"
  42. fi
  43. fi
  44. done
  45. }
  46. # Autodetermine the path of /tools
  47. if test ! -L /tools
  48. then
  49. echo "${PROGRAM}: cannot determine ${rootdir}: /tools does not exist as symlink" 1>&2
  50. exit 1
  51. fi
  52. if test -L /tools -a ! -e /tools
  53. then
  54. echo "${PROGRAM}: cannot access /tools: Dangling symlink" 1>&2
  55. exit 1
  56. fi
  57. # Set canonical name to compose the root directory, removing
  58. # the last component from the path name "/tools"
  59. rootdir=$(CDPATH= cd -P -- /tools && printf "$PWD")
  60. rootdir="${rootdir%/*}"
  61. # Unmount directories and file systems before the next mounting
  62. unmountSeries
  63. # Create required directory for destination
  64. mkdir -p -- "${rootdir}/dev" \
  65. "${rootdir}/dev/pts" \
  66. "${rootdir}/proc" \
  67. "${rootdir}/sys" \
  68. "${rootdir}/usr/src/qi/archive" \
  69. "${rootdir}/usr/src/qi/patches" \
  70. "${rootdir}/usr/src/qi/recipes" \
  71. "${rootdir}/usr/src/qi/sources"
  72. echo "Mounting virtual file systems ..."
  73. mount -o bind /dev "${rootdir}/dev"
  74. mount -t devpts devpts "${rootdir}/dev/pts"
  75. mount -t proc proc "${rootdir}/proc"
  76. mount -t sysfs sysfs "${rootdir}/sys"
  77. echo "Binding directories to mount: archive, patches, recipes, sources ..."
  78. mount -o bind "${CWD}/archive" "${rootdir}/usr/src/qi/archive"
  79. mount -o bind "${CWD}/patches" "${rootdir}/usr/src/qi/patches"
  80. mount -o bind "${CWD}/recipes" "${rootdir}/usr/src/qi/recipes"
  81. mount -o bind "${CWD}/sources" "${rootdir}/usr/src/qi/sources"
  82. # Add trap for unmount the series, signalled or not
  83. trap 'echo ; unmountSeries' EXIT HUP QUIT ABRT TERM
  84. echo "Entering via chroot $rootdir ..."
  85. chroot "$rootdir" /tools/bin/env -i \
  86. PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
  87. SHELL=/bin/sh \
  88. HOME=/root \
  89. TERM="${TERM:-linux}" \
  90. LC_ALL=C \
  91. LANGUAGE=C \
  92. PKG_CONFIG=/usr/bin/pkgconf \
  93. PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig \
  94. /bin/sh -