Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #!/bin/bash
  2. # mer-sdk-chroot
  3.  
  4. # TODO
  5. #
  6. # Support a Mer clean setup (ie no .oscrc)
  7. # Support multiple shells (ie split setup/entry)
  8.  
  9. usage()
  10. {
  11. cat <<EOF
  12. usage: $0 [-u <user>] [-m <all|none|root|home>] [-r <SDK root path>] [<command> <args> ..]
  13. $0 -h
  14.  
  15. This is the Mer chroot SDK.
  16. For information see http://wiki.merproject.org/wiki/Platform_SDK
  17.  
  18. If command is not present,
  19. used to enter the SDK and begin working. The SDK bash shell is a
  20. login shell. See below for .profile handling
  21.  
  22. If command is present,
  23. used to execute an arbitrary command from within the SDK chroot
  24. environment. The environment variable MERSDK is set to allow
  25. SDK detection.
  26.  
  27. Options:
  28.  
  29. -u System user to link into SDK (not needed if using sudo)
  30. -m Devices to bind mount from host: none, all (default)
  31. root, home
  32. -r The root of the SDK to use - normally derived from the
  33. pathname of $0
  34. -h Show this help
  35.  
  36. Profile
  37.  
  38. Entering the SDK runs the user's normal .profile and any (SDK)
  39. system profile entries. It will not execute the host's system
  40. profile entries.
  41.  
  42. The environment variable MERSDK is set to allow .profile to
  43. detect the SDK.
  44.  
  45. If the user has a ~/.mersdk.profile then it is sourced after the
  46. normal .profile handling (this allows the common use case of
  47. setting a profile to be handled).
  48.  
  49. Hooks
  50.  
  51. If the user specified has a .mersdkrc in their $HOME, it will be
  52. sourced to allow hook functions to be defined. Hooks are run as
  53. root. No commands should be executed immediately.
  54.  
  55. These hooks are usually used to define symbolic links from any
  56. /parentroot/data type filesystems into the SDK root to setup
  57. system specific shared caches or filesystem layouts etc
  58.  
  59. EOF
  60. return 0
  61. }
  62.  
  63. MY_SSH_AUTH_SOCK=${SSH_AUTH_SOCK#/parentroot}
  64. [[ $MY_SSH_AUTH_SOCK ]] && MY_SSH_AUTH_SOCK="/parentroot$MY_SSH_AUTH_SOCK"
  65.  
  66. if [[ $EUID -ne 0 ]]; then
  67. exec sudo SSH_AGENT_PID=${SSH_AGENT_PID:-} SSH_AUTH_SOCK=${MY_SSH_AUTH_SOCK} $0 "$@"
  68. echo "$0 must be run as root and sudo failed; exiting"
  69. exit 1
  70. fi
  71.  
  72. if cmp -s /proc/$PPID/mountinfo /proc/self/mountinfo; then
  73. exec unshare -m -- "$0" "$@"
  74. echo "$0 must be run in private namespace and unshare failed; exiting"
  75. exit 1
  76. fi
  77.  
  78. # Make sure that mountpoints in the new namespace are really unshared from the
  79. # parental namespace. See unshare(1).
  80. # This prevents mounts in the sdk from appearing in the parent fs.
  81. mount --make-rslave /
  82.  
  83. # Use the SUDO value if present
  84. user=$SUDO_USER || true;
  85.  
  86. bind_mount_root="yes";
  87. bind_mount_home="yes";
  88.  
  89. while getopts "u:m:r:" opt; do
  90. case $opt in
  91. u ) user=$OPTARG;;
  92. m )
  93. case $OPTARG in
  94. all) ;;
  95. home)
  96. bind_mount_root="no";;
  97. root)
  98. bind_mount_home="no";;
  99. none)
  100. bind_mount_root="no";
  101. bind_mount_home="no";;
  102. *) echo "Only 'none', 'all' or 'home' are permitted for -m"
  103. usage
  104. exit 1;;
  105. esac ;;
  106. r ) sdkroot=$OPTARG;;
  107. h|\? ) usage
  108. exit 1;;
  109. : ) echo "Option -$OPTARG requires an argument." >&2
  110. usage
  111. exit 1;;
  112. * ) usage
  113. exit 1;;
  114. esac
  115. done
  116. shift $(($OPTIND - 1))
  117.  
  118. if [[ -z "${sdkroot}" ]] ; then
  119. sdkroot=$(dirname $(readlink -f $0))
  120. else
  121. sdkroot=$(readlink -f $sdkroot)
  122. fi
  123.  
  124. if [[ ! -f ${sdkroot}/etc/MerSDK ]] ; then
  125. echo "${sdkroot} does not look like a Mer SDK rootfs"
  126. echo "if you are sure it is, you may mark it by running"
  127. echo "echo 'MerSDK' | sudo tee ${sdkroot}/etc/MerSDK"
  128. exit 1
  129. fi
  130.  
  131. if ! [[ $(basename $(dirname $sdkroot)) == "sdks" ]]; then
  132. echo "Non-standard SDK installation layout - cannot determine location for "
  133. echo "SDK targets and toolings. Expected layout is:"
  134. echo ""
  135. echo " <path/to/SDKs...>/"
  136. echo " ├── sdks/"
  137. echo " │ ├── <sdk_1>/"
  138. echo " │ ├── <sdk_2>/"
  139. echo " │ └── .../"
  140. echo " ├── targets/"
  141. echo " │ ├── <targets_1>/"
  142. echo " │ ├── <targets_2>/"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement