enter-chroot 3.5 KB

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