enter-chroot 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #! /bin/sh -
  2. # Copyright (c) 2016-2018 Matias Fonzo, <selk@dragora.org>.
  3. # Copyright (c) 2020-2022 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 physical working directory (absolute path)
  23. CWD="$(CDPATH='' cd -P -- "$(dirname -- "$0")" && pwd -P)" || exit $?
  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}/var/cache/qi/packages" \
  32. "${rootdir}/sys" \
  33. "${rootdir}/proc" \
  34. "${rootdir}/dev/pts" \
  35. "${rootdir}/dev/shm" \
  36. "${rootdir}/dev" ; \
  37. do
  38. if mount | grep -q "$node"
  39. then
  40. echo "Unmounting ${node} ..."
  41. if ! umount "$node"
  42. then
  43. echo "Doing a lazy unmount for $node ..."
  44. umount -l "$node"
  45. fi
  46. fi
  47. done
  48. unset -v node
  49. }
  50. # Autodetermine the path of /tools
  51. if test ! -L /tools
  52. then
  53. echo "${PROGRAM}: cannot determine ${rootdir}: /tools does not exist as symlink" 1>&2
  54. exit 1
  55. fi
  56. if test -L /tools && test ! -e /tools
  57. then
  58. echo "${PROGRAM}: cannot access /tools: Dangling symlink" 1>&2
  59. exit 1
  60. fi
  61. # Set canonical name to compose the root directory, removing
  62. # the last component from the path name "/tools"
  63. rootdir="$(CDPATH='' cd -P -- /tools && pwd -P)"
  64. rootdir="${rootdir%/*}"
  65. # Unmount directories and file systems before the next mounting
  66. unmountSeries
  67. # Create required directory for destination
  68. mkdir -p -- \
  69. "${rootdir}/dev" \
  70. "${rootdir}/dev/pts" \
  71. "${rootdir}/dev/shm" \
  72. "${rootdir}/proc" \
  73. "${rootdir}/sys" \
  74. "${rootdir}/usr/src/qi/archive" \
  75. "${rootdir}/usr/src/qi/patches" \
  76. "${rootdir}/usr/src/qi/recipes" \
  77. "${rootdir}/usr/src/qi/sources" \
  78. "${rootdir}/var/cache/qi/packages"
  79. echo "Mounting virtual file systems ..."
  80. mount -o bind /dev "${rootdir}/dev"
  81. mount -t devpts devpts "${rootdir}/dev/pts"
  82. mount -t tmpfs devshm "${rootdir}/dev/shm"
  83. mount -t proc proc "${rootdir}/proc"
  84. mount -t sysfs sysfs "${rootdir}/sys"
  85. echo "Binding directories for archive/, patches/, recipes/, sources/, and packages/ ..."
  86. mount -o bind "${CWD}/archive" "${rootdir}/usr/src/qi/archive"
  87. mount -o bind "${CWD}/patches" "${rootdir}/usr/src/qi/patches"
  88. mount -o bind "${CWD}/recipes" "${rootdir}/usr/src/qi/recipes"
  89. mount -o bind "${CWD}/sources" "${rootdir}/usr/src/qi/sources"
  90. mount -o bind "${CWD}/packages" "${rootdir}/var/cache/qi/packages"
  91. # Change reported architecture for an appropriate environment
  92. _machine_type=""
  93. if file /tools/bin/busybox | grep -q "32-bit"
  94. then
  95. echo "Using the linux32 command to change the reported architecure ..."
  96. if command -v linux32 > /dev/null
  97. then
  98. _machine_type="linux32"
  99. else
  100. echo " The linux32 command is not available in your system." 1>&2
  101. echo "The current architecture will be used for the environment: $(uname -m)" 1>&2
  102. fi
  103. fi
  104. # Add trap for unmount the series, signalled or not
  105. trap 'echo ""; unmountSeries' EXIT HUP QUIT ABRT TERM
  106. echo "Entering via chroot $rootdir ..."
  107. $_machine_type chroot "$rootdir" /tools/bin/env -i \
  108. PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin:/opt/trinity/bin \
  109. SHELL=/bin/sh \
  110. HOME=/root \
  111. TERM=linux \
  112. HOSTNAME=dragora \
  113. LC_ALL=C \
  114. PKG_CONFIG=/usr/bin/pkgconf \
  115. PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig:/opt/trinity/lib/pkgconfig \
  116. /bin/sh -