Set of files and scripts for Embedded Lab 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

164 lines
4.2 KiB

  1. #!/bin/sh -
  2. #
  3. # Copyright (c) 2020 The FreeBSD Foundation
  4. #
  5. # This software1 was developed by John-Mark Gurney under sponsorship
  6. # from the FreeBSD Foundation.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. # 1. Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # 2. Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in the
  15. # documentation and/or other materials provided with the distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. # SUCH DAMAGE.
  28. #
  29. set -e
  30. . $(dirname $0)/settings.conf
  31. cmd="$1"
  32. board="$2"
  33. user="$3"
  34. sshkey="$4"
  35. # standard globals
  36. jaildir="$userzfsmount/$user/$board"
  37. # XXX - instead, move to an allocated dir, makes cleaning up easier
  38. allocateresource()
  39. {
  40. resdir="$1"
  41. while :; do
  42. res="$(cd "$resdir" && ls | head -n 1)"
  43. if [ x"$res" = x"" ]; then
  44. echo "Resource allocation failure for: $resdir"
  45. exit 1
  46. fi
  47. if rmdir "$resdir/$res" 2>/dev/null; then
  48. fsync "$resdir"
  49. break
  50. fi
  51. done
  52. echo "$res"
  53. }
  54. releaseresource()
  55. {
  56. resdir="$1"
  57. res="$2"
  58. mkdir "$resdir/$res" || exit 1
  59. }
  60. if [ x"$cmd" = x"reserve" ]; then
  61. zfs clone -p "$basezfs" "$labuserzfs/$user/$board"
  62. if [ ! -z "$sshkey" ]; then
  63. mkdir -p $(dirname "$jaildir/$sshkeydest")
  64. echo "$sshkey" > "$jaildir/$sshkeydest"
  65. chown -R 1001:1001 $(dirname "$jaildir/$sshkeydest")
  66. chmod -R 0700 $(dirname "$jaildir/$sshkeydest")
  67. fi
  68. ip=$(allocateresource "$ipresourcedir")
  69. devfsrule=$(allocateresource "$devfsruleresourcedir")
  70. epair="$(ifconfig epair create)"
  71. iface="${epair%a}b"
  72. sed \
  73. -e "s/@@BOARD@@/$board/g" \
  74. -e "s/@@IP@@/$ip/g" \
  75. -e "s/@@IFACE@@/$iface/g" \
  76. < "$templatercconf" > "$jaildir/etc/rc.conf"
  77. ifconfig "$ifacebridge" addm "$epair"
  78. ifconfig "$epair" up
  79. # devfs ruleset needs work
  80. # allow.mount \
  81. # allow.mount.devfs \
  82. # enforce_statfs=1 \
  83. # devfs_ruleset=10 \
  84. # copy devfs rulesets from devfsdefaultruleset to devfsrule
  85. devfspath="$jaildir"/dev
  86. devfs rule -s "$devfsrule" delset
  87. devfs rule -s "$devfsdefaultruleset" show | devfs rule -s "$devfsrule" add -
  88. mount -t devfs -o ruleset="$devfsrule" devfs "$devfspath"
  89. jailstart=$(jail -c \
  90. name="$board" \
  91. path="$jaildir" \
  92. vnet=new \
  93. vnet.interface="$iface" \
  94. exec.start="/bin/sh /etc/rc")
  95. # wait for ssh host keys and add them
  96. sshhostkeys="$(jexec "$board" cat /etc/ssh/ssh_host_*.pub)"
  97. # output additional attributes on reserve
  98. # NOTE: Make sure to update bitelab to pass these variables back.
  99. export ip
  100. export iface
  101. export jailstart
  102. export devfsrule
  103. export devfspath
  104. export sshhostkeys
  105. jq \
  106. --arg allargs "$*" \
  107. -n \
  108. '{
  109. allargs: $allargs,
  110. ip: $ENV.ip,
  111. iface: $ENV.iface,
  112. jailstart: $ENV.jailstart,
  113. devfsrule: $ENV.devfsrule,
  114. devfspath: $ENV.devfspath,
  115. sshhostkeys: $ENV.sshhostkeys
  116. }'
  117. elif [ x"$cmd" = x"release" ]; then
  118. jail -r "$board"
  119. umount "$devfspath"
  120. # epair doesn't immediate reappear, schedule it
  121. nohup sh -c 'for i in $(jot 5 1); do
  122. if ifconfig "$iface" destroy; then
  123. break;
  124. fi;
  125. sleep 1;
  126. done' > /dev/null 2>&1 &
  127. releaseresource "$ipresourcedir" "$ip"
  128. releaseresource "$devfsruleresourcedir" "$devfsrule"
  129. # for some reason not all jail processes are terminated,
  130. # need to retry
  131. sleep .5
  132. for x in $(jot 5 1); do
  133. if zfs destroy "$labuserzfs/$user/$board"; then
  134. break
  135. fi
  136. sleep 1
  137. done
  138. # no output on release
  139. fi